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..24a3507b5d805078829f30183a4c2d042839c637 |
--- /dev/null |
+++ b/sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart |
@@ -0,0 +1,265 @@ |
+// 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. |
ngeoffray
2013/11/21 15:02:05
Please add high-level comments on classes in this
lukas
2013/11/21 17:14:27
Done.
|
+ |
+library ir_builder; |
+ |
+import 'ir_nodes.dart'; |
+import '../elements/elements.dart'; |
+import '../dart2jslib.dart'; |
+import '../source_file.dart'; |
+import '../tree/tree.dart'; |
+import '../scanner/scannerlib.dart' show Token; |
+import '../js_backend/js_backend.dart' show JavaScriptBackend; |
+ |
+class IrBuilderTask extends CompilerTask { |
+ final Map<Element, IrNode> nodes = new Map<Element, IrNode>(); |
+ |
+ IrBuilderTask(Compiler compiler) : super(compiler); |
+ |
+ String get name => 'IR builder'; |
+ |
+ void buildNodes() { |
+ // TODO(lry): support checked-mode checks. |
+ if (compiler.enableTypeAssertions) return; |
+ |
+ Map<Element, TreeElements> resolved = |
+ compiler.enqueuer.resolution.resolvedElements; |
+ resolved.forEach((Element element, TreeElements elementsMapping) { |
+ element = element.implementation; |
+ |
+ SourceFile sourceFile = elementSourceFile(element); |
+ IrNodeBuilderVisitor visitor = |
+ new IrNodeBuilderVisitor(elementsMapping, compiler, sourceFile); |
+ IrNode irNode; |
+ ElementKind kind = element.kind; |
+ if (kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
+ // TODO(lry): build ir for constructors. |
+ } 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) { |
+ // TODO(lry): build ir for lazy initializers of static fields. |
+ } else { |
+ compiler.internalErrorOnElement(element, |
+ 'unexpected element kind $kind'); |
+ } |
+ |
+ if (irNode != null) { |
+ nodes[element] = irNode; |
+ unlinkTreeAndToken(element); |
+ } |
+ }); |
+ } |
+ |
+ void unlinkTreeAndToken(element) { |
+ // TODO(lry): Make the dart backend generate code from IR nodes. |
+ if (compiler.backend is JavaScriptBackend) { |
+ element.beginToken.next = null; |
+ element.cachedNode = null; |
+ } |
+ } |
+ |
+ 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> { |
+ final SourceFile sourceFile; |
+ |
+ IrNodeBuilderVisitor( |
+ TreeElements elements, |
+ Compiler compiler, |
+ this.sourceFile) |
+ : super(elements, compiler); |
+ |
+ IrBuilder builder; |
+ |
+ IrNode buildMethod(FunctionElement functionElement) { |
+ return nullIfGiveup(() => buildMethodInternal(functionElement)); |
+ } |
+ |
+ IrNode buildMethodInternal(FunctionElement functionElement) { |
+ assert(invariant(functionElement, functionElement.isImplementation)); |
+ FunctionExpression function = functionElement.parseNode(compiler); |
+ assert(function != null); |
+ assert(!function.modifiers.isExternal()); |
+ assert(elements[function] != null); |
+ |
+ int endPosition = function.getEndToken().charOffset; |
+ int namePosition = elements[function].position().charOffset; |
+ IrFunction result = new IrFunction( |
+ nodePosition(function), endPosition, namePosition, <IrNode>[]); |
+ builder = new IrBuilder(this); |
+ builder.enterBlock(); |
+ if (function.hasBody()) { |
+ function.body.accept(this); |
+ ensureReturn(function); |
+ result.statements |
+ ..addAll(builder.constants.values) |
+ ..addAll(builder.statements); |
+ } |
+ builder.exitBlock(); |
+ return result; |
+ } |
+ |
+ IrNode buildNode(Node node) { |
ngeoffray
2013/11/21 15:02:05
Unused.
lukas
2013/11/21 17:14:27
Done.
|
+ } |
+ |
+ ConstantSystem get constantSystem => compiler.backend.constantSystem; |
+ |
+ /* int | PositionWithIdentifierName */ nodePosition(Node node) { |
+ Token token = node.getBeginToken(); |
+ if (token.isIdentifier()) { |
+ return new PositionWithIdentifierName(token.charOffset, token.value); |
+ } else { |
+ return token.charOffset; |
+ } |
+ } |
+ |
+ void ensureReturn(FunctionExpression node) { |
+ if (!builder.returnOnAllBranches) { |
ngeoffray
2013/11/21 15:02:05
Nit: Reverse the condition and return to avoid ind
lukas
2013/11/21 17:14:27
Done.
|
+ IrConstant nullValue = |
+ builder.enterConstant(constantSystem.createNull(), node); |
+ builder.enterStatement(new IrReturn(nodePosition(node), nullValue)); |
+ } |
+ } |
+ |
+ IrNode visitBlock(Block node) { |
+ for (Node n in node.statements.nodes) { |
+ n.accept(this); |
+ } |
+ } |
+ |
+ IrNode visitReturn(Return node) { |
+ IrNode value; |
+ if (node.expression == null) { |
+ if (node.beginToken.value == 'native') return giveup(); |
+ value = builder.enterConstant(constantSystem.createNull(), node); |
+ } else { |
+ value = node.expression.accept(this); |
+ } |
+ builder.branchReturns(); |
+ return builder.enterStatement( |
+ new IrReturn(nodePosition(node), value)); |
+ } |
+ |
+ IrConstant visitLiteralBool(LiteralBool node) => |
+ builder.enterConstant(constantSystem.createBool(node.value), node); |
+ |
+ IrConstant visitLiteralDouble(LiteralDouble node) => |
+ builder.enterConstant(constantSystem.createDouble(node.value), node); |
+ |
+ IrConstant visitLiteralInt(LiteralInt node) => |
+ builder.enterConstant(constantSystem.createInt(node.value), node); |
+ |
+ IrConstant visitLiteralString(LiteralString node) => |
+ builder.enterConstant( |
+ constantSystem.createString(node.dartString, node), node); |
+ |
+ IrConstant visitLiteralNull(LiteralNull node) => |
+ builder.enterConstant(constantSystem.createNull(), node); |
+ |
+// TODO(lry): other literals. |
+// 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; |
+ |
+ IrNode nullIfGiveup(IrNode action()) { |
+ try { |
+ return action(); |
+ } catch(e) { |
+ if (e == ABORT_IRNODE_BUILDER) return null; |
+ rethrow; |
+ } |
+ } |
+ |
+ void internalError(String reason, {Node node}) { |
+ giveup(); |
+ } |
+} |
+ |
+class IrBuilder { |
+ final IrNodeBuilderVisitor visitor; |
+ IrBuilder(this.visitor); |
+ |
+ // Statements lists for nested blocks. |
+ List<List<IrNode>> statementsList = <List<IrNode>>[]; |
+ List<IrNode> get statements => statementsList.last; |
+ |
+ // TODO(lry): Need to fix this once we actually have branching. Probably |
+ // this will be handled by the LocalsHandler, not here. |
+ List<bool> returnOnAllBranchesList = <bool>[]; |
+ bool get returnOnAllBranches => returnOnAllBranchesList.last; |
+ void branchReturns() { |
+ returnOnAllBranchesList[returnOnAllBranchesList.length - 1] = true; |
+ } |
+ |
+ Map<Constant, IrConstant> constants = <Constant, IrConstant>{}; |
+ |
+ IrConstant enterConstant(Constant value, Node node) { |
ngeoffray
2013/11/21 15:02:05
addConstant?
lukas
2013/11/21 17:14:27
Done.
|
+ return constants.putIfAbsent( |
+ value, () => new IrConstant(visitor.nodePosition(node), value)); |
+ } |
+ |
+ IrNode enterStatement(IrNode statement) { |
ngeoffray
2013/11/21 15:02:05
addStatement?
lukas
2013/11/21 17:14:27
Done.
|
+ statements.add(statement); |
+ return statement; |
+ } |
+ |
+ void enterBlock() { |
+ statementsList.add(<IrNode>[]); |
+ returnOnAllBranchesList.add(false); |
+ } |
+ |
+ void exitBlock() { |
+ statementsList.removeLast(); |
+ returnOnAllBranchesList.removeLast(); |
+ } |
+} |