blob: 234e8b96f6ef33c3fd716cfc166e2664c6312ec6 [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"
18#include "driver/compiler_driver.h"
19
20namespace art {
21
22CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -080023 const ArrayRef<const uint8_t>& quick_code)
Ian Rogersef7d42f2014-01-06 12:55:46 -080024 : compiler_driver_(compiler_driver), instruction_set_(instruction_set),
Elliott Hughes956af0f2014-12-11 14:34:28 -080025 quick_code_(nullptr) {
26 SetCode(&quick_code);
Mathieu Chartier193bad92013-08-29 18:46:00 -070027}
28
Andreas Gampee21dc3d2014-12-08 16:59:43 -080029void CompiledCode::SetCode(const ArrayRef<const uint8_t>* quick_code) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080030 if (quick_code != nullptr) {
31 CHECK(!quick_code->empty());
32 quick_code_ = compiler_driver_->DeduplicateCode(*quick_code);
33 }
34}
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
49uint32_t CompiledCode::AlignCode(uint32_t offset) const {
50 return AlignCode(offset, instruction_set_);
51}
52
53uint32_t CompiledCode::AlignCode(uint32_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:
66 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070067 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070068 return 0;
69 case kThumb2: {
70 // +1 to set the low-order bit so a BLX will switch to Thumb mode
71 return 1;
72 }
73 default:
Dave Allison50abf0a2014-06-23 13:19:59 -070074 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Mathieu Chartier193bad92013-08-29 18:46:00 -070075 return 0;
76 }
77}
78
79const void* CompiledCode::CodePointer(const void* code_pointer,
80 InstructionSet instruction_set) {
81 switch (instruction_set) {
82 case kArm:
Stuart Monteithb95a5342014-03-12 13:32:32 +000083 case kArm64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070084 case kMips:
85 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070086 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070087 return code_pointer;
88 case kThumb2: {
89 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
90 // Set the low-order bit so a BLX will switch to Thumb mode
91 address |= 0x1;
92 return reinterpret_cast<const void*>(address);
93 }
94 default:
95 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
96 return NULL;
97 }
98}
99
Mathieu Chartier193bad92013-08-29 18:46:00 -0700100const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800101 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size());
Mathieu Chartier193bad92013-08-29 18:46:00 -0700102 return oatdata_offsets_to_compiled_code_offset_;
103}
104
105void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
106 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
107}
Mathieu Chartier193bad92013-08-29 18:46:00 -0700108
Ian Rogers72d32622014-05-06 16:20:11 -0700109CompiledMethod::CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700110 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800111 const ArrayRef<const uint8_t>& quick_code,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700112 const size_t frame_size_in_bytes,
113 const uint32_t core_spill_mask,
114 const uint32_t fp_spill_mask,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800115 DefaultSrcMap* src_mapping_table,
116 const ArrayRef<const uint8_t>& mapping_table,
117 const ArrayRef<const uint8_t>& vmap_table,
118 const ArrayRef<const uint8_t>& native_gc_map,
119 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markof4da6752014-08-01 19:04:18 +0100120 const ArrayRef<LinkerPatch>& patches)
Ian Rogers72d32622014-05-06 16:20:11 -0700121 : CompiledCode(driver, instruction_set, quick_code), frame_size_in_bytes_(frame_size_in_bytes),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700122 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800123 src_mapping_table_(src_mapping_table == nullptr ?
124 driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>()) :
125 driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>(src_mapping_table->Arrange()))),
126 mapping_table_(mapping_table.data() == nullptr ?
127 nullptr : driver->DeduplicateMappingTable(mapping_table)),
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700128 vmap_table_(driver->DeduplicateVMapTable(vmap_table)),
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800129 gc_map_(native_gc_map.data() == nullptr ? nullptr : driver->DeduplicateGCMap(native_gc_map)),
130 cfi_info_(cfi_info.data() == nullptr ? nullptr : driver->DeduplicateCFIInfo(cfi_info)),
131 patches_(patches.begin(), patches.end(), driver->GetSwapSpaceAllocator()) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700132}
133
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800134CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
135 CompilerDriver* driver,
136 InstructionSet instruction_set,
137 const ArrayRef<const uint8_t>& quick_code,
138 const size_t frame_size_in_bytes,
139 const uint32_t core_spill_mask,
140 const uint32_t fp_spill_mask,
141 DefaultSrcMap* src_mapping_table,
142 const ArrayRef<const uint8_t>& mapping_table,
143 const ArrayRef<const uint8_t>& vmap_table,
144 const ArrayRef<const uint8_t>& native_gc_map,
145 const ArrayRef<const uint8_t>& cfi_info,
146 const ArrayRef<LinkerPatch>& patches) {
147 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
148 CompiledMethod* ret = alloc.allocate(1);
149 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
150 fp_spill_mask, src_mapping_table, mapping_table, vmap_table, native_gc_map,
151 cfi_info, patches);
152 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100153}
154
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800155CompiledMethod* CompiledMethod::SwapAllocCompiledMethodStackMap(
156 CompilerDriver* driver,
157 InstructionSet instruction_set,
158 const ArrayRef<const uint8_t>& quick_code,
159 const size_t frame_size_in_bytes,
160 const uint32_t core_spill_mask,
161 const uint32_t fp_spill_mask,
162 const ArrayRef<const uint8_t>& stack_map) {
163 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
164 CompiledMethod* ret = alloc.allocate(1);
165 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
166 fp_spill_mask, nullptr, ArrayRef<const uint8_t>(), stack_map,
167 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(), ArrayRef<LinkerPatch>());
168 return ret;
169}
170
171CompiledMethod* CompiledMethod::SwapAllocCompiledMethodCFI(
172 CompilerDriver* driver,
173 InstructionSet instruction_set,
174 const ArrayRef<const uint8_t>& quick_code,
175 const size_t frame_size_in_bytes,
176 const uint32_t core_spill_mask,
177 const uint32_t fp_spill_mask,
178 const ArrayRef<const uint8_t>& cfi_info) {
179 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
180 CompiledMethod* ret = alloc.allocate(1);
181 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
182 fp_spill_mask, nullptr, ArrayRef<const uint8_t>(),
183 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(),
184 cfi_info, ArrayRef<LinkerPatch>());
185 return ret;
186}
187
188
189void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
190 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
191 alloc.destroy(m);
192 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700193}
194
Mathieu Chartier193bad92013-08-29 18:46:00 -0700195} // namespace art