blob: 7c87f4555cac9abdb6ee43a9e3ee52bba89f571d [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_STACK_H_
18#define ART_RUNTIME_STACK_H_
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
jeffhao725a9572012-11-13 18:20:12 -080021#include "instrumentation.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/macros.h"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "arch/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024
25#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080026#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070027
28namespace art {
29
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070031 class ArtMethod;
32 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033} // namespace mirror
34
35class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070036class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070037class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070039class Thread;
40
Ian Rogers2bcb4a42012-11-08 10:39:18 -080041// The kind of vreg being accessed in calls to Set/GetVReg.
42enum VRegKind {
43 kReferenceVReg,
44 kIntVReg,
45 kFloatVReg,
46 kLongLoVReg,
47 kLongHiVReg,
48 kDoubleLoVReg,
49 kDoubleHiVReg,
50 kConstant,
51 kImpreciseConstant,
52 kUndefined,
53};
54
Mathieu Chartier67022432012-11-29 18:04:50 -080055// ShadowFrame has 3 possible layouts:
56// - portable - a unified array of VRegs and references. Precise references need GC maps.
57// - interpreter - separate VRegs and reference arrays. References are in the reference array.
58// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -070059class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070060 public:
Jeff Hao66135192013-05-14 11:02:41 -070061 // Compute size of ShadowFrame in bytes.
62 static size_t ComputeSize(uint32_t num_vregs) {
63 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
64 (sizeof(mirror::Object*) * num_vregs);
65 }
66
67 // Create ShadowFrame in heap for deoptimization.
TDYa127ce4cc0d2012-11-18 16:59:53 -080068 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -070069 mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -070070 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
71 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
72 return sf;
73 }
74
75 // Create ShadowFrame for interpreter using provided memory.
76 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -070077 mirror::ArtMethod* method, uint32_t dex_pc, void* memory) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080078 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
79 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070080 }
81 ~ShadowFrame() {}
82
TDYa127ce4cc0d2012-11-18 16:59:53 -080083 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070084#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080085 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070086#else
87 return true;
88#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070089 }
Elliott Hughes68e76522011-10-05 13:22:16 -070090
TDYa127ce4cc0d2012-11-18 16:59:53 -080091 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070092#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080093 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070094#else
95 return number_of_vregs_;
96#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070097 }
98
TDYa127ce4cc0d2012-11-18 16:59:53 -080099 void SetNumberOfVRegs(uint32_t number_of_vregs) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700100#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800101 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700102#else
103 UNUSED(number_of_vregs);
104 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
105#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700106 }
107
Ian Rogers0399dde2012-06-06 17:09:28 -0700108 uint32_t GetDexPC() const {
109 return dex_pc_;
110 }
111
112 void SetDexPC(uint32_t dex_pc) {
113 dex_pc_ = dex_pc;
114 }
115
Ian Rogers0399dde2012-06-06 17:09:28 -0700116 ShadowFrame* GetLink() const {
117 return link_;
118 }
119
120 void SetLink(ShadowFrame* frame) {
121 DCHECK_NE(this, frame);
122 link_ = frame;
123 }
124
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700125 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800126 DCHECK_LT(i, NumberOfVRegs());
127 const uint32_t* vreg = &vregs_[i];
128 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700129 }
130
131 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800132 DCHECK_LT(i, NumberOfVRegs());
133 // NOTE: Strict-aliasing?
134 const uint32_t* vreg = &vregs_[i];
135 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700136 }
137
138 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200139 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800140 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700141 // Alignment attribute required for GCC 4.8
142 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
143 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700144 }
145
146 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200147 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800148 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700149 // Alignment attribute required for GCC 4.8
150 typedef const double unaligned_double __attribute__ ((aligned (4)));
151 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800152 }
153
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800155 DCHECK_LT(i, NumberOfVRegs());
156 if (HasReferenceArray()) {
157 return References()[i];
158 } else {
159 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800161 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700162 }
163
Jeff Hao16743632013-05-08 10:59:04 -0700164 // Get view of vregs as range of consecutive arguments starting at i.
165 uint32_t* GetVRegArgs(size_t i) {
166 return &vregs_[i];
167 }
168
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700169 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800170 DCHECK_LT(i, NumberOfVRegs());
171 uint32_t* vreg = &vregs_[i];
172 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 }
174
175 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800176 DCHECK_LT(i, NumberOfVRegs());
177 uint32_t* vreg = &vregs_[i];
178 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700179 }
180
181 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200182 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800183 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700184 // Alignment attribute required for GCC 4.8
185 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
186 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700187 }
188
189 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200190 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800191 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700192 // Alignment attribute required for GCC 4.8
193 typedef double unaligned_double __attribute__ ((aligned (4)));
194 *reinterpret_cast<unaligned_double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700195 }
196
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800198 DCHECK_LT(i, NumberOfVRegs());
199 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800201 if (HasReferenceArray()) {
202 References()[i] = val;
203 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700204 }
205
Brian Carlstromea46f952013-07-30 01:26:50 -0700206 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700207 DCHECK_NE(method_, static_cast<void*>(NULL));
208 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700209 }
210
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
212
Jeff Haoe701f482013-05-24 11:50:49 -0700213 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
214
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
216
Brian Carlstromea46f952013-07-30 01:26:50 -0700217 void SetMethod(mirror::ArtMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700218#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700219 DCHECK_NE(method, static_cast<void*>(NULL));
220 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700221#else
222 UNUSED(method);
223 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
224#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700225 }
226
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800228 if (HasReferenceArray()) {
229 return ((&References()[0] <= shadow_frame_entry_obj) &&
230 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
231 } else {
232 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
233 return ((&vregs_[0] <= shadow_frame_entry) &&
234 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700235 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700236 }
237
Ian Rogers0399dde2012-06-06 17:09:28 -0700238 static size_t LinkOffset() {
239 return OFFSETOF_MEMBER(ShadowFrame, link_);
240 }
241
Ian Rogers0399dde2012-06-06 17:09:28 -0700242 static size_t MethodOffset() {
243 return OFFSETOF_MEMBER(ShadowFrame, method_);
244 }
245
Ian Rogers0399dde2012-06-06 17:09:28 -0700246 static size_t DexPCOffset() {
247 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
248 }
249
Ian Rogers5438ad82012-10-15 17:22:44 -0700250 static size_t NumberOfVRegsOffset() {
251 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
252 }
253
TDYa127ce4cc0d2012-11-18 16:59:53 -0800254 static size_t VRegsOffset() {
255 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700256 }
257
Elliott Hughes68e76522011-10-05 13:22:16 -0700258 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700259 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800261 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
262 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700263#if defined(ART_USE_PORTABLE_COMPILER)
264 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800265 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700266#endif
Jeff Haoe701f482013-05-24 11:50:49 -0700267 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(mirror::Object*)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800268 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700269 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700270 }
271 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700272
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800274 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800275 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800277 }
278
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 mirror::Object** References() {
280 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800281 }
282
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700283#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800284 enum ShadowFrameFlag {
285 kHasReferenceArray = 1ul << 31
286 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700287 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800288 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700289#else
290 const uint32_t number_of_vregs_;
291#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700292 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700293 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700294#if defined(ART_USE_PORTABLE_COMPILER)
295 // TODO: make const in the portable case.
Brian Carlstromea46f952013-07-30 01:26:50 -0700296 mirror::ArtMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700297#else
Brian Carlstromea46f952013-07-30 01:26:50 -0700298 mirror::ArtMethod* const method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700299#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700300 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800301 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700302
Ian Rogers0399dde2012-06-06 17:09:28 -0700303 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700304};
305
Ian Rogers0399dde2012-06-06 17:09:28 -0700306// The managed stack is used to record fragments of managed code stacks. Managed code stacks
307// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
308// necessary for transitions between code using different frame layouts and transitions into native
309// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800310class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700311 public:
Ian Rogersca190662012-06-26 15:45:57 -0700312 ManagedStack()
313 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700314
315 void PushManagedStackFragment(ManagedStack* fragment) {
316 // Copy this top fragment into given fragment.
317 memcpy(fragment, this, sizeof(ManagedStack));
318 // Clear this fragment, which has become the top.
319 memset(this, 0, sizeof(ManagedStack));
320 // Link our top fragment onto the given fragment.
321 link_ = fragment;
322 }
323
324 void PopManagedStackFragment(const ManagedStack& fragment) {
325 DCHECK(&fragment == link_);
326 // Copy this given fragment back to the top.
327 memcpy(this, &fragment, sizeof(ManagedStack));
328 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700329
330 ManagedStack* GetLink() const {
331 return link_;
332 }
333
Brian Carlstromea46f952013-07-30 01:26:50 -0700334 mirror::ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700335 return top_quick_frame_;
336 }
337
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 void SetTopQuickFrame(mirror::ArtMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200339 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700340 top_quick_frame_ = top;
341 }
342
343 uintptr_t GetTopQuickFramePc() const {
344 return top_quick_frame_pc_;
345 }
346
347 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200348 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700349 top_quick_frame_pc_ = pc;
350 }
351
352 static size_t TopQuickFrameOffset() {
353 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
354 }
355
356 static size_t TopQuickFramePcOffset() {
357 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
358 }
359
360 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200361 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700362 ShadowFrame* old_frame = top_shadow_frame_;
363 top_shadow_frame_ = new_top_frame;
364 new_top_frame->SetLink(old_frame);
365 return old_frame;
366 }
367
368 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200369 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700370 CHECK(top_shadow_frame_ != NULL);
371 ShadowFrame* frame = top_shadow_frame_;
372 top_shadow_frame_ = frame->GetLink();
373 return frame;
374 }
375
376 ShadowFrame* GetTopShadowFrame() const {
377 return top_shadow_frame_;
378 }
379
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800380 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200381 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800382 top_shadow_frame_ = top;
383 }
384
Ian Rogers0399dde2012-06-06 17:09:28 -0700385 static size_t TopShadowFrameOffset() {
386 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
387 }
388
TDYa127ce4cc0d2012-11-18 16:59:53 -0800389 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700390
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700392
393 private:
394 ManagedStack* link_;
395 ShadowFrame* top_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700396 mirror::ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700397 uintptr_t top_quick_frame_pc_;
398};
399
400class StackVisitor {
401 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800402 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700403
404 public:
405 virtual ~StackVisitor() {}
406
407 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700408 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700409
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700410 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700412
Brian Carlstromea46f952013-07-30 01:26:50 -0700413 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700414 if (cur_shadow_frame_ != NULL) {
415 return cur_shadow_frame_->GetMethod();
416 } else if (cur_quick_frame_ != NULL) {
417 return *cur_quick_frame_;
418 } else {
419 return NULL;
420 }
421 }
422
423 bool IsShadowFrame() const {
424 return cur_shadow_frame_ != NULL;
425 }
426
Ian Rogers0c7abda2012-09-19 13:33:42 -0700427 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
428
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
430
Ian Rogers0c7abda2012-09-19 13:33:42 -0700431 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
432
Mathieu Chartier67022432012-11-29 18:04:50 -0800433 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700434 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800435 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700436 byte* save_addr =
437 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
438#if defined(__i386__)
439 save_addr -= kPointerSize; // account for return address
440#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800441 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700442 }
443
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700444 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800446 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700447 }
448
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700451 return GetFrameHeight() + 1;
452 }
453
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700455 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800456 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700457 }
458 return num_frames_;
459 }
460
Brian Carlstromea46f952013-07-30 01:26:50 -0700461 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800462 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700463
Brian Carlstromea46f952013-07-30 01:26:50 -0700464 void SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700465 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700466
467 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800468 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700469
Brian Carlstromea46f952013-07-30 01:26:50 -0700470 uint32_t GetVReg(mirror::ArtMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800471 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
472 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700473 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700474 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
475 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700476 return *reinterpret_cast<uint32_t*>(vreg_addr);
477 }
478
479 uintptr_t GetReturnPc() const;
480
481 void SetReturnPc(uintptr_t new_ret_pc);
482
483 /*
484 * Return sp-relative offset for a Dalvik virtual register, compiler
485 * spill or Method* in bytes using Method*.
486 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
487 * denotes Method* and (reg <= -3) denotes a compiler temp.
488 *
489 * +------------------------+
490 * | IN[ins-1] | {Note: resides in caller's frame}
491 * | . |
492 * | IN[0] |
493 * | caller's Method* |
494 * +========================+ {Note: start of callee's frame}
495 * | core callee-save spill | {variable sized}
496 * +------------------------+
497 * | fp callee-save spill |
498 * +------------------------+
499 * | filler word | {For compatibility, if V[locals-1] used as wide
500 * +------------------------+
501 * | V[locals-1] |
502 * | V[locals-2] |
503 * | . |
504 * | . | ... (reg == 2)
505 * | V[1] | ... (reg == 1)
506 * | V[0] | ... (reg == 0) <---- "locals_start"
507 * +------------------------+
508 * | Compiler temps | ... (reg == -2)
509 * | | ... (reg == -3)
510 * | | ... (reg == -4)
511 * +------------------------+
512 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
513 * +------------------------+
514 * | OUT[outs-1] |
515 * | OUT[outs-2] |
516 * | . |
517 * | OUT[0] |
518 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
519 * +========================+
520 */
521 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700522 uint32_t core_spills, uint32_t fp_spills,
523 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700524 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700525 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
Ian Rogers0399dde2012-06-06 17:09:28 -0700526 int num_ins = code_item->ins_size_;
527 int num_regs = code_item->registers_size_ - num_ins;
528 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
529 if (reg == -2) {
530 return 0; // Method*
531 } else if (reg <= -3) {
532 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
533 } else if (reg < num_regs) {
534 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
535 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700536 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 }
538 }
539
540 uintptr_t GetCurrentQuickFramePc() const {
541 return cur_quick_frame_pc_;
542 }
543
Brian Carlstromea46f952013-07-30 01:26:50 -0700544 mirror::ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700545 return cur_quick_frame_;
546 }
547
548 ShadowFrame* GetCurrentShadowFrame() const {
549 return cur_shadow_frame_;
550 }
551
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700552 StackIndirectReferenceTable* GetCurrentSirt() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700553 mirror::ArtMethod** sp = GetCurrentQuickFrame();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700554 ++sp; // Skip Method*; SIRT comes next;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700555 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
556 }
557
Ian Rogers40e3bac2012-11-20 00:09:14 -0800558 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
559
Ian Rogers7a22fa62013-01-23 12:16:16 -0800560 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800561
Ian Rogers7a22fa62013-01-23 12:16:16 -0800562 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800563
Ian Rogers0399dde2012-06-06 17:09:28 -0700564 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800565 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700566
Ian Rogersb726dcb2012-09-05 08:57:23 -0700567 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700568
Ian Rogers7a22fa62013-01-23 12:16:16 -0800569 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700570 ShadowFrame* cur_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700571 mirror::ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700572 uintptr_t cur_quick_frame_pc_;
573 // Lazily computed, number of frames in the stack.
574 size_t num_frames_;
575 // Depth of the frame we're currently at.
576 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700577
Ian Rogers0399dde2012-06-06 17:09:28 -0700578 protected:
579 Context* const context_;
580};
581
Elliott Hughes68e76522011-10-05 13:22:16 -0700582} // namespace art
583
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700584#endif // ART_RUNTIME_STACK_H_