blob: fbfacb1733bdc6621993f256ada560dbdb0b157e [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
jeffhao725a9572012-11-13 18:20:12 -080021#include "instrumentation.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "base/macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070023#include "oat/runtime/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 {
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class 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,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069 mirror::AbstractMethod* 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,
77 mirror::AbstractMethod* 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];
141 return *reinterpret_cast<const int64_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700142 }
143
144 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200145 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800146 const uint32_t* vreg = &vregs_[i];
147 return *reinterpret_cast<const double*>(vreg);
148 }
149
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800151 DCHECK_LT(i, NumberOfVRegs());
152 if (HasReferenceArray()) {
153 return References()[i];
154 } else {
155 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800157 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700158 }
159
Jeff Hao16743632013-05-08 10:59:04 -0700160 // Get view of vregs as range of consecutive arguments starting at i.
161 uint32_t* GetVRegArgs(size_t i) {
162 return &vregs_[i];
163 }
164
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700165 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800166 DCHECK_LT(i, NumberOfVRegs());
167 uint32_t* vreg = &vregs_[i];
168 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700169 }
170
171 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800172 DCHECK_LT(i, NumberOfVRegs());
173 uint32_t* vreg = &vregs_[i];
174 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700175 }
176
177 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200178 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800179 uint32_t* vreg = &vregs_[i];
180 *reinterpret_cast<int64_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700181 }
182
183 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200184 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800185 uint32_t* vreg = &vregs_[i];
186 *reinterpret_cast<double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700187 }
188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800190 DCHECK_LT(i, NumberOfVRegs());
191 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800193 if (HasReferenceArray()) {
194 References()[i] = val;
195 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700196 }
197
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700199 DCHECK_NE(method_, static_cast<void*>(NULL));
200 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700201 }
202
Ian Rogers62d6c772013-02-27 08:32:07 -0800203 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
204
Jeff Haoe701f482013-05-24 11:50:49 -0700205 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
206
Ian Rogers62d6c772013-02-27 08:32:07 -0800207 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 void SetMethod(mirror::AbstractMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700210#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700211 DCHECK_NE(method, static_cast<void*>(NULL));
212 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700213#else
214 UNUSED(method);
215 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
216#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700217 }
218
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800220 if (HasReferenceArray()) {
221 return ((&References()[0] <= shadow_frame_entry_obj) &&
222 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
223 } else {
224 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
225 return ((&vregs_[0] <= shadow_frame_entry) &&
226 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700227 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700228 }
229
Ian Rogers0399dde2012-06-06 17:09:28 -0700230 static size_t LinkOffset() {
231 return OFFSETOF_MEMBER(ShadowFrame, link_);
232 }
233
Ian Rogers0399dde2012-06-06 17:09:28 -0700234 static size_t MethodOffset() {
235 return OFFSETOF_MEMBER(ShadowFrame, method_);
236 }
237
Ian Rogers0399dde2012-06-06 17:09:28 -0700238 static size_t DexPCOffset() {
239 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
240 }
241
Ian Rogers5438ad82012-10-15 17:22:44 -0700242 static size_t NumberOfVRegsOffset() {
243 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
244 }
245
TDYa127ce4cc0d2012-11-18 16:59:53 -0800246 static size_t VRegsOffset() {
247 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700248 }
249
Elliott Hughes68e76522011-10-05 13:22:16 -0700250 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::AbstractMethod* method,
252 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800253 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
254 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700255#if defined(ART_USE_PORTABLE_COMPILER)
256 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800257 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700258#endif
Jeff Haoe701f482013-05-24 11:50:49 -0700259 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(mirror::Object*)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800260 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700261 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700262 }
263 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700264
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800266 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800267 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800269 }
270
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 mirror::Object** References() {
272 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800273 }
274
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700275#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800276 enum ShadowFrameFlag {
277 kHasReferenceArray = 1ul << 31
278 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700279 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800280 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700281#else
282 const uint32_t number_of_vregs_;
283#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700284 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700285 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700286#if defined(ART_USE_PORTABLE_COMPILER)
287 // TODO: make const in the portable case.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 mirror::AbstractMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700289#else
290 mirror::AbstractMethod* const method_;
291#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700292 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800293 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700294
Ian Rogers0399dde2012-06-06 17:09:28 -0700295 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700296};
297
Ian Rogers0399dde2012-06-06 17:09:28 -0700298// The managed stack is used to record fragments of managed code stacks. Managed code stacks
299// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
300// necessary for transitions between code using different frame layouts and transitions into native
301// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800302class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700303 public:
Ian Rogersca190662012-06-26 15:45:57 -0700304 ManagedStack()
305 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700306
307 void PushManagedStackFragment(ManagedStack* fragment) {
308 // Copy this top fragment into given fragment.
309 memcpy(fragment, this, sizeof(ManagedStack));
310 // Clear this fragment, which has become the top.
311 memset(this, 0, sizeof(ManagedStack));
312 // Link our top fragment onto the given fragment.
313 link_ = fragment;
314 }
315
316 void PopManagedStackFragment(const ManagedStack& fragment) {
317 DCHECK(&fragment == link_);
318 // Copy this given fragment back to the top.
319 memcpy(this, &fragment, sizeof(ManagedStack));
320 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700321
322 ManagedStack* GetLink() const {
323 return link_;
324 }
325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 mirror::AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700327 return top_quick_frame_;
328 }
329
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 void SetTopQuickFrame(mirror::AbstractMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200331 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700332 top_quick_frame_ = top;
333 }
334
335 uintptr_t GetTopQuickFramePc() const {
336 return top_quick_frame_pc_;
337 }
338
339 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200340 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700341 top_quick_frame_pc_ = pc;
342 }
343
344 static size_t TopQuickFrameOffset() {
345 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
346 }
347
348 static size_t TopQuickFramePcOffset() {
349 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
350 }
351
352 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200353 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700354 ShadowFrame* old_frame = top_shadow_frame_;
355 top_shadow_frame_ = new_top_frame;
356 new_top_frame->SetLink(old_frame);
357 return old_frame;
358 }
359
360 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200361 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700362 CHECK(top_shadow_frame_ != NULL);
363 ShadowFrame* frame = top_shadow_frame_;
364 top_shadow_frame_ = frame->GetLink();
365 return frame;
366 }
367
368 ShadowFrame* GetTopShadowFrame() const {
369 return top_shadow_frame_;
370 }
371
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800372 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200373 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800374 top_shadow_frame_ = top;
375 }
376
Ian Rogers0399dde2012-06-06 17:09:28 -0700377 static size_t TopShadowFrameOffset() {
378 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
379 }
380
TDYa127ce4cc0d2012-11-18 16:59:53 -0800381 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700382
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700384
385 private:
386 ManagedStack* link_;
387 ShadowFrame* top_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 mirror::AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700389 uintptr_t top_quick_frame_pc_;
390};
391
392class StackVisitor {
393 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800394 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700395
396 public:
397 virtual ~StackVisitor() {}
398
399 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700400 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700401
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700402 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700403 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700404
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700406 if (cur_shadow_frame_ != NULL) {
407 return cur_shadow_frame_->GetMethod();
408 } else if (cur_quick_frame_ != NULL) {
409 return *cur_quick_frame_;
410 } else {
411 return NULL;
412 }
413 }
414
415 bool IsShadowFrame() const {
416 return cur_shadow_frame_ != NULL;
417 }
418
Ian Rogers0c7abda2012-09-19 13:33:42 -0700419 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
420
Ian Rogers62d6c772013-02-27 08:32:07 -0800421 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
422
Ian Rogers0c7abda2012-09-19 13:33:42 -0700423 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
424
Mathieu Chartier67022432012-11-29 18:04:50 -0800425 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700426 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800427 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700428 byte* save_addr =
429 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
430#if defined(__i386__)
431 save_addr -= kPointerSize; // account for return address
432#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800433 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700434 }
435
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700436 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700437 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800438 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700439 }
440
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700441 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700442 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700443 return GetFrameHeight() + 1;
444 }
445
Ian Rogersb726dcb2012-09-05 08:57:23 -0700446 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700447 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800448 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700449 }
450 return num_frames_;
451 }
452
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 uint32_t GetVReg(mirror::AbstractMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700455
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 void SetVReg(mirror::AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700458
459 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800460 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700461
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800462 uint32_t GetVReg(mirror::AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800463 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
464 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700465 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700466 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
467 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700468 return *reinterpret_cast<uint32_t*>(vreg_addr);
469 }
470
471 uintptr_t GetReturnPc() const;
472
473 void SetReturnPc(uintptr_t new_ret_pc);
474
475 /*
476 * Return sp-relative offset for a Dalvik virtual register, compiler
477 * spill or Method* in bytes using Method*.
478 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
479 * denotes Method* and (reg <= -3) denotes a compiler temp.
480 *
481 * +------------------------+
482 * | IN[ins-1] | {Note: resides in caller's frame}
483 * | . |
484 * | IN[0] |
485 * | caller's Method* |
486 * +========================+ {Note: start of callee's frame}
487 * | core callee-save spill | {variable sized}
488 * +------------------------+
489 * | fp callee-save spill |
490 * +------------------------+
491 * | filler word | {For compatibility, if V[locals-1] used as wide
492 * +------------------------+
493 * | V[locals-1] |
494 * | V[locals-2] |
495 * | . |
496 * | . | ... (reg == 2)
497 * | V[1] | ... (reg == 1)
498 * | V[0] | ... (reg == 0) <---- "locals_start"
499 * +------------------------+
500 * | Compiler temps | ... (reg == -2)
501 * | | ... (reg == -3)
502 * | | ... (reg == -4)
503 * +------------------------+
504 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
505 * +------------------------+
506 * | OUT[outs-1] |
507 * | OUT[outs-2] |
508 * | . |
509 * | OUT[0] |
510 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
511 * +========================+
512 */
513 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700514 uint32_t core_spills, uint32_t fp_spills,
515 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700516 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
517 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
518 int num_ins = code_item->ins_size_;
519 int num_regs = code_item->registers_size_ - num_ins;
520 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
521 if (reg == -2) {
522 return 0; // Method*
523 } else if (reg <= -3) {
524 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
525 } else if (reg < num_regs) {
526 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
527 } else {
528 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
529 }
530 }
531
532 uintptr_t GetCurrentQuickFramePc() const {
533 return cur_quick_frame_pc_;
534 }
535
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 mirror::AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700537 return cur_quick_frame_;
538 }
539
540 ShadowFrame* GetCurrentShadowFrame() const {
541 return cur_shadow_frame_;
542 }
543
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700544 StackIndirectReferenceTable* GetCurrentSirt() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700546 ++sp; // Skip Method*; SIRT comes next;
547 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
548 }
549
Ian Rogers40e3bac2012-11-20 00:09:14 -0800550 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
551
Ian Rogers7a22fa62013-01-23 12:16:16 -0800552 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800553
Ian Rogers7a22fa62013-01-23 12:16:16 -0800554 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800555
Ian Rogers0399dde2012-06-06 17:09:28 -0700556 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700557
Ian Rogers62d6c772013-02-27 08:32:07 -0800558 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700559
Ian Rogersb726dcb2012-09-05 08:57:23 -0700560 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700561
Ian Rogers7a22fa62013-01-23 12:16:16 -0800562 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700563 ShadowFrame* cur_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 mirror::AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700565 uintptr_t cur_quick_frame_pc_;
566 // Lazily computed, number of frames in the stack.
567 size_t num_frames_;
568 // Depth of the frame we're currently at.
569 size_t cur_depth_;
570 protected:
571 Context* const context_;
572};
573
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800574class VmapTable {
575 public:
576 explicit VmapTable(const uint16_t* table) : table_(table) {
577 }
578
579 uint16_t operator[](size_t i) const {
580 return table_[i + 1];
581 }
582
583 size_t size() const {
584 return table_[0];
585 }
586
587 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
588 // 'kind' is unknown or constant.
589 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
590 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
591 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
592 kind == kDoubleHiVReg || kind == kImpreciseConstant);
593 vmap_offset = 0xEBAD0FF5;
594 // TODO: take advantage of the registers being ordered
595 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
596 // are never promoted to floating point registers.
597 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
598 bool in_floats = false;
599 for (size_t i = 0; i < size(); ++i) {
600 // Stop if we find what we are are looking for.
601 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
602 vmap_offset = i;
603 return true;
604 }
605 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
606 if (table_[i + 1] == 0xffff) {
607 in_floats = true;
608 }
609 }
610 return false;
611 }
612
613 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
614 // by IsInContext above). If the kind is floating point then the result will be a floating point
615 // register number, otherwise it will be an integer register number.
616 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
617 // Compute the register we need to load from the context.
618 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
619 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
620 kind == kDoubleHiVReg || kind == kImpreciseConstant);
621 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
622 // are never promoted to floating point registers.
623 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
624 uint32_t matches = 0;
625 if (is_float) {
626 while (table_[matches] != 0xffff) {
627 matches++;
628 }
629 }
630 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
631 uint32_t spill_shifts = 0;
632 while (matches != (vmap_offset + 1)) {
633 DCHECK_NE(spill_mask, 0u);
634 matches += spill_mask & 1; // Add 1 if the low bit is set
635 spill_mask >>= 1;
636 spill_shifts++;
637 }
638 spill_shifts--; // wind back one as we want the last match
639 return spill_shifts;
640 }
641 private:
642 const uint16_t* table_;
643};
644
Elliott Hughes68e76522011-10-05 13:22:16 -0700645} // namespace art
646
647#endif // ART_SRC_STACK_H_