blob: 1134b2524670609559496d4a2843425e937213ed [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#ifndef ART_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070021#include "heap.h"
jeffhao725a9572012-11-13 18:20:12 -080022#include "instrumentation.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070023#include "jni.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024#include "macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070025#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070026
27#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080028#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070029
30namespace art {
31
Mathieu Chartier66f19252012-09-18 08:57:04 -070032class AbstractMethod;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070034class Object;
35class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070036class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070038class Thread;
39
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040// The kind of vreg being accessed in calls to Set/GetVReg.
41enum VRegKind {
42 kReferenceVReg,
43 kIntVReg,
44 kFloatVReg,
45 kLongLoVReg,
46 kLongHiVReg,
47 kDoubleLoVReg,
48 kDoubleHiVReg,
49 kConstant,
50 kImpreciseConstant,
51 kUndefined,
52};
53
Ian Rogers0399dde2012-06-06 17:09:28 -070054class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070055 public:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070056 static ShadowFrame* Create(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link,
57 AbstractMethod* method, uint32_t dex_pc) {
58 size_t sz = sizeof(ShadowFrame) + (sizeof(Object*) * num_refs) + (sizeof(uint32_t) * num_vregs);
59 uint8_t* memory = new uint8_t[sz];
60 return new (memory) ShadowFrame(num_refs, num_vregs, link, method, dex_pc);
61 }
62 ~ShadowFrame() {}
63
Ian Rogers0399dde2012-06-06 17:09:28 -070064 uint32_t NumberOfReferences() const {
65 return number_of_references_;
66 }
Elliott Hughes68e76522011-10-05 13:22:16 -070067
Ian Rogers5438ad82012-10-15 17:22:44 -070068 void SetNumberOfReferences(uint16_t number_of_references) {
Ian Rogers0399dde2012-06-06 17:09:28 -070069 number_of_references_ = number_of_references;
70 }
71
Ian Rogers5438ad82012-10-15 17:22:44 -070072 void SetNumberOfVRegs(uint16_t number_of_vregs) {
73 number_of_vregs_ = number_of_vregs;
74 }
75
Ian Rogers0399dde2012-06-06 17:09:28 -070076 uint32_t GetDexPC() const {
77 return dex_pc_;
78 }
79
80 void SetDexPC(uint32_t dex_pc) {
81 dex_pc_ = dex_pc;
82 }
83
Ian Rogers0399dde2012-06-06 17:09:28 -070084 ShadowFrame* GetLink() const {
85 return link_;
86 }
87
88 void SetLink(ShadowFrame* frame) {
89 DCHECK_NE(this, frame);
90 link_ = frame;
91 }
92
93 Object* GetReference(size_t i) const {
94 DCHECK_LT(i, number_of_references_);
95 return references_[i];
96 }
97
98 void SetReference(size_t i, Object* object) {
99 DCHECK_LT(i, number_of_references_);
100 references_[i] = object;
101 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800102
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700103 int32_t GetVReg(size_t i) const {
104 DCHECK_LT(i, number_of_vregs_);
105 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
106 return reinterpret_cast<const int32_t*>(vregs)[i];
107 }
108
109 float GetVRegFloat(size_t i) const {
110 DCHECK_LT(i, number_of_vregs_);
111 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
112 return reinterpret_cast<const float*>(vregs)[i];
113 }
114
115 int64_t GetVRegLong(size_t i) const {
116 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
117 const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i];
118 return *reinterpret_cast<const int64_t*>(low_half);
119 }
120
121 double GetVRegDouble(size_t i) const {
122 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
123 const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i];
124 return *reinterpret_cast<const double*>(low_half);
125 }
126
127 void SetVReg(size_t i, int32_t val) {
128 DCHECK_LT(i, number_of_vregs_);
129 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
130 reinterpret_cast<int32_t*>(vregs)[i] = val;
131 }
132
133 void SetVRegFloat(size_t i, float val) {
134 DCHECK_LT(i, number_of_vregs_);
135 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
136 reinterpret_cast<float*>(vregs)[i] = val;
137 }
138
139 void SetVRegLong(size_t i, int64_t val) {
140 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
141 int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i];
142 *reinterpret_cast<int64_t*>(low_half) = val;
143 }
144
145 void SetVRegDouble(size_t i, double val) {
146 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
147 int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i];
148 *reinterpret_cast<double*>(low_half) = val;
149 }
150
151 void SetReferenceAndVReg(size_t i, Object* val) {
152 SetReference(i, val);
153 SetVReg(i, reinterpret_cast<int32_t>(val));
154 }
155
Mathieu Chartier66f19252012-09-18 08:57:04 -0700156 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700157 DCHECK_NE(method_, static_cast<void*>(NULL));
158 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700159 }
160
Mathieu Chartier66f19252012-09-18 08:57:04 -0700161 void SetMethod(AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700162 DCHECK_NE(method, static_cast<void*>(NULL));
163 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -0700164 }
165
Ian Rogers0399dde2012-06-06 17:09:28 -0700166 bool Contains(Object** shadow_frame_entry) const {
167 return ((&references_[0] <= shadow_frame_entry) &&
168 (shadow_frame_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -0700169 }
170
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700171 template <typename Visitor>
172 void VisitRoots(const Visitor& visitor) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700173 size_t num_refs = NumberOfReferences();
174 for (size_t j = 0; j < num_refs; j++) {
175 Object* object = GetReference(j);
176 if (object != NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700177 visitor(object, j);
Ian Rogers0399dde2012-06-06 17:09:28 -0700178 }
179 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700180 }
181
Ian Rogers0399dde2012-06-06 17:09:28 -0700182 static size_t LinkOffset() {
183 return OFFSETOF_MEMBER(ShadowFrame, link_);
184 }
185
Ian Rogers0399dde2012-06-06 17:09:28 -0700186 static size_t MethodOffset() {
187 return OFFSETOF_MEMBER(ShadowFrame, method_);
188 }
189
Ian Rogers0399dde2012-06-06 17:09:28 -0700190 static size_t DexPCOffset() {
191 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
192 }
193
Ian Rogers0399dde2012-06-06 17:09:28 -0700194 static size_t NumberOfReferencesOffset() {
195 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
196 }
197
Ian Rogers5438ad82012-10-15 17:22:44 -0700198 static size_t NumberOfVRegsOffset() {
199 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
200 }
201
Ian Rogers0399dde2012-06-06 17:09:28 -0700202 static size_t ReferencesOffset() {
203 return OFFSETOF_MEMBER(ShadowFrame, references_);
204 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700205
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700206 size_t VRegsOffset() const {
Ian Rogers5438ad82012-10-15 17:22:44 -0700207 return ReferencesOffset() + (sizeof(Object*) * NumberOfReferences());
208 }
209
Elliott Hughes68e76522011-10-05 13:22:16 -0700210 private:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700211 ShadowFrame(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link, AbstractMethod* method,
212 uint32_t dex_pc)
213 : number_of_references_ (num_refs), number_of_vregs_(num_vregs), link_(link),
214 method_(method), dex_pc_(dex_pc) {
215 for (size_t i = 0; i < num_refs; ++i) {
216 SetReference(i, NULL);
217 }
218 for (size_t i = 0; i < num_vregs; ++i) {
219 SetVReg(i, 0);
220 }
221 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700222
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700223 // TODO: make the majority of these fields const.
Ian Rogers5438ad82012-10-15 17:22:44 -0700224 uint16_t number_of_references_;
225 uint16_t number_of_vregs_;
226 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700227 ShadowFrame* link_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700228 AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700229 uint32_t dex_pc_;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700230 Object* references_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700231
Ian Rogers0399dde2012-06-06 17:09:28 -0700232 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700233};
234
Ian Rogers0399dde2012-06-06 17:09:28 -0700235// The managed stack is used to record fragments of managed code stacks. Managed code stacks
236// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
237// necessary for transitions between code using different frame layouts and transitions into native
238// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800239class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700240 public:
Ian Rogersca190662012-06-26 15:45:57 -0700241 ManagedStack()
242 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700243
244 void PushManagedStackFragment(ManagedStack* fragment) {
245 // Copy this top fragment into given fragment.
246 memcpy(fragment, this, sizeof(ManagedStack));
247 // Clear this fragment, which has become the top.
248 memset(this, 0, sizeof(ManagedStack));
249 // Link our top fragment onto the given fragment.
250 link_ = fragment;
251 }
252
253 void PopManagedStackFragment(const ManagedStack& fragment) {
254 DCHECK(&fragment == link_);
255 // Copy this given fragment back to the top.
256 memcpy(this, &fragment, sizeof(ManagedStack));
257 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700258
259 ManagedStack* GetLink() const {
260 return link_;
261 }
262
Mathieu Chartier66f19252012-09-18 08:57:04 -0700263 AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700264 return top_quick_frame_;
265 }
266
Mathieu Chartier66f19252012-09-18 08:57:04 -0700267 void SetTopQuickFrame(AbstractMethod** top) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700268 top_quick_frame_ = top;
269 }
270
271 uintptr_t GetTopQuickFramePc() const {
272 return top_quick_frame_pc_;
273 }
274
275 void SetTopQuickFramePc(uintptr_t pc) {
276 top_quick_frame_pc_ = pc;
277 }
278
279 static size_t TopQuickFrameOffset() {
280 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
281 }
282
283 static size_t TopQuickFramePcOffset() {
284 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
285 }
286
287 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
288 ShadowFrame* old_frame = top_shadow_frame_;
289 top_shadow_frame_ = new_top_frame;
290 new_top_frame->SetLink(old_frame);
291 return old_frame;
292 }
293
294 ShadowFrame* PopShadowFrame() {
295 CHECK(top_shadow_frame_ != NULL);
296 ShadowFrame* frame = top_shadow_frame_;
297 top_shadow_frame_ = frame->GetLink();
298 return frame;
299 }
300
301 ShadowFrame* GetTopShadowFrame() const {
302 return top_shadow_frame_;
303 }
304
305 static size_t TopShadowFrameOffset() {
306 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
307 }
308
309 size_t NumShadowFrameReferences() const;
310
311 bool ShadowFramesContain(Object** shadow_frame_entry) const;
312
313 private:
314 ManagedStack* link_;
315 ShadowFrame* top_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700316 AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700317 uintptr_t top_quick_frame_pc_;
318};
319
320class StackVisitor {
321 protected:
jeffhao725a9572012-11-13 18:20:12 -0800322 StackVisitor(const ManagedStack* stack, const std::vector<InstrumentationStackFrame>* instrumentation_stack,
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700323 Context* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
jeffhao725a9572012-11-13 18:20:12 -0800325 : stack_start_(stack), instrumentation_stack_(instrumentation_stack), cur_shadow_frame_(NULL),
Ian Rogersca190662012-06-26 15:45:57 -0700326 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
327 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700328
329 public:
330 virtual ~StackVisitor() {}
331
332 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700334
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700335 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700336 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700337
Mathieu Chartier66f19252012-09-18 08:57:04 -0700338 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700339 if (cur_shadow_frame_ != NULL) {
340 return cur_shadow_frame_->GetMethod();
341 } else if (cur_quick_frame_ != NULL) {
342 return *cur_quick_frame_;
343 } else {
344 return NULL;
345 }
346 }
347
348 bool IsShadowFrame() const {
349 return cur_shadow_frame_ != NULL;
350 }
351
Ian Rogers0c7abda2012-09-19 13:33:42 -0700352 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
353
354 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
355
Ian Rogers0399dde2012-06-06 17:09:28 -0700356 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
357 // Callee saves are held at the top of the frame
Mathieu Chartier66f19252012-09-18 08:57:04 -0700358 AbstractMethod* method = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -0700359 DCHECK(method != NULL);
360 byte* save_addr =
361 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
362#if defined(__i386__)
363 save_addr -= kPointerSize; // account for return address
364#endif
365 return *reinterpret_cast<uintptr_t*>(save_addr);
366 }
367
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700368 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700369 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700370 return GetNumFrames() - cur_depth_;
371 }
372
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700373 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700374 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700375 return GetFrameHeight() + 1;
376 }
377
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700379 if (num_frames_ == 0) {
380 num_frames_ = ComputeNumFrames();
381 }
382 return num_frames_;
383 }
384
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800385 uint32_t GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const
386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700387
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800388 void SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700390
391 uintptr_t GetGPR(uint32_t reg) const;
392
Mathieu Chartier66f19252012-09-18 08:57:04 -0700393 uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800394 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
395 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700396 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700397 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
398 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700399 return *reinterpret_cast<uint32_t*>(vreg_addr);
400 }
401
402 uintptr_t GetReturnPc() const;
403
404 void SetReturnPc(uintptr_t new_ret_pc);
405
406 /*
407 * Return sp-relative offset for a Dalvik virtual register, compiler
408 * spill or Method* in bytes using Method*.
409 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
410 * denotes Method* and (reg <= -3) denotes a compiler temp.
411 *
412 * +------------------------+
413 * | IN[ins-1] | {Note: resides in caller's frame}
414 * | . |
415 * | IN[0] |
416 * | caller's Method* |
417 * +========================+ {Note: start of callee's frame}
418 * | core callee-save spill | {variable sized}
419 * +------------------------+
420 * | fp callee-save spill |
421 * +------------------------+
422 * | filler word | {For compatibility, if V[locals-1] used as wide
423 * +------------------------+
424 * | V[locals-1] |
425 * | V[locals-2] |
426 * | . |
427 * | . | ... (reg == 2)
428 * | V[1] | ... (reg == 1)
429 * | V[0] | ... (reg == 0) <---- "locals_start"
430 * +------------------------+
431 * | Compiler temps | ... (reg == -2)
432 * | | ... (reg == -3)
433 * | | ... (reg == -4)
434 * +------------------------+
435 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
436 * +------------------------+
437 * | OUT[outs-1] |
438 * | OUT[outs-2] |
439 * | . |
440 * | OUT[0] |
441 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
442 * +========================+
443 */
444 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700445 uint32_t core_spills, uint32_t fp_spills,
446 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700447 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
448 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
449 int num_ins = code_item->ins_size_;
450 int num_regs = code_item->registers_size_ - num_ins;
451 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
452 if (reg == -2) {
453 return 0; // Method*
454 } else if (reg <= -3) {
455 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
456 } else if (reg < num_regs) {
457 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
458 } else {
459 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
460 }
461 }
462
463 uintptr_t GetCurrentQuickFramePc() const {
464 return cur_quick_frame_pc_;
465 }
466
Mathieu Chartier66f19252012-09-18 08:57:04 -0700467 AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700468 return cur_quick_frame_;
469 }
470
471 ShadowFrame* GetCurrentShadowFrame() const {
472 return cur_shadow_frame_;
473 }
474
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700475 StackIndirectReferenceTable* GetCurrentSirt() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700476 AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700477 ++sp; // Skip Method*; SIRT comes next;
478 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
479 }
480
Ian Rogers40e3bac2012-11-20 00:09:14 -0800481 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
482
Ian Rogers0399dde2012-06-06 17:09:28 -0700483 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700484 size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700485
jeffhao725a9572012-11-13 18:20:12 -0800486 InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const {
487 return instrumentation_stack_->at(instrumentation_stack_->size() - depth - 1);
Ian Rogers0399dde2012-06-06 17:09:28 -0700488 }
489
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700491
492 const ManagedStack* const stack_start_;
jeffhao725a9572012-11-13 18:20:12 -0800493 const std::vector<InstrumentationStackFrame>* const instrumentation_stack_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700494 ShadowFrame* cur_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700495 AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700496 uintptr_t cur_quick_frame_pc_;
497 // Lazily computed, number of frames in the stack.
498 size_t num_frames_;
499 // Depth of the frame we're currently at.
500 size_t cur_depth_;
501 protected:
502 Context* const context_;
503};
504
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800505class VmapTable {
506 public:
507 explicit VmapTable(const uint16_t* table) : table_(table) {
508 }
509
510 uint16_t operator[](size_t i) const {
511 return table_[i + 1];
512 }
513
514 size_t size() const {
515 return table_[0];
516 }
517
518 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
519 // 'kind' is unknown or constant.
520 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
521 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
522 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
523 kind == kDoubleHiVReg || kind == kImpreciseConstant);
524 vmap_offset = 0xEBAD0FF5;
525 // TODO: take advantage of the registers being ordered
526 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
527 // are never promoted to floating point registers.
528 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
529 bool in_floats = false;
530 for (size_t i = 0; i < size(); ++i) {
531 // Stop if we find what we are are looking for.
532 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
533 vmap_offset = i;
534 return true;
535 }
536 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
537 if (table_[i + 1] == 0xffff) {
538 in_floats = true;
539 }
540 }
541 return false;
542 }
543
544 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
545 // by IsInContext above). If the kind is floating point then the result will be a floating point
546 // register number, otherwise it will be an integer register number.
547 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
548 // Compute the register we need to load from the context.
549 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
550 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
551 kind == kDoubleHiVReg || kind == kImpreciseConstant);
552 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
553 // are never promoted to floating point registers.
554 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
555 uint32_t matches = 0;
556 if (is_float) {
557 while (table_[matches] != 0xffff) {
558 matches++;
559 }
560 }
561 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
562 uint32_t spill_shifts = 0;
563 while (matches != (vmap_offset + 1)) {
564 DCHECK_NE(spill_mask, 0u);
565 matches += spill_mask & 1; // Add 1 if the low bit is set
566 spill_mask >>= 1;
567 spill_shifts++;
568 }
569 spill_shifts--; // wind back one as we want the last match
570 return spill_shifts;
571 }
572 private:
573 const uint16_t* table_;
574};
575
Elliott Hughes68e76522011-10-05 13:22:16 -0700576} // namespace art
577
578#endif // ART_SRC_STACK_H_