Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [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 | */ |
| 16 | |
| 17 | #include "compiled_method.h" |
| 18 | #include "driver/compiler_driver.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 23 | const ArrayRef<const uint8_t>& quick_code) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 24 | : compiler_driver_(compiler_driver), instruction_set_(instruction_set), |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 25 | quick_code_(nullptr) { |
| 26 | SetCode(&quick_code); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 29 | void CompiledCode::SetCode(const ArrayRef<const uint8_t>* quick_code) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 30 | if (quick_code != nullptr) { |
| 31 | CHECK(!quick_code->empty()); |
| 32 | quick_code_ = compiler_driver_->DeduplicateCode(*quick_code); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | bool CompiledCode::operator==(const CompiledCode& rhs) const { |
| 37 | if (quick_code_ != nullptr) { |
| 38 | if (rhs.quick_code_ == nullptr) { |
| 39 | return false; |
| 40 | } else if (quick_code_->size() != rhs.quick_code_->size()) { |
| 41 | return false; |
| 42 | } else { |
| 43 | return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin()); |
| 44 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 45 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 46 | return (rhs.quick_code_ == nullptr); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | uint32_t CompiledCode::AlignCode(uint32_t offset) const { |
| 50 | return AlignCode(offset, instruction_set_); |
| 51 | } |
| 52 | |
| 53 | uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) { |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 54 | return RoundUp(offset, GetInstructionSetAlignment(instruction_set)); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | size_t CompiledCode::CodeDelta() const { |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 58 | return CodeDelta(instruction_set_); |
| 59 | } |
| 60 | |
| 61 | size_t CompiledCode::CodeDelta(InstructionSet instruction_set) { |
| 62 | switch (instruction_set) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 63 | case kArm: |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 64 | case kArm64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 65 | case kMips: |
| 66 | case kX86: |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 67 | case kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 68 | return 0; |
| 69 | case kThumb2: { |
| 70 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 71 | return 1; |
| 72 | } |
| 73 | default: |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 74 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | const void* CompiledCode::CodePointer(const void* code_pointer, |
| 80 | InstructionSet instruction_set) { |
| 81 | switch (instruction_set) { |
| 82 | case kArm: |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 83 | case kArm64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 84 | case kMips: |
| 85 | case kX86: |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 86 | case kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 87 | return code_pointer; |
| 88 | case kThumb2: { |
| 89 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 90 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 91 | address |= 0x1; |
| 92 | return reinterpret_cast<const void*>(address); |
| 93 | } |
| 94 | default: |
| 95 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
| 96 | return NULL; |
| 97 | } |
| 98 | } |
| 99 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 100 | const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const { |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 101 | CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 102 | return oatdata_offsets_to_compiled_code_offset_; |
| 103 | } |
| 104 | |
| 105 | void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) { |
| 106 | oatdata_offsets_to_compiled_code_offset_.push_back(offset); |
| 107 | } |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 108 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 109 | CompiledMethod::CompiledMethod(CompilerDriver* driver, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 110 | InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 111 | const ArrayRef<const uint8_t>& quick_code, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 112 | const size_t frame_size_in_bytes, |
| 113 | const uint32_t core_spill_mask, |
| 114 | const uint32_t fp_spill_mask, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 115 | DefaultSrcMap* src_mapping_table, |
| 116 | const ArrayRef<const uint8_t>& mapping_table, |
| 117 | const ArrayRef<const uint8_t>& vmap_table, |
| 118 | const ArrayRef<const uint8_t>& native_gc_map, |
| 119 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 120 | const ArrayRef<LinkerPatch>& patches) |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 121 | : CompiledCode(driver, instruction_set, quick_code), frame_size_in_bytes_(frame_size_in_bytes), |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 122 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask), |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 123 | src_mapping_table_(src_mapping_table == nullptr ? |
| 124 | driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>()) : |
| 125 | driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>(src_mapping_table->Arrange()))), |
| 126 | mapping_table_(mapping_table.data() == nullptr ? |
| 127 | nullptr : driver->DeduplicateMappingTable(mapping_table)), |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 128 | vmap_table_(driver->DeduplicateVMapTable(vmap_table)), |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 129 | gc_map_(native_gc_map.data() == nullptr ? nullptr : driver->DeduplicateGCMap(native_gc_map)), |
| 130 | cfi_info_(cfi_info.data() == nullptr ? nullptr : driver->DeduplicateCFIInfo(cfi_info)), |
| 131 | patches_(patches.begin(), patches.end(), driver->GetSwapSpaceAllocator()) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 134 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethod( |
| 135 | CompilerDriver* driver, |
| 136 | InstructionSet instruction_set, |
| 137 | const ArrayRef<const uint8_t>& quick_code, |
| 138 | const size_t frame_size_in_bytes, |
| 139 | const uint32_t core_spill_mask, |
| 140 | const uint32_t fp_spill_mask, |
| 141 | DefaultSrcMap* src_mapping_table, |
| 142 | const ArrayRef<const uint8_t>& mapping_table, |
| 143 | const ArrayRef<const uint8_t>& vmap_table, |
| 144 | const ArrayRef<const uint8_t>& native_gc_map, |
| 145 | const ArrayRef<const uint8_t>& cfi_info, |
| 146 | const ArrayRef<LinkerPatch>& patches) { |
| 147 | SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator()); |
| 148 | CompiledMethod* ret = alloc.allocate(1); |
| 149 | alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask, |
| 150 | fp_spill_mask, src_mapping_table, mapping_table, vmap_table, native_gc_map, |
| 151 | cfi_info, patches); |
| 152 | return ret; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 155 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethodStackMap( |
| 156 | CompilerDriver* driver, |
| 157 | InstructionSet instruction_set, |
| 158 | const ArrayRef<const uint8_t>& quick_code, |
| 159 | const size_t frame_size_in_bytes, |
| 160 | const uint32_t core_spill_mask, |
| 161 | const uint32_t fp_spill_mask, |
| 162 | const ArrayRef<const uint8_t>& stack_map) { |
| 163 | SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator()); |
| 164 | CompiledMethod* ret = alloc.allocate(1); |
| 165 | alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask, |
| 166 | fp_spill_mask, nullptr, ArrayRef<const uint8_t>(), stack_map, |
| 167 | ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(), ArrayRef<LinkerPatch>()); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethodCFI( |
| 172 | CompilerDriver* driver, |
| 173 | InstructionSet instruction_set, |
| 174 | const ArrayRef<const uint8_t>& quick_code, |
| 175 | const size_t frame_size_in_bytes, |
| 176 | const uint32_t core_spill_mask, |
| 177 | const uint32_t fp_spill_mask, |
| 178 | const ArrayRef<const uint8_t>& cfi_info) { |
| 179 | SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator()); |
| 180 | CompiledMethod* ret = alloc.allocate(1); |
| 181 | alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask, |
| 182 | fp_spill_mask, nullptr, ArrayRef<const uint8_t>(), |
| 183 | ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(), |
| 184 | cfi_info, ArrayRef<LinkerPatch>()); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) { |
| 190 | SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator()); |
| 191 | alloc.destroy(m); |
| 192 | alloc.deallocate(m, 1); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 195 | } // namespace art |