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 'glue.dart'; | 7 import 'glue.dart'; |
8 | 8 |
9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
10 import '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator; | 10 import '../../tree_ir/tree_ir_nodes.dart' show BuiltinOperator; |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 (int index) => visitExpression(expressions[index]), | 164 (int index) => visitExpression(expressions[index]), |
165 growable: false); | 165 growable: false); |
166 } | 166 } |
167 | 167 |
168 giveup(tree_ir.Node node, | 168 giveup(tree_ir.Node node, |
169 [String reason = 'unimplemented in CodeGenerator']) { | 169 [String reason = 'unimplemented in CodeGenerator']) { |
170 throw new CodegenBailout(node, reason); | 170 throw new CodegenBailout(node, reason); |
171 } | 171 } |
172 | 172 |
173 @override | 173 @override |
| 174 js.Expression visitConcatenateStrings(tree_ir.ConcatenateStrings node) { |
| 175 js.Expression addStrings(js.Expression left, js.Expression right) { |
| 176 return new js.Binary('+', left, right); |
| 177 } |
| 178 |
| 179 js.Expression toString(tree_ir.Expression input) { |
| 180 bool useDirectly = input is tree_ir.Constant && |
| 181 (input.value.isString || |
| 182 input.value.isInt || |
| 183 input.value.isBool); |
| 184 js.Expression value = visitExpression(input); |
| 185 if (useDirectly) { |
| 186 return value; |
| 187 } else { |
| 188 Element convertToString = glue.getStringConversion(); |
| 189 registry.registerStaticUse(convertToString); |
| 190 js.Expression access = glue.staticFunctionAccess(convertToString); |
| 191 return (new js.Call(access, <js.Expression>[value])); |
| 192 } |
| 193 } |
| 194 |
| 195 return node.arguments.map(toString).reduce(addStrings); |
| 196 } |
| 197 |
| 198 @override |
174 js.Expression visitConditional(tree_ir.Conditional node) { | 199 js.Expression visitConditional(tree_ir.Conditional node) { |
175 return new js.Conditional( | 200 return new js.Conditional( |
176 visitExpression(node.condition), | 201 visitExpression(node.condition), |
177 visitExpression(node.thenExpression), | 202 visitExpression(node.thenExpression), |
178 visitExpression(node.elseExpression)); | 203 visitExpression(node.elseExpression)); |
179 } | 204 } |
180 | 205 |
181 js.Expression buildConstant(ConstantValue constant) { | 206 js.Expression buildConstant(ConstantValue constant) { |
182 registry.registerCompileTimeConstant(constant); | 207 registry.registerCompileTimeConstant(constant); |
183 return glue.constantReference(constant); | 208 return glue.constantReference(constant); |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
707 case BuiltinOperator.NumXor: | 732 case BuiltinOperator.NumXor: |
708 return js.js('(# ^ #) >>> 0', args); | 733 return js.js('(# ^ #) >>> 0', args); |
709 case BuiltinOperator.NumLt: | 734 case BuiltinOperator.NumLt: |
710 return new js.Binary('<', args[0], args[1]); | 735 return new js.Binary('<', args[0], args[1]); |
711 case BuiltinOperator.NumLe: | 736 case BuiltinOperator.NumLe: |
712 return new js.Binary('<=', args[0], args[1]); | 737 return new js.Binary('<=', args[0], args[1]); |
713 case BuiltinOperator.NumGt: | 738 case BuiltinOperator.NumGt: |
714 return new js.Binary('>', args[0], args[1]); | 739 return new js.Binary('>', args[0], args[1]); |
715 case BuiltinOperator.NumGe: | 740 case BuiltinOperator.NumGe: |
716 return new js.Binary('>=', args[0], args[1]); | 741 return new js.Binary('>=', args[0], args[1]); |
717 case BuiltinOperator.StringConcatenate: | |
718 if (args.isEmpty) return js.string(''); | |
719 return args.reduce((e1,e2) => new js.Binary('+', e1, e2)); | |
720 case BuiltinOperator.StrictEq: | 742 case BuiltinOperator.StrictEq: |
721 return new js.Binary('===', args[0], args[1]); | 743 return new js.Binary('===', args[0], args[1]); |
722 case BuiltinOperator.StrictNeq: | 744 case BuiltinOperator.StrictNeq: |
723 return new js.Binary('!==', args[0], args[1]); | 745 return new js.Binary('!==', args[0], args[1]); |
724 case BuiltinOperator.LooseEq: | 746 case BuiltinOperator.LooseEq: |
725 return new js.Binary('==', args[0], args[1]); | 747 return new js.Binary('==', args[0], args[1]); |
726 case BuiltinOperator.LooseNeq: | 748 case BuiltinOperator.LooseNeq: |
727 return new js.Binary('!=', args[0], args[1]); | 749 return new js.Binary('!=', args[0], args[1]); |
728 case BuiltinOperator.IsFalsy: | 750 case BuiltinOperator.IsFalsy: |
729 return new js.Prefix('!', args[0]); | 751 return new js.Prefix('!', args[0]); |
730 case BuiltinOperator.IsNumber: | 752 case BuiltinOperator.IsNumber: |
731 return js.js("typeof # === 'number'", args); | 753 return js.js("typeof # === 'number'", args); |
732 case BuiltinOperator.IsNotNumber: | 754 case BuiltinOperator.IsNotNumber: |
733 return js.js("typeof # !== 'number'", args); | 755 return js.js("typeof # !== 'number'", args); |
734 case BuiltinOperator.IsFloor: | 756 case BuiltinOperator.IsFloor: |
735 return js.js("Math.floor(#) === #", args); | 757 return js.js("Math.floor(#) === #", args); |
736 case BuiltinOperator.IsNumberAndFloor: | 758 case BuiltinOperator.IsNumberAndFloor: |
737 return js.js("typeof # === 'number' && Math.floor(#) === #", args); | 759 return js.js("typeof # === 'number' && Math.floor(#) === #", args); |
738 } | 760 } |
739 } | 761 } |
740 | 762 |
741 visitFunctionExpression(tree_ir.FunctionExpression node) { | 763 visitFunctionExpression(tree_ir.FunctionExpression node) { |
742 // FunctionExpressions are currently unused. | 764 // FunctionExpressions are currently unused. |
743 // We might need them if we want to emit raw JS nested functions. | 765 // We might need them if we want to emit raw JS nested functions. |
744 throw 'FunctionExpressions should not be used'; | 766 throw 'FunctionExpressions should not be used'; |
745 } | 767 } |
746 } | 768 } |
OLD | NEW |