OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 part of ssa; |
| 6 |
| 7 class SsaFromIrBuilderTask extends CompilerTask { |
| 8 SsaFromIrBuilderTask(Compiler compiler) : super(compiler); |
| 9 |
| 10 HGraph build(CodegenWorkItem work) { |
| 11 return measure(() { |
| 12 Element element = work.element.implementation; |
| 13 return compiler.withCurrentElement(element, () { |
| 14 HInstruction.idCounter = 0; |
| 15 SsaFromIrBuilder builder = new SsaFromIrBuilder(compiler, element); |
| 16 |
| 17 HGraph graph; |
| 18 ElementKind kind = element.kind; |
| 19 if (kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 20 throw "Build HGraph for constructor from IR"; |
| 21 // graph = compileConstructor(builder, work); |
| 22 } else if (kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY || |
| 23 kind == ElementKind.FUNCTION || |
| 24 kind == ElementKind.GETTER || |
| 25 kind == ElementKind.SETTER) { |
| 26 graph = builder.buildMethod(); |
| 27 } else if (kind == ElementKind.FIELD) { |
| 28 throw "Build HGraph for field from IR"; |
| 29 // assert(!element.isInstanceMember()); |
| 30 // graph = builder.buildLazyInitializer(element); |
| 31 } else { |
| 32 compiler.internalErrorOnElement(element, |
| 33 'unexpected element kind $kind'); |
| 34 } |
| 35 assert(graph.isValid()); |
| 36 // TODO(lry): for default arguments, register constants in backend. |
| 37 // TODO(lry): tracing (factor out code in SsaBuilderTask). |
| 38 return graph; |
| 39 }); |
| 40 }); |
| 41 } |
| 42 } |
| 43 |
| 44 /** |
| 45 * This builder generates SSA nodes for elements that have an IR representation. |
| 46 * It mixes in [SsaGraphBuilderMixin] to share functionality with the |
| 47 * [SsaBuilder] that creates SSA nodes from trees. |
| 48 */ |
| 49 class SsaFromIrBuilder |
| 50 extends IrNodesVisitor<HInstruction> with SsaGraphBuilderMixin { |
| 51 final Compiler compiler; |
| 52 |
| 53 final Element sourceElement; |
| 54 |
| 55 SsaFromIrBuilder(this.compiler, this.sourceElement); |
| 56 |
| 57 /** |
| 58 * Maps IR nodes ot the generated [HInstruction]. Because the IR is itself |
| 59 * in an SSA form, the arguments of an [IrNode] have already been visited |
| 60 * prior to the node. This map is used to obtain the corresponding generated |
| 61 * SSA node. |
| 62 */ |
| 63 final Map<IrNode, HInstruction> generated = new Map<IrNode, HInstruction>(); |
| 64 |
| 65 HInstruction recordGenerated(IrNode irNode, HInstruction ssaNode) { |
| 66 return generated[irNode] = ssaNode; |
| 67 } |
| 68 |
| 69 HInstruction attachPosition(HInstruction target, IrNode node) { |
| 70 target.sourcePosition = sourceFileLocation(node); |
| 71 return target; |
| 72 } |
| 73 |
| 74 SourceFileLocation sourceFileLocation(IrNode node) { |
| 75 Element element = sourceElement; |
| 76 // TODO(johnniwinther): remove the 'element.patch' hack. |
| 77 if (element is FunctionElement) { |
| 78 FunctionElement functionElement = element; |
| 79 if (functionElement.patch != null) element = functionElement.patch; |
| 80 } |
| 81 Script script = element.getCompilationUnit().script; |
| 82 SourceFile sourceFile = script.file; |
| 83 SourceFileLocation location = |
| 84 new OffsetSourceFileLocation(sourceFile, node.offset, node.sourceName); |
| 85 if (!location.isValid()) { |
| 86 throw MessageKind.INVALID_SOURCE_FILE_LOCATION.message( |
| 87 {'offset': node.offset, |
| 88 'fileName': sourceFile.filename, |
| 89 'length': sourceFile.length}); |
| 90 } |
| 91 return location; |
| 92 } |
| 93 |
| 94 HGraph buildMethod() { |
| 95 graph.calledInLoop = compiler.world.isCalledInLoop(sourceElement); |
| 96 |
| 97 open(graph.entry); |
| 98 HBasicBlock block = graph.addNewBlock(); |
| 99 close(new HGoto()).addSuccessor(block); |
| 100 open(block); |
| 101 |
| 102 IrFunction function = compiler.irBuilder.getIr(sourceElement); |
| 103 visitAll(function.statements); |
| 104 if (!isAborted()) closeAndGotoExit(new HGoto()); |
| 105 graph.finalize(); |
| 106 return graph; |
| 107 } |
| 108 |
| 109 HInstruction visitIrConstant(IrConstant node) { |
| 110 return recordGenerated(node, graph.addConstant(node.value, compiler)); |
| 111 } |
| 112 |
| 113 HInstruction visitIrReturn(IrReturn node) { |
| 114 assert(isReachable); |
| 115 HInstruction value = generated[node.value]; |
| 116 // TODO(lry): add code for dynamic type check. |
| 117 // value = potentiallyCheckType(value, returnType); |
| 118 closeAndGotoExit(attachPosition(new HReturn(value), node)); |
| 119 } |
| 120 |
| 121 HInstruction visitNode(IrNode node) { |
| 122 abort(node); |
| 123 } |
| 124 |
| 125 void abort(IrNode node) { |
| 126 throw 'Cannot build SSA from IR for $node'; |
| 127 } |
| 128 } |
OLD | NEW |