summaryrefslogtreecommitdiff
path: root/deps/v8/tools/turbolizer/src/graphmultiview.ts
blob: 8942526313c219570c16603d24ac1987617e6343 (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
181
182
183
184
185
186
// Copyright 2018 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 { storageGetItem, storageSetItem } from "./common/util";
import { GraphView } from "./views/graph-view";
import { ScheduleView } from "./views/schedule-view";
import { SequenceView } from "./views/sequence-view";
import { DynamicPhase, SourceResolver } from "./source-resolver";
import { SelectionBroker } from "./selection/selection-broker";
import { PhaseView, View } from "./views/view";
import { GraphPhase } from "./phases/graph-phase/graph-phase";
import { PhaseType } from "./phases/phase";
import { TurboshaftGraphView } from "./views/turboshaft-graph-view";
import { SelectionStorage } from "./selection/selection-storage";
import { TurboshaftGraphPhase } from "./phases/turboshaft-graph-phase/turboshaft-graph-phase";

const toolboxHTML = `
<div class="graph-toolbox">
  <select id="phase-select">
    <option disabled selected>(please open a file)</option>
  </select>
  <input id="search-input" type="text" title="search nodes for regex" alt="search node for regex" class="search-input"
    placeholder="find with regexp&hellip;">
  <label><input id="search-only-visible" type="checkbox" name="instruction-address" alt="Apply search to visible nodes only">only visible</label>
</div>`;

export class GraphMultiView extends View {
  sourceResolver: SourceResolver;
  selectionBroker: SelectionBroker;
  graph: GraphView;
  turboshaftGraph: TurboshaftGraphView;
  schedule: ScheduleView;
  sequence: SequenceView;
  selectMenu: HTMLSelectElement;
  currentPhaseView: PhaseView;

  constructor(id: string, selectionBroker: SelectionBroker, sourceResolver: SourceResolver) {
    super(id);
    const view = this;
    view.sourceResolver = sourceResolver;
    view.selectionBroker = selectionBroker;
    const toolbox = document.createElement("div");
    toolbox.className = "toolbox-anchor";
    toolbox.innerHTML = toolboxHTML;
    view.divNode.appendChild(toolbox);
    const searchInput = toolbox.querySelector("#search-input") as HTMLInputElement;
    const onlyVisibleCheckbox = toolbox.querySelector("#search-only-visible") as HTMLInputElement;
    searchInput.addEventListener("keyup", (e: KeyboardEvent) => {
      view.currentPhaseView?.searchInputAction(searchInput, e, onlyVisibleCheckbox.checked);
    });
    view.divNode.addEventListener("keyup", (e: KeyboardEvent) => {
      if (e.keyCode == 191) { // keyCode == '/'
        searchInput.focus();
      } else if (e.keyCode == 78) { // keyCode == 'n'
        view.displayNextGraphPhase();
      } else if (e.keyCode == 66) { // keyCode == 'b'
        view.displayPreviousGraphPhase();
      }
    });
    searchInput.setAttribute("value", storageGetItem("lastSearch", "", false));
    this.graph = new GraphView(this.divNode, selectionBroker, view.displayPhaseByName.bind(this),
      toolbox.querySelector(".graph-toolbox"));
    this.turboshaftGraph = new TurboshaftGraphView(this.divNode, selectionBroker,
      view.displayPhaseByName.bind(this), toolbox.querySelector(".graph-toolbox"));
    this.schedule = new ScheduleView(this.divNode, selectionBroker);
    this.sequence = new SequenceView(this.divNode, selectionBroker);
    this.selectMenu = toolbox.querySelector("#phase-select") as HTMLSelectElement;
  }

  public createViewElement(): HTMLDivElement {
    const pane = document.createElement("div");
    pane.setAttribute("id", C.MULTIVIEW_ID);
    pane.setAttribute("tabindex", "1");
    pane.className = "viewpane";
    return pane;
  }

  public hide(): void {
    this.container.className = "";
    this.hideCurrentPhase();
    super.hide();
  }

  public show(): void {
    // Insert before is used so that the display is inserted before the
    // resizer for the RangeView.
    this.container.insertBefore(this.divNode, this.container.firstChild);
    this.initializeSelect();
    const lastPhaseIndex = storageGetItem("lastSelectedPhase");
    const initialPhaseIndex = this.sourceResolver.repairPhaseId(lastPhaseIndex);
    this.selectMenu.selectedIndex = initialPhaseIndex;
    this.displayPhase(this.sourceResolver.getDynamicPhase(initialPhaseIndex));
  }

  public displayPhaseByName(phaseName: string, selection?: SelectionStorage): void {
    this.currentPhaseView.hide();
    const phaseId = this.sourceResolver.getPhaseIdByName(phaseName);
    this.selectMenu.selectedIndex = phaseId;
    this.displayPhase(this.sourceResolver.getDynamicPhase(phaseId), selection);
  }

  public onresize(): void {
    this.currentPhaseView?.onresize();
  }

  private displayPhase(phase: DynamicPhase, selection?: SelectionStorage): void {
    this.sourceResolver.positions = phase.positions;
    this.sourceResolver.instructionsPhase = phase.instructionsPhase;
    if (phase.type == PhaseType.Graph) {
      this.displayPhaseView(this.graph, phase, selection);
    } else if (phase.type == PhaseType.TurboshaftGraph) {
      this.displayPhaseView(this.turboshaftGraph, phase, selection);
    } else if (phase.type == PhaseType.Schedule) {
      this.displayPhaseView(this.schedule, phase, selection);
    } else if (phase.type == PhaseType.Sequence) {
      this.displayPhaseView(this.sequence, phase, selection);
    }
  }

  private displayPhaseView(view: PhaseView, data: DynamicPhase, selection?: SelectionStorage):
    void {
    const rememberedSelection = selection ? selection : this.hideCurrentPhase();
    view.initializeContent(data, rememberedSelection);
    this.currentPhaseView = view;
  }

  private displayNextGraphPhase(): void {
    let nextPhaseIndex = this.selectMenu.selectedIndex + 1;
    while (nextPhaseIndex < this.sourceResolver.phases.length) {
      const nextPhase = this.sourceResolver.getDynamicPhase(nextPhaseIndex);
      if (nextPhase && nextPhase.isGraph()) {
        this.selectMenu.selectedIndex = nextPhaseIndex;
        storageSetItem("lastSelectedPhase", nextPhaseIndex);
        this.displayPhase(nextPhase);
        break;
      }
      nextPhaseIndex += 1;
    }
  }

  private displayPreviousGraphPhase(): void {
    let previousPhaseIndex = this.selectMenu.selectedIndex - 1;
    while (previousPhaseIndex >= 0) {
      const previousPhase = this.sourceResolver.getDynamicPhase(previousPhaseIndex);
      if (previousPhase && previousPhase.isGraph()) {
        this.selectMenu.selectedIndex = previousPhaseIndex;
        storageSetItem("lastSelectedPhase", previousPhaseIndex);
        this.displayPhase(previousPhase);
        break;
      }
      previousPhaseIndex -= 1;
    }
  }

  private initializeSelect(): void {
    const view = this;
    view.selectMenu.innerHTML = "";
    for (const phase of view.sourceResolver.phases) {
      const optionElement = document.createElement("option");
      let maxNodeId = "";
      if ((phase instanceof GraphPhase || phase instanceof TurboshaftGraphPhase)
        && phase.highestNodeId != 0) {
        maxNodeId = ` ${phase.highestNodeId}`;
      }
      optionElement.text = `${phase.name}${maxNodeId}`;
      view.selectMenu.add(optionElement);
    }
    this.selectMenu.onchange = function (this: HTMLSelectElement) {
      const phaseIndex = this.selectedIndex;
      storageSetItem("lastSelectedPhase", phaseIndex);
      view.displayPhase(view.sourceResolver.getDynamicPhase(phaseIndex));
    };
  }

  private hideCurrentPhase(): SelectionStorage {
    let rememberedSelection = null;
    if (this.currentPhaseView != null) {
      rememberedSelection = this.currentPhaseView.detachSelection();
      this.currentPhaseView.hide();
      this.currentPhaseView = null;
    }
    return rememberedSelection;
  }
}