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