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 | |
| 20 | #include <vector> |
| 21 | |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 22 | #include "instruction_set.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 23 | #include "utils.h" |
TDYa127 | eead4ac | 2012-06-03 07:15:25 -0700 | [diff] [blame] | 24 | #include "UniquePtr.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 25 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 26 | namespace llvm { |
| 27 | class Function; |
| 28 | } |
| 29 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame^] | 32 | class CompiledCode { |
| 33 | public: |
| 34 | CompiledCode(InstructionSet instruction_set) |
| 35 | : instruction_set_(instruction_set), elf_idx_(-1), elf_func_idx_(-1) { |
| 36 | } |
| 37 | |
| 38 | CompiledCode(InstructionSet instruction_set, const std::vector<uint8_t>& code) |
| 39 | : instruction_set_(instruction_set), code_(code), elf_idx_(-1), |
| 40 | elf_func_idx_(-1) { |
| 41 | CHECK_NE(code.size(), 0U); |
| 42 | } |
| 43 | |
| 44 | CompiledCode(InstructionSet instruction_set, |
| 45 | uint16_t elf_idx, |
| 46 | uint16_t elf_func_idx) |
| 47 | : instruction_set_(instruction_set), elf_idx_(elf_idx), |
| 48 | elf_func_idx_(elf_func_idx) { |
| 49 | } |
| 50 | |
| 51 | InstructionSet GetInstructionSet() const { |
| 52 | return instruction_set_; |
| 53 | } |
| 54 | |
| 55 | const std::vector<uint8_t>& GetCode() const { |
| 56 | return code_; |
| 57 | } |
| 58 | |
| 59 | void SetCode(const std::vector<uint8_t>& code) { |
| 60 | code_ = code; |
| 61 | } |
| 62 | |
| 63 | bool operator==(const CompiledCode& rhs) const { |
| 64 | return (code_ == rhs.code_); |
| 65 | } |
| 66 | |
| 67 | bool IsExecutableInElf() const { |
| 68 | return (elf_idx_ != static_cast<uint16_t>(-1u)); |
| 69 | } |
| 70 | |
| 71 | uint16_t GetElfIndex() const { |
| 72 | return elf_idx_; |
| 73 | } |
| 74 | |
| 75 | uint16_t GetElfFuncIndex() const { |
| 76 | return elf_func_idx_; |
| 77 | } |
| 78 | |
| 79 | // To align an offset from a page-aligned value to make it suitable |
| 80 | // for code storage. For example on ARM, to ensure that PC relative |
| 81 | // valu computations work out as expected. |
| 82 | uint32_t AlignCode(uint32_t offset) const; |
| 83 | static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set); |
| 84 | |
| 85 | // returns the difference between the code address and a usable PC. |
| 86 | // mainly to cope with kThumb2 where the lower bit must be set. |
| 87 | size_t CodeDelta() const; |
| 88 | |
| 89 | // Returns a pointer suitable for invoking the code at the argument |
| 90 | // code_pointer address. Mainly to cope with kThumb2 where the |
| 91 | // lower bit must be set to indicate Thumb mode. |
| 92 | static const void* CodePointer(const void* code_pointer, |
| 93 | InstructionSet instruction_set); |
| 94 | |
| 95 | private: |
| 96 | const InstructionSet instruction_set_; |
| 97 | std::vector<uint8_t> code_; |
| 98 | |
| 99 | // LLVM-specific fields |
| 100 | uint16_t elf_idx_; |
| 101 | uint16_t elf_func_idx_; |
| 102 | }; |
| 103 | |
| 104 | class CompiledMethod : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 105 | public: |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 106 | // Constructs a CompiledMethod for the non-LLVM compilers. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 107 | CompiledMethod(InstructionSet instruction_set, |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 108 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 109 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 110 | const uint32_t core_spill_mask, |
| 111 | const uint32_t fp_spill_mask, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 112 | const std::vector<uint32_t>& mapping_table, |
| 113 | const std::vector<uint16_t>& vmap_table); |
| 114 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 115 | // Sets the GC map for a CompiledMethod. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 116 | void SetGcMap(const std::vector<uint8_t>& gc_map); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 117 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 118 | // Constructs a CompiledMethod for the JniCompiler. |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 119 | CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 120 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 121 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 122 | const uint32_t core_spill_mask, |
| 123 | const uint32_t fp_spill_mask); |
| 124 | |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 125 | // Constructs a CompiledMethod for the LLVM compiler. |
Logan Chien | 937105a | 2012-04-02 02:37:37 +0800 | [diff] [blame] | 126 | CompiledMethod(InstructionSet instruction_set, |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame^] | 127 | uint16_t elf_idx, |
| 128 | uint16_t elf_func_idx) |
| 129 | : CompiledCode(instruction_set, elf_idx, elf_func_idx), |
| 130 | frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0), |
| 131 | fp_spill_mask_(0) { |
| 132 | } |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 133 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 134 | ~CompiledMethod(); |
| 135 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 136 | size_t GetFrameSizeInBytes() const; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 137 | uint32_t GetCoreSpillMask() const; |
| 138 | uint32_t GetFpSpillMask() const; |
| 139 | const std::vector<uint32_t>& GetMappingTable() const; |
| 140 | const std::vector<uint16_t>& GetVmapTable() const; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 141 | const std::vector<uint8_t>& GetGcMap() const; |
| 142 | |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 143 | #if defined(ART_USE_LLVM_COMPILER) |
| 144 | void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) { |
| 145 | frame_size_in_bytes_ = new_frame_size_in_bytes; |
| 146 | } |
| 147 | #endif |
| 148 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 149 | private: |
Logan Chien | 110bcba | 2012-04-16 19:11:28 +0800 | [diff] [blame] | 150 | size_t frame_size_in_bytes_; |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 151 | const uint32_t core_spill_mask_; |
| 152 | const uint32_t fp_spill_mask_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 153 | std::vector<uint32_t> mapping_table_; |
| 154 | std::vector<uint16_t> vmap_table_; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 155 | std::vector<uint8_t> gc_map_; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame^] | 158 | class CompiledInvokeStub : public CompiledCode { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 159 | public: |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame^] | 160 | explicit CompiledInvokeStub(InstructionSet instruction_set); |
| 161 | |
| 162 | explicit CompiledInvokeStub(InstructionSet instruction_set, |
| 163 | const std::vector<uint8_t>& code); |
| 164 | |
| 165 | explicit CompiledInvokeStub(InstructionSet instruction_set, |
| 166 | uint16_t elf_idx, |
| 167 | uint16_t elf_func_idx) |
| 168 | : CompiledCode(instruction_set, elf_idx, elf_func_idx) { |
| 169 | } |
| 170 | |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 171 | ~CompiledInvokeStub(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | } // namespace art |
| 175 | |
| 176 | #endif // ART_SRC_COMPILED_METHOD_H_ |