1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "context_mips.h"
18
19 #include "base/bit_utils.h"
20 #include "quick/quick_method_frame_info.h"
21
22 namespace art {
23 namespace mips {
24
25 static constexpr uint32_t gZero = 0;
26
Reset()27 void MipsContext::Reset() {
28 std::fill_n(gprs_, arraysize(gprs_), nullptr);
29 std::fill_n(fprs_, arraysize(fprs_), nullptr);
30 gprs_[SP] = &sp_;
31 gprs_[T9] = &t9_;
32 gprs_[A0] = &arg0_;
33 // Initialize registers with easy to spot debug values.
34 sp_ = MipsContext::kBadGprBase + SP;
35 t9_ = MipsContext::kBadGprBase + T9;
36 arg0_ = 0;
37 }
38
FillCalleeSaves(uint8_t * frame,const QuickMethodFrameInfo & frame_info)39 void MipsContext::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
40 int spill_pos = 0;
41
42 // Core registers come first, from the highest down to the lowest.
43 for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask())) {
44 gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
45 ++spill_pos;
46 }
47 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
48
49 // FP registers come second, from the highest down to the lowest.
50 for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
51 fprs_[fp_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
52 ++spill_pos;
53 }
54 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
55 }
56
SetGPR(uint32_t reg,uintptr_t value)57 void MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
58 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
59 DCHECK(IsAccessibleGPR(reg));
60 CHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
61 *gprs_[reg] = value;
62 }
63
SetFPR(uint32_t reg,uintptr_t value)64 void MipsContext::SetFPR(uint32_t reg, uintptr_t value) {
65 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters));
66 DCHECK(IsAccessibleFPR(reg));
67 CHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
68 *fprs_[reg] = value;
69 }
70
SmashCallerSaves()71 void MipsContext::SmashCallerSaves() {
72 // This needs to be 0 because we want a null/zero return value.
73 gprs_[V0] = const_cast<uint32_t*>(&gZero);
74 gprs_[V1] = const_cast<uint32_t*>(&gZero);
75 gprs_[A1] = nullptr;
76 gprs_[A2] = nullptr;
77 gprs_[A3] = nullptr;
78 gprs_[T0] = nullptr;
79 gprs_[T1] = nullptr;
80
81 fprs_[F8] = nullptr;
82 fprs_[F9] = nullptr;
83 fprs_[F10] = nullptr;
84 fprs_[F11] = nullptr;
85 fprs_[F12] = nullptr;
86 fprs_[F13] = nullptr;
87 fprs_[F14] = nullptr;
88 fprs_[F15] = nullptr;
89 fprs_[F16] = nullptr;
90 fprs_[F17] = nullptr;
91 fprs_[F18] = nullptr;
92 fprs_[F19] = nullptr;
93 }
94
95 extern "C" NO_RETURN void art_quick_do_long_jump(uint32_t*, uint32_t*);
96
DoLongJump()97 void MipsContext::DoLongJump() {
98 uintptr_t gprs[kNumberOfCoreRegisters];
99 uint32_t fprs[kNumberOfFRegisters];
100 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
101 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : MipsContext::kBadGprBase + i;
102 }
103 for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
104 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : MipsContext::kBadFprBase + i;
105 }
106 art_quick_do_long_jump(gprs, fprs);
107 }
108
109 } // namespace mips
110 } // namespace art
111