blob: 2a66f2fe3b5b45e7250456910f02c282559a5bfd [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
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"
30#include "object_utils.h"
31#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033
34namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080038 // Number of bytes for each out register in the caller method's frame.
39 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070040 // Frame size in bytes of a callee-save frame for RefsAndArgs.
41 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
42 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070043#if defined(__arm__)
44 // The callee save frame is pointed to by SP.
45 // | argN | |
46 // | ... | |
47 // | arg4 | |
48 // | arg3 spill | | Caller's frame
49 // | arg2 spill | |
50 // | arg1 spill | |
51 // | Method* | ---
52 // | LR |
53 // | ... | callee saves
54 // | R3 | arg3
55 // | R2 | arg2
56 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080057 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070058 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080059 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
60 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
61 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080062 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
63 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
64 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080065 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000066 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080067 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000068#elif defined(__aarch64__)
69 // The callee save frame is pointed to by SP.
70 // | argN | |
71 // | ... | |
72 // | arg4 | |
73 // | arg3 spill | | Caller's frame
74 // | arg2 spill | |
75 // | arg1 spill | |
76 // | Method* | ---
77 // | LR |
78 // | X28 |
79 // | : |
80 // | X19 |
81 // | X7 |
82 // | : |
83 // | X1 |
84 // | D15 |
85 // | : |
86 // | D0 |
87 // | | padding
88 // | Method* | <- sp
89 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
90 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
91 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Stuart Monteithb95a5342014-03-12 13:32:32 +000093 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
94 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000096 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000097 }
Ian Rogers848871b2013-08-05 10:56:33 -070098#elif defined(__mips__)
99 // The callee save frame is pointed to by SP.
100 // | argN | |
101 // | ... | |
102 // | arg4 | |
103 // | arg3 spill | | Caller's frame
104 // | arg2 spill | |
105 // | arg1 spill | |
106 // | Method* | ---
107 // | RA |
108 // | ... | callee saves
109 // | A3 | arg3
110 // | A2 | arg2
111 // | A1 | arg1
112 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800113 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
114 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
115 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
118 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800119 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000120 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800121 }
Ian Rogers848871b2013-08-05 10:56:33 -0700122#elif defined(__i386__)
123 // The callee save frame is pointed to by SP.
124 // | argN | |
125 // | ... | |
126 // | arg4 | |
127 // | arg3 spill | | Caller's frame
128 // | arg2 spill | |
129 // | arg1 spill | |
130 // | Method* | ---
131 // | Return |
132 // | EBP,ESI,EDI | callee saves
133 // | EBX | arg3
134 // | EDX | arg2
135 // | ECX | arg1
136 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800137 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
138 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
139 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
142 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800143 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000144 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800145 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800147 // The callee save frame is pointed to by SP.
148 // | argN | |
149 // | ... | |
150 // | reg. arg spills | | Caller's frame
151 // | Method* | ---
152 // | Return |
153 // | R15 | callee save
154 // | R14 | callee save
155 // | R13 | callee save
156 // | R12 | callee save
157 // | R9 | arg5
158 // | R8 | arg4
159 // | RSI/R6 | arg1
160 // | RBP/R5 | callee save
161 // | RBX/R3 | callee save
162 // | RDX/R2 | arg2
163 // | RCX/R1 | arg3
164 // | XMM7 | float arg 8
165 // | XMM6 | float arg 7
166 // | XMM5 | float arg 6
167 // | XMM4 | float arg 5
168 // | XMM3 | float arg 4
169 // | XMM2 | float arg 3
170 // | XMM1 | float arg 2
171 // | XMM0 | float arg 1
172 // | Padding |
173 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800174 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700175 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700176 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800180 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
181 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000182 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
183 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
186 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800187 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700188 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
189 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800190 }
191 }
Ian Rogers848871b2013-08-05 10:56:33 -0700192#else
193#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700194#endif
195
Ian Rogers936b37f2014-02-14 00:52:24 -0800196 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700197 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800198 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700199 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800200 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700201 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700202 }
203
Ian Rogers936b37f2014-02-14 00:52:24 -0800204 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700205 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700207 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800208 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700209 return *reinterpret_cast<uintptr_t*>(lr);
210 }
211
Andreas Gampec200a4a2014-06-16 18:39:09 -0700212 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
213 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
214 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
215 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
216 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
217 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
218 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
219 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
220 is_split_long_or_double_(false) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700221
222 virtual ~QuickArgumentVisitor() {}
223
224 virtual void Visit() = 0;
225
Ian Rogers936b37f2014-02-14 00:52:24 -0800226 Primitive::Type GetParamPrimitiveType() const {
227 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700228 }
229
230 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800231 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800232 Primitive::Type type = GetParamPrimitiveType();
233 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800234 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000235 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700237 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 }
239 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800240 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
242 }
243 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700244 }
245
246 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000247 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 return is_split_long_or_double_;
249 } else {
250 return false; // An optimization for when GPR and FPRs are 64bit.
251 }
Ian Rogers848871b2013-08-05 10:56:33 -0700252 }
253
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700255 return GetParamPrimitiveType() == Primitive::kPrimNot;
256 }
257
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700259 Primitive::Type type = GetParamPrimitiveType();
260 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
261 }
262
263 uint64_t ReadSplitLongParam() const {
264 DCHECK(IsSplitLongOrDouble());
265 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
266 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
267 return (low_half & 0xffffffffULL) | (high_half << 32);
268 }
269
270 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700271 // This implementation doesn't support reg-spill area for hard float
272 // ABI targets such as x86_64 and aarch64. So, for those targets whose
273 // 'kQuickSoftFloatAbi' is 'false':
274 // (a) 'stack_args_' should point to the first method's argument
275 // (b) whatever the argument type it is, the 'stack_index_' should
276 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800277 gpr_index_ = 0;
278 fpr_index_ = 0;
279 stack_index_ = 0;
280 if (!is_static_) { // Handle this.
281 cur_type_ = Primitive::kPrimNot;
282 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700283 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700284 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
285 stack_index_++;
286 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800287 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 }
Ian Rogers848871b2013-08-05 10:56:33 -0700290 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
292 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
293 switch (cur_type_) {
294 case Primitive::kPrimNot:
295 case Primitive::kPrimBoolean:
296 case Primitive::kPrimByte:
297 case Primitive::kPrimChar:
298 case Primitive::kPrimShort:
299 case Primitive::kPrimInt:
300 is_split_long_or_double_ = false;
301 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700302 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
303 stack_index_++;
304 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800305 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800307 }
308 break;
309 case Primitive::kPrimFloat:
310 is_split_long_or_double_ = false;
311 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800312 if (kQuickSoftFloatAbi) {
313 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800314 gpr_index_++;
315 } else {
316 stack_index_++;
317 }
318 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800319 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800321 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700322 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800323 }
324 break;
325 case Primitive::kPrimDouble:
326 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800327 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000328 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800329 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800330 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700331 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800332 if (kBytesStackArgLocation == 4) {
333 stack_index_+= 2;
334 } else {
335 CHECK_EQ(kBytesStackArgLocation, 8U);
336 stack_index_++;
337 }
338 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700339 if (gpr_index_ < kNumQuickGprArgs) {
340 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000341 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700342 if (gpr_index_ < kNumQuickGprArgs) {
343 gpr_index_++;
344 } else if (kQuickSoftFloatAbi) {
345 stack_index_++;
346 }
347 }
348 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800349 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000350 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800351 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800352 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800353 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 }
359 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700360 }
361 if (kBytesStackArgLocation == 4) {
362 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700364 CHECK_EQ(kBytesStackArgLocation, 8U);
365 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800366 }
367 }
368 break;
369 default:
370 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
371 }
Ian Rogers848871b2013-08-05 10:56:33 -0700372 }
373 }
374
375 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800376 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
377 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800378 if (kQuickSoftFloatAbi) {
379 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000380 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700381 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800382 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700383 // For now, there is no reg-spill area for the targets with
384 // hard float ABI. So, the offset pointing to the first method's
385 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700386 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700387 }
Ian Rogers848871b2013-08-05 10:56:33 -0700388 }
389
Andreas Gampec200a4a2014-06-16 18:39:09 -0700390 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700391 const bool is_static_;
392 const char* const shorty_;
393 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700394
395 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800396 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
397 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
398 byte* const stack_args_; // Address of stack arguments in caller's frame.
399 uint32_t gpr_index_; // Index into spilled GPRs.
400 uint32_t fpr_index_; // Index into spilled FPRs.
401 uint32_t stack_index_; // Index into arguments on the stack.
402 // The current type of argument during VisitArguments.
403 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700404 // Does a 64bit parameter straddle the register and stack arguments?
405 bool is_split_long_or_double_;
406};
407
408// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800409class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700410 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700411 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
412 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
413 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700414 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700415
Ian Rogers9758f792014-03-13 09:02:55 -0700416 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700417
418 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800419 ShadowFrame* const sf_;
420 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700421
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700422 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700423};
424
Andreas Gampec200a4a2014-06-16 18:39:09 -0700425void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700426 Primitive::Type type = GetParamPrimitiveType();
427 switch (type) {
428 case Primitive::kPrimLong: // Fall-through.
429 case Primitive::kPrimDouble:
430 if (IsSplitLongOrDouble()) {
431 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
432 } else {
433 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
434 }
435 ++cur_reg_;
436 break;
437 case Primitive::kPrimNot: {
438 StackReference<mirror::Object>* stack_ref =
439 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
440 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
441 }
442 break;
443 case Primitive::kPrimBoolean: // Fall-through.
444 case Primitive::kPrimByte: // Fall-through.
445 case Primitive::kPrimChar: // Fall-through.
446 case Primitive::kPrimShort: // Fall-through.
447 case Primitive::kPrimInt: // Fall-through.
448 case Primitive::kPrimFloat:
449 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
450 break;
451 case Primitive::kPrimVoid:
452 LOG(FATAL) << "UNREACHABLE";
453 break;
454 }
455 ++cur_reg_;
456}
457
Brian Carlstromea46f952013-07-30 01:26:50 -0700458extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700459 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
461 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
462 // frame.
463 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
464
465 if (method->IsAbstract()) {
466 ThrowAbstractMethodError(method);
467 return 0;
468 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800469 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700470 const char* old_cause = self->StartAssertNoThreadSuspension(
471 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800473 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700474 uint16_t num_regs = code_item->registers_size_;
475 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700476 // No last shadow coming from quick.
477 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700478 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479 uint32_t shorty_len = 0;
480 const char* shorty = method->GetShorty(&shorty_len);
481 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800482 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700483 shadow_frame_builder.VisitArguments();
484 // Push a transition back into managed code onto the linked list in thread.
485 ManagedStack fragment;
486 self->PushManagedStackFragment(&fragment);
487 self->PushShadowFrame(shadow_frame);
488 self->EndAssertNoThreadSuspension(old_cause);
489
Ian Rogers6c5cb212014-06-18 16:07:20 -0700490 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700491 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 StackHandleScope<1> hs(self);
493 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
494 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800495 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700496 self->PopManagedStackFragment(fragment);
497 return 0;
498 }
499 }
500
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700501 StackHandleScope<1> hs(self);
502 MethodHelper mh(hs.NewHandle(method));
Ian Rogers848871b2013-08-05 10:56:33 -0700503 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
504 // Pop transition.
505 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800506 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700507 return result.GetJ();
508 }
509}
510
511// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
512// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800513class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700514 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700515 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
516 const char* shorty, uint32_t shorty_len,
517 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700518 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700519
Ian Rogers9758f792014-03-13 09:02:55 -0700520 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700521
Ian Rogers9758f792014-03-13 09:02:55 -0700522 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800523
Ian Rogers848871b2013-08-05 10:56:33 -0700524 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700525 ScopedObjectAccessUnchecked* const soa_;
526 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800527 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700528 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700529
Ian Rogers848871b2013-08-05 10:56:33 -0700530 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
531};
532
Ian Rogers9758f792014-03-13 09:02:55 -0700533void BuildQuickArgumentVisitor::Visit() {
534 jvalue val;
535 Primitive::Type type = GetParamPrimitiveType();
536 switch (type) {
537 case Primitive::kPrimNot: {
538 StackReference<mirror::Object>* stack_ref =
539 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
540 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
541 references_.push_back(std::make_pair(val.l, stack_ref));
542 break;
543 }
544 case Primitive::kPrimLong: // Fall-through.
545 case Primitive::kPrimDouble:
546 if (IsSplitLongOrDouble()) {
547 val.j = ReadSplitLongParam();
548 } else {
549 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
550 }
551 break;
552 case Primitive::kPrimBoolean: // Fall-through.
553 case Primitive::kPrimByte: // Fall-through.
554 case Primitive::kPrimChar: // Fall-through.
555 case Primitive::kPrimShort: // Fall-through.
556 case Primitive::kPrimInt: // Fall-through.
557 case Primitive::kPrimFloat:
558 val.i = *reinterpret_cast<jint*>(GetParamAddress());
559 break;
560 case Primitive::kPrimVoid:
561 LOG(FATAL) << "UNREACHABLE";
562 val.j = 0;
563 break;
564 }
565 args_->push_back(val);
566}
567
568void BuildQuickArgumentVisitor::FixupReferences() {
569 // Fixup any references which may have changed.
570 for (const auto& pair : references_) {
571 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700572 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700573 }
574}
575
Ian Rogers848871b2013-08-05 10:56:33 -0700576// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
577// which is responsible for recording callee save registers. We explicitly place into jobjects the
578// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
579// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700580extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700581 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700582 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700583 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700584 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
585 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700586 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
587 const char* old_cause =
588 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
589 // Register the top of the managed stack, making stack crawlable.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700590 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method)
591 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700592 self->SetTopOfStack(sp, 0);
593 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700594 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
595 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700596 self->VerifyStack();
597 // Start new JNI local reference state.
598 JNIEnvExt* env = self->GetJniEnv();
599 ScopedObjectAccessUnchecked soa(env);
600 ScopedJniEnvLocalRefState env_state(env);
601 // Create local ref. copies of proxy method and the receiver.
602 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
603
604 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700605 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
606 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700607 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700608 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700609 uint32_t shorty_len = 0;
610 const char* shorty = proxy_method->GetShorty(&shorty_len);
611 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700612
Ian Rogers848871b2013-08-05 10:56:33 -0700613 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700614 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700615 args.erase(args.begin());
616
617 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700618 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700619 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700620 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
621 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
622
623 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
624 // that performs allocations.
625 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700626 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800627 // Restore references which might have moved.
628 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700629 return result.GetJ();
630}
631
632// Read object references held in arguments from quick frames and place in a JNI local references,
633// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800634class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700635 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700636 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
637 const char* shorty, uint32_t shorty_len,
638 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700639 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700640
Ian Rogers9758f792014-03-13 09:02:55 -0700641 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700642
Ian Rogers9758f792014-03-13 09:02:55 -0700643 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700644
645 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700646 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800647 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700648 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
649
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700651};
652
Ian Rogers9758f792014-03-13 09:02:55 -0700653void RememberForGcArgumentVisitor::Visit() {
654 if (IsParamAReference()) {
655 StackReference<mirror::Object>* stack_ref =
656 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
657 jobject reference =
658 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
659 references_.push_back(std::make_pair(reference, stack_ref));
660 }
661}
662
663void RememberForGcArgumentVisitor::FixupReferences() {
664 // Fixup any references which may have changed.
665 for (const auto& pair : references_) {
666 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700667 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700668 }
669}
670
Ian Rogers848871b2013-08-05 10:56:33 -0700671// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700672extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700673 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700674 Thread* self,
675 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800677 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700678 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800679 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700680 ScopedObjectAccessUnchecked soa(env);
681 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800682 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700683
684 // Compute details about the called method (avoid GCs)
685 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700686 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700687 InvokeType invoke_type;
688 const DexFile* dex_file;
689 uint32_t dex_method_idx;
690 if (called->IsRuntimeMethod()) {
691 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
692 const DexFile::CodeItem* code;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700693 dex_file = caller->GetDexFile();
694 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700695 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
696 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
697 Instruction::Code instr_code = instr->Opcode();
698 bool is_range;
699 switch (instr_code) {
700 case Instruction::INVOKE_DIRECT:
701 invoke_type = kDirect;
702 is_range = false;
703 break;
704 case Instruction::INVOKE_DIRECT_RANGE:
705 invoke_type = kDirect;
706 is_range = true;
707 break;
708 case Instruction::INVOKE_STATIC:
709 invoke_type = kStatic;
710 is_range = false;
711 break;
712 case Instruction::INVOKE_STATIC_RANGE:
713 invoke_type = kStatic;
714 is_range = true;
715 break;
716 case Instruction::INVOKE_SUPER:
717 invoke_type = kSuper;
718 is_range = false;
719 break;
720 case Instruction::INVOKE_SUPER_RANGE:
721 invoke_type = kSuper;
722 is_range = true;
723 break;
724 case Instruction::INVOKE_VIRTUAL:
725 invoke_type = kVirtual;
726 is_range = false;
727 break;
728 case Instruction::INVOKE_VIRTUAL_RANGE:
729 invoke_type = kVirtual;
730 is_range = true;
731 break;
732 case Instruction::INVOKE_INTERFACE:
733 invoke_type = kInterface;
734 is_range = false;
735 break;
736 case Instruction::INVOKE_INTERFACE_RANGE:
737 invoke_type = kInterface;
738 is_range = true;
739 break;
740 default:
741 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
742 // Avoid used uninitialized warnings.
743 invoke_type = kDirect;
744 is_range = false;
745 }
746 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700747 } else {
748 invoke_type = kStatic;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700749 dex_file = called->GetDexFile();
Ian Rogers848871b2013-08-05 10:56:33 -0700750 dex_method_idx = called->GetDexMethodIndex();
751 }
752 uint32_t shorty_len;
753 const char* shorty =
754 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700755 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700756 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800757 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800758 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700759 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700760 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700761 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700762 mirror::Object* dummy = nullptr;
763 HandleWrapper<mirror::Object> h_receiver(
764 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
765 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700766 }
767 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800768 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700769 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800770 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
771 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800772 if (virtual_or_interface) {
773 // Refine called method based on receiver.
774 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700775
776 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800777 if (invoke_type == kVirtual) {
778 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
779 } else {
780 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
781 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700782
783 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
784 << PrettyTypeOf(receiver) << " "
785 << invoke_type << " " << orig_called->GetVtableIndex();
786
Ian Rogers83883d72013-10-21 21:07:24 -0700787 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
788 // of the sharpened method.
789 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100790 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700791 } else {
792 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000793 // the caller's dex file. Since we get here only if the original called was a runtime
794 // method, we've got the correct dex_file and a dex_method_idx from above.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700795 DCHECK_EQ(caller->GetDexFile(), dex_file);
796 StackHandleScope<1> hs(self);
797 MethodHelper mh(hs.NewHandle(called));
798 uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700799 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100800 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700801 }
802 }
803 }
Ian Rogers848871b2013-08-05 10:56:33 -0700804 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700805 StackHandleScope<1> hs(soa.Self());
806 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700807 linker->EnsureInitialized(called_class, true, true);
808 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700810 } else if (called_class->IsInitializing()) {
811 if (invoke_type == kStatic) {
812 // Class is still initializing, go to oat and grab code (trampoline must be left in place
813 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800814 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700815 } else {
816 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800817 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700818 }
819 } else {
820 DCHECK(called_class->IsErroneous());
821 }
822 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800823 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700824 // Fixup any locally saved objects may have moved during a GC.
825 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700826 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700827 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 return code;
829}
830
Andreas Gampec147b002014-03-06 18:11:06 -0800831/*
832 * This class uses a couple of observations to unite the different calling conventions through
833 * a few constants.
834 *
835 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
836 * possible alignment.
837 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
838 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
839 * when we have to split things
840 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
841 * and we can use Int handling directly.
842 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
843 * necessary when widening. Also, widening of Ints will take place implicitly, and the
844 * extension should be compatible with Aarch64, which mandates copying the available bits
845 * into LSB and leaving the rest unspecified.
846 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
847 * the stack.
848 * 6) There is only little endian.
849 *
850 *
851 * Actual work is supposed to be done in a delegate of the template type. The interface is as
852 * follows:
853 *
854 * void PushGpr(uintptr_t): Add a value for the next GPR
855 *
856 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
857 * padding, that is, think the architecture is 32b and aligns 64b.
858 *
859 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
860 * split this if necessary. The current state will have aligned, if
861 * necessary.
862 *
863 * void PushStack(uintptr_t): Push a value to the stack.
864 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700865 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700866 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800867 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700868 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800869 *
870 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700871template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800872 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800873#if defined(__arm__)
874 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800875 static constexpr bool kNativeSoftFloatAbi = true;
876 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800877 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
878
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800879 static constexpr size_t kRegistersNeededForLong = 2;
880 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800881 static constexpr bool kMultiRegistersAligned = true;
882 static constexpr bool kMultiRegistersWidened = false;
883 static constexpr bool kAlignLongOnStack = true;
884 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000885#elif defined(__aarch64__)
886 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
887 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
888 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
889
890 static constexpr size_t kRegistersNeededForLong = 1;
891 static constexpr size_t kRegistersNeededForDouble = 1;
892 static constexpr bool kMultiRegistersAligned = false;
893 static constexpr bool kMultiRegistersWidened = false;
894 static constexpr bool kAlignLongOnStack = false;
895 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800896#elif defined(__mips__)
897 // TODO: These are all dummy values!
898 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
899 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
900 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
901
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800902 static constexpr size_t kRegistersNeededForLong = 2;
903 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800904 static constexpr bool kMultiRegistersAligned = true;
905 static constexpr bool kMultiRegistersWidened = true;
906 static constexpr bool kAlignLongOnStack = false;
907 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800908#elif defined(__i386__)
909 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800910 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800911 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
912 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
913
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800914 static constexpr size_t kRegistersNeededForLong = 2;
915 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700916 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800917 static constexpr bool kMultiRegistersWidened = false;
918 static constexpr bool kAlignLongOnStack = false;
919 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800920#elif defined(__x86_64__)
921 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
922 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
923 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
924
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800925 static constexpr size_t kRegistersNeededForLong = 1;
926 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800927 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800928 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800929 static constexpr bool kAlignLongOnStack = false;
930 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800931#else
932#error "Unsupported architecture"
933#endif
934
Andreas Gampec147b002014-03-06 18:11:06 -0800935 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700936 explicit BuildNativeCallFrameStateMachine(T* delegate)
937 : gpr_index_(kNumNativeGprArgs),
938 fpr_index_(kNumNativeFprArgs),
939 stack_entries_(0),
940 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800941 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
942 // the next register is even; counting down is just to make the compiler happy...
943 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
944 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
945 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800946
Andreas Gampec200a4a2014-06-16 18:39:09 -0700947 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800948
949 bool HavePointerGpr() {
950 return gpr_index_ > 0;
951 }
952
Andreas Gampec200a4a2014-06-16 18:39:09 -0700953 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800954 if (HavePointerGpr()) {
955 gpr_index_--;
956 PushGpr(reinterpret_cast<uintptr_t>(val));
957 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700958 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800959 PushStack(reinterpret_cast<uintptr_t>(val));
960 gpr_index_ = 0;
961 }
962 }
963
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700964 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800965 return gpr_index_ > 0;
966 }
967
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700968 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
969 uintptr_t handle = PushHandle(ptr);
970 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800971 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700972 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800973 } else {
974 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700975 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800976 gpr_index_ = 0;
977 }
978 }
979
Andreas Gampec147b002014-03-06 18:11:06 -0800980 bool HaveIntGpr() {
981 return gpr_index_ > 0;
982 }
983
984 void AdvanceInt(uint32_t val) {
985 if (HaveIntGpr()) {
986 gpr_index_--;
987 PushGpr(val);
988 } else {
989 stack_entries_++;
990 PushStack(val);
991 gpr_index_ = 0;
992 }
993 }
994
Andreas Gampec147b002014-03-06 18:11:06 -0800995 bool HaveLongGpr() {
996 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
997 }
998
999 bool LongGprNeedsPadding() {
1000 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1001 kAlignLongOnStack && // and when it needs alignment
1002 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1003 }
1004
1005 bool LongStackNeedsPadding() {
1006 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1007 kAlignLongOnStack && // and when it needs 8B alignment
1008 (stack_entries_ & 1) == 1; // counter is odd
1009 }
1010
1011 void AdvanceLong(uint64_t val) {
1012 if (HaveLongGpr()) {
1013 if (LongGprNeedsPadding()) {
1014 PushGpr(0);
1015 gpr_index_--;
1016 }
1017 if (kRegistersNeededForLong == 1) {
1018 PushGpr(static_cast<uintptr_t>(val));
1019 } else {
1020 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1021 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1022 }
1023 gpr_index_ -= kRegistersNeededForLong;
1024 } else {
1025 if (LongStackNeedsPadding()) {
1026 PushStack(0);
1027 stack_entries_++;
1028 }
1029 if (kRegistersNeededForLong == 1) {
1030 PushStack(static_cast<uintptr_t>(val));
1031 stack_entries_++;
1032 } else {
1033 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1034 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1035 stack_entries_ += 2;
1036 }
1037 gpr_index_ = 0;
1038 }
1039 }
1040
Andreas Gampec147b002014-03-06 18:11:06 -08001041 bool HaveFloatFpr() {
1042 return fpr_index_ > 0;
1043 }
1044
Andreas Gampec147b002014-03-06 18:11:06 -08001045 void AdvanceFloat(float val) {
1046 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001047 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001048 } else {
1049 if (HaveFloatFpr()) {
1050 fpr_index_--;
1051 if (kRegistersNeededForDouble == 1) {
1052 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001053 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001054 } else {
1055 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001056 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001057 }
1058 } else {
1059 PushFpr4(val);
1060 }
1061 } else {
1062 stack_entries_++;
1063 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1064 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001065 // Note: We need to jump through those hoops to make the compiler happy.
1066 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1067 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001068 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001069 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001070 }
1071 fpr_index_ = 0;
1072 }
1073 }
1074 }
1075
Andreas Gampec147b002014-03-06 18:11:06 -08001076 bool HaveDoubleFpr() {
1077 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1078 }
1079
1080 bool DoubleFprNeedsPadding() {
1081 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1082 kAlignDoubleOnStack && // and when it needs alignment
1083 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1084 }
1085
1086 bool DoubleStackNeedsPadding() {
1087 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1088 kAlignDoubleOnStack && // and when it needs 8B alignment
1089 (stack_entries_ & 1) == 1; // counter is odd
1090 }
1091
1092 void AdvanceDouble(uint64_t val) {
1093 if (kNativeSoftFloatAbi) {
1094 AdvanceLong(val);
1095 } else {
1096 if (HaveDoubleFpr()) {
1097 if (DoubleFprNeedsPadding()) {
1098 PushFpr4(0);
1099 fpr_index_--;
1100 }
1101 PushFpr8(val);
1102 fpr_index_ -= kRegistersNeededForDouble;
1103 } else {
1104 if (DoubleStackNeedsPadding()) {
1105 PushStack(0);
1106 stack_entries_++;
1107 }
1108 if (kRegistersNeededForDouble == 1) {
1109 PushStack(static_cast<uintptr_t>(val));
1110 stack_entries_++;
1111 } else {
1112 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1113 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1114 stack_entries_ += 2;
1115 }
1116 fpr_index_ = 0;
1117 }
1118 }
1119 }
1120
1121 uint32_t getStackEntries() {
1122 return stack_entries_;
1123 }
1124
1125 uint32_t getNumberOfUsedGprs() {
1126 return kNumNativeGprArgs - gpr_index_;
1127 }
1128
1129 uint32_t getNumberOfUsedFprs() {
1130 return kNumNativeFprArgs - fpr_index_;
1131 }
1132
1133 private:
1134 void PushGpr(uintptr_t val) {
1135 delegate_->PushGpr(val);
1136 }
1137 void PushFpr4(float val) {
1138 delegate_->PushFpr4(val);
1139 }
1140 void PushFpr8(uint64_t val) {
1141 delegate_->PushFpr8(val);
1142 }
1143 void PushStack(uintptr_t val) {
1144 delegate_->PushStack(val);
1145 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001146 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1147 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001148 }
1149
1150 uint32_t gpr_index_; // Number of free GPRs
1151 uint32_t fpr_index_; // Number of free FPRs
1152 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1153 // extended
1154 T* delegate_; // What Push implementation gets called
1155};
1156
Andreas Gampec200a4a2014-06-16 18:39:09 -07001157// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1158// in subclasses.
1159//
1160// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1161// them with handles.
1162class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001163 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001164 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1165
1166 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001167
Andreas Gampec147b002014-03-06 18:11:06 -08001168 uint32_t GetStackSize() {
1169 return num_stack_entries_ * sizeof(uintptr_t);
1170 }
1171
Andreas Gampec200a4a2014-06-16 18:39:09 -07001172 uint8_t* LayoutCallStack(uint8_t* sp8) {
Andreas Gampec147b002014-03-06 18:11:06 -08001173 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001174 // Align by kStackAlignment.
1175 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001176 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001177 }
1178
Andreas Gampec200a4a2014-06-16 18:39:09 -07001179 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) {
1180 // Assumption is OK right now, as we have soft-float arm
1181 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1182 sp8 -= fregs * sizeof(uintptr_t);
1183 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1184 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1185 sp8 -= iregs * sizeof(uintptr_t);
1186 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1187 return sp8;
1188 }
Andreas Gampec147b002014-03-06 18:11:06 -08001189
Andreas Gampec200a4a2014-06-16 18:39:09 -07001190 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
1191 uint32_t** start_fpr) {
1192 // Native call stack.
1193 sp8 = LayoutCallStack(sp8);
1194 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001195
Andreas Gampec200a4a2014-06-16 18:39:09 -07001196 // Put fprs and gprs below.
1197 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001198
Andreas Gampec200a4a2014-06-16 18:39:09 -07001199 // Return the new bottom.
1200 return sp8;
1201 }
1202
1203 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {}
1205
1206 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1207 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1208
1209 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001210
1211 for (uint32_t i = 1; i < shorty_len; ++i) {
1212 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1213 switch (cur_type_) {
1214 case Primitive::kPrimNot:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001215 sm.AdvanceHandleScope(
1216 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001217 break;
1218
1219 case Primitive::kPrimBoolean:
1220 case Primitive::kPrimByte:
1221 case Primitive::kPrimChar:
1222 case Primitive::kPrimShort:
1223 case Primitive::kPrimInt:
1224 sm.AdvanceInt(0);
1225 break;
1226 case Primitive::kPrimFloat:
1227 sm.AdvanceFloat(0);
1228 break;
1229 case Primitive::kPrimDouble:
1230 sm.AdvanceDouble(0);
1231 break;
1232 case Primitive::kPrimLong:
1233 sm.AdvanceLong(0);
1234 break;
1235 default:
1236 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1237 }
1238 }
1239
1240 num_stack_entries_ = sm.getStackEntries();
1241 }
1242
1243 void PushGpr(uintptr_t /* val */) {
1244 // not optimizing registers, yet
1245 }
1246
1247 void PushFpr4(float /* val */) {
1248 // not optimizing registers, yet
1249 }
1250
1251 void PushFpr8(uint64_t /* val */) {
1252 // not optimizing registers, yet
1253 }
1254
1255 void PushStack(uintptr_t /* val */) {
1256 // counting is already done in the superclass
1257 }
1258
Andreas Gampec200a4a2014-06-16 18:39:09 -07001259 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001260 return reinterpret_cast<uintptr_t>(nullptr);
1261 }
1262
Andreas Gampec200a4a2014-06-16 18:39:09 -07001263 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001264 uint32_t num_stack_entries_;
1265};
1266
Andreas Gampec200a4a2014-06-16 18:39:09 -07001267class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001268 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001269 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001270
Andreas Gampec200a4a2014-06-16 18:39:09 -07001271 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1272 // is at *m = sp. Will update to point to the bottom of the save frame.
1273 //
1274 // Note: assumes ComputeAll() has been run before.
1275 void LayoutCalleeSaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1276 uint32_t* handle_scope_entries)
1277 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1278 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001279
Andreas Gampec200a4a2014-06-16 18:39:09 -07001280 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1281
1282 // First, fix up the layout of the callee-save frame.
1283 // We have to squeeze in the HandleScope, and relocate the method pointer.
1284
1285 // "Free" the slot for the method.
1286 sp8 += kPointerSize; // In the callee-save frame we use a full pointer.
1287
1288 // Under the callee saves put handle scope and new method stack reference.
1289 *handle_scope_entries = num_handle_scope_references_;
1290
1291 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1292 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1293
1294 sp8 -= scope_and_method;
1295 // Align by kStackAlignment.
1296 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1297 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1298
1299 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1300 *table = reinterpret_cast<HandleScope*>(sp8_table);
1301 (*table)->SetNumberOfReferences(num_handle_scope_references_);
1302
1303 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1304 uint8_t* method_pointer = sp8;
1305 StackReference<mirror::ArtMethod>* new_method_ref =
1306 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1307 new_method_ref->Assign(method);
1308 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001309 }
1310
Andreas Gampec200a4a2014-06-16 18:39:09 -07001311 // Adds space for the cookie. Note: may leave stack unaligned.
1312 void LayoutCookie(uint8_t** sp) {
1313 // Reference cookie and padding
1314 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001315 }
1316
Andreas Gampec200a4a2014-06-16 18:39:09 -07001317 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1318 // Returns the new bottom. Note: this may be unaligned.
1319 uint8_t* LayoutJNISaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1320 uint32_t* handle_scope_entries)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001321 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001322 // First, fix up the layout of the callee-save frame.
1323 // We have to squeeze in the HandleScope, and relocate the method pointer.
1324 LayoutCalleeSaveFrame(m, sp, table, handle_scope_entries);
1325
1326 // The bottom of the callee-save frame is now where the method is, *m.
1327 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1328
1329 // Add space for cookie.
1330 LayoutCookie(&sp8);
1331
1332 return sp8;
1333 }
1334
1335 // WARNING: After this, *sp won't be pointing to the method anymore!
1336 uint8_t* ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty,
1337 uint32_t shorty_len, HandleScope** table, uint32_t* handle_scope_entries,
1338 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1339 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1340 Walk(shorty, shorty_len);
1341
1342 // JNI part.
1343 uint8_t* sp8 = LayoutJNISaveFrame(m, reinterpret_cast<void*>(*m), table, handle_scope_entries);
1344
1345 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1346
1347 // Return the new bottom.
1348 return sp8;
1349 }
1350
1351 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1352
1353 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1354 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1355 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1356
1357 private:
1358 uint32_t num_handle_scope_references_;
1359};
1360
1361uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1362 num_handle_scope_references_++;
1363 return reinterpret_cast<uintptr_t>(nullptr);
1364}
1365
1366void ComputeGenericJniFrameSize::WalkHeader(
1367 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1368 // JNIEnv
1369 sm->AdvancePointer(nullptr);
1370
1371 // Class object or this as first argument
1372 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1373}
1374
1375// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1376// the template requirements of BuildGenericJniFrameStateMachine.
1377class FillNativeCall {
1378 public:
1379 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1380 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1381
1382 virtual ~FillNativeCall() {}
1383
1384 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1385 cur_gpr_reg_ = gpr_regs;
1386 cur_fpr_reg_ = fpr_regs;
1387 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001388 }
1389
1390 void PushGpr(uintptr_t val) {
1391 *cur_gpr_reg_ = val;
1392 cur_gpr_reg_++;
1393 }
1394
1395 void PushFpr4(float val) {
1396 *cur_fpr_reg_ = val;
1397 cur_fpr_reg_++;
1398 }
1399
1400 void PushFpr8(uint64_t val) {
1401 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1402 *tmp = val;
1403 cur_fpr_reg_ += 2;
1404 }
1405
1406 void PushStack(uintptr_t val) {
1407 *cur_stack_arg_ = val;
1408 cur_stack_arg_++;
1409 }
1410
Andreas Gampec200a4a2014-06-16 18:39:09 -07001411 virtual uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1412 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1413 return 0U;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001414 }
1415
1416 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001417 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001418 uint32_t* cur_fpr_reg_;
1419 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001420};
Andreas Gampec147b002014-03-06 18:11:06 -08001421
Andreas Gampec200a4a2014-06-16 18:39:09 -07001422// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1423// of transitioning into native code.
1424class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1425 public:
1426 BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static,
1427 const char* shorty, uint32_t shorty_len, Thread* self)
1428 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1429 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1430 ComputeGenericJniFrameSize fsc;
1431 uintptr_t* start_gpr_reg;
1432 uint32_t* start_fpr_reg;
1433 uintptr_t* start_stack_arg;
1434 uint32_t handle_scope_entries;
1435 bottom_of_used_area_ = fsc.ComputeLayout(sp, is_static, shorty, shorty_len, &handle_scope_,
1436 &handle_scope_entries, &start_stack_arg,
1437 &start_gpr_reg, &start_fpr_reg);
1438
1439 handle_scope_->SetNumberOfReferences(handle_scope_entries);
1440 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1441
1442 // jni environment is always first argument
1443 sm_.AdvancePointer(self->GetJniEnv());
1444
1445 if (is_static) {
1446 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1447 }
1448 }
1449
1450 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1451
1452 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1453
1454 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1455 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1456 return handle_scope_->GetHandle(0).GetReference();
1457 }
1458
1459 jobject GetFirstHandleScopeJObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1460 return handle_scope_->GetHandle(0).ToJObject();
1461 }
1462
1463 void* GetBottomOfUsedArea() {
1464 return bottom_of_used_area_;
1465 }
1466
1467 private:
1468 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1469 class FillJniCall FINAL : public FillNativeCall {
1470 public:
1471 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1472 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1473 handle_scope_(handle_scope), cur_entry_(0) {}
1474
1475 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1476
1477 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1478 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1479 handle_scope_ = scope;
1480 cur_entry_ = 0U;
1481 }
1482
1483 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1484 // Initialize padding entries.
1485 size_t expected_slots = handle_scope_->NumberOfReferences();
1486 while (cur_entry_ < expected_slots) {
1487 handle_scope_->GetHandle(cur_entry_++).Assign(nullptr);
1488 }
1489 DCHECK_NE(cur_entry_, 0U);
1490 }
1491
1492 private:
1493 HandleScope* handle_scope_;
1494 size_t cur_entry_;
1495 };
1496
1497 HandleScope* handle_scope_;
1498 FillJniCall jni_call_;
1499 void* bottom_of_used_area_;
1500
1501 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001502
1503 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1504};
1505
Andreas Gampec200a4a2014-06-16 18:39:09 -07001506uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1507 uintptr_t tmp;
1508 Handle<mirror::Object> h = handle_scope_->GetHandle(cur_entry_);
1509 h.Assign(ref);
1510 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1511 cur_entry_++;
1512 return tmp;
1513}
1514
Ian Rogers9758f792014-03-13 09:02:55 -07001515void BuildGenericJniFrameVisitor::Visit() {
1516 Primitive::Type type = GetParamPrimitiveType();
1517 switch (type) {
1518 case Primitive::kPrimLong: {
1519 jlong long_arg;
1520 if (IsSplitLongOrDouble()) {
1521 long_arg = ReadSplitLongParam();
1522 } else {
1523 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1524 }
1525 sm_.AdvanceLong(long_arg);
1526 break;
1527 }
1528 case Primitive::kPrimDouble: {
1529 uint64_t double_arg;
1530 if (IsSplitLongOrDouble()) {
1531 // Read into union so that we don't case to a double.
1532 double_arg = ReadSplitLongParam();
1533 } else {
1534 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1535 }
1536 sm_.AdvanceDouble(double_arg);
1537 break;
1538 }
1539 case Primitive::kPrimNot: {
1540 StackReference<mirror::Object>* stack_ref =
1541 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001542 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001543 break;
1544 }
1545 case Primitive::kPrimFloat:
1546 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1547 break;
1548 case Primitive::kPrimBoolean: // Fall-through.
1549 case Primitive::kPrimByte: // Fall-through.
1550 case Primitive::kPrimChar: // Fall-through.
1551 case Primitive::kPrimShort: // Fall-through.
1552 case Primitive::kPrimInt: // Fall-through.
1553 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1554 break;
1555 case Primitive::kPrimVoid:
1556 LOG(FATAL) << "UNREACHABLE";
1557 break;
1558 }
1559}
1560
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001561void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001562 // Clear out rest of the scope.
1563 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001564 // Install HandleScope.
1565 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001566}
1567
Ian Rogers04c31d22014-07-07 21:44:06 -07001568#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001569extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001570#else
1571extern "C" void* artFindNativeMethod(Thread* self);
1572#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001573
Andreas Gampead615172014-04-04 16:20:13 -07001574uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1575 if (lock != nullptr) {
1576 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1577 } else {
1578 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1579 }
1580}
1581
1582void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1583 if (lock != nullptr) {
1584 JniMethodEndSynchronized(cookie, lock, self);
1585 } else {
1586 JniMethodEnd(cookie, self);
1587 }
1588}
1589
Andreas Gampec147b002014-03-06 18:11:06 -08001590/*
1591 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001592 * 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 -08001593 * The final element on the stack is a pointer to the native code.
1594 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001595 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001596 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001597 *
Andreas Gampec147b002014-03-06 18:11:06 -08001598 * The return of this function denotes:
1599 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1600 * 2) An error, if the value is negative.
1601 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001602extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1603 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001605 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001606 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001607 uint32_t shorty_len = 0;
1608 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001609
1610 // Run the visitor.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001611 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), shorty, shorty_len, self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001612 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001613 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001614
Andreas Gampec200a4a2014-06-16 18:39:09 -07001615 // Fix up managed-stack things in Thread.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001616 self->SetTopOfStack(sp, 0);
1617
Ian Rogerse0dcd462014-03-08 15:21:04 -08001618 self->VerifyStack();
1619
Andreas Gampe90546832014-03-12 18:07:19 -07001620 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001621 uint32_t cookie;
1622 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001623 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001624 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001625 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001626 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001627 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001628 }
1629 } else {
1630 cookie = JniMethodStart(self);
1631 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001632 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001633 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001634
Andreas Gampe90546832014-03-12 18:07:19 -07001635 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001636 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001637
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001638 // There are two cases for the content of nativeCode:
1639 // 1) Pointer to the native function.
1640 // 2) Pointer to the trampoline for native code binding.
1641 // In the second case, we need to execute the binding and continue with the actual native function
1642 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001643 DCHECK(nativeCode != nullptr);
1644 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001645#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001646 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001647#else
1648 nativeCode = artFindNativeMethod(self);
1649#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001650
1651 if (nativeCode == nullptr) {
1652 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001653
1654 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001655 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001656 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001657 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1658 } else {
1659 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1660 }
1661
Andreas Gampec200a4a2014-06-16 18:39:09 -07001662 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001663 }
1664 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001665 }
1666
Andreas Gampec200a4a2014-06-16 18:39:09 -07001667 // Return native code addr(lo) and bottom of alloca address(hi).
1668 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1669 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001670}
1671
1672/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001673 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001674 * unlocking.
1675 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001677 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001678 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001679 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001680 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001681 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001682
Andreas Gampead615172014-04-04 16:20:13 -07001683 jobject lock = nullptr;
1684 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001685 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1686 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001687 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001688 }
1689
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001690 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001691
1692 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001693 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001694 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001695 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001696
1697 switch (return_shorty_char) {
1698 case 'F': // Fall-through.
1699 case 'D':
1700 return result_f;
1701 case 'Z':
1702 return result.z;
1703 case 'B':
1704 return result.b;
1705 case 'C':
1706 return result.c;
1707 case 'S':
1708 return result.s;
1709 case 'I':
1710 return result.i;
1711 case 'J':
1712 return result.j;
1713 case 'V':
1714 return 0;
1715 default:
1716 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1717 return 0;
1718 }
1719 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001720}
1721
Andreas Gamped58342c2014-06-05 14:18:08 -07001722// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1723// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001724//
Andreas Gamped58342c2014-06-05 14:18:08 -07001725// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1726// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001727
1728template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001729static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001730 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001731 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001732
1733template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001734static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001735 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001736 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001737 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1738 type);
1739 if (UNLIKELY(method == nullptr)) {
1740 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1741 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1742 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001743 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001744 {
1745 // Remember the args in case a GC happens in FindMethodFromCode.
1746 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1747 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1748 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001749 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1750 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001751 visitor.FixupReferences();
1752 }
1753
1754 if (UNLIKELY(method == NULL)) {
1755 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001756 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001757 }
1758 }
1759 DCHECK(!self->IsExceptionPending());
1760 const void* code = method->GetEntryPointFromQuickCompiledCode();
1761
1762 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001763 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1764 << " location: "
1765 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001766
Andreas Gamped58342c2014-06-05 14:18:08 -07001767 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1768 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001769}
1770
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001771// Explicit artInvokeCommon template function declarations to please analysis tool.
1772#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1773 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001774 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001775 mirror::Object* this_object, \
1776 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001777 Thread* self, \
1778 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001779
1780EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1781EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1782EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1783EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1784EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1785EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1786EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1787EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1788EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1789EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1790#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1791
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001792// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1794 uint32_t method_idx, mirror::Object* this_object,
1795 mirror::ArtMethod* caller_method, Thread* self,
1796 StackReference<mirror::ArtMethod>* sp)
1797 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1798 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1799 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001800}
1801
Andreas Gampec200a4a2014-06-16 18:39:09 -07001802extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1803 uint32_t method_idx, mirror::Object* this_object,
1804 mirror::ArtMethod* caller_method, Thread* self,
1805 StackReference<mirror::ArtMethod>* sp)
1806 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1807 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1808 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001809}
1810
Andreas Gampec200a4a2014-06-16 18:39:09 -07001811extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1812 uint32_t method_idx, mirror::Object* this_object,
1813 mirror::ArtMethod* caller_method, Thread* self,
1814 StackReference<mirror::ArtMethod>* sp)
1815 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1816 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1817 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001818}
1819
Andreas Gampec200a4a2014-06-16 18:39:09 -07001820extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1821 uint32_t method_idx, mirror::Object* this_object,
1822 mirror::ArtMethod* caller_method, Thread* self,
1823 StackReference<mirror::ArtMethod>* sp)
1824 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1825 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1826 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001827}
1828
Andreas Gampec200a4a2014-06-16 18:39:09 -07001829extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1830 uint32_t method_idx, mirror::Object* this_object,
1831 mirror::ArtMethod* caller_method, Thread* self,
1832 StackReference<mirror::ArtMethod>* sp)
1833 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1834 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1835 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001836}
1837
1838// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001839extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001840 mirror::Object* this_object,
1841 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001842 Thread* self,
1843 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001844 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1845 mirror::ArtMethod* method;
1846 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1847 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1848 if (UNLIKELY(method == NULL)) {
1849 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1850 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1851 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001852 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001853 }
1854 } else {
1855 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1856 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001857
1858 // Find the caller PC.
1859 constexpr size_t pc_offset = GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsAndArgs);
1860 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + pc_offset);
1861
1862 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001863 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001864 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001865 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1866 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1867 Instruction::Code instr_code = instr->Opcode();
1868 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1869 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1870 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1871 uint32_t dex_method_idx;
1872 if (instr_code == Instruction::INVOKE_INTERFACE) {
1873 dex_method_idx = instr->VRegB_35c();
1874 } else {
1875 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1876 dex_method_idx = instr->VRegB_3rc();
1877 }
1878
Andreas Gampec200a4a2014-06-16 18:39:09 -07001879 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1880 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001881 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001882 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1883 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001884 {
1885 // Remember the args in case a GC happens in FindMethodFromCode.
1886 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1887 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1888 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001889 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001890 self);
1891 visitor.FixupReferences();
1892 }
1893
1894 if (UNLIKELY(method == nullptr)) {
1895 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001896 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001897 }
1898 }
1899 const void* code = method->GetEntryPointFromQuickCompiledCode();
1900
1901 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001902 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1903 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001904
Andreas Gamped58342c2014-06-05 14:18:08 -07001905 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1906 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001907}
1908
Ian Rogers848871b2013-08-05 10:56:33 -07001909} // namespace art