summaryrefslogtreecommitdiff
path: root/deps/v8/tools/turbolizer/src/views/schedule-view.ts
blob: a3dfb6f84a6ee70b7c89a584deae22a9c3a7799e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import * as C from "./../common/constants";
import { storageSetItem } from "../common/util";
import { TextView } from "./text-view";
import { SelectionStorage } from "../selection/selection-storage";
import { SelectionBroker } from "../selection/selection-broker";
import { ScheduleBlock, ScheduleNode, SchedulePhase } from "../phases/schedule-phase";

export class ScheduleView extends TextView {
  schedule: SchedulePhase;

  constructor(parent: HTMLElement, broker: SelectionBroker) {
    super(parent, broker);
    this.sourceResolver = broker.sourceResolver;
  }

  public createViewElement(): HTMLDivElement {
    const pane = document.createElement("div");
    pane.setAttribute("id", C.SCHEDULE_PANE_ID);
    pane.classList.add("scrollable");
    pane.setAttribute("tabindex", "0");
    return pane;
  }

  public initializeContent(schedule: SchedulePhase, rememberedSelection: SelectionStorage): void {
    this.divNode.innerHTML = "";
    this.schedule = schedule;
    this.addBlocks(schedule.data.blocks);
    this.show();
    if (rememberedSelection) {
      const adaptedSelection = this.adaptSelection(rememberedSelection);
      this.attachSelection(adaptedSelection);
    }
  }

  public detachSelection(): SelectionStorage {
    return new SelectionStorage(this.nodeSelection.detachSelection(),
      this.blockSelection.detachSelection());
  }

  public adaptSelection(selection: SelectionStorage): SelectionStorage {
    for (const key of selection.nodes.keys()) selection.adaptedNodes.add(key);
    for (const key of selection.blocks.keys()) selection.adaptedBocks.add(key);
    return selection;
  }

  public searchInputAction(searchBar: HTMLInputElement, e: KeyboardEvent, onlyVisible: boolean):
    void {
    e.stopPropagation();
    this.nodeSelectionHandler.clear();
    const query = searchBar.value;
    if (query.length == 0) return;
    const select = new Array<number>();
    storageSetItem("lastSearch", query);
    const reg = new RegExp(query);
    for (const node of this.schedule.data.nodes) {
      if (node === undefined) continue;
      if (reg.exec(node.toString()) !== null) {
        select.push(node.id);
      }
    }
    this.nodeSelectionHandler.select(select, true);
  }

  private addBlocks(blocks: Array<ScheduleBlock>) {
    for (const block of blocks) {
      const blockEl = this.createElementForBlock(block);
      this.divNode.appendChild(blockEl);
    }
  }

  private attachSelection(adaptedSelection: SelectionStorage): void {
    if (!(adaptedSelection instanceof SelectionStorage)) return;
    this.nodeSelectionHandler.clear();
    this.blockSelectionHandler.clear();
    this.nodeSelectionHandler.select(adaptedSelection.adaptedNodes, true);
    this.blockSelectionHandler.select(adaptedSelection.adaptedBocks, true);
  }

  private createElementForBlock(block: ScheduleBlock): HTMLElement {
    const scheduleBlock = this.createElement("div", "schedule-block");
    scheduleBlock.classList.toggle("deferred", block.deferred);

    const [start, end] = this.sourceResolver.instructionsPhase
      .getInstructionRangeForBlock(block.id);
    const instrMarker = this.createElement("div", "instr-marker com", "&#8857;");
    instrMarker.setAttribute("title", `Instructions range for this block is [${start}, ${end})`);
    instrMarker.onclick = this.mkBlockLinkHandler(block.id);
    scheduleBlock.appendChild(instrMarker);

    const blockId = this.createElement("div", "block-id com clickable", String(block.id));
    blockId.onclick = this.mkBlockLinkHandler(block.id);
    scheduleBlock.appendChild(blockId);
    const blockPred = this.createElement("div", "predecessor-list block-list comma-sep-list");
    for (const pred of block.predecessors) {
      const predEl = this.createElement("div", "block-id com clickable", String(pred));
      predEl.onclick = this.mkBlockLinkHandler(pred);
      blockPred.appendChild(predEl);
    }
    if (block.predecessors.length) scheduleBlock.appendChild(blockPred);
    const nodes = this.createElement("div", "nodes");
    for (const node of block.nodes) {
      nodes.appendChild(this.createElementForNode(node));
    }
    scheduleBlock.appendChild(nodes);

    const blockSucc = this.createElement("div", "successor-list block-list comma-sep-list");
    for (const succ of block.successors) {
      const succEl = this.createElement("div", "block-id com clickable", String(succ));
      succEl.onclick = this.mkBlockLinkHandler(succ);
      blockSucc.appendChild(succEl);
    }
    if (block.successors.length) scheduleBlock.appendChild(blockSucc);
    this.addHtmlElementForBlockId(block.id, scheduleBlock);
    return scheduleBlock;
  }

  private createElementForNode(node: ScheduleNode): HTMLElement {
    const nodeEl = this.createElement("div", "node");

    const [start, end] = this.sourceResolver.instructionsPhase.getInstruction(node.id);
    const [marker, tooltip] = this.sourceResolver.instructionsPhase
      .getInstructionMarker(start, end);
    const instrMarker = this.createElement("div", "instr-marker com", marker);
    instrMarker.setAttribute("title", tooltip);
    instrMarker.onclick = this.mkNodeLinkHandler(node.id);
    nodeEl.appendChild(instrMarker);

    const nodeIdEl = this.createElement("div", "node-id tag clickable", String(node.id));
    nodeIdEl.onclick = this.mkNodeLinkHandler(node.id);
    this.addHtmlElementForNodeId(node.id, nodeIdEl);
    nodeEl.appendChild(nodeIdEl);
    const nodeLabel = this.createElement("div", "node-label", node.label);
    nodeEl.appendChild(nodeLabel);
    if (node.inputs.length > 0) {
      const nodeParameters = this.createElement("div", "parameter-list comma-sep-list");
      for (const param of node.inputs) {
        const paramEl = this.createElement("div", "parameter tag clickable", String(param));
        nodeParameters.appendChild(paramEl);
        paramEl.onclick = this.mkNodeLinkHandler(param);
        this.addHtmlElementForNodeId(param, paramEl);
      }
      nodeEl.appendChild(nodeParameters);
    }

    return nodeEl;
  }

  private mkBlockLinkHandler(blockId: number): (e: MouseEvent) => void {
    const view = this;
    return function (e: MouseEvent) {
      e.stopPropagation();
      if (!e.shiftKey) {
        view.blockSelectionHandler.clear();
      }
      view.blockSelectionHandler.select([blockId], true);
    };
  }

  private mkNodeLinkHandler(nodeId: number): (e: MouseEvent) => void {
    const view = this;
    return function (e: MouseEvent) {
      e.stopPropagation();
      if (!e.shiftKey) {
        view.nodeSelectionHandler.clear();
      }
      view.nodeSelectionHandler.select([nodeId], true);
    };
  }

  private createElement(tag: string, cls: string, content?: string) {
    const el = document.createElement(tag);
    el.className = cls;
    if (content !== undefined) el.innerHTML = content;
    return el;
  }
}