Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_STACK_H_ |
| 18 | #define ART_RUNTIME_STACK_H_ |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 20 | #include "dex_file.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 21 | #include "instrumentation.h" |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 22 | #include "arch/context.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 23 | #include "base/casts.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "base/macros.h" |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 25 | #include "instruction_set.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 26 | #include "mirror/object.h" |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 27 | #include "mirror/object_reference.h" |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 28 | #include "utils.h" |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 29 | #include "verify_object.h" |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 30 | |
| 31 | #include <stdint.h> |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 32 | #include <string> |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | namespace mirror { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 37 | class ArtMethod; |
| 38 | class Object; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 39 | } // namespace mirror |
| 40 | |
| 41 | class Context; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 42 | class ShadowFrame; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 43 | class HandleScope; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | class ScopedObjectAccess; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 45 | class Thread; |
| 46 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 47 | // The kind of vreg being accessed in calls to Set/GetVReg. |
| 48 | enum VRegKind { |
| 49 | kReferenceVReg, |
| 50 | kIntVReg, |
| 51 | kFloatVReg, |
| 52 | kLongLoVReg, |
| 53 | kLongHiVReg, |
| 54 | kDoubleLoVReg, |
| 55 | kDoubleHiVReg, |
| 56 | kConstant, |
| 57 | kImpreciseConstant, |
| 58 | kUndefined, |
| 59 | }; |
| 60 | |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 61 | /** |
| 62 | * @brief Represents the virtual register numbers that denote special meaning. |
| 63 | * @details This is used to make some virtual register numbers to have specific |
| 64 | * semantic meaning. This is done so that the compiler can treat all virtual |
| 65 | * registers the same way and only special case when needed. For example, |
| 66 | * calculating SSA does not care whether a virtual register is a normal one or |
| 67 | * a compiler temporary, so it can deal with them in a consistent manner. But, |
| 68 | * for example if backend cares about temporaries because it has custom spill |
| 69 | * location, then it can special case them only then. |
| 70 | */ |
| 71 | enum VRegBaseRegNum : int { |
| 72 | /** |
| 73 | * @brief Virtual registers originating from dex have number >= 0. |
| 74 | */ |
| 75 | kVRegBaseReg = 0, |
| 76 | |
| 77 | /** |
| 78 | * @brief Invalid virtual register number. |
| 79 | */ |
| 80 | kVRegInvalid = -1, |
| 81 | |
| 82 | /** |
| 83 | * @brief Used to denote the base register for compiler temporaries. |
| 84 | * @details Compiler temporaries are virtual registers not originating |
| 85 | * from dex but that are created by compiler. All virtual register numbers |
| 86 | * that are <= kVRegTempBaseReg are categorized as compiler temporaries. |
| 87 | */ |
| 88 | kVRegTempBaseReg = -2, |
| 89 | |
| 90 | /** |
| 91 | * @brief Base register of temporary that holds the method pointer. |
| 92 | * @details This is a special compiler temporary because it has a specific |
| 93 | * location on stack. |
| 94 | */ |
| 95 | kVRegMethodPtrBaseReg = kVRegTempBaseReg, |
| 96 | |
| 97 | /** |
| 98 | * @brief Base register of non-special compiler temporary. |
| 99 | * @details A non-special compiler temporary is one whose spill location |
| 100 | * is flexible. |
| 101 | */ |
| 102 | kVRegNonSpecialTempBaseReg = -3, |
| 103 | }; |
| 104 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 105 | // A reference from the shadow stack to a MirrorType object within the Java heap. |
| 106 | template<class MirrorType> |
| 107 | class MANAGED StackReference : public mirror::ObjectReference<false, MirrorType> { |
| 108 | public: |
| 109 | StackReference<MirrorType>() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 110 | : mirror::ObjectReference<false, MirrorType>(nullptr) {} |
| 111 | |
| 112 | static StackReference<MirrorType> FromMirrorPtr(MirrorType* p) |
| 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 114 | return StackReference<MirrorType>(p); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | StackReference<MirrorType>(MirrorType* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 119 | : mirror::ObjectReference<false, MirrorType>(p) {} |
| 120 | }; |
| 121 | |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 122 | // ShadowFrame has 3 possible layouts: |
| 123 | // - portable - a unified array of VRegs and references. Precise references need GC maps. |
| 124 | // - interpreter - separate VRegs and reference arrays. References are in the reference array. |
| 125 | // - JNI - just VRegs, but where every VReg holds a reference. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 126 | class ShadowFrame { |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 127 | public: |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 128 | // Compute size of ShadowFrame in bytes assuming it has a reference array. |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 129 | static size_t ComputeSize(uint32_t num_vregs) { |
| 130 | return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) + |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 131 | (sizeof(StackReference<mirror::Object>) * num_vregs); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Create ShadowFrame in heap for deoptimization. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 135 | static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 136 | mirror::ArtMethod* method, uint32_t dex_pc) { |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 137 | uint8_t* memory = new uint8_t[ComputeSize(num_vregs)]; |
Sebastien Hertz | c61124b | 2013-09-10 11:44:19 +0200 | [diff] [blame] | 138 | return Create(num_vregs, link, method, dex_pc, memory); |
Jeff Hao | 6613519 | 2013-05-14 11:02:41 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Create ShadowFrame for interpreter using provided memory. |
| 142 | static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 143 | mirror::ArtMethod* method, uint32_t dex_pc, void* memory) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 144 | ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true); |
| 145 | return sf; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 146 | } |
| 147 | ~ShadowFrame() {} |
| 148 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 149 | bool HasReferenceArray() const { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 150 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 151 | return (number_of_vregs_ & kHasReferenceArray) != 0; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 152 | #else |
| 153 | return true; |
| 154 | #endif |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 155 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 156 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 157 | uint32_t NumberOfVRegs() const { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 158 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 159 | return number_of_vregs_ & ~kHasReferenceArray; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 160 | #else |
| 161 | return number_of_vregs_; |
| 162 | #endif |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 163 | } |
| 164 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 165 | void SetNumberOfVRegs(uint32_t number_of_vregs) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 166 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 167 | number_of_vregs_ = number_of_vregs | (number_of_vregs_ & kHasReferenceArray); |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 168 | #else |
| 169 | UNUSED(number_of_vregs); |
| 170 | UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled"; |
| 171 | #endif |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 174 | uint32_t GetDexPC() const { |
| 175 | return dex_pc_; |
| 176 | } |
| 177 | |
| 178 | void SetDexPC(uint32_t dex_pc) { |
| 179 | dex_pc_ = dex_pc; |
| 180 | } |
| 181 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 182 | ShadowFrame* GetLink() const { |
| 183 | return link_; |
| 184 | } |
| 185 | |
| 186 | void SetLink(ShadowFrame* frame) { |
| 187 | DCHECK_NE(this, frame); |
| 188 | link_ = frame; |
| 189 | } |
| 190 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 191 | int32_t GetVReg(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 192 | DCHECK_LT(i, NumberOfVRegs()); |
| 193 | const uint32_t* vreg = &vregs_[i]; |
| 194 | return *reinterpret_cast<const int32_t*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | float GetVRegFloat(size_t i) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 198 | DCHECK_LT(i, NumberOfVRegs()); |
| 199 | // NOTE: Strict-aliasing? |
| 200 | const uint32_t* vreg = &vregs_[i]; |
| 201 | return *reinterpret_cast<const float*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int64_t GetVRegLong(size_t i) const { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 205 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 206 | const uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 207 | // Alignment attribute required for GCC 4.8 |
| 208 | typedef const int64_t unaligned_int64 __attribute__ ((aligned (4))); |
| 209 | return *reinterpret_cast<unaligned_int64*>(vreg); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | double GetVRegDouble(size_t i) const { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 213 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 214 | const uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 215 | // Alignment attribute required for GCC 4.8 |
| 216 | typedef const double unaligned_double __attribute__ ((aligned (4))); |
| 217 | return *reinterpret_cast<unaligned_double*>(vreg); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 220 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 221 | mirror::Object* GetVRegReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 222 | DCHECK_LT(i, NumberOfVRegs()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 223 | mirror::Object* ref; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 224 | if (HasReferenceArray()) { |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 225 | ref = References()[i].AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 226 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 227 | const uint32_t* vreg_ptr = &vregs_[i]; |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 228 | ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr(); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 229 | } |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 230 | if (kVerifyFlags & kVerifyReads) { |
| 231 | VerifyObject(ref); |
| 232 | } |
| 233 | return ref; |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 236 | // Get view of vregs as range of consecutive arguments starting at i. |
| 237 | uint32_t* GetVRegArgs(size_t i) { |
| 238 | return &vregs_[i]; |
| 239 | } |
| 240 | |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 241 | void SetVReg(size_t i, int32_t val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 242 | DCHECK_LT(i, NumberOfVRegs()); |
| 243 | uint32_t* vreg = &vregs_[i]; |
| 244 | *reinterpret_cast<int32_t*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 245 | // This is needed for moving collectors since these can update the vreg references if they |
| 246 | // happen to agree with references in the reference array. |
| 247 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 248 | References()[i].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 249 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void SetVRegFloat(size_t i, float val) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 253 | DCHECK_LT(i, NumberOfVRegs()); |
| 254 | uint32_t* vreg = &vregs_[i]; |
| 255 | *reinterpret_cast<float*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 256 | // This is needed for moving collectors since these can update the vreg references if they |
| 257 | // happen to agree with references in the reference array. |
| 258 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 259 | References()[i].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 260 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void SetVRegLong(size_t i, int64_t val) { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 264 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 265 | uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 266 | // Alignment attribute required for GCC 4.8 |
| 267 | typedef int64_t unaligned_int64 __attribute__ ((aligned (4))); |
| 268 | *reinterpret_cast<unaligned_int64*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 269 | // This is needed for moving collectors since these can update the vreg references if they |
| 270 | // happen to agree with references in the reference array. |
| 271 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 272 | References()[i].Clear(); |
| 273 | References()[i + 1].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 274 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void SetVRegDouble(size_t i, double val) { |
Sebastien Hertz | 807a256 | 2013-04-15 09:33:39 +0200 | [diff] [blame] | 278 | DCHECK_LT(i, NumberOfVRegs()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 279 | uint32_t* vreg = &vregs_[i]; |
Jeff Hao | e47637c | 2013-09-19 15:13:16 -0700 | [diff] [blame] | 280 | // Alignment attribute required for GCC 4.8 |
| 281 | typedef double unaligned_double __attribute__ ((aligned (4))); |
| 282 | *reinterpret_cast<unaligned_double*>(vreg) = val; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 283 | // This is needed for moving collectors since these can update the vreg references if they |
| 284 | // happen to agree with references in the reference array. |
| 285 | if (kMovingCollector && HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 286 | References()[i].Clear(); |
| 287 | References()[i + 1].Clear(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 288 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 291 | template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 292 | void SetVRegReference(size_t i, mirror::Object* val) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 293 | DCHECK_LT(i, NumberOfVRegs()); |
Mathieu Chartier | 4e30541 | 2014-02-19 10:54:44 -0800 | [diff] [blame] | 294 | if (kVerifyFlags & kVerifyWrites) { |
| 295 | VerifyObject(val); |
| 296 | } |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 297 | uint32_t* vreg = &vregs_[i]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 298 | reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 299 | if (HasReferenceArray()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 300 | References()[i].Assign(val); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 301 | } |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 304 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 305 | DCHECK(method_ != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 306 | return method_; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Hiroshi Yamauchi | 92d1a66 | 2014-05-15 21:43:59 -0700 | [diff] [blame] | 309 | mirror::ArtMethod** GetMethodAddress() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 310 | DCHECK(method_ != nullptr); |
| 311 | return &method_; |
| 312 | } |
| 313 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 314 | mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 315 | |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 316 | mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 317 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 318 | ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 319 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 320 | void SetMethod(mirror::ArtMethod* method) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 321 | #if defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 322 | DCHECK(method != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 323 | method_ = method; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 324 | #else |
| 325 | UNUSED(method); |
| 326 | UNIMPLEMENTED(FATAL) << "Should only be called when portable is enabled"; |
| 327 | #endif |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 330 | bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const { |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 331 | if (HasReferenceArray()) { |
| 332 | return ((&References()[0] <= shadow_frame_entry_obj) && |
| 333 | (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1]))); |
| 334 | } else { |
| 335 | uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj); |
| 336 | return ((&vregs_[0] <= shadow_frame_entry) && |
| 337 | (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1]))); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 338 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 341 | static size_t LinkOffset() { |
| 342 | return OFFSETOF_MEMBER(ShadowFrame, link_); |
| 343 | } |
| 344 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 345 | static size_t MethodOffset() { |
| 346 | return OFFSETOF_MEMBER(ShadowFrame, method_); |
| 347 | } |
| 348 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 349 | static size_t DexPCOffset() { |
| 350 | return OFFSETOF_MEMBER(ShadowFrame, dex_pc_); |
| 351 | } |
| 352 | |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 353 | static size_t NumberOfVRegsOffset() { |
| 354 | return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_); |
| 355 | } |
| 356 | |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 357 | static size_t VRegsOffset() { |
| 358 | return OFFSETOF_MEMBER(ShadowFrame, vregs_); |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 361 | private: |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 362 | ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 363 | uint32_t dex_pc, bool has_reference_array) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 364 | : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) { |
| 365 | if (has_reference_array) { |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 366 | #if defined(ART_USE_PORTABLE_COMPILER) |
| 367 | CHECK_LT(num_vregs, static_cast<uint32_t>(kHasReferenceArray)); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 368 | number_of_vregs_ |= kHasReferenceArray; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 369 | #endif |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 370 | memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>))); |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 371 | } else { |
Jeff Hao | e701f48 | 2013-05-24 11:50:49 -0700 | [diff] [blame] | 372 | memset(vregs_, 0, num_vregs * sizeof(uint32_t)); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 375 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 376 | const StackReference<mirror::Object>* References() const { |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 377 | DCHECK(HasReferenceArray()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 378 | const uint32_t* vreg_end = &vregs_[NumberOfVRegs()]; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 379 | return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 382 | StackReference<mirror::Object>* References() { |
| 383 | return const_cast<StackReference<mirror::Object>*>(const_cast<const ShadowFrame*>(this)->References()); |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 386 | #if defined(ART_USE_PORTABLE_COMPILER) |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 387 | enum ShadowFrameFlag { |
| 388 | kHasReferenceArray = 1ul << 31 |
| 389 | }; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 390 | // TODO: make const in the portable case. |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 391 | uint32_t number_of_vregs_; |
Ian Rogers | 8a01a3a | 2013-05-06 13:25:44 -0700 | [diff] [blame] | 392 | #else |
| 393 | const uint32_t number_of_vregs_; |
| 394 | #endif |
Ian Rogers | 5438ad8 | 2012-10-15 17:22:44 -0700 | [diff] [blame] | 395 | // Link to previous shadow frame or NULL. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 396 | ShadowFrame* link_; |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 397 | mirror::ArtMethod* method_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 398 | uint32_t dex_pc_; |
TDYa127 | ce4cc0d | 2012-11-18 16:59:53 -0800 | [diff] [blame] | 399 | uint32_t vregs_[0]; |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 400 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 401 | DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame); |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 404 | // The managed stack is used to record fragments of managed code stacks. Managed code stacks |
| 405 | // may either be shadow frames or lists of frames using fixed frame sizes. Transition records are |
| 406 | // necessary for transitions between code using different frame layouts and transitions into native |
| 407 | // code. |
Ian Rogers | df1ce91 | 2012-11-27 17:07:11 -0800 | [diff] [blame] | 408 | class PACKED(4) ManagedStack { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 409 | public: |
Ian Rogers | ca19066 | 2012-06-26 15:45:57 -0700 | [diff] [blame] | 410 | ManagedStack() |
| 411 | : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {} |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 412 | |
| 413 | void PushManagedStackFragment(ManagedStack* fragment) { |
| 414 | // Copy this top fragment into given fragment. |
| 415 | memcpy(fragment, this, sizeof(ManagedStack)); |
| 416 | // Clear this fragment, which has become the top. |
| 417 | memset(this, 0, sizeof(ManagedStack)); |
| 418 | // Link our top fragment onto the given fragment. |
| 419 | link_ = fragment; |
| 420 | } |
| 421 | |
| 422 | void PopManagedStackFragment(const ManagedStack& fragment) { |
| 423 | DCHECK(&fragment == link_); |
| 424 | // Copy this given fragment back to the top. |
| 425 | memcpy(this, &fragment, sizeof(ManagedStack)); |
| 426 | } |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 427 | |
| 428 | ManagedStack* GetLink() const { |
| 429 | return link_; |
| 430 | } |
| 431 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 432 | StackReference<mirror::ArtMethod>* GetTopQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 433 | return top_quick_frame_; |
| 434 | } |
| 435 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 436 | void SetTopQuickFrame(StackReference<mirror::ArtMethod>* top) { |
Sebastien Hertz | a7b0c42 | 2013-04-05 16:19:39 +0200 | [diff] [blame] | 437 | DCHECK(top_shadow_frame_ == NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 438 | top_quick_frame_ = top; |
| 439 | } |
| 440 | |
| 441 | uintptr_t GetTopQuickFramePc() const { |
| 442 | return top_quick_frame_pc_; |
| 443 | } |
| 444 | |
| 445 | void SetTopQuickFramePc(uintptr_t pc) { |
Sebastien Hertz | a7b0c42 | 2013-04-05 16:19:39 +0200 | [diff] [blame] | 446 | DCHECK(top_shadow_frame_ == NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 447 | top_quick_frame_pc_ = pc; |
| 448 | } |
| 449 | |
| 450 | static size_t TopQuickFrameOffset() { |
| 451 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_); |
| 452 | } |
| 453 | |
| 454 | static size_t TopQuickFramePcOffset() { |
| 455 | return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_); |
| 456 | } |
| 457 | |
| 458 | ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) { |
Sebastien Hertz | a7b0c42 | 2013-04-05 16:19:39 +0200 | [diff] [blame] | 459 | DCHECK(top_quick_frame_ == NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 460 | ShadowFrame* old_frame = top_shadow_frame_; |
| 461 | top_shadow_frame_ = new_top_frame; |
| 462 | new_top_frame->SetLink(old_frame); |
| 463 | return old_frame; |
| 464 | } |
| 465 | |
| 466 | ShadowFrame* PopShadowFrame() { |
Sebastien Hertz | a7b0c42 | 2013-04-05 16:19:39 +0200 | [diff] [blame] | 467 | DCHECK(top_quick_frame_ == NULL); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 468 | CHECK(top_shadow_frame_ != NULL); |
| 469 | ShadowFrame* frame = top_shadow_frame_; |
| 470 | top_shadow_frame_ = frame->GetLink(); |
| 471 | return frame; |
| 472 | } |
| 473 | |
| 474 | ShadowFrame* GetTopShadowFrame() const { |
| 475 | return top_shadow_frame_; |
| 476 | } |
| 477 | |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 478 | void SetTopShadowFrame(ShadowFrame* top) { |
Sebastien Hertz | a7b0c42 | 2013-04-05 16:19:39 +0200 | [diff] [blame] | 479 | DCHECK(top_quick_frame_ == NULL); |
Jeff Hao | 11ffc2d | 2013-02-01 11:52:17 -0800 | [diff] [blame] | 480 | top_shadow_frame_ = top; |
| 481 | } |
| 482 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 483 | static size_t TopShadowFrameOffset() { |
| 484 | return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_); |
| 485 | } |
| 486 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 487 | size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 488 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 489 | bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 490 | |
| 491 | private: |
| 492 | ManagedStack* link_; |
| 493 | ShadowFrame* top_shadow_frame_; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 494 | StackReference<mirror::ArtMethod>* top_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 495 | uintptr_t top_quick_frame_pc_; |
| 496 | }; |
| 497 | |
| 498 | class StackVisitor { |
| 499 | protected: |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 500 | StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 501 | |
| 502 | public: |
| 503 | virtual ~StackVisitor() {} |
| 504 | |
| 505 | // Return 'true' if we should continue to visit more frames, 'false' to stop. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 506 | virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 507 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 508 | void WalkStack(bool include_transitions = false) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 509 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 510 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 511 | mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 512 | if (cur_shadow_frame_ != nullptr) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 513 | return cur_shadow_frame_->GetMethod(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 514 | } else if (cur_quick_frame_ != nullptr) { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 515 | return cur_quick_frame_->AsMirrorPtr(); |
Hiroshi Yamauchi | 92d1a66 | 2014-05-15 21:43:59 -0700 | [diff] [blame] | 516 | } else { |
| 517 | return nullptr; |
| 518 | } |
| 519 | } |
| 520 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 521 | bool IsShadowFrame() const { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 522 | return cur_shadow_frame_ != nullptr; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Dave Allison | b373e09 | 2014-02-20 16:06:36 -0800 | [diff] [blame] | 525 | uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 526 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 527 | mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 528 | |
Ian Rogers | 0c7abda | 2012-09-19 13:33:42 -0700 | [diff] [blame] | 529 | size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 530 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 531 | uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const |
| 532 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 533 | // Callee saves are held at the top of the frame |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 534 | DCHECK(GetMethod() != nullptr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 535 | byte* save_addr = |
| 536 | reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize); |
Andreas Gampe | bf6b92a | 2014-03-05 16:11:04 -0800 | [diff] [blame] | 537 | #if defined(__i386__) || defined(__x86_64__) |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 538 | save_addr -= kPointerSize; // account for return address |
| 539 | #endif |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 540 | return reinterpret_cast<uintptr_t*>(save_addr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 543 | // Returns the height of the stack in the managed stack frames, including transitions. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 544 | size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 545 | return GetNumFrames() - cur_depth_ - 1; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 548 | // Returns a frame ID for JDWP use, starting from 1. |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 549 | size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 550 | return GetFrameHeight() + 1; |
| 551 | } |
| 552 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 553 | size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 554 | if (num_frames_ == 0) { |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 555 | num_frames_ = ComputeNumFrames(thread_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 556 | } |
| 557 | return num_frames_; |
| 558 | } |
| 559 | |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 560 | // Get the method and dex pc immediately after the one that's currently being visited. |
| 561 | bool GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc) |
| 562 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 563 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 564 | uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 565 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 566 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 567 | void SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 568 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 569 | |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 570 | uintptr_t* GetGPRAddress(uint32_t reg) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 571 | uintptr_t GetGPR(uint32_t reg) const; |
Mathieu Chartier | 6702243 | 2012-11-29 18:04:50 -0800 | [diff] [blame] | 572 | void SetGPR(uint32_t reg, uintptr_t value); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 573 | |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 574 | // This is a fast-path for getting/setting values in a quick frame. |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 575 | uint32_t* GetVRegAddr(StackReference<mirror::ArtMethod>* cur_quick_frame, |
| 576 | const DexFile::CodeItem* code_item, |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 577 | uint32_t core_spills, uint32_t fp_spills, size_t frame_size, |
| 578 | uint16_t vreg) const { |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 579 | int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA); |
Ian Rogers | 0ec569a | 2012-07-01 16:43:46 -0700 | [diff] [blame] | 580 | DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame()); |
| 581 | byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset; |
Mathieu Chartier | 423d2a3 | 2013-09-12 17:33:56 -0700 | [diff] [blame] | 582 | return reinterpret_cast<uint32_t*>(vreg_addr); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 585 | uintptr_t GetReturnPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 586 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 587 | void SetReturnPc(uintptr_t new_ret_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 588 | |
| 589 | /* |
| 590 | * Return sp-relative offset for a Dalvik virtual register, compiler |
| 591 | * spill or Method* in bytes using Method*. |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 592 | * Note that (reg >= 0) refers to a Dalvik register, (reg == -1) |
| 593 | * denotes an invalid Dalvik register, (reg == -2) denotes Method* |
| 594 | * and (reg <= -3) denotes a compiler temporary. A compiler temporary |
| 595 | * can be thought of as a virtual register that does not exist in the |
| 596 | * dex but holds intermediate values to help optimizations and code |
| 597 | * generation. A special compiler temporary is one whose location |
| 598 | * in frame is well known while non-special ones do not have a requirement |
| 599 | * on location in frame as long as code generator itself knows how |
| 600 | * to access them. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 601 | * |
Zheng Xu | 511c8a6 | 2014-06-03 16:22:23 +0800 | [diff] [blame] | 602 | * +---------------------------+ |
| 603 | * | IN[ins-1] | {Note: resides in caller's frame} |
| 604 | * | . | |
| 605 | * | IN[0] | |
| 606 | * | caller's ArtMethod | ... StackReference<ArtMethod> |
| 607 | * +===========================+ {Note: start of callee's frame} |
| 608 | * | core callee-save spill | {variable sized} |
| 609 | * +---------------------------+ |
| 610 | * | fp callee-save spill | |
| 611 | * +---------------------------+ |
| 612 | * | filler word | {For compatibility, if V[locals-1] used as wide |
| 613 | * +---------------------------+ |
| 614 | * | V[locals-1] | |
| 615 | * | V[locals-2] | |
| 616 | * | . | |
| 617 | * | . | ... (reg == 2) |
| 618 | * | V[1] | ... (reg == 1) |
| 619 | * | V[0] | ... (reg == 0) <---- "locals_start" |
| 620 | * +---------------------------+ |
| 621 | * | Compiler temp region | ... (reg <= -3) |
| 622 | * | | |
| 623 | * | | |
| 624 | * +---------------------------+ |
| 625 | * | stack alignment padding | {0 to (kStackAlignWords-1) of padding} |
| 626 | * +---------------------------+ |
| 627 | * | OUT[outs-1] | |
| 628 | * | OUT[outs-2] | |
| 629 | * | . | |
| 630 | * | OUT[0] | |
| 631 | * | StackReference<ArtMethod> | ... (reg == -2) <<== sp, 16-byte aligned |
| 632 | * +===========================+ |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 633 | */ |
| 634 | static int GetVRegOffset(const DexFile::CodeItem* code_item, |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 635 | uint32_t core_spills, uint32_t fp_spills, |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 636 | size_t frame_size, int reg, InstructionSet isa) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 637 | DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 638 | DCHECK_NE(reg, static_cast<int>(kVRegInvalid)); |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 639 | int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa) |
| 640 | + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa) |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 641 | + sizeof(uint32_t); // Filler. |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 642 | int num_ins = code_item->ins_size_; |
| 643 | int num_regs = code_item->registers_size_ - num_ins; |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 644 | int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 645 | if (reg == static_cast<int>(kVRegMethodPtrBaseReg)) { |
| 646 | // The current method pointer corresponds to special location on stack. |
| 647 | return 0; |
| 648 | } else if (reg <= static_cast<int>(kVRegNonSpecialTempBaseReg)) { |
| 649 | /* |
| 650 | * Special temporaries may have custom locations and the logic above deals with that. |
| 651 | * However, non-special temporaries are placed relative to the locals. Since the |
| 652 | * virtual register numbers for temporaries "grow" in negative direction, reg number |
| 653 | * will always be <= to the temp base reg. Thus, the logic ensures that the first |
| 654 | * temp is at offset -4 bytes from locals, the second is at -8 bytes from locals, |
| 655 | * and so on. |
| 656 | */ |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 657 | int relative_offset = |
| 658 | (reg + std::abs(static_cast<int>(kVRegNonSpecialTempBaseReg)) - 1) * sizeof(uint32_t); |
Razvan A Lupusoru | da7a69b | 2014-01-08 15:09:50 -0800 | [diff] [blame] | 659 | return locals_start + relative_offset; |
| 660 | } else if (reg < num_regs) { |
| 661 | return locals_start + (reg * sizeof(uint32_t)); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 662 | } else { |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 663 | // Handle ins. |
buzbee | 8281864 | 2014-06-04 15:35:41 -0700 | [diff] [blame^] | 664 | return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + |
| 665 | sizeof(StackReference<mirror::ArtMethod>); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
Nicolas Geoffray | 42fcd98 | 2014-04-22 11:03:52 +0000 | [diff] [blame] | 669 | static int GetOutVROffset(uint16_t out_num, InstructionSet isa) { |
buzbee | 8281864 | 2014-06-04 15:35:41 -0700 | [diff] [blame^] | 670 | // According to stack model, the first out is above the Method referernce. |
| 671 | return sizeof(StackReference<mirror::ArtMethod>) + (out_num * sizeof(uint32_t)); |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 674 | uintptr_t GetCurrentQuickFramePc() const { |
| 675 | return cur_quick_frame_pc_; |
| 676 | } |
| 677 | |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 678 | StackReference<mirror::ArtMethod>* GetCurrentQuickFrame() const { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 679 | return cur_quick_frame_; |
| 680 | } |
| 681 | |
| 682 | ShadowFrame* GetCurrentShadowFrame() const { |
| 683 | return cur_shadow_frame_; |
| 684 | } |
| 685 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 686 | HandleScope* GetCurrentHandleScope() const { |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 687 | StackReference<mirror::ArtMethod>* sp = GetCurrentQuickFrame(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 688 | ++sp; // Skip Method*; handle scope comes next; |
| 689 | return reinterpret_cast<HandleScope*>(sp); |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Ian Rogers | 40e3bac | 2012-11-20 00:09:14 -0800 | [diff] [blame] | 692 | std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 693 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 694 | static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 695 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 696 | static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 306057f | 2012-11-26 12:45:53 -0800 | [diff] [blame] | 697 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 698 | private: |
Ian Rogers | 5cf9819 | 2014-05-29 21:31:50 -0700 | [diff] [blame] | 699 | // Private constructor known in the case that num_frames_ has already been computed. |
| 700 | StackVisitor(Thread* thread, Context* context, size_t num_frames) |
| 701 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 702 | |
Sebastien Hertz | 74e256b | 2013-10-04 10:40:37 +0200 | [diff] [blame] | 703 | instrumentation::InstrumentationStackFrame& GetInstrumentationStackFrame(uint32_t depth) const; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 704 | |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 705 | void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 706 | |
Ian Rogers | 7a22fa6 | 2013-01-23 12:16:16 -0800 | [diff] [blame] | 707 | Thread* const thread_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 708 | ShadowFrame* cur_shadow_frame_; |
Andreas Gampe | cf4035a | 2014-05-28 22:43:01 -0700 | [diff] [blame] | 709 | StackReference<mirror::ArtMethod>* cur_quick_frame_; |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 710 | uintptr_t cur_quick_frame_pc_; |
| 711 | // Lazily computed, number of frames in the stack. |
| 712 | size_t num_frames_; |
| 713 | // Depth of the frame we're currently at. |
| 714 | size_t cur_depth_; |
Brian Carlstrom | 0cd7ec2 | 2013-07-17 23:40:20 -0700 | [diff] [blame] | 715 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 716 | protected: |
| 717 | Context* const context_; |
| 718 | }; |
| 719 | |
Elliott Hughes | 68e7652 | 2011-10-05 13:22:16 -0700 | [diff] [blame] | 720 | } // namespace art |
| 721 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 722 | #endif // ART_RUNTIME_STACK_H_ |