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 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 25 | CompiledCode::CompiledCode(CompilerDriver* compiler_driver, |
| 26 | InstructionSet instruction_set, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 27 | const ArrayRef<const uint8_t>& quick_code) |
| 28 | : compiler_driver_(compiler_driver), |
| 29 | instruction_set_(instruction_set), |
| 30 | quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | CompiledCode::~CompiledCode() { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 34 | compiler_driver_->GetCompiledMethodStorage()->ReleaseCode(quick_code_); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool CompiledCode::operator==(const CompiledCode& rhs) const { |
| 38 | if (quick_code_ != nullptr) { |
| 39 | if (rhs.quick_code_ == nullptr) { |
| 40 | return false; |
| 41 | } else if (quick_code_->size() != rhs.quick_code_->size()) { |
| 42 | return false; |
| 43 | } else { |
| 44 | return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin()); |
| 45 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 46 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 47 | return (rhs.quick_code_ == nullptr); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 50 | size_t CompiledCode::AlignCode(size_t offset) const { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 51 | return AlignCode(offset, instruction_set_); |
| 52 | } |
| 53 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 54 | size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) { |
Andreas Gampe | af13ad9 | 2014-04-11 12:07:48 -0700 | [diff] [blame] | 55 | return RoundUp(offset, GetInstructionSetAlignment(instruction_set)); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | size_t CompiledCode::CodeDelta() const { |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 59 | return CodeDelta(instruction_set_); |
| 60 | } |
| 61 | |
| 62 | size_t CompiledCode::CodeDelta(InstructionSet instruction_set) { |
| 63 | switch (instruction_set) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 64 | case InstructionSet::kArm: |
| 65 | case InstructionSet::kArm64: |
| 66 | case InstructionSet::kMips: |
| 67 | case InstructionSet::kMips64: |
| 68 | case InstructionSet::kX86: |
| 69 | case InstructionSet::kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 70 | return 0; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 71 | case InstructionSet::kThumb2: { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 72 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 73 | return 1; |
| 74 | } |
| 75 | default: |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 76 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 81 | const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 82 | switch (instruction_set) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 83 | case InstructionSet::kArm: |
| 84 | case InstructionSet::kArm64: |
| 85 | case InstructionSet::kMips: |
| 86 | case InstructionSet::kMips64: |
| 87 | case InstructionSet::kX86: |
| 88 | case InstructionSet::kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 89 | return code_pointer; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 90 | case InstructionSet::kThumb2: { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 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, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 108 | const ArrayRef<const uint8_t>& method_info, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 109 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 110 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 111 | const ArrayRef<const linker::LinkerPatch>& patches) |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 112 | : CompiledCode(driver, instruction_set, quick_code), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 113 | frame_size_in_bytes_(frame_size_in_bytes), |
| 114 | core_spill_mask_(core_spill_mask), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 115 | fp_spill_mask_(fp_spill_mask), |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 116 | method_info_(driver->GetCompiledMethodStorage()->DeduplicateMethodInfo(method_info)), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 117 | vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 118 | cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)), |
| 119 | patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 122 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethod( |
| 123 | CompilerDriver* driver, |
| 124 | InstructionSet instruction_set, |
| 125 | const ArrayRef<const uint8_t>& quick_code, |
| 126 | const size_t frame_size_in_bytes, |
| 127 | const uint32_t core_spill_mask, |
| 128 | const uint32_t fp_spill_mask, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 129 | const ArrayRef<const uint8_t>& method_info, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 130 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 131 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 132 | const ArrayRef<const linker::LinkerPatch>& patches) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 133 | SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 134 | CompiledMethod* ret = alloc.allocate(1); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 135 | alloc.construct(ret, |
| 136 | driver, |
| 137 | instruction_set, |
| 138 | quick_code, |
| 139 | frame_size_in_bytes, |
| 140 | core_spill_mask, |
| 141 | fp_spill_mask, |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 142 | method_info, |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 143 | vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 144 | cfi_info, patches); |
| 145 | return ret; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 148 | void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 149 | SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 150 | alloc.destroy(m); |
| 151 | alloc.deallocate(m, 1); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 154 | CompiledMethod::~CompiledMethod() { |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 155 | CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage(); |
| 156 | storage->ReleaseLinkerPatches(patches_); |
| 157 | storage->ReleaseCFIInfo(cfi_info_); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 158 | storage->ReleaseVMapTable(vmap_table_); |
Mathieu Chartier | cbcedbf | 2017-03-12 22:24:50 -0700 | [diff] [blame] | 159 | storage->ReleaseMethodInfo(method_info_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 162 | } // namespace art |