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 "mirror/art_method.h"
20#include "mirror/object-inl.h"
21#include "stack.h"
22
23namespace art {
24namespace mips {
25
26static const uint32_t gZero = 0;
27
28void MipsContext::Reset() {
29  for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
30    gprs_[i] = NULL;
31  }
32  for (size_t i = 0; i < kNumberOfFRegisters; i++) {
33    fprs_[i] = NULL;
34  }
35  gprs_[SP] = &sp_;
36  gprs_[RA] = &ra_;
37  // Initialize registers with easy to spot debug values.
38  sp_ = MipsContext::kBadGprBase + SP;
39  ra_ = MipsContext::kBadGprBase + RA;
40}
41
42void MipsContext::FillCalleeSaves(const StackVisitor& fr) {
43  mirror::ArtMethod* method = fr.GetMethod();
44  uint32_t core_spills = method->GetCoreSpillMask();
45  uint32_t fp_core_spills = method->GetFpSpillMask();
46  size_t spill_count = __builtin_popcount(core_spills);
47  size_t fp_spill_count = __builtin_popcount(fp_core_spills);
48  size_t frame_size = method->GetFrameSizeInBytes();
49  if (spill_count > 0) {
50    // Lowest number spill is farthest away, walk registers and fill into context.
51    int j = 1;
52    for (size_t i = 0; i < kNumberOfCoreRegisters; i++) {
53      if (((core_spills >> i) & 1) != 0) {
54        gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_size);
55        j++;
56      }
57    }
58  }
59  if (fp_spill_count > 0) {
60    // Lowest number spill is farthest away, walk registers and fill into context.
61    int j = 1;
62    for (size_t i = 0; i < kNumberOfFRegisters; i++) {
63      if (((fp_core_spills >> i) & 1) != 0) {
64        fprs_[i] = fr.CalleeSaveAddress(spill_count + fp_spill_count - j, frame_size);
65        j++;
66      }
67    }
68  }
69}
70
71void MipsContext::SetGPR(uint32_t reg, uintptr_t value) {
72  CHECK_LT(reg, static_cast<uint32_t>(kNumberOfCoreRegisters));
73  CHECK_NE(gprs_[reg], &gZero);  // Can't overwrite this static value since they are never reset.
74  CHECK(gprs_[reg] != NULL);
75  *gprs_[reg] = value;
76}
77
78void MipsContext::SmashCallerSaves() {
79  // This needs to be 0 because we want a null/zero return value.
80  gprs_[V0] = const_cast<uint32_t*>(&gZero);
81  gprs_[V1] = const_cast<uint32_t*>(&gZero);
82  gprs_[A1] = NULL;
83  gprs_[A2] = NULL;
84  gprs_[A3] = NULL;
85}
86
87extern "C" void art_quick_do_long_jump(uint32_t*, uint32_t*);
88
89void MipsContext::DoLongJump() {
90  uintptr_t gprs[kNumberOfCoreRegisters];
91  uint32_t fprs[kNumberOfFRegisters];
92  for (size_t i = 0; i < kNumberOfCoreRegisters; ++i) {
93    gprs[i] = gprs_[i] != NULL ? *gprs_[i] : MipsContext::kBadGprBase + i;
94  }
95  for (size_t i = 0; i < kNumberOfFRegisters; ++i) {
96    fprs[i] = fprs_[i] != NULL ? *fprs_[i] : MipsContext::kBadGprBase + i;
97  }
98  art_quick_do_long_jump(gprs, fprs);
99}
100
101}  // namespace mips
102}  // namespace art
103