Index: sdk/lib/_internal/compiler/implementation/ir/inode_builder.dart |
diff --git a/sdk/lib/_internal/compiler/implementation/ir/inode_builder.dart b/sdk/lib/_internal/compiler/implementation/ir/inode_builder.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cd8c188ab968519ce518337bdc214bc6a90c0535 |
--- /dev/null |
+++ b/sdk/lib/_internal/compiler/implementation/ir/inode_builder.dart |
@@ -0,0 +1,240 @@ |
+part of ir; |
kasperl
2013/11/06 08:30:04
Add copyright header.
I'd make the IrBuilder live
lukas
2013/11/06 11:36:08
Done.
|
+ |
+class IrBuilderTask extends CompilerTask { |
+ final Map<Element, INode> iNodes = new Map<Element, INode>(); |
+ |
+ IrBuilderTask(Compiler compiler) : super(compiler); |
+ |
+ String get name => 'IR builder'; |
+ |
+ void buildNodes() { |
+ Map<Element, TreeElements> resolved = |
+ compiler.enqueuer.resolution.resolvedElements; |
+ resolved.forEach((Element element, TreeElements elementsMapping) { |
+ element = element.implementation; |
+ |
+ // @lry debug |
+ if (element.name == 'noInline') |
+ print(0); |
+ |
+ SourceFile sourceFile = elementSourceFile(element); |
+ INodeBuilderVisitor visitor = |
+ new INodeBuilderVisitor(elementsMapping, compiler, sourceFile); |
+ INode iNode; |
+ ElementKind kind = element.kind; |
+ if (kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
+// iNode = compileConstructor(builder, work); |
+ } else if (kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY || |
+ kind == ElementKind.FUNCTION || |
+ kind == ElementKind.GETTER || |
+ kind == ElementKind.SETTER) { |
+ iNode = visitor.buildMethod(element); |
+ } else if (kind == ElementKind.FIELD) { |
+ // @lry the below assertion holds in the ssa-builder, but not here. |
+ // are instance fields never enqueued in the backend? |
+ // compare to inferrer, which is not worklist-based but runs over all |
+ // resolved elements |
+// assert(!element.isInstanceMember()); |
+// iNode = builder.buildLazyInitializer(element); |
+ } else { |
+ compiler.internalErrorOnElement(element, |
+ 'unexpected element kind $kind'); |
+ } |
+ |
+ if (iNode != null) { |
+ iNodes[element] = iNode; |
+ } |
+ |
+ // @lry debug |
+ if (element.name == 'noInline') { |
+ (element as dynamic).beginToken.next = null; |
+ (element as dynamic).cachedNode = null; |
+ print(iNode); |
+ } |
+ |
+ }); |
+ } |
+ |
+ SourceFile elementSourceFile(Element element) { |
+ if (element is FunctionElement) { |
+ FunctionElement functionElement = element; |
+ if (functionElement.patch != null) element = functionElement.patch; |
+ } |
+ return element.getCompilationUnit().script.file; |
+ } |
+} |
+ |
+ |
+class INodeBuilderVisitor extends ResolvedVisitor<INode> { |
+ SourceFile sourceFile; |
+ |
+ Context context; |
+ |
+ INodeBuilderVisitor(TreeElements elements, Compiler compiler, |
+ this.sourceFile) : super(elements, compiler) { |
+ context = new Context(this); |
+ } |
+ |
+ INode buildMethod(FunctionElement functionElement) { |
+ assert(invariant(functionElement, functionElement.isImplementation)); |
+ FunctionExpression function = functionElement.parseNode(compiler); |
+ assert(function != null); |
+ assert(!function.modifiers.isExternal()); |
+ assert(elements[function] != null); |
+ return buildNode(function); |
+ } |
+ |
+ INode buildNode(Node node) { |
+ try { |
+ return node.accept(this); |
+ } catch(e) { |
+ if (e == ABORT_INODE_BUILDER) return null; |
+ throw e; |
+ } |
+ } |
+ |
+ ConstantSystem get constantSystem => compiler.backend.constantSystem; |
+ |
+ Position nodePos(Node node) { |
+ return new Position(sourceFile, node.getBeginToken().charOffset); |
+ } |
+ |
+ INode visitFunctionExpression(FunctionExpression node) { |
+ IFunction result = new IFunction(nodePos(node), const<INode>[]); |
ngeoffray
2013/11/05 12:58:35
Should we create that at the end of the visit, onc
lukas
2013/11/05 17:10:50
It's done here for now because the "IrReturn" poin
|
+ context.openFunction(result); |
+ INode body = node.body.accept(this); |
+ if (context.statements.isNotEmpty) { |
+ result.statements = |
+ <INode>[]..addAll(context.constants)..addAll(context.statements); |
+ } |
+ context.closeFunction(); |
+ if (!context.isTopLevel()) { |
+ context.enterStatement(result); |
+ } |
+ return result; |
+ } |
+ |
+ INode visitBlock(Block node) { |
+ for (Node n in node.statements.nodes) { |
+ n.accept(this); |
+ } |
+ return new NoValue(); |
+ } |
+ |
+ INode visitReturn(Return node) { |
+ INode value = node.expression.accept(this); |
+ return context.enterStatement( |
+ new IReturn(nodePos(node), context.returnFunction, value)); |
+ } |
+ |
+ IConstant visitLiteralBool(LiteralBool node) => |
+ context.enterConstant(constantSystem.createBool(node.value), node); |
+ |
+ IConstant visitLiteralDouble(LiteralDouble node) => |
+ context.enterConstant(constantSystem.createDouble(node.value), node); |
+ |
+ IConstant visitLiteralInt(LiteralInt node) => |
+ context.enterConstant(constantSystem.createInt(node.value), node); |
+ |
+ IConstant visitLiteralString(LiteralString node) => |
+ context.enterConstant( |
+ constantSystem.createString(node.dartString, node), node); |
+ |
+ IConstant visitLiteralNull(LiteralNull node) => |
+ context.enterConstant(constantSystem.createNull(), node); |
+ |
+// INode visitLiteralList(LiteralList node) => visitExpression(node); |
+// INode visitLiteralMap(LiteralMap node) => visitExpression(node); |
+// INode visitLiteralMapEntry(LiteralMapEntry node) => visitNode(node); |
+// INode visitLiteralSymbol(LiteralSymbol node) => visitExpression(node); |
+ |
+ |
+ INode visitAssert(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitClosureSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitDynamicSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitGetterSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitOperatorSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitStaticSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitSuperSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ INode visitTypeReferenceSend(Send node) { |
+ return giveup(); |
+ } |
+ |
+ static final String ABORT_INODE_BUILDER = "INode builder aborted"; |
+ |
+ INode giveup() => throw ABORT_INODE_BUILDER; |
+ |
+ void internalError(String reason, {Node node}) { |
+ giveup(); |
+ } |
+} |
+ |
+class Context { |
+ INodeBuilderVisitor visitor; |
+ Context(this.visitor); |
+ |
+ List<IFunction> returnFunctionList = <IFunction>[]; |
+ IFunction get returnFunction => returnFunctionList.last; |
ngeoffray
2013/11/05 12:58:35
Are you anticipating inlining?
lukas
2013/11/05 17:10:50
Yes - again, depends on the IR we use.
|
+ |
+ List<List<INode>> statementsList = <List<INode>>[]; |
+ List<INode> get statements => statementsList.last; |
+ |
+ List<List<IConstant>> constantsList = <List<IConstant>>[]; |
ngeoffray
2013/11/05 12:58:35
I think we'll just have one giant list/set of cons
lukas
2013/11/05 17:10:50
We have one list per function here. The reason it'
|
+ List<IConstant> get constants => constantsList.last; |
+ |
+ IConstant enterConstant(Constant value, Node node) { |
+ return constants.firstWhere((c) => c.value == value, orElse: () { |
+ IConstant c = new IConstant(visitor.nodePos(node), value); |
+ constants.add(c); |
+ return c; |
+ }); |
+ } |
+ |
+ INode enterStatement(INode statement) { |
+ statements.add(statement); |
+ return statement; |
+ } |
+ |
+ void openFunction(IFunction function) { |
+ returnFunctionList.add(function); |
+ statementsList.add(<INode>[]); |
+ constantsList.add(<IConstant>[]); |
+ } |
+ |
+ void closeFunction() { |
+ returnFunctionList.removeLast(); |
+ statementsList.removeLast(); |
+ constantsList.removeLast(); |
+ } |
+ |
+ void openContinuation() { |
+ statementsList.add(<INode>[]); |
+ } |
+ |
+ void closeContinuation() { |
+ statementsList.removeLast(); |
+ } |
+ |
+ isTopLevel() => returnFunctionList.isEmpty; |
+} |