blob: bab36c4fd7ca35af095a5fa54343af6aae1d1c82 [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*.
buzbee9c044ce2012-03-18 13:24:07 -070055 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
56 * denotes Method* and (reg <= -3) denotes a compiler temp.
buzbeeefccc562012-03-11 11:19:28 -070057 *
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,
Elliott Hughesb25c3f62012-03-26 16:35:06 -070092 size_t frame_size, int reg) {
93 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
buzbee70c96d42012-03-15 15:27:56 -070094 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1 /* filler */;
buzbeeefccc562012-03-11 11:19:28 -070095 int num_ins = code_item->ins_size_;
96 int num_regs = code_item->registers_size_ - num_ins;
97 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
buzbee9c044ce2012-03-18 13:24:07 -070098 if (reg == -2) {
buzbeeefccc562012-03-11 11:19:28 -070099 return 0; // Method*
buzbee9c044ce2012-03-18 13:24:07 -0700100 } else if (reg <= -3) {
buzbeeefccc562012-03-11 11:19:28 -0700101 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp
102 } else if (reg < num_regs) {
103 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg
104 } else {
105 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in
106 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800107}
108
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800109uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
110 uint32_t fp_spills, size_t frame_size, int vreg) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800111 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughes68e76522011-10-05 13:22:16 -0700112 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800113 return *reinterpret_cast<uint32_t*>(vreg_addr);
Elliott Hughes68e76522011-10-05 13:22:16 -0700114}
115
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800116uint32_t Frame::GetVReg(Method* m, int vreg) const {
117 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800118 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800119 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
120 uint32_t core_spills = m->GetCoreSpillMask();
121 uint32_t fp_spills = m->GetFpSpillMask();
122 size_t frame_size = m->GetFrameSizeInBytes();
123 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
124}
125
126void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) {
127 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800128 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
130 uint32_t core_spills = m->GetCoreSpillMask();
131 uint32_t fp_spills = m->GetFpSpillMask();
132 size_t frame_size = m->GetFrameSizeInBytes();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800133 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughescccd84f2011-12-05 16:51:54 -0800134 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
135 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
136}
137
Elliott Hughes68e76522011-10-05 13:22:16 -0700138uintptr_t Frame::LoadCalleeSave(int num) const {
139 // Callee saves are held at the top of the frame
140 Method* method = GetMethod();
141 DCHECK(method != NULL);
142 size_t frame_size = method->GetFrameSizeInBytes();
143 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
144#if defined(__i386__)
145 save_addr -= kPointerSize; // account for return address
146#endif
147 return *reinterpret_cast<uintptr_t*>(save_addr);
148}
149
150Method* Frame::NextMethod() const {
151 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
152 return *reinterpret_cast<Method**>(next_sp);
153}
154
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700155class StackGetter {
156 public:
157 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
158 }
159
160 static void Callback(void* arg) {
161 reinterpret_cast<StackGetter*>(arg)->Callback();
162 }
163
164 jobject GetTrace() {
165 return trace_;
166 }
167
168 private:
169 void Callback() {
170 trace_ = thread_->CreateInternalStackTrace(env_);
171 }
172
173 JNIEnv* env_;
174 Thread* thread_;
175 jobject trace_;
176};
177
178jobject GetThreadStack(JNIEnv* env, Thread* thread) {
179 ThreadList* thread_list = Runtime::Current()->GetThreadList();
180 StackGetter stack_getter(env, thread);
181 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
182 return stack_getter.GetTrace();
183}
184
Elliott Hughes68e76522011-10-05 13:22:16 -0700185} // namespace art