11cb0ef41Sopenharmony_ci// Copyright 2015 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciimport { SourceResolver, sourcePositionValid } from "../src/source-resolver";
61cb0ef41Sopenharmony_ciimport { ClearableHandler, SelectionHandler, NodeSelectionHandler, BlockSelectionHandler, InstructionSelectionHandler, RegisterAllocationSelectionHandler } from "../src/selection-handler";
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciexport class SelectionBroker {
91cb0ef41Sopenharmony_ci  sourceResolver: SourceResolver;
101cb0ef41Sopenharmony_ci  allHandlers: Array<ClearableHandler>;
111cb0ef41Sopenharmony_ci  sourcePositionHandlers: Array<SelectionHandler>;
121cb0ef41Sopenharmony_ci  nodeHandlers: Array<NodeSelectionHandler>;
131cb0ef41Sopenharmony_ci  blockHandlers: Array<BlockSelectionHandler>;
141cb0ef41Sopenharmony_ci  instructionHandlers: Array<InstructionSelectionHandler>;
151cb0ef41Sopenharmony_ci  registerAllocationHandlers: Array<RegisterAllocationSelectionHandler>;
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  constructor(sourceResolver) {
181cb0ef41Sopenharmony_ci    this.allHandlers = [];
191cb0ef41Sopenharmony_ci    this.sourcePositionHandlers = [];
201cb0ef41Sopenharmony_ci    this.nodeHandlers = [];
211cb0ef41Sopenharmony_ci    this.blockHandlers = [];
221cb0ef41Sopenharmony_ci    this.instructionHandlers = [];
231cb0ef41Sopenharmony_ci    this.registerAllocationHandlers = [];
241cb0ef41Sopenharmony_ci    this.sourceResolver = sourceResolver;
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  addSourcePositionHandler(handler: SelectionHandler & ClearableHandler) {
281cb0ef41Sopenharmony_ci    this.allHandlers.push(handler);
291cb0ef41Sopenharmony_ci    this.sourcePositionHandlers.push(handler);
301cb0ef41Sopenharmony_ci  }
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  addNodeHandler(handler: NodeSelectionHandler & ClearableHandler) {
331cb0ef41Sopenharmony_ci    this.allHandlers.push(handler);
341cb0ef41Sopenharmony_ci    this.nodeHandlers.push(handler);
351cb0ef41Sopenharmony_ci  }
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci  addBlockHandler(handler: BlockSelectionHandler & ClearableHandler) {
381cb0ef41Sopenharmony_ci    this.allHandlers.push(handler);
391cb0ef41Sopenharmony_ci    this.blockHandlers.push(handler);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  addInstructionHandler(handler: InstructionSelectionHandler & ClearableHandler) {
431cb0ef41Sopenharmony_ci    this.allHandlers.push(handler);
441cb0ef41Sopenharmony_ci    this.instructionHandlers.push(handler);
451cb0ef41Sopenharmony_ci  }
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  addRegisterAllocatorHandler(handler: RegisterAllocationSelectionHandler & ClearableHandler) {
481cb0ef41Sopenharmony_ci    this.allHandlers.push(handler);
491cb0ef41Sopenharmony_ci    this.registerAllocationHandlers.push(handler);
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  broadcastInstructionSelect(from, instructionOffsets, selected) {
531cb0ef41Sopenharmony_ci    // Select the lines from the disassembly (right panel)
541cb0ef41Sopenharmony_ci    for (const b of this.instructionHandlers) {
551cb0ef41Sopenharmony_ci      if (b != from) b.brokeredInstructionSelect(instructionOffsets, selected);
561cb0ef41Sopenharmony_ci    }
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    // Select the lines from the source panel (left panel)
591cb0ef41Sopenharmony_ci    const pcOffsets = this.sourceResolver.instructionsToKeyPcOffsets(instructionOffsets);
601cb0ef41Sopenharmony_ci    for (const offset of pcOffsets) {
611cb0ef41Sopenharmony_ci      const nodes = this.sourceResolver.nodesForPCOffset(offset)[0];
621cb0ef41Sopenharmony_ci      const sourcePositions = this.sourceResolver.nodeIdsToSourcePositions(nodes);
631cb0ef41Sopenharmony_ci      for (const b of this.sourcePositionHandlers) {
641cb0ef41Sopenharmony_ci        if (b != from) b.brokeredSourcePositionSelect(sourcePositions, selected);
651cb0ef41Sopenharmony_ci      }
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    // The middle panel lines have already been selected so there's no need to reselect them.
691cb0ef41Sopenharmony_ci  }
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  broadcastSourcePositionSelect(from, sourcePositions, selected) {
721cb0ef41Sopenharmony_ci    sourcePositions = sourcePositions.filter(l => {
731cb0ef41Sopenharmony_ci      if (!sourcePositionValid(l)) {
741cb0ef41Sopenharmony_ci        console.log("Warning: invalid source position");
751cb0ef41Sopenharmony_ci        return false;
761cb0ef41Sopenharmony_ci      }
771cb0ef41Sopenharmony_ci      return true;
781cb0ef41Sopenharmony_ci    });
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    // Select the lines from the source panel (left panel)
811cb0ef41Sopenharmony_ci    for (const b of this.sourcePositionHandlers) {
821cb0ef41Sopenharmony_ci      if (b != from) b.brokeredSourcePositionSelect(sourcePositions, selected);
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci    // Select the nodes (middle panel)
861cb0ef41Sopenharmony_ci    const nodes = this.sourceResolver.sourcePositionsToNodeIds(sourcePositions);
871cb0ef41Sopenharmony_ci    for (const b of this.nodeHandlers) {
881cb0ef41Sopenharmony_ci      if (b != from) b.brokeredNodeSelect(nodes, selected);
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    for (const node of nodes) {
921cb0ef41Sopenharmony_ci      const instructionOffsets = this.sourceResolver.nodeIdToInstructionRange[node];
931cb0ef41Sopenharmony_ci      // Skip nodes which do not have an associated instruction range.
941cb0ef41Sopenharmony_ci      if (instructionOffsets == undefined) continue;
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci      // Select the lines from the disassembly (right panel)
971cb0ef41Sopenharmony_ci      for (const b of this.instructionHandlers) {
981cb0ef41Sopenharmony_ci        if (b != from) b.brokeredInstructionSelect(instructionOffsets, selected);
991cb0ef41Sopenharmony_ci      }
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci      // Select the lines from the middle panel for the register allocation phase.
1021cb0ef41Sopenharmony_ci      for (const b of this.registerAllocationHandlers) {
1031cb0ef41Sopenharmony_ci        if (b != from) b.brokeredRegisterAllocationSelect(instructionOffsets, selected);
1041cb0ef41Sopenharmony_ci      }
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  broadcastNodeSelect(from, nodes, selected) {
1091cb0ef41Sopenharmony_ci    // Select the nodes (middle panel)
1101cb0ef41Sopenharmony_ci    for (const b of this.nodeHandlers) {
1111cb0ef41Sopenharmony_ci      if (b != from) b.brokeredNodeSelect(nodes, selected);
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci    // Select the lines from the source panel (left panel)
1151cb0ef41Sopenharmony_ci    const sourcePositions = this.sourceResolver.nodeIdsToSourcePositions(nodes);
1161cb0ef41Sopenharmony_ci    for (const b of this.sourcePositionHandlers) {
1171cb0ef41Sopenharmony_ci      if (b != from) b.brokeredSourcePositionSelect(sourcePositions, selected);
1181cb0ef41Sopenharmony_ci    }
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci    for (const node of nodes) {
1211cb0ef41Sopenharmony_ci      const instructionOffsets = this.sourceResolver.nodeIdToInstructionRange[node];
1221cb0ef41Sopenharmony_ci      // Skip nodes which do not have an associated instruction range.
1231cb0ef41Sopenharmony_ci      if (instructionOffsets == undefined) continue;
1241cb0ef41Sopenharmony_ci      // Select the lines from the disassembly (right panel)
1251cb0ef41Sopenharmony_ci      for (const b of this.instructionHandlers) {
1261cb0ef41Sopenharmony_ci        if (b != from) b.brokeredInstructionSelect(instructionOffsets, selected);
1271cb0ef41Sopenharmony_ci      }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci      // Select the lines from the middle panel for the register allocation phase.
1301cb0ef41Sopenharmony_ci      for (const b of this.registerAllocationHandlers) {
1311cb0ef41Sopenharmony_ci        if (b != from) b.brokeredRegisterAllocationSelect(instructionOffsets, selected);
1321cb0ef41Sopenharmony_ci      }
1331cb0ef41Sopenharmony_ci    }
1341cb0ef41Sopenharmony_ci  }
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  broadcastBlockSelect(from, blocks, selected) {
1371cb0ef41Sopenharmony_ci    for (const b of this.blockHandlers) {
1381cb0ef41Sopenharmony_ci      if (b != from) b.brokeredBlockSelect(blocks, selected);
1391cb0ef41Sopenharmony_ci    }
1401cb0ef41Sopenharmony_ci  }
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  broadcastClear(from) {
1431cb0ef41Sopenharmony_ci    this.allHandlers.forEach(function (b) {
1441cb0ef41Sopenharmony_ci      if (b != from) b.brokeredClear();
1451cb0ef41Sopenharmony_ci    });
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci}
148