Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 /// Generate code using the cps-based IR pipeline. | |
| 6 library code_generator_task; | |
| 7 | |
| 8 import '../../dart2jslib.dart'; | |
| 9 import '../../source_file.dart'; | |
| 10 import '../../cps_ir/cps_ir_nodes.dart' as cps_ir; | |
|
floitsch
2014/11/12 11:03:20
I would go with "as cps".
sigurdm
2014/11/13 08:29:42
Done.
| |
| 11 import '../../cps_ir/cps_ir_builder.dart'; | |
| 12 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | |
| 13 import '../../tree/tree.dart' as ast; | |
| 14 import '../../scanner/scannerlib.dart' as scanner; | |
| 15 import '../../elements/elements.dart'; | |
| 16 import '../../js/js.dart' as js; | |
| 17 import '../../source_map_builder.dart'; | |
| 18 import '../../tree_ir/tree_ir_builder.dart' as tree_builder; | |
| 19 import '../../dart_backend/backend_ast_emitter.dart' as backend_ast_emitter; | |
| 20 import '../../cps_ir/optimizers.dart'; | |
| 21 import '../../tracer.dart'; | |
| 22 import '../../dart_backend/statement_rewriter.dart'; | |
| 23 import '../../dart_backend/copy_propagator.dart'; | |
| 24 import '../../dart_backend/loop_rewriter.dart'; | |
| 25 import '../../dart_backend/logical_rewriter.dart'; | |
| 26 import '../../js_backend/codegen/codegen.dart'; | |
| 27 | |
| 28 class CodeGenTask extends CompilerTask { | |
| 29 | |
| 30 Tracer tracer; // TODO(sigurdm): Assign this. | |
|
floitsch
2014/11/12 11:03:20
nit: two spaces before "//".
sigurdm
2014/11/13 08:29:42
Done.
| |
| 31 | |
| 32 String get name => 'CPS Ir pipeline'; | |
| 33 | |
| 34 final IrBuilderTask irBuilderTask; | |
| 35 | |
| 36 ConstantSystem get constantSystem => compiler.backend.constantSystem; | |
| 37 | |
| 38 CodeGenTask(Compiler compiler) | |
| 39 : irBuilderTask = new IrBuilderTask(compiler), | |
| 40 super(compiler); | |
| 41 | |
| 42 js.Fun build(CodegenWorkItem work) { | |
|
floitsch
2014/11/12 11:03:20
This is the public entry point.
Add dartdoc.
sigurdm
2014/11/13 08:29:42
Done.
| |
| 43 return measure(() { | |
| 44 AstElement element = work.element; | |
| 45 | |
| 46 return compiler.withCurrentElement(element, () { | |
| 47 cps_ir.FunctionDefinition cps_definition; | |
|
floitsch
2014/11/12 11:03:20
cpsDefinition =
irBuilderTask.buildNode(elemen
floitsch
2014/11/12 11:03:20
cpsDefinition
Probably a copy/paste. Please fix t
sigurdm
2014/11/13 08:29:42
Seems to be my mistake only
sigurdm
2014/11/13 08:29:42
Done.
| |
| 48 cps_definition = irBuilderTask.buildNode(element); | |
| 49 if (cps_definition == null) return null; | |
| 50 | |
| 51 // Transformations on the CPS IR. | |
| 52 if (tracer != null) { | |
| 53 tracer.traceCompilation(element.name, null); | |
|
floitsch
2014/11/12 11:03:20
The cps_ir_builder_visitor.dart already does a tra
sigurdm
2014/11/13 08:29:42
It seems so - I disabled the tracing from cps_ir_b
| |
| 54 } | |
| 55 | |
| 56 void traceGraph(String title, var irObject) { | |
| 57 if (tracer != null) { | |
| 58 tracer.traceGraph(title, irObject); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 new ConstantPropagator(compiler, constantSystem) | |
| 63 .rewrite(cps_definition); | |
| 64 traceGraph("Sparse constant propagation", cps_definition); | |
| 65 new RedundantPhiEliminator().rewrite(cps_definition); | |
| 66 traceGraph("Redundant phi elimination", cps_definition); | |
| 67 new ShrinkingReducer().rewrite(cps_definition); | |
| 68 traceGraph("Shrinking reductions", cps_definition); | |
| 69 | |
| 70 // Do not rewrite the IR after variable allocation. Allocation | |
| 71 // makes decisions based on an approximation of IR variable live | |
| 72 // ranges that can be invalidated by transforming the IR. | |
| 73 new cps_ir.RegisterAllocator().visit(cps_definition); | |
| 74 | |
| 75 tree_builder.Builder builder = new tree_builder.Builder(compiler); | |
| 76 tree_ir.FunctionDefinition definition = builder.build(cps_definition); | |
| 77 assert(definition != null); | |
| 78 traceGraph('Tree builder', definition); | |
| 79 | |
| 80 // Transformations on the Tree IR. | |
| 81 new StatementRewriter().rewrite(definition); | |
| 82 traceGraph('Statement rewriter', definition); | |
| 83 new CopyPropagator().rewrite(definition); | |
| 84 traceGraph('Copy propagation', definition); | |
| 85 new LoopRewriter().rewrite(definition); | |
| 86 traceGraph('Loop rewriter', definition); | |
| 87 new LogicalRewriter().rewrite(definition); | |
| 88 traceGraph('Logical rewriter', definition); | |
| 89 new backend_ast_emitter.UnshadowParameters().unshadow(definition); | |
| 90 traceGraph('Unshadow parameters', definition); | |
| 91 | |
| 92 CodeGen codeGen = new CodeGen(); | |
| 93 try { | |
| 94 codeGen.buildFunction(definition); | |
| 95 return buildJavaScriptFunction(work.element, | |
| 96 codeGen.parameters, | |
|
floitsch
2014/11/12 11:03:20
I don't like to return values as fields.
Let's rev
sigurdm
2014/11/13 08:29:42
Acknowledged.
| |
| 97 codeGen.body); | |
| 98 } catch (e, tr) { | |
| 99 if (e == CodeGen.UNIMPLEMENTED) { | |
| 100 return null; | |
| 101 } else { | |
| 102 rethrow; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 }); | |
| 107 }); | |
| 108 } | |
| 109 | |
| 110 js.Node attachPosition(js.Node node, AstElement element) { | |
| 111 // TODO(sra): Attaching positions might be cleaner if the source position | |
| 112 // was on a wrapping node. | |
| 113 SourceFile sourceFile = sourceFileOfElement(element); | |
| 114 String name = element.name; | |
| 115 AstElement implementation = element.implementation; | |
| 116 ast.Node expression = implementation.node; | |
| 117 scanner.Token beginToken; | |
| 118 scanner.Token endToken; | |
| 119 if (expression == null) { | |
| 120 // Synthesized node. Use the enclosing element for the location. | |
| 121 beginToken = endToken = element.position; | |
| 122 } else { | |
| 123 beginToken = expression.getBeginToken(); | |
| 124 endToken = expression.getEndToken(); | |
| 125 } | |
| 126 // TODO(podivilov): find the right sourceFile here and remove offset | |
| 127 // checks below. | |
| 128 var sourcePosition, endSourcePosition; | |
| 129 if (beginToken.charOffset < sourceFile.length) { | |
| 130 sourcePosition = | |
| 131 new TokenSourceFileLocation(sourceFile, beginToken, name); | |
| 132 } | |
| 133 if (endToken.charOffset < sourceFile.length) { | |
| 134 endSourcePosition = | |
| 135 new TokenSourceFileLocation(sourceFile, endToken, name); | |
| 136 } | |
| 137 return node.withPosition(sourcePosition, endSourcePosition); | |
| 138 } | |
| 139 | |
| 140 SourceFile sourceFileOfElement(Element element) { | |
| 141 return element.implementation.compilationUnit.script.file; | |
| 142 } | |
| 143 | |
| 144 js.Fun buildJavaScriptFunction(FunctionElement element, | |
| 145 List<js.Parameter> parameters, | |
| 146 js.Block body) { | |
| 147 return attachPosition(new js.Fun(parameters, body), element); | |
| 148 } | |
| 149 } | |
| OLD | NEW |