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" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 18 | |
| 19 | #include "driver/compiled_method_storage.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 20 | #include "driver/compiler_driver.h" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 21 | #include "utils/swap_space.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 26 | const ArrayRef<const uint8_t>& quick_code) |
| 27 | : compiler_driver_(compiler_driver), |
| 28 | instruction_set_(instruction_set), |
| 29 | quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | CompiledCode::~CompiledCode() { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 33 | compiler_driver_->GetCompiledMethodStorage()->ReleaseCode(quick_code_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 49 | size_t CompiledCode::AlignCode(size_t offset) const { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 50 | return AlignCode(offset, instruction_set_); |
| 51 | } |
| 52 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 53 | size_t CompiledCode::AlignCode(size_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: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 66 | case kMips64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 67 | case kX86: |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 68 | case kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 69 | return 0; |
| 70 | case kThumb2: { |
| 71 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 72 | return 1; |
| 73 | } |
| 74 | default: |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 75 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | const void* CompiledCode::CodePointer(const void* code_pointer, |
| 81 | InstructionSet instruction_set) { |
| 82 | switch (instruction_set) { |
| 83 | case kArm: |
Stuart Monteith | b95a534 | 2014-03-12 13:32:32 +0000 | [diff] [blame] | 84 | case kArm64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 85 | case kMips: |
Andreas Gampe | 57b3429 | 2015-01-14 15:45:59 -0800 | [diff] [blame] | 86 | case kMips64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 87 | case kX86: |
Dmitry Petrochenko | fca8220 | 2014-03-21 11:21:37 +0700 | [diff] [blame] | 88 | case kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 89 | return code_pointer; |
| 90 | case kThumb2: { |
| 91 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 92 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 93 | address |= 0x1; |
| 94 | return reinterpret_cast<const void*>(address); |
| 95 | } |
| 96 | default: |
| 97 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 98 | return nullptr; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Ian Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 102 | CompiledMethod::CompiledMethod(CompilerDriver* driver, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 103 | InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 104 | const ArrayRef<const uint8_t>& quick_code, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 105 | const size_t frame_size_in_bytes, |
| 106 | const uint32_t core_spill_mask, |
| 107 | const uint32_t fp_spill_mask, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 108 | const ArrayRef<const SrcMapElem>& src_mapping_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 109 | const ArrayRef<const uint8_t>& mapping_table, |
| 110 | const ArrayRef<const uint8_t>& vmap_table, |
| 111 | const ArrayRef<const uint8_t>& native_gc_map, |
| 112 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 113 | const ArrayRef<const LinkerPatch>& patches) |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 114 | : CompiledCode(driver, instruction_set, quick_code), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | frame_size_in_bytes_(frame_size_in_bytes), core_spill_mask_(core_spill_mask), |
| 116 | fp_spill_mask_(fp_spill_mask), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 117 | src_mapping_table_( |
| 118 | driver->GetCompiledMethodStorage()->DeduplicateSrcMappingTable(src_mapping_table)), |
| 119 | mapping_table_(driver->GetCompiledMethodStorage()->DeduplicateMappingTable(mapping_table)), |
| 120 | vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)), |
| 121 | gc_map_(driver->GetCompiledMethodStorage()->DeduplicateGCMap(native_gc_map)), |
| 122 | cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)), |
| 123 | patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 126 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethod( |
| 127 | CompilerDriver* driver, |
| 128 | InstructionSet instruction_set, |
| 129 | const ArrayRef<const uint8_t>& quick_code, |
| 130 | const size_t frame_size_in_bytes, |
| 131 | const uint32_t core_spill_mask, |
| 132 | const uint32_t fp_spill_mask, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 133 | const ArrayRef<const SrcMapElem>& src_mapping_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 134 | const ArrayRef<const uint8_t>& mapping_table, |
| 135 | const ArrayRef<const uint8_t>& vmap_table, |
| 136 | const ArrayRef<const uint8_t>& native_gc_map, |
| 137 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | b207e14 | 2015-04-02 21:25:21 +0100 | [diff] [blame] | 138 | const ArrayRef<const LinkerPatch>& patches) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 139 | SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 140 | CompiledMethod* ret = alloc.allocate(1); |
| 141 | alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask, |
| 142 | fp_spill_mask, src_mapping_table, mapping_table, vmap_table, native_gc_map, |
| 143 | cfi_info, patches); |
| 144 | return ret; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 147 | void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 148 | SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 149 | alloc.destroy(m); |
| 150 | alloc.deallocate(m, 1); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 153 | CompiledMethod::~CompiledMethod() { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 154 | CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage(); |
| 155 | storage->ReleaseLinkerPatches(patches_); |
| 156 | storage->ReleaseCFIInfo(cfi_info_); |
| 157 | storage->ReleaseGCMap(gc_map_); |
| 158 | storage->ReleaseVMapTable(vmap_table_); |
| 159 | storage->ReleaseMappingTable(mapping_table_); |
| 160 | storage->ReleaseSrcMappingTable(src_mapping_table_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 163 | } // namespace art |