blob: 83169fdf704d254c95e498725f4895a2bf6d4053 [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 * +------------------------+
buzbeee1965672012-03-11 18:39:19 -070066 * | fp callee-save spill |
buzbeeefccc562012-03-11 11:19:28 -070067 * +------------------------+
buzbee70c96d42012-03-15 15:27:56 -070068 * | filler word | {For compatibility, if V[locals-1] used as wide
69 * +------------------------+
buzbeeefccc562012-03-11 11:19:28 -070070 * | V[locals-1] |
71 * | V[locals-2] |
72 * | . |
73 * | . | ... (reg == 2)
74 * | V[1] | ... (reg == 1)
75 * | V[0] | ... (reg == 0) <---- "locals_start"
76 * +------------------------+
77 * | Compiler temps | ... (reg == -2)
78 * | | ... (reg == -3)
79 * | | ... (reg == -4)
80 * +------------------------+
81 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
82 * +------------------------+
83 * | OUT[outs-1] |
84 * | OUT[outs-2] |
85 * | . |
86 * | OUT[0] |
87 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
88 * +========================+
89 */
90int Frame::GetVRegOffset(const DexFile::CodeItem* code_item,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080091 uint32_t core_spills, uint32_t fp_spills,
92 size_t frame_size, int reg)
93{
buzbeeefccc562012-03-11 11:19:28 -070094 DCHECK_EQ( frame_size & (kStackAlignment - 1), 0U);
buzbee70c96d42012-03-15 15:27:56 -070095 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1 /* filler */;
buzbeeefccc562012-03-11 11:19:28 -070096 int num_ins = code_item->ins_size_;
97 int num_regs = code_item->registers_size_ - num_ins;
98 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
99 if (reg == -1) {
100 return 0; // Method*
101 } else if (reg <= -2) {
102 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp
103 } else if (reg < num_regs) {
104 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg
105 } else {
106 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in
107 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800108}
109
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
111 uint32_t fp_spills, size_t frame_size, int vreg) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800112 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughes68e76522011-10-05 13:22:16 -0700113 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800114 return *reinterpret_cast<uint32_t*>(vreg_addr);
Elliott Hughes68e76522011-10-05 13:22:16 -0700115}
116
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800117uint32_t Frame::GetVReg(Method* m, int vreg) const {
118 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800119 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
121 uint32_t core_spills = m->GetCoreSpillMask();
122 uint32_t fp_spills = m->GetFpSpillMask();
123 size_t frame_size = m->GetFrameSizeInBytes();
124 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
125}
126
127void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) {
128 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800129 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
131 uint32_t core_spills = m->GetCoreSpillMask();
132 uint32_t fp_spills = m->GetFpSpillMask();
133 size_t frame_size = m->GetFrameSizeInBytes();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800134 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughescccd84f2011-12-05 16:51:54 -0800135 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
136 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
137}
138
Elliott Hughes68e76522011-10-05 13:22:16 -0700139uintptr_t Frame::LoadCalleeSave(int num) const {
140 // Callee saves are held at the top of the frame
141 Method* method = GetMethod();
142 DCHECK(method != NULL);
143 size_t frame_size = method->GetFrameSizeInBytes();
144 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
145#if defined(__i386__)
146 save_addr -= kPointerSize; // account for return address
147#endif
148 return *reinterpret_cast<uintptr_t*>(save_addr);
149}
150
151Method* Frame::NextMethod() const {
152 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
153 return *reinterpret_cast<Method**>(next_sp);
154}
155
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700156class StackGetter {
157 public:
158 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
159 }
160
161 static void Callback(void* arg) {
162 reinterpret_cast<StackGetter*>(arg)->Callback();
163 }
164
165 jobject GetTrace() {
166 return trace_;
167 }
168
169 private:
170 void Callback() {
171 trace_ = thread_->CreateInternalStackTrace(env_);
172 }
173
174 JNIEnv* env_;
175 Thread* thread_;
176 jobject trace_;
177};
178
179jobject GetThreadStack(JNIEnv* env, Thread* thread) {
180 ThreadList* thread_list = Runtime::Current()->GetThreadList();
181 StackGetter stack_getter(env, thread);
182 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
183 return stack_getter.GetTrace();
184}
185
Elliott Hughes68e76522011-10-05 13:22:16 -0700186} // namespace art