OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library fletchc.codegen_visitor; | 5 library dartino_compiler.codegen_visitor; |
6 | 6 |
7 import 'package:compiler/src/resolution/semantic_visitor.dart'; | 7 import 'package:compiler/src/resolution/semantic_visitor.dart'; |
8 | 8 |
9 import 'package:compiler/src/resolution/operators.dart' show | 9 import 'package:compiler/src/resolution/operators.dart' show |
10 AssignmentOperator, | 10 AssignmentOperator, |
11 BinaryOperator, | 11 BinaryOperator, |
12 IncDecOperator, | 12 IncDecOperator, |
13 UnaryOperator; | 13 UnaryOperator; |
14 | 14 |
15 import 'package:compiler/src/constants/expressions.dart' show | 15 import 'package:compiler/src/constants/expressions.dart' show |
(...skipping 19 matching lines...) Expand all Loading... |
35 import 'package:compiler/src/elements/elements.dart'; | 35 import 'package:compiler/src/elements/elements.dart'; |
36 import 'package:compiler/src/tree/tree.dart'; | 36 import 'package:compiler/src/tree/tree.dart'; |
37 import 'package:compiler/src/universe/call_structure.dart' show | 37 import 'package:compiler/src/universe/call_structure.dart' show |
38 CallStructure; | 38 CallStructure; |
39 import 'package:compiler/src/universe/selector.dart' show | 39 import 'package:compiler/src/universe/selector.dart' show |
40 Selector; | 40 Selector; |
41 import 'package:compiler/src/diagnostics/spannable.dart' show | 41 import 'package:compiler/src/diagnostics/spannable.dart' show |
42 Spannable; | 42 Spannable; |
43 import 'package:compiler/src/dart_types.dart'; | 43 import 'package:compiler/src/dart_types.dart'; |
44 | 44 |
45 import 'fletch_context.dart'; | 45 import 'dartino_context.dart'; |
46 | 46 |
47 import 'fletch_backend.dart'; | 47 import 'dartino_backend.dart'; |
48 | 48 |
49 import 'fletch_constants.dart' show | 49 import 'dartino_constants.dart' show |
50 FletchClassConstant, | 50 DartinoClassConstant, |
51 FletchClassInstanceConstant; | 51 DartinoClassInstanceConstant; |
52 | 52 |
53 import 'fletch_function_builder.dart' show | 53 import 'dartino_function_builder.dart' show |
54 FletchFunctionBuilder; | 54 DartinoFunctionBuilder; |
55 | 55 |
56 import 'fletch_class_builder.dart' show | 56 import 'dartino_class_builder.dart' show |
57 FletchClassBuilder; | 57 DartinoClassBuilder; |
58 | 58 |
59 import 'fletch_selector.dart'; | 59 import 'dartino_selector.dart'; |
60 | 60 |
61 import '../fletch_system.dart'; | 61 import '../dartino_system.dart'; |
62 | 62 |
63 import 'closure_environment.dart'; | 63 import 'closure_environment.dart'; |
64 | 64 |
65 import '../incremental/fletchc_incremental.dart' show | 65 import '../incremental/dartino_compiler_incremental.dart' show |
66 IncrementalCompilationFailed; // TODO(ahe): Remove this import. | 66 IncrementalCompilationFailed; // TODO(ahe): Remove this import. |
67 | 67 |
68 import 'fletch_registry.dart' show | 68 import 'dartino_registry.dart' show |
69 ClosureKind, | 69 ClosureKind, |
70 FletchRegistry; | 70 DartinoRegistry; |
71 | 71 |
72 import 'package:compiler/src/diagnostics/diagnostic_listener.dart' show | 72 import 'package:compiler/src/diagnostics/diagnostic_listener.dart' show |
73 DiagnosticMessage; | 73 DiagnosticMessage; |
74 | 74 |
75 import 'package:compiler/src/diagnostics/messages.dart' show | 75 import 'package:compiler/src/diagnostics/messages.dart' show |
76 MessageKind; | 76 MessageKind; |
77 | 77 |
78 import 'package:compiler/src/constants/values.dart' show | 78 import 'package:compiler/src/constants/values.dart' show |
79 ConstantValue; | 79 ConstantValue; |
80 | 80 |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 InitializerBulkMixin, | 235 InitializerBulkMixin, |
236 BaseImplementationOfStaticsMixin, | 236 BaseImplementationOfStaticsMixin, |
237 BaseImplementationOfLocalsMixin, | 237 BaseImplementationOfLocalsMixin, |
238 SetIfNullBulkMixin | 238 SetIfNullBulkMixin |
239 implements SemanticSendVisitor, SemanticDeclarationVisitor { | 239 implements SemanticSendVisitor, SemanticDeclarationVisitor { |
240 // A literal int can have up to 31 bits of information (32 minus sign). | 240 // A literal int can have up to 31 bits of information (32 minus sign). |
241 static const int LITERAL_INT_MAX = 0x3FFFFFFF; | 241 static const int LITERAL_INT_MAX = 0x3FFFFFFF; |
242 static const int MAX_INT64 = (1 << 63) - 1; | 242 static const int MAX_INT64 = (1 << 63) - 1; |
243 static const int MIN_INT64 = -(1 << 63); | 243 static const int MIN_INT64 = -(1 << 63); |
244 | 244 |
245 final FletchContext context; | 245 final DartinoContext context; |
246 | 246 |
247 final ClosureEnvironment closureEnvironment; | 247 final ClosureEnvironment closureEnvironment; |
248 | 248 |
249 final ExecutableElement element; | 249 final ExecutableElement element; |
250 | 250 |
251 final FletchFunctionBuilder functionBuilder; | 251 final DartinoFunctionBuilder functionBuilder; |
252 | 252 |
253 final Map<Element, LocalValue> scope = <Element, LocalValue>{}; | 253 final Map<Element, LocalValue> scope = <Element, LocalValue>{}; |
254 | 254 |
255 final Map<Node, JumpInfo> jumpInfo = <Node, JumpInfo>{}; | 255 final Map<Node, JumpInfo> jumpInfo = <Node, JumpInfo>{}; |
256 | 256 |
257 // Stack of try blocks (inner-most first), in the lexical scope. | 257 // Stack of try blocks (inner-most first), in the lexical scope. |
258 Link<TryBlock> tryBlockStack = const Link<TryBlock>(); | 258 Link<TryBlock> tryBlockStack = const Link<TryBlock>(); |
259 | 259 |
260 VisitState visitState; | 260 VisitState visitState; |
261 BytecodeLabel trueLabel; | 261 BytecodeLabel trueLabel; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 int allocateConstantFromNode(Node node, {TreeElements elements}) { | 325 int allocateConstantFromNode(Node node, {TreeElements elements}) { |
326 ConstantExpression expression = compileConstant( | 326 ConstantExpression expression = compileConstant( |
327 node, | 327 node, |
328 elements: elements, | 328 elements: elements, |
329 isConst: false); | 329 isConst: false); |
330 return functionBuilder.allocateConstant( | 330 return functionBuilder.allocateConstant( |
331 context.getConstantValue(expression)); | 331 context.getConstantValue(expression)); |
332 } | 332 } |
333 | 333 |
334 int allocateConstantClassInstance(int classId) { | 334 int allocateConstantClassInstance(int classId) { |
335 var constant = new FletchClassInstanceConstant(classId); | 335 var constant = new DartinoClassInstanceConstant(classId); |
336 context.markConstantUsed(constant); | 336 context.markConstantUsed(constant); |
337 return functionBuilder.allocateConstant(constant); | 337 return functionBuilder.allocateConstant(constant); |
338 } | 338 } |
339 | 339 |
340 int allocateStringConstant(String string) { | 340 int allocateStringConstant(String string) { |
341 return functionBuilder.allocateConstant( | 341 return functionBuilder.allocateConstant( |
342 context.backend.constantSystem.createString( | 342 context.backend.constantSystem.createString( |
343 new DartString.literal(string))); | 343 new DartString.literal(string))); |
344 } | 344 } |
345 | 345 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 /// various kinds of implicit or explicit closurizations that can occur. | 415 /// various kinds of implicit or explicit closurizations that can occur. |
416 void registerClosurization(FunctionElement element, ClosureKind kind); | 416 void registerClosurization(FunctionElement element, ClosureKind kind); |
417 | 417 |
418 int compileLazyFieldInitializer(FieldElement field); | 418 int compileLazyFieldInitializer(FieldElement field); |
419 | 419 |
420 void invokeMethod(Node node, Selector selector) { | 420 void invokeMethod(Node node, Selector selector) { |
421 registerDynamicUse(selector); | 421 registerDynamicUse(selector); |
422 String symbol = context.getSymbolFromSelector(selector); | 422 String symbol = context.getSymbolFromSelector(selector); |
423 int id = context.getSymbolId(symbol); | 423 int id = context.getSymbolId(symbol); |
424 int arity = selector.argumentCount; | 424 int arity = selector.argumentCount; |
425 int fletchSelector = FletchSelector.encodeMethod(id, arity); | 425 int dartinoSelector = DartinoSelector.encodeMethod(id, arity); |
426 assembler.invokeMethod(fletchSelector, arity, selector.name); | 426 assembler.invokeMethod(dartinoSelector, arity, selector.name); |
427 } | 427 } |
428 | 428 |
429 void invokeGetter(Node node, Name name) { | 429 void invokeGetter(Node node, Name name) { |
430 registerDynamicUse(new Selector.getter(name)); | 430 registerDynamicUse(new Selector.getter(name)); |
431 String symbol = context.mangleName(name); | 431 String symbol = context.mangleName(name); |
432 int id = context.getSymbolId(symbol); | 432 int id = context.getSymbolId(symbol); |
433 int fletchSelector = FletchSelector.encodeGetter(id); | 433 int dartinoSelector = DartinoSelector.encodeGetter(id); |
434 assembler.invokeMethod(fletchSelector, 0); | 434 assembler.invokeMethod(dartinoSelector, 0); |
435 } | 435 } |
436 | 436 |
437 void invokeSetter(Node node, Name name) { | 437 void invokeSetter(Node node, Name name) { |
438 registerDynamicUse(new Selector.setter(name)); | 438 registerDynamicUse(new Selector.setter(name)); |
439 String symbol = context.mangleName(name); | 439 String symbol = context.mangleName(name); |
440 int id = context.getSymbolId(symbol); | 440 int id = context.getSymbolId(symbol); |
441 int fletchSelector = FletchSelector.encodeSetter(id); | 441 int dartinoSelector = DartinoSelector.encodeSetter(id); |
442 assembler.invokeMethod(fletchSelector, 1); | 442 assembler.invokeMethod(dartinoSelector, 1); |
443 } | 443 } |
444 | 444 |
445 void invokeFactory(Node node, int constId, int arity) { | 445 void invokeFactory(Node node, int constId, int arity) { |
446 assembler.invokeFactory(constId, arity); | 446 assembler.invokeFactory(constId, arity); |
447 } | 447 } |
448 | 448 |
449 void invokeStatic(Node node, int constId, int arity) { | 449 void invokeStatic(Node node, int constId, int arity) { |
450 assembler.invokeStatic(constId, arity); | 450 assembler.invokeStatic(constId, arity); |
451 } | 451 } |
452 | 452 |
(...skipping 21 matching lines...) Expand all Loading... |
474 assembler.dup(); | 474 assembler.dup(); |
475 int constId = allocateConstantFromNode(caseMatch.expression); | 475 int constId = allocateConstantFromNode(caseMatch.expression); |
476 assembler.loadConst(constId); | 476 assembler.loadConst(constId); |
477 // For debugging, ignore the equality checks in connection | 477 // For debugging, ignore the equality checks in connection |
478 // with case matches by not associating the calls with | 478 // with case matches by not associating the calls with |
479 // any node. | 479 // any node. |
480 invokeMethod(null, new Selector.binaryOperator('==')); | 480 invokeMethod(null, new Selector.binaryOperator('==')); |
481 assembler.branchIfTrue(ifTrue); | 481 assembler.branchIfTrue(ifTrue); |
482 } | 482 } |
483 | 483 |
484 FletchFunctionBase requireFunction(FunctionElement element) { | 484 DartinoFunctionBase requireFunction(FunctionElement element) { |
485 // TODO(johnniwinther): More precise use. | 485 // TODO(johnniwinther): More precise use. |
486 registerStaticUse(new StaticUse.foreignUse(element)); | 486 registerStaticUse(new StaticUse.foreignUse(element)); |
487 return context.backend.getFunctionForElement(element); | 487 return context.backend.getFunctionForElement(element); |
488 } | 488 } |
489 | 489 |
490 FletchFunctionBase requireConstructorInitializer( | 490 DartinoFunctionBase requireConstructorInitializer( |
491 ConstructorElement constructor) { | 491 ConstructorElement constructor) { |
492 assert(constructor.isGenerativeConstructor); | 492 assert(constructor.isGenerativeConstructor); |
493 registerInstantiatedClass(constructor.enclosingClass); | 493 registerInstantiatedClass(constructor.enclosingClass); |
494 registerStaticUse(new StaticUse.foreignUse(constructor)); | 494 registerStaticUse(new StaticUse.foreignUse(constructor)); |
495 return context.backend.getConstructorInitializerFunction(constructor); | 495 return context.backend.getConstructorInitializerFunction(constructor); |
496 } | 496 } |
497 | 497 |
498 void doStaticFunctionInvoke( | 498 void doStaticFunctionInvoke( |
499 Node node, | 499 Node node, |
500 FletchFunctionBase function, | 500 DartinoFunctionBase function, |
501 NodeList arguments, | 501 NodeList arguments, |
502 CallStructure callStructure, | 502 CallStructure callStructure, |
503 {bool factoryInvoke: false}) { | 503 {bool factoryInvoke: false}) { |
504 if (function.isInstanceMember) loadThis(); | 504 if (function.isInstanceMember) loadThis(); |
505 FunctionSignature signature = function.signature; | 505 FunctionSignature signature = function.signature; |
506 int functionId; | 506 int functionId; |
507 int arity; | 507 int arity; |
508 if (signature.hasOptionalParameters && | 508 if (signature.hasOptionalParameters && |
509 signature.optionalParametersAreNamed) { | 509 signature.optionalParametersAreNamed) { |
510 if (FletchBackend.isExactParameterMatch(signature, callStructure)) { | 510 if (DartinoBackend.isExactParameterMatch(signature, callStructure)) { |
511 functionId = function.functionId; | 511 functionId = function.functionId; |
512 } else if (callStructure.signatureApplies(signature)) { | 512 } else if (callStructure.signatureApplies(signature)) { |
513 // TODO(ajohnsen): Inline parameter stub? | 513 // TODO(ajohnsen): Inline parameter stub? |
514 FletchFunctionBase stub = context.backend.createParameterStub( | 514 DartinoFunctionBase stub = context.backend.createParameterStub( |
515 function, | 515 function, |
516 callStructure.callSelector); | 516 callStructure.callSelector); |
517 functionId = stub.functionId; | 517 functionId = stub.functionId; |
518 } else { | 518 } else { |
519 doUnresolved(function.name); | 519 doUnresolved(function.name); |
520 return; | 520 return; |
521 } | 521 } |
522 for (Node argument in arguments) { | 522 for (Node argument in arguments) { |
523 visitForValue(argument); | 523 visitForValue(argument); |
524 } | 524 } |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 assembler.pop(); | 935 assembler.pop(); |
936 assembler.loadLiteralTrue(); | 936 assembler.loadLiteralTrue(); |
937 return; | 937 return; |
938 } | 938 } |
939 | 939 |
940 if (type.isTypedef) { | 940 if (type.isTypedef) { |
941 // TODO(ajohnsen): This only matches with the number of arguments, not | 941 // TODO(ajohnsen): This only matches with the number of arguments, not |
942 // the actual argument types. | 942 // the actual argument types. |
943 TypedefType typedefType = type; | 943 TypedefType typedefType = type; |
944 int arity = typedefType.element.functionSignature.parameterCount; | 944 int arity = typedefType.element.functionSignature.parameterCount; |
945 int fletchSelector = context.toFletchIsSelector( | 945 int dartinoSelector = context.toDartinoIsSelector( |
946 context.backend.compiler.coreClasses.functionClass, arity); | 946 context.backend.compiler.coreClasses.functionClass, arity); |
947 assembler.invokeTest(fletchSelector, 0); | 947 assembler.invokeTest(dartinoSelector, 0); |
948 return; | 948 return; |
949 } | 949 } |
950 | 950 |
951 if (!type.isInterfaceType) { | 951 if (!type.isInterfaceType) { |
952 assembler.pop(); | 952 assembler.pop(); |
953 generateUnimplementedError( | 953 generateUnimplementedError( |
954 diagnosticLocation, "Unhandled type test for $type."); | 954 diagnosticLocation, "Unhandled type test for $type."); |
955 return; | 955 return; |
956 } | 956 } |
957 | 957 |
958 Element element = type.element; | 958 Element element = type.element; |
959 int fletchSelector = context.toFletchIsSelector(element); | 959 int dartinoSelector = context.toDartinoIsSelector(element); |
960 assembler.invokeTest(fletchSelector, 0); | 960 assembler.invokeTest(dartinoSelector, 0); |
961 } | 961 } |
962 | 962 |
963 void doIs( | 963 void doIs( |
964 Node node, | 964 Node node, |
965 Node expression, | 965 Node expression, |
966 DartType type, | 966 DartType type, |
967 // TODO(ahe): Remove [diagnosticLocation] when callIsSelector does not | 967 // TODO(ahe): Remove [diagnosticLocation] when callIsSelector does not |
968 // require it. | 968 // require it. |
969 Spannable diagnosticLocation) { | 969 Spannable diagnosticLocation) { |
970 visitForValue(expression); | 970 visitForValue(expression); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1017 visitForValue(argument); | 1017 visitForValue(argument); |
1018 } | 1018 } |
1019 generateIdentical(node); | 1019 generateIdentical(node); |
1020 } | 1020 } |
1021 | 1021 |
1022 void handleStaticFunctionGet( | 1022 void handleStaticFunctionGet( |
1023 Send node, | 1023 Send node, |
1024 MethodElement function, | 1024 MethodElement function, |
1025 _) { | 1025 _) { |
1026 registerClosurization(function, ClosureKind.tearOff); | 1026 registerClosurization(function, ClosureKind.tearOff); |
1027 FletchFunctionBase target = requireFunction(function); | 1027 DartinoFunctionBase target = requireFunction(function); |
1028 FletchClassBuilder classBuilder = | 1028 DartinoClassBuilder classBuilder = |
1029 context.backend.createTearoffClass(target); | 1029 context.backend.createTearoffClass(target); |
1030 assert(classBuilder.fields == 0); | 1030 assert(classBuilder.fields == 0); |
1031 int constId = allocateConstantClassInstance(classBuilder.classId); | 1031 int constId = allocateConstantClassInstance(classBuilder.classId); |
1032 assembler.loadConst(constId); | 1032 assembler.loadConst(constId); |
1033 applyVisitState(); | 1033 applyVisitState(); |
1034 } | 1034 } |
1035 | 1035 |
1036 void doMainCall(Send node, NodeList arguments) { | 1036 void doMainCall(Send node, NodeList arguments) { |
1037 FunctionElement function = context.compiler.mainFunction; | 1037 FunctionElement function = context.compiler.mainFunction; |
1038 if (function.isMalformed) { | 1038 if (function.isMalformed) { |
(...skipping 20 matching lines...) Expand all Loading... |
1059 int argumentCount = 0; | 1059 int argumentCount = 0; |
1060 for (Node argument in arguments) { | 1060 for (Node argument in arguments) { |
1061 if (argumentCount == parameterCount) break; | 1061 if (argumentCount == parameterCount) break; |
1062 visitForValue(argument); | 1062 visitForValue(argument); |
1063 argumentCount++; | 1063 argumentCount++; |
1064 } | 1064 } |
1065 for (int i = argumentCount; i < parameterCount; i++) { | 1065 for (int i = argumentCount; i < parameterCount; i++) { |
1066 assembler.loadLiteralNull(); | 1066 assembler.loadLiteralNull(); |
1067 } | 1067 } |
1068 | 1068 |
1069 FletchFunctionBase base = requireFunction(function); | 1069 DartinoFunctionBase base = requireFunction(function); |
1070 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 1070 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
1071 invokeStatic(node, constId, parameterCount); | 1071 invokeStatic(node, constId, parameterCount); |
1072 } | 1072 } |
1073 | 1073 |
1074 void doStaticallyBoundInvoke( | 1074 void doStaticallyBoundInvoke( |
1075 Send node, | 1075 Send node, |
1076 MethodElement element, | 1076 MethodElement element, |
1077 NodeList arguments, | 1077 NodeList arguments, |
1078 CallStructure callStructure) { | 1078 CallStructure callStructure) { |
1079 if (checkCompileError(element)) return; | 1079 if (checkCompileError(element)) return; |
1080 if (element.declaration == context.compiler.identicalFunction) { | 1080 if (element.declaration == context.compiler.identicalFunction) { |
1081 doIdenticalCall(node, arguments); | 1081 doIdenticalCall(node, arguments); |
1082 return; | 1082 return; |
1083 } | 1083 } |
1084 if (element.isExternal) { | 1084 if (element.isExternal) { |
1085 // Patch known functions directly. | 1085 // Patch known functions directly. |
1086 if (element == context.backend.fletchExternalInvokeMain) { | 1086 if (element == context.backend.dartinoExternalInvokeMain) { |
1087 doMainCall(node, arguments); | 1087 doMainCall(node, arguments); |
1088 return; | 1088 return; |
1089 } else if (element == context.backend.fletchExternalCoroutineChange) { | 1089 } else if (element == context.backend.dartinoExternalCoroutineChange) { |
1090 for (Node argument in arguments) { | 1090 for (Node argument in arguments) { |
1091 visitForValue(argument); | 1091 visitForValue(argument); |
1092 } | 1092 } |
1093 assembler.coroutineChange(); | 1093 assembler.coroutineChange(); |
1094 return; | 1094 return; |
1095 } | 1095 } |
1096 // TODO(ajohnsen): Define a known set of external functions we allow | 1096 // TODO(ajohnsen): Define a known set of external functions we allow |
1097 // calls to? | 1097 // calls to? |
1098 } | 1098 } |
1099 FletchFunctionBase target = requireFunction(element); | 1099 DartinoFunctionBase target = requireFunction(element); |
1100 doStaticFunctionInvoke(node, target, arguments, callStructure); | 1100 doStaticFunctionInvoke(node, target, arguments, callStructure); |
1101 } | 1101 } |
1102 | 1102 |
1103 void handleStaticFunctionInvoke( | 1103 void handleStaticFunctionInvoke( |
1104 Send node, | 1104 Send node, |
1105 MethodElement element, | 1105 MethodElement element, |
1106 NodeList arguments, | 1106 NodeList arguments, |
1107 CallStructure callStructure, | 1107 CallStructure callStructure, |
1108 _) { | 1108 _) { |
1109 doStaticallyBoundInvoke( | 1109 doStaticallyBoundInvoke( |
1110 node, element.declaration, arguments, callStructure); | 1110 node, element.declaration, arguments, callStructure); |
1111 applyVisitState(); | 1111 applyVisitState(); |
1112 } | 1112 } |
1113 | 1113 |
1114 void visitSuperMethodInvoke( | 1114 void visitSuperMethodInvoke( |
1115 Send node, | 1115 Send node, |
1116 MethodElement element, | 1116 MethodElement element, |
1117 NodeList arguments, | 1117 NodeList arguments, |
1118 CallStructure callStructure, | 1118 CallStructure callStructure, |
1119 _) { | 1119 _) { |
1120 doStaticallyBoundInvoke(node, element, arguments, callStructure); | 1120 doStaticallyBoundInvoke(node, element, arguments, callStructure); |
1121 applyVisitState(); | 1121 applyVisitState(); |
1122 } | 1122 } |
1123 | 1123 |
1124 void doSuperCall(Node node, FunctionElement function) { | 1124 void doSuperCall(Node node, FunctionElement function) { |
1125 registerStaticUse(new StaticUse.foreignUse(function)); | 1125 registerStaticUse(new StaticUse.foreignUse(function)); |
1126 int arity = function.functionSignature.parameterCount + 1; | 1126 int arity = function.functionSignature.parameterCount + 1; |
1127 FletchFunctionBase base = requireFunction(function); | 1127 DartinoFunctionBase base = requireFunction(function); |
1128 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 1128 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
1129 invokeStatic(node, constId, arity); | 1129 invokeStatic(node, constId, arity); |
1130 } | 1130 } |
1131 | 1131 |
1132 void visitSuperGetterGet( | 1132 void visitSuperGetterGet( |
1133 Send node, | 1133 Send node, |
1134 FunctionElement getter, | 1134 FunctionElement getter, |
1135 _) { | 1135 _) { |
1136 loadThis(); | 1136 loadThis(); |
1137 doSuperCall(node, getter); | 1137 doSuperCall(node, getter); |
1138 applyVisitState(); | 1138 applyVisitState(); |
1139 } | 1139 } |
1140 | 1140 |
1141 void visitSuperMethodGet( | 1141 void visitSuperMethodGet( |
1142 Send node, | 1142 Send node, |
1143 MethodElement method, | 1143 MethodElement method, |
1144 _) { | 1144 _) { |
1145 registerClosurization(method, ClosureKind.superTearOff); | 1145 registerClosurization(method, ClosureKind.superTearOff); |
1146 loadThis(); | 1146 loadThis(); |
1147 FletchFunctionBase target = requireFunction(method); | 1147 DartinoFunctionBase target = requireFunction(method); |
1148 FletchClassBuilder classBuilder = | 1148 DartinoClassBuilder classBuilder = |
1149 context.backend.createTearoffClass(target); | 1149 context.backend.createTearoffClass(target); |
1150 assert(classBuilder.fields == 1); | 1150 assert(classBuilder.fields == 1); |
1151 int constId = functionBuilder.allocateConstantFromClass( | 1151 int constId = functionBuilder.allocateConstantFromClass( |
1152 classBuilder.classId); | 1152 classBuilder.classId); |
1153 assembler.allocate(constId, classBuilder.fields); | 1153 assembler.allocate(constId, classBuilder.fields); |
1154 applyVisitState(); | 1154 applyVisitState(); |
1155 } | 1155 } |
1156 | 1156 |
1157 void visitSuperSetterSet( | 1157 void visitSuperSetterSet( |
1158 SendSet node, | 1158 SendSet node, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1272 FunctionElement function, | 1272 FunctionElement function, |
1273 _) { | 1273 _) { |
1274 loadThis(); | 1274 loadThis(); |
1275 doSuperCall(node, function); | 1275 doSuperCall(node, function); |
1276 applyVisitState(); | 1276 applyVisitState(); |
1277 } | 1277 } |
1278 | 1278 |
1279 int computeFieldIndex(FieldElement field) { | 1279 int computeFieldIndex(FieldElement field) { |
1280 ClassElement classElement = element.enclosingClass; | 1280 ClassElement classElement = element.enclosingClass; |
1281 int fieldIndex; | 1281 int fieldIndex; |
1282 FletchClassBuilder classBuilder; | 1282 DartinoClassBuilder classBuilder; |
1283 do { | 1283 do { |
1284 // We need to find the mixin application of the class, where the field | 1284 // We need to find the mixin application of the class, where the field |
1285 // is stored. Iterate until it's found. | 1285 // is stored. Iterate until it's found. |
1286 classBuilder = context.backend.registerClassElement(classElement); | 1286 classBuilder = context.backend.registerClassElement(classElement); |
1287 classElement = classElement.implementation; | 1287 classElement = classElement.implementation; |
1288 int i = 0; | 1288 int i = 0; |
1289 classElement.forEachInstanceField((_, FieldElement member) { | 1289 classElement.forEachInstanceField((_, FieldElement member) { |
1290 if (member == field) { | 1290 if (member == field) { |
1291 assert(fieldIndex == null); | 1291 assert(fieldIndex == null); |
1292 fieldIndex = i; | 1292 fieldIndex = i; |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1451 | 1451 |
1452 void visitDynamicPropertyGet( | 1452 void visitDynamicPropertyGet( |
1453 Send node, | 1453 Send node, |
1454 Node receiver, | 1454 Node receiver, |
1455 Name name, | 1455 Name name, |
1456 _) { | 1456 _) { |
1457 if (name.text == "runtimeType") { | 1457 if (name.text == "runtimeType") { |
1458 // TODO(ahe): Implement runtimeType. | 1458 // TODO(ahe): Implement runtimeType. |
1459 generateUnimplementedError( | 1459 generateUnimplementedError( |
1460 node, | 1460 node, |
1461 "'runtimeType' isn't supported in Fletch. See https://goo.gl/ELH6Zc"); | 1461 "'runtimeType' isn't supported in Dartino. See https://goo.gl/ELH6Zc")
; |
1462 applyVisitState(); | 1462 applyVisitState(); |
1463 return; | 1463 return; |
1464 } | 1464 } |
1465 visitForValue(receiver); | 1465 visitForValue(receiver); |
1466 invokeGetter(node, name); | 1466 invokeGetter(node, name); |
1467 applyVisitState(); | 1467 applyVisitState(); |
1468 } | 1468 } |
1469 | 1469 |
1470 void visitIfNotNullDynamicPropertyGet( | 1470 void visitIfNotNullDynamicPropertyGet( |
1471 Send node, | 1471 Send node, |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1694 } | 1694 } |
1695 ClassElement literalClass = | 1695 ClassElement literalClass = |
1696 context.backend.mapImplementation.implementation; | 1696 context.backend.mapImplementation.implementation; |
1697 ConstructorElement constructor = literalClass.lookupConstructor(""); | 1697 ConstructorElement constructor = literalClass.lookupConstructor(""); |
1698 if (constructor == null) { | 1698 if (constructor == null) { |
1699 internalError(literalClass, "Failed to lookup default map constructor"); | 1699 internalError(literalClass, "Failed to lookup default map constructor"); |
1700 return; | 1700 return; |
1701 } | 1701 } |
1702 // The default constructor is a redirecting factory constructor. Follow it. | 1702 // The default constructor is a redirecting factory constructor. Follow it. |
1703 constructor = constructor.effectiveTarget; | 1703 constructor = constructor.effectiveTarget; |
1704 FletchFunctionBase function = requireFunction(constructor.declaration); | 1704 DartinoFunctionBase function = requireFunction(constructor.declaration); |
1705 doStaticFunctionInvoke( | 1705 doStaticFunctionInvoke( |
1706 node, | 1706 node, |
1707 function, | 1707 function, |
1708 new NodeList.empty(), | 1708 new NodeList.empty(), |
1709 CallStructure.NO_ARGS, | 1709 CallStructure.NO_ARGS, |
1710 factoryInvoke: true); | 1710 factoryInvoke: true); |
1711 | 1711 |
1712 Selector selector = new Selector.indexSet(); | 1712 Selector selector = new Selector.indexSet(); |
1713 for (Node element in node.entries) { | 1713 for (Node element in node.entries) { |
1714 assembler.dup(); | 1714 assembler.dup(); |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2216 // TODO(ahe): It seems suboptimal that each throw is followed by a pop. | 2216 // TODO(ahe): It seems suboptimal that each throw is followed by a pop. |
2217 generateThrow(node); | 2217 generateThrow(node); |
2218 } | 2218 } |
2219 assembler.pop(); | 2219 assembler.pop(); |
2220 } | 2220 } |
2221 | 2221 |
2222 void callConstructor(Node node, | 2222 void callConstructor(Node node, |
2223 ConstructorElement constructor, | 2223 ConstructorElement constructor, |
2224 NodeList arguments, | 2224 NodeList arguments, |
2225 CallStructure callStructure) { | 2225 CallStructure callStructure) { |
2226 FletchFunctionBase function = requireConstructorInitializer(constructor); | 2226 DartinoFunctionBase function = requireConstructorInitializer(constructor); |
2227 doStaticFunctionInvoke(node, function, arguments, callStructure); | 2227 doStaticFunctionInvoke(node, function, arguments, callStructure); |
2228 } | 2228 } |
2229 | 2229 |
2230 void doConstConstructorInvoke(ConstantExpression constant) { | 2230 void doConstConstructorInvoke(ConstantExpression constant) { |
2231 var value = context.getConstantValue(constant); | 2231 var value = context.getConstantValue(constant); |
2232 context.markConstantUsed(value); | 2232 context.markConstantUsed(value); |
2233 int constId = functionBuilder.allocateConstant(value); | 2233 int constId = functionBuilder.allocateConstant(value); |
2234 assembler.loadConst(constId); | 2234 assembler.loadConst(constId); |
2235 } | 2235 } |
2236 | 2236 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2300 constructor, | 2300 constructor, |
2301 type, | 2301 type, |
2302 implementation.effectiveTarget, | 2302 implementation.effectiveTarget, |
2303 null, | 2303 null, |
2304 arguments, | 2304 arguments, |
2305 callStructure, | 2305 callStructure, |
2306 null); | 2306 null); |
2307 return; | 2307 return; |
2308 } | 2308 } |
2309 // TODO(ahe): Remove ".declaration" when issue 23135 is fixed. | 2309 // TODO(ahe): Remove ".declaration" when issue 23135 is fixed. |
2310 FletchFunctionBase function = requireFunction(constructor.declaration); | 2310 DartinoFunctionBase function = requireFunction(constructor.declaration); |
2311 doStaticFunctionInvoke( | 2311 doStaticFunctionInvoke( |
2312 node, function, arguments, callStructure, factoryInvoke: true); | 2312 node, function, arguments, callStructure, factoryInvoke: true); |
2313 applyVisitState(); | 2313 applyVisitState(); |
2314 } | 2314 } |
2315 | 2315 |
2316 void visitConstructorIncompatibleInvoke( | 2316 void visitConstructorIncompatibleInvoke( |
2317 NewExpression node, | 2317 NewExpression node, |
2318 ConstructorElement constructor, | 2318 ConstructorElement constructor, |
2319 InterfaceType type, | 2319 InterfaceType type, |
2320 NodeList arguments, | 2320 NodeList arguments, |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2410 ConstructorElement constructor, | 2410 ConstructorElement constructor, |
2411 InterfaceType type, | 2411 InterfaceType type, |
2412 NodeList arguments, | 2412 NodeList arguments, |
2413 CallStructure callStructure, | 2413 CallStructure callStructure, |
2414 _) { | 2414 _) { |
2415 doUnresolved(node.send.toString()); | 2415 doUnresolved(node.send.toString()); |
2416 applyVisitState(); | 2416 applyVisitState(); |
2417 } | 2417 } |
2418 | 2418 |
2419 void doStaticGetterGet(Send node, FunctionElement getter) { | 2419 void doStaticGetterGet(Send node, FunctionElement getter) { |
2420 if (getter == context.backend.fletchExternalNativeError) { | 2420 if (getter == context.backend.dartinoExternalNativeError) { |
2421 assembler.loadSlot(0); | 2421 assembler.loadSlot(0); |
2422 return; | 2422 return; |
2423 } | 2423 } |
2424 | 2424 |
2425 if (getter.isDeferredLoaderGetter) { | 2425 if (getter.isDeferredLoaderGetter) { |
2426 generateUnimplementedError(node, "Deferred loading is not supported."); | 2426 generateUnimplementedError(node, "Deferred loading is not supported."); |
2427 return; | 2427 return; |
2428 } | 2428 } |
2429 | 2429 |
2430 FletchFunctionBase base = requireFunction(getter); | 2430 DartinoFunctionBase base = requireFunction(getter); |
2431 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 2431 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
2432 invokeStatic(node, constId, 0); | 2432 invokeStatic(node, constId, 0); |
2433 } | 2433 } |
2434 | 2434 |
2435 void handleStaticGetterGet(Send node, FunctionElement getter, _) { | 2435 void handleStaticGetterGet(Send node, FunctionElement getter, _) { |
2436 doStaticGetterGet(node, getter); | 2436 doStaticGetterGet(node, getter); |
2437 applyVisitState(); | 2437 applyVisitState(); |
2438 } | 2438 } |
2439 | 2439 |
2440 void handleStaticGetterInvoke( | 2440 void handleStaticGetterInvoke( |
2441 Send node, | 2441 Send node, |
2442 FunctionElement getter, | 2442 FunctionElement getter, |
2443 NodeList arguments, | 2443 NodeList arguments, |
2444 CallStructure callStructure, | 2444 CallStructure callStructure, |
2445 _) { | 2445 _) { |
2446 doStaticGetterGet(node, getter); | 2446 doStaticGetterGet(node, getter); |
2447 for (Node argument in arguments) { | 2447 for (Node argument in arguments) { |
2448 visitForValue(argument); | 2448 visitForValue(argument); |
2449 } | 2449 } |
2450 invokeMethod(node, callStructure.callSelector); | 2450 invokeMethod(node, callStructure.callSelector); |
2451 applyVisitState(); | 2451 applyVisitState(); |
2452 } | 2452 } |
2453 | 2453 |
2454 void handleStaticSetterSet( | 2454 void handleStaticSetterSet( |
2455 Send node, | 2455 Send node, |
2456 FunctionElement setter, | 2456 FunctionElement setter, |
2457 Node rhs, | 2457 Node rhs, |
2458 _) { | 2458 _) { |
2459 visitForValue(rhs); | 2459 visitForValue(rhs); |
2460 FletchFunctionBase base = requireFunction(setter); | 2460 DartinoFunctionBase base = requireFunction(setter); |
2461 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 2461 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
2462 invokeStatic(node, constId, 1); | 2462 invokeStatic(node, constId, 1); |
2463 applyVisitState(); | 2463 applyVisitState(); |
2464 } | 2464 } |
2465 | 2465 |
2466 /** | 2466 /** |
2467 * Load the captured variables of [function], expressed in [info]. | 2467 * Load the captured variables of [function], expressed in [info]. |
2468 * | 2468 * |
2469 * If [function] captures itself, its field index is returned. | 2469 * If [function] captures itself, its field index is returned. |
2470 */ | 2470 */ |
(...skipping 28 matching lines...) Expand all Loading... |
2499 } | 2499 } |
2500 | 2500 |
2501 void visitFunctionExpression(FunctionExpression node) { | 2501 void visitFunctionExpression(FunctionExpression node) { |
2502 FunctionElement function = elements[node]; | 2502 FunctionElement function = elements[node]; |
2503 | 2503 |
2504 // If the closure captures itself, thisClosureIndex is the field-index in | 2504 // If the closure captures itself, thisClosureIndex is the field-index in |
2505 // the closure. | 2505 // the closure. |
2506 int thisClosureIndex = pushCapturedVariables(function); | 2506 int thisClosureIndex = pushCapturedVariables(function); |
2507 bool needToStoreThisReference = thisClosureIndex >= 0; | 2507 bool needToStoreThisReference = thisClosureIndex >= 0; |
2508 | 2508 |
2509 FletchClassBuilder classBuilder = context.backend.createClosureClass( | 2509 DartinoClassBuilder classBuilder = context.backend.createClosureClass( |
2510 function, | 2510 function, |
2511 closureEnvironment); | 2511 closureEnvironment); |
2512 int classConstant = functionBuilder.allocateConstantFromClass( | 2512 int classConstant = functionBuilder.allocateConstantFromClass( |
2513 classBuilder.classId); | 2513 classBuilder.classId); |
2514 | 2514 |
2515 // NOTE: Currently we emit a storeField instruction in case a closure | 2515 // NOTE: Currently we emit a storeField instruction in case a closure |
2516 // captures itself. Changing fields makes it a mutable object. | 2516 // captures itself. Changing fields makes it a mutable object. |
2517 // We can therefore not allocate the object with `immutable = true`. | 2517 // We can therefore not allocate the object with `immutable = true`. |
2518 // TODO(fletchc-team): Could we restrict this limitation. | 2518 // TODO(dartino_compiler-team): Could we restrict this limitation. |
2519 bool immutable = !closureEnvironment.closures[function].free.any( | 2519 bool immutable = !closureEnvironment.closures[function].free.any( |
2520 closureEnvironment.shouldBeBoxed) && !needToStoreThisReference; | 2520 closureEnvironment.shouldBeBoxed) && !needToStoreThisReference; |
2521 | 2521 |
2522 assembler.allocate( | 2522 assembler.allocate( |
2523 classConstant, classBuilder.fields, immutable: immutable); | 2523 classConstant, classBuilder.fields, immutable: immutable); |
2524 | 2524 |
2525 if (needToStoreThisReference) { | 2525 if (needToStoreThisReference) { |
2526 assert(!immutable); | 2526 assert(!immutable); |
2527 assembler.dup(); | 2527 assembler.dup(); |
2528 assembler.storeField(thisClosureIndex); | 2528 assembler.storeField(thisClosureIndex); |
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3055 | 3055 |
3056 // Pop exception slot. | 3056 // Pop exception slot. |
3057 assembler.pop(); | 3057 assembler.pop(); |
3058 } | 3058 } |
3059 | 3059 |
3060 void doUnresolved(String name) { | 3060 void doUnresolved(String name) { |
3061 var constString = context.backend.constantSystem.createString( | 3061 var constString = context.backend.constantSystem.createString( |
3062 new DartString.literal(name)); | 3062 new DartString.literal(name)); |
3063 context.markConstantUsed(constString); | 3063 context.markConstantUsed(constString); |
3064 assembler.loadConst(functionBuilder.allocateConstant(constString)); | 3064 assembler.loadConst(functionBuilder.allocateConstant(constString)); |
3065 FunctionElement function = context.backend.fletchUnresolved; | 3065 FunctionElement function = context.backend.dartinoUnresolved; |
3066 FletchFunctionBase base = requireFunction(function); | 3066 DartinoFunctionBase base = requireFunction(function); |
3067 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 3067 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
3068 assembler.invokeStatic(constId, 1); | 3068 assembler.invokeStatic(constId, 1); |
3069 } | 3069 } |
3070 | 3070 |
3071 bool checkCompileError(Element element) { | 3071 bool checkCompileError(Element element) { |
3072 DiagnosticMessage message = | 3072 DiagnosticMessage message = |
3073 context.compiler.elementsWithCompileTimeErrors[element]; | 3073 context.compiler.elementsWithCompileTimeErrors[element]; |
3074 if (message != null) { | 3074 if (message != null) { |
3075 doCompileError(message); | 3075 doCompileError(message); |
3076 return true; | 3076 return true; |
3077 } | 3077 } |
3078 return false; | 3078 return false; |
3079 } | 3079 } |
3080 | 3080 |
3081 String formatError(DiagnosticMessage diagnosticMessage) { | 3081 String formatError(DiagnosticMessage diagnosticMessage) { |
3082 return diagnosticMessage.message.computeMessage(); | 3082 return diagnosticMessage.message.computeMessage(); |
3083 } | 3083 } |
3084 | 3084 |
3085 | 3085 |
3086 void doCompileError(DiagnosticMessage errorMessage) { | 3086 void doCompileError(DiagnosticMessage errorMessage) { |
3087 FunctionElement function = context.backend.fletchCompileError; | 3087 FunctionElement function = context.backend.dartinoCompileError; |
3088 FletchFunctionBase base = requireFunction(function); | 3088 DartinoFunctionBase base = requireFunction(function); |
3089 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); | 3089 int constId = functionBuilder.allocateConstantFromFunction(base.functionId); |
3090 String errorString = formatError(errorMessage); | 3090 String errorString = formatError(errorMessage); |
3091 ConstantValue stringConstant = | 3091 ConstantValue stringConstant = |
3092 context.backend.constantSystem.createString( | 3092 context.backend.constantSystem.createString( |
3093 new DartString.literal(errorString)); | 3093 new DartString.literal(errorString)); |
3094 int messageConstId = functionBuilder.allocateConstant(stringConstant); | 3094 int messageConstId = functionBuilder.allocateConstant(stringConstant); |
3095 context.markConstantUsed(stringConstant); | 3095 context.markConstantUsed(stringConstant); |
3096 assembler.loadConst(messageConstId); | 3096 assembler.loadConst(messageConstId); |
3097 registerInstantiatedClass(context.backend.stringImplementation); | 3097 registerInstantiatedClass(context.backend.stringImplementation); |
3098 assembler.invokeStatic(constId, 1); | 3098 assembler.invokeStatic(constId, 1); |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3238 | 3238 |
3239 void applyInitializers(FunctionExpression initializers, _) { | 3239 void applyInitializers(FunctionExpression initializers, _) { |
3240 internalError(initializers, "[applyInitializers] isn't implemented."); | 3240 internalError(initializers, "[applyInitializers] isn't implemented."); |
3241 } | 3241 } |
3242 | 3242 |
3243 void applyParameters(NodeList parameters, _) { | 3243 void applyParameters(NodeList parameters, _) { |
3244 internalError(parameters, "[applyParameters] isn't implemented."); | 3244 internalError(parameters, "[applyParameters] isn't implemented."); |
3245 } | 3245 } |
3246 } | 3246 } |
3247 | 3247 |
3248 abstract class FletchRegistryMixin { | 3248 abstract class DartinoRegistryMixin { |
3249 FletchRegistry get registry; | 3249 DartinoRegistry get registry; |
3250 FletchContext get context; | 3250 DartinoContext get context; |
3251 | 3251 |
3252 void registerDynamicUse(Selector selector) { | 3252 void registerDynamicUse(Selector selector) { |
3253 registry.registerDynamicUse(selector); | 3253 registry.registerDynamicUse(selector); |
3254 } | 3254 } |
3255 | 3255 |
3256 void registerStaticUse(StaticUse staticUse) { | 3256 void registerStaticUse(StaticUse staticUse) { |
3257 registry.registerStaticUse(staticUse); | 3257 registry.registerStaticUse(staticUse); |
3258 } | 3258 } |
3259 | 3259 |
3260 void registerInstantiatedClass(ClassElement klass) { | 3260 void registerInstantiatedClass(ClassElement klass) { |
(...skipping 16 matching lines...) Expand all Loading... |
3277 // is a getter. This happens for both named and unnamed. | 3277 // is a getter. This happens for both named and unnamed. |
3278 registerStaticUse(new StaticUse.foreignUse(element)); | 3278 registerStaticUse(new StaticUse.foreignUse(element)); |
3279 } | 3279 } |
3280 registry.registerClosurization(element, kind); | 3280 registry.registerClosurization(element, kind); |
3281 } | 3281 } |
3282 | 3282 |
3283 int compileLazyFieldInitializer(FieldElement field) { | 3283 int compileLazyFieldInitializer(FieldElement field) { |
3284 return context.backend.compileLazyFieldInitializer(field, registry); | 3284 return context.backend.compileLazyFieldInitializer(field, registry); |
3285 } | 3285 } |
3286 } | 3286 } |
OLD | NEW |