blob: 3d6b06a32d732740bc89ab430e823b6b0d963c78 [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
Mathieu Chartier590fee92013-09-13 13:46:47 -0700153 template <bool kChecked = false>
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];
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 if (kChecked) {
159 CHECK(VerifyReference(ref)) << "VReg " << i << "(" << ref
160 << ") is in protected space, reference array " << true;
161 }
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700162 // If the vreg reference is not equal to the vreg then the vreg reference is stale.
163 if (reinterpret_cast<uint32_t>(ref) != vregs_[i]) {
164 return nullptr;
165 }
166 return ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800167 } else {
168 const uint32_t* vreg = &vregs_[i];
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169 mirror::Object* ref = *reinterpret_cast<mirror::Object* const*>(vreg);
170 if (kChecked) {
171 CHECK(VerifyReference(ref)) << "VReg " << i
172 << "(" << ref << ") is in protected space, reference array " << false;
173 }
174 return ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800175 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700176 }
177
Jeff Hao16743632013-05-08 10:59:04 -0700178 // Get view of vregs as range of consecutive arguments starting at i.
179 uint32_t* GetVRegArgs(size_t i) {
180 return &vregs_[i];
181 }
182
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700183 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800184 DCHECK_LT(i, NumberOfVRegs());
185 uint32_t* vreg = &vregs_[i];
186 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700187 // This is needed for moving collectors since these can update the vreg references if they
188 // happen to agree with references in the reference array.
189 if (kMovingCollector && HasReferenceArray()) {
190 References()[i] = nullptr;
191 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700192 }
193
194 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800195 DCHECK_LT(i, NumberOfVRegs());
196 uint32_t* vreg = &vregs_[i];
197 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700198 // This is needed for moving collectors since these can update the vreg references if they
199 // happen to agree with references in the reference array.
200 if (kMovingCollector && HasReferenceArray()) {
201 References()[i] = nullptr;
202 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700203 }
204
205 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200206 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800207 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700208 // Alignment attribute required for GCC 4.8
209 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
210 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700211 // This is needed for moving collectors since these can update the vreg references if they
212 // happen to agree with references in the reference array.
213 if (kMovingCollector && HasReferenceArray()) {
214 References()[i] = nullptr;
215 References()[i + 1] = nullptr;
216 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700217 }
218
219 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200220 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800221 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700222 // Alignment attribute required for GCC 4.8
223 typedef double unaligned_double __attribute__ ((aligned (4)));
224 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700225 // This is needed for moving collectors since these can update the vreg references if they
226 // happen to agree with references in the reference array.
227 if (kMovingCollector && HasReferenceArray()) {
228 References()[i] = nullptr;
229 References()[i + 1] = nullptr;
230 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700231 }
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800234 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier590fee92013-09-13 13:46:47 -0700235 DCHECK(!kMovingCollector || VerifyReference(val))
236 << "VReg " << i << "(" << val << ") is in protected space";
TDYa127ce4cc0d2012-11-18 16:59:53 -0800237 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800239 if (HasReferenceArray()) {
240 References()[i] = val;
241 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700242 }
243
Brian Carlstromea46f952013-07-30 01:26:50 -0700244 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700245 DCHECK_NE(method_, static_cast<void*>(NULL));
246 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700247 }
248
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
250
Jeff Haoe701f482013-05-24 11:50:49 -0700251 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
252
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
254
Brian Carlstromea46f952013-07-30 01:26:50 -0700255 void SetMethod(mirror::ArtMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700256#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700257 DCHECK_NE(method, static_cast<void*>(NULL));
258 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700259#else
260 UNUSED(method);
261 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
262#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700263 }
264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800266 if (HasReferenceArray()) {
267 return ((&References()[0] <= shadow_frame_entry_obj) &&
268 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
269 } else {
270 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
271 return ((&vregs_[0] <= shadow_frame_entry) &&
272 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700273 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700274 }
275
Ian Rogers0399dde2012-06-06 17:09:28 -0700276 static size_t LinkOffset() {
277 return OFFSETOF_MEMBER(ShadowFrame, link_);
278 }
279
Ian Rogers0399dde2012-06-06 17:09:28 -0700280 static size_t MethodOffset() {
281 return OFFSETOF_MEMBER(ShadowFrame, method_);
282 }
283
Ian Rogers0399dde2012-06-06 17:09:28 -0700284 static size_t DexPCOffset() {
285 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
286 }
287
Ian Rogers5438ad82012-10-15 17:22:44 -0700288 static size_t NumberOfVRegsOffset() {
289 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
290 }
291
TDYa127ce4cc0d2012-11-18 16:59:53 -0800292 static size_t VRegsOffset() {
293 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700294 }
295
Elliott Hughes68e76522011-10-05 13:22:16 -0700296 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700297 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800299 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
300 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700301#if defined(ART_USE_PORTABLE_COMPILER)
302 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800303 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700304#endif
Jeff Haoe701f482013-05-24 11:50:49 -0700305 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(mirror::Object*)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800306 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700307 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700308 }
309 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700310
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800312 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800313 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800315 }
316
Mathieu Chartier590fee92013-09-13 13:46:47 -0700317 bool VerifyReference(const mirror::Object* val) const;
318
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 mirror::Object** References() {
320 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800321 }
322
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700323#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800324 enum ShadowFrameFlag {
325 kHasReferenceArray = 1ul << 31
326 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700327 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800328 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700329#else
330 const uint32_t number_of_vregs_;
331#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700332 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700333 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700334#if defined(ART_USE_PORTABLE_COMPILER)
335 // TODO: make const in the portable case.
Brian Carlstromea46f952013-07-30 01:26:50 -0700336 mirror::ArtMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700337#else
Brian Carlstromea46f952013-07-30 01:26:50 -0700338 mirror::ArtMethod* const method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700339#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700340 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800341 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700342
Ian Rogers0399dde2012-06-06 17:09:28 -0700343 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700344};
345
Ian Rogers0399dde2012-06-06 17:09:28 -0700346// The managed stack is used to record fragments of managed code stacks. Managed code stacks
347// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
348// necessary for transitions between code using different frame layouts and transitions into native
349// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800350class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700351 public:
Ian Rogersca190662012-06-26 15:45:57 -0700352 ManagedStack()
353 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700354
355 void PushManagedStackFragment(ManagedStack* fragment) {
356 // Copy this top fragment into given fragment.
357 memcpy(fragment, this, sizeof(ManagedStack));
358 // Clear this fragment, which has become the top.
359 memset(this, 0, sizeof(ManagedStack));
360 // Link our top fragment onto the given fragment.
361 link_ = fragment;
362 }
363
364 void PopManagedStackFragment(const ManagedStack& fragment) {
365 DCHECK(&fragment == link_);
366 // Copy this given fragment back to the top.
367 memcpy(this, &fragment, sizeof(ManagedStack));
368 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700369
370 ManagedStack* GetLink() const {
371 return link_;
372 }
373
Brian Carlstromea46f952013-07-30 01:26:50 -0700374 mirror::ArtMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700375 return top_quick_frame_;
376 }
377
Brian Carlstromea46f952013-07-30 01:26:50 -0700378 void SetTopQuickFrame(mirror::ArtMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200379 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700380 top_quick_frame_ = top;
381 }
382
383 uintptr_t GetTopQuickFramePc() const {
384 return top_quick_frame_pc_;
385 }
386
387 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200388 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700389 top_quick_frame_pc_ = pc;
390 }
391
392 static size_t TopQuickFrameOffset() {
393 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
394 }
395
396 static size_t TopQuickFramePcOffset() {
397 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
398 }
399
400 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200401 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700402 ShadowFrame* old_frame = top_shadow_frame_;
403 top_shadow_frame_ = new_top_frame;
404 new_top_frame->SetLink(old_frame);
405 return old_frame;
406 }
407
408 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200409 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700410 CHECK(top_shadow_frame_ != NULL);
411 ShadowFrame* frame = top_shadow_frame_;
412 top_shadow_frame_ = frame->GetLink();
413 return frame;
414 }
415
416 ShadowFrame* GetTopShadowFrame() const {
417 return top_shadow_frame_;
418 }
419
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800420 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200421 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800422 top_shadow_frame_ = top;
423 }
424
Ian Rogers0399dde2012-06-06 17:09:28 -0700425 static size_t TopShadowFrameOffset() {
426 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
427 }
428
TDYa127ce4cc0d2012-11-18 16:59:53 -0800429 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700430
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700432
433 private:
434 ManagedStack* link_;
435 ShadowFrame* top_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 mirror::ArtMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700437 uintptr_t top_quick_frame_pc_;
438};
439
440class StackVisitor {
441 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800442 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700443
444 public:
445 virtual ~StackVisitor() {}
446
447 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700448 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700449
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700450 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700452
Brian Carlstromea46f952013-07-30 01:26:50 -0700453 mirror::ArtMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700454 if (cur_shadow_frame_ != NULL) {
455 return cur_shadow_frame_->GetMethod();
456 } else if (cur_quick_frame_ != NULL) {
457 return *cur_quick_frame_;
458 } else {
459 return NULL;
460 }
461 }
462
463 bool IsShadowFrame() const {
464 return cur_shadow_frame_ != NULL;
465 }
466
Ian Rogers0c7abda2012-09-19 13:33:42 -0700467 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
468
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
470
Ian Rogers0c7abda2012-09-19 13:33:42 -0700471 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
472
Mathieu Chartier67022432012-11-29 18:04:50 -0800473 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700474 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800475 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700476 byte* save_addr =
477 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
478#if defined(__i386__)
479 save_addr -= kPointerSize; // account for return address
480#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800481 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700482 }
483
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700484 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700485 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800486 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700487 }
488
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700489 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700490 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700491 return GetFrameHeight() + 1;
492 }
493
Ian Rogersb726dcb2012-09-05 08:57:23 -0700494 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700495 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800496 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700497 }
498 return num_frames_;
499 }
500
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700503
Brian Carlstromea46f952013-07-30 01:26:50 -0700504 void SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700506
507 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800508 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700509
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700510 // This is a fast-path for getting/setting values in a quick frame.
511 uint32_t* GetVRegAddr(mirror::ArtMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800512 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
513 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700514 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700515 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
516 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700517 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700518 }
519
520 uintptr_t GetReturnPc() const;
521
522 void SetReturnPc(uintptr_t new_ret_pc);
523
524 /*
525 * Return sp-relative offset for a Dalvik virtual register, compiler
526 * spill or Method* in bytes using Method*.
527 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
528 * denotes Method* and (reg <= -3) denotes a compiler temp.
529 *
530 * +------------------------+
531 * | IN[ins-1] | {Note: resides in caller's frame}
532 * | . |
533 * | IN[0] |
534 * | caller's Method* |
535 * +========================+ {Note: start of callee's frame}
536 * | core callee-save spill | {variable sized}
537 * +------------------------+
538 * | fp callee-save spill |
539 * +------------------------+
540 * | filler word | {For compatibility, if V[locals-1] used as wide
541 * +------------------------+
542 * | V[locals-1] |
543 * | V[locals-2] |
544 * | . |
545 * | . | ... (reg == 2)
546 * | V[1] | ... (reg == 1)
547 * | V[0] | ... (reg == 0) <---- "locals_start"
548 * +------------------------+
549 * | Compiler temps | ... (reg == -2)
550 * | | ... (reg == -3)
551 * | | ... (reg == -4)
552 * +------------------------+
553 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
554 * +------------------------+
555 * | OUT[outs-1] |
556 * | OUT[outs-2] |
557 * | . |
558 * | OUT[0] |
559 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
560 * +========================+
561 */
562 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700563 uint32_t core_spills, uint32_t fp_spills,
564 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700565 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700566 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
Ian Rogers0399dde2012-06-06 17:09:28 -0700567 int num_ins = code_item->ins_size_;
568 int num_regs = code_item->registers_size_ - num_ins;
569 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
570 if (reg == -2) {
571 return 0; // Method*
572 } else if (reg <= -3) {
573 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
574 } else if (reg < num_regs) {
575 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
576 } else {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700577 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
Ian Rogers0399dde2012-06-06 17:09:28 -0700578 }
579 }
580
581 uintptr_t GetCurrentQuickFramePc() const {
582 return cur_quick_frame_pc_;
583 }
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 mirror::ArtMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700586 return cur_quick_frame_;
587 }
588
589 ShadowFrame* GetCurrentShadowFrame() const {
590 return cur_shadow_frame_;
591 }
592
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700593 StackIndirectReferenceTable* GetCurrentSirt() const {
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 mirror::ArtMethod** sp = GetCurrentQuickFrame();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700595 ++sp; // Skip Method*; SIRT comes next;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700596 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
597 }
598
Ian Rogers40e3bac2012-11-20 00:09:14 -0800599 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
600
Ian Rogers7a22fa62013-01-23 12:16:16 -0800601 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800602
Ian Rogers7a22fa62013-01-23 12:16:16 -0800603 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800604
Ian Rogers0399dde2012-06-06 17:09:28 -0700605 private:
Sebastien Hertz74e256b2013-10-04 10:40:37 +0200606 instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700607
Ian Rogersb726dcb2012-09-05 08:57:23 -0700608 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700609
Ian Rogers7a22fa62013-01-23 12:16:16 -0800610 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700611 ShadowFrame* cur_shadow_frame_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 mirror::ArtMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700613 uintptr_t cur_quick_frame_pc_;
614 // Lazily computed, number of frames in the stack.
615 size_t num_frames_;
616 // Depth of the frame we're currently at.
617 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700618
Ian Rogers0399dde2012-06-06 17:09:28 -0700619 protected:
620 Context* const context_;
621};
622
Elliott Hughes68e76522011-10-05 13:22:16 -0700623} // namespace art
624
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700625#endif // ART_RUNTIME_STACK_H_