OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 /// This is an interface to the Dart Kernel parser and Kernel binary generator. | 5 /// This is an interface to the Dart Kernel parser and Kernel binary generator. |
6 /// | 6 /// |
7 /// It is used by the kernel-isolate to load Dart source code and generate | 7 /// It is used by the kernel-isolate to load Dart source code and generate |
8 /// Kernel binary format. | 8 /// Kernel binary format. |
9 /// | 9 /// |
10 /// This is either invoked as the root script of the Kernel isolate when used | 10 /// This is either invoked as the root script of the Kernel isolate when used |
11 /// as a part of | 11 /// as a part of |
12 /// | 12 /// |
13 /// dart --dfe=utils/kernel-service/kernel-service.dart ... | 13 /// dart --dfe=utils/kernel-service/kernel-service.dart ... |
14 /// | 14 /// |
15 /// invocation or it is invoked as a standalone script to perform training for | 15 /// invocation or it is invoked as a standalone script to perform training for |
16 /// the app-jit snapshot | 16 /// the app-jit snapshot |
17 /// | 17 /// |
18 /// dart utils/kernel-service/kernel-service.dart --train <source-file> | 18 /// dart utils/kernel-service/kernel-service.dart --train <source-file> |
19 /// | 19 /// |
20 /// | 20 /// |
21 library runtime.tools.kernel_service; | 21 library runtime.tools.kernel_service; |
22 | 22 |
23 import 'dart:async' show Future; | 23 import 'dart:async' show Future; |
24 import 'dart:convert' show JSON; | |
25 import 'dart:io' show Platform hide FileSystemEntity; | 24 import 'dart:io' show Platform hide FileSystemEntity; |
26 import 'dart:isolate'; | 25 import 'dart:isolate'; |
27 import 'dart:typed_data' show Uint8List; | 26 import 'dart:typed_data' show Uint8List; |
28 | 27 |
29 import 'package:front_end/file_system.dart'; | 28 import 'package:front_end/file_system.dart'; |
30 import 'package:front_end/front_end.dart'; | 29 import 'package:front_end/front_end.dart'; |
31 import 'package:front_end/incremental_kernel_generator.dart'; | |
32 import 'package:front_end/memory_file_system.dart'; | 30 import 'package:front_end/memory_file_system.dart'; |
33 import 'package:front_end/physical_file_system.dart'; | 31 import 'package:front_end/physical_file_system.dart'; |
34 import 'package:front_end/src/fasta/kernel/utils.dart'; | 32 import 'package:front_end/src/fasta/kernel/utils.dart'; |
35 import 'package:front_end/src/testing/hybrid_file_system.dart'; | 33 import 'package:front_end/src/testing/hybrid_file_system.dart'; |
36 import 'package:kernel/kernel.dart' show Program; | 34 import 'package:kernel/kernel.dart' show Program; |
37 import 'package:kernel/target/targets.dart' show TargetFlags; | 35 import 'package:kernel/target/targets.dart' show TargetFlags; |
38 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; | 36 import 'package:kernel/target/vm_fasta.dart' show VmFastaTarget; |
39 | 37 |
40 const bool verbose = const bool.fromEnvironment('DFE_VERBOSE'); | 38 const bool verbose = const bool.fromEnvironment('DFE_VERBOSE'); |
41 const bool strongMode = const bool.fromEnvironment('DFE_STRONG_MODE'); | 39 const bool strongMode = const bool.fromEnvironment('DFE_STRONG_MODE'); |
42 | 40 |
43 abstract class Compiler { | |
44 final FileSystem fileSystem; | |
45 final List<String> errors = new List<String>(); | |
46 | |
47 CompilerOptions options; | |
48 | |
49 Compiler(this.fileSystem) { | |
50 Uri packagesUri = (Platform.packageConfig != null) | |
51 ? Uri.parse(Platform.packageConfig) | |
52 : null; | |
53 | |
54 Uri sdkSummary = Uri.base | |
55 .resolveUri(new Uri.file(Platform.resolvedExecutable)) | |
56 .resolveUri(new Uri.directory("patched_sdk")) | |
57 // TODO(sigmund): use outline.dill when the mixin transformer is | |
58 // modular. | |
59 .resolve('platform.dill'); | |
60 | |
61 if (verbose) { | |
62 print("DFE: Platform.packageConfig: ${Platform.packageConfig}"); | |
63 print("DFE: packagesUri: ${packagesUri}"); | |
64 print("DFE: Platform.resolvedExecutable: ${Platform.resolvedExecutable}"); | |
65 print("DFE: sdkSummary: ${sdkSummary}"); | |
66 } | |
67 | |
68 options = new CompilerOptions() | |
69 ..strongMode = strongMode | |
70 ..fileSystem = fileSystem | |
71 ..target = new VmFastaTarget(new TargetFlags(strongMode: strongMode)) | |
72 ..packagesFileUri = packagesUri | |
73 ..sdkSummary = sdkSummary | |
74 ..verbose = verbose | |
75 ..throwOnErrors = false | |
76 ..reportMessages = true | |
77 ..onError = (CompilationMessage e) { | |
78 if (e.severity == Severity.error) { | |
79 // TODO(sigmund): support emitting code with errors as long as they | |
80 // are handled in the generated code (issue #30194). | |
81 errors.add(e.message); | |
82 } | |
83 }; | |
84 } | |
85 | |
86 Future<Program> compile(Uri script); | |
87 } | |
88 | |
89 class IncrementalCompiler extends Compiler { | |
90 IncrementalKernelGenerator generator; | |
91 | |
92 IncrementalCompiler(FileSystem fileSystem) : super(fileSystem); | |
93 | |
94 @override | |
95 Future<Program> compile(Uri script) async { | |
96 if (generator == null) { | |
97 generator = await IncrementalKernelGenerator.newInstance(options, script); | |
98 } | |
99 DeltaProgram deltaProgram = await generator.computeDelta(); | |
100 // TODO(aam): Accepting/rejecting should be done based on VM response. | |
101 generator.acceptLastDelta(); | |
102 return deltaProgram.newProgram; | |
103 } | |
104 | |
105 void invalidate(Uri uri) { | |
106 generator.invalidate(uri); | |
107 } | |
108 } | |
109 | |
110 class SingleShotCompiler extends Compiler { | |
111 final bool requireMain; | |
112 | |
113 SingleShotCompiler(FileSystem fileSystem, this.requireMain) | |
114 : super(fileSystem); | |
115 | |
116 @override | |
117 Future<Program> compile(Uri script) async { | |
118 return requireMain | |
119 ? kernelForProgram(script, options) | |
120 : kernelForBuildUnit([script], options..chaseDependencies = true); | |
121 } | |
122 } | |
123 | |
124 final Map<int, Compiler> isolateCompilers = new Map<int, Compiler>(); | |
125 | |
126 Future<Compiler> lookupOrBuildNewIncrementalCompiler( | |
127 int isolateId, List sourceFiles) async { | |
128 IncrementalCompiler compiler; | |
129 if (isolateCompilers.containsKey(isolateId)) { | |
130 compiler = isolateCompilers[isolateId]; | |
131 final HybridFileSystem fileSystem = compiler.fileSystem; | |
132 if (sourceFiles != null) { | |
133 for (int i = 0; i < sourceFiles.length ~/ 2; i++) { | |
134 Uri uri = Uri.parse(sourceFiles[i * 2]); | |
135 fileSystem.memory | |
136 .entityForUri(uri) | |
137 .writeAsBytesSync(sourceFiles[i * 2 + 1]); | |
138 compiler.invalidate(uri); | |
139 } | |
140 } | |
141 } else { | |
142 final FileSystem fileSystem = sourceFiles == null | |
143 ? PhysicalFileSystem.instance | |
144 : _buildFileSystem(sourceFiles); | |
145 | |
146 // TODO(aam): IncrementalCompiler instance created below have to be | |
147 // destroyed when corresponding isolate is shut down. To achieve that kernel | |
148 // isolate needs to receive a message indicating that particular | |
149 // isolate was shut down. Message should be handled here in this script. | |
150 compiler = new IncrementalCompiler(fileSystem); | |
151 isolateCompilers[isolateId] = compiler; | |
152 } | |
153 return compiler; | |
154 } | |
155 | |
156 // Process a request from the runtime. See KernelIsolate::CompileToKernel in | 41 // Process a request from the runtime. See KernelIsolate::CompileToKernel in |
157 // kernel_isolate.cc and Loader::SendKernelRequest in loader.cc. | 42 // kernel_isolate.cc and Loader::SendKernelRequest in loader.cc. |
158 Future _processLoadRequest(request) async { | 43 Future _processLoadRequest(request) async { |
159 if (verbose) print("DFE: request: $request"); | 44 if (verbose) print("DFE: request: $request"); |
160 | 45 |
161 int tag = request[0]; | 46 int tag = request[0]; |
162 final SendPort port = request[1]; | 47 final SendPort port = request[1]; |
163 final String inputFileUri = request[2]; | 48 final String inputFileUri = request[2]; |
164 final Uri script = Uri.base.resolve(inputFileUri); | 49 final Uri script = Uri.base.resolve(inputFileUri); |
165 final bool incremental = request[3]; | |
166 | 50 |
167 final List sourceFiles = request.length > 5 ? request[5] : null; | 51 FileSystem fileSystem = PhysicalFileSystem.instance; |
| 52 bool requireMain = true; |
168 | 53 |
169 Compiler compiler; | 54 if (request.length > 3) { |
170 // TODO(aam): There should be no need to have an option to choose | 55 fileSystem = _buildFileSystem(request[3]); |
171 // one compiler or another. We should always use an incremental | 56 requireMain = false; |
172 // compiler as its functionality is a super set of the other one. We need to | |
173 // watch the performance though. | |
174 if (incremental) { | |
175 final int isolateId = request[4]; | |
176 compiler = | |
177 await lookupOrBuildNewIncrementalCompiler(isolateId, sourceFiles); | |
178 } else { | |
179 final FileSystem fileSystem = sourceFiles == null | |
180 ? PhysicalFileSystem.instance | |
181 : _buildFileSystem(sourceFiles); | |
182 compiler = new SingleShotCompiler( | |
183 fileSystem, sourceFiles == null /* requireMain */); | |
184 } | 57 } |
185 | 58 |
| 59 Uri packagesUri = (Platform.packageConfig != null) |
| 60 ? Uri.parse(Platform.packageConfig) |
| 61 : null; |
| 62 |
| 63 Uri sdkSummary = Uri.base |
| 64 .resolveUri(new Uri.file(Platform.resolvedExecutable)) |
| 65 .resolveUri(new Uri.directory("patched_sdk")) |
| 66 // TODO(sigmund): use outline.dill when the mixin transformer is modular. |
| 67 .resolve('platform.dill'); |
| 68 |
| 69 if (verbose) { |
| 70 print("DFE: scriptUri: ${script}"); |
| 71 print("DFE: Platform.packageConfig: ${Platform.packageConfig}"); |
| 72 print("DFE: packagesUri: ${packagesUri}"); |
| 73 print("DFE: Platform.resolvedExecutable: ${Platform.resolvedExecutable}"); |
| 74 print("DFE: sdkSummary: ${sdkSummary}"); |
| 75 } |
| 76 |
| 77 var errors = <String>[]; |
| 78 var options = new CompilerOptions() |
| 79 ..strongMode = strongMode |
| 80 ..fileSystem = fileSystem |
| 81 ..target = new VmFastaTarget(new TargetFlags(strongMode: strongMode)) |
| 82 ..packagesFileUri = packagesUri |
| 83 ..sdkSummary = sdkSummary |
| 84 ..verbose = verbose |
| 85 ..throwOnErrors = false |
| 86 ..reportMessages = true |
| 87 ..onError = (CompilationMessage e) { |
| 88 if (e.severity == Severity.error) { |
| 89 // TODO(sigmund): support emitting code with errors as long as they are |
| 90 // handled in the generated code (issue #30194). |
| 91 errors.add(e.message); |
| 92 } |
| 93 }; |
| 94 |
186 CompilationResult result; | 95 CompilationResult result; |
187 try { | 96 try { |
188 if (verbose) { | 97 Program program = requireMain |
189 print("DFE: scriptUri: ${script}"); | 98 ? await kernelForProgram(script, options) |
190 } | 99 : await kernelForBuildUnit([script], options..chaseDependencies = true); |
191 | 100 |
192 Program program = await compiler.compile(script); | 101 if (errors.isNotEmpty) { |
193 | |
194 if (compiler.errors.isNotEmpty) { | |
195 // TODO(sigmund): the compiler prints errors to the console, so we | 102 // TODO(sigmund): the compiler prints errors to the console, so we |
196 // shouldn't print those messages again here. | 103 // shouldn't print those messages again here. |
197 result = new CompilationResult.errors(compiler.errors); | 104 result = new CompilationResult.errors(errors); |
198 } else { | 105 } else { |
199 // We serialize the program excluding platform.dill because the VM has | 106 // We serialize the program excluding platform.dill because the VM has |
200 // these sources built-in. Everything loaded as a summary in | 107 // these sources built-in. Everything loaded as a summary in |
201 // [kernelForProgram] is marked `external`, so we can use that bit to | 108 // [kernelForProgram] is marked `external`, so we can use that bit to |
202 // decide what to exclude. | 109 // decide what to excluce. |
203 // TODO(sigmund): remove the following line (Issue #30111) | 110 // TODO(sigmund): remove the following line (Issue #30111) |
204 program.libraries.forEach((e) => e.isExternal = false); | 111 program.libraries.forEach((e) => e.isExternal = false); |
205 result = new CompilationResult.ok( | 112 result = new CompilationResult.ok( |
206 serializeProgram(program, filter: (lib) => !lib.isExternal)); | 113 serializeProgram(program, filter: (lib) => !lib.isExternal)); |
207 } | 114 } |
208 } catch (error, stack) { | 115 } catch (error, stack) { |
209 result = new CompilationResult.crash(error, stack); | 116 result = new CompilationResult.crash(error, stack); |
210 } | 117 } |
211 | 118 |
212 if (verbose) print("DFE:> ${result}"); | 119 if (verbose) print("DFE:> ${result}"); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 if (response[0] == tag) { | 159 if (response[0] == tag) { |
253 // Success. | 160 // Success. |
254 responsePort.close(); | 161 responsePort.close(); |
255 } else if (response[0] == -tag) { | 162 } else if (response[0] == -tag) { |
256 // Compilation error. | 163 // Compilation error. |
257 throw response[4]; | 164 throw response[4]; |
258 } else { | 165 } else { |
259 throw "Unexpected response: $response"; | 166 throw "Unexpected response: $response"; |
260 } | 167 } |
261 }; | 168 }; |
262 var request = [ | 169 var request = [tag, responsePort.sendPort, scriptUri]; |
263 tag, | |
264 responsePort.sendPort, | |
265 scriptUri, | |
266 1 /* isolateId chosen randomly */, | |
267 false /* incremental */ | |
268 ]; | |
269 _processLoadRequest(request); | 170 _processLoadRequest(request); |
270 } | 171 } |
271 | 172 |
272 main([args]) { | 173 main([args]) { |
273 if (args?.length == 2 && args[0] == '--train') { | 174 if (args?.length == 2 && args[0] == '--train') { |
274 // This entry point is used when creating an app snapshot. The argument | 175 // This entry point is used when creating an app snapshot. The argument |
275 // provides a script to compile to warm-up generated code. | 176 // provides a script to compile to warm-up generated code. |
276 train(args[1]); | 177 train(args[1]); |
277 } else { | 178 } else { |
278 // Entry point for the Kernel isolate. | 179 // Entry point for the Kernel isolate. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 _CompilationCrash(this.exception, this.stack); | 258 _CompilationCrash(this.exception, this.stack); |
358 | 259 |
359 @override | 260 @override |
360 Status get status => Status.crash; | 261 Status get status => Status.crash; |
361 | 262 |
362 @override | 263 @override |
363 String get errorString => "${exception}\n${stack}"; | 264 String get errorString => "${exception}\n${stack}"; |
364 | 265 |
365 String toString() => "_CompilationCrash(${errorString})"; | 266 String toString() => "_CompilationCrash(${errorString})"; |
366 } | 267 } |
OLD | NEW |