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" |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 20 | #include "utils/swap_space.h" |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 21 | |
| 22 | namespace art { |
| 23 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 24 | CompiledCode::CompiledCode(CompiledMethodStorage* storage, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 25 | InstructionSet instruction_set, |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 26 | const ArrayRef<const uint8_t>& quick_code) |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 27 | : storage_(storage), |
| 28 | quick_code_(storage->DeduplicateCode(quick_code)), |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 29 | packed_fields_(InstructionSetField::Encode(instruction_set)) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | CompiledCode::~CompiledCode() { |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 33 | GetStorage()->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 { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 50 | return AlignCode(offset, GetInstructionSet()); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 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 { |
Vladimir Marko | 92f7f3c | 2017-10-31 11:38:30 +0000 | [diff] [blame] | 58 | return CodeDelta(GetInstructionSet()); |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | size_t CompiledCode::CodeDelta(InstructionSet instruction_set) { |
| 62 | switch (instruction_set) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 63 | case InstructionSet::kArm: |
| 64 | case InstructionSet::kArm64: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 65 | case InstructionSet::kX86: |
| 66 | case InstructionSet::kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 67 | return 0; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 68 | case InstructionSet::kThumb2: { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 69 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 70 | return 1; |
| 71 | } |
| 72 | default: |
Dave Allison | 50abf0a | 2014-06-23 13:19:59 -0700 | [diff] [blame] | 73 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 74 | UNREACHABLE(); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 78 | const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 79 | switch (instruction_set) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 80 | case InstructionSet::kArm: |
| 81 | case InstructionSet::kArm64: |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 82 | case InstructionSet::kX86: |
| 83 | case InstructionSet::kX86_64: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 84 | return code_pointer; |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 85 | case InstructionSet::kThumb2: { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 86 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 87 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 88 | address |= 0x1; |
| 89 | return reinterpret_cast<const void*>(address); |
| 90 | } |
| 91 | default: |
| 92 | LOG(FATAL) << "Unknown InstructionSet: " << instruction_set; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 93 | UNREACHABLE(); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 97 | CompiledMethod::CompiledMethod(CompiledMethodStorage* storage, |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 98 | InstructionSet instruction_set, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 99 | const ArrayRef<const uint8_t>& quick_code, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 100 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 101 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 102 | const ArrayRef<const linker::LinkerPatch>& patches) |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 103 | : CompiledCode(storage, instruction_set, quick_code), |
| 104 | vmap_table_(storage->DeduplicateVMapTable(vmap_table)), |
| 105 | cfi_info_(storage->DeduplicateCFIInfo(cfi_info)), |
| 106 | patches_(storage->DeduplicateLinkerPatches(patches)) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 109 | CompiledMethod* CompiledMethod::SwapAllocCompiledMethod( |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 110 | CompiledMethodStorage* storage, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 111 | InstructionSet instruction_set, |
| 112 | const ArrayRef<const uint8_t>& quick_code, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 113 | const ArrayRef<const uint8_t>& vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 114 | const ArrayRef<const uint8_t>& cfi_info, |
Vladimir Marko | d8dbc8d | 2017-09-20 13:37:47 +0100 | [diff] [blame] | 115 | const ArrayRef<const linker::LinkerPatch>& patches) { |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 116 | SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 117 | CompiledMethod* ret = alloc.allocate(1); |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 118 | alloc.construct(ret, |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 119 | storage, |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 120 | instruction_set, |
| 121 | quick_code, |
Vladimir Marko | 9d07e3d | 2016-03-31 12:02:28 +0100 | [diff] [blame] | 122 | vmap_table, |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 123 | cfi_info, patches); |
| 124 | return ret; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 127 | void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage, |
| 128 | CompiledMethod* m) { |
| 129 | SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator()); |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 130 | alloc.destroy(m); |
| 131 | alloc.deallocate(m, 1); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 134 | CompiledMethod::~CompiledMethod() { |
Vladimir Marko | 33f7c8a | 2018-11-19 10:22:01 +0000 | [diff] [blame] | 135 | CompiledMethodStorage* storage = GetStorage(); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 136 | storage->ReleaseLinkerPatches(patches_); |
| 137 | storage->ReleaseCFIInfo(cfi_info_); |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 138 | storage->ReleaseVMapTable(vmap_table_); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 141 | } // namespace art |