blob: e41371855ddfafdb37f314d77aebf3d3580cab40 [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,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700105 const size_t frame_size_in_bytes,
106 const uint32_t core_spill_mask,
107 const uint32_t fp_spill_mask,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700108 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800109 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800110 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100111 const ArrayRef<const linker::LinkerPatch>& patches)
Vladimir Marko35831e82015-09-11 11:59:18 +0100112 : CompiledCode(driver, instruction_set, quick_code),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700113 frame_size_in_bytes_(frame_size_in_bytes),
114 core_spill_mask_(core_spill_mask),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115 fp_spill_mask_(fp_spill_mask),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700116 method_info_(driver->GetCompiledMethodStorage()->DeduplicateMethodInfo(method_info)),
Vladimir Marko35831e82015-09-11 11:59:18 +0100117 vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)),
Vladimir Marko35831e82015-09-11 11:59:18 +0100118 cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)),
119 patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700120}
121
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800122CompiledMethod* 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 Chartiercbcedbf2017-03-12 22:24:50 -0700129 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800130 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800131 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100132 const ArrayRef<const linker::LinkerPatch>& patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100133 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800134 CompiledMethod* ret = alloc.allocate(1);
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100135 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 Chartiercbcedbf2017-03-12 22:24:50 -0700142 method_info,
Vladimir Marko9d07e3d2016-03-31 12:02:28 +0100143 vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800144 cfi_info, patches);
145 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100146}
147
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800148void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100149 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800150 alloc.destroy(m);
151 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700152}
153
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154CompiledMethod::~CompiledMethod() {
Vladimir Marko35831e82015-09-11 11:59:18 +0100155 CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage();
156 storage->ReleaseLinkerPatches(patches_);
157 storage->ReleaseCFIInfo(cfi_info_);
Vladimir Marko35831e82015-09-11 11:59:18 +0100158 storage->ReleaseVMapTable(vmap_table_);
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700159 storage->ReleaseMethodInfo(method_info_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160}
161
Mathieu Chartier193bad92013-08-29 18:46:00 -0700162} // namespace art