blob: 03b87ef09ea7562e9aebda3886e9c542f592916c [file] [log] [blame]
Mathieu Chartier193bad92013-08-29 18:46:00 -07001/*
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 Marko35831e82015-09-11 11:59:18 +010018
19#include "driver/compiled_method_storage.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010020#include "utils/swap_space.h"
Mathieu Chartier193bad92013-08-29 18:46:00 -070021
22namespace art {
23
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000024CompiledCode::CompiledCode(CompiledMethodStorage* storage,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025 InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010026 const ArrayRef<const uint8_t>& quick_code)
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000027 : storage_(storage),
28 quick_code_(storage->DeduplicateCode(quick_code)),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000029 packed_fields_(InstructionSetField::Encode(instruction_set)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030}
31
32CompiledCode::~CompiledCode() {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000033 GetStorage()->ReleaseCode(quick_code_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080034}
35
36bool 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 Rogersef7d42f2014-01-06 12:55:46 -080045 }
Elliott Hughes956af0f2014-12-11 14:34:28 -080046 return (rhs.quick_code_ == nullptr);
Mathieu Chartier193bad92013-08-29 18:46:00 -070047}
48
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080049size_t CompiledCode::AlignCode(size_t offset) const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000050 return AlignCode(offset, GetInstructionSet());
Mathieu Chartier193bad92013-08-29 18:46:00 -070051}
52
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080053size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
Andreas Gampeaf13ad92014-04-11 12:07:48 -070054 return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
Mathieu Chartier193bad92013-08-29 18:46:00 -070055}
56
57size_t CompiledCode::CodeDelta() const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000058 return CodeDelta(GetInstructionSet());
Dave Allison50abf0a2014-06-23 13:19:59 -070059}
60
61size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
62 switch (instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +000063 case InstructionSet::kArm:
64 case InstructionSet::kArm64:
Vladimir Marko33bff252017-11-01 14:35:42 +000065 case InstructionSet::kX86:
66 case InstructionSet::kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070067 return 0;
Vladimir Marko33bff252017-11-01 14:35:42 +000068 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070069 // +1 to set the low-order bit so a BLX will switch to Thumb mode
70 return 1;
71 }
72 default:
Dave Allison50abf0a2014-06-23 13:19:59 -070073 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Elliott Hughesc1896c92018-11-29 11:33:18 -080074 UNREACHABLE();
Mathieu Chartier193bad92013-08-29 18:46:00 -070075 }
76}
77
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010078const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070079 switch (instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +000080 case InstructionSet::kArm:
81 case InstructionSet::kArm64:
Vladimir Marko33bff252017-11-01 14:35:42 +000082 case InstructionSet::kX86:
83 case InstructionSet::kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070084 return code_pointer;
Vladimir Marko33bff252017-11-01 14:35:42 +000085 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070086 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 Hughesc1896c92018-11-29 11:33:18 -080093 UNREACHABLE();
Mathieu Chartier193bad92013-08-29 18:46:00 -070094 }
95}
96
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000097CompiledMethod::CompiledMethod(CompiledMethodStorage* storage,
Mathieu Chartier193bad92013-08-29 18:46:00 -070098 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -080099 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800100 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800101 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100102 const ArrayRef<const linker::LinkerPatch>& patches)
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000103 : 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 Chartier193bad92013-08-29 18:46:00 -0700107}
108
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800109CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000110 CompiledMethodStorage* storage,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800111 InstructionSet instruction_set,
112 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800113 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800114 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100115 const ArrayRef<const linker::LinkerPatch>& patches) {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000116 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800117 CompiledMethod* ret = alloc.allocate(1);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100118 alloc.construct(ret,
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000119 storage,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100120 instruction_set,
121 quick_code,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100122 vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800123 cfi_info, patches);
124 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100125}
126
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000127void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage,
128 CompiledMethod* m) {
129 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800130 alloc.destroy(m);
131 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700132}
133
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800134CompiledMethod::~CompiledMethod() {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000135 CompiledMethodStorage* storage = GetStorage();
Vladimir Marko35831e82015-09-11 11:59:18 +0100136 storage->ReleaseLinkerPatches(patches_);
137 storage->ReleaseCFIInfo(cfi_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100138 storage->ReleaseVMapTable(vmap_table_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800139}
140
Mathieu Chartier193bad92013-08-29 18:46:00 -0700141} // namespace art