blob: 08ab356855827d8857dbb67685cc53ee6a64b41e [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010020#include "base/bit_utils.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_;
32 gprs_[RA] = &ra_;
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;
36 ra_ = MipsContext::kBadGprBase + RA;
Andreas Gampe639bdd12015-06-03 11:22:45 -070037 arg0_ = 0;
jeffhao7fbee072012-08-24 17:56:54 -070038}
39
40void MipsContext::FillCalleeSaves(const StackVisitor& fr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070041 ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010042 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
Vladimir Marko80afd022015-05-19 18:08:00 +010043 int spill_pos = 0;
44
45 // Core registers come first, from the highest down to the lowest.
46 for (uint32_t core_reg : HighToLowBits(frame_info.CoreSpillMask())) {
47 gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
48 ++spill_pos;
jeffhao7fbee072012-08-24 17:56:54 -070049 }
Vladimir Marko80afd022015-05-19 18:08:00 +010050 DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()));
51
52 // FP registers come second, from the highest down to the lowest.
53 for (uint32_t fp_reg : HighToLowBits(frame_info.FpSpillMask())) {
54 fprs_[fp_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
55 ++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()) + POPCOUNT(frame_info.FpSpillMask()));
jeffhao7fbee072012-08-24 17:56:54 -070058}
59
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010060void MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom8b1ce162013-03-31 00:17:54 -070061 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010062 DCHECK(IsAccessibleGPR(reg));
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063 CHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010064 *gprs_[reg] = value;
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020065}
66
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010067void MipsContext::SetFPR(uint32_t reg, uintptr_t value) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020068 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters));
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010069 DCHECK(IsAccessibleFPR(reg));
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020070 CHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz96ba8dc2015-01-22 18:57:14 +010071 *fprs_[reg] = value;
Mathieu Chartier67022432012-11-29 18:04:50 -080072}
73
jeffhao7fbee072012-08-24 17:56:54 -070074void MipsContext::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080075 // This needs to be 0 because we want a null/zero return value.
76 gprs_[V0] = const_cast<uint32_t*>(&gZero);
77 gprs_[V1] = const_cast<uint32_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020078 gprs_[A1] = nullptr;
79 gprs_[A2] = nullptr;
80 gprs_[A3] = nullptr;
Goran Jakovljevicff734982015-08-24 12:58:55 +000081
82 fprs_[F12] = nullptr;
83 fprs_[F13] = nullptr;
84 fprs_[F14] = nullptr;
85 fprs_[F15] = nullptr;
jeffhao7fbee072012-08-24 17:56:54 -070086}
87
Andreas Gampe794ad762015-02-23 08:12:24 -080088extern "C" NO_RETURN void art_quick_do_long_jump(uint32_t*, uint32_t*);
jeffhao7fbee072012-08-24 17:56:54 -070089
90void MipsContext::DoLongJump() {
Mathieu Chartier67022432012-11-29 18:04:50 -080091 uintptr_t gprs[kNumberOfCoreRegisters];
92 uint32_t fprs[kNumberOfFRegisters];
93 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020094 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : MipsContext::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -080095 }
96 for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
Vladimir Marko5b09ea02015-05-27 14:07:08 +010097 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : MipsContext::kBadFprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -080098 }
Logan Chien8dbb7082013-01-25 20:31:17 +080099 art_quick_do_long_jump(gprs, fprs);
jeffhao7fbee072012-08-24 17:56:54 -0700100}
101
102} // namespace mips
103} // namespace art