| OLD | NEW |
| 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 | 5 |
| 6 /** | 6 /** |
| 7 * If true, print a warning for each method that was resolved, but not | 7 * If true, print a warning for each method that was resolved, but not |
| 8 * compiled. | 8 * compiled. |
| 9 */ | 9 */ |
| 10 final bool REPORT_EXCESS_RESOLUTION = false; | 10 final bool REPORT_EXCESS_RESOLUTION = false; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 abstract void enqueueHelpers(Enqueuer world); | 46 abstract void enqueueHelpers(Enqueuer world); |
| 47 abstract CodeBuffer codegen(WorkItem work); | 47 abstract CodeBuffer codegen(WorkItem work); |
| 48 abstract void processNativeClasses(Enqueuer world, | 48 abstract void processNativeClasses(Enqueuer world, |
| 49 Collection<LibraryElement> libraries); | 49 Collection<LibraryElement> libraries); |
| 50 abstract void assembleProgram(); | 50 abstract void assembleProgram(); |
| 51 abstract List<CompilerTask> get tasks(); | 51 abstract List<CompilerTask> get tasks(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 class InvocationInfo { | |
| 55 int parameterCount; | |
| 56 List<HType> providedTypes; | |
| 57 List<Element> compiledFunctions; | |
| 58 | |
| 59 InvocationInfo(List<HType> types) | |
| 60 : parameterCount = types != null ? types.length : -1, | |
| 61 providedTypes = types, | |
| 62 compiledFunctions = new List<Element>(); | |
| 63 | |
| 64 addCompiledFunction(FunctionElement function) => | |
| 65 compiledFunctions.add(function); | |
| 66 | |
| 67 void clearTypeInformation() => providedTypes = null; | |
| 68 bool get hasTypeInformation() => providedTypes != null; | |
| 69 | |
| 70 } | |
| 71 | |
| 72 class JavaScriptBackend extends Backend { | 54 class JavaScriptBackend extends Backend { |
| 73 SsaBuilderTask builder; | 55 SsaBuilderTask builder; |
| 74 SsaOptimizerTask optimizer; | 56 SsaOptimizerTask optimizer; |
| 75 SsaCodeGeneratorTask generator; | 57 SsaCodeGeneratorTask generator; |
| 76 CodeEmitterTask emitter; | 58 CodeEmitterTask emitter; |
| 77 final Map<Element, Map<Element, HType>> fieldInitializers; | 59 final Map<Element, Map<Element, HType>> fieldInitializers; |
| 78 final Map<Element, Map<Element, HType>> fieldConstructorSetters; | 60 final Map<Element, Map<Element, HType>> fieldConstructorSetters; |
| 79 final Map<Element, Map<Element, HType>> fieldSettersType; | 61 final Map<Element, Map<Element, HType>> fieldSettersType; |
| 80 | 62 |
| 81 final Map<SourceString, Map<Selector, InvocationInfo>> invocationInfo; | |
| 82 | |
| 83 List<CompilerTask> get tasks() { | 63 List<CompilerTask> get tasks() { |
| 84 return <CompilerTask>[builder, optimizer, generator, emitter]; | 64 return <CompilerTask>[builder, optimizer, generator, emitter]; |
| 85 } | 65 } |
| 86 | 66 |
| 87 JavaScriptBackend(Compiler compiler, bool generateSourceMap) | 67 JavaScriptBackend(Compiler compiler, bool generateSourceMap) |
| 88 : emitter = new CodeEmitterTask(compiler, generateSourceMap), | 68 : emitter = new CodeEmitterTask(compiler, generateSourceMap), |
| 89 fieldInitializers = new Map<Element, Map<Element, HType>>(), | 69 fieldInitializers = new Map<Element, Map<Element, HType>>(), |
| 90 fieldConstructorSetters = new Map<Element, Map<Element, HType>>(), | 70 fieldConstructorSetters = new Map<Element, Map<Element, HType>>(), |
| 91 fieldSettersType = new Map<Element, Map<Element, HType>>(), | 71 fieldSettersType = new Map<Element, Map<Element, HType>>(), |
| 92 invocationInfo = new Map<SourceString, Map<Selector, InvocationInfo>>(), | |
| 93 super(compiler) { | 72 super(compiler) { |
| 94 builder = new SsaBuilderTask(this); | 73 builder = new SsaBuilderTask(this); |
| 95 optimizer = new SsaOptimizerTask(this); | 74 optimizer = new SsaOptimizerTask(this); |
| 96 generator = new SsaCodeGeneratorTask(this); | 75 generator = new SsaCodeGeneratorTask(this); |
| 97 } | 76 } |
| 98 | 77 |
| 99 void enqueueHelpers(Enqueuer world) { | 78 void enqueueHelpers(Enqueuer world) { |
| 100 enqueueAllTopLevelFunctions(compiler.jsHelperLibrary, world); | 79 enqueueAllTopLevelFunctions(compiler.jsHelperLibrary, world); |
| 101 enqueueAllTopLevelFunctions(compiler.interceptorsLibrary, world); | 80 enqueueAllTopLevelFunctions(compiler.interceptorsLibrary, world); |
| 102 for (var helper in [const SourceString('Closure'), | 81 for (var helper in [const SourceString('Closure'), |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 HType fieldSettersTypeSoFar(Element field) { | 197 HType fieldSettersTypeSoFar(Element field) { |
| 219 assert(field.isField()); | 198 assert(field.isField()); |
| 220 assert(field.enclosingElement.isClass()); | 199 assert(field.enclosingElement.isClass()); |
| 221 if (!fieldSettersType.containsKey(field.enclosingElement)) { | 200 if (!fieldSettersType.containsKey(field.enclosingElement)) { |
| 222 return HType.CONFLICTING; | 201 return HType.CONFLICTING; |
| 223 } | 202 } |
| 224 Map<Element, HType> fields = fieldSettersType[field.enclosingElement]; | 203 Map<Element, HType> fields = fieldSettersType[field.enclosingElement]; |
| 225 if (!fields.containsKey(field)) return HType.CONFLICTING; | 204 if (!fields.containsKey(field)) return HType.CONFLICTING; |
| 226 return fields[field]; | 205 return fields[field]; |
| 227 } | 206 } |
| 228 | |
| 229 /** | |
| 230 * Register a dynamic invocation and collect the provided types for the | |
| 231 * named selector. | |
| 232 */ | |
| 233 void registerDynamicInvocation(HInvokeDynamicMethod node, Selector selector) { | |
| 234 Map<Selector, InvocationInfo> invocationInfos = | |
| 235 invocationInfo.putIfAbsent(node.name, | |
| 236 () => new Map<Selector, InvocationInfo>()); | |
| 237 InvocationInfo info = invocationInfos[selector]; | |
| 238 if (info != null) { | |
| 239 // If we don't know anything useful about the types adding more | |
| 240 // information will not help. | |
| 241 if (!info.hasTypeInformation) return; | |
| 242 | |
| 243 // Update the type information with the provided types. | |
| 244 bool typesChanged = false; | |
| 245 List<HType> types = info.providedTypes; | |
| 246 bool allUnknown = true; | |
| 247 for (int i = 0; i < types.length; i++) { | |
| 248 HType newType = types[i].union(node.inputs[i + 1].propagatedType); | |
| 249 if (newType != types[i]) { | |
| 250 typesChanged = true; | |
| 251 types[i] = newType; | |
| 252 } | |
| 253 if (types[i] != HType.UNKNOWN) allUnknown = false; | |
| 254 } | |
| 255 // If the provided types change we need to recompile all functions which | |
| 256 // have been compiled under the now invalidated assumptions. | |
| 257 if (typesChanged && info.compiledFunctions.length != 0) { | |
| 258 if (compiler.phase == Compiler.PHASE_COMPILING) { | |
| 259 info.compiledFunctions.forEach( | |
| 260 compiler.enqueuer.codegen.eagerRecompile); | |
| 261 info.compiledFunctions.clear(); | |
| 262 } | |
| 263 } | |
| 264 // If all information is lost no need to keep it around. | |
| 265 if (allUnknown) info.clearTypeInformation(); | |
| 266 } else { | |
| 267 // Gather the type information provided. If the types contains no useful | |
| 268 // information there is no need to actually store them. | |
| 269 bool allUnknown = true; | |
| 270 for (int i = 1; i < node.inputs.length; i++) { | |
| 271 if (node.inputs[i].propagatedType != HType.UNKNOWN) { | |
| 272 allUnknown = false; | |
| 273 break; | |
| 274 } | |
| 275 } | |
| 276 List<HType> types = null; | |
| 277 if (!allUnknown) { | |
| 278 types = new List<HType>(node.inputs.length - 1); | |
| 279 for (int i = 0; i < types.length; i++) { | |
| 280 types[i] = node.inputs[i + 1].propagatedType; | |
| 281 } | |
| 282 } | |
| 283 InvocationInfo info = new InvocationInfo(types); | |
| 284 invocationInfos[selector] = info; | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 /** | |
| 289 * Retreive the types of the parameters used for calling the [element] | |
| 290 * function. The types are optimistic in the sense as they are based on the | |
| 291 * possible invocations of the function seen so far. As compiling more | |
| 292 * code can invalidate this asumption the function is registered for being | |
| 293 * re-compiled if new possible invocations of this function invalidate these | |
| 294 * asumptions. | |
| 295 */ | |
| 296 List<HType> optimisticParameterTypesWithRecompilationOnTypeChange( | |
| 297 FunctionElement element) { | |
| 298 Map<Selector, InvocationInfo> invocationInfos = | |
| 299 invocationInfo[element.name]; | |
| 300 if (invocationInfos == null) return null; | |
| 301 | |
| 302 int foundCount = 0; | |
| 303 InvocationInfo found = null; | |
| 304 invocationInfos.forEach((Selector selector, InvocationInfo info) { | |
| 305 if (selector.applies(element, compiler)) { | |
| 306 found = info; | |
| 307 foundCount++; | |
| 308 } | |
| 309 }); | |
| 310 | |
| 311 if (foundCount == 1 && found.hasTypeInformation) { | |
| 312 FunctionSignature signature = element.computeSignature(compiler); | |
| 313 if (signature.parameterCount == found.parameterCount) { | |
| 314 found.addCompiledFunction(element); | |
| 315 return found.providedTypes; | |
| 316 } | |
| 317 } | |
| 318 return null; | |
| 319 } | |
| 320 } | 207 } |
| 321 | 208 |
| 322 class Compiler implements DiagnosticListener { | 209 class Compiler implements DiagnosticListener { |
| 323 final Map<String, LibraryElement> libraries; | 210 final Map<String, LibraryElement> libraries; |
| 324 int nextFreeClassId = 0; | 211 int nextFreeClassId = 0; |
| 325 World world; | 212 World world; |
| 326 String assembledCode; | 213 String assembledCode; |
| 327 Namer namer; | 214 Namer namer; |
| 328 Types types; | 215 Types types; |
| 329 final bool enableTypeAssertions; | 216 final bool enableTypeAssertions; |
| (...skipping 884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1214 final endOffset = end.charOffset + end.slowCharCount; | 1101 final endOffset = end.charOffset + end.slowCharCount; |
| 1215 | 1102 |
| 1216 // [begin] and [end] might be the same for the same empty token. This | 1103 // [begin] and [end] might be the same for the same empty token. This |
| 1217 // happens for instance when scanning '$$'. | 1104 // happens for instance when scanning '$$'. |
| 1218 assert(endOffset >= beginOffset); | 1105 assert(endOffset >= beginOffset); |
| 1219 return f(beginOffset, endOffset); | 1106 return f(beginOffset, endOffset); |
| 1220 } | 1107 } |
| 1221 | 1108 |
| 1222 String toString() => 'SourceSpan($uri, $begin, $end)'; | 1109 String toString() => 'SourceSpan($uri, $begin, $end)'; |
| 1223 } | 1110 } |
| OLD | NEW |