[vm/bytecode] Wire up bytecode generation in kernel service

In order to simplify testing and performance comparison, it is useful to
be able to run a command-line VM with a Dart source and use bytecode pipeline.
The bytecode generation is turned on in kernel service if --enable-interpreter
or --use-bytecode-compiler VM option is specified.

Change-Id: I6eb222b4df1721075c08d5f48c6d299ec779cb8c
Reviewed-on: https://dart-review.googlesource.com/c/80960
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: RĂ©gis Crelier <regis@google.com>
diff --git a/pkg/vm/bin/kernel_service.dart b/pkg/vm/bin/kernel_service.dart
index 7f9d55f..01d4c2c 100644
--- a/pkg/vm/bin/kernel_service.dart
+++ b/pkg/vm/bin/kernel_service.dart
@@ -30,7 +30,9 @@
 import 'package:front_end/src/api_unstable/vm.dart';
 import 'package:kernel/kernel.dart' show Component, Procedure;
 import 'package:kernel/target/targets.dart' show TargetFlags;
+import 'package:vm/bytecode/gen_bytecode.dart' show generateBytecode;
 import 'package:vm/incremental_compiler.dart';
+import 'package:vm/kernel_front_end.dart' show runWithFrontEndCompilerContext;
 import 'package:vm/http_filesystem.dart';
 import 'package:vm/target/vm.dart' show VmTarget;
 
@@ -67,7 +69,7 @@
 
   Compiler(this.fileSystem, Uri platformKernelPath,
       {bool suppressWarnings: false,
-      bool syncAsync: false,
+      bool bytecode: false,
       String packageConfig: null}) {
     Uri packagesUri = null;
     if (packageConfig != null) {
@@ -89,6 +91,7 @@
       ..packagesFileUri = packagesUri
       ..sdkSummary = platformKernelPath
       ..verbose = verbose
+      ..bytecode = bytecode
       ..onDiagnostic = (DiagnosticMessage message) {
         bool printMessage;
         switch (message.severity) {
@@ -114,7 +117,18 @@
   }
 
   Future<Component> compile(Uri script) {
-    return runWithPrintToStderr(() => compileInternal(script));
+    return runWithPrintToStderr(() async {
+      final component = await compileInternal(script);
+
+      if (options.bytecode && errors.isEmpty) {
+        await runWithFrontEndCompilerContext(script, options, component, () {
+          // TODO(alexmarkov): pass environment defines
+          generateBytecode(component);
+        });
+      }
+
+      return component;
+    });
   }
 
   Future<Component> compileInternal(Uri script);
@@ -124,9 +138,13 @@
   IncrementalCompiler generator;
 
   IncrementalCompilerWrapper(FileSystem fileSystem, Uri platformKernelPath,
-      {bool suppressWarnings: false, String packageConfig: null})
+      {bool suppressWarnings: false,
+      bool bytecode: false,
+      String packageConfig: null})
       : super(fileSystem, platformKernelPath,
-            suppressWarnings: suppressWarnings, packageConfig: packageConfig);
+            suppressWarnings: suppressWarnings,
+            bytecode: bytecode,
+            packageConfig: packageConfig);
 
   @override
   Future<Component> compileInternal(Uri script) async {
@@ -147,9 +165,12 @@
   SingleShotCompilerWrapper(FileSystem fileSystem, Uri platformKernelPath,
       {this.requireMain: false,
       bool suppressWarnings: false,
+      bool bytecode: false,
       String packageConfig: null})
       : super(fileSystem, platformKernelPath,
-            suppressWarnings: suppressWarnings, packageConfig: packageConfig);
+            suppressWarnings: suppressWarnings,
+            bytecode: bytecode,
+            packageConfig: packageConfig);
 
   @override
   Future<Component> compileInternal(Uri script) async {
@@ -171,6 +192,7 @@
 Future<Compiler> lookupOrBuildNewIncrementalCompiler(int isolateId,
     List sourceFiles, Uri platformKernelPath, List<int> platformKernel,
     {bool suppressWarnings: false,
+    bool bytecode: false,
     String packageConfig: null,
     String multirootFilepaths,
     String multirootScheme}) async {
@@ -187,7 +209,9 @@
     // isolate needs to receive a message indicating that particular
     // isolate was shut down. Message should be handled here in this script.
     compiler = new IncrementalCompilerWrapper(fileSystem, platformKernelPath,
-        suppressWarnings: suppressWarnings, packageConfig: packageConfig);
+        suppressWarnings: suppressWarnings,
+        bytecode: bytecode,
+        packageConfig: packageConfig);
     isolateCompilers[isolateId] = compiler;
   }
   return compiler;
@@ -353,6 +377,7 @@
   final int isolateId = request[6];
   final List sourceFiles = request[7];
   final bool suppressWarnings = request[8];
+  final bool bytecode = request[9];
   final String packageConfig = request[10];
   final String multirootFilepaths = request[11];
   final String multirootScheme = request[12];
@@ -406,6 +431,7 @@
     compiler = await lookupOrBuildNewIncrementalCompiler(
         isolateId, sourceFiles, platformKernelPath, platformKernel,
         suppressWarnings: suppressWarnings,
+        bytecode: bytecode,
         packageConfig: packageConfig,
         multirootFilepaths: multirootFilepaths,
         multirootScheme: multirootScheme);
@@ -415,6 +441,7 @@
     compiler = new SingleShotCompilerWrapper(fileSystem, platformKernelPath,
         requireMain: false,
         suppressWarnings: suppressWarnings,
+        bytecode: bytecode,
         packageConfig: packageConfig);
   }
 
diff --git a/runtime/vm/kernel_isolate.cc b/runtime/vm/kernel_isolate.cc
index 02f38c88..c56df9b 100644
--- a/runtime/vm/kernel_isolate.cc
+++ b/runtime/vm/kernel_isolate.cc
@@ -448,10 +448,6 @@
     suppress_warnings.type = Dart_CObject_kBool;
     suppress_warnings.value.as_bool = FLAG_suppress_fe_warnings;
 
-    Dart_CObject dart_sync_async;
-    dart_sync_async.type = Dart_CObject_kBool;
-    dart_sync_async.value.as_bool = FLAG_sync_async;
-
     Dart_CObject* message_arr[] = {&tag,
                                    &send_port,
                                    &isolate_id,
@@ -461,8 +457,7 @@
                                    &library_uri_object,
                                    &class_object,
                                    &is_static_object,
-                                   &suppress_warnings,
-                                   &dart_sync_async};
+                                   &suppress_warnings};
     message.value.as_array.values = message_arr;
     message.value.as_array.length = ARRAY_SIZE(message_arr);
     // Send the message.
@@ -568,9 +563,16 @@
     suppress_warnings.type = Dart_CObject_kBool;
     suppress_warnings.value.as_bool = FLAG_suppress_fe_warnings;
 
-    Dart_CObject dart_sync_async;
-    dart_sync_async.type = Dart_CObject_kBool;
-    dart_sync_async.value.as_bool = FLAG_sync_async;
+    Dart_CObject bytecode;
+    bytecode.type = Dart_CObject_kBool;
+    // Interpreter is supported only on x64 and arm64.
+#if defined(TARGET_ARCH_X64) || defined(TARGET_ARCH_ARM64)
+    bytecode.value.as_bool =
+        FLAG_enable_interpreter || FLAG_use_bytecode_compiler;
+#else
+    bytecode.value.as_bool =
+        FLAG_use_bytecode_compiler && !FLAG_enable_interpreter;
+#endif
 
     Dart_CObject package_config_uri;
     if (package_config != NULL) {
@@ -616,7 +618,7 @@
                                    &isolate_id,
                                    &files,
                                    &suppress_warnings,
-                                   &dart_sync_async,
+                                   &bytecode,
                                    &package_config_uri,
                                    &multiroot_filepaths_object,
                                    &multiroot_scheme_object};