| 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.outline; | 5 library fasta.outline; |
| 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 exitCode; | 11 import 'dart:io' show BytesBuilder, FileSystemEntity, exitCode; |
| 12 |
| 13 import 'package:kernel/binary/ast_to_binary.dart' show BinaryPrinter; |
| 14 |
| 15 import 'package:kernel/kernel.dart' show Program; |
| 16 |
| 17 import 'package:kernel/target/targets.dart' show Target, TargetFlags, getTarget; |
| 12 | 18 |
| 13 import 'kernel/verifier.dart' show verifyProgram; | 19 import 'kernel/verifier.dart' show verifyProgram; |
| 14 | 20 |
| 15 import 'compiler_command_line.dart' show CompilerCommandLine; | 21 import 'compiler_command_line.dart' show CompilerCommandLine; |
| 16 | 22 |
| 17 import 'compiler_context.dart' show CompilerContext; | 23 import 'compiler_context.dart' show CompilerContext; |
| 18 | 24 |
| 19 import 'errors.dart' show InputError, inputError; | 25 import 'errors.dart' show InputError, inputError; |
| 20 | 26 |
| 21 import 'kernel/kernel_target.dart' show KernelTarget; | 27 import 'kernel/kernel_target.dart' show KernelTarget; |
| 22 | 28 |
| 23 import 'dill/dill_target.dart' show DillTarget; | 29 import 'dill/dill_target.dart' show DillTarget; |
| 24 | 30 |
| 25 import 'ticker.dart' show Ticker; | 31 import 'ticker.dart' show Ticker; |
| 26 | 32 |
| 27 import 'translate_uri.dart' show TranslateUri; | 33 import 'translate_uri.dart' show TranslateUri; |
| 28 | 34 |
| 35 import 'vm.dart' show CompilationResult; |
| 36 |
| 29 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); | 37 const bool summary = const bool.fromEnvironment("summary", defaultValue: false); |
| 30 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); | 38 const int iterations = const int.fromEnvironment("iterations", defaultValue: 1); |
| 31 | 39 |
| 32 compileEntryPoint(List<String> arguments) async { | 40 compileEntryPoint(List<String> arguments) async { |
| 33 // Timing results for each iteration | 41 // Timing results for each iteration |
| 34 List<double> elapsedTimes = <double>[]; | 42 List<double> elapsedTimes = <double>[]; |
| 35 | 43 |
| 36 for (int i = 0; i < iterations; i++) { | 44 for (int i = 0; i < iterations; i++) { |
| 37 if (i > 0) { | 45 if (i > 0) { |
| 38 print("\n\n=== Iteration ${i+1} of $iterations"); | 46 print("\n\n=== Iteration ${i+1} of $iterations"); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 exitCode = 1; | 156 exitCode = 1; |
| 149 print("Verification of program failed: $e"); | 157 print("Verification of program failed: $e"); |
| 150 if (s != null && c.options.verbose) { | 158 if (s != null && c.options.verbose) { |
| 151 print(s); | 159 print(s); |
| 152 } | 160 } |
| 153 } | 161 } |
| 154 } | 162 } |
| 155 return uri; | 163 return uri; |
| 156 } | 164 } |
| 157 } | 165 } |
| 166 |
| 167 Future<CompilationResult> parseScript( |
| 168 Uri fileName, Uri packages, Uri patchedSdk, bool verbose) async { |
| 169 if (!FileSystemEntity.isFileSync(fileName.toFilePath())) { |
| 170 throw "Input file '${fileName.toFilePath()}' does not exist."; |
| 171 } |
| 172 |
| 173 if (!FileSystemEntity.isDirectorySync(patchedSdk.toFilePath())) { |
| 174 throw "Patched sdk directory not found at ${patchedSdk.toFilePath()}"; |
| 175 } |
| 176 |
| 177 Target target = getTarget("vm", new TargetFlags(strongMode: false)); |
| 178 |
| 179 Program program; |
| 180 final uriTranslator = await TranslateUri.parse(null, packages); |
| 181 final Ticker ticker = new Ticker(isVerbose: verbose); |
| 182 final DillTarget dillTarget = new DillTarget(ticker, uriTranslator); |
| 183 dillTarget.read(patchedSdk.resolve('platform.dill')); |
| 184 final KernelTarget kernelTarget = new KernelTarget(dillTarget, uriTranslator); |
| 185 try { |
| 186 kernelTarget.read(fileName); |
| 187 await dillTarget.writeOutline(null); |
| 188 program = await kernelTarget.writeOutline(null); |
| 189 program = await kernelTarget.writeProgram(null); |
| 190 if (kernelTarget.errors.isNotEmpty) { |
| 191 return new CompilationResult.error(kernelTarget.errors |
| 192 .map((err) => err.toString()) |
| 193 .toList(growable: false)); |
| 194 } |
| 195 } on InputError catch (e) { |
| 196 return new CompilationResult.error(<String>[e.format()]); |
| 197 } |
| 198 |
| 199 // Perform target-specific transformations. |
| 200 target.performModularTransformations(program); |
| 201 target.performGlobalTransformations(program); |
| 202 |
| 203 // Write the program to a list of bytes and return it. |
| 204 var sink = new ByteSink(); |
| 205 new BinaryPrinter(sink).writeProgramFile(program); |
| 206 return new CompilationResult.ok(sink.builder.takeBytes()); |
| 207 } |
| 208 |
| 209 // TODO(ahe): https://github.com/dart-lang/sdk/issues/28316 |
| 210 class ByteSink implements Sink<List<int>> { |
| 211 final BytesBuilder builder = new BytesBuilder(); |
| 212 |
| 213 void add(List<int> data) { |
| 214 builder.add(data); |
| 215 } |
| 216 |
| 217 void close() { |
| 218 // Nothing to do. |
| 219 } |
| 220 } |
| OLD | NEW |