HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Android 10
|
10.0.0_r6
下载
查看原文件
收藏
根目录
art
compiler
optimizing
code_generator_mips.cc
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "code_generator_mips.h" #include "arch/mips/asm_support_mips.h" #include "arch/mips/entrypoints_direct_mips.h" #include "arch/mips/instruction_set_features_mips.h" #include "art_method.h" #include "class_table.h" #include "code_generator_utils.h" #include "compiled_method.h" #include "entrypoints/quick/quick_entrypoints.h" #include "entrypoints/quick/quick_entrypoints_enum.h" #include "gc/accounting/card_table.h" #include "gc/space/image_space.h" #include "heap_poisoning.h" #include "intrinsics.h" #include "intrinsics_mips.h" #include "linker/linker_patch.h" #include "mirror/array-inl.h" #include "mirror/class-inl.h" #include "offsets.h" #include "stack_map_stream.h" #include "thread.h" #include "utils/assembler.h" #include "utils/mips/assembler_mips.h" #include "utils/stack_checks.h" namespace art { namespace mips { static constexpr int kCurrentMethodStackOffset = 0; static constexpr Register kMethodRegisterArgument = A0; // Flags controlling the use of thunks for Baker read barriers. constexpr bool kBakerReadBarrierThunksEnableForFields = true; constexpr bool kBakerReadBarrierThunksEnableForArrays = true; constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true; Location MipsReturnLocation(DataType::Type return_type) { switch (return_type) { case DataType::Type::kReference: case DataType::Type::kBool: case DataType::Type::kUint8: case DataType::Type::kInt8: case DataType::Type::kUint16: case DataType::Type::kInt16: case DataType::Type::kUint32: case DataType::Type::kInt32: return Location::RegisterLocation(V0); case DataType::Type::kUint64: case DataType::Type::kInt64: return Location::RegisterPairLocation(V0, V1); case DataType::Type::kFloat32: case DataType::Type::kFloat64: return Location::FpuRegisterLocation(F0); case DataType::Type::kVoid: return Location(); } UNREACHABLE(); } Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const { return MipsReturnLocation(type); } Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const { return Location::RegisterLocation(kMethodRegisterArgument); } Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) { Location next_location; switch (type) { case DataType::Type::kReference: case DataType::Type::kBool: case DataType::Type::kUint8: case DataType::Type::kInt8: case DataType::Type::kUint16: case DataType::Type::kInt16: case DataType::Type::kInt32: { uint32_t gp_index = gp_index_++; if (gp_index < calling_convention.GetNumberOfRegisters()) { next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index)); } else { size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_); next_location = Location::StackSlot(stack_offset); } break; } case DataType::Type::kInt64: { uint32_t gp_index = gp_index_; gp_index_ += 2; if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) { Register reg = calling_convention.GetRegisterAt(gp_index); if (reg == A1 || reg == A3) { gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead. gp_index++; } Register low_even = calling_convention.GetRegisterAt(gp_index); Register high_odd = calling_convention.GetRegisterAt(gp_index + 1); DCHECK_EQ(low_even + 1, high_odd); next_location = Location::RegisterPairLocation(low_even, high_odd); } else { size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_); next_location = Location::DoubleStackSlot(stack_offset); } break; } // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double // will take up the even/odd pair, while floats are stored in even regs only. // On 64 bit FPU, both double and float are stored in even registers only. case DataType::Type::kFloat32: case DataType::Type::kFloat64: { uint32_t float_index = float_index_++; if (float_index < calling_convention.GetNumberOfFpuRegisters()) { next_location = Location::FpuRegisterLocation( calling_convention.GetFpuRegisterAt(float_index)); } else { size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_); next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset) : Location::StackSlot(stack_offset); } break; } case DataType::Type::kUint32: case DataType::Type::kUint64: case DataType::Type::kVoid: LOG(FATAL) << "Unexpected parameter type " << type; UNREACHABLE(); } // Space on the stack is reserved for all arguments. stack_index_ += DataType::Is64BitType(type) ? 2 : 1; return next_location; } Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) { return MipsReturnLocation(type); } static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() { InvokeRuntimeCallingConvention calling_convention; RegisterSet caller_saves = RegisterSet::Empty(); caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); // The reference is returned in the same register. This differs from the standard return location. return caller_saves; } // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. #define __ down_cast
(codegen)->GetAssembler()-> // NOLINT #define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value() class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS { public: explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {} void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); if (instruction_->CanThrowIntoCatchBlock()) { // Live registers will be restored in the catch block if caught. SaveLiveRegisters(codegen, instruction_->GetLocations()); } // We're moving two locations to locations that could overlap, so we need a parallel // move resolver. InvokeRuntimeCallingConvention calling_convention; codegen->EmitParallelMoves(locations->InAt(0), Location::RegisterLocation(calling_convention.GetRegisterAt(0)), DataType::Type::kInt32, locations->InAt(1), Location::RegisterLocation(calling_convention.GetRegisterAt(1)), DataType::Type::kInt32); QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt() ? kQuickThrowStringBounds : kQuickThrowArrayBounds; mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); CheckEntrypointTypes
(); } bool IsFatal() const override { return true; } const char* GetDescription() const override { return "BoundsCheckSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS); }; class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS { public: explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {} void EmitNativeCode(CodeGenerator* codegen) override { CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); } bool IsFatal() const override { return true; } const char* GetDescription() const override { return "DivZeroCheckSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS); }; class LoadClassSlowPathMIPS : public SlowPathCodeMIPS { public: LoadClassSlowPathMIPS(HLoadClass* cls, HInstruction* at) : SlowPathCodeMIPS(at), cls_(cls) { DCHECK(at->IsLoadClass() || at->IsClinitCheck()); DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_); } void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); Location out = locations->Out(); const uint32_t dex_pc = instruction_->GetDexPc(); bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath(); bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck(); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); InvokeRuntimeCallingConvention calling_convention; if (must_resolve_type) { DCHECK(IsSameDexFile(cls_->GetDexFile(), mips_codegen->GetGraph()->GetDexFile())); dex::TypeIndex type_index = cls_->GetTypeIndex(); __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_); mips_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this); CheckEntrypointTypes
(); // If we also must_do_clinit, the resolved type is now in the correct register. } else { DCHECK(must_do_clinit); Location source = instruction_->IsLoadClass() ? out : locations->InAt(0); mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source, cls_->GetType()); } if (must_do_clinit) { mips_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this); CheckEntrypointTypes
(); } // Move the class to the desired location. if (out.IsValid()) { DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg())); DataType::Type type = instruction_->GetType(); mips_codegen->MoveLocation(out, Location::RegisterLocation(calling_convention.GetRegisterAt(0)), type); } RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } const char* GetDescription() const override { return "LoadClassSlowPathMIPS"; } private: // The class this slow path will load. HLoadClass* const cls_; DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS); }; class LoadStringSlowPathMIPS : public SlowPathCodeMIPS { public: explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {} void EmitNativeCode(CodeGenerator* codegen) override { DCHECK(instruction_->IsLoadString()); DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry); LocationSummary* locations = instruction_->GetLocations(); DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex(); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); InvokeRuntimeCallingConvention calling_convention; __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_); mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); DataType::Type type = instruction_->GetType(); mips_codegen->MoveLocation(locations->Out(), Location::RegisterLocation(calling_convention.GetRegisterAt(0)), type); RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } const char* GetDescription() const override { return "LoadStringSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS); }; class NullCheckSlowPathMIPS : public SlowPathCodeMIPS { public: explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {} void EmitNativeCode(CodeGenerator* codegen) override { CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); if (instruction_->CanThrowIntoCatchBlock()) { // Live registers will be restored in the catch block if caught. SaveLiveRegisters(codegen, instruction_->GetLocations()); } mips_codegen->InvokeRuntime(kQuickThrowNullPointer, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); } bool IsFatal() const override { return true; } const char* GetDescription() const override { return "NullCheckSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS); }; class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS { public: SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor) : SlowPathCodeMIPS(instruction), successor_(successor) {} void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD. mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD. if (successor_ == nullptr) { __ B(GetReturnLabel()); } else { __ B(mips_codegen->GetLabelOf(successor_)); } } MipsLabel* GetReturnLabel() { DCHECK(successor_ == nullptr); return &return_label_; } const char* GetDescription() const override { return "SuspendCheckSlowPathMIPS"; } HBasicBlock* GetSuccessor() const { return successor_; } private: // If not null, the block to branch to after the suspend check. HBasicBlock* const successor_; // If `successor_` is null, the label to branch to after the suspend check. MipsLabel return_label_; DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS); }; class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS { public: explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal) : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {} void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); uint32_t dex_pc = instruction_->GetDexPc(); DCHECK(instruction_->IsCheckCast() || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) { SaveLiveRegisters(codegen, locations); } // We're moving two locations to locations that could overlap, so we need a parallel // move resolver. InvokeRuntimeCallingConvention calling_convention; codegen->EmitParallelMoves(locations->InAt(0), Location::RegisterLocation(calling_convention.GetRegisterAt(0)), DataType::Type::kReference, locations->InAt(1), Location::RegisterLocation(calling_convention.GetRegisterAt(1)), DataType::Type::kReference); if (instruction_->IsInstanceOf()) { mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this); CheckEntrypointTypes
(); DataType::Type ret_type = instruction_->GetType(); Location ret_loc = calling_convention.GetReturnLocation(ret_type); mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type); } else { DCHECK(instruction_->IsCheckCast()); mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this); CheckEntrypointTypes
(); } if (!is_fatal_) { RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } } const char* GetDescription() const override { return "TypeCheckSlowPathMIPS"; } bool IsFatal() const override { return is_fatal_; } private: const bool is_fatal_; DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS); }; class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS { public: explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction) : SlowPathCodeMIPS(instruction) {} void EmitNativeCode(CodeGenerator* codegen) override { CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); __ Bind(GetEntryLabel()); LocationSummary* locations = instruction_->GetLocations(); SaveLiveRegisters(codegen, locations); InvokeRuntimeCallingConvention calling_convention; __ LoadConst32(calling_convention.GetRegisterAt(0), static_cast
(instruction_->AsDeoptimize()->GetDeoptimizationKind())); mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); } const char* GetDescription() const override { return "DeoptimizationSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS); }; class ArraySetSlowPathMIPS : public SlowPathCodeMIPS { public: explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {} void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); InvokeRuntimeCallingConvention calling_convention; HParallelMove parallel_move(codegen->GetGraph()->GetAllocator()); parallel_move.AddMove( locations->InAt(0), Location::RegisterLocation(calling_convention.GetRegisterAt(0)), DataType::Type::kReference, nullptr); parallel_move.AddMove( locations->InAt(1), Location::RegisterLocation(calling_convention.GetRegisterAt(1)), DataType::Type::kInt32, nullptr); parallel_move.AddMove( locations->InAt(2), Location::RegisterLocation(calling_convention.GetRegisterAt(2)), DataType::Type::kReference, nullptr); codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
(); RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } const char* GetDescription() const override { return "ArraySetSlowPathMIPS"; } private: DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS); }; // Slow path marking an object reference `ref` during a read // barrier. The field `obj.field` in the object `obj` holding this // reference does not get updated by this slow path after marking (see // ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that). // // This means that after the execution of this slow path, `ref` will // always be up-to-date, but `obj.field` may not; i.e., after the // flip, `ref` will be a to-space reference, but `obj.field` will // probably still be a from-space reference (unless it gets updated by // another thread, or if another thread installed another object // reference (different from `ref`) in `obj.field`). // // If `entrypoint` is a valid location it is assumed to already be // holding the entrypoint. The case where the entrypoint is passed in // is for the GcRoot read barrier. class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS { public: ReadBarrierMarkSlowPathMIPS(HInstruction* instruction, Location ref, Location entrypoint = Location::NoLocation()) : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) { DCHECK(kEmitCompilerReadBarrier); } const char* GetDescription() const override { return "ReadBarrierMarkSlowPathMIPS"; } void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); Register ref_reg = ref_.AsRegister
(); DCHECK(locations->CanCall()); DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg; DCHECK(instruction_->IsInstanceFieldGet() || instruction_->IsStaticFieldGet() || instruction_->IsArrayGet() || instruction_->IsArraySet() || instruction_->IsLoadClass() || instruction_->IsLoadString() || instruction_->IsInstanceOf() || instruction_->IsCheckCast() || (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) || (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified())) << "Unexpected instruction in read barrier marking slow path: " << instruction_->DebugName(); __ Bind(GetEntryLabel()); // No need to save live registers; it's taken care of by the // entrypoint. Also, there is no need to update the stack mask, // as this runtime call will not trigger a garbage collection. CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); DCHECK((V0 <= ref_reg && ref_reg <= T7) || (S2 <= ref_reg && ref_reg <= S7) || (ref_reg == FP)) << ref_reg; // "Compact" slow path, saving two moves. // // Instead of using the standard runtime calling convention (input // and output in A0 and V0 respectively): // // A0 <- ref // V0 <- ReadBarrierMark(A0) // ref <- V0 // // we just use rX (the register containing `ref`) as input and output // of a dedicated entrypoint: // // rX <- ReadBarrierMarkRegX(rX) // if (entrypoint_.IsValid()) { mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this); DCHECK_EQ(entrypoint_.AsRegister
(), T9); __ Jalr(entrypoint_.AsRegister
()); __ NopIfNoReordering(); } else { int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset
(ref_reg - 1); // This runtime call does not require a stack map. mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this, /* direct= */ false); } __ B(GetExitLabel()); } private: // The location (register) of the marked object reference. const Location ref_; // The location of the entrypoint if already loaded. const Location entrypoint_; DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS); }; // Slow path marking an object reference `ref` during a read barrier, // and if needed, atomically updating the field `obj.field` in the // object `obj` holding this reference after marking (contrary to // ReadBarrierMarkSlowPathMIPS above, which never tries to update // `obj.field`). // // This means that after the execution of this slow path, both `ref` // and `obj.field` will be up-to-date; i.e., after the flip, both will // hold the same to-space reference (unless another thread installed // another object reference (different from `ref`) in `obj.field`). class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS { public: ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction, Location ref, Register obj, Location field_offset, Register temp1) : SlowPathCodeMIPS(instruction), ref_(ref), obj_(obj), field_offset_(field_offset), temp1_(temp1) { DCHECK(kEmitCompilerReadBarrier); } const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS"; } void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); Register ref_reg = ref_.AsRegister
(); DCHECK(locations->CanCall()); DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg; // This slow path is only used by the UnsafeCASObject intrinsic. DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified())) << "Unexpected instruction in read barrier marking and field updating slow path: " << instruction_->DebugName(); DCHECK(instruction_->GetLocations()->Intrinsified()); DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject); DCHECK(field_offset_.IsRegisterPair()) << field_offset_; __ Bind(GetEntryLabel()); // Save the old reference. // Note that we cannot use AT or TMP to save the old reference, as those // are used by the code that follows, but we need the old reference after // the call to the ReadBarrierMarkRegX entry point. DCHECK_NE(temp1_, AT); DCHECK_NE(temp1_, TMP); __ Move(temp1_, ref_reg); // No need to save live registers; it's taken care of by the // entrypoint. Also, there is no need to update the stack mask, // as this runtime call will not trigger a garbage collection. CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); DCHECK((V0 <= ref_reg && ref_reg <= T7) || (S2 <= ref_reg && ref_reg <= S7) || (ref_reg == FP)) << ref_reg; // "Compact" slow path, saving two moves. // // Instead of using the standard runtime calling convention (input // and output in A0 and V0 respectively): // // A0 <- ref // V0 <- ReadBarrierMark(A0) // ref <- V0 // // we just use rX (the register containing `ref`) as input and output // of a dedicated entrypoint: // // rX <- ReadBarrierMarkRegX(rX) // int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset
(ref_reg - 1); // This runtime call does not require a stack map. mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this, /* direct= */ false); // If the new reference is different from the old reference, // update the field in the holder (`*(obj_ + field_offset_)`). // // Note that this field could also hold a different object, if // another thread had concurrently changed it. In that case, the // the compare-and-set (CAS) loop below would abort, leaving the // field as-is. MipsLabel done; __ Beq(temp1_, ref_reg, &done); // Update the the holder's field atomically. This may fail if // mutator updates before us, but it's OK. This is achieved // using a strong compare-and-set (CAS) operation with relaxed // memory synchronization ordering, where the expected value is // the old reference and the desired value is the new reference. // Convenience aliases. Register base = obj_; // The UnsafeCASObject intrinsic uses a register pair as field // offset ("long offset"), of which only the low part contains // data. Register offset = field_offset_.AsRegisterPairLow
(); Register expected = temp1_; Register value = ref_reg; Register tmp_ptr = TMP; // Pointer to actual memory. Register tmp = AT; // Value in memory. __ Addu(tmp_ptr, base, offset); if (kPoisonHeapReferences) { __ PoisonHeapReference(expected); // Do not poison `value` if it is the same register as // `expected`, which has just been poisoned. if (value != expected) { __ PoisonHeapReference(value); } } // do { // tmp = [r_ptr] - expected; // } while (tmp == 0 && failure([r_ptr] <- r_new_value)); bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6(); MipsLabel loop_head, exit_loop; __ Bind(&loop_head); if (is_r6) { __ LlR6(tmp, tmp_ptr); } else { __ LlR2(tmp, tmp_ptr); } __ Bne(tmp, expected, &exit_loop); __ Move(tmp, value); if (is_r6) { __ ScR6(tmp, tmp_ptr); } else { __ ScR2(tmp, tmp_ptr); } __ Beqz(tmp, &loop_head); __ Bind(&exit_loop); if (kPoisonHeapReferences) { __ UnpoisonHeapReference(expected); // Do not unpoison `value` if it is the same register as // `expected`, which has just been unpoisoned. if (value != expected) { __ UnpoisonHeapReference(value); } } __ Bind(&done); __ B(GetExitLabel()); } private: // The location (register) of the marked object reference. const Location ref_; // The register containing the object holding the marked object reference field. const Register obj_; // The location of the offset of the marked reference field within `obj_`. Location field_offset_; const Register temp1_; DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS); }; // Slow path generating a read barrier for a heap reference. class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS { public: ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction, Location out, Location ref, Location obj, uint32_t offset, Location index) : SlowPathCodeMIPS(instruction), out_(out), ref_(ref), obj_(obj), offset_(offset), index_(index) { DCHECK(kEmitCompilerReadBarrier); // If `obj` is equal to `out` or `ref`, it means the initial object // has been overwritten by (or after) the heap object reference load // to be instrumented, e.g.: // // __ LoadFromOffset(kLoadWord, out, out, offset); // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset); // // In that case, we have lost the information about the original // object, and the emitted read barrier cannot work properly. DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out; DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref; } void EmitNativeCode(CodeGenerator* codegen) override { CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); LocationSummary* locations = instruction_->GetLocations(); Register reg_out = out_.AsRegister
(); DCHECK(locations->CanCall()); DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out)); DCHECK(instruction_->IsInstanceFieldGet() || instruction_->IsStaticFieldGet() || instruction_->IsArrayGet() || instruction_->IsInstanceOf() || instruction_->IsCheckCast() || (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified())) << "Unexpected instruction in read barrier for heap reference slow path: " << instruction_->DebugName(); __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); // We may have to change the index's value, but as `index_` is a // constant member (like other "inputs" of this slow path), // introduce a copy of it, `index`. Location index = index_; if (index_.IsValid()) { // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics. if (instruction_->IsArrayGet()) { // Compute the actual memory offset and store it in `index`. Register index_reg = index_.AsRegister
(); DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg)); if (codegen->IsCoreCalleeSaveRegister(index_reg)) { // We are about to change the value of `index_reg` (see the // calls to art::mips::MipsAssembler::Sll and // art::mips::MipsAssembler::Addiu32 below), but it has // not been saved by the previous call to // art::SlowPathCode::SaveLiveRegisters, as it is a // callee-save register -- // art::SlowPathCode::SaveLiveRegisters does not consider // callee-save registers, as it has been designed with the // assumption that callee-save registers are supposed to be // handled by the called function. So, as a callee-save // register, `index_reg` _would_ eventually be saved onto // the stack, but it would be too late: we would have // changed its value earlier. Therefore, we manually save // it here into another freely available register, // `free_reg`, chosen of course among the caller-save // registers (as a callee-save `free_reg` register would // exhibit the same problem). // // Note we could have requested a temporary register from // the register allocator instead; but we prefer not to, as // this is a slow path, and we know we can find a // caller-save register that is available. Register free_reg = FindAvailableCallerSaveRegister(codegen); __ Move(free_reg, index_reg); index_reg = free_reg; index = Location::RegisterLocation(index_reg); } else { // The initial register stored in `index_` has already been // saved in the call to art::SlowPathCode::SaveLiveRegisters // (as it is not a callee-save register), so we can freely // use it. } // Shifting the index value contained in `index_reg` by the scale // factor (2) cannot overflow in practice, as the runtime is // unable to allocate object arrays with a size larger than // 2^26 - 1 (that is, 2^28 - 4 bytes). __ Sll(index_reg, index_reg, TIMES_4); static_assert( sizeof(mirror::HeapReference
) == sizeof(int32_t), "art::mirror::HeapReference
and int32_t have different sizes."); __ Addiu32(index_reg, index_reg, offset_); } else { // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile // intrinsics, `index_` is not shifted by a scale factor of 2 // (as in the case of ArrayGet), as it is actually an offset // to an object field within an object. DCHECK(instruction_->IsInvoke()) << instruction_->DebugName(); DCHECK(instruction_->GetLocations()->Intrinsified()); DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) || (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile)) << instruction_->AsInvoke()->GetIntrinsic(); DCHECK_EQ(offset_, 0U); DCHECK(index_.IsRegisterPair()); // UnsafeGet's offset location is a register pair, the low // part contains the correct offset. index = index_.ToLow(); } } // We're moving two or three locations to locations that could // overlap, so we need a parallel move resolver. InvokeRuntimeCallingConvention calling_convention; HParallelMove parallel_move(codegen->GetGraph()->GetAllocator()); parallel_move.AddMove(ref_, Location::RegisterLocation(calling_convention.GetRegisterAt(0)), DataType::Type::kReference, nullptr); parallel_move.AddMove(obj_, Location::RegisterLocation(calling_convention.GetRegisterAt(1)), DataType::Type::kReference, nullptr); if (index.IsValid()) { parallel_move.AddMove(index, Location::RegisterLocation(calling_convention.GetRegisterAt(2)), DataType::Type::kInt32, nullptr); codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); } else { codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); __ LoadConst32(calling_convention.GetRegisterAt(2), offset_); } mips_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes< kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>(); mips_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(DataType::Type::kReference), DataType::Type::kReference); RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathMIPS"; } private: Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) { size_t ref = static_cast
(ref_.AsRegister
()); size_t obj = static_cast
(obj_.AsRegister
()); for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) { if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i) && !codegen->IsBlockedCoreRegister(i)) { return static_cast
(i); } } // We shall never fail to find a free caller-save register, as // there are more than two core caller-save registers on MIPS // (meaning it is possible to find one which is different from // `ref` and `obj`). DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u); LOG(FATAL) << "Could not find a free caller-save register"; UNREACHABLE(); } const Location out_; const Location ref_; const Location obj_; const uint32_t offset_; // An additional location containing an index to an array. // Only used for HArrayGet and the UnsafeGetObject & // UnsafeGetObjectVolatile intrinsics. const Location index_; DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS); }; // Slow path generating a read barrier for a GC root. class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS { public: ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root) : SlowPathCodeMIPS(instruction), out_(out), root_(root) { DCHECK(kEmitCompilerReadBarrier); } void EmitNativeCode(CodeGenerator* codegen) override { LocationSummary* locations = instruction_->GetLocations(); Register reg_out = out_.AsRegister
(); DCHECK(locations->CanCall()); DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out)); DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString()) << "Unexpected instruction in read barrier for GC root slow path: " << instruction_->DebugName(); __ Bind(GetEntryLabel()); SaveLiveRegisters(codegen, locations); InvokeRuntimeCallingConvention calling_convention; CodeGeneratorMIPS* mips_codegen = down_cast
(codegen); mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_, DataType::Type::kReference); mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow, instruction_, instruction_->GetDexPc(), this); CheckEntrypointTypes
*>(); mips_codegen->MoveLocation(out_, calling_convention.GetReturnLocation(DataType::Type::kReference), DataType::Type::kReference); RestoreLiveRegisters(codegen, locations); __ B(GetExitLabel()); } const char* GetDescription() const override { return "ReadBarrierForRootSlowPathMIPS"; } private: const Location out_; const Location root_; DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS); }; CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph, const CompilerOptions& compiler_options, OptimizingCompilerStats* stats) : CodeGenerator(graph, kNumberOfCoreRegisters, kNumberOfFRegisters, kNumberOfRegisterPairs, ComputeRegisterMask(reinterpret_cast
(kCoreCalleeSaves), arraysize(kCoreCalleeSaves)), ComputeRegisterMask(reinterpret_cast
(kFpuCalleeSaves), arraysize(kFpuCalleeSaves)), compiler_options, stats), block_labels_(nullptr), location_builder_(graph, this), instruction_visitor_(graph, this), move_resolver_(graph->GetAllocator(), this), assembler_(graph->GetAllocator(), compiler_options.GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()), uint32_literals_(std::less
(), graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)), clobbered_ra_(false) { // Save RA (containing the return address) to mimic Quick. AddAllocatedRegister(Location::RegisterLocation(RA)); } #undef __ // NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy. #define __ down_cast
(GetAssembler())-> // NOLINT #define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value() void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) { // Ensure that we fix up branches. __ FinalizeCode(); // Adjust native pc offsets in stack maps. StackMapStream* stack_map_stream = GetStackMapStream(); for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) { uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i); uint32_t new_position = __ GetAdjustedPosition(old_position); DCHECK_GE(new_position, old_position); stack_map_stream->SetStackMapNativePcOffset(i, new_position); } // Adjust pc offsets for the disassembly information. if (disasm_info_ != nullptr) { GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval(); frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start); frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end); for (auto& it : *disasm_info_->GetInstructionIntervals()) { it.second.start = __ GetAdjustedPosition(it.second.start); it.second.end = __ GetAdjustedPosition(it.second.end); } for (auto& it : *disasm_info_->GetSlowPathIntervals()) { it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start); it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end); } } CodeGenerator::Finalize(allocator); } MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const { return codegen_->GetAssembler(); } void ParallelMoveResolverMIPS::EmitMove(size_t index) { DCHECK_LT(index, moves_.size()); MoveOperands* move = moves_[index]; codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType()); } void ParallelMoveResolverMIPS::EmitSwap(size_t index) { DCHECK_LT(index, moves_.size()); MoveOperands* move = moves_[index]; DataType::Type type = move->GetType(); Location loc1 = move->GetDestination(); Location loc2 = move->GetSource(); DCHECK(!loc1.IsConstant()); DCHECK(!loc2.IsConstant()); if (loc1.Equals(loc2)) { return; } if (loc1.IsRegister() && loc2.IsRegister()) { // Swap 2 GPRs. Register r1 = loc1.AsRegister
(); Register r2 = loc2.AsRegister
(); __ Move(TMP, r2); __ Move(r2, r1); __ Move(r1, TMP); } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) { if (codegen_->GetGraph()->HasSIMD()) { __ MoveV(static_cast
(FTMP), VectorRegisterFrom(loc1)); __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2)); __ MoveV(VectorRegisterFrom(loc2), static_cast
(FTMP)); } else { FRegister f1 = loc1.AsFpuRegister
(); FRegister f2 = loc2.AsFpuRegister
(); if (type == DataType::Type::kFloat32) { __ MovS(FTMP, f2); __ MovS(f2, f1); __ MovS(f1, FTMP); } else { DCHECK_EQ(type, DataType::Type::kFloat64); __ MovD(FTMP, f2); __ MovD(f2, f1); __ MovD(f1, FTMP); } } } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) || (loc1.IsFpuRegister() && loc2.IsRegister())) { // Swap FPR and GPR. DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float. FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister
() : loc2.AsFpuRegister
(); Register r2 = loc1.IsRegister() ? loc1.AsRegister
() : loc2.AsRegister
(); __ Move(TMP, r2); __ Mfc1(r2, f1); __ Mtc1(TMP, f1); } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) { // Swap 2 GPR register pairs. Register r1 = loc1.AsRegisterPairLow
(); Register r2 = loc2.AsRegisterPairLow
(); __ Move(TMP, r2); __ Move(r2, r1); __ Move(r1, TMP); r1 = loc1.AsRegisterPairHigh
(); r2 = loc2.AsRegisterPairHigh
(); __ Move(TMP, r2); __ Move(r2, r1); __ Move(r1, TMP); } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) || (loc1.IsFpuRegister() && loc2.IsRegisterPair())) { // Swap FPR and GPR register pair. DCHECK_EQ(type, DataType::Type::kFloat64); FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister
() : loc2.AsFpuRegister
(); Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow
() : loc2.AsRegisterPairLow
(); Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh
() : loc2.AsRegisterPairHigh
(); // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR // unpredictable and the following mfch1 will fail. __ Mfc1(TMP, f1); __ MoveFromFpuHigh(AT, f1); __ Mtc1(r2_l, f1); __ MoveToFpuHigh(r2_h, f1); __ Move(r2_l, TMP); __ Move(r2_h, AT); } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) { Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot= */ false); } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) { Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot= */ true); } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) { ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex()); } else if ((loc1.IsRegister() && loc2.IsStackSlot()) || (loc1.IsStackSlot() && loc2.IsRegister())) { Register reg = loc1.IsRegister() ? loc1.AsRegister
() : loc2.AsRegister
(); intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex(); __ Move(TMP, reg); __ LoadFromOffset(kLoadWord, reg, SP, offset); __ StoreToOffset(kStoreWord, TMP, SP, offset); } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) || (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) { Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow
() : loc2.AsRegisterPairLow
(); Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh
() : loc2.AsRegisterPairHigh
(); intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex(); intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize) : loc2.GetHighStackIndex(kMipsWordSize); __ Move(TMP, reg_l); __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l); __ StoreToOffset(kStoreWord, TMP, SP, offset_l); __ Move(TMP, reg_h); __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h); __ StoreToOffset(kStoreWord, TMP, SP, offset_h); } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) || (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) { Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2; intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex(); __ MoveV(static_cast
(FTMP), VectorRegisterFrom(fp_loc)); __ LoadQFromOffset(fp_loc.AsFpuRegister
(), SP, offset); __ StoreQToOffset(FTMP, SP, offset); } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) { FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister
() : loc2.AsFpuRegister
(); intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex(); if (type == DataType::Type::kFloat32) { __ MovS(FTMP, reg); __ LoadSFromOffset(reg, SP, offset); __ StoreSToOffset(FTMP, SP, offset); } else { DCHECK_EQ(type, DataType::Type::kFloat64); __ MovD(FTMP, reg); __ LoadDFromOffset(reg, SP, offset); __ StoreDToOffset(FTMP, SP, offset); } } else { LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported"; } } void ParallelMoveResolverMIPS::RestoreScratch(int reg) { __ Pop(static_cast
(reg)); } void ParallelMoveResolverMIPS::SpillScratch(int reg) { __ Push(static_cast
(reg)); } void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) { // Allocate a scratch register other than TMP, if available. // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be // automatically unspilled when the scratch scope object is destroyed). ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters()); // If V0 spills onto the stack, SP-relative offsets need to be adjusted. int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0; for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) { __ LoadFromOffset(kLoadWord, Register(ensure_scratch.GetRegister()), SP, index1 + stack_offset); __ LoadFromOffset(kLoadWord, TMP, SP, index2 + stack_offset); __ StoreToOffset(kStoreWord, Register(ensure_scratch.GetRegister()), SP, index2 + stack_offset); __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset); } } void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) { __ LoadQFromOffset(FTMP, SP, index1); __ LoadQFromOffset(FTMP2, SP, index2); __ StoreQToOffset(FTMP, SP, index2); __ StoreQToOffset(FTMP2, SP, index1); } void CodeGeneratorMIPS::ComputeSpillMask() { core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_; fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_; DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved"; // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved // registers, include the ZERO register to force alignment of FPU callee-saved registers // within the stack frame. if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) { core_spill_mask_ |= (1 << ZERO); } } bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const { // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration() // into the path that creates a stack frame so that RA can be explicitly saved and restored. // RA can't otherwise be saved/restored when it's the only spilled register. return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_; } static dwarf::Reg DWARFReg(Register reg) { return dwarf::Reg::MipsCore(static_cast
(reg)); } // TODO: mapping of floating-point registers to DWARF. void CodeGeneratorMIPS::GenerateFrameEntry() { __ Bind(&frame_entry_label_); if (GetCompilerOptions().CountHotnessInCompiledCode()) { __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()); __ Addiu(TMP, TMP, 1); __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value()); } bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod(); if (do_overflow_check) { __ LoadFromOffset(kLoadWord, ZERO, SP, -static_cast
(GetStackOverflowReservedBytes(InstructionSet::kMips))); RecordPcInfo(nullptr, 0); } if (HasEmptyFrame()) { CHECK_EQ(fpu_spill_mask_, 0u); CHECK_EQ(core_spill_mask_, 1u << RA); CHECK(!clobbered_ra_); return; } // Make sure the frame size isn't unreasonably large. if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) { LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes"; } // Spill callee-saved registers. uint32_t ofs = GetFrameSize(); __ IncreaseFrameSize(ofs); for (uint32_t mask = core_spill_mask_; mask != 0; ) { Register reg = static_cast
(MostSignificantBit(mask)); mask ^= 1u << reg; ofs -= kMipsWordSize; // The ZERO register is only included for alignment. if (reg != ZERO) { __ StoreToOffset(kStoreWord, reg, SP, ofs); __ cfi().RelOffset(DWARFReg(reg), ofs); } } for (uint32_t mask = fpu_spill_mask_; mask != 0; ) { FRegister reg = static_cast
(MostSignificantBit(mask)); mask ^= 1u << reg; ofs -= kMipsDoublewordSize; __ StoreDToOffset(reg, SP, ofs); // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs); } // Save the current method if we need it. Note that we do not // do this in HCurrentMethod, as the instruction might have been removed // in the SSA graph. if (RequiresCurrentMethod()) { __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset); } if (GetGraph()->HasShouldDeoptimizeFlag()) { // Initialize should deoptimize flag to 0. __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag()); } } void CodeGeneratorMIPS::GenerateFrameExit() { __ cfi().RememberState(); if (!HasEmptyFrame()) { // Restore callee-saved registers. // For better instruction scheduling restore RA before other registers. uint32_t ofs = GetFrameSize(); for (uint32_t mask = core_spill_mask_; mask != 0; ) { Register reg = static_cast
(MostSignificantBit(mask)); mask ^= 1u << reg; ofs -= kMipsWordSize; // The ZERO register is only included for alignment. if (reg != ZERO) { __ LoadFromOffset(kLoadWord, reg, SP, ofs); __ cfi().Restore(DWARFReg(reg)); } } for (uint32_t mask = fpu_spill_mask_; mask != 0; ) { FRegister reg = static_cast
(MostSignificantBit(mask)); mask ^= 1u << reg; ofs -= kMipsDoublewordSize; __ LoadDFromOffset(reg, SP, ofs); // TODO: __ cfi().Restore(DWARFReg(reg)); } size_t frame_size = GetFrameSize(); // Adjust the stack pointer in the delay slot if doing so doesn't break CFI. bool exchange = IsInt<16>(static_cast
(frame_size)); bool reordering = __ SetReorder(false); if (exchange) { __ Jr(RA); __ DecreaseFrameSize(frame_size); // Single instruction in delay slot. } else { __ DecreaseFrameSize(frame_size); __ Jr(RA); __ Nop(); // In delay slot. } __ SetReorder(reordering); } else { __ Jr(RA); __ NopIfNoReordering(); } __ cfi().RestoreState(); __ cfi().DefCFAOffset(GetFrameSize()); } void CodeGeneratorMIPS::Bind(HBasicBlock* block) { __ Bind(GetLabelOf(block)); } VectorRegister VectorRegisterFrom(Location location) { DCHECK(location.IsFpuRegister()); return static_cast
(location.AsFpuRegister
()); } void CodeGeneratorMIPS::MoveLocation(Location destination, Location source, DataType::Type dst_type) { if (source.Equals(destination)) { return; } if (source.IsConstant()) { MoveConstant(destination, source.GetConstant()); } else { if (destination.IsRegister()) { if (source.IsRegister()) { __ Move(destination.AsRegister
(), source.AsRegister
()); } else if (source.IsFpuRegister()) { __ Mfc1(destination.AsRegister
(), source.AsFpuRegister
()); } else { DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination; __ LoadFromOffset(kLoadWord, destination.AsRegister
(), SP, source.GetStackIndex()); } } else if (destination.IsRegisterPair()) { if (source.IsRegisterPair()) { __ Move(destination.AsRegisterPairHigh
(), source.AsRegisterPairHigh
()); __ Move(destination.AsRegisterPairLow
(), source.AsRegisterPairLow
()); } else if (source.IsFpuRegister()) { Register dst_high = destination.AsRegisterPairHigh
(); Register dst_low = destination.AsRegisterPairLow
(); FRegister src = source.AsFpuRegister
(); __ Mfc1(dst_low, src); __ MoveFromFpuHigh(dst_high, src); } else { DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination; int32_t off = source.GetStackIndex(); Register r = destination.AsRegisterPairLow
(); __ LoadFromOffset(kLoadDoubleword, r, SP, off); } } else if (destination.IsFpuRegister()) { if (source.IsRegister()) { DCHECK(!DataType::Is64BitType(dst_type)); __ Mtc1(source.AsRegister
(), destination.AsFpuRegister
()); } else if (source.IsRegisterPair()) { DCHECK(DataType::Is64BitType(dst_type)); FRegister dst = destination.AsFpuRegister
(); Register src_high = source.AsRegisterPairHigh
(); Register src_low = source.AsRegisterPairLow
(); __ Mtc1(src_low, dst); __ MoveToFpuHigh(src_high, dst); } else if (source.IsFpuRegister()) { if (GetGraph()->HasSIMD()) { __ MoveV(VectorRegisterFrom(destination), VectorRegisterFrom(source)); } else { if (DataType::Is64BitType(dst_type)) { __ MovD(destination.AsFpuRegister
(), source.AsFpuRegister
()); } else { DCHECK_EQ(dst_type, DataType::Type::kFloat32); __ MovS(destination.AsFpuRegister
(), source.AsFpuRegister
()); } } } else if (source.IsSIMDStackSlot()) { __ LoadQFromOffset(destination.AsFpuRegister
(), SP, source.GetStackIndex()); } else if (source.IsDoubleStackSlot()) { DCHECK(DataType::Is64BitType(dst_type)); __ LoadDFromOffset(destination.AsFpuRegister
(), SP, source.GetStackIndex()); } else { DCHECK(!DataType::Is64BitType(dst_type)); DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination; __ LoadSFromOffset(destination.AsFpuRegister
(), SP, source.GetStackIndex()); } } else if (destination.IsSIMDStackSlot()) { if (source.IsFpuRegister()) { __ StoreQToOffset(source.AsFpuRegister
(), SP, destination.GetStackIndex()); } else { DCHECK(source.IsSIMDStackSlot()); __ LoadQFromOffset(FTMP, SP, source.GetStackIndex()); __ StoreQToOffset(FTMP, SP, destination.GetStackIndex()); } } else if (destination.IsDoubleStackSlot()) { int32_t dst_offset = destination.GetStackIndex(); if (source.IsRegisterPair()) { __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow
(), SP, dst_offset); } else if (source.IsFpuRegister()) { __ StoreDToOffset(source.AsFpuRegister
(), SP, dst_offset); } else { DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination; __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex()); __ StoreToOffset(kStoreWord, TMP, SP, dst_offset); __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4); __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4); } } else { DCHECK(destination.IsStackSlot()) << destination; int32_t dst_offset = destination.GetStackIndex(); if (source.IsRegister()) { __ StoreToOffset(kStoreWord, source.AsRegister
(), SP, dst_offset); } else if (source.IsFpuRegister()) { __ StoreSToOffset(source.AsFpuRegister
(), SP, dst_offset); } else { DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination; __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex()); __ StoreToOffset(kStoreWord, TMP, SP, dst_offset); } } } } void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) { if (c->IsIntConstant() || c->IsNullConstant()) { // Move 32 bit constant. int32_t value = GetInt32ValueOf(c); if (destination.IsRegister()) { Register dst = destination.AsRegister
(); __ LoadConst32(dst, value); } else { DCHECK(destination.IsStackSlot()) << "Cannot move " << c->DebugName() << " to " << destination; __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP); } } else if (c->IsLongConstant()) { // Move 64 bit constant. int64_t value = GetInt64ValueOf(c); if (destination.IsRegisterPair()) { Register r_h = destination.AsRegisterPairHigh
(); Register r_l = destination.AsRegisterPairLow
(); __ LoadConst64(r_h, r_l, value); } else { DCHECK(destination.IsDoubleStackSlot()) << "Cannot move " << c->DebugName() << " to " << destination; __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP); } } else if (c->IsFloatConstant()) { // Move 32 bit float constant. int32_t value = GetInt32ValueOf(c); if (destination.IsFpuRegister()) { __ LoadSConst32(destination.AsFpuRegister
(), value, TMP); } else { DCHECK(destination.IsStackSlot()) << "Cannot move " << c->DebugName() << " to " << destination; __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP); } } else { // Move 64 bit double constant. DCHECK(c->IsDoubleConstant()) << c->DebugName(); int64_t value = GetInt64ValueOf(c); if (destination.IsFpuRegister()) { FRegister fd = destination.AsFpuRegister
(); __ LoadDConst64(fd, value, TMP); } else { DCHECK(destination.IsDoubleStackSlot()) << "Cannot move " << c->DebugName() << " to " << destination; __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP); } } } void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) { DCHECK(destination.IsRegister()); Register dst = destination.AsRegister
(); __ LoadConst32(dst, value); } void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) { if (location.IsRegister()) { locations->AddTemp(location); } else if (location.IsRegisterPair()) { locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow
())); locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh
())); } else { UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location; } } template
inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches( const ArenaDeque
& infos, ArenaVector
* linker_patches) { for (const PcRelativePatchInfo& info : infos) { const DexFile* dex_file = info.target_dex_file; size_t offset_or_index = info.offset_or_index; DCHECK(info.label.IsBound()); uint32_t literal_offset = __ GetLabelLocation(&info.label); // On R2 we use HMipsComputeBaseMethodAddress and patch relative to // the assembler's base label used for PC-relative addressing. const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info; uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound() ? __ GetLabelLocation(&info_high.pc_rel_label) : __ GetPcRelBaseLabelLocation(); linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index)); } } template
linker::LinkerPatch NoDexFileAdapter(size_t literal_offset, const DexFile* target_dex_file, uint32_t pc_insn_offset, uint32_t boot_image_offset) { DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null. return Factory(literal_offset, pc_insn_offset, boot_image_offset); } void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector
* linker_patches) { DCHECK(linker_patches->empty()); size_t size = boot_image_method_patches_.size() + method_bss_entry_patches_.size() + boot_image_type_patches_.size() + type_bss_entry_patches_.size() + boot_image_string_patches_.size() + string_bss_entry_patches_.size() + boot_image_intrinsic_patches_.size(); linker_patches->reserve(size); if (GetCompilerOptions().IsBootImage()) { EmitPcRelativeLinkerPatches
( boot_image_method_patches_, linker_patches); EmitPcRelativeLinkerPatches
( boot_image_type_patches_, linker_patches); EmitPcRelativeLinkerPatches
( boot_image_string_patches_, linker_patches); EmitPcRelativeLinkerPatches
>( boot_image_intrinsic_patches_, linker_patches); } else { EmitPcRelativeLinkerPatches
>( boot_image_method_patches_, linker_patches); DCHECK(boot_image_type_patches_.empty()); DCHECK(boot_image_string_patches_.empty()); DCHECK(boot_image_intrinsic_patches_.empty()); } EmitPcRelativeLinkerPatches
( method_bss_entry_patches_, linker_patches); EmitPcRelativeLinkerPatches
( type_bss_entry_patches_, linker_patches); EmitPcRelativeLinkerPatches
( string_bss_entry_patches_, linker_patches); DCHECK_EQ(size, linker_patches->size()); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageIntrinsicPatch( uint32_t intrinsic_data, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch( /* dex_file= */ nullptr, intrinsic_data, info_high, &boot_image_intrinsic_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch( uint32_t boot_image_offset, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch( /* dex_file= */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch( MethodReference target_method, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch( target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch( MethodReference target_method, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch( target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch( const DexFile& dex_file, dex::TypeIndex type_index, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch( const DexFile& dex_file, dex::TypeIndex type_index, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch( const DexFile& dex_file, dex::StringIndex string_index, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch( &dex_file, string_index.index_, info_high, &boot_image_string_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch( const DexFile& dex_file, dex::StringIndex string_index, const PcRelativePatchInfo* info_high) { return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_); } CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch( const DexFile* dex_file, uint32_t offset_or_index, const PcRelativePatchInfo* info_high, ArenaDeque
* patches) { patches->emplace_back(dex_file, offset_or_index, info_high); return &patches->back(); } Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) { return map->GetOrCreate( value, [this, value]() { return __ NewLiteral
(value); }); } Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) { return DeduplicateUint32Literal(dchecked_integral_cast
(address), &uint32_literals_); } void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high, Register out, Register base) { DCHECK(!info_high->patch_info_high); DCHECK_NE(out, base); bool reordering = __ SetReorder(false); if (GetInstructionSetFeatures().IsR6()) { DCHECK_EQ(base, ZERO); __ Bind(&info_high->label); __ Bind(&info_high->pc_rel_label); // Add the high half of a 32-bit offset to PC. __ Auipc(out, /* imm16= */ 0x1234); __ SetReorder(reordering); } else { // If base is ZERO, emit NAL to obtain the actual base. if (base == ZERO) { // Generate a dummy PC-relative call to obtain PC. __ Nal(); } __ Bind(&info_high->label); __ Lui(out, /* imm16= */ 0x1234); // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler. if (base == ZERO) { __ Bind(&info_high->pc_rel_label); } __ SetReorder(reordering); // Add the high half of a 32-bit offset to PC. __ Addu(out, out, (base == ZERO) ? RA : base); } // A following instruction will add the sign-extended low half of the 32-bit // offset to `out` (e.g. lw, jialc, addiu). } void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_reference) { if (GetCompilerOptions().IsBootImage()) { PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference); PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high); EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, /* base= */ ZERO); __ Addiu(reg, TMP, /* imm16= */ 0x5678, &info_low->label); } else if (GetCompilerOptions().GetCompilePic()) { PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference); PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high); EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base= */ ZERO); __ Lw(reg, reg, /* imm16= */ 0x5678, &info_low->label); } else { DCHECK(Runtime::Current()->UseJitCompilation()); gc::Heap* heap = Runtime::Current()->GetHeap(); DCHECK(!heap->GetBootImageSpaces().empty()); const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference; __ LoadConst32(reg, dchecked_integral_cast
(reinterpret_cast
(address))); } } void CodeGeneratorMIPS::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke, uint32_t boot_image_offset) { DCHECK(invoke->IsStatic()); InvokeRuntimeCallingConvention calling_convention; Register argument = calling_convention.GetRegisterAt(0); if (GetCompilerOptions().IsBootImage()) { DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference); // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative. MethodReference target_method = invoke->GetTargetMethod(); dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_; PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx); PcRelativePatchInfo* info_low = NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high); EmitPcRelativeAddressPlaceholderHigh(info_high, argument, /* base= */ ZERO); __ Addiu(argument, argument, /* imm16= */ 0x5678, &info_low->label); } else { LoadBootImageAddress(argument, boot_image_offset); } InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc()); CheckEntrypointTypes
(); } CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch( const DexFile& dex_file, dex::StringIndex string_index, Handle
handle) { ReserveJitStringRoot(StringReference(&dex_file, string_index), handle); jit_string_patches_.emplace_back(dex_file, string_index.index_); return &jit_string_patches_.back(); } CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch( const DexFile& dex_file, dex::TypeIndex type_index, Handle
handle) { ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle); jit_class_patches_.emplace_back(dex_file, type_index.index_); return &jit_class_patches_.back(); } void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code, const uint8_t* roots_data, const CodeGeneratorMIPS::JitPatchInfo& info, uint64_t index_in_table) const { uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label); uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label); uintptr_t address = reinterpret_cast
(roots_data) + index_in_table * sizeof(GcRoot
); uint32_t addr32 = dchecked_integral_cast
(address); // lui reg, addr32_high DCHECK_EQ(code[high_literal_offset + 0], 0x34); DCHECK_EQ(code[high_literal_offset + 1], 0x12); DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00); DCHECK_EQ(code[high_literal_offset + 3], 0x3C); // instr reg, reg, addr32_low DCHECK_EQ(code[low_literal_offset + 0], 0x78); DCHECK_EQ(code[low_literal_offset + 1], 0x56); addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low". // lui reg, addr32_high code[high_literal_offset + 0] = static_cast
(addr32 >> 16); code[high_literal_offset + 1] = static_cast
(addr32 >> 24); // instr reg, reg, addr32_low code[low_literal_offset + 0] = static_cast
(addr32 >> 0); code[low_literal_offset + 1] = static_cast
(addr32 >> 8); } void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) { for (const JitPatchInfo& info : jit_string_patches_) { StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index)); uint64_t index_in_table = GetJitStringRootIndex(string_reference); PatchJitRootUse(code, roots_data, info, index_in_table); } for (const JitPatchInfo& info : jit_class_patches_) { TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index)); uint64_t index_in_table = GetJitClassRootIndex(type_reference); PatchJitRootUse(code, roots_data, info, index_in_table); } } void CodeGeneratorMIPS::MarkGCCard(Register object, Register value, bool value_can_be_null) { MipsLabel done; Register card = AT; Register temp = TMP; if (value_can_be_null) { __ Beqz(value, &done); } // Load the address of the card table into `card`. __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset
().Int32Value()); // Calculate the address of the card corresponding to `object`. __ Srl(temp, object, gc::accounting::CardTable::kCardShift); __ Addu(temp, card, temp); // Write the `art::gc::accounting::CardTable::kCardDirty` value into the // `object`'s card. // // Register `card` contains the address of the card table. Note that the card // table's base is biased during its creation so that it always starts at an // address whose least-significant byte is equal to `kCardDirty` (see // art::gc::accounting::CardTable::Create). Therefore the SB instruction // below writes the `kCardDirty` (byte) value into the `object`'s card // (located at `card + object >> kCardShift`). // // This dual use of the value in register `card` (1. to calculate the location // of the card to mark; and 2. to load the `kCardDirty` value) saves a load // (no need to explicitly load `kCardDirty` as an immediate value). __ Sb(card, temp, 0); if (value_can_be_null) { __ Bind(&done); } } void CodeGeneratorMIPS::SetupBlockedRegisters() const { // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated. blocked_core_registers_[ZERO] = true; blocked_core_registers_[K0] = true; blocked_core_registers_[K1] = true; blocked_core_registers_[GP] = true; blocked_core_registers_[SP] = true; blocked_core_registers_[RA] = true; // AT and TMP(T8) are used as temporary/scratch registers // (similar to how AT is used by MIPS assemblers). blocked_core_registers_[AT] = true; blocked_core_registers_[TMP] = true; blocked_fpu_registers_[FTMP] = true; if (GetInstructionSetFeatures().HasMsa()) { // To be used just for MSA instructions. blocked_fpu_registers_[FTMP2] = true; } // Reserve suspend and thread registers. blocked_core_registers_[S0] = true; blocked_core_registers_[TR] = true; // Reserve T9 for function calls blocked_core_registers_[T9] = true; // Reserve odd-numbered FPU registers. for (size_t i = 1; i < kNumberOfFRegisters; i += 2) { blocked_fpu_registers_[i] = true; } if (GetGraph()->IsDebuggable()) { // Stubs do not save callee-save floating point registers. If the graph // is debuggable, we need to deal with these registers differently. For // now, just block them. for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) { blocked_fpu_registers_[kFpuCalleeSaves[i]] = true; } } } size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) { __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index); return kMipsWordSize; } size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) { __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index); return kMipsWordSize; } size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) { if (GetGraph()->HasSIMD()) { __ StoreQToOffset(FRegister(reg_id), SP, stack_index); } else { __ StoreDToOffset(FRegister(reg_id), SP, stack_index); } return GetFloatingPointSpillSlotSize(); } size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) { if (GetGraph()->HasSIMD()) { __ LoadQFromOffset(FRegister(reg_id), SP, stack_index); } else { __ LoadDFromOffset(FRegister(reg_id), SP, stack_index); } return GetFloatingPointSpillSlotSize(); } void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const { stream << Register(reg); } void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const { stream << FRegister(reg); } const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const { return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures(); } constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16; void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint, HInstruction* instruction, uint32_t dex_pc, SlowPathCode* slow_path) { ValidateInvokeRuntime(entrypoint, instruction, slow_path); GenerateInvokeRuntime(GetThreadOffset
(entrypoint).Int32Value(), IsDirectEntrypoint(entrypoint)); if (EntrypointRequiresStackMap(entrypoint)) { RecordPcInfo(instruction, dex_pc, slow_path); } } void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset, HInstruction* instruction, SlowPathCode* slow_path, bool direct) { ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path); GenerateInvokeRuntime(entry_point_offset, direct); } void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) { bool reordering = __ SetReorder(false); __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset); __ Jalr(T9); if (direct) { // Reserve argument space on stack (for $a0-$a3) for // entrypoints that directly reference native implementations. // Called function may use this space to store $a0-$a3 regs. __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot. __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); } else { __ Nop(); // In delay slot. } __ SetReorder(reordering); } void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path, Register class_reg) { constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf(); const size_t status_byte_offset = mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte); constexpr uint32_t shifted_initialized_value = enum_cast
(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte); __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset); __ Sltiu(TMP, TMP, shifted_initialized_value); __ Bnez(TMP, slow_path->GetEntryLabel()); // Even if the initialized flag is set, we need to ensure consistent memory ordering. __ Sync(0); __ Bind(slow_path->GetExitLabel()); } void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check, Register temp) { uint32_t path_to_root = check->GetBitstringPathToRoot(); uint32_t mask = check->GetBitstringMask(); DCHECK(IsPowerOfTwo(mask + 1)); size_t mask_bits = WhichPowerOf2(mask + 1); if (mask_bits == 16u) { // Load only the bitstring part of the status word. __ LoadFromOffset( kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value()); // Compare the bitstring bits using XOR. __ Xori(temp, temp, dchecked_integral_cast
(path_to_root)); } else { // /* uint32_t */ temp = temp->status_ __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value()); // Compare the bitstring bits using XOR. if (IsUint<16>(path_to_root)) { __ Xori(temp, temp, dchecked_integral_cast
(path_to_root)); } else { __ LoadConst32(TMP, path_to_root); __ Xor(temp, temp, TMP); } // Shift out bits that do not contribute to the comparison. __ Sll(temp, temp, 32 - mask_bits); } } void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) { __ Sync(0); // Only stype 0 is supported. } void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor) { SuspendCheckSlowPathMIPS* slow_path = down_cast
(instruction->GetSlowPath()); if (slow_path == nullptr) { slow_path = new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor); instruction->SetSlowPath(slow_path); codegen_->AddSlowPath(slow_path); if (successor != nullptr) { DCHECK(successor->IsLoopHeader()); } } else { DCHECK_EQ(slow_path->GetSuccessor(), successor); } __ LoadFromOffset(kLoadUnsignedHalfword, TMP, TR, Thread::ThreadFlagsOffset
().Int32Value()); if (successor == nullptr) { __ Bnez(TMP, slow_path->GetEntryLabel()); __ Bind(slow_path->GetReturnLabel()); } else { __ Beqz(TMP, codegen_->GetLabelOf(successor)); __ B(slow_path->GetEntryLabel()); // slow_path will return to GetLabelOf(successor). } } InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph, CodeGeneratorMIPS* codegen) : InstructionCodeGenerator(graph, codegen), assembler_(codegen->GetAssembler()), codegen_(codegen) {} void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) { DCHECK_EQ(instruction->InputCount(), 2U); LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction); DataType::Type type = instruction->GetResultType(); bool isR6 = codegen_->GetInstructionSetFeatures().IsR6(); switch (type) { case DataType::Type::kInt32: { locations->SetInAt(0, Location::RequiresRegister()); HInstruction* right = instruction->InputAt(1); bool can_use_imm = false; if (right->IsConstant()) { int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant()); if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) { can_use_imm = IsUint<16>(imm); } else { DCHECK(instruction->IsSub() || instruction->IsAdd()); if (instruction->IsSub()) { imm = -imm; } if (isR6) { bool single_use = right->GetUses().HasExactlyOneElement(); int16_t imm_high = High16Bits(imm); int16_t imm_low = Low16Bits(imm); if (imm_low < 0) { imm_high += 1; } can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use; } else { can_use_imm = IsInt<16>(imm); } } } if (can_use_imm) locations->SetInAt(1, Location::ConstantLocation(right->AsConstant())); else locations->SetInAt(1, Location::RequiresRegister()); locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); break; } case DataType::Type::kInt64: { locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); break; } case DataType::Type::kFloat32: case DataType::Type::kFloat64: DCHECK(instruction->IsAdd() || instruction->IsSub()); locations->SetInAt(0, Location::RequiresFpuRegister()); locations->SetInAt(1, Location::RequiresFpuRegister()); locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); break; default: LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type; } } void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) { DataType::Type type = instruction->GetType(); LocationSummary* locations = instruction->GetLocations(); bool isR6 = codegen_->GetInstructionSetFeatures().IsR6(); switch (type) { case DataType::Type::kInt32: { Register dst = locations->Out().AsRegister
(); Register lhs = locations->InAt(0).AsRegister
(); Location rhs_location = locations->InAt(1); Register rhs_reg = ZERO; int32_t rhs_imm = 0; bool use_imm = rhs_location.IsConstant(); if (use_imm) { rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant()); } else { rhs_reg = rhs_location.AsRegister
(); } if (instruction->IsAnd()) { if (use_imm) __ Andi(dst, lhs, rhs_imm); else __ And(dst, lhs, rhs_reg); } else if (instruction->IsOr()) { if (use_imm) __ Ori(dst, lhs, rhs_imm); else __ Or(dst, lhs, rhs_reg); } else if (instruction->IsXor()) { if (use_imm) __ Xori(dst, lhs, rhs_imm); else __ Xor(dst, lhs, rhs_reg); } else { DCHECK(instruction->IsAdd() || instruction->IsSub()); if (use_imm) { if (instruction->IsSub()) { rhs_imm = -rhs_imm; } if (IsInt<16>(rhs_imm)) { __ Addiu(dst, lhs, rhs_imm); } else { DCHECK(isR6); int16_t rhs_imm_high = High16Bits(rhs_imm); int16_t rhs_imm_low = Low16Bits(rhs_imm); if (rhs_imm_low < 0) { rhs_imm_high += 1; } __ Aui(dst, lhs, rhs_imm_high); if (rhs_imm_low != 0) { __ Addiu(dst, dst, rhs_imm_low); } } } else if (instruction->IsAdd()) { __ Addu(dst, lhs, rhs_reg); } else { DCHECK(instruction->IsSub()); __ Subu(dst, lhs, rhs_reg); } } break; } case DataType::Type::kInt64: { Register dst_high = locations->Out().AsRegisterPairHigh
(); Register dst_low = locations->Out().AsRegisterPairLow
(); Register lhs_high = locations->InAt(0).AsRegisterPairHigh
(); Register lhs_low = locations->InAt(0).AsRegisterPairLow
(); Location rhs_location = locations->InAt(1); bool use_imm = rhs_location.IsConstant(); if (!use_imm) { Register rhs_high = rhs_location.AsRegisterPairHigh
(); Register rhs_low = rhs_location.AsRegisterPairLow
(); if (instruction->IsAnd()) { __ And(dst_low, lhs_low, rhs_low); __ And(dst_high, lhs_high, rhs_high); } else if (instruction->IsOr()) { __ Or(dst_low, lhs_low, rhs_low); __ Or(dst_high, lhs_high, rhs_high); } else if (instruction->IsXor()) { __ Xor(dst_low, lhs_low, rhs_low); __ Xor(dst_high, lhs_high, rhs_high); } else if (instruction->IsAdd()) { if (lhs_low == rhs_low) { // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs. __ Slt(TMP, lhs_low, ZERO); __ Addu(dst_low, lhs_low, rhs_low); } else { __ Addu(dst_low, lhs_low, rhs_low); // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged. __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low); } __ Addu(dst_high, lhs_high, rhs_high); __ Addu(dst_high, dst_high, TMP); } else { DCHECK(instruction->IsSub()); __ Sltu(TMP, lhs_low, rhs_low); __ Subu(dst_low, lhs_low, rhs_low); __ Subu(dst_high, lhs_high, rhs_high); __ Subu(dst_high, dst_high, TMP); } } else { int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant()); if (instruction->IsOr()) { uint32_t low = Low32Bits(value); uint32_t high = High32Bits(value); if (IsUint<16>(low)) { if (dst_low != lhs_low || low != 0) { __ Ori(dst_low, lhs_low, low); } } else { __ LoadConst32(TMP, low); __ Or(dst_low, lhs_low, TMP); } if (IsUint<16>(high)) { if (dst_high != lhs_high || high != 0) { __ Ori(dst_high, lhs_high, high); } } else { if (high != low) { __ LoadConst32(TMP, high); } __ Or(dst_high, lhs_high, TMP); } } else if (instruction->IsXor()) { uint32_t low = Low32Bits(value); uint32_t high = High32Bits(value); if (IsUint<16>(low)) { if (dst_low != lhs_low || low != 0) { __ Xori(dst_low, lhs_low, low); } } else { __ LoadConst32(TMP, low); __ Xor(dst_low, lhs_low, TMP); } if (IsUint<16>(high)) { if (dst_high != lhs_high || high != 0) { __ Xori(dst_high, lhs_high, high); } } else { if (high != low) { __ LoadConst32(TMP, high); } __ Xor(dst_high, lhs_high, TMP); } } else if (instruction->IsAnd()) { uint32_t low = Low32Bits(value); uint32_t high = High32Bits(value); if (IsUint<16>(low)) { __ Andi(dst_low, lhs_low, low); } else if (low != 0xFFFFFFFF) { __ LoadConst32(TMP, low); __ And(dst_low, lhs_low, TMP); } else if (dst_low != lhs_low) { __ Move(dst_low, lhs_low); } if (IsUint<16>(high)) { __ Andi(dst_high, lhs_high, high); } else if (high != 0xFFFFFFFF) { if (high != low) { __ LoadConst32(TMP, high); } __ And(dst_high, lhs_high, TMP); } else if (dst_high != lhs_high) { __ Move(dst_high, lhs_high); } } else { if (instruction->IsSub()) { value = -value; } else { DCHECK(instruction->IsAdd()); } int32_t low = Low32Bits(value); int32_t high = High32Bits(value); if (IsInt<16>(low)) { if (dst_low != lhs_low || low != 0) { __ Addiu(dst_low, lhs_low, low); } if (low != 0) { __ Sltiu(AT, dst_low, low); } } else { __ LoadConst32(TMP, low); __ Addu(dst_low, lhs_low, TMP); __ Sltu(AT, dst_low, TMP); } if (IsInt<16>(high)) { if (dst_high != lhs_high || high != 0) { __ Addiu(dst_high, lhs_high, high); } } else { if (high != low) { __ LoadConst32(TMP, high); } __ Addu(dst_high, lhs_high, TMP); } if (low != 0) { __ Addu(dst_high, dst_high, AT); } } } break; } case DataType::Type::kFloat32: case DataType::Type::kFloat64: { FRegister dst = locations->Out().AsFpuRegister
(); FRegister lhs = locations->InAt(0).AsFpuRegister
(); FRegister rhs = locations->InAt(1).AsFpuRegister
(); if (instruction->IsAdd()) { if (type == DataType::Type::kFloat32) { __ AddS(dst, lhs, rhs); } else { __ AddD(dst, lhs, rhs); } } else { DCHECK(instruction->IsSub()); if (type == DataType::Type::kFloat32) { __ SubS(dst, lhs, rhs); } else { __ SubD(dst, lhs, rhs); } } break; } default: LOG(FATAL) << "Unexpected binary operation type " << type; } } void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) { DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor()); LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr); DataType::Type type = instr->GetResultType(); switch (type) { case DataType::Type::kInt32: locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1))); locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); break; case DataType::Type::kInt64: locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1))); locations->SetOut(Location::RequiresRegister()); break; default: LOG(FATAL) << "Unexpected shift type " << type; } } static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte; void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) { DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor()); LocationSummary* locations = instr->GetLocations(); DataType::Type type = instr->GetType(); Location rhs_location = locations->InAt(1); bool use_imm = rhs_location.IsConstant(); Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister
(); int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0; const uint32_t shift_mask = (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance; const uint32_t shift_value = rhs_imm & shift_mask; // Are the INS (Insert Bit Field) and ROTR instructions supported? bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2(); switch (type) { case DataType::Type::kInt32: { Register dst = locations->Out().AsRegister
(); Register lhs = locations->InAt(0).AsRegister
(); if (use_imm) { if (shift_value == 0) { if (dst != lhs) { __ Move(dst, lhs); } } else if (instr->IsShl()) { __ Sll(dst, lhs, shift_value); } else if (instr->IsShr()) { __ Sra(dst, lhs, shift_value); } else if (instr->IsUShr()) { __ Srl(dst, lhs, shift_value); } else { if (has_ins_rotr) { __ Rotr(dst, lhs, shift_value); } else { __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask); __ Srl(dst, lhs, shift_value); __ Or(dst, dst, TMP); } } } else { if (instr->IsShl()) { __ Sllv(dst, lhs, rhs_reg); } else if (instr->IsShr()) { __ Srav(dst, lhs, rhs_reg); } else if (instr->IsUShr()) { __ Srlv(dst, lhs, rhs_reg); } else { if (has_ins_rotr) { __ Rotrv(dst, lhs, rhs_reg); } else { __ Subu(TMP, ZERO, rhs_reg); // 32-bit shift instructions use the 5 least significant bits of the shift count, so // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`, // IOW, the OR'd values are equal. __ Sllv(TMP, lhs, TMP); __ Srlv(dst, lhs, rhs_reg); __ Or(dst, dst, TMP); } } } break; } case DataType::Type::kInt64: { Register dst_high = locations->Out().AsRegisterPairHigh
(); Register dst_low = locations->Out().AsRegisterPairLow
(); Register lhs_high = locations->InAt(0).AsRegisterPairHigh
(); Register lhs_low = locations->InAt(0).AsRegisterPairLow
(); if (use_imm) { if (shift_value == 0) { codegen_->MoveLocation(locations->Out(), locations->InAt(0), type); } else if (shift_value < kMipsBitsPerWord) { if (has_ins_rotr) { if (instr->IsShl()) { __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value); __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value); __ Sll(dst_low, lhs_low, shift_value); } else if (instr->IsShr()) { __ Srl(dst_low, lhs_low, shift_value); __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value); __ Sra(dst_high, lhs_high, shift_value); } else if (instr->IsUShr()) { __ Srl(dst_low, lhs_low, shift_value); __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value); __ Srl(dst_high, lhs_high, shift_value); } else { __ Srl(dst_low, lhs_low, shift_value); __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value); __ Srl(dst_high, lhs_high, shift_value); __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value); } } else { if (instr->IsShl()) { __ Sll(dst_low, lhs_low, shift_value); __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value); __ Sll(dst_high, lhs_high, shift_value); __ Or(dst_high, dst_high, TMP); } else if (instr->IsShr()) { __ Sra(dst_high, lhs_high, shift_value); __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value); __ Srl(dst_low, lhs_low, shift_value); __ Or(dst_low, dst_low, TMP); } else if (instr->IsUShr()) { __ Srl(dst_high, lhs_high, shift_value); __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value); __ Srl(dst_low, lhs_low, shift_value); __ Or(dst_low, dst_low, TMP); } else { __ Srl(TMP, lhs_low, shift_value); __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value); __ Or(dst_low, dst_low, TMP); __ Srl(TMP, lhs_high, shift_value); __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value); __ Or(dst_high, dst_high, TMP); } } } else { const uint32_t shift_value_high = shift_value - kMipsBitsPerWord; if (instr->IsShl()) { __ Sll(dst_high, lhs_low, shift_value_high); __ Move(dst_low, ZERO); } else if (instr->IsShr()) { __ Sra(dst_low, lhs_high, shift_value_high); __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1); } else if (instr->IsUShr()) { __ Srl(dst_low, lhs_high, shift_value_high); __ Move(dst_high, ZERO); } else { if (shift_value == kMipsBitsPerWord) { // 64-bit rotation by 32 is just a swap. __ Move(dst_low, lhs_high); __ Move(dst_high, lhs_low); } else { if (has_ins_rotr) { __ Srl(dst_low, lhs_high, shift_value_high); __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high); __ Srl(dst_high, lhs_low, shift_value_high); __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high); } else { __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high); __ Srl(dst_low, lhs_high, shift_value_high); __ Or(dst_low, dst_low, TMP); __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high); __ Srl(dst_high, lhs_low, shift_value_high); __ Or(dst_high, dst_high, TMP); } } } } } else { const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6(); MipsLabel done; if (instr->IsShl()) { __ Sllv(dst_low, lhs_low, rhs_reg); __ Nor(AT, ZERO, rhs_reg); __ Srl(TMP, lhs_low, 1); __ Srlv(TMP, TMP, AT); __ Sllv(dst_high, lhs_high, rhs_reg); __ Or(dst_high, dst_high, TMP); __ Andi(TMP, rhs_reg, kMipsBitsPerWord); if (isR6) { __ Beqzc(TMP, &done, /* is_bare= */ true); __ Move(dst_high, dst_low); __ Move(dst_low, ZERO); } else { __ Movn(dst_high, dst_low, TMP); __ Movn(dst_low, ZERO, TMP); } } else if (instr->IsShr()) { __ Srav(dst_high, lhs_high, rhs_reg); __ Nor(AT, ZERO, rhs_reg); __ Sll(TMP, lhs_high, 1); __ Sllv(TMP, TMP, AT); __ Srlv(dst_low, lhs_low, rhs_reg); __ Or(dst_low, dst_low, TMP); __ Andi(TMP, rhs_reg, kMipsBitsPerWord); if (isR6) { __ Beqzc(TMP, &done, /* is_bare= */ true); __ Move(dst_low, dst_high); __ Sra(dst_high, dst_high, 31); } else { __ Sra(AT, dst_high, 31); __ Movn(dst_low, dst_high, TMP); __ Movn(dst_high, AT, TMP); } } else if (instr->IsUShr()) { __ Srlv(dst_high, lhs_high, rhs_reg); __ Nor(AT, ZERO, rhs_reg); __ Sll(TMP, lhs_high, 1); __ Sllv(TMP, TMP, AT); __ Srlv(dst_low, lhs_low, rhs_reg); __ Or(dst_low, dst_low, TMP); __ Andi(TMP, rhs_reg, kMipsBitsPerWord); if (isR6) { __ Beqzc(TMP, &done, /* is_bare= */ true); __ Move(dst_low, dst_high); __ Move(dst_high, ZERO); } else { __ Movn(dst_low, dst_high, TMP); __ Movn(dst_high, ZERO, TMP); } } else { // Rotate. __ Nor(AT, ZERO, rhs_reg); __ Srlv(TMP, lhs_low, rhs_reg); __ Sll(dst_low, lhs_high, 1); __ Sllv(dst_low, dst_low, AT); __ Or(dst_low, dst_low, TMP); __ Srlv(TMP, lhs_high, rhs_reg); __ Sll(dst_high, lhs_low, 1); __ Sllv(dst_high, dst_high, AT); __ Or(dst_high, dst_high, TMP); __ Andi(TMP, rhs_reg, kMipsBitsPerWord); if (isR6) { __ Beqzc(TMP, &done, /* is_bare= */ true); __ Move(TMP, dst_high); __ Move(dst_high, dst_low); __ Move(dst_low, TMP); } else { __ Movn(AT, dst_high, TMP); __ Movn(dst_high, dst_low, TMP); __ Movn(dst_low, AT, TMP); } } __ Bind(&done); } break; } default: LOG(FATAL) << "Unexpected shift operation type " << type; } } void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) { HandleBinaryOp(instruction); } void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) { HandleBinaryOp(instruction); } void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) { HandleBinaryOp(instruction); } void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) { HandleBinaryOp(instruction); } void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) { DataType::Type type = instruction->GetType(); bool object_array_get_with_read_barrier = kEmitCompilerReadBarrier && (type == DataType::Type::kReference); LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction, object_array_get_with_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall); if (object_array_get_with_read_barrier && kUseBakerReadBarrier) { locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers. } locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); if (DataType::IsFloatingPointType(type)) { locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); } else { // The output overlaps in the case of an object array get with // read barriers enabled: we do not want the move to overwrite the // array's location, as we need it to emit the read barrier. locations->SetOut(Location::RequiresRegister(), object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap); } // We need a temporary register for the read barrier marking slow // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier. if (object_array_get_with_read_barrier && kUseBakerReadBarrier) { bool temp_needed = instruction->GetIndex()->IsConstant() ? !kBakerReadBarrierThunksEnableForFields : !kBakerReadBarrierThunksEnableForArrays; if (temp_needed) { locations->AddTemp(Location::RequiresRegister()); } } } static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) { auto null_checker = [codegen, instruction]() { codegen->MaybeRecordImplicitNullCheck(instruction); }; return null_checker; } void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) { LocationSummary* locations = instruction->GetLocations(); Location obj_loc = locations->InAt(0); Register obj = obj_loc.AsRegister
(); Location out_loc = locations->Out(); Location index = locations->InAt(1); uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction); auto null_checker = GetImplicitNullChecker(instruction, codegen_); DataType::Type type = instruction->GetType(); const bool maybe_compressed_char_at = mirror::kUseStringCompression && instruction->IsStringCharAt(); switch (type) { case DataType::Type::kBool: case DataType::Type::kUint8: { Register out = out_loc.AsRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset; __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker); } else { __ Addu(TMP, obj, index.AsRegister
()); __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker); } break; } case DataType::Type::kInt8: { Register out = out_loc.AsRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset; __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker); } else { __ Addu(TMP, obj, index.AsRegister
()); __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker); } break; } case DataType::Type::kUint16: { Register out = out_loc.AsRegister
(); if (maybe_compressed_char_at) { uint32_t count_offset = mirror::String::CountOffset().Uint32Value(); __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker); __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP. static_assert(static_cast
(mirror::StringCompressionFlag::kCompressed) == 0u, "Expecting 0=compressed, 1=uncompressed"); } if (index.IsConstant()) { int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue(); if (maybe_compressed_char_at) { MipsLabel uncompressed_load, done; __ Bnez(TMP, &uncompressed_load); __ LoadFromOffset(kLoadUnsignedByte, out, obj, data_offset + (const_index << TIMES_1)); __ B(&done); __ Bind(&uncompressed_load); __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, data_offset + (const_index << TIMES_2)); __ Bind(&done); } else { __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, data_offset + (const_index << TIMES_2), null_checker); } } else { Register index_reg = index.AsRegister
(); if (maybe_compressed_char_at) { MipsLabel uncompressed_load, done; __ Bnez(TMP, &uncompressed_load); __ Addu(TMP, obj, index_reg); __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset); __ B(&done); __ Bind(&uncompressed_load); __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP); __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset); __ Bind(&done); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index_reg, obj); __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP); __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker); } } break; } case DataType::Type::kInt16: { Register out = out_loc.AsRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset; __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index.AsRegister
(), obj); __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_2, TMP); __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker); } break; } case DataType::Type::kInt32: { DCHECK_EQ(sizeof(mirror::HeapReference
), sizeof(int32_t)); Register out = out_loc.AsRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index.AsRegister
(), obj); __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_4, TMP); __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker); } break; } case DataType::Type::kReference: { static_assert( sizeof(mirror::HeapReference
) == sizeof(int32_t), "art::mirror::HeapReference
and int32_t have different sizes."); // /* HeapReference
*/ out = // *(obj + data_offset + index * sizeof(HeapReference
)) if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) { bool temp_needed = index.IsConstant() ? !kBakerReadBarrierThunksEnableForFields : !kBakerReadBarrierThunksEnableForArrays; Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation(); // Note that a potential implicit null check is handled in this // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call. DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0))); if (index.IsConstant()) { // Array load with a constant index can be treated as a field load. size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction, out_loc, obj, offset, temp, /* needs_null_check= */ false); } else { codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check= */ false); } } else { Register out = out_loc.AsRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker); // If read barriers are enabled, emit read barriers other than // Baker's using a slow path (and also unpoison the loaded // reference, if heap poisoning is enabled). codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_4, TMP); __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker); // If read barriers are enabled, emit read barriers other than // Baker's using a slow path (and also unpoison the loaded // reference, if heap poisoning is enabled). codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, data_offset, index); } } break; } case DataType::Type::kInt64: { Register out = out_loc.AsRegisterPairLow
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index.AsRegister
(), obj); __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_8, TMP); __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker); } break; } case DataType::Type::kFloat32: { FRegister out = out_loc.AsFpuRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; __ LoadSFromOffset(out, obj, offset, null_checker); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index.AsRegister
(), obj); __ LoadSFromOffset(out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_4, TMP); __ LoadSFromOffset(out, TMP, data_offset, null_checker); } break; } case DataType::Type::kFloat64: { FRegister out = out_loc.AsFpuRegister
(); if (index.IsConstant()) { size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; __ LoadDFromOffset(out, obj, offset, null_checker); } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(TMP, index.AsRegister
(), obj); __ LoadDFromOffset(out, TMP, data_offset, null_checker); } else { __ ShiftAndAdd(TMP, index.AsRegister
(), obj, TIMES_8, TMP); __ LoadDFromOffset(out, TMP, data_offset, null_checker); } break; } case DataType::Type::kUint32: case DataType::Type::kUint64: case DataType::Type::kVoid: LOG(FATAL) << "Unreachable type " << instruction->GetType(); UNREACHABLE(); } } void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) { LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction); locations->SetInAt(0, Location::RequiresRegister()); locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); } void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) { LocationSummary* locations = instruction->GetLocations(); uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction); Register obj = locations->InAt(0).AsRegister
(); Register out = locations->Out().AsRegister
(); __ LoadFromOffset(kLoadWord, out, obj, offset); codegen_->MaybeRecordImplicitNullCheck(instruction); // Mask out compression flag from String's array length. if (mirror::kUseStringCompression && instruction->IsStringLength()) { __ Srl(out, out, 1u); } } Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) { return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern()) ? Location::ConstantLocation(instruction->AsConstant()) : Location::RequiresRegister(); } Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) { // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register. // We can store a non-zero float or double constant without first loading it into the FPU, // but we should only prefer this if the constant has a single use. if (instruction->IsConstant() && (instruction->AsConstant()->IsZeroBitPattern() || instruction->GetUses().HasExactlyOneElement())) { return Location::ConstantLocation(instruction->AsConstant()); // Otherwise fall through and require an FPU register for the constant. } return Location::RequiresFpuRegister(); } void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) { DataType::Type value_type = instruction->GetComponentType(); bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck(); LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary( instruction, may_need_runtime_call_for_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall); locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) { locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2))); } else { locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2))); } if (needs_write_barrier) { // Temporary register for the write barrier. locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too. } } void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) { LocationSummary* locations = instruction->GetLocations(); Register obj = locations->InAt(0).AsRegister
(); Location index = locations->InAt(1); Location value_location = locations->InAt(2); DataType::Type value_type = instruction->GetComponentType(); bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck(); bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); auto null_checker = GetImplicitNullChecker(instruction, codegen_); Register base_reg = index.IsConstant() ? obj : TMP; switch (value_type) { case DataType::Type::kBool: case DataType::Type::kUint8: case DataType::Type::kInt8: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1; } else { __ Addu(base_reg, obj, index.AsRegister
()); } if (value_location.IsConstant()) { int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker); } else { Register value = value_location.AsRegister
(); __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kUint16: case DataType::Type::kInt16: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_2, base_reg); } if (value_location.IsConstant()) { int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker); } else { Register value = value_location.AsRegister
(); __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kInt32: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_4, base_reg); } if (value_location.IsConstant()) { int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker); } else { Register value = value_location.AsRegister
(); __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kReference: { if (value_location.IsConstant()) { // Just setting null. uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4; } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_4, base_reg); } int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant()); DCHECK_EQ(value, 0); __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker); DCHECK(!needs_write_barrier); DCHECK(!may_need_runtime_call_for_type_check); break; } DCHECK(needs_write_barrier); Register value = value_location.AsRegister
(); Register temp1 = locations->GetTemp(0).AsRegister
(); Register temp2 = TMP; // Doesn't need to survive slow path. uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); MipsLabel done; SlowPathCodeMIPS* slow_path = nullptr; if (may_need_runtime_call_for_type_check) { slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction); codegen_->AddSlowPath(slow_path); if (instruction->GetValueCanBeNull()) { MipsLabel non_zero; __ Bnez(value, &non_zero); uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_4, base_reg); } __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker); __ B(&done); __ Bind(&non_zero); } // Note that when read barriers are enabled, the type checks // are performed without read barriers. This is fine, even in // the case where a class object is in the from-space after // the flip, as a comparison involving such a type would not // produce a false positive; it may of course produce a false // negative, in which case we would take the ArraySet slow // path. // /* HeapReference
*/ temp1 = obj->klass_ __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker); __ MaybeUnpoisonHeapReference(temp1); // /* HeapReference
*/ temp1 = temp1->component_type_ __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset); // /* HeapReference
*/ temp2 = value->klass_ __ LoadFromOffset(kLoadWord, temp2, value, class_offset); // If heap poisoning is enabled, no need to unpoison `temp1` // nor `temp2`, as we are comparing two poisoned references. if (instruction->StaticTypeOfArrayIsObjectArray()) { MipsLabel do_put; __ Beq(temp1, temp2, &do_put); // If heap poisoning is enabled, the `temp1` reference has // not been unpoisoned yet; unpoison it now. __ MaybeUnpoisonHeapReference(temp1); // /* HeapReference
*/ temp1 = temp1->super_class_ __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset); // If heap poisoning is enabled, no need to unpoison // `temp1`, as we are comparing against null below. __ Bnez(temp1, slow_path->GetEntryLabel()); __ Bind(&do_put); } else { __ Bne(temp1, temp2, slow_path->GetEntryLabel()); } } Register source = value; if (kPoisonHeapReferences) { // Note that in the case where `value` is a null reference, // we do not enter this block, as a null reference does not // need poisoning. __ Move(temp1, value); __ PoisonHeapReference(temp1); source = temp1; } uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4; } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_4, base_reg); } __ StoreToOffset(kStoreWord, source, base_reg, data_offset); if (!may_need_runtime_call_for_type_check) { codegen_->MaybeRecordImplicitNullCheck(instruction); } codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull()); if (done.IsLinked()) { __ Bind(&done); } if (slow_path != nullptr) { __ Bind(slow_path->GetExitLabel()); } break; } case DataType::Type::kInt64: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_8, base_reg); } if (value_location.IsConstant()) { int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker); } else { Register value = value_location.AsRegisterPairLow
(); __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kFloat32: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_4, base_reg); } if (value_location.IsConstant()) { int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker); } else { FRegister value = value_location.AsFpuRegister
(); __ StoreSToOffset(value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kFloat64: { uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value(); if (index.IsConstant()) { data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8; } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) { __ Addu(base_reg, index.AsRegister
(), obj); } else { __ ShiftAndAdd(base_reg, index.AsRegister
(), obj, TIMES_8, base_reg); } if (value_location.IsConstant()) { int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant()); __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker); } else { FRegister value = value_location.AsFpuRegister
(); __ StoreDToOffset(value, base_reg, data_offset, null_checker); } break; } case DataType::Type::kUint32: case DataType::Type::kUint64: case DataType::Type::kVoid: LOG(FATAL) << "Unreachable type " << instruction->GetType(); UNREACHABLE(); } } void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex( HIntermediateArrayAddressIndex* instruction) { LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall); HIntConstant* shift = instruction->GetShift()->AsIntConstant(); locations->SetInAt(0, Location::RequiresRegister()); locations->SetInAt(1, Location::ConstantLocation(shift)); locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); } void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex( HIntermediateArrayAddressIndex* instruction) { LocationSummary* locations = instruction->GetLocations(); Register index_reg = locations->InAt(0).AsRegister
(); uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue(); __ Sll(locations->Out().AsRegister
(), index_reg, shift); } void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) { RegisterSet caller_saves = RegisterSet::Empty(); InvokeRuntimeCallingConvention calling_convention; caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1))); LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves); HInstruction* index = instruction->InputAt(0); HInstruction* length = instruction->InputAt(1); bool const_index = false; bool const_length = false; if (index->IsConstant()) { if (length->IsConstant()) { const_index = true; const_length = true; } else { int32_t index_value = index->AsIntConstant()->GetValue(); if (index_value < 0 || IsInt<16>(index_value + 1)) { const_index = true; } } } else if (length->IsConstant()) { int32_t length_value = length->AsIntConstant()->GetValue(); if (IsUint<15>(length_value)) { const_length = true; } } locations->SetInAt(0, const_index ? Location::ConstantLocation(index->AsConstant()) : Location::RequiresRegister()); locations->SetInAt(1, const_length ? Location::ConstantLocation(length->AsConstant()) : Location::RequiresRegister()); } void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) { LocationSummary* locations = instruction->GetLocations(); Location index_loc = locations->InAt(0); Location length_loc = locations->InAt(1); if (length_loc.IsConstant()) { int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue(); if (index_loc.IsConstant()) { int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue(); if (index < 0 || index >= length) { BoundsCheckSlowPathMIPS* slow_path = new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction); codegen_->AddSlowPath(slow_path); __ B(slow_path->GetEntryLabel()); } else { // Nothing to be done. } return; } BoundsCheckSlowPathMIPS* slow_path = new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction); codegen_->AddSlowPath(slow_path); Register index = index_loc.AsRegister
(); if (length == 0) { __ B(slow_path->GetEntryLabel()); } else if (length == 1) { __ Bnez(index, slow_path->GetEntryLabel()); } else { DCHECK(IsUint<15>(length)) << length; __ Sltiu(TMP, index, length); __ Beqz(TMP, slow_path->GetEntryLabel()); } } else { Register length = length_loc.AsRegister
(); BoundsCheckSlowPathMIPS* slow_path = new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction); codegen_->AddSlowPath(slow_path); if (index_loc.IsConstant()) { int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue(); if (index < 0) { __ B(slow_path->GetEntryLabel()); } else if (index == 0) { __ Blez(length, slow_path->GetEntryLabel()); } else { DCHECK(IsInt<16>(index + 1)) << index; __ Sltiu(TMP, length, index + 1); __ Bnez(TMP, slow_path->GetEntryLabel()); } } else { Register index = index_loc.AsRegister
(); __ Bgeu(index, length, slow_path->GetEntryLabel()); } } } // Temp is used for read barrier. static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) { if (kEmitCompilerReadBarrier && !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) && (kUseBakerReadBarrier || type_check_kind == TypeCheckKind::kAbstractClassCheck || type_check_kind == TypeCheckKind::kClassHierarchyCheck || type_check_kind == TypeCheckKind::kArrayObjectCheck)) { return 1; } return 0; } // Extra temp is used for read barrier. static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) { return 1 + NumberOfInstanceOfTemps(type_check_kind); } void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) { TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction); LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind); locations->SetInAt(0, Location::RequiresRegister()); if (type_check_kind == TypeCheckKind::kBitstringCheck) { locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant())); locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant())); locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant())); } else { locations->SetInAt(1, Location::RequiresRegister()); } locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind)); } void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) { TypeCheckKind type_check_kind = instruction->GetTypeCheckKind(); LocationSummary* locations = instruction->GetLocations(); Location obj_loc = locations->InAt(0); Register obj = obj_loc.AsRegister
(); Location cls = locations->InAt(1); Location temp_loc = locations->GetTemp(0); Register temp = temp_loc.AsRegister
(); const size_t num_temps = NumberOfCheckCastTemps(type_check_kind); DCHECK_LE(num_temps, 2u); Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation(); const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value(); const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value(); const uint32_t object_array_data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value(); MipsLabel done; bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction); SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS( instruction, is_type_check_slow_path_fatal); codegen_->AddSlowPath(slow_path); // Avoid this check if we know `obj` is not null. if (instruction->MustDoNullCheck()) { __ Beqz(obj, &done); } switch (type_check_kind) { case TypeCheckKind::kExactCheck: case TypeCheckKind::kArrayCheck: { // /* HeapReference
*/ temp = obj->klass_ GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc, kWithoutReadBarrier); // Jump to slow path for throwing the exception or doing a // more involved array check. __ Bne(temp, cls.AsRegister
(), slow_path->GetEntryLabel()); break; } case TypeCheckKind::kAbstractClassCheck: { // /* HeapReference
*/ temp = obj->klass_ GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc, kWithoutReadBarrier); // If the class is abstract, we eagerly fetch the super class of the // object to avoid doing a comparison we know will fail. MipsLabel loop; __ Bind(&loop); // /* HeapReference
*/ temp = temp->super_class_ GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc, kWithoutReadBarrier); // If the class reference currently in `temp` is null, jump to the slow path to throw the // exception. __ Beqz(temp, slow_path->GetEntryLabel()); // Otherwise, compare the classes. __ Bne(temp, cls.AsRegister