blob: 403313af02fa1256584d7c9bdd38c78fefee221f [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Andreas Gamped58342c2014-06-05 14:18:08 -070023#include "instruction_set.h"
Ian Rogers848871b2013-08-05 10:56:33 -070024#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070025#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070026#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070027#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070028#include "mirror/object-inl.h"
29#include "mirror/object_array-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070031#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032
33namespace art {
34
35// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
36class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080037 // Number of bytes for each out register in the caller method's frame.
38 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070039 // Frame size in bytes of a callee-save frame for RefsAndArgs.
40 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
41 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070042#if defined(__arm__)
43 // The callee save frame is pointed to by SP.
44 // | argN | |
45 // | ... | |
46 // | arg4 | |
47 // | arg3 spill | | Caller's frame
48 // | arg2 spill | |
49 // | arg1 spill | |
50 // | Method* | ---
51 // | LR |
52 // | ... | callee saves
53 // | R3 | arg3
54 // | R2 | arg2
55 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080056 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070057 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080058 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
59 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
60 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +080061 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
62 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
63 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
64 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
65 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
66 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080067 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000068 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080069 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000070#elif defined(__aarch64__)
71 // The callee save frame is pointed to by SP.
72 // | argN | |
73 // | ... | |
74 // | arg4 | |
75 // | arg3 spill | | Caller's frame
76 // | arg2 spill | |
77 // | arg1 spill | |
78 // | Method* | ---
79 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080080 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000081 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080082 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000083 // | X7 |
84 // | : |
85 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080086 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000087 // | : |
88 // | D0 |
89 // | | padding
90 // | Method* | <- sp
91 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
92 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
93 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +080094 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
95 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
96 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
97 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
98 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
99 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000100 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000101 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000102 }
Ian Rogers848871b2013-08-05 10:56:33 -0700103#elif defined(__mips__)
104 // The callee save frame is pointed to by SP.
105 // | argN | |
106 // | ... | |
107 // | arg4 | |
108 // | arg3 spill | | Caller's frame
109 // | arg2 spill | |
110 // | arg1 spill | |
111 // | Method* | ---
112 // | RA |
113 // | ... | callee saves
114 // | A3 | arg3
115 // | A2 | arg2
116 // | A1 | arg1
117 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800118 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
119 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
120 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800121 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
122 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
123 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800124 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000125 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800126 }
Ian Rogers848871b2013-08-05 10:56:33 -0700127#elif defined(__i386__)
128 // The callee save frame is pointed to by SP.
129 // | argN | |
130 // | ... | |
131 // | arg4 | |
132 // | arg3 spill | | Caller's frame
133 // | arg2 spill | |
134 // | arg1 spill | |
135 // | Method* | ---
136 // | Return |
137 // | EBP,ESI,EDI | callee saves
138 // | EBX | arg3
139 // | EDX | arg2
140 // | ECX | arg1
141 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800142 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
143 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
144 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800145 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
146 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
147 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800148 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000149 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800150 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800152 // The callee save frame is pointed to by SP.
153 // | argN | |
154 // | ... | |
155 // | reg. arg spills | | Caller's frame
156 // | Method* | ---
157 // | Return |
158 // | R15 | callee save
159 // | R14 | callee save
160 // | R13 | callee save
161 // | R12 | callee save
162 // | R9 | arg5
163 // | R8 | arg4
164 // | RSI/R6 | arg1
165 // | RBP/R5 | callee save
166 // | RBX/R3 | callee save
167 // | RDX/R2 | arg2
168 // | RCX/R1 | arg3
169 // | XMM7 | float arg 8
170 // | XMM6 | float arg 7
171 // | XMM5 | float arg 6
172 // | XMM4 | float arg 5
173 // | XMM3 | float arg 4
174 // | XMM2 | float arg 3
175 // | XMM1 | float arg 2
176 // | XMM0 | float arg 1
177 // | Padding |
178 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800179 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700180 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700181 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800182 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800185 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
186 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000187 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
188 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
189 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
190 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
191 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800192 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700193 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
194 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800195 }
196 }
Ian Rogers848871b2013-08-05 10:56:33 -0700197#else
198#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700199#endif
200
Ian Rogers936b37f2014-02-14 00:52:24 -0800201 public:
Sebastien Hertzfcfc8a72014-11-25 16:30:53 +0100202 // Special handling for proxy methods. Proxy methods are instance methods so the
203 // 'this' object is the 1st argument. They also have the same frame layout as the
204 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
205 // 1st GPR.
206 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
208 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
209 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
210 CHECK_GT(kNumQuickGprArgs, 0u);
211 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
212 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
213 GprIndexToGprOffset(kThisGprIndex);
214 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
215 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
216 }
217
Andreas Gampecf4035a2014-05-28 22:43:01 -0700218 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800219 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700220 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800221 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700222 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700223 }
224
Ian Rogers936b37f2014-02-14 00:52:24 -0800225 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700226 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800227 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700228 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800229 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700230 return *reinterpret_cast<uintptr_t*>(lr);
231 }
232
Andreas Gampec200a4a2014-06-16 18:39:09 -0700233 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
234 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
235 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
236 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
237 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
238 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
239 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
240 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
241 is_split_long_or_double_(false) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700242
243 virtual ~QuickArgumentVisitor() {}
244
245 virtual void Visit() = 0;
246
Ian Rogers936b37f2014-02-14 00:52:24 -0800247 Primitive::Type GetParamPrimitiveType() const {
248 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700249 }
250
251 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800252 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 Primitive::Type type = GetParamPrimitiveType();
254 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800255 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000256 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700258 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 }
260 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800261 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
263 }
264 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700265 }
266
267 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000268 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800269 return is_split_long_or_double_;
270 } else {
271 return false; // An optimization for when GPR and FPRs are 64bit.
272 }
Ian Rogers848871b2013-08-05 10:56:33 -0700273 }
274
Ian Rogers936b37f2014-02-14 00:52:24 -0800275 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700276 return GetParamPrimitiveType() == Primitive::kPrimNot;
277 }
278
Ian Rogers936b37f2014-02-14 00:52:24 -0800279 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700280 Primitive::Type type = GetParamPrimitiveType();
281 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
282 }
283
284 uint64_t ReadSplitLongParam() const {
285 DCHECK(IsSplitLongOrDouble());
286 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
287 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
288 return (low_half & 0xffffffffULL) | (high_half << 32);
289 }
290
291 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700292 // This implementation doesn't support reg-spill area for hard float
293 // ABI targets such as x86_64 and aarch64. So, for those targets whose
294 // 'kQuickSoftFloatAbi' is 'false':
295 // (a) 'stack_args_' should point to the first method's argument
296 // (b) whatever the argument type it is, the 'stack_index_' should
297 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 gpr_index_ = 0;
299 fpr_index_ = 0;
300 stack_index_ = 0;
301 if (!is_static_) { // Handle this.
302 cur_type_ = Primitive::kPrimNot;
303 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700304 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700305 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
306 stack_index_++;
307 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800308 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800309 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800310 }
Ian Rogers848871b2013-08-05 10:56:33 -0700311 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
313 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
314 switch (cur_type_) {
315 case Primitive::kPrimNot:
316 case Primitive::kPrimBoolean:
317 case Primitive::kPrimByte:
318 case Primitive::kPrimChar:
319 case Primitive::kPrimShort:
320 case Primitive::kPrimInt:
321 is_split_long_or_double_ = false;
322 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700323 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
324 stack_index_++;
325 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800326 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800327 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800328 }
329 break;
330 case Primitive::kPrimFloat:
331 is_split_long_or_double_ = false;
332 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800333 if (kQuickSoftFloatAbi) {
334 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800335 gpr_index_++;
336 } else {
337 stack_index_++;
338 }
339 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800340 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800341 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800342 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700343 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800344 }
345 break;
346 case Primitive::kPrimDouble:
347 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800348 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000349 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800350 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800351 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700352 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800353 if (kBytesStackArgLocation == 4) {
354 stack_index_+= 2;
355 } else {
356 CHECK_EQ(kBytesStackArgLocation, 8U);
357 stack_index_++;
358 }
359 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700360 if (gpr_index_ < kNumQuickGprArgs) {
361 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000362 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700363 if (gpr_index_ < kNumQuickGprArgs) {
364 gpr_index_++;
365 } else if (kQuickSoftFloatAbi) {
366 stack_index_++;
367 }
368 }
369 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800370 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000371 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800372 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800373 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800374 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800375 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000376 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800377 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800379 }
380 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700381 }
382 if (kBytesStackArgLocation == 4) {
383 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800384 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700385 CHECK_EQ(kBytesStackArgLocation, 8U);
386 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 }
388 }
389 break;
390 default:
391 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
392 }
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
394 }
395
396 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800397 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
398 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800399 if (kQuickSoftFloatAbi) {
400 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000401 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700402 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800403 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700404 // For now, there is no reg-spill area for the targets with
405 // hard float ABI. So, the offset pointing to the first method's
406 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700407 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700408 }
Ian Rogers848871b2013-08-05 10:56:33 -0700409 }
410
Andreas Gampec200a4a2014-06-16 18:39:09 -0700411 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700412 const bool is_static_;
413 const char* const shorty_;
414 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700415
416 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800417 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
418 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
419 byte* const stack_args_; // Address of stack arguments in caller's frame.
420 uint32_t gpr_index_; // Index into spilled GPRs.
421 uint32_t fpr_index_; // Index into spilled FPRs.
422 uint32_t stack_index_; // Index into arguments on the stack.
423 // The current type of argument during VisitArguments.
424 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700425 // Does a 64bit parameter straddle the register and stack arguments?
426 bool is_split_long_or_double_;
427};
428
Sebastien Hertzfcfc8a72014-11-25 16:30:53 +0100429// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
430// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
431extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
432 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
433 return QuickArgumentVisitor::GetProxyThisObject(sp);
434}
435
Ian Rogers848871b2013-08-05 10:56:33 -0700436// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800437class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700438 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700439 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
440 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
441 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700442 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700443
Ian Rogers9758f792014-03-13 09:02:55 -0700444 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700445
446 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800447 ShadowFrame* const sf_;
448 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700449
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700450 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700451};
452
Andreas Gampec200a4a2014-06-16 18:39:09 -0700453void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700454 Primitive::Type type = GetParamPrimitiveType();
455 switch (type) {
456 case Primitive::kPrimLong: // Fall-through.
457 case Primitive::kPrimDouble:
458 if (IsSplitLongOrDouble()) {
459 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
460 } else {
461 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
462 }
463 ++cur_reg_;
464 break;
465 case Primitive::kPrimNot: {
466 StackReference<mirror::Object>* stack_ref =
467 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
468 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
469 }
470 break;
471 case Primitive::kPrimBoolean: // Fall-through.
472 case Primitive::kPrimByte: // Fall-through.
473 case Primitive::kPrimChar: // Fall-through.
474 case Primitive::kPrimShort: // Fall-through.
475 case Primitive::kPrimInt: // Fall-through.
476 case Primitive::kPrimFloat:
477 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
478 break;
479 case Primitive::kPrimVoid:
480 LOG(FATAL) << "UNREACHABLE";
481 break;
482 }
483 ++cur_reg_;
484}
485
Brian Carlstromea46f952013-07-30 01:26:50 -0700486extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700487 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700488 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
489 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
490 // frame.
491 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
492
493 if (method->IsAbstract()) {
494 ThrowAbstractMethodError(method);
495 return 0;
496 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800497 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700498 const char* old_cause = self->StartAssertNoThreadSuspension(
499 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700500 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800501 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700502 uint16_t num_regs = code_item->registers_size_;
503 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700504 // No last shadow coming from quick.
505 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700506 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700507 uint32_t shorty_len = 0;
508 const char* shorty = method->GetShorty(&shorty_len);
509 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800510 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700511 shadow_frame_builder.VisitArguments();
512 // Push a transition back into managed code onto the linked list in thread.
513 ManagedStack fragment;
514 self->PushManagedStackFragment(&fragment);
515 self->PushShadowFrame(shadow_frame);
516 self->EndAssertNoThreadSuspension(old_cause);
517
Ian Rogers6c5cb212014-06-18 16:07:20 -0700518 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitialized()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700519 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700520 StackHandleScope<1> hs(self);
521 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
522 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800523 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700524 self->PopManagedStackFragment(fragment);
525 return 0;
526 }
527 }
528
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700529 StackHandleScope<1> hs(self);
530 MethodHelper mh(hs.NewHandle(method));
Ian Rogers848871b2013-08-05 10:56:33 -0700531 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
532 // Pop transition.
533 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800534 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700535 return result.GetJ();
536 }
537}
538
539// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
540// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800541class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700542 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700543 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
544 const char* shorty, uint32_t shorty_len,
545 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700546 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700547
Ian Rogers9758f792014-03-13 09:02:55 -0700548 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700549
Ian Rogers9758f792014-03-13 09:02:55 -0700550 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800551
Ian Rogers848871b2013-08-05 10:56:33 -0700552 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700553 ScopedObjectAccessUnchecked* const soa_;
554 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800555 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700556 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700557
Ian Rogers848871b2013-08-05 10:56:33 -0700558 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
559};
560
Ian Rogers9758f792014-03-13 09:02:55 -0700561void BuildQuickArgumentVisitor::Visit() {
562 jvalue val;
563 Primitive::Type type = GetParamPrimitiveType();
564 switch (type) {
565 case Primitive::kPrimNot: {
566 StackReference<mirror::Object>* stack_ref =
567 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
568 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
569 references_.push_back(std::make_pair(val.l, stack_ref));
570 break;
571 }
572 case Primitive::kPrimLong: // Fall-through.
573 case Primitive::kPrimDouble:
574 if (IsSplitLongOrDouble()) {
575 val.j = ReadSplitLongParam();
576 } else {
577 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
578 }
579 break;
580 case Primitive::kPrimBoolean: // Fall-through.
581 case Primitive::kPrimByte: // Fall-through.
582 case Primitive::kPrimChar: // Fall-through.
583 case Primitive::kPrimShort: // Fall-through.
584 case Primitive::kPrimInt: // Fall-through.
585 case Primitive::kPrimFloat:
586 val.i = *reinterpret_cast<jint*>(GetParamAddress());
587 break;
588 case Primitive::kPrimVoid:
589 LOG(FATAL) << "UNREACHABLE";
590 val.j = 0;
591 break;
592 }
593 args_->push_back(val);
594}
595
596void BuildQuickArgumentVisitor::FixupReferences() {
597 // Fixup any references which may have changed.
598 for (const auto& pair : references_) {
599 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700600 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700601 }
602}
603
Ian Rogers848871b2013-08-05 10:56:33 -0700604// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
605// which is responsible for recording callee save registers. We explicitly place into jobjects the
606// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
607// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700608extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700609 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700610 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700612 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
613 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700614 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
615 const char* old_cause =
616 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
617 // Register the top of the managed stack, making stack crawlable.
Jeff Hao4bf8d112014-07-24 16:26:09 -0700618 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700619 self->SetTopOfStack(sp, 0);
620 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700621 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
622 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700623 self->VerifyStack();
624 // Start new JNI local reference state.
625 JNIEnvExt* env = self->GetJniEnv();
626 ScopedObjectAccessUnchecked soa(env);
627 ScopedJniEnvLocalRefState env_state(env);
628 // Create local ref. copies of proxy method and the receiver.
629 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
630
631 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700632 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
633 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700634 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700635 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700636 uint32_t shorty_len = 0;
637 const char* shorty = proxy_method->GetShorty(&shorty_len);
638 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700639
Ian Rogers848871b2013-08-05 10:56:33 -0700640 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700641 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700642 args.erase(args.begin());
643
644 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700646 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700647 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
648 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
649
650 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
651 // that performs allocations.
652 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700653 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800654 // Restore references which might have moved.
655 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700656 return result.GetJ();
657}
658
659// Read object references held in arguments from quick frames and place in a JNI local references,
660// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800661class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700662 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700663 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
664 const char* shorty, uint32_t shorty_len,
665 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700666 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700667
Ian Rogers9758f792014-03-13 09:02:55 -0700668 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700669
Ian Rogers9758f792014-03-13 09:02:55 -0700670 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700671
672 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700673 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800674 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700675 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
676
Mathieu Chartier590fee92013-09-13 13:46:47 -0700677 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700678};
679
Ian Rogers9758f792014-03-13 09:02:55 -0700680void RememberForGcArgumentVisitor::Visit() {
681 if (IsParamAReference()) {
682 StackReference<mirror::Object>* stack_ref =
683 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
684 jobject reference =
685 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
686 references_.push_back(std::make_pair(reference, stack_ref));
687 }
688}
689
690void RememberForGcArgumentVisitor::FixupReferences() {
691 // Fixup any references which may have changed.
692 for (const auto& pair : references_) {
693 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700694 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700695 }
696}
697
Ian Rogers848871b2013-08-05 10:56:33 -0700698// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700699extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700700 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700701 Thread* self,
702 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700703 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800704 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700705 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800706 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700707 ScopedObjectAccessUnchecked soa(env);
708 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800709 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700710
711 // Compute details about the called method (avoid GCs)
712 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700713 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700714 InvokeType invoke_type;
715 const DexFile* dex_file;
716 uint32_t dex_method_idx;
717 if (called->IsRuntimeMethod()) {
718 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
719 const DexFile::CodeItem* code;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700720 dex_file = caller->GetDexFile();
721 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700722 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
723 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
724 Instruction::Code instr_code = instr->Opcode();
725 bool is_range;
726 switch (instr_code) {
727 case Instruction::INVOKE_DIRECT:
728 invoke_type = kDirect;
729 is_range = false;
730 break;
731 case Instruction::INVOKE_DIRECT_RANGE:
732 invoke_type = kDirect;
733 is_range = true;
734 break;
735 case Instruction::INVOKE_STATIC:
736 invoke_type = kStatic;
737 is_range = false;
738 break;
739 case Instruction::INVOKE_STATIC_RANGE:
740 invoke_type = kStatic;
741 is_range = true;
742 break;
743 case Instruction::INVOKE_SUPER:
744 invoke_type = kSuper;
745 is_range = false;
746 break;
747 case Instruction::INVOKE_SUPER_RANGE:
748 invoke_type = kSuper;
749 is_range = true;
750 break;
751 case Instruction::INVOKE_VIRTUAL:
752 invoke_type = kVirtual;
753 is_range = false;
754 break;
755 case Instruction::INVOKE_VIRTUAL_RANGE:
756 invoke_type = kVirtual;
757 is_range = true;
758 break;
759 case Instruction::INVOKE_INTERFACE:
760 invoke_type = kInterface;
761 is_range = false;
762 break;
763 case Instruction::INVOKE_INTERFACE_RANGE:
764 invoke_type = kInterface;
765 is_range = true;
766 break;
767 default:
768 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
769 // Avoid used uninitialized warnings.
770 invoke_type = kDirect;
771 is_range = false;
772 }
773 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700774 } else {
775 invoke_type = kStatic;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700776 dex_file = called->GetDexFile();
Ian Rogers848871b2013-08-05 10:56:33 -0700777 dex_method_idx = called->GetDexMethodIndex();
778 }
779 uint32_t shorty_len;
780 const char* shorty =
781 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700782 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700783 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800784 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800785 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700786 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700787 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700788 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700789 mirror::Object* dummy = nullptr;
790 HandleWrapper<mirror::Object> h_receiver(
791 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
792 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700793 }
794 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800795 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700796 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800797 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
798 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800799 if (virtual_or_interface) {
800 // Refine called method based on receiver.
801 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700802
803 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800804 if (invoke_type == kVirtual) {
805 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
806 } else {
807 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
808 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700809
810 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
811 << PrettyTypeOf(receiver) << " "
812 << invoke_type << " " << orig_called->GetVtableIndex();
813
Ian Rogers83883d72013-10-21 21:07:24 -0700814 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
815 // of the sharpened method.
Andreas Gampe4ef12f52014-07-31 16:23:49 -0700816 if (called->HasSameDexCacheResolvedMethods(caller)) {
817 caller->SetDexCacheResolvedMethod(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700818 } else {
819 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000820 // the caller's dex file. Since we get here only if the original called was a runtime
821 // method, we've got the correct dex_file and a dex_method_idx from above.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700822 DCHECK_EQ(caller->GetDexFile(), dex_file);
823 StackHandleScope<1> hs(self);
824 MethodHelper mh(hs.NewHandle(called));
825 uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700826 if (method_index != DexFile::kDexNoIndex) {
Andreas Gampe4ef12f52014-07-31 16:23:49 -0700827 caller->SetDexCacheResolvedMethod(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700828 }
829 }
830 }
Ian Rogers848871b2013-08-05 10:56:33 -0700831 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700832 StackHandleScope<1> hs(soa.Self());
833 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700834 linker->EnsureInitialized(called_class, true, true);
835 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800836 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700837 } else if (called_class->IsInitializing()) {
838 if (invoke_type == kStatic) {
839 // Class is still initializing, go to oat and grab code (trampoline must be left in place
840 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800841 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700842 } else {
843 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800844 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700845 }
846 } else {
847 DCHECK(called_class->IsErroneous());
848 }
849 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800850 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700851 // Fixup any locally saved objects may have moved during a GC.
852 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700853 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700854 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700855 return code;
856}
857
Andreas Gampec147b002014-03-06 18:11:06 -0800858/*
859 * This class uses a couple of observations to unite the different calling conventions through
860 * a few constants.
861 *
862 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
863 * possible alignment.
864 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
865 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
866 * when we have to split things
867 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
868 * and we can use Int handling directly.
869 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
870 * necessary when widening. Also, widening of Ints will take place implicitly, and the
871 * extension should be compatible with Aarch64, which mandates copying the available bits
872 * into LSB and leaving the rest unspecified.
873 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
874 * the stack.
875 * 6) There is only little endian.
876 *
877 *
878 * Actual work is supposed to be done in a delegate of the template type. The interface is as
879 * follows:
880 *
881 * void PushGpr(uintptr_t): Add a value for the next GPR
882 *
883 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
884 * padding, that is, think the architecture is 32b and aligns 64b.
885 *
886 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
887 * split this if necessary. The current state will have aligned, if
888 * necessary.
889 *
890 * void PushStack(uintptr_t): Push a value to the stack.
891 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700892 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700893 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800894 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700895 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800896 *
897 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700898template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800899 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800900#if defined(__arm__)
901 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800902 static constexpr bool kNativeSoftFloatAbi = true;
903 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800904 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
905
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800906 static constexpr size_t kRegistersNeededForLong = 2;
907 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800908 static constexpr bool kMultiRegistersAligned = true;
909 static constexpr bool kMultiRegistersWidened = false;
910 static constexpr bool kAlignLongOnStack = true;
911 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000912#elif defined(__aarch64__)
913 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
914 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
915 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
916
917 static constexpr size_t kRegistersNeededForLong = 1;
918 static constexpr size_t kRegistersNeededForDouble = 1;
919 static constexpr bool kMultiRegistersAligned = false;
920 static constexpr bool kMultiRegistersWidened = false;
921 static constexpr bool kAlignLongOnStack = false;
922 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800923#elif defined(__mips__)
924 // TODO: These are all dummy values!
925 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
926 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
927 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
928
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800929 static constexpr size_t kRegistersNeededForLong = 2;
930 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800931 static constexpr bool kMultiRegistersAligned = true;
932 static constexpr bool kMultiRegistersWidened = true;
933 static constexpr bool kAlignLongOnStack = false;
934 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800935#elif defined(__i386__)
936 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800937 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800938 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
939 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
940
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800941 static constexpr size_t kRegistersNeededForLong = 2;
942 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700943 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800944 static constexpr bool kMultiRegistersWidened = false;
945 static constexpr bool kAlignLongOnStack = false;
946 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800947#elif defined(__x86_64__)
948 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
949 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
950 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
951
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800952 static constexpr size_t kRegistersNeededForLong = 1;
953 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800954 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800955 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800956 static constexpr bool kAlignLongOnStack = false;
957 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800958#else
959#error "Unsupported architecture"
960#endif
961
Andreas Gampec147b002014-03-06 18:11:06 -0800962 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700963 explicit BuildNativeCallFrameStateMachine(T* delegate)
964 : gpr_index_(kNumNativeGprArgs),
965 fpr_index_(kNumNativeFprArgs),
966 stack_entries_(0),
967 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800968 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
969 // the next register is even; counting down is just to make the compiler happy...
970 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
971 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
972 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800973
Andreas Gampec200a4a2014-06-16 18:39:09 -0700974 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800975
976 bool HavePointerGpr() {
977 return gpr_index_ > 0;
978 }
979
Andreas Gampec200a4a2014-06-16 18:39:09 -0700980 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800981 if (HavePointerGpr()) {
982 gpr_index_--;
983 PushGpr(reinterpret_cast<uintptr_t>(val));
984 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700985 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800986 PushStack(reinterpret_cast<uintptr_t>(val));
987 gpr_index_ = 0;
988 }
989 }
990
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700991 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800992 return gpr_index_ > 0;
993 }
994
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700995 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
996 uintptr_t handle = PushHandle(ptr);
997 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800998 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700999 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001000 } else {
1001 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001002 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001003 gpr_index_ = 0;
1004 }
1005 }
1006
Andreas Gampec147b002014-03-06 18:11:06 -08001007 bool HaveIntGpr() {
1008 return gpr_index_ > 0;
1009 }
1010
1011 void AdvanceInt(uint32_t val) {
1012 if (HaveIntGpr()) {
1013 gpr_index_--;
1014 PushGpr(val);
1015 } else {
1016 stack_entries_++;
1017 PushStack(val);
1018 gpr_index_ = 0;
1019 }
1020 }
1021
Andreas Gampec147b002014-03-06 18:11:06 -08001022 bool HaveLongGpr() {
1023 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1024 }
1025
1026 bool LongGprNeedsPadding() {
1027 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1028 kAlignLongOnStack && // and when it needs alignment
1029 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1030 }
1031
1032 bool LongStackNeedsPadding() {
1033 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1034 kAlignLongOnStack && // and when it needs 8B alignment
1035 (stack_entries_ & 1) == 1; // counter is odd
1036 }
1037
1038 void AdvanceLong(uint64_t val) {
1039 if (HaveLongGpr()) {
1040 if (LongGprNeedsPadding()) {
1041 PushGpr(0);
1042 gpr_index_--;
1043 }
1044 if (kRegistersNeededForLong == 1) {
1045 PushGpr(static_cast<uintptr_t>(val));
1046 } else {
1047 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1048 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1049 }
1050 gpr_index_ -= kRegistersNeededForLong;
1051 } else {
1052 if (LongStackNeedsPadding()) {
1053 PushStack(0);
1054 stack_entries_++;
1055 }
1056 if (kRegistersNeededForLong == 1) {
1057 PushStack(static_cast<uintptr_t>(val));
1058 stack_entries_++;
1059 } else {
1060 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1061 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1062 stack_entries_ += 2;
1063 }
1064 gpr_index_ = 0;
1065 }
1066 }
1067
Andreas Gampec147b002014-03-06 18:11:06 -08001068 bool HaveFloatFpr() {
1069 return fpr_index_ > 0;
1070 }
1071
Andreas Gampec147b002014-03-06 18:11:06 -08001072 void AdvanceFloat(float val) {
1073 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001074 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001075 } else {
1076 if (HaveFloatFpr()) {
1077 fpr_index_--;
1078 if (kRegistersNeededForDouble == 1) {
1079 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001080 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001081 } else {
1082 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001083 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001084 }
1085 } else {
1086 PushFpr4(val);
1087 }
1088 } else {
1089 stack_entries_++;
1090 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1091 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001092 // Note: We need to jump through those hoops to make the compiler happy.
1093 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1094 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001095 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001096 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001097 }
1098 fpr_index_ = 0;
1099 }
1100 }
1101 }
1102
Andreas Gampec147b002014-03-06 18:11:06 -08001103 bool HaveDoubleFpr() {
1104 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1105 }
1106
1107 bool DoubleFprNeedsPadding() {
1108 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1109 kAlignDoubleOnStack && // and when it needs alignment
1110 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1111 }
1112
1113 bool DoubleStackNeedsPadding() {
1114 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1115 kAlignDoubleOnStack && // and when it needs 8B alignment
1116 (stack_entries_ & 1) == 1; // counter is odd
1117 }
1118
1119 void AdvanceDouble(uint64_t val) {
1120 if (kNativeSoftFloatAbi) {
1121 AdvanceLong(val);
1122 } else {
1123 if (HaveDoubleFpr()) {
1124 if (DoubleFprNeedsPadding()) {
1125 PushFpr4(0);
1126 fpr_index_--;
1127 }
1128 PushFpr8(val);
1129 fpr_index_ -= kRegistersNeededForDouble;
1130 } else {
1131 if (DoubleStackNeedsPadding()) {
1132 PushStack(0);
1133 stack_entries_++;
1134 }
1135 if (kRegistersNeededForDouble == 1) {
1136 PushStack(static_cast<uintptr_t>(val));
1137 stack_entries_++;
1138 } else {
1139 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1140 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1141 stack_entries_ += 2;
1142 }
1143 fpr_index_ = 0;
1144 }
1145 }
1146 }
1147
1148 uint32_t getStackEntries() {
1149 return stack_entries_;
1150 }
1151
1152 uint32_t getNumberOfUsedGprs() {
1153 return kNumNativeGprArgs - gpr_index_;
1154 }
1155
1156 uint32_t getNumberOfUsedFprs() {
1157 return kNumNativeFprArgs - fpr_index_;
1158 }
1159
1160 private:
1161 void PushGpr(uintptr_t val) {
1162 delegate_->PushGpr(val);
1163 }
1164 void PushFpr4(float val) {
1165 delegate_->PushFpr4(val);
1166 }
1167 void PushFpr8(uint64_t val) {
1168 delegate_->PushFpr8(val);
1169 }
1170 void PushStack(uintptr_t val) {
1171 delegate_->PushStack(val);
1172 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001173 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1174 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001175 }
1176
1177 uint32_t gpr_index_; // Number of free GPRs
1178 uint32_t fpr_index_; // Number of free FPRs
1179 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1180 // extended
1181 T* delegate_; // What Push implementation gets called
1182};
1183
Andreas Gampec200a4a2014-06-16 18:39:09 -07001184// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1185// in subclasses.
1186//
1187// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1188// them with handles.
1189class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001190 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001191 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1192
1193 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001194
Andreas Gampec147b002014-03-06 18:11:06 -08001195 uint32_t GetStackSize() {
1196 return num_stack_entries_ * sizeof(uintptr_t);
1197 }
1198
Andreas Gampec200a4a2014-06-16 18:39:09 -07001199 uint8_t* LayoutCallStack(uint8_t* sp8) {
Andreas Gampec147b002014-03-06 18:11:06 -08001200 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001201 // Align by kStackAlignment.
1202 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001203 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001204 }
1205
Andreas Gampec200a4a2014-06-16 18:39:09 -07001206 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) {
1207 // Assumption is OK right now, as we have soft-float arm
1208 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1209 sp8 -= fregs * sizeof(uintptr_t);
1210 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1211 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1212 sp8 -= iregs * sizeof(uintptr_t);
1213 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1214 return sp8;
1215 }
Andreas Gampec147b002014-03-06 18:11:06 -08001216
Andreas Gampec200a4a2014-06-16 18:39:09 -07001217 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
1218 uint32_t** start_fpr) {
1219 // Native call stack.
1220 sp8 = LayoutCallStack(sp8);
1221 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001222
Andreas Gampec200a4a2014-06-16 18:39:09 -07001223 // Put fprs and gprs below.
1224 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001225
Andreas Gampec200a4a2014-06-16 18:39:09 -07001226 // Return the new bottom.
1227 return sp8;
1228 }
1229
1230 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {}
1232
1233 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1234 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1235
1236 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001237
1238 for (uint32_t i = 1; i < shorty_len; ++i) {
1239 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1240 switch (cur_type_) {
1241 case Primitive::kPrimNot:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001242 sm.AdvanceHandleScope(
1243 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001244 break;
1245
1246 case Primitive::kPrimBoolean:
1247 case Primitive::kPrimByte:
1248 case Primitive::kPrimChar:
1249 case Primitive::kPrimShort:
1250 case Primitive::kPrimInt:
1251 sm.AdvanceInt(0);
1252 break;
1253 case Primitive::kPrimFloat:
1254 sm.AdvanceFloat(0);
1255 break;
1256 case Primitive::kPrimDouble:
1257 sm.AdvanceDouble(0);
1258 break;
1259 case Primitive::kPrimLong:
1260 sm.AdvanceLong(0);
1261 break;
1262 default:
1263 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1264 }
1265 }
1266
1267 num_stack_entries_ = sm.getStackEntries();
1268 }
1269
1270 void PushGpr(uintptr_t /* val */) {
1271 // not optimizing registers, yet
1272 }
1273
1274 void PushFpr4(float /* val */) {
1275 // not optimizing registers, yet
1276 }
1277
1278 void PushFpr8(uint64_t /* val */) {
1279 // not optimizing registers, yet
1280 }
1281
1282 void PushStack(uintptr_t /* val */) {
1283 // counting is already done in the superclass
1284 }
1285
Andreas Gampec200a4a2014-06-16 18:39:09 -07001286 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001287 return reinterpret_cast<uintptr_t>(nullptr);
1288 }
1289
Andreas Gampec200a4a2014-06-16 18:39:09 -07001290 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001291 uint32_t num_stack_entries_;
1292};
1293
Andreas Gampec200a4a2014-06-16 18:39:09 -07001294class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001295 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001296 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001297
Andreas Gampec200a4a2014-06-16 18:39:09 -07001298 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1299 // is at *m = sp. Will update to point to the bottom of the save frame.
1300 //
1301 // Note: assumes ComputeAll() has been run before.
1302 void LayoutCalleeSaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1303 uint32_t* handle_scope_entries)
1304 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1305 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001306
Andreas Gampec200a4a2014-06-16 18:39:09 -07001307 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1308
1309 // First, fix up the layout of the callee-save frame.
1310 // We have to squeeze in the HandleScope, and relocate the method pointer.
1311
1312 // "Free" the slot for the method.
1313 sp8 += kPointerSize; // In the callee-save frame we use a full pointer.
1314
1315 // Under the callee saves put handle scope and new method stack reference.
1316 *handle_scope_entries = num_handle_scope_references_;
1317
1318 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1319 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1320
1321 sp8 -= scope_and_method;
1322 // Align by kStackAlignment.
1323 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1324 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1325
1326 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1327 *table = reinterpret_cast<HandleScope*>(sp8_table);
1328 (*table)->SetNumberOfReferences(num_handle_scope_references_);
1329
1330 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1331 uint8_t* method_pointer = sp8;
1332 StackReference<mirror::ArtMethod>* new_method_ref =
1333 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1334 new_method_ref->Assign(method);
1335 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001336 }
1337
Andreas Gampec200a4a2014-06-16 18:39:09 -07001338 // Adds space for the cookie. Note: may leave stack unaligned.
1339 void LayoutCookie(uint8_t** sp) {
1340 // Reference cookie and padding
1341 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001342 }
1343
Andreas Gampec200a4a2014-06-16 18:39:09 -07001344 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1345 // Returns the new bottom. Note: this may be unaligned.
1346 uint8_t* LayoutJNISaveFrame(StackReference<mirror::ArtMethod>** m, void* sp, HandleScope** table,
1347 uint32_t* handle_scope_entries)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001348 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001349 // First, fix up the layout of the callee-save frame.
1350 // We have to squeeze in the HandleScope, and relocate the method pointer.
1351 LayoutCalleeSaveFrame(m, sp, table, handle_scope_entries);
1352
1353 // The bottom of the callee-save frame is now where the method is, *m.
1354 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1355
1356 // Add space for cookie.
1357 LayoutCookie(&sp8);
1358
1359 return sp8;
1360 }
1361
1362 // WARNING: After this, *sp won't be pointing to the method anymore!
1363 uint8_t* ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty,
1364 uint32_t shorty_len, HandleScope** table, uint32_t* handle_scope_entries,
1365 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1366 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1367 Walk(shorty, shorty_len);
1368
1369 // JNI part.
1370 uint8_t* sp8 = LayoutJNISaveFrame(m, reinterpret_cast<void*>(*m), table, handle_scope_entries);
1371
1372 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1373
1374 // Return the new bottom.
1375 return sp8;
1376 }
1377
1378 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1379
1380 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1381 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1382 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1383
1384 private:
1385 uint32_t num_handle_scope_references_;
1386};
1387
1388uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1389 num_handle_scope_references_++;
1390 return reinterpret_cast<uintptr_t>(nullptr);
1391}
1392
1393void ComputeGenericJniFrameSize::WalkHeader(
1394 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1395 // JNIEnv
1396 sm->AdvancePointer(nullptr);
1397
1398 // Class object or this as first argument
1399 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1400}
1401
1402// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1403// the template requirements of BuildGenericJniFrameStateMachine.
1404class FillNativeCall {
1405 public:
1406 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1407 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1408
1409 virtual ~FillNativeCall() {}
1410
1411 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1412 cur_gpr_reg_ = gpr_regs;
1413 cur_fpr_reg_ = fpr_regs;
1414 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001415 }
1416
1417 void PushGpr(uintptr_t val) {
1418 *cur_gpr_reg_ = val;
1419 cur_gpr_reg_++;
1420 }
1421
1422 void PushFpr4(float val) {
1423 *cur_fpr_reg_ = val;
1424 cur_fpr_reg_++;
1425 }
1426
1427 void PushFpr8(uint64_t val) {
1428 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1429 *tmp = val;
1430 cur_fpr_reg_ += 2;
1431 }
1432
1433 void PushStack(uintptr_t val) {
1434 *cur_stack_arg_ = val;
1435 cur_stack_arg_++;
1436 }
1437
Andreas Gampec200a4a2014-06-16 18:39:09 -07001438 virtual uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1439 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1440 return 0U;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001441 }
1442
1443 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001444 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001445 uint32_t* cur_fpr_reg_;
1446 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001447};
Andreas Gampec147b002014-03-06 18:11:06 -08001448
Andreas Gampec200a4a2014-06-16 18:39:09 -07001449// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1450// of transitioning into native code.
1451class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1452 public:
1453 BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static,
1454 const char* shorty, uint32_t shorty_len, Thread* self)
1455 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1456 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1457 ComputeGenericJniFrameSize fsc;
1458 uintptr_t* start_gpr_reg;
1459 uint32_t* start_fpr_reg;
1460 uintptr_t* start_stack_arg;
1461 uint32_t handle_scope_entries;
1462 bottom_of_used_area_ = fsc.ComputeLayout(sp, is_static, shorty, shorty_len, &handle_scope_,
1463 &handle_scope_entries, &start_stack_arg,
1464 &start_gpr_reg, &start_fpr_reg);
1465
1466 handle_scope_->SetNumberOfReferences(handle_scope_entries);
1467 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1468
1469 // jni environment is always first argument
1470 sm_.AdvancePointer(self->GetJniEnv());
1471
1472 if (is_static) {
1473 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1474 }
1475 }
1476
1477 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1478
1479 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1480
1481 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1483 return handle_scope_->GetHandle(0).GetReference();
1484 }
1485
1486 jobject GetFirstHandleScopeJObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1487 return handle_scope_->GetHandle(0).ToJObject();
1488 }
1489
1490 void* GetBottomOfUsedArea() {
1491 return bottom_of_used_area_;
1492 }
1493
1494 private:
1495 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1496 class FillJniCall FINAL : public FillNativeCall {
1497 public:
1498 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1499 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1500 handle_scope_(handle_scope), cur_entry_(0) {}
1501
1502 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1503
1504 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1505 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1506 handle_scope_ = scope;
1507 cur_entry_ = 0U;
1508 }
1509
1510 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1511 // Initialize padding entries.
1512 size_t expected_slots = handle_scope_->NumberOfReferences();
1513 while (cur_entry_ < expected_slots) {
1514 handle_scope_->GetHandle(cur_entry_++).Assign(nullptr);
1515 }
1516 DCHECK_NE(cur_entry_, 0U);
1517 }
1518
1519 private:
1520 HandleScope* handle_scope_;
1521 size_t cur_entry_;
1522 };
1523
1524 HandleScope* handle_scope_;
1525 FillJniCall jni_call_;
1526 void* bottom_of_used_area_;
1527
1528 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001529
1530 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1531};
1532
Andreas Gampec200a4a2014-06-16 18:39:09 -07001533uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1534 uintptr_t tmp;
1535 Handle<mirror::Object> h = handle_scope_->GetHandle(cur_entry_);
1536 h.Assign(ref);
1537 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1538 cur_entry_++;
1539 return tmp;
1540}
1541
Ian Rogers9758f792014-03-13 09:02:55 -07001542void BuildGenericJniFrameVisitor::Visit() {
1543 Primitive::Type type = GetParamPrimitiveType();
1544 switch (type) {
1545 case Primitive::kPrimLong: {
1546 jlong long_arg;
1547 if (IsSplitLongOrDouble()) {
1548 long_arg = ReadSplitLongParam();
1549 } else {
1550 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1551 }
1552 sm_.AdvanceLong(long_arg);
1553 break;
1554 }
1555 case Primitive::kPrimDouble: {
1556 uint64_t double_arg;
1557 if (IsSplitLongOrDouble()) {
1558 // Read into union so that we don't case to a double.
1559 double_arg = ReadSplitLongParam();
1560 } else {
1561 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1562 }
1563 sm_.AdvanceDouble(double_arg);
1564 break;
1565 }
1566 case Primitive::kPrimNot: {
1567 StackReference<mirror::Object>* stack_ref =
1568 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001569 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001570 break;
1571 }
1572 case Primitive::kPrimFloat:
1573 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1574 break;
1575 case Primitive::kPrimBoolean: // Fall-through.
1576 case Primitive::kPrimByte: // Fall-through.
1577 case Primitive::kPrimChar: // Fall-through.
1578 case Primitive::kPrimShort: // Fall-through.
1579 case Primitive::kPrimInt: // Fall-through.
1580 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1581 break;
1582 case Primitive::kPrimVoid:
1583 LOG(FATAL) << "UNREACHABLE";
1584 break;
1585 }
1586}
1587
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001588void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001589 // Clear out rest of the scope.
1590 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001591 // Install HandleScope.
1592 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001593}
1594
Ian Rogers04c31d22014-07-07 21:44:06 -07001595#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001596extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001597#else
1598extern "C" void* artFindNativeMethod(Thread* self);
1599#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001600
Andreas Gampead615172014-04-04 16:20:13 -07001601uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1602 if (lock != nullptr) {
1603 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1604 } else {
1605 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1606 }
1607}
1608
1609void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1610 if (lock != nullptr) {
1611 JniMethodEndSynchronized(cookie, lock, self);
1612 } else {
1613 JniMethodEnd(cookie, self);
1614 }
1615}
1616
Andreas Gampec147b002014-03-06 18:11:06 -08001617/*
1618 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001619 * 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 -08001620 * The final element on the stack is a pointer to the native code.
1621 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001622 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001623 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001624 *
Andreas Gampec147b002014-03-06 18:11:06 -08001625 * The return of this function denotes:
1626 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1627 * 2) An error, if the value is negative.
1628 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1630 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001631 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001632 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001633 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001634 uint32_t shorty_len = 0;
1635 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001636
1637 // Run the visitor.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001638 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), shorty, shorty_len, self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001639 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001640 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001641
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 // Fix up managed-stack things in Thread.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001643 self->SetTopOfStack(sp, 0);
1644
Ian Rogerse0dcd462014-03-08 15:21:04 -08001645 self->VerifyStack();
1646
Andreas Gampe90546832014-03-12 18:07:19 -07001647 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001648 uint32_t cookie;
1649 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001650 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001651 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001652 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001653 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001654 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001655 }
1656 } else {
1657 cookie = JniMethodStart(self);
1658 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001659 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001660 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001661
Andreas Gampe90546832014-03-12 18:07:19 -07001662 // Retrieve the stored native code.
Mathieu Chartiere832e642014-11-10 11:08:06 -08001663 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001664
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001665 // There are two cases for the content of nativeCode:
1666 // 1) Pointer to the native function.
1667 // 2) Pointer to the trampoline for native code binding.
1668 // In the second case, we need to execute the binding and continue with the actual native function
1669 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001670 DCHECK(nativeCode != nullptr);
1671 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001672#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001673 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001674#else
1675 nativeCode = artFindNativeMethod(self);
1676#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001677
1678 if (nativeCode == nullptr) {
1679 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001680
1681 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001682 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001683 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001684 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1685 } else {
1686 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1687 }
1688
Andreas Gampec200a4a2014-06-16 18:39:09 -07001689 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001690 }
1691 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001692 }
1693
Andreas Gampec200a4a2014-06-16 18:39:09 -07001694 // Return native code addr(lo) and bottom of alloca address(hi).
1695 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1696 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001697}
1698
1699/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001700 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001701 * unlocking.
1702 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001703extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001704 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001705 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001706 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001707 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001708 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001709
Andreas Gampead615172014-04-04 16:20:13 -07001710 jobject lock = nullptr;
1711 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001712 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1713 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001714 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001715 }
1716
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001717 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001718
1719 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001720 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001721 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001722 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001723
1724 switch (return_shorty_char) {
Nicolas Geoffrayebee8de2014-08-13 03:40:45 +01001725 case 'F': {
1726 if (kRuntimeISA == kX86) {
1727 // Convert back the result to float.
1728 double d = bit_cast<uint64_t, double>(result_f);
1729 return bit_cast<float, uint32_t>(static_cast<float>(d));
1730 } else {
1731 return result_f;
1732 }
1733 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001734 case 'D':
1735 return result_f;
1736 case 'Z':
1737 return result.z;
1738 case 'B':
1739 return result.b;
1740 case 'C':
1741 return result.c;
1742 case 'S':
1743 return result.s;
1744 case 'I':
1745 return result.i;
1746 case 'J':
1747 return result.j;
1748 case 'V':
1749 return 0;
1750 default:
1751 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1752 return 0;
1753 }
1754 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001755}
1756
Andreas Gamped58342c2014-06-05 14:18:08 -07001757// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1758// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001759//
Andreas Gamped58342c2014-06-05 14:18:08 -07001760// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1761// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001762
1763template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001764static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001765 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001766 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001767
1768template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001769static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001770 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001771 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001772 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1773 type);
1774 if (UNLIKELY(method == nullptr)) {
1775 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1776 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1777 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001778 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001779 {
1780 // Remember the args in case a GC happens in FindMethodFromCode.
1781 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1782 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1783 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001784 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1785 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001786 visitor.FixupReferences();
1787 }
1788
1789 if (UNLIKELY(method == NULL)) {
1790 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001791 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001792 }
1793 }
1794 DCHECK(!self->IsExceptionPending());
1795 const void* code = method->GetEntryPointFromQuickCompiledCode();
1796
1797 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001798 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1799 << " location: "
1800 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001801
Andreas Gamped58342c2014-06-05 14:18:08 -07001802 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1803 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001804}
1805
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001806// Explicit artInvokeCommon template function declarations to please analysis tool.
1807#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1808 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001809 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001810 mirror::Object* this_object, \
1811 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001812 Thread* self, \
1813 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001814
1815EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1816EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1817EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1818EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1819EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1820EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1821EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1822EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1823EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1824EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1825#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1826
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001827// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001828extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1829 uint32_t method_idx, mirror::Object* this_object,
1830 mirror::ArtMethod* caller_method, Thread* self,
1831 StackReference<mirror::ArtMethod>* sp)
1832 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1833 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1834 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001835}
1836
Andreas Gampec200a4a2014-06-16 18:39:09 -07001837extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1838 uint32_t method_idx, mirror::Object* this_object,
1839 mirror::ArtMethod* caller_method, Thread* self,
1840 StackReference<mirror::ArtMethod>* sp)
1841 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1842 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1843 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001844}
1845
Andreas Gampec200a4a2014-06-16 18:39:09 -07001846extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1847 uint32_t method_idx, mirror::Object* this_object,
1848 mirror::ArtMethod* caller_method, Thread* self,
1849 StackReference<mirror::ArtMethod>* sp)
1850 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1851 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1852 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001853}
1854
Andreas Gampec200a4a2014-06-16 18:39:09 -07001855extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1856 uint32_t method_idx, mirror::Object* this_object,
1857 mirror::ArtMethod* caller_method, Thread* self,
1858 StackReference<mirror::ArtMethod>* sp)
1859 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1860 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1861 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001862}
1863
Andreas Gampec200a4a2014-06-16 18:39:09 -07001864extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1865 uint32_t method_idx, mirror::Object* this_object,
1866 mirror::ArtMethod* caller_method, Thread* self,
1867 StackReference<mirror::ArtMethod>* sp)
1868 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1869 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1870 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001871}
1872
1873// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001874extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001875 mirror::Object* this_object,
1876 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001877 Thread* self,
1878 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001879 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1880 mirror::ArtMethod* method;
1881 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1882 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1883 if (UNLIKELY(method == NULL)) {
1884 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1885 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1886 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001887 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001888 }
1889 } else {
1890 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1891 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001892
1893 // Find the caller PC.
1894 constexpr size_t pc_offset = GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsAndArgs);
1895 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + pc_offset);
1896
1897 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001898 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001899 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001900 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1901 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1902 Instruction::Code instr_code = instr->Opcode();
1903 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1904 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1905 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1906 uint32_t dex_method_idx;
1907 if (instr_code == Instruction::INVOKE_INTERFACE) {
1908 dex_method_idx = instr->VRegB_35c();
1909 } else {
1910 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1911 dex_method_idx = instr->VRegB_3rc();
1912 }
1913
Andreas Gampec200a4a2014-06-16 18:39:09 -07001914 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1915 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001916 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001917 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1918 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001919 {
1920 // Remember the args in case a GC happens in FindMethodFromCode.
1921 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1922 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1923 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001924 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001925 self);
1926 visitor.FixupReferences();
1927 }
1928
1929 if (UNLIKELY(method == nullptr)) {
1930 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001931 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001932 }
1933 }
1934 const void* code = method->GetEntryPointFromQuickCompiledCode();
1935
1936 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001937 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1938 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001939
Andreas Gamped58342c2014-06-05 14:18:08 -07001940 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1941 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001942}
1943
Ian Rogers848871b2013-08-05 10:56:33 -07001944} // namespace art