// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. library vm.bytecode.gen_bytecode; // TODO(askesc): We should not need to call the constant evaluator // explicitly once constant-update-2018 is shipped. import 'package:front_end/src/api_prototype/constant_evaluator.dart' show ConstantEvaluator, EvaluationEnvironment; import 'package:front_end/src/api_unstable/vm.dart' show CompilerContext, Severity, messageBytecodeLimitExceededTooManyArguments, noLength, templateIllegalRecursiveType; import 'package:kernel/ast.dart' hide MapEntry, Component, FunctionDeclaration; import 'package:kernel/ast.dart' as ast show Component, FunctionDeclaration; import 'package:kernel/class_hierarchy.dart' show ClassHierarchy; import 'package:kernel/core_types.dart' show CoreTypes; import 'package:kernel/external_name.dart' show getExternalName, getNativeExtensionUris; import 'package:kernel/library_index.dart' show LibraryIndex; import 'package:kernel/type_algebra.dart' show Substitution, containsTypeVariable; import 'package:kernel/type_environment.dart' show TypeEnvironment; import 'package:kernel/vm/constants_native_effects.dart' show VmConstantsBackend; import 'assembler.dart'; import 'bytecode_serialization.dart' show StringTable; import 'constant_pool.dart'; import 'dbc.dart'; import 'declarations.dart'; import 'exceptions.dart'; import 'generics.dart' show flattenInstantiatorTypeArguments, getDefaultFunctionTypeArguments, getInstantiatorTypeArguments, getStaticType, getTypeParameterTypes, hasFreeTypeParameters, hasInstantiatorTypeArguments, isAllDynamic, isInstantiatedInterfaceCall, isUncheckedCall, isUncheckedClosureCall; import 'local_variable_table.dart' show LocalVariableTable; import 'local_vars.dart' show LocalVariables; import 'nullability_detector.dart' show NullabilityDetector; import 'object_table.dart' show ObjectHandle, ObjectTable, NameAndType, topLevelClassName; import 'options.dart' show BytecodeOptions; import 'recognized_methods.dart' show RecognizedMethods; import 'recursive_types_validator.dart' show IllegalRecursiveTypeException; import 'source_positions.dart' show LineStarts, SourcePositions; import '../constants_error_reporter.dart' show ForwardConstantEvaluationErrors; import '../metadata/bytecode.dart'; import 'dart:convert' show utf8; import 'dart:math' as math; // This symbol is used as the name in assert assignable's to indicate it comes // from an explicit 'as' check. This will cause the runtime to throw the right // exception. const String symbolForTypeCast = ' in type cast'; void generateBytecode( ast.Component component, { BytecodeOptions options, List libraries, CoreTypes coreTypes, ClassHierarchy hierarchy, }) { options ??= new BytecodeOptions(); verifyBytecodeInstructionDeclarations(); coreTypes ??= new CoreTypes(component); void ignoreAmbiguousSupertypes(Class cls, Supertype a, Supertype b) {} hierarchy ??= new ClassHierarchy(component, onAmbiguousSupertypes: ignoreAmbiguousSupertypes); final typeEnvironment = new TypeEnvironment(coreTypes, hierarchy); final constantsBackend = new VmConstantsBackend(coreTypes); final errorReporter = new ForwardConstantEvaluationErrors(); final constantEvaluator = new ConstantEvaluator(constantsBackend, options.environmentDefines, typeEnvironment, errorReporter); libraries ??= component.libraries; try { final bytecodeGenerator = new BytecodeGenerator(component, coreTypes, hierarchy, typeEnvironment, constantEvaluator, options); for (var library in libraries) { bytecodeGenerator.visitLibrary(library); } } on IllegalRecursiveTypeException catch (e) { CompilerContext.current.options.report( templateIllegalRecursiveType.withArguments(e.type).withoutLocation(), Severity.error); } } class BytecodeGenerator extends RecursiveVisitor { final CoreTypes coreTypes; final ClassHierarchy hierarchy; final TypeEnvironment typeEnvironment; final ConstantEvaluator constantEvaluator; final BytecodeOptions options; final BytecodeMetadataRepository metadata = new BytecodeMetadataRepository(); final RecognizedMethods recognizedMethods; final int formatVersion; final Map astUriToSource; StringTable stringTable; ObjectTable objectTable; Component bytecodeComponent; NullabilityDetector nullabilityDetector; List classDeclarations; List fieldDeclarations; List functionDeclarations; Class enclosingClass; Member enclosingMember; FunctionNode enclosingFunction; FunctionNode parentFunction; bool isClosure; Set classTypeParameters; List functionTypeParameters; Set functionTypeParametersSet; List instantiatorTypeArguments; LocalVariables locals; Map labeledStatements; Map switchCases; Map tryCatches; Map> finallyBlocks; List