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 | #include "compiled_method.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 21 | #if defined(ART_USE_LLVM_COMPILER) |
| 22 | CompiledMethod::CompiledMethod(art::InstructionSet instruction_set, |
| 23 | llvm::Function *func) |
| 24 | : instruction_set_(instruction_set), func_(func), frame_size_in_bytes_(0), |
| 25 | core_spill_mask_(0), fp_spill_mask_(0) { |
| 26 | } |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 27 | #endif |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 28 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 29 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 30 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 31 | const uint32_t core_spill_mask, |
| 32 | const uint32_t fp_spill_mask, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 33 | const std::vector<uint32_t>& mapping_table, |
| 34 | const std::vector<uint16_t>& vmap_table) |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 35 | : instruction_set_(instruction_set), frame_size_in_bytes_(frame_size_in_bytes), |
| 36 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) { |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 37 | CHECK_NE(code.size(), 0U); |
Ian Rogers | f7d9ad3 | 2012-03-13 18:45:39 -0700 | [diff] [blame] | 38 | CHECK_GE(vmap_table.size(), 1U); // should always contain an entry for LR |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 39 | DCHECK_EQ(vmap_table.size(), |
| 40 | static_cast<uint32_t>(__builtin_popcount(core_spill_mask) |
| 41 | + __builtin_popcount(fp_spill_mask))); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 42 | CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1 |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 43 | |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 44 | size_t code_byte_count = code.size() * sizeof(code[0]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 45 | std::vector<uint8_t> byte_code(code_byte_count); |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [diff] [blame] | 46 | memcpy(&byte_code[0], &code[0], code_byte_count); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 47 | |
| 48 | std::vector<uint32_t> length_prefixed_mapping_table; |
| 49 | length_prefixed_mapping_table.push_back(mapping_table.size()); |
| 50 | length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(), |
| 51 | mapping_table.begin(), |
| 52 | mapping_table.end()); |
| 53 | DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size()); |
| 54 | |
| 55 | std::vector<uint16_t> length_prefixed_vmap_table; |
| 56 | length_prefixed_vmap_table.push_back(vmap_table.size()); |
| 57 | length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(), |
| 58 | vmap_table.begin(), |
| 59 | vmap_table.end()); |
| 60 | DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size()); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 61 | DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 62 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 63 | code_ = byte_code; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 64 | mapping_table_ = length_prefixed_mapping_table; |
| 65 | vmap_table_ = length_prefixed_vmap_table; |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 66 | |
| 67 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask) + __builtin_popcount(fp_spill_mask))); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 70 | void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) { |
| 71 | CHECK_NE(gc_map.size(), 0U); |
| 72 | |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 73 | #if !defined(ART_USE_LLVM_COMPILER) |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 74 | // Should only be used with CompiledMethods created with the non-LLVM compilers. |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 75 | CHECK_NE(mapping_table_.size(), 0U); |
| 76 | CHECK_NE(vmap_table_.size(), 0U); |
Shih-wei Liao | d1fec81 | 2012-02-13 09:51:10 -0800 | [diff] [blame] | 77 | #endif |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 78 | |
Brian Carlstrom | 7541288 | 2012-01-18 01:26:54 -0800 | [diff] [blame] | 79 | gc_map_ = gc_map; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 82 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 83 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 84 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 85 | const uint32_t core_spill_mask, |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 86 | const uint32_t fp_spill_mask) |
| 87 | : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes), |
| 88 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 89 | CHECK_NE(code.size(), 0U); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | CompiledMethod::~CompiledMethod() {} |
| 93 | |
| 94 | InstructionSet CompiledMethod::GetInstructionSet() const { |
| 95 | return instruction_set_; |
| 96 | } |
| 97 | |
| 98 | const std::vector<uint8_t>& CompiledMethod::GetCode() const { |
| 99 | return code_; |
| 100 | } |
| 101 | |
| 102 | size_t CompiledMethod::GetFrameSizeInBytes() const { |
| 103 | return frame_size_in_bytes_; |
| 104 | } |
| 105 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 106 | uint32_t CompiledMethod::GetCoreSpillMask() const { |
| 107 | return core_spill_mask_; |
| 108 | } |
| 109 | |
| 110 | uint32_t CompiledMethod::GetFpSpillMask() const { |
| 111 | return fp_spill_mask_; |
| 112 | } |
| 113 | |
| 114 | const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const { |
| 115 | return mapping_table_; |
| 116 | } |
| 117 | |
| 118 | const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const { |
| 119 | return vmap_table_; |
| 120 | } |
| 121 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 122 | const std::vector<uint8_t>& CompiledMethod::GetGcMap() const { |
| 123 | return gc_map_; |
| 124 | } |
| 125 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 126 | uint32_t CompiledMethod::AlignCode(uint32_t offset) const { |
| 127 | return AlignCode(offset, instruction_set_); |
| 128 | } |
| 129 | |
| 130 | uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) { |
| 131 | switch (instruction_set) { |
| 132 | case kArm: |
| 133 | case kThumb2: |
| 134 | return RoundUp(offset, kArmAlignment); |
| 135 | case kX86: |
Ian Rogers | b41b33b | 2012-03-20 14:22:54 -0700 | [diff] [blame^] | 136 | return RoundUp(offset, kX86Alignment); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 137 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 138 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | size_t CompiledMethod::CodeDelta() const { |
| 144 | switch (instruction_set_) { |
| 145 | case kArm: |
| 146 | case kX86: |
| 147 | return 0; |
| 148 | case kThumb2: { |
| 149 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 150 | return 1; |
| 151 | } |
| 152 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 153 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set_); |
Brian Carlstrom | 413f9e0 | 2012-01-09 22:24:30 -0800 | [diff] [blame] | 154 | return 0; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | const void* CompiledMethod::CodePointer(const void* code_pointer, |
| 159 | InstructionSet instruction_set) { |
| 160 | switch (instruction_set) { |
| 161 | case kArm: |
| 162 | case kX86: |
| 163 | return code_pointer; |
| 164 | case kThumb2: { |
| 165 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 166 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 167 | address |= 0x1; |
| 168 | return reinterpret_cast<const void*>(address); |
| 169 | } |
| 170 | default: |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 171 | LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 172 | return NULL; |
| 173 | } |
| 174 | } |
| 175 | |
Logan Chien | f04364f | 2012-02-10 12:01:39 +0800 | [diff] [blame] | 176 | #if defined(ART_USE_LLVM_COMPILER) |
| 177 | CompiledInvokeStub::CompiledInvokeStub(llvm::Function* func) : func_(func) { |
| 178 | CHECK_NE(func, static_cast<llvm::Function*>(NULL)); |
| 179 | } |
Shih-wei Liao | c4c9881 | 2012-03-10 21:55:51 -0800 | [diff] [blame] | 180 | #endif |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 181 | CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) { |
| 182 | CHECK_NE(code.size(), 0U); |
| 183 | code_ = code; |
| 184 | } |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 185 | |
| 186 | CompiledInvokeStub::~CompiledInvokeStub() {} |
| 187 | |
| 188 | const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const { |
| 189 | return code_; |
| 190 | } |
| 191 | |
| 192 | } // namespace art |