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 library fasta; | 5 library fasta; |
6 | 6 |
7 import 'dart:async' show Future; | 7 import 'dart:async' show Future; |
8 | 8 |
9 import 'dart:convert' show JSON; | 9 import 'dart:convert' show JSON; |
10 | 10 |
11 import 'dart:io' show BytesBuilder, Directory, File, exitCode; | 11 import 'dart:io' show BytesBuilder, Directory, File, exitCode; |
12 | 12 |
13 import 'package:front_end/file_system.dart'; | 13 import 'package:front_end/file_system.dart'; |
14 import 'package:front_end/physical_file_system.dart'; | 14 import 'package:front_end/physical_file_system.dart'; |
15 import 'package:front_end/src/fasta/kernel/utils.dart'; | 15 import 'package:front_end/src/fasta/kernel/utils.dart'; |
16 import 'package:kernel/binary/ast_to_binary.dart' | 16 import 'package:kernel/binary/ast_to_binary.dart' |
17 show LibraryFilteringBinaryPrinter; | 17 show LibraryFilteringBinaryPrinter; |
18 | 18 |
19 import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; | |
20 | |
21 import 'package:kernel/core_types.dart' show CoreTypes; | |
22 | |
23 import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes; | 19 import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes; |
24 | 20 |
25 import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget; | 21 import 'package:kernel/target/targets.dart' show TargetFlags; |
26 | 22 |
27 import 'compiler_command_line.dart' show CompilerCommandLine; | 23 import 'compiler_command_line.dart' show CompilerCommandLine; |
28 | 24 |
29 import 'compiler_context.dart' show CompilerContext; | 25 import 'compiler_context.dart' show CompilerContext; |
30 | 26 |
31 import 'errors.dart' show InputError, formatUnexpected, inputError, reportCrash; | 27 import 'errors.dart' show InputError, formatUnexpected, inputError, reportCrash; |
32 | 28 |
33 import 'kernel/kernel_target.dart' show KernelTarget; | 29 import 'kernel/kernel_target.dart' show KernelTarget; |
34 | 30 |
35 import 'dill/dill_target.dart' show DillTarget; | 31 import 'dill/dill_target.dart' show DillTarget; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 107 } |
112 } | 108 } |
113 | 109 |
114 class CompileTask { | 110 class CompileTask { |
115 final CompilerContext c; | 111 final CompilerContext c; |
116 final Ticker ticker; | 112 final Ticker ticker; |
117 | 113 |
118 CompileTask(this.c, this.ticker); | 114 CompileTask(this.c, this.ticker); |
119 | 115 |
120 DillTarget createDillTarget(TranslateUri uriTranslator) { | 116 DillTarget createDillTarget(TranslateUri uriTranslator) { |
121 return new DillTarget(ticker, uriTranslator, c.options.target); | 117 return new DillTarget(ticker, uriTranslator, c.options.target, |
| 118 flags: new TargetFlags(strongMode: c.options.strongMode)); |
122 } | 119 } |
123 | 120 |
124 KernelTarget createKernelTarget( | 121 KernelTarget createKernelTarget( |
125 DillTarget dillTarget, TranslateUri uriTranslator, bool strongMode) { | 122 DillTarget dillTarget, TranslateUri uriTranslator, bool strongMode) { |
126 return new KernelTarget( | 123 return new KernelTarget( |
127 c.fileSystem, dillTarget, uriTranslator, strongMode, c.uriToSource); | 124 c.fileSystem, dillTarget, uriTranslator, c.uriToSource); |
128 } | 125 } |
129 | 126 |
130 Future<KernelTarget> buildOutline([Uri output]) async { | 127 Future<KernelTarget> buildOutline([Uri output]) async { |
131 TranslateUri uriTranslator = await TranslateUri | 128 TranslateUri uriTranslator = await TranslateUri |
132 .parse(c.fileSystem, c.options.sdk, packages: c.options.packages); | 129 .parse(c.fileSystem, c.options.sdk, packages: c.options.packages); |
133 ticker.logMs("Read packages file"); | 130 ticker.logMs("Read packages file"); |
134 DillTarget dillTarget = createDillTarget(uriTranslator); | 131 DillTarget dillTarget = createDillTarget(uriTranslator); |
135 KernelTarget kernelTarget = | 132 KernelTarget kernelTarget = |
136 createKernelTarget(dillTarget, uriTranslator, c.options.strongMode); | 133 createKernelTarget(dillTarget, uriTranslator, c.options.strongMode); |
137 if (c.options.strongMode) { | 134 if (c.options.strongMode) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 Uri fileName, Uri packages, Uri patchedSdk, | 176 Uri fileName, Uri packages, Uri patchedSdk, |
180 {bool verbose: false, bool strongMode: false}) async { | 177 {bool verbose: false, bool strongMode: false}) async { |
181 return parseScriptInFileSystem( | 178 return parseScriptInFileSystem( |
182 fileName, PhysicalFileSystem.instance, packages, patchedSdk, | 179 fileName, PhysicalFileSystem.instance, packages, patchedSdk, |
183 verbose: verbose, strongMode: strongMode); | 180 verbose: verbose, strongMode: strongMode); |
184 } | 181 } |
185 | 182 |
186 Future<CompilationResult> parseScriptInFileSystem( | 183 Future<CompilationResult> parseScriptInFileSystem( |
187 Uri fileName, FileSystem fileSystem, Uri packages, Uri patchedSdk, | 184 Uri fileName, FileSystem fileSystem, Uri packages, Uri patchedSdk, |
188 {bool verbose: false, bool strongMode: false, String backendTarget}) async { | 185 {bool verbose: false, bool strongMode: false, String backendTarget}) async { |
189 backendTarget ??= "vm"; | 186 backendTarget ??= "vm_fasta"; |
190 try { | 187 try { |
191 if (!await fileSystem.entityForUri(fileName).exists()) { | 188 if (!await fileSystem.entityForUri(fileName).exists()) { |
192 return new CompilationResult.error( | 189 return new CompilationResult.error( |
193 formatUnexpected(fileName, -1, "No such file.")); | 190 formatUnexpected(fileName, -1, "No such file.")); |
194 } | 191 } |
195 if (!await new Directory.fromUri(patchedSdk).exists()) { | 192 if (!await new Directory.fromUri(patchedSdk).exists()) { |
196 return new CompilationResult.error( | 193 return new CompilationResult.error( |
197 formatUnexpected(patchedSdk, -1, "Patched sdk directory not found.")); | 194 formatUnexpected(patchedSdk, -1, "Patched sdk directory not found.")); |
198 } | 195 } |
199 | 196 |
200 CoreTypes coreTypes; | |
201 ClassHierarchy hierarchy; | |
202 Program program; | 197 Program program; |
203 try { | 198 try { |
204 TranslateUri uriTranslator = | 199 TranslateUri uriTranslator = |
205 await TranslateUri.parse(fileSystem, patchedSdk, packages: packages); | 200 await TranslateUri.parse(fileSystem, patchedSdk, packages: packages); |
206 final Ticker ticker = new Ticker(isVerbose: verbose); | 201 final Ticker ticker = new Ticker(isVerbose: verbose); |
207 final DillTarget dillTarget = | 202 final DillTarget dillTarget = new DillTarget( |
208 new DillTarget(ticker, uriTranslator, backendTarget); | 203 ticker, uriTranslator, backendTarget, |
| 204 flags: new TargetFlags(strongMode: strongMode)); |
209 _appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill')); | 205 _appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill')); |
210 final KernelTarget kernelTarget = | 206 final KernelTarget kernelTarget = |
211 new KernelTarget(fileSystem, dillTarget, uriTranslator, strongMode); | 207 new KernelTarget(fileSystem, dillTarget, uriTranslator); |
212 kernelTarget.read(fileName); | 208 kernelTarget.read(fileName); |
213 await dillTarget.buildOutlines(); | 209 await dillTarget.buildOutlines(); |
214 await kernelTarget.buildOutlines(); | 210 await kernelTarget.buildOutlines(); |
215 coreTypes = kernelTarget.loader.coreTypes; | |
216 hierarchy = kernelTarget.loader.hierarchy; | |
217 program = await kernelTarget.buildProgram(); | 211 program = await kernelTarget.buildProgram(); |
218 if (kernelTarget.errors.isNotEmpty) { | 212 if (kernelTarget.errors.isNotEmpty) { |
219 return new CompilationResult.errors(kernelTarget.errors); | 213 return new CompilationResult.errors(kernelTarget.errors); |
220 } | 214 } |
221 } on InputError catch (e) { | 215 } on InputError catch (e) { |
222 return new CompilationResult.error(e.format()); | 216 return new CompilationResult.error(e.format()); |
223 } | 217 } |
224 | 218 |
225 if (program.mainMethod == null) { | 219 if (program.mainMethod == null) { |
226 return new CompilationResult.error("No 'main' method found."); | 220 return new CompilationResult.error("No 'main' method found."); |
227 } | 221 } |
228 | 222 |
229 // Perform target-specific transformations. | |
230 Target target = getTarget("vm", new TargetFlags(strongMode: false)); | |
231 target.performModularTransformations(coreTypes, hierarchy, program); | |
232 target.performGlobalTransformations(coreTypes, program); | |
233 | |
234 // Write the program to a list of bytes and return it. Do not include | 223 // Write the program to a list of bytes and return it. Do not include |
235 // libraries that have a dart: import URI. | 224 // libraries that have a dart: import URI. |
236 // | 225 // |
237 // TODO(kmillikin): This is intended to exclude platform libraries that are | 226 // TODO(kmillikin): This is intended to exclude platform libraries that are |
238 // included in the Kernel binary platform platform.dill. It does not | 227 // included in the Kernel binary platform platform.dill. It does not |
239 // necessarily exclude exactly the platform libraries. Use a better | 228 // necessarily exclude exactly the platform libraries. Use a better |
240 // predicate that knows what is included in platform.dill. | 229 // predicate that knows what is included in platform.dill. |
241 var sink = new ByteSink(); | 230 var sink = new ByteSink(); |
242 bool predicate(Library library) => !library.importUri.isScheme('dart'); | 231 bool predicate(Library library) => !library.importUri.isScheme('dart'); |
243 new LibraryFilteringBinaryPrinter(sink, predicate) | 232 new LibraryFilteringBinaryPrinter(sink, predicate) |
244 .writeProgramFile(program); | 233 .writeProgramFile(program); |
245 return new CompilationResult.ok(sink.builder.takeBytes()); | 234 return new CompilationResult.ok(sink.builder.takeBytes()); |
246 } catch (e, s) { | 235 } catch (e, s) { |
247 return reportCrash(e, s, fileName); | 236 return reportCrash(e, s, fileName); |
248 } | 237 } |
249 } | 238 } |
250 | 239 |
251 Future compilePlatform(Uri patchedSdk, Uri fullOutput, | 240 Future compilePlatform(Uri patchedSdk, Uri fullOutput, |
252 {Uri outlineOutput, Uri packages, bool verbose: false}) async { | 241 {Uri outlineOutput, |
| 242 Uri packages, |
| 243 bool verbose: false, |
| 244 String backendTarget}) async { |
| 245 backendTarget ??= "vm_fasta"; |
253 Ticker ticker = new Ticker(isVerbose: verbose); | 246 Ticker ticker = new Ticker(isVerbose: verbose); |
254 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { | 247 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { |
| 248 c.options.options["--target"] = backendTarget; |
255 c.options.options["--packages"] = packages; | 249 c.options.options["--packages"] = packages; |
256 if (verbose) { | 250 if (verbose) { |
257 c.options.options["--verbose"] = true; | 251 c.options.options["--verbose"] = true; |
258 } | 252 } |
259 return compilePlatformInternal( | 253 return compilePlatformInternal( |
260 c, ticker, patchedSdk, fullOutput, outlineOutput); | 254 c, ticker, patchedSdk, fullOutput, outlineOutput); |
261 }); | 255 }); |
262 } | 256 } |
263 | 257 |
264 Future writeDepsFile(Uri script, Uri depsFile, Uri output, | 258 Future writeDepsFile(Uri script, Uri depsFile, Uri output, |
265 {Uri sdk, | 259 {Uri sdk, |
266 Uri packages, | 260 Uri packages, |
267 Uri platform, | 261 Uri platform, |
268 Iterable<Uri> extraDependencies, | 262 Iterable<Uri> extraDependencies, |
269 bool verbose: false, | 263 bool verbose: false, |
270 String backendTarget}) async { | 264 String backendTarget}) async { |
271 backendTarget ??= "vm"; | 265 backendTarget ??= "vm_fasta"; |
272 Ticker ticker = new Ticker(isVerbose: verbose); | 266 Ticker ticker = new Ticker(isVerbose: verbose); |
273 await CompilerCommandLine.withGlobalOptions("", [""], | 267 await CompilerCommandLine.withGlobalOptions("", [""], |
274 (CompilerContext c) async { | 268 (CompilerContext c) async { |
275 c.options.options["--packages"] = packages; | 269 c.options.options["--packages"] = packages; |
276 if (verbose) { | 270 if (verbose) { |
277 c.options.options["--verbose"] = true; | 271 c.options.options["--verbose"] = true; |
278 } | 272 } |
279 sdk ??= c.options.sdk; | 273 sdk ??= c.options.sdk; |
280 | 274 |
281 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, | 275 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, |
282 packages: c.options.packages); | 276 packages: c.options.packages); |
283 ticker.logMs("Read packages file"); | 277 ticker.logMs("Read packages file"); |
284 DillTarget dillTarget = | 278 DillTarget dillTarget = new DillTarget(ticker, uriTranslator, backendTarget, |
285 new DillTarget(ticker, uriTranslator, backendTarget); | 279 flags: new TargetFlags(strongMode: false)); |
286 _appendDillForUri(dillTarget, platform); | 280 _appendDillForUri(dillTarget, platform); |
287 KernelTarget kernelTarget = new KernelTarget(PhysicalFileSystem.instance, | 281 KernelTarget kernelTarget = new KernelTarget( |
288 dillTarget, uriTranslator, false, c.uriToSource); | 282 PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); |
289 | 283 |
290 kernelTarget.read(script); | 284 kernelTarget.read(script); |
291 await dillTarget.buildOutlines(); | 285 await dillTarget.buildOutlines(); |
292 await kernelTarget.loader.buildOutlines(); | 286 await kernelTarget.loader.buildOutlines(); |
293 await kernelTarget.writeDepsFile(output, depsFile, | 287 await kernelTarget.writeDepsFile(output, depsFile, |
294 extraDependencies: extraDependencies); | 288 extraDependencies: extraDependencies); |
295 }); | 289 }); |
296 } | 290 } |
297 | 291 |
298 /// Load the [Program] from the given [uri] and append its libraries | 292 /// Load the [Program] from the given [uri] and append its libraries |
(...skipping 10 matching lines...) Expand all Loading... |
309 final BytesBuilder builder = new BytesBuilder(); | 303 final BytesBuilder builder = new BytesBuilder(); |
310 | 304 |
311 void add(List<int> data) { | 305 void add(List<int> data) { |
312 builder.add(data); | 306 builder.add(data); |
313 } | 307 } |
314 | 308 |
315 void close() { | 309 void close() { |
316 // Nothing to do. | 310 // Nothing to do. |
317 } | 311 } |
318 } | 312 } |
OLD | NEW |