blob: 58f7e4f227be45b12b6001501702595f34a761d9 [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:
65 case InstructionSet::kMips:
66 case InstructionSet::kMips64:
67 case InstructionSet::kX86:
68 case InstructionSet::kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070069 return 0;
Vladimir Marko33bff252017-11-01 14:35:42 +000070 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070071 // +1 to set the low-order bit so a BLX will switch to Thumb mode
72 return 1;
73 }
74 default:
Dave Allison50abf0a2014-06-23 13:19:59 -070075 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Elliott Hughesc1896c92018-11-29 11:33:18 -080076 UNREACHABLE();
Mathieu Chartier193bad92013-08-29 18:46:00 -070077 }
78}
79
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010080const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070081 switch (instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +000082 case InstructionSet::kArm:
83 case InstructionSet::kArm64:
84 case InstructionSet::kMips:
85 case InstructionSet::kMips64:
86 case InstructionSet::kX86:
87 case InstructionSet::kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070088 return code_pointer;
Vladimir Marko33bff252017-11-01 14:35:42 +000089 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070090 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
91 // Set the low-order bit so a BLX will switch to Thumb mode
92 address |= 0x1;
93 return reinterpret_cast<const void*>(address);
94 }
95 default:
96 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Elliott Hughesc1896c92018-11-29 11:33:18 -080097 UNREACHABLE();
Mathieu Chartier193bad92013-08-29 18:46:00 -070098 }
99}
100
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000101CompiledMethod::CompiledMethod(CompiledMethodStorage* storage,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700102 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800103 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800104 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800105 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100106 const ArrayRef<const linker::LinkerPatch>& patches)
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000107 : CompiledCode(storage, instruction_set, quick_code),
108 vmap_table_(storage->DeduplicateVMapTable(vmap_table)),
109 cfi_info_(storage->DeduplicateCFIInfo(cfi_info)),
110 patches_(storage->DeduplicateLinkerPatches(patches)) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700111}
112
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800113CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000114 CompiledMethodStorage* storage,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800115 InstructionSet instruction_set,
116 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800117 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800118 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100119 const ArrayRef<const linker::LinkerPatch>& patches) {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000120 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800121 CompiledMethod* ret = alloc.allocate(1);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100122 alloc.construct(ret,
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000123 storage,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100124 instruction_set,
125 quick_code,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100126 vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800127 cfi_info, patches);
128 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100129}
130
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000131void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage,
132 CompiledMethod* m) {
133 SwapAllocator<CompiledMethod> alloc(storage->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800134 alloc.destroy(m);
135 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700136}
137
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800138CompiledMethod::~CompiledMethod() {
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000139 CompiledMethodStorage* storage = GetStorage();
Vladimir Marko35831e82015-09-11 11:59:18 +0100140 storage->ReleaseLinkerPatches(patches_);
141 storage->ReleaseCFIInfo(cfi_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100142 storage->ReleaseVMapTable(vmap_table_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800143}
144
Mathieu Chartier193bad92013-08-29 18:46:00 -0700145} // namespace art