summaryrefslogtreecommitdiff
path: root/deps/v8/tools/turbolizer/src/views/bytecode-source-view.ts
blob: 39d1b4fb6b63933ff42b8277f125c034dc4b69c3 (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
// Copyright 2022 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 { createElement } from "../common/util";
import { CodeMode, View } from "./view";
import { SelectionBroker } from "../selection/selection-broker";
import { BytecodeSource } from "../source";
import { SourceResolver } from "../source-resolver";
import { SelectionMap } from "../selection/selection-map";
import { ViewElements } from "../common/view-elements";
import { BytecodeOffsetSelectionHandler, ClearableHandler } from "../selection/selection-handler";
import { BytecodePosition } from "../position";

export class BytecodeSourceView extends View {
  broker: SelectionBroker;
  source: BytecodeSource;
  sourceResolver: SourceResolver;
  codeMode: CodeMode;
  bytecodeOffsetToHtmlElement: Map<number, HTMLElement>;
  bytecodeOffsetSelection: SelectionMap;
  bytecodeOffsetSelectionHandler: BytecodeOffsetSelectionHandler & ClearableHandler;

  constructor(parent: HTMLElement, broker: SelectionBroker,  sourceFunction: BytecodeSource,
              sourceResolver: SourceResolver, codeMode: CodeMode) {
    super(parent);
    this.broker = broker;
    this.source = sourceFunction;
    this.sourceResolver = sourceResolver;
    this.codeMode = codeMode;
    this.bytecodeOffsetToHtmlElement = new Map<number, HTMLElement>();
    this.bytecodeOffsetSelection = new SelectionMap((offset: number) => String(offset));
    this.bytecodeOffsetSelectionHandler = this.initializeBytecodeOffsetSelectionHandler();
    this.broker.addBytecodeOffsetHandler(this.bytecodeOffsetSelectionHandler);

    this.initializeCode();
  }

  protected createViewElement(): HTMLElement {
    return createElement("div", "bytecode-source-container");
  }

  private initializeCode(): void {
    const view = this;
    const source = this.source;
    const bytecodeContainer = this.divNode;
    bytecodeContainer.classList.add(view.getSourceClass());

    const bytecodeHeader = createElement("div", "code-header");
    bytecodeHeader.setAttribute("id", view.getBytecodeHeaderHtmlElementName());

    const codeFileFunction = createElement("div", "code-file-function", source.functionName);
    bytecodeHeader.appendChild(codeFileFunction);

    const codeMode = createElement("div", "code-mode", view.codeMode);
    bytecodeHeader.appendChild(codeMode);

    const clearElement = document.createElement("div");
    clearElement.style.clear = "both";
    bytecodeHeader.appendChild(clearElement);
    bytecodeContainer.appendChild(bytecodeHeader);

    const codePre = createElement("pre", "prettyprint linenums");
    codePre.setAttribute("id", view.getBytecodeHtmlElementName());
    bytecodeContainer.appendChild(codePre);

    bytecodeHeader.onclick = () => {
      codePre.style.display = codePre.style.display === "none" ? "block" : "none";
    };

    const sourceList = createElement("ol", "linenums");
    for (const bytecodeSource of view.source.data) {
      const currentLine = createElement("li", `L${bytecodeSource.offset}`);
      currentLine.setAttribute("id", `li${bytecodeSource.offset}`);
      view.insertLineContent(currentLine, bytecodeSource.disassembly);
      view.insertLineNumber(currentLine, bytecodeSource.offset);
      view.bytecodeOffsetToHtmlElement.set(bytecodeSource.offset, currentLine);
      sourceList.appendChild(currentLine);
    }
    codePre.appendChild(sourceList);

    if (!view.source.constantPool) return;

    const constantList = createElement("ol", "linenums constants");
    const constantListHeader = createElement("li", "");
    view.insertLineContent(constantListHeader,
      `Constant pool (size = ${view.source.constantPool.length})`);
    constantList.appendChild(constantListHeader);

    for (const [idx, constant] of view.source.constantPool.entries()) {
      const currentLine = createElement("li", `C${idx}`);
      view.insertLineContent(currentLine, `${idx}: ${constant}`);
      constantList.appendChild(currentLine);
    }
    codePre.appendChild(constantList);
  }

  private initializeBytecodeOffsetSelectionHandler(): BytecodeOffsetSelectionHandler
    & ClearableHandler {
    const view = this;
    const broker = this.broker;
    return {
      select: function (offsets: Array<number>, selected: boolean) {
        const bytecodePositions = new Array<BytecodePosition>();
        for (const offset of offsets) {
          view.source.inliningIds.forEach(inliningId =>
                                  bytecodePositions.push(new BytecodePosition(offset, inliningId)));
        }
        view.bytecodeOffsetSelection.select(offsets, selected);
        view.updateSelection();
        broker.broadcastBytecodePositionsSelect(this, bytecodePositions, selected);
      },
      clear: function () {
        view.bytecodeOffsetSelection.clear();
        view.updateSelection();
        broker.broadcastClear(this);
      },
      brokeredBytecodeOffsetSelect: function (positions: Array<BytecodePosition>,
                                              selected: boolean) {
        const offsets = new Array<number>();
        const firstSelect = view.bytecodeOffsetSelection.isEmpty();
        for (const position of positions) {
          if (view.source.inliningIds.includes(position.inliningId)) {
            offsets.push(position.bytecodePosition);
          }
        }
        view.bytecodeOffsetSelection.select(offsets, selected);
        view.updateSelection(firstSelect);
      },
      brokeredClear: function () {
        view.bytecodeOffsetSelection.clear();
        view.updateSelection();
      },
    };
  }

  private getBytecodeHeaderHtmlElementName(): string {
    return `source-pre-${this.source.sourceId}-header`;
  }

  private getBytecodeHtmlElementName(): string {
    return `source-pre-${this.source.sourceId}`;
  }

  private getSourceClass(): string {
    return this.codeMode == CodeMode.MainSource ? "main-source" : "inlined-source";
  }

  private updateSelection(scrollIntoView: boolean = false): void {
    const mkVisible = new ViewElements(this.divNode.parentNode as HTMLElement);
    for (const [offset, element] of this.bytecodeOffsetToHtmlElement.entries()) {
      const key = this.bytecodeOffsetSelection.stringKey(offset);
      const isSelected = this.bytecodeOffsetSelection.isKeySelected(key);
      mkVisible.consider(element, isSelected);
      element.classList.toggle("selected", isSelected);
    }
    mkVisible.apply(scrollIntoView);
  }

  private onSelectBytecodeOffset(offset: number, doClear: boolean) {
    if (doClear) {
      this.bytecodeOffsetSelectionHandler.clear();
    }
    this.bytecodeOffsetSelectionHandler.select([offset], undefined);
  }

  private insertLineContent(lineElement: HTMLElement, content: string): void {
    const lineContentElement = createElement("span", "", content);
    lineElement.appendChild(lineContentElement);
  }

  private insertLineNumber(lineElement: HTMLElement, lineNumber: number): void {
    const view = this;
    const lineNumberElement = createElement("div", "line-number", String(lineNumber));
    lineNumberElement.onclick = function (e: MouseEvent) {
      e.stopPropagation();
      view.onSelectBytecodeOffset(lineNumber, !e.shiftKey);
    };
    lineElement.insertBefore(lineNumberElement, lineElement.firstChild);
  }
}