1 /* 2 * Copyright (C) 2011 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_COMPILED_METHOD_H_ 18 #define ART_COMPILER_COMPILED_METHOD_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "instruction_set.h" 25 #include "utils.h" 26 27 namespace llvm { 28 class Function; 29 } // namespace llvm 30 31 namespace art { 32 33 class CompilerDriver; 34 35 class CompiledCode { 36 public: 37 // For Quick to supply an code blob 38 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set, 39 const std::vector<uint8_t>& quick_code); 40 41 // For Portable to supply an ELF object 42 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set, 43 const std::string& elf_object, const std::string &symbol); 44 GetInstructionSet()45 InstructionSet GetInstructionSet() const { 46 return instruction_set_; 47 } 48 GetPortableCode()49 const std::vector<uint8_t>* GetPortableCode() const { 50 return portable_code_; 51 } 52 GetQuickCode()53 const std::vector<uint8_t>* GetQuickCode() const { 54 return quick_code_; 55 } 56 57 void SetCode(const std::vector<uint8_t>* quick_code, const std::vector<uint8_t>* portable_code); 58 59 bool operator==(const CompiledCode& rhs) const; 60 61 // To align an offset from a page-aligned value to make it suitable 62 // for code storage. For example on ARM, to ensure that PC relative 63 // valu computations work out as expected. 64 uint32_t AlignCode(uint32_t offset) const; 65 static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set); 66 67 // returns the difference between the code address and a usable PC. 68 // mainly to cope with kThumb2 where the lower bit must be set. 69 size_t CodeDelta() const; 70 static size_t CodeDelta(InstructionSet instruction_set); 71 72 // Returns a pointer suitable for invoking the code at the argument 73 // code_pointer address. Mainly to cope with kThumb2 where the 74 // lower bit must be set to indicate Thumb mode. 75 static const void* CodePointer(const void* code_pointer, 76 InstructionSet instruction_set); 77 78 const std::string& GetSymbol() const; 79 const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const; 80 void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset); 81 82 private: 83 CompilerDriver* const compiler_driver_; 84 85 const InstructionSet instruction_set_; 86 87 // The ELF image for portable. 88 std::vector<uint8_t>* portable_code_; 89 90 // Used to store the PIC code for Quick. 91 std::vector<uint8_t>* quick_code_; 92 93 // Used for the Portable ELF symbol name. 94 const std::string symbol_; 95 96 // There are offsets from the oatdata symbol to where the offset to 97 // the compiled method will be found. These are computed by the 98 // OatWriter and then used by the ElfWriter to add relocations so 99 // that MCLinker can update the values to the location in the linked .so. 100 std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_; 101 }; 102 103 class CompiledMethod : public CompiledCode { 104 public: 105 // Constructs a CompiledMethod for the non-LLVM compilers. 106 CompiledMethod(CompilerDriver* driver, 107 InstructionSet instruction_set, 108 const std::vector<uint8_t>& quick_code, 109 const size_t frame_size_in_bytes, 110 const uint32_t core_spill_mask, 111 const uint32_t fp_spill_mask, 112 const std::vector<uint8_t>& mapping_table, 113 const std::vector<uint8_t>& vmap_table, 114 const std::vector<uint8_t>& native_gc_map, 115 const std::vector<uint8_t>* cfi_info); 116 117 // Constructs a CompiledMethod for the QuickJniCompiler. 118 CompiledMethod(CompilerDriver* driver, 119 InstructionSet instruction_set, 120 const std::vector<uint8_t>& quick_code, 121 const size_t frame_size_in_bytes, 122 const uint32_t core_spill_mask, 123 const uint32_t fp_spill_mask); 124 125 // Constructs a CompiledMethod for the Portable compiler. 126 CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set, const std::string& code, 127 const std::vector<uint8_t>& gc_map, const std::string& symbol); 128 129 // Constructs a CompiledMethod for the Portable JniCompiler. 130 CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set, const std::string& code, 131 const std::string& symbol); 132 ~CompiledMethod()133 ~CompiledMethod() {} 134 GetFrameSizeInBytes()135 size_t GetFrameSizeInBytes() const { 136 return frame_size_in_bytes_; 137 } 138 GetCoreSpillMask()139 uint32_t GetCoreSpillMask() const { 140 return core_spill_mask_; 141 } 142 GetFpSpillMask()143 uint32_t GetFpSpillMask() const { 144 return fp_spill_mask_; 145 } 146 GetMappingTable()147 const std::vector<uint8_t>& GetMappingTable() const { 148 DCHECK(mapping_table_ != nullptr); 149 return *mapping_table_; 150 } 151 GetVmapTable()152 const std::vector<uint8_t>& GetVmapTable() const { 153 DCHECK(vmap_table_ != nullptr); 154 return *vmap_table_; 155 } 156 GetGcMap()157 const std::vector<uint8_t>& GetGcMap() const { 158 DCHECK(gc_map_ != nullptr); 159 return *gc_map_; 160 } 161 GetCFIInfo()162 const std::vector<uint8_t>* GetCFIInfo() const { 163 return cfi_info_; 164 } 165 166 private: 167 // For quick code, the size of the activation used by the code. 168 const size_t frame_size_in_bytes_; 169 // For quick code, a bit mask describing spilled GPR callee-save registers. 170 const uint32_t core_spill_mask_; 171 // For quick code, a bit mask describing spilled FPR callee-save registers. 172 const uint32_t fp_spill_mask_; 173 // For quick code, a uleb128 encoded map from native PC offset to dex PC aswell as dex PC to 174 // native PC offset. Size prefixed. 175 std::vector<uint8_t>* mapping_table_; 176 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed. 177 std::vector<uint8_t>* vmap_table_; 178 // For quick code, a map keyed by native PC indices to bitmaps describing what dalvik registers 179 // are live. For portable code, the key is a dalvik PC. 180 std::vector<uint8_t>* gc_map_; 181 // For quick code, a FDE entry for the debug_frame section. 182 std::vector<uint8_t>* cfi_info_; 183 }; 184 185 } // namespace art 186 187 #endif // ART_COMPILER_COMPILED_METHOD_H_ 188