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