blob: da5c31eab3daf45e643a5079d710189e760b92d6 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -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 "stack.h"
18
19#include "compiler.h"
20#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "object_utils.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070022#include "thread_list.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080024namespace art {
25
Elliott Hughes68e76522011-10-05 13:22:16 -070026bool Frame::HasMethod() const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070027 return GetMethod() != NULL && (!GetMethod()->IsCalleeSaveMethod());
Elliott Hughes68e76522011-10-05 13:22:16 -070028}
29
30void Frame::Next() {
31 size_t frame_size = GetMethod()->GetFrameSizeInBytes();
32 DCHECK_NE(frame_size, 0u);
33 DCHECK_LT(frame_size, 1024u);
34 byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size;
35 sp_ = reinterpret_cast<Method**>(next_sp);
36 if (*sp_ != NULL) {
37 DCHECK((*sp_)->GetClass() == Method::GetMethodClass() ||
38 (*sp_)->GetClass() == Method::GetConstructorClass());
39 }
40}
41
42uintptr_t Frame::GetReturnPC() const {
43 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
44 return *reinterpret_cast<uintptr_t*>(pc_addr);
45}
46
jeffhaoe343b762011-12-05 16:36:44 -080047void Frame::SetReturnPC(uintptr_t pc) {
48 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
49 *reinterpret_cast<uintptr_t*>(pc_addr) = pc;
50}
51
buzbeeefccc562012-03-11 11:19:28 -070052/*
53 * Return sp-relative offset for a Dalvik virtual register, compiler
54 * spill or Method* in bytes using Method*.
55 * Note that (reg >= 0) refers to a Dalvik register, (reg == -1)
56 * denotes Method* and (reg <= -2) denotes a compiler temp.
57 *
58 * +------------------------+
59 * | IN[ins-1] | {Note: resides in caller's frame}
60 * | . |
61 * | IN[0] |
62 * | caller's Method* |
63 * +========================+ {Note: start of callee's frame}
64 * | core callee-save spill | {variable sized}
65 * +------------------------+
66 * | fp calle-save spill |
67 * +------------------------+
68 * | V[locals-1] |
69 * | V[locals-2] |
70 * | . |
71 * | . | ... (reg == 2)
72 * | V[1] | ... (reg == 1)
73 * | V[0] | ... (reg == 0) <---- "locals_start"
74 * +------------------------+
75 * | Compiler temps | ... (reg == -2)
76 * | | ... (reg == -3)
77 * | | ... (reg == -4)
78 * +------------------------+
79 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
80 * +------------------------+
81 * | OUT[outs-1] |
82 * | OUT[outs-2] |
83 * | . |
84 * | OUT[0] |
85 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
86 * +========================+
87 */
88int Frame::GetVRegOffset(const DexFile::CodeItem* code_item,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 uint32_t core_spills, uint32_t fp_spills,
90 size_t frame_size, int reg)
91{
buzbeeefccc562012-03-11 11:19:28 -070092 DCHECK_EQ( frame_size & (kStackAlignment - 1), 0U);
93 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills);
94 int num_ins = code_item->ins_size_;
95 int num_regs = code_item->registers_size_ - num_ins;
96 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
97 if (reg == -1) {
98 return 0; // Method*
99 } else if (reg <= -2) {
100 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp
101 } else if (reg < num_regs) {
102 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg
103 } else {
104 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in
105 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800106}
107
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800108uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
109 uint32_t fp_spills, size_t frame_size, int vreg) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800110 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughes68e76522011-10-05 13:22:16 -0700111 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800112 return *reinterpret_cast<uint32_t*>(vreg_addr);
Elliott Hughes68e76522011-10-05 13:22:16 -0700113}
114
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800115uint32_t Frame::GetVReg(Method* m, int vreg) const {
116 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800117 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
119 uint32_t core_spills = m->GetCoreSpillMask();
120 uint32_t fp_spills = m->GetFpSpillMask();
121 size_t frame_size = m->GetFrameSizeInBytes();
122 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
123}
124
125void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) {
126 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800127 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
129 uint32_t core_spills = m->GetCoreSpillMask();
130 uint32_t fp_spills = m->GetFpSpillMask();
131 size_t frame_size = m->GetFrameSizeInBytes();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800132 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughescccd84f2011-12-05 16:51:54 -0800133 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
134 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
135}
136
Elliott Hughes68e76522011-10-05 13:22:16 -0700137uintptr_t Frame::LoadCalleeSave(int num) const {
138 // Callee saves are held at the top of the frame
139 Method* method = GetMethod();
140 DCHECK(method != NULL);
141 size_t frame_size = method->GetFrameSizeInBytes();
142 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
143#if defined(__i386__)
144 save_addr -= kPointerSize; // account for return address
145#endif
146 return *reinterpret_cast<uintptr_t*>(save_addr);
147}
148
149Method* Frame::NextMethod() const {
150 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
151 return *reinterpret_cast<Method**>(next_sp);
152}
153
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700154class StackGetter {
155 public:
156 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
157 }
158
159 static void Callback(void* arg) {
160 reinterpret_cast<StackGetter*>(arg)->Callback();
161 }
162
163 jobject GetTrace() {
164 return trace_;
165 }
166
167 private:
168 void Callback() {
169 trace_ = thread_->CreateInternalStackTrace(env_);
170 }
171
172 JNIEnv* env_;
173 Thread* thread_;
174 jobject trace_;
175};
176
177jobject GetThreadStack(JNIEnv* env, Thread* thread) {
178 ThreadList* thread_list = Runtime::Current()->GetThreadList();
179 StackGetter stack_getter(env, thread);
180 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
181 return stack_getter.GetTrace();
182}
183
Elliott Hughes68e76522011-10-05 13:22:16 -0700184} // namespace art