blob: 4404aa3289752e0a56d87b670c97d49bb3fbfb44 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
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_mips.h"
18
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23#include "entrypoints/quick/quick_entrypoints_enum.h"
24#include "gc/accounting/card_table.h"
25#include "intrinsics.h"
26#include "mirror/array-inl.h"
27#include "mirror/class-inl.h"
28#include "offsets.h"
29#include "thread.h"
30#include "utils/assembler.h"
31#include "utils/mips/assembler_mips.h"
32#include "utils/stack_checks.h"
33
34namespace art {
35namespace mips {
36
37static constexpr int kCurrentMethodStackOffset = 0;
38static constexpr Register kMethodRegisterArgument = A0;
39
40// We need extra temporary/scratch registers (in addition to AT) in some cases.
41static constexpr Register TMP = T8;
42static constexpr FRegister FTMP = F8;
43
44// ART Thread Register.
45static constexpr Register TR = S1;
46
47Location MipsReturnLocation(Primitive::Type return_type) {
48 switch (return_type) {
49 case Primitive::kPrimBoolean:
50 case Primitive::kPrimByte:
51 case Primitive::kPrimChar:
52 case Primitive::kPrimShort:
53 case Primitive::kPrimInt:
54 case Primitive::kPrimNot:
55 return Location::RegisterLocation(V0);
56
57 case Primitive::kPrimLong:
58 return Location::RegisterPairLocation(V0, V1);
59
60 case Primitive::kPrimFloat:
61 case Primitive::kPrimDouble:
62 return Location::FpuRegisterLocation(F0);
63
64 case Primitive::kPrimVoid:
65 return Location();
66 }
67 UNREACHABLE();
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
71 return MipsReturnLocation(type);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
75 return Location::RegisterLocation(kMethodRegisterArgument);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
79 Location next_location;
80
81 switch (type) {
82 case Primitive::kPrimBoolean:
83 case Primitive::kPrimByte:
84 case Primitive::kPrimChar:
85 case Primitive::kPrimShort:
86 case Primitive::kPrimInt:
87 case Primitive::kPrimNot: {
88 uint32_t gp_index = gp_index_++;
89 if (gp_index < calling_convention.GetNumberOfRegisters()) {
90 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
91 } else {
92 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
93 next_location = Location::StackSlot(stack_offset);
94 }
95 break;
96 }
97
98 case Primitive::kPrimLong: {
99 uint32_t gp_index = gp_index_;
100 gp_index_ += 2;
101 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
102 if (calling_convention.GetRegisterAt(gp_index) == A1) {
103 gp_index_++; // Skip A1, and use A2_A3 instead.
104 gp_index++;
105 }
106 Register low_even = calling_convention.GetRegisterAt(gp_index);
107 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
108 DCHECK_EQ(low_even + 1, high_odd);
109 next_location = Location::RegisterPairLocation(low_even, high_odd);
110 } else {
111 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
112 next_location = Location::DoubleStackSlot(stack_offset);
113 }
114 break;
115 }
116
117 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
118 // will take up the even/odd pair, while floats are stored in even regs only.
119 // On 64 bit FPU, both double and float are stored in even registers only.
120 case Primitive::kPrimFloat:
121 case Primitive::kPrimDouble: {
122 uint32_t float_index = float_index_++;
123 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
124 next_location = Location::FpuRegisterLocation(
125 calling_convention.GetFpuRegisterAt(float_index));
126 } else {
127 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
128 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
129 : Location::StackSlot(stack_offset);
130 }
131 break;
132 }
133
134 case Primitive::kPrimVoid:
135 LOG(FATAL) << "Unexpected parameter type " << type;
136 break;
137 }
138
139 // Space on the stack is reserved for all arguments.
140 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
141
142 return next_location;
143}
144
145Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
146 return MipsReturnLocation(type);
147}
148
149#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()->
150#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
151
152class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
153 public:
154 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : instruction_(instruction) {}
155
156 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
157 LocationSummary* locations = instruction_->GetLocations();
158 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
159 __ Bind(GetEntryLabel());
160 if (instruction_->CanThrowIntoCatchBlock()) {
161 // Live registers will be restored in the catch block if caught.
162 SaveLiveRegisters(codegen, instruction_->GetLocations());
163 }
164 // We're moving two locations to locations that could overlap, so we need a parallel
165 // move resolver.
166 InvokeRuntimeCallingConvention calling_convention;
167 codegen->EmitParallelMoves(locations->InAt(0),
168 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
169 Primitive::kPrimInt,
170 locations->InAt(1),
171 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
172 Primitive::kPrimInt);
173 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
174 instruction_,
175 instruction_->GetDexPc(),
176 this,
177 IsDirectEntrypoint(kQuickThrowArrayBounds));
178 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
179 }
180
181 bool IsFatal() const OVERRIDE { return true; }
182
183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
184
185 private:
186 HBoundsCheck* const instruction_;
187
188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
189};
190
191class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
192 public:
193 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : instruction_(instruction) {}
194
195 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
196 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
197 __ Bind(GetEntryLabel());
198 if (instruction_->CanThrowIntoCatchBlock()) {
199 // Live registers will be restored in the catch block if caught.
200 SaveLiveRegisters(codegen, instruction_->GetLocations());
201 }
202 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
203 instruction_,
204 instruction_->GetDexPc(),
205 this,
206 IsDirectEntrypoint(kQuickThrowDivZero));
207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
215 HDivZeroCheck* const instruction_;
216 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
217};
218
219class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
220 public:
221 LoadClassSlowPathMIPS(HLoadClass* cls,
222 HInstruction* at,
223 uint32_t dex_pc,
224 bool do_clinit)
225 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
230 LocationSummary* locations = at_->GetLocations();
231 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
232
233 __ Bind(GetEntryLabel());
234 SaveLiveRegisters(codegen, locations);
235
236 InvokeRuntimeCallingConvention calling_convention;
237 __ LoadConst32(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
238
239 int32_t entry_point_offset = do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
240 : QUICK_ENTRY_POINT(pInitializeType);
241 bool direct = do_clinit_ ? IsDirectEntrypoint(kQuickInitializeStaticStorage)
242 : IsDirectEntrypoint(kQuickInitializeType);
243
244 mips_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this, direct);
245 if (do_clinit_) {
246 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
247 } else {
248 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
249 }
250
251 // Move the class to the desired location.
252 Location out = locations->Out();
253 if (out.IsValid()) {
254 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
255 Primitive::Type type = at_->GetType();
256 mips_codegen->MoveLocation(out, calling_convention.GetReturnLocation(type), type);
257 }
258
259 RestoreLiveRegisters(codegen, locations);
260 __ B(GetExitLabel());
261 }
262
263 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
264
265 private:
266 // The class this slow path will load.
267 HLoadClass* const cls_;
268
269 // The instruction where this slow path is happening.
270 // (Might be the load class or an initialization check).
271 HInstruction* const at_;
272
273 // The dex PC of `at_`.
274 const uint32_t dex_pc_;
275
276 // Whether to initialize the class.
277 const bool do_clinit_;
278
279 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
280};
281
282class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
283 public:
284 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : instruction_(instruction) {}
285
286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
289 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
290
291 __ Bind(GetEntryLabel());
292 SaveLiveRegisters(codegen, locations);
293
294 InvokeRuntimeCallingConvention calling_convention;
295 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex());
296 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
297 instruction_,
298 instruction_->GetDexPc(),
299 this,
300 IsDirectEntrypoint(kQuickResolveString));
301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
302 Primitive::Type type = instruction_->GetType();
303 mips_codegen->MoveLocation(locations->Out(),
304 calling_convention.GetReturnLocation(type),
305 type);
306
307 RestoreLiveRegisters(codegen, locations);
308 __ B(GetExitLabel());
309 }
310
311 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
312
313 private:
314 HLoadString* const instruction_;
315
316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : instruction_(instr) {}
322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
330 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
331 instruction_,
332 instruction_->GetDexPc(),
333 this,
334 IsDirectEntrypoint(kQuickThrowNullPointer));
335 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
336 }
337
338 bool IsFatal() const OVERRIDE { return true; }
339
340 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
341
342 private:
343 HNullCheck* const instruction_;
344
345 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
346};
347
348class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
349 public:
350 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
351 : instruction_(instruction), successor_(successor) {}
352
353 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
354 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
355 __ Bind(GetEntryLabel());
356 SaveLiveRegisters(codegen, instruction_->GetLocations());
357 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
358 instruction_,
359 instruction_->GetDexPc(),
360 this,
361 IsDirectEntrypoint(kQuickTestSuspend));
362 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
363 RestoreLiveRegisters(codegen, instruction_->GetLocations());
364 if (successor_ == nullptr) {
365 __ B(GetReturnLabel());
366 } else {
367 __ B(mips_codegen->GetLabelOf(successor_));
368 }
369 }
370
371 MipsLabel* GetReturnLabel() {
372 DCHECK(successor_ == nullptr);
373 return &return_label_;
374 }
375
376 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
377
378 private:
379 HSuspendCheck* const instruction_;
380 // If not null, the block to branch to after the suspend check.
381 HBasicBlock* const successor_;
382
383 // If `successor_` is null, the label to branch to after the suspend check.
384 MipsLabel return_label_;
385
386 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
387};
388
389class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
390 public:
391 explicit TypeCheckSlowPathMIPS(HInstruction* instruction) : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = instruction_->GetLocations();
395 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) : locations->Out();
396 uint32_t dex_pc = instruction_->GetDexPc();
397 DCHECK(instruction_->IsCheckCast()
398 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
399 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
400
401 __ Bind(GetEntryLabel());
402 SaveLiveRegisters(codegen, locations);
403
404 // We're moving two locations to locations that could overlap, so we need a parallel
405 // move resolver.
406 InvokeRuntimeCallingConvention calling_convention;
407 codegen->EmitParallelMoves(locations->InAt(1),
408 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
409 Primitive::kPrimNot,
410 object_class,
411 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
412 Primitive::kPrimNot);
413
414 if (instruction_->IsInstanceOf()) {
415 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
416 instruction_,
417 dex_pc,
418 this,
419 IsDirectEntrypoint(kQuickInstanceofNonTrivial));
420 Primitive::Type ret_type = instruction_->GetType();
421 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
422 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
423 CheckEntrypointTypes<kQuickInstanceofNonTrivial,
424 uint32_t,
425 const mirror::Class*,
426 const mirror::Class*>();
427 } else {
428 DCHECK(instruction_->IsCheckCast());
429 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
430 instruction_,
431 dex_pc,
432 this,
433 IsDirectEntrypoint(kQuickCheckCast));
434 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
435 }
436
437 RestoreLiveRegisters(codegen, locations);
438 __ B(GetExitLabel());
439 }
440
441 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
442
443 private:
444 HInstruction* const instruction_;
445
446 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
447};
448
449class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
450 public:
451 explicit DeoptimizationSlowPathMIPS(HInstruction* instruction)
452 : instruction_(instruction) {}
453
454 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
455 __ Bind(GetEntryLabel());
456 SaveLiveRegisters(codegen, instruction_->GetLocations());
457 DCHECK(instruction_->IsDeoptimize());
458 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
459 uint32_t dex_pc = deoptimize->GetDexPc();
460 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
461 mips_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
462 instruction_,
463 dex_pc,
464 this,
465 IsDirectEntrypoint(kQuickDeoptimize));
466 }
467
468 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
469
470 private:
471 HInstruction* const instruction_;
472 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
473};
474
475CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
476 const MipsInstructionSetFeatures& isa_features,
477 const CompilerOptions& compiler_options,
478 OptimizingCompilerStats* stats)
479 : CodeGenerator(graph,
480 kNumberOfCoreRegisters,
481 kNumberOfFRegisters,
482 kNumberOfRegisterPairs,
483 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
484 arraysize(kCoreCalleeSaves)),
485 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
486 arraysize(kFpuCalleeSaves)),
487 compiler_options,
488 stats),
489 block_labels_(nullptr),
490 location_builder_(graph, this),
491 instruction_visitor_(graph, this),
492 move_resolver_(graph->GetArena(), this),
493 assembler_(&isa_features),
494 isa_features_(isa_features) {
495 // Save RA (containing the return address) to mimic Quick.
496 AddAllocatedRegister(Location::RegisterLocation(RA));
497}
498
499#undef __
500#define __ down_cast<MipsAssembler*>(GetAssembler())->
501#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsWordSize, x).Int32Value()
502
503void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
504 // Ensure that we fix up branches.
505 __ FinalizeCode();
506
507 // Adjust native pc offsets in stack maps.
508 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
509 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
510 uint32_t new_position = __ GetAdjustedPosition(old_position);
511 DCHECK_GE(new_position, old_position);
512 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
513 }
514
515 // Adjust pc offsets for the disassembly information.
516 if (disasm_info_ != nullptr) {
517 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
518 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
519 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
520 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
521 it.second.start = __ GetAdjustedPosition(it.second.start);
522 it.second.end = __ GetAdjustedPosition(it.second.end);
523 }
524 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
525 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
526 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
527 }
528 }
529
530 CodeGenerator::Finalize(allocator);
531}
532
533MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
534 return codegen_->GetAssembler();
535}
536
537void ParallelMoveResolverMIPS::EmitMove(size_t index) {
538 DCHECK_LT(index, moves_.size());
539 MoveOperands* move = moves_[index];
540 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
541}
542
543void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
544 DCHECK_LT(index, moves_.size());
545 MoveOperands* move = moves_[index];
546 Primitive::Type type = move->GetType();
547 Location loc1 = move->GetDestination();
548 Location loc2 = move->GetSource();
549
550 DCHECK(!loc1.IsConstant());
551 DCHECK(!loc2.IsConstant());
552
553 if (loc1.Equals(loc2)) {
554 return;
555 }
556
557 if (loc1.IsRegister() && loc2.IsRegister()) {
558 // Swap 2 GPRs.
559 Register r1 = loc1.AsRegister<Register>();
560 Register r2 = loc2.AsRegister<Register>();
561 __ Move(TMP, r2);
562 __ Move(r2, r1);
563 __ Move(r1, TMP);
564 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
565 FRegister f1 = loc1.AsFpuRegister<FRegister>();
566 FRegister f2 = loc2.AsFpuRegister<FRegister>();
567 if (type == Primitive::kPrimFloat) {
568 __ MovS(FTMP, f2);
569 __ MovS(f2, f1);
570 __ MovS(f1, FTMP);
571 } else {
572 DCHECK_EQ(type, Primitive::kPrimDouble);
573 __ MovD(FTMP, f2);
574 __ MovD(f2, f1);
575 __ MovD(f1, FTMP);
576 }
577 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
578 (loc1.IsFpuRegister() && loc2.IsRegister())) {
579 // Swap FPR and GPR.
580 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
581 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
582 : loc2.AsFpuRegister<FRegister>();
583 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>()
584 : loc2.AsRegister<Register>();
585 __ Move(TMP, r2);
586 __ Mfc1(r2, f1);
587 __ Mtc1(TMP, f1);
588 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
589 // Swap 2 GPR register pairs.
590 Register r1 = loc1.AsRegisterPairLow<Register>();
591 Register r2 = loc2.AsRegisterPairLow<Register>();
592 __ Move(TMP, r2);
593 __ Move(r2, r1);
594 __ Move(r1, TMP);
595 r1 = loc1.AsRegisterPairHigh<Register>();
596 r2 = loc2.AsRegisterPairHigh<Register>();
597 __ Move(TMP, r2);
598 __ Move(r2, r1);
599 __ Move(r1, TMP);
600 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
601 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
602 // Swap FPR and GPR register pair.
603 DCHECK_EQ(type, Primitive::kPrimDouble);
604 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
605 : loc2.AsFpuRegister<FRegister>();
606 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
607 : loc2.AsRegisterPairLow<Register>();
608 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
609 : loc2.AsRegisterPairHigh<Register>();
610 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
611 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
612 // unpredictable and the following mfch1 will fail.
613 __ Mfc1(TMP, f1);
614 __ Mfhc1(AT, f1);
615 __ Mtc1(r2_l, f1);
616 __ Mthc1(r2_h, f1);
617 __ Move(r2_l, TMP);
618 __ Move(r2_h, AT);
619 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
620 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
621 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
622 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
623 } else {
624 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
625 }
626}
627
628void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
629 __ Pop(static_cast<Register>(reg));
630}
631
632void ParallelMoveResolverMIPS::SpillScratch(int reg) {
633 __ Push(static_cast<Register>(reg));
634}
635
636void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
637 // Allocate a scratch register other than TMP, if available.
638 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
639 // automatically unspilled when the scratch scope object is destroyed).
640 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
641 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
642 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
643 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
644 __ LoadFromOffset(kLoadWord,
645 Register(ensure_scratch.GetRegister()),
646 SP,
647 index1 + stack_offset);
648 __ LoadFromOffset(kLoadWord,
649 TMP,
650 SP,
651 index2 + stack_offset);
652 __ StoreToOffset(kStoreWord,
653 Register(ensure_scratch.GetRegister()),
654 SP,
655 index2 + stack_offset);
656 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
657 }
658}
659
660static dwarf::Reg DWARFReg(Register reg) {
661 return dwarf::Reg::MipsCore(static_cast<int>(reg));
662}
663
664// TODO: mapping of floating-point registers to DWARF.
665
666void CodeGeneratorMIPS::GenerateFrameEntry() {
667 __ Bind(&frame_entry_label_);
668
669 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
670
671 if (do_overflow_check) {
672 __ LoadFromOffset(kLoadWord,
673 ZERO,
674 SP,
675 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
676 RecordPcInfo(nullptr, 0);
677 }
678
679 if (HasEmptyFrame()) {
680 return;
681 }
682
683 // Make sure the frame size isn't unreasonably large.
684 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
685 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
686 }
687
688 // Spill callee-saved registers.
689 // Note that their cumulative size is small and they can be indexed using
690 // 16-bit offsets.
691
692 // TODO: increment/decrement SP in one step instead of two or remove this comment.
693
694 uint32_t ofs = FrameEntrySpillSize();
695 bool unaligned_float = ofs & 0x7;
696 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
697 __ IncreaseFrameSize(ofs);
698
699 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
700 Register reg = kCoreCalleeSaves[i];
701 if (allocated_registers_.ContainsCoreRegister(reg)) {
702 ofs -= kMipsWordSize;
703 __ Sw(reg, SP, ofs);
704 __ cfi().RelOffset(DWARFReg(reg), ofs);
705 }
706 }
707
708 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
709 FRegister reg = kFpuCalleeSaves[i];
710 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
711 ofs -= kMipsDoublewordSize;
712 // TODO: Change the frame to avoid unaligned accesses for fpu registers.
713 if (unaligned_float) {
714 if (fpu_32bit) {
715 __ Swc1(reg, SP, ofs);
716 __ Swc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
717 } else {
718 __ Mfhc1(TMP, reg);
719 __ Swc1(reg, SP, ofs);
720 __ Sw(TMP, SP, ofs + 4);
721 }
722 } else {
723 __ Sdc1(reg, SP, ofs);
724 }
725 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
726 }
727 }
728
729 // Allocate the rest of the frame and store the current method pointer
730 // at its end.
731
732 __ IncreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
733
734 static_assert(IsInt<16>(kCurrentMethodStackOffset),
735 "kCurrentMethodStackOffset must fit into int16_t");
736 __ Sw(kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
737}
738
739void CodeGeneratorMIPS::GenerateFrameExit() {
740 __ cfi().RememberState();
741
742 if (!HasEmptyFrame()) {
743 // Deallocate the rest of the frame.
744
745 __ DecreaseFrameSize(GetFrameSize() - FrameEntrySpillSize());
746
747 // Restore callee-saved registers.
748 // Note that their cumulative size is small and they can be indexed using
749 // 16-bit offsets.
750
751 // TODO: increment/decrement SP in one step instead of two or remove this comment.
752
753 uint32_t ofs = 0;
754 bool unaligned_float = FrameEntrySpillSize() & 0x7;
755 bool fpu_32bit = isa_features_.Is32BitFloatingPoint();
756
757 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
758 FRegister reg = kFpuCalleeSaves[i];
759 if (allocated_registers_.ContainsFloatingPointRegister(reg)) {
760 if (unaligned_float) {
761 if (fpu_32bit) {
762 __ Lwc1(reg, SP, ofs);
763 __ Lwc1(static_cast<FRegister>(reg + 1), SP, ofs + 4);
764 } else {
765 __ Lwc1(reg, SP, ofs);
766 __ Lw(TMP, SP, ofs + 4);
767 __ Mthc1(TMP, reg);
768 }
769 } else {
770 __ Ldc1(reg, SP, ofs);
771 }
772 ofs += kMipsDoublewordSize;
773 // TODO: __ cfi().Restore(DWARFReg(reg));
774 }
775 }
776
777 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
778 Register reg = kCoreCalleeSaves[i];
779 if (allocated_registers_.ContainsCoreRegister(reg)) {
780 __ Lw(reg, SP, ofs);
781 ofs += kMipsWordSize;
782 __ cfi().Restore(DWARFReg(reg));
783 }
784 }
785
786 DCHECK_EQ(ofs, FrameEntrySpillSize());
787 __ DecreaseFrameSize(ofs);
788 }
789
790 __ Jr(RA);
791 __ Nop();
792
793 __ cfi().RestoreState();
794 __ cfi().DefCFAOffset(GetFrameSize());
795}
796
797void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
798 __ Bind(GetLabelOf(block));
799}
800
801void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
802 if (src.Equals(dst)) {
803 return;
804 }
805
806 if (src.IsConstant()) {
807 MoveConstant(dst, src.GetConstant());
808 } else {
809 if (Primitive::Is64BitType(dst_type)) {
810 Move64(dst, src);
811 } else {
812 Move32(dst, src);
813 }
814 }
815}
816
817void CodeGeneratorMIPS::Move32(Location destination, Location source) {
818 if (source.Equals(destination)) {
819 return;
820 }
821
822 if (destination.IsRegister()) {
823 if (source.IsRegister()) {
824 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
825 } else if (source.IsFpuRegister()) {
826 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
827 } else {
828 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
829 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
830 }
831 } else if (destination.IsFpuRegister()) {
832 if (source.IsRegister()) {
833 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
834 } else if (source.IsFpuRegister()) {
835 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
836 } else {
837 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
838 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
839 }
840 } else {
841 DCHECK(destination.IsStackSlot()) << destination;
842 if (source.IsRegister()) {
843 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
844 } else if (source.IsFpuRegister()) {
845 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
846 } else {
847 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
848 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
849 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
850 }
851 }
852}
853
854void CodeGeneratorMIPS::Move64(Location destination, Location source) {
855 if (source.Equals(destination)) {
856 return;
857 }
858
859 if (destination.IsRegisterPair()) {
860 if (source.IsRegisterPair()) {
861 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
862 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
863 } else if (source.IsFpuRegister()) {
864 Register dst_high = destination.AsRegisterPairHigh<Register>();
865 Register dst_low = destination.AsRegisterPairLow<Register>();
866 FRegister src = source.AsFpuRegister<FRegister>();
867 __ Mfc1(dst_low, src);
868 __ Mfhc1(dst_high, src);
869 } else {
870 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
871 int32_t off = source.GetStackIndex();
872 Register r = destination.AsRegisterPairLow<Register>();
873 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
874 }
875 } else if (destination.IsFpuRegister()) {
876 if (source.IsRegisterPair()) {
877 FRegister dst = destination.AsFpuRegister<FRegister>();
878 Register src_high = source.AsRegisterPairHigh<Register>();
879 Register src_low = source.AsRegisterPairLow<Register>();
880 __ Mtc1(src_low, dst);
881 __ Mthc1(src_high, dst);
882 } else if (source.IsFpuRegister()) {
883 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
884 } else {
885 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
886 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
887 }
888 } else {
889 DCHECK(destination.IsDoubleStackSlot()) << destination;
890 int32_t off = destination.GetStackIndex();
891 if (source.IsRegisterPair()) {
892 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
893 } else if (source.IsFpuRegister()) {
894 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
895 } else {
896 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
897 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
898 __ StoreToOffset(kStoreWord, TMP, SP, off);
899 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
900 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
901 }
902 }
903}
904
905void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
906 if (c->IsIntConstant() || c->IsNullConstant()) {
907 // Move 32 bit constant.
908 int32_t value = GetInt32ValueOf(c);
909 if (destination.IsRegister()) {
910 Register dst = destination.AsRegister<Register>();
911 __ LoadConst32(dst, value);
912 } else {
913 DCHECK(destination.IsStackSlot())
914 << "Cannot move " << c->DebugName() << " to " << destination;
915 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
916 }
917 } else if (c->IsLongConstant()) {
918 // Move 64 bit constant.
919 int64_t value = GetInt64ValueOf(c);
920 if (destination.IsRegisterPair()) {
921 Register r_h = destination.AsRegisterPairHigh<Register>();
922 Register r_l = destination.AsRegisterPairLow<Register>();
923 __ LoadConst64(r_h, r_l, value);
924 } else {
925 DCHECK(destination.IsDoubleStackSlot())
926 << "Cannot move " << c->DebugName() << " to " << destination;
927 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
928 }
929 } else if (c->IsFloatConstant()) {
930 // Move 32 bit float constant.
931 int32_t value = GetInt32ValueOf(c);
932 if (destination.IsFpuRegister()) {
933 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
934 } else {
935 DCHECK(destination.IsStackSlot())
936 << "Cannot move " << c->DebugName() << " to " << destination;
937 __ StoreConst32ToOffset(value, SP, destination.GetStackIndex(), TMP);
938 }
939 } else {
940 // Move 64 bit double constant.
941 DCHECK(c->IsDoubleConstant()) << c->DebugName();
942 int64_t value = GetInt64ValueOf(c);
943 if (destination.IsFpuRegister()) {
944 FRegister fd = destination.AsFpuRegister<FRegister>();
945 __ LoadDConst64(fd, value, TMP);
946 } else {
947 DCHECK(destination.IsDoubleStackSlot())
948 << "Cannot move " << c->DebugName() << " to " << destination;
949 __ StoreConst64ToOffset(value, SP, destination.GetStackIndex(), TMP);
950 }
951 }
952}
953
954void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
955 DCHECK(destination.IsRegister());
956 Register dst = destination.AsRegister<Register>();
957 __ LoadConst32(dst, value);
958}
959
960void CodeGeneratorMIPS::Move(HInstruction* instruction,
961 Location location,
962 HInstruction* move_for) {
963 LocationSummary* locations = instruction->GetLocations();
964 Primitive::Type type = instruction->GetType();
965 DCHECK_NE(type, Primitive::kPrimVoid);
966
967 if (instruction->IsCurrentMethod()) {
968 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
969 } else if (locations != nullptr && locations->Out().Equals(location)) {
970 return;
971 } else if (instruction->IsIntConstant()
972 || instruction->IsLongConstant()
973 || instruction->IsNullConstant()) {
974 MoveConstant(location, instruction->AsConstant());
975 } else if (instruction->IsTemporary()) {
976 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
977 if (temp_location.IsStackSlot()) {
978 Move32(location, temp_location);
979 } else {
980 DCHECK(temp_location.IsDoubleStackSlot());
981 Move64(location, temp_location);
982 }
983 } else if (instruction->IsLoadLocal()) {
984 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
985 if (Primitive::Is64BitType(type)) {
986 Move64(location, Location::DoubleStackSlot(stack_slot));
987 } else {
988 Move32(location, Location::StackSlot(stack_slot));
989 }
990 } else {
991 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
992 if (Primitive::Is64BitType(type)) {
993 Move64(location, locations->Out());
994 } else {
995 Move32(location, locations->Out());
996 }
997 }
998}
999
1000void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1001 if (location.IsRegister()) {
1002 locations->AddTemp(location);
1003 } else {
1004 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1005 }
1006}
1007
1008Location CodeGeneratorMIPS::GetStackLocation(HLoadLocal* load) const {
1009 Primitive::Type type = load->GetType();
1010
1011 switch (type) {
1012 case Primitive::kPrimNot:
1013 case Primitive::kPrimInt:
1014 case Primitive::kPrimFloat:
1015 return Location::StackSlot(GetStackSlot(load->GetLocal()));
1016
1017 case Primitive::kPrimLong:
1018 case Primitive::kPrimDouble:
1019 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
1020
1021 case Primitive::kPrimBoolean:
1022 case Primitive::kPrimByte:
1023 case Primitive::kPrimChar:
1024 case Primitive::kPrimShort:
1025 case Primitive::kPrimVoid:
1026 LOG(FATAL) << "Unexpected type " << type;
1027 }
1028
1029 LOG(FATAL) << "Unreachable";
1030 return Location::NoLocation();
1031}
1032
1033void CodeGeneratorMIPS::MarkGCCard(Register object, Register value) {
1034 MipsLabel done;
1035 Register card = AT;
1036 Register temp = TMP;
1037 __ Beqz(value, &done);
1038 __ LoadFromOffset(kLoadWord,
1039 card,
1040 TR,
1041 Thread::CardTableOffset<kMipsWordSize>().Int32Value());
1042 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1043 __ Addu(temp, card, temp);
1044 __ Sb(card, temp, 0);
1045 __ Bind(&done);
1046}
1047
1048void CodeGeneratorMIPS::SetupBlockedRegisters(bool is_baseline) const {
1049 // Don't allocate the dalvik style register pair passing.
1050 blocked_register_pairs_[A1_A2] = true;
1051
1052 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1053 blocked_core_registers_[ZERO] = true;
1054 blocked_core_registers_[K0] = true;
1055 blocked_core_registers_[K1] = true;
1056 blocked_core_registers_[GP] = true;
1057 blocked_core_registers_[SP] = true;
1058 blocked_core_registers_[RA] = true;
1059
1060 // AT and TMP(T8) are used as temporary/scratch registers
1061 // (similar to how AT is used by MIPS assemblers).
1062 blocked_core_registers_[AT] = true;
1063 blocked_core_registers_[TMP] = true;
1064 blocked_fpu_registers_[FTMP] = true;
1065
1066 // Reserve suspend and thread registers.
1067 blocked_core_registers_[S0] = true;
1068 blocked_core_registers_[TR] = true;
1069
1070 // Reserve T9 for function calls
1071 blocked_core_registers_[T9] = true;
1072
1073 // Reserve odd-numbered FPU registers.
1074 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1075 blocked_fpu_registers_[i] = true;
1076 }
1077
1078 if (is_baseline) {
1079 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1080 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
1081 }
1082
1083 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1084 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1085 }
1086 }
1087
1088 UpdateBlockedPairRegisters();
1089}
1090
1091void CodeGeneratorMIPS::UpdateBlockedPairRegisters() const {
1092 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1093 MipsManagedRegister current =
1094 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1095 if (blocked_core_registers_[current.AsRegisterPairLow()]
1096 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1097 blocked_register_pairs_[i] = true;
1098 }
1099 }
1100}
1101
1102Location CodeGeneratorMIPS::AllocateFreeRegister(Primitive::Type type) const {
1103 switch (type) {
1104 case Primitive::kPrimLong: {
1105 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
1106 MipsManagedRegister pair =
1107 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
1108 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
1109 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
1110
1111 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
1112 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
1113 UpdateBlockedPairRegisters();
1114 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
1115 }
1116
1117 case Primitive::kPrimByte:
1118 case Primitive::kPrimBoolean:
1119 case Primitive::kPrimChar:
1120 case Primitive::kPrimShort:
1121 case Primitive::kPrimInt:
1122 case Primitive::kPrimNot: {
1123 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters);
1124 // Block all register pairs that contain `reg`.
1125 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1126 MipsManagedRegister current =
1127 MipsManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1128 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
1129 blocked_register_pairs_[i] = true;
1130 }
1131 }
1132 return Location::RegisterLocation(reg);
1133 }
1134
1135 case Primitive::kPrimFloat:
1136 case Primitive::kPrimDouble: {
1137 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFRegisters);
1138 return Location::FpuRegisterLocation(reg);
1139 }
1140
1141 case Primitive::kPrimVoid:
1142 LOG(FATAL) << "Unreachable type " << type;
1143 }
1144
1145 UNREACHABLE();
1146}
1147
1148size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1149 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1150 return kMipsWordSize;
1151}
1152
1153size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1154 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1155 return kMipsWordSize;
1156}
1157
1158size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1159 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1160 return kMipsDoublewordSize;
1161}
1162
1163size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1164 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1165 return kMipsDoublewordSize;
1166}
1167
1168void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
1169 stream << MipsManagedRegister::FromCoreRegister(Register(reg));
1170}
1171
1172void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
1173 stream << MipsManagedRegister::FromFRegister(FRegister(reg));
1174}
1175
1176void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1177 HInstruction* instruction,
1178 uint32_t dex_pc,
1179 SlowPathCode* slow_path) {
1180 InvokeRuntime(GetThreadOffset<kMipsWordSize>(entrypoint).Int32Value(),
1181 instruction,
1182 dex_pc,
1183 slow_path,
1184 IsDirectEntrypoint(entrypoint));
1185}
1186
1187constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1188
1189void CodeGeneratorMIPS::InvokeRuntime(int32_t entry_point_offset,
1190 HInstruction* instruction,
1191 uint32_t dex_pc,
1192 SlowPathCode* slow_path,
1193 bool is_direct_entrypoint) {
1194 if (is_direct_entrypoint) {
1195 // Reserve argument space on stack (for $a0-$a3) for
1196 // entrypoints that directly reference native implementations.
1197 // Called function may use this space to store $a0-$a3 regs.
1198 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1199 }
1200 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
1201 __ Jalr(T9);
1202 __ Nop();
1203 if (is_direct_entrypoint) {
1204 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
1205 }
1206 RecordPcInfo(instruction, dex_pc, slow_path);
1207}
1208
1209void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1210 Register class_reg) {
1211 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1212 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1213 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1214 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1215 __ Sync(0);
1216 __ Bind(slow_path->GetExitLabel());
1217}
1218
1219void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1220 __ Sync(0); // Only stype 0 is supported.
1221}
1222
1223void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1224 HBasicBlock* successor) {
1225 SuspendCheckSlowPathMIPS* slow_path =
1226 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1227 codegen_->AddSlowPath(slow_path);
1228
1229 __ LoadFromOffset(kLoadUnsignedHalfword,
1230 TMP,
1231 TR,
1232 Thread::ThreadFlagsOffset<kMipsWordSize>().Int32Value());
1233 if (successor == nullptr) {
1234 __ Bnez(TMP, slow_path->GetEntryLabel());
1235 __ Bind(slow_path->GetReturnLabel());
1236 } else {
1237 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1238 __ B(slow_path->GetEntryLabel());
1239 // slow_path will return to GetLabelOf(successor).
1240 }
1241}
1242
1243InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1244 CodeGeneratorMIPS* codegen)
1245 : HGraphVisitor(graph),
1246 assembler_(codegen->GetAssembler()),
1247 codegen_(codegen) {}
1248
1249void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1250 DCHECK_EQ(instruction->InputCount(), 2U);
1251 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1252 Primitive::Type type = instruction->GetResultType();
1253 switch (type) {
1254 case Primitive::kPrimInt: {
1255 locations->SetInAt(0, Location::RequiresRegister());
1256 HInstruction* right = instruction->InputAt(1);
1257 bool can_use_imm = false;
1258 if (right->IsConstant()) {
1259 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1260 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1261 can_use_imm = IsUint<16>(imm);
1262 } else if (instruction->IsAdd()) {
1263 can_use_imm = IsInt<16>(imm);
1264 } else {
1265 DCHECK(instruction->IsSub());
1266 can_use_imm = IsInt<16>(-imm);
1267 }
1268 }
1269 if (can_use_imm)
1270 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1271 else
1272 locations->SetInAt(1, Location::RequiresRegister());
1273 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1274 break;
1275 }
1276
1277 case Primitive::kPrimLong: {
1278 // TODO: can 2nd param be const?
1279 locations->SetInAt(0, Location::RequiresRegister());
1280 locations->SetInAt(1, Location::RequiresRegister());
1281 if (instruction->IsAdd() || instruction->IsSub()) {
1282 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1283 } else {
1284 DCHECK(instruction->IsAnd() || instruction->IsOr() || instruction->IsXor());
1285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1286 }
1287 break;
1288 }
1289
1290 case Primitive::kPrimFloat:
1291 case Primitive::kPrimDouble:
1292 DCHECK(instruction->IsAdd() || instruction->IsSub());
1293 locations->SetInAt(0, Location::RequiresFpuRegister());
1294 locations->SetInAt(1, Location::RequiresFpuRegister());
1295 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1296 break;
1297
1298 default:
1299 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1300 }
1301}
1302
1303void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1304 Primitive::Type type = instruction->GetType();
1305 LocationSummary* locations = instruction->GetLocations();
1306
1307 switch (type) {
1308 case Primitive::kPrimInt: {
1309 Register dst = locations->Out().AsRegister<Register>();
1310 Register lhs = locations->InAt(0).AsRegister<Register>();
1311 Location rhs_location = locations->InAt(1);
1312
1313 Register rhs_reg = ZERO;
1314 int32_t rhs_imm = 0;
1315 bool use_imm = rhs_location.IsConstant();
1316 if (use_imm) {
1317 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1318 } else {
1319 rhs_reg = rhs_location.AsRegister<Register>();
1320 }
1321
1322 if (instruction->IsAnd()) {
1323 if (use_imm)
1324 __ Andi(dst, lhs, rhs_imm);
1325 else
1326 __ And(dst, lhs, rhs_reg);
1327 } else if (instruction->IsOr()) {
1328 if (use_imm)
1329 __ Ori(dst, lhs, rhs_imm);
1330 else
1331 __ Or(dst, lhs, rhs_reg);
1332 } else if (instruction->IsXor()) {
1333 if (use_imm)
1334 __ Xori(dst, lhs, rhs_imm);
1335 else
1336 __ Xor(dst, lhs, rhs_reg);
1337 } else if (instruction->IsAdd()) {
1338 if (use_imm)
1339 __ Addiu(dst, lhs, rhs_imm);
1340 else
1341 __ Addu(dst, lhs, rhs_reg);
1342 } else {
1343 DCHECK(instruction->IsSub());
1344 if (use_imm)
1345 __ Addiu(dst, lhs, -rhs_imm);
1346 else
1347 __ Subu(dst, lhs, rhs_reg);
1348 }
1349 break;
1350 }
1351
1352 case Primitive::kPrimLong: {
1353 // TODO: can 2nd param be const?
1354 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1355 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1356 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1357 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1358 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
1359 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
1360
1361 if (instruction->IsAnd()) {
1362 __ And(dst_low, lhs_low, rhs_low);
1363 __ And(dst_high, lhs_high, rhs_high);
1364 } else if (instruction->IsOr()) {
1365 __ Or(dst_low, lhs_low, rhs_low);
1366 __ Or(dst_high, lhs_high, rhs_high);
1367 } else if (instruction->IsXor()) {
1368 __ Xor(dst_low, lhs_low, rhs_low);
1369 __ Xor(dst_high, lhs_high, rhs_high);
1370 } else if (instruction->IsAdd()) {
1371 __ Addu(dst_low, lhs_low, rhs_low);
1372 __ Sltu(TMP, dst_low, lhs_low);
1373 __ Addu(dst_high, lhs_high, rhs_high);
1374 __ Addu(dst_high, dst_high, TMP);
1375 } else {
1376 DCHECK(instruction->IsSub());
1377 __ Subu(dst_low, lhs_low, rhs_low);
1378 __ Sltu(TMP, lhs_low, dst_low);
1379 __ Subu(dst_high, lhs_high, rhs_high);
1380 __ Subu(dst_high, dst_high, TMP);
1381 }
1382 break;
1383 }
1384
1385 case Primitive::kPrimFloat:
1386 case Primitive::kPrimDouble: {
1387 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
1388 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
1389 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
1390 if (instruction->IsAdd()) {
1391 if (type == Primitive::kPrimFloat) {
1392 __ AddS(dst, lhs, rhs);
1393 } else {
1394 __ AddD(dst, lhs, rhs);
1395 }
1396 } else {
1397 DCHECK(instruction->IsSub());
1398 if (type == Primitive::kPrimFloat) {
1399 __ SubS(dst, lhs, rhs);
1400 } else {
1401 __ SubD(dst, lhs, rhs);
1402 }
1403 }
1404 break;
1405 }
1406
1407 default:
1408 LOG(FATAL) << "Unexpected binary operation type " << type;
1409 }
1410}
1411
1412void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
1413 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1414
1415 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
1416 Primitive::Type type = instr->GetResultType();
1417 switch (type) {
1418 case Primitive::kPrimInt:
1419 case Primitive::kPrimLong: {
1420 locations->SetInAt(0, Location::RequiresRegister());
1421 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
1422 locations->SetOut(Location::RequiresRegister());
1423 break;
1424 }
1425 default:
1426 LOG(FATAL) << "Unexpected shift type " << type;
1427 }
1428}
1429
1430static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
1431
1432void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
1433 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr());
1434 LocationSummary* locations = instr->GetLocations();
1435 Primitive::Type type = instr->GetType();
1436
1437 Location rhs_location = locations->InAt(1);
1438 bool use_imm = rhs_location.IsConstant();
1439 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
1440 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
1441 uint32_t shift_mask = (type == Primitive::kPrimInt) ? kMaxIntShiftValue : kMaxLongShiftValue;
1442 uint32_t shift_value = rhs_imm & shift_mask;
1443
1444 switch (type) {
1445 case Primitive::kPrimInt: {
1446 Register dst = locations->Out().AsRegister<Register>();
1447 Register lhs = locations->InAt(0).AsRegister<Register>();
1448 if (use_imm) {
1449 if (instr->IsShl()) {
1450 __ Sll(dst, lhs, shift_value);
1451 } else if (instr->IsShr()) {
1452 __ Sra(dst, lhs, shift_value);
1453 } else {
1454 __ Srl(dst, lhs, shift_value);
1455 }
1456 } else {
1457 if (instr->IsShl()) {
1458 __ Sllv(dst, lhs, rhs_reg);
1459 } else if (instr->IsShr()) {
1460 __ Srav(dst, lhs, rhs_reg);
1461 } else {
1462 __ Srlv(dst, lhs, rhs_reg);
1463 }
1464 }
1465 break;
1466 }
1467
1468 case Primitive::kPrimLong: {
1469 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
1470 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
1471 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
1472 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
1473 if (use_imm) {
1474 if (shift_value == 0) {
1475 codegen_->Move64(locations->Out(), locations->InAt(0));
1476 } else if (shift_value < kMipsBitsPerWord) {
1477 if (instr->IsShl()) {
1478 __ Sll(dst_low, lhs_low, shift_value);
1479 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
1480 __ Sll(dst_high, lhs_high, shift_value);
1481 __ Or(dst_high, dst_high, TMP);
1482 } else if (instr->IsShr()) {
1483 __ Sra(dst_high, lhs_high, shift_value);
1484 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1485 __ Srl(dst_low, lhs_low, shift_value);
1486 __ Or(dst_low, dst_low, TMP);
1487 } else {
1488 __ Srl(dst_high, lhs_high, shift_value);
1489 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
1490 __ Srl(dst_low, lhs_low, shift_value);
1491 __ Or(dst_low, dst_low, TMP);
1492 }
1493 } else {
1494 shift_value -= kMipsBitsPerWord;
1495 if (instr->IsShl()) {
1496 __ Sll(dst_high, lhs_low, shift_value);
1497 __ Move(dst_low, ZERO);
1498 } else if (instr->IsShr()) {
1499 __ Sra(dst_low, lhs_high, shift_value);
1500 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
1501 } else {
1502 __ Srl(dst_low, lhs_high, shift_value);
1503 __ Move(dst_high, ZERO);
1504 }
1505 }
1506 } else {
1507 MipsLabel done;
1508 if (instr->IsShl()) {
1509 __ Sllv(dst_low, lhs_low, rhs_reg);
1510 __ Nor(AT, ZERO, rhs_reg);
1511 __ Srl(TMP, lhs_low, 1);
1512 __ Srlv(TMP, TMP, AT);
1513 __ Sllv(dst_high, lhs_high, rhs_reg);
1514 __ Or(dst_high, dst_high, TMP);
1515 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1516 __ Beqz(TMP, &done);
1517 __ Move(dst_high, dst_low);
1518 __ Move(dst_low, ZERO);
1519 } else if (instr->IsShr()) {
1520 __ Srav(dst_high, lhs_high, rhs_reg);
1521 __ Nor(AT, ZERO, rhs_reg);
1522 __ Sll(TMP, lhs_high, 1);
1523 __ Sllv(TMP, TMP, AT);
1524 __ Srlv(dst_low, lhs_low, rhs_reg);
1525 __ Or(dst_low, dst_low, TMP);
1526 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1527 __ Beqz(TMP, &done);
1528 __ Move(dst_low, dst_high);
1529 __ Sra(dst_high, dst_high, 31);
1530 } else {
1531 __ Srlv(dst_high, lhs_high, rhs_reg);
1532 __ Nor(AT, ZERO, rhs_reg);
1533 __ Sll(TMP, lhs_high, 1);
1534 __ Sllv(TMP, TMP, AT);
1535 __ Srlv(dst_low, lhs_low, rhs_reg);
1536 __ Or(dst_low, dst_low, TMP);
1537 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
1538 __ Beqz(TMP, &done);
1539 __ Move(dst_low, dst_high);
1540 __ Move(dst_high, ZERO);
1541 }
1542 __ Bind(&done);
1543 }
1544 break;
1545 }
1546
1547 default:
1548 LOG(FATAL) << "Unexpected shift operation type " << type;
1549 }
1550}
1551
1552void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
1553 HandleBinaryOp(instruction);
1554}
1555
1556void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
1557 HandleBinaryOp(instruction);
1558}
1559
1560void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
1561 HandleBinaryOp(instruction);
1562}
1563
1564void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
1565 HandleBinaryOp(instruction);
1566}
1567
1568void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
1571 locations->SetInAt(0, Location::RequiresRegister());
1572 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1573 if (Primitive::IsFloatingPointType(instruction->GetType())) {
1574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1575 } else {
1576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1577 }
1578}
1579
1580void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
1581 LocationSummary* locations = instruction->GetLocations();
1582 Register obj = locations->InAt(0).AsRegister<Register>();
1583 Location index = locations->InAt(1);
1584 Primitive::Type type = instruction->GetType();
1585
1586 switch (type) {
1587 case Primitive::kPrimBoolean: {
1588 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1589 Register out = locations->Out().AsRegister<Register>();
1590 if (index.IsConstant()) {
1591 size_t offset =
1592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1593 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset);
1594 } else {
1595 __ Addu(TMP, obj, index.AsRegister<Register>());
1596 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
1597 }
1598 break;
1599 }
1600
1601 case Primitive::kPrimByte: {
1602 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
1603 Register out = locations->Out().AsRegister<Register>();
1604 if (index.IsConstant()) {
1605 size_t offset =
1606 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1607 __ LoadFromOffset(kLoadSignedByte, out, obj, offset);
1608 } else {
1609 __ Addu(TMP, obj, index.AsRegister<Register>());
1610 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset);
1611 }
1612 break;
1613 }
1614
1615 case Primitive::kPrimShort: {
1616 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
1617 Register out = locations->Out().AsRegister<Register>();
1618 if (index.IsConstant()) {
1619 size_t offset =
1620 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1621 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset);
1622 } else {
1623 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1624 __ Addu(TMP, obj, TMP);
1625 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset);
1626 }
1627 break;
1628 }
1629
1630 case Primitive::kPrimChar: {
1631 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1632 Register out = locations->Out().AsRegister<Register>();
1633 if (index.IsConstant()) {
1634 size_t offset =
1635 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1636 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset);
1637 } else {
1638 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1639 __ Addu(TMP, obj, TMP);
1640 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
1641 }
1642 break;
1643 }
1644
1645 case Primitive::kPrimInt:
1646 case Primitive::kPrimNot: {
1647 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
1648 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1649 Register out = locations->Out().AsRegister<Register>();
1650 if (index.IsConstant()) {
1651 size_t offset =
1652 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1653 __ LoadFromOffset(kLoadWord, out, obj, offset);
1654 } else {
1655 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1656 __ Addu(TMP, obj, TMP);
1657 __ LoadFromOffset(kLoadWord, out, TMP, data_offset);
1658 }
1659 break;
1660 }
1661
1662 case Primitive::kPrimLong: {
1663 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1664 Register out = locations->Out().AsRegisterPairLow<Register>();
1665 if (index.IsConstant()) {
1666 size_t offset =
1667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1668 __ LoadFromOffset(kLoadDoubleword, out, obj, offset);
1669 } else {
1670 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1671 __ Addu(TMP, obj, TMP);
1672 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset);
1673 }
1674 break;
1675 }
1676
1677 case Primitive::kPrimFloat: {
1678 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1679 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1680 if (index.IsConstant()) {
1681 size_t offset =
1682 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1683 __ LoadSFromOffset(out, obj, offset);
1684 } else {
1685 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1686 __ Addu(TMP, obj, TMP);
1687 __ LoadSFromOffset(out, TMP, data_offset);
1688 }
1689 break;
1690 }
1691
1692 case Primitive::kPrimDouble: {
1693 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1694 FRegister out = locations->Out().AsFpuRegister<FRegister>();
1695 if (index.IsConstant()) {
1696 size_t offset =
1697 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1698 __ LoadDFromOffset(out, obj, offset);
1699 } else {
1700 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1701 __ Addu(TMP, obj, TMP);
1702 __ LoadDFromOffset(out, TMP, data_offset);
1703 }
1704 break;
1705 }
1706
1707 case Primitive::kPrimVoid:
1708 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1709 UNREACHABLE();
1710 }
1711 codegen_->MaybeRecordImplicitNullCheck(instruction);
1712}
1713
1714void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
1715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1716 locations->SetInAt(0, Location::RequiresRegister());
1717 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1718}
1719
1720void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
1721 LocationSummary* locations = instruction->GetLocations();
1722 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
1723 Register obj = locations->InAt(0).AsRegister<Register>();
1724 Register out = locations->Out().AsRegister<Register>();
1725 __ LoadFromOffset(kLoadWord, out, obj, offset);
1726 codegen_->MaybeRecordImplicitNullCheck(instruction);
1727}
1728
1729void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
1730 Primitive::Type value_type = instruction->GetComponentType();
1731 bool is_object = value_type == Primitive::kPrimNot;
1732 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1733 instruction,
1734 is_object ? LocationSummary::kCall : LocationSummary::kNoCall);
1735 if (is_object) {
1736 InvokeRuntimeCallingConvention calling_convention;
1737 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1738 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1739 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1740 } else {
1741 locations->SetInAt(0, Location::RequiresRegister());
1742 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1743 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
1744 locations->SetInAt(2, Location::RequiresFpuRegister());
1745 } else {
1746 locations->SetInAt(2, Location::RequiresRegister());
1747 }
1748 }
1749}
1750
1751void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
1752 LocationSummary* locations = instruction->GetLocations();
1753 Register obj = locations->InAt(0).AsRegister<Register>();
1754 Location index = locations->InAt(1);
1755 Primitive::Type value_type = instruction->GetComponentType();
1756 bool needs_runtime_call = locations->WillCall();
1757 bool needs_write_barrier =
1758 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
1759
1760 switch (value_type) {
1761 case Primitive::kPrimBoolean:
1762 case Primitive::kPrimByte: {
1763 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
1764 Register value = locations->InAt(2).AsRegister<Register>();
1765 if (index.IsConstant()) {
1766 size_t offset =
1767 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
1768 __ StoreToOffset(kStoreByte, value, obj, offset);
1769 } else {
1770 __ Addu(TMP, obj, index.AsRegister<Register>());
1771 __ StoreToOffset(kStoreByte, value, TMP, data_offset);
1772 }
1773 break;
1774 }
1775
1776 case Primitive::kPrimShort:
1777 case Primitive::kPrimChar: {
1778 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
1779 Register value = locations->InAt(2).AsRegister<Register>();
1780 if (index.IsConstant()) {
1781 size_t offset =
1782 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
1783 __ StoreToOffset(kStoreHalfword, value, obj, offset);
1784 } else {
1785 __ Sll(TMP, index.AsRegister<Register>(), TIMES_2);
1786 __ Addu(TMP, obj, TMP);
1787 __ StoreToOffset(kStoreHalfword, value, TMP, data_offset);
1788 }
1789 break;
1790 }
1791
1792 case Primitive::kPrimInt:
1793 case Primitive::kPrimNot: {
1794 if (!needs_runtime_call) {
1795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
1796 Register value = locations->InAt(2).AsRegister<Register>();
1797 if (index.IsConstant()) {
1798 size_t offset =
1799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1800 __ StoreToOffset(kStoreWord, value, obj, offset);
1801 } else {
1802 DCHECK(index.IsRegister()) << index;
1803 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1804 __ Addu(TMP, obj, TMP);
1805 __ StoreToOffset(kStoreWord, value, TMP, data_offset);
1806 }
1807 codegen_->MaybeRecordImplicitNullCheck(instruction);
1808 if (needs_write_barrier) {
1809 DCHECK_EQ(value_type, Primitive::kPrimNot);
1810 codegen_->MarkGCCard(obj, value);
1811 }
1812 } else {
1813 DCHECK_EQ(value_type, Primitive::kPrimNot);
1814 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
1815 instruction,
1816 instruction->GetDexPc(),
1817 nullptr,
1818 IsDirectEntrypoint(kQuickAputObject));
1819 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
1820 }
1821 break;
1822 }
1823
1824 case Primitive::kPrimLong: {
1825 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
1826 Register value = locations->InAt(2).AsRegisterPairLow<Register>();
1827 if (index.IsConstant()) {
1828 size_t offset =
1829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1830 __ StoreToOffset(kStoreDoubleword, value, obj, offset);
1831 } else {
1832 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1833 __ Addu(TMP, obj, TMP);
1834 __ StoreToOffset(kStoreDoubleword, value, TMP, data_offset);
1835 }
1836 break;
1837 }
1838
1839 case Primitive::kPrimFloat: {
1840 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
1841 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1842 DCHECK(locations->InAt(2).IsFpuRegister());
1843 if (index.IsConstant()) {
1844 size_t offset =
1845 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
1846 __ StoreSToOffset(value, obj, offset);
1847 } else {
1848 __ Sll(TMP, index.AsRegister<Register>(), TIMES_4);
1849 __ Addu(TMP, obj, TMP);
1850 __ StoreSToOffset(value, TMP, data_offset);
1851 }
1852 break;
1853 }
1854
1855 case Primitive::kPrimDouble: {
1856 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
1857 FRegister value = locations->InAt(2).AsFpuRegister<FRegister>();
1858 DCHECK(locations->InAt(2).IsFpuRegister());
1859 if (index.IsConstant()) {
1860 size_t offset =
1861 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
1862 __ StoreDToOffset(value, obj, offset);
1863 } else {
1864 __ Sll(TMP, index.AsRegister<Register>(), TIMES_8);
1865 __ Addu(TMP, obj, TMP);
1866 __ StoreDToOffset(value, TMP, data_offset);
1867 }
1868 break;
1869 }
1870
1871 case Primitive::kPrimVoid:
1872 LOG(FATAL) << "Unreachable type " << instruction->GetType();
1873 UNREACHABLE();
1874 }
1875
1876 // Ints and objects are handled in the switch.
1877 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) {
1878 codegen_->MaybeRecordImplicitNullCheck(instruction);
1879 }
1880}
1881
1882void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1883 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
1884 ? LocationSummary::kCallOnSlowPath
1885 : LocationSummary::kNoCall;
1886 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 locations->SetInAt(1, Location::RequiresRegister());
1889 if (instruction->HasUses()) {
1890 locations->SetOut(Location::SameAsFirstInput());
1891 }
1892}
1893
1894void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
1895 LocationSummary* locations = instruction->GetLocations();
1896 BoundsCheckSlowPathMIPS* slow_path =
1897 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
1898 codegen_->AddSlowPath(slow_path);
1899
1900 Register index = locations->InAt(0).AsRegister<Register>();
1901 Register length = locations->InAt(1).AsRegister<Register>();
1902
1903 // length is limited by the maximum positive signed 32-bit integer.
1904 // Unsigned comparison of length and index checks for index < 0
1905 // and for length <= index simultaneously.
1906 __ Bgeu(index, length, slow_path->GetEntryLabel());
1907}
1908
1909void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
1910 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
1911 instruction,
1912 LocationSummary::kCallOnSlowPath);
1913 locations->SetInAt(0, Location::RequiresRegister());
1914 locations->SetInAt(1, Location::RequiresRegister());
1915 // Note that TypeCheckSlowPathMIPS uses this register too.
1916 locations->AddTemp(Location::RequiresRegister());
1917}
1918
1919void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
1920 LocationSummary* locations = instruction->GetLocations();
1921 Register obj = locations->InAt(0).AsRegister<Register>();
1922 Register cls = locations->InAt(1).AsRegister<Register>();
1923 Register obj_cls = locations->GetTemp(0).AsRegister<Register>();
1924
1925 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
1926 codegen_->AddSlowPath(slow_path);
1927
1928 // TODO: avoid this check if we know obj is not null.
1929 __ Beqz(obj, slow_path->GetExitLabel());
1930 // Compare the class of `obj` with `cls`.
1931 __ LoadFromOffset(kLoadWord, obj_cls, obj, mirror::Object::ClassOffset().Int32Value());
1932 __ Bne(obj_cls, cls, slow_path->GetEntryLabel());
1933 __ Bind(slow_path->GetExitLabel());
1934}
1935
1936void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
1937 LocationSummary* locations =
1938 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
1939 locations->SetInAt(0, Location::RequiresRegister());
1940 if (check->HasUses()) {
1941 locations->SetOut(Location::SameAsFirstInput());
1942 }
1943}
1944
1945void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
1946 // We assume the class is not null.
1947 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
1948 check->GetLoadClass(),
1949 check,
1950 check->GetDexPc(),
1951 true);
1952 codegen_->AddSlowPath(slow_path);
1953 GenerateClassInitializationCheck(slow_path,
1954 check->GetLocations()->InAt(0).AsRegister<Register>());
1955}
1956
1957void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
1958 Primitive::Type in_type = compare->InputAt(0)->GetType();
1959
1960 LocationSummary::CallKind call_kind = Primitive::IsFloatingPointType(in_type)
1961 ? LocationSummary::kCall
1962 : LocationSummary::kNoCall;
1963
1964 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(compare, call_kind);
1965
1966 switch (in_type) {
1967 case Primitive::kPrimLong:
1968 locations->SetInAt(0, Location::RequiresRegister());
1969 locations->SetInAt(1, Location::RequiresRegister());
1970 // Output overlaps because it is written before doing the low comparison.
1971 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1972 break;
1973
1974 case Primitive::kPrimFloat:
1975 case Primitive::kPrimDouble: {
1976 InvokeRuntimeCallingConvention calling_convention;
1977 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1978 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1979 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1980 break;
1981 }
1982
1983 default:
1984 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
1985 }
1986}
1987
1988void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
1989 LocationSummary* locations = instruction->GetLocations();
1990 Primitive::Type in_type = instruction->InputAt(0)->GetType();
1991
1992 // 0 if: left == right
1993 // 1 if: left > right
1994 // -1 if: left < right
1995 switch (in_type) {
1996 case Primitive::kPrimLong: {
1997 MipsLabel done;
1998 Register res = locations->Out().AsRegister<Register>();
1999 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2000 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2001 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
2002 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
2003 // TODO: more efficient (direct) comparison with a constant.
2004 __ Slt(TMP, lhs_high, rhs_high);
2005 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
2006 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2007 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
2008 __ Sltu(TMP, lhs_low, rhs_low);
2009 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
2010 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
2011 __ Bind(&done);
2012 break;
2013 }
2014
2015 case Primitive::kPrimFloat:
2016 case Primitive::kPrimDouble: {
2017 int32_t entry_point_offset;
2018 bool direct;
2019 if (in_type == Primitive::kPrimFloat) {
2020 if (instruction->IsGtBias()) {
2021 entry_point_offset = QUICK_ENTRY_POINT(pCmpgFloat);
2022 direct = IsDirectEntrypoint(kQuickCmpgFloat);
2023 } else {
2024 entry_point_offset = QUICK_ENTRY_POINT(pCmplFloat);
2025 direct = IsDirectEntrypoint(kQuickCmplFloat);
2026 }
2027 } else {
2028 if (instruction->IsGtBias()) {
2029 entry_point_offset = QUICK_ENTRY_POINT(pCmpgDouble);
2030 direct = IsDirectEntrypoint(kQuickCmpgDouble);
2031 } else {
2032 entry_point_offset = QUICK_ENTRY_POINT(pCmplDouble);
2033 direct = IsDirectEntrypoint(kQuickCmplDouble);
2034 }
2035 }
2036 codegen_->InvokeRuntime(entry_point_offset,
2037 instruction,
2038 instruction->GetDexPc(),
2039 nullptr,
2040 direct);
2041 if (in_type == Primitive::kPrimFloat) {
2042 if (instruction->IsGtBias()) {
2043 CheckEntrypointTypes<kQuickCmpgFloat, int32_t, float, float>();
2044 } else {
2045 CheckEntrypointTypes<kQuickCmplFloat, int32_t, float, float>();
2046 }
2047 } else {
2048 if (instruction->IsGtBias()) {
2049 CheckEntrypointTypes<kQuickCmpgDouble, int32_t, double, double>();
2050 } else {
2051 CheckEntrypointTypes<kQuickCmplDouble, int32_t, double, double>();
2052 }
2053 }
2054 break;
2055 }
2056
2057 default:
2058 LOG(FATAL) << "Unimplemented compare type " << in_type;
2059 }
2060}
2061
2062void LocationsBuilderMIPS::VisitCondition(HCondition* instruction) {
2063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2064 locations->SetInAt(0, Location::RequiresRegister());
2065 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2066 if (instruction->NeedsMaterialization()) {
2067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2068 }
2069}
2070
2071void InstructionCodeGeneratorMIPS::VisitCondition(HCondition* instruction) {
2072 if (!instruction->NeedsMaterialization()) {
2073 return;
2074 }
2075 // TODO: generalize to long
2076 DCHECK_NE(instruction->InputAt(0)->GetType(), Primitive::kPrimLong);
2077
2078 LocationSummary* locations = instruction->GetLocations();
2079 Register dst = locations->Out().AsRegister<Register>();
2080
2081 Register lhs = locations->InAt(0).AsRegister<Register>();
2082 Location rhs_location = locations->InAt(1);
2083
2084 Register rhs_reg = ZERO;
2085 int64_t rhs_imm = 0;
2086 bool use_imm = rhs_location.IsConstant();
2087 if (use_imm) {
2088 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2089 } else {
2090 rhs_reg = rhs_location.AsRegister<Register>();
2091 }
2092
2093 IfCondition if_cond = instruction->GetCondition();
2094
2095 switch (if_cond) {
2096 case kCondEQ:
2097 case kCondNE:
2098 if (use_imm && IsUint<16>(rhs_imm)) {
2099 __ Xori(dst, lhs, rhs_imm);
2100 } else {
2101 if (use_imm) {
2102 rhs_reg = TMP;
2103 __ LoadConst32(rhs_reg, rhs_imm);
2104 }
2105 __ Xor(dst, lhs, rhs_reg);
2106 }
2107 if (if_cond == kCondEQ) {
2108 __ Sltiu(dst, dst, 1);
2109 } else {
2110 __ Sltu(dst, ZERO, dst);
2111 }
2112 break;
2113
2114 case kCondLT:
2115 case kCondGE:
2116 if (use_imm && IsInt<16>(rhs_imm)) {
2117 __ Slti(dst, lhs, rhs_imm);
2118 } else {
2119 if (use_imm) {
2120 rhs_reg = TMP;
2121 __ LoadConst32(rhs_reg, rhs_imm);
2122 }
2123 __ Slt(dst, lhs, rhs_reg);
2124 }
2125 if (if_cond == kCondGE) {
2126 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2127 // only the slt instruction but no sge.
2128 __ Xori(dst, dst, 1);
2129 }
2130 break;
2131
2132 case kCondLE:
2133 case kCondGT:
2134 if (use_imm && IsInt<16>(rhs_imm + 1)) {
2135 // Simulate lhs <= rhs via lhs < rhs + 1.
2136 __ Slti(dst, lhs, rhs_imm + 1);
2137 if (if_cond == kCondGT) {
2138 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2139 // only the slti instruction but no sgti.
2140 __ Xori(dst, dst, 1);
2141 }
2142 } else {
2143 if (use_imm) {
2144 rhs_reg = TMP;
2145 __ LoadConst32(rhs_reg, rhs_imm);
2146 }
2147 __ Slt(dst, rhs_reg, lhs);
2148 if (if_cond == kCondLE) {
2149 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2150 // only the slt instruction but no sle.
2151 __ Xori(dst, dst, 1);
2152 }
2153 }
2154 break;
2155
2156 case kCondB:
2157 case kCondAE:
2158 // Use sltiu instruction if rhs_imm is in range [0, 32767] or in
2159 // [max_unsigned - 32767 = 0xffff8000, max_unsigned = 0xffffffff].
2160 if (use_imm &&
2161 (IsUint<15>(rhs_imm) ||
2162 IsUint<15>(rhs_imm - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(15))))) {
2163 if (IsUint<15>(rhs_imm)) {
2164 __ Sltiu(dst, lhs, rhs_imm);
2165 } else {
2166 // 16-bit value (in range [0x8000, 0xffff]) passed to sltiu is sign-extended
2167 // and then used as unsigned integer (range [0xffff8000, 0xffffffff]).
2168 __ Sltiu(dst, lhs, rhs_imm - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(16)));
2169 }
2170 } else {
2171 if (use_imm) {
2172 rhs_reg = TMP;
2173 __ LoadConst32(rhs_reg, rhs_imm);
2174 }
2175 __ Sltu(dst, lhs, rhs_reg);
2176 }
2177 if (if_cond == kCondAE) {
2178 // Simulate lhs >= rhs via !(lhs < rhs) since there's
2179 // only the sltu instruction but no sgeu.
2180 __ Xori(dst, dst, 1);
2181 }
2182 break;
2183
2184 case kCondBE:
2185 case kCondA:
2186 // Use sltiu instruction if rhs_imm is in range [0, 32766] or in
2187 // [max_unsigned - 32767 - 1 = 0xffff7fff, max_unsigned - 1 = 0xfffffffe].
2188 // lhs <= rhs is simulated via lhs < rhs + 1.
2189 if (use_imm && (rhs_imm != -1) &&
2190 (IsUint<15>(rhs_imm + 1) ||
2191 IsUint<15>(rhs_imm + 1 - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(15))))) {
2192 if (IsUint<15>(rhs_imm + 1)) {
2193 // Simulate lhs <= rhs via lhs < rhs + 1.
2194 __ Sltiu(dst, lhs, rhs_imm + 1);
2195 } else {
2196 // 16-bit value (in range [0x8000, 0xffff]) passed to sltiu is sign-extended
2197 // and then used as unsigned integer (range [0xffff8000, 0xffffffff] where rhs_imm
2198 // is in range [0xffff7fff, 0xfffffffe] since lhs <= rhs is simulated via lhs < rhs + 1).
2199 __ Sltiu(dst, lhs, rhs_imm + 1 - (MaxInt<uint64_t>(32) - MaxInt<uint64_t>(16)));
2200 }
2201 if (if_cond == kCondA) {
2202 // Simulate lhs > rhs via !(lhs <= rhs) since there's
2203 // only the sltiu instruction but no sgtiu.
2204 __ Xori(dst, dst, 1);
2205 }
2206 } else {
2207 if (use_imm) {
2208 rhs_reg = TMP;
2209 __ LoadConst32(rhs_reg, rhs_imm);
2210 }
2211 __ Sltu(dst, rhs_reg, lhs);
2212 if (if_cond == kCondBE) {
2213 // Simulate lhs <= rhs via !(rhs < lhs) since there's
2214 // only the sltu instruction but no sleu.
2215 __ Xori(dst, dst, 1);
2216 }
2217 }
2218 break;
2219 }
2220}
2221
2222void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
2223 Primitive::Type type = div->GetResultType();
2224 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
2225 ? LocationSummary::kCall
2226 : LocationSummary::kNoCall;
2227
2228 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2229
2230 switch (type) {
2231 case Primitive::kPrimInt:
2232 locations->SetInAt(0, Location::RequiresRegister());
2233 locations->SetInAt(1, Location::RequiresRegister());
2234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2235 break;
2236
2237 case Primitive::kPrimLong: {
2238 InvokeRuntimeCallingConvention calling_convention;
2239 locations->SetInAt(0, Location::RegisterPairLocation(
2240 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2241 locations->SetInAt(1, Location::RegisterPairLocation(
2242 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2243 locations->SetOut(calling_convention.GetReturnLocation(type));
2244 break;
2245 }
2246
2247 case Primitive::kPrimFloat:
2248 case Primitive::kPrimDouble:
2249 locations->SetInAt(0, Location::RequiresFpuRegister());
2250 locations->SetInAt(1, Location::RequiresFpuRegister());
2251 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected div type " << type;
2256 }
2257}
2258
2259void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
2260 Primitive::Type type = instruction->GetType();
2261 LocationSummary* locations = instruction->GetLocations();
2262 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
2263
2264 switch (type) {
2265 case Primitive::kPrimInt: {
2266 Register dst = locations->Out().AsRegister<Register>();
2267 Register lhs = locations->InAt(0).AsRegister<Register>();
2268 Register rhs = locations->InAt(1).AsRegister<Register>();
2269 if (isR6) {
2270 __ DivR6(dst, lhs, rhs);
2271 } else {
2272 __ DivR2(dst, lhs, rhs);
2273 }
2274 break;
2275 }
2276 case Primitive::kPrimLong: {
2277 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2278 instruction,
2279 instruction->GetDexPc(),
2280 nullptr,
2281 IsDirectEntrypoint(kQuickLdiv));
2282 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
2283 break;
2284 }
2285 case Primitive::kPrimFloat:
2286 case Primitive::kPrimDouble: {
2287 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2288 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2289 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2290 if (type == Primitive::kPrimFloat) {
2291 __ DivS(dst, lhs, rhs);
2292 } else {
2293 __ DivD(dst, lhs, rhs);
2294 }
2295 break;
2296 }
2297 default:
2298 LOG(FATAL) << "Unexpected div type " << type;
2299 }
2300}
2301
2302void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2303 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
2304 ? LocationSummary::kCallOnSlowPath
2305 : LocationSummary::kNoCall;
2306 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2307 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
2308 if (instruction->HasUses()) {
2309 locations->SetOut(Location::SameAsFirstInput());
2310 }
2311}
2312
2313void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
2314 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
2315 codegen_->AddSlowPath(slow_path);
2316 Location value = instruction->GetLocations()->InAt(0);
2317 Primitive::Type type = instruction->GetType();
2318
2319 switch (type) {
2320 case Primitive::kPrimByte:
2321 case Primitive::kPrimChar:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt: {
2324 if (value.IsConstant()) {
2325 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
2326 __ B(slow_path->GetEntryLabel());
2327 } else {
2328 // A division by a non-null constant is valid. We don't need to perform
2329 // any check, so simply fall through.
2330 }
2331 } else {
2332 DCHECK(value.IsRegister()) << value;
2333 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
2334 }
2335 break;
2336 }
2337 case Primitive::kPrimLong: {
2338 if (value.IsConstant()) {
2339 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
2340 __ B(slow_path->GetEntryLabel());
2341 } else {
2342 // A division by a non-null constant is valid. We don't need to perform
2343 // any check, so simply fall through.
2344 }
2345 } else {
2346 DCHECK(value.IsRegisterPair()) << value;
2347 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
2348 __ Beqz(TMP, slow_path->GetEntryLabel());
2349 }
2350 break;
2351 }
2352 default:
2353 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
2354 }
2355}
2356
2357void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
2358 LocationSummary* locations =
2359 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2360 locations->SetOut(Location::ConstantLocation(constant));
2361}
2362
2363void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
2364 // Will be generated at use site.
2365}
2366
2367void LocationsBuilderMIPS::VisitExit(HExit* exit) {
2368 exit->SetLocations(nullptr);
2369}
2370
2371void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
2372}
2373
2374void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
2375 LocationSummary* locations =
2376 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2377 locations->SetOut(Location::ConstantLocation(constant));
2378}
2379
2380void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
2381 // Will be generated at use site.
2382}
2383
2384void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
2385 got->SetLocations(nullptr);
2386}
2387
2388void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
2389 DCHECK(!successor->IsExitBlock());
2390 HBasicBlock* block = got->GetBlock();
2391 HInstruction* previous = got->GetPrevious();
2392 HLoopInformation* info = block->GetLoopInformation();
2393
2394 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
2395 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2396 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2397 return;
2398 }
2399 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2400 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2401 }
2402 if (!codegen_->GoesToNextBlock(block, successor)) {
2403 __ B(codegen_->GetLabelOf(successor));
2404 }
2405}
2406
2407void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
2408 HandleGoto(got, got->GetSuccessor());
2409}
2410
2411void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2412 try_boundary->SetLocations(nullptr);
2413}
2414
2415void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
2416 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2417 if (!successor->IsExitBlock()) {
2418 HandleGoto(try_boundary, successor);
2419 }
2420}
2421
2422void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
2423 MipsLabel* true_target,
2424 MipsLabel* false_target,
2425 MipsLabel* always_true_target) {
2426 HInstruction* cond = instruction->InputAt(0);
2427 HCondition* condition = cond->AsCondition();
2428
2429 if (cond->IsIntConstant()) {
2430 int32_t cond_value = cond->AsIntConstant()->GetValue();
2431 if (cond_value == 1) {
2432 if (always_true_target != nullptr) {
2433 __ B(always_true_target);
2434 }
2435 return;
2436 } else {
2437 DCHECK_EQ(cond_value, 0);
2438 }
2439 } else if (!cond->IsCondition() || condition->NeedsMaterialization()) {
2440 // The condition instruction has been materialized, compare the output to 0.
2441 Location cond_val = instruction->GetLocations()->InAt(0);
2442 DCHECK(cond_val.IsRegister());
2443 __ Bnez(cond_val.AsRegister<Register>(), true_target);
2444 } else {
2445 // The condition instruction has not been materialized, use its inputs as
2446 // the comparison and its condition as the branch condition.
2447 Register lhs = condition->GetLocations()->InAt(0).AsRegister<Register>();
2448 Location rhs_location = condition->GetLocations()->InAt(1);
2449 Register rhs_reg = ZERO;
2450 int32_t rhs_imm = 0;
2451 bool use_imm = rhs_location.IsConstant();
2452 if (use_imm) {
2453 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2454 } else {
2455 rhs_reg = rhs_location.AsRegister<Register>();
2456 }
2457
2458 IfCondition if_cond = condition->GetCondition();
2459 if (use_imm && rhs_imm == 0) {
2460 switch (if_cond) {
2461 case kCondEQ:
2462 __ Beqz(lhs, true_target);
2463 break;
2464 case kCondNE:
2465 __ Bnez(lhs, true_target);
2466 break;
2467 case kCondLT:
2468 __ Bltz(lhs, true_target);
2469 break;
2470 case kCondGE:
2471 __ Bgez(lhs, true_target);
2472 break;
2473 case kCondLE:
2474 __ Blez(lhs, true_target);
2475 break;
2476 case kCondGT:
2477 __ Bgtz(lhs, true_target);
2478 break;
2479 case kCondB:
2480 break; // always false
2481 case kCondBE:
2482 __ Beqz(lhs, true_target); // <= 0 if zero
2483 break;
2484 case kCondA:
2485 __ Bnez(lhs, true_target); // > 0 if non-zero
2486 break;
2487 case kCondAE:
2488 __ B(true_target); // always true
2489 break;
2490 }
2491 } else {
2492 if (use_imm) {
2493 // TODO: more efficient comparison with 16-bit constants without loading them into TMP.
2494 rhs_reg = TMP;
2495 __ LoadConst32(rhs_reg, rhs_imm);
2496 }
2497 switch (if_cond) {
2498 case kCondEQ:
2499 __ Beq(lhs, rhs_reg, true_target);
2500 break;
2501 case kCondNE:
2502 __ Bne(lhs, rhs_reg, true_target);
2503 break;
2504 case kCondLT:
2505 __ Blt(lhs, rhs_reg, true_target);
2506 break;
2507 case kCondGE:
2508 __ Bge(lhs, rhs_reg, true_target);
2509 break;
2510 case kCondLE:
2511 __ Bge(rhs_reg, lhs, true_target);
2512 break;
2513 case kCondGT:
2514 __ Blt(rhs_reg, lhs, true_target);
2515 break;
2516 case kCondB:
2517 __ Bltu(lhs, rhs_reg, true_target);
2518 break;
2519 case kCondAE:
2520 __ Bgeu(lhs, rhs_reg, true_target);
2521 break;
2522 case kCondBE:
2523 __ Bgeu(rhs_reg, lhs, true_target);
2524 break;
2525 case kCondA:
2526 __ Bltu(rhs_reg, lhs, true_target);
2527 break;
2528 }
2529 }
2530 }
2531 if (false_target != nullptr) {
2532 __ B(false_target);
2533 }
2534}
2535
2536void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
2537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2538 HInstruction* cond = if_instr->InputAt(0);
2539 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2540 locations->SetInAt(0, Location::RequiresRegister());
2541 }
2542}
2543
2544void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
2545 MipsLabel* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
2546 MipsLabel* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
2547 MipsLabel* always_true_target = true_target;
2548 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2549 if_instr->IfTrueSuccessor())) {
2550 always_true_target = nullptr;
2551 }
2552 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
2553 if_instr->IfFalseSuccessor())) {
2554 false_target = nullptr;
2555 }
2556 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
2557}
2558
2559void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
2560 LocationSummary* locations = new (GetGraph()->GetArena())
2561 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
2562 HInstruction* cond = deoptimize->InputAt(0);
2563 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
2564 locations->SetInAt(0, Location::RequiresRegister());
2565 }
2566}
2567
2568void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
2569 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
2570 DeoptimizationSlowPathMIPS(deoptimize);
2571 codegen_->AddSlowPath(slow_path);
2572 MipsLabel* slow_path_entry = slow_path->GetEntryLabel();
2573 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
2574}
2575
2576void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
2577 Primitive::Type field_type = field_info.GetFieldType();
2578 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2579 bool generate_volatile = field_info.IsVolatile() && is_wide;
2580 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2581 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2582
2583 locations->SetInAt(0, Location::RequiresRegister());
2584 if (generate_volatile) {
2585 InvokeRuntimeCallingConvention calling_convention;
2586 // need A0 to hold base + offset
2587 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2588 if (field_type == Primitive::kPrimLong) {
2589 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
2590 } else {
2591 locations->SetOut(Location::RequiresFpuRegister());
2592 // Need some temp core regs since FP results are returned in core registers
2593 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
2594 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
2595 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
2596 }
2597 } else {
2598 if (Primitive::IsFloatingPointType(instruction->GetType())) {
2599 locations->SetOut(Location::RequiresFpuRegister());
2600 } else {
2601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2602 }
2603 }
2604}
2605
2606void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
2607 const FieldInfo& field_info,
2608 uint32_t dex_pc) {
2609 Primitive::Type type = field_info.GetFieldType();
2610 LocationSummary* locations = instruction->GetLocations();
2611 Register obj = locations->InAt(0).AsRegister<Register>();
2612 LoadOperandType load_type = kLoadUnsignedByte;
2613 bool is_volatile = field_info.IsVolatile();
2614
2615 switch (type) {
2616 case Primitive::kPrimBoolean:
2617 load_type = kLoadUnsignedByte;
2618 break;
2619 case Primitive::kPrimByte:
2620 load_type = kLoadSignedByte;
2621 break;
2622 case Primitive::kPrimShort:
2623 load_type = kLoadSignedHalfword;
2624 break;
2625 case Primitive::kPrimChar:
2626 load_type = kLoadUnsignedHalfword;
2627 break;
2628 case Primitive::kPrimInt:
2629 case Primitive::kPrimFloat:
2630 case Primitive::kPrimNot:
2631 load_type = kLoadWord;
2632 break;
2633 case Primitive::kPrimLong:
2634 case Primitive::kPrimDouble:
2635 load_type = kLoadDoubleword;
2636 break;
2637 case Primitive::kPrimVoid:
2638 LOG(FATAL) << "Unreachable type " << type;
2639 UNREACHABLE();
2640 }
2641
2642 if (is_volatile && load_type == kLoadDoubleword) {
2643 InvokeRuntimeCallingConvention calling_convention;
2644 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(),
2645 obj, field_info.GetFieldOffset().Uint32Value());
2646 // Do implicit Null check
2647 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2648 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2649 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Load),
2650 instruction,
2651 dex_pc,
2652 nullptr,
2653 IsDirectEntrypoint(kQuickA64Load));
2654 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
2655 if (type == Primitive::kPrimDouble) {
2656 // Need to move to FP regs since FP results are returned in core registers.
2657 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(),
2658 locations->Out().AsFpuRegister<FRegister>());
2659 __ Mthc1(locations->GetTemp(2).AsRegister<Register>(),
2660 locations->Out().AsFpuRegister<FRegister>());
2661 }
2662 } else {
2663 if (!Primitive::IsFloatingPointType(type)) {
2664 Register dst;
2665 if (type == Primitive::kPrimLong) {
2666 DCHECK(locations->Out().IsRegisterPair());
2667 dst = locations->Out().AsRegisterPairLow<Register>();
2668 } else {
2669 DCHECK(locations->Out().IsRegister());
2670 dst = locations->Out().AsRegister<Register>();
2671 }
2672 __ LoadFromOffset(load_type, dst, obj, field_info.GetFieldOffset().Uint32Value());
2673 } else {
2674 DCHECK(locations->Out().IsFpuRegister());
2675 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2676 if (type == Primitive::kPrimFloat) {
2677 __ LoadSFromOffset(dst, obj, field_info.GetFieldOffset().Uint32Value());
2678 } else {
2679 __ LoadDFromOffset(dst, obj, field_info.GetFieldOffset().Uint32Value());
2680 }
2681 }
2682 codegen_->MaybeRecordImplicitNullCheck(instruction);
2683 }
2684
2685 if (is_volatile) {
2686 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2687 }
2688}
2689
2690void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
2691 Primitive::Type field_type = field_info.GetFieldType();
2692 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
2693 bool generate_volatile = field_info.IsVolatile() && is_wide;
2694 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2695 instruction, generate_volatile ? LocationSummary::kCall : LocationSummary::kNoCall);
2696
2697 locations->SetInAt(0, Location::RequiresRegister());
2698 if (generate_volatile) {
2699 InvokeRuntimeCallingConvention calling_convention;
2700 // need A0 to hold base + offset
2701 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2702 if (field_type == Primitive::kPrimLong) {
2703 locations->SetInAt(1, Location::RegisterPairLocation(
2704 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
2705 } else {
2706 locations->SetInAt(1, Location::RequiresFpuRegister());
2707 // Pass FP parameters in core registers.
2708 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
2709 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
2710 }
2711 } else {
2712 if (Primitive::IsFloatingPointType(field_type)) {
2713 locations->SetInAt(1, Location::RequiresFpuRegister());
2714 } else {
2715 locations->SetInAt(1, Location::RequiresRegister());
2716 }
2717 }
2718}
2719
2720void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
2721 const FieldInfo& field_info,
2722 uint32_t dex_pc) {
2723 Primitive::Type type = field_info.GetFieldType();
2724 LocationSummary* locations = instruction->GetLocations();
2725 Register obj = locations->InAt(0).AsRegister<Register>();
2726 StoreOperandType store_type = kStoreByte;
2727 bool is_volatile = field_info.IsVolatile();
2728
2729 switch (type) {
2730 case Primitive::kPrimBoolean:
2731 case Primitive::kPrimByte:
2732 store_type = kStoreByte;
2733 break;
2734 case Primitive::kPrimShort:
2735 case Primitive::kPrimChar:
2736 store_type = kStoreHalfword;
2737 break;
2738 case Primitive::kPrimInt:
2739 case Primitive::kPrimFloat:
2740 case Primitive::kPrimNot:
2741 store_type = kStoreWord;
2742 break;
2743 case Primitive::kPrimLong:
2744 case Primitive::kPrimDouble:
2745 store_type = kStoreDoubleword;
2746 break;
2747 case Primitive::kPrimVoid:
2748 LOG(FATAL) << "Unreachable type " << type;
2749 UNREACHABLE();
2750 }
2751
2752 if (is_volatile) {
2753 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
2754 }
2755
2756 if (is_volatile && store_type == kStoreDoubleword) {
2757 InvokeRuntimeCallingConvention calling_convention;
2758 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(),
2759 obj, field_info.GetFieldOffset().Uint32Value());
2760 // Do implicit Null check.
2761 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
2762 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
2763 if (type == Primitive::kPrimDouble) {
2764 // Pass FP parameters in core registers.
2765 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
2766 locations->InAt(1).AsFpuRegister<FRegister>());
2767 __ Mfhc1(locations->GetTemp(2).AsRegister<Register>(),
2768 locations->InAt(1).AsFpuRegister<FRegister>());
2769 }
2770 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pA64Store),
2771 instruction,
2772 dex_pc,
2773 nullptr,
2774 IsDirectEntrypoint(kQuickA64Store));
2775 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
2776 } else {
2777 if (!Primitive::IsFloatingPointType(type)) {
2778 Register src;
2779 if (type == Primitive::kPrimLong) {
2780 DCHECK(locations->InAt(1).IsRegisterPair());
2781 src = locations->InAt(1).AsRegisterPairLow<Register>();
2782 } else {
2783 DCHECK(locations->InAt(1).IsRegister());
2784 src = locations->InAt(1).AsRegister<Register>();
2785 }
2786 __ StoreToOffset(store_type, src, obj, field_info.GetFieldOffset().Uint32Value());
2787 } else {
2788 DCHECK(locations->InAt(1).IsFpuRegister());
2789 FRegister src = locations->InAt(1).AsFpuRegister<FRegister>();
2790 if (type == Primitive::kPrimFloat) {
2791 __ StoreSToOffset(src, obj, field_info.GetFieldOffset().Uint32Value());
2792 } else {
2793 __ StoreDToOffset(src, obj, field_info.GetFieldOffset().Uint32Value());
2794 }
2795 }
2796 codegen_->MaybeRecordImplicitNullCheck(instruction);
2797 }
2798
2799 // TODO: memory barriers?
2800 if (CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1))) {
2801 DCHECK(locations->InAt(1).IsRegister());
2802 Register src = locations->InAt(1).AsRegister<Register>();
2803 codegen_->MarkGCCard(obj, src);
2804 }
2805
2806 if (is_volatile) {
2807 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
2808 }
2809}
2810
2811void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2812 HandleFieldGet(instruction, instruction->GetFieldInfo());
2813}
2814
2815void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
2816 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
2817}
2818
2819void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2820 HandleFieldSet(instruction, instruction->GetFieldInfo());
2821}
2822
2823void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
2824 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
2825}
2826
2827void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
2828 LocationSummary::CallKind call_kind =
2829 instruction->IsExactCheck() ? LocationSummary::kNoCall : LocationSummary::kCallOnSlowPath;
2830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
2831 locations->SetInAt(0, Location::RequiresRegister());
2832 locations->SetInAt(1, Location::RequiresRegister());
2833 // The output does overlap inputs.
2834 // Note that TypeCheckSlowPathMIPS uses this register too.
2835 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2836}
2837
2838void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
2839 LocationSummary* locations = instruction->GetLocations();
2840 Register obj = locations->InAt(0).AsRegister<Register>();
2841 Register cls = locations->InAt(1).AsRegister<Register>();
2842 Register out = locations->Out().AsRegister<Register>();
2843
2844 MipsLabel done;
2845
2846 // Return 0 if `obj` is null.
2847 // TODO: Avoid this check if we know `obj` is not null.
2848 __ Move(out, ZERO);
2849 __ Beqz(obj, &done);
2850
2851 // Compare the class of `obj` with `cls`.
2852 __ LoadFromOffset(kLoadWord, out, obj, mirror::Object::ClassOffset().Int32Value());
2853 if (instruction->IsExactCheck()) {
2854 // Classes must be equal for the instanceof to succeed.
2855 __ Xor(out, out, cls);
2856 __ Sltiu(out, out, 1);
2857 } else {
2858 // If the classes are not equal, we go into a slow path.
2859 DCHECK(locations->OnlyCallsOnSlowPath());
2860 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction);
2861 codegen_->AddSlowPath(slow_path);
2862 __ Bne(out, cls, slow_path->GetEntryLabel());
2863 __ LoadConst32(out, 1);
2864 __ Bind(slow_path->GetExitLabel());
2865 }
2866
2867 __ Bind(&done);
2868}
2869
2870void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
2871 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2872 locations->SetOut(Location::ConstantLocation(constant));
2873}
2874
2875void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
2876 // Will be generated at use site.
2877}
2878
2879void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
2880 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
2881 locations->SetOut(Location::ConstantLocation(constant));
2882}
2883
2884void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
2885 // Will be generated at use site.
2886}
2887
2888void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
2889 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
2890 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2891}
2892
2893void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
2894 HandleInvoke(invoke);
2895 // The register T0 is required to be used for the hidden argument in
2896 // art_quick_imt_conflict_trampoline, so add the hidden argument.
2897 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T0));
2898}
2899
2900void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
2901 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
2902 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
2903 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2904 invoke->GetImtIndex() % mirror::Class::kImtSize, kMipsPointerSize).Uint32Value();
2905 Location receiver = invoke->GetLocations()->InAt(0);
2906 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2907 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
2908
2909 // Set the hidden argument.
2910 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
2911 invoke->GetDexMethodIndex());
2912
2913 // temp = object->GetClass();
2914 if (receiver.IsStackSlot()) {
2915 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
2916 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2917 } else {
2918 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
2919 }
2920 codegen_->MaybeRecordImplicitNullCheck(invoke);
2921 // temp = temp->GetImtEntryAt(method_offset);
2922 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
2923 // T9 = temp->GetEntryPoint();
2924 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
2925 // T9();
2926 __ Jalr(T9);
2927 __ Nop();
2928 DCHECK(!codegen_->IsLeafMethod());
2929 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2930}
2931
2932void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
2933 // TODO: intrinsic function.
2934 HandleInvoke(invoke);
2935}
2936
2937void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
2938 // When we do not run baseline, explicit clinit checks triggered by static
2939 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2940 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
2941
2942 // TODO: intrinsic function.
2943 HandleInvoke(invoke);
2944}
2945
2946static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen ATTRIBUTE_UNUSED) {
2947 if (invoke->GetLocations()->Intrinsified()) {
2948 // TODO: intrinsic function.
2949 return true;
2950 }
2951 return false;
2952}
2953
2954void CodeGeneratorMIPS::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
2955 // All registers are assumed to be correctly set up per the calling convention.
2956
2957 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
2958 switch (invoke->GetMethodLoadKind()) {
2959 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
2960 // temp = thread->string_init_entrypoint
2961 __ LoadFromOffset(kLoadWord,
2962 temp.AsRegister<Register>(),
2963 TR,
2964 invoke->GetStringInitOffset());
2965 break;
2966 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
2967 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2968 break;
2969 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
2970 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
2971 break;
2972 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
2973 // TODO: Implement this type. (Needs literal support.) At the moment, the
2974 // CompilerDriver will not direct the backend to use this type for MIPS.
2975 LOG(FATAL) << "Unsupported!";
2976 UNREACHABLE();
2977 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
2978 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
2979 FALLTHROUGH_INTENDED;
2980 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
2981 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
2982 Register reg = temp.AsRegister<Register>();
2983 Register method_reg;
2984 if (current_method.IsRegister()) {
2985 method_reg = current_method.AsRegister<Register>();
2986 } else {
2987 // TODO: use the appropriate DCHECK() here if possible.
2988 // DCHECK(invoke->GetLocations()->Intrinsified());
2989 DCHECK(!current_method.IsValid());
2990 method_reg = reg;
2991 __ Lw(reg, SP, kCurrentMethodStackOffset);
2992 }
2993
2994 // temp = temp->dex_cache_resolved_methods_;
2995 __ LoadFromOffset(kLoadWord,
2996 reg,
2997 method_reg,
2998 ArtMethod::DexCacheResolvedMethodsOffset(kMipsPointerSize).Int32Value());
2999 // temp = temp[index_in_cache]
3000 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3001 __ LoadFromOffset(kLoadWord,
3002 reg,
3003 reg,
3004 CodeGenerator::GetCachePointerOffset(index_in_cache));
3005 break;
3006 }
3007 }
3008
3009 switch (invoke->GetCodePtrLocation()) {
3010 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3011 __ Jalr(&frame_entry_label_, T9);
3012 break;
3013 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3014 // LR = invoke->GetDirectCodePtr();
3015 __ LoadConst32(T9, invoke->GetDirectCodePtr());
3016 // LR()
3017 __ Jalr(T9);
3018 __ Nop();
3019 break;
3020 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
3021 // TODO: Implement kCallPCRelative. For the moment, we fall back to kMethodCode.
3022 FALLTHROUGH_INTENDED;
3023 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3024 // TODO: Implement kDirectCodeFixup. For the moment, we fall back to kMethodCode.
3025 FALLTHROUGH_INTENDED;
3026 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3027 // T9 = callee_method->entry_point_from_quick_compiled_code_;
3028 __ LoadFromOffset(kLoadDoubleword,
3029 T9,
3030 callee_method.AsRegister<Register>(),
3031 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3032 kMipsWordSize).Int32Value());
3033 // T9()
3034 __ Jalr(T9);
3035 __ Nop();
3036 break;
3037 }
3038 DCHECK(!IsLeafMethod());
3039}
3040
3041void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
3042 // When we do not run baseline, explicit clinit checks triggered by static
3043 // invokes must have been pruned by art::PrepareForRegisterAllocation.
3044 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
3045
3046 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3047 return;
3048 }
3049
3050 LocationSummary* locations = invoke->GetLocations();
3051 codegen_->GenerateStaticOrDirectCall(invoke,
3052 locations->HasTemps()
3053 ? locations->GetTemp(0)
3054 : Location::NoLocation());
3055 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3056}
3057
3058void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
3059 // TODO: Try to generate intrinsics code.
3060 LocationSummary* locations = invoke->GetLocations();
3061 Location receiver = locations->InAt(0);
3062 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
3063 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3064 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
3065 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3066 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsWordSize);
3067
3068 // temp = object->GetClass();
3069 if (receiver.IsStackSlot()) {
3070 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
3071 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3072 } else {
3073 DCHECK(receiver.IsRegister());
3074 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
3075 }
3076 codegen_->MaybeRecordImplicitNullCheck(invoke);
3077 // temp = temp->GetMethodAt(method_offset);
3078 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
3079 // T9 = temp->GetEntryPoint();
3080 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
3081 // T9();
3082 __ Jalr(T9);
3083 __ Nop();
3084 DCHECK(!codegen_->IsLeafMethod());
3085 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3086}
3087
3088void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
3089 LocationSummary::CallKind call_kind = cls->CanCallRuntime() ? LocationSummary::kCallOnSlowPath
3090 : LocationSummary::kNoCall;
3091 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
3092 locations->SetInAt(0, Location::RequiresRegister());
3093 locations->SetOut(Location::RequiresRegister());
3094}
3095
3096void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) {
3097 LocationSummary* locations = cls->GetLocations();
3098 Register out = locations->Out().AsRegister<Register>();
3099 Register current_method = locations->InAt(0).AsRegister<Register>();
3100 if (cls->IsReferrersClass()) {
3101 DCHECK(!cls->CanCallRuntime());
3102 DCHECK(!cls->MustGenerateClinitCheck());
3103 __ LoadFromOffset(kLoadWord, out, current_method,
3104 ArtMethod::DeclaringClassOffset().Int32Value());
3105 } else {
3106 DCHECK(cls->CanCallRuntime());
3107 __ LoadFromOffset(kLoadWord, out, current_method,
3108 ArtMethod::DexCacheResolvedTypesOffset(kMipsPointerSize).Int32Value());
3109 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
3110 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3111 cls,
3112 cls,
3113 cls->GetDexPc(),
3114 cls->MustGenerateClinitCheck());
3115 codegen_->AddSlowPath(slow_path);
3116 __ Beqz(out, slow_path->GetEntryLabel());
3117 if (cls->MustGenerateClinitCheck()) {
3118 GenerateClassInitializationCheck(slow_path, out);
3119 } else {
3120 __ Bind(slow_path->GetExitLabel());
3121 }
3122 }
3123}
3124
3125static int32_t GetExceptionTlsOffset() {
3126 return Thread::ExceptionOffset<kMipsWordSize>().Int32Value();
3127}
3128
3129void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
3130 LocationSummary* locations =
3131 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
3132 locations->SetOut(Location::RequiresRegister());
3133}
3134
3135void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
3136 Register out = load->GetLocations()->Out().AsRegister<Register>();
3137 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
3138}
3139
3140void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
3141 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
3142}
3143
3144void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
3145 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
3146}
3147
3148void LocationsBuilderMIPS::VisitLoadLocal(HLoadLocal* load) {
3149 load->SetLocations(nullptr);
3150}
3151
3152void InstructionCodeGeneratorMIPS::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
3153 // Nothing to do, this is driven by the code generator.
3154}
3155
3156void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
3157 LocationSummary* locations =
3158 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
3159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetOut(Location::RequiresRegister());
3161}
3162
3163void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) {
3164 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
3165 codegen_->AddSlowPath(slow_path);
3166
3167 LocationSummary* locations = load->GetLocations();
3168 Register out = locations->Out().AsRegister<Register>();
3169 Register current_method = locations->InAt(0).AsRegister<Register>();
3170 __ LoadFromOffset(kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
3171 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value());
3172 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
3173 __ Beqz(out, slow_path->GetEntryLabel());
3174 __ Bind(slow_path->GetExitLabel());
3175}
3176
3177void LocationsBuilderMIPS::VisitLocal(HLocal* local) {
3178 local->SetLocations(nullptr);
3179}
3180
3181void InstructionCodeGeneratorMIPS::VisitLocal(HLocal* local) {
3182 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
3183}
3184
3185void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
3186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
3187 locations->SetOut(Location::ConstantLocation(constant));
3188}
3189
3190void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
3191 // Will be generated at use site.
3192}
3193
3194void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3195 LocationSummary* locations =
3196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3197 InvokeRuntimeCallingConvention calling_convention;
3198 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3199}
3200
3201void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
3202 if (instruction->IsEnter()) {
3203 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLockObject),
3204 instruction,
3205 instruction->GetDexPc(),
3206 nullptr,
3207 IsDirectEntrypoint(kQuickLockObject));
3208 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
3209 } else {
3210 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pUnlockObject),
3211 instruction,
3212 instruction->GetDexPc(),
3213 nullptr,
3214 IsDirectEntrypoint(kQuickUnlockObject));
3215 }
3216 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
3217}
3218
3219void LocationsBuilderMIPS::VisitMul(HMul* mul) {
3220 LocationSummary* locations =
3221 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3222 switch (mul->GetResultType()) {
3223 case Primitive::kPrimInt:
3224 case Primitive::kPrimLong:
3225 locations->SetInAt(0, Location::RequiresRegister());
3226 locations->SetInAt(1, Location::RequiresRegister());
3227 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3228 break;
3229
3230 case Primitive::kPrimFloat:
3231 case Primitive::kPrimDouble:
3232 locations->SetInAt(0, Location::RequiresFpuRegister());
3233 locations->SetInAt(1, Location::RequiresFpuRegister());
3234 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3235 break;
3236
3237 default:
3238 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
3239 }
3240}
3241
3242void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
3243 Primitive::Type type = instruction->GetType();
3244 LocationSummary* locations = instruction->GetLocations();
3245 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3246
3247 switch (type) {
3248 case Primitive::kPrimInt: {
3249 Register dst = locations->Out().AsRegister<Register>();
3250 Register lhs = locations->InAt(0).AsRegister<Register>();
3251 Register rhs = locations->InAt(1).AsRegister<Register>();
3252
3253 if (isR6) {
3254 __ MulR6(dst, lhs, rhs);
3255 } else {
3256 __ MulR2(dst, lhs, rhs);
3257 }
3258 break;
3259 }
3260 case Primitive::kPrimLong: {
3261 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3262 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3263 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3264 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3265 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3266 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3267
3268 // Extra checks to protect caused by the existance of A1_A2.
3269 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
3270 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
3271 DCHECK_NE(dst_high, lhs_low);
3272 DCHECK_NE(dst_high, rhs_low);
3273
3274 // A_B * C_D
3275 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
3276 // dst_lo: [ low(B*D) ]
3277 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
3278
3279 if (isR6) {
3280 __ MulR6(TMP, lhs_high, rhs_low);
3281 __ MulR6(dst_high, lhs_low, rhs_high);
3282 __ Addu(dst_high, dst_high, TMP);
3283 __ MuhuR6(TMP, lhs_low, rhs_low);
3284 __ Addu(dst_high, dst_high, TMP);
3285 __ MulR6(dst_low, lhs_low, rhs_low);
3286 } else {
3287 __ MulR2(TMP, lhs_high, rhs_low);
3288 __ MulR2(dst_high, lhs_low, rhs_high);
3289 __ Addu(dst_high, dst_high, TMP);
3290 __ MultuR2(lhs_low, rhs_low);
3291 __ Mfhi(TMP);
3292 __ Addu(dst_high, dst_high, TMP);
3293 __ Mflo(dst_low);
3294 }
3295 break;
3296 }
3297 case Primitive::kPrimFloat:
3298 case Primitive::kPrimDouble: {
3299 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3300 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3301 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3302 if (type == Primitive::kPrimFloat) {
3303 __ MulS(dst, lhs, rhs);
3304 } else {
3305 __ MulD(dst, lhs, rhs);
3306 }
3307 break;
3308 }
3309 default:
3310 LOG(FATAL) << "Unexpected mul type " << type;
3311 }
3312}
3313
3314void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
3315 LocationSummary* locations =
3316 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3317 switch (neg->GetResultType()) {
3318 case Primitive::kPrimInt:
3319 case Primitive::kPrimLong:
3320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3322 break;
3323
3324 case Primitive::kPrimFloat:
3325 case Primitive::kPrimDouble:
3326 locations->SetInAt(0, Location::RequiresFpuRegister());
3327 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3328 break;
3329
3330 default:
3331 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3332 }
3333}
3334
3335void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
3336 Primitive::Type type = instruction->GetType();
3337 LocationSummary* locations = instruction->GetLocations();
3338
3339 switch (type) {
3340 case Primitive::kPrimInt: {
3341 Register dst = locations->Out().AsRegister<Register>();
3342 Register src = locations->InAt(0).AsRegister<Register>();
3343 __ Subu(dst, ZERO, src);
3344 break;
3345 }
3346 case Primitive::kPrimLong: {
3347 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3348 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3349 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3350 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3351 __ Subu(dst_low, ZERO, src_low);
3352 __ Sltu(TMP, ZERO, dst_low);
3353 __ Subu(dst_high, ZERO, src_high);
3354 __ Subu(dst_high, dst_high, TMP);
3355 break;
3356 }
3357 case Primitive::kPrimFloat:
3358 case Primitive::kPrimDouble: {
3359 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3360 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
3361 if (type == Primitive::kPrimFloat) {
3362 __ NegS(dst, src);
3363 } else {
3364 __ NegD(dst, src);
3365 }
3366 break;
3367 }
3368 default:
3369 LOG(FATAL) << "Unexpected neg type " << type;
3370 }
3371}
3372
3373void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
3374 LocationSummary* locations =
3375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3376 InvokeRuntimeCallingConvention calling_convention;
3377 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3378 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
3379 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3380 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3381}
3382
3383void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
3384 InvokeRuntimeCallingConvention calling_convention;
3385 Register current_method_register = calling_convention.GetRegisterAt(2);
3386 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
3387 // Move an uint16_t value to a register.
3388 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3389 codegen_->InvokeRuntime(
3390 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3391 instruction,
3392 instruction->GetDexPc(),
3393 nullptr,
3394 IsDirectEntrypoint(kQuickAllocArrayWithAccessCheck));
3395 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck,
3396 void*, uint32_t, int32_t, ArtMethod*>();
3397}
3398
3399void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
3400 LocationSummary* locations =
3401 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3402 InvokeRuntimeCallingConvention calling_convention;
3403 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3404 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3405 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
3406}
3407
3408void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
3409 InvokeRuntimeCallingConvention calling_convention;
3410 Register current_method_register = calling_convention.GetRegisterAt(1);
3411 __ Lw(current_method_register, SP, kCurrentMethodStackOffset);
3412 // Move an uint16_t value to a register.
3413 __ LoadConst32(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
3414 codegen_->InvokeRuntime(
3415 GetThreadOffset<kMipsWordSize>(instruction->GetEntrypoint()).Int32Value(),
3416 instruction,
3417 instruction->GetDexPc(),
3418 nullptr,
3419 IsDirectEntrypoint(kQuickAllocObjectWithAccessCheck));
3420 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3421}
3422
3423void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
3424 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3425 locations->SetInAt(0, Location::RequiresRegister());
3426 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3427}
3428
3429void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
3430 Primitive::Type type = instruction->GetType();
3431 LocationSummary* locations = instruction->GetLocations();
3432
3433 switch (type) {
3434 case Primitive::kPrimInt: {
3435 Register dst = locations->Out().AsRegister<Register>();
3436 Register src = locations->InAt(0).AsRegister<Register>();
3437 __ Nor(dst, src, ZERO);
3438 break;
3439 }
3440
3441 case Primitive::kPrimLong: {
3442 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3443 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3444 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3445 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
3446 __ Nor(dst_high, src_high, ZERO);
3447 __ Nor(dst_low, src_low, ZERO);
3448 break;
3449 }
3450
3451 default:
3452 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
3453 }
3454}
3455
3456void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3457 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3460}
3461
3462void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
3463 LocationSummary* locations = instruction->GetLocations();
3464 __ Xori(locations->Out().AsRegister<Register>(),
3465 locations->InAt(0).AsRegister<Register>(),
3466 1);
3467}
3468
3469void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
3470 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3471 ? LocationSummary::kCallOnSlowPath
3472 : LocationSummary::kNoCall;
3473 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
3474 locations->SetInAt(0, Location::RequiresRegister());
3475 if (instruction->HasUses()) {
3476 locations->SetOut(Location::SameAsFirstInput());
3477 }
3478}
3479
3480void InstructionCodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
3481 if (codegen_->CanMoveNullCheckToUser(instruction)) {
3482 return;
3483 }
3484 Location obj = instruction->GetLocations()->InAt(0);
3485
3486 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
3487 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3488}
3489
3490void InstructionCodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
3491 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
3492 codegen_->AddSlowPath(slow_path);
3493
3494 Location obj = instruction->GetLocations()->InAt(0);
3495
3496 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
3497}
3498
3499void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
3500 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
3501 GenerateImplicitNullCheck(instruction);
3502 } else {
3503 GenerateExplicitNullCheck(instruction);
3504 }
3505}
3506
3507void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
3508 HandleBinaryOp(instruction);
3509}
3510
3511void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
3512 HandleBinaryOp(instruction);
3513}
3514
3515void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
3516 LOG(FATAL) << "Unreachable";
3517}
3518
3519void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
3520 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
3521}
3522
3523void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
3524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3525 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3526 if (location.IsStackSlot()) {
3527 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3528 } else if (location.IsDoubleStackSlot()) {
3529 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3530 }
3531 locations->SetOut(location);
3532}
3533
3534void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
3535 ATTRIBUTE_UNUSED) {
3536 // Nothing to do, the parameter is already at its location.
3537}
3538
3539void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
3540 LocationSummary* locations =
3541 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3542 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3543}
3544
3545void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
3546 ATTRIBUTE_UNUSED) {
3547 // Nothing to do, the method is already at its location.
3548}
3549
3550void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
3551 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
3552 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3553 locations->SetInAt(i, Location::Any());
3554 }
3555 locations->SetOut(Location::Any());
3556}
3557
3558void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
3559 LOG(FATAL) << "Unreachable";
3560}
3561
3562void LocationsBuilderMIPS::VisitRem(HRem* rem) {
3563 Primitive::Type type = rem->GetResultType();
3564 LocationSummary::CallKind call_kind =
3565 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCall;
3566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3567
3568 switch (type) {
3569 case Primitive::kPrimInt:
3570 locations->SetInAt(0, Location::RequiresRegister());
3571 locations->SetInAt(1, Location::RequiresRegister());
3572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3573 break;
3574
3575 case Primitive::kPrimLong: {
3576 InvokeRuntimeCallingConvention calling_convention;
3577 locations->SetInAt(0, Location::RegisterPairLocation(
3578 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3579 locations->SetInAt(1, Location::RegisterPairLocation(
3580 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3581 locations->SetOut(calling_convention.GetReturnLocation(type));
3582 break;
3583 }
3584
3585 case Primitive::kPrimFloat:
3586 case Primitive::kPrimDouble: {
3587 InvokeRuntimeCallingConvention calling_convention;
3588 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3589 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3590 locations->SetOut(calling_convention.GetReturnLocation(type));
3591 break;
3592 }
3593
3594 default:
3595 LOG(FATAL) << "Unexpected rem type " << type;
3596 }
3597}
3598
3599void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
3600 Primitive::Type type = instruction->GetType();
3601 LocationSummary* locations = instruction->GetLocations();
3602 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3603
3604 switch (type) {
3605 case Primitive::kPrimInt: {
3606 Register dst = locations->Out().AsRegister<Register>();
3607 Register lhs = locations->InAt(0).AsRegister<Register>();
3608 Register rhs = locations->InAt(1).AsRegister<Register>();
3609 if (isR6) {
3610 __ ModR6(dst, lhs, rhs);
3611 } else {
3612 __ ModR2(dst, lhs, rhs);
3613 }
3614 break;
3615 }
3616 case Primitive::kPrimLong: {
3617 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3618 instruction,
3619 instruction->GetDexPc(),
3620 nullptr,
3621 IsDirectEntrypoint(kQuickLmod));
3622 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
3623 break;
3624 }
3625 case Primitive::kPrimFloat: {
3626 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf),
3627 instruction, instruction->GetDexPc(),
3628 nullptr,
3629 IsDirectEntrypoint(kQuickFmodf));
3630 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3631 break;
3632 }
3633 case Primitive::kPrimDouble: {
3634 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod),
3635 instruction, instruction->GetDexPc(),
3636 nullptr,
3637 IsDirectEntrypoint(kQuickFmod));
3638 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
3639 break;
3640 }
3641 default:
3642 LOG(FATAL) << "Unexpected rem type " << type;
3643 }
3644}
3645
3646void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3647 memory_barrier->SetLocations(nullptr);
3648}
3649
3650void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3651 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
3652}
3653
3654void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
3655 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
3656 Primitive::Type return_type = ret->InputAt(0)->GetType();
3657 locations->SetInAt(0, MipsReturnLocation(return_type));
3658}
3659
3660void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
3661 codegen_->GenerateFrameExit();
3662}
3663
3664void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
3665 ret->SetLocations(nullptr);
3666}
3667
3668void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
3669 codegen_->GenerateFrameExit();
3670}
3671
3672void LocationsBuilderMIPS::VisitShl(HShl* shl) {
3673 HandleShift(shl);
3674}
3675
3676void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
3677 HandleShift(shl);
3678}
3679
3680void LocationsBuilderMIPS::VisitShr(HShr* shr) {
3681 HandleShift(shr);
3682}
3683
3684void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
3685 HandleShift(shr);
3686}
3687
3688void LocationsBuilderMIPS::VisitStoreLocal(HStoreLocal* store) {
3689 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(store);
3690 Primitive::Type field_type = store->InputAt(1)->GetType();
3691 switch (field_type) {
3692 case Primitive::kPrimNot:
3693 case Primitive::kPrimBoolean:
3694 case Primitive::kPrimByte:
3695 case Primitive::kPrimChar:
3696 case Primitive::kPrimShort:
3697 case Primitive::kPrimInt:
3698 case Primitive::kPrimFloat:
3699 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
3700 break;
3701
3702 case Primitive::kPrimLong:
3703 case Primitive::kPrimDouble:
3704 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
3705 break;
3706
3707 default:
3708 LOG(FATAL) << "Unimplemented local type " << field_type;
3709 }
3710}
3711
3712void InstructionCodeGeneratorMIPS::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
3713}
3714
3715void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
3716 HandleBinaryOp(instruction);
3717}
3718
3719void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
3720 HandleBinaryOp(instruction);
3721}
3722
3723void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3724 HandleFieldGet(instruction, instruction->GetFieldInfo());
3725}
3726
3727void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
3728 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3729}
3730
3731void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3732 HandleFieldSet(instruction, instruction->GetFieldInfo());
3733}
3734
3735void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
3736 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
3737}
3738
3739void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
3740 HUnresolvedInstanceFieldGet* instruction) {
3741 FieldAccessCallingConventionMIPS calling_convention;
3742 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3743 instruction->GetFieldType(),
3744 calling_convention);
3745}
3746
3747void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
3748 HUnresolvedInstanceFieldGet* instruction) {
3749 FieldAccessCallingConventionMIPS calling_convention;
3750 codegen_->GenerateUnresolvedFieldAccess(instruction,
3751 instruction->GetFieldType(),
3752 instruction->GetFieldIndex(),
3753 instruction->GetDexPc(),
3754 calling_convention);
3755}
3756
3757void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
3758 HUnresolvedInstanceFieldSet* instruction) {
3759 FieldAccessCallingConventionMIPS calling_convention;
3760 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3761 instruction->GetFieldType(),
3762 calling_convention);
3763}
3764
3765void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
3766 HUnresolvedInstanceFieldSet* instruction) {
3767 FieldAccessCallingConventionMIPS calling_convention;
3768 codegen_->GenerateUnresolvedFieldAccess(instruction,
3769 instruction->GetFieldType(),
3770 instruction->GetFieldIndex(),
3771 instruction->GetDexPc(),
3772 calling_convention);
3773}
3774
3775void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
3776 HUnresolvedStaticFieldGet* instruction) {
3777 FieldAccessCallingConventionMIPS calling_convention;
3778 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3779 instruction->GetFieldType(),
3780 calling_convention);
3781}
3782
3783void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
3784 HUnresolvedStaticFieldGet* instruction) {
3785 FieldAccessCallingConventionMIPS calling_convention;
3786 codegen_->GenerateUnresolvedFieldAccess(instruction,
3787 instruction->GetFieldType(),
3788 instruction->GetFieldIndex(),
3789 instruction->GetDexPc(),
3790 calling_convention);
3791}
3792
3793void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
3794 HUnresolvedStaticFieldSet* instruction) {
3795 FieldAccessCallingConventionMIPS calling_convention;
3796 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
3797 instruction->GetFieldType(),
3798 calling_convention);
3799}
3800
3801void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
3802 HUnresolvedStaticFieldSet* instruction) {
3803 FieldAccessCallingConventionMIPS calling_convention;
3804 codegen_->GenerateUnresolvedFieldAccess(instruction,
3805 instruction->GetFieldType(),
3806 instruction->GetFieldIndex(),
3807 instruction->GetDexPc(),
3808 calling_convention);
3809}
3810
3811void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
3812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
3813}
3814
3815void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
3816 HBasicBlock* block = instruction->GetBlock();
3817 if (block->GetLoopInformation() != nullptr) {
3818 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
3819 // The back edge will generate the suspend check.
3820 return;
3821 }
3822 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
3823 // The goto will generate the suspend check.
3824 return;
3825 }
3826 GenerateSuspendCheck(instruction, nullptr);
3827}
3828
3829void LocationsBuilderMIPS::VisitTemporary(HTemporary* temp) {
3830 temp->SetLocations(nullptr);
3831}
3832
3833void InstructionCodeGeneratorMIPS::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
3834 // Nothing to do, this is driven by the code generator.
3835}
3836
3837void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
3838 LocationSummary* locations =
3839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3840 InvokeRuntimeCallingConvention calling_convention;
3841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3842}
3843
3844void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
3845 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
3846 instruction,
3847 instruction->GetDexPc(),
3848 nullptr,
3849 IsDirectEntrypoint(kQuickDeliverException));
3850 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
3851}
3852
3853void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
3854 Primitive::Type input_type = conversion->GetInputType();
3855 Primitive::Type result_type = conversion->GetResultType();
3856 DCHECK_NE(input_type, result_type);
3857
3858 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
3859 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
3860 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
3861 }
3862
3863 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3864 if ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
3865 (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type))) {
3866 call_kind = LocationSummary::kCall;
3867 }
3868
3869 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3870
3871 if (call_kind == LocationSummary::kNoCall) {
3872 if (Primitive::IsFloatingPointType(input_type)) {
3873 locations->SetInAt(0, Location::RequiresFpuRegister());
3874 } else {
3875 locations->SetInAt(0, Location::RequiresRegister());
3876 }
3877
3878 if (Primitive::IsFloatingPointType(result_type)) {
3879 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3880 } else {
3881 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3882 }
3883 } else {
3884 InvokeRuntimeCallingConvention calling_convention;
3885
3886 if (Primitive::IsFloatingPointType(input_type)) {
3887 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3888 } else {
3889 DCHECK_EQ(input_type, Primitive::kPrimLong);
3890 locations->SetInAt(0, Location::RegisterPairLocation(
3891 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3892 }
3893
3894 locations->SetOut(calling_convention.GetReturnLocation(result_type));
3895 }
3896}
3897
3898void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
3899 LocationSummary* locations = conversion->GetLocations();
3900 Primitive::Type result_type = conversion->GetResultType();
3901 Primitive::Type input_type = conversion->GetInputType();
3902 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3903
3904 DCHECK_NE(input_type, result_type);
3905
3906 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
3907 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
3908 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
3909 Register src = locations->InAt(0).AsRegister<Register>();
3910
3911 __ Move(dst_low, src);
3912 __ Sra(dst_high, src, 31);
3913 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
3914 Register dst = locations->Out().AsRegister<Register>();
3915 Register src = (input_type == Primitive::kPrimLong)
3916 ? locations->InAt(0).AsRegisterPairLow<Register>()
3917 : locations->InAt(0).AsRegister<Register>();
3918
3919 switch (result_type) {
3920 case Primitive::kPrimChar:
3921 __ Andi(dst, src, 0xFFFF);
3922 break;
3923 case Primitive::kPrimByte:
3924 if (has_sign_extension) {
3925 __ Seb(dst, src);
3926 } else {
3927 __ Sll(dst, src, 24);
3928 __ Sra(dst, dst, 24);
3929 }
3930 break;
3931 case Primitive::kPrimShort:
3932 if (has_sign_extension) {
3933 __ Seh(dst, src);
3934 } else {
3935 __ Sll(dst, src, 16);
3936 __ Sra(dst, dst, 16);
3937 }
3938 break;
3939 case Primitive::kPrimInt:
3940 __ Move(dst, src);
3941 break;
3942
3943 default:
3944 LOG(FATAL) << "Unexpected type conversion from " << input_type
3945 << " to " << result_type;
3946 }
3947 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
3948 if (input_type != Primitive::kPrimLong) {
3949 Register src = locations->InAt(0).AsRegister<Register>();
3950 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3951 __ Mtc1(src, FTMP);
3952 if (result_type == Primitive::kPrimFloat) {
3953 __ Cvtsw(dst, FTMP);
3954 } else {
3955 __ Cvtdw(dst, FTMP);
3956 }
3957 } else {
3958 int32_t entry_offset = (result_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pL2f)
3959 : QUICK_ENTRY_POINT(pL2d);
3960 bool direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickL2f)
3961 : IsDirectEntrypoint(kQuickL2d);
3962 codegen_->InvokeRuntime(entry_offset,
3963 conversion,
3964 conversion->GetDexPc(),
3965 nullptr,
3966 direct);
3967 if (result_type == Primitive::kPrimFloat) {
3968 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
3969 } else {
3970 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
3971 }
3972 }
3973 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
3974 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
3975 int32_t entry_offset;
3976 bool direct;
3977 if (result_type != Primitive::kPrimLong) {
3978 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2iz)
3979 : QUICK_ENTRY_POINT(pD2iz);
3980 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2iz)
3981 : IsDirectEntrypoint(kQuickD2iz);
3982 } else {
3983 entry_offset = (input_type == Primitive::kPrimFloat) ? QUICK_ENTRY_POINT(pF2l)
3984 : QUICK_ENTRY_POINT(pD2l);
3985 direct = (result_type == Primitive::kPrimFloat) ? IsDirectEntrypoint(kQuickF2l)
3986 : IsDirectEntrypoint(kQuickD2l);
3987 }
3988 codegen_->InvokeRuntime(entry_offset,
3989 conversion,
3990 conversion->GetDexPc(),
3991 nullptr,
3992 direct);
3993 if (result_type != Primitive::kPrimLong) {
3994 if (input_type == Primitive::kPrimFloat) {
3995 CheckEntrypointTypes<kQuickF2iz, int32_t, float>();
3996 } else {
3997 CheckEntrypointTypes<kQuickD2iz, int32_t, double>();
3998 }
3999 } else {
4000 if (input_type == Primitive::kPrimFloat) {
4001 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
4002 } else {
4003 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
4004 }
4005 }
4006 } else if (Primitive::IsFloatingPointType(result_type) &&
4007 Primitive::IsFloatingPointType(input_type)) {
4008 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4009 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
4010 if (result_type == Primitive::kPrimFloat) {
4011 __ Cvtsd(dst, src);
4012 } else {
4013 __ Cvtds(dst, src);
4014 }
4015 } else {
4016 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
4017 << " to " << result_type;
4018 }
4019}
4020
4021void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
4022 HandleShift(ushr);
4023}
4024
4025void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
4026 HandleShift(ushr);
4027}
4028
4029void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
4030 HandleBinaryOp(instruction);
4031}
4032
4033void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
4034 HandleBinaryOp(instruction);
4035}
4036
4037void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4038 // Nothing to do, this should be removed during prepare for register allocator.
4039 LOG(FATAL) << "Unreachable";
4040}
4041
4042void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
4043 // Nothing to do, this should be removed during prepare for register allocator.
4044 LOG(FATAL) << "Unreachable";
4045}
4046
4047void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
4048 VisitCondition(comp);
4049}
4050
4051void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
4052 VisitCondition(comp);
4053}
4054
4055void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
4056 VisitCondition(comp);
4057}
4058
4059void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
4060 VisitCondition(comp);
4061}
4062
4063void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
4064 VisitCondition(comp);
4065}
4066
4067void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
4068 VisitCondition(comp);
4069}
4070
4071void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4072 VisitCondition(comp);
4073}
4074
4075void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
4076 VisitCondition(comp);
4077}
4078
4079void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
4080 VisitCondition(comp);
4081}
4082
4083void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
4084 VisitCondition(comp);
4085}
4086
4087void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4088 VisitCondition(comp);
4089}
4090
4091void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
4092 VisitCondition(comp);
4093}
4094
4095void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
4096 VisitCondition(comp);
4097}
4098
4099void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
4100 VisitCondition(comp);
4101}
4102
4103void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4104 VisitCondition(comp);
4105}
4106
4107void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
4108 VisitCondition(comp);
4109}
4110
4111void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
4112 VisitCondition(comp);
4113}
4114
4115void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
4116 VisitCondition(comp);
4117}
4118
4119void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4120 VisitCondition(comp);
4121}
4122
4123void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
4124 VisitCondition(comp);
4125}
4126
4127void LocationsBuilderMIPS::VisitFakeString(HFakeString* instruction) {
4128 DCHECK(codegen_->IsBaseline());
4129 LocationSummary* locations =
4130 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4131 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
4132}
4133
4134void InstructionCodeGeneratorMIPS::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
4135 DCHECK(codegen_->IsBaseline());
4136 // Will be generated at use site.
4137}
4138
4139void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4140 LocationSummary* locations =
4141 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
4142 locations->SetInAt(0, Location::RequiresRegister());
4143}
4144
4145void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
4146 int32_t lower_bound = switch_instr->GetStartValue();
4147 int32_t num_entries = switch_instr->GetNumEntries();
4148 LocationSummary* locations = switch_instr->GetLocations();
4149 Register value_reg = locations->InAt(0).AsRegister<Register>();
4150 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
4151
4152 // Create a set of compare/jumps.
4153 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
4154 for (int32_t i = 0; i < num_entries; ++i) {
4155 int32_t case_value = lower_bound + i;
4156 MipsLabel* successor_label = codegen_->GetLabelOf(successors[i]);
4157 if (case_value == 0) {
4158 __ Beqz(value_reg, successor_label);
4159 } else {
4160 __ LoadConst32(TMP, case_value);
4161 __ Beq(value_reg, TMP, successor_label);
4162 }
4163 }
4164
4165 // Insert the default branch for every other value.
4166 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
4167 __ B(codegen_->GetLabelOf(default_block));
4168 }
4169}
4170
4171void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4172 // The trampoline uses the same calling convention as dex calling conventions,
4173 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
4174 // the method_idx.
4175 HandleInvoke(invoke);
4176}
4177
4178void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
4179 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
4180}
4181
4182#undef __
4183#undef QUICK_ENTRY_POINT
4184
4185} // namespace mips
4186} // namespace art