blob: 3f362de7ce2ac9439ab2fb3a4fbf0bb77e3269ee [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -07001/*
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
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "base/bit_utils.h"
Andreas Gampe5678db52017-06-08 14:11:18 -070020#include "base/bit_utils_iterator.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010021#include "quick/quick_method_frame_info.h"
jeffhao7fbee072012-08-24 17:56:54 -070022
23namespace art {
24namespace mips {
25
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020026static constexpr uint32_t gZero = 0;
Mathieu Chartier67022432012-11-29 18:04:50 -080027
28void MipsContext::Reset() {
Vladimir Marko80afd022015-05-19 18:08:00 +010029 std::fill_n(gprs_, arraysize(gprs_), nullptr);
30 std::fill_n(fprs_, arraysize(fprs_), nullptr);
Mathieu Chartier67022432012-11-29 18:04:50 -080031 gprs_[SP] = &sp_;
Goran Jakovljevic75969962015-10-27 12:29:07 +010032 gprs_[T9] = &t9_;
Andreas Gampe639bdd12015-06-03 11:22:45 -070033 gprs_[A0] = &arg0_;
jeffhao7fbee072012-08-24 17:56:54 -070034 // Initialize registers with easy to spot debug values.
Mathieu Chartier67022432012-11-29 18:04:50 -080035 sp_ = MipsContext::kBadGprBase + SP;
Goran Jakovljevic75969962015-10-27 12:29:07 +010036 t9_ = MipsContext::kBadGprBase + T9;
Andreas Gampe639bdd12015-06-03 11:22:45 -070037 arg0_ = 0;
jeffhao7fbee072012-08-24 17:56:54 -070038}
39
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010040void MipsContext::FillCalleeSaves(uint8_t* frame, const QuickMethodFrameInfo& frame_info) {
Vladimir Marko80afd022015-05-19 18:08:00 +010041 int spill_pos = 0;
42
43 // Core registers come first, from the highest down to the lowest.
44 for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask())) {
Chris Larsen715f43e2017-10-23 11:00:32 -070045 // If the $ZERO register shows up in the list of registers to
46 // be saved this was only done to properly align the floating
47 // point register save locations to addresses which are
48 // multiples of 8. We only store the address of a register in
49 // gprs_ if the register is not the $ZERO register. The $ZERO
50 // register is read-only so there's never a reason to save it
51 // on the stack.
52 if (core_reg != 0u) {
53 gprs_[core_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
54 }
Vladimir Marko80afd022015-05-19 18:08:00 +010055 ++spill_pos;
jeffhao7fbee072012-08-24 17:56:54 -070056 }
Vladimir Marko80afd022015-05-19 18:08:00 +010057 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
58
59 // FP registers come second, from the highest down to the lowest.
60 for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010061 fprs_[fp_reg] = CalleeSaveAddress(frame, spill_pos, frame_info.FrameSizeInBytes());
Vladimir Marko80afd022015-05-19 18:08:00 +010062 ++spill_pos;
jeffhao7fbee072012-08-24 17:56:54 -070063 }
Vladimir Marko80afd022015-05-19 18:08:00 +010064 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) + POPCOUNT(frame_info.FpSpillMask()));
jeffhao7fbee072012-08-24 17:56:54 -070065}
66
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010067void MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom8b1ce162013-03-31 00:17:54 -070068 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010069 DCHECK(IsAccessibleGPR(reg));
Brian Carlstrom7934ac22013-07-26 10:54:15 -070070 CHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010071 *gprs_[reg] = value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020072}
73
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010074void MipsContext::SetFPR(uint32_t reg, uintptr_t value) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020075 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010076 DCHECK(IsAccessibleFPR(reg));
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020077 CHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010078 *fprs_[reg] = value;
Mathieu Chartier67022432012-11-29 18:04:50 -080079}
80
jeffhao7fbee072012-08-24 17:56:54 -070081void MipsContext::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080082 // This needs to be 0 because we want a null/zero return value.
83 gprs_[V0] = const_cast<uint32_t*>(&gZero);
84 gprs_[V1] = const_cast<uint32_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020085 gprs_[A1] = nullptr;
86 gprs_[A2] = nullptr;
87 gprs_[A3] = nullptr;
Alexey Frunze1b8464d2016-11-12 17:22:05 -080088 gprs_[T0] = nullptr;
89 gprs_[T1] = nullptr;
Goran Jakovljevicff734982015-08-24 12:58:55 +000090
Alexey Frunze1b8464d2016-11-12 17:22:05 -080091 fprs_[F8] = nullptr;
92 fprs_[F9] = nullptr;
93 fprs_[F10] = nullptr;
94 fprs_[F11] = nullptr;
Goran Jakovljevicff734982015-08-24 12:58:55 +000095 fprs_[F12] = nullptr;
96 fprs_[F13] = nullptr;
97 fprs_[F14] = nullptr;
98 fprs_[F15] = nullptr;
Alexey Frunze1b8464d2016-11-12 17:22:05 -080099 fprs_[F16] = nullptr;
100 fprs_[F17] = nullptr;
101 fprs_[F18] = nullptr;
102 fprs_[F19] = nullptr;
jeffhao7fbee072012-08-24 17:56:54 -0700103}
104
Andreas Gampe794ad762015-02-23 08:12:24 -0800105extern "C" NO_RETURN void art_quick_do_long_jump(uint32_t*, uint32_t*);
jeffhao7fbee072012-08-24 17:56:54 -0700106
107void MipsContext::DoLongJump() {
Mathieu Chartier67022432012-11-29 18:04:50 -0800108 uintptr_t gprs[kNumberOfCoreRegisters];
Chris Larsen715f43e2017-10-23 11:00:32 -0700109 // Align fprs[] so that art_quick_do_long_jump() can load FPU
110 // registers from it using the ldc1 instruction.
111 uint32_t fprs[kNumberOfFRegisters] __attribute__((aligned(8)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800112 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200113 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : MipsContext::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800114 }
115 for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
Vladimir Marko5b09ea02015-05-27 14:07:08 +0100116 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : MipsContext::kBadFprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800117 }
Logan Chien8dbb7082013-01-25 20:31:17 +0800118 art_quick_do_long_jump(gprs, fprs);
jeffhao7fbee072012-08-24 17:56:54 -0700119}
120
121} // namespace mips
122} // namespace art