blob: 700b7f1960342b324207c8a22c9e91fa28a8b5c0 [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)];
Sebastien Hertzc61124b2013-09-10 11:44:19 +020071 return Create(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -070072 }
73
74 // Create ShadowFrame for interpreter using provided memory.
75 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -070076 mirror::ArtMethod* method, uint32_t dex_pc, void* memory) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080077 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
78 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070079 }
80 ~ShadowFrame() {}
81
TDYa127ce4cc0d2012-11-18 16:59:53 -080082 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070083#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080084 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070085#else
86 return true;
87#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070088 }
Elliott Hughes68e76522011-10-05 13:22:16 -070089
TDYa127ce4cc0d2012-11-18 16:59:53 -080090 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070091#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080092 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070093#else
94 return number_of_vregs_;
95#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070096 }
97
TDYa127ce4cc0d2012-11-18 16:59:53 -080098 void SetNumberOfVRegs(uint32_t number_of_vregs) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070099#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800100 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700101#else
102 UNUSED(number_of_vregs);
103 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
104#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700105 }
106
Ian Rogers0399dde2012-06-06 17:09:28 -0700107 uint32_t GetDexPC() const {
108 return dex_pc_;
109 }
110
111 void SetDexPC(uint32_t dex_pc) {
112 dex_pc_ = dex_pc;
113 }
114
Ian Rogers0399dde2012-06-06 17:09:28 -0700115 ShadowFrame* GetLink() const {
116 return link_;
117 }
118
119 void SetLink(ShadowFrame* frame) {
120 DCHECK_NE(this, frame);
121 link_ = frame;
122 }
123
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700124 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800125 DCHECK_LT(i, NumberOfVRegs());
126 const uint32_t* vreg = &vregs_[i];
127 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700128 }
129
130 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800131 DCHECK_LT(i, NumberOfVRegs());
132 // NOTE: Strict-aliasing?
133 const uint32_t* vreg = &vregs_[i];
134 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700135 }
136
137 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200138 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800139 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700140 // Alignment attribute required for GCC 4.8
141 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
142 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700143 }
144
145 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200146 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800147 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700148 // Alignment attribute required for GCC 4.8
149 typedef const double unaligned_double __attribute__ ((aligned (4)));
150 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800151 }
152
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800154 DCHECK_LT(i, NumberOfVRegs());
155 if (HasReferenceArray()) {
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700156 mirror::Object* ref = References()[i];
157 // If the vreg reference is not equal to the vreg then the vreg reference is stale.
158 if (reinterpret_cast<uint32_t>(ref) != vregs_[i]) {
159 return nullptr;
160 }
161 return ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800162 } else {
163 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800165 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700166 }
167
Jeff Hao16743632013-05-08 10:59:04 -0700168 // Get view of vregs as range of consecutive arguments starting at i.
169 uint32_t* GetVRegArgs(size_t i) {
170 return &vregs_[i];
171 }
172
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800174 DCHECK_LT(i, NumberOfVRegs());
175 uint32_t* vreg = &vregs_[i];
176 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700177 }
178
179 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800180 DCHECK_LT(i, NumberOfVRegs());
181 uint32_t* vreg = &vregs_[i];
182 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700183 }
184
185 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200186 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800187 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700188 // Alignment attribute required for GCC 4.8
189 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
190 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700191 }
192
193 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200194 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800195 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700196 // Alignment attribute required for GCC 4.8
197 typedef double unaligned_double __attribute__ ((aligned (4)));
198 *reinterpret_cast<unaligned_double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700199 }
200
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800202 DCHECK_LT(i, NumberOfVRegs());
203 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800205 if (HasReferenceArray()) {
206 References()[i] = val;
207 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700208 }
209
Brian Carlstromea46f952013-07-30 01:26:50 -0700210 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700211 DCHECK_NE(method_, static_cast<void*>(NULL));
212 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700213 }
214
Ian Rogers62d6c772013-02-27 08:32:07 -0800215 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
216
Jeff Haoe701f482013-05-24 11:50:49 -0700217 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
218
Ian Rogers62d6c772013-02-27 08:32:07 -0800219 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
220
Brian Carlstromea46f952013-07-30 01:26:50 -0700221 void SetMethod(mirror::ArtMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700222#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700223 DCHECK_NE(method, static_cast<void*>(NULL));
224 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700225#else
226 UNUSED(method);
227 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
228#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700229 }
230
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800232 if (HasReferenceArray()) {
233 return ((&References()[0] <= shadow_frame_entry_obj) &&
234 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
235 } else {
236 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
237 return ((&vregs_[0] <= shadow_frame_entry) &&
238 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700239 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700240 }
241
Ian Rogers0399dde2012-06-06 17:09:28 -0700242 static size_t LinkOffset() {
243 return OFFSETOF_MEMBER(ShadowFrame, link_);
244 }
245
Ian Rogers0399dde2012-06-06 17:09:28 -0700246 static size_t MethodOffset() {
247 return OFFSETOF_MEMBER(ShadowFrame, method_);
248 }
249
Ian Rogers0399dde2012-06-06 17:09:28 -0700250 static size_t DexPCOffset() {
251 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
252 }
253
Ian Rogers5438ad82012-10-15 17:22:44 -0700254 static size_t NumberOfVRegsOffset() {
255 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
256 }
257
TDYa127ce4cc0d2012-11-18 16:59:53 -0800258 static size_t VRegsOffset() {
259 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700260 }
261
Elliott Hughes68e76522011-10-05 13:22:16 -0700262 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700263 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800265 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
266 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700267#if defined(ART_USE_PORTABLE_COMPILER)
268 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800269 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700270#endif
Jeff Haoe701f482013-05-24 11:50:49 -0700271 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(mirror::Object*)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800272 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700273 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700274 }
275 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700276
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800278 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800279 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800281 }
282
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 mirror::Object** References() {
284 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800285 }
286
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700287#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800288 enum ShadowFrameFlag {
289 kHasReferenceArray = 1ul << 31
290 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700291 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800292 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700293#else
294 const uint32_t number_of_vregs_;
295#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700296 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700297 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700298#if defined(ART_USE_PORTABLE_COMPILER)
299 // TODO: make const in the portable case.
Brian Carlstromea46f952013-07-30 01:26:50 -0700300 mirror::ArtMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700301#else
Brian Carlstromea46f952013-07-30 01:26:50 -0700302 mirror::ArtMethod* const method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700303#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700304 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800305 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700306
Ian Rogers0399dde2012-06-06 17:09:28 -0700307 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700308};
309
Ian Rogers0399dde2012-06-06 17:09:28 -0700310// The managed stack is used to record fragments of managed code stacks. Managed code stacks
311// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
312// necessary for transitions between code using different frame layouts and transitions into native
313// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800314class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700315 public:
Ian Rogersca190662012-06-26 15:45:57 -0700316 ManagedStack()
317 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700318
319 void PushManagedStackFragment(ManagedStack* fragment) {
320 // Copy this top fragment into given fragment.
321 memcpy(fragment, this, sizeof(ManagedStack));
322 // Clear this fragment, which has become the top.
323 memset(this, 0, sizeof(ManagedStack));
324 // Link our top fragment onto the given fragment.
325 link_ = fragment;
326 }
327
328 void PopManagedStackFragment(const ManagedStack& fragment) {
329 DCHECK(&fragment == link_);
330 // Copy this given fragment back to the top.
331 memcpy(this, &fragment, sizeof(ManagedStack));
332 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700333
334 ManagedStack* GetLink() const {
335 return link_;
336 }
337
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 mirror::ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700339 return top_quick_frame_;
340 }
341
Brian Carlstromea46f952013-07-30 01:26:50 -0700342 void SetTopQuickFrame(mirror::ArtMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200343 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700344 top_quick_frame_ = top;
345 }
346
347 uintptr_t GetTopQuickFramePc() const {
348 return top_quick_frame_pc_;
349 }
350
351 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200352 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700353 top_quick_frame_pc_ = pc;
354 }
355
356 static size_t TopQuickFrameOffset() {
357 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
358 }
359
360 static size_t TopQuickFramePcOffset() {
361 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
362 }
363
364 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200365 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700366 ShadowFrame* old_frame = top_shadow_frame_;
367 top_shadow_frame_ = new_top_frame;
368 new_top_frame->SetLink(old_frame);
369 return old_frame;
370 }
371
372 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200373 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700374 CHECK(top_shadow_frame_ != NULL);
375 ShadowFrame* frame = top_shadow_frame_;
376 top_shadow_frame_ = frame->GetLink();
377 return frame;
378 }
379
380 ShadowFrame* GetTopShadowFrame() const {
381 return top_shadow_frame_;
382 }
383
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800384 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200385 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800386 top_shadow_frame_ = top;
387 }
388
Ian Rogers0399dde2012-06-06 17:09:28 -0700389 static size_t TopShadowFrameOffset() {
390 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
391 }
392
TDYa127ce4cc0d2012-11-18 16:59:53 -0800393 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700394
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700396
397 private:
398 ManagedStack* link_;
399 ShadowFrame* top_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 mirror::ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700401 uintptr_t top_quick_frame_pc_;
402};
403
404class StackVisitor {
405 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800406 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700407
408 public:
409 virtual ~StackVisitor() {}
410
411 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700412 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700413
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700415 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700416
Brian Carlstromea46f952013-07-30 01:26:50 -0700417 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700418 if (cur_shadow_frame_ != NULL) {
419 return cur_shadow_frame_->GetMethod();
420 } else if (cur_quick_frame_ != NULL) {
421 return *cur_quick_frame_;
422 } else {
423 return NULL;
424 }
425 }
426
427 bool IsShadowFrame() const {
428 return cur_shadow_frame_ != NULL;
429 }
430
Ian Rogers0c7abda2012-09-19 13:33:42 -0700431 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
432
Ian Rogers62d6c772013-02-27 08:32:07 -0800433 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
434
Ian Rogers0c7abda2012-09-19 13:33:42 -0700435 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
436
Mathieu Chartier67022432012-11-29 18:04:50 -0800437 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700438 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800439 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700440 byte* save_addr =
441 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
442#if defined(__i386__)
443 save_addr -= kPointerSize; // account for return address
444#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800445 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700446 }
447
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700448 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700449 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700451 }
452
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700453 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700454 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700455 return GetFrameHeight() + 1;
456 }
457
Ian Rogersb726dcb2012-09-05 08:57:23 -0700458 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700459 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800460 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700461 }
462 return num_frames_;
463 }
464
Brian Carlstromea46f952013-07-30 01:26:50 -0700465 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700467
Brian Carlstromea46f952013-07-30 01:26:50 -0700468 void SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700469 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700470
471 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800472 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700473
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700474 // This is a fast-path for getting/setting values in a quick frame.
475 uint32_t* GetVRegAddr(mirror::ArtMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800476 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
477 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700478 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700479 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
480 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700481 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700482 }
483
484 uintptr_t GetReturnPc() const;
485
486 void SetReturnPc(uintptr_t new_ret_pc);
487
488 /*
489 * Return sp-relative offset for a Dalvik virtual register, compiler
490 * spill or Method* in bytes using Method*.
491 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
492 * denotes Method* and (reg <= -3) denotes a compiler temp.
493 *
494 * +------------------------+
495 * | IN[ins-1] | {Note: resides in caller's frame}
496 * | . |
497 * | IN[0] |
498 * | caller's Method* |
499 * +========================+ {Note: start of callee's frame}
500 * | core callee-save spill | {variable sized}
501 * +------------------------+
502 * | fp callee-save spill |
503 * +------------------------+
504 * | filler word | {For compatibility, if V[locals-1] used as wide
505 * +------------------------+
506 * | V[locals-1] |
507 * | V[locals-2] |
508 * | . |
509 * | . | ... (reg == 2)
510 * | V[1] | ... (reg == 1)
511 * | V[0] | ... (reg == 0) <---- "locals_start"
512 * +------------------------+
513 * | Compiler temps | ... (reg == -2)
514 * | | ... (reg == -3)
515 * | | ... (reg == -4)
516 * +------------------------+
517 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
518 * +------------------------+
519 * | OUT[outs-1] |
520 * | OUT[outs-2] |
521 * | . |
522 * | OUT[0] |
523 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
524 * +========================+
525 */
526 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700527 uint32_t core_spills, uint32_t fp_spills,
528 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700529 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700530 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
Ian Rogers0399dde2012-06-06 17:09:28 -0700531 int num_ins = code_item->ins_size_;
532 int num_regs = code_item->registers_size_ - num_ins;
533 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
534 if (reg == -2) {
535 return 0; // Method*
536 } else if (reg <= -3) {
537 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
538 } else if (reg < num_regs) {
539 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
540 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700541 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
Ian Rogers0399dde2012-06-06 17:09:28 -0700542 }
543 }
544
545 uintptr_t GetCurrentQuickFramePc() const {
546 return cur_quick_frame_pc_;
547 }
548
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 mirror::ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700550 return cur_quick_frame_;
551 }
552
553 ShadowFrame* GetCurrentShadowFrame() const {
554 return cur_shadow_frame_;
555 }
556
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700557 StackIndirectReferenceTable* GetCurrentSirt() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700558 mirror::ArtMethod** sp = GetCurrentQuickFrame();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700559 ++sp; // Skip Method*; SIRT comes next;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700560 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
561 }
562
Ian Rogers40e3bac2012-11-20 00:09:14 -0800563 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
564
Ian Rogers7a22fa62013-01-23 12:16:16 -0800565 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800566
Ian Rogers7a22fa62013-01-23 12:16:16 -0800567 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800568
Ian Rogers0399dde2012-06-06 17:09:28 -0700569 private:
Ian Rogers62d6c772013-02-27 08:32:07 -0800570 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700571
Ian Rogersb726dcb2012-09-05 08:57:23 -0700572 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700573
Ian Rogers7a22fa62013-01-23 12:16:16 -0800574 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700575 ShadowFrame* cur_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 mirror::ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 uintptr_t cur_quick_frame_pc_;
578 // Lazily computed, number of frames in the stack.
579 size_t num_frames_;
580 // Depth of the frame we're currently at.
581 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700582
Ian Rogers0399dde2012-06-06 17:09:28 -0700583 protected:
584 Context* const context_;
585};
586
Elliott Hughes68e76522011-10-05 13:22:16 -0700587} // namespace art
588
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700589#endif // ART_RUNTIME_STACK_H_