Chromium Code Reviews| Index: sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart |
| diff --git a/sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart b/sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5f694cd825dbe68f41223764ad0db1371c9ac158 |
| --- /dev/null |
| +++ b/sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart |
| @@ -0,0 +1,255 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library ir_builder; |
| + |
| +import 'ir_nodes.dart'; |
| +import '../elements/elements.dart'; |
| +import '../dart2jslib.dart'; |
| +import '../source_file.dart'; |
| +import '../tree/tree.dart'; |
| + |
| +class IrBuilderTask extends CompilerTask { |
| + final Map<Element, IrNode> irNodes = new Map<Element, IrNode>(); |
|
karlklose
2013/11/06 09:11:28
How about calling this field 'nodes'?
lukas
2013/11/06 11:36:08
Done.
|
| + |
| + 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); |
| + IrNodeBuilderVisitor visitor = |
| + new IrNodeBuilderVisitor(elementsMapping, compiler, sourceFile); |
| + IrNode irNode; |
| + ElementKind kind = element.kind; |
| + if (kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| +// irNode = compileConstructor(builder, work); |
| + } else if (kind == ElementKind.GENERATIVE_CONSTRUCTOR_BODY || |
| + kind == ElementKind.FUNCTION || |
| + kind == ElementKind.GETTER || |
| + kind == ElementKind.SETTER) { |
| + irNode = 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()); |
| +// irNode = builder.buildLazyInitializer(element); |
| + } else { |
| + compiler.internalErrorOnElement(element, |
| + 'unexpected element kind $kind'); |
| + } |
| + |
| + if (irNode != null) { |
| + irNodes[element] = irNode; |
| + } |
| + |
| + // @lry debug |
| + if (element.name == 'noInline') { |
| +// (element as dynamic).beginToken.next = null; |
| +// (element as dynamic).cachedNode = null; |
| + print(irNode); |
| + } |
| + |
| + }); |
| + } |
| + |
| + SourceFile elementSourceFile(Element element) { |
| + if (element is FunctionElement) { |
| + FunctionElement functionElement = element; |
| + if (functionElement.patch != null) element = functionElement.patch; |
| + } |
| + return element.getCompilationUnit().script.file; |
| + } |
| +} |
| + |
| + |
| +class IrNodeBuilderVisitor extends ResolvedVisitor<IrNode> { |
| + SourceFile sourceFile; |
| + |
| + Context context; |
| + |
| + IrNodeBuilderVisitor(TreeElements elements, Compiler compiler, |
| + this.sourceFile) : super(elements, compiler) { |
| + context = new Context(this); |
| + } |
| + |
| + IrNode 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); |
| + } |
| + |
| + IrNode buildNode(Node node) { |
| + try { |
| + return node.accept(this); |
| + } catch(e) { |
| + if (e == ABORT_IRNODE_BUILDER) return null; |
| + throw e; |
| + } |
| + } |
| + |
| + ConstantSystem get constantSystem => compiler.backend.constantSystem; |
| + |
| + int nodeOffset(Node node) { |
| + return node.getBeginToken().charOffset; |
| + } |
| + |
| + IrNode visitFunctionExpression(FunctionExpression node) { |
| + IrFunction result = new IrFunction(nodeOffset(node), const<IrNode>[]); |
|
karlklose
2013/11/06 09:11:28
Initialize with <IrNode>[] (growable list) and fil
lukas
2013/11/06 11:36:08
Done.
|
| + context.openFunction(result); |
| + IrNode body = node.body.accept(this); |
| + if (context.statements.isNotEmpty) { |
| + result.statements = |
| + <IrNode>[]..addAll(context.constants)..addAll(context.statements); |
| + } |
| + context.closeFunction(); |
| + if (!context.isTopLevel()) { |
| + context.enterStatement(result); |
| + } |
| + return result; |
| + } |
| + |
| + IrNode visitBlock(Block node) { |
| + for (Node n in node.statements.nodes) { |
| + n.accept(this); |
| + } |
| + return const IrNoValue(); |
| + } |
| + |
| + IrNode visitReturn(Return node) { |
| + IrNode value; |
| + if (node.expression == null) { |
| + value = context.enterConstant(constantSystem.createNull(), node); |
| + } else { |
| + value = node.expression.accept(this); |
| + } |
| + return context.enterStatement( |
| + new IrReturn(nodeOffset(node), context.returnFunction, value)); |
| + } |
| + |
| + IrConstant visitLiteralBool(LiteralBool node) => |
| + context.enterConstant(constantSystem.createBool(node.value), node); |
| + |
| + IrConstant visitLiteralDouble(LiteralDouble node) => |
| + context.enterConstant(constantSystem.createDouble(node.value), node); |
| + |
| + IrConstant visitLiteralInt(LiteralInt node) => |
| + context.enterConstant(constantSystem.createInt(node.value), node); |
| + |
| + IrConstant visitLiteralString(LiteralString node) => |
| + context.enterConstant( |
| + constantSystem.createString(node.dartString, node), node); |
| + |
| + IrConstant visitLiteralNull(LiteralNull node) => |
| + context.enterConstant(constantSystem.createNull(), node); |
| + |
| +// IrNode visitLiteralList(LiteralList node) => visitExpression(node); |
| +// IrNode visitLiteralMap(LiteralMap node) => visitExpression(node); |
| +// IrNode visitLiteralMapEntry(LiteralMapEntry node) => visitNode(node); |
| +// IrNode visitLiteralSymbol(LiteralSymbol node) => visitExpression(node); |
| + |
| + |
| + IrNode visitAssert(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitClosureSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitDynamicSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitGetterSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitOperatorSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitStaticSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitSuperSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + IrNode visitTypeReferenceSend(Send node) { |
| + return giveup(); |
| + } |
| + |
| + static final String ABORT_IRNODE_BUILDER = "IrNode builder aborted"; |
| + |
| + IrNode giveup() => throw ABORT_IRNODE_BUILDER; |
| + |
| + void internalError(String reason, {Node node}) { |
| + giveup(); |
| + } |
| +} |
| + |
| +class Context { |
| + IrNodeBuilderVisitor visitor; |
| + Context(this.visitor); |
| + |
| + List<IrFunction> returnFunctionList = <IrFunction>[]; |
| + IrFunction get returnFunction => returnFunctionList.last; |
| + |
| + List<List<IrNode>> statementsList = <List<IrNode>>[]; |
| + List<IrNode> get statements => statementsList.last; |
| + |
| + List<List<IrConstant>> constantsList = <List<IrConstant>>[]; |
| + List<IrConstant> get constants => constantsList.last; |
| + |
| + IrConstant enterConstant(Constant value, Node node) { |
| + return constants.firstWhere((c) => c.value == value, orElse: () { |
| + IrConstant c = new IrConstant(visitor.nodeOffset(node), value); |
| + constants.add(c); |
| + return c; |
| + }); |
| + } |
| + |
| + IrNode enterStatement(IrNode statement) { |
| + statements.add(statement); |
| + return statement; |
| + } |
| + |
| + void openFunction(IrFunction function) { |
| + returnFunctionList.add(function); |
| + statementsList.add(<IrNode>[]); |
| + constantsList.add(<IrConstant>[]); |
| + } |
| + |
| + void closeFunction() { |
| + returnFunctionList.removeLast(); |
| + statementsList.removeLast(); |
| + constantsList.removeLast(); |
| + } |
| + |
| + void openContinuation() { |
| + statementsList.add(<IrNode>[]); |
| + } |
| + |
| + void closeContinuation() { |
| + statementsList.removeLast(); |
| + } |
| + |
| + isTopLevel() => returnFunctionList.isEmpty; |
| +} |