| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library code_generator; | 5 library code_generator; |
| 6 | 6 |
| 7 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | 7 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
| 8 import '../../js/js.dart' as js; | 8 import '../../js/js.dart' as js; |
| 9 import '../../elements/elements.dart'; | 9 import '../../elements/elements.dart'; |
| 10 import '../../util/maplet.dart'; | 10 import '../../util/maplet.dart'; |
| 11 | 11 |
| 12 class CodegenBailout { |
| 13 final tree_ir.Node node; |
| 14 final String reason; |
| 15 CodegenBailout(this.node, this.reason); |
| 16 String get message { |
| 17 return 'bailout${node != null ? " on $node" : ""}: $reason'; |
| 18 } |
| 19 } |
| 20 |
| 12 class CodeGenerator extends tree_ir.Visitor<dynamic, js.Expression> { | 21 class CodeGenerator extends tree_ir.Visitor<dynamic, js.Expression> { |
| 13 static const String UNIMPLEMENTED = "Javascript generation aborted"; | |
| 14 | |
| 15 /// Variables to be hoisted at the top of the current function. | 22 /// Variables to be hoisted at the top of the current function. |
| 16 List<js.VariableDeclaration> variables = <js.VariableDeclaration>[]; | 23 List<js.VariableDeclaration> variables = <js.VariableDeclaration>[]; |
| 17 | 24 |
| 18 /// Maps variables to their name. | 25 /// Maps variables to their name. |
| 19 Map<tree_ir.Variable, String> variableNames = <tree_ir.Variable, String>{}; | 26 Map<tree_ir.Variable, String> variableNames = <tree_ir.Variable, String>{}; |
| 20 | 27 |
| 21 /// Maps local constants to their name. | 28 /// Maps local constants to their name. |
| 22 Maplet<VariableElement, String> constantNames = | 29 Maplet<VariableElement, String> constantNames = |
| 23 new Maplet<VariableElement, String>(); | 30 new Maplet<VariableElement, String>(); |
| 24 | 31 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 for (tree_ir.Variable parameter in function.parameters) { | 47 for (tree_ir.Variable parameter in function.parameters) { |
| 41 parameters.add(new js.Parameter(variableNames[parameter])); | 48 parameters.add(new js.Parameter(variableNames[parameter])); |
| 42 } | 49 } |
| 43 body = new js.Block(accumulator); | 50 body = new js.Block(accumulator); |
| 44 } | 51 } |
| 45 | 52 |
| 46 List<js.Expression> visitArguments(List<tree_ir.Expression> arguments) { | 53 List<js.Expression> visitArguments(List<tree_ir.Expression> arguments) { |
| 47 return arguments.map(visitExpression).toList(); | 54 return arguments.map(visitExpression).toList(); |
| 48 } | 55 } |
| 49 | 56 |
| 50 giveup(tree_ir.Node node) { | 57 giveup(tree_ir.Node node, |
| 51 throw UNIMPLEMENTED; | 58 [String reason = 'unimplemented in CodeGenerator']) { |
| 59 throw new CodegenBailout(node, reason); |
| 52 } | 60 } |
| 53 | 61 |
| 54 @override | 62 @override |
| 55 js.Expression visitConcatenateStrings(tree_ir.ConcatenateStrings node) { | 63 js.Expression visitConcatenateStrings(tree_ir.ConcatenateStrings node) { |
| 56 return giveup(node); | 64 return giveup(node); |
| 57 // TODO: implement visitConcatenateStrings | 65 // TODO: implement visitConcatenateStrings |
| 58 } | 66 } |
| 59 | 67 |
| 60 @override | 68 @override |
| 61 js.Expression visitConditional(tree_ir.Conditional node) { | 69 js.Expression visitConditional(tree_ir.Conditional node) { |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // TODO: implement visitWhileTrue | 210 // TODO: implement visitWhileTrue |
| 203 } | 211 } |
| 204 | 212 |
| 205 @override | 213 @override |
| 206 void visitReturn(tree_ir.Return node) { | 214 void visitReturn(tree_ir.Return node) { |
| 207 if (node.value != null) { | 215 if (node.value != null) { |
| 208 accumulator.add(new js.Return(visitExpression(node.value))); | 216 accumulator.add(new js.Return(visitExpression(node.value))); |
| 209 } | 217 } |
| 210 } | 218 } |
| 211 } | 219 } |
| OLD | NEW |