blob: b3fce5aefab0bc558a1ef359ea5ed773c51f2115 [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
Dragos Sbirleabd136a22013-08-13 18:07:04 -070032
33
Ian Rogers848871b2013-08-05 10:56:33 -070034namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080038 // Size of each spilled GPR.
39#ifdef __LP64__
40 static constexpr size_t kBytesPerGprSpillLocation = 8;
41#else
42 static constexpr size_t kBytesPerGprSpillLocation = 4;
43#endif
44 // Number of bytes for each out register in the caller method's frame.
45 static constexpr size_t kBytesStackArgLocation = 4;
Ian Rogers848871b2013-08-05 10:56:33 -070046#if defined(__arm__)
47 // The callee save frame is pointed to by SP.
48 // | argN | |
49 // | ... | |
50 // | arg4 | |
51 // | arg3 spill | | Caller's frame
52 // | arg2 spill | |
53 // | arg1 spill | |
54 // | Method* | ---
55 // | LR |
56 // | ... | callee saves
57 // | R3 | arg3
58 // | R2 | arg2
59 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080060 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070061 // | Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -080062 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
63 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
64 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
65 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes.
66 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
67 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
68 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
69 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size.
70 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
71 return gpr_index * kBytesPerGprSpillLocation;
72 }
Ian Rogers848871b2013-08-05 10:56:33 -070073#elif defined(__mips__)
74 // The callee save frame is pointed to by SP.
75 // | argN | |
76 // | ... | |
77 // | arg4 | |
78 // | arg3 spill | | Caller's frame
79 // | arg2 spill | |
80 // | arg1 spill | |
81 // | Method* | ---
82 // | RA |
83 // | ... | callee saves
84 // | A3 | arg3
85 // | A2 | arg2
86 // | A1 | arg1
87 // | A0/Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -080088 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
89 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
90 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
91 static constexpr size_t kBytesPerFprSpillLocation = 4; // FPR spill size is 4 bytes.
92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
94 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
95 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size.
96 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
97 return gpr_index * kBytesPerGprSpillLocation;
98 }
Ian Rogers848871b2013-08-05 10:56:33 -070099#elif defined(__i386__)
100 // The callee save frame is pointed to by SP.
101 // | argN | |
102 // | ... | |
103 // | arg4 | |
104 // | arg3 spill | | Caller's frame
105 // | arg2 spill | |
106 // | arg1 spill | |
107 // | Method* | ---
108 // | Return |
109 // | EBP,ESI,EDI | callee saves
110 // | EBX | arg3
111 // | EDX | arg2
112 // | ECX | arg1
113 // | EAX/Method* | <- sp
Ian Rogers936b37f2014-02-14 00:52:24 -0800114 static constexpr bool kSoftFloatAbi = true; // This is a soft float ABI.
115 static constexpr size_t kNumGprArgs = 3; // 3 arguments passed in GPRs.
116 static constexpr size_t kNumFprArgs = 0; // 0 arguments passed in FPRs.
117 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes.
118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
119 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
120 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
121 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size.
122 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
123 return gpr_index * kBytesPerGprSpillLocation;
124 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800125#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800126 // The callee save frame is pointed to by SP.
127 // | argN | |
128 // | ... | |
129 // | reg. arg spills | | Caller's frame
130 // | Method* | ---
131 // | Return |
132 // | R15 | callee save
133 // | R14 | callee save
134 // | R13 | callee save
135 // | R12 | callee save
136 // | R9 | arg5
137 // | R8 | arg4
138 // | RSI/R6 | arg1
139 // | RBP/R5 | callee save
140 // | RBX/R3 | callee save
141 // | RDX/R2 | arg2
142 // | RCX/R1 | arg3
143 // | XMM7 | float arg 8
144 // | XMM6 | float arg 7
145 // | XMM5 | float arg 6
146 // | XMM4 | float arg 5
147 // | XMM3 | float arg 4
148 // | XMM2 | float arg 3
149 // | XMM1 | float arg 2
150 // | XMM0 | float arg 1
151 // | Padding |
152 // | RDI/Method* | <- sp
153 static constexpr bool kSoftFloatAbi = false; // This is a hard float ABI.
154 static constexpr size_t kNumGprArgs = 5; // 3 arguments passed in GPRs.
155 static constexpr size_t kNumFprArgs = 8; // 0 arguments passed in FPRs.
156 static constexpr size_t kBytesPerFprSpillLocation = 8; // FPR spill size is 8 bytes.
157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
158 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
159 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
160 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size.
161 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
162 switch (gpr_index) {
163 case 0: return (4 * kBytesPerGprSpillLocation);
164 case 1: return (1 * kBytesPerGprSpillLocation);
165 case 2: return (0 * kBytesPerGprSpillLocation);
166 case 3: return (5 * kBytesPerGprSpillLocation);
167 case 4: return (6 * kBytesPerGprSpillLocation);
168 default:
169 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
170 return 0;
171 }
172 }
Ian Rogers848871b2013-08-05 10:56:33 -0700173#else
174#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700175#endif
176
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 public:
178 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp)
179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
180 DCHECK((*sp)->IsCalleeSaveMethod());
181 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Brian Carlstromea46f952013-07-30 01:26:50 -0700182 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700183 }
184
Ian Rogers936b37f2014-02-14 00:52:24 -0800185 // For the given quick ref and args quick frame, return the caller's PC.
186 static uintptr_t GetCallingPc(mirror::ArtMethod** sp)
187 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
188 DCHECK((*sp)->IsCalleeSaveMethod());
189 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700190 return *reinterpret_cast<uintptr_t*>(lr);
191 }
192
Brian Carlstromea46f952013-07-30 01:26:50 -0700193 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700194 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800195 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
196 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
197 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
198 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
199 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
200 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
201 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
202 is_split_long_or_double_(false) {
203 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
Ian Rogers848871b2013-08-05 10:56:33 -0700204 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
205 }
206
207 virtual ~QuickArgumentVisitor() {}
208
209 virtual void Visit() = 0;
210
Ian Rogers936b37f2014-02-14 00:52:24 -0800211 Primitive::Type GetParamPrimitiveType() const {
212 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700213 }
214
215 byte* GetParamAddress() const {
Ian Rogers936b37f2014-02-14 00:52:24 -0800216 if (!kSoftFloatAbi) {
217 Primitive::Type type = GetParamPrimitiveType();
218 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
219 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
220 return fpr_args_ + (fpr_index_ * kBytesPerFprSpillLocation);
221 }
222 }
223 }
224 if (gpr_index_ < kNumGprArgs) {
225 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
226 }
227 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700228 }
229
230 bool IsSplitLongOrDouble() const {
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 if ((kBytesPerGprSpillLocation == 4) || (kBytesPerFprSpillLocation == 4)) {
232 return is_split_long_or_double_;
233 } else {
234 return false; // An optimization for when GPR and FPRs are 64bit.
235 }
Ian Rogers848871b2013-08-05 10:56:33 -0700236 }
237
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700239 return GetParamPrimitiveType() == Primitive::kPrimNot;
240 }
241
Ian Rogers936b37f2014-02-14 00:52:24 -0800242 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700243 Primitive::Type type = GetParamPrimitiveType();
244 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
245 }
246
247 uint64_t ReadSplitLongParam() const {
248 DCHECK(IsSplitLongOrDouble());
249 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
250 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
251 return (low_half & 0xffffffffULL) | (high_half << 32);
252 }
253
254 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800255 gpr_index_ = 0;
256 fpr_index_ = 0;
257 stack_index_ = 0;
258 if (!is_static_) { // Handle this.
259 cur_type_ = Primitive::kPrimNot;
260 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700261 Visit();
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 if (kNumGprArgs > 0) {
263 gpr_index_++;
264 } else {
265 stack_index_++;
266 }
Ian Rogers848871b2013-08-05 10:56:33 -0700267 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800268 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
269 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
270 switch (cur_type_) {
271 case Primitive::kPrimNot:
272 case Primitive::kPrimBoolean:
273 case Primitive::kPrimByte:
274 case Primitive::kPrimChar:
275 case Primitive::kPrimShort:
276 case Primitive::kPrimInt:
277 is_split_long_or_double_ = false;
278 Visit();
279 if (gpr_index_ < kNumGprArgs) {
280 gpr_index_++;
281 } else {
282 stack_index_++;
283 }
284 break;
285 case Primitive::kPrimFloat:
286 is_split_long_or_double_ = false;
287 Visit();
288 if (kSoftFloatAbi) {
289 if (gpr_index_ < kNumGprArgs) {
290 gpr_index_++;
291 } else {
292 stack_index_++;
293 }
294 } else {
295 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
296 fpr_index_++;
297 } else {
298 stack_index_++;
299 }
300 }
301 break;
302 case Primitive::kPrimDouble:
303 case Primitive::kPrimLong:
304 if (kSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
305 is_split_long_or_double_ = (kBytesPerGprSpillLocation == 4) &&
306 ((gpr_index_ + 1) == kNumGprArgs);
307 Visit();
308 if (gpr_index_ < kNumGprArgs) {
309 gpr_index_++;
310 if (kBytesPerGprSpillLocation == 4) {
311 if (gpr_index_ < kNumGprArgs) {
312 gpr_index_++;
313 } else {
314 stack_index_++;
315 }
316 }
317 } else {
318 if (kBytesStackArgLocation == 4) {
319 stack_index_+= 2;
320 } else {
321 CHECK_EQ(kBytesStackArgLocation, 8U);
322 stack_index_++;
323 }
324 }
325 } else {
326 is_split_long_or_double_ = (kBytesPerFprSpillLocation == 4) &&
327 ((fpr_index_ + 1) == kNumFprArgs);
328 Visit();
329 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
330 fpr_index_++;
331 if (kBytesPerFprSpillLocation == 4) {
332 if ((kNumFprArgs != 0) && (fpr_index_ + 1 < kNumFprArgs + 1)) {
333 fpr_index_++;
334 } else {
335 stack_index_++;
336 }
337 }
338 } else {
339 if (kBytesStackArgLocation == 4) {
340 stack_index_+= 2;
341 } else {
342 CHECK_EQ(kBytesStackArgLocation, 8U);
343 stack_index_++;
344 }
345 }
346 }
347 break;
348 default:
349 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
350 }
Ian Rogers848871b2013-08-05 10:56:33 -0700351 }
352 }
353
354 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800355 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
356 uint32_t shorty_len) {
357 if (kSoftFloatAbi) {
358 CHECK_EQ(kNumFprArgs, 0U);
359 return (kNumGprArgs * kBytesPerGprSpillLocation) + kBytesPerGprSpillLocation /* ArtMethod* */;
360 } else {
361 size_t offset = kBytesPerGprSpillLocation; // Skip Method*.
362 size_t gprs_seen = 0;
363 size_t fprs_seen = 0;
364 if (!is_static && (gprs_seen < kNumGprArgs)) {
365 gprs_seen++;
366 offset += kBytesStackArgLocation;
Ian Rogers848871b2013-08-05 10:56:33 -0700367 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 for (uint32_t i = 1; i < shorty_len; ++i) {
369 switch (shorty[i]) {
370 case 'Z':
371 case 'B':
372 case 'C':
373 case 'S':
374 case 'I':
375 case 'L':
376 if (gprs_seen < kNumGprArgs) {
377 gprs_seen++;
378 offset += kBytesStackArgLocation;
379 }
380 break;
381 case 'J':
382 if (gprs_seen < kNumGprArgs) {
383 gprs_seen++;
384 offset += 2 * kBytesStackArgLocation;
385 if (kBytesPerGprSpillLocation == 4) {
386 if (gprs_seen < kNumGprArgs) {
387 gprs_seen++;
388 }
389 }
390 }
391 break;
392 case 'F':
393 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
394 fprs_seen++;
395 offset += kBytesStackArgLocation;
396 }
397 break;
398 case 'D':
399 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
400 fprs_seen++;
401 offset += 2 * kBytesStackArgLocation;
402 if (kBytesPerFprSpillLocation == 4) {
403 if ((kNumFprArgs != 0) && (fprs_seen + 1 < kNumFprArgs + 1)) {
404 fprs_seen++;
405 }
406 }
407 }
408 break;
409 default:
410 LOG(FATAL) << "Unexpected shorty character: " << shorty[i] << " in " << shorty;
411 }
Ian Rogers848871b2013-08-05 10:56:33 -0700412 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 return offset;
Ian Rogers848871b2013-08-05 10:56:33 -0700414 }
Ian Rogers848871b2013-08-05 10:56:33 -0700415 }
416
417 const bool is_static_;
418 const char* const shorty_;
419 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800420 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
421 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
422 byte* const stack_args_; // Address of stack arguments in caller's frame.
423 uint32_t gpr_index_; // Index into spilled GPRs.
424 uint32_t fpr_index_; // Index into spilled FPRs.
425 uint32_t stack_index_; // Index into arguments on the stack.
426 // The current type of argument during VisitArguments.
427 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700428 // Does a 64bit parameter straddle the register and stack arguments?
429 bool is_split_long_or_double_;
430};
431
432// Visits arguments on the stack placing them into the shadow frame.
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700433class BuildQuickShadowFrameVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700434 public:
Ian Rogers936b37f2014-02-14 00:52:24 -0800435 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
436 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700437 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
438
439 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
440 Primitive::Type type = GetParamPrimitiveType();
441 switch (type) {
442 case Primitive::kPrimLong: // Fall-through.
443 case Primitive::kPrimDouble:
444 if (IsSplitLongOrDouble()) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
Ian Rogers848871b2013-08-05 10:56:33 -0700446 } else {
Ian Rogers936b37f2014-02-14 00:52:24 -0800447 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
Ian Rogers848871b2013-08-05 10:56:33 -0700448 }
449 ++cur_reg_;
450 break;
451 case Primitive::kPrimNot:
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 sf_->SetVRegReference(cur_reg_, *reinterpret_cast<mirror::Object**>(GetParamAddress()));
Ian Rogers848871b2013-08-05 10:56:33 -0700453 break;
454 case Primitive::kPrimBoolean: // Fall-through.
455 case Primitive::kPrimByte: // Fall-through.
456 case Primitive::kPrimChar: // Fall-through.
457 case Primitive::kPrimShort: // Fall-through.
458 case Primitive::kPrimInt: // Fall-through.
459 case Primitive::kPrimFloat:
Ian Rogers936b37f2014-02-14 00:52:24 -0800460 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
Ian Rogers848871b2013-08-05 10:56:33 -0700461 break;
462 case Primitive::kPrimVoid:
463 LOG(FATAL) << "UNREACHABLE";
464 break;
465 }
466 ++cur_reg_;
467 }
468
469 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800470 ShadowFrame* const sf_;
471 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700472
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700473 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700474};
475
Brian Carlstromea46f952013-07-30 01:26:50 -0700476extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
477 mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700478 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
479 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
480 // frame.
481 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
482
483 if (method->IsAbstract()) {
484 ThrowAbstractMethodError(method);
485 return 0;
486 } else {
487 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
488 MethodHelper mh(method);
489 const DexFile::CodeItem* code_item = mh.GetCodeItem();
490 uint16_t num_regs = code_item->registers_size_;
491 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
492 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
493 method, 0, memory));
494 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700495 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 mh.GetShortyLength(),
497 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700498 shadow_frame_builder.VisitArguments();
499 // Push a transition back into managed code onto the linked list in thread.
500 ManagedStack fragment;
501 self->PushManagedStackFragment(&fragment);
502 self->PushShadowFrame(shadow_frame);
503 self->EndAssertNoThreadSuspension(old_cause);
504
505 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
506 // Ensure static method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800507 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
508 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) {
Ian Rogers848871b2013-08-05 10:56:33 -0700509 DCHECK(Thread::Current()->IsExceptionPending());
510 self->PopManagedStackFragment(fragment);
511 return 0;
512 }
513 }
514
515 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
516 // Pop transition.
517 self->PopManagedStackFragment(fragment);
518 return result.GetJ();
519 }
520}
521
522// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
523// to jobjects.
524class BuildQuickArgumentVisitor : public QuickArgumentVisitor {
525 public:
Brian Carlstromea46f952013-07-30 01:26:50 -0700526 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
Ian Rogers848871b2013-08-05 10:56:33 -0700527 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa,
528 std::vector<jvalue>* args) :
529 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
530
531 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
532 jvalue val;
533 Primitive::Type type = GetParamPrimitiveType();
534 switch (type) {
535 case Primitive::kPrimNot: {
536 mirror::Object* obj = *reinterpret_cast<mirror::Object**>(GetParamAddress());
537 val.l = soa_->AddLocalReference<jobject>(obj);
538 break;
539 }
540 case Primitive::kPrimLong: // Fall-through.
541 case Primitive::kPrimDouble:
542 if (IsSplitLongOrDouble()) {
543 val.j = ReadSplitLongParam();
544 } else {
545 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
546 }
547 break;
548 case Primitive::kPrimBoolean: // Fall-through.
549 case Primitive::kPrimByte: // Fall-through.
550 case Primitive::kPrimChar: // Fall-through.
551 case Primitive::kPrimShort: // Fall-through.
552 case Primitive::kPrimInt: // Fall-through.
553 case Primitive::kPrimFloat:
554 val.i = *reinterpret_cast<jint*>(GetParamAddress());
555 break;
556 case Primitive::kPrimVoid:
557 LOG(FATAL) << "UNREACHABLE";
558 val.j = 0;
559 break;
560 }
561 args_->push_back(val);
562 }
563
564 private:
565 ScopedObjectAccessUnchecked* soa_;
566 std::vector<jvalue>* args_;
567
568 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
569};
570
571// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
572// which is responsible for recording callee save registers. We explicitly place into jobjects the
573// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
574// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700575extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700576 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700578 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700579 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
580 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700581 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
582 const char* old_cause =
583 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
584 // Register the top of the managed stack, making stack crawlable.
Brian Carlstromd3633d52013-08-20 21:06:26 -0700585 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700586 self->SetTopOfStack(sp, 0);
587 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700588 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
589 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700590 self->VerifyStack();
591 // Start new JNI local reference state.
592 JNIEnvExt* env = self->GetJniEnv();
593 ScopedObjectAccessUnchecked soa(env);
594 ScopedJniEnvLocalRefState env_state(env);
595 // Create local ref. copies of proxy method and the receiver.
596 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
597
598 // Placing arguments into args vector and remove the receiver.
599 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700600 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700601 std::vector<jvalue> args;
602 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
603 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700604
Ian Rogers848871b2013-08-05 10:56:33 -0700605 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700606 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700607 args.erase(args.begin());
608
609 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700611 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700612 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
613 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
614
615 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
616 // that performs allocations.
617 self->EndAssertNoThreadSuspension(old_cause);
618 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
619 rcvr_jobj, interface_method_jobj, args);
620 return result.GetJ();
621}
622
623// Read object references held in arguments from quick frames and place in a JNI local references,
624// so they don't get garbage collected.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700625class RememberForGcArgumentVisitor : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700626 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700627 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
628 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700629 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
630
631 virtual void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
632 if (IsParamAReference()) {
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700633 mirror::Object** param_address = reinterpret_cast<mirror::Object**>(GetParamAddress());
634 jobject reference =
635 soa_->AddLocalReference<jobject>(*param_address);
636 references_.push_back(std::make_pair(reference, param_address));
637 }
638 }
639
640 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
641 // Fixup any references which may have changed.
642 for (std::pair<jobject, mirror::Object**>& it : references_) {
643 *it.second = soa_->Decode<mirror::Object*>(it.first);
Ian Rogers848871b2013-08-05 10:56:33 -0700644 }
645 }
646
647 private:
648 ScopedObjectAccessUnchecked* soa_;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700649 std::vector<std::pair<jobject, mirror::Object**> > references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700651};
652
653// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700654extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700655 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700656 Thread* thread, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700657 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
658 FinishCalleeSaveFrameSetup(thread, sp, Runtime::kRefsAndArgs);
659 // Start new JNI local reference state
660 JNIEnvExt* env = thread->GetJniEnv();
661 ScopedObjectAccessUnchecked soa(env);
662 ScopedJniEnvLocalRefState env_state(env);
663 const char* old_cause = thread->StartAssertNoThreadSuspension("Quick method resolution set up");
664
665 // Compute details about the called method (avoid GCs)
666 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700667 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700668 InvokeType invoke_type;
669 const DexFile* dex_file;
670 uint32_t dex_method_idx;
671 if (called->IsRuntimeMethod()) {
672 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
673 const DexFile::CodeItem* code;
674 {
675 MethodHelper mh(caller);
676 dex_file = &mh.GetDexFile();
677 code = mh.GetCodeItem();
678 }
679 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
680 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
681 Instruction::Code instr_code = instr->Opcode();
682 bool is_range;
683 switch (instr_code) {
684 case Instruction::INVOKE_DIRECT:
685 invoke_type = kDirect;
686 is_range = false;
687 break;
688 case Instruction::INVOKE_DIRECT_RANGE:
689 invoke_type = kDirect;
690 is_range = true;
691 break;
692 case Instruction::INVOKE_STATIC:
693 invoke_type = kStatic;
694 is_range = false;
695 break;
696 case Instruction::INVOKE_STATIC_RANGE:
697 invoke_type = kStatic;
698 is_range = true;
699 break;
700 case Instruction::INVOKE_SUPER:
701 invoke_type = kSuper;
702 is_range = false;
703 break;
704 case Instruction::INVOKE_SUPER_RANGE:
705 invoke_type = kSuper;
706 is_range = true;
707 break;
708 case Instruction::INVOKE_VIRTUAL:
709 invoke_type = kVirtual;
710 is_range = false;
711 break;
712 case Instruction::INVOKE_VIRTUAL_RANGE:
713 invoke_type = kVirtual;
714 is_range = true;
715 break;
716 case Instruction::INVOKE_INTERFACE:
717 invoke_type = kInterface;
718 is_range = false;
719 break;
720 case Instruction::INVOKE_INTERFACE_RANGE:
721 invoke_type = kInterface;
722 is_range = true;
723 break;
724 default:
725 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
726 // Avoid used uninitialized warnings.
727 invoke_type = kDirect;
728 is_range = false;
729 }
730 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
731
732 } else {
733 invoke_type = kStatic;
734 dex_file = &MethodHelper(called).GetDexFile();
735 dex_method_idx = called->GetDexMethodIndex();
736 }
737 uint32_t shorty_len;
738 const char* shorty =
739 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700740 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700741 visitor.VisitArguments();
742 thread->EndAssertNoThreadSuspension(old_cause);
743 // Resolve method filling in dex cache.
744 if (called->IsRuntimeMethod()) {
745 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type);
746 }
747 const void* code = NULL;
748 if (LIKELY(!thread->IsExceptionPending())) {
749 // Incompatible class change should have been handled in resolve method.
750 CHECK(!called->CheckIncompatibleClassChange(invoke_type));
751 // Refine called method based on receiver.
752 if (invoke_type == kVirtual) {
753 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
754 } else if (invoke_type == kInterface) {
755 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
756 }
Ian Rogers83883d72013-10-21 21:07:24 -0700757 if ((invoke_type == kVirtual) || (invoke_type == kInterface)) {
758 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
759 // of the sharpened method.
760 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100761 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700762 } else {
763 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000764 // the caller's dex file. Since we get here only if the original called was a runtime
765 // method, we've got the correct dex_file and a dex_method_idx from above.
766 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700767 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000768 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700769 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100770 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700771 }
772 }
773 }
Ian Rogers848871b2013-08-05 10:56:33 -0700774 // Ensure that the called method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800775 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass());
Ian Rogers848871b2013-08-05 10:56:33 -0700776 linker->EnsureInitialized(called_class, true, true);
777 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800778 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700779 } else if (called_class->IsInitializing()) {
780 if (invoke_type == kStatic) {
781 // Class is still initializing, go to oat and grab code (trampoline must be left in place
782 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800783 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700784 } else {
785 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800786 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700787 }
788 } else {
789 DCHECK(called_class->IsErroneous());
790 }
791 }
792 CHECK_EQ(code == NULL, thread->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700793 // Fixup any locally saved objects may have moved during a GC.
794 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700795 // Place called method in callee-save frame to be placed as first argument to quick method.
796 *sp = called;
797 return code;
798}
799
800} // namespace art