blob: 8e597b2b3a5b6cdcfb101c450f1eda3291c55ff8 [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:
TDYa127ce4cc0d2012-11-18 16:59:53 -080061 // Create ShadowFrame for interpreter.
62 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::AbstractMethod* method, uint32_t dex_pc) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080064 size_t sz = sizeof(ShadowFrame) +
65 (sizeof(uint32_t) * num_vregs) +
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066 (sizeof(mirror::Object*) * num_vregs);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070067 uint8_t* memory = new uint8_t[sz];
TDYa127ce4cc0d2012-11-18 16:59:53 -080068 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
69 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070070 }
71 ~ShadowFrame() {}
72
TDYa127ce4cc0d2012-11-18 16:59:53 -080073 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070074#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080075 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070076#else
77 return true;
78#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070079 }
Elliott Hughes68e76522011-10-05 13:22:16 -070080
TDYa127ce4cc0d2012-11-18 16:59:53 -080081 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070082#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080083 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -070084#else
85 return number_of_vregs_;
86#endif
Ian Rogers0399dde2012-06-06 17:09:28 -070087 }
88
TDYa127ce4cc0d2012-11-18 16:59:53 -080089 void SetNumberOfVRegs(uint32_t number_of_vregs) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -070090#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -080091 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers8a01a3a2013-05-06 13:25:44 -070092#else
93 UNUSED(number_of_vregs);
94 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
95#endif
Ian Rogers5438ad82012-10-15 17:22:44 -070096 }
97
Ian Rogers0399dde2012-06-06 17:09:28 -070098 uint32_t GetDexPC() const {
99 return dex_pc_;
100 }
101
102 void SetDexPC(uint32_t dex_pc) {
103 dex_pc_ = dex_pc;
104 }
105
Ian Rogers0399dde2012-06-06 17:09:28 -0700106 ShadowFrame* GetLink() const {
107 return link_;
108 }
109
110 void SetLink(ShadowFrame* frame) {
111 DCHECK_NE(this, frame);
112 link_ = frame;
113 }
114
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700115 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800116 DCHECK_LT(i, NumberOfVRegs());
117 const uint32_t* vreg = &vregs_[i];
118 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700119 }
120
121 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800122 DCHECK_LT(i, NumberOfVRegs());
123 // NOTE: Strict-aliasing?
124 const uint32_t* vreg = &vregs_[i];
125 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700126 }
127
128 int64_t GetVRegLong(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800129 const uint32_t* vreg = &vregs_[i];
130 return *reinterpret_cast<const int64_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700131 }
132
133 double GetVRegDouble(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800134 const uint32_t* vreg = &vregs_[i];
135 return *reinterpret_cast<const double*>(vreg);
136 }
137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 mirror::Object* GetVRegReference(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800139 DCHECK_LT(i, NumberOfVRegs());
140 if (HasReferenceArray()) {
141 return References()[i];
142 } else {
143 const uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 return *reinterpret_cast<mirror::Object* const*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800145 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700146 }
147
Jeff Hao16743632013-05-08 10:59:04 -0700148 // Get view of vregs as range of consecutive arguments starting at i.
149 uint32_t* GetVRegArgs(size_t i) {
150 return &vregs_[i];
151 }
152
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700153 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800154 DCHECK_LT(i, NumberOfVRegs());
155 uint32_t* vreg = &vregs_[i];
156 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700157 }
158
159 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800160 DCHECK_LT(i, NumberOfVRegs());
161 uint32_t* vreg = &vregs_[i];
162 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700163 }
164
165 void SetVRegLong(size_t i, int64_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800166 uint32_t* vreg = &vregs_[i];
167 *reinterpret_cast<int64_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700168 }
169
170 void SetVRegDouble(size_t i, double val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800171 uint32_t* vreg = &vregs_[i];
172 *reinterpret_cast<double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700173 }
174
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 void SetVRegReference(size_t i, mirror::Object* val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800176 DCHECK_LT(i, NumberOfVRegs());
177 uint32_t* vreg = &vregs_[i];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 *reinterpret_cast<mirror::Object**>(vreg) = val;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800179 if (HasReferenceArray()) {
180 References()[i] = val;
181 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700182 }
183
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700185 DCHECK_NE(method_, static_cast<void*>(NULL));
186 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700187 }
188
Ian Rogers62d6c772013-02-27 08:32:07 -0800189 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
190
191 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
192
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 void SetMethod(mirror::AbstractMethod* method) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700194#if defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers0399dde2012-06-06 17:09:28 -0700195 DCHECK_NE(method, static_cast<void*>(NULL));
196 method_ = method;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700197#else
198 UNUSED(method);
199 UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled";
200#endif
Elliott Hughes68e76522011-10-05 13:22:16 -0700201 }
202
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 bool Contains(mirror::Object** shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800204 if (HasReferenceArray()) {
205 return ((&References()[0] <= shadow_frame_entry_obj) &&
206 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
207 } else {
208 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
209 return ((&vregs_[0] <= shadow_frame_entry) &&
210 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700211 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700212 }
213
Ian Rogers0399dde2012-06-06 17:09:28 -0700214 static size_t LinkOffset() {
215 return OFFSETOF_MEMBER(ShadowFrame, link_);
216 }
217
Ian Rogers0399dde2012-06-06 17:09:28 -0700218 static size_t MethodOffset() {
219 return OFFSETOF_MEMBER(ShadowFrame, method_);
220 }
221
Ian Rogers0399dde2012-06-06 17:09:28 -0700222 static size_t DexPCOffset() {
223 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
224 }
225
Ian Rogers5438ad82012-10-15 17:22:44 -0700226 static size_t NumberOfVRegsOffset() {
227 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
228 }
229
TDYa127ce4cc0d2012-11-18 16:59:53 -0800230 static size_t VRegsOffset() {
231 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700232 }
233
Elliott Hughes68e76522011-10-05 13:22:16 -0700234 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::AbstractMethod* method,
236 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800237 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
238 if (has_reference_array) {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700239#if defined(ART_USE_PORTABLE_COMPILER)
240 CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray));
TDYa127ce4cc0d2012-11-18 16:59:53 -0800241 number_of_vregs_ |= kHasReferenceArray;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700242#endif
TDYa127ce4cc0d2012-11-18 16:59:53 -0800243 for (size_t i = 0; i < num_vregs; ++i) {
244 SetVRegReference(i, NULL);
245 }
Mathieu Chartier67022432012-11-29 18:04:50 -0800246 } else {
247 for (size_t i = 0; i < num_vregs; ++i) {
248 SetVReg(i, 0);
249 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700250 }
251 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700252
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 mirror::Object* const* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800254 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800255 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 return reinterpret_cast<mirror::Object* const*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800257 }
258
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 mirror::Object** References() {
260 return const_cast<mirror::Object**>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800261 }
262
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700263#if defined(ART_USE_PORTABLE_COMPILER)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800264 enum ShadowFrameFlag {
265 kHasReferenceArray = 1ul << 31
266 };
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700267 // TODO: make const in the portable case.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800268 uint32_t number_of_vregs_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700269#else
270 const uint32_t number_of_vregs_;
271#endif
Ian Rogers5438ad82012-10-15 17:22:44 -0700272 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700273 ShadowFrame* link_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700274#if defined(ART_USE_PORTABLE_COMPILER)
275 // TODO: make const in the portable case.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 mirror::AbstractMethod* method_;
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700277#else
278 mirror::AbstractMethod* const method_;
279#endif
Ian Rogers0399dde2012-06-06 17:09:28 -0700280 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800281 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700282
Ian Rogers0399dde2012-06-06 17:09:28 -0700283 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700284};
285
Ian Rogers0399dde2012-06-06 17:09:28 -0700286// The managed stack is used to record fragments of managed code stacks. Managed code stacks
287// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
288// necessary for transitions between code using different frame layouts and transitions into native
289// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800290class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700291 public:
Ian Rogersca190662012-06-26 15:45:57 -0700292 ManagedStack()
293 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700294
295 void PushManagedStackFragment(ManagedStack* fragment) {
296 // Copy this top fragment into given fragment.
297 memcpy(fragment, this, sizeof(ManagedStack));
298 // Clear this fragment, which has become the top.
299 memset(this, 0, sizeof(ManagedStack));
300 // Link our top fragment onto the given fragment.
301 link_ = fragment;
302 }
303
304 void PopManagedStackFragment(const ManagedStack& fragment) {
305 DCHECK(&fragment == link_);
306 // Copy this given fragment back to the top.
307 memcpy(this, &fragment, sizeof(ManagedStack));
308 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700309
310 ManagedStack* GetLink() const {
311 return link_;
312 }
313
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 mirror::AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700315 return top_quick_frame_;
316 }
317
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 void SetTopQuickFrame(mirror::AbstractMethod** top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200319 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700320 top_quick_frame_ = top;
321 }
322
323 uintptr_t GetTopQuickFramePc() const {
324 return top_quick_frame_pc_;
325 }
326
327 void SetTopQuickFramePc(uintptr_t pc) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200328 DCHECK(top_shadow_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700329 top_quick_frame_pc_ = pc;
330 }
331
332 static size_t TopQuickFrameOffset() {
333 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
334 }
335
336 static size_t TopQuickFramePcOffset() {
337 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
338 }
339
340 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200341 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700342 ShadowFrame* old_frame = top_shadow_frame_;
343 top_shadow_frame_ = new_top_frame;
344 new_top_frame->SetLink(old_frame);
345 return old_frame;
346 }
347
348 ShadowFrame* PopShadowFrame() {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200349 DCHECK(top_quick_frame_ == NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700350 CHECK(top_shadow_frame_ != NULL);
351 ShadowFrame* frame = top_shadow_frame_;
352 top_shadow_frame_ = frame->GetLink();
353 return frame;
354 }
355
356 ShadowFrame* GetTopShadowFrame() const {
357 return top_shadow_frame_;
358 }
359
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800360 void SetTopShadowFrame(ShadowFrame* top) {
Sebastien Hertza7b0c422013-04-05 16:19:39 +0200361 DCHECK(top_quick_frame_ == NULL);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800362 top_shadow_frame_ = top;
363 }
364
Ian Rogers0399dde2012-06-06 17:09:28 -0700365 static size_t TopShadowFrameOffset() {
366 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
367 }
368
TDYa127ce4cc0d2012-11-18 16:59:53 -0800369 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700370
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 bool ShadowFramesContain(mirror::Object** shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700372
373 private:
374 ManagedStack* link_;
375 ShadowFrame* top_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 mirror::AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700377 uintptr_t top_quick_frame_pc_;
378};
379
380class StackVisitor {
381 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800382 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700383
384 public:
385 virtual ~StackVisitor() {}
386
387 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700389
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700390 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700391 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700392
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 mirror::AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700394 if (cur_shadow_frame_ != NULL) {
395 return cur_shadow_frame_->GetMethod();
396 } else if (cur_quick_frame_ != NULL) {
397 return *cur_quick_frame_;
398 } else {
399 return NULL;
400 }
401 }
402
403 bool IsShadowFrame() const {
404 return cur_shadow_frame_ != NULL;
405 }
406
Ian Rogers0c7abda2012-09-19 13:33:42 -0700407 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
408
Ian Rogers62d6c772013-02-27 08:32:07 -0800409 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
410
Ian Rogers0c7abda2012-09-19 13:33:42 -0700411 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
412
Mathieu Chartier67022432012-11-29 18:04:50 -0800413 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700414 // Callee saves are held at the top of the frame
Mathieu Chartier67022432012-11-29 18:04:50 -0800415 DCHECK(GetMethod() != NULL);
Ian Rogers0399dde2012-06-06 17:09:28 -0700416 byte* save_addr =
417 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
418#if defined(__i386__)
419 save_addr -= kPointerSize; // account for return address
420#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800421 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700422 }
423
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700424 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700425 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800426 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700427 }
428
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700429 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700430 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700431 return GetFrameHeight() + 1;
432 }
433
Ian Rogersb726dcb2012-09-05 08:57:23 -0700434 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700435 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800436 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700437 }
438 return num_frames_;
439 }
440
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 uint32_t GetVReg(mirror::AbstractMethod* m, uint16_t vreg, VRegKind kind) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800442 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700443
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 void SetVReg(mirror::AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700446
447 uintptr_t GetGPR(uint32_t reg) const;
Mathieu Chartier67022432012-11-29 18:04:50 -0800448 void SetGPR(uint32_t reg, uintptr_t value);
Ian Rogers0399dde2012-06-06 17:09:28 -0700449
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 uint32_t GetVReg(mirror::AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800451 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
452 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700453 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700454 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
455 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700456 return *reinterpret_cast<uint32_t*>(vreg_addr);
457 }
458
459 uintptr_t GetReturnPc() const;
460
461 void SetReturnPc(uintptr_t new_ret_pc);
462
463 /*
464 * Return sp-relative offset for a Dalvik virtual register, compiler
465 * spill or Method* in bytes using Method*.
466 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
467 * denotes Method* and (reg <= -3) denotes a compiler temp.
468 *
469 * +------------------------+
470 * | IN[ins-1] | {Note: resides in caller's frame}
471 * | . |
472 * | IN[0] |
473 * | caller's Method* |
474 * +========================+ {Note: start of callee's frame}
475 * | core callee-save spill | {variable sized}
476 * +------------------------+
477 * | fp callee-save spill |
478 * +------------------------+
479 * | filler word | {For compatibility, if V[locals-1] used as wide
480 * +------------------------+
481 * | V[locals-1] |
482 * | V[locals-2] |
483 * | . |
484 * | . | ... (reg == 2)
485 * | V[1] | ... (reg == 1)
486 * | V[0] | ... (reg == 0) <---- "locals_start"
487 * +------------------------+
488 * | Compiler temps | ... (reg == -2)
489 * | | ... (reg == -3)
490 * | | ... (reg == -4)
491 * +------------------------+
492 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
493 * +------------------------+
494 * | OUT[outs-1] |
495 * | OUT[outs-2] |
496 * | . |
497 * | OUT[0] |
498 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
499 * +========================+
500 */
501 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700502 uint32_t core_spills, uint32_t fp_spills,
503 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700504 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
505 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
506 int num_ins = code_item->ins_size_;
507 int num_regs = code_item->registers_size_ - num_ins;
508 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
509 if (reg == -2) {
510 return 0; // Method*
511 } else if (reg <= -3) {
512 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
513 } else if (reg < num_regs) {
514 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
515 } else {
516 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
517 }
518 }
519
520 uintptr_t GetCurrentQuickFramePc() const {
521 return cur_quick_frame_pc_;
522 }
523
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524 mirror::AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700525 return cur_quick_frame_;
526 }
527
528 ShadowFrame* GetCurrentShadowFrame() const {
529 return cur_shadow_frame_;
530 }
531
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700532 StackIndirectReferenceTable* GetCurrentSirt() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 mirror::AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700534 ++sp; // Skip Method*; SIRT comes next;
535 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
536 }
537
Ian Rogers40e3bac2012-11-20 00:09:14 -0800538 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
539
Ian Rogers7a22fa62013-01-23 12:16:16 -0800540 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800541
Ian Rogers7a22fa62013-01-23 12:16:16 -0800542 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800543
Ian Rogers0399dde2012-06-06 17:09:28 -0700544 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700545
Ian Rogers62d6c772013-02-27 08:32:07 -0800546 instrumentation::InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700547
Ian Rogersb726dcb2012-09-05 08:57:23 -0700548 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700549
Ian Rogers7a22fa62013-01-23 12:16:16 -0800550 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700551 ShadowFrame* cur_shadow_frame_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 mirror::AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700553 uintptr_t cur_quick_frame_pc_;
554 // Lazily computed, number of frames in the stack.
555 size_t num_frames_;
556 // Depth of the frame we're currently at.
557 size_t cur_depth_;
558 protected:
559 Context* const context_;
560};
561
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800562class VmapTable {
563 public:
564 explicit VmapTable(const uint16_t* table) : table_(table) {
565 }
566
567 uint16_t operator[](size_t i) const {
568 return table_[i + 1];
569 }
570
571 size_t size() const {
572 return table_[0];
573 }
574
575 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
576 // 'kind' is unknown or constant.
577 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
578 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
579 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
580 kind == kDoubleHiVReg || kind == kImpreciseConstant);
581 vmap_offset = 0xEBAD0FF5;
582 // TODO: take advantage of the registers being ordered
583 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
584 // are never promoted to floating point registers.
585 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
586 bool in_floats = false;
587 for (size_t i = 0; i < size(); ++i) {
588 // Stop if we find what we are are looking for.
589 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
590 vmap_offset = i;
591 return true;
592 }
593 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
594 if (table_[i + 1] == 0xffff) {
595 in_floats = true;
596 }
597 }
598 return false;
599 }
600
601 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
602 // by IsInContext above). If the kind is floating point then the result will be a floating point
603 // register number, otherwise it will be an integer register number.
604 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
605 // Compute the register we need to load from the context.
606 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
607 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
608 kind == kDoubleHiVReg || kind == kImpreciseConstant);
609 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
610 // are never promoted to floating point registers.
611 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
612 uint32_t matches = 0;
613 if (is_float) {
614 while (table_[matches] != 0xffff) {
615 matches++;
616 }
617 }
618 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
619 uint32_t spill_shifts = 0;
620 while (matches != (vmap_offset + 1)) {
621 DCHECK_NE(spill_mask, 0u);
622 matches += spill_mask & 1; // Add 1 if the low bit is set
623 spill_mask >>= 1;
624 spill_shifts++;
625 }
626 spill_shifts--; // wind back one as we want the last match
627 return spill_shifts;
628 }
629 private:
630 const uint16_t* table_;
631};
632
Elliott Hughes68e76522011-10-05 13:22:16 -0700633} // namespace art
634
635#endif // ART_SRC_STACK_H_