| 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, File, exitCode; |
| 12 | 12 |
| 13 import 'package:front_end/file_system.dart'; | |
| 14 import 'package:front_end/physical_file_system.dart'; | 13 import 'package:front_end/physical_file_system.dart'; |
| 15 import 'package:front_end/src/fasta/kernel/utils.dart'; | 14 import 'package:front_end/src/fasta/kernel/utils.dart'; |
| 16 import 'package:kernel/binary/ast_to_binary.dart' | |
| 17 show LibraryFilteringBinaryPrinter; | |
| 18 | 15 |
| 19 import 'package:kernel/kernel.dart' show Library, Program, loadProgramFromBytes; | 16 import 'package:kernel/kernel.dart' show Program, loadProgramFromBytes; |
| 20 | |
| 21 import 'package:kernel/target/targets.dart' show Target; | |
| 22 | 17 |
| 23 import 'compiler_command_line.dart' show CompilerCommandLine; | 18 import 'compiler_command_line.dart' show CompilerCommandLine; |
| 24 | 19 |
| 25 import 'compiler_context.dart' show CompilerContext; | 20 import 'compiler_context.dart' show CompilerContext; |
| 26 | 21 |
| 27 import 'errors.dart' show InputError, formatUnexpected, inputError, reportCrash; | 22 import 'errors.dart' show InputError, inputError; |
| 28 | 23 |
| 29 import 'kernel/kernel_target.dart' show KernelTarget; | 24 import 'kernel/kernel_target.dart' show KernelTarget; |
| 30 | 25 |
| 31 import 'dill/dill_target.dart' show DillTarget; | 26 import 'dill/dill_target.dart' show DillTarget; |
| 32 | 27 |
| 33 import 'compile_platform.dart' show compilePlatformInternal; | 28 import 'compile_platform.dart' show compilePlatformInternal; |
| 34 | 29 |
| 35 import 'ticker.dart' show Ticker; | 30 import 'ticker.dart' show Ticker; |
| 36 | 31 |
| 37 import 'translate_uri.dart' show TranslateUri; | 32 import 'translate_uri.dart' show TranslateUri; |
| 38 | 33 |
| 39 import 'vm.dart' show CompilationResult; | |
| 40 | |
| 41 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | 34 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
| 42 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | 35 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
| 43 | 36 |
| 44 compileEntryPoint(List<String> arguments) async { | 37 compileEntryPoint(List<String> arguments) async { |
| 45 // Timing results for each iteration | 38 // Timing results for each iteration |
| 46 List<double> elapsedTimes = <double>[]; | 39 List<double> elapsedTimes = <double>[]; |
| 47 | 40 |
| 48 for (int i = 0; i < iterations; i++) { | 41 for (int i = 0; i < iterations; i++) { |
| 49 if (i > 0) { | 42 if (i > 0) { |
| 50 print("\n\n=== Iteration ${i+1} of $iterations"); | 43 print("\n\n=== Iteration ${i+1} of $iterations"); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 var program = await kernelTarget.buildProgram(verify: c.options.verify); | 157 var program = await kernelTarget.buildProgram(verify: c.options.verify); |
| 165 if (c.options.dumpIr) { | 158 if (c.options.dumpIr) { |
| 166 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); | 159 printProgramText(program, libraryFilter: kernelTarget.isSourceLibrary); |
| 167 } | 160 } |
| 168 await writeProgramToFile(program, uri); | 161 await writeProgramToFile(program, uri); |
| 169 ticker.logMs("Wrote program to ${uri.toFilePath()}"); | 162 ticker.logMs("Wrote program to ${uri.toFilePath()}"); |
| 170 return uri; | 163 return uri; |
| 171 } | 164 } |
| 172 } | 165 } |
| 173 | 166 |
| 174 Future<CompilationResult> parseScript( | |
| 175 Uri fileName, Uri packages, Uri patchedSdk, Target backendTarget, | |
| 176 {bool verbose: false}) async { | |
| 177 return parseScriptInFileSystem(fileName, PhysicalFileSystem.instance, | |
| 178 packages, patchedSdk, backendTarget, | |
| 179 verbose: verbose); | |
| 180 } | |
| 181 | |
| 182 Future<CompilationResult> parseScriptInFileSystem(Uri fileName, | |
| 183 FileSystem fileSystem, Uri packages, Uri patchedSdk, Target backendTarget, | |
| 184 {bool verbose: false}) async { | |
| 185 try { | |
| 186 if (!await fileSystem.entityForUri(fileName).exists()) { | |
| 187 return new CompilationResult.error( | |
| 188 formatUnexpected(fileName, -1, "No such file.")); | |
| 189 } | |
| 190 if (!await new Directory.fromUri(patchedSdk).exists()) { | |
| 191 return new CompilationResult.error( | |
| 192 formatUnexpected(patchedSdk, -1, "Patched sdk directory not found.")); | |
| 193 } | |
| 194 | |
| 195 Program program; | |
| 196 try { | |
| 197 TranslateUri uriTranslator = | |
| 198 await TranslateUri.parse(fileSystem, patchedSdk, packages: packages); | |
| 199 final Ticker ticker = new Ticker(isVerbose: verbose); | |
| 200 final DillTarget dillTarget = | |
| 201 new DillTarget(ticker, uriTranslator, backendTarget); | |
| 202 _appendDillForUri(dillTarget, patchedSdk.resolve('platform.dill')); | |
| 203 final KernelTarget kernelTarget = | |
| 204 new KernelTarget(fileSystem, dillTarget, uriTranslator); | |
| 205 kernelTarget.read(fileName); | |
| 206 await dillTarget.buildOutlines(); | |
| 207 await kernelTarget.buildOutlines(); | |
| 208 program = await kernelTarget.buildProgram(); | |
| 209 if (kernelTarget.errors.isNotEmpty) { | |
| 210 return new CompilationResult.errors(kernelTarget.errors); | |
| 211 } | |
| 212 } on InputError catch (e) { | |
| 213 return new CompilationResult.error(e.format()); | |
| 214 } | |
| 215 | |
| 216 if (program.mainMethod == null) { | |
| 217 return new CompilationResult.error("No 'main' method found."); | |
| 218 } | |
| 219 | |
| 220 // Write the program to a list of bytes and return it. Do not include | |
| 221 // libraries that have a dart: import URI. | |
| 222 // | |
| 223 // TODO(kmillikin): This is intended to exclude platform libraries that are | |
| 224 // included in the Kernel binary platform platform.dill. It does not | |
| 225 // necessarily exclude exactly the platform libraries. Use a better | |
| 226 // predicate that knows what is included in platform.dill. | |
| 227 var sink = new ByteSink(); | |
| 228 bool predicate(Library library) => !library.importUri.isScheme('dart'); | |
| 229 new LibraryFilteringBinaryPrinter(sink, predicate) | |
| 230 .writeProgramFile(program); | |
| 231 return new CompilationResult.ok(sink.builder.takeBytes()); | |
| 232 } catch (e, s) { | |
| 233 return reportCrash(e, s, fileName); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 Future compilePlatform(Uri patchedSdk, Uri fullOutput, | 167 Future compilePlatform(Uri patchedSdk, Uri fullOutput, |
| 238 {Uri outlineOutput, | 168 {Uri outlineOutput, |
| 239 Uri packages, | 169 Uri packages, |
| 240 bool verbose: false, | 170 bool verbose: false, |
| 241 String backendTarget}) async { | 171 String backendTarget}) async { |
| 242 backendTarget ??= "vm_fasta"; | 172 backendTarget ??= "vm_fasta"; |
| 243 Ticker ticker = new Ticker(isVerbose: verbose); | 173 Ticker ticker = new Ticker(isVerbose: verbose); |
| 244 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { | 174 await CompilerCommandLine.withGlobalOptions("", [""], (CompilerContext c) { |
| 245 c.options.options["--target"] = backendTarget; | 175 c.options.options["--target"] = backendTarget; |
| 246 c.options.options["--packages"] = packages; | 176 c.options.options["--packages"] = packages; |
| 247 if (verbose) { | 177 if (verbose) { |
| 248 c.options.options["--verbose"] = true; | 178 c.options.options["--verbose"] = true; |
| 249 } | 179 } |
| 250 c.options.validate(); | 180 c.options.validate(); |
| 251 return compilePlatformInternal( | 181 return compilePlatformInternal( |
| 252 c, ticker, patchedSdk, fullOutput, outlineOutput); | 182 c, ticker, patchedSdk, fullOutput, outlineOutput); |
| 253 }); | 183 }); |
| 254 } | 184 } |
| 255 | 185 |
| 256 Future writeDepsFile(Uri script, Uri depsFile, Uri output, | 186 // TODO(sigmund): reimplement this API using the directive listener intead. |
| 187 Future<List<Uri>> getDependencies(Uri script, |
| 257 {Uri sdk, | 188 {Uri sdk, |
| 258 Uri packages, | 189 Uri packages, |
| 259 Uri platform, | 190 Uri platform, |
| 260 Iterable<Uri> extraDependencies, | |
| 261 bool verbose: false, | 191 bool verbose: false, |
| 262 String backendTarget}) async { | 192 String backendTarget}) async { |
| 263 backendTarget ??= "vm_fasta"; | 193 backendTarget ??= "vm_fasta"; |
| 264 Ticker ticker = new Ticker(isVerbose: verbose); | 194 Ticker ticker = new Ticker(isVerbose: verbose); |
| 265 await CompilerCommandLine.withGlobalOptions("", [""], | 195 return await CompilerCommandLine.withGlobalOptions("", [""], |
| 266 (CompilerContext c) async { | 196 (CompilerContext c) async { |
| 267 c.options.options["--target"] = backendTarget; | 197 c.options.options["--target"] = backendTarget; |
| 268 c.options.options["--strong-mode"] = false; | 198 c.options.options["--strong-mode"] = false; |
| 269 c.options.options["--packages"] = packages; | 199 c.options.options["--packages"] = packages; |
| 270 if (verbose) { | 200 if (verbose) { |
| 271 c.options.options["--verbose"] = true; | 201 c.options.options["--verbose"] = true; |
| 272 } | 202 } |
| 273 c.options.validate(); | 203 c.options.validate(); |
| 274 sdk ??= c.options.sdk; | 204 sdk ??= c.options.sdk; |
| 275 | 205 |
| 276 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, | 206 TranslateUri uriTranslator = await TranslateUri.parse(c.fileSystem, sdk, |
| 277 packages: c.options.packages); | 207 packages: c.options.packages); |
| 278 ticker.logMs("Read packages file"); | 208 ticker.logMs("Read packages file"); |
| 279 DillTarget dillTarget = | 209 DillTarget dillTarget = |
| 280 new DillTarget(ticker, uriTranslator, c.options.target); | 210 new DillTarget(ticker, uriTranslator, c.options.target); |
| 281 _appendDillForUri(dillTarget, platform); | 211 if (platform != null) _appendDillForUri(dillTarget, platform); |
| 282 KernelTarget kernelTarget = new KernelTarget( | 212 KernelTarget kernelTarget = new KernelTarget( |
| 283 PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); | 213 PhysicalFileSystem.instance, dillTarget, uriTranslator, c.uriToSource); |
| 284 | 214 |
| 285 kernelTarget.read(script); | 215 kernelTarget.read(script); |
| 286 await dillTarget.buildOutlines(); | 216 await dillTarget.buildOutlines(); |
| 287 await kernelTarget.loader.buildOutlines(); | 217 await kernelTarget.loader.buildOutlines(); |
| 288 await kernelTarget.writeDepsFile(output, depsFile, | 218 return await kernelTarget.loader.getDependencies(); |
| 289 extraDependencies: extraDependencies); | |
| 290 }); | 219 }); |
| 291 } | 220 } |
| 292 | 221 |
| 293 /// Load the [Program] from the given [uri] and append its libraries | 222 /// Load the [Program] from the given [uri] and append its libraries |
| 294 /// to the [dillTarget]. | 223 /// to the [dillTarget]. |
| 295 void _appendDillForUri(DillTarget dillTarget, Uri uri) { | 224 void _appendDillForUri(DillTarget dillTarget, Uri uri) { |
| 296 var bytes = new File.fromUri(uri).readAsBytesSync(); | 225 var bytes = new File.fromUri(uri).readAsBytesSync(); |
| 297 var platformProgram = loadProgramFromBytes(bytes); | 226 var platformProgram = loadProgramFromBytes(bytes); |
| 298 platformProgram.unbindCanonicalNames(); | 227 platformProgram.unbindCanonicalNames(); |
| 299 dillTarget.loader.appendLibraries(platformProgram); | 228 dillTarget.loader.appendLibraries(platformProgram); |
| 300 } | 229 } |
| 301 | 230 |
| 302 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 | 231 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 |
| 303 class ByteSink implements Sink<List<int>> { | 232 class ByteSink implements Sink<List<int>> { |
| 304 final BytesBuilder builder = new BytesBuilder(); | 233 final BytesBuilder builder = new BytesBuilder(); |
| 305 | 234 |
| 306 void add(List<int> data) { | 235 void add(List<int> data) { |
| 307 builder.add(data); | 236 builder.add(data); |
| 308 } | 237 } |
| 309 | 238 |
| 310 void close() { | 239 void close() { |
| 311 // Nothing to do. | 240 // Nothing to do. |
| 312 } | 241 } |
| 313 } | 242 } |
| OLD | NEW |