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