| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 // Partial test that the closed world computed from [WorldImpact]s derived from |
| 6 // kernel is equivalent to the original computed from resolution. |
| 7 library dart2js.kernel.compiler_helper; |
| 8 |
| 9 import 'dart:async'; |
| 10 |
| 11 import 'package:compiler/src/commandline_options.dart'; |
| 12 import 'package:compiler/src/common.dart'; |
| 13 import 'package:compiler/src/common/names.dart'; |
| 14 import 'package:compiler/src/common/tasks.dart'; |
| 15 import 'package:compiler/src/compiler.dart'; |
| 16 import 'package:compiler/src/elements/elements.dart'; |
| 17 import 'package:compiler/src/kernel/element_map.dart'; |
| 18 import 'package:compiler/src/kernel/kernel_strategy.dart'; |
| 19 import 'package:compiler/src/library_loader.dart'; |
| 20 import 'package:compiler/src/universe/world_builder.dart'; |
| 21 import 'package:compiler/src/util/util.dart'; |
| 22 import 'package:expect/expect.dart'; |
| 23 import 'package:kernel/ast.dart' as ir; |
| 24 import '../memory_compiler.dart'; |
| 25 |
| 26 typedef Future<Compiler> CompileFunction(); |
| 27 |
| 28 /// Create multiple compilations for a list of [sources]. |
| 29 /// |
| 30 /// This methods speeds up testing kernel based compilation by creating the IR |
| 31 /// nodes for all [sources] at the same time. The returned list of |
| 32 /// [CompileFunction]s compiles one of the [source] at a time using the kernel |
| 33 /// based compiler. |
| 34 /// |
| 35 /// Currently, the returned compile function only runs with '--analyze-only' |
| 36 /// flag. |
| 37 Future<List<CompileFunction>> compileMultiple(List<String> sources) async { |
| 38 List<Uri> uris = <Uri>[]; |
| 39 Uri entryPoint = Uri.parse('memory:main.dart'); |
| 40 Map<String, String> memorySourceFiles = <String, String>{ |
| 41 'main.dart': 'main() {}' |
| 42 }; |
| 43 for (String source in sources) { |
| 44 String name = 'input${memorySourceFiles.length}.dart'; |
| 45 Uri uri = Uri.parse('memory:$name'); |
| 46 memorySourceFiles[name] = source; |
| 47 uris.add(uri); |
| 48 } |
| 49 Compiler compiler = compilerFor( |
| 50 entryPoint: entryPoint, |
| 51 memorySourceFiles: memorySourceFiles, |
| 52 options: [Flags.analyzeAll, Flags.useKernel, Flags.enableAssertMessage]); |
| 53 compiler.librariesToAnalyzeWhenRun = uris; |
| 54 await compiler.run(entryPoint); |
| 55 |
| 56 List<CompileFunction> compilers = <CompileFunction>[]; |
| 57 for (Uri uri in uris) { |
| 58 compilers.add(() async { |
| 59 Compiler compiler2 = compilerFor( |
| 60 entryPoint: uri, |
| 61 memorySourceFiles: memorySourceFiles, |
| 62 options: [ |
| 63 Flags.analyzeOnly, |
| 64 Flags.enableAssertMessage, |
| 65 Flags.loadFromDill |
| 66 ]); |
| 67 ElementResolutionWorldBuilder.useInstantiationMap = true; |
| 68 compiler2.resolution.retainCachesForTesting = true; |
| 69 KernelFrontEndStrategy frontEndStrategy = compiler2.frontEndStrategy; |
| 70 KernelToElementMap elementMap = frontEndStrategy.elementMap; |
| 71 ir.Program program = new ir.Program( |
| 72 compiler.backend.kernelTask.kernel.libraryDependencies(uri)); |
| 73 LibraryElement library = compiler.libraryLoader.lookupLibrary(uri); |
| 74 Expect.isNotNull(library, 'No library found for $uri'); |
| 75 program.mainMethod = compiler.backend.kernelTask.kernel |
| 76 .functionToIr(library.findExported(Identifiers.main)); |
| 77 compiler2.libraryLoader = new MemoryDillLibraryLoaderTask( |
| 78 elementMap, compiler2.reporter, compiler2.measurer, program); |
| 79 await compiler2.run(uri); |
| 80 return compiler2; |
| 81 }); |
| 82 } |
| 83 return compilers; |
| 84 } |
| 85 |
| 86 /// Analyze [memorySourceFiles] with [entryPoint] as entry-point using the |
| 87 /// kernel based element model. The returned [Pair] contains the compiler used |
| 88 /// to create the IR and the kernel based compiler. |
| 89 Future<Pair<Compiler, Compiler>> analyzeOnly( |
| 90 Uri entryPoint, Map<String, String> memorySourceFiles, |
| 91 {bool printSteps: false}) async { |
| 92 if (printSteps) { |
| 93 print('---- analyze-all -------------------------------------------------'); |
| 94 } |
| 95 Compiler compiler = compilerFor( |
| 96 entryPoint: entryPoint, |
| 97 memorySourceFiles: memorySourceFiles, |
| 98 options: [Flags.analyzeAll, Flags.useKernel, Flags.enableAssertMessage]); |
| 99 await compiler.run(entryPoint); |
| 100 |
| 101 if (printSteps) { |
| 102 print('---- closed world from kernel ------------------------------------'); |
| 103 } |
| 104 Compiler compiler2 = compilerFor( |
| 105 entryPoint: entryPoint, |
| 106 memorySourceFiles: memorySourceFiles, |
| 107 options: [ |
| 108 Flags.analyzeOnly, |
| 109 Flags.enableAssertMessage, |
| 110 Flags.loadFromDill |
| 111 ]); |
| 112 ElementResolutionWorldBuilder.useInstantiationMap = true; |
| 113 compiler2.resolution.retainCachesForTesting = true; |
| 114 KernelFrontEndStrategy frontEndStrategy = compiler2.frontEndStrategy; |
| 115 KernelToElementMap elementMap = frontEndStrategy.elementMap; |
| 116 compiler2.libraryLoader = new MemoryDillLibraryLoaderTask( |
| 117 elementMap, |
| 118 compiler2.reporter, |
| 119 compiler2.measurer, |
| 120 compiler.backend.kernelTask.program); |
| 121 await compiler2.run(entryPoint); |
| 122 return new Pair<Compiler, Compiler>(compiler, compiler2); |
| 123 } |
| 124 |
| 125 class MemoryDillLibraryLoaderTask extends DillLibraryLoaderTask { |
| 126 final ir.Program program; |
| 127 |
| 128 MemoryDillLibraryLoaderTask(KernelToElementMap elementMap, |
| 129 DiagnosticReporter reporter, Measurer measurer, this.program) |
| 130 : super(elementMap, null, null, reporter, measurer); |
| 131 |
| 132 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, |
| 133 {bool skipFileWithPartOfTag: false}) async { |
| 134 return createLoadedLibraries(program); |
| 135 } |
| 136 } |
| OLD | NEW |