blob: 8bc3707894e4ded8c0096e9ca8e7291aef4b69d0 [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Andreas Gamped58342c2014-06-05 14:18:08 -070023#include "instruction_set.h"
Ian Rogers848871b2013-08-05 10:56:33 -070024#include "interpreter/interpreter.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"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070027#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "mirror/object-inl.h"
29#include "mirror/object_array-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070031#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032
33namespace art {
34
35// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
36class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080037 // Number of bytes for each out register in the caller method's frame.
38 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070039 // Frame size in bytes of a callee-save frame for RefsAndArgs.
40 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
41 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070042#if defined(__arm__)
43 // The callee save frame is pointed to by SP.
44 // | argN | |
45 // | ... | |
46 // | arg4 | |
47 // | arg3 spill | | Caller's frame
48 // | arg2 spill | |
49 // | arg1 spill | |
50 // | Method* | ---
51 // | LR |
52 // | ... | callee saves
53 // | R3 | arg3
54 // | R2 | arg2
55 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080056 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070057 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080058 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
59 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
60 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080061 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
62 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
63 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080064 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000065 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080066 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000067#elif defined(__aarch64__)
68 // The callee save frame is pointed to by SP.
69 // | argN | |
70 // | ... | |
71 // | arg4 | |
72 // | arg3 spill | | Caller's frame
73 // | arg2 spill | |
74 // | arg1 spill | |
75 // | Method* | ---
76 // | LR |
77 // | X28 |
78 // | : |
79 // | X19 |
80 // | X7 |
81 // | : |
82 // | X1 |
83 // | D15 |
84 // | : |
85 // | D0 |
86 // | | padding
87 // | Method* | <- sp
88 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
89 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
90 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070091 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Stuart Monteithb95a5342014-03-12 13:32:32 +000092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +000094 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000095 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000096 }
Ian Rogers848871b2013-08-05 10:56:33 -070097#elif defined(__mips__)
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 // | RA |
107 // | ... | callee saves
108 // | A3 | arg3
109 // | A2 | arg2
110 // | A1 | arg1
111 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800112 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
113 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
114 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800120 }
Ian Rogers848871b2013-08-05 10:56:33 -0700121#elif defined(__i386__)
122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | Return |
131 // | EBP,ESI,EDI | callee saves
132 // | EBX | arg3
133 // | EDX | arg2
134 // | ECX | arg1
135 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800142 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000143 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800144 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800146 // The callee save frame is pointed to by SP.
147 // | argN | |
148 // | ... | |
149 // | reg. arg spills | | Caller's frame
150 // | Method* | ---
151 // | Return |
152 // | R15 | callee save
153 // | R14 | callee save
154 // | R13 | callee save
155 // | R12 | callee save
156 // | R9 | arg5
157 // | R8 | arg4
158 // | RSI/R6 | arg1
159 // | RBP/R5 | callee save
160 // | RBX/R3 | callee save
161 // | RDX/R2 | arg2
162 // | RCX/R1 | arg3
163 // | XMM7 | float arg 8
164 // | XMM6 | float arg 7
165 // | XMM5 | float arg 6
166 // | XMM4 | float arg 5
167 // | XMM3 | float arg 4
168 // | XMM2 | float arg 3
169 // | XMM1 | float arg 2
170 // | XMM0 | float arg 1
171 // | Padding |
172 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800173 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700174 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700175 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800176 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800179 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
180 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000181 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
182 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
183 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800186 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700187 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
188 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800189 }
190 }
Ian Rogers848871b2013-08-05 10:56:33 -0700191#else
192#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700193#endif
194
Ian Rogers936b37f2014-02-14 00:52:24 -0800195 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700196 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700198 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800199 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700200 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700201 }
202
Ian Rogers936b37f2014-02-14 00:52:24 -0800203 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700204 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700206 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800207 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700208 return *reinterpret_cast<uintptr_t*>(lr);
209 }
210
Andreas Gampec200a4a2014-06-16 18:39:09 -0700211 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
212 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
213 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
214 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
215 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
216 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
217 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
218 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
219 is_split_long_or_double_(false) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700220
221 virtual ~QuickArgumentVisitor() {}
222
223 virtual void Visit() = 0;
224
Ian Rogers936b37f2014-02-14 00:52:24 -0800225 Primitive::Type GetParamPrimitiveType() const {
226 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700227 }
228
229 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800230 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 Primitive::Type type = GetParamPrimitiveType();
232 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800233 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000234 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800235 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700236 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 }
238 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800239 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800240 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
241 }
242 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700243 }
244
245 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000246 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800247 return is_split_long_or_double_;
248 } else {
249 return false; // An optimization for when GPR and FPRs are 64bit.
250 }
Ian Rogers848871b2013-08-05 10:56:33 -0700251 }
252
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700254 return GetParamPrimitiveType() == Primitive::kPrimNot;
255 }
256
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700258 Primitive::Type type = GetParamPrimitiveType();
259 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
260 }
261
262 uint64_t ReadSplitLongParam() const {
263 DCHECK(IsSplitLongOrDouble());
264 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
265 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
266 return (low_half & 0xffffffffULL) | (high_half << 32);
267 }
268
269 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700270 // This implementation doesn't support reg-spill area for hard float
271 // ABI targets such as x86_64 and aarch64. So, for those targets whose
272 // 'kQuickSoftFloatAbi' is 'false':
273 // (a) 'stack_args_' should point to the first method's argument
274 // (b) whatever the argument type it is, the 'stack_index_' should
275 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800276 gpr_index_ = 0;
277 fpr_index_ = 0;
278 stack_index_ = 0;
279 if (!is_static_) { // Handle this.
280 cur_type_ = Primitive::kPrimNot;
281 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700282 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700283 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
284 stack_index_++;
285 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800286 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800287 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 }
Ian Rogers848871b2013-08-05 10:56:33 -0700289 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800290 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
291 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
292 switch (cur_type_) {
293 case Primitive::kPrimNot:
294 case Primitive::kPrimBoolean:
295 case Primitive::kPrimByte:
296 case Primitive::kPrimChar:
297 case Primitive::kPrimShort:
298 case Primitive::kPrimInt:
299 is_split_long_or_double_ = false;
300 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700301 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
302 stack_index_++;
303 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800304 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800305 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 }
307 break;
308 case Primitive::kPrimFloat:
309 is_split_long_or_double_ = false;
310 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800311 if (kQuickSoftFloatAbi) {
312 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800313 gpr_index_++;
314 } else {
315 stack_index_++;
316 }
317 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800318 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800319 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700321 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800322 }
323 break;
324 case Primitive::kPrimDouble:
325 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800326 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000327 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800328 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800329 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700330 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800331 if (kBytesStackArgLocation == 4) {
332 stack_index_+= 2;
333 } else {
334 CHECK_EQ(kBytesStackArgLocation, 8U);
335 stack_index_++;
336 }
337 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700338 if (gpr_index_ < kNumQuickGprArgs) {
339 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000340 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700341 if (gpr_index_ < kNumQuickGprArgs) {
342 gpr_index_++;
343 } else if (kQuickSoftFloatAbi) {
344 stack_index_++;
345 }
346 }
347 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800348 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000349 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800350 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800351 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800352 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800353 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000354 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800355 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800356 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 }
358 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700359 }
360 if (kBytesStackArgLocation == 4) {
361 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700363 CHECK_EQ(kBytesStackArgLocation, 8U);
364 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800365 }
366 }
367 break;
368 default:
369 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
370 }
Ian Rogers848871b2013-08-05 10:56:33 -0700371 }
372 }
373
374 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800375 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
376 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800377 if (kQuickSoftFloatAbi) {
378 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000379 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700380 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700382 // For now, there is no reg-spill area for the targets with
383 // hard float ABI. So, the offset pointing to the first method's
384 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700385 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700386 }
Ian Rogers848871b2013-08-05 10:56:33 -0700387 }
388
Andreas Gampec200a4a2014-06-16 18:39:09 -0700389 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700390 const bool is_static_;
391 const char* const shorty_;
392 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700393
394 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
396 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
397 byte* const stack_args_; // Address of stack arguments in caller's frame.
398 uint32_t gpr_index_; // Index into spilled GPRs.
399 uint32_t fpr_index_; // Index into spilled FPRs.
400 uint32_t stack_index_; // Index into arguments on the stack.
401 // The current type of argument during VisitArguments.
402 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700403 // Does a 64bit parameter straddle the register and stack arguments?
404 bool is_split_long_or_double_;
405};
406
407// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800408class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700409 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700410 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
411 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
412 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700413 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700414
Ian Rogers9758f792014-03-13 09:02:55 -0700415 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700416
417 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800418 ShadowFrame* const sf_;
419 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700420
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700421 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700422};
423
Andreas Gampec200a4a2014-06-16 18:39:09 -0700424void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700425 Primitive::Type type = GetParamPrimitiveType();
426 switch (type) {
427 case Primitive::kPrimLong: // Fall-through.
428 case Primitive::kPrimDouble:
429 if (IsSplitLongOrDouble()) {
430 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
431 } else {
432 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
433 }
434 ++cur_reg_;
435 break;
436 case Primitive::kPrimNot: {
437 StackReference<mirror::Object>* stack_ref =
438 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
439 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
440 }
441 break;
442 case Primitive::kPrimBoolean: // Fall-through.
443 case Primitive::kPrimByte: // Fall-through.
444 case Primitive::kPrimChar: // Fall-through.
445 case Primitive::kPrimShort: // Fall-through.
446 case Primitive::kPrimInt: // Fall-through.
447 case Primitive::kPrimFloat:
448 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
449 break;
450 case Primitive::kPrimVoid:
451 LOG(FATAL) << "UNREACHABLE";
452 break;
453 }
454 ++cur_reg_;
455}
456
Brian Carlstromea46f952013-07-30 01:26:50 -0700457extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700458 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700459 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
460 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
461 // frame.
462 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
463
464 if (method->IsAbstract()) {
465 ThrowAbstractMethodError(method);
466 return 0;
467 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800468 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700469 const char* old_cause = self->StartAssertNoThreadSuspension(
470 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700471 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800472 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700473 uint16_t num_regs = code_item->registers_size_;
474 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700475 // No last shadow coming from quick.
476 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700477 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 uint32_t shorty_len = 0;
479 const char* shorty = method->GetShorty(&shorty_len);
480 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700482 shadow_frame_builder.VisitArguments();
483 // Push a transition back into managed code onto the linked list in thread.
484 ManagedStack fragment;
485 self->PushManagedStackFragment(&fragment);
486 self->PushShadowFrame(shadow_frame);
487 self->EndAssertNoThreadSuspension(old_cause);
488
Ian Rogers6c5cb212014-06-18 16:07:20 -0700489 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700490 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700491 StackHandleScope<1> hs(self);
492 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
493 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800494 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700495 self->PopManagedStackFragment(fragment);
496 return 0;
497 }
498 }
499
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700500 StackHandleScope<1> hs(self);
501 MethodHelper mh(hs.NewHandle(method));
Ian Rogers848871b2013-08-05 10:56:33 -0700502 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
503 // Pop transition.
504 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800505 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700506 return result.GetJ();
507 }
508}
509
510// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
511// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800512class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700513 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700514 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
515 const char* shorty, uint32_t shorty_len,
516 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700517 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700518
Ian Rogers9758f792014-03-13 09:02:55 -0700519 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700520
Ian Rogers9758f792014-03-13 09:02:55 -0700521 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800522
Ian Rogers848871b2013-08-05 10:56:33 -0700523 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700524 ScopedObjectAccessUnchecked* const soa_;
525 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800526 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700527 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700528
Ian Rogers848871b2013-08-05 10:56:33 -0700529 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
530};
531
Ian Rogers9758f792014-03-13 09:02:55 -0700532void BuildQuickArgumentVisitor::Visit() {
533 jvalue val;
534 Primitive::Type type = GetParamPrimitiveType();
535 switch (type) {
536 case Primitive::kPrimNot: {
537 StackReference<mirror::Object>* stack_ref =
538 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
539 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
540 references_.push_back(std::make_pair(val.l, stack_ref));
541 break;
542 }
543 case Primitive::kPrimLong: // Fall-through.
544 case Primitive::kPrimDouble:
545 if (IsSplitLongOrDouble()) {
546 val.j = ReadSplitLongParam();
547 } else {
548 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
549 }
550 break;
551 case Primitive::kPrimBoolean: // Fall-through.
552 case Primitive::kPrimByte: // Fall-through.
553 case Primitive::kPrimChar: // Fall-through.
554 case Primitive::kPrimShort: // Fall-through.
555 case Primitive::kPrimInt: // Fall-through.
556 case Primitive::kPrimFloat:
557 val.i = *reinterpret_cast<jint*>(GetParamAddress());
558 break;
559 case Primitive::kPrimVoid:
560 LOG(FATAL) << "UNREACHABLE";
561 val.j = 0;
562 break;
563 }
564 args_->push_back(val);
565}
566
567void BuildQuickArgumentVisitor::FixupReferences() {
568 // Fixup any references which may have changed.
569 for (const auto& pair : references_) {
570 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700571 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700572 }
573}
574
Ian Rogers848871b2013-08-05 10:56:33 -0700575// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
576// which is responsible for recording callee save registers. We explicitly place into jobjects the
577// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
578// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700579extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700580 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700581 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700583 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
584 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700585 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
586 const char* old_cause =
587 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
588 // Register the top of the managed stack, making stack crawlable.
Jeff Hao0398e172014-07-24 16:26:09 -0700589 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700590 self->SetTopOfStack(sp, 0);
591 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700592 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
593 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700594 self->VerifyStack();
595 // Start new JNI local reference state.
596 JNIEnvExt* env = self->GetJniEnv();
597 ScopedObjectAccessUnchecked soa(env);
598 ScopedJniEnvLocalRefState env_state(env);
599 // Create local ref. copies of proxy method and the receiver.
600 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
601
602 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
604 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700605 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700606 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700607 uint32_t shorty_len = 0;
608 const char* shorty = proxy_method->GetShorty(&shorty_len);
609 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700610
Ian Rogers848871b2013-08-05 10:56:33 -0700611 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700612 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700613 args.erase(args.begin());
614
615 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700616 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700617 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700618 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
619 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
620
621 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
622 // that performs allocations.
623 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700624 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800625 // Restore references which might have moved.
626 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700627 return result.GetJ();
628}
629
630// Read object references held in arguments from quick frames and place in a JNI local references,
631// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800632class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700633 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700634 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
635 const char* shorty, uint32_t shorty_len,
636 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700637 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700638
Ian Rogers9758f792014-03-13 09:02:55 -0700639 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700640
Ian Rogers9758f792014-03-13 09:02:55 -0700641 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700642
643 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700644 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800645 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700646 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
647
Mathieu Chartier590fee92013-09-13 13:46:47 -0700648 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700649};
650
Ian Rogers9758f792014-03-13 09:02:55 -0700651void RememberForGcArgumentVisitor::Visit() {
652 if (IsParamAReference()) {
653 StackReference<mirror::Object>* stack_ref =
654 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
655 jobject reference =
656 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
657 references_.push_back(std::make_pair(reference, stack_ref));
658 }
659}
660
661void RememberForGcArgumentVisitor::FixupReferences() {
662 // Fixup any references which may have changed.
663 for (const auto& pair : references_) {
664 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700665 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700666 }
667}
668
Ian Rogers848871b2013-08-05 10:56:33 -0700669// 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,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700672 Thread* self,
673 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700674 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800675 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700676 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800677 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700678 ScopedObjectAccessUnchecked soa(env);
679 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800680 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700681
682 // Compute details about the called method (avoid GCs)
683 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700684 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700685 InvokeType invoke_type;
686 const DexFile* dex_file;
687 uint32_t dex_method_idx;
688 if (called->IsRuntimeMethod()) {
689 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
690 const DexFile::CodeItem* code;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700691 dex_file = caller->GetDexFile();
692 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700693 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
694 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
695 Instruction::Code instr_code = instr->Opcode();
696 bool is_range;
697 switch (instr_code) {
698 case Instruction::INVOKE_DIRECT:
699 invoke_type = kDirect;
700 is_range = false;
701 break;
702 case Instruction::INVOKE_DIRECT_RANGE:
703 invoke_type = kDirect;
704 is_range = true;
705 break;
706 case Instruction::INVOKE_STATIC:
707 invoke_type = kStatic;
708 is_range = false;
709 break;
710 case Instruction::INVOKE_STATIC_RANGE:
711 invoke_type = kStatic;
712 is_range = true;
713 break;
714 case Instruction::INVOKE_SUPER:
715 invoke_type = kSuper;
716 is_range = false;
717 break;
718 case Instruction::INVOKE_SUPER_RANGE:
719 invoke_type = kSuper;
720 is_range = true;
721 break;
722 case Instruction::INVOKE_VIRTUAL:
723 invoke_type = kVirtual;
724 is_range = false;
725 break;
726 case Instruction::INVOKE_VIRTUAL_RANGE:
727 invoke_type = kVirtual;
728 is_range = true;
729 break;
730 case Instruction::INVOKE_INTERFACE:
731 invoke_type = kInterface;
732 is_range = false;
733 break;
734 case Instruction::INVOKE_INTERFACE_RANGE:
735 invoke_type = kInterface;
736 is_range = true;
737 break;
738 default:
739 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
740 // Avoid used uninitialized warnings.
741 invoke_type = kDirect;
742 is_range = false;
743 }
744 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700745 } else {
746 invoke_type = kStatic;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700747 dex_file = called->GetDexFile();
Ian Rogers848871b2013-08-05 10:56:33 -0700748 dex_method_idx = called->GetDexMethodIndex();
749 }
750 uint32_t shorty_len;
751 const char* shorty =
752 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700753 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700754 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800755 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800756 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700757 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700758 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700759 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700760 mirror::Object* dummy = nullptr;
761 HandleWrapper<mirror::Object> h_receiver(
762 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
763 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700764 }
765 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800766 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700767 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800768 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
769 << PrettyMethod(called) << " " << 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;
Mingyao Yangf4867782014-05-05 11:55:02 -0700773
774 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800775 if (invoke_type == kVirtual) {
776 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
777 } else {
778 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
779 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700780
781 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
782 << PrettyTypeOf(receiver) << " "
783 << invoke_type << " " << orig_called->GetVtableIndex();
784
Ian Rogers83883d72013-10-21 21:07:24 -0700785 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
786 // of the sharpened method.
787 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100788 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700789 } else {
790 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000791 // the caller's dex file. Since we get here only if the original called was a runtime
792 // method, we've got the correct dex_file and a dex_method_idx from above.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700793 DCHECK_EQ(caller->GetDexFile(), dex_file);
794 StackHandleScope<1> hs(self);
795 MethodHelper mh(hs.NewHandle(called));
796 uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700797 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100798 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700799 }
800 }
801 }
Ian Rogers848871b2013-08-05 10:56:33 -0700802 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700803 StackHandleScope<1> hs(soa.Self());
804 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700805 linker->EnsureInitialized(called_class, true, true);
806 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800807 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700808 } else if (called_class->IsInitializing()) {
809 if (invoke_type == kStatic) {
810 // Class is still initializing, go to oat and grab code (trampoline must be left in place
811 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800812 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700813 } else {
814 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800815 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700816 }
817 } else {
818 DCHECK(called_class->IsErroneous());
819 }
820 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800821 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700822 // Fixup any locally saved objects may have moved during a GC.
823 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700824 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700825 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700826 return code;
827}
828
Andreas Gampec147b002014-03-06 18:11:06 -0800829/*
830 * This class uses a couple of observations to unite the different calling conventions through
831 * a few constants.
832 *
833 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
834 * possible alignment.
835 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
836 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
837 * when we have to split things
838 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
839 * and we can use Int handling directly.
840 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
841 * necessary when widening. Also, widening of Ints will take place implicitly, and the
842 * extension should be compatible with Aarch64, which mandates copying the available bits
843 * into LSB and leaving the rest unspecified.
844 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
845 * the stack.
846 * 6) There is only little endian.
847 *
848 *
849 * Actual work is supposed to be done in a delegate of the template type. The interface is as
850 * follows:
851 *
852 * void PushGpr(uintptr_t): Add a value for the next GPR
853 *
854 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
855 * padding, that is, think the architecture is 32b and aligns 64b.
856 *
857 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
858 * split this if necessary. The current state will have aligned, if
859 * necessary.
860 *
861 * void PushStack(uintptr_t): Push a value to the stack.
862 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700863 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700864 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800865 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700866 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800867 *
868 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700869template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800870 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800871#if defined(__arm__)
872 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800873 static constexpr bool kNativeSoftFloatAbi = true;
874 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800875 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
876
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800877 static constexpr size_t kRegistersNeededForLong = 2;
878 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800879 static constexpr bool kMultiRegistersAligned = true;
880 static constexpr bool kMultiRegistersWidened = false;
881 static constexpr bool kAlignLongOnStack = true;
882 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000883#elif defined(__aarch64__)
884 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
885 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
886 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
887
888 static constexpr size_t kRegistersNeededForLong = 1;
889 static constexpr size_t kRegistersNeededForDouble = 1;
890 static constexpr bool kMultiRegistersAligned = false;
891 static constexpr bool kMultiRegistersWidened = false;
892 static constexpr bool kAlignLongOnStack = false;
893 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800894#elif defined(__mips__)
895 // TODO: These are all dummy values!
896 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
897 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
898 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
899
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800900 static constexpr size_t kRegistersNeededForLong = 2;
901 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800902 static constexpr bool kMultiRegistersAligned = true;
903 static constexpr bool kMultiRegistersWidened = true;
904 static constexpr bool kAlignLongOnStack = false;
905 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800906#elif defined(__i386__)
907 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800908 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800909 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
910 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
911
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800912 static constexpr size_t kRegistersNeededForLong = 2;
913 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700914 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800915 static constexpr bool kMultiRegistersWidened = false;
916 static constexpr bool kAlignLongOnStack = false;
917 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800918#elif defined(__x86_64__)
919 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
920 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
921 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
922
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800923 static constexpr size_t kRegistersNeededForLong = 1;
924 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800925 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800926 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800927 static constexpr bool kAlignLongOnStack = false;
928 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800929#else
930#error "Unsupported architecture"
931#endif
932
Andreas Gampec147b002014-03-06 18:11:06 -0800933 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700934 explicit BuildNativeCallFrameStateMachine(T* delegate)
935 : gpr_index_(kNumNativeGprArgs),
936 fpr_index_(kNumNativeFprArgs),
937 stack_entries_(0),
938 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800939 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
940 // the next register is even; counting down is just to make the compiler happy...
941 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
942 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
943 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800944
Andreas Gampec200a4a2014-06-16 18:39:09 -0700945 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800946
947 bool HavePointerGpr() {
948 return gpr_index_ > 0;
949 }
950
Andreas Gampec200a4a2014-06-16 18:39:09 -0700951 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800952 if (HavePointerGpr()) {
953 gpr_index_--;
954 PushGpr(reinterpret_cast<uintptr_t>(val));
955 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700956 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800957 PushStack(reinterpret_cast<uintptr_t>(val));
958 gpr_index_ = 0;
959 }
960 }
961
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700962 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800963 return gpr_index_ > 0;
964 }
965
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700966 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
967 uintptr_t handle = PushHandle(ptr);
968 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800969 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700970 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800971 } else {
972 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700973 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800974 gpr_index_ = 0;
975 }
976 }
977
Andreas Gampec147b002014-03-06 18:11:06 -0800978 bool HaveIntGpr() {
979 return gpr_index_ > 0;
980 }
981
982 void AdvanceInt(uint32_t val) {
983 if (HaveIntGpr()) {
984 gpr_index_--;
985 PushGpr(val);
986 } else {
987 stack_entries_++;
988 PushStack(val);
989 gpr_index_ = 0;
990 }
991 }
992
Andreas Gampec147b002014-03-06 18:11:06 -0800993 bool HaveLongGpr() {
994 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
995 }
996
997 bool LongGprNeedsPadding() {
998 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
999 kAlignLongOnStack && // and when it needs alignment
1000 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1001 }
1002
1003 bool LongStackNeedsPadding() {
1004 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1005 kAlignLongOnStack && // and when it needs 8B alignment
1006 (stack_entries_ & 1) == 1; // counter is odd
1007 }
1008
1009 void AdvanceLong(uint64_t val) {
1010 if (HaveLongGpr()) {
1011 if (LongGprNeedsPadding()) {
1012 PushGpr(0);
1013 gpr_index_--;
1014 }
1015 if (kRegistersNeededForLong == 1) {
1016 PushGpr(static_cast<uintptr_t>(val));
1017 } else {
1018 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1019 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1020 }
1021 gpr_index_ -= kRegistersNeededForLong;
1022 } else {
1023 if (LongStackNeedsPadding()) {
1024 PushStack(0);
1025 stack_entries_++;
1026 }
1027 if (kRegistersNeededForLong == 1) {
1028 PushStack(static_cast<uintptr_t>(val));
1029 stack_entries_++;
1030 } else {
1031 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1032 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1033 stack_entries_ += 2;
1034 }
1035 gpr_index_ = 0;
1036 }
1037 }
1038
Andreas Gampec147b002014-03-06 18:11:06 -08001039 bool HaveFloatFpr() {
1040 return fpr_index_ > 0;
1041 }
1042
Andreas Gampec147b002014-03-06 18:11:06 -08001043 void AdvanceFloat(float val) {
1044 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001045 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001046 } else {
1047 if (HaveFloatFpr()) {
1048 fpr_index_--;
1049 if (kRegistersNeededForDouble == 1) {
1050 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001051 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001052 } else {
1053 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001054 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001055 }
1056 } else {
1057 PushFpr4(val);
1058 }
1059 } else {
1060 stack_entries_++;
1061 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1062 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001063 // Note: We need to jump through those hoops to make the compiler happy.
1064 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1065 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001066 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001067 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001068 }
1069 fpr_index_ = 0;
1070 }
1071 }
1072 }
1073
Andreas Gampec147b002014-03-06 18:11:06 -08001074 bool HaveDoubleFpr() {
1075 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1076 }
1077
1078 bool DoubleFprNeedsPadding() {
1079 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1080 kAlignDoubleOnStack && // and when it needs alignment
1081 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1082 }
1083
1084 bool DoubleStackNeedsPadding() {
1085 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1086 kAlignDoubleOnStack && // and when it needs 8B alignment
1087 (stack_entries_ & 1) == 1; // counter is odd
1088 }
1089
1090 void AdvanceDouble(uint64_t val) {
1091 if (kNativeSoftFloatAbi) {
1092 AdvanceLong(val);
1093 } else {
1094 if (HaveDoubleFpr()) {
1095 if (DoubleFprNeedsPadding()) {
1096 PushFpr4(0);
1097 fpr_index_--;
1098 }
1099 PushFpr8(val);
1100 fpr_index_ -= kRegistersNeededForDouble;
1101 } else {
1102 if (DoubleStackNeedsPadding()) {
1103 PushStack(0);
1104 stack_entries_++;
1105 }
1106 if (kRegistersNeededForDouble == 1) {
1107 PushStack(static_cast<uintptr_t>(val));
1108 stack_entries_++;
1109 } else {
1110 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1111 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1112 stack_entries_ += 2;
1113 }
1114 fpr_index_ = 0;
1115 }
1116 }
1117 }
1118
1119 uint32_t getStackEntries() {
1120 return stack_entries_;
1121 }
1122
1123 uint32_t getNumberOfUsedGprs() {
1124 return kNumNativeGprArgs - gpr_index_;
1125 }
1126
1127 uint32_t getNumberOfUsedFprs() {
1128 return kNumNativeFprArgs - fpr_index_;
1129 }
1130
1131 private:
1132 void PushGpr(uintptr_t val) {
1133 delegate_->PushGpr(val);
1134 }
1135 void PushFpr4(float val) {
1136 delegate_->PushFpr4(val);
1137 }
1138 void PushFpr8(uint64_t val) {
1139 delegate_->PushFpr8(val);
1140 }
1141 void PushStack(uintptr_t val) {
1142 delegate_->PushStack(val);
1143 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001144 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1145 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001146 }
1147
1148 uint32_t gpr_index_; // Number of free GPRs
1149 uint32_t fpr_index_; // Number of free FPRs
1150 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1151 // extended
1152 T* delegate_; // What Push implementation gets called
1153};
1154
Andreas Gampec200a4a2014-06-16 18:39:09 -07001155// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1156// in subclasses.
1157//
1158// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1159// them with handles.
1160class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001161 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001162 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1163
1164 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001165
Andreas Gampec147b002014-03-06 18:11:06 -08001166 uint32_t GetStackSize() {
1167 return num_stack_entries_ * sizeof(uintptr_t);
1168 }
1169
Andreas Gampec200a4a2014-06-16 18:39:09 -07001170 uint8_t* LayoutCallStack(uint8_t* sp8) {
Andreas Gampec147b002014-03-06 18:11:06 -08001171 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001172 // Align by kStackAlignment.
1173 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001174 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001175 }
1176
Andreas Gampec200a4a2014-06-16 18:39:09 -07001177 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) {
1178 // Assumption is OK right now, as we have soft-float arm
1179 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1180 sp8 -= fregs * sizeof(uintptr_t);
1181 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1182 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1183 sp8 -= iregs * sizeof(uintptr_t);
1184 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1185 return sp8;
1186 }
Andreas Gampec147b002014-03-06 18:11:06 -08001187
Andreas Gampec200a4a2014-06-16 18:39:09 -07001188 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
1189 uint32_t** start_fpr) {
1190 // Native call stack.
1191 sp8 = LayoutCallStack(sp8);
1192 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001193
Andreas Gampec200a4a2014-06-16 18:39:09 -07001194 // Put fprs and gprs below.
1195 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001196
Andreas Gampec200a4a2014-06-16 18:39:09 -07001197 // Return the new bottom.
1198 return sp8;
1199 }
1200
1201 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1202 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {}
1203
1204 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1205 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1206
1207 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001208
1209 for (uint32_t i = 1; i < shorty_len; ++i) {
1210 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1211 switch (cur_type_) {
1212 case Primitive::kPrimNot:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001213 sm.AdvanceHandleScope(
1214 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001215 break;
1216
1217 case Primitive::kPrimBoolean:
1218 case Primitive::kPrimByte:
1219 case Primitive::kPrimChar:
1220 case Primitive::kPrimShort:
1221 case Primitive::kPrimInt:
1222 sm.AdvanceInt(0);
1223 break;
1224 case Primitive::kPrimFloat:
1225 sm.AdvanceFloat(0);
1226 break;
1227 case Primitive::kPrimDouble:
1228 sm.AdvanceDouble(0);
1229 break;
1230 case Primitive::kPrimLong:
1231 sm.AdvanceLong(0);
1232 break;
1233 default:
1234 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1235 }
1236 }
1237
1238 num_stack_entries_ = sm.getStackEntries();
1239 }
1240
1241 void PushGpr(uintptr_t /* val */) {
1242 // not optimizing registers, yet
1243 }
1244
1245 void PushFpr4(float /* val */) {
1246 // not optimizing registers, yet
1247 }
1248
1249 void PushFpr8(uint64_t /* val */) {
1250 // not optimizing registers, yet
1251 }
1252
1253 void PushStack(uintptr_t /* val */) {
1254 // counting is already done in the superclass
1255 }
1256
Andreas Gampec200a4a2014-06-16 18:39:09 -07001257 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001258 return reinterpret_cast<uintptr_t>(nullptr);
1259 }
1260
Andreas Gampec200a4a2014-06-16 18:39:09 -07001261 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001262 uint32_t num_stack_entries_;
1263};
1264
Andreas Gampec200a4a2014-06-16 18:39:09 -07001265class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001266 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001267 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001268
Andreas Gampec200a4a2014-06-16 18:39:09 -07001269 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1270 // is at *m = sp. Will update to point to the bottom of the save frame.
1271 //
1272 // Note: assumes ComputeAll() has been run before.
1273 void LayoutCalleeSaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1274 uint32_t* handle_scope_entries)
1275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1276 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001277
Andreas Gampec200a4a2014-06-16 18:39:09 -07001278 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1279
1280 // First, fix up the layout of the callee-save frame.
1281 // We have to squeeze in the HandleScope, and relocate the method pointer.
1282
1283 // "Free" the slot for the method.
1284 sp8 += kPointerSize; // In the callee-save frame we use a full pointer.
1285
1286 // Under the callee saves put handle scope and new method stack reference.
1287 *handle_scope_entries = num_handle_scope_references_;
1288
1289 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1290 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1291
1292 sp8 -= scope_and_method;
1293 // Align by kStackAlignment.
1294 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1295 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1296
1297 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1298 *table = reinterpret_cast<HandleScope*>(sp8_table);
1299 (*table)->SetNumberOfReferences(num_handle_scope_references_);
1300
1301 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1302 uint8_t* method_pointer = sp8;
1303 StackReference<mirror::ArtMethod>* new_method_ref =
1304 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1305 new_method_ref->Assign(method);
1306 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001307 }
1308
Andreas Gampec200a4a2014-06-16 18:39:09 -07001309 // Adds space for the cookie. Note: may leave stack unaligned.
1310 void LayoutCookie(uint8_t** sp) {
1311 // Reference cookie and padding
1312 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001313 }
1314
Andreas Gampec200a4a2014-06-16 18:39:09 -07001315 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1316 // Returns the new bottom. Note: this may be unaligned.
1317 uint8_t* LayoutJNISaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1318 uint32_t* handle_scope_entries)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001319 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001320 // First, fix up the layout of the callee-save frame.
1321 // We have to squeeze in the HandleScope, and relocate the method pointer.
1322 LayoutCalleeSaveFrame(m, sp, table, handle_scope_entries);
1323
1324 // The bottom of the callee-save frame is now where the method is, *m.
1325 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1326
1327 // Add space for cookie.
1328 LayoutCookie(&sp8);
1329
1330 return sp8;
1331 }
1332
1333 // WARNING: After this, *sp won't be pointing to the method anymore!
1334 uint8_t* ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty,
1335 uint32_t shorty_len, HandleScope** table, uint32_t* handle_scope_entries,
1336 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1338 Walk(shorty, shorty_len);
1339
1340 // JNI part.
1341 uint8_t* sp8 = LayoutJNISaveFrame(m, reinterpret_cast<void*>(*m), table, handle_scope_entries);
1342
1343 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1344
1345 // Return the new bottom.
1346 return sp8;
1347 }
1348
1349 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1350
1351 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1352 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1354
1355 private:
1356 uint32_t num_handle_scope_references_;
1357};
1358
1359uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1360 num_handle_scope_references_++;
1361 return reinterpret_cast<uintptr_t>(nullptr);
1362}
1363
1364void ComputeGenericJniFrameSize::WalkHeader(
1365 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1366 // JNIEnv
1367 sm->AdvancePointer(nullptr);
1368
1369 // Class object or this as first argument
1370 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1371}
1372
1373// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1374// the template requirements of BuildGenericJniFrameStateMachine.
1375class FillNativeCall {
1376 public:
1377 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1378 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1379
1380 virtual ~FillNativeCall() {}
1381
1382 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1383 cur_gpr_reg_ = gpr_regs;
1384 cur_fpr_reg_ = fpr_regs;
1385 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001386 }
1387
1388 void PushGpr(uintptr_t val) {
1389 *cur_gpr_reg_ = val;
1390 cur_gpr_reg_++;
1391 }
1392
1393 void PushFpr4(float val) {
1394 *cur_fpr_reg_ = val;
1395 cur_fpr_reg_++;
1396 }
1397
1398 void PushFpr8(uint64_t val) {
1399 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1400 *tmp = val;
1401 cur_fpr_reg_ += 2;
1402 }
1403
1404 void PushStack(uintptr_t val) {
1405 *cur_stack_arg_ = val;
1406 cur_stack_arg_++;
1407 }
1408
Andreas Gampec200a4a2014-06-16 18:39:09 -07001409 virtual uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1410 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1411 return 0U;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001412 }
1413
1414 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001415 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001416 uint32_t* cur_fpr_reg_;
1417 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001418};
Andreas Gampec147b002014-03-06 18:11:06 -08001419
Andreas Gampec200a4a2014-06-16 18:39:09 -07001420// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1421// of transitioning into native code.
1422class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1423 public:
1424 BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static,
1425 const char* shorty, uint32_t shorty_len, Thread* self)
1426 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1427 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1428 ComputeGenericJniFrameSize fsc;
1429 uintptr_t* start_gpr_reg;
1430 uint32_t* start_fpr_reg;
1431 uintptr_t* start_stack_arg;
1432 uint32_t handle_scope_entries;
1433 bottom_of_used_area_ = fsc.ComputeLayout(sp, is_static, shorty, shorty_len, &handle_scope_,
1434 &handle_scope_entries, &start_stack_arg,
1435 &start_gpr_reg, &start_fpr_reg);
1436
1437 handle_scope_->SetNumberOfReferences(handle_scope_entries);
1438 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1439
1440 // jni environment is always first argument
1441 sm_.AdvancePointer(self->GetJniEnv());
1442
1443 if (is_static) {
1444 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1445 }
1446 }
1447
1448 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1449
1450 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1451
1452 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1453 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1454 return handle_scope_->GetHandle(0).GetReference();
1455 }
1456
1457 jobject GetFirstHandleScopeJObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1458 return handle_scope_->GetHandle(0).ToJObject();
1459 }
1460
1461 void* GetBottomOfUsedArea() {
1462 return bottom_of_used_area_;
1463 }
1464
1465 private:
1466 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1467 class FillJniCall FINAL : public FillNativeCall {
1468 public:
1469 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1470 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1471 handle_scope_(handle_scope), cur_entry_(0) {}
1472
1473 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1474
1475 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1476 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1477 handle_scope_ = scope;
1478 cur_entry_ = 0U;
1479 }
1480
1481 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1482 // Initialize padding entries.
1483 size_t expected_slots = handle_scope_->NumberOfReferences();
1484 while (cur_entry_ < expected_slots) {
1485 handle_scope_->GetHandle(cur_entry_++).Assign(nullptr);
1486 }
1487 DCHECK_NE(cur_entry_, 0U);
1488 }
1489
1490 private:
1491 HandleScope* handle_scope_;
1492 size_t cur_entry_;
1493 };
1494
1495 HandleScope* handle_scope_;
1496 FillJniCall jni_call_;
1497 void* bottom_of_used_area_;
1498
1499 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001500
1501 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1502};
1503
Andreas Gampec200a4a2014-06-16 18:39:09 -07001504uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1505 uintptr_t tmp;
1506 Handle<mirror::Object> h = handle_scope_->GetHandle(cur_entry_);
1507 h.Assign(ref);
1508 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1509 cur_entry_++;
1510 return tmp;
1511}
1512
Ian Rogers9758f792014-03-13 09:02:55 -07001513void BuildGenericJniFrameVisitor::Visit() {
1514 Primitive::Type type = GetParamPrimitiveType();
1515 switch (type) {
1516 case Primitive::kPrimLong: {
1517 jlong long_arg;
1518 if (IsSplitLongOrDouble()) {
1519 long_arg = ReadSplitLongParam();
1520 } else {
1521 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1522 }
1523 sm_.AdvanceLong(long_arg);
1524 break;
1525 }
1526 case Primitive::kPrimDouble: {
1527 uint64_t double_arg;
1528 if (IsSplitLongOrDouble()) {
1529 // Read into union so that we don't case to a double.
1530 double_arg = ReadSplitLongParam();
1531 } else {
1532 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1533 }
1534 sm_.AdvanceDouble(double_arg);
1535 break;
1536 }
1537 case Primitive::kPrimNot: {
1538 StackReference<mirror::Object>* stack_ref =
1539 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001540 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001541 break;
1542 }
1543 case Primitive::kPrimFloat:
1544 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1545 break;
1546 case Primitive::kPrimBoolean: // Fall-through.
1547 case Primitive::kPrimByte: // Fall-through.
1548 case Primitive::kPrimChar: // Fall-through.
1549 case Primitive::kPrimShort: // Fall-through.
1550 case Primitive::kPrimInt: // Fall-through.
1551 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1552 break;
1553 case Primitive::kPrimVoid:
1554 LOG(FATAL) << "UNREACHABLE";
1555 break;
1556 }
1557}
1558
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001559void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001560 // Clear out rest of the scope.
1561 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001562 // Install HandleScope.
1563 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001564}
1565
Ian Rogers04c31d22014-07-07 21:44:06 -07001566#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001567extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001568#else
1569extern "C" void* artFindNativeMethod(Thread* self);
1570#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001571
Andreas Gampead615172014-04-04 16:20:13 -07001572uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1573 if (lock != nullptr) {
1574 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1575 } else {
1576 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1577 }
1578}
1579
1580void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1581 if (lock != nullptr) {
1582 JniMethodEndSynchronized(cookie, lock, self);
1583 } else {
1584 JniMethodEnd(cookie, self);
1585 }
1586}
1587
Andreas Gampec147b002014-03-06 18:11:06 -08001588/*
1589 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001590 * Create a HandleScope and call stack and fill a mini stack with values to be pushed to registers.
Andreas Gampec147b002014-03-06 18:11:06 -08001591 * The final element on the stack is a pointer to the native code.
1592 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001593 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001594 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001595 *
Andreas Gampec147b002014-03-06 18:11:06 -08001596 * The return of this function denotes:
1597 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1598 * 2) An error, if the value is negative.
1599 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001600extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1601 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001602 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001603 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001604 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001605 uint32_t shorty_len = 0;
1606 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001607
1608 // Run the visitor.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001609 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), shorty, shorty_len, self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001610 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001611 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001612
Andreas Gampec200a4a2014-06-16 18:39:09 -07001613 // Fix up managed-stack things in Thread.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001614 self->SetTopOfStack(sp, 0);
1615
Ian Rogerse0dcd462014-03-08 15:21:04 -08001616 self->VerifyStack();
1617
Andreas Gampe90546832014-03-12 18:07:19 -07001618 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001619 uint32_t cookie;
1620 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001621 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001622 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001623 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001624 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001625 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001626 }
1627 } else {
1628 cookie = JniMethodStart(self);
1629 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001630 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001631 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001632
Andreas Gampe90546832014-03-12 18:07:19 -07001633 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001634 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001635
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001636 // There are two cases for the content of nativeCode:
1637 // 1) Pointer to the native function.
1638 // 2) Pointer to the trampoline for native code binding.
1639 // In the second case, we need to execute the binding and continue with the actual native function
1640 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001641 DCHECK(nativeCode != nullptr);
1642 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001643#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001644 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001645#else
1646 nativeCode = artFindNativeMethod(self);
1647#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001648
1649 if (nativeCode == nullptr) {
1650 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001651
1652 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001653 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001654 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001655 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1656 } else {
1657 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1658 }
1659
Andreas Gampec200a4a2014-06-16 18:39:09 -07001660 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001661 }
1662 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001663 }
1664
Andreas Gampec200a4a2014-06-16 18:39:09 -07001665 // Return native code addr(lo) and bottom of alloca address(hi).
1666 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1667 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001668}
1669
1670/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001671 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001672 * unlocking.
1673 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001674extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001675 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001677 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001678 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001679 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001680
Andreas Gampead615172014-04-04 16:20:13 -07001681 jobject lock = nullptr;
1682 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001683 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1684 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001685 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001686 }
1687
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001688 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001689
1690 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001691 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001692 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001693 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001694
1695 switch (return_shorty_char) {
1696 case 'F': // Fall-through.
1697 case 'D':
1698 return result_f;
1699 case 'Z':
1700 return result.z;
1701 case 'B':
1702 return result.b;
1703 case 'C':
1704 return result.c;
1705 case 'S':
1706 return result.s;
1707 case 'I':
1708 return result.i;
1709 case 'J':
1710 return result.j;
1711 case 'V':
1712 return 0;
1713 default:
1714 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1715 return 0;
1716 }
1717 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001718}
1719
Andreas Gamped58342c2014-06-05 14:18:08 -07001720// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1721// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001722//
Andreas Gamped58342c2014-06-05 14:18:08 -07001723// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1724// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001725
1726template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001727static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001728 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001729 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001730
1731template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001732static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001733 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001734 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001735 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1736 type);
1737 if (UNLIKELY(method == nullptr)) {
1738 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1739 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1740 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001741 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001742 {
1743 // Remember the args in case a GC happens in FindMethodFromCode.
1744 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1745 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1746 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001747 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1748 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001749 visitor.FixupReferences();
1750 }
1751
1752 if (UNLIKELY(method == NULL)) {
1753 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001754 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001755 }
1756 }
1757 DCHECK(!self->IsExceptionPending());
1758 const void* code = method->GetEntryPointFromQuickCompiledCode();
1759
1760 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001761 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1762 << " location: "
1763 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001764
Andreas Gamped58342c2014-06-05 14:18:08 -07001765 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1766 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001767}
1768
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001769// Explicit artInvokeCommon template function declarations to please analysis tool.
1770#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1771 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001772 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001773 mirror::Object* this_object, \
1774 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001775 Thread* self, \
1776 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001777
1778EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1779EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1780EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1781EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1782EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1783EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1784EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1785EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1786EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1787EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1788#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1789
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001790// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001791extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1792 uint32_t method_idx, mirror::Object* this_object,
1793 mirror::ArtMethod* caller_method, Thread* self,
1794 StackReference<mirror::ArtMethod>* sp)
1795 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1796 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1797 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001798}
1799
Andreas Gampec200a4a2014-06-16 18:39:09 -07001800extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1801 uint32_t method_idx, mirror::Object* this_object,
1802 mirror::ArtMethod* caller_method, Thread* self,
1803 StackReference<mirror::ArtMethod>* sp)
1804 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1805 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1806 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001807}
1808
Andreas Gampec200a4a2014-06-16 18:39:09 -07001809extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1810 uint32_t method_idx, mirror::Object* this_object,
1811 mirror::ArtMethod* caller_method, Thread* self,
1812 StackReference<mirror::ArtMethod>* sp)
1813 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1814 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1815 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001816}
1817
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1819 uint32_t method_idx, mirror::Object* this_object,
1820 mirror::ArtMethod* caller_method, Thread* self,
1821 StackReference<mirror::ArtMethod>* sp)
1822 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1823 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1824 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001825}
1826
Andreas Gampec200a4a2014-06-16 18:39:09 -07001827extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1828 uint32_t method_idx, mirror::Object* this_object,
1829 mirror::ArtMethod* caller_method, Thread* self,
1830 StackReference<mirror::ArtMethod>* sp)
1831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1832 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1833 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001834}
1835
1836// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001837extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001838 mirror::Object* this_object,
1839 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001840 Thread* self,
1841 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001842 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1843 mirror::ArtMethod* method;
1844 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1845 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1846 if (UNLIKELY(method == NULL)) {
1847 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1848 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1849 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001850 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001851 }
1852 } else {
1853 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1854 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001855
1856 // Find the caller PC.
1857 constexpr size_t pc_offset = GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsAndArgs);
1858 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + pc_offset);
1859
1860 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001861 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001862 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001863 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1864 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1865 Instruction::Code instr_code = instr->Opcode();
1866 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1867 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1868 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1869 uint32_t dex_method_idx;
1870 if (instr_code == Instruction::INVOKE_INTERFACE) {
1871 dex_method_idx = instr->VRegB_35c();
1872 } else {
1873 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1874 dex_method_idx = instr->VRegB_3rc();
1875 }
1876
Andreas Gampec200a4a2014-06-16 18:39:09 -07001877 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1878 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001879 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001880 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1881 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001882 {
1883 // Remember the args in case a GC happens in FindMethodFromCode.
1884 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1885 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1886 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001887 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001888 self);
1889 visitor.FixupReferences();
1890 }
1891
1892 if (UNLIKELY(method == nullptr)) {
1893 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001894 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001895 }
1896 }
1897 const void* code = method->GetEntryPointFromQuickCompiledCode();
1898
1899 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001900 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1901 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001902
Andreas Gamped58342c2014-06-05 14:18:08 -07001903 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1904 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001905}
1906
Ian Rogers848871b2013-08-05 10:56:33 -07001907} // namespace art