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