node.ts 5.29 KB
Newer Older
1 2 3 4
// Copyright 2014 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.

5
import { MINIMUM_EDGE_SEPARATION, Edge } from "../src/edge";
6 7 8
import { NodeLabel } from "./node-label";
import { MAX_RANK_SENTINEL } from "./constants";
import { alignUp, measureText } from "./util";
9 10 11 12

export const DEFAULT_NODE_BUBBLE_RADIUS = 12;
export const NODE_INPUT_WIDTH = 50;
export const MINIMUM_NODE_OUTPUT_APPROACH = 15;
13
const MINIMUM_NODE_INPUT_APPROACH = 15 + 2 * DEFAULT_NODE_BUBBLE_RADIUS;
14

15
export class GNode {
16 17 18
  id: number;
  nodeLabel: NodeLabel;
  displayLabel: string;
19 20
  inputs: Array<Edge>;
  outputs: Array<Edge>;
21
  visible: boolean;
22 23 24
  x: number;
  y: number;
  rank: number;
25
  outputApproach: number;
26
  cfg: boolean;
27 28
  labelbbox: { width: number, height: number };
  width: number;
29
  normalheight: number;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  visitOrderWithinRank: number;

  constructor(nodeLabel: NodeLabel) {
    this.id = nodeLabel.id;
    this.nodeLabel = nodeLabel;
    this.displayLabel = nodeLabel.getDisplayLabel();
    this.inputs = [];
    this.outputs = [];
    this.visible = false;
    this.x = 0;
    this.y = 0;
    this.rank = MAX_RANK_SENTINEL;
    this.outputApproach = MINIMUM_NODE_OUTPUT_APPROACH;
    // Every control node is a CFG node.
    this.cfg = nodeLabel.control;
    this.labelbbox = measureText(this.displayLabel);
    const typebbox = measureText(this.getDisplayType());
    const innerwidth = Math.max(this.labelbbox.width, typebbox.width);
    this.width = alignUp(innerwidth + NODE_INPUT_WIDTH * 2,
      NODE_INPUT_WIDTH);
    const innerheight = Math.max(this.labelbbox.height, typebbox.height);
    this.normalheight = innerheight + 20;
    this.visitOrderWithinRank = 0;
  }
54 55

  isControl() {
56
    return this.nodeLabel.control;
57 58
  }
  isInput() {
59
    return this.nodeLabel.opcode == 'Parameter' || this.nodeLabel.opcode.endsWith('Constant');
60 61
  }
  isLive() {
62
    return this.nodeLabel.live !== false;
63 64
  }
  isJavaScript() {
65
    return this.nodeLabel.opcode.startsWith('JS');
66 67
  }
  isSimplified() {
68
    if (this.isJavaScript()) return false;
69 70 71 72 73 74 75 76 77 78 79 80 81
    const opcode = this.nodeLabel.opcode;
    return opcode.endsWith('Phi') ||
      opcode.startsWith('Boolean') ||
      opcode.startsWith('Number') ||
      opcode.startsWith('String') ||
      opcode.startsWith('Change') ||
      opcode.startsWith('Object') ||
      opcode.startsWith('Reference') ||
      opcode.startsWith('Any') ||
      opcode.endsWith('ToNumber') ||
      (opcode == 'AnyToBoolean') ||
      (opcode.startsWith('Load') && opcode.length > 4) ||
      (opcode.startsWith('Store') && opcode.length > 5);
82 83
  }
  isMachine() {
84
    return !(this.isControl() || this.isInput() ||
85 86 87
      this.isJavaScript() || this.isSimplified());
  }
  getTotalNodeWidth() {
88
    const inputWidth = this.inputs.length * NODE_INPUT_WIDTH;
89
    return Math.max(inputWidth, this.width);
90 91
  }
  getTitle() {
92
    return this.nodeLabel.getTitle();
93 94
  }
  getDisplayLabel() {
95
    return this.nodeLabel.getDisplayLabel();
96 97
  }
  getType() {
98
    return this.nodeLabel.type;
99 100
  }
  getDisplayType() {
101
    let typeString = this.nodeLabel.type;
102 103 104
    if (typeString == undefined) return "";
    if (typeString.length > 24) {
      typeString = typeString.substr(0, 25) + "...";
105
    }
106
    return typeString;
107 108
  }
  deepestInputRank() {
109
    let deepestRank = 0;
110
    this.inputs.forEach(function (e) {
111 112 113 114 115 116 117
      if (e.isVisible() && !e.isBackEdge()) {
        if (e.source.rank > deepestRank) {
          deepestRank = e.source.rank;
        }
      }
    });
    return deepestRank;
118 119
  }
  areAnyOutputsVisible() {
120
    let visibleCount = 0;
121
    this.outputs.forEach(function (e) { if (e.isVisible())++visibleCount; });
122 123 124
    if (this.outputs.length == visibleCount) return 2;
    if (visibleCount != 0) return 1;
    return 0;
125 126
  }
  setOutputVisibility(v) {
127
    let result = false;
128
    this.outputs.forEach(function (e) {
129 130 131 132 133 134 135 136 137
      e.visible = v;
      if (v) {
        if (!e.target.visible) {
          e.target.visible = true;
          result = true;
        }
      }
    });
    return result;
138 139
  }
  setInputVisibility(i, v) {
140
    const edge = this.inputs[i];
141 142 143 144 145 146 147 148
    edge.visible = v;
    if (v) {
      if (!edge.source.visible) {
        edge.source.visible = true;
        return true;
      }
    }
    return false;
149 150
  }
  getInputApproach(index) {
151
    return this.y - MINIMUM_NODE_INPUT_APPROACH -
152
      (index % 4) * MINIMUM_EDGE_SEPARATION - DEFAULT_NODE_BUBBLE_RADIUS;
153
  }
154
  getNodeHeight(showTypes: boolean): number {
155 156 157 158 159 160
    if (showTypes) {
      return this.normalheight + this.labelbbox.height;
    } else {
      return this.normalheight;
    }
  }
161
  getOutputApproach(showTypes: boolean) {
162
    return this.y + this.outputApproach + this.getNodeHeight(showTypes) +
163
      + DEFAULT_NODE_BUBBLE_RADIUS;
164 165
  }
  getInputX(index) {
166
    const result = this.getTotalNodeWidth() - (NODE_INPUT_WIDTH / 2) +
167
      (index - this.inputs.length + 1) * NODE_INPUT_WIDTH;
168
    return result;
169 170
  }
  getOutputX() {
171
    return this.getTotalNodeWidth() - (NODE_INPUT_WIDTH / 2);
172 173
  }
  hasBackEdges() {
174 175 176
    return (this.nodeLabel.opcode == "Loop") ||
      ((this.nodeLabel.opcode == "Phi" || this.nodeLabel.opcode == "EffectPhi" || this.nodeLabel.opcode == "InductionVariablePhi") &&
        this.inputs[this.inputs.length - 1].source.nodeLabel.opcode == "Loop");
177
  }
178
}
179

180
export const nodeToStr = (n: GNode) => "N" + n.id;