blob: 9b1ef172749b8b973cf4bea4289772fb1e1835c5 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 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 "code_generator.h"
18
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010039size_t CodeGenerator::GetCacheOffset(uint32_t index) {
40 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
41}
42
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010043void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000044 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010045 if (!is_leaf) {
46 MarkNotLeaf();
47 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000048 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
49 + GetGraph()->GetTemporariesVRegSlots()
50 + 1 /* filler */,
51 0, /* the baseline compiler does not have live registers at slow path */
52 0, /* the baseline compiler does not have live registers at slow path */
53 GetGraph()->GetMaximumNumberOfOutVRegs()
54 + 1 /* current method */,
55 GetGraph()->GetBlocks());
56 CompileInternal(allocator, /* is_baseline */ true);
57}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000059bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
60 DCHECK_EQ(block_order_->Get(current_block_index_), current);
61 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
62}
63
64HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
65 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
66 HBasicBlock* block = block_order_->Get(i);
David Brazdil46e2a392015-03-16 17:31:52 +000067 if (!block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000068 return block;
69 }
70 }
71 return nullptr;
72}
73
74HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdil46e2a392015-03-16 17:31:52 +000075 while (block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000076 block = block->GetSuccessors().Get(0);
77 }
78 return block;
79}
80
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000081void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010082 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000083 DCHECK_EQ(current_block_index_, 0u);
84 GenerateFrameEntry();
85 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
86 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000087 // Don't generate code for an empty block. Its predecessors will branch to its successor
88 // directly. Also, the label of that block will not be emitted, so this helps catch
89 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +000090 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010091 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010092 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
93 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000094 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000095 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000096 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010097 current->Accept(instruction_visitor);
98 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000100
101 // Generate the slow paths.
102 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
103 slow_paths_.Get(i)->EmitNativeCode(this);
104 }
105
106 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000107 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108}
109
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100110void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000111 // The register allocator already called `InitializeCodeGeneration`,
112 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000113 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100114 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000115 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000116}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100117
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000118void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100119 size_t code_size = GetAssembler()->CodeSize();
120 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000121
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100122 MemoryRegion code(buffer, code_size);
123 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124}
125
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100126size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
127 for (size_t i = 0; i < length; ++i) {
128 if (!array[i]) {
129 array[i] = true;
130 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100131 }
132 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100133 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000134 UNREACHABLE();
135 return -1;
136}
137
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000138size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
139 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000140 if (!array[i] && !array[i + 1]) {
141 array[i] = true;
142 array[i + 1] = true;
143 return i;
144 }
145 }
146 LOG(FATAL) << "Could not find a register in baseline register allocator";
147 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100148 return -1;
149}
150
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000151void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
152 size_t maximum_number_of_live_core_registers,
153 size_t maximum_number_of_live_fp_registers,
154 size_t number_of_out_slots,
155 const GrowableArray<HBasicBlock*>& block_order) {
156 block_order_ = &block_order;
157 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
158 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000159 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100160 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
161
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000162 if (number_of_spill_slots == 0
163 && !HasAllocatedCalleeSaveRegisters()
164 && IsLeafMethod()
165 && !RequiresCurrentMethod()) {
166 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
167 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
168 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
169 } else {
170 SetFrameSize(RoundUp(
171 number_of_spill_slots * kVRegSize
172 + number_of_out_slots * kVRegSize
173 + maximum_number_of_live_core_registers * GetWordSize()
174 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
175 + FrameEntrySpillSize(),
176 kStackAlignment));
177 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100178}
179
180Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
181 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000182 // The type of the previous instruction tells us if we need a single or double stack slot.
183 Primitive::Type type = temp->GetType();
184 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100185 // Use the temporary region (right below the dex registers).
186 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
187 - kVRegSize // filler
188 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000189 - ((temp_size + temp->GetIndex()) * kVRegSize);
190 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100191}
192
193int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
194 uint16_t reg_number = local->GetRegNumber();
195 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
196 if (reg_number >= number_of_locals) {
197 // Local is a parameter of the method. It is stored in the caller's frame.
198 return GetFrameSize() + kVRegSize // ART method
199 + (reg_number - number_of_locals) * kVRegSize;
200 } else {
201 // Local is a temporary in this method. It is stored in this method's frame.
202 return GetFrameSize() - FrameEntrySpillSize()
203 - kVRegSize // filler.
204 - (number_of_locals * kVRegSize)
205 + (reg_number * kVRegSize);
206 }
207}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100208
Mark Mendell5f874182015-03-04 15:42:45 -0500209void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
210 // The DCHECKS below check that a register is not specified twice in
211 // the summary. The out location can overlap with an input, so we need
212 // to special case it.
213 if (location.IsRegister()) {
214 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
215 blocked_core_registers_[location.reg()] = true;
216 } else if (location.IsFpuRegister()) {
217 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
218 blocked_fpu_registers_[location.reg()] = true;
219 } else if (location.IsFpuRegisterPair()) {
220 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
221 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
222 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
223 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
224 } else if (location.IsRegisterPair()) {
225 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
226 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
227 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
228 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
229 }
230}
231
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100232void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
233 LocationSummary* locations = instruction->GetLocations();
234 if (locations == nullptr) return;
235
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100236 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
237 blocked_core_registers_[i] = false;
238 }
239
240 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
241 blocked_fpu_registers_[i] = false;
242 }
243
244 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
245 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100246 }
247
248 // Mark all fixed input, temp and output registers as used.
249 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500250 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100251 }
252
253 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
254 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500255 BlockIfInRegister(loc);
256 }
257 Location result_location = locations->Out();
258 if (locations->OutputCanOverlapWithInputs()) {
259 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100260 }
261
Mark Mendell5f874182015-03-04 15:42:45 -0500262 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100263
264 // Allocate all unallocated input locations.
265 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
266 Location loc = locations->InAt(i);
267 HInstruction* input = instruction->InputAt(i);
268 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100269 if ((loc.GetPolicy() == Location::kRequiresRegister)
270 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100271 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100272 } else {
273 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
274 HLoadLocal* load = input->AsLoadLocal();
275 if (load != nullptr) {
276 loc = GetStackLocation(load);
277 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100278 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100279 }
280 }
281 locations->SetInAt(i, loc);
282 }
283 }
284
285 // Allocate all unallocated temp locations.
286 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
287 Location loc = locations->GetTemp(i);
288 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000289 switch (loc.GetPolicy()) {
290 case Location::kRequiresRegister:
291 // Allocate a core register (large enough to fit a 32-bit integer).
292 loc = AllocateFreeRegister(Primitive::kPrimInt);
293 break;
294
295 case Location::kRequiresFpuRegister:
296 // Allocate a core register (large enough to fit a 64-bit double).
297 loc = AllocateFreeRegister(Primitive::kPrimDouble);
298 break;
299
300 default:
301 LOG(FATAL) << "Unexpected policy for temporary location "
302 << loc.GetPolicy();
303 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100304 locations->SetTempAt(i, loc);
305 }
306 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100307 if (result_location.IsUnallocated()) {
308 switch (result_location.GetPolicy()) {
309 case Location::kAny:
310 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100311 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100312 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100313 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100314 case Location::kSameAsFirstInput:
315 result_location = locations->InAt(0);
316 break;
317 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000318 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100319 }
320}
321
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000322void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
323 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100324 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100325 if (instruction->IsTemporary()) {
326 HInstruction* previous = instruction->GetPrevious();
327 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
328 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100329 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100330 return;
331 }
332 AllocateRegistersLocally(instruction);
333 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000334 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000335 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000336 if (location.IsValid()) {
337 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000338 if (input->GetNext()->IsTemporary()) {
339 // If the input was stored in a temporary, use that temporary to
340 // perform the move.
341 Move(input->GetNext(), location, instruction);
342 } else {
343 Move(input, location, instruction);
344 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000345 }
346 }
347}
348
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000349void CodeGenerator::AllocateLocations(HInstruction* instruction) {
350 instruction->Accept(GetLocationBuilder());
351 LocationSummary* locations = instruction->GetLocations();
352 if (!instruction->IsSuspendCheckEntry()) {
353 if (locations != nullptr && locations->CanCall()) {
354 MarkNotLeaf();
355 }
356 if (instruction->NeedsCurrentMethod()) {
357 SetRequiresCurrentMethod();
358 }
359 }
360}
361
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000362CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000363 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000364 const InstructionSetFeatures& isa_features,
365 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000366 switch (instruction_set) {
367 case kArm:
368 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000369 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000370 *isa_features.AsArmInstructionSetFeatures(),
371 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100373 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000374 return new arm64::CodeGeneratorARM64(graph,
375 *isa_features.AsArm64InstructionSetFeatures(),
376 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100377 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000378 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000379 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000380 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400381 return new x86::CodeGeneratorX86(graph,
382 *isa_features.AsX86InstructionSetFeatures(),
383 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000384 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700385 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400386 return new x86_64::CodeGeneratorX86_64(graph,
387 *isa_features.AsX86_64InstructionSetFeatures(),
388 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700389 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000390 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000391 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000392 }
393}
394
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000395void CodeGenerator::BuildNativeGCMap(
396 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
397 const std::vector<uint8_t>& gc_map_raw =
398 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
399 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
400
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000401 uint32_t max_native_offset = 0;
402 for (size_t i = 0; i < pc_infos_.Size(); i++) {
403 uint32_t native_offset = pc_infos_.Get(i).native_pc;
404 if (native_offset > max_native_offset) {
405 max_native_offset = native_offset;
406 }
407 }
408
409 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
410 for (size_t i = 0; i < pc_infos_.Size(); i++) {
411 struct PcInfo pc_info = pc_infos_.Get(i);
412 uint32_t native_offset = pc_info.native_pc;
413 uint32_t dex_pc = pc_info.dex_pc;
414 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800415 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000416 builder.AddEntry(native_offset, references);
417 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000418}
419
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800420void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000421 uint32_t pc2dex_data_size = 0u;
422 uint32_t pc2dex_entries = pc_infos_.Size();
423 uint32_t pc2dex_offset = 0u;
424 int32_t pc2dex_dalvik_offset = 0;
425 uint32_t dex2pc_data_size = 0u;
426 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000427 uint32_t dex2pc_offset = 0u;
428 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000429
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700430 if (src_map != nullptr) {
431 src_map->reserve(pc2dex_entries);
432 }
433
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000434 for (size_t i = 0; i < pc2dex_entries; i++) {
435 struct PcInfo pc_info = pc_infos_.Get(i);
436 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
437 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
438 pc2dex_offset = pc_info.native_pc;
439 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700440 if (src_map != nullptr) {
441 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
442 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000443 }
444
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000445 // Walk over the blocks and find which ones correspond to catch block entries.
446 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
447 HBasicBlock* block = graph_->GetBlocks().Get(i);
448 if (block->IsCatchBlock()) {
449 intptr_t native_pc = GetAddressOf(block);
450 ++dex2pc_entries;
451 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
452 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
453 dex2pc_offset = native_pc;
454 dex2pc_dalvik_offset = block->GetDexPc();
455 }
456 }
457
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000458 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
459 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
460 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
461 data->resize(data_size);
462
463 uint8_t* data_ptr = &(*data)[0];
464 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000465
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000466 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
467 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
468 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
469 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
470
471 pc2dex_offset = 0u;
472 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000473 dex2pc_offset = 0u;
474 dex2pc_dalvik_offset = 0u;
475
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000476 for (size_t i = 0; i < pc2dex_entries; i++) {
477 struct PcInfo pc_info = pc_infos_.Get(i);
478 DCHECK(pc2dex_offset <= pc_info.native_pc);
479 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
480 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
481 pc2dex_offset = pc_info.native_pc;
482 pc2dex_dalvik_offset = pc_info.dex_pc;
483 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000484
485 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
486 HBasicBlock* block = graph_->GetBlocks().Get(i);
487 if (block->IsCatchBlock()) {
488 intptr_t native_pc = GetAddressOf(block);
489 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
490 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
491 dex2pc_offset = native_pc;
492 dex2pc_dalvik_offset = block->GetDexPc();
493 }
494 }
495
496
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000497 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
498 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
499
500 if (kIsDebugBuild) {
501 // Verify the encoded table holds the expected data.
502 MappingTable table(data_ptr);
503 CHECK_EQ(table.TotalSize(), total_entries);
504 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
505 auto it = table.PcToDexBegin();
506 auto it2 = table.DexToPcBegin();
507 for (size_t i = 0; i < pc2dex_entries; i++) {
508 struct PcInfo pc_info = pc_infos_.Get(i);
509 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
510 CHECK_EQ(pc_info.dex_pc, it.DexPc());
511 ++it;
512 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000513 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
514 HBasicBlock* block = graph_->GetBlocks().Get(i);
515 if (block->IsCatchBlock()) {
516 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
517 CHECK_EQ(block->GetDexPc(), it2.DexPc());
518 ++it2;
519 }
520 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000521 CHECK(it == table.PcToDexEnd());
522 CHECK(it2 == table.DexToPcEnd());
523 }
524}
525
526void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
527 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100528 // We currently don't use callee-saved registers.
529 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000530 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
531 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000532 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
533
534 *data = vmap_encoder.GetData();
535}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000536
Nicolas Geoffray39468442014-09-02 15:17:15 +0100537void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
538 uint32_t size = stack_map_stream_.ComputeNeededSize();
539 data->resize(size);
540 MemoryRegion region(data->data(), size);
541 stack_map_stream_.FillIn(region);
542}
543
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000544void CodeGenerator::RecordPcInfo(HInstruction* instruction,
545 uint32_t dex_pc,
546 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000547 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000548 // The code generated for some type conversions may call the
549 // runtime, thus normally requiring a subsequent call to this
550 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000551 // information for certain instructions, which are considered "atomic"
552 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000553 // Therefore we do not currently record PC information for such
554 // instructions. As this may change later, we added this special
555 // case so that code generators may nevertheless call
556 // CodeGenerator::RecordPcInfo without triggering an error in
557 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
558 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000559 if (instruction->IsTypeConversion()) {
560 return;
561 }
562 if (instruction->IsRem()) {
563 Primitive::Type type = instruction->AsRem()->GetResultType();
564 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
565 return;
566 }
567 }
Roland Levillain624279f2014-12-04 11:54:28 +0000568 }
569
Nicolas Geoffray39468442014-09-02 15:17:15 +0100570 // Collect PC infos for the mapping table.
571 struct PcInfo pc_info;
572 pc_info.dex_pc = dex_pc;
573 pc_info.native_pc = GetAssembler()->CodeSize();
574 pc_infos_.Add(pc_info);
575
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000576 uint32_t inlining_depth = 0;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000577
Nicolas Geoffray39468442014-09-02 15:17:15 +0100578 if (instruction == nullptr) {
579 // For stack overflow checks.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000580 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, inlining_depth);
581 return;
582 }
583 LocationSummary* locations = instruction->GetLocations();
584 HEnvironment* environment = instruction->GetEnvironment();
585 size_t environment_size = instruction->EnvironmentSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100586
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000587 uint32_t register_mask = locations->GetRegisterMask();
588 if (locations->OnlyCallsOnSlowPath()) {
589 // In case of slow path, we currently set the location of caller-save registers
590 // to register (instead of their stack location when pushed before the slow-path
591 // call). Therefore register_mask contains both callee-save and caller-save
592 // registers that hold objects. We must remove the caller-save from the mask, since
593 // they will be overwritten by the callee.
594 register_mask &= core_callee_save_mask_;
595 }
596 // The register mask must be a subset of callee-save registers.
597 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
598 stack_map_stream_.AddStackMapEntry(dex_pc,
599 pc_info.native_pc,
600 register_mask,
601 locations->GetStackMask(),
602 environment_size,
603 inlining_depth);
604
605 // Walk over the environment, and record the location of dex registers.
606 for (size_t i = 0; i < environment_size; ++i) {
607 HInstruction* current = environment->GetInstructionAt(i);
608 if (current == nullptr) {
609 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
610 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100611 }
612
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000613 Location location = locations->GetEnvironmentAt(i);
614 switch (location.GetKind()) {
615 case Location::kConstant: {
616 DCHECK_EQ(current, location.GetConstant());
617 if (current->IsLongConstant()) {
618 int64_t value = current->AsLongConstant()->GetValue();
619 stack_map_stream_.AddDexRegisterEntry(
620 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
621 stack_map_stream_.AddDexRegisterEntry(
622 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
623 DCHECK_LT(i, environment_size);
624 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000625 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000626 stack_map_stream_.AddDexRegisterEntry(
627 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
628 stack_map_stream_.AddDexRegisterEntry(
629 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
630 DCHECK_LT(i, environment_size);
631 } else if (current->IsIntConstant()) {
632 int32_t value = current->AsIntConstant()->GetValue();
633 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
634 } else if (current->IsNullConstant()) {
635 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, 0);
636 } else {
637 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000638 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000639 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
640 }
641 break;
642 }
643
644 case Location::kStackSlot: {
645 stack_map_stream_.AddDexRegisterEntry(
646 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
647 break;
648 }
649
650 case Location::kDoubleStackSlot: {
651 stack_map_stream_.AddDexRegisterEntry(
652 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
653 stack_map_stream_.AddDexRegisterEntry(
654 ++i, DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
655 DCHECK_LT(i, environment_size);
656 break;
657 }
658
659 case Location::kRegister : {
660 int id = location.reg();
661 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
662 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
663 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
664 if (current->GetType() == Primitive::kPrimLong) {
665 stack_map_stream_.AddDexRegisterEntry(
666 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
667 DCHECK_LT(i, environment_size);
668 }
669 } else {
670 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, id);
671 if (current->GetType() == Primitive::kPrimLong) {
672 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInRegister, id);
673 DCHECK_LT(i, environment_size);
674 }
675 }
676 break;
677 }
678
679 case Location::kFpuRegister : {
680 int id = location.reg();
681 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
682 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
683 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
684 if (current->GetType() == Primitive::kPrimDouble) {
685 stack_map_stream_.AddDexRegisterEntry(
686 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
687 DCHECK_LT(i, environment_size);
688 }
689 } else {
690 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, id);
691 if (current->GetType() == Primitive::kPrimDouble) {
692 stack_map_stream_.AddDexRegisterEntry(
693 ++i, DexRegisterLocation::Kind::kInFpuRegister, id);
694 DCHECK_LT(i, environment_size);
695 }
696 }
697 break;
698 }
699
700 case Location::kFpuRegisterPair : {
701 int low = location.low();
702 int high = location.high();
703 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
704 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
705 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
706 } else {
707 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, low);
708 }
709 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
710 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
711 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
712 } else {
713 stack_map_stream_.AddDexRegisterEntry(
714 ++i, DexRegisterLocation::Kind::kInFpuRegister, high);
715 }
716 DCHECK_LT(i, environment_size);
717 break;
718 }
719
720 case Location::kRegisterPair : {
721 int low = location.low();
722 int high = location.high();
723 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
724 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
725 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
726 } else {
727 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, low);
728 }
729 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
730 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
731 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
732 } else {
733 stack_map_stream_.AddDexRegisterEntry(
734 ++i, DexRegisterLocation::Kind::kInRegister, high);
735 }
736 DCHECK_LT(i, environment_size);
737 break;
738 }
739
740 case Location::kInvalid: {
741 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
742 break;
743 }
744
745 default:
746 LOG(FATAL) << "Unexpected kind " << location.GetKind();
747 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100748 }
749}
750
Calin Juravle77520bc2015-01-12 18:45:46 +0000751bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
752 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
753 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
754}
755
756void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
757 // If we are from a static path don't record the pc as we can't throw NPE.
758 // NB: having the checks here makes the code much less verbose in the arch
759 // specific code generators.
760 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
761 return;
762 }
763
764 if (!compiler_options_.GetImplicitNullChecks()) {
765 return;
766 }
767
768 if (!instr->CanDoImplicitNullCheck()) {
769 return;
770 }
771
772 // Find the first previous instruction which is not a move.
773 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
774
775 // If the instruction is a null check it means that `instr` is the first user
776 // and needs to record the pc.
777 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
778 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
779 // TODO: The parallel moves modify the environment. Their changes need to be reverted
780 // otherwise the stack maps at the throw point will not be correct.
781 RecordPcInfo(null_check, null_check->GetDexPc());
782 }
783}
784
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100785void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
786 LocationSummary* locations = suspend_check->GetLocations();
787 HBasicBlock* block = suspend_check->GetBlock();
788 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
789 DCHECK(block->IsLoopHeader());
790
791 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
792 HInstruction* current = it.Current();
793 LiveInterval* interval = current->GetLiveInterval();
794 // We only need to clear bits of loop phis containing objects and allocated in register.
795 // Loop phis allocated on stack already have the object in the stack.
796 if (current->GetType() == Primitive::kPrimNot
797 && interval->HasRegister()
798 && interval->HasSpillSlot()) {
799 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
800 }
801 }
802}
803
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000804void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000805 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000806 parallel_move.AddMove(from1, to1, nullptr);
807 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000808 GetMoveResolver()->EmitNativeCode(&parallel_move);
809}
810
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000811void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000812 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000813}
814
815void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
816 RegisterSet* register_set = locations->GetLiveRegisters();
817 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
818 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
819 if (!codegen->IsCoreCalleeSaveRegister(i)) {
820 if (register_set->ContainsCoreRegister(i)) {
821 // If the register holds an object, update the stack mask.
822 if (locations->RegisterContainsObject(i)) {
823 locations->SetStackBit(stack_offset / kVRegSize);
824 }
825 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000826 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
827 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000828 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
829 }
830 }
831 }
832
833 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
834 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
835 if (register_set->ContainsFloatingPointRegister(i)) {
836 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000837 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
838 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000839 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
840 }
841 }
842 }
843}
844
845void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
846 RegisterSet* register_set = locations->GetLiveRegisters();
847 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
848 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
849 if (!codegen->IsCoreCalleeSaveRegister(i)) {
850 if (register_set->ContainsCoreRegister(i)) {
851 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
852 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
853 }
854 }
855 }
856
857 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
858 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
859 if (register_set->ContainsFloatingPointRegister(i)) {
860 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
861 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
862 }
863 }
864 }
865}
866
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000867} // namespace art