blob: 29f004cf87ee18aa16142c40a61dd07681862f4e [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"
Mathieu Chartier193bad92013-08-29 18:46:00 -070020#include "driver/compiler_driver.h"
Vladimir Marko35831e82015-09-11 11:59:18 +010021#include "utils/swap_space.h"
Mathieu Chartier193bad92013-08-29 18:46:00 -070022
23namespace art {
24
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010025CompiledCode::CompiledCode(CompilerDriver* compiler_driver,
26 InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010027 const ArrayRef<const uint8_t>& quick_code)
28 : compiler_driver_(compiler_driver),
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000029 quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)),
30 packed_fields_(InstructionSetField::Encode(instruction_set)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031}
32
33CompiledCode::~CompiledCode() {
Vladimir Marko35831e82015-09-11 11:59:18 +010034 compiler_driver_->GetCompiledMethodStorage()->ReleaseCode(quick_code_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080035}
36
37bool 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 Rogersef7d42f2014-01-06 12:55:46 -080046 }
Elliott Hughes956af0f2014-12-11 14:34:28 -080047 return (rhs.quick_code_ == nullptr);
Mathieu Chartier193bad92013-08-29 18:46:00 -070048}
49
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080050size_t CompiledCode::AlignCode(size_t offset) const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000051 return AlignCode(offset, GetInstructionSet());
Mathieu Chartier193bad92013-08-29 18:46:00 -070052}
53
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080054size_t CompiledCode::AlignCode(size_t offset, InstructionSet instruction_set) {
Andreas Gampeaf13ad92014-04-11 12:07:48 -070055 return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
Mathieu Chartier193bad92013-08-29 18:46:00 -070056}
57
58size_t CompiledCode::CodeDelta() const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000059 return CodeDelta(GetInstructionSet());
Dave Allison50abf0a2014-06-23 13:19:59 -070060}
61
62size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
63 switch (instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +000064 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 Chartier193bad92013-08-29 18:46:00 -070070 return 0;
Vladimir Marko33bff252017-11-01 14:35:42 +000071 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070072 // +1 to set the low-order bit so a BLX will switch to Thumb mode
73 return 1;
74 }
75 default:
Dave Allison50abf0a2014-06-23 13:19:59 -070076 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Mathieu Chartier193bad92013-08-29 18:46:00 -070077 return 0;
78 }
79}
80
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010081const void* CompiledCode::CodePointer(const void* code_pointer, InstructionSet instruction_set) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070082 switch (instruction_set) {
Vladimir Marko33bff252017-11-01 14:35:42 +000083 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 Chartier193bad92013-08-29 18:46:00 -070089 return code_pointer;
Vladimir Marko33bff252017-11-01 14:35:42 +000090 case InstructionSet::kThumb2: {
Mathieu Chartier193bad92013-08-29 18:46:00 -070091 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 Chartier2cebb242015-04-21 16:50:40 -070098 return nullptr;
Mathieu Chartier193bad92013-08-29 18:46:00 -070099 }
100}
101
Ian Rogers72d32622014-05-06 16:20:11 -0700102CompiledMethod::CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700103 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800104 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800105 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800106 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100107 const ArrayRef<const linker::LinkerPatch>& patches)
Vladimir Marko35831e82015-09-11 11:59:18 +0100108 : CompiledCode(driver, instruction_set, quick_code),
Vladimir Marko35831e82015-09-11 11:59:18 +0100109 vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)),
Vladimir Marko35831e82015-09-11 11:59:18 +0100110 cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)),
111 patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700112}
113
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800114CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
115 CompilerDriver* driver,
116 InstructionSet instruction_set,
117 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800118 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800119 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100120 const ArrayRef<const linker::LinkerPatch>& patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100121 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800122 CompiledMethod* ret = alloc.allocate(1);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100123 alloc.construct(ret,
124 driver,
125 instruction_set,
126 quick_code,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100127 vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800128 cfi_info, patches);
129 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100130}
131
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800132void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100133 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->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 Marko35831e82015-09-11 11:59:18 +0100139 CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage();
140 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