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