blob: ef40be825adf55326112470208865375a22d22a1 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 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#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "interpreter/interpreter.h"
24#include "invoke_arg_array_builder.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070026#include "mirror/class-inl.h"
27#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
31
32namespace art {
33
34// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
35class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080036 // Size of each spilled GPR.
37#ifdef __LP64__
38 static constexpr size_t kBytesPerGprSpillLocation = 8;
39#else
40 static constexpr size_t kBytesPerGprSpillLocation = 4;
41#endif
42 // Number of bytes for each out register in the caller method's frame.
43 static constexpr size_t kBytesStackArgLocation = 4;
Ian Rogers848871b2013-08-05 10:56:33 -070044#if defined(__arm__)
45 // The callee save frame is pointed to by SP.
46 // | argN | |
47 // | ... | |
48 // | arg4 | |
49 // | arg3 spill | | Caller's frame
50 // | arg2 spill | |
51 // | arg1 spill | |
52 // | Method* | ---
53 // | LR |
54 // | ... | callee saves
55 // | R3 | arg3
56 // | R2 | arg2
57 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080058 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070059 // | Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -080060 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
61 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
62 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
63 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes.
64 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
65 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
66 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
67 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size.
68 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
69 return gpr_index * kBytesPerGprSpillLocation;
70 }
Ian Rogers848871b2013-08-05 10:56:33 -070071#elif defined(__mips__)
72 // The callee save frame is pointed to by SP.
73 // | argN | |
74 // | ... | |
75 // | arg4 | |
76 // | arg3 spill | | Caller's frame
77 // | arg2 spill | |
78 // | arg1 spill | |
79 // | Method* | ---
80 // | RA |
81 // | ... | callee saves
82 // | A3 | arg3
83 // | A2 | arg2
84 // | A1 | arg1
85 // | A0/Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -080086 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
87 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
88 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
89 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes.
90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size.
94 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
95 return gpr_index * kBytesPerGprSpillLocation;
96 }
Ian Rogers848871b2013-08-05 10:56:33 -070097#elif defined(__i386__)
98 // The callee save frame is pointed to by SP.
99 // | argN | |
100 // | ... | |
101 // | arg4 | |
102 // | arg3 spill | | Caller's frame
103 // | arg2 spill | |
104 // | arg1 spill | |
105 // | Method* | ---
106 // | Return |
107 // | EBP,ESI,EDI | callee saves
108 // | EBX | arg3
109 // | EDX | arg2
110 // | ECX | arg1
111 // | EAX/Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -0800112 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
113 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
114 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
115 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size.
120 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
121 return gpr_index * kBytesPerGprSpillLocation;
122 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800124 // The callee save frame is pointed to by SP.
125 // | argN | |
126 // | ... | |
127 // | reg. arg spills | | Caller's frame
128 // | Method* | ---
129 // | Return |
130 // | R15 | callee save
131 // | R14 | callee save
132 // | R13 | callee save
133 // | R12 | callee save
134 // | R9 | arg5
135 // | R8 | arg4
136 // | RSI/R6 | arg1
137 // | RBP/R5 | callee save
138 // | RBX/R3 | callee save
139 // | RDX/R2 | arg2
140 // | RCX/R1 | arg3
141 // | XMM7 | float arg 8
142 // | XMM6 | float arg 7
143 // | XMM5 | float arg 6
144 // | XMM4 | float arg 5
145 // | XMM3 | float arg 4
146 // | XMM2 | float arg 3
147 // | XMM1 | float arg 2
148 // | XMM0 | float arg 1
149 // | Padding |
150 // | RDI/Method* | <- sp
151 static constexpr bool kSoftFloatAbi = false; // This is a hard float ABI.
152 static constexpr size_t kNumGprArgs = 5; // 3 arguments passed in GPRs.
153 static constexpr size_t kNumFprArgs = 8; // 0 arguments passed in FPRs.
154 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes.
155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
156 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
158 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size.
159 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
160 switch (gpr_index) {
161 case 0: return (4 * kBytesPerGprSpillLocation);
162 case 1: return (1 * kBytesPerGprSpillLocation);
163 case 2: return (0 * kBytesPerGprSpillLocation);
164 case 3: return (5 * kBytesPerGprSpillLocation);
165 case 4: return (6 * kBytesPerGprSpillLocation);
166 default:
167 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
168 return 0;
169 }
170 }
Ian Rogers848871b2013-08-05 10:56:33 -0700171#else
172#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700173#endif
174
Ian Rogers936b37f2014-02-14 00:52:24 -0800175 public:
176 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp)
177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
178 DCHECK((*sp)->IsCalleeSaveMethod());
179 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Brian Carlstromea46f952013-07-30 01:26:50 -0700180 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700181 }
182
Ian Rogers936b37f2014-02-14 00:52:24 -0800183 // For the given quick ref and args quick frame, return the caller's PC.
184 static uintptr_t GetCallingPc(mirror::ArtMethod** sp)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
186 DCHECK((*sp)->IsCalleeSaveMethod());
187 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700188 return *reinterpret_cast<uintptr_t*>(lr);
189 }
190
Brian Carlstromea46f952013-07-30 01:26:50 -0700191 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700192 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800193 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
194 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
195 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
196 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
197 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
198 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
199 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
200 is_split_long_or_double_(false) {
201 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
Ian Rogers848871b2013-08-05 10:56:33 -0700202 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
203 }
204
205 virtual ~QuickArgumentVisitor() {}
206
207 virtual void Visit() = 0;
208
Ian Rogers936b37f2014-02-14 00:52:24 -0800209 Primitive::Type GetParamPrimitiveType() const {
210 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700211 }
212
213 byte* GetParamAddress() const {
Ian Rogers936b37f2014-02-14 00:52:24 -0800214 if (!kSoftFloatAbi) {
215 Primitive::Type type = GetParamPrimitiveType();
216 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
217 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
218 return fpr_args_ + (fpr_index_ * kBytesPerFprSpillLocation);
219 }
220 }
221 }
222 if (gpr_index_ < kNumGprArgs) {
223 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
224 }
225 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700226 }
227
228 bool IsSplitLongOrDouble() const {
Ian Rogers936b37f2014-02-14 00:52:24 -0800229 if ((kBytesPerGprSpillLocation == 4) || (kBytesPerFprSpillLocation == 4)) {
230 return is_split_long_or_double_;
231 } else {
232 return false; // An optimization for when GPR and FPRs are 64bit.
233 }
Ian Rogers848871b2013-08-05 10:56:33 -0700234 }
235
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700237 return GetParamPrimitiveType() == Primitive::kPrimNot;
238 }
239
Ian Rogers936b37f2014-02-14 00:52:24 -0800240 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700241 Primitive::Type type = GetParamPrimitiveType();
242 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
243 }
244
245 uint64_t ReadSplitLongParam() const {
246 DCHECK(IsSplitLongOrDouble());
247 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
248 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
249 return (low_half & 0xffffffffULL) | (high_half << 32);
250 }
251
252 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 gpr_index_ = 0;
254 fpr_index_ = 0;
255 stack_index_ = 0;
256 if (!is_static_) { // Handle this.
257 cur_type_ = Primitive::kPrimNot;
258 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700259 Visit();
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 if (kNumGprArgs > 0) {
261 gpr_index_++;
262 } else {
263 stack_index_++;
264 }
Ian Rogers848871b2013-08-05 10:56:33 -0700265 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800266 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
267 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
268 switch (cur_type_) {
269 case Primitive::kPrimNot:
270 case Primitive::kPrimBoolean:
271 case Primitive::kPrimByte:
272 case Primitive::kPrimChar:
273 case Primitive::kPrimShort:
274 case Primitive::kPrimInt:
275 is_split_long_or_double_ = false;
276 Visit();
277 if (gpr_index_ < kNumGprArgs) {
278 gpr_index_++;
279 } else {
280 stack_index_++;
281 }
282 break;
283 case Primitive::kPrimFloat:
284 is_split_long_or_double_ = false;
285 Visit();
286 if (kSoftFloatAbi) {
287 if (gpr_index_ < kNumGprArgs) {
288 gpr_index_++;
289 } else {
290 stack_index_++;
291 }
292 } else {
293 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
294 fpr_index_++;
295 } else {
296 stack_index_++;
297 }
298 }
299 break;
300 case Primitive::kPrimDouble:
301 case Primitive::kPrimLong:
302 if (kSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
303 is_split_long_or_double_ = (kBytesPerGprSpillLocation == 4) &&
304 ((gpr_index_ + 1) == kNumGprArgs);
305 Visit();
306 if (gpr_index_ < kNumGprArgs) {
307 gpr_index_++;
308 if (kBytesPerGprSpillLocation == 4) {
309 if (gpr_index_ < kNumGprArgs) {
310 gpr_index_++;
311 } else {
312 stack_index_++;
313 }
314 }
315 } else {
316 if (kBytesStackArgLocation == 4) {
317 stack_index_+= 2;
318 } else {
319 CHECK_EQ(kBytesStackArgLocation, 8U);
320 stack_index_++;
321 }
322 }
323 } else {
324 is_split_long_or_double_ = (kBytesPerFprSpillLocation == 4) &&
325 ((fpr_index_ + 1) == kNumFprArgs);
326 Visit();
327 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
328 fpr_index_++;
329 if (kBytesPerFprSpillLocation == 4) {
330 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
331 fpr_index_++;
332 } else {
333 stack_index_++;
334 }
335 }
336 } else {
337 if (kBytesStackArgLocation == 4) {
338 stack_index_+= 2;
339 } else {
340 CHECK_EQ(kBytesStackArgLocation, 8U);
341 stack_index_++;
342 }
343 }
344 }
345 break;
346 default:
347 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
348 }
Ian Rogers848871b2013-08-05 10:56:33 -0700349 }
350 }
351
352 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800353 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
354 uint32_t shorty_len) {
355 if (kSoftFloatAbi) {
356 CHECK_EQ(kNumFprArgs, 0U);
357 return (kNumGprArgs * kBytesPerGprSpillLocation) + kBytesPerGprSpillLocation /* ArtMethod* */;
358 } else {
359 size_t offset = kBytesPerGprSpillLocation; // Skip Method*.
360 size_t gprs_seen = 0;
361 size_t fprs_seen = 0;
362 if (!is_static && (gprs_seen < kNumGprArgs)) {
363 gprs_seen++;
364 offset += kBytesStackArgLocation;
Ian Rogers848871b2013-08-05 10:56:33 -0700365 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800366 for (uint32_t i = 1; i < shorty_len; ++i) {
367 switch (shorty[i]) {
368 case 'Z':
369 case 'B':
370 case 'C':
371 case 'S':
372 case 'I':
373 case 'L':
374 if (gprs_seen < kNumGprArgs) {
375 gprs_seen++;
376 offset += kBytesStackArgLocation;
377 }
378 break;
379 case 'J':
380 if (gprs_seen < kNumGprArgs) {
381 gprs_seen++;
382 offset += 2 * kBytesStackArgLocation;
383 if (kBytesPerGprSpillLocation == 4) {
384 if (gprs_seen < kNumGprArgs) {
385 gprs_seen++;
386 }
387 }
388 }
389 break;
390 case 'F':
391 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
392 fprs_seen++;
393 offset += kBytesStackArgLocation;
394 }
395 break;
396 case 'D':
397 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
398 fprs_seen++;
399 offset += 2 * kBytesStackArgLocation;
400 if (kBytesPerFprSpillLocation == 4) {
401 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
402 fprs_seen++;
403 }
404 }
405 }
406 break;
407 default:
408 LOG(FATAL) << "Unexpected shorty character: " << shorty[i] << " in " << shorty;
409 }
Ian Rogers848871b2013-08-05 10:56:33 -0700410 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800411 return offset;
Ian Rogers848871b2013-08-05 10:56:33 -0700412 }
Ian Rogers848871b2013-08-05 10:56:33 -0700413 }
414
415 const bool is_static_;
416 const char* const shorty_;
417 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800418 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
419 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
420 byte* const stack_args_; // Address of stack arguments in caller's frame.
421 uint32_t gpr_index_; // Index into spilled GPRs.
422 uint32_t fpr_index_; // Index into spilled FPRs.
423 uint32_t stack_index_; // Index into arguments on the stack.
424 // The current type of argument during VisitArguments.
425 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700426 // Does a 64bit parameter straddle the register and stack arguments?
427 bool is_split_long_or_double_;
428};
429
430// Visits arguments on the stack placing them into the shadow frame.
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700431class BuildQuickShadowFrameVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700432 public:
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
434 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700435 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
436
437 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
438 Primitive::Type type = GetParamPrimitiveType();
439 switch (type) {
440 case Primitive::kPrimLong: // Fall-through.
441 case Primitive::kPrimDouble:
442 if (IsSplitLongOrDouble()) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
Ian Rogers848871b2013-08-05 10:56:33 -0700444 } else {
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
Ian Rogers848871b2013-08-05 10:56:33 -0700446 }
447 ++cur_reg_;
448 break;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800449 case Primitive::kPrimNot: {
450 StackReference<mirror::Object>* stack_ref =
451 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
452 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
453 }
Ian Rogers848871b2013-08-05 10:56:33 -0700454 break;
455 case Primitive::kPrimBoolean: // Fall-through.
456 case Primitive::kPrimByte: // Fall-through.
457 case Primitive::kPrimChar: // Fall-through.
458 case Primitive::kPrimShort: // Fall-through.
459 case Primitive::kPrimInt: // Fall-through.
460 case Primitive::kPrimFloat:
Ian Rogers936b37f2014-02-14 00:52:24 -0800461 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
Ian Rogers848871b2013-08-05 10:56:33 -0700462 break;
463 case Primitive::kPrimVoid:
464 LOG(FATAL) << "UNREACHABLE";
465 break;
466 }
467 ++cur_reg_;
468 }
469
470 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800471 ShadowFrame* const sf_;
472 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700473
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700474 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700475};
476
Brian Carlstromea46f952013-07-30 01:26:50 -0700477extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
478 mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
480 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
481 // frame.
482 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
483
484 if (method->IsAbstract()) {
485 ThrowAbstractMethodError(method);
486 return 0;
487 } else {
488 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
489 MethodHelper mh(method);
490 const DexFile::CodeItem* code_item = mh.GetCodeItem();
491 uint16_t num_regs = code_item->registers_size_;
492 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
493 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
494 method, 0, memory));
495 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700496 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 mh.GetShortyLength(),
498 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700499 shadow_frame_builder.VisitArguments();
500 // Push a transition back into managed code onto the linked list in thread.
501 ManagedStack fragment;
502 self->PushManagedStackFragment(&fragment);
503 self->PushShadowFrame(shadow_frame);
504 self->EndAssertNoThreadSuspension(old_cause);
505
506 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
507 // Ensure static method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800508 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
509 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700510 DCHECK(Thread::Current()->IsExceptionPending());
511 self->PopManagedStackFragment(fragment);
512 return 0;
513 }
514 }
515
516 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
517 // Pop transition.
518 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800519 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700520 return result.GetJ();
521 }
522}
523
524// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
525// to jobjects.
526class BuildQuickArgumentVisitor : public QuickArgumentVisitor {
527 public:
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
Ian Rogers848871b2013-08-05 10:56:33 -0700529 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa,
530 std::vector<jvalue>* args) :
531 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
532
533 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
534 jvalue val;
535 Primitive::Type type = GetParamPrimitiveType();
536 switch (type) {
537 case Primitive::kPrimNot: {
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800538 StackReference<mirror::Object>* stack_ref =
539 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
540 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
541 references_.push_back(std::make_pair(val.l, stack_ref));
Ian Rogers848871b2013-08-05 10:56:33 -0700542 break;
543 }
544 case Primitive::kPrimLong: // Fall-through.
545 case Primitive::kPrimDouble:
546 if (IsSplitLongOrDouble()) {
547 val.j = ReadSplitLongParam();
548 } else {
549 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
550 }
551 break;
552 case Primitive::kPrimBoolean: // Fall-through.
553 case Primitive::kPrimByte: // Fall-through.
554 case Primitive::kPrimChar: // Fall-through.
555 case Primitive::kPrimShort: // Fall-through.
556 case Primitive::kPrimInt: // Fall-through.
557 case Primitive::kPrimFloat:
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800558 val.i = *reinterpret_cast<jint*>(GetParamAddress());
Ian Rogers848871b2013-08-05 10:56:33 -0700559 break;
560 case Primitive::kPrimVoid:
561 LOG(FATAL) << "UNREACHABLE";
562 val.j = 0;
563 break;
564 }
565 args_->push_back(val);
566 }
567
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800568 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
569 // Fixup any references which may have changed.
570 for (const auto& pair : references_) {
571 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
572 }
573 }
574
Ian Rogers848871b2013-08-05 10:56:33 -0700575 private:
576 ScopedObjectAccessUnchecked* soa_;
577 std::vector<jvalue>* args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800578 // References which we must update when exiting in case the GC moved the objects.
579 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
Ian Rogers848871b2013-08-05 10:56:33 -0700580 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
581};
582
583// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
584// which is responsible for recording callee save registers. We explicitly place into jobjects the
585// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
586// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700587extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700588 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700590 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700591 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
592 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700593 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
594 const char* old_cause =
595 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
596 // Register the top of the managed stack, making stack crawlable.
Brian Carlstromd3633d52013-08-20 21:06:26 -0700597 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700598 self->SetTopOfStack(sp, 0);
599 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700600 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
601 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700602 self->VerifyStack();
603 // Start new JNI local reference state.
604 JNIEnvExt* env = self->GetJniEnv();
605 ScopedObjectAccessUnchecked soa(env);
606 ScopedJniEnvLocalRefState env_state(env);
607 // Create local ref. copies of proxy method and the receiver.
608 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
609
610 // Placing arguments into args vector and remove the receiver.
611 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700612 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700613 std::vector<jvalue> args;
614 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
615 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700616
Ian Rogers848871b2013-08-05 10:56:33 -0700617 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700618 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700619 args.erase(args.begin());
620
621 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700622 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700623 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700624 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
625 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
626
627 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
628 // that performs allocations.
629 self->EndAssertNoThreadSuspension(old_cause);
630 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
631 rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800632 // Restore references which might have moved.
633 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700634 return result.GetJ();
635}
636
637// Read object references held in arguments from quick frames and place in a JNI local references,
638// so they don't get garbage collected.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700639class RememberForGcArgumentVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700640 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
642 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700643 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
644
645 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
646 if (IsParamAReference()) {
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800647 StackReference<mirror::Object>* stack_ref =
648 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700649 jobject reference =
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800650 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
651 references_.push_back(std::make_pair(reference, stack_ref));
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700652 }
653 }
654
655 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
656 // Fixup any references which may have changed.
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800657 for (const auto& pair : references_) {
658 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Ian Rogers848871b2013-08-05 10:56:33 -0700659 }
660 }
661
662 private:
663 ScopedObjectAccessUnchecked* soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800664 // References which we must update when exiting in case the GC moved the objects.
665 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700666 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700667};
668
669// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700670extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700671 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 Thread* thread, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
674 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
675 // Start new JNI local reference state
676 JNIEnvExt* env = thread->GetJniEnv();
677 ScopedObjectAccessUnchecked soa(env);
678 ScopedJniEnvLocalRefState env_state(env);
679 const char* old_cause = thread->StartAssertNoThreadSuspension("Quick method resolution set up");
680
681 // Compute details about the called method (avoid GCs)
682 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700683 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700684 InvokeType invoke_type;
685 const DexFile* dex_file;
686 uint32_t dex_method_idx;
687 if (called->IsRuntimeMethod()) {
688 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
689 const DexFile::CodeItem* code;
690 {
691 MethodHelper mh(caller);
692 dex_file = &mh.GetDexFile();
693 code = mh.GetCodeItem();
694 }
695 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
696 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
697 Instruction::Code instr_code = instr->Opcode();
698 bool is_range;
699 switch (instr_code) {
700 case Instruction::INVOKE_DIRECT:
701 invoke_type = kDirect;
702 is_range = false;
703 break;
704 case Instruction::INVOKE_DIRECT_RANGE:
705 invoke_type = kDirect;
706 is_range = true;
707 break;
708 case Instruction::INVOKE_STATIC:
709 invoke_type = kStatic;
710 is_range = false;
711 break;
712 case Instruction::INVOKE_STATIC_RANGE:
713 invoke_type = kStatic;
714 is_range = true;
715 break;
716 case Instruction::INVOKE_SUPER:
717 invoke_type = kSuper;
718 is_range = false;
719 break;
720 case Instruction::INVOKE_SUPER_RANGE:
721 invoke_type = kSuper;
722 is_range = true;
723 break;
724 case Instruction::INVOKE_VIRTUAL:
725 invoke_type = kVirtual;
726 is_range = false;
727 break;
728 case Instruction::INVOKE_VIRTUAL_RANGE:
729 invoke_type = kVirtual;
730 is_range = true;
731 break;
732 case Instruction::INVOKE_INTERFACE:
733 invoke_type = kInterface;
734 is_range = false;
735 break;
736 case Instruction::INVOKE_INTERFACE_RANGE:
737 invoke_type = kInterface;
738 is_range = true;
739 break;
740 default:
741 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
742 // Avoid used uninitialized warnings.
743 invoke_type = kDirect;
744 is_range = false;
745 }
746 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
747
748 } else {
749 invoke_type = kStatic;
750 dex_file = &MethodHelper(called).GetDexFile();
751 dex_method_idx = called->GetDexMethodIndex();
752 }
753 uint32_t shorty_len;
754 const char* shorty =
755 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700757 visitor.VisitArguments();
758 thread->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800759 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700760 // Resolve method filling in dex cache.
761 if (called->IsRuntimeMethod()) {
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800762 SirtRef<mirror::Object> sirt_receiver(soa.Self(), virtual_or_interface ? receiver : nullptr);
Ian Rogers848871b2013-08-05 10:56:33 -0700763 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800764 receiver = sirt_receiver.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700765 }
766 const void* code = NULL;
767 if (LIKELY(!thread->IsExceptionPending())) {
768 // Incompatible class change should have been handled in resolve method.
769 CHECK(!called->CheckIncompatibleClassChange(invoke_type));
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800770 if (virtual_or_interface) {
771 // Refine called method based on receiver.
772 CHECK(receiver != nullptr) << invoke_type;
773 if (invoke_type == kVirtual) {
774 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
775 } else {
776 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
777 }
Ian Rogers83883d72013-10-21 21:07:24 -0700778 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
779 // of the sharpened method.
780 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100781 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700782 } else {
783 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000784 // the caller's dex file. Since we get here only if the original called was a runtime
785 // method, we've got the correct dex_file and a dex_method_idx from above.
786 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700787 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000788 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700789 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100790 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700791 }
792 }
793 }
Ian Rogers848871b2013-08-05 10:56:33 -0700794 // Ensure that the called method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800795 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass());
Ian Rogers848871b2013-08-05 10:56:33 -0700796 linker->EnsureInitialized(called_class, true, true);
797 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800798 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700799 } else if (called_class->IsInitializing()) {
800 if (invoke_type == kStatic) {
801 // Class is still initializing, go to oat and grab code (trampoline must be left in place
802 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800803 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700804 } else {
805 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800806 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700807 }
808 } else {
809 DCHECK(called_class->IsErroneous());
810 }
811 }
812 CHECK_EQ(code == NULL, thread->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700813 // Fixup any locally saved objects may have moved during a GC.
814 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700815 // Place called method in callee-save frame to be placed as first argument to quick method.
816 *sp = called;
817 return code;
818}
819
Andreas Gampe2da88232014-02-27 12:26:20 -0800820extern "C" const void* artQuickGenericJniTrampoline(mirror::ArtMethod* called,
821 mirror::Object* receiver,
822 Thread* thread, mirror::ArtMethod** sp)
823 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
824 LOG(FATAL) << "artQuickGenericJniTrampoline not implemented: "
825 << PrettyMethod(called);
826 return NULL;
827}
828
Ian Rogers848871b2013-08-05 10:56:33 -0700829} // namespace art