blob: 5eea2521fa12025e436196a19868ba0c657ce2df [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;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070039 // Frame size in bytes of a callee-save frame for RefsAndArgs.
40 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
41 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070042#if defined(__arm__)
43 // The callee save frame is pointed to by SP.
44 // | argN | |
45 // | ... | |
46 // | arg4 | |
47 // | arg3 spill | | Caller's frame
48 // | arg2 spill | |
49 // | arg1 spill | |
50 // | Method* | ---
51 // | LR |
52 // | ... | callee saves
53 // | R3 | arg3
54 // | R2 | arg2
55 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080056 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070057 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080058 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
59 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
60 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080061 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
62 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
63 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080064 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000065 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080066 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000067#elif defined(__aarch64__)
68 // The callee save frame is pointed to by SP.
69 // | argN | |
70 // | ... | |
71 // | arg4 | |
72 // | arg3 spill | | Caller's frame
73 // | arg2 spill | |
74 // | arg1 spill | |
75 // | Method* | ---
76 // | LR |
77 // | X28 |
78 // | : |
79 // | X19 |
80 // | X7 |
81 // | : |
82 // | X1 |
83 // | D15 |
84 // | : |
85 // | D0 |
86 // | | padding
87 // | Method* | <- sp
88 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
89 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
90 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070091 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Stuart Monteithb95a5342014-03-12 13:32:32 +000092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
93 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +000094 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000095 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000096 }
Ian Rogers848871b2013-08-05 10:56:33 -070097#elif defined(__mips__)
98 // The callee save frame is pointed to by SP.
99 // | argN | |
100 // | ... | |
101 // | arg4 | |
102 // | arg3 spill | | Caller's frame
103 // | arg2 spill | |
104 // | arg1 spill | |
105 // | Method* | ---
106 // | RA |
107 // | ... | callee saves
108 // | A3 | arg3
109 // | A2 | arg2
110 // | A1 | arg1
111 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800112 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
113 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
114 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800120 }
Ian Rogers848871b2013-08-05 10:56:33 -0700121#elif defined(__i386__)
122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | Return |
131 // | EBP,ESI,EDI | callee saves
132 // | EBX | arg3
133 // | EDX | arg2
134 // | ECX | arg1
135 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800142 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000143 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800144 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800146 // The callee save frame is pointed to by SP.
147 // | argN | |
148 // | ... | |
149 // | reg. arg spills | | Caller's frame
150 // | Method* | ---
151 // | Return |
152 // | R15 | callee save
153 // | R14 | callee save
154 // | R13 | callee save
155 // | R12 | callee save
156 // | R9 | arg5
157 // | R8 | arg4
158 // | RSI/R6 | arg1
159 // | RBP/R5 | callee save
160 // | RBX/R3 | callee save
161 // | RDX/R2 | arg2
162 // | RCX/R1 | arg3
163 // | XMM7 | float arg 8
164 // | XMM6 | float arg 7
165 // | XMM5 | float arg 6
166 // | XMM4 | float arg 5
167 // | XMM3 | float arg 4
168 // | XMM2 | float arg 3
169 // | XMM1 | float arg 2
170 // | XMM0 | float arg 1
171 // | Padding |
172 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800173 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700174 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700175 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800176 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800179 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
180 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000181 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
182 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
183 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800186 default:
187 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
188 return 0;
189 }
190 }
Ian Rogers848871b2013-08-05 10:56:33 -0700191#else
192#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700193#endif
194
Ian Rogers936b37f2014-02-14 00:52:24 -0800195 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700196 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800197 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700198 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800199 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700200 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700201 }
202
Ian Rogers936b37f2014-02-14 00:52:24 -0800203 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700204 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800205 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700206 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers936b37f2014-02-14 00:52:24 -0800207 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700208 return *reinterpret_cast<uintptr_t*>(lr);
209 }
210
Andreas Gampecf4035a2014-05-28 22:43:01 -0700211 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700212 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800213 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
214 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
215 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
216 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
217 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
218 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
219 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
Alexei Zavjalov41c507a2014-05-15 16:02:46 +0700220 is_split_long_or_double_(false) { }
Ian Rogers848871b2013-08-05 10:56:33 -0700221
222 virtual ~QuickArgumentVisitor() {}
223
224 virtual void Visit() = 0;
225
Ian Rogers936b37f2014-02-14 00:52:24 -0800226 Primitive::Type GetParamPrimitiveType() const {
227 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700228 }
229
230 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800231 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800232 Primitive::Type type = GetParamPrimitiveType();
233 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800234 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000235 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800236 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700237 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 }
239 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800240 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
242 }
243 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700244 }
245
246 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000247 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 return is_split_long_or_double_;
249 } else {
250 return false; // An optimization for when GPR and FPRs are 64bit.
251 }
Ian Rogers848871b2013-08-05 10:56:33 -0700252 }
253
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700255 return GetParamPrimitiveType() == Primitive::kPrimNot;
256 }
257
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700259 Primitive::Type type = GetParamPrimitiveType();
260 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
261 }
262
263 uint64_t ReadSplitLongParam() const {
264 DCHECK(IsSplitLongOrDouble());
265 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
266 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
267 return (low_half & 0xffffffffULL) | (high_half << 32);
268 }
269
270 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700271 // This implementation doesn't support reg-spill area for hard float
272 // ABI targets such as x86_64 and aarch64. So, for those targets whose
273 // 'kQuickSoftFloatAbi' is 'false':
274 // (a) 'stack_args_' should point to the first method's argument
275 // (b) whatever the argument type it is, the 'stack_index_' should
276 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800277 gpr_index_ = 0;
278 fpr_index_ = 0;
279 stack_index_ = 0;
280 if (!is_static_) { // Handle this.
281 cur_type_ = Primitive::kPrimNot;
282 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700283 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700284 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
285 stack_index_++;
286 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800287 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800288 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 }
Ian Rogers848871b2013-08-05 10:56:33 -0700290 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
292 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
293 switch (cur_type_) {
294 case Primitive::kPrimNot:
295 case Primitive::kPrimBoolean:
296 case Primitive::kPrimByte:
297 case Primitive::kPrimChar:
298 case Primitive::kPrimShort:
299 case Primitive::kPrimInt:
300 is_split_long_or_double_ = false;
301 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700302 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
303 stack_index_++;
304 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800305 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800307 }
308 break;
309 case Primitive::kPrimFloat:
310 is_split_long_or_double_ = false;
311 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800312 if (kQuickSoftFloatAbi) {
313 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800314 gpr_index_++;
315 } else {
316 stack_index_++;
317 }
318 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800319 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800321 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700322 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800323 }
324 break;
325 case Primitive::kPrimDouble:
326 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800327 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000328 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800329 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800330 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700331 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800332 if (kBytesStackArgLocation == 4) {
333 stack_index_+= 2;
334 } else {
335 CHECK_EQ(kBytesStackArgLocation, 8U);
336 stack_index_++;
337 }
338 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700339 if (gpr_index_ < kNumQuickGprArgs) {
340 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000341 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700342 if (gpr_index_ < kNumQuickGprArgs) {
343 gpr_index_++;
344 } else if (kQuickSoftFloatAbi) {
345 stack_index_++;
346 }
347 }
348 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800349 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000350 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800351 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800352 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800353 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 }
359 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700360 }
361 if (kBytesStackArgLocation == 4) {
362 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700364 CHECK_EQ(kBytesStackArgLocation, 8U);
365 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800366 }
367 }
368 break;
369 default:
370 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
371 }
Ian Rogers848871b2013-08-05 10:56:33 -0700372 }
373 }
374
375 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800376 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
377 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800378 if (kQuickSoftFloatAbi) {
379 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000380 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700381 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800382 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700383 // For now, there is no reg-spill area for the targets with
384 // hard float ABI. So, the offset pointing to the first method's
385 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700386 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700387 }
Ian Rogers848871b2013-08-05 10:56:33 -0700388 }
389
390 const bool is_static_;
391 const char* const shorty_;
392 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800393 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
394 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
395 byte* const stack_args_; // Address of stack arguments in caller's frame.
396 uint32_t gpr_index_; // Index into spilled GPRs.
397 uint32_t fpr_index_; // Index into spilled FPRs.
398 uint32_t stack_index_; // Index into arguments on the stack.
399 // The current type of argument during VisitArguments.
400 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700401 // Does a 64bit parameter straddle the register and stack arguments?
402 bool is_split_long_or_double_;
403};
404
405// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800406class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700407 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700408 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
409 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
410 size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700411 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
412
Ian Rogers9758f792014-03-13 09:02:55 -0700413 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700414
415 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800416 ShadowFrame* const sf_;
417 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700418
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700419 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700420};
421
Ian Rogers9758f792014-03-13 09:02:55 -0700422void BuildQuickShadowFrameVisitor::Visit() {
423 Primitive::Type type = GetParamPrimitiveType();
424 switch (type) {
425 case Primitive::kPrimLong: // Fall-through.
426 case Primitive::kPrimDouble:
427 if (IsSplitLongOrDouble()) {
428 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
429 } else {
430 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
431 }
432 ++cur_reg_;
433 break;
434 case Primitive::kPrimNot: {
435 StackReference<mirror::Object>* stack_ref =
436 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
437 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
438 }
439 break;
440 case Primitive::kPrimBoolean: // Fall-through.
441 case Primitive::kPrimByte: // Fall-through.
442 case Primitive::kPrimChar: // Fall-through.
443 case Primitive::kPrimShort: // Fall-through.
444 case Primitive::kPrimInt: // Fall-through.
445 case Primitive::kPrimFloat:
446 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
447 break;
448 case Primitive::kPrimVoid:
449 LOG(FATAL) << "UNREACHABLE";
450 break;
451 }
452 ++cur_reg_;
453}
454
Brian Carlstromea46f952013-07-30 01:26:50 -0700455extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700456 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
458 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
459 // frame.
460 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
461
462 if (method->IsAbstract()) {
463 ThrowAbstractMethodError(method);
464 return 0;
465 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800466 DCHECK(!method->IsNative()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700467 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
468 MethodHelper mh(method);
469 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800470 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700471 uint16_t num_regs = code_item->registers_size_;
472 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
473 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
474 method, 0, memory));
475 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700476 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800477 mh.GetShortyLength(),
478 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700479 shadow_frame_builder.VisitArguments();
480 // Push a transition back into managed code onto the linked list in thread.
481 ManagedStack fragment;
482 self->PushManagedStackFragment(&fragment);
483 self->PushShadowFrame(shadow_frame);
484 self->EndAssertNoThreadSuspension(old_cause);
485
486 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
487 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700488 StackHandleScope<1> hs(self);
489 Handle<mirror::Class> h_class(hs.NewHandle(method->GetDeclaringClass()));
490 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800491 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700492 self->PopManagedStackFragment(fragment);
493 return 0;
494 }
495 }
496
497 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
498 // Pop transition.
499 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800500 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700501 return result.GetJ();
502 }
503}
504
505// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
506// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800507class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700508 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700509 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
510 const char* shorty, uint32_t shorty_len,
511 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Ian Rogers848871b2013-08-05 10:56:33 -0700512 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
513
Ian Rogers9758f792014-03-13 09:02:55 -0700514 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700515
Ian Rogers9758f792014-03-13 09:02:55 -0700516 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800517
Ian Rogers848871b2013-08-05 10:56:33 -0700518 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700519 ScopedObjectAccessUnchecked* const soa_;
520 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800521 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700522 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700523
Ian Rogers848871b2013-08-05 10:56:33 -0700524 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
525};
526
Ian Rogers9758f792014-03-13 09:02:55 -0700527void BuildQuickArgumentVisitor::Visit() {
528 jvalue val;
529 Primitive::Type type = GetParamPrimitiveType();
530 switch (type) {
531 case Primitive::kPrimNot: {
532 StackReference<mirror::Object>* stack_ref =
533 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
534 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
535 references_.push_back(std::make_pair(val.l, stack_ref));
536 break;
537 }
538 case Primitive::kPrimLong: // Fall-through.
539 case Primitive::kPrimDouble:
540 if (IsSplitLongOrDouble()) {
541 val.j = ReadSplitLongParam();
542 } else {
543 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
544 }
545 break;
546 case Primitive::kPrimBoolean: // Fall-through.
547 case Primitive::kPrimByte: // Fall-through.
548 case Primitive::kPrimChar: // Fall-through.
549 case Primitive::kPrimShort: // Fall-through.
550 case Primitive::kPrimInt: // Fall-through.
551 case Primitive::kPrimFloat:
552 val.i = *reinterpret_cast<jint*>(GetParamAddress());
553 break;
554 case Primitive::kPrimVoid:
555 LOG(FATAL) << "UNREACHABLE";
556 val.j = 0;
557 break;
558 }
559 args_->push_back(val);
560}
561
562void BuildQuickArgumentVisitor::FixupReferences() {
563 // Fixup any references which may have changed.
564 for (const auto& pair : references_) {
565 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700566 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700567 }
568}
569
Ian Rogers848871b2013-08-05 10:56:33 -0700570// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
571// which is responsible for recording callee save registers. We explicitly place into jobjects the
572// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
573// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700574extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700575 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700576 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700578 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
579 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700580 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
581 const char* old_cause =
582 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
583 // Register the top of the managed stack, making stack crawlable.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700584 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700585 self->SetTopOfStack(sp, 0);
586 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700587 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
588 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700589 self->VerifyStack();
590 // Start new JNI local reference state.
591 JNIEnvExt* env = self->GetJniEnv();
592 ScopedObjectAccessUnchecked soa(env);
593 ScopedJniEnvLocalRefState env_state(env);
594 // Create local ref. copies of proxy method and the receiver.
595 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
596
597 // Placing arguments into args vector and remove the receiver.
598 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700599 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700600 std::vector<jvalue> args;
601 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
602 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700603
Ian Rogers848871b2013-08-05 10:56:33 -0700604 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700605 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700606 args.erase(args.begin());
607
608 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700610 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700611 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
612 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
613
614 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
615 // that performs allocations.
616 self->EndAssertNoThreadSuspension(old_cause);
617 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
618 rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800619 // Restore references which might have moved.
620 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700621 return result.GetJ();
622}
623
624// Read object references held in arguments from quick frames and place in a JNI local references,
625// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800626class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700627 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700628 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
629 const char* shorty, uint32_t shorty_len,
630 ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700631 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
632
Ian Rogers9758f792014-03-13 09:02:55 -0700633 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700634
Ian Rogers9758f792014-03-13 09:02:55 -0700635 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700636
637 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700638 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800639 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700640 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700641 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700642};
643
Ian Rogers9758f792014-03-13 09:02:55 -0700644void RememberForGcArgumentVisitor::Visit() {
645 if (IsParamAReference()) {
646 StackReference<mirror::Object>* stack_ref =
647 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
648 jobject reference =
649 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
650 references_.push_back(std::make_pair(reference, stack_ref));
651 }
652}
653
654void RememberForGcArgumentVisitor::FixupReferences() {
655 // Fixup any references which may have changed.
656 for (const auto& pair : references_) {
657 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700658 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700659 }
660}
661
662
Ian Rogers848871b2013-08-05 10:56:33 -0700663// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700664extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700665 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700666 Thread* self,
667 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700668 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800669 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700670 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800671 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700672 ScopedObjectAccessUnchecked soa(env);
673 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800674 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700675
676 // Compute details about the called method (avoid GCs)
677 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700679 InvokeType invoke_type;
680 const DexFile* dex_file;
681 uint32_t dex_method_idx;
682 if (called->IsRuntimeMethod()) {
683 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
684 const DexFile::CodeItem* code;
685 {
686 MethodHelper mh(caller);
687 dex_file = &mh.GetDexFile();
688 code = mh.GetCodeItem();
689 }
690 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
691 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
692 Instruction::Code instr_code = instr->Opcode();
693 bool is_range;
694 switch (instr_code) {
695 case Instruction::INVOKE_DIRECT:
696 invoke_type = kDirect;
697 is_range = false;
698 break;
699 case Instruction::INVOKE_DIRECT_RANGE:
700 invoke_type = kDirect;
701 is_range = true;
702 break;
703 case Instruction::INVOKE_STATIC:
704 invoke_type = kStatic;
705 is_range = false;
706 break;
707 case Instruction::INVOKE_STATIC_RANGE:
708 invoke_type = kStatic;
709 is_range = true;
710 break;
711 case Instruction::INVOKE_SUPER:
712 invoke_type = kSuper;
713 is_range = false;
714 break;
715 case Instruction::INVOKE_SUPER_RANGE:
716 invoke_type = kSuper;
717 is_range = true;
718 break;
719 case Instruction::INVOKE_VIRTUAL:
720 invoke_type = kVirtual;
721 is_range = false;
722 break;
723 case Instruction::INVOKE_VIRTUAL_RANGE:
724 invoke_type = kVirtual;
725 is_range = true;
726 break;
727 case Instruction::INVOKE_INTERFACE:
728 invoke_type = kInterface;
729 is_range = false;
730 break;
731 case Instruction::INVOKE_INTERFACE_RANGE:
732 invoke_type = kInterface;
733 is_range = true;
734 break;
735 default:
736 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
737 // Avoid used uninitialized warnings.
738 invoke_type = kDirect;
739 is_range = false;
740 }
741 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
742
743 } else {
744 invoke_type = kStatic;
745 dex_file = &MethodHelper(called).GetDexFile();
746 dex_method_idx = called->GetDexMethodIndex();
747 }
748 uint32_t shorty_len;
749 const char* shorty =
750 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700751 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700752 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800753 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800754 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700755 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700756 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700757 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700758 mirror::Object* dummy = nullptr;
759 HandleWrapper<mirror::Object> h_receiver(
760 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
761 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700762 }
763 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800764 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700765 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800766 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
767 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800768 if (virtual_or_interface) {
769 // Refine called method based on receiver.
770 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700771
772 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800773 if (invoke_type == kVirtual) {
774 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
775 } else {
776 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
777 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700778
779 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
780 << PrettyTypeOf(receiver) << " "
781 << invoke_type << " " << orig_called->GetVtableIndex();
782
Ian Rogers83883d72013-10-21 21:07:24 -0700783 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
784 // of the sharpened method.
785 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100786 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700787 } else {
788 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000789 // the caller's dex file. Since we get here only if the original called was a runtime
790 // method, we've got the correct dex_file and a dex_method_idx from above.
791 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700792 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000793 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700794 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100795 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700796 }
797 }
798 }
Ian Rogers848871b2013-08-05 10:56:33 -0700799 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700800 StackHandleScope<1> hs(soa.Self());
801 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers848871b2013-08-05 10:56:33 -0700802 linker->EnsureInitialized(called_class, true, true);
803 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800804 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700805 } else if (called_class->IsInitializing()) {
806 if (invoke_type == kStatic) {
807 // Class is still initializing, go to oat and grab code (trampoline must be left in place
808 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800809 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700810 } else {
811 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800812 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700813 }
814 } else {
815 DCHECK(called_class->IsErroneous());
816 }
817 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800818 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700819 // Fixup any locally saved objects may have moved during a GC.
820 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700821 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700822 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700823 return code;
824}
825
Andreas Gampec147b002014-03-06 18:11:06 -0800826
827
828/*
829 * This class uses a couple of observations to unite the different calling conventions through
830 * a few constants.
831 *
832 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
833 * possible alignment.
834 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
835 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
836 * when we have to split things
837 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
838 * and we can use Int handling directly.
839 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
840 * necessary when widening. Also, widening of Ints will take place implicitly, and the
841 * extension should be compatible with Aarch64, which mandates copying the available bits
842 * into LSB and leaving the rest unspecified.
843 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
844 * the stack.
845 * 6) There is only little endian.
846 *
847 *
848 * Actual work is supposed to be done in a delegate of the template type. The interface is as
849 * follows:
850 *
851 * void PushGpr(uintptr_t): Add a value for the next GPR
852 *
853 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
854 * padding, that is, think the architecture is 32b and aligns 64b.
855 *
856 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
857 * split this if necessary. The current state will have aligned, if
858 * necessary.
859 *
860 * void PushStack(uintptr_t): Push a value to the stack.
861 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700862 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700863 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800864 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700865 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800866 *
867 */
868template <class T> class BuildGenericJniFrameStateMachine {
869 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800870#if defined(__arm__)
871 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800872 static constexpr bool kNativeSoftFloatAbi = true;
873 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800874 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
875
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800876 static constexpr size_t kRegistersNeededForLong = 2;
877 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800878 static constexpr bool kMultiRegistersAligned = true;
879 static constexpr bool kMultiRegistersWidened = false;
880 static constexpr bool kAlignLongOnStack = true;
881 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000882#elif defined(__aarch64__)
883 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
884 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
885 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
886
887 static constexpr size_t kRegistersNeededForLong = 1;
888 static constexpr size_t kRegistersNeededForDouble = 1;
889 static constexpr bool kMultiRegistersAligned = false;
890 static constexpr bool kMultiRegistersWidened = false;
891 static constexpr bool kAlignLongOnStack = false;
892 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800893#elif defined(__mips__)
894 // TODO: These are all dummy values!
895 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
896 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
897 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
898
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800899 static constexpr size_t kRegistersNeededForLong = 2;
900 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800901 static constexpr bool kMultiRegistersAligned = true;
902 static constexpr bool kMultiRegistersWidened = true;
903 static constexpr bool kAlignLongOnStack = false;
904 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800905#elif defined(__i386__)
906 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800907 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800908 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
909 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
910
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800911 static constexpr size_t kRegistersNeededForLong = 2;
912 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800913 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
914 static constexpr bool kMultiRegistersWidened = false;
915 static constexpr bool kAlignLongOnStack = false;
916 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800917#elif defined(__x86_64__)
918 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
919 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
920 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
921
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800922 static constexpr size_t kRegistersNeededForLong = 1;
923 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800924 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800925 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800926 static constexpr bool kAlignLongOnStack = false;
927 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800928#else
929#error "Unsupported architecture"
930#endif
931
Andreas Gampec147b002014-03-06 18:11:06 -0800932 public:
933 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs),
934 fpr_index_(kNumNativeFprArgs),
935 stack_entries_(0),
936 delegate_(delegate) {
937 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
938 // the next register is even; counting down is just to make the compiler happy...
939 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
940 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
941 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800942
Andreas Gampec147b002014-03-06 18:11:06 -0800943 virtual ~BuildGenericJniFrameStateMachine() {}
944
945 bool HavePointerGpr() {
946 return gpr_index_ > 0;
947 }
948
949 void AdvancePointer(void* val) {
950 if (HavePointerGpr()) {
951 gpr_index_--;
952 PushGpr(reinterpret_cast<uintptr_t>(val));
953 } else {
954 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
955 PushStack(reinterpret_cast<uintptr_t>(val));
956 gpr_index_ = 0;
957 }
958 }
959
960
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700961 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800962 return gpr_index_ > 0;
963 }
964
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700965 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
966 uintptr_t handle = PushHandle(ptr);
967 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800968 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700969 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800970 } else {
971 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700972 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800973 gpr_index_ = 0;
974 }
975 }
976
977
978 bool HaveIntGpr() {
979 return gpr_index_ > 0;
980 }
981
982 void AdvanceInt(uint32_t val) {
983 if (HaveIntGpr()) {
984 gpr_index_--;
985 PushGpr(val);
986 } else {
987 stack_entries_++;
988 PushStack(val);
989 gpr_index_ = 0;
990 }
991 }
992
993
994 bool HaveLongGpr() {
995 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
996 }
997
998 bool LongGprNeedsPadding() {
999 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1000 kAlignLongOnStack && // and when it needs alignment
1001 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1002 }
1003
1004 bool LongStackNeedsPadding() {
1005 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1006 kAlignLongOnStack && // and when it needs 8B alignment
1007 (stack_entries_ & 1) == 1; // counter is odd
1008 }
1009
1010 void AdvanceLong(uint64_t val) {
1011 if (HaveLongGpr()) {
1012 if (LongGprNeedsPadding()) {
1013 PushGpr(0);
1014 gpr_index_--;
1015 }
1016 if (kRegistersNeededForLong == 1) {
1017 PushGpr(static_cast<uintptr_t>(val));
1018 } else {
1019 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1020 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1021 }
1022 gpr_index_ -= kRegistersNeededForLong;
1023 } else {
1024 if (LongStackNeedsPadding()) {
1025 PushStack(0);
1026 stack_entries_++;
1027 }
1028 if (kRegistersNeededForLong == 1) {
1029 PushStack(static_cast<uintptr_t>(val));
1030 stack_entries_++;
1031 } else {
1032 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1033 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1034 stack_entries_ += 2;
1035 }
1036 gpr_index_ = 0;
1037 }
1038 }
1039
1040
1041 bool HaveFloatFpr() {
1042 return fpr_index_ > 0;
1043 }
1044
Andreas Gampec147b002014-03-06 18:11:06 -08001045 template <typename U, typename V> V convert(U in) {
1046 CHECK_LE(sizeof(U), sizeof(V));
1047 union { U u; V v; } tmp;
1048 tmp.u = in;
1049 return tmp.v;
1050 }
1051
1052 void AdvanceFloat(float val) {
1053 if (kNativeSoftFloatAbi) {
1054 AdvanceInt(convert<float, uint32_t>(val));
1055 } else {
1056 if (HaveFloatFpr()) {
1057 fpr_index_--;
1058 if (kRegistersNeededForDouble == 1) {
1059 if (kMultiRegistersWidened) {
1060 PushFpr8(convert<double, uint64_t>(val));
1061 } else {
1062 // No widening, just use the bits.
1063 PushFpr8(convert<float, uint64_t>(val));
1064 }
1065 } else {
1066 PushFpr4(val);
1067 }
1068 } else {
1069 stack_entries_++;
1070 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1071 // Need to widen before storing: Note the "double" in the template instantiation.
1072 PushStack(convert<double, uintptr_t>(val));
1073 } else {
1074 PushStack(convert<float, uintptr_t>(val));
1075 }
1076 fpr_index_ = 0;
1077 }
1078 }
1079 }
1080
1081
1082 bool HaveDoubleFpr() {
1083 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1084 }
1085
1086 bool DoubleFprNeedsPadding() {
1087 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1088 kAlignDoubleOnStack && // and when it needs alignment
1089 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1090 }
1091
1092 bool DoubleStackNeedsPadding() {
1093 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1094 kAlignDoubleOnStack && // and when it needs 8B alignment
1095 (stack_entries_ & 1) == 1; // counter is odd
1096 }
1097
1098 void AdvanceDouble(uint64_t val) {
1099 if (kNativeSoftFloatAbi) {
1100 AdvanceLong(val);
1101 } else {
1102 if (HaveDoubleFpr()) {
1103 if (DoubleFprNeedsPadding()) {
1104 PushFpr4(0);
1105 fpr_index_--;
1106 }
1107 PushFpr8(val);
1108 fpr_index_ -= kRegistersNeededForDouble;
1109 } else {
1110 if (DoubleStackNeedsPadding()) {
1111 PushStack(0);
1112 stack_entries_++;
1113 }
1114 if (kRegistersNeededForDouble == 1) {
1115 PushStack(static_cast<uintptr_t>(val));
1116 stack_entries_++;
1117 } else {
1118 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1119 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1120 stack_entries_ += 2;
1121 }
1122 fpr_index_ = 0;
1123 }
1124 }
1125 }
1126
1127 uint32_t getStackEntries() {
1128 return stack_entries_;
1129 }
1130
1131 uint32_t getNumberOfUsedGprs() {
1132 return kNumNativeGprArgs - gpr_index_;
1133 }
1134
1135 uint32_t getNumberOfUsedFprs() {
1136 return kNumNativeFprArgs - fpr_index_;
1137 }
1138
1139 private:
1140 void PushGpr(uintptr_t val) {
1141 delegate_->PushGpr(val);
1142 }
1143 void PushFpr4(float val) {
1144 delegate_->PushFpr4(val);
1145 }
1146 void PushFpr8(uint64_t val) {
1147 delegate_->PushFpr8(val);
1148 }
1149 void PushStack(uintptr_t val) {
1150 delegate_->PushStack(val);
1151 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001152 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1153 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001154 }
1155
1156 uint32_t gpr_index_; // Number of free GPRs
1157 uint32_t fpr_index_; // Number of free FPRs
1158 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1159 // extended
1160 T* delegate_; // What Push implementation gets called
1161};
1162
1163class ComputeGenericJniFrameSize FINAL {
1164 public:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001165 ComputeGenericJniFrameSize() : num_handle_scope_references_(0), num_stack_entries_(0) {}
Andreas Gampec147b002014-03-06 18:11:06 -08001166
Andreas Gampec147b002014-03-06 18:11:06 -08001167 uint32_t GetStackSize() {
1168 return num_stack_entries_ * sizeof(uintptr_t);
1169 }
1170
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001171 // WARNING: After this, *sp won't be pointing to the method anymore!
Andreas Gampecf4035a2014-05-28 22:43:01 -07001172 void ComputeLayout(StackReference<mirror::ArtMethod>** m, bool is_static, const char* shorty,
1173 uint32_t shorty_len, void* sp, HandleScope** table,
1174 uint32_t* handle_scope_entries, uintptr_t** start_stack, uintptr_t** start_gpr,
1175 uint32_t** start_fpr, void** code_return, size_t* overall_size)
Andreas Gampec147b002014-03-06 18:11:06 -08001176 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1177 ComputeAll(is_static, shorty, shorty_len);
1178
Andreas Gampecf4035a2014-05-28 22:43:01 -07001179 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001180
Andreas Gampec147b002014-03-06 18:11:06 -08001181 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
Andreas Gampec147b002014-03-06 18:11:06 -08001182
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001183 // First, fix up the layout of the callee-save frame.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001184 // We have to squeeze in the HandleScope, and relocate the method pointer.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001185
1186 // "Free" the slot for the method.
Andreas Gampecf4035a2014-05-28 22:43:01 -07001187 sp8 += kPointerSize; // In the callee-save frame we use a full pointer.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001188
Andreas Gampecf4035a2014-05-28 22:43:01 -07001189 // Under the callee saves put handle scope and new method stack reference.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001190 *handle_scope_entries = num_handle_scope_references_;
Andreas Gampecf4035a2014-05-28 22:43:01 -07001191
1192 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1193 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1194
1195 sp8 -= scope_and_method;
Andreas Gampe779f8c92014-06-09 18:29:38 -07001196 // Align by kStackAlignment.
1197 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampecf4035a2014-05-28 22:43:01 -07001198
1199 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
1200 *table = reinterpret_cast<HandleScope*>(sp8_table);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001201 (*table)->SetNumberOfReferences(num_handle_scope_references_);
Andreas Gampec147b002014-03-06 18:11:06 -08001202
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001203 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001204 uint8_t* method_pointer = sp8;
Andreas Gampecf4035a2014-05-28 22:43:01 -07001205 StackReference<mirror::ArtMethod>* new_method_ref =
1206 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1207 new_method_ref->Assign(method);
1208 *m = new_method_ref;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001209
1210 // Reference cookie and padding
1211 sp8 -= 8;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001212 // Store HandleScope size
1213 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(handle_scope_size & 0xFFFFFFFF);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001214
1215 // Next comes the native call stack.
Andreas Gampec147b002014-03-06 18:11:06 -08001216 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001217 // Align by kStackAlignment.
1218 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec147b002014-03-06 18:11:06 -08001219 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
1220
1221 // put fprs and gprs below
1222 // Assumption is OK right now, as we have soft-float arm
1223 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs;
1224 sp8 -= fregs * sizeof(uintptr_t);
1225 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1226 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs;
1227 sp8 -= iregs * sizeof(uintptr_t);
1228 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1229
1230 // reserve space for the code pointer
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001231 sp8 -= kPointerSize;
Andreas Gampec147b002014-03-06 18:11:06 -08001232 *code_return = reinterpret_cast<void*>(sp8);
1233
1234 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001235
1236 // The new SP is stored at the end of the alloca, so it can be immediately popped
1237 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB;
1238 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer;
Andreas Gampec147b002014-03-06 18:11:06 -08001239 }
1240
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001241 void ComputeHandleScopeOffset() { } // nothing to do, static right now
Andreas Gampec147b002014-03-06 18:11:06 -08001242
1243 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len)
1244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1245 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this);
1246
1247 // JNIEnv
1248 sm.AdvancePointer(nullptr);
1249
1250 // Class object or this as first argument
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001251 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001252
1253 for (uint32_t i = 1; i < shorty_len; ++i) {
1254 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1255 switch (cur_type_) {
1256 case Primitive::kPrimNot:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001257 sm.AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001258 break;
1259
1260 case Primitive::kPrimBoolean:
1261 case Primitive::kPrimByte:
1262 case Primitive::kPrimChar:
1263 case Primitive::kPrimShort:
1264 case Primitive::kPrimInt:
1265 sm.AdvanceInt(0);
1266 break;
1267 case Primitive::kPrimFloat:
1268 sm.AdvanceFloat(0);
1269 break;
1270 case Primitive::kPrimDouble:
1271 sm.AdvanceDouble(0);
1272 break;
1273 case Primitive::kPrimLong:
1274 sm.AdvanceLong(0);
1275 break;
1276 default:
1277 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1278 }
1279 }
1280
1281 num_stack_entries_ = sm.getStackEntries();
1282 }
1283
1284 void PushGpr(uintptr_t /* val */) {
1285 // not optimizing registers, yet
1286 }
1287
1288 void PushFpr4(float /* val */) {
1289 // not optimizing registers, yet
1290 }
1291
1292 void PushFpr8(uint64_t /* val */) {
1293 // not optimizing registers, yet
1294 }
1295
1296 void PushStack(uintptr_t /* val */) {
1297 // counting is already done in the superclass
1298 }
1299
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001300 uintptr_t PushHandle(mirror::Object* /* ptr */) {
1301 num_handle_scope_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001302 return reinterpret_cast<uintptr_t>(nullptr);
1303 }
1304
1305 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001306 uint32_t num_handle_scope_references_;
Andreas Gampec147b002014-03-06 18:11:06 -08001307 uint32_t num_stack_entries_;
1308};
1309
1310// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1311// of transitioning into native code.
1312class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001313 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -07001314 BuildGenericJniFrameVisitor(StackReference<mirror::ArtMethod>** sp, bool is_static,
1315 const char* shorty, uint32_t shorty_len, Thread* self) :
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001316 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) {
Andreas Gampec147b002014-03-06 18:11:06 -08001317 ComputeGenericJniFrameSize fsc;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001318 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &handle_scope_, &handle_scope_expected_refs_,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001319 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_,
1320 &alloca_used_size_);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001321 handle_scope_number_of_references_ = 0;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001322 cur_hs_entry_ = GetFirstHandleScopeEntry();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001323
1324 // jni environment is always first argument
Andreas Gampec147b002014-03-06 18:11:06 -08001325 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001326
1327 if (is_static) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001328 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001329 }
1330 }
1331
Ian Rogers9758f792014-03-13 09:02:55 -07001332 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001333
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001334 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001335
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001336 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1337 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1338 return handle_scope_->GetHandle(0).GetReference();
1339 }
1340
1341 jobject GetFirstHandleScopeJObject()
1342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001343 return handle_scope_->GetHandle(0).ToJObject();
Andreas Gampec147b002014-03-06 18:11:06 -08001344 }
1345
1346 void PushGpr(uintptr_t val) {
1347 *cur_gpr_reg_ = val;
1348 cur_gpr_reg_++;
1349 }
1350
1351 void PushFpr4(float val) {
1352 *cur_fpr_reg_ = val;
1353 cur_fpr_reg_++;
1354 }
1355
1356 void PushFpr8(uint64_t val) {
1357 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1358 *tmp = val;
1359 cur_fpr_reg_ += 2;
1360 }
1361
1362 void PushStack(uintptr_t val) {
1363 *cur_stack_arg_ = val;
1364 cur_stack_arg_++;
1365 }
1366
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001367 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001368 uintptr_t tmp;
1369 if (ref == nullptr) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001370 *cur_hs_entry_ = StackReference<mirror::Object>();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001371 tmp = reinterpret_cast<uintptr_t>(nullptr);
1372 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001373 *cur_hs_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref);
1374 tmp = reinterpret_cast<uintptr_t>(cur_hs_entry_);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001375 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001376 cur_hs_entry_++;
1377 handle_scope_number_of_references_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001378 return tmp;
1379 }
1380
1381 // Size of the part of the alloca that we actually need.
1382 size_t GetAllocaUsedSize() {
1383 return alloca_used_size_;
1384 }
1385
1386 void* GetCodeReturn() {
1387 return code_return_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001388 }
1389
1390 private:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001391 uint32_t handle_scope_number_of_references_;
1392 StackReference<mirror::Object>* cur_hs_entry_;
1393 HandleScope* handle_scope_;
1394 uint32_t handle_scope_expected_refs_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001395 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001396 uint32_t* cur_fpr_reg_;
1397 uintptr_t* cur_stack_arg_;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001398 // StackReference<mirror::Object>* top_of_handle_scope_;
Andreas Gampec147b002014-03-06 18:11:06 -08001399 void* code_return_;
1400 size_t alloca_used_size_;
1401
1402 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001403
1404 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1405};
1406
Ian Rogers9758f792014-03-13 09:02:55 -07001407void BuildGenericJniFrameVisitor::Visit() {
1408 Primitive::Type type = GetParamPrimitiveType();
1409 switch (type) {
1410 case Primitive::kPrimLong: {
1411 jlong long_arg;
1412 if (IsSplitLongOrDouble()) {
1413 long_arg = ReadSplitLongParam();
1414 } else {
1415 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1416 }
1417 sm_.AdvanceLong(long_arg);
1418 break;
1419 }
1420 case Primitive::kPrimDouble: {
1421 uint64_t double_arg;
1422 if (IsSplitLongOrDouble()) {
1423 // Read into union so that we don't case to a double.
1424 double_arg = ReadSplitLongParam();
1425 } else {
1426 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1427 }
1428 sm_.AdvanceDouble(double_arg);
1429 break;
1430 }
1431 case Primitive::kPrimNot: {
1432 StackReference<mirror::Object>* stack_ref =
1433 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001434 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001435 break;
1436 }
1437 case Primitive::kPrimFloat:
1438 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1439 break;
1440 case Primitive::kPrimBoolean: // Fall-through.
1441 case Primitive::kPrimByte: // Fall-through.
1442 case Primitive::kPrimChar: // Fall-through.
1443 case Primitive::kPrimShort: // Fall-through.
1444 case Primitive::kPrimInt: // Fall-through.
1445 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1446 break;
1447 case Primitive::kPrimVoid:
1448 LOG(FATAL) << "UNREACHABLE";
1449 break;
1450 }
1451}
1452
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001453void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Ian Rogers9758f792014-03-13 09:02:55 -07001454 // Initialize padding entries.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001455 while (handle_scope_number_of_references_ < handle_scope_expected_refs_) {
1456 *cur_hs_entry_ = StackReference<mirror::Object>();
1457 cur_hs_entry_++;
1458 handle_scope_number_of_references_++;
Ian Rogers9758f792014-03-13 09:02:55 -07001459 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001460 handle_scope_->SetNumberOfReferences(handle_scope_expected_refs_);
1461 DCHECK_NE(handle_scope_expected_refs_, 0U);
1462 // Install HandleScope.
1463 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001464}
1465
Andreas Gampe90546832014-03-12 18:07:19 -07001466extern "C" void* artFindNativeMethod();
1467
Andreas Gampead615172014-04-04 16:20:13 -07001468uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1469 if (lock != nullptr) {
1470 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1471 } else {
1472 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1473 }
1474}
1475
1476void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1477 if (lock != nullptr) {
1478 JniMethodEndSynchronized(cookie, lock, self);
1479 } else {
1480 JniMethodEnd(cookie, self);
1481 }
1482}
1483
Andreas Gampec147b002014-03-06 18:11:06 -08001484/*
1485 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001486 * 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 -08001487 * The final element on the stack is a pointer to the native code.
1488 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001489 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001490 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001491 *
Andreas Gampec147b002014-03-06 18:11:06 -08001492 * The return of this function denotes:
1493 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1494 * 2) An error, if the value is negative.
1495 */
Andreas Gampecf4035a2014-05-28 22:43:01 -07001496extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001497 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001498 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001499 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001500
1501 // run the visitor
1502 MethodHelper mh(called);
Andreas Gampec147b002014-03-06 18:11:06 -08001503
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001504 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(),
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001505 self);
1506 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001507 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001508
1509 // fix up managed-stack things in Thread
1510 self->SetTopOfStack(sp, 0);
1511
Ian Rogerse0dcd462014-03-08 15:21:04 -08001512 self->VerifyStack();
1513
Andreas Gampe90546832014-03-12 18:07:19 -07001514 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001515 uint32_t cookie;
1516 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001517 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001518 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001519 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001520 // A negative value denotes an error.
1521 return -1;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001522 }
1523 } else {
1524 cookie = JniMethodStart(self);
1525 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001526 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001527 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001528
Andreas Gampe90546832014-03-12 18:07:19 -07001529 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001530 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001531
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001532 // There are two cases for the content of nativeCode:
1533 // 1) Pointer to the native function.
1534 // 2) Pointer to the trampoline for native code binding.
1535 // In the second case, we need to execute the binding and continue with the actual native function
1536 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001537 DCHECK(nativeCode != nullptr);
1538 if (nativeCode == GetJniDlsymLookupStub()) {
1539 nativeCode = artFindNativeMethod();
1540
1541 if (nativeCode == nullptr) {
1542 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001543
1544 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001545 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Andreas Gampead615172014-04-04 16:20:13 -07001546 if (mh.GetShorty()[0] == 'L') {
1547 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1548 } else {
1549 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1550 }
1551
Andreas Gampe90546832014-03-12 18:07:19 -07001552 return -1;
1553 }
1554 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001555 }
1556
Andreas Gampe90546832014-03-12 18:07:19 -07001557 // Store the native code pointer in the stack at the right location.
Andreas Gampec147b002014-03-06 18:11:06 -08001558 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn());
Andreas Gampec147b002014-03-06 18:11:06 -08001559 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode);
1560
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001561 // 5K reserved, window_size + frame pointer used.
Andreas Gampe90546832014-03-12 18:07:19 -07001562 size_t window_size = visitor.GetAllocaUsedSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001563 return (5 * KB) - window_size - kPointerSize;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001564}
1565
1566/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001567 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001568 * unlocking.
1569 */
Andreas Gampecf4035a2014-05-28 22:43:01 -07001570extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
1571 StackReference<mirror::ArtMethod>* sp,
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001572 jvalue result, uint64_t result_f)
1573 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1574 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001575 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001576 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001577
Andreas Gampead615172014-04-04 16:20:13 -07001578 jobject lock = nullptr;
1579 if (called->IsSynchronized()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001580 HandleScope* table = reinterpret_cast<HandleScope*>(
Andreas Gampecf4035a2014-05-28 22:43:01 -07001581 reinterpret_cast<uint8_t*>(sp) + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001582 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001583 }
1584
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001585 MethodHelper mh(called);
1586 char return_shorty_char = mh.GetShorty()[0];
1587
1588 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001589 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001590 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001591 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001592
1593 switch (return_shorty_char) {
1594 case 'F': // Fall-through.
1595 case 'D':
1596 return result_f;
1597 case 'Z':
1598 return result.z;
1599 case 'B':
1600 return result.b;
1601 case 'C':
1602 return result.c;
1603 case 'S':
1604 return result.s;
1605 case 'I':
1606 return result.i;
1607 case 'J':
1608 return result.j;
1609 case 'V':
1610 return 0;
1611 default:
1612 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1613 return 0;
1614 }
1615 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001616}
1617
Andreas Gampe51f76352014-05-21 08:28:48 -07001618// The following definitions create return types for two word-sized entities that will be passed
1619// in registers so that memory operations for the interface trampolines can be avoided. The entities
1620// are the resolved method and the pointer to the code to be invoked.
1621//
1622// On x86, ARM32 and MIPS, this is given for a *scalar* 64bit value. The definition thus *must* be
1623// uint64_t or long long int. We use the upper 32b for code, and the lower 32b for the method.
1624//
1625// On x86_64 and ARM64, structs are decomposed for allocation, so we can create a structs of two
1626// size_t-sized values.
1627//
1628// We need two operations:
1629//
1630// 1) A flag value that signals failure. The assembly stubs expect the method part to be "0".
1631// GetFailureValue() will return a value that has method == 0.
1632//
1633// 2) A value that combines a code pointer and a method pointer.
1634// GetSuccessValue() constructs this.
1635
1636#if defined(__i386__) || defined(__arm__) || defined(__mips__)
1637typedef uint64_t MethodAndCode;
1638
1639// Encodes method_ptr==nullptr and code_ptr==nullptr
1640static constexpr MethodAndCode GetFailureValue() {
1641 return 0;
1642}
1643
1644// Use the lower 32b for the method pointer and the upper 32b for the code pointer.
1645static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1646 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1647 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1648 return ((code_uint << 32) | method_uint);
1649}
1650
1651#elif defined(__x86_64__) || defined(__aarch64__)
1652struct MethodAndCode {
1653 uintptr_t method;
1654 uintptr_t code;
1655};
1656
1657// Encodes method_ptr==nullptr. Leaves random value in code pointer.
1658static MethodAndCode GetFailureValue() {
1659 MethodAndCode ret;
1660 ret.method = 0;
1661 return ret;
1662}
1663
1664// Write values into their respective members.
1665static MethodAndCode GetSuccessValue(const void* code, mirror::ArtMethod* method) {
1666 MethodAndCode ret;
1667 ret.method = reinterpret_cast<uintptr_t>(method);
1668 ret.code = reinterpret_cast<uintptr_t>(code);
1669 return ret;
1670}
1671#else
1672#error "Unsupported architecture"
1673#endif
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001674
1675template<InvokeType type, bool access_check>
Andreas Gampe51f76352014-05-21 08:28:48 -07001676static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1677 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001678 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001679
1680template<InvokeType type, bool access_check>
1681static MethodAndCode artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1682 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001683 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001684 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1685 type);
1686 if (UNLIKELY(method == nullptr)) {
1687 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1688 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1689 uint32_t shorty_len;
1690 const char* shorty =
1691 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
1692 {
1693 // Remember the args in case a GC happens in FindMethodFromCode.
1694 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1695 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1696 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001697 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1698 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001699 visitor.FixupReferences();
1700 }
1701
1702 if (UNLIKELY(method == NULL)) {
1703 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001704 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001705 }
1706 }
1707 DCHECK(!self->IsExceptionPending());
1708 const void* code = method->GetEntryPointFromQuickCompiledCode();
1709
1710 // When we return, the caller will branch to this address, so it had better not be 0!
1711 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1712 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001713
1714 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001715}
1716
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001717// Explicit artInvokeCommon template function declarations to please analysis tool.
1718#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1719 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gampe51f76352014-05-21 08:28:48 -07001720 MethodAndCode artInvokeCommon<type, access_check>(uint32_t method_idx, \
1721 mirror::Object* this_object, \
1722 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001723 Thread* self, \
1724 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001725
1726EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1727EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1728EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1729EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1730EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1731EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1732EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1733EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1734EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1735EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1736#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1737
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001738
1739// See comments in runtime_support_asm.S
Andreas Gampe51f76352014-05-21 08:28:48 -07001740extern "C" MethodAndCode artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001741 mirror::Object* this_object,
1742 mirror::ArtMethod* caller_method,
1743 Thread* self,
1744 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001745 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp);
1746}
1747
1748
Andreas Gampe51f76352014-05-21 08:28:48 -07001749extern "C" MethodAndCode artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001750 mirror::Object* this_object,
1751 mirror::ArtMethod* caller_method,
1752 Thread* self,
1753 StackReference<mirror::ArtMethod>* sp) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001754 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp);
1755}
1756
Andreas Gampe51f76352014-05-21 08:28:48 -07001757extern "C" MethodAndCode artInvokeStaticTrampolineWithAccessCheck(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<kStatic, true>(method_idx, this_object, caller_method, self, sp);
1763}
1764
Andreas Gampe51f76352014-05-21 08:28:48 -07001765extern "C" MethodAndCode artInvokeSuperTrampolineWithAccessCheck(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<kSuper, true>(method_idx, this_object, caller_method, self, sp);
1771}
1772
Andreas Gampe51f76352014-05-21 08:28:48 -07001773extern "C" MethodAndCode artInvokeVirtualTrampolineWithAccessCheck(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<kVirtual, true>(method_idx, this_object, caller_method, self, sp);
1779}
1780
1781// Determine target of interface dispatch. This object is known non-null.
Andreas Gampe51f76352014-05-21 08:28:48 -07001782extern "C" MethodAndCode artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
1783 mirror::Object* this_object,
1784 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001785 Thread* self,
1786 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001787 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1788 mirror::ArtMethod* method;
1789 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1790 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1791 if (UNLIKELY(method == NULL)) {
1792 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1793 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1794 caller_method);
Andreas Gampe51f76352014-05-21 08:28:48 -07001795 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001796 }
1797 } else {
1798 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1799 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001800
1801 // Find the caller PC.
1802 constexpr size_t pc_offset = GetCalleeSavePCOffset(kRuntimeISA, Runtime::kRefsAndArgs);
1803 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + pc_offset);
1804
1805 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001806 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
1807 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
1808 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1809 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1810 Instruction::Code instr_code = instr->Opcode();
1811 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1812 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1813 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1814 uint32_t dex_method_idx;
1815 if (instr_code == Instruction::INVOKE_INTERFACE) {
1816 dex_method_idx = instr->VRegB_35c();
1817 } else {
1818 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1819 dex_method_idx = instr->VRegB_3rc();
1820 }
1821
1822 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1823 uint32_t shorty_len;
1824 const char* shorty =
1825 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
1826 {
1827 // Remember the args in case a GC happens in FindMethodFromCode.
1828 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1829 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1830 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001831 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001832 self);
1833 visitor.FixupReferences();
1834 }
1835
1836 if (UNLIKELY(method == nullptr)) {
1837 CHECK(self->IsExceptionPending());
Andreas Gampe51f76352014-05-21 08:28:48 -07001838 return GetFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001839 }
1840 }
1841 const void* code = method->GetEntryPointFromQuickCompiledCode();
1842
1843 // When we return, the caller will branch to this address, so it had better not be 0!
1844 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1845 << MethodHelper(method).GetDexFile().GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001846
1847 return GetSuccessValue(code, method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001848}
1849
Ian Rogers848871b2013-08-05 10:56:33 -07001850} // namespace art