blob: 22be28c4d9eaded63dc4579911646d87e9b63223 [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:
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;
98 return NULL;
99 }
100}
101
Mathieu Chartier193bad92013-08-29 18:46:00 -0700102const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
Elliott Hughes956af0f2014-12-11 14:34:28 -0800103 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size());
Mathieu Chartier193bad92013-08-29 18:46:00 -0700104 return oatdata_offsets_to_compiled_code_offset_;
105}
106
107void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
108 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
109}
Mathieu Chartier193bad92013-08-29 18:46:00 -0700110
Ian Rogers72d32622014-05-06 16:20:11 -0700111CompiledMethod::CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700112 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800113 const ArrayRef<const uint8_t>& quick_code,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700114 const size_t frame_size_in_bytes,
115 const uint32_t core_spill_mask,
116 const uint32_t fp_spill_mask,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800117 DefaultSrcMap* src_mapping_table,
118 const ArrayRef<const uint8_t>& mapping_table,
119 const ArrayRef<const uint8_t>& vmap_table,
120 const ArrayRef<const uint8_t>& native_gc_map,
121 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markof4da6752014-08-01 19:04:18 +0100122 const ArrayRef<LinkerPatch>& patches)
Ian Rogers72d32622014-05-06 16:20:11 -0700123 : CompiledCode(driver, instruction_set, quick_code), frame_size_in_bytes_(frame_size_in_bytes),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700124 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800125 src_mapping_table_(src_mapping_table == nullptr ?
126 driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>()) :
127 driver->DeduplicateSrcMappingTable(ArrayRef<SrcMapElem>(src_mapping_table->Arrange()))),
128 mapping_table_(mapping_table.data() == nullptr ?
129 nullptr : driver->DeduplicateMappingTable(mapping_table)),
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700130 vmap_table_(driver->DeduplicateVMapTable(vmap_table)),
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800131 gc_map_(native_gc_map.data() == nullptr ? nullptr : driver->DeduplicateGCMap(native_gc_map)),
132 cfi_info_(cfi_info.data() == nullptr ? nullptr : driver->DeduplicateCFIInfo(cfi_info)),
133 patches_(patches.begin(), patches.end(), driver->GetSwapSpaceAllocator()) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700134}
135
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800136CompiledMethod* CompiledMethod::SwapAllocCompiledMethod(
137 CompilerDriver* driver,
138 InstructionSet instruction_set,
139 const ArrayRef<const uint8_t>& quick_code,
140 const size_t frame_size_in_bytes,
141 const uint32_t core_spill_mask,
142 const uint32_t fp_spill_mask,
143 DefaultSrcMap* src_mapping_table,
144 const ArrayRef<const uint8_t>& mapping_table,
145 const ArrayRef<const uint8_t>& vmap_table,
146 const ArrayRef<const uint8_t>& native_gc_map,
147 const ArrayRef<const uint8_t>& cfi_info,
148 const ArrayRef<LinkerPatch>& patches) {
149 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
150 CompiledMethod* ret = alloc.allocate(1);
151 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
152 fp_spill_mask, src_mapping_table, mapping_table, vmap_table, native_gc_map,
153 cfi_info, patches);
154 return ret;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100155}
156
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800157CompiledMethod* CompiledMethod::SwapAllocCompiledMethodStackMap(
158 CompilerDriver* driver,
159 InstructionSet instruction_set,
160 const ArrayRef<const uint8_t>& quick_code,
161 const size_t frame_size_in_bytes,
162 const uint32_t core_spill_mask,
163 const uint32_t fp_spill_mask,
164 const ArrayRef<const uint8_t>& stack_map) {
165 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
166 CompiledMethod* ret = alloc.allocate(1);
167 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
168 fp_spill_mask, nullptr, ArrayRef<const uint8_t>(), stack_map,
169 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(), ArrayRef<LinkerPatch>());
170 return ret;
171}
172
173CompiledMethod* CompiledMethod::SwapAllocCompiledMethodCFI(
174 CompilerDriver* driver,
175 InstructionSet instruction_set,
176 const ArrayRef<const uint8_t>& quick_code,
177 const size_t frame_size_in_bytes,
178 const uint32_t core_spill_mask,
179 const uint32_t fp_spill_mask,
180 const ArrayRef<const uint8_t>& cfi_info) {
181 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
182 CompiledMethod* ret = alloc.allocate(1);
183 alloc.construct(ret, driver, instruction_set, quick_code, frame_size_in_bytes, core_spill_mask,
184 fp_spill_mask, nullptr, ArrayRef<const uint8_t>(),
185 ArrayRef<const uint8_t>(), ArrayRef<const uint8_t>(),
186 cfi_info, ArrayRef<LinkerPatch>());
187 return ret;
188}
189
190
191void CompiledMethod::ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m) {
192 SwapAllocator<CompiledMethod> alloc(driver->GetSwapSpaceAllocator());
193 alloc.destroy(m);
194 alloc.deallocate(m, 1);
Mathieu Chartier193bad92013-08-29 18:46:00 -0700195}
196
Mathieu Chartier193bad92013-08-29 18:46:00 -0700197} // namespace art