blob: bd6e943bf0319f76e28cca96bce34196286d3eb5 [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: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000381 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000382 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700383 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000384 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700385 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000386 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000387 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000388 }
389}
390
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000391void CodeGenerator::BuildNativeGCMap(
392 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
393 const std::vector<uint8_t>& gc_map_raw =
394 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
395 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
396
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000397 uint32_t max_native_offset = 0;
398 for (size_t i = 0; i < pc_infos_.Size(); i++) {
399 uint32_t native_offset = pc_infos_.Get(i).native_pc;
400 if (native_offset > max_native_offset) {
401 max_native_offset = native_offset;
402 }
403 }
404
405 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
406 for (size_t i = 0; i < pc_infos_.Size(); i++) {
407 struct PcInfo pc_info = pc_infos_.Get(i);
408 uint32_t native_offset = pc_info.native_pc;
409 uint32_t dex_pc = pc_info.dex_pc;
410 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800411 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000412 builder.AddEntry(native_offset, references);
413 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000414}
415
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800416void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000417 uint32_t pc2dex_data_size = 0u;
418 uint32_t pc2dex_entries = pc_infos_.Size();
419 uint32_t pc2dex_offset = 0u;
420 int32_t pc2dex_dalvik_offset = 0;
421 uint32_t dex2pc_data_size = 0u;
422 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000423 uint32_t dex2pc_offset = 0u;
424 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000425
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700426 if (src_map != nullptr) {
427 src_map->reserve(pc2dex_entries);
428 }
429
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000430 for (size_t i = 0; i < pc2dex_entries; i++) {
431 struct PcInfo pc_info = pc_infos_.Get(i);
432 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
433 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
434 pc2dex_offset = pc_info.native_pc;
435 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700436 if (src_map != nullptr) {
437 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
438 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000439 }
440
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000441 // Walk over the blocks and find which ones correspond to catch block entries.
442 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
443 HBasicBlock* block = graph_->GetBlocks().Get(i);
444 if (block->IsCatchBlock()) {
445 intptr_t native_pc = GetAddressOf(block);
446 ++dex2pc_entries;
447 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
448 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
449 dex2pc_offset = native_pc;
450 dex2pc_dalvik_offset = block->GetDexPc();
451 }
452 }
453
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000454 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
455 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
456 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
457 data->resize(data_size);
458
459 uint8_t* data_ptr = &(*data)[0];
460 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000461
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000462 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
463 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
464 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
465 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
466
467 pc2dex_offset = 0u;
468 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000469 dex2pc_offset = 0u;
470 dex2pc_dalvik_offset = 0u;
471
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000472 for (size_t i = 0; i < pc2dex_entries; i++) {
473 struct PcInfo pc_info = pc_infos_.Get(i);
474 DCHECK(pc2dex_offset <= pc_info.native_pc);
475 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
476 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
477 pc2dex_offset = pc_info.native_pc;
478 pc2dex_dalvik_offset = pc_info.dex_pc;
479 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000480
481 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
482 HBasicBlock* block = graph_->GetBlocks().Get(i);
483 if (block->IsCatchBlock()) {
484 intptr_t native_pc = GetAddressOf(block);
485 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
486 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
487 dex2pc_offset = native_pc;
488 dex2pc_dalvik_offset = block->GetDexPc();
489 }
490 }
491
492
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000493 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
494 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
495
496 if (kIsDebugBuild) {
497 // Verify the encoded table holds the expected data.
498 MappingTable table(data_ptr);
499 CHECK_EQ(table.TotalSize(), total_entries);
500 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
501 auto it = table.PcToDexBegin();
502 auto it2 = table.DexToPcBegin();
503 for (size_t i = 0; i < pc2dex_entries; i++) {
504 struct PcInfo pc_info = pc_infos_.Get(i);
505 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
506 CHECK_EQ(pc_info.dex_pc, it.DexPc());
507 ++it;
508 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000509 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
510 HBasicBlock* block = graph_->GetBlocks().Get(i);
511 if (block->IsCatchBlock()) {
512 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
513 CHECK_EQ(block->GetDexPc(), it2.DexPc());
514 ++it2;
515 }
516 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000517 CHECK(it == table.PcToDexEnd());
518 CHECK(it2 == table.DexToPcEnd());
519 }
520}
521
522void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
523 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100524 // We currently don't use callee-saved registers.
525 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000526 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
527 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000528 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
529
530 *data = vmap_encoder.GetData();
531}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000532
Nicolas Geoffray39468442014-09-02 15:17:15 +0100533void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
534 uint32_t size = stack_map_stream_.ComputeNeededSize();
535 data->resize(size);
536 MemoryRegion region(data->data(), size);
537 stack_map_stream_.FillIn(region);
538}
539
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000540void CodeGenerator::RecordPcInfo(HInstruction* instruction,
541 uint32_t dex_pc,
542 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000543 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000544 // The code generated for some type conversions may call the
545 // runtime, thus normally requiring a subsequent call to this
546 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000547 // information for certain instructions, which are considered "atomic"
548 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000549 // Therefore we do not currently record PC information for such
550 // instructions. As this may change later, we added this special
551 // case so that code generators may nevertheless call
552 // CodeGenerator::RecordPcInfo without triggering an error in
553 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
554 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000555 if (instruction->IsTypeConversion()) {
556 return;
557 }
558 if (instruction->IsRem()) {
559 Primitive::Type type = instruction->AsRem()->GetResultType();
560 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
561 return;
562 }
563 }
Roland Levillain624279f2014-12-04 11:54:28 +0000564 }
565
Nicolas Geoffray39468442014-09-02 15:17:15 +0100566 // Collect PC infos for the mapping table.
567 struct PcInfo pc_info;
568 pc_info.dex_pc = dex_pc;
569 pc_info.native_pc = GetAssembler()->CodeSize();
570 pc_infos_.Add(pc_info);
571
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000572 uint32_t inlining_depth = 0;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000573
Nicolas Geoffray39468442014-09-02 15:17:15 +0100574 if (instruction == nullptr) {
575 // For stack overflow checks.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000576 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, inlining_depth);
577 return;
578 }
579 LocationSummary* locations = instruction->GetLocations();
580 HEnvironment* environment = instruction->GetEnvironment();
581 size_t environment_size = instruction->EnvironmentSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100582
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000583 uint32_t register_mask = locations->GetRegisterMask();
584 if (locations->OnlyCallsOnSlowPath()) {
585 // In case of slow path, we currently set the location of caller-save registers
586 // to register (instead of their stack location when pushed before the slow-path
587 // call). Therefore register_mask contains both callee-save and caller-save
588 // registers that hold objects. We must remove the caller-save from the mask, since
589 // they will be overwritten by the callee.
590 register_mask &= core_callee_save_mask_;
591 }
592 // The register mask must be a subset of callee-save registers.
593 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
594 stack_map_stream_.AddStackMapEntry(dex_pc,
595 pc_info.native_pc,
596 register_mask,
597 locations->GetStackMask(),
598 environment_size,
599 inlining_depth);
600
601 // Walk over the environment, and record the location of dex registers.
602 for (size_t i = 0; i < environment_size; ++i) {
603 HInstruction* current = environment->GetInstructionAt(i);
604 if (current == nullptr) {
605 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
606 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100607 }
608
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000609 Location location = locations->GetEnvironmentAt(i);
610 switch (location.GetKind()) {
611 case Location::kConstant: {
612 DCHECK_EQ(current, location.GetConstant());
613 if (current->IsLongConstant()) {
614 int64_t value = current->AsLongConstant()->GetValue();
615 stack_map_stream_.AddDexRegisterEntry(
616 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
617 stack_map_stream_.AddDexRegisterEntry(
618 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
619 DCHECK_LT(i, environment_size);
620 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000621 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000622 stack_map_stream_.AddDexRegisterEntry(
623 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
624 stack_map_stream_.AddDexRegisterEntry(
625 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
626 DCHECK_LT(i, environment_size);
627 } else if (current->IsIntConstant()) {
628 int32_t value = current->AsIntConstant()->GetValue();
629 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
630 } else if (current->IsNullConstant()) {
631 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, 0);
632 } else {
633 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000634 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000635 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
636 }
637 break;
638 }
639
640 case Location::kStackSlot: {
641 stack_map_stream_.AddDexRegisterEntry(
642 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
643 break;
644 }
645
646 case Location::kDoubleStackSlot: {
647 stack_map_stream_.AddDexRegisterEntry(
648 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
649 stack_map_stream_.AddDexRegisterEntry(
650 ++i, DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
651 DCHECK_LT(i, environment_size);
652 break;
653 }
654
655 case Location::kRegister : {
656 int id = location.reg();
657 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
658 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
659 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
660 if (current->GetType() == Primitive::kPrimLong) {
661 stack_map_stream_.AddDexRegisterEntry(
662 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
663 DCHECK_LT(i, environment_size);
664 }
665 } else {
666 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, id);
667 if (current->GetType() == Primitive::kPrimLong) {
668 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInRegister, id);
669 DCHECK_LT(i, environment_size);
670 }
671 }
672 break;
673 }
674
675 case Location::kFpuRegister : {
676 int id = location.reg();
677 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
678 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
679 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
680 if (current->GetType() == Primitive::kPrimDouble) {
681 stack_map_stream_.AddDexRegisterEntry(
682 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
683 DCHECK_LT(i, environment_size);
684 }
685 } else {
686 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, id);
687 if (current->GetType() == Primitive::kPrimDouble) {
688 stack_map_stream_.AddDexRegisterEntry(
689 ++i, DexRegisterLocation::Kind::kInFpuRegister, id);
690 DCHECK_LT(i, environment_size);
691 }
692 }
693 break;
694 }
695
696 case Location::kFpuRegisterPair : {
697 int low = location.low();
698 int high = location.high();
699 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
700 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
701 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
702 } else {
703 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, low);
704 }
705 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
706 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
707 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
708 } else {
709 stack_map_stream_.AddDexRegisterEntry(
710 ++i, DexRegisterLocation::Kind::kInFpuRegister, high);
711 }
712 DCHECK_LT(i, environment_size);
713 break;
714 }
715
716 case Location::kRegisterPair : {
717 int low = location.low();
718 int high = location.high();
719 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
720 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
721 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
722 } else {
723 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, low);
724 }
725 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
726 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
727 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
728 } else {
729 stack_map_stream_.AddDexRegisterEntry(
730 ++i, DexRegisterLocation::Kind::kInRegister, high);
731 }
732 DCHECK_LT(i, environment_size);
733 break;
734 }
735
736 case Location::kInvalid: {
737 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
738 break;
739 }
740
741 default:
742 LOG(FATAL) << "Unexpected kind " << location.GetKind();
743 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100744 }
745}
746
Calin Juravle77520bc2015-01-12 18:45:46 +0000747bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
748 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
749 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
750}
751
752void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
753 // If we are from a static path don't record the pc as we can't throw NPE.
754 // NB: having the checks here makes the code much less verbose in the arch
755 // specific code generators.
756 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
757 return;
758 }
759
760 if (!compiler_options_.GetImplicitNullChecks()) {
761 return;
762 }
763
764 if (!instr->CanDoImplicitNullCheck()) {
765 return;
766 }
767
768 // Find the first previous instruction which is not a move.
769 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
770
771 // If the instruction is a null check it means that `instr` is the first user
772 // and needs to record the pc.
773 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
774 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
775 // TODO: The parallel moves modify the environment. Their changes need to be reverted
776 // otherwise the stack maps at the throw point will not be correct.
777 RecordPcInfo(null_check, null_check->GetDexPc());
778 }
779}
780
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100781void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
782 LocationSummary* locations = suspend_check->GetLocations();
783 HBasicBlock* block = suspend_check->GetBlock();
784 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
785 DCHECK(block->IsLoopHeader());
786
787 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
788 HInstruction* current = it.Current();
789 LiveInterval* interval = current->GetLiveInterval();
790 // We only need to clear bits of loop phis containing objects and allocated in register.
791 // Loop phis allocated on stack already have the object in the stack.
792 if (current->GetType() == Primitive::kPrimNot
793 && interval->HasRegister()
794 && interval->HasSpillSlot()) {
795 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
796 }
797 }
798}
799
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000800void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000801 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000802 parallel_move.AddMove(from1, to1, nullptr);
803 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000804 GetMoveResolver()->EmitNativeCode(&parallel_move);
805}
806
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000807void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000808 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000809}
810
811void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
812 RegisterSet* register_set = locations->GetLiveRegisters();
813 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
814 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
815 if (!codegen->IsCoreCalleeSaveRegister(i)) {
816 if (register_set->ContainsCoreRegister(i)) {
817 // If the register holds an object, update the stack mask.
818 if (locations->RegisterContainsObject(i)) {
819 locations->SetStackBit(stack_offset / kVRegSize);
820 }
821 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000822 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
823 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000824 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
825 }
826 }
827 }
828
829 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
830 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
831 if (register_set->ContainsFloatingPointRegister(i)) {
832 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000833 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
834 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000835 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
836 }
837 }
838 }
839}
840
841void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
842 RegisterSet* register_set = locations->GetLiveRegisters();
843 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
844 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
845 if (!codegen->IsCoreCalleeSaveRegister(i)) {
846 if (register_set->ContainsCoreRegister(i)) {
847 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
848 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
849 }
850 }
851 }
852
853 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
854 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
855 if (register_set->ContainsFloatingPointRegister(i)) {
856 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
857 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
858 }
859 }
860 }
861}
862
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000863} // namespace art