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

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

Issue 10823389: Start inferring return types for static functions (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 class InvocationInfo { 5 class InvocationInfo {
6 int parameterCount = -1; 6 int parameterCount = -1;
7 List<HType> providedTypes; 7 List<HType> providedTypes;
8 List<Element> compiledFunctions; 8 List<Element> compiledFunctions;
9 9
10 InvocationInfo(HInvoke node, HTypeMap types) 10 InvocationInfo(HInvoke node, HTypeMap types)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 } 59 }
60 60
61 addCompiledFunction(FunctionElement function) => 61 addCompiledFunction(FunctionElement function) =>
62 compiledFunctions.add(function); 62 compiledFunctions.add(function);
63 63
64 void clearTypeInformation() => providedTypes = null; 64 void clearTypeInformation() => providedTypes = null;
65 bool get hasTypeInformation() => providedTypes != null; 65 bool get hasTypeInformation() => providedTypes != null;
66 66
67 } 67 }
68 68
69 class ReturnInfo {
70 HType returnType;
71 List<Element> compiledFunctions;
72
73 ReturnInfo(HType this.returnType)
74 : compiledFunctions = new List<Element>();
75
76 ReturnInfo.unknownType()
77 : this.returnType = null,
78 compiledFunctions = new List<Element>();
79
80 void update(HType type, var recompile) {
81 HType newType = returnType != null ? returnType.union(type) : type;
82 if (newType != returnType) {
83 if (returnType == null && newType === HType.UNKNOWN) {
84 // If the first actual piece of information is not providing any type
85 // information there is no need to recompile callers.
86 compiledFunctions.clear();
87 }
88 returnType = newType;
89 if (recompile != null) {
90 compiledFunctions.forEach(recompile);
91 }
92 compiledFunctions.clear();
93 }
94 }
95
96 addCompiledFunction(FunctionElement function) =>
97 compiledFunctions.add(function);
98 }
99
69 class JavaScriptItemCompilationContext extends ItemCompilationContext { 100 class JavaScriptItemCompilationContext extends ItemCompilationContext {
70 final HTypeMap types; 101 final HTypeMap types;
71 102
72 JavaScriptItemCompilationContext() : types = new HTypeMap(); 103 JavaScriptItemCompilationContext() : types = new HTypeMap();
73 } 104 }
74 105
75 class JavaScriptBackend extends Backend { 106 class JavaScriptBackend extends Backend {
76 SsaBuilderTask builder; 107 SsaBuilderTask builder;
77 SsaOptimizerTask optimizer; 108 SsaOptimizerTask optimizer;
78 SsaCodeGeneratorTask generator; 109 SsaCodeGeneratorTask generator;
79 CodeEmitterTask emitter; 110 CodeEmitterTask emitter;
80 final Map<Element, Map<Element, HType>> fieldInitializers; 111 final Map<Element, Map<Element, HType>> fieldInitializers;
81 final Map<Element, Map<Element, HType>> fieldConstructorSetters; 112 final Map<Element, Map<Element, HType>> fieldConstructorSetters;
82 final Map<Element, Map<Element, HType>> fieldSettersType; 113 final Map<Element, Map<Element, HType>> fieldSettersType;
83 114
84 final Map<Element, InvocationInfo> staticInvocationInfo; 115 final Map<Element, InvocationInfo> staticInvocationInfo;
85 final Map<SourceString, Map<Selector, InvocationInfo>> invocationInfo; 116 final Map<SourceString, Map<Selector, InvocationInfo>> invocationInfo;
117 final Map<Element, ReturnInfo> returnInfo;
86 118
87 final List<Element> invalidateAfterCodegen; 119 final List<Element> invalidateAfterCodegen;
88 120
89 List<CompilerTask> get tasks() { 121 List<CompilerTask> get tasks() {
90 return <CompilerTask>[builder, optimizer, generator, emitter]; 122 return <CompilerTask>[builder, optimizer, generator, emitter];
91 } 123 }
92 124
93 JavaScriptBackend(Compiler compiler, bool generateSourceMap) 125 JavaScriptBackend(Compiler compiler, bool generateSourceMap)
94 : emitter = new CodeEmitterTask(compiler, generateSourceMap), 126 : emitter = new CodeEmitterTask(compiler, generateSourceMap),
95 fieldInitializers = new Map<Element, Map<Element, HType>>(), 127 fieldInitializers = new Map<Element, Map<Element, HType>>(),
96 fieldConstructorSetters = new Map<Element, Map<Element, HType>>(), 128 fieldConstructorSetters = new Map<Element, Map<Element, HType>>(),
97 fieldSettersType = new Map<Element, Map<Element, HType>>(), 129 fieldSettersType = new Map<Element, Map<Element, HType>>(),
98 invocationInfo = new Map<SourceString, Map<Selector, InvocationInfo>>(), 130 invocationInfo = new Map<SourceString, Map<Selector, InvocationInfo>>(),
99 staticInvocationInfo = new Map<Element, InvocationInfo>(), 131 staticInvocationInfo = new Map<Element, InvocationInfo>(),
132 returnInfo = new Map<Element, ReturnInfo>(),
100 invalidateAfterCodegen = new List<Element>(), 133 invalidateAfterCodegen = new List<Element>(),
101 super(compiler) { 134 super(compiler) {
102 builder = new SsaBuilderTask(this); 135 builder = new SsaBuilderTask(this);
103 optimizer = new SsaOptimizerTask(this); 136 optimizer = new SsaOptimizerTask(this);
104 generator = new SsaCodeGeneratorTask(this); 137 generator = new SsaCodeGeneratorTask(this);
105 } 138 }
106 139
107 JavaScriptItemCompilationContext createItemCompilationContext() { 140 JavaScriptItemCompilationContext createItemCompilationContext() {
108 return new JavaScriptItemCompilationContext(); 141 return new JavaScriptItemCompilationContext();
109 } 142 }
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 if (foundCount == 1 && found.hasTypeInformation) { 378 if (foundCount == 1 && found.hasTypeInformation) {
346 FunctionSignature signature = element.computeSignature(compiler); 379 FunctionSignature signature = element.computeSignature(compiler);
347 if (signature.parameterCount == found.parameterCount) { 380 if (signature.parameterCount == found.parameterCount) {
348 found.addCompiledFunction(element); 381 found.addCompiledFunction(element);
349 return found.providedTypes; 382 return found.providedTypes;
350 } 383 }
351 } 384 }
352 return null; 385 return null;
353 } 386 }
354 } 387 }
388
389 void registerReturnType(FunctionElement element, HType returnType) {
390 ReturnInfo info = returnInfo[element];
391 if (info != null) {
392 recompile(Element element) {
393 if (compiler.phase == Compiler.PHASE_COMPILING) {
394 invalidateAfterCodegen.add(element);
395 }
396 }
397
398 info.update(returnType, recompile);
399 } else {
400 returnInfo[element] = new ReturnInfo(returnType);
401 }
402 }
403
404 /**
405 * Retreive the return type of the function [calee]. The type is optimistic
ngeoffray 2012/08/17 10:02:51 why not callee?
Søren Gjesse 2012/08/17 10:08:54 Because I cannot spell - fixed.
kasperl 2012/08/17 10:11:07 i before e except after c (Retrieve).
Søren Gjesse 2012/08/17 10:21:04 Done.
406 * in the sense that is is based on the compilation og [calee]. If [calee] is
ngeoffray 2012/08/17 10:02:51 og -> of
Søren Gjesse 2012/08/17 10:08:54 Done.
407 * recompiled the return type might change to someting broader. For that
408 * reason [calller] is registered for recompilation if this happens. If the
ngeoffray 2012/08/17 10:02:51 calller -> caller
Søren Gjesse 2012/08/17 10:08:54 Done.
409 * function [calee] has not yet been compiled the returned type is [null].
410 */
411 HType optimisticReturnTypesWithRecompilationOnTypeChange(
412 FunctionElement caller, FunctionElement calee) {
413 returnInfo.putIfAbsent(calee, () => new ReturnInfo.unknownType());
ngeoffray 2012/08/17 10:02:51 Why do you need to put a return info? Could you ju
Søren Gjesse 2012/08/17 10:08:54 The ReturnInfo with returnType null indicates that
414 ReturnInfo info = returnInfo[calee];
415 if (info.returnType != HType.UNKNOWN && caller != null) {
416 info.addCompiledFunction(caller);
417 }
418 return info.returnType;
419 }
355 } 420 }
OLDNEW
« no previous file with comments | « no previous file | lib/compiler/implementation/ssa/builder.dart » ('j') | lib/compiler/implementation/ssa/codegen.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698