Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1958)

Unified Diff: sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart

Issue 57873002: Build new IR for functions returning a constant (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: type inference and inlining for new ir nodes Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..93c61657dadd100012202a2b6bac350a37b37bdf
--- /dev/null
+++ b/sdk/lib/_internal/compiler/implementation/ir/ir_builder.dart
@@ -0,0 +1,275 @@
+// 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';
+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() {
+ 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 element) {
ngeoffray 2013/11/20 14:55:27 I'd just untype element to avoid the 'as dynamic'.
lukas 2013/11/21 12:20:21 Done.
+ if (compiler.backend is JavaScriptBackend) {
+ (element as dynamic).beginToken.next = null;
+ (element as dynamic).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> {
+ SourceFile sourceFile;
+
+ Context context;
+
+ IrNodeBuilderVisitor(TreeElements elements, Compiler compiler,
+ this.sourceFile) : super(elements, compiler) {
ngeoffray 2013/11/20 14:55:27 Nit: One argument per line, and super on a new lin
lukas 2013/11/21 12:20:21 Done.
+ 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;
+ rethrow;
+ }
+ }
+
+ 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;
+ }
+ }
+
+ IrNode visitFunctionExpression(FunctionExpression node) {
+ int endPosition = node.getEndToken().charOffset;
+ int namePosition = elements[node].position().charOffset;
+ IrFunction result = new IrFunction(
+ nodePosition(node), endPosition, namePosition, <IrNode>[]);
+ context.openFunction(result);
+ if (node.hasBody()) {
+ node.body.accept(this);
+ ensureReturn(node);
+ result.statements..addAll(context.constants)..addAll(context.statements);
+ }
+ context.closeFunction();
+ if (!context.isTopLevel()) {
+ context.enterStatement(result);
+ }
+ return result;
+ }
+
+ void ensureReturn(FunctionExpression node) {
+ if (!context.returnOnAllBranches) {
+ IrConstant nullValue =
+ context.enterConstant(constantSystem.createNull(), node);
+ context.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 = context.enterConstant(constantSystem.createNull(), node);
+ } else {
+ value = node.expression.accept(this);
+ }
+ context.branchReturns();
+ return context.enterStatement(
+ new IrReturn(nodePosition(node), 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> currentFunctionList = <IrFunction>[];
+ IrFunction get currentFunction => currentFunctionList.last;
ngeoffray 2013/11/20 14:55:27 Actually, we will probably not inline at this leve
lukas 2013/11/21 12:20:21 Maybe we want to keep nested functions as such in
+
+ // TODO(lry): Need to fix this once we actually have branching. The things we
+ // currently achieve with the Conctext class might overlap with LocalHandlers.
ngeoffray 2013/11/20 14:55:27 Conctext -> Context
lukas 2013/11/21 12:20:21 Done.
+ List<bool> returnOnAllBranchesList = <bool>[];
+ bool get returnOnAllBranches => returnOnAllBranchesList.last;
+ void branchReturns() {
+ returnOnAllBranchesList[returnOnAllBranchesList.length - 1] = true;
+ }
+
+ 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) {
ngeoffray 2013/11/20 14:55:27 Why not a Map of Constant -> IrConstant? Like we h
lukas 2013/11/21 12:20:21 Done.
+ return constants.firstWhere((c) => c.value == value, orElse: () {
+ IrConstant c = new IrConstant(visitor.nodePosition(node), value);
+ constants.add(c);
+ return c;
+ });
+ }
+
+ IrNode enterStatement(IrNode statement) {
+ statements.add(statement);
+ return statement;
+ }
+
+ void openFunction(IrFunction function) {
+ currentFunctionList.add(function);
+ statementsList.add(<IrNode>[]);
+ returnOnAllBranchesList.add(false);
+ constantsList.add(<IrConstant>[]);
+ }
+
+ void closeFunction() {
+ currentFunctionList.removeLast();
+ statementsList.removeLast();
+ returnOnAllBranchesList.removeLast();
+ constantsList.removeLast();
+ }
+
+ void openContinuation() {
ngeoffray 2013/11/20 14:55:27 Unused.
lukas 2013/11/21 12:20:21 Done.
+ statementsList.add(<IrNode>[]);
+ }
+
+ void closeContinuation() {
ngeoffray 2013/11/20 14:55:27 Unused.
lukas 2013/11/21 12:20:21 Done.
+ statementsList.removeLast();
+ }
+
+ isTopLevel() => currentFunctionList.isEmpty;
+}

Powered by Google App Engine
This is Rietveld 408576698