blob: 2bf3340c99537a3f6be4cf9289f20e07e71b1430 [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()) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700157 mirror::Object* ref = References()[i];
158 // If the vreg reference is not equal to the vreg then the vreg reference is stale.
159 if (reinterpret_cast<uint32_t>(ref) != vregs_[i]) {
160 return nullptr;
161 }
162 return ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800163 } else {
164 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800166 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700167 }
168
Jeff Hao16743632013-05-08 10:59:04 -0700169 // Get view of vregs as range of consecutive arguments starting at i.
170 uint32_t* GetVRegArgs(size_t i) {
171 return &vregs_[i];
172 }
173
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700174 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800175 DCHECK_LT(i, NumberOfVRegs());
176 uint32_t* vreg = &vregs_[i];
177 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700178 }
179
180 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800181 DCHECK_LT(i, NumberOfVRegs());
182 uint32_t* vreg = &vregs_[i];
183 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700184 }
185
186 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200187 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800188 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700189 // Alignment attribute required for GCC 4.8
190 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
191 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700192 }
193
194 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200195 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800196 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700197 // Alignment attribute required for GCC 4.8
198 typedef double unaligned_double __attribute__ ((aligned (4)));
199 *reinterpret_cast<unaligned_double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700200 }
201
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800203 DCHECK_LT(i, NumberOfVRegs());
204 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800206 if (HasReferenceArray()) {
207 References()[i] = val;
208 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700209 }
210
Brian Carlstromea46f952013-07-30 01:26:50 -0700211 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700212 DCHECK_NE(method_, static_cast<void*>(NULL));
213 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700214 }
215
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
217
Jeff Haoe701f482013-05-24 11:50:49 -0700218 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
219
Ian Rogers62d6c772013-02-27 08:32:07 -0800220 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
221
Brian Carlstromea46f952013-07-30 01:26:50 -0700222 void SetMethod(mirror::ArtMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700223#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700224 DCHECK_NE(method, static_cast<void*>(NULL));
225 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700226#else
227 UNUSED(method);
228 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
229#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700230 }
231
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800233 if (HasReferenceArray()) {
234 return ((&References()[0] <= shadow_frame_entry_obj) &&
235 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
236 } else {
237 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
238 return ((&vregs_[0] <= shadow_frame_entry) &&
239 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700240 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700241 }
242
Ian Rogers0399dde2012-06-06 17:09:28 -0700243 static size_t LinkOffset() {
244 return OFFSETOF_MEMBER(ShadowFrame, link_);
245 }
246
Ian Rogers0399dde2012-06-06 17:09:28 -0700247 static size_t MethodOffset() {
248 return OFFSETOF_MEMBER(ShadowFrame, method_);
249 }
250
Ian Rogers0399dde2012-06-06 17:09:28 -0700251 static size_t DexPCOffset() {
252 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
253 }
254
Ian Rogers5438ad82012-10-15 17:22:44 -0700255 static size_t NumberOfVRegsOffset() {
256 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
257 }
258
TDYa127ce4cc0d2012-11-18 16:59:53 -0800259 static size_t VRegsOffset() {
260 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700261 }
262
Elliott Hughes68e76522011-10-05 13:22:16 -0700263 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700264 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800266 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
267 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700268#if defined(ART_USE_PORTABLE_COMPILER)
269 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800270 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700271#endif
Jeff Haoe701f482013-05-24 11:50:49 -0700272 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(mirror::Object*)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800273 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700274 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700275 }
276 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800279 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800280 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800282 }
283
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 mirror::Object** References() {
285 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800286 }
287
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700288#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800289 enum ShadowFrameFlag {
290 kHasReferenceArray = 1ul << 31
291 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700292 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800293 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700294#else
295 const uint32_t number_of_vregs_;
296#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700297 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700298 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700299#if defined(ART_USE_PORTABLE_COMPILER)
300 // TODO: make const in the portable case.
Brian Carlstromea46f952013-07-30 01:26:50 -0700301 mirror::ArtMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700302#else
Brian Carlstromea46f952013-07-30 01:26:50 -0700303 mirror::ArtMethod* const method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700304#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700305 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800306 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700307
Ian Rogers0399dde2012-06-06 17:09:28 -0700308 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700309};
310
Ian Rogers0399dde2012-06-06 17:09:28 -0700311// The managed stack is used to record fragments of managed code stacks. Managed code stacks
312// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
313// necessary for transitions between code using different frame layouts and transitions into native
314// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800315class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700316 public:
Ian Rogersca190662012-06-26 15:45:57 -0700317 ManagedStack()
318 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700319
320 void PushManagedStackFragment(ManagedStack* fragment) {
321 // Copy this top fragment into given fragment.
322 memcpy(fragment, this, sizeof(ManagedStack));
323 // Clear this fragment, which has become the top.
324 memset(this, 0, sizeof(ManagedStack));
325 // Link our top fragment onto the given fragment.
326 link_ = fragment;
327 }
328
329 void PopManagedStackFragment(const ManagedStack& fragment) {
330 DCHECK(&fragment == link_);
331 // Copy this given fragment back to the top.
332 memcpy(this, &fragment, sizeof(ManagedStack));
333 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700334
335 ManagedStack* GetLink() const {
336 return link_;
337 }
338
Brian Carlstromea46f952013-07-30 01:26:50 -0700339 mirror::ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700340 return top_quick_frame_;
341 }
342
Brian Carlstromea46f952013-07-30 01:26:50 -0700343 void SetTopQuickFrame(mirror::ArtMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200344 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700345 top_quick_frame_ = top;
346 }
347
348 uintptr_t GetTopQuickFramePc() const {
349 return top_quick_frame_pc_;
350 }
351
352 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200353 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700354 top_quick_frame_pc_ = pc;
355 }
356
357 static size_t TopQuickFrameOffset() {
358 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
359 }
360
361 static size_t TopQuickFramePcOffset() {
362 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
363 }
364
365 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200366 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700367 ShadowFrame* old_frame = top_shadow_frame_;
368 top_shadow_frame_ = new_top_frame;
369 new_top_frame->SetLink(old_frame);
370 return old_frame;
371 }
372
373 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200374 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700375 CHECK(top_shadow_frame_ != NULL);
376 ShadowFrame* frame = top_shadow_frame_;
377 top_shadow_frame_ = frame->GetLink();
378 return frame;
379 }
380
381 ShadowFrame* GetTopShadowFrame() const {
382 return top_shadow_frame_;
383 }
384
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800385 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200386 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800387 top_shadow_frame_ = top;
388 }
389
Ian Rogers0399dde2012-06-06 17:09:28 -0700390 static size_t TopShadowFrameOffset() {
391 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
392 }
393
TDYa127ce4cc0d2012-11-18 16:59:53 -0800394 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700395
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700397
398 private:
399 ManagedStack* link_;
400 ShadowFrame* top_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700401 mirror::ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700402 uintptr_t top_quick_frame_pc_;
403};
404
405class StackVisitor {
406 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800407 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700408
409 public:
410 virtual ~StackVisitor() {}
411
412 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700413 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700414
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700415 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700416 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700417
Brian Carlstromea46f952013-07-30 01:26:50 -0700418 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700419 if (cur_shadow_frame_ != NULL) {
420 return cur_shadow_frame_->GetMethod();
421 } else if (cur_quick_frame_ != NULL) {
422 return *cur_quick_frame_;
423 } else {
424 return NULL;
425 }
426 }
427
428 bool IsShadowFrame() const {
429 return cur_shadow_frame_ != NULL;
430 }
431
Ian Rogers0c7abda2012-09-19 13:33:42 -0700432 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
433
Ian Rogers62d6c772013-02-27 08:32:07 -0800434 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
435
Ian Rogers0c7abda2012-09-19 13:33:42 -0700436 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
437
Mathieu Chartier67022432012-11-29 18:04:50 -0800438 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700439 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800440 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700441 byte* save_addr =
442 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
443#if defined(__i386__)
444 save_addr -= kPointerSize; // account for return address
445#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800446 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700447 }
448
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700450 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800451 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700452 }
453
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700454 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700455 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700456 return GetFrameHeight() + 1;
457 }
458
Ian Rogersb726dcb2012-09-05 08:57:23 -0700459 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700460 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800461 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700462 }
463 return num_frames_;
464 }
465
Brian Carlstromea46f952013-07-30 01:26:50 -0700466 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800467 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700468
Brian Carlstromea46f952013-07-30 01:26:50 -0700469 void SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700470 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700471
472 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800473 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700474
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700475 // This is a fast-path for getting/setting values in a quick frame.
476 uint32_t* GetVRegAddr(mirror::ArtMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800477 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
478 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700479 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700480 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
481 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700482 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700483 }
484
485 uintptr_t GetReturnPc() const;
486
487 void SetReturnPc(uintptr_t new_ret_pc);
488
489 /*
490 * Return sp-relative offset for a Dalvik virtual register, compiler
491 * spill or Method* in bytes using Method*.
492 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
493 * denotes Method* and (reg <= -3) denotes a compiler temp.
494 *
495 * +------------------------+
496 * | IN[ins-1] | {Note: resides in caller's frame}
497 * | . |
498 * | IN[0] |
499 * | caller's Method* |
500 * +========================+ {Note: start of callee's frame}
501 * | core callee-save spill | {variable sized}
502 * +------------------------+
503 * | fp callee-save spill |
504 * +------------------------+
505 * | filler word | {For compatibility, if V[locals-1] used as wide
506 * +------------------------+
507 * | V[locals-1] |
508 * | V[locals-2] |
509 * | . |
510 * | . | ... (reg == 2)
511 * | V[1] | ... (reg == 1)
512 * | V[0] | ... (reg == 0) <---- "locals_start"
513 * +------------------------+
514 * | Compiler temps | ... (reg == -2)
515 * | | ... (reg == -3)
516 * | | ... (reg == -4)
517 * +------------------------+
518 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
519 * +------------------------+
520 * | OUT[outs-1] |
521 * | OUT[outs-2] |
522 * | . |
523 * | OUT[0] |
524 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
525 * +========================+
526 */
527 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700528 uint32_t core_spills, uint32_t fp_spills,
529 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700530 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700531 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
Ian Rogers0399dde2012-06-06 17:09:28 -0700532 int num_ins = code_item->ins_size_;
533 int num_regs = code_item->registers_size_ - num_ins;
534 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
535 if (reg == -2) {
536 return 0; // Method*
537 } else if (reg <= -3) {
538 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
539 } else if (reg < num_regs) {
540 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
541 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700542 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
Ian Rogers0399dde2012-06-06 17:09:28 -0700543 }
544 }
545
546 uintptr_t GetCurrentQuickFramePc() const {
547 return cur_quick_frame_pc_;
548 }
549
Brian Carlstromea46f952013-07-30 01:26:50 -0700550 mirror::ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 return cur_quick_frame_;
552 }
553
554 ShadowFrame* GetCurrentShadowFrame() const {
555 return cur_shadow_frame_;
556 }
557
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700558 StackIndirectReferenceTable* GetCurrentSirt() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700559 mirror::ArtMethod** sp = GetCurrentQuickFrame();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700560 ++sp; // Skip Method*; SIRT comes next;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700561 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
562 }
563
Ian Rogers40e3bac2012-11-20 00:09:14 -0800564 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
565
Ian Rogers7a22fa62013-01-23 12:16:16 -0800566 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800567
Ian Rogers7a22fa62013-01-23 12:16:16 -0800568 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800569
Ian Rogers0399dde2012-06-06 17:09:28 -0700570 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800571 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700572
Ian Rogersb726dcb2012-09-05 08:57:23 -0700573 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700574
Ian Rogers7a22fa62013-01-23 12:16:16 -0800575 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700576 ShadowFrame* cur_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 mirror::ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700578 uintptr_t cur_quick_frame_pc_;
579 // Lazily computed, number of frames in the stack.
580 size_t num_frames_;
581 // Depth of the frame we're currently at.
582 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700583
Ian Rogers0399dde2012-06-06 17:09:28 -0700584 protected:
585 Context* const context_;
586};
587
Elliott Hughes68e76522011-10-05 13:22:16 -0700588} // namespace art
589
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700590#endif // ART_RUNTIME_STACK_H_