blob: 5b5609c2c6a0a5fd25bce6a6e993ff945c1ad1cf [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>
28
29namespace art {
30
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080032class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070033class Object;
34class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070035class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070037class Thread;
38
Ian Rogers2bcb4a42012-11-08 10:39:18 -080039// The kind of vreg being accessed in calls to Set/GetVReg.
40enum VRegKind {
41 kReferenceVReg,
42 kIntVReg,
43 kFloatVReg,
44 kLongLoVReg,
45 kLongHiVReg,
46 kDoubleLoVReg,
47 kDoubleHiVReg,
48 kConstant,
49 kImpreciseConstant,
50 kUndefined,
51};
52
Ian Rogers0399dde2012-06-06 17:09:28 -070053class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070054 public:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070055 static ShadowFrame* Create(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link,
56 AbstractMethod* method, uint32_t dex_pc) {
57 size_t sz = sizeof(ShadowFrame) + (sizeof(Object*) * num_refs) + (sizeof(uint32_t) * num_vregs);
58 uint8_t* memory = new uint8_t[sz];
59 return new (memory) ShadowFrame(num_refs, num_vregs, link, method, dex_pc);
60 }
61 ~ShadowFrame() {}
62
Ian Rogers0399dde2012-06-06 17:09:28 -070063 uint32_t NumberOfReferences() const {
64 return number_of_references_;
65 }
Elliott Hughes68e76522011-10-05 13:22:16 -070066
Ian Rogers5438ad82012-10-15 17:22:44 -070067 void SetNumberOfReferences(uint16_t number_of_references) {
Ian Rogers0399dde2012-06-06 17:09:28 -070068 number_of_references_ = number_of_references;
69 }
70
Ian Rogers5438ad82012-10-15 17:22:44 -070071 void SetNumberOfVRegs(uint16_t number_of_vregs) {
72 number_of_vregs_ = number_of_vregs;
73 }
74
Ian Rogers0399dde2012-06-06 17:09:28 -070075 uint32_t GetDexPC() const {
76 return dex_pc_;
77 }
78
79 void SetDexPC(uint32_t dex_pc) {
80 dex_pc_ = dex_pc;
81 }
82
Ian Rogers0399dde2012-06-06 17:09:28 -070083 ShadowFrame* GetLink() const {
84 return link_;
85 }
86
87 void SetLink(ShadowFrame* frame) {
88 DCHECK_NE(this, frame);
89 link_ = frame;
90 }
91
92 Object* GetReference(size_t i) const {
93 DCHECK_LT(i, number_of_references_);
94 return references_[i];
95 }
96
97 void SetReference(size_t i, Object* object) {
98 DCHECK_LT(i, number_of_references_);
99 references_[i] = object;
100 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800101
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700102 int32_t GetVReg(size_t i) const {
103 DCHECK_LT(i, number_of_vregs_);
104 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
105 return reinterpret_cast<const int32_t*>(vregs)[i];
106 }
107
108 float GetVRegFloat(size_t i) const {
109 DCHECK_LT(i, number_of_vregs_);
110 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
111 return reinterpret_cast<const float*>(vregs)[i];
112 }
113
114 int64_t GetVRegLong(size_t i) const {
115 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
116 const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i];
117 return *reinterpret_cast<const int64_t*>(low_half);
118 }
119
120 double GetVRegDouble(size_t i) const {
121 const int8_t* vregs = reinterpret_cast<const int8_t*>(this) + VRegsOffset();
122 const int32_t* low_half = &reinterpret_cast<const int32_t*>(vregs)[i];
123 return *reinterpret_cast<const double*>(low_half);
124 }
125
126 void SetVReg(size_t i, int32_t val) {
127 DCHECK_LT(i, number_of_vregs_);
128 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
129 reinterpret_cast<int32_t*>(vregs)[i] = val;
130 }
131
132 void SetVRegFloat(size_t i, float val) {
133 DCHECK_LT(i, number_of_vregs_);
134 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
135 reinterpret_cast<float*>(vregs)[i] = val;
136 }
137
138 void SetVRegLong(size_t i, int64_t val) {
139 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
140 int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i];
141 *reinterpret_cast<int64_t*>(low_half) = val;
142 }
143
144 void SetVRegDouble(size_t i, double val) {
145 int8_t* vregs = reinterpret_cast<int8_t*>(this) + VRegsOffset();
146 int32_t* low_half = &reinterpret_cast<int32_t*>(vregs)[i];
147 *reinterpret_cast<double*>(low_half) = val;
148 }
149
150 void SetReferenceAndVReg(size_t i, Object* val) {
151 SetReference(i, val);
152 SetVReg(i, reinterpret_cast<int32_t>(val));
153 }
154
Mathieu Chartier66f19252012-09-18 08:57:04 -0700155 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700156 DCHECK_NE(method_, static_cast<void*>(NULL));
157 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700158 }
159
Mathieu Chartier66f19252012-09-18 08:57:04 -0700160 void SetMethod(AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700161 DCHECK_NE(method, static_cast<void*>(NULL));
162 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -0700163 }
164
Ian Rogers0399dde2012-06-06 17:09:28 -0700165 bool Contains(Object** shadow_frame_entry) const {
166 return ((&references_[0] <= shadow_frame_entry) &&
167 (shadow_frame_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -0700168 }
169
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700170 template <typename Visitor>
171 void VisitRoots(const Visitor& visitor) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700172 size_t num_refs = NumberOfReferences();
173 for (size_t j = 0; j < num_refs; j++) {
174 Object* object = GetReference(j);
175 if (object != NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -0700176 visitor(object, j);
Ian Rogers0399dde2012-06-06 17:09:28 -0700177 }
178 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700179 }
180
Ian Rogers0399dde2012-06-06 17:09:28 -0700181 static size_t LinkOffset() {
182 return OFFSETOF_MEMBER(ShadowFrame, link_);
183 }
184
Ian Rogers0399dde2012-06-06 17:09:28 -0700185 static size_t MethodOffset() {
186 return OFFSETOF_MEMBER(ShadowFrame, method_);
187 }
188
Ian Rogers0399dde2012-06-06 17:09:28 -0700189 static size_t DexPCOffset() {
190 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
191 }
192
Ian Rogers0399dde2012-06-06 17:09:28 -0700193 static size_t NumberOfReferencesOffset() {
194 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
195 }
196
Ian Rogers5438ad82012-10-15 17:22:44 -0700197 static size_t NumberOfVRegsOffset() {
198 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
199 }
200
Ian Rogers0399dde2012-06-06 17:09:28 -0700201 static size_t ReferencesOffset() {
202 return OFFSETOF_MEMBER(ShadowFrame, references_);
203 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700204
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700205 size_t VRegsOffset() const {
Ian Rogers5438ad82012-10-15 17:22:44 -0700206 return ReferencesOffset() + (sizeof(Object*) * NumberOfReferences());
207 }
208
Elliott Hughes68e76522011-10-05 13:22:16 -0700209 private:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700210 ShadowFrame(uint16_t num_refs, uint16_t num_vregs, ShadowFrame* link, AbstractMethod* method,
211 uint32_t dex_pc)
212 : number_of_references_ (num_refs), number_of_vregs_(num_vregs), link_(link),
213 method_(method), dex_pc_(dex_pc) {
214 for (size_t i = 0; i < num_refs; ++i) {
215 SetReference(i, NULL);
216 }
217 for (size_t i = 0; i < num_vregs; ++i) {
218 SetVReg(i, 0);
219 }
220 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700221
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700222 // TODO: make the majority of these fields const.
Ian Rogers5438ad82012-10-15 17:22:44 -0700223 uint16_t number_of_references_;
224 uint16_t number_of_vregs_;
225 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700226 ShadowFrame* link_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700227 AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700228 uint32_t dex_pc_;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700229 Object* references_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700230
Ian Rogers0399dde2012-06-06 17:09:28 -0700231 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700232};
233
Ian Rogers0399dde2012-06-06 17:09:28 -0700234// The managed stack is used to record fragments of managed code stacks. Managed code stacks
235// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
236// necessary for transitions between code using different frame layouts and transitions into native
237// code.
238class PACKED ManagedStack {
239 public:
Ian Rogersca190662012-06-26 15:45:57 -0700240 ManagedStack()
241 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700242
243 void PushManagedStackFragment(ManagedStack* fragment) {
244 // Copy this top fragment into given fragment.
245 memcpy(fragment, this, sizeof(ManagedStack));
246 // Clear this fragment, which has become the top.
247 memset(this, 0, sizeof(ManagedStack));
248 // Link our top fragment onto the given fragment.
249 link_ = fragment;
250 }
251
252 void PopManagedStackFragment(const ManagedStack& fragment) {
253 DCHECK(&fragment == link_);
254 // Copy this given fragment back to the top.
255 memcpy(this, &fragment, sizeof(ManagedStack));
256 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700257
258 ManagedStack* GetLink() const {
259 return link_;
260 }
261
Mathieu Chartier66f19252012-09-18 08:57:04 -0700262 AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700263 return top_quick_frame_;
264 }
265
Mathieu Chartier66f19252012-09-18 08:57:04 -0700266 void SetTopQuickFrame(AbstractMethod** top) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700267 top_quick_frame_ = top;
268 }
269
270 uintptr_t GetTopQuickFramePc() const {
271 return top_quick_frame_pc_;
272 }
273
274 void SetTopQuickFramePc(uintptr_t pc) {
275 top_quick_frame_pc_ = pc;
276 }
277
278 static size_t TopQuickFrameOffset() {
279 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
280 }
281
282 static size_t TopQuickFramePcOffset() {
283 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
284 }
285
286 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
287 ShadowFrame* old_frame = top_shadow_frame_;
288 top_shadow_frame_ = new_top_frame;
289 new_top_frame->SetLink(old_frame);
290 return old_frame;
291 }
292
293 ShadowFrame* PopShadowFrame() {
294 CHECK(top_shadow_frame_ != NULL);
295 ShadowFrame* frame = top_shadow_frame_;
296 top_shadow_frame_ = frame->GetLink();
297 return frame;
298 }
299
300 ShadowFrame* GetTopShadowFrame() const {
301 return top_shadow_frame_;
302 }
303
304 static size_t TopShadowFrameOffset() {
305 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
306 }
307
308 size_t NumShadowFrameReferences() const;
309
310 bool ShadowFramesContain(Object** shadow_frame_entry) const;
311
312 private:
313 ManagedStack* link_;
314 ShadowFrame* top_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700315 AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700316 uintptr_t top_quick_frame_pc_;
317};
318
319class StackVisitor {
320 protected:
jeffhao725a9572012-11-13 18:20:12 -0800321 StackVisitor(const ManagedStack* stack, const std::vector<InstrumentationStackFrame>* instrumentation_stack,
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700322 Context* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
jeffhao725a9572012-11-13 18:20:12 -0800324 : stack_start_(stack), instrumentation_stack_(instrumentation_stack), cur_shadow_frame_(NULL),
Ian Rogersca190662012-06-26 15:45:57 -0700325 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
326 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700327
328 public:
329 virtual ~StackVisitor() {}
330
331 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700332 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700333
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700334 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700336
Mathieu Chartier66f19252012-09-18 08:57:04 -0700337 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700338 if (cur_shadow_frame_ != NULL) {
339 return cur_shadow_frame_->GetMethod();
340 } else if (cur_quick_frame_ != NULL) {
341 return *cur_quick_frame_;
342 } else {
343 return NULL;
344 }
345 }
346
347 bool IsShadowFrame() const {
348 return cur_shadow_frame_ != NULL;
349 }
350
Ian Rogers0c7abda2012-09-19 13:33:42 -0700351 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
352
353 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
354
Ian Rogers0399dde2012-06-06 17:09:28 -0700355 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
356 // Callee saves are held at the top of the frame
Mathieu Chartier66f19252012-09-18 08:57:04 -0700357 AbstractMethod* method = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -0700358 DCHECK(method != NULL);
359 byte* save_addr =
360 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
361#if defined(__i386__)
362 save_addr -= kPointerSize; // account for return address
363#endif
364 return *reinterpret_cast<uintptr_t*>(save_addr);
365 }
366
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700367 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700368 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700369 return GetNumFrames() - cur_depth_;
370 }
371
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700372 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700373 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700374 return GetFrameHeight() + 1;
375 }
376
Ian Rogersb726dcb2012-09-05 08:57:23 -0700377 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700378 if (num_frames_ == 0) {
379 num_frames_ = ComputeNumFrames();
380 }
381 return num_frames_;
382 }
383
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800384 uint32_t GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const
385 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700386
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800387 void SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700389
390 uintptr_t GetGPR(uint32_t reg) const;
391
Mathieu Chartier66f19252012-09-18 08:57:04 -0700392 uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800393 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
394 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700395 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700396 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
397 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700398 return *reinterpret_cast<uint32_t*>(vreg_addr);
399 }
400
401 uintptr_t GetReturnPc() const;
402
403 void SetReturnPc(uintptr_t new_ret_pc);
404
405 /*
406 * Return sp-relative offset for a Dalvik virtual register, compiler
407 * spill or Method* in bytes using Method*.
408 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
409 * denotes Method* and (reg <= -3) denotes a compiler temp.
410 *
411 * +------------------------+
412 * | IN[ins-1] | {Note: resides in caller's frame}
413 * | . |
414 * | IN[0] |
415 * | caller's Method* |
416 * +========================+ {Note: start of callee's frame}
417 * | core callee-save spill | {variable sized}
418 * +------------------------+
419 * | fp callee-save spill |
420 * +------------------------+
421 * | filler word | {For compatibility, if V[locals-1] used as wide
422 * +------------------------+
423 * | V[locals-1] |
424 * | V[locals-2] |
425 * | . |
426 * | . | ... (reg == 2)
427 * | V[1] | ... (reg == 1)
428 * | V[0] | ... (reg == 0) <---- "locals_start"
429 * +------------------------+
430 * | Compiler temps | ... (reg == -2)
431 * | | ... (reg == -3)
432 * | | ... (reg == -4)
433 * +------------------------+
434 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
435 * +------------------------+
436 * | OUT[outs-1] |
437 * | OUT[outs-2] |
438 * | . |
439 * | OUT[0] |
440 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
441 * +========================+
442 */
443 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700444 uint32_t core_spills, uint32_t fp_spills,
445 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700446 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
447 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
448 int num_ins = code_item->ins_size_;
449 int num_regs = code_item->registers_size_ - num_ins;
450 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
451 if (reg == -2) {
452 return 0; // Method*
453 } else if (reg <= -3) {
454 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
455 } else if (reg < num_regs) {
456 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
457 } else {
458 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
459 }
460 }
461
462 uintptr_t GetCurrentQuickFramePc() const {
463 return cur_quick_frame_pc_;
464 }
465
Mathieu Chartier66f19252012-09-18 08:57:04 -0700466 AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700467 return cur_quick_frame_;
468 }
469
470 ShadowFrame* GetCurrentShadowFrame() const {
471 return cur_shadow_frame_;
472 }
473
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700474 StackIndirectReferenceTable* GetCurrentSirt() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700475 AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700476 ++sp; // Skip Method*; SIRT comes next;
477 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
478 }
479
Ian Rogers0399dde2012-06-06 17:09:28 -0700480 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700481 size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700482
jeffhao725a9572012-11-13 18:20:12 -0800483 InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const {
484 return instrumentation_stack_->at(instrumentation_stack_->size() - depth - 1);
Ian Rogers0399dde2012-06-06 17:09:28 -0700485 }
486
Ian Rogersb726dcb2012-09-05 08:57:23 -0700487 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700488
489 const ManagedStack* const stack_start_;
jeffhao725a9572012-11-13 18:20:12 -0800490 const std::vector<InstrumentationStackFrame>* const instrumentation_stack_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700491 ShadowFrame* cur_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700492 AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700493 uintptr_t cur_quick_frame_pc_;
494 // Lazily computed, number of frames in the stack.
495 size_t num_frames_;
496 // Depth of the frame we're currently at.
497 size_t cur_depth_;
498 protected:
499 Context* const context_;
500};
501
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800502class VmapTable {
503 public:
504 explicit VmapTable(const uint16_t* table) : table_(table) {
505 }
506
507 uint16_t operator[](size_t i) const {
508 return table_[i + 1];
509 }
510
511 size_t size() const {
512 return table_[0];
513 }
514
515 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
516 // 'kind' is unknown or constant.
517 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
518 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
519 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
520 kind == kDoubleHiVReg || kind == kImpreciseConstant);
521 vmap_offset = 0xEBAD0FF5;
522 // TODO: take advantage of the registers being ordered
523 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
524 // are never promoted to floating point registers.
525 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
526 bool in_floats = false;
527 for (size_t i = 0; i < size(); ++i) {
528 // Stop if we find what we are are looking for.
529 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
530 vmap_offset = i;
531 return true;
532 }
533 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
534 if (table_[i + 1] == 0xffff) {
535 in_floats = true;
536 }
537 }
538 return false;
539 }
540
541 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
542 // by IsInContext above). If the kind is floating point then the result will be a floating point
543 // register number, otherwise it will be an integer register number.
544 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
545 // Compute the register we need to load from the context.
546 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
547 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
548 kind == kDoubleHiVReg || kind == kImpreciseConstant);
549 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
550 // are never promoted to floating point registers.
551 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
552 uint32_t matches = 0;
553 if (is_float) {
554 while (table_[matches] != 0xffff) {
555 matches++;
556 }
557 }
558 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
559 uint32_t spill_shifts = 0;
560 while (matches != (vmap_offset + 1)) {
561 DCHECK_NE(spill_mask, 0u);
562 matches += spill_mask & 1; // Add 1 if the low bit is set
563 spill_mask >>= 1;
564 spill_shifts++;
565 }
566 spill_shifts--; // wind back one as we want the last match
567 return spill_shifts;
568 }
569 private:
570 const uint16_t* table_;
571};
572
Elliott Hughes68e76522011-10-05 13:22:16 -0700573} // namespace art
574
575#endif // ART_SRC_STACK_H_