blob: 80fdadb0a777cb73c7c658738ef425e10d6764c3 [file] [log] [blame]
Andreas Gampe36a296f2017-06-13 14:11:11 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_
18#define ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_
19
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <cstdint>
Andreas Gampe36a296f2017-06-13 14:11:11 -070021#include <cstring>
Andreas Gampe36a296f2017-06-13 14:11:11 -070022#include <string>
23
24#include "base/macros.h"
25#include "base/mutex.h"
26#include "dex_file.h"
27#include "lock_count_data.h"
28#include "read_barrier.h"
29#include "stack_reference.h"
30#include "verify_object.h"
31
32namespace art {
33
34namespace mirror {
35 class Object;
36} // namespace mirror
37
38class ArtMethod;
39class ShadowFrame;
40class Thread;
41union JValue;
42
43// Forward declaration. Just calls the destructor.
44struct ShadowFrameDeleter;
45using ShadowFrameAllocaUniquePtr = std::unique_ptr<ShadowFrame, ShadowFrameDeleter>;
46
47// ShadowFrame has 2 possible layouts:
48// - interpreter - separate VRegs and reference arrays. References are in the reference array.
49// - JNI - just VRegs, but where every VReg holds a reference.
50class ShadowFrame {
51 public:
52 // Compute size of ShadowFrame in bytes assuming it has a reference array.
53 static size_t ComputeSize(uint32_t num_vregs) {
54 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
55 (sizeof(StackReference<mirror::Object>) * num_vregs);
56 }
57
58 // Create ShadowFrame in heap for deoptimization.
59 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
60 ArtMethod* method, uint32_t dex_pc) {
61 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
62 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
63 }
64
65 // Delete a ShadowFrame allocated on the heap for deoptimization.
66 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
67 sf->~ShadowFrame(); // Explicitly destruct.
68 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
69 delete[] memory;
70 }
71
72 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
73 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
74#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
75 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
76 void* alloca_mem = alloca(frame_size); \
77 ShadowFrameAllocaUniquePtr( \
78 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
79 (alloca_mem))); \
80 })
81
82 ~ShadowFrame() {}
83
84 // TODO(iam): Clean references array up since they're always there,
85 // we don't need to do conditionals.
86 bool HasReferenceArray() const {
87 return true;
88 }
89
90 uint32_t NumberOfVRegs() const {
91 return number_of_vregs_;
92 }
93
94 uint32_t GetDexPC() const {
95 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - code_item_->insns_;
96 }
97
98 int16_t GetCachedHotnessCountdown() const {
99 return cached_hotness_countdown_;
100 }
101
102 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
103 cached_hotness_countdown_ = cached_hotness_countdown;
104 }
105
106 int16_t GetHotnessCountdown() const {
107 return hotness_countdown_;
108 }
109
110 void SetHotnessCountdown(int16_t hotness_countdown) {
111 hotness_countdown_ = hotness_countdown;
112 }
113
114 void SetDexPC(uint32_t dex_pc) {
115 dex_pc_ = dex_pc;
116 dex_pc_ptr_ = nullptr;
117 }
118
119 ShadowFrame* GetLink() const {
120 return link_;
121 }
122
123 void SetLink(ShadowFrame* frame) {
124 DCHECK_NE(this, frame);
125 link_ = frame;
126 }
127
128 int32_t GetVReg(size_t i) const {
129 DCHECK_LT(i, NumberOfVRegs());
130 const uint32_t* vreg = &vregs_[i];
131 return *reinterpret_cast<const int32_t*>(vreg);
132 }
133
134 // Shorts are extended to Ints in VRegs. Interpreter intrinsics needs them as shorts.
135 int16_t GetVRegShort(size_t i) const {
136 return static_cast<int16_t>(GetVReg(i));
137 }
138
139 uint32_t* GetVRegAddr(size_t i) {
140 return &vregs_[i];
141 }
142
143 uint32_t* GetShadowRefAddr(size_t i) {
144 DCHECK(HasReferenceArray());
145 DCHECK_LT(i, NumberOfVRegs());
146 return &vregs_[i + NumberOfVRegs()];
147 }
148
149 void SetCodeItem(const DexFile::CodeItem* code_item) {
150 code_item_ = code_item;
151 }
152
153 const DexFile::CodeItem* GetCodeItem() const {
154 return code_item_;
155 }
156
157 float GetVRegFloat(size_t i) const {
158 DCHECK_LT(i, NumberOfVRegs());
159 // NOTE: Strict-aliasing?
160 const uint32_t* vreg = &vregs_[i];
161 return *reinterpret_cast<const float*>(vreg);
162 }
163
164 int64_t GetVRegLong(size_t i) const {
165 DCHECK_LT(i, NumberOfVRegs());
166 const uint32_t* vreg = &vregs_[i];
167 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
168 return *reinterpret_cast<unaligned_int64*>(vreg);
169 }
170
171 double GetVRegDouble(size_t i) const {
172 DCHECK_LT(i, NumberOfVRegs());
173 const uint32_t* vreg = &vregs_[i];
174 typedef const double unaligned_double __attribute__ ((aligned (4)));
175 return *reinterpret_cast<unaligned_double*>(vreg);
176 }
177
178 // Look up the reference given its virtual register number.
179 // If this returns non-null then this does not mean the vreg is currently a reference
180 // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain.
181 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
182 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
183 DCHECK_LT(i, NumberOfVRegs());
184 mirror::Object* ref;
185 if (HasReferenceArray()) {
186 ref = References()[i].AsMirrorPtr();
187 } else {
188 const uint32_t* vreg_ptr = &vregs_[i];
189 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
190 }
Roland Levillaina78f5b62017-09-29 13:50:44 +0100191 ReadBarrier::MaybeAssertToSpaceInvariant(ref);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700192 if (kVerifyFlags & kVerifyReads) {
193 VerifyObject(ref);
194 }
195 return ref;
196 }
197
198 // Get view of vregs as range of consecutive arguments starting at i.
199 uint32_t* GetVRegArgs(size_t i) {
200 return &vregs_[i];
201 }
202
203 void SetVReg(size_t i, int32_t val) {
204 DCHECK_LT(i, NumberOfVRegs());
205 uint32_t* vreg = &vregs_[i];
206 *reinterpret_cast<int32_t*>(vreg) = val;
207 // This is needed for moving collectors since these can update the vreg references if they
208 // happen to agree with references in the reference array.
209 if (kMovingCollector && HasReferenceArray()) {
210 References()[i].Clear();
211 }
212 }
213
214 void SetVRegFloat(size_t i, float val) {
215 DCHECK_LT(i, NumberOfVRegs());
216 uint32_t* vreg = &vregs_[i];
217 *reinterpret_cast<float*>(vreg) = val;
218 // This is needed for moving collectors since these can update the vreg references if they
219 // happen to agree with references in the reference array.
220 if (kMovingCollector && HasReferenceArray()) {
221 References()[i].Clear();
222 }
223 }
224
225 void SetVRegLong(size_t i, int64_t val) {
226 DCHECK_LT(i, NumberOfVRegs());
227 uint32_t* vreg = &vregs_[i];
228 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
229 *reinterpret_cast<unaligned_int64*>(vreg) = val;
230 // This is needed for moving collectors since these can update the vreg references if they
231 // happen to agree with references in the reference array.
232 if (kMovingCollector && HasReferenceArray()) {
233 References()[i].Clear();
234 References()[i + 1].Clear();
235 }
236 }
237
238 void SetVRegDouble(size_t i, double val) {
239 DCHECK_LT(i, NumberOfVRegs());
240 uint32_t* vreg = &vregs_[i];
241 typedef double unaligned_double __attribute__ ((aligned (4)));
242 *reinterpret_cast<unaligned_double*>(vreg) = val;
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()) {
246 References()[i].Clear();
247 References()[i + 1].Clear();
248 }
249 }
250
251 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
252 void SetVRegReference(size_t i, mirror::Object* val) REQUIRES_SHARED(Locks::mutator_lock_) {
253 DCHECK_LT(i, NumberOfVRegs());
254 if (kVerifyFlags & kVerifyWrites) {
255 VerifyObject(val);
256 }
Roland Levillaina78f5b62017-09-29 13:50:44 +0100257 ReadBarrier::MaybeAssertToSpaceInvariant(val);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700258 uint32_t* vreg = &vregs_[i];
259 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
260 if (HasReferenceArray()) {
261 References()[i].Assign(val);
262 }
263 }
264
265 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) {
266 DCHECK(method != nullptr);
267 DCHECK(method_ != nullptr);
268 method_ = method;
269 }
270
271 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
272 DCHECK(method_ != nullptr);
273 return method_;
274 }
275
276 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
277
278 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
279
280 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
281 if (HasReferenceArray()) {
282 return ((&References()[0] <= shadow_frame_entry_obj) &&
283 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
284 } else {
285 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
286 return ((&vregs_[0] <= shadow_frame_entry) &&
287 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
288 }
289 }
290
291 LockCountData& GetLockCountData() {
292 return lock_count_data_;
293 }
294
295 static size_t LockCountDataOffset() {
296 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
297 }
298
299 static size_t LinkOffset() {
300 return OFFSETOF_MEMBER(ShadowFrame, link_);
301 }
302
303 static size_t MethodOffset() {
304 return OFFSETOF_MEMBER(ShadowFrame, method_);
305 }
306
307 static size_t DexPCOffset() {
308 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
309 }
310
311 static size_t NumberOfVRegsOffset() {
312 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
313 }
314
315 static size_t VRegsOffset() {
316 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
317 }
318
319 static size_t ResultRegisterOffset() {
320 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
321 }
322
323 static size_t DexPCPtrOffset() {
324 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
325 }
326
327 static size_t CodeItemOffset() {
328 return OFFSETOF_MEMBER(ShadowFrame, code_item_);
329 }
330
331 static size_t CachedHotnessCountdownOffset() {
332 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
333 }
334
335 static size_t HotnessCountdownOffset() {
336 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
337 }
338
339 // Create ShadowFrame for interpreter using provided memory.
340 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
341 ShadowFrame* link,
342 ArtMethod* method,
343 uint32_t dex_pc,
344 void* memory) {
345 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
346 }
347
348 const uint16_t* GetDexPCPtr() {
349 return dex_pc_ptr_;
350 }
351
352 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
353 dex_pc_ptr_ = dex_pc_ptr;
354 }
355
356 JValue* GetResultRegister() {
357 return result_register_;
358 }
359
Alex Lighte814f9d2017-07-31 16:14:39 -0700360 bool NeedsNotifyPop() const {
361 return needs_notify_pop_;
362 }
363
364 void SetNotifyPop(bool notify) {
365 needs_notify_pop_ = notify;
366 }
367
Andreas Gampe36a296f2017-06-13 14:11:11 -0700368 private:
369 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
370 uint32_t dex_pc, bool has_reference_array)
371 : link_(link),
372 method_(method),
373 result_register_(nullptr),
374 dex_pc_ptr_(nullptr),
375 code_item_(nullptr),
376 number_of_vregs_(num_vregs),
377 dex_pc_(dex_pc),
378 cached_hotness_countdown_(0),
Alex Lighte814f9d2017-07-31 16:14:39 -0700379 hotness_countdown_(0),
380 needs_notify_pop_(0) {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700381 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
382 DCHECK(has_reference_array);
383 if (has_reference_array) {
384 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
385 } else {
386 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
387 }
388 }
389
390 const StackReference<mirror::Object>* References() const {
391 DCHECK(HasReferenceArray());
392 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
393 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
394 }
395
396 StackReference<mirror::Object>* References() {
397 return const_cast<StackReference<mirror::Object>*>(
398 const_cast<const ShadowFrame*>(this)->References());
399 }
400
401 // Link to previous shadow frame or null.
402 ShadowFrame* link_;
403 ArtMethod* method_;
404 JValue* result_register_;
405 const uint16_t* dex_pc_ptr_;
406 const DexFile::CodeItem* code_item_;
407 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
408 const uint32_t number_of_vregs_;
409 uint32_t dex_pc_;
410 int16_t cached_hotness_countdown_;
411 int16_t hotness_countdown_;
Alex Lighte814f9d2017-07-31 16:14:39 -0700412 // TODO Might be worth it to try to bit-pack this into some other field to reduce stack usage.
413 // NB alignment requires that this field takes 4 bytes. Only 1 bit is actually ever used.
414 bool needs_notify_pop_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700415
416 // This is a two-part array:
417 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
418 // bytes.
419 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
420 // ptr-sized.
421 // In other words when a primitive is stored in vX, the second (reference) part of the array will
422 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
423 // copy of vX.
424 uint32_t vregs_[0];
425
426 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
427};
428
429struct ShadowFrameDeleter {
430 inline void operator()(ShadowFrame* frame) {
431 if (frame != nullptr) {
432 frame->~ShadowFrame();
433 }
434 }
435};
436
437} // namespace art
438
439#endif // ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_