blob: 0c2f9159d072f68ad015c57e105e57fa70b4d02d [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
19#include "object.h"
20
21namespace art {
22namespace mips {
23
Mathieu Chartier67022432012-11-29 18:04:50 -080024static const uint32_t gZero = 0;
25
26void MipsContext::Reset() {
27 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
28 gprs_[i] = NULL;
29 }
30 for (size_t i = 0; i < kNumberOfFRegisters; i++) {
31 fprs_[i] = NULL;
32 }
33 gprs_[SP] = &sp_;
34 gprs_[RA] = &ra_;
jeffhao7fbee072012-08-24 17:56:54 -070035 // Initialize registers with easy to spot debug values.
Mathieu Chartier67022432012-11-29 18:04:50 -080036 sp_ = MipsContext::kBadGprBase + SP;
37 ra_ = MipsContext::kBadGprBase + RA;
jeffhao7fbee072012-08-24 17:56:54 -070038}
39
40void MipsContext::FillCalleeSaves(const StackVisitor& fr) {
Mathieu Chartier66f19252012-09-18 08:57:04 -070041 AbstractMethod* method = fr.GetMethod();
jeffhao7fbee072012-08-24 17:56:54 -070042 uint32_t core_spills = method->GetCoreSpillMask();
43 uint32_t fp_core_spills = method->GetFpSpillMask();
44 size_t spill_count = __builtin_popcount(core_spills);
45 size_t fp_spill_count = __builtin_popcount(fp_core_spills);
46 size_t frame_size = method->GetFrameSizeInBytes();
47 if (spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080048 // Lowest number spill is farthest away, walk registers and fill into context.
jeffhao7fbee072012-08-24 17:56:54 -070049 int j = 1;
Mathieu Chartier67022432012-11-29 18:04:50 -080050 for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
jeffhao7fbee072012-08-24 17:56:54 -070051 if (((core_spills >> i) & 1) != 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080052 gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
jeffhao7fbee072012-08-24 17:56:54 -070053 j++;
54 }
55 }
56 }
57 if (fp_spill_count > 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080058 // Lowest number spill is farthest away, walk registers and fill into context.
jeffhao7fbee072012-08-24 17:56:54 -070059 int j = 1;
Mathieu Chartier67022432012-11-29 18:04:50 -080060 for (size_t i = 0; i < kNumberOfFRegisters; i++) {
jeffhao7fbee072012-08-24 17:56:54 -070061 if (((fp_core_spills >> i) & 1) != 0) {
Mathieu Chartier67022432012-11-29 18:04:50 -080062 fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j, frame_size);
jeffhao7fbee072012-08-24 17:56:54 -070063 j++;
64 }
65 }
66 }
67}
68
Mathieu Chartier67022432012-11-29 18:04:50 -080069void MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
70 CHECK_LT(reg, kNumberOfCoreRegisters);
71 CHECK_NE(gprs_[reg], &gZero); // Can't overwrite this static value since they are never reset.
72 CHECK(gprs_[reg] != NULL);
73 *gprs_[reg] = value;
74}
75
jeffhao7fbee072012-08-24 17:56:54 -070076void MipsContext::SmashCallerSaves() {
Mathieu Chartier67022432012-11-29 18:04:50 -080077 // This needs to be 0 because we want a null/zero return value.
78 gprs_[V0] = const_cast<uint32_t*>(&gZero);
79 gprs_[V1] = const_cast<uint32_t*>(&gZero);
80 gprs_[A1] = NULL;
81 gprs_[A2] = NULL;
82 gprs_[A3] = NULL;
jeffhao7fbee072012-08-24 17:56:54 -070083}
84
85extern "C" void art_do_long_jump(uint32_t*, uint32_t*);
86
87void MipsContext::DoLongJump() {
Mathieu Chartier67022432012-11-29 18:04:50 -080088 uintptr_t gprs[kNumberOfCoreRegisters];
89 uint32_t fprs[kNumberOfFRegisters];
90 for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
91 gprs[i] = gprs_[i] != NULL ? *gprs_[i] : MipsContext::kBadGprBase + i;
92 }
93 for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
94 fprs[i] = fprs_[i] != NULL ? *fprs_[i] : MipsContext::kBadGprBase + i;
95 }
96 art_do_long_jump(gprs, fprs);
jeffhao7fbee072012-08-24 17:56:54 -070097}
98
99} // namespace mips
100} // namespace art