Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_COMPILED_METHOD_H_ |
| 18 | #define ART_SRC_COMPILED_METHOD_H_ |
| 19 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 20 | #include <string> |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 23 | #include "instruction_set.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 24 | #include "utils.h" |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 25 | #include "UniquePtr.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 26 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 27 | namespace llvm { |
| 28 | class Function; |
| 29 | } |
| 30 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 33 | class CompiledCode { |
| 34 | public: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 35 | // For Quick to supply an code blob |
| 36 | CompiledCode(InstructionSet instruction_set, const std::vector<uint8_t>& code); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 37 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 38 | // For Portable to supply an ELF object |
| 39 | CompiledCode(InstructionSet instruction_set, |
| 40 | const std::string& elf_object, |
| 41 | const std::string &symbol); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 42 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 43 | InstructionSet GetInstructionSet() const { |
| 44 | return instruction_set_; |
| 45 | } |
| 46 | |
| 47 | const std::vector<uint8_t>& GetCode() const { |
| 48 | return code_; |
| 49 | } |
| 50 | |
| 51 | void SetCode(const std::vector<uint8_t>& code) { |
Logan Chien | 971bf3f | 2012-05-01 15:47:55 +0800 | [diff] [blame] | 52 | CHECK_NE(code.size(), 0U); |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 53 | code_ = code; |
| 54 | } |
| 55 | |
| 56 | bool operator==(const CompiledCode& rhs) const { |
| 57 | return (code_ == rhs.code_); |
| 58 | } |
| 59 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 60 | // To align an offset from a page-aligned value to make it suitable |
| 61 | // for code storage. For example on ARM, to ensure that PC relative |
| 62 | // valu computations work out as expected. |
| 63 | uint32_t AlignCode(uint32_t offset) const; |
| 64 | static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set); |
| 65 | |
| 66 | // returns the difference between the code address and a usable PC. |
| 67 | // mainly to cope with kThumb2 where the lower bit must be set. |
| 68 | size_t CodeDelta() const; |
| 69 | |
| 70 | // Returns a pointer suitable for invoking the code at the argument |
| 71 | // code_pointer address. Mainly to cope with kThumb2 where the |
| 72 | // lower bit must be set to indicate Thumb mode. |
| 73 | static const void* CodePointer(const void* code_pointer, |
| 74 | InstructionSet instruction_set); |
| 75 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 76 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 77 | const std::string& GetSymbol() const; |
| 78 | const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const; |
| 79 | void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset); |
| 80 | #endif |
| 81 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 82 | private: |
| 83 | const InstructionSet instruction_set_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 84 | |
| 85 | // Used to store the PIC code for Quick and an ELF image for portable. |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 86 | std::vector<uint8_t> code_; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 87 | |
| 88 | // Used for the Portable ELF symbol name. |
| 89 | std::string symbol_; |
| 90 | |
| 91 | // There are offsets from the oatdata symbol to where the offset to |
| 92 | // the compiled method will be found. These are computed by the |
| 93 | // OatWriter and then used by the ElfWriter to add relocations so |
| 94 | // that MCLinker can update the values to the location in the linked .so. |
| 95 | std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_; |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | class CompiledMethod : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 99 | public: |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 100 | // Constructs a CompiledMethod for the non-LLVM compilers. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 101 | CompiledMethod(InstructionSet instruction_set, |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 102 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 103 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 104 | const uint32_t core_spill_mask, |
| 105 | const uint32_t fp_spill_mask, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 106 | const std::vector<uint32_t>& mapping_table, |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 107 | const std::vector<uint16_t>& vmap_table, |
| 108 | const std::vector<uint8_t>& native_gc_map); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 109 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 110 | // Constructs a CompiledMethod for the JniCompiler. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 111 | CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 112 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 113 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 114 | const uint32_t core_spill_mask, |
| 115 | const uint32_t fp_spill_mask); |
| 116 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 117 | // Constructs a CompiledMethod for the Portable compiler. |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 118 | CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 119 | const std::string& code, |
| 120 | const std::vector<uint8_t>& gc_map, |
| 121 | const std::string& symbol) |
| 122 | : CompiledCode(instruction_set, code, symbol), |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 123 | frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0), |
| 124 | fp_spill_mask_(0), native_gc_map_(gc_map) { |
| 125 | } |
| 126 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 127 | // Constructs a CompiledMethod for the Portable JniCompiler. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 128 | CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 129 | const std::string& code, |
| 130 | const std::string& symbol) |
| 131 | : CompiledCode(instruction_set, code, symbol), |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 132 | frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0), |
| 133 | fp_spill_mask_(0) { |
| 134 | } |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 135 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 136 | ~CompiledMethod() {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 137 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 138 | size_t GetFrameSizeInBytes() const { |
| 139 | return frame_size_in_bytes_; |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 140 | } |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 141 | |
| 142 | uint32_t GetCoreSpillMask() const { |
| 143 | return core_spill_mask_; |
| 144 | } |
| 145 | |
| 146 | uint32_t GetFpSpillMask() const { |
| 147 | return fp_spill_mask_; |
| 148 | } |
| 149 | |
| 150 | const std::vector<uint32_t>& GetMappingTable() const { |
| 151 | return mapping_table_; |
| 152 | } |
| 153 | |
| 154 | const std::vector<uint16_t>& GetVmapTable() const { |
| 155 | return vmap_table_; |
| 156 | } |
| 157 | |
| 158 | const std::vector<uint8_t>& GetNativeGcMap() const { |
| 159 | return native_gc_map_; |
| 160 | } |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 161 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 162 | private: |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 163 | const size_t frame_size_in_bytes_; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 164 | const uint32_t core_spill_mask_; |
| 165 | const uint32_t fp_spill_mask_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 166 | std::vector<uint32_t> mapping_table_; |
| 167 | std::vector<uint16_t> vmap_table_; |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 168 | std::vector<uint8_t> native_gc_map_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 171 | class CompiledInvokeStub : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 172 | public: |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 173 | // Used by Quick to provide a blob of code. |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 174 | explicit CompiledInvokeStub(InstructionSet instruction_set, |
| 175 | const std::vector<uint8_t>& code); |
| 176 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame^] | 177 | // Used by Portable to provide ELF object. |
| 178 | explicit CompiledInvokeStub(InstructionSet instruction_set, |
| 179 | const std::string& elf_object, |
| 180 | const std::string& symbol); |
| 181 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 182 | ~CompiledInvokeStub() {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | } // namespace art |
| 186 | |
| 187 | #endif // ART_SRC_COMPILED_METHOD_H_ |