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

Side by Side Diff: lib/compiler/implementation/js_backend/backend.dart

Issue 10964016: Change the type inference for fields in dart2js (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Rebased to r12709 Created 8 years, 3 months 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 typedef void Recompile(Element element); 5 typedef void Recompile(Element element);
6 6
7 class ReturnInfo { 7 class ReturnInfo {
8 HType returnType; 8 HType returnType;
9 List<Element> compiledFunctions; 9 List<Element> compiledFunctions;
10 10
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 } 216 }
217 index++; 217 index++;
218 }); 218 });
219 return result; 219 return result;
220 } 220 }
221 221
222 String toString() => 222 String toString() =>
223 allUnknown ? "HTypeList.ALL_UNKNOWN" : "HTypeList $types"; 223 allUnknown ? "HTypeList.ALL_UNKNOWN" : "HTypeList $types";
224 } 224 }
225 225
226 class FieldTypesRegistry {
227 final JavaScriptBackend backend;
228 final Map<Element, HType> fieldInitializerTypeMap;
229 final Map<Element, HType> fieldConstructorTypeMap;
230 final Map<Element, HType> fieldTypeMap;
231 final Map<Element, FunctionSet> optimizedFunctions;
232
233 FieldTypesRegistry(JavaScriptBackend backend)
234 : fieldInitializerTypeMap = new Map<Element, HType>(),
235 fieldConstructorTypeMap = new Map<Element, HType>(),
236 fieldTypeMap = new Map<Element, HType>(),
237 optimizedFunctions = new Map<Element, FunctionSet>(),
238 this.backend = backend;
239
240 Compiler get compiler => backend.compiler;
241
242 void scheduleRecompilation(Element field) {
243 FunctionSet optimized = optimizedFunctions[field];
244 if (optimized != null) {
245 optimized.forEach(backend.scheduleForRecompilation);
246 }
247 optimizedFunctions.remove(field);
248 }
249
250 void registerFieldType(Map<Element, HType> typeMap,
251 Element field,
252 HType type) {
253 assert(field.isField());
254 HType oldType = typeMap[field];
255 HType newType;
256
257 if (oldType != null) {
258 newType = oldType.union(type);
259 } else {
260 newType = type;
261 }
262 typeMap[field] = newType;
263 if (oldType != newType) {
264 scheduleRecompilation(field);
265 }
266 }
267
268 void registerFieldInitializer(Element field, HType type) {
269 registerFieldType(fieldInitializerTypeMap, field, type);
270 }
271
272 void registerFieldConstructor(Element field, HType type) {
273 registerFieldType(fieldConstructorTypeMap, field, type);
274 }
275
276 void registerFieldSetter(HFieldSet node, HTypeMap types) {
277 Element field = node.element;
278 HType initializerType = fieldInitializerTypeMap[field];
279 HType constructorType = fieldConstructorTypeMap[field];
280 HType setterType = fieldTypeMap[field];
281 HType type = types[node.value];
282 // No need to register UNKONWN if there is currently no type information
283 // present for the field.
284 if (type == HType.UNKNOWN &&
285 initializerType == null &&
286 constructorType == null &&
287 setterType == null) {
288 return;
289 }
290 registerFieldType(fieldTypeMap, field, type);
291 }
292
293 void addedDynamicSetter(Selector setter) {
294 optimizedFunctions.forEach((Element field, _) {
295 if (field.name == setter.name) {
296 if (compiler.codegenWorld.hasInvokedSetter(field, compiler)) {
ngeoffray 2012/09/24 08:15:49 Why do you need the hasInvokedSetter check? The se
Søren Gjesse 2012/09/24 15:06:50 Changed this to have a local selector collection.
297 scheduleRecompilation(field);
ngeoffray 2012/09/24 08:15:49 So you invalidate even though the type may not hav
Søren Gjesse 2012/09/24 15:06:50 I will postpone taking the type of the dynamic set
298 }
299 }
300 });
301 }
302
303 HType optimisticFieldType(Element field) {
304 assert(field.isField());
305 assert(field.isMember());
306 if (compiler.codegenWorld.hasInvokedSetter(field, compiler)) {
ngeoffray 2012/09/24 08:15:49 Isn't this too pessimistic? Maybe you should mark
Søren Gjesse 2012/09/24 15:06:50 See comments above.
307 return HType.UNKNOWN;
308 }
309 HType initializerType = fieldInitializerTypeMap[field];
310 HType constructorType = fieldConstructorTypeMap[field];
311 if (initializerType === null && constructorType === null) {
312 return HType.UNKNOWN;
313 }
314 HType result = constructorType != null ? constructorType : initializerType;
315 HType type = fieldTypeMap[field];
316 if (type !== null) result = result.union(type);
317 return result;
318 }
319
320 void registerOptimizedFunction(FunctionElement element,
321 Element field,
322 HType type) {
323 assert(field.isField());
324 optimizedFunctions.putIfAbsent(
325 field, () => new FunctionSet(backend.compiler));
326 optimizedFunctions[field].add(element);
327 }
328
329 void dump() {
330 Set<Element> allFields = new Set<Element>();
331 fieldInitializerTypeMap.getKeys().forEach(allFields.add);
332 fieldConstructorTypeMap.getKeys().forEach(allFields.add);
333 fieldTypeMap.getKeys().forEach(allFields.add);
334 allFields.forEach((Element field) {
335 print("Inferred $field has type ${optimisticFieldType(field)}");
336 });
337 }
338 }
339
226 class ArgumentTypesRegistry { 340 class ArgumentTypesRegistry {
227 final JavaScriptBackend backend; 341 final JavaScriptBackend backend;
228 342
229 /** 343 /**
230 * Documentation wanted -- johnniwinther 344 * Documentation wanted -- johnniwinther
231 * 345 *
232 * Invariant: Keys must be declaration elements. 346 * Invariant: Keys must be declaration elements.
233 */ 347 */
234 final Map<Element, HTypeList> staticTypeMap; 348 final Map<Element, HTypeList> staticTypeMap;
235 349
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 signature, 492 signature,
379 defaultValueTypes); 493 defaultValueTypes);
380 } 494 }
381 assert(types.allUnknown || types.length == signature.parameterCount); 495 assert(types.allUnknown || types.length == signature.parameterCount);
382 found = (found === null) ? types : found.union(types); 496 found = (found === null) ? types : found.union(types);
383 return !found.allUnknown; 497 return !found.allUnknown;
384 }); 498 });
385 return found !== null ? found : HTypeList.ALL_UNKNOWN; 499 return found !== null ? found : HTypeList.ALL_UNKNOWN;
386 } 500 }
387 501
388 void registerOptimization(Element element, 502 void registerOptimizedFunction(Element element,
389 HTypeList parameterTypes, 503 HTypeList parameterTypes,
390 OptionalParameterTypes defaultValueTypes) { 504 OptionalParameterTypes defaultValueTypes) {
391 assert(invariant(element, element.isDeclaration));
392 if (Elements.isStaticOrTopLevelFunction(element)) { 505 if (Elements.isStaticOrTopLevelFunction(element)) {
393 if (parameterTypes.allUnknown) { 506 if (parameterTypes.allUnknown) {
394 optimizedStaticFunctions.remove(element); 507 optimizedStaticFunctions.remove(element);
395 } else { 508 } else {
396 optimizedStaticFunctions.add(element); 509 optimizedStaticFunctions.add(element);
397 } 510 }
398 } 511 }
399 512
400 // TODO(kasperl): What kind of non-members do we get here? 513 // TODO(kasperl): What kind of non-members do we get here?
401 if (!element.isMember()) return; 514 if (!element.isMember()) return;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 549
437 final Namer namer; 550 final Namer namer;
438 551
439 /** 552 /**
440 * Interface used to determine if an object has the JavaScript 553 * Interface used to determine if an object has the JavaScript
441 * indexing behavior. The interface is only visible to specific 554 * indexing behavior. The interface is only visible to specific
442 * libraries. 555 * libraries.
443 */ 556 */
444 ClassElement jsIndexingBehaviorInterface; 557 ClassElement jsIndexingBehaviorInterface;
445 558
446 final Map<Element, Map<Element, HType>> fieldInitializers;
447 final Map<Element, Map<Element, HType>> fieldConstructorSetters;
448 final Map<Element, Map<Element, HType>> fieldSettersType;
449
450 final Map<Element, ReturnInfo> returnInfo; 559 final Map<Element, ReturnInfo> returnInfo;
451 560
452 /** 561 /**
453 * Documentation wanted -- johnniwinther 562 * Documentation wanted -- johnniwinther
454 * 563 *
455 * Invariant: Elements must be declaration elements. 564 * Invariant: Elements must be declaration elements.
456 */ 565 */
457 final List<Element> invalidateAfterCodegen; 566 final List<Element> invalidateAfterCodegen;
458 ArgumentTypesRegistry argumentTypes; 567 ArgumentTypesRegistry argumentTypes;
568 FieldTypesRegistry fieldTypes;
459 569
460 List<CompilerTask> get tasks { 570 List<CompilerTask> get tasks {
461 return <CompilerTask>[builder, optimizer, generator, emitter]; 571 return <CompilerTask>[builder, optimizer, generator, emitter];
462 } 572 }
463 573
464 JavaScriptBackend(Compiler compiler, bool generateSourceMap) 574 JavaScriptBackend(Compiler compiler, bool generateSourceMap)
465 : fieldInitializers = new Map<Element, Map<Element, HType>>(), 575 : namer = new Namer(compiler),
466 fieldConstructorSetters = new Map<Element, Map<Element, HType>>(),
467 fieldSettersType = new Map<Element, Map<Element, HType>>(),
468 namer = new Namer(compiler),
469 returnInfo = new Map<Element, ReturnInfo>(), 576 returnInfo = new Map<Element, ReturnInfo>(),
470 invalidateAfterCodegen = new List<Element>(), 577 invalidateAfterCodegen = new List<Element>(),
471 super(compiler, constantSystem: JAVA_SCRIPT_CONSTANT_SYSTEM) { 578 super(compiler, constantSystem: JAVA_SCRIPT_CONSTANT_SYSTEM) {
472 emitter = new CodeEmitterTask(compiler, namer, generateSourceMap); 579 emitter = new CodeEmitterTask(compiler, namer, generateSourceMap);
473 builder = new SsaBuilderTask(this); 580 builder = new SsaBuilderTask(this);
474 optimizer = new SsaOptimizerTask(this); 581 optimizer = new SsaOptimizerTask(this);
475 generator = new SsaCodeGeneratorTask(this); 582 generator = new SsaCodeGeneratorTask(this);
476 argumentTypes = new ArgumentTypesRegistry(this); 583 argumentTypes = new ArgumentTypesRegistry(this);
584 fieldTypes = new FieldTypesRegistry(this);
477 } 585 }
478 586
479 Element get cyclicThrowHelper { 587 Element get cyclicThrowHelper {
480 return compiler.findHelper(const SourceString("throwCyclicInit")); 588 return compiler.findHelper(const SourceString("throwCyclicInit"));
481 } 589 }
482 590
483 JavaScriptItemCompilationContext createItemCompilationContext() { 591 JavaScriptItemCompilationContext createItemCompilationContext() {
484 return new JavaScriptItemCompilationContext(); 592 return new JavaScriptItemCompilationContext();
485 } 593 }
486 594
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 640
533 void processNativeClasses(Enqueuer world, 641 void processNativeClasses(Enqueuer world,
534 Collection<LibraryElement> libraries) { 642 Collection<LibraryElement> libraries) {
535 native.processNativeClasses(world, emitter, libraries); 643 native.processNativeClasses(world, emitter, libraries);
536 } 644 }
537 645
538 void assembleProgram() { 646 void assembleProgram() {
539 emitter.assembleProgram(); 647 emitter.assembleProgram();
540 } 648 }
541 649
542 void updateFieldInitializers(Element field, HType propagatedType) {
543 assert(field.isField());
544 assert(field.isMember());
545 Map<Element, HType> fields =
546 fieldInitializers.putIfAbsent(
547 field.getEnclosingClass(), () => new Map<Element, HType>());
548 if (!fields.containsKey(field)) {
549 fields[field] = propagatedType;
550 } else {
551 fields[field] = fields[field].union(propagatedType);
552 }
553 }
554
555 HType typeFromInitializersSoFar(Element field) {
556 assert(field.isField());
557 assert(field.isMember());
558 if (!fieldInitializers.containsKey(field.getEnclosingClass())) {
559 return HType.CONFLICTING;
560 }
561 Map<Element, HType> fields = fieldInitializers[field.getEnclosingClass()];
562 return fields[field];
563 }
564
565 void updateFieldConstructorSetters(Element field, HType type) {
566 assert(field.isField());
567 assert(field.isMember());
568 Map<Element, HType> fields =
569 fieldConstructorSetters.putIfAbsent(
570 field.getEnclosingClass(), () => new Map<Element, HType>());
571 if (!fields.containsKey(field)) {
572 fields[field] = type;
573 } else {
574 fields[field] = fields[field].union(type);
575 }
576 }
577
578 // Check if this field is set in the constructor body.
579 bool hasConstructorBodyFieldSetter(Element field) {
580 ClassElement enclosingClass = field.getEnclosingClass();
581 if (!fieldConstructorSetters.containsKey(enclosingClass)) {
582 return false;
583 }
584 return fieldConstructorSetters[enclosingClass][field] != null;
585 }
586
587 // Provide an optimistic estimate of the type of a field after construction.
588 // If the constructor body has setters for fields returns HType.UNKNOWN.
589 // This only takes the initializer lists and field assignments in the
590 // constructor body into account. The constructor body might have method calls
591 // that could alter the field.
592 HType optimisticFieldTypeAfterConstruction(Element field) {
593 assert(field.isField());
594 assert(field.isMember());
595
596 ClassElement classElement = field.getEnclosingClass();
597 if (hasConstructorBodyFieldSetter(field)) {
598 // If there are field setters but there is only constructor then the type
599 // of the field is determined by the assignments in the constructor
600 // body.
601 var constructors = classElement.constructors;
602 if (constructors.head !== null && constructors.tail.isEmpty()) {
603 return fieldConstructorSetters[classElement][field];
604 } else {
605 return HType.UNKNOWN;
606 }
607 } else if (fieldInitializers.containsKey(classElement)) {
608 HType type = fieldInitializers[classElement][field];
609 return type == null ? HType.CONFLICTING : type;
610 } else {
611 return HType.CONFLICTING;
612 }
613 }
614
615 void updateFieldSetters(Element field, HType type) {
616 assert(field.isField());
617 assert(field.isMember());
618 Map<Element, HType> fields =
619 fieldSettersType.putIfAbsent(
620 field.getEnclosingClass(), () => new Map<Element, HType>());
621 if (!fields.containsKey(field)) {
622 fields[field] = type;
623 } else {
624 fields[field] = fields[field].union(type);
625 }
626 }
627
628 // Returns the type that field setters are setting the field to based on what
629 // have been seen during compilation so far.
630 HType fieldSettersTypeSoFar(Element field) {
631 assert(field.isField());
632 assert(field.isMember());
633 ClassElement enclosingClass = field.getEnclosingClass();
634 if (!fieldSettersType.containsKey(enclosingClass)) {
635 return HType.CONFLICTING;
636 }
637 Map<Element, HType> fields = fieldSettersType[enclosingClass];
638 if (!fields.containsKey(field)) return HType.CONFLICTING;
639 return fields[field];
640 }
641
642 /** 650 /**
643 * Documentation wanted -- johnniwinther 651 * Documentation wanted -- johnniwinther
644 * 652 *
645 * Invariant: [element] must be a declaration element. 653 * Invariant: [element] must be a declaration element.
646 */ 654 */
647 void scheduleForRecompilation(Element element) { 655 void scheduleForRecompilation(Element element) {
648 assert(invariant(element, element.isDeclaration)); 656 assert(invariant(element, element.isDeclaration));
649 if (compiler.phase == Compiler.PHASE_COMPILING) { 657 if (compiler.phase == Compiler.PHASE_COMPILING) {
650 invalidateAfterCodegen.add(element); 658 invalidateAfterCodegen.add(element);
651 } 659 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 * scheduled for recompilation. 708 * scheduled for recompilation.
701 * 709 *
702 * Invariant: [element] must be a declaration element. 710 * Invariant: [element] must be a declaration element.
703 */ 711 */
704 registerParameterTypesOptimization( 712 registerParameterTypesOptimization(
705 FunctionElement element, 713 FunctionElement element,
706 HTypeList parameterTypes, 714 HTypeList parameterTypes,
707 OptionalParameterTypes defaultValueTypes) { 715 OptionalParameterTypes defaultValueTypes) {
708 assert(invariant(element, element.isDeclaration)); 716 assert(invariant(element, element.isDeclaration));
709 if (element.parameterCount(compiler) == 0) return; 717 if (element.parameterCount(compiler) == 0) return;
710 argumentTypes.registerOptimization( 718 argumentTypes.registerOptimizedFunction(
711 element, parameterTypes, defaultValueTypes); 719 element, parameterTypes, defaultValueTypes);
712 } 720 }
713 721
722 registerFieldTypesOptimization(FunctionElement element,
723 Element field,
724 HType type) {
725 fieldTypes.registerOptimizedFunction(element, field, type);
726 }
727
714 /** 728 /**
715 * Documentation wanted -- johnniwinther 729 * Documentation wanted -- johnniwinther
716 * 730 *
717 * Invariant: [element] must be a declaration element. 731 * Invariant: [element] must be a declaration element.
718 */ 732 */
719 void registerReturnType(FunctionElement element, HType returnType) { 733 void registerReturnType(FunctionElement element, HType returnType) {
720 assert(invariant(element, element.isDeclaration)); 734 assert(invariant(element, element.isDeclaration));
721 ReturnInfo info = returnInfo[element]; 735 ReturnInfo info = returnInfo[element];
722 if (info != null) { 736 if (info != null) {
723 info.update(returnType, scheduleForRecompilation); 737 info.update(returnType, scheduleForRecompilation);
(...skipping 24 matching lines...) Expand all
748 } 762 }
749 763
750 void dumpReturnTypes() { 764 void dumpReturnTypes() {
751 returnInfo.forEach((Element element, ReturnInfo info) { 765 returnInfo.forEach((Element element, ReturnInfo info) {
752 if (info.returnType != HType.UNKNOWN) { 766 if (info.returnType != HType.UNKNOWN) {
753 print("Inferred $element has return type ${info.returnType}"); 767 print("Inferred $element has return type ${info.returnType}");
754 } 768 }
755 }); 769 });
756 } 770 }
757 771
772 void registerFieldInitializer(Element field, HType type) {
773 fieldTypes.registerFieldInitializer(field, type);
774 }
775
776 void registerFieldConstructor(Element field, HType type) {
777 fieldTypes.registerFieldConstructor(field, type);
778 }
779
780 void registerFieldSetter(HFieldSet node, HTypeMap types) {
781 fieldTypes.registerFieldSetter(node, types);
782 }
783
784 void addedDynamicSetter(Selector setter) {
785 fieldTypes.addedDynamicSetter(setter);
786 }
787
788 HType optimisticFieldType(Element element) {
789 return fieldTypes.optimisticFieldType(element);
790 }
791
758 SourceString getCheckedModeHelper(DartType type) { 792 SourceString getCheckedModeHelper(DartType type) {
759 Element element = type.element; 793 Element element = type.element;
760 bool nativeCheck = 794 bool nativeCheck =
761 emitter.nativeEmitter.requiresNativeIsCheck(element); 795 emitter.nativeEmitter.requiresNativeIsCheck(element);
762 if (element == compiler.stringClass) { 796 if (element == compiler.stringClass) {
763 return const SourceString('stringTypeCheck'); 797 return const SourceString('stringTypeCheck');
764 } else if (element == compiler.doubleClass) { 798 } else if (element == compiler.doubleClass) {
765 return const SourceString('doubleTypeCheck'); 799 return const SourceString('doubleTypeCheck');
766 } else if (element == compiler.numClass) { 800 } else if (element == compiler.numClass) {
767 return const SourceString('numTypeCheck'); 801 return const SourceString('numTypeCheck');
(...skipping 19 matching lines...) Expand all
787 ? const SourceString('listSuperNativeTypeCheck') 821 ? const SourceString('listSuperNativeTypeCheck')
788 : const SourceString('listSuperTypeCheck'); 822 : const SourceString('listSuperTypeCheck');
789 } else { 823 } else {
790 return nativeCheck 824 return nativeCheck
791 ? const SourceString('callTypeCheck') 825 ? const SourceString('callTypeCheck')
792 : const SourceString('propertyTypeCheck'); 826 : const SourceString('propertyTypeCheck');
793 } 827 }
794 } 828 }
795 } 829 }
796 } 830 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698