blob: 5dda584d31c3382c43da6437bf3110987584ecda [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() {
TDYa1279b2ba2e2012-03-19 10:12:32 -070031#if defined(ART_USE_LLVM_COMPILER)
32 LOG(FATAL) << "LLVM compiler don't support this function";
33#else
Elliott Hughes68e76522011-10-05 13:22:16 -070034 size_t frame_size = GetMethod()->GetFrameSizeInBytes();
35 DCHECK_NE(frame_size, 0u);
36 DCHECK_LT(frame_size, 1024u);
37 byte* next_sp = reinterpret_cast<byte*>(sp_) + frame_size;
38 sp_ = reinterpret_cast<Method**>(next_sp);
39 if (*sp_ != NULL) {
40 DCHECK((*sp_)->GetClass() == Method::GetMethodClass() ||
41 (*sp_)->GetClass() == Method::GetConstructorClass());
42 }
TDYa1279b2ba2e2012-03-19 10:12:32 -070043#endif
Elliott Hughes68e76522011-10-05 13:22:16 -070044}
45
46uintptr_t Frame::GetReturnPC() const {
TDYa1279b2ba2e2012-03-19 10:12:32 -070047#if defined(ART_USE_LLVM_COMPILER)
48 LOG(FATAL) << "LLVM compiler don't support this function";
49 return 0;
50#else
Elliott Hughes68e76522011-10-05 13:22:16 -070051 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
52 return *reinterpret_cast<uintptr_t*>(pc_addr);
TDYa1279b2ba2e2012-03-19 10:12:32 -070053#endif
Elliott Hughes68e76522011-10-05 13:22:16 -070054}
55
jeffhaoe343b762011-12-05 16:36:44 -080056void Frame::SetReturnPC(uintptr_t pc) {
TDYa1279b2ba2e2012-03-19 10:12:32 -070057#if defined(ART_USE_LLVM_COMPILER)
58 LOG(FATAL) << "LLVM compiler don't support this function";
59#else
jeffhaoe343b762011-12-05 16:36:44 -080060 byte* pc_addr = reinterpret_cast<byte*>(sp_) + GetMethod()->GetReturnPcOffsetInBytes();
61 *reinterpret_cast<uintptr_t*>(pc_addr) = pc;
TDYa1279b2ba2e2012-03-19 10:12:32 -070062#endif
jeffhaoe343b762011-12-05 16:36:44 -080063}
64
buzbeeefccc562012-03-11 11:19:28 -070065/*
66 * Return sp-relative offset for a Dalvik virtual register, compiler
67 * spill or Method* in bytes using Method*.
buzbee9c044ce2012-03-18 13:24:07 -070068 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
69 * denotes Method* and (reg <= -3) denotes a compiler temp.
buzbeeefccc562012-03-11 11:19:28 -070070 *
71 * +------------------------+
72 * | IN[ins-1] | {Note: resides in caller's frame}
73 * | . |
74 * | IN[0] |
75 * | caller's Method* |
76 * +========================+ {Note: start of callee's frame}
77 * | core callee-save spill | {variable sized}
78 * +------------------------+
buzbeee1965672012-03-11 18:39:19 -070079 * | fp callee-save spill |
buzbeeefccc562012-03-11 11:19:28 -070080 * +------------------------+
buzbee70c96d42012-03-15 15:27:56 -070081 * | filler word | {For compatibility, if V[locals-1] used as wide
82 * +------------------------+
buzbeeefccc562012-03-11 11:19:28 -070083 * | V[locals-1] |
84 * | V[locals-2] |
85 * | . |
86 * | . | ... (reg == 2)
87 * | V[1] | ... (reg == 1)
88 * | V[0] | ... (reg == 0) <---- "locals_start"
89 * +------------------------+
90 * | Compiler temps | ... (reg == -2)
91 * | | ... (reg == -3)
92 * | | ... (reg == -4)
93 * +------------------------+
94 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
95 * +------------------------+
96 * | OUT[outs-1] |
97 * | OUT[outs-2] |
98 * | . |
99 * | OUT[0] |
100 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
101 * +========================+
102 */
103int Frame::GetVRegOffset(const DexFile::CodeItem* code_item,
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800104 uint32_t core_spills, uint32_t fp_spills,
TDYa1279b2ba2e2012-03-19 10:12:32 -0700105 size_t frame_size, int reg)
106{
107#if defined(ART_USE_LLVM_COMPILER)
108 LOG(FATAL) << "LLVM compiler don't support this function";
109 return 0;
110#else
111 DCHECK_EQ( frame_size & (kStackAlignment - 1), 0U);
buzbee70c96d42012-03-15 15:27:56 -0700112 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1 /* filler */;
buzbeeefccc562012-03-11 11:19:28 -0700113 int num_ins = code_item->ins_size_;
114 int num_regs = code_item->registers_size_ - num_ins;
115 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
buzbee9c044ce2012-03-18 13:24:07 -0700116 if (reg == -2) {
buzbeeefccc562012-03-11 11:19:28 -0700117 return 0; // Method*
buzbee9c044ce2012-03-18 13:24:07 -0700118 } else if (reg <= -3) {
buzbeeefccc562012-03-11 11:19:28 -0700119 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp
120 } else if (reg < num_regs) {
121 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg
122 } else {
123 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in
124 }
TDYa1279b2ba2e2012-03-19 10:12:32 -0700125#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800126}
127
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800128uint32_t Frame::GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
129 uint32_t fp_spills, size_t frame_size, int vreg) const {
TDYa1279b2ba2e2012-03-19 10:12:32 -0700130#if defined(ART_USE_LLVM_COMPILER)
131 LOG(FATAL) << "LLVM compiler don't support this function";
132 return 0;
133#else
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800134 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughes68e76522011-10-05 13:22:16 -0700135 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
Elliott Hughes1bba14f2011-12-01 18:00:36 -0800136 return *reinterpret_cast<uint32_t*>(vreg_addr);
TDYa1279b2ba2e2012-03-19 10:12:32 -0700137#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700138}
139
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140uint32_t Frame::GetVReg(Method* m, int vreg) const {
TDYa1279b2ba2e2012-03-19 10:12:32 -0700141#if defined(ART_USE_LLVM_COMPILER)
142 LOG(FATAL) << "LLVM compiler don't support this function";
143 return 0;
144#else
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800146 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800147 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
148 uint32_t core_spills = m->GetCoreSpillMask();
149 uint32_t fp_spills = m->GetFpSpillMask();
150 size_t frame_size = m->GetFrameSizeInBytes();
151 return GetVReg(code_item, core_spills, fp_spills, frame_size, vreg);
TDYa1279b2ba2e2012-03-19 10:12:32 -0700152#endif
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153}
154
155void Frame::SetVReg(Method* m, int vreg, uint32_t new_value) {
TDYa1279b2ba2e2012-03-19 10:12:32 -0700156#if defined(ART_USE_LLVM_COMPILER)
157 LOG(FATAL) << "LLVM compiler don't support this function";
158#else
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 DCHECK(m == GetMethod());
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800160 const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800161 DCHECK(code_item != NULL); // can't be NULL or how would we compile its instructions?
162 uint32_t core_spills = m->GetCoreSpillMask();
163 uint32_t fp_spills = m->GetFpSpillMask();
164 size_t frame_size = m->GetFrameSizeInBytes();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800165 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Elliott Hughescccd84f2011-12-05 16:51:54 -0800166 byte* vreg_addr = reinterpret_cast<byte*>(sp_) + offset;
167 *reinterpret_cast<uint32_t*>(vreg_addr) = new_value;
TDYa1279b2ba2e2012-03-19 10:12:32 -0700168#endif
Elliott Hughescccd84f2011-12-05 16:51:54 -0800169}
170
Elliott Hughes68e76522011-10-05 13:22:16 -0700171uintptr_t Frame::LoadCalleeSave(int num) const {
TDYa1279b2ba2e2012-03-19 10:12:32 -0700172#if defined(ART_USE_LLVM_COMPILER)
173 LOG(FATAL) << "LLVM compiler don't support this function";
174 return 0;
175#else
Elliott Hughes68e76522011-10-05 13:22:16 -0700176 // Callee saves are held at the top of the frame
177 Method* method = GetMethod();
178 DCHECK(method != NULL);
179 size_t frame_size = method->GetFrameSizeInBytes();
180 byte* save_addr = reinterpret_cast<byte*>(sp_) + frame_size - ((num + 1) * kPointerSize);
181#if defined(__i386__)
182 save_addr -= kPointerSize; // account for return address
183#endif
184 return *reinterpret_cast<uintptr_t*>(save_addr);
TDYa1279b2ba2e2012-03-19 10:12:32 -0700185#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700186}
187
188Method* Frame::NextMethod() const {
TDYa1279b2ba2e2012-03-19 10:12:32 -0700189#if defined(ART_USE_LLVM_COMPILER)
190 LOG(FATAL) << "LLVM compiler don't support this function";
191 return NULL;
192#else
Elliott Hughes68e76522011-10-05 13:22:16 -0700193 byte* next_sp = reinterpret_cast<byte*>(sp_) + GetMethod()->GetFrameSizeInBytes();
194 return *reinterpret_cast<Method**>(next_sp);
TDYa1279b2ba2e2012-03-19 10:12:32 -0700195#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700196}
197
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700198class StackGetter {
199 public:
200 StackGetter(JNIEnv* env, Thread* thread) : env_(env), thread_(thread), trace_(NULL) {
201 }
202
203 static void Callback(void* arg) {
204 reinterpret_cast<StackGetter*>(arg)->Callback();
205 }
206
207 jobject GetTrace() {
208 return trace_;
209 }
210
211 private:
212 void Callback() {
213 trace_ = thread_->CreateInternalStackTrace(env_);
214 }
215
216 JNIEnv* env_;
217 Thread* thread_;
218 jobject trace_;
219};
220
221jobject GetThreadStack(JNIEnv* env, Thread* thread) {
222 ThreadList* thread_list = Runtime::Current()->GetThreadList();
223 StackGetter stack_getter(env, thread);
224 thread_list->RunWhileSuspended(thread, StackGetter::Callback, &stack_getter);
225 return stack_getter.GetTrace();
226}
227
Elliott Hughes68e76522011-10-05 13:22:16 -0700228} // namespace art