1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_COMPILER_OPTIMIZING_FAST_COMPILER_H_ 18 #define ART_COMPILER_OPTIMIZING_FAST_COMPILER_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include "arch/instruction_set.h" 24 #include "base/array_ref.h" 25 #include "base/macros.h" 26 #include "base/scoped_arena_containers.h" 27 #include "driver/compiler_options.h" 28 #include "handle_scope.h" 29 30 namespace art HIDDEN { 31 32 class ArenaAllocator; 33 class ArtMethod; 34 class DexCompilationUnit; 35 class VariableSizedHandleScope; 36 37 namespace mirror { 38 class Object; 39 } 40 41 /** 42 * A lightweight, one-pass compiler. Goes over each dex instruction and emits 43 * native code for it. 44 */ 45 class FastCompiler { 46 public: Compile(ArtMethod * method,ArenaAllocator * allocator,ArenaStack * arena_stack,VariableSizedHandleScope * handles,const CompilerOptions & compiler_options,const DexCompilationUnit & dex_compilation_unit)47 static std::unique_ptr<FastCompiler> Compile( 48 ArtMethod* method, 49 [[maybe_unused]] ArenaAllocator* allocator, 50 [[maybe_unused]] ArenaStack* arena_stack, 51 [[maybe_unused]] VariableSizedHandleScope* handles, 52 [[maybe_unused]] const CompilerOptions& compiler_options, 53 [[maybe_unused]] const DexCompilationUnit& dex_compilation_unit) { 54 if (method == nullptr) { 55 return nullptr; 56 } 57 switch (compiler_options.GetInstructionSet()) { 58 #ifdef ART_ENABLE_CODEGEN_arm64 59 case InstructionSet::kArm64: 60 return CompileARM64(method, 61 allocator, 62 arena_stack, 63 handles, 64 compiler_options, 65 dex_compilation_unit); 66 #endif 67 default: 68 return nullptr; 69 } 70 } 71 72 virtual ArrayRef<const uint8_t> GetCode() const = 0; 73 virtual ScopedArenaVector<uint8_t> BuildStackMaps() const = 0; 74 virtual ArrayRef<const uint8_t> GetCfiData() const = 0; 75 virtual int32_t GetFrameSize() const = 0; 76 virtual uint32_t GetNumberOfJitRoots() const = 0; 77 virtual void EmitJitRoots(uint8_t* code, 78 const uint8_t* roots_data, 79 /*out*/std::vector<Handle<mirror::Object>>* roots) 80 REQUIRES_SHARED(Locks::mutator_lock_) = 0; 81 82 virtual ~FastCompiler() = default; 83 84 private: 85 #ifdef ART_ENABLE_CODEGEN_arm64 86 static std::unique_ptr<FastCompiler> CompileARM64(ArtMethod* method, 87 ArenaAllocator* allocator, 88 ArenaStack* arena_stack, 89 VariableSizedHandleScope* handles, 90 const CompilerOptions& compiler_options, 91 const DexCompilationUnit& dex_compilation_unit); 92 #endif 93 }; 94 95 } // namespace art 96 97 #endif // ART_COMPILER_OPTIMIZING_FAST_COMPILER_H_ 98