blob: ce84807098176fbee77a45e610c0ce4477d00cc5 [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"
Ian Rogers0399dde2012-06-06 17:09:28 -070021#include "heap.h"
jeffhao725a9572012-11-13 18:20:12 -080022#include "instrumentation.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070023#include "jni.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070024#include "macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070025#include "oat/runtime/context.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070026
27#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080028#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070029
30namespace art {
31
Mathieu Chartier66f19252012-09-18 08:57:04 -070032class AbstractMethod;
Ian Rogers2bcb4a42012-11-08 10:39:18 -080033class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070034class Object;
35class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070036class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070038class Thread;
39
Ian Rogers2bcb4a42012-11-08 10:39:18 -080040// The kind of vreg being accessed in calls to Set/GetVReg.
41enum VRegKind {
42 kReferenceVReg,
43 kIntVReg,
44 kFloatVReg,
45 kLongLoVReg,
46 kLongHiVReg,
47 kDoubleLoVReg,
48 kDoubleHiVReg,
49 kConstant,
50 kImpreciseConstant,
51 kUndefined,
52};
53
TDYa127ce4cc0d2012-11-18 16:59:53 -080054// ShadowFrame has 3 possible layouts: portable (VRegs & references overlap),
55// interpreter (VRegs and separate references array), JNI (just VRegs, but where
56// VRegs really => references).
Ian Rogers0399dde2012-06-06 17:09:28 -070057class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070058 public:
TDYa127ce4cc0d2012-11-18 16:59:53 -080059 // Create ShadowFrame for interpreter.
60 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070061 AbstractMethod* method, uint32_t dex_pc) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080062 size_t sz = sizeof(ShadowFrame) +
63 (sizeof(uint32_t) * num_vregs) +
64 (sizeof(Object*) * num_vregs);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070065 uint8_t* memory = new uint8_t[sz];
TDYa127ce4cc0d2012-11-18 16:59:53 -080066 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
67 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070068 }
69 ~ShadowFrame() {}
70
TDYa127ce4cc0d2012-11-18 16:59:53 -080071 bool HasReferenceArray() const {
72 return (number_of_vregs_ & kHasReferenceArray) != 0;
Ian Rogers0399dde2012-06-06 17:09:28 -070073 }
Elliott Hughes68e76522011-10-05 13:22:16 -070074
TDYa127ce4cc0d2012-11-18 16:59:53 -080075 uint32_t NumberOfVRegs() const {
76 return number_of_vregs_ & ~kHasReferenceArray;
Ian Rogers0399dde2012-06-06 17:09:28 -070077 }
78
TDYa127ce4cc0d2012-11-18 16:59:53 -080079 void SetNumberOfVRegs(uint32_t number_of_vregs) {
80 DCHECK(number_of_vregs < kHasReferenceArray);
81 number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray);
Ian Rogers5438ad82012-10-15 17:22:44 -070082 }
83
Ian Rogers0399dde2012-06-06 17:09:28 -070084 uint32_t GetDexPC() const {
85 return dex_pc_;
86 }
87
88 void SetDexPC(uint32_t dex_pc) {
89 dex_pc_ = dex_pc;
90 }
91
Ian Rogers0399dde2012-06-06 17:09:28 -070092 ShadowFrame* GetLink() const {
93 return link_;
94 }
95
96 void SetLink(ShadowFrame* frame) {
97 DCHECK_NE(this, frame);
98 link_ = frame;
99 }
100
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700101 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800102 DCHECK_LT(i, NumberOfVRegs());
103 const uint32_t* vreg = &vregs_[i];
104 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700105 }
106
107 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800108 DCHECK_LT(i, NumberOfVRegs());
109 // NOTE: Strict-aliasing?
110 const uint32_t* vreg = &vregs_[i];
111 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700112 }
113
114 int64_t GetVRegLong(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800115 const uint32_t* vreg = &vregs_[i];
116 return *reinterpret_cast<const int64_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700117 }
118
119 double GetVRegDouble(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800120 const uint32_t* vreg = &vregs_[i];
121 return *reinterpret_cast<const double*>(vreg);
122 }
123
124 Object* GetVRegReference(size_t i) const {
125 DCHECK_LT(i, NumberOfVRegs());
126 if (HasReferenceArray()) {
127 return References()[i];
128 } else {
129 const uint32_t* vreg = &vregs_[i];
130 return *reinterpret_cast<Object* const*>(vreg);
131 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700132 }
133
134 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800135 DCHECK_LT(i, NumberOfVRegs());
136 uint32_t* vreg = &vregs_[i];
137 *reinterpret_cast<int32_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700138 }
139
140 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800141 DCHECK_LT(i, NumberOfVRegs());
142 uint32_t* vreg = &vregs_[i];
143 *reinterpret_cast<float*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700144 }
145
146 void SetVRegLong(size_t i, int64_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800147 uint32_t* vreg = &vregs_[i];
148 *reinterpret_cast<int64_t*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700149 }
150
151 void SetVRegDouble(size_t i, double val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800152 uint32_t* vreg = &vregs_[i];
153 *reinterpret_cast<double*>(vreg) = val;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700154 }
155
TDYa127ce4cc0d2012-11-18 16:59:53 -0800156 void SetVRegReference(size_t i, Object* val) {
157 DCHECK_LT(i, NumberOfVRegs());
158 uint32_t* vreg = &vregs_[i];
159 *reinterpret_cast<Object**>(vreg) = val;
160 if (HasReferenceArray()) {
161 References()[i] = val;
162 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700163 }
164
Mathieu Chartier66f19252012-09-18 08:57:04 -0700165 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700166 DCHECK_NE(method_, static_cast<void*>(NULL));
167 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700168 }
169
Mathieu Chartier66f19252012-09-18 08:57:04 -0700170 void SetMethod(AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700171 DCHECK_NE(method, static_cast<void*>(NULL));
172 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -0700173 }
174
TDYa127ce4cc0d2012-11-18 16:59:53 -0800175 bool Contains(Object** shadow_frame_entry_obj) const {
176 if (HasReferenceArray()) {
177 return ((&References()[0] <= shadow_frame_entry_obj) &&
178 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
179 } else {
180 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
181 return ((&vregs_[0] <= shadow_frame_entry) &&
182 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700183 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700184 }
185
Ian Rogers0399dde2012-06-06 17:09:28 -0700186 static size_t LinkOffset() {
187 return OFFSETOF_MEMBER(ShadowFrame, link_);
188 }
189
Ian Rogers0399dde2012-06-06 17:09:28 -0700190 static size_t MethodOffset() {
191 return OFFSETOF_MEMBER(ShadowFrame, method_);
192 }
193
Ian Rogers0399dde2012-06-06 17:09:28 -0700194 static size_t DexPCOffset() {
195 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
196 }
197
Ian Rogers5438ad82012-10-15 17:22:44 -0700198 static size_t NumberOfVRegsOffset() {
199 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
200 }
201
TDYa127ce4cc0d2012-11-18 16:59:53 -0800202 static size_t VRegsOffset() {
203 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700204 }
205
Elliott Hughes68e76522011-10-05 13:22:16 -0700206 private:
TDYa127ce4cc0d2012-11-18 16:59:53 -0800207 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, AbstractMethod* method, uint32_t dex_pc,
208 bool has_reference_array)
209 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
210 if (has_reference_array) {
211 number_of_vregs_ |= kHasReferenceArray;
212 for (size_t i = 0; i < num_vregs; ++i) {
213 SetVRegReference(i, NULL);
214 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700215 }
216 for (size_t i = 0; i < num_vregs; ++i) {
217 SetVReg(i, 0);
218 }
219 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700220
TDYa127ce4cc0d2012-11-18 16:59:53 -0800221 Object* const* References() const {
222 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
223 return reinterpret_cast<Object* const*>(vreg_end);
224 }
225
226 Object** References() {
227 return const_cast<Object**>(const_cast<const ShadowFrame*>(this)->References());
228 }
229
230 enum ShadowFrameFlag {
231 kHasReferenceArray = 1ul << 31
232 };
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700233 // TODO: make the majority of these fields const.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800234 uint32_t number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700235 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700236 ShadowFrame* link_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700237 AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700238 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800239 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700240
Ian Rogers0399dde2012-06-06 17:09:28 -0700241 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700242};
243
Ian Rogers0399dde2012-06-06 17:09:28 -0700244// The managed stack is used to record fragments of managed code stacks. Managed code stacks
245// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
246// necessary for transitions between code using different frame layouts and transitions into native
247// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800248class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700249 public:
Ian Rogersca190662012-06-26 15:45:57 -0700250 ManagedStack()
251 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700252
253 void PushManagedStackFragment(ManagedStack* fragment) {
254 // Copy this top fragment into given fragment.
255 memcpy(fragment, this, sizeof(ManagedStack));
256 // Clear this fragment, which has become the top.
257 memset(this, 0, sizeof(ManagedStack));
258 // Link our top fragment onto the given fragment.
259 link_ = fragment;
260 }
261
262 void PopManagedStackFragment(const ManagedStack& fragment) {
263 DCHECK(&fragment == link_);
264 // Copy this given fragment back to the top.
265 memcpy(this, &fragment, sizeof(ManagedStack));
266 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700267
268 ManagedStack* GetLink() const {
269 return link_;
270 }
271
Mathieu Chartier66f19252012-09-18 08:57:04 -0700272 AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700273 return top_quick_frame_;
274 }
275
Mathieu Chartier66f19252012-09-18 08:57:04 -0700276 void SetTopQuickFrame(AbstractMethod** top) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700277 top_quick_frame_ = top;
278 }
279
280 uintptr_t GetTopQuickFramePc() const {
281 return top_quick_frame_pc_;
282 }
283
284 void SetTopQuickFramePc(uintptr_t pc) {
285 top_quick_frame_pc_ = pc;
286 }
287
288 static size_t TopQuickFrameOffset() {
289 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
290 }
291
292 static size_t TopQuickFramePcOffset() {
293 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
294 }
295
296 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
297 ShadowFrame* old_frame = top_shadow_frame_;
298 top_shadow_frame_ = new_top_frame;
299 new_top_frame->SetLink(old_frame);
300 return old_frame;
301 }
302
303 ShadowFrame* PopShadowFrame() {
304 CHECK(top_shadow_frame_ != NULL);
305 ShadowFrame* frame = top_shadow_frame_;
306 top_shadow_frame_ = frame->GetLink();
307 return frame;
308 }
309
310 ShadowFrame* GetTopShadowFrame() const {
311 return top_shadow_frame_;
312 }
313
314 static size_t TopShadowFrameOffset() {
315 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
316 }
317
TDYa127ce4cc0d2012-11-18 16:59:53 -0800318 size_t NumJniShadowFrameReferences() const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700319
320 bool ShadowFramesContain(Object** shadow_frame_entry) const;
321
322 private:
323 ManagedStack* link_;
324 ShadowFrame* top_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700325 AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700326 uintptr_t top_quick_frame_pc_;
327};
328
329class StackVisitor {
330 protected:
jeffhao725a9572012-11-13 18:20:12 -0800331 StackVisitor(const ManagedStack* stack, const std::vector<InstrumentationStackFrame>* instrumentation_stack,
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700332 Context* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
jeffhao725a9572012-11-13 18:20:12 -0800334 : stack_start_(stack), instrumentation_stack_(instrumentation_stack), cur_shadow_frame_(NULL),
Ian Rogersca190662012-06-26 15:45:57 -0700335 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
336 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700337
338 public:
339 virtual ~StackVisitor() {}
340
341 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700342 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700343
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700346
Mathieu Chartier66f19252012-09-18 08:57:04 -0700347 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700348 if (cur_shadow_frame_ != NULL) {
349 return cur_shadow_frame_->GetMethod();
350 } else if (cur_quick_frame_ != NULL) {
351 return *cur_quick_frame_;
352 } else {
353 return NULL;
354 }
355 }
356
357 bool IsShadowFrame() const {
358 return cur_shadow_frame_ != NULL;
359 }
360
Ian Rogers0c7abda2012-09-19 13:33:42 -0700361 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
362
363 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
364
Ian Rogers0399dde2012-06-06 17:09:28 -0700365 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
366 // Callee saves are held at the top of the frame
Mathieu Chartier66f19252012-09-18 08:57:04 -0700367 AbstractMethod* method = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -0700368 DCHECK(method != NULL);
369 byte* save_addr =
370 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
371#if defined(__i386__)
372 save_addr -= kPointerSize; // account for return address
373#endif
374 return *reinterpret_cast<uintptr_t*>(save_addr);
375 }
376
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700377 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700379 return GetNumFrames() - cur_depth_;
380 }
381
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700382 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700383 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700384 return GetFrameHeight() + 1;
385 }
386
Ian Rogersb726dcb2012-09-05 08:57:23 -0700387 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700388 if (num_frames_ == 0) {
389 num_frames_ = ComputeNumFrames();
390 }
391 return num_frames_;
392 }
393
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800394 uint32_t GetVReg(AbstractMethod* m, uint16_t vreg, VRegKind kind) const
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700396
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800397 void SetVReg(AbstractMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700398 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700399
400 uintptr_t GetGPR(uint32_t reg) const;
401
Mathieu Chartier66f19252012-09-18 08:57:04 -0700402 uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800403 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
404 uint16_t vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700405 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700406 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
407 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700408 return *reinterpret_cast<uint32_t*>(vreg_addr);
409 }
410
411 uintptr_t GetReturnPc() const;
412
413 void SetReturnPc(uintptr_t new_ret_pc);
414
415 /*
416 * Return sp-relative offset for a Dalvik virtual register, compiler
417 * spill or Method* in bytes using Method*.
418 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
419 * denotes Method* and (reg <= -3) denotes a compiler temp.
420 *
421 * +------------------------+
422 * | IN[ins-1] | {Note: resides in caller's frame}
423 * | . |
424 * | IN[0] |
425 * | caller's Method* |
426 * +========================+ {Note: start of callee's frame}
427 * | core callee-save spill | {variable sized}
428 * +------------------------+
429 * | fp callee-save spill |
430 * +------------------------+
431 * | filler word | {For compatibility, if V[locals-1] used as wide
432 * +------------------------+
433 * | V[locals-1] |
434 * | V[locals-2] |
435 * | . |
436 * | . | ... (reg == 2)
437 * | V[1] | ... (reg == 1)
438 * | V[0] | ... (reg == 0) <---- "locals_start"
439 * +------------------------+
440 * | Compiler temps | ... (reg == -2)
441 * | | ... (reg == -3)
442 * | | ... (reg == -4)
443 * +------------------------+
444 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
445 * +------------------------+
446 * | OUT[outs-1] |
447 * | OUT[outs-2] |
448 * | . |
449 * | OUT[0] |
450 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
451 * +========================+
452 */
453 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700454 uint32_t core_spills, uint32_t fp_spills,
455 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700456 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
457 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
458 int num_ins = code_item->ins_size_;
459 int num_regs = code_item->registers_size_ - num_ins;
460 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
461 if (reg == -2) {
462 return 0; // Method*
463 } else if (reg <= -3) {
464 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
465 } else if (reg < num_regs) {
466 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
467 } else {
468 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
469 }
470 }
471
472 uintptr_t GetCurrentQuickFramePc() const {
473 return cur_quick_frame_pc_;
474 }
475
Mathieu Chartier66f19252012-09-18 08:57:04 -0700476 AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700477 return cur_quick_frame_;
478 }
479
480 ShadowFrame* GetCurrentShadowFrame() const {
481 return cur_shadow_frame_;
482 }
483
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700484 StackIndirectReferenceTable* GetCurrentSirt() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700485 AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700486 ++sp; // Skip Method*; SIRT comes next;
487 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
488 }
489
Ian Rogers40e3bac2012-11-20 00:09:14 -0800490 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
491
Ian Rogers0399dde2012-06-06 17:09:28 -0700492 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700493 size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700494
jeffhao725a9572012-11-13 18:20:12 -0800495 InstrumentationStackFrame GetInstrumentationStackFrame(uint32_t depth) const {
496 return instrumentation_stack_->at(instrumentation_stack_->size() - depth - 1);
Ian Rogers0399dde2012-06-06 17:09:28 -0700497 }
498
Ian Rogersb726dcb2012-09-05 08:57:23 -0700499 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700500
501 const ManagedStack* const stack_start_;
jeffhao725a9572012-11-13 18:20:12 -0800502 const std::vector<InstrumentationStackFrame>* const instrumentation_stack_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700503 ShadowFrame* cur_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700504 AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700505 uintptr_t cur_quick_frame_pc_;
506 // Lazily computed, number of frames in the stack.
507 size_t num_frames_;
508 // Depth of the frame we're currently at.
509 size_t cur_depth_;
510 protected:
511 Context* const context_;
512};
513
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800514class VmapTable {
515 public:
516 explicit VmapTable(const uint16_t* table) : table_(table) {
517 }
518
519 uint16_t operator[](size_t i) const {
520 return table_[i + 1];
521 }
522
523 size_t size() const {
524 return table_[0];
525 }
526
527 // Is the dex register 'vreg' in the context or on the stack? Should not be called when the
528 // 'kind' is unknown or constant.
529 bool IsInContext(size_t vreg, uint32_t& vmap_offset, VRegKind kind) const {
530 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
531 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
532 kind == kDoubleHiVReg || kind == kImpreciseConstant);
533 vmap_offset = 0xEBAD0FF5;
534 // TODO: take advantage of the registers being ordered
535 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
536 // are never promoted to floating point registers.
537 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
538 bool in_floats = false;
539 for (size_t i = 0; i < size(); ++i) {
540 // Stop if we find what we are are looking for.
541 if ((table_[i + 1] == vreg) && (in_floats == is_float)) {
542 vmap_offset = i;
543 return true;
544 }
545 // 0xffff is the marker for LR (return PC on x86), following it are spilled float registers.
546 if (table_[i + 1] == 0xffff) {
547 in_floats = true;
548 }
549 }
550 return false;
551 }
552
553 // Compute the register number that corresponds to the entry in the vmap (vmap_offset, computed
554 // by IsInContext above). If the kind is floating point then the result will be a floating point
555 // register number, otherwise it will be an integer register number.
556 uint32_t ComputeRegister(uint32_t spill_mask, uint32_t vmap_offset, VRegKind kind) const {
557 // Compute the register we need to load from the context.
558 DCHECK(kind == kReferenceVReg || kind == kIntVReg || kind == kFloatVReg ||
559 kind == kLongLoVReg || kind == kLongHiVReg || kind == kDoubleLoVReg ||
560 kind == kDoubleHiVReg || kind == kImpreciseConstant);
561 // TODO: we treat kImpreciseConstant as an integer below, need to ensure that such values
562 // are never promoted to floating point registers.
563 bool is_float = (kind == kFloatVReg) || (kind == kDoubleLoVReg) || (kind == kDoubleHiVReg);
564 uint32_t matches = 0;
565 if (is_float) {
566 while (table_[matches] != 0xffff) {
567 matches++;
568 }
569 }
570 CHECK_LT(vmap_offset - matches, static_cast<uint32_t>(__builtin_popcount(spill_mask)));
571 uint32_t spill_shifts = 0;
572 while (matches != (vmap_offset + 1)) {
573 DCHECK_NE(spill_mask, 0u);
574 matches += spill_mask & 1; // Add 1 if the low bit is set
575 spill_mask >>= 1;
576 spill_shifts++;
577 }
578 spill_shifts--; // wind back one as we want the last match
579 return spill_shifts;
580 }
581 private:
582 const uint16_t* table_;
583};
584
Elliott Hughes68e76522011-10-05 13:22:16 -0700585} // namespace art
586
587#endif // ART_SRC_STACK_H_