blob: d521d78e878547efb405a0f73bb5046318b5d1ab [file] [log] [blame]
Alexey Frunze4dda3372015-06-01 18:31:49 -07001/*
2 * Copyright (C) 2015 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_mips64.h"
18
19#include "entrypoints/quick/quick_entrypoints.h"
20#include "entrypoints/quick/quick_entrypoints_enum.h"
21#include "gc/accounting/card_table.h"
22#include "intrinsics.h"
23#include "art_method.h"
24#include "mirror/array-inl.h"
25#include "mirror/class-inl.h"
26#include "offsets.h"
27#include "thread.h"
28#include "utils/mips64/assembler_mips64.h"
29#include "utils/assembler.h"
30#include "utils/stack_checks.h"
31
32namespace art {
33namespace mips64 {
34
35static constexpr int kCurrentMethodStackOffset = 0;
36static constexpr GpuRegister kMethodRegisterArgument = A0;
37
38// We need extra temporary/scratch registers (in addition to AT) in some cases.
39static constexpr GpuRegister TMP = T8;
40static constexpr FpuRegister FTMP = F8;
41
42// ART Thread Register.
43static constexpr GpuRegister TR = S1;
44
45Location Mips64ReturnLocation(Primitive::Type return_type) {
46 switch (return_type) {
47 case Primitive::kPrimBoolean:
48 case Primitive::kPrimByte:
49 case Primitive::kPrimChar:
50 case Primitive::kPrimShort:
51 case Primitive::kPrimInt:
52 case Primitive::kPrimNot:
53 case Primitive::kPrimLong:
54 return Location::RegisterLocation(V0);
55
56 case Primitive::kPrimFloat:
57 case Primitive::kPrimDouble:
58 return Location::FpuRegisterLocation(F0);
59
60 case Primitive::kPrimVoid:
61 return Location();
62 }
63 UNREACHABLE();
64}
65
66Location InvokeDexCallingConventionVisitorMIPS64::GetReturnLocation(Primitive::Type type) const {
67 return Mips64ReturnLocation(type);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS64::GetMethodLocation() const {
71 return Location::RegisterLocation(kMethodRegisterArgument);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS64::GetNextLocation(Primitive::Type type) {
75 Location next_location;
76 if (type == Primitive::kPrimVoid) {
77 LOG(FATAL) << "Unexpected parameter type " << type;
78 }
79
80 if (Primitive::IsFloatingPointType(type) &&
81 (float_index_ < calling_convention.GetNumberOfFpuRegisters())) {
82 next_location = Location::FpuRegisterLocation(
83 calling_convention.GetFpuRegisterAt(float_index_++));
84 gp_index_++;
85 } else if (!Primitive::IsFloatingPointType(type) &&
86 (gp_index_ < calling_convention.GetNumberOfRegisters())) {
87 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index_++));
88 float_index_++;
89 } else {
90 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
91 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
92 : Location::StackSlot(stack_offset);
93 }
94
95 // Space on the stack is reserved for all arguments.
96 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
97
98 // TODO: review
99
100 // TODO: shouldn't we use a whole machine word per argument on the stack?
101 // Implicit 4-byte method pointer (and such) will cause misalignment.
102
103 return next_location;
104}
105
106Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
107 return Mips64ReturnLocation(type);
108}
109
110#define __ down_cast<CodeGeneratorMIPS64*>(codegen)->GetAssembler()->
111#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
112
113class BoundsCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
114 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100115 explicit BoundsCheckSlowPathMIPS64(HBoundsCheck* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700116
117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100118 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700119 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
120 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000121 if (instruction_->CanThrowIntoCatchBlock()) {
122 // Live registers will be restored in the catch block if caught.
123 SaveLiveRegisters(codegen, instruction_->GetLocations());
124 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700125 // We're moving two locations to locations that could overlap, so we need a parallel
126 // move resolver.
127 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 codegen->EmitParallelMoves(locations->InAt(0),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700129 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
130 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700132 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
133 Primitive::kPrimInt);
134 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
135 instruction_,
136 instruction_->GetDexPc(),
137 this);
138 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
139 }
140
Alexandre Rames8158f282015-08-07 10:26:17 +0100141 bool IsFatal() const OVERRIDE { return true; }
142
Roland Levillain46648892015-06-19 16:07:18 +0100143 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS64"; }
144
Alexey Frunze4dda3372015-06-01 18:31:49 -0700145 private:
146 HBoundsCheck* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700147
148 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS64);
149};
150
151class DivZeroCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
152 public:
153 explicit DivZeroCheckSlowPathMIPS64(HDivZeroCheck* instruction) : instruction_(instruction) {}
154
155 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
156 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
157 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000158 if (instruction_->CanThrowIntoCatchBlock()) {
159 // Live registers will be restored in the catch block if caught.
160 SaveLiveRegisters(codegen, instruction_->GetLocations());
161 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700162 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
163 instruction_,
164 instruction_->GetDexPc(),
165 this);
166 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
167 }
168
Alexandre Rames8158f282015-08-07 10:26:17 +0100169 bool IsFatal() const OVERRIDE { return true; }
170
Roland Levillain46648892015-06-19 16:07:18 +0100171 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS64"; }
172
Alexey Frunze4dda3372015-06-01 18:31:49 -0700173 private:
174 HDivZeroCheck* const instruction_;
175 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS64);
176};
177
178class LoadClassSlowPathMIPS64 : public SlowPathCodeMIPS64 {
179 public:
180 LoadClassSlowPathMIPS64(HLoadClass* cls,
181 HInstruction* at,
182 uint32_t dex_pc,
183 bool do_clinit)
184 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
185 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
186 }
187
188 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
189 LocationSummary* locations = at_->GetLocations();
190 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
191
192 __ Bind(GetEntryLabel());
193 SaveLiveRegisters(codegen, locations);
194
195 InvokeRuntimeCallingConvention calling_convention;
196 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
197 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
198 : QUICK_ENTRY_POINT(pInitializeType);
199 mips64_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this);
200 if (do_clinit_) {
201 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
202 } else {
203 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
204 }
205
206 // Move the class to the desired location.
207 Location out = locations->Out();
208 if (out.IsValid()) {
209 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
210 Primitive::Type type = at_->GetType();
211 mips64_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
212 }
213
214 RestoreLiveRegisters(codegen, locations);
215 __ B(GetExitLabel());
216 }
217
Roland Levillain46648892015-06-19 16:07:18 +0100218 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS64"; }
219
Alexey Frunze4dda3372015-06-01 18:31:49 -0700220 private:
221 // The class this slow path will load.
222 HLoadClass* const cls_;
223
224 // The instruction where this slow path is happening.
225 // (Might be the load class or an initialization check).
226 HInstruction* const at_;
227
228 // The dex PC of `at_`.
229 const uint32_t dex_pc_;
230
231 // Whether to initialize the class.
232 const bool do_clinit_;
233
234 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS64);
235};
236
237class LoadStringSlowPathMIPS64 : public SlowPathCodeMIPS64 {
238 public:
239 explicit LoadStringSlowPathMIPS64(HLoadString* instruction) : instruction_(instruction) {}
240
241 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
242 LocationSummary* locations = instruction_->GetLocations();
243 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
244 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
245
246 __ Bind(GetEntryLabel());
247 SaveLiveRegisters(codegen, locations);
248
249 InvokeRuntimeCallingConvention calling_convention;
250 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
251 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
252 instruction_,
253 instruction_->GetDexPc(),
254 this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
256 Primitive::Type type = instruction_->GetType();
257 mips64_codegen->MoveLocation(locations->Out(),
258 calling_convention.GetReturnLocation(type),
259 type);
260
261 RestoreLiveRegisters(codegen, locations);
262 __ B(GetExitLabel());
263 }
264
Roland Levillain46648892015-06-19 16:07:18 +0100265 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS64"; }
266
Alexey Frunze4dda3372015-06-01 18:31:49 -0700267 private:
268 HLoadString* const instruction_;
269
270 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS64);
271};
272
273class NullCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
274 public:
275 explicit NullCheckSlowPathMIPS64(HNullCheck* instr) : instruction_(instr) {}
276
277 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
278 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
279 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000280 if (instruction_->CanThrowIntoCatchBlock()) {
281 // Live registers will be restored in the catch block if caught.
282 SaveLiveRegisters(codegen, instruction_->GetLocations());
283 }
Alexey Frunze4dda3372015-06-01 18:31:49 -0700284 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
285 instruction_,
286 instruction_->GetDexPc(),
287 this);
288 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
289 }
290
Alexandre Rames8158f282015-08-07 10:26:17 +0100291 bool IsFatal() const OVERRIDE { return true; }
292
Roland Levillain46648892015-06-19 16:07:18 +0100293 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS64"; }
294
Alexey Frunze4dda3372015-06-01 18:31:49 -0700295 private:
296 HNullCheck* const instruction_;
297
298 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS64);
299};
300
301class SuspendCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
302 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100303 SuspendCheckSlowPathMIPS64(HSuspendCheck* instruction, HBasicBlock* successor)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700304 : instruction_(instruction), successor_(successor) {}
305
306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
307 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
308 __ Bind(GetEntryLabel());
309 SaveLiveRegisters(codegen, instruction_->GetLocations());
310 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
311 instruction_,
312 instruction_->GetDexPc(),
313 this);
314 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
315 RestoreLiveRegisters(codegen, instruction_->GetLocations());
316 if (successor_ == nullptr) {
317 __ B(GetReturnLabel());
318 } else {
319 __ B(mips64_codegen->GetLabelOf(successor_));
320 }
321 }
322
323 Label* GetReturnLabel() {
324 DCHECK(successor_ == nullptr);
325 return &return_label_;
326 }
327
Roland Levillain46648892015-06-19 16:07:18 +0100328 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS64"; }
329
Alexey Frunze4dda3372015-06-01 18:31:49 -0700330 private:
331 HSuspendCheck* const instruction_;
332 // If not null, the block to branch to after the suspend check.
333 HBasicBlock* const successor_;
334
335 // If `successor_` is null, the label to branch to after the suspend check.
336 Label return_label_;
337
338 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS64);
339};
340
341class TypeCheckSlowPathMIPS64 : public SlowPathCodeMIPS64 {
342 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100343 explicit TypeCheckSlowPathMIPS64(HInstruction* instruction) : instruction_(instruction) {}
Alexey Frunze4dda3372015-06-01 18:31:49 -0700344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
346 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
348 : locations->Out();
349 uint32_t dex_pc = instruction_->GetDexPc();
Alexey Frunze4dda3372015-06-01 18:31:49 -0700350 DCHECK(instruction_->IsCheckCast()
351 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
352 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
353
354 __ Bind(GetEntryLabel());
355 SaveLiveRegisters(codegen, locations);
356
357 // We're moving two locations to locations that could overlap, so we need a parallel
358 // move resolver.
359 InvokeRuntimeCallingConvention calling_convention;
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100360 codegen->EmitParallelMoves(locations->InAt(1),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700361 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
362 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100363 object_class,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700364 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
365 Primitive::kPrimNot);
366
367 if (instruction_->IsInstanceOf()) {
368 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
369 instruction_,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100370 dex_pc,
Alexey Frunze4dda3372015-06-01 18:31:49 -0700371 this);
372 Primitive::Type ret_type = instruction_->GetType();
373 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
374 mips64_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
375 CheckEntrypointTypes<kQuickInstanceofNonTrivial,
376 uint32_t,
377 const mirror::Class*,
378 const mirror::Class*>();
379 } else {
380 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100381 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), instruction_, dex_pc, this);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700382 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
383 }
384
385 RestoreLiveRegisters(codegen, locations);
386 __ B(GetExitLabel());
387 }
388
Roland Levillain46648892015-06-19 16:07:18 +0100389 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS64"; }
390
Alexey Frunze4dda3372015-06-01 18:31:49 -0700391 private:
392 HInstruction* const instruction_;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700393
394 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS64);
395};
396
397class DeoptimizationSlowPathMIPS64 : public SlowPathCodeMIPS64 {
398 public:
399 explicit DeoptimizationSlowPathMIPS64(HInstruction* instruction)
400 : instruction_(instruction) {}
401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
403 __ Bind(GetEntryLabel());
404 SaveLiveRegisters(codegen, instruction_->GetLocations());
405 DCHECK(instruction_->IsDeoptimize());
406 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
407 uint32_t dex_pc = deoptimize->GetDexPc();
408 CodeGeneratorMIPS64* mips64_codegen = down_cast<CodeGeneratorMIPS64*>(codegen);
409 mips64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this);
410 }
411
Roland Levillain46648892015-06-19 16:07:18 +0100412 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS64"; }
413
Alexey Frunze4dda3372015-06-01 18:31:49 -0700414 private:
415 HInstruction* const instruction_;
416 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS64);
417};
418
419CodeGeneratorMIPS64::CodeGeneratorMIPS64(HGraph* graph,
420 const Mips64InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100421 const CompilerOptions& compiler_options,
422 OptimizingCompilerStats* stats)
Alexey Frunze4dda3372015-06-01 18:31:49 -0700423 : CodeGenerator(graph,
424 kNumberOfGpuRegisters,
425 kNumberOfFpuRegisters,
426 0, // kNumberOfRegisterPairs
427 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
428 arraysize(kCoreCalleeSaves)),
429 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
430 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100431 compiler_options,
432 stats),
Alexey Frunze4dda3372015-06-01 18:31:49 -0700433 block_labels_(graph->GetArena(), 0),
434 location_builder_(graph, this),
435 instruction_visitor_(graph, this),
436 move_resolver_(graph->GetArena(), this),
437 isa_features_(isa_features) {
438 // Save RA (containing the return address) to mimic Quick.
439 AddAllocatedRegister(Location::RegisterLocation(RA));
440}
441
442#undef __
443#define __ down_cast<Mips64Assembler*>(GetAssembler())->
444#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMips64WordSize, x).Int32Value()
445
446void CodeGeneratorMIPS64::Finalize(CodeAllocator* allocator) {
447 CodeGenerator::Finalize(allocator);
448}
449
450Mips64Assembler* ParallelMoveResolverMIPS64::GetAssembler() const {
451 return codegen_->GetAssembler();
452}
453
454void ParallelMoveResolverMIPS64::EmitMove(size_t index) {
455 MoveOperands* move = moves_.Get(index);
456 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
457}
458
459void ParallelMoveResolverMIPS64::EmitSwap(size_t index) {
460 MoveOperands* move = moves_.Get(index);
461 codegen_->SwapLocations(move->GetDestination(), move->GetSource(), move->GetType());
462}
463
464void ParallelMoveResolverMIPS64::RestoreScratch(int reg) {
465 // Pop reg
466 __ Ld(GpuRegister(reg), SP, 0);
467 __ DecreaseFrameSize(kMips64WordSize);
468}
469
470void ParallelMoveResolverMIPS64::SpillScratch(int reg) {
471 // Push reg
472 __ IncreaseFrameSize(kMips64WordSize);
473 __ Sd(GpuRegister(reg), SP, 0);
474}
475
476void ParallelMoveResolverMIPS64::Exchange(int index1, int index2, bool double_slot) {
477 LoadOperandType load_type = double_slot ? kLoadDoubleword : kLoadWord;
478 StoreOperandType store_type = double_slot ? kStoreDoubleword : kStoreWord;
479 // Allocate a scratch register other than TMP, if available.
480 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
481 // automatically unspilled when the scratch scope object is destroyed).
482 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
483 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
484 int stack_offset = ensure_scratch.IsSpilled() ? kMips64WordSize : 0;
485 __ LoadFromOffset(load_type,
486 GpuRegister(ensure_scratch.GetRegister()),
487 SP,
488 index1 + stack_offset);
489 __ LoadFromOffset(load_type,
490 TMP,
491 SP,
492 index2 + stack_offset);
493 __ StoreToOffset(store_type,
494 GpuRegister(ensure_scratch.GetRegister()),
495 SP,
496 index2 + stack_offset);
497 __ StoreToOffset(store_type, TMP, SP, index1 + stack_offset);
498}
499
500static dwarf::Reg DWARFReg(GpuRegister reg) {
501 return dwarf::Reg::Mips64Core(static_cast<int>(reg));
502}
503
504// TODO: mapping of floating-point registers to DWARF
505
506void CodeGeneratorMIPS64::GenerateFrameEntry() {
507 __ Bind(&frame_entry_label_);
508
509 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips64) || !IsLeafMethod();
510
511 if (do_overflow_check) {
512 __ LoadFromOffset(kLoadWord,
513 ZERO,
514 SP,
515 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips64)));
516 RecordPcInfo(nullptr, 0);
517 }
518
519 // TODO: anything related to T9/GP/GOT/PIC/.so's?
520
521 if (HasEmptyFrame()) {
522 return;
523 }
524
525 // Make sure the frame size isn't unreasonably large. Per the various APIs
526 // it looks like it should always be less than 2GB in size, which allows
527 // us using 32-bit signed offsets from the stack pointer.
528 if (GetFrameSize() > 0x7FFFFFFF)
529 LOG(FATAL) << "Stack frame larger than 2GB";
530
531 // Spill callee-saved registers.
532 // Note that their cumulative size is small and they can be indexed using
533 // 16-bit offsets.
534
535 // TODO: increment/decrement SP in one step instead of two or remove this comment.
536
537 uint32_t ofs = FrameEntrySpillSize();
538 __ IncreaseFrameSize(ofs);
539
540 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
541 GpuRegister reg = kCoreCalleeSaves[i];
542 if (allocated_registers_.ContainsCoreRegister(reg)) {
543 ofs -= kMips64WordSize;
544 __ Sd(reg, SP, ofs);
545 __ cfi().RelOffset(DWARFReg(reg), ofs);
546 }
547 }
548
549 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
550 FpuRegister reg = kFpuCalleeSaves[i];
551 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
552 ofs -= kMips64WordSize;
553 __ Sdc1(reg, SP, ofs);
554 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
555 }
556 }
557
558 // Allocate the rest of the frame and store the current method pointer
559 // at its end.
560
561 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
562
563 static_assert(IsInt<16>(kCurrentMethodStackOffset),
564 "kCurrentMethodStackOffset must fit into int16_t");
565 __ Sd(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
566}
567
568void CodeGeneratorMIPS64::GenerateFrameExit() {
569 __ cfi().RememberState();
570
571 // TODO: anything related to T9/GP/GOT/PIC/.so's?
572
573 if (!HasEmptyFrame()) {
574 // Deallocate the rest of the frame.
575
576 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
577
578 // Restore callee-saved registers.
579 // Note that their cumulative size is small and they can be indexed using
580 // 16-bit offsets.
581
582 // TODO: increment/decrement SP in one step instead of two or remove this comment.
583
584 uint32_t ofs = 0;
585
586 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
587 FpuRegister reg = kFpuCalleeSaves[i];
588 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
589 __ Ldc1(reg, SP, ofs);
590 ofs += kMips64WordSize;
591 // TODO: __ cfi().Restore(DWARFReg(reg));
592 }
593 }
594
595 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
596 GpuRegister reg = kCoreCalleeSaves[i];
597 if (allocated_registers_.ContainsCoreRegister(reg)) {
598 __ Ld(reg, SP, ofs);
599 ofs += kMips64WordSize;
600 __ cfi().Restore(DWARFReg(reg));
601 }
602 }
603
604 DCHECK_EQ(ofs, FrameEntrySpillSize());
605 __ DecreaseFrameSize(ofs);
606 }
607
608 __ Jr(RA);
609
610 __ cfi().RestoreState();
611 __ cfi().DefCFAOffset(GetFrameSize());
612}
613
614void CodeGeneratorMIPS64::Bind(HBasicBlock* block) {
615 __ Bind(GetLabelOf(block));
616}
617
618void CodeGeneratorMIPS64::MoveLocation(Location destination,
619 Location source,
620 Primitive::Type type) {
621 if (source.Equals(destination)) {
622 return;
623 }
624
625 // A valid move can always be inferred from the destination and source
626 // locations. When moving from and to a register, the argument type can be
627 // used to generate 32bit instead of 64bit moves.
628 bool unspecified_type = (type == Primitive::kPrimVoid);
629 DCHECK_EQ(unspecified_type, false);
630
631 if (destination.IsRegister() || destination.IsFpuRegister()) {
632 if (unspecified_type) {
633 HConstant* src_cst = source.IsConstant() ? source.GetConstant() : nullptr;
634 if (source.IsStackSlot() ||
635 (src_cst != nullptr && (src_cst->IsIntConstant()
636 || src_cst->IsFloatConstant()
637 || src_cst->IsNullConstant()))) {
638 // For stack slots and 32bit constants, a 64bit type is appropriate.
639 type = destination.IsRegister() ? Primitive::kPrimInt : Primitive::kPrimFloat;
640 } else {
641 // If the source is a double stack slot or a 64bit constant, a 64bit
642 // type is appropriate. Else the source is a register, and since the
643 // type has not been specified, we chose a 64bit type to force a 64bit
644 // move.
645 type = destination.IsRegister() ? Primitive::kPrimLong : Primitive::kPrimDouble;
646 }
647 }
648 DCHECK((destination.IsFpuRegister() && Primitive::IsFloatingPointType(type)) ||
649 (destination.IsRegister() && !Primitive::IsFloatingPointType(type)));
650 if (source.IsStackSlot() || source.IsDoubleStackSlot()) {
651 // Move to GPR/FPR from stack
652 LoadOperandType load_type = source.IsStackSlot() ? kLoadWord : kLoadDoubleword;
653 if (Primitive::IsFloatingPointType(type)) {
654 __ LoadFpuFromOffset(load_type,
655 destination.AsFpuRegister<FpuRegister>(),
656 SP,
657 source.GetStackIndex());
658 } else {
659 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
660 __ LoadFromOffset(load_type,
661 destination.AsRegister<GpuRegister>(),
662 SP,
663 source.GetStackIndex());
664 }
665 } else if (source.IsConstant()) {
666 // Move to GPR/FPR from constant
667 GpuRegister gpr = AT;
668 if (!Primitive::IsFloatingPointType(type)) {
669 gpr = destination.AsRegister<GpuRegister>();
670 }
671 if (type == Primitive::kPrimInt || type == Primitive::kPrimFloat) {
672 __ LoadConst32(gpr, GetInt32ValueOf(source.GetConstant()->AsConstant()));
673 } else {
674 __ LoadConst64(gpr, GetInt64ValueOf(source.GetConstant()->AsConstant()));
675 }
676 if (type == Primitive::kPrimFloat) {
677 __ Mtc1(gpr, destination.AsFpuRegister<FpuRegister>());
678 } else if (type == Primitive::kPrimDouble) {
679 __ Dmtc1(gpr, destination.AsFpuRegister<FpuRegister>());
680 }
681 } else {
682 if (destination.IsRegister()) {
683 // Move to GPR from GPR
684 __ Move(destination.AsRegister<GpuRegister>(), source.AsRegister<GpuRegister>());
685 } else {
686 // Move to FPR from FPR
687 if (type == Primitive::kPrimFloat) {
688 __ MovS(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
689 } else {
690 DCHECK_EQ(type, Primitive::kPrimDouble);
691 __ MovD(destination.AsFpuRegister<FpuRegister>(), source.AsFpuRegister<FpuRegister>());
692 }
693 }
694 }
695 } else { // The destination is not a register. It must be a stack slot.
696 DCHECK(destination.IsStackSlot() || destination.IsDoubleStackSlot());
697 if (source.IsRegister() || source.IsFpuRegister()) {
698 if (unspecified_type) {
699 if (source.IsRegister()) {
700 type = destination.IsStackSlot() ? Primitive::kPrimInt : Primitive::kPrimLong;
701 } else {
702 type = destination.IsStackSlot() ? Primitive::kPrimFloat : Primitive::kPrimDouble;
703 }
704 }
705 DCHECK((destination.IsDoubleStackSlot() == Primitive::Is64BitType(type)) &&
706 (source.IsFpuRegister() == Primitive::IsFloatingPointType(type)));
707 // Move to stack from GPR/FPR
708 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
709 if (source.IsRegister()) {
710 __ StoreToOffset(store_type,
711 source.AsRegister<GpuRegister>(),
712 SP,
713 destination.GetStackIndex());
714 } else {
715 __ StoreFpuToOffset(store_type,
716 source.AsFpuRegister<FpuRegister>(),
717 SP,
718 destination.GetStackIndex());
719 }
720 } else if (source.IsConstant()) {
721 // Move to stack from constant
722 HConstant* src_cst = source.GetConstant();
723 StoreOperandType store_type = destination.IsStackSlot() ? kStoreWord : kStoreDoubleword;
724 if (destination.IsStackSlot()) {
725 __ LoadConst32(TMP, GetInt32ValueOf(src_cst->AsConstant()));
726 } else {
727 __ LoadConst64(TMP, GetInt64ValueOf(src_cst->AsConstant()));
728 }
729 __ StoreToOffset(store_type, TMP, SP, destination.GetStackIndex());
730 } else {
731 DCHECK(source.IsStackSlot() || source.IsDoubleStackSlot());
732 DCHECK_EQ(source.IsDoubleStackSlot(), destination.IsDoubleStackSlot());
733 // Move to stack from stack
734 if (destination.IsStackSlot()) {
735 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
736 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
737 } else {
738 __ LoadFromOffset(kLoadDoubleword, TMP, SP, source.GetStackIndex());
739 __ StoreToOffset(kStoreDoubleword, TMP, SP, destination.GetStackIndex());
740 }
741 }
742 }
743}
744
745void CodeGeneratorMIPS64::SwapLocations(Location loc1,
746 Location loc2,
747 Primitive::Type type ATTRIBUTE_UNUSED) {
748 DCHECK(!loc1.IsConstant());
749 DCHECK(!loc2.IsConstant());
750
751 if (loc1.Equals(loc2)) {
752 return;
753 }
754
755 bool is_slot1 = loc1.IsStackSlot() || loc1.IsDoubleStackSlot();
756 bool is_slot2 = loc2.IsStackSlot() || loc2.IsDoubleStackSlot();
757 bool is_fp_reg1 = loc1.IsFpuRegister();
758 bool is_fp_reg2 = loc2.IsFpuRegister();
759
760 if (loc2.IsRegister() && loc1.IsRegister()) {
761 // Swap 2 GPRs
762 GpuRegister r1 = loc1.AsRegister<GpuRegister>();
763 GpuRegister r2 = loc2.AsRegister<GpuRegister>();
764 __ Move(TMP, r2);
765 __ Move(r2, r1);
766 __ Move(r1, TMP);
767 } else if (is_fp_reg2 && is_fp_reg1) {
768 // Swap 2 FPRs
769 FpuRegister r1 = loc1.AsFpuRegister<FpuRegister>();
770 FpuRegister r2 = loc2.AsFpuRegister<FpuRegister>();
771 // TODO: Can MOV.S/MOV.D be used here to save one instruction?
772 // Need to distinguish float from double, right?
773 __ Dmfc1(TMP, r2);
774 __ Dmfc1(AT, r1);
775 __ Dmtc1(TMP, r1);
776 __ Dmtc1(AT, r2);
777 } else if (is_slot1 != is_slot2) {
778 // Swap GPR/FPR and stack slot
779 Location reg_loc = is_slot1 ? loc2 : loc1;
780 Location mem_loc = is_slot1 ? loc1 : loc2;
781 LoadOperandType load_type = mem_loc.IsStackSlot() ? kLoadWord : kLoadDoubleword;
782 StoreOperandType store_type = mem_loc.IsStackSlot() ? kStoreWord : kStoreDoubleword;
783 // TODO: use load_type = kLoadUnsignedWord when type == Primitive::kPrimNot.
784 __ LoadFromOffset(load_type, TMP, SP, mem_loc.GetStackIndex());
785 if (reg_loc.IsFpuRegister()) {
786 __ StoreFpuToOffset(store_type,
787 reg_loc.AsFpuRegister<FpuRegister>(),
788 SP,
789 mem_loc.GetStackIndex());
790 // TODO: review this MTC1/DMTC1 move
791 if (mem_loc.IsStackSlot()) {
792 __ Mtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
793 } else {
794 DCHECK(mem_loc.IsDoubleStackSlot());
795 __ Dmtc1(TMP, reg_loc.AsFpuRegister<FpuRegister>());
796 }
797 } else {
798 __ StoreToOffset(store_type, reg_loc.AsRegister<GpuRegister>(), SP, mem_loc.GetStackIndex());
799 __ Move(reg_loc.AsRegister<GpuRegister>(), TMP);
800 }
801 } else if (is_slot1 && is_slot2) {
802 move_resolver_.Exchange(loc1.GetStackIndex(),
803 loc2.GetStackIndex(),
804 loc1.IsDoubleStackSlot());
805 } else {
806 LOG(FATAL) << "Unimplemented swap between locations " << loc1 << " and " << loc2;
807 }
808}
809
810void CodeGeneratorMIPS64::Move(HInstruction* instruction,
811 Location location,
812 HInstruction* move_for) {
813 LocationSummary* locations = instruction->GetLocations();
814 Primitive::Type type = instruction->GetType();
815 DCHECK_NE(type, Primitive::kPrimVoid);
816
817 if (instruction->IsCurrentMethod()) {
818 MoveLocation(location, Location::DoubleStackSlot(kCurrentMethodStackOffset), type);
819 } else if (locations != nullptr && locations->Out().Equals(location)) {
820 return;
821 } else if (instruction->IsIntConstant()
822 || instruction->IsLongConstant()
823 || instruction->IsNullConstant()) {
824 if (location.IsRegister()) {
825 // Move to GPR from constant
826 GpuRegister dst = location.AsRegister<GpuRegister>();
827 if (instruction->IsNullConstant() || instruction->IsIntConstant()) {
828 __ LoadConst32(dst, GetInt32ValueOf(instruction->AsConstant()));
829 } else {
830 __ LoadConst64(dst, instruction->AsLongConstant()->GetValue());
831 }
832 } else {
833 DCHECK(location.IsStackSlot() || location.IsDoubleStackSlot());
834 // Move to stack from constant
835 if (location.IsStackSlot()) {
836 __ LoadConst32(TMP, GetInt32ValueOf(instruction->AsConstant()));
837 __ StoreToOffset(kStoreWord, TMP, SP, location.GetStackIndex());
838 } else {
839 __ LoadConst64(TMP, instruction->AsLongConstant()->GetValue());
840 __ StoreToOffset(kStoreDoubleword, TMP, SP, location.GetStackIndex());
841 }
842 }
843 } else if (instruction->IsTemporary()) {
844 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
845 MoveLocation(location, temp_location, type);
846 } else if (instruction->IsLoadLocal()) {
847 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
848 if (Primitive::Is64BitType(type)) {
849 MoveLocation(location, Location::DoubleStackSlot(stack_slot), type);
850 } else {
851 MoveLocation(location, Location::StackSlot(stack_slot), type);
852 }
853 } else {
854 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
855 MoveLocation(location, locations->Out(), type);
856 }
857}
858
Calin Juravle175dc732015-08-25 15:42:32 +0100859void CodeGeneratorMIPS64::MoveConstant(Location location, int32_t value) {
860 DCHECK(location.IsRegister());
861 __ LoadConst32(location.AsRegister<GpuRegister>(), value);
862}
863
Calin Juravle23a8e352015-09-08 19:56:31 +0100864void CodeGeneratorMIPS64::AddLocationAsTemp(Location location, LocationSummary* locations) {
865 if (location.IsRegister()) {
866 locations->AddTemp(location);
867 } else {
868 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
869 }
870}
871
872void CodeGeneratorMIPS64::MoveLocationToTemp(Location source,
873 const LocationSummary& locations,
874 int temp_index,
875 Primitive::Type type) {
876 if (!Primitive::IsFloatingPointType(type)) {
877 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
878 }
879
880 DCHECK(source.IsFpuRegister()) << source;
881 if (type == Primitive::kPrimFloat) {
882 __ Mfc1(locations.GetTemp(temp_index).AsRegister<GpuRegister>(),
883 source.AsFpuRegister<FpuRegister>());
884 } else {
885 DCHECK_EQ(type, Primitive::kPrimDouble);
886 __ Dmfc1(locations.GetTemp(temp_index).AsRegister<GpuRegister>(),
887 source.AsFpuRegister<FpuRegister>());
888 }
889}
890
891void CodeGeneratorMIPS64::MoveTempToLocation(const LocationSummary& locations,
892 int temp_index,
893 Location destination,
894 Primitive::Type type) {
895 if (!Primitive::IsFloatingPointType(type)) {
896 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
897 }
898
899 DCHECK(destination.IsFpuRegister()) << destination;
900 if (type == Primitive::kPrimFloat) {
901 __ Mtc1(locations.GetTemp(temp_index).AsRegister<GpuRegister>(),
902 destination.AsFpuRegister<FpuRegister>());
903 } else {
904 DCHECK_EQ(type, Primitive::kPrimDouble);
905 __ Dmtc1(locations.GetTemp(temp_index).AsRegister<GpuRegister>(),
906 destination.AsFpuRegister<FpuRegister>());
907 }
908}
909
Alexey Frunze4dda3372015-06-01 18:31:49 -0700910Location CodeGeneratorMIPS64::GetStackLocation(HLoadLocal* load) const {
911 Primitive::Type type = load->GetType();
912
913 switch (type) {
914 case Primitive::kPrimNot:
915 case Primitive::kPrimInt:
916 case Primitive::kPrimFloat:
917 return Location::StackSlot(GetStackSlot(load->GetLocal()));
918
919 case Primitive::kPrimLong:
920 case Primitive::kPrimDouble:
921 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
922
923 case Primitive::kPrimBoolean:
924 case Primitive::kPrimByte:
925 case Primitive::kPrimChar:
926 case Primitive::kPrimShort:
927 case Primitive::kPrimVoid:
928 LOG(FATAL) << "Unexpected type " << type;
929 }
930
931 LOG(FATAL) << "Unreachable";
932 return Location::NoLocation();
933}
934
935void CodeGeneratorMIPS64::MarkGCCard(GpuRegister object, GpuRegister value) {
936 Label done;
937 GpuRegister card = AT;
938 GpuRegister temp = TMP;
939 __ Beqzc(value, &done);
940 __ LoadFromOffset(kLoadDoubleword,
941 card,
942 TR,
943 Thread::CardTableOffset<kMips64WordSize>().Int32Value());
944 __ Dsrl(temp, object, gc::accounting::CardTable::kCardShift);
945 __ Daddu(temp, card, temp);
946 __ Sb(card, temp, 0);
947 __ Bind(&done);
948}
949
950void CodeGeneratorMIPS64::SetupBlockedRegisters(bool is_baseline ATTRIBUTE_UNUSED) const {
951 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
952 blocked_core_registers_[ZERO] = true;
953 blocked_core_registers_[K0] = true;
954 blocked_core_registers_[K1] = true;
955 blocked_core_registers_[GP] = true;
956 blocked_core_registers_[SP] = true;
957 blocked_core_registers_[RA] = true;
958
959 // AT and TMP(T8) are used as temporary/scratch registers
960 // (similar to how AT is used by MIPS assemblers).
961 blocked_core_registers_[AT] = true;
962 blocked_core_registers_[TMP] = true;
963 blocked_fpu_registers_[FTMP] = true;
964
965 // Reserve suspend and thread registers.
966 blocked_core_registers_[S0] = true;
967 blocked_core_registers_[TR] = true;
968
969 // Reserve T9 for function calls
970 blocked_core_registers_[T9] = true;
971
972 // TODO: review; anything else?
973
974 // TODO: make these two for's conditional on is_baseline once
975 // all the issues with register saving/restoring are sorted out.
976 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
977 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
978 }
979
980 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
981 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
982 }
983}
984
985Location CodeGeneratorMIPS64::AllocateFreeRegister(Primitive::Type type) const {
986 if (type == Primitive::kPrimVoid) {
987 LOG(FATAL) << "Unreachable type " << type;
988 }
989
990 if (Primitive::IsFloatingPointType(type)) {
991 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFpuRegisters);
992 return Location::FpuRegisterLocation(reg);
993 } else {
994 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfGpuRegisters);
995 return Location::RegisterLocation(reg);
996 }
997}
998
999size_t CodeGeneratorMIPS64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1000 __ StoreToOffset(kStoreDoubleword, GpuRegister(reg_id), SP, stack_index);
1001 return kMips64WordSize;
1002}
1003
1004size_t CodeGeneratorMIPS64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1005 __ LoadFromOffset(kLoadDoubleword, GpuRegister(reg_id), SP, stack_index);
1006 return kMips64WordSize;
1007}
1008
1009size_t CodeGeneratorMIPS64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1010 __ StoreFpuToOffset(kStoreDoubleword, FpuRegister(reg_id), SP, stack_index);
1011 return kMips64WordSize;
1012}
1013
1014size_t CodeGeneratorMIPS64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1015 __ LoadFpuFromOffset(kLoadDoubleword, FpuRegister(reg_id), SP, stack_index);
1016 return kMips64WordSize;
1017}
1018
1019void CodeGeneratorMIPS64::DumpCoreRegister(std::ostream& stream, int reg) const {
1020 stream << Mips64ManagedRegister::FromGpuRegister(GpuRegister(reg));
1021}
1022
1023void CodeGeneratorMIPS64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1024 stream << Mips64ManagedRegister::FromFpuRegister(FpuRegister(reg));
1025}
1026
Calin Juravle175dc732015-08-25 15:42:32 +01001027void CodeGeneratorMIPS64::InvokeRuntime(QuickEntrypointEnum entrypoint,
1028 HInstruction* instruction,
1029 uint32_t dex_pc,
1030 SlowPathCode* slow_path) {
1031 InvokeRuntime(GetThreadOffset<kMips64WordSize>(entrypoint).Int32Value(),
1032 instruction,
1033 dex_pc,
1034 slow_path);
1035}
1036
Alexey Frunze4dda3372015-06-01 18:31:49 -07001037void CodeGeneratorMIPS64::InvokeRuntime(int32_t entry_point_offset,
1038 HInstruction* instruction,
1039 uint32_t dex_pc,
1040 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001041 ValidateInvokeRuntime(instruction, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001042 // TODO: anything related to T9/GP/GOT/PIC/.so's?
1043 __ LoadFromOffset(kLoadDoubleword, T9, TR, entry_point_offset);
1044 __ Jalr(T9);
1045 RecordPcInfo(instruction, dex_pc, slow_path);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001046}
1047
1048void InstructionCodeGeneratorMIPS64::GenerateClassInitializationCheck(SlowPathCodeMIPS64* slow_path,
1049 GpuRegister class_reg) {
1050 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1051 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1052 __ Bltc(TMP, AT, slow_path->GetEntryLabel());
1053 // TODO: barrier needed?
1054 __ Bind(slow_path->GetExitLabel());
1055}
1056
1057void InstructionCodeGeneratorMIPS64::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1058 __ Sync(0); // only stype 0 is supported
1059}
1060
1061void InstructionCodeGeneratorMIPS64::GenerateSuspendCheck(HSuspendCheck* instruction,
1062 HBasicBlock* successor) {
1063 SuspendCheckSlowPathMIPS64* slow_path =
1064 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS64(instruction, successor);
1065 codegen_->AddSlowPath(slow_path);
1066
1067 __ LoadFromOffset(kLoadUnsignedHalfword,
1068 TMP,
1069 TR,
1070 Thread::ThreadFlagsOffset<kMips64WordSize>().Int32Value());
1071 if (successor == nullptr) {
1072 __ Bnezc(TMP, slow_path->GetEntryLabel());
1073 __ Bind(slow_path->GetReturnLabel());
1074 } else {
1075 __ Beqzc(TMP, codegen_->GetLabelOf(successor));
1076 __ B(slow_path->GetEntryLabel());
1077 // slow_path will return to GetLabelOf(successor).
1078 }
1079}
1080
1081InstructionCodeGeneratorMIPS64::InstructionCodeGeneratorMIPS64(HGraph* graph,
1082 CodeGeneratorMIPS64* codegen)
1083 : HGraphVisitor(graph),
1084 assembler_(codegen->GetAssembler()),
1085 codegen_(codegen) {}
1086
1087void LocationsBuilderMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1088 DCHECK_EQ(instruction->InputCount(), 2U);
1089 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1090 Primitive::Type type = instruction->GetResultType();
1091 switch (type) {
1092 case Primitive::kPrimInt:
1093 case Primitive::kPrimLong: {
1094 locations->SetInAt(0, Location::RequiresRegister());
1095 HInstruction* right = instruction->InputAt(1);
1096 bool can_use_imm = false;
1097 if (right->IsConstant()) {
1098 int64_t imm = CodeGenerator::GetInt64ValueOf(right->AsConstant());
1099 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1100 can_use_imm = IsUint<16>(imm);
1101 } else if (instruction->IsAdd()) {
1102 can_use_imm = IsInt<16>(imm);
1103 } else {
1104 DCHECK(instruction->IsSub());
1105 can_use_imm = IsInt<16>(-imm);
1106 }
1107 }
1108 if (can_use_imm)
1109 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1110 else
1111 locations->SetInAt(1, Location::RequiresRegister());
1112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1113 }
1114 break;
1115
1116 case Primitive::kPrimFloat:
1117 case Primitive::kPrimDouble:
1118 locations->SetInAt(0, Location::RequiresFpuRegister());
1119 locations->SetInAt(1, Location::RequiresFpuRegister());
1120 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1121 break;
1122
1123 default:
1124 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1125 }
1126}
1127
1128void InstructionCodeGeneratorMIPS64::HandleBinaryOp(HBinaryOperation* instruction) {
1129 Primitive::Type type = instruction->GetType();
1130 LocationSummary* locations = instruction->GetLocations();
1131
1132 switch (type) {
1133 case Primitive::kPrimInt:
1134 case Primitive::kPrimLong: {
1135 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1136 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1137 Location rhs_location = locations->InAt(1);
1138
1139 GpuRegister rhs_reg = ZERO;
1140 int64_t rhs_imm = 0;
1141 bool use_imm = rhs_location.IsConstant();
1142 if (use_imm) {
1143 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1144 } else {
1145 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1146 }
1147
1148 if (instruction->IsAnd()) {
1149 if (use_imm)
1150 __ Andi(dst, lhs, rhs_imm);
1151 else
1152 __ And(dst, lhs, rhs_reg);
1153 } else if (instruction->IsOr()) {
1154 if (use_imm)
1155 __ Ori(dst, lhs, rhs_imm);
1156 else
1157 __ Or(dst, lhs, rhs_reg);
1158 } else if (instruction->IsXor()) {
1159 if (use_imm)
1160 __ Xori(dst, lhs, rhs_imm);
1161 else
1162 __ Xor(dst, lhs, rhs_reg);
1163 } else if (instruction->IsAdd()) {
1164 if (type == Primitive::kPrimInt) {
1165 if (use_imm)
1166 __ Addiu(dst, lhs, rhs_imm);
1167 else
1168 __ Addu(dst, lhs, rhs_reg);
1169 } else {
1170 if (use_imm)
1171 __ Daddiu(dst, lhs, rhs_imm);
1172 else
1173 __ Daddu(dst, lhs, rhs_reg);
1174 }
1175 } else {
1176 DCHECK(instruction->IsSub());
1177 if (type == Primitive::kPrimInt) {
1178 if (use_imm)
1179 __ Addiu(dst, lhs, -rhs_imm);
1180 else
1181 __ Subu(dst, lhs, rhs_reg);
1182 } else {
1183 if (use_imm)
1184 __ Daddiu(dst, lhs, -rhs_imm);
1185 else
1186 __ Dsubu(dst, lhs, rhs_reg);
1187 }
1188 }
1189 break;
1190 }
1191 case Primitive::kPrimFloat:
1192 case Primitive::kPrimDouble: {
1193 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1194 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1195 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1196 if (instruction->IsAdd()) {
1197 if (type == Primitive::kPrimFloat)
1198 __ AddS(dst, lhs, rhs);
1199 else
1200 __ AddD(dst, lhs, rhs);
1201 } else if (instruction->IsSub()) {
1202 if (type == Primitive::kPrimFloat)
1203 __ SubS(dst, lhs, rhs);
1204 else
1205 __ SubD(dst, lhs, rhs);
1206 } else {
1207 LOG(FATAL) << "Unexpected floating-point binary operation";
1208 }
1209 break;
1210 }
1211 default:
1212 LOG(FATAL) << "Unexpected binary operation type " << type;
1213 }
1214}
1215
1216void LocationsBuilderMIPS64::HandleShift(HBinaryOperation* instr) {
1217 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1218
1219 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1220 Primitive::Type type = instr->GetResultType();
1221 switch (type) {
1222 case Primitive::kPrimInt:
1223 case Primitive::kPrimLong: {
1224 locations->SetInAt(0, Location::RequiresRegister());
1225 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1226 locations->SetOut(Location::RequiresRegister());
1227 break;
1228 }
1229 default:
1230 LOG(FATAL) << "Unexpected shift type " << type;
1231 }
1232}
1233
1234void InstructionCodeGeneratorMIPS64::HandleShift(HBinaryOperation* instr) {
1235 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1236 LocationSummary* locations = instr->GetLocations();
1237 Primitive::Type type = instr->GetType();
1238
1239 switch (type) {
1240 case Primitive::kPrimInt:
1241 case Primitive::kPrimLong: {
1242 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1243 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1244 Location rhs_location = locations->InAt(1);
1245
1246 GpuRegister rhs_reg = ZERO;
1247 int64_t rhs_imm = 0;
1248 bool use_imm = rhs_location.IsConstant();
1249 if (use_imm) {
1250 rhs_imm = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant());
1251 } else {
1252 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1253 }
1254
1255 if (use_imm) {
1256 uint32_t shift_value = (type == Primitive::kPrimInt)
1257 ? static_cast<uint32_t>(rhs_imm & kMaxIntShiftValue)
1258 : static_cast<uint32_t>(rhs_imm & kMaxLongShiftValue);
1259
1260 if (type == Primitive::kPrimInt) {
1261 if (instr->IsShl()) {
1262 __ Sll(dst, lhs, shift_value);
1263 } else if (instr->IsShr()) {
1264 __ Sra(dst, lhs, shift_value);
1265 } else {
1266 __ Srl(dst, lhs, shift_value);
1267 }
1268 } else {
1269 if (shift_value < 32) {
1270 if (instr->IsShl()) {
1271 __ Dsll(dst, lhs, shift_value);
1272 } else if (instr->IsShr()) {
1273 __ Dsra(dst, lhs, shift_value);
1274 } else {
1275 __ Dsrl(dst, lhs, shift_value);
1276 }
1277 } else {
1278 shift_value -= 32;
1279 if (instr->IsShl()) {
1280 __ Dsll32(dst, lhs, shift_value);
1281 } else if (instr->IsShr()) {
1282 __ Dsra32(dst, lhs, shift_value);
1283 } else {
1284 __ Dsrl32(dst, lhs, shift_value);
1285 }
1286 }
1287 }
1288 } else {
1289 if (type == Primitive::kPrimInt) {
1290 if (instr->IsShl()) {
1291 __ Sllv(dst, lhs, rhs_reg);
1292 } else if (instr->IsShr()) {
1293 __ Srav(dst, lhs, rhs_reg);
1294 } else {
1295 __ Srlv(dst, lhs, rhs_reg);
1296 }
1297 } else {
1298 if (instr->IsShl()) {
1299 __ Dsllv(dst, lhs, rhs_reg);
1300 } else if (instr->IsShr()) {
1301 __ Dsrav(dst, lhs, rhs_reg);
1302 } else {
1303 __ Dsrlv(dst, lhs, rhs_reg);
1304 }
1305 }
1306 }
1307 break;
1308 }
1309 default:
1310 LOG(FATAL) << "Unexpected shift operation type " << type;
1311 }
1312}
1313
1314void LocationsBuilderMIPS64::VisitAdd(HAdd* instruction) {
1315 HandleBinaryOp(instruction);
1316}
1317
1318void InstructionCodeGeneratorMIPS64::VisitAdd(HAdd* instruction) {
1319 HandleBinaryOp(instruction);
1320}
1321
1322void LocationsBuilderMIPS64::VisitAnd(HAnd* instruction) {
1323 HandleBinaryOp(instruction);
1324}
1325
1326void InstructionCodeGeneratorMIPS64::VisitAnd(HAnd* instruction) {
1327 HandleBinaryOp(instruction);
1328}
1329
1330void LocationsBuilderMIPS64::VisitArrayGet(HArrayGet* instruction) {
1331 LocationSummary* locations =
1332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1333 locations->SetInAt(0, Location::RequiresRegister());
1334 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1335 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1337 } else {
1338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1339 }
1340}
1341
1342void InstructionCodeGeneratorMIPS64::VisitArrayGet(HArrayGet* instruction) {
1343 LocationSummary* locations = instruction->GetLocations();
1344 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1345 Location index = locations->InAt(1);
1346 Primitive::Type type = instruction->GetType();
1347
1348 switch (type) {
1349 case Primitive::kPrimBoolean: {
1350 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1351 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1352 if (index.IsConstant()) {
1353 size_t offset =
1354 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1355 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1356 } else {
1357 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1358 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1359 }
1360 break;
1361 }
1362
1363 case Primitive::kPrimByte: {
1364 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1365 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1366 if (index.IsConstant()) {
1367 size_t offset =
1368 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1369 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1370 } else {
1371 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1372 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1373 }
1374 break;
1375 }
1376
1377 case Primitive::kPrimShort: {
1378 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1379 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1380 if (index.IsConstant()) {
1381 size_t offset =
1382 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1383 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1384 } else {
1385 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1386 __ Daddu(TMP, obj, TMP);
1387 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1388 }
1389 break;
1390 }
1391
1392 case Primitive::kPrimChar: {
1393 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1394 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1395 if (index.IsConstant()) {
1396 size_t offset =
1397 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1398 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1399 } else {
1400 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1401 __ Daddu(TMP, obj, TMP);
1402 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1403 }
1404 break;
1405 }
1406
1407 case Primitive::kPrimInt:
1408 case Primitive::kPrimNot: {
1409 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1410 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1411 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1412 LoadOperandType load_type = (type == Primitive::kPrimNot) ? kLoadUnsignedWord : kLoadWord;
1413 if (index.IsConstant()) {
1414 size_t offset =
1415 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1416 __ LoadFromOffset(load_type, out, obj, offset);
1417 } else {
1418 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1419 __ Daddu(TMP, obj, TMP);
1420 __ LoadFromOffset(load_type, out, TMP, data_offset);
1421 }
1422 break;
1423 }
1424
1425 case Primitive::kPrimLong: {
1426 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1427 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1428 if (index.IsConstant()) {
1429 size_t offset =
1430 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1431 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1432 } else {
1433 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1434 __ Daddu(TMP, obj, TMP);
1435 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1436 }
1437 break;
1438 }
1439
1440 case Primitive::kPrimFloat: {
1441 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1442 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1443 if (index.IsConstant()) {
1444 size_t offset =
1445 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1446 __ LoadFpuFromOffset(kLoadWord, out, obj, offset);
1447 } else {
1448 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1449 __ Daddu(TMP, obj, TMP);
1450 __ LoadFpuFromOffset(kLoadWord, out, TMP, data_offset);
1451 }
1452 break;
1453 }
1454
1455 case Primitive::kPrimDouble: {
1456 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1457 FpuRegister out = locations->Out().AsFpuRegister<FpuRegister>();
1458 if (index.IsConstant()) {
1459 size_t offset =
1460 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1461 __ LoadFpuFromOffset(kLoadDoubleword, out, obj, offset);
1462 } else {
1463 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1464 __ Daddu(TMP, obj, TMP);
1465 __ LoadFpuFromOffset(kLoadDoubleword, out, TMP, data_offset);
1466 }
1467 break;
1468 }
1469
1470 case Primitive::kPrimVoid:
1471 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1472 UNREACHABLE();
1473 }
1474 codegen_->MaybeRecordImplicitNullCheck(instruction);
1475}
1476
1477void LocationsBuilderMIPS64::VisitArrayLength(HArrayLength* instruction) {
1478 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1479 locations->SetInAt(0, Location::RequiresRegister());
1480 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1481}
1482
1483void InstructionCodeGeneratorMIPS64::VisitArrayLength(HArrayLength* instruction) {
1484 LocationSummary* locations = instruction->GetLocations();
1485 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1486 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1487 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
1488 __ LoadFromOffset(kLoadWord, out, obj, offset);
1489 codegen_->MaybeRecordImplicitNullCheck(instruction);
1490}
1491
1492void LocationsBuilderMIPS64::VisitArraySet(HArraySet* instruction) {
1493 Primitive::Type value_type = instruction->GetComponentType();
1494 bool is_object = value_type == Primitive::kPrimNot;
1495 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1496 instruction,
1497 is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1498 if (is_object) {
1499 InvokeRuntimeCallingConvention calling_convention;
1500 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1501 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1502 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1503 } else {
1504 locations->SetInAt(0, Location::RequiresRegister());
1505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1506 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1507 locations->SetInAt(2, Location::RequiresFpuRegister());
1508 } else {
1509 locations->SetInAt(2, Location::RequiresRegister());
1510 }
1511 }
1512}
1513
1514void InstructionCodeGeneratorMIPS64::VisitArraySet(HArraySet* instruction) {
1515 LocationSummary* locations = instruction->GetLocations();
1516 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1517 Location index = locations->InAt(1);
1518 Primitive::Type value_type = instruction->GetComponentType();
1519 bool needs_runtime_call = locations->WillCall();
1520 bool needs_write_barrier =
1521 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1522
1523 switch (value_type) {
1524 case Primitive::kPrimBoolean:
1525 case Primitive::kPrimByte: {
1526 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1527 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1528 if (index.IsConstant()) {
1529 size_t offset =
1530 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1531 __ StoreToOffset(kStoreByte, value, obj, offset);
1532 } else {
1533 __ Daddu(TMP, obj, index.AsRegister<GpuRegister>());
1534 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1535 }
1536 break;
1537 }
1538
1539 case Primitive::kPrimShort:
1540 case Primitive::kPrimChar: {
1541 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1542 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1543 if (index.IsConstant()) {
1544 size_t offset =
1545 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1546 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1547 } else {
1548 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_2);
1549 __ Daddu(TMP, obj, TMP);
1550 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1551 }
1552 break;
1553 }
1554
1555 case Primitive::kPrimInt:
1556 case Primitive::kPrimNot: {
1557 if (!needs_runtime_call) {
1558 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1559 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1560 if (index.IsConstant()) {
1561 size_t offset =
1562 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1563 __ StoreToOffset(kStoreWord, value, obj, offset);
1564 } else {
1565 DCHECK(index.IsRegister()) << index;
1566 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1567 __ Daddu(TMP, obj, TMP);
1568 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1569 }
1570 codegen_->MaybeRecordImplicitNullCheck(instruction);
1571 if (needs_write_barrier) {
1572 DCHECK_EQ(value_type, Primitive::kPrimNot);
1573 codegen_->MarkGCCard(obj, value);
1574 }
1575 } else {
1576 DCHECK_EQ(value_type, Primitive::kPrimNot);
1577 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1578 instruction,
1579 instruction->GetDexPc(),
1580 nullptr);
1581 }
1582 break;
1583 }
1584
1585 case Primitive::kPrimLong: {
1586 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1587 GpuRegister value = locations->InAt(2).AsRegister<GpuRegister>();
1588 if (index.IsConstant()) {
1589 size_t offset =
1590 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1591 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1592 } else {
1593 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1594 __ Daddu(TMP, obj, TMP);
1595 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1596 }
1597 break;
1598 }
1599
1600 case Primitive::kPrimFloat: {
1601 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1602 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1603 DCHECK(locations->InAt(2).IsFpuRegister());
1604 if (index.IsConstant()) {
1605 size_t offset =
1606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1607 __ StoreFpuToOffset(kStoreWord, value, obj, offset);
1608 } else {
1609 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_4);
1610 __ Daddu(TMP, obj, TMP);
1611 __ StoreFpuToOffset(kStoreWord, value, TMP, data_offset);
1612 }
1613 break;
1614 }
1615
1616 case Primitive::kPrimDouble: {
1617 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1618 FpuRegister value = locations->InAt(2).AsFpuRegister<FpuRegister>();
1619 DCHECK(locations->InAt(2).IsFpuRegister());
1620 if (index.IsConstant()) {
1621 size_t offset =
1622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1623 __ StoreFpuToOffset(kStoreDoubleword, value, obj, offset);
1624 } else {
1625 __ Dsll(TMP, index.AsRegister<GpuRegister>(), TIMES_8);
1626 __ Daddu(TMP, obj, TMP);
1627 __ StoreFpuToOffset(kStoreDoubleword, value, TMP, data_offset);
1628 }
1629 break;
1630 }
1631
1632 case Primitive::kPrimVoid:
1633 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1634 UNREACHABLE();
1635 }
1636
1637 // Ints and objects are handled in the switch.
1638 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1639 codegen_->MaybeRecordImplicitNullCheck(instruction);
1640 }
1641}
1642
1643void LocationsBuilderMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001644 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1645 ? LocationSummary::kCallOnSlowPath
1646 : LocationSummary::kNoCall;
1647 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001648 locations->SetInAt(0, Location::RequiresRegister());
1649 locations->SetInAt(1, Location::RequiresRegister());
1650 if (instruction->HasUses()) {
1651 locations->SetOut(Location::SameAsFirstInput());
1652 }
1653}
1654
1655void InstructionCodeGeneratorMIPS64::VisitBoundsCheck(HBoundsCheck* instruction) {
1656 LocationSummary* locations = instruction->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001657 BoundsCheckSlowPathMIPS64* slow_path =
1658 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001659 codegen_->AddSlowPath(slow_path);
1660
1661 GpuRegister index = locations->InAt(0).AsRegister<GpuRegister>();
1662 GpuRegister length = locations->InAt(1).AsRegister<GpuRegister>();
1663
1664 // length is limited by the maximum positive signed 32-bit integer.
1665 // Unsigned comparison of length and index checks for index < 0
1666 // and for length <= index simultaneously.
1667 // Mips R6 requires lhs != rhs for compact branches.
1668 if (index == length) {
1669 __ B(slow_path->GetEntryLabel());
1670 } else {
1671 __ Bgeuc(index, length, slow_path->GetEntryLabel());
1672 }
1673}
1674
1675void LocationsBuilderMIPS64::VisitCheckCast(HCheckCast* instruction) {
1676 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1677 instruction,
1678 LocationSummary::kCallOnSlowPath);
1679 locations->SetInAt(0, Location::RequiresRegister());
1680 locations->SetInAt(1, Location::RequiresRegister());
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001681 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07001682 locations->AddTemp(Location::RequiresRegister());
1683}
1684
1685void InstructionCodeGeneratorMIPS64::VisitCheckCast(HCheckCast* instruction) {
1686 LocationSummary* locations = instruction->GetLocations();
1687 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
1688 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
1689 GpuRegister obj_cls = locations->GetTemp(0).AsRegister<GpuRegister>();
1690
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01001691 SlowPathCodeMIPS64* slow_path =
1692 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001693 codegen_->AddSlowPath(slow_path);
1694
1695 // TODO: avoid this check if we know obj is not null.
1696 __ Beqzc(obj, slow_path->GetExitLabel());
1697 // Compare the class of `obj` with `cls`.
1698 __ LoadFromOffset(kLoadUnsignedWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1699 __ Bnec(obj_cls, cls, slow_path->GetEntryLabel());
1700 __ Bind(slow_path->GetExitLabel());
1701}
1702
1703void LocationsBuilderMIPS64::VisitClinitCheck(HClinitCheck* check) {
1704 LocationSummary* locations =
1705 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1706 locations->SetInAt(0, Location::RequiresRegister());
1707 if (check->HasUses()) {
1708 locations->SetOut(Location::SameAsFirstInput());
1709 }
1710}
1711
1712void InstructionCodeGeneratorMIPS64::VisitClinitCheck(HClinitCheck* check) {
1713 // We assume the class is not null.
1714 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
1715 check->GetLoadClass(),
1716 check,
1717 check->GetDexPc(),
1718 true);
1719 codegen_->AddSlowPath(slow_path);
1720 GenerateClassInitializationCheck(slow_path,
1721 check->GetLocations()->InAt(0).AsRegister<GpuRegister>());
1722}
1723
1724void LocationsBuilderMIPS64::VisitCompare(HCompare* compare) {
1725 Primitive::Type in_type = compare->InputAt(0)->GetType();
1726
1727 LocationSummary::CallKind call_kind = Primitive::IsFloatingPointType(in_type)
1728 ? LocationSummary::kCall
1729 : LocationSummary::kNoCall;
1730
1731 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare, call_kind);
1732
1733 switch (in_type) {
1734 case Primitive::kPrimLong:
1735 locations->SetInAt(0, Location::RequiresRegister());
1736 locations->SetInAt(1, Location::RequiresRegister());
1737 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1738 break;
1739
1740 case Primitive::kPrimFloat:
1741 case Primitive::kPrimDouble: {
1742 InvokeRuntimeCallingConvention calling_convention;
1743 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1744 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1745 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1746 break;
1747 }
1748
1749 default:
1750 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1751 }
1752}
1753
1754void InstructionCodeGeneratorMIPS64::VisitCompare(HCompare* instruction) {
1755 LocationSummary* locations = instruction->GetLocations();
1756 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1757
1758 // 0 if: left == right
1759 // 1 if: left > right
1760 // -1 if: left < right
1761 switch (in_type) {
1762 case Primitive::kPrimLong: {
1763 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1764 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1765 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
1766 // TODO: more efficient (direct) comparison with a constant
1767 __ Slt(TMP, lhs, rhs);
1768 __ Slt(dst, rhs, lhs);
1769 __ Subu(dst, dst, TMP);
1770 break;
1771 }
1772
1773 case Primitive::kPrimFloat:
1774 case Primitive::kPrimDouble: {
1775 int32_t entry_point_offset;
1776 if (in_type == Primitive::kPrimFloat) {
1777 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgFloat)
1778 : QUICK_ENTRY_POINT(pCmplFloat);
1779 } else {
1780 entry_point_offset = instruction->IsGtBias() ? QUICK_ENTRY_POINT(pCmpgDouble)
1781 : QUICK_ENTRY_POINT(pCmplDouble);
1782 }
1783 codegen_->InvokeRuntime(entry_point_offset, instruction, instruction->GetDexPc(), nullptr);
1784 break;
1785 }
1786
1787 default:
1788 LOG(FATAL) << "Unimplemented compare type " << in_type;
1789 }
1790}
1791
1792void LocationsBuilderMIPS64::VisitCondition(HCondition* instruction) {
1793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1794 locations->SetInAt(0, Location::RequiresRegister());
1795 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1796 if (instruction->NeedsMaterialization()) {
1797 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1798 }
1799}
1800
1801void InstructionCodeGeneratorMIPS64::VisitCondition(HCondition* instruction) {
1802 if (!instruction->NeedsMaterialization()) {
1803 return;
1804 }
1805
1806 LocationSummary* locations = instruction->GetLocations();
1807
1808 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1809 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1810 Location rhs_location = locations->InAt(1);
1811
1812 GpuRegister rhs_reg = ZERO;
1813 int64_t rhs_imm = 0;
1814 bool use_imm = rhs_location.IsConstant();
1815 if (use_imm) {
1816 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1817 } else {
1818 rhs_reg = rhs_location.AsRegister<GpuRegister>();
1819 }
1820
1821 IfCondition if_cond = instruction->GetCondition();
1822
1823 switch (if_cond) {
1824 case kCondEQ:
1825 case kCondNE:
1826 if (use_imm && IsUint<16>(rhs_imm)) {
1827 __ Xori(dst, lhs, rhs_imm);
1828 } else {
1829 if (use_imm) {
1830 rhs_reg = TMP;
1831 __ LoadConst32(rhs_reg, rhs_imm);
1832 }
1833 __ Xor(dst, lhs, rhs_reg);
1834 }
1835 if (if_cond == kCondEQ) {
1836 __ Sltiu(dst, dst, 1);
1837 } else {
1838 __ Sltu(dst, ZERO, dst);
1839 }
1840 break;
1841
1842 case kCondLT:
1843 case kCondGE:
1844 if (use_imm && IsInt<16>(rhs_imm)) {
1845 __ Slti(dst, lhs, rhs_imm);
1846 } else {
1847 if (use_imm) {
1848 rhs_reg = TMP;
1849 __ LoadConst32(rhs_reg, rhs_imm);
1850 }
1851 __ Slt(dst, lhs, rhs_reg);
1852 }
1853 if (if_cond == kCondGE) {
1854 // Simulate lhs >= rhs via !(lhs < rhs) since there's
1855 // only the slt instruction but no sge.
1856 __ Xori(dst, dst, 1);
1857 }
1858 break;
1859
1860 case kCondLE:
1861 case kCondGT:
1862 if (use_imm && IsInt<16>(rhs_imm + 1)) {
1863 // Simulate lhs <= rhs via lhs < rhs + 1.
1864 __ Slti(dst, lhs, rhs_imm + 1);
1865 if (if_cond == kCondGT) {
1866 // Simulate lhs > rhs via !(lhs <= rhs) since there's
1867 // only the slti instruction but no sgti.
1868 __ Xori(dst, dst, 1);
1869 }
1870 } else {
1871 if (use_imm) {
1872 rhs_reg = TMP;
1873 __ LoadConst32(rhs_reg, rhs_imm);
1874 }
1875 __ Slt(dst, rhs_reg, lhs);
1876 if (if_cond == kCondLE) {
1877 // Simulate lhs <= rhs via !(rhs < lhs) since there's
1878 // only the slt instruction but no sle.
1879 __ Xori(dst, dst, 1);
1880 }
1881 }
1882 break;
1883 }
1884}
1885
1886void LocationsBuilderMIPS64::VisitDiv(HDiv* div) {
1887 LocationSummary* locations =
1888 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
1889 switch (div->GetResultType()) {
1890 case Primitive::kPrimInt:
1891 case Primitive::kPrimLong:
1892 locations->SetInAt(0, Location::RequiresRegister());
1893 locations->SetInAt(1, Location::RequiresRegister());
1894 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1895 break;
1896
1897 case Primitive::kPrimFloat:
1898 case Primitive::kPrimDouble:
1899 locations->SetInAt(0, Location::RequiresFpuRegister());
1900 locations->SetInAt(1, Location::RequiresFpuRegister());
1901 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
1906 }
1907}
1908
1909void InstructionCodeGeneratorMIPS64::VisitDiv(HDiv* instruction) {
1910 Primitive::Type type = instruction->GetType();
1911 LocationSummary* locations = instruction->GetLocations();
1912
1913 switch (type) {
1914 case Primitive::kPrimInt:
1915 case Primitive::kPrimLong: {
1916 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
1917 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
1918 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
1919 if (type == Primitive::kPrimInt)
1920 __ DivR6(dst, lhs, rhs);
1921 else
1922 __ Ddiv(dst, lhs, rhs);
1923 break;
1924 }
1925 case Primitive::kPrimFloat:
1926 case Primitive::kPrimDouble: {
1927 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
1928 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
1929 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
1930 if (type == Primitive::kPrimFloat)
1931 __ DivS(dst, lhs, rhs);
1932 else
1933 __ DivD(dst, lhs, rhs);
1934 break;
1935 }
1936 default:
1937 LOG(FATAL) << "Unexpected div type " << type;
1938 }
1939}
1940
1941void LocationsBuilderMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001942 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1943 ? LocationSummary::kCallOnSlowPath
1944 : LocationSummary::kNoCall;
1945 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07001946 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
1947 if (instruction->HasUses()) {
1948 locations->SetOut(Location::SameAsFirstInput());
1949 }
1950}
1951
1952void InstructionCodeGeneratorMIPS64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
1953 SlowPathCodeMIPS64* slow_path =
1954 new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS64(instruction);
1955 codegen_->AddSlowPath(slow_path);
1956 Location value = instruction->GetLocations()->InAt(0);
1957
1958 Primitive::Type type = instruction->GetType();
1959
Serguei Katkov8c0676c2015-08-03 13:55:33 +06001960 if ((type == Primitive::kPrimBoolean) || !Primitive::IsIntegralType(type)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07001961 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
Serguei Katkov8c0676c2015-08-03 13:55:33 +06001962 return;
Alexey Frunze4dda3372015-06-01 18:31:49 -07001963 }
1964
1965 if (value.IsConstant()) {
1966 int64_t divisor = codegen_->GetInt64ValueOf(value.GetConstant()->AsConstant());
1967 if (divisor == 0) {
1968 __ B(slow_path->GetEntryLabel());
1969 } else {
1970 // A division by a non-null constant is valid. We don't need to perform
1971 // any check, so simply fall through.
1972 }
1973 } else {
1974 __ Beqzc(value.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
1975 }
1976}
1977
1978void LocationsBuilderMIPS64::VisitDoubleConstant(HDoubleConstant* constant) {
1979 LocationSummary* locations =
1980 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1981 locations->SetOut(Location::ConstantLocation(constant));
1982}
1983
1984void InstructionCodeGeneratorMIPS64::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
1985 // Will be generated at use site.
1986}
1987
1988void LocationsBuilderMIPS64::VisitExit(HExit* exit) {
1989 exit->SetLocations(nullptr);
1990}
1991
1992void InstructionCodeGeneratorMIPS64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
1993}
1994
1995void LocationsBuilderMIPS64::VisitFloatConstant(HFloatConstant* constant) {
1996 LocationSummary* locations =
1997 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1998 locations->SetOut(Location::ConstantLocation(constant));
1999}
2000
2001void InstructionCodeGeneratorMIPS64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2002 // Will be generated at use site.
2003}
2004
David Brazdilfc6a86a2015-06-26 10:33:45 +00002005void InstructionCodeGeneratorMIPS64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002006 DCHECK(!successor->IsExitBlock());
2007 HBasicBlock* block = got->GetBlock();
2008 HInstruction* previous = got->GetPrevious();
2009 HLoopInformation* info = block->GetLoopInformation();
2010
2011 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2012 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2013 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2014 return;
2015 }
2016 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2017 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2018 }
2019 if (!codegen_->GoesToNextBlock(block, successor)) {
2020 __ B(codegen_->GetLabelOf(successor));
2021 }
2022}
2023
David Brazdilfc6a86a2015-06-26 10:33:45 +00002024void LocationsBuilderMIPS64::VisitGoto(HGoto* got) {
2025 got->SetLocations(nullptr);
2026}
2027
2028void InstructionCodeGeneratorMIPS64::VisitGoto(HGoto* got) {
2029 HandleGoto(got, got->GetSuccessor());
2030}
2031
2032void LocationsBuilderMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2033 try_boundary->SetLocations(nullptr);
2034}
2035
2036void InstructionCodeGeneratorMIPS64::VisitTryBoundary(HTryBoundary* try_boundary) {
2037 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2038 if (!successor->IsExitBlock()) {
2039 HandleGoto(try_boundary, successor);
2040 }
2041}
2042
Alexey Frunze4dda3372015-06-01 18:31:49 -07002043void InstructionCodeGeneratorMIPS64::GenerateTestAndBranch(HInstruction* instruction,
2044 Label* true_target,
2045 Label* false_target,
2046 Label* always_true_target) {
2047 HInstruction* cond = instruction->InputAt(0);
2048 HCondition* condition = cond->AsCondition();
2049
2050 if (cond->IsIntConstant()) {
2051 int32_t cond_value = cond->AsIntConstant()->GetValue();
2052 if (cond_value == 1) {
2053 if (always_true_target != nullptr) {
2054 __ B(always_true_target);
2055 }
2056 return;
2057 } else {
2058 DCHECK_EQ(cond_value, 0);
2059 }
2060 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
2061 // The condition instruction has been materialized, compare the output to 0.
2062 Location cond_val = instruction->GetLocations()->InAt(0);
2063 DCHECK(cond_val.IsRegister());
2064 __ Bnezc(cond_val.AsRegister<GpuRegister>(), true_target);
2065 } else {
2066 // The condition instruction has not been materialized, use its inputs as
2067 // the comparison and its condition as the branch condition.
2068 GpuRegister lhs = condition->GetLocations()->InAt(0).AsRegister<GpuRegister>();
2069 Location rhs_location = condition->GetLocations()->InAt(1);
2070 GpuRegister rhs_reg = ZERO;
2071 int32_t rhs_imm = 0;
2072 bool use_imm = rhs_location.IsConstant();
2073 if (use_imm) {
2074 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2075 } else {
2076 rhs_reg = rhs_location.AsRegister<GpuRegister>();
2077 }
2078
2079 IfCondition if_cond = condition->GetCondition();
2080 if (use_imm && rhs_imm == 0) {
2081 switch (if_cond) {
2082 case kCondEQ:
2083 __ Beqzc(lhs, true_target);
2084 break;
2085 case kCondNE:
2086 __ Bnezc(lhs, true_target);
2087 break;
2088 case kCondLT:
2089 __ Bltzc(lhs, true_target);
2090 break;
2091 case kCondGE:
2092 __ Bgezc(lhs, true_target);
2093 break;
2094 case kCondLE:
2095 __ Blezc(lhs, true_target);
2096 break;
2097 case kCondGT:
2098 __ Bgtzc(lhs, true_target);
2099 break;
2100 }
2101 } else {
2102 if (use_imm) {
2103 rhs_reg = TMP;
2104 __ LoadConst32(rhs_reg, rhs_imm);
2105 }
2106 // It looks like we can get here with lhs == rhs. Should that be possible at all?
2107 // Mips R6 requires lhs != rhs for compact branches.
2108 if (lhs == rhs_reg) {
2109 DCHECK(!use_imm);
2110 switch (if_cond) {
2111 case kCondEQ:
2112 case kCondGE:
2113 case kCondLE:
2114 // if lhs == rhs for a positive condition, then it is a branch
2115 __ B(true_target);
2116 break;
2117 case kCondNE:
2118 case kCondLT:
2119 case kCondGT:
2120 // if lhs == rhs for a negative condition, then it is a NOP
2121 break;
2122 }
2123 } else {
2124 switch (if_cond) {
2125 case kCondEQ:
2126 __ Beqc(lhs, rhs_reg, true_target);
2127 break;
2128 case kCondNE:
2129 __ Bnec(lhs, rhs_reg, true_target);
2130 break;
2131 case kCondLT:
2132 __ Bltc(lhs, rhs_reg, true_target);
2133 break;
2134 case kCondGE:
2135 __ Bgec(lhs, rhs_reg, true_target);
2136 break;
2137 case kCondLE:
2138 __ Bgec(rhs_reg, lhs, true_target);
2139 break;
2140 case kCondGT:
2141 __ Bltc(rhs_reg, lhs, true_target);
2142 break;
2143 }
2144 }
2145 }
2146 }
2147 if (false_target != nullptr) {
2148 __ B(false_target);
2149 }
2150}
2151
2152void LocationsBuilderMIPS64::VisitIf(HIf* if_instr) {
2153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2154 HInstruction* cond = if_instr->InputAt(0);
2155 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2156 locations->SetInAt(0, Location::RequiresRegister());
2157 }
2158}
2159
2160void InstructionCodeGeneratorMIPS64::VisitIf(HIf* if_instr) {
2161 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2162 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2163 Label* always_true_target = true_target;
2164 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2165 if_instr->IfTrueSuccessor())) {
2166 always_true_target = nullptr;
2167 }
2168 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2169 if_instr->IfFalseSuccessor())) {
2170 false_target = nullptr;
2171 }
2172 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2173}
2174
2175void LocationsBuilderMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2176 LocationSummary* locations = new (GetGraph()->GetArena())
2177 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2178 HInstruction* cond = deoptimize->InputAt(0);
2179 DCHECK(cond->IsCondition());
2180 if (cond->AsCondition()->NeedsMaterialization()) {
2181 locations->SetInAt(0, Location::RequiresRegister());
2182 }
2183}
2184
2185void InstructionCodeGeneratorMIPS64::VisitDeoptimize(HDeoptimize* deoptimize) {
2186 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena())
2187 DeoptimizationSlowPathMIPS64(deoptimize);
2188 codegen_->AddSlowPath(slow_path);
2189 Label* slow_path_entry = slow_path->GetEntryLabel();
2190 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2191}
2192
2193void LocationsBuilderMIPS64::HandleFieldGet(HInstruction* instruction,
2194 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2197 locations->SetInAt(0, Location::RequiresRegister());
2198 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2199 locations->SetOut(Location::RequiresFpuRegister());
2200 } else {
2201 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2202 }
2203}
2204
2205void InstructionCodeGeneratorMIPS64::HandleFieldGet(HInstruction* instruction,
2206 const FieldInfo& field_info) {
2207 Primitive::Type type = field_info.GetFieldType();
2208 LocationSummary* locations = instruction->GetLocations();
2209 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2210 LoadOperandType load_type = kLoadUnsignedByte;
2211 switch (type) {
2212 case Primitive::kPrimBoolean:
2213 load_type = kLoadUnsignedByte;
2214 break;
2215 case Primitive::kPrimByte:
2216 load_type = kLoadSignedByte;
2217 break;
2218 case Primitive::kPrimShort:
2219 load_type = kLoadSignedHalfword;
2220 break;
2221 case Primitive::kPrimChar:
2222 load_type = kLoadUnsignedHalfword;
2223 break;
2224 case Primitive::kPrimInt:
2225 case Primitive::kPrimFloat:
2226 load_type = kLoadWord;
2227 break;
2228 case Primitive::kPrimLong:
2229 case Primitive::kPrimDouble:
2230 load_type = kLoadDoubleword;
2231 break;
2232 case Primitive::kPrimNot:
2233 load_type = kLoadUnsignedWord;
2234 break;
2235 case Primitive::kPrimVoid:
2236 LOG(FATAL) << "Unreachable type " << type;
2237 UNREACHABLE();
2238 }
2239 if (!Primitive::IsFloatingPointType(type)) {
2240 DCHECK(locations->Out().IsRegister());
2241 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2242 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2243 } else {
2244 DCHECK(locations->Out().IsFpuRegister());
2245 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2246 __ LoadFpuFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2247 }
2248
2249 codegen_->MaybeRecordImplicitNullCheck(instruction);
2250 // TODO: memory barrier?
2251}
2252
2253void LocationsBuilderMIPS64::HandleFieldSet(HInstruction* instruction,
2254 const FieldInfo& field_info ATTRIBUTE_UNUSED) {
2255 LocationSummary* locations =
2256 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2257 locations->SetInAt(0, Location::RequiresRegister());
2258 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
2259 locations->SetInAt(1, Location::RequiresFpuRegister());
2260 } else {
2261 locations->SetInAt(1, Location::RequiresRegister());
2262 }
2263}
2264
2265void InstructionCodeGeneratorMIPS64::HandleFieldSet(HInstruction* instruction,
2266 const FieldInfo& field_info) {
2267 Primitive::Type type = field_info.GetFieldType();
2268 LocationSummary* locations = instruction->GetLocations();
2269 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2270 StoreOperandType store_type = kStoreByte;
2271 switch (type) {
2272 case Primitive::kPrimBoolean:
2273 case Primitive::kPrimByte:
2274 store_type = kStoreByte;
2275 break;
2276 case Primitive::kPrimShort:
2277 case Primitive::kPrimChar:
2278 store_type = kStoreHalfword;
2279 break;
2280 case Primitive::kPrimInt:
2281 case Primitive::kPrimFloat:
2282 case Primitive::kPrimNot:
2283 store_type = kStoreWord;
2284 break;
2285 case Primitive::kPrimLong:
2286 case Primitive::kPrimDouble:
2287 store_type = kStoreDoubleword;
2288 break;
2289 case Primitive::kPrimVoid:
2290 LOG(FATAL) << "Unreachable type " << type;
2291 UNREACHABLE();
2292 }
2293 if (!Primitive::IsFloatingPointType(type)) {
2294 DCHECK(locations->InAt(1).IsRegister());
2295 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2296 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2297 } else {
2298 DCHECK(locations->InAt(1).IsFpuRegister());
2299 FpuRegister src = locations->InAt(1).AsFpuRegister<FpuRegister>();
2300 __ StoreFpuToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2301 }
2302
2303 codegen_->MaybeRecordImplicitNullCheck(instruction);
2304 // TODO: memory barriers?
2305 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2306 DCHECK(locations->InAt(1).IsRegister());
2307 GpuRegister src = locations->InAt(1).AsRegister<GpuRegister>();
2308 codegen_->MarkGCCard(obj, src);
2309 }
2310}
2311
2312void LocationsBuilderMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2313 HandleFieldGet(instruction, instruction->GetFieldInfo());
2314}
2315
2316void InstructionCodeGeneratorMIPS64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2317 HandleFieldGet(instruction, instruction->GetFieldInfo());
2318}
2319
2320void LocationsBuilderMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2321 HandleFieldSet(instruction, instruction->GetFieldInfo());
2322}
2323
2324void InstructionCodeGeneratorMIPS64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2325 HandleFieldSet(instruction, instruction->GetFieldInfo());
2326}
2327
2328void LocationsBuilderMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2329 LocationSummary::CallKind call_kind =
2330 instruction->IsClassFinal() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2332 locations->SetInAt(0, Location::RequiresRegister());
2333 locations->SetInAt(1, Location::RequiresRegister());
2334 // The output does overlap inputs.
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002335 // Note that TypeCheckSlowPathMIPS64 uses this register too.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002336 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2337}
2338
2339void InstructionCodeGeneratorMIPS64::VisitInstanceOf(HInstanceOf* instruction) {
2340 LocationSummary* locations = instruction->GetLocations();
2341 GpuRegister obj = locations->InAt(0).AsRegister<GpuRegister>();
2342 GpuRegister cls = locations->InAt(1).AsRegister<GpuRegister>();
2343 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2344
2345 Label done;
2346
2347 // Return 0 if `obj` is null.
2348 // TODO: Avoid this check if we know `obj` is not null.
2349 __ Move(out, ZERO);
2350 __ Beqzc(obj, &done);
2351
2352 // Compare the class of `obj` with `cls`.
2353 __ LoadFromOffset(kLoadUnsignedWord, out, obj, mirror::Object::ClassOffset().Int32Value());
2354 if (instruction->IsClassFinal()) {
2355 // Classes must be equal for the instanceof to succeed.
2356 __ Xor(out, out, cls);
2357 __ Sltiu(out, out, 1);
2358 } else {
2359 // If the classes are not equal, we go into a slow path.
2360 DCHECK(locations->OnlyCallsOnSlowPath());
2361 SlowPathCodeMIPS64* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01002362 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS64(instruction);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002363 codegen_->AddSlowPath(slow_path);
2364 __ Bnec(out, cls, slow_path->GetEntryLabel());
2365 __ LoadConst32(out, 1);
2366 __ Bind(slow_path->GetExitLabel());
2367 }
2368
2369 __ Bind(&done);
2370}
2371
2372void LocationsBuilderMIPS64::VisitIntConstant(HIntConstant* constant) {
2373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2374 locations->SetOut(Location::ConstantLocation(constant));
2375}
2376
2377void InstructionCodeGeneratorMIPS64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2378 // Will be generated at use site.
2379}
2380
2381void LocationsBuilderMIPS64::VisitNullConstant(HNullConstant* constant) {
2382 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2383 locations->SetOut(Location::ConstantLocation(constant));
2384}
2385
2386void InstructionCodeGeneratorMIPS64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2387 // Will be generated at use site.
2388}
2389
Calin Juravle175dc732015-08-25 15:42:32 +01002390void LocationsBuilderMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2391 // The trampoline uses the same calling convention as dex calling conventions,
2392 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2393 // the method_idx.
2394 HandleInvoke(invoke);
2395}
2396
2397void InstructionCodeGeneratorMIPS64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2398 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2399}
2400
Alexey Frunze4dda3372015-06-01 18:31:49 -07002401void LocationsBuilderMIPS64::HandleInvoke(HInvoke* invoke) {
2402 InvokeDexCallingConventionVisitorMIPS64 calling_convention_visitor;
2403 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2404}
2405
2406void LocationsBuilderMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2407 HandleInvoke(invoke);
2408 // The register T0 is required to be used for the hidden argument in
2409 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2410 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2411}
2412
2413void InstructionCodeGeneratorMIPS64::VisitInvokeInterface(HInvokeInterface* invoke) {
2414 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2415 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2416 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2417 invoke->GetImtIndex() % mirror::Class::kImtSize, kMips64PointerSize).Uint32Value();
2418 Location receiver = invoke->GetLocations()->InAt(0);
2419 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2420 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2421
2422 // Set the hidden argument.
2423 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<GpuRegister>(),
2424 invoke->GetDexMethodIndex());
2425
2426 // temp = object->GetClass();
2427 if (receiver.IsStackSlot()) {
2428 __ LoadFromOffset(kLoadUnsignedWord, temp, SP, receiver.GetStackIndex());
2429 __ LoadFromOffset(kLoadUnsignedWord, temp, temp, class_offset);
2430 } else {
2431 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2432 }
2433 codegen_->MaybeRecordImplicitNullCheck(invoke);
2434 // temp = temp->GetImtEntryAt(method_offset);
2435 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2436 // T9 = temp->GetEntryPoint();
2437 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2438 // T9();
2439 __ Jalr(T9);
2440 DCHECK(!codegen_->IsLeafMethod());
2441 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2442}
2443
2444void LocationsBuilderMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2445 // TODO intrinsic function
2446 HandleInvoke(invoke);
2447}
2448
2449void LocationsBuilderMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2450 // When we do not run baseline, explicit clinit checks triggered by static
2451 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2452 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2453
2454 // TODO - intrinsic function
2455 HandleInvoke(invoke);
2456
2457 // While SetupBlockedRegisters() blocks registers S2-S8 due to their
2458 // clobbering somewhere else, reduce further register pressure by avoiding
2459 // allocation of a register for the current method pointer like on x86 baseline.
2460 // TODO: remove this once all the issues with register saving/restoring are
2461 // sorted out.
2462 LocationSummary* locations = invoke->GetLocations();
2463 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
2464 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
2465 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
2466 }
2467}
2468
2469static bool TryGenerateIntrinsicCode(HInvoke* invoke,
2470 CodeGeneratorMIPS64* codegen ATTRIBUTE_UNUSED) {
2471 if (invoke->GetLocations()->Intrinsified()) {
2472 // TODO - intrinsic function
2473 return true;
2474 }
2475 return false;
2476}
2477
2478void CodeGeneratorMIPS64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
2479 // All registers are assumed to be correctly set up per the calling convention.
2480
Vladimir Marko58155012015-08-19 12:49:41 +00002481 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2482 switch (invoke->GetMethodLoadKind()) {
2483 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2484 // temp = thread->string_init_entrypoint
2485 __ LoadFromOffset(kLoadDoubleword,
2486 temp.AsRegister<GpuRegister>(),
2487 TR,
2488 invoke->GetStringInitOffset());
2489 break;
2490 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2491 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2492 break;
2493 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2494 __ LoadConst64(temp.AsRegister<GpuRegister>(), invoke->GetMethodAddress());
2495 break;
2496 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2497 // TODO: Implement this type. (Needs literal support.) At the moment, the
2498 // CompilerDriver will not direct the backend to use this type for MIPS.
2499 LOG(FATAL) << "Unsupported!";
2500 UNREACHABLE();
2501 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2502 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
2503 FALLTHROUGH_INTENDED;
2504 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2505 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2506 GpuRegister reg = temp.AsRegister<GpuRegister>();
2507 GpuRegister method_reg;
2508 if (current_method.IsRegister()) {
2509 method_reg = current_method.AsRegister<GpuRegister>();
2510 } else {
2511 // TODO: use the appropriate DCHECK() here if possible.
2512 // DCHECK(invoke->GetLocations()->Intrinsified());
2513 DCHECK(!current_method.IsValid());
2514 method_reg = reg;
2515 __ Ld(reg, SP, kCurrentMethodStackOffset);
2516 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002517
Vladimir Marko58155012015-08-19 12:49:41 +00002518 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01002519 __ LoadFromOffset(kLoadDoubleword,
Vladimir Marko58155012015-08-19 12:49:41 +00002520 reg,
2521 method_reg,
Vladimir Marko05792b92015-08-03 11:56:49 +01002522 ArtMethod::DexCacheResolvedMethodsOffset(kMips64PointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00002523 // temp = temp[index_in_cache]
2524 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
2525 __ LoadFromOffset(kLoadDoubleword,
2526 reg,
2527 reg,
2528 CodeGenerator::GetCachePointerOffset(index_in_cache));
2529 break;
Alexey Frunze4dda3372015-06-01 18:31:49 -07002530 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002531 }
2532
Vladimir Marko58155012015-08-19 12:49:41 +00002533 switch (invoke->GetCodePtrLocation()) {
2534 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
2535 __ Jalr(&frame_entry_label_, T9);
2536 break;
2537 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
2538 // LR = invoke->GetDirectCodePtr();
2539 __ LoadConst64(T9, invoke->GetDirectCodePtr());
2540 // LR()
2541 __ Jalr(T9);
2542 break;
2543 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
2544 // TODO: Implement kCallPCRelative. For the moment, we fall back to kMethodCode.
2545 FALLTHROUGH_INTENDED;
2546 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
2547 // TODO: Implement kDirectCodeFixup. For the moment, we fall back to kMethodCode.
2548 FALLTHROUGH_INTENDED;
2549 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
2550 // T9 = callee_method->entry_point_from_quick_compiled_code_;
2551 __ LoadFromOffset(kLoadDoubleword,
2552 T9,
2553 callee_method.AsRegister<GpuRegister>(),
2554 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
2555 kMips64WordSize).Int32Value());
2556 // T9()
2557 __ Jalr(T9);
2558 break;
2559 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07002560 DCHECK(!IsLeafMethod());
2561}
2562
2563void InstructionCodeGeneratorMIPS64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2564 // When we do not run baseline, explicit clinit checks triggered by static
2565 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2566 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2567
2568 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2569 return;
2570 }
2571
2572 LocationSummary* locations = invoke->GetLocations();
2573 codegen_->GenerateStaticOrDirectCall(invoke,
2574 locations->HasTemps()
2575 ? locations->GetTemp(0)
2576 : Location::NoLocation());
2577 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2578}
2579
2580void InstructionCodeGeneratorMIPS64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2581 // TODO: Try to generate intrinsics code.
2582 LocationSummary* locations = invoke->GetLocations();
2583 Location receiver = locations->InAt(0);
2584 GpuRegister temp = invoke->GetLocations()->GetTemp(0).AsRegister<GpuRegister>();
2585 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
2586 invoke->GetVTableIndex(), kMips64PointerSize).SizeValue();
2587 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2588 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMips64WordSize);
2589
2590 // temp = object->GetClass();
2591 DCHECK(receiver.IsRegister());
2592 __ LoadFromOffset(kLoadUnsignedWord, temp, receiver.AsRegister<GpuRegister>(), class_offset);
2593 codegen_->MaybeRecordImplicitNullCheck(invoke);
2594 // temp = temp->GetMethodAt(method_offset);
2595 __ LoadFromOffset(kLoadDoubleword, temp, temp, method_offset);
2596 // T9 = temp->GetEntryPoint();
2597 __ LoadFromOffset(kLoadDoubleword, T9, temp, entry_point.Int32Value());
2598 // T9();
2599 __ Jalr(T9);
2600 DCHECK(!codegen_->IsLeafMethod());
2601 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2602}
2603
2604void LocationsBuilderMIPS64::VisitLoadClass(HLoadClass* cls) {
2605 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
2606 : LocationSummary::kNoCall;
2607 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
2608 locations->SetInAt(0, Location::RequiresRegister());
2609 locations->SetOut(Location::RequiresRegister());
2610}
2611
2612void InstructionCodeGeneratorMIPS64::VisitLoadClass(HLoadClass* cls) {
2613 LocationSummary* locations = cls->GetLocations();
2614 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2615 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2616 if (cls->IsReferrersClass()) {
2617 DCHECK(!cls->CanCallRuntime());
2618 DCHECK(!cls->MustGenerateClinitCheck());
2619 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2620 ArtMethod::DeclaringClassOffset().Int32Value());
2621 } else {
2622 DCHECK(cls->CanCallRuntime());
Vladimir Marko05792b92015-08-03 11:56:49 +01002623 __ LoadFromOffset(kLoadDoubleword, out, current_method,
2624 ArtMethod::DexCacheResolvedTypesOffset(kMips64PointerSize).Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002625 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002626 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002627 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS64(
2628 cls,
2629 cls,
2630 cls->GetDexPc(),
2631 cls->MustGenerateClinitCheck());
2632 codegen_->AddSlowPath(slow_path);
2633 __ Beqzc(out, slow_path->GetEntryLabel());
2634 if (cls->MustGenerateClinitCheck()) {
2635 GenerateClassInitializationCheck(slow_path, out);
2636 } else {
2637 __ Bind(slow_path->GetExitLabel());
2638 }
2639 }
2640}
2641
David Brazdilcb1c0552015-08-04 16:22:25 +01002642static int32_t GetExceptionTlsOffset() {
2643 return Thread::ExceptionOffset<kMips64WordSize>().Int32Value();
2644}
2645
Alexey Frunze4dda3372015-06-01 18:31:49 -07002646void LocationsBuilderMIPS64::VisitLoadException(HLoadException* load) {
2647 LocationSummary* locations =
2648 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
2649 locations->SetOut(Location::RequiresRegister());
2650}
2651
2652void InstructionCodeGeneratorMIPS64::VisitLoadException(HLoadException* load) {
2653 GpuRegister out = load->GetLocations()->Out().AsRegister<GpuRegister>();
David Brazdilcb1c0552015-08-04 16:22:25 +01002654 __ LoadFromOffset(kLoadUnsignedWord, out, TR, GetExceptionTlsOffset());
2655}
2656
2657void LocationsBuilderMIPS64::VisitClearException(HClearException* clear) {
2658 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
2659}
2660
2661void InstructionCodeGeneratorMIPS64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
2662 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002663}
2664
2665void LocationsBuilderMIPS64::VisitLoadLocal(HLoadLocal* load) {
2666 load->SetLocations(nullptr);
2667}
2668
2669void InstructionCodeGeneratorMIPS64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
2670 // Nothing to do, this is driven by the code generator.
2671}
2672
2673void LocationsBuilderMIPS64::VisitLoadString(HLoadString* load) {
2674 LocationSummary* locations =
2675 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
2676 locations->SetInAt(0, Location::RequiresRegister());
2677 locations->SetOut(Location::RequiresRegister());
2678}
2679
2680void InstructionCodeGeneratorMIPS64::VisitLoadString(HLoadString* load) {
2681 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS64(load);
2682 codegen_->AddSlowPath(slow_path);
2683
2684 LocationSummary* locations = load->GetLocations();
2685 GpuRegister out = locations->Out().AsRegister<GpuRegister>();
2686 GpuRegister current_method = locations->InAt(0).AsRegister<GpuRegister>();
2687 __ LoadFromOffset(kLoadUnsignedWord, out, current_method,
2688 ArtMethod::DeclaringClassOffset().Int32Value());
Vladimir Marko05792b92015-08-03 11:56:49 +01002689 __ LoadFromOffset(kLoadDoubleword, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
Alexey Frunze4dda3372015-06-01 18:31:49 -07002690 __ LoadFromOffset(kLoadUnsignedWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Vladimir Marko05792b92015-08-03 11:56:49 +01002691 // TODO: We will need a read barrier here.
Alexey Frunze4dda3372015-06-01 18:31:49 -07002692 __ Beqzc(out, slow_path->GetEntryLabel());
2693 __ Bind(slow_path->GetExitLabel());
2694}
2695
2696void LocationsBuilderMIPS64::VisitLocal(HLocal* local) {
2697 local->SetLocations(nullptr);
2698}
2699
2700void InstructionCodeGeneratorMIPS64::VisitLocal(HLocal* local) {
2701 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
2702}
2703
2704void LocationsBuilderMIPS64::VisitLongConstant(HLongConstant* constant) {
2705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2706 locations->SetOut(Location::ConstantLocation(constant));
2707}
2708
2709void InstructionCodeGeneratorMIPS64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
2710 // Will be generated at use site.
2711}
2712
2713void LocationsBuilderMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2714 LocationSummary* locations =
2715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2716 InvokeRuntimeCallingConvention calling_convention;
2717 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2718}
2719
2720void InstructionCodeGeneratorMIPS64::VisitMonitorOperation(HMonitorOperation* instruction) {
2721 codegen_->InvokeRuntime(instruction->IsEnter()
2722 ? QUICK_ENTRY_POINT(pLockObject)
2723 : QUICK_ENTRY_POINT(pUnlockObject),
2724 instruction,
2725 instruction->GetDexPc(),
2726 nullptr);
2727 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
2728}
2729
2730void LocationsBuilderMIPS64::VisitMul(HMul* mul) {
2731 LocationSummary* locations =
2732 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2733 switch (mul->GetResultType()) {
2734 case Primitive::kPrimInt:
2735 case Primitive::kPrimLong:
2736 locations->SetInAt(0, Location::RequiresRegister());
2737 locations->SetInAt(1, Location::RequiresRegister());
2738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2739 break;
2740
2741 case Primitive::kPrimFloat:
2742 case Primitive::kPrimDouble:
2743 locations->SetInAt(0, Location::RequiresFpuRegister());
2744 locations->SetInAt(1, Location::RequiresFpuRegister());
2745 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2746 break;
2747
2748 default:
2749 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
2750 }
2751}
2752
2753void InstructionCodeGeneratorMIPS64::VisitMul(HMul* instruction) {
2754 Primitive::Type type = instruction->GetType();
2755 LocationSummary* locations = instruction->GetLocations();
2756
2757 switch (type) {
2758 case Primitive::kPrimInt:
2759 case Primitive::kPrimLong: {
2760 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2761 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
2762 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
2763 if (type == Primitive::kPrimInt)
2764 __ MulR6(dst, lhs, rhs);
2765 else
2766 __ Dmul(dst, lhs, rhs);
2767 break;
2768 }
2769 case Primitive::kPrimFloat:
2770 case Primitive::kPrimDouble: {
2771 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2772 FpuRegister lhs = locations->InAt(0).AsFpuRegister<FpuRegister>();
2773 FpuRegister rhs = locations->InAt(1).AsFpuRegister<FpuRegister>();
2774 if (type == Primitive::kPrimFloat)
2775 __ MulS(dst, lhs, rhs);
2776 else
2777 __ MulD(dst, lhs, rhs);
2778 break;
2779 }
2780 default:
2781 LOG(FATAL) << "Unexpected mul type " << type;
2782 }
2783}
2784
2785void LocationsBuilderMIPS64::VisitNeg(HNeg* neg) {
2786 LocationSummary* locations =
2787 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2788 switch (neg->GetResultType()) {
2789 case Primitive::kPrimInt:
2790 case Primitive::kPrimLong:
2791 locations->SetInAt(0, Location::RequiresRegister());
2792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2793 break;
2794
2795 case Primitive::kPrimFloat:
2796 case Primitive::kPrimDouble:
2797 locations->SetInAt(0, Location::RequiresFpuRegister());
2798 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2799 break;
2800
2801 default:
2802 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2803 }
2804}
2805
2806void InstructionCodeGeneratorMIPS64::VisitNeg(HNeg* instruction) {
2807 Primitive::Type type = instruction->GetType();
2808 LocationSummary* locations = instruction->GetLocations();
2809
2810 switch (type) {
2811 case Primitive::kPrimInt:
2812 case Primitive::kPrimLong: {
2813 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2814 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2815 if (type == Primitive::kPrimInt)
2816 __ Subu(dst, ZERO, src);
2817 else
2818 __ Dsubu(dst, ZERO, src);
2819 break;
2820 }
2821 case Primitive::kPrimFloat:
2822 case Primitive::kPrimDouble: {
2823 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
2824 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
2825 if (type == Primitive::kPrimFloat)
2826 __ NegS(dst, src);
2827 else
2828 __ NegD(dst, src);
2829 break;
2830 }
2831 default:
2832 LOG(FATAL) << "Unexpected neg type " << type;
2833 }
2834}
2835
2836void LocationsBuilderMIPS64::VisitNewArray(HNewArray* instruction) {
2837 LocationSummary* locations =
2838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2839 InvokeRuntimeCallingConvention calling_convention;
2840 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2841 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2842 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2843 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2844}
2845
2846void InstructionCodeGeneratorMIPS64::VisitNewArray(HNewArray* instruction) {
2847 LocationSummary* locations = instruction->GetLocations();
2848 // Move an uint16_t value to a register.
2849 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002850 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2851 instruction,
2852 instruction->GetDexPc(),
2853 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002854 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
2855}
2856
2857void LocationsBuilderMIPS64::VisitNewInstance(HNewInstance* instruction) {
2858 LocationSummary* locations =
2859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
2860 InvokeRuntimeCallingConvention calling_convention;
2861 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2862 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2863 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
2864}
2865
2866void InstructionCodeGeneratorMIPS64::VisitNewInstance(HNewInstance* instruction) {
2867 LocationSummary* locations = instruction->GetLocations();
2868 // Move an uint16_t value to a register.
2869 __ LoadConst32(locations->GetTemp(0).AsRegister<GpuRegister>(), instruction->GetTypeIndex());
Calin Juravle175dc732015-08-25 15:42:32 +01002870 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
2871 instruction,
2872 instruction->GetDexPc(),
2873 nullptr);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002874 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
2875}
2876
2877void LocationsBuilderMIPS64::VisitNot(HNot* instruction) {
2878 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2881}
2882
2883void InstructionCodeGeneratorMIPS64::VisitNot(HNot* instruction) {
2884 Primitive::Type type = instruction->GetType();
2885 LocationSummary* locations = instruction->GetLocations();
2886
2887 switch (type) {
2888 case Primitive::kPrimInt:
2889 case Primitive::kPrimLong: {
2890 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
2891 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
2892 __ Nor(dst, src, ZERO);
2893 break;
2894 }
2895
2896 default:
2897 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
2898 }
2899}
2900
2901void LocationsBuilderMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
2902 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2903 locations->SetInAt(0, Location::RequiresRegister());
2904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2905}
2906
2907void InstructionCodeGeneratorMIPS64::VisitBooleanNot(HBooleanNot* instruction) {
2908 LocationSummary* locations = instruction->GetLocations();
2909 __ Xori(locations->Out().AsRegister<GpuRegister>(),
2910 locations->InAt(0).AsRegister<GpuRegister>(),
2911 1);
2912}
2913
2914void LocationsBuilderMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002915 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2916 ? LocationSummary::kCallOnSlowPath
2917 : LocationSummary::kNoCall;
2918 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunze4dda3372015-06-01 18:31:49 -07002919 locations->SetInAt(0, Location::RequiresRegister());
2920 if (instruction->HasUses()) {
2921 locations->SetOut(Location::SameAsFirstInput());
2922 }
2923}
2924
2925void InstructionCodeGeneratorMIPS64::GenerateImplicitNullCheck(HNullCheck* instruction) {
2926 if (codegen_->CanMoveNullCheckToUser(instruction)) {
2927 return;
2928 }
2929 Location obj = instruction->GetLocations()->InAt(0);
2930
2931 __ Lw(ZERO, obj.AsRegister<GpuRegister>(), 0);
2932 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2933}
2934
2935void InstructionCodeGeneratorMIPS64::GenerateExplicitNullCheck(HNullCheck* instruction) {
2936 SlowPathCodeMIPS64* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS64(instruction);
2937 codegen_->AddSlowPath(slow_path);
2938
2939 Location obj = instruction->GetLocations()->InAt(0);
2940
2941 __ Beqzc(obj.AsRegister<GpuRegister>(), slow_path->GetEntryLabel());
2942}
2943
2944void InstructionCodeGeneratorMIPS64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00002945 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Alexey Frunze4dda3372015-06-01 18:31:49 -07002946 GenerateImplicitNullCheck(instruction);
2947 } else {
2948 GenerateExplicitNullCheck(instruction);
2949 }
2950}
2951
2952void LocationsBuilderMIPS64::VisitOr(HOr* instruction) {
2953 HandleBinaryOp(instruction);
2954}
2955
2956void InstructionCodeGeneratorMIPS64::VisitOr(HOr* instruction) {
2957 HandleBinaryOp(instruction);
2958}
2959
2960void LocationsBuilderMIPS64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
2961 LOG(FATAL) << "Unreachable";
2962}
2963
2964void InstructionCodeGeneratorMIPS64::VisitParallelMove(HParallelMove* instruction) {
2965 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
2966}
2967
2968void LocationsBuilderMIPS64::VisitParameterValue(HParameterValue* instruction) {
2969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2970 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
2971 if (location.IsStackSlot()) {
2972 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2973 } else if (location.IsDoubleStackSlot()) {
2974 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
2975 }
2976 locations->SetOut(location);
2977}
2978
2979void InstructionCodeGeneratorMIPS64::VisitParameterValue(HParameterValue* instruction
2980 ATTRIBUTE_UNUSED) {
2981 // Nothing to do, the parameter is already at its location.
2982}
2983
2984void LocationsBuilderMIPS64::VisitCurrentMethod(HCurrentMethod* instruction) {
2985 LocationSummary* locations =
2986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
2987 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
2988}
2989
2990void InstructionCodeGeneratorMIPS64::VisitCurrentMethod(HCurrentMethod* instruction
2991 ATTRIBUTE_UNUSED) {
2992 // Nothing to do, the method is already at its location.
2993}
2994
2995void LocationsBuilderMIPS64::VisitPhi(HPhi* instruction) {
2996 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2997 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
2998 locations->SetInAt(i, Location::Any());
2999 }
3000 locations->SetOut(Location::Any());
3001}
3002
3003void InstructionCodeGeneratorMIPS64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3004 LOG(FATAL) << "Unreachable";
3005}
3006
3007void LocationsBuilderMIPS64::VisitRem(HRem* rem) {
3008 Primitive::Type type = rem->GetResultType();
3009 LocationSummary::CallKind call_kind =
3010 Primitive::IsFloatingPointType(type) ? LocationSummary::kCall : LocationSummary::kNoCall;
3011 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3012
3013 switch (type) {
3014 case Primitive::kPrimInt:
3015 case Primitive::kPrimLong:
3016 locations->SetInAt(0, Location::RequiresRegister());
3017 locations->SetInAt(1, Location::RequiresRegister());
3018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3019 break;
3020
3021 case Primitive::kPrimFloat:
3022 case Primitive::kPrimDouble: {
3023 InvokeRuntimeCallingConvention calling_convention;
3024 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3025 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3026 locations->SetOut(calling_convention.GetReturnLocation(type));
3027 break;
3028 }
3029
3030 default:
3031 LOG(FATAL) << "Unexpected rem type " << type;
3032 }
3033}
3034
3035void InstructionCodeGeneratorMIPS64::VisitRem(HRem* instruction) {
3036 Primitive::Type type = instruction->GetType();
3037 LocationSummary* locations = instruction->GetLocations();
3038
3039 switch (type) {
3040 case Primitive::kPrimInt:
3041 case Primitive::kPrimLong: {
3042 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3043 GpuRegister lhs = locations->InAt(0).AsRegister<GpuRegister>();
3044 GpuRegister rhs = locations->InAt(1).AsRegister<GpuRegister>();
3045 if (type == Primitive::kPrimInt)
3046 __ ModR6(dst, lhs, rhs);
3047 else
3048 __ Dmod(dst, lhs, rhs);
3049 break;
3050 }
3051
3052 case Primitive::kPrimFloat:
3053 case Primitive::kPrimDouble: {
3054 int32_t entry_offset = (type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pFmodf)
3055 : QUICK_ENTRY_POINT(pFmod);
3056 codegen_->InvokeRuntime(entry_offset, instruction, instruction->GetDexPc(), nullptr);
3057 break;
3058 }
3059 default:
3060 LOG(FATAL) << "Unexpected rem type " << type;
3061 }
3062}
3063
3064void LocationsBuilderMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3065 memory_barrier->SetLocations(nullptr);
3066}
3067
3068void InstructionCodeGeneratorMIPS64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3069 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3070}
3071
3072void LocationsBuilderMIPS64::VisitReturn(HReturn* ret) {
3073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3074 Primitive::Type return_type = ret->InputAt(0)->GetType();
3075 locations->SetInAt(0, Mips64ReturnLocation(return_type));
3076}
3077
3078void InstructionCodeGeneratorMIPS64::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3079 codegen_->GenerateFrameExit();
3080}
3081
3082void LocationsBuilderMIPS64::VisitReturnVoid(HReturnVoid* ret) {
3083 ret->SetLocations(nullptr);
3084}
3085
3086void InstructionCodeGeneratorMIPS64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3087 codegen_->GenerateFrameExit();
3088}
3089
3090void LocationsBuilderMIPS64::VisitShl(HShl* shl) {
3091 HandleShift(shl);
3092}
3093
3094void InstructionCodeGeneratorMIPS64::VisitShl(HShl* shl) {
3095 HandleShift(shl);
3096}
3097
3098void LocationsBuilderMIPS64::VisitShr(HShr* shr) {
3099 HandleShift(shr);
3100}
3101
3102void InstructionCodeGeneratorMIPS64::VisitShr(HShr* shr) {
3103 HandleShift(shr);
3104}
3105
3106void LocationsBuilderMIPS64::VisitStoreLocal(HStoreLocal* store) {
3107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3108 Primitive::Type field_type = store->InputAt(1)->GetType();
3109 switch (field_type) {
3110 case Primitive::kPrimNot:
3111 case Primitive::kPrimBoolean:
3112 case Primitive::kPrimByte:
3113 case Primitive::kPrimChar:
3114 case Primitive::kPrimShort:
3115 case Primitive::kPrimInt:
3116 case Primitive::kPrimFloat:
3117 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3118 break;
3119
3120 case Primitive::kPrimLong:
3121 case Primitive::kPrimDouble:
3122 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3123 break;
3124
3125 default:
3126 LOG(FATAL) << "Unimplemented local type " << field_type;
3127 }
3128}
3129
3130void InstructionCodeGeneratorMIPS64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3131}
3132
3133void LocationsBuilderMIPS64::VisitSub(HSub* instruction) {
3134 HandleBinaryOp(instruction);
3135}
3136
3137void InstructionCodeGeneratorMIPS64::VisitSub(HSub* instruction) {
3138 HandleBinaryOp(instruction);
3139}
3140
3141void LocationsBuilderMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3142 HandleFieldGet(instruction, instruction->GetFieldInfo());
3143}
3144
3145void InstructionCodeGeneratorMIPS64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3146 HandleFieldGet(instruction, instruction->GetFieldInfo());
3147}
3148
3149void LocationsBuilderMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3150 HandleFieldSet(instruction, instruction->GetFieldInfo());
3151}
3152
3153void InstructionCodeGeneratorMIPS64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3154 HandleFieldSet(instruction, instruction->GetFieldInfo());
3155}
3156
Calin Juravle23a8e352015-09-08 19:56:31 +01003157void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldGet(
3158 HUnresolvedInstanceFieldGet* instruction) {
3159 FieldAccessCallingConvetionMIPS64 calling_convention;
3160 codegen_->CreateUnresolvedFieldLocationSummary(
3161 instruction, instruction->GetFieldType(), calling_convention);
3162}
3163
3164void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldGet(
3165 HUnresolvedInstanceFieldGet* instruction) {
3166 codegen_->GenerateUnresolvedFieldAccess(instruction,
3167 instruction->GetFieldType(),
3168 instruction->GetFieldIndex(),
3169 instruction->GetDexPc());
3170}
3171
3172void LocationsBuilderMIPS64::VisitUnresolvedInstanceFieldSet(
3173 HUnresolvedInstanceFieldSet* instruction) {
3174 FieldAccessCallingConvetionMIPS64 calling_convention;
3175 codegen_->CreateUnresolvedFieldLocationSummary(
3176 instruction, instruction->GetFieldType(), calling_convention);
3177}
3178
3179void InstructionCodeGeneratorMIPS64::VisitUnresolvedInstanceFieldSet(
3180 HUnresolvedInstanceFieldSet* instruction) {
3181 codegen_->GenerateUnresolvedFieldAccess(instruction,
3182 instruction->GetFieldType(),
3183 instruction->GetFieldIndex(),
3184 instruction->GetDexPc());
3185}
3186
3187void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldGet(
3188 HUnresolvedStaticFieldGet* instruction) {
3189 FieldAccessCallingConvetionMIPS64 calling_convention;
3190 codegen_->CreateUnresolvedFieldLocationSummary(
3191 instruction, instruction->GetFieldType(), calling_convention);
3192}
3193
3194void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldGet(
3195 HUnresolvedStaticFieldGet* instruction) {
3196 codegen_->GenerateUnresolvedFieldAccess(instruction,
3197 instruction->GetFieldType(),
3198 instruction->GetFieldIndex(),
3199 instruction->GetDexPc());
3200}
3201
3202void LocationsBuilderMIPS64::VisitUnresolvedStaticFieldSet(
3203 HUnresolvedStaticFieldSet* instruction) {
3204 FieldAccessCallingConvetionMIPS64 calling_convention;
3205 codegen_->CreateUnresolvedFieldLocationSummary(
3206 instruction, instruction->GetFieldType(), calling_convention);
3207}
3208
3209void InstructionCodeGeneratorMIPS64::VisitUnresolvedStaticFieldSet(
3210 HUnresolvedStaticFieldSet* instruction) {
3211 codegen_->GenerateUnresolvedFieldAccess(instruction,
3212 instruction->GetFieldType(),
3213 instruction->GetFieldIndex(),
3214 instruction->GetDexPc());
3215}
3216
Alexey Frunze4dda3372015-06-01 18:31:49 -07003217void LocationsBuilderMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3218 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3219}
3220
3221void InstructionCodeGeneratorMIPS64::VisitSuspendCheck(HSuspendCheck* instruction) {
3222 HBasicBlock* block = instruction->GetBlock();
3223 if (block->GetLoopInformation() != nullptr) {
3224 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3225 // The back edge will generate the suspend check.
3226 return;
3227 }
3228 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3229 // The goto will generate the suspend check.
3230 return;
3231 }
3232 GenerateSuspendCheck(instruction, nullptr);
3233}
3234
3235void LocationsBuilderMIPS64::VisitTemporary(HTemporary* temp) {
3236 temp->SetLocations(nullptr);
3237}
3238
3239void InstructionCodeGeneratorMIPS64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3240 // Nothing to do, this is driven by the code generator.
3241}
3242
3243void LocationsBuilderMIPS64::VisitThrow(HThrow* instruction) {
3244 LocationSummary* locations =
3245 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3246 InvokeRuntimeCallingConvention calling_convention;
3247 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3248}
3249
3250void InstructionCodeGeneratorMIPS64::VisitThrow(HThrow* instruction) {
3251 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3252 instruction,
3253 instruction->GetDexPc(),
3254 nullptr);
3255 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3256}
3257
3258void LocationsBuilderMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3259 Primitive::Type input_type = conversion->GetInputType();
3260 Primitive::Type result_type = conversion->GetResultType();
3261 DCHECK_NE(input_type, result_type);
3262
3263 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3264 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3265 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3266 }
3267
3268 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3269 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3270 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3271 call_kind = LocationSummary::kCall;
3272 }
3273
3274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3275
3276 if (call_kind == LocationSummary::kNoCall) {
3277 if (Primitive::IsFloatingPointType(input_type)) {
3278 locations->SetInAt(0, Location::RequiresFpuRegister());
3279 } else {
3280 locations->SetInAt(0, Location::RequiresRegister());
3281 }
3282
3283 if (Primitive::IsFloatingPointType(result_type)) {
3284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3285 } else {
3286 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3287 }
3288 } else {
3289 InvokeRuntimeCallingConvention calling_convention;
3290
3291 if (Primitive::IsFloatingPointType(input_type)) {
3292 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3293 } else {
3294 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3295 }
3296
3297 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3298 }
3299}
3300
3301void InstructionCodeGeneratorMIPS64::VisitTypeConversion(HTypeConversion* conversion) {
3302 LocationSummary* locations = conversion->GetLocations();
3303 Primitive::Type result_type = conversion->GetResultType();
3304 Primitive::Type input_type = conversion->GetInputType();
3305
3306 DCHECK_NE(input_type, result_type);
3307
3308 if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3309 GpuRegister dst = locations->Out().AsRegister<GpuRegister>();
3310 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3311
3312 switch (result_type) {
3313 case Primitive::kPrimChar:
3314 __ Andi(dst, src, 0xFFFF);
3315 break;
3316 case Primitive::kPrimByte:
3317 // long is never converted into types narrower than int directly,
3318 // so SEB and SEH can be used without ever causing unpredictable results
3319 // on 64-bit inputs
3320 DCHECK(input_type != Primitive::kPrimLong);
3321 __ Seb(dst, src);
3322 break;
3323 case Primitive::kPrimShort:
3324 // long is never converted into types narrower than int directly,
3325 // so SEB and SEH can be used without ever causing unpredictable results
3326 // on 64-bit inputs
3327 DCHECK(input_type != Primitive::kPrimLong);
3328 __ Seh(dst, src);
3329 break;
3330 case Primitive::kPrimInt:
3331 case Primitive::kPrimLong:
3332 // Sign-extend 32-bit int into bits 32 through 63 for
3333 // int-to-long and long-to-int conversions
3334 __ Sll(dst, src, 0);
3335 break;
3336
3337 default:
3338 LOG(FATAL) << "Unexpected type conversion from " << input_type
3339 << " to " << result_type;
3340 }
3341 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3342 if (input_type != Primitive::kPrimLong) {
3343 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3344 GpuRegister src = locations->InAt(0).AsRegister<GpuRegister>();
3345 __ Mtc1(src, FTMP);
3346 if (result_type == Primitive::kPrimFloat) {
3347 __ Cvtsw(dst, FTMP);
3348 } else {
3349 __ Cvtdw(dst, FTMP);
3350 }
3351 } else {
3352 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
3353 : QUICK_ENTRY_POINT(pL2d);
3354 codegen_->InvokeRuntime(entry_offset,
3355 conversion,
3356 conversion->GetDexPc(),
3357 nullptr);
3358 }
3359 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3360 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3361 int32_t entry_offset;
3362 if (result_type != Primitive::kPrimLong) {
3363 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
3364 : QUICK_ENTRY_POINT(pD2iz);
3365 } else {
3366 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
3367 : QUICK_ENTRY_POINT(pD2l);
3368 }
3369 codegen_->InvokeRuntime(entry_offset,
3370 conversion,
3371 conversion->GetDexPc(),
3372 nullptr);
3373 } else if (Primitive::IsFloatingPointType(result_type) &&
3374 Primitive::IsFloatingPointType(input_type)) {
3375 FpuRegister dst = locations->Out().AsFpuRegister<FpuRegister>();
3376 FpuRegister src = locations->InAt(0).AsFpuRegister<FpuRegister>();
3377 if (result_type == Primitive::kPrimFloat) {
3378 __ Cvtsd(dst, src);
3379 } else {
3380 __ Cvtds(dst, src);
3381 }
3382 } else {
3383 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
3384 << " to " << result_type;
3385 }
3386}
3387
3388void LocationsBuilderMIPS64::VisitUShr(HUShr* ushr) {
3389 HandleShift(ushr);
3390}
3391
3392void InstructionCodeGeneratorMIPS64::VisitUShr(HUShr* ushr) {
3393 HandleShift(ushr);
3394}
3395
3396void LocationsBuilderMIPS64::VisitXor(HXor* instruction) {
3397 HandleBinaryOp(instruction);
3398}
3399
3400void InstructionCodeGeneratorMIPS64::VisitXor(HXor* instruction) {
3401 HandleBinaryOp(instruction);
3402}
3403
3404void LocationsBuilderMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3405 // Nothing to do, this should be removed during prepare for register allocator.
3406 LOG(FATAL) << "Unreachable";
3407}
3408
3409void InstructionCodeGeneratorMIPS64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
3410 // Nothing to do, this should be removed during prepare for register allocator.
3411 LOG(FATAL) << "Unreachable";
3412}
3413
3414void LocationsBuilderMIPS64::VisitEqual(HEqual* comp) {
3415 VisitCondition(comp);
3416}
3417
3418void InstructionCodeGeneratorMIPS64::VisitEqual(HEqual* comp) {
3419 VisitCondition(comp);
3420}
3421
3422void LocationsBuilderMIPS64::VisitNotEqual(HNotEqual* comp) {
3423 VisitCondition(comp);
3424}
3425
3426void InstructionCodeGeneratorMIPS64::VisitNotEqual(HNotEqual* comp) {
3427 VisitCondition(comp);
3428}
3429
3430void LocationsBuilderMIPS64::VisitLessThan(HLessThan* comp) {
3431 VisitCondition(comp);
3432}
3433
3434void InstructionCodeGeneratorMIPS64::VisitLessThan(HLessThan* comp) {
3435 VisitCondition(comp);
3436}
3437
3438void LocationsBuilderMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3439 VisitCondition(comp);
3440}
3441
3442void InstructionCodeGeneratorMIPS64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
3443 VisitCondition(comp);
3444}
3445
3446void LocationsBuilderMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3447 VisitCondition(comp);
3448}
3449
3450void InstructionCodeGeneratorMIPS64::VisitGreaterThan(HGreaterThan* comp) {
3451 VisitCondition(comp);
3452}
3453
3454void LocationsBuilderMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3455 VisitCondition(comp);
3456}
3457
3458void InstructionCodeGeneratorMIPS64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
3459 VisitCondition(comp);
3460}
3461
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01003462void LocationsBuilderMIPS64::VisitFakeString(HFakeString* instruction) {
3463 DCHECK(codegen_->IsBaseline());
3464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3466 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
3467}
3468
3469void InstructionCodeGeneratorMIPS64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
3470 DCHECK(codegen_->IsBaseline());
3471 // Will be generated at use site.
3472}
3473
Alexey Frunze4dda3372015-06-01 18:31:49 -07003474} // namespace mips64
3475} // namespace art