blob: 789dbbb6d7d564783e0d4e707cd818bafbd5ea37 [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 Marko7624d252014-05-02 14:40:15 +010019#include "mirror/art_method-inl.h"
Ian Rogers8e26b312013-03-18 18:48:21 -070020#include "mirror/object-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010021#include "quick/quick_method_frame_info.h"
Brian Carlstrom4a4552d2013-02-10 20:41:26 -080022#include "stack.h"
jeffhao7fbee072012-08-24 17:56:54 -070023
24namespace art {
25namespace mips {
26
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020027static constexpr uint32_t gZero = 0;
Mathieu Chartier67022432012-11-29 18:04:50 -080028
29void MipsContext::Reset() {
30 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020031 gprs_[i] = nullptr;
Mathieu Chartier67022432012-11-29 18:04:50 -080032 }
33 for (size_t i = 0; i < kNumberOfFRegisters; i++) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020034 fprs_[i] = nullptr;
Mathieu Chartier67022432012-11-29 18:04:50 -080035 }
36 gprs_[SP] = &sp_;
37 gprs_[RA] = &ra_;
jeffhao7fbee072012-08-24 17:56:54 -070038 // Initialize registers with easy to spot debug values.
Mathieu Chartier67022432012-11-29 18:04:50 -080039 sp_ = MipsContext::kBadGprBase + SP;
40 ra_ = MipsContext::kBadGprBase + RA;
jeffhao7fbee072012-08-24 17:56:54 -070041}
42
43void MipsContext::FillCalleeSaves(const StackVisitor& fr) {
Brian Carlstromea46f952013-07-30 01:26:50 -070044 mirror::ArtMethod* method = fr.GetMethod();
Vladimir Marko7624d252014-05-02 14:40:15 +010045 const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
46 size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
47 size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask());
jeffhao7fbee072012-08-24 17:56:54 -070048 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080049 // Lowest number spill is farthest away, walk registers and fill into context.
jeffhao7fbee072012-08-24 17:56:54 -070050 int j = 1;
Mathieu Chartier67022432012-11-29 18:04:50 -080051 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010052 if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
53 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
jeffhao7fbee072012-08-24 17:56:54 -070054 j++;
55 }
56 }
57 }
58 if (fp_spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080059 // Lowest number spill is farthest away, walk registers and fill into context.
jeffhao7fbee072012-08-24 17:56:54 -070060 int j = 1;
Mathieu Chartier67022432012-11-29 18:04:50 -080061 for (size_t i = 0; i < kNumberOfFRegisters; i++) {
Vladimir Marko7624d252014-05-02 14:40:15 +010062 if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
63 fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j,
64 frame_info.FrameSizeInBytes());
jeffhao7fbee072012-08-24 17:56:54 -070065 j++;
66 }
67 }
68 }
69}
70
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020071bool MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
Brian Carlstrom8b1ce162013-03-31 00:17:54 -070072 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
Brian Carlstrom7934ac22013-07-26 10:54:15 -070073 CHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020074 if (gprs_[reg] != nullptr) {
75 *gprs_[reg] = value;
76 return true;
77 } else {
78 return false;
79 }
80}
81
82bool MipsContext::SetFPR(uint32_t reg, uintptr_t value) {
83 CHECK_LT(reg, static_cast<uint32_t>(kNumberOfFRegisters));
84 CHECK_NE(fprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
85 if (fprs_[reg] != nullptr) {
86 *fprs_[reg] = value;
87 return true;
88 } else {
89 return false;
90 }
Mathieu Chartier67022432012-11-29 18:04:50 -080091}
92
jeffhao7fbee072012-08-24 17:56:54 -070093void MipsContext::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080094 // This needs to be 0 because we want a null/zero return value.
95 gprs_[V0] = const_cast<uint32_t*>(&gZero);
96 gprs_[V1] = const_cast<uint32_t*>(&gZero);
Sebastien Hertz0bcb2902014-06-17 15:52:45 +020097 gprs_[A1] = nullptr;
98 gprs_[A2] = nullptr;
99 gprs_[A3] = nullptr;
jeffhao7fbee072012-08-24 17:56:54 -0700100}
101
Logan Chien8dbb7082013-01-25 20:31:17 +0800102extern "C" void art_quick_do_long_jump(uint32_t*, uint32_t*);
jeffhao7fbee072012-08-24 17:56:54 -0700103
104void MipsContext::DoLongJump() {
Mathieu Chartier67022432012-11-29 18:04:50 -0800105 uintptr_t gprs[kNumberOfCoreRegisters];
106 uint32_t fprs[kNumberOfFRegisters];
107 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200108 gprs[i] = gprs_[i] != nullptr ? *gprs_[i] : MipsContext::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800109 }
110 for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200111 fprs[i] = fprs_[i] != nullptr ? *fprs_[i] : MipsContext::kBadGprBase + i;
Mathieu Chartier67022432012-11-29 18:04:50 -0800112 }
Logan Chien8dbb7082013-01-25 20:31:17 +0800113 art_quick_do_long_jump(gprs, fprs);
jeffhao7fbee072012-08-24 17:56:54 -0700114}
115
116} // namespace mips
117} // namespace art