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 class SsaFromIrBuilder | |
45 extends IrNodesVisitor<HInstruction> with SsaGraphBuilderMixin { | |
46 final Compiler compiler; | |
47 | |
48 final Element sourceElement; | |
49 | |
50 SsaFromIrBuilder(this.compiler, this.sourceElement); | |
51 | |
52 final Map<IrNode, HInstruction> generated = new Map<IrNode, HInstruction>(); | |
53 | |
54 HInstruction recordGenerated(IrNode irNode, HInstruction ssaNode) { | |
55 generated[irNode] = ssaNode; | |
ngeoffray
2013/11/21 15:02:05
return generated[irNode] = ssaNode;
lukas
2013/11/21 17:14:27
Done.
| |
56 return ssaNode; | |
57 } | |
58 | |
59 HInstruction attachPosition(HInstruction target, IrNode node) { | |
60 target.sourcePosition = sourceFileLocation(node); | |
61 return target; | |
62 } | |
63 | |
64 SourceFileLocation sourceFileLocation(IrNode node) { | |
65 Element element = sourceElement; | |
66 // TODO(johnniwinther): remove the 'element.patch' hack. | |
67 if (element is FunctionElement) { | |
68 FunctionElement functionElement = element; | |
69 if (functionElement.patch != null) element = functionElement.patch; | |
70 } | |
71 Script script = element.getCompilationUnit().script; | |
72 SourceFile sourceFile = script.file; | |
73 SourceFileLocation location = | |
74 new OffsetSourceFileLocation(sourceFile, node.offset, node.sourceName); | |
75 if (!location.isValid()) { | |
76 throw MessageKind.INVALID_SOURCE_FILE_LOCATION.message( | |
77 {'offset': node.offset, | |
78 'fileName': sourceFile.filename, | |
79 'length': sourceFile.length}); | |
80 } | |
81 return location; | |
82 } | |
83 | |
84 HGraph buildMethod() { | |
85 graph.calledInLoop = compiler.world.isCalledInLoop(sourceElement); | |
86 | |
87 open(graph.entry); | |
88 HBasicBlock block = graph.addNewBlock(); | |
89 close(new HGoto()).addSuccessor(block); | |
90 open(block); | |
91 | |
92 IrFunction function = sourceElement.irNode(compiler); | |
93 visitAll(function.statements); | |
94 if (!isAborted()) closeAndGotoExit(new HGoto()); | |
95 graph.finalize(); | |
96 return graph; | |
97 } | |
98 | |
99 HInstruction visitIrConstant(IrConstant node) { | |
100 return recordGenerated(node, graph.addConstant(node.value, compiler)); | |
101 } | |
102 | |
103 HInstruction visitIrReturn(IrReturn node) { | |
104 HInstruction value = generated[node.value]; | |
105 // TODO(lry): add code for dynamic type check. | |
106 // value = potentiallyCheckType(value, returnType); | |
107 closeAndGotoExit(attachPosition(new HReturn(value), node)); | |
108 } | |
109 | |
110 HInstruction visitNode(IrNode node) { | |
111 abort(node); | |
112 } | |
113 | |
114 void abort(IrNode node) { | |
115 throw 'Cannot build SSA from IR for $node'; | |
116 } | |
117 } | |
OLD | NEW |