blob: 1d524cb53b40cb66c776f7aee5cf10b74f651ccc [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070026#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
29#include "object_utils.h"
30#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;
Ian Rogers848871b2013-08-05 10:56:33 -070039#if defined(__arm__)
40 // The callee save frame is pointed to by SP.
41 // | argN | |
42 // | ... | |
43 // | arg4 | |
44 // | arg3 spill | | Caller's frame
45 // | arg2 spill | |
46 // | arg1 spill | |
47 // | Method* | ---
48 // | LR |
49 // | ... | callee saves
50 // | R3 | arg3
51 // | R2 | arg2
52 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080053 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070054 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080055 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
56 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
57 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080058 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
59 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
60 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
61 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size.
62 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000063 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080064 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000065#elif defined(__aarch64__)
66 // The callee save frame is pointed to by SP.
67 // | argN | |
68 // | ... | |
69 // | arg4 | |
70 // | arg3 spill | | Caller's frame
71 // | arg2 spill | |
72 // | arg1 spill | |
73 // | Method* | ---
74 // | LR |
75 // | X28 |
76 // | : |
77 // | X19 |
78 // | X7 |
79 // | : |
80 // | X1 |
81 // | D15 |
82 // | : |
83 // | D0 |
84 // | | padding
85 // | Method* | <- sp
86 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
87 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
88 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Stuart Monteithb95a5342014-03-12 13:32:32 +000089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =16; // Offset of first FPR arg.
90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 304; // Frame size.
93 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000094 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 }
Ian Rogers848871b2013-08-05 10:56:33 -070096#elif defined(__mips__)
97 // The callee save frame is pointed to by SP.
98 // | argN | |
99 // | ... | |
100 // | arg4 | |
101 // | arg3 spill | | Caller's frame
102 // | arg2 spill | |
103 // | arg1 spill | |
104 // | Method* | ---
105 // | RA |
106 // | ... | callee saves
107 // | A3 | arg3
108 // | A2 | arg2
109 // | A1 | arg1
110 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800111 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
112 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
113 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size.
118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800120 }
Ian Rogers848871b2013-08-05 10:56:33 -0700121#elif defined(__i386__)
122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | Return |
131 // | EBP,ESI,EDI | callee saves
132 // | EBX | arg3
133 // | EDX | arg2
134 // | ECX | arg1
135 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
142 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size.
143 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000144 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800145 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800147 // The callee save frame is pointed to by SP.
148 // | argN | |
149 // | ... | |
150 // | reg. arg spills | | Caller's frame
151 // | Method* | ---
152 // | Return |
153 // | R15 | callee save
154 // | R14 | callee save
155 // | R13 | callee save
156 // | R12 | callee save
157 // | R9 | arg5
158 // | R8 | arg4
159 // | RSI/R6 | arg1
160 // | RBP/R5 | callee save
161 // | RBX/R3 | callee save
162 // | RDX/R2 | arg2
163 // | RCX/R1 | arg3
164 // | XMM7 | float arg 8
165 // | XMM6 | float arg 7
166 // | XMM5 | float arg 6
167 // | XMM4 | float arg 5
168 // | XMM3 | float arg 4
169 // | XMM2 | float arg 3
170 // | XMM1 | float arg 2
171 // | XMM0 | float arg 1
172 // | Padding |
173 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800174 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
175 static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs.
176 static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size.
181 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
182 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000183 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
186 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
187 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800188 default:
189 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
190 return 0;
191 }
192 }
Ian Rogers848871b2013-08-05 10:56:33 -0700193#else
194#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700195#endif
196
Ian Rogers936b37f2014-02-14 00:52:24 -0800197 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700198 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700200 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800201 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700202 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700203 }
204
Ian Rogers936b37f2014-02-14 00:52:24 -0800205 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700206 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700208 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800209 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700210 return *reinterpret_cast<uintptr_t*>(lr);
211 }
212
Andreas Gampecf4035a2014-05-28 22:43:01 -0700213 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700214 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
216 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
217 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
218 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
219 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
220 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
221 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
222 is_split_long_or_double_(false) {
223 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
Ian Rogers848871b2013-08-05 10:56:33 -0700224 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
225 }
226
227 virtual ~QuickArgumentVisitor() {}
228
229 virtual void Visit() = 0;
230
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 Primitive::Type GetParamPrimitiveType() const {
232 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700233 }
234
235 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800236 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 Primitive::Type type = GetParamPrimitiveType();
238 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800239 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000240 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700242 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800243 }
244 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800245 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800246 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
247 }
248 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700249 }
250
251 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000252 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 return is_split_long_or_double_;
254 } else {
255 return false; // An optimization for when GPR and FPRs are 64bit.
256 }
Ian Rogers848871b2013-08-05 10:56:33 -0700257 }
258
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700260 return GetParamPrimitiveType() == Primitive::kPrimNot;
261 }
262
Ian Rogers936b37f2014-02-14 00:52:24 -0800263 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700264 Primitive::Type type = GetParamPrimitiveType();
265 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
266 }
267
268 uint64_t ReadSplitLongParam() const {
269 DCHECK(IsSplitLongOrDouble());
270 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
271 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
272 return (low_half & 0xffffffffULL) | (high_half << 32);
273 }
274
275 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700276 // This implementation doesn't support reg-spill area for hard float
277 // ABI targets such as x86_64 and aarch64. So, for those targets whose
278 // 'kQuickSoftFloatAbi' is 'false':
279 // (a) 'stack_args_' should point to the first method's argument
280 // (b) whatever the argument type it is, the 'stack_index_' should
281 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800282 gpr_index_ = 0;
283 fpr_index_ = 0;
284 stack_index_ = 0;
285 if (!is_static_) { // Handle this.
286 cur_type_ = Primitive::kPrimNot;
287 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700288 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700289 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
290 stack_index_++;
291 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800292 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 }
Ian Rogers848871b2013-08-05 10:56:33 -0700295 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
297 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
298 switch (cur_type_) {
299 case Primitive::kPrimNot:
300 case Primitive::kPrimBoolean:
301 case Primitive::kPrimByte:
302 case Primitive::kPrimChar:
303 case Primitive::kPrimShort:
304 case Primitive::kPrimInt:
305 is_split_long_or_double_ = false;
306 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700307 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
308 stack_index_++;
309 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800310 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800311 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 }
313 break;
314 case Primitive::kPrimFloat:
315 is_split_long_or_double_ = false;
316 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800317 if (kQuickSoftFloatAbi) {
318 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800319 gpr_index_++;
320 } else {
321 stack_index_++;
322 }
323 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800324 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800325 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800326 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700327 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800328 }
329 break;
330 case Primitive::kPrimDouble:
331 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800332 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000333 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800334 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800335 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700336 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800337 if (kBytesStackArgLocation == 4) {
338 stack_index_+= 2;
339 } else {
340 CHECK_EQ(kBytesStackArgLocation, 8U);
341 stack_index_++;
342 }
343 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700344 if (gpr_index_ < kNumQuickGprArgs) {
345 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000346 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700347 if (gpr_index_ < kNumQuickGprArgs) {
348 gpr_index_++;
349 } else if (kQuickSoftFloatAbi) {
350 stack_index_++;
351 }
352 }
353 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800358 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800359 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000360 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800361 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 }
364 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700365 }
366 if (kBytesStackArgLocation == 4) {
367 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700369 CHECK_EQ(kBytesStackArgLocation, 8U);
370 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800371 }
372 }
373 break;
374 default:
375 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
376 }
Ian Rogers848871b2013-08-05 10:56:33 -0700377 }
378 }
379
380 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
382 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800383 if (kQuickSoftFloatAbi) {
384 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000385 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700386 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700388 // For now, there is no reg-spill area for the targets with
389 // hard float ABI. So, the offset pointing to the first method's
390 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700391 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700392 }
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
394
395 const bool is_static_;
396 const char* const shorty_;
397 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
399 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
400 byte* const stack_args_; // Address of stack arguments in caller's frame.
401 uint32_t gpr_index_; // Index into spilled GPRs.
402 uint32_t fpr_index_; // Index into spilled FPRs.
403 uint32_t stack_index_; // Index into arguments on the stack.
404 // The current type of argument during VisitArguments.
405 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700406 // Does a 64bit parameter straddle the register and stack arguments?
407 bool is_split_long_or_double_;
408};
409
410// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800411class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700412 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700413 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
414 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
415 size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700416 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
417
Ian Rogers9758f792014-03-13 09:02:55 -0700418 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700419
420 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800421 ShadowFrame* const sf_;
422 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700423
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700424 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700425};
426
Ian Rogers9758f792014-03-13 09:02:55 -0700427void BuildQuickShadowFrameVisitor::Visit() {
428 Primitive::Type type = GetParamPrimitiveType();
429 switch (type) {
430 case Primitive::kPrimLong: // Fall-through.
431 case Primitive::kPrimDouble:
432 if (IsSplitLongOrDouble()) {
433 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
434 } else {
435 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
436 }
437 ++cur_reg_;
438 break;
439 case Primitive::kPrimNot: {
440 StackReference<mirror::Object>* stack_ref =
441 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
442 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
443 }
444 break;
445 case Primitive::kPrimBoolean: // Fall-through.
446 case Primitive::kPrimByte: // Fall-through.
447 case Primitive::kPrimChar: // Fall-through.
448 case Primitive::kPrimShort: // Fall-through.
449 case Primitive::kPrimInt: // Fall-through.
450 case Primitive::kPrimFloat:
451 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
452 break;
453 case Primitive::kPrimVoid:
454 LOG(FATAL) << "UNREACHABLE";
455 break;
456 }
457 ++cur_reg_;
458}
459
Brian Carlstromea46f952013-07-30 01:26:50 -0700460extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700461 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700462 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
463 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
464 // frame.
465 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
466
467 if (method->IsAbstract()) {
468 ThrowAbstractMethodError(method);
469 return 0;
470 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800471 DCHECK(!method->IsNative()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700472 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
473 MethodHelper mh(method);
474 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800475 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700476 uint16_t num_regs = code_item->registers_size_;
477 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
478 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
479 method, 0, memory));
480 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700481 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800482 mh.GetShortyLength(),
483 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700484 shadow_frame_builder.VisitArguments();
485 // Push a transition back into managed code onto the linked list in thread.
486 ManagedStack fragment;
487 self->PushManagedStackFragment(&fragment);
488 self->PushShadowFrame(shadow_frame);
489 self->EndAssertNoThreadSuspension(old_cause);
490
491 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
492 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700493 StackHandleScope<1> hs(self);
494 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
495 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800496 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700497 self->PopManagedStackFragment(fragment);
498 return 0;
499 }
500 }
501
502 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
503 // Pop transition.
504 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800505 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700506 return result.GetJ();
507 }
508}
509
510// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
511// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800512class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700513 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700514 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
515 const char* shorty, uint32_t shorty_len,
516 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Ian Rogers848871b2013-08-05 10:56:33 -0700517 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
518
Ian Rogers9758f792014-03-13 09:02:55 -0700519 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700520
Ian Rogers9758f792014-03-13 09:02:55 -0700521 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800522
Ian Rogers848871b2013-08-05 10:56:33 -0700523 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700524 ScopedObjectAccessUnchecked* const soa_;
525 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800526 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700527 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700528
Ian Rogers848871b2013-08-05 10:56:33 -0700529 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
530};
531
Ian Rogers9758f792014-03-13 09:02:55 -0700532void BuildQuickArgumentVisitor::Visit() {
533 jvalue val;
534 Primitive::Type type = GetParamPrimitiveType();
535 switch (type) {
536 case Primitive::kPrimNot: {
537 StackReference<mirror::Object>* stack_ref =
538 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
539 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
540 references_.push_back(std::make_pair(val.l, stack_ref));
541 break;
542 }
543 case Primitive::kPrimLong: // Fall-through.
544 case Primitive::kPrimDouble:
545 if (IsSplitLongOrDouble()) {
546 val.j = ReadSplitLongParam();
547 } else {
548 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
549 }
550 break;
551 case Primitive::kPrimBoolean: // Fall-through.
552 case Primitive::kPrimByte: // Fall-through.
553 case Primitive::kPrimChar: // Fall-through.
554 case Primitive::kPrimShort: // Fall-through.
555 case Primitive::kPrimInt: // Fall-through.
556 case Primitive::kPrimFloat:
557 val.i = *reinterpret_cast<jint*>(GetParamAddress());
558 break;
559 case Primitive::kPrimVoid:
560 LOG(FATAL) << "UNREACHABLE";
561 val.j = 0;
562 break;
563 }
564 args_->push_back(val);
565}
566
567void BuildQuickArgumentVisitor::FixupReferences() {
568 // Fixup any references which may have changed.
569 for (const auto& pair : references_) {
570 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700571 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700572 }
573}
574
Ian Rogers848871b2013-08-05 10:56:33 -0700575// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
576// which is responsible for recording callee save registers. We explicitly place into jobjects the
577// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
578// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700579extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700580 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700581 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700582 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700583 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
584 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700585 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
586 const char* old_cause =
587 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
588 // Register the top of the managed stack, making stack crawlable.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700589 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700590 self->SetTopOfStack(sp, 0);
591 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700592 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
593 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700594 self->VerifyStack();
595 // Start new JNI local reference state.
596 JNIEnvExt* env = self->GetJniEnv();
597 ScopedObjectAccessUnchecked soa(env);
598 ScopedJniEnvLocalRefState env_state(env);
599 // Create local ref. copies of proxy method and the receiver.
600 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
601
602 // Placing arguments into args vector and remove the receiver.
603 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700604 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700605 std::vector<jvalue> args;
606 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
607 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700608
Ian Rogers848871b2013-08-05 10:56:33 -0700609 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700610 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700611 args.erase(args.begin());
612
613 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700615 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700616 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
617 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
618
619 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
620 // that performs allocations.
621 self->EndAssertNoThreadSuspension(old_cause);
622 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
623 rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800624 // Restore references which might have moved.
625 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700626 return result.GetJ();
627}
628
629// Read object references held in arguments from quick frames and place in a JNI local references,
630// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800631class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700632 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700633 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
634 const char* shorty, uint32_t shorty_len,
635 ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700636 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
637
Ian Rogers9758f792014-03-13 09:02:55 -0700638 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700639
Ian Rogers9758f792014-03-13 09:02:55 -0700640 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700641
642 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700643 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800644 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700645 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700646 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700647};
648
Ian Rogers9758f792014-03-13 09:02:55 -0700649void RememberForGcArgumentVisitor::Visit() {
650 if (IsParamAReference()) {
651 StackReference<mirror::Object>* stack_ref =
652 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
653 jobject reference =
654 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
655 references_.push_back(std::make_pair(reference, stack_ref));
656 }
657}
658
659void RememberForGcArgumentVisitor::FixupReferences() {
660 // Fixup any references which may have changed.
661 for (const auto& pair : references_) {
662 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700663 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700664 }
665}
666
667
Ian Rogers848871b2013-08-05 10:56:33 -0700668// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700669extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700670 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700671 Thread* self,
672 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700673 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800674 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700675 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800676 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700677 ScopedObjectAccessUnchecked soa(env);
678 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800679 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700680
681 // Compute details about the called method (avoid GCs)
682 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700683 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700684 InvokeType invoke_type;
685 const DexFile* dex_file;
686 uint32_t dex_method_idx;
687 if (called->IsRuntimeMethod()) {
688 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
689 const DexFile::CodeItem* code;
690 {
691 MethodHelper mh(caller);
692 dex_file = &mh.GetDexFile();
693 code = mh.GetCodeItem();
694 }
695 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
696 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
697 Instruction::Code instr_code = instr->Opcode();
698 bool is_range;
699 switch (instr_code) {
700 case Instruction::INVOKE_DIRECT:
701 invoke_type = kDirect;
702 is_range = false;
703 break;
704 case Instruction::INVOKE_DIRECT_RANGE:
705 invoke_type = kDirect;
706 is_range = true;
707 break;
708 case Instruction::INVOKE_STATIC:
709 invoke_type = kStatic;
710 is_range = false;
711 break;
712 case Instruction::INVOKE_STATIC_RANGE:
713 invoke_type = kStatic;
714 is_range = true;
715 break;
716 case Instruction::INVOKE_SUPER:
717 invoke_type = kSuper;
718 is_range = false;
719 break;
720 case Instruction::INVOKE_SUPER_RANGE:
721 invoke_type = kSuper;
722 is_range = true;
723 break;
724 case Instruction::INVOKE_VIRTUAL:
725 invoke_type = kVirtual;
726 is_range = false;
727 break;
728 case Instruction::INVOKE_VIRTUAL_RANGE:
729 invoke_type = kVirtual;
730 is_range = true;
731 break;
732 case Instruction::INVOKE_INTERFACE:
733 invoke_type = kInterface;
734 is_range = false;
735 break;
736 case Instruction::INVOKE_INTERFACE_RANGE:
737 invoke_type = kInterface;
738 is_range = true;
739 break;
740 default:
741 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
742 // Avoid used uninitialized warnings.
743 invoke_type = kDirect;
744 is_range = false;
745 }
746 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
747
748 } else {
749 invoke_type = kStatic;
750 dex_file = &MethodHelper(called).GetDexFile();
751 dex_method_idx = called->GetDexMethodIndex();
752 }
753 uint32_t shorty_len;
754 const char* shorty =
755 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700756 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700757 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800758 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800759 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700760 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700761 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700762 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700763 mirror::Object* dummy = nullptr;
764 HandleWrapper<mirror::Object> h_receiver(
765 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
766 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700767 }
768 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800769 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700770 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800771 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
772 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800773 if (virtual_or_interface) {
774 // Refine called method based on receiver.
775 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700776
777 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800778 if (invoke_type == kVirtual) {
779 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
780 } else {
781 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
782 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700783
784 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
785 << PrettyTypeOf(receiver) << " "
786 << invoke_type << " " << orig_called->GetVtableIndex();
787
Ian Rogers83883d72013-10-21 21:07:24 -0700788 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
789 // of the sharpened method.
790 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100791 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700792 } else {
793 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000794 // the caller's dex file. Since we get here only if the original called was a runtime
795 // method, we've got the correct dex_file and a dex_method_idx from above.
796 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700797 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000798 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700799 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100800 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700801 }
802 }
803 }
Ian Rogers848871b2013-08-05 10:56:33 -0700804 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700805 StackHandleScope<1> hs(soa.Self());
806 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700807 linker->EnsureInitialized(called_class, true, true);
808 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700810 } else if (called_class->IsInitializing()) {
811 if (invoke_type == kStatic) {
812 // Class is still initializing, go to oat and grab code (trampoline must be left in place
813 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800814 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700815 } else {
816 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800817 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700818 }
819 } else {
820 DCHECK(called_class->IsErroneous());
821 }
822 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800823 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700824 // Fixup any locally saved objects may have moved during a GC.
825 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700826 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700827 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700828 return code;
829}
830
Andreas Gampec147b002014-03-06 18:11:06 -0800831
832
833/*
834 * This class uses a couple of observations to unite the different calling conventions through
835 * a few constants.
836 *
837 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
838 * possible alignment.
839 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
840 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
841 * when we have to split things
842 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
843 * and we can use Int handling directly.
844 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
845 * necessary when widening. Also, widening of Ints will take place implicitly, and the
846 * extension should be compatible with Aarch64, which mandates copying the available bits
847 * into LSB and leaving the rest unspecified.
848 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
849 * the stack.
850 * 6) There is only little endian.
851 *
852 *
853 * Actual work is supposed to be done in a delegate of the template type. The interface is as
854 * follows:
855 *
856 * void PushGpr(uintptr_t): Add a value for the next GPR
857 *
858 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
859 * padding, that is, think the architecture is 32b and aligns 64b.
860 *
861 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
862 * split this if necessary. The current state will have aligned, if
863 * necessary.
864 *
865 * void PushStack(uintptr_t): Push a value to the stack.
866 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700867 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700868 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800869 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700870 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800871 *
872 */
873template <class T> class BuildGenericJniFrameStateMachine {
874 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800875#if defined(__arm__)
876 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800877 static constexpr bool kNativeSoftFloatAbi = true;
878 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800879 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
880
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800881 static constexpr size_t kRegistersNeededForLong = 2;
882 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800883 static constexpr bool kMultiRegistersAligned = true;
884 static constexpr bool kMultiRegistersWidened = false;
885 static constexpr bool kAlignLongOnStack = true;
886 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000887#elif defined(__aarch64__)
888 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
889 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
890 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
891
892 static constexpr size_t kRegistersNeededForLong = 1;
893 static constexpr size_t kRegistersNeededForDouble = 1;
894 static constexpr bool kMultiRegistersAligned = false;
895 static constexpr bool kMultiRegistersWidened = false;
896 static constexpr bool kAlignLongOnStack = false;
897 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800898#elif defined(__mips__)
899 // TODO: These are all dummy values!
900 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
901 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
902 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
903
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800904 static constexpr size_t kRegistersNeededForLong = 2;
905 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800906 static constexpr bool kMultiRegistersAligned = true;
907 static constexpr bool kMultiRegistersWidened = true;
908 static constexpr bool kAlignLongOnStack = false;
909 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800910#elif defined(__i386__)
911 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800912 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800913 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
914 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
915
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800916 static constexpr size_t kRegistersNeededForLong = 2;
917 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800918 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
919 static constexpr bool kMultiRegistersWidened = false;
920 static constexpr bool kAlignLongOnStack = false;
921 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800922#elif defined(__x86_64__)
923 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
924 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
925 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
926
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800927 static constexpr size_t kRegistersNeededForLong = 1;
928 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800929 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800930 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800931 static constexpr bool kAlignLongOnStack = false;
932 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800933#else
934#error "Unsupported architecture"
935#endif
936
Andreas Gampec147b002014-03-06 18:11:06 -0800937 public:
938 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs),
939 fpr_index_(kNumNativeFprArgs),
940 stack_entries_(0),
941 delegate_(delegate) {
942 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
943 // the next register is even; counting down is just to make the compiler happy...
944 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
945 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
946 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800947
Andreas Gampec147b002014-03-06 18:11:06 -0800948 virtual ~BuildGenericJniFrameStateMachine() {}
949
950 bool HavePointerGpr() {
951 return gpr_index_ > 0;
952 }
953
954 void AdvancePointer(void* val) {
955 if (HavePointerGpr()) {
956 gpr_index_--;
957 PushGpr(reinterpret_cast<uintptr_t>(val));
958 } else {
959 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
960 PushStack(reinterpret_cast<uintptr_t>(val));
961 gpr_index_ = 0;
962 }
963 }
964
965
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700966 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800967 return gpr_index_ > 0;
968 }
969
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700970 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
971 uintptr_t handle = PushHandle(ptr);
972 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800973 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700974 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800975 } else {
976 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700977 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800978 gpr_index_ = 0;
979 }
980 }
981
982
983 bool HaveIntGpr() {
984 return gpr_index_ > 0;
985 }
986
987 void AdvanceInt(uint32_t val) {
988 if (HaveIntGpr()) {
989 gpr_index_--;
990 PushGpr(val);
991 } else {
992 stack_entries_++;
993 PushStack(val);
994 gpr_index_ = 0;
995 }
996 }
997
998
999 bool HaveLongGpr() {
1000 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1001 }
1002
1003 bool LongGprNeedsPadding() {
1004 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1005 kAlignLongOnStack && // and when it needs alignment
1006 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1007 }
1008
1009 bool LongStackNeedsPadding() {
1010 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1011 kAlignLongOnStack && // and when it needs 8B alignment
1012 (stack_entries_ & 1) == 1; // counter is odd
1013 }
1014
1015 void AdvanceLong(uint64_t val) {
1016 if (HaveLongGpr()) {
1017 if (LongGprNeedsPadding()) {
1018 PushGpr(0);
1019 gpr_index_--;
1020 }
1021 if (kRegistersNeededForLong == 1) {
1022 PushGpr(static_cast<uintptr_t>(val));
1023 } else {
1024 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1025 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1026 }
1027 gpr_index_ -= kRegistersNeededForLong;
1028 } else {
1029 if (LongStackNeedsPadding()) {
1030 PushStack(0);
1031 stack_entries_++;
1032 }
1033 if (kRegistersNeededForLong == 1) {
1034 PushStack(static_cast<uintptr_t>(val));
1035 stack_entries_++;
1036 } else {
1037 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1038 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1039 stack_entries_ += 2;
1040 }
1041 gpr_index_ = 0;
1042 }
1043 }
1044
1045
1046 bool HaveFloatFpr() {
1047 return fpr_index_ > 0;
1048 }
1049
Andreas Gampec147b002014-03-06 18:11:06 -08001050 template <typename U, typename V> V convert(U in) {
1051 CHECK_LE(sizeof(U), sizeof(V));
1052 union { U u; V v; } tmp;
1053 tmp.u = in;
1054 return tmp.v;
1055 }
1056
1057 void AdvanceFloat(float val) {
1058 if (kNativeSoftFloatAbi) {
1059 AdvanceInt(convert<float, uint32_t>(val));
1060 } else {
1061 if (HaveFloatFpr()) {
1062 fpr_index_--;
1063 if (kRegistersNeededForDouble == 1) {
1064 if (kMultiRegistersWidened) {
1065 PushFpr8(convert<double, uint64_t>(val));
1066 } else {
1067 // No widening, just use the bits.
1068 PushFpr8(convert<float, uint64_t>(val));
1069 }
1070 } else {
1071 PushFpr4(val);
1072 }
1073 } else {
1074 stack_entries_++;
1075 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1076 // Need to widen before storing: Note the "double" in the template instantiation.
1077 PushStack(convert<double, uintptr_t>(val));
1078 } else {
1079 PushStack(convert<float, uintptr_t>(val));
1080 }
1081 fpr_index_ = 0;
1082 }
1083 }
1084 }
1085
1086
1087 bool HaveDoubleFpr() {
1088 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1089 }
1090
1091 bool DoubleFprNeedsPadding() {
1092 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1093 kAlignDoubleOnStack && // and when it needs alignment
1094 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1095 }
1096
1097 bool DoubleStackNeedsPadding() {
1098 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1099 kAlignDoubleOnStack && // and when it needs 8B alignment
1100 (stack_entries_ & 1) == 1; // counter is odd
1101 }
1102
1103 void AdvanceDouble(uint64_t val) {
1104 if (kNativeSoftFloatAbi) {
1105 AdvanceLong(val);
1106 } else {
1107 if (HaveDoubleFpr()) {
1108 if (DoubleFprNeedsPadding()) {
1109 PushFpr4(0);
1110 fpr_index_--;
1111 }
1112 PushFpr8(val);
1113 fpr_index_ -= kRegistersNeededForDouble;
1114 } else {
1115 if (DoubleStackNeedsPadding()) {
1116 PushStack(0);
1117 stack_entries_++;
1118 }
1119 if (kRegistersNeededForDouble == 1) {
1120 PushStack(static_cast<uintptr_t>(val));
1121 stack_entries_++;
1122 } else {
1123 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1124 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1125 stack_entries_ += 2;
1126 }
1127 fpr_index_ = 0;
1128 }
1129 }
1130 }
1131
1132 uint32_t getStackEntries() {
1133 return stack_entries_;
1134 }
1135
1136 uint32_t getNumberOfUsedGprs() {
1137 return kNumNativeGprArgs - gpr_index_;
1138 }
1139
1140 uint32_t getNumberOfUsedFprs() {
1141 return kNumNativeFprArgs - fpr_index_;
1142 }
1143
1144 private:
1145 void PushGpr(uintptr_t val) {
1146 delegate_->PushGpr(val);
1147 }
1148 void PushFpr4(float val) {
1149 delegate_->PushFpr4(val);
1150 }
1151 void PushFpr8(uint64_t val) {
1152 delegate_->PushFpr8(val);
1153 }
1154 void PushStack(uintptr_t val) {
1155 delegate_->PushStack(val);
1156 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001157 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1158 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001159 }
1160
1161 uint32_t gpr_index_; // Number of free GPRs
1162 uint32_t fpr_index_; // Number of free FPRs
1163 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1164 // extended
1165 T* delegate_; // What Push implementation gets called
1166};
1167
1168class ComputeGenericJniFrameSize FINAL {
1169 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001170 ComputeGenericJniFrameSize() : num_handle_scope_references_(0), num_stack_entries_(0) {}
Andreas Gampec147b002014-03-06 18:11:06 -08001171
Andreas Gampec147b002014-03-06 18:11:06 -08001172 uint32_t GetStackSize() {
1173 return num_stack_entries_ * sizeof(uintptr_t);
1174 }
1175
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001176 // WARNING: After this, *sp won't be pointing to the method anymore!
Andreas Gampecf4035a2014-05-28 22:43:01 -07001177 void ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty,
1178 uint32_t shorty_len, void* sp, HandleScope** table,
1179 uint32_t* handle_scope_entries, uintptr_t** start_stack, uintptr_t** start_gpr,
1180 uint32_t** start_fpr, void** code_return, size_t* overall_size)
Andreas Gampec147b002014-03-06 18:11:06 -08001181 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1182 ComputeAll(is_static, shorty, shorty_len);
1183
Andreas Gampecf4035a2014-05-28 22:43:01 -07001184 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001185
Andreas Gampec147b002014-03-06 18:11:06 -08001186 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
Andreas Gampec147b002014-03-06 18:11:06 -08001187
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001188 // First, fix up the layout of the callee-save frame.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001189 // We have to squeeze in the HandleScope, and relocate the method pointer.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001190
1191 // "Free" the slot for the method.
Andreas Gampecf4035a2014-05-28 22:43:01 -07001192 sp8 += kPointerSize; // In the callee-save frame we use a full pointer.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001193
Andreas Gampecf4035a2014-05-28 22:43:01 -07001194 // Under the callee saves put handle scope and new method stack reference.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001195 *handle_scope_entries = num_handle_scope_references_;
Andreas Gampecf4035a2014-05-28 22:43:01 -07001196
1197 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1198 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1199
1200 sp8 -= scope_and_method;
1201 // Align by kStackAlignment
1202 uintptr_t sp_to_align = reinterpret_cast<uintptr_t>(sp8);
1203 sp_to_align = RoundDown(sp_to_align, kStackAlignment);
1204 sp8 = reinterpret_cast<uint8_t*>(sp_to_align);
1205
1206 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1207 *table = reinterpret_cast<HandleScope*>(sp8_table);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001208 (*table)->SetNumberOfReferences(num_handle_scope_references_);
Andreas Gampec147b002014-03-06 18:11:06 -08001209
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001210 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001211 uint8_t* method_pointer = sp8;
Andreas Gampecf4035a2014-05-28 22:43:01 -07001212 StackReference<mirror::ArtMethod>* new_method_ref =
1213 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1214 new_method_ref->Assign(method);
1215 *m = new_method_ref;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001216
1217 // Reference cookie and padding
1218 sp8 -= 8;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001219 // Store HandleScope size
1220 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(handle_scope_size & 0xFFFFFFFF);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001221
1222 // Next comes the native call stack.
Andreas Gampec147b002014-03-06 18:11:06 -08001223 sp8 -= GetStackSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001224 // Now align the call stack below. This aligns by 16, as AArch64 seems to require.
Andreas Gampec147b002014-03-06 18:11:06 -08001225 uintptr_t mask = ~0x0F;
1226 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask);
1227 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
1228
1229 // put fprs and gprs below
1230 // Assumption is OK right now, as we have soft-float arm
1231 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs;
1232 sp8 -= fregs * sizeof(uintptr_t);
1233 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1234 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs;
1235 sp8 -= iregs * sizeof(uintptr_t);
1236 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1237
1238 // reserve space for the code pointer
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001239 sp8 -= kPointerSize;
Andreas Gampec147b002014-03-06 18:11:06 -08001240 *code_return = reinterpret_cast<void*>(sp8);
1241
1242 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001243
1244 // The new SP is stored at the end of the alloca, so it can be immediately popped
1245 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB;
1246 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer;
Andreas Gampec147b002014-03-06 18:11:06 -08001247 }
1248
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001249 void ComputeHandleScopeOffset() { } // nothing to do, static right now
Andreas Gampec147b002014-03-06 18:11:06 -08001250
1251 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len)
1252 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1253 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this);
1254
1255 // JNIEnv
1256 sm.AdvancePointer(nullptr);
1257
1258 // Class object or this as first argument
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001259 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001260
1261 for (uint32_t i = 1; i < shorty_len; ++i) {
1262 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1263 switch (cur_type_) {
1264 case Primitive::kPrimNot:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001265 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001266 break;
1267
1268 case Primitive::kPrimBoolean:
1269 case Primitive::kPrimByte:
1270 case Primitive::kPrimChar:
1271 case Primitive::kPrimShort:
1272 case Primitive::kPrimInt:
1273 sm.AdvanceInt(0);
1274 break;
1275 case Primitive::kPrimFloat:
1276 sm.AdvanceFloat(0);
1277 break;
1278 case Primitive::kPrimDouble:
1279 sm.AdvanceDouble(0);
1280 break;
1281 case Primitive::kPrimLong:
1282 sm.AdvanceLong(0);
1283 break;
1284 default:
1285 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1286 }
1287 }
1288
1289 num_stack_entries_ = sm.getStackEntries();
1290 }
1291
1292 void PushGpr(uintptr_t /* val */) {
1293 // not optimizing registers, yet
1294 }
1295
1296 void PushFpr4(float /* val */) {
1297 // not optimizing registers, yet
1298 }
1299
1300 void PushFpr8(uint64_t /* val */) {
1301 // not optimizing registers, yet
1302 }
1303
1304 void PushStack(uintptr_t /* val */) {
1305 // counting is already done in the superclass
1306 }
1307
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001308 uintptr_t PushHandle(mirror::Object* /* ptr */) {
1309 num_handle_scope_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001310 return reinterpret_cast<uintptr_t>(nullptr);
1311 }
1312
1313 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001314 uint32_t num_handle_scope_references_;
Andreas Gampec147b002014-03-06 18:11:06 -08001315 uint32_t num_stack_entries_;
1316};
1317
1318// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1319// of transitioning into native code.
1320class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001321 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -07001322 BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static,
1323 const char* shorty, uint32_t shorty_len, Thread* self) :
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001324 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) {
Andreas Gampec147b002014-03-06 18:11:06 -08001325 ComputeGenericJniFrameSize fsc;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001326 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &handle_scope_, &handle_scope_expected_refs_,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001327 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_,
1328 &alloca_used_size_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001329 handle_scope_number_of_references_ = 0;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001330 cur_hs_entry_ = GetFirstHandleScopeEntry();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001331
1332 // jni environment is always first argument
Andreas Gampec147b002014-03-06 18:11:06 -08001333 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001334
1335 if (is_static) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001336 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001337 }
1338 }
1339
Ian Rogers9758f792014-03-13 09:02:55 -07001340 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001341
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001342 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001343
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001344 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1345 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1346 return handle_scope_->GetHandle(0).GetReference();
1347 }
1348
1349 jobject GetFirstHandleScopeJObject()
1350 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001351 return handle_scope_->GetHandle(0).ToJObject();
Andreas Gampec147b002014-03-06 18:11:06 -08001352 }
1353
1354 void PushGpr(uintptr_t val) {
1355 *cur_gpr_reg_ = val;
1356 cur_gpr_reg_++;
1357 }
1358
1359 void PushFpr4(float val) {
1360 *cur_fpr_reg_ = val;
1361 cur_fpr_reg_++;
1362 }
1363
1364 void PushFpr8(uint64_t val) {
1365 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1366 *tmp = val;
1367 cur_fpr_reg_ += 2;
1368 }
1369
1370 void PushStack(uintptr_t val) {
1371 *cur_stack_arg_ = val;
1372 cur_stack_arg_++;
1373 }
1374
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001375 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001376 uintptr_t tmp;
1377 if (ref == nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001378 *cur_hs_entry_ = StackReference<mirror::Object>();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001379 tmp = reinterpret_cast<uintptr_t>(nullptr);
1380 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001381 *cur_hs_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref);
1382 tmp = reinterpret_cast<uintptr_t>(cur_hs_entry_);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001383 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001384 cur_hs_entry_++;
1385 handle_scope_number_of_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001386 return tmp;
1387 }
1388
1389 // Size of the part of the alloca that we actually need.
1390 size_t GetAllocaUsedSize() {
1391 return alloca_used_size_;
1392 }
1393
1394 void* GetCodeReturn() {
1395 return code_return_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001396 }
1397
1398 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001399 uint32_t handle_scope_number_of_references_;
1400 StackReference<mirror::Object>* cur_hs_entry_;
1401 HandleScope* handle_scope_;
1402 uint32_t handle_scope_expected_refs_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001403 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001404 uint32_t* cur_fpr_reg_;
1405 uintptr_t* cur_stack_arg_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001406 // StackReference<mirror::Object>* top_of_handle_scope_;
Andreas Gampec147b002014-03-06 18:11:06 -08001407 void* code_return_;
1408 size_t alloca_used_size_;
1409
1410 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001411
1412 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1413};
1414
Ian Rogers9758f792014-03-13 09:02:55 -07001415void BuildGenericJniFrameVisitor::Visit() {
1416 Primitive::Type type = GetParamPrimitiveType();
1417 switch (type) {
1418 case Primitive::kPrimLong: {
1419 jlong long_arg;
1420 if (IsSplitLongOrDouble()) {
1421 long_arg = ReadSplitLongParam();
1422 } else {
1423 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1424 }
1425 sm_.AdvanceLong(long_arg);
1426 break;
1427 }
1428 case Primitive::kPrimDouble: {
1429 uint64_t double_arg;
1430 if (IsSplitLongOrDouble()) {
1431 // Read into union so that we don't case to a double.
1432 double_arg = ReadSplitLongParam();
1433 } else {
1434 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1435 }
1436 sm_.AdvanceDouble(double_arg);
1437 break;
1438 }
1439 case Primitive::kPrimNot: {
1440 StackReference<mirror::Object>* stack_ref =
1441 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001442 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001443 break;
1444 }
1445 case Primitive::kPrimFloat:
1446 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1447 break;
1448 case Primitive::kPrimBoolean: // Fall-through.
1449 case Primitive::kPrimByte: // Fall-through.
1450 case Primitive::kPrimChar: // Fall-through.
1451 case Primitive::kPrimShort: // Fall-through.
1452 case Primitive::kPrimInt: // Fall-through.
1453 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1454 break;
1455 case Primitive::kPrimVoid:
1456 LOG(FATAL) << "UNREACHABLE";
1457 break;
1458 }
1459}
1460
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001461void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Ian Rogers9758f792014-03-13 09:02:55 -07001462 // Initialize padding entries.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001463 while (handle_scope_number_of_references_ < handle_scope_expected_refs_) {
1464 *cur_hs_entry_ = StackReference<mirror::Object>();
1465 cur_hs_entry_++;
1466 handle_scope_number_of_references_++;
Ian Rogers9758f792014-03-13 09:02:55 -07001467 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001468 handle_scope_->SetNumberOfReferences(handle_scope_expected_refs_);
1469 DCHECK_NE(handle_scope_expected_refs_, 0U);
1470 // Install HandleScope.
1471 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001472}
1473
Andreas Gampe90546832014-03-12 18:07:19 -07001474extern "C" void* artFindNativeMethod();
1475
Andreas Gampead615172014-04-04 16:20:13 -07001476uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1477 if (lock != nullptr) {
1478 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1479 } else {
1480 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1481 }
1482}
1483
1484void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1485 if (lock != nullptr) {
1486 JniMethodEndSynchronized(cookie, lock, self);
1487 } else {
1488 JniMethodEnd(cookie, self);
1489 }
1490}
1491
Andreas Gampec147b002014-03-06 18:11:06 -08001492/*
1493 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001494 * 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 -08001495 * The final element on the stack is a pointer to the native code.
1496 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001497 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001498 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001499 *
Andreas Gampec147b002014-03-06 18:11:06 -08001500 * The return of this function denotes:
1501 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1502 * 2) An error, if the value is negative.
1503 */
Andreas Gampecf4035a2014-05-28 22:43:01 -07001504extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001505 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001506 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001507 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001508
1509 // run the visitor
1510 MethodHelper mh(called);
Andreas Gampec147b002014-03-06 18:11:06 -08001511
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001512 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(),
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001513 self);
1514 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001515 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001516
1517 // fix up managed-stack things in Thread
1518 self->SetTopOfStack(sp, 0);
1519
Ian Rogerse0dcd462014-03-08 15:21:04 -08001520 self->VerifyStack();
1521
Andreas Gampe90546832014-03-12 18:07:19 -07001522 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001523 uint32_t cookie;
1524 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001525 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001526 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001527 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001528 // A negative value denotes an error.
1529 return -1;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001530 }
1531 } else {
1532 cookie = JniMethodStart(self);
1533 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001534 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001535 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001536
Andreas Gampe90546832014-03-12 18:07:19 -07001537 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001538 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001539
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001540 // There are two cases for the content of nativeCode:
1541 // 1) Pointer to the native function.
1542 // 2) Pointer to the trampoline for native code binding.
1543 // In the second case, we need to execute the binding and continue with the actual native function
1544 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001545 DCHECK(nativeCode != nullptr);
1546 if (nativeCode == GetJniDlsymLookupStub()) {
1547 nativeCode = artFindNativeMethod();
1548
1549 if (nativeCode == nullptr) {
1550 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001551
1552 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001553 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Andreas Gampead615172014-04-04 16:20:13 -07001554 if (mh.GetShorty()[0] == 'L') {
1555 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1556 } else {
1557 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1558 }
1559
Andreas Gampe90546832014-03-12 18:07:19 -07001560 return -1;
1561 }
1562 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563 }
1564
Andreas Gampe90546832014-03-12 18:07:19 -07001565 // Store the native code pointer in the stack at the right location.
Andreas Gampec147b002014-03-06 18:11:06 -08001566 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn());
Andreas Gampec147b002014-03-06 18:11:06 -08001567 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode);
1568
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001569 // 5K reserved, window_size + frame pointer used.
Andreas Gampe90546832014-03-12 18:07:19 -07001570 size_t window_size = visitor.GetAllocaUsedSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001571 return (5 * KB) - window_size - kPointerSize;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001572}
1573
1574/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001575 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001576 * unlocking.
1577 */
Andreas Gampecf4035a2014-05-28 22:43:01 -07001578extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
1579 StackReference<mirror::ArtMethod>* sp,
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001580 jvalue result, uint64_t result_f)
1581 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1582 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001583 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001584 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001585
Andreas Gampead615172014-04-04 16:20:13 -07001586 jobject lock = nullptr;
1587 if (called->IsSynchronized()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001588 HandleScope* table = reinterpret_cast<HandleScope*>(
Andreas Gampecf4035a2014-05-28 22:43:01 -07001589 reinterpret_cast<uint8_t*>(sp) + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001590 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001591 }
1592
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001593 MethodHelper mh(called);
1594 char return_shorty_char = mh.GetShorty()[0];
1595
1596 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001597 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001598 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001599 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001600
1601 switch (return_shorty_char) {
1602 case 'F': // Fall-through.
1603 case 'D':
1604 return result_f;
1605 case 'Z':
1606 return result.z;
1607 case 'B':
1608 return result.b;
1609 case 'C':
1610 return result.c;
1611 case 'S':
1612 return result.s;
1613 case 'I':
1614 return result.i;
1615 case 'J':
1616 return result.j;
1617 case 'V':
1618 return 0;
1619 default:
1620 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1621 return 0;
1622 }
1623 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001624}
1625
Andreas Gampe51f76352014-05-21 08:28:48 -07001626// The following definitions create return types for two word-sized entities that will be passed
1627// in registers so that memory operations for the interface trampolines can be avoided. The entities
1628// are the resolved method and the pointer to the code to be invoked.
1629//
1630// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
1631// uint64_t or long long int. We use the upper 32b for code, and the lower 32b for the method.
1632//
1633// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two
1634// size_t-sized values.
1635//
1636// We need two operations:
1637//
1638// 1) A flag value that signals failure. The assembly stubs expect the method part to be "0".
1639// GetFailureValue() will return a value that has method == 0.
1640//
1641// 2) A value that combines a code pointer and a method pointer.
1642// GetSuccessValue() constructs this.
1643
1644#if defined(__i386__) || defined(__arm__) || defined(__mips__)
1645typedef uint64_t MethodAndCode;
1646
1647// Encodes method_ptr==nullptr and code_ptr==nullptr
1648static constexpr MethodAndCode GetFailureValue() {
1649 return 0;
1650}
1651
1652// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
1653static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1654 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1655 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1656 return ((code_uint << 32) | method_uint);
1657}
1658
1659#elif defined(__x86_64__) || defined(__aarch64__)
1660struct MethodAndCode {
1661 uintptr_t method;
1662 uintptr_t code;
1663};
1664
1665// Encodes method_ptr==nullptr. Leaves random value in code pointer.
1666static MethodAndCode GetFailureValue() {
1667 MethodAndCode ret;
1668 ret.method = 0;
1669 return ret;
1670}
1671
1672// Write values into their respective members.
1673static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1674 MethodAndCode ret;
1675 ret.method = reinterpret_cast<uintptr_t>(method);
1676 ret.code = reinterpret_cast<uintptr_t>(code);
1677 return ret;
1678}
1679#else
1680#error "Unsupported architecture"
1681#endif
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001682
1683template<InvokeType type, bool access_check>
Andreas Gampe51f76352014-05-21 08:28:48 -07001684static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1685 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001686 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001687
1688template<InvokeType type, bool access_check>
1689static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1690 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001691 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001692 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1693 type);
1694 if (UNLIKELY(method == nullptr)) {
1695 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1696 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1697 uint32_t shorty_len;
1698 const char* shorty =
1699 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
1700 {
1701 // Remember the args in case a GC happens in FindMethodFromCode.
1702 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1703 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1704 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001705 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1706 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001707 visitor.FixupReferences();
1708 }
1709
1710 if (UNLIKELY(method == NULL)) {
1711 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001712 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001713 }
1714 }
1715 DCHECK(!self->IsExceptionPending());
1716 const void* code = method->GetEntryPointFromQuickCompiledCode();
1717
1718 // When we return, the caller will branch to this address, so it had better not be 0!
1719 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1720 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001721
1722 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001723}
1724
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001725// Explicit artInvokeCommon template function declarations to please analysis tool.
1726#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1727 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gampe51f76352014-05-21 08:28:48 -07001728 MethodAndCode artInvokeCommon<type, access_check>(uint32_t method_idx, \
1729 mirror::Object* this_object, \
1730 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001731 Thread* self, \
1732 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001733
1734EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1735EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1736EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1737EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1738EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1739EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1740EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1741EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1742EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1743EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1744#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1745
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001746
1747// See comments in runtime_support_asm.S
Andreas Gampe51f76352014-05-21 08:28:48 -07001748extern "C" MethodAndCode artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001749 mirror::Object* this_object,
1750 mirror::ArtMethod* caller_method,
1751 Thread* self,
1752 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001753 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp);
1754}
1755
1756
Andreas Gampe51f76352014-05-21 08:28:48 -07001757extern "C" MethodAndCode artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001758 mirror::Object* this_object,
1759 mirror::ArtMethod* caller_method,
1760 Thread* self,
1761 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001762 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp);
1763}
1764
Andreas Gampe51f76352014-05-21 08:28:48 -07001765extern "C" MethodAndCode artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001766 mirror::Object* this_object,
1767 mirror::ArtMethod* caller_method,
1768 Thread* self,
1769 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001770 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp);
1771}
1772
Andreas Gampe51f76352014-05-21 08:28:48 -07001773extern "C" MethodAndCode artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001774 mirror::Object* this_object,
1775 mirror::ArtMethod* caller_method,
1776 Thread* self,
1777 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001778 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp);
1779}
1780
Andreas Gampe51f76352014-05-21 08:28:48 -07001781extern "C" MethodAndCode artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001782 mirror::Object* this_object,
1783 mirror::ArtMethod* caller_method,
1784 Thread* self,
1785 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001786 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp);
1787}
1788
1789// Determine target of interface dispatch. This object is known non-null.
Andreas Gampe51f76352014-05-21 08:28:48 -07001790extern "C" MethodAndCode artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
1791 mirror::Object* this_object,
1792 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001793 Thread* self,
1794 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001795 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1796 mirror::ArtMethod* method;
1797 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1798 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1799 if (UNLIKELY(method == NULL)) {
1800 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1801 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1802 caller_method);
Andreas Gampe51f76352014-05-21 08:28:48 -07001803 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001804 }
1805 } else {
1806 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1807 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
1808 // Determine method index from calling dex instruction.
1809#if defined(__arm__)
1810 // On entry the stack pointed by sp is:
1811 // | argN | |
1812 // | ... | |
1813 // | arg4 | |
1814 // | arg3 spill | | Caller's frame
1815 // | arg2 spill | |
1816 // | arg1 spill | |
1817 // | Method* | ---
1818 // | LR |
1819 // | ... | callee saves
1820 // | R3 | arg3
1821 // | R2 | arg2
1822 // | R1 | arg1
1823 // | R0 |
1824 // | Method* | <- sp
1825 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1826 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
1827 uintptr_t caller_pc = regs[10];
1828#elif defined(__i386__)
1829 // On entry the stack pointed by sp is:
1830 // | argN | |
1831 // | ... | |
1832 // | arg4 | |
1833 // | arg3 spill | | Caller's frame
1834 // | arg2 spill | |
1835 // | arg1 spill | |
1836 // | Method* | ---
1837 // | Return |
1838 // | EBP,ESI,EDI | callee saves
1839 // | EBX | arg3
1840 // | EDX | arg2
1841 // | ECX | arg1
1842 // | EAX/Method* | <- sp
1843 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1844 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1845 uintptr_t caller_pc = regs[7];
1846#elif defined(__mips__)
1847 // On entry the stack pointed by sp is:
1848 // | argN | |
1849 // | ... | |
1850 // | arg4 | |
1851 // | arg3 spill | | Caller's frame
1852 // | arg2 spill | |
1853 // | arg1 spill | |
1854 // | Method* | ---
1855 // | RA |
1856 // | ... | callee saves
1857 // | A3 | arg3
1858 // | A2 | arg2
1859 // | A1 | arg1
1860 // | A0/Method* | <- sp
1861 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1862 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1863 uintptr_t caller_pc = regs[15];
1864#else
1865 UNIMPLEMENTED(FATAL);
1866 uintptr_t caller_pc = 0;
1867#endif
1868 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
1869 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
1870 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1871 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1872 Instruction::Code instr_code = instr->Opcode();
1873 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1874 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1875 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1876 uint32_t dex_method_idx;
1877 if (instr_code == Instruction::INVOKE_INTERFACE) {
1878 dex_method_idx = instr->VRegB_35c();
1879 } else {
1880 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1881 dex_method_idx = instr->VRegB_3rc();
1882 }
1883
1884 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1885 uint32_t shorty_len;
1886 const char* shorty =
1887 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
1888 {
1889 // Remember the args in case a GC happens in FindMethodFromCode.
1890 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1891 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1892 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001893 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001894 self);
1895 visitor.FixupReferences();
1896 }
1897
1898 if (UNLIKELY(method == nullptr)) {
1899 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001900 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001901 }
1902 }
1903 const void* code = method->GetEntryPointFromQuickCompiledCode();
1904
1905 // When we return, the caller will branch to this address, so it had better not be 0!
1906 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1907 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001908
1909 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001910}
1911
Ian Rogers848871b2013-08-05 10:56:33 -07001912} // namespace art