blob: 9551d2298bd685d9a65addc3f6e4d595005afeba [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
25CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010026 const ArrayRef<const uint8_t>& quick_code)
27 : compiler_driver_(compiler_driver),
28 instruction_set_(instruction_set),
29 quick_code_(compiler_driver_->GetCompiledMethodStorage()->DeduplicateCode(quick_code)) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080030}
31
32CompiledCode::~CompiledCode() {
Vladimir Marko35831e82015-09-11 11:59:18 +010033 compiler_driver_->GetCompiledMethodStorage()->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 {
Mathieu Chartier193bad92013-08-29 18:46:00 -070050 return AlignCode(offset, instruction_set_);
51}
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 {
Dave Allison50abf0a2014-06-23 13:19:59 -070058 return CodeDelta(instruction_set_);
59}
60
61size_t CompiledCode::CodeDelta(InstructionSet instruction_set) {
62 switch (instruction_set) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070063 case kArm:
Stuart Monteithb95a5342014-03-12 13:32:32 +000064 case kArm64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070065 case kMips:
Andreas Gampe57b34292015-01-14 15:45:59 -080066 case kMips64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070067 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070068 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070069 return 0;
70 case kThumb2: {
71 // +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;
Mathieu Chartier193bad92013-08-29 18:46:00 -070076 return 0;
77 }
78}
79
80const void* CompiledCode::CodePointer(const void* code_pointer,
81 InstructionSet instruction_set) {
82 switch (instruction_set) {
83 case kArm:
Stuart Monteithb95a5342014-03-12 13:32:32 +000084 case kArm64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070085 case kMips:
Andreas Gampe57b34292015-01-14 15:45:59 -080086 case kMips64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070087 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070088 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070089 return code_pointer;
90 case kThumb2: {
91 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,
Vladimir Marko35831e82015-09-11 11:59:18 +0100108 const ArrayRef<const SrcMapElem>& src_mapping_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800109 const ArrayRef<const uint8_t>& mapping_table,
110 const ArrayRef<const uint8_t>& vmap_table,
111 const ArrayRef<const uint8_t>& native_gc_map,
112 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markob207e142015-04-02 21:25:21 +0100113 const ArrayRef<const LinkerPatch>& patches)
Vladimir Marko35831e82015-09-11 11:59:18 +0100114 : CompiledCode(driver, instruction_set, quick_code),
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115 frame_size_in_bytes_(frame_size_in_bytes), core_spill_mask_(core_spill_mask),
116 fp_spill_mask_(fp_spill_mask),
Vladimir Marko35831e82015-09-11 11:59:18 +0100117 src_mapping_table_(
118 driver->GetCompiledMethodStorage()->DeduplicateSrcMappingTable(src_mapping_table)),
119 mapping_table_(driver->GetCompiledMethodStorage()->DeduplicateMappingTable(mapping_table)),
120 vmap_table_(driver->GetCompiledMethodStorage()->DeduplicateVMapTable(vmap_table)),
121 gc_map_(driver->GetCompiledMethodStorage()->DeduplicateGCMap(native_gc_map)),
122 cfi_info_(driver->GetCompiledMethodStorage()->DeduplicateCFIInfo(cfi_info)),
123 patches_(driver->GetCompiledMethodStorage()->DeduplicateLinkerPatches(patches)) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700124}
125
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800126CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
127 CompilerDriver* driver,
128 InstructionSet instruction_set,
129 const ArrayRef<const uint8_t>& quick_code,
130 const size_t frame_size_in_bytes,
131 const uint32_t core_spill_mask,
132 const uint32_t fp_spill_mask,
Vladimir Marko35831e82015-09-11 11:59:18 +0100133 const ArrayRef<const SrcMapElem>& src_mapping_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800134 const ArrayRef<const uint8_t>& mapping_table,
135 const ArrayRef<const uint8_t>& vmap_table,
136 const ArrayRef<const uint8_t>& native_gc_map,
137 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markob207e142015-04-02 21:25:21 +0100138 const ArrayRef<const LinkerPatch>& patches) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100139 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800140 CompiledMethod* ret = alloc.allocate(1);
141 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
142 fp_spill_mask, src_mapping_table, mapping_table, vmap_table, native_gc_map,
143 cfi_info, patches);
144 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100145}
146
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800147void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
Vladimir Marko35831e82015-09-11 11:59:18 +0100148 SwapAllocator<CompiledMethod> alloc(driver->GetCompiledMethodStorage()->GetSwapSpaceAllocator());
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800149 alloc.destroy(m);
150 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700151}
152
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800153CompiledMethod::~CompiledMethod() {
Vladimir Marko35831e82015-09-11 11:59:18 +0100154 CompiledMethodStorage* storage = GetCompilerDriver()->GetCompiledMethodStorage();
155 storage->ReleaseLinkerPatches(patches_);
156 storage->ReleaseCFIInfo(cfi_info_);
157 storage->ReleaseGCMap(gc_map_);
158 storage->ReleaseVMapTable(vmap_table_);
159 storage->ReleaseMappingTable(mapping_table_);
160 storage->ReleaseSrcMappingTable(src_mapping_table_);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800161}
162
Mathieu Chartier193bad92013-08-29 18:46:00 -0700163} // namespace art