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 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 21 | CompiledCode::CompiledCode(InstructionSet instruction_set, const std::vector<uint8_t>& code) |
| 22 | : instruction_set_(instruction_set), code_(code) |
| 23 | { |
| 24 | CHECK_NE(code.size(), 0U); |
| 25 | } |
| 26 | |
| 27 | CompiledCode::CompiledCode(InstructionSet instruction_set, |
| 28 | const std::string& elf_object, |
| 29 | const std::string& symbol) |
| 30 | : instruction_set_(instruction_set), symbol_(symbol) { |
| 31 | CHECK_NE(elf_object.size(), 0U); |
| 32 | CHECK_NE(symbol.size(), 0U); |
| 33 | // TODO: we shouldn't just shove ELF objects in as "code" but |
| 34 | // change to have different kinds of compiled methods. This is |
| 35 | // being deferred until we work on hybrid execution or at least |
| 36 | // until we work on batch compilation. |
| 37 | code_.resize(elf_object.size()); |
| 38 | memcpy(&code_[0], &elf_object[0], elf_object.size()); |
| 39 | } |
| 40 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 41 | uint32_t CompiledCode::AlignCode(uint32_t offset) const { |
| 42 | return AlignCode(offset, instruction_set_); |
| 43 | } |
| 44 | |
| 45 | uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) { |
| 46 | switch (instruction_set) { |
| 47 | case kArm: |
| 48 | case kThumb2: |
| 49 | return RoundUp(offset, kArmAlignment); |
| 50 | case kMips: |
| 51 | return RoundUp(offset, kMipsAlignment); |
| 52 | case kX86: |
| 53 | return RoundUp(offset, kX86Alignment); |
| 54 | default: |
| 55 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | size_t CompiledCode::CodeDelta() const { |
| 61 | switch (instruction_set_) { |
| 62 | case kArm: |
| 63 | case kMips: |
| 64 | case kX86: |
| 65 | return 0; |
| 66 | case kThumb2: { |
| 67 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 68 | return 1; |
| 69 | } |
| 70 | default: |
| 71 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_; |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | const void* CompiledCode::CodePointer(const void* code_pointer, |
| 77 | InstructionSet instruction_set) { |
| 78 | switch (instruction_set) { |
| 79 | case kArm: |
| 80 | case kMips: |
| 81 | case kX86: |
| 82 | return code_pointer; |
| 83 | case kThumb2: { |
| 84 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 85 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 86 | address |= 0x1; |
| 87 | return reinterpret_cast<const void*>(address); |
| 88 | } |
| 89 | default: |
| 90 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 95 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 96 | const std::string& CompiledCode::GetSymbol() const { |
| 97 | CHECK_NE(0U, symbol_.size()); |
| 98 | return symbol_; |
| 99 | } |
| 100 | |
| 101 | const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const { |
| 102 | CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()) << symbol_; |
| 103 | return oatdata_offsets_to_compiled_code_offset_; |
| 104 | } |
| 105 | |
| 106 | void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) { |
| 107 | oatdata_offsets_to_compiled_code_offset_.push_back(offset); |
| 108 | } |
| 109 | #endif |
| 110 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 111 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Ian Rogers | ab058bb | 2012-03-11 22:19:38 -0700 | [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, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 116 | const std::vector<uint32_t>& mapping_table, |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 117 | const std::vector<uint16_t>& vmap_table, |
| 118 | const std::vector<uint8_t>& native_gc_map) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 119 | : CompiledCode(instruction_set, code), frame_size_in_bytes_(frame_size_in_bytes), |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 120 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask), |
| 121 | native_gc_map_(native_gc_map) |
Logan Chien | 6920bce | 2012-03-17 21:44:01 +0800 | [diff] [blame] | 122 | { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 123 | DCHECK_EQ(vmap_table.size(), |
| 124 | static_cast<uint32_t>(__builtin_popcount(core_spill_mask) |
| 125 | + __builtin_popcount(fp_spill_mask))); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 126 | 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] | 127 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 128 | std::vector<uint32_t> length_prefixed_mapping_table; |
| 129 | length_prefixed_mapping_table.push_back(mapping_table.size()); |
| 130 | length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(), |
| 131 | mapping_table.begin(), |
| 132 | mapping_table.end()); |
| 133 | DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size()); |
| 134 | |
| 135 | std::vector<uint16_t> length_prefixed_vmap_table; |
| 136 | length_prefixed_vmap_table.push_back(vmap_table.size()); |
| 137 | length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(), |
| 138 | vmap_table.begin(), |
| 139 | vmap_table.end()); |
| 140 | DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size()); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 141 | DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 142 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 143 | mapping_table_ = length_prefixed_mapping_table; |
| 144 | vmap_table_ = length_prefixed_vmap_table; |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 145 | 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] | 146 | } |
| 147 | |
| 148 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 149 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 150 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 151 | const uint32_t core_spill_mask, |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 152 | const uint32_t fp_spill_mask) |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 153 | : CompiledCode(instruction_set, code), |
| 154 | frame_size_in_bytes_(frame_size_in_bytes), |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 155 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 156 | |
Logan Chien | 598c513 | 2012-04-28 22:00:44 +0800 | [diff] [blame] | 157 | CompiledInvokeStub::CompiledInvokeStub(InstructionSet instruction_set, |
| 158 | const std::vector<uint8_t>& code) |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 159 | : CompiledCode(instruction_set, code) {} |
| 160 | |
| 161 | CompiledInvokeStub::CompiledInvokeStub(InstructionSet instruction_set, |
| 162 | const std::string& elf_object, |
| 163 | const std::string& symbol) |
| 164 | : CompiledCode(instruction_set, elf_object, symbol) {} |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 165 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 166 | } // namespace art |