blob: 680ffbeef9008f07105904edd0218eb0761da315 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070021#include "entrypoints/entrypoint_utils.h"
Ian Rogers83883d72013-10-21 21:07:24 -070022#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070024#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070026#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/object-inl.h"
28#include "mirror/object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070031#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070032
33namespace art {
34
35// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
36class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080037 // Number of bytes for each out register in the caller method's frame.
38 static constexpr size_t kBytesStackArgLocation = 4;
Ian Rogers848871b2013-08-05 10:56:33 -070039#if defined(__arm__)
40 // The callee save frame is pointed to by SP.
41 // | argN | |
42 // | ... | |
43 // | arg4 | |
44 // | arg3 spill | | Caller's frame
45 // | arg2 spill | |
46 // | arg1 spill | |
47 // | Method* | ---
48 // | LR |
49 // | ... | callee saves
50 // | R3 | arg3
51 // | R2 | arg2
52 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080053 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070054 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080055 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
56 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
57 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -080058 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
59 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 8; // Offset of first GPR arg.
60 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 44; // Offset of return address.
61 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 48; // Frame size.
62 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000063 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080064 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000065#elif defined(__aarch64__)
66 // The callee save frame is pointed to by SP.
67 // | argN | |
68 // | ... | |
69 // | arg4 | |
70 // | arg3 spill | | Caller's frame
71 // | arg2 spill | |
72 // | arg1 spill | |
73 // | Method* | ---
74 // | LR |
75 // | X28 |
76 // | : |
77 // | X19 |
78 // | X7 |
79 // | : |
80 // | X1 |
81 // | D15 |
82 // | : |
83 // | D0 |
84 // | | padding
85 // | Method* | <- sp
86 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
87 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
88 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Stuart Monteithb95a5342014-03-12 13:32:32 +000089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =16; // Offset of first FPR arg.
90 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 144; // Offset of first GPR arg.
91 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 296; // Offset of return address.
92 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 304; // Frame size.
93 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000094 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 }
Ian Rogers848871b2013-08-05 10:56:33 -070096#elif defined(__mips__)
97 // The callee save frame is pointed to by SP.
98 // | argN | |
99 // | ... | |
100 // | arg4 | |
101 // | arg3 spill | | Caller's frame
102 // | arg2 spill | |
103 // | arg1 spill | |
104 // | Method* | ---
105 // | RA |
106 // | ... | callee saves
107 // | A3 | arg3
108 // | A2 | arg2
109 // | A1 | arg1
110 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800111 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
112 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
113 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800114 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
115 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
116 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
117 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 64; // Frame size.
118 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000119 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800120 }
Ian Rogers848871b2013-08-05 10:56:33 -0700121#elif defined(__i386__)
122 // The callee save frame is pointed to by SP.
123 // | argN | |
124 // | ... | |
125 // | arg4 | |
126 // | arg3 spill | | Caller's frame
127 // | arg2 spill | |
128 // | arg1 spill | |
129 // | Method* | ---
130 // | Return |
131 // | EBP,ESI,EDI | callee saves
132 // | EBX | arg3
133 // | EDX | arg2
134 // | ECX | arg1
135 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800136 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
137 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
138 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
141 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
142 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 32; // Frame size.
143 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000144 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800145 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800147 // The callee save frame is pointed to by SP.
148 // | argN | |
149 // | ... | |
150 // | reg. arg spills | | Caller's frame
151 // | Method* | ---
152 // | Return |
153 // | R15 | callee save
154 // | R14 | callee save
155 // | R13 | callee save
156 // | R12 | callee save
157 // | R9 | arg5
158 // | R8 | arg4
159 // | RSI/R6 | arg1
160 // | RBP/R5 | callee save
161 // | RBX/R3 | callee save
162 // | RDX/R2 | arg2
163 // | RCX/R1 | arg3
164 // | XMM7 | float arg 8
165 // | XMM6 | float arg 7
166 // | XMM5 | float arg 6
167 // | XMM4 | float arg 5
168 // | XMM3 | float arg 4
169 // | XMM2 | float arg 3
170 // | XMM1 | float arg 2
171 // | XMM0 | float arg 1
172 // | Padding |
173 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800174 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
175 static constexpr size_t kNumQuickGprArgs = 5; // 3 arguments passed in GPRs.
176 static constexpr size_t kNumQuickFprArgs = 8; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168; // Offset of return address.
180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize = 176; // Frame size.
181 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
182 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000183 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
184 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
185 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
186 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
187 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800188 default:
189 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
190 return 0;
191 }
192 }
Ian Rogers848871b2013-08-05 10:56:33 -0700193#else
194#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700195#endif
196
Ian Rogers936b37f2014-02-14 00:52:24 -0800197 public:
198 static mirror::ArtMethod* GetCallingMethod(mirror::ArtMethod** sp)
199 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
200 DCHECK((*sp)->IsCalleeSaveMethod());
201 byte* previous_sp = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Brian Carlstromea46f952013-07-30 01:26:50 -0700202 return *reinterpret_cast<mirror::ArtMethod**>(previous_sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700203 }
204
Ian Rogers936b37f2014-02-14 00:52:24 -0800205 // For the given quick ref and args quick frame, return the caller's PC.
206 static uintptr_t GetCallingPc(mirror::ArtMethod** sp)
207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
208 DCHECK((*sp)->IsCalleeSaveMethod());
209 byte* lr = reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700210 return *reinterpret_cast<uintptr_t*>(lr);
211 }
212
Brian Carlstromea46f952013-07-30 01:26:50 -0700213 QuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static,
Ian Rogers848871b2013-08-05 10:56:33 -0700214 const char* shorty, uint32_t shorty_len)
Ian Rogers936b37f2014-02-14 00:52:24 -0800215 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
216 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
217 gpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
218 fpr_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
219 stack_args_(reinterpret_cast<byte*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
220 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
221 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
222 is_split_long_or_double_(false) {
223 DCHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize,
Ian Rogers848871b2013-08-05 10:56:33 -0700224 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
225 }
226
227 virtual ~QuickArgumentVisitor() {}
228
229 virtual void Visit() = 0;
230
Ian Rogers936b37f2014-02-14 00:52:24 -0800231 Primitive::Type GetParamPrimitiveType() const {
232 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700233 }
234
235 byte* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800236 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800237 Primitive::Type type = GetParamPrimitiveType();
238 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800239 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000240 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700242 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800243 }
244 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800245 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800246 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
247 }
248 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700249 }
250
251 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000252 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800253 return is_split_long_or_double_;
254 } else {
255 return false; // An optimization for when GPR and FPRs are 64bit.
256 }
Ian Rogers848871b2013-08-05 10:56:33 -0700257 }
258
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700260 return GetParamPrimitiveType() == Primitive::kPrimNot;
261 }
262
Ian Rogers936b37f2014-02-14 00:52:24 -0800263 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700264 Primitive::Type type = GetParamPrimitiveType();
265 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
266 }
267
268 uint64_t ReadSplitLongParam() const {
269 DCHECK(IsSplitLongOrDouble());
270 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
271 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
272 return (low_half & 0xffffffffULL) | (high_half << 32);
273 }
274
275 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700276 // This implementation doesn't support reg-spill area for hard float
277 // ABI targets such as x86_64 and aarch64. So, for those targets whose
278 // 'kQuickSoftFloatAbi' is 'false':
279 // (a) 'stack_args_' should point to the first method's argument
280 // (b) whatever the argument type it is, the 'stack_index_' should
281 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800282 gpr_index_ = 0;
283 fpr_index_ = 0;
284 stack_index_ = 0;
285 if (!is_static_) { // Handle this.
286 cur_type_ = Primitive::kPrimNot;
287 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700288 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700289 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
290 stack_index_++;
291 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800292 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 }
Ian Rogers848871b2013-08-05 10:56:33 -0700295 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
297 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
298 switch (cur_type_) {
299 case Primitive::kPrimNot:
300 case Primitive::kPrimBoolean:
301 case Primitive::kPrimByte:
302 case Primitive::kPrimChar:
303 case Primitive::kPrimShort:
304 case Primitive::kPrimInt:
305 is_split_long_or_double_ = false;
306 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700307 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
308 stack_index_++;
309 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800310 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800311 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 }
313 break;
314 case Primitive::kPrimFloat:
315 is_split_long_or_double_ = false;
316 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800317 if (kQuickSoftFloatAbi) {
318 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800319 gpr_index_++;
320 } else {
321 stack_index_++;
322 }
323 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800324 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800325 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800326 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700327 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800328 }
329 break;
330 case Primitive::kPrimDouble:
331 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800332 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000333 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800334 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800335 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700336 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800337 if (kBytesStackArgLocation == 4) {
338 stack_index_+= 2;
339 } else {
340 CHECK_EQ(kBytesStackArgLocation, 8U);
341 stack_index_++;
342 }
343 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700344 if (gpr_index_ < kNumQuickGprArgs) {
345 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000346 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700347 if (gpr_index_ < kNumQuickGprArgs) {
348 gpr_index_++;
349 } else if (kQuickSoftFloatAbi) {
350 stack_index_++;
351 }
352 }
353 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800358 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800359 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000360 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800361 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 }
364 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700365 }
366 if (kBytesStackArgLocation == 4) {
367 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700369 CHECK_EQ(kBytesStackArgLocation, 8U);
370 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800371 }
372 }
373 break;
374 default:
375 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
376 }
Ian Rogers848871b2013-08-05 10:56:33 -0700377 }
378 }
379
380 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
382 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800383 if (kQuickSoftFloatAbi) {
384 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000385 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
386 + GetBytesPerGprSpillLocation(kRuntimeISA) /* ArtMethod* */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700388 // For now, there is no reg-spill area for the targets with
389 // hard float ABI. So, the offset pointing to the first method's
390 // parameter ('this' for non-static methods) should be returned.
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000391 return GetBytesPerGprSpillLocation(kRuntimeISA); // Skip Method*.
Ian Rogers848871b2013-08-05 10:56:33 -0700392 }
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
394
395 const bool is_static_;
396 const char* const shorty_;
397 const uint32_t shorty_len_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800398 byte* const gpr_args_; // Address of GPR arguments in callee save frame.
399 byte* const fpr_args_; // Address of FPR arguments in callee save frame.
400 byte* const stack_args_; // Address of stack arguments in caller's frame.
401 uint32_t gpr_index_; // Index into spilled GPRs.
402 uint32_t fpr_index_; // Index into spilled FPRs.
403 uint32_t stack_index_; // Index into arguments on the stack.
404 // The current type of argument during VisitArguments.
405 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700406 // Does a 64bit parameter straddle the register and stack arguments?
407 bool is_split_long_or_double_;
408};
409
410// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800411class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700412 public:
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 BuildQuickShadowFrameVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
414 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Ian Rogers848871b2013-08-05 10:56:33 -0700415 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
416
Ian Rogers9758f792014-03-13 09:02:55 -0700417 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700418
419 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800420 ShadowFrame* const sf_;
421 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700422
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700423 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700424};
425
Ian Rogers9758f792014-03-13 09:02:55 -0700426void BuildQuickShadowFrameVisitor::Visit() {
427 Primitive::Type type = GetParamPrimitiveType();
428 switch (type) {
429 case Primitive::kPrimLong: // Fall-through.
430 case Primitive::kPrimDouble:
431 if (IsSplitLongOrDouble()) {
432 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
433 } else {
434 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
435 }
436 ++cur_reg_;
437 break;
438 case Primitive::kPrimNot: {
439 StackReference<mirror::Object>* stack_ref =
440 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
441 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
442 }
443 break;
444 case Primitive::kPrimBoolean: // Fall-through.
445 case Primitive::kPrimByte: // Fall-through.
446 case Primitive::kPrimChar: // Fall-through.
447 case Primitive::kPrimShort: // Fall-through.
448 case Primitive::kPrimInt: // Fall-through.
449 case Primitive::kPrimFloat:
450 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
451 break;
452 case Primitive::kPrimVoid:
453 LOG(FATAL) << "UNREACHABLE";
454 break;
455 }
456 ++cur_reg_;
457}
458
Brian Carlstromea46f952013-07-30 01:26:50 -0700459extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
460 mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
462 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
463 // frame.
464 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
465
466 if (method->IsAbstract()) {
467 ThrowAbstractMethodError(method);
468 return 0;
469 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800470 DCHECK(!method->IsNative()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700471 const char* old_cause = self->StartAssertNoThreadSuspension("Building interpreter shadow frame");
472 MethodHelper mh(method);
473 const DexFile::CodeItem* code_item = mh.GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800474 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700475 uint16_t num_regs = code_item->registers_size_;
476 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
477 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, NULL, // No last shadow coming from quick.
478 method, 0, memory));
479 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700480 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, mh.IsStatic(), mh.GetShorty(),
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 mh.GetShortyLength(),
482 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700483 shadow_frame_builder.VisitArguments();
484 // Push a transition back into managed code onto the linked list in thread.
485 ManagedStack fragment;
486 self->PushManagedStackFragment(&fragment);
487 self->PushShadowFrame(shadow_frame);
488 self->EndAssertNoThreadSuspension(old_cause);
489
490 if (method->IsStatic() && !method->GetDeclaringClass()->IsInitializing()) {
491 // Ensure static method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800492 SirtRef<mirror::Class> sirt_c(self, method->GetDeclaringClass());
493 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(sirt_c, true, true)) {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800494 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700495 self->PopManagedStackFragment(fragment);
496 return 0;
497 }
498 }
499
500 JValue result = interpreter::EnterInterpreterFromStub(self, mh, code_item, *shadow_frame);
501 // Pop transition.
502 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800503 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700504 return result.GetJ();
505 }
506}
507
508// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
509// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800510class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700511 public:
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 BuildQuickArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
Ian Rogers848871b2013-08-05 10:56:33 -0700513 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa,
514 std::vector<jvalue>* args) :
515 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
516
Ian Rogers9758f792014-03-13 09:02:55 -0700517 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700518
Ian Rogers9758f792014-03-13 09:02:55 -0700519 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800520
Ian Rogers848871b2013-08-05 10:56:33 -0700521 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700522 ScopedObjectAccessUnchecked* const soa_;
523 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800524 // References which we must update when exiting in case the GC moved the objects.
525 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700526
Ian Rogers848871b2013-08-05 10:56:33 -0700527 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
528};
529
Ian Rogers9758f792014-03-13 09:02:55 -0700530void BuildQuickArgumentVisitor::Visit() {
531 jvalue val;
532 Primitive::Type type = GetParamPrimitiveType();
533 switch (type) {
534 case Primitive::kPrimNot: {
535 StackReference<mirror::Object>* stack_ref =
536 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
537 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
538 references_.push_back(std::make_pair(val.l, stack_ref));
539 break;
540 }
541 case Primitive::kPrimLong: // Fall-through.
542 case Primitive::kPrimDouble:
543 if (IsSplitLongOrDouble()) {
544 val.j = ReadSplitLongParam();
545 } else {
546 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
547 }
548 break;
549 case Primitive::kPrimBoolean: // Fall-through.
550 case Primitive::kPrimByte: // Fall-through.
551 case Primitive::kPrimChar: // Fall-through.
552 case Primitive::kPrimShort: // Fall-through.
553 case Primitive::kPrimInt: // Fall-through.
554 case Primitive::kPrimFloat:
555 val.i = *reinterpret_cast<jint*>(GetParamAddress());
556 break;
557 case Primitive::kPrimVoid:
558 LOG(FATAL) << "UNREACHABLE";
559 val.j = 0;
560 break;
561 }
562 args_->push_back(val);
563}
564
565void BuildQuickArgumentVisitor::FixupReferences() {
566 // Fixup any references which may have changed.
567 for (const auto& pair : references_) {
568 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700569 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700570 }
571}
572
Ian Rogers848871b2013-08-05 10:56:33 -0700573// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
574// which is responsible for recording callee save registers. We explicitly place into jobjects the
575// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
576// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700577extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700578 mirror::Object* receiver,
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700581 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
582 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700583 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
584 const char* old_cause =
585 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
586 // Register the top of the managed stack, making stack crawlable.
Brian Carlstromd3633d52013-08-20 21:06:26 -0700587 DCHECK_EQ(*sp, proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700588 self->SetTopOfStack(sp, 0);
589 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700590 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
591 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700592 self->VerifyStack();
593 // Start new JNI local reference state.
594 JNIEnvExt* env = self->GetJniEnv();
595 ScopedObjectAccessUnchecked soa(env);
596 ScopedJniEnvLocalRefState env_state(env);
597 // Create local ref. copies of proxy method and the receiver.
598 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
599
600 // Placing arguments into args vector and remove the receiver.
601 MethodHelper proxy_mh(proxy_method);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700602 DCHECK(!proxy_mh.IsStatic()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700603 std::vector<jvalue> args;
604 BuildQuickArgumentVisitor local_ref_visitor(sp, proxy_mh.IsStatic(), proxy_mh.GetShorty(),
605 proxy_mh.GetShortyLength(), &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700606
Ian Rogers848871b2013-08-05 10:56:33 -0700607 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700608 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700609 args.erase(args.begin());
610
611 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700612 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700613 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700614 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
615 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
616
617 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
618 // that performs allocations.
619 self->EndAssertNoThreadSuspension(old_cause);
620 JValue result = InvokeProxyInvocationHandler(soa, proxy_mh.GetShorty(),
621 rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800622 // Restore references which might have moved.
623 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700624 return result.GetJ();
625}
626
627// Read object references held in arguments from quick frames and place in a JNI local references,
628// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800629class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700630 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700631 RememberForGcArgumentVisitor(mirror::ArtMethod** sp, bool is_static, const char* shorty,
632 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Ian Rogers848871b2013-08-05 10:56:33 -0700633 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
634
Ian Rogers9758f792014-03-13 09:02:55 -0700635 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700636
Ian Rogers9758f792014-03-13 09:02:55 -0700637 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700638
639 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700640 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800641 // References which we must update when exiting in case the GC moved the objects.
642 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700643 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700644};
645
Ian Rogers9758f792014-03-13 09:02:55 -0700646void RememberForGcArgumentVisitor::Visit() {
647 if (IsParamAReference()) {
648 StackReference<mirror::Object>* stack_ref =
649 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
650 jobject reference =
651 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
652 references_.push_back(std::make_pair(reference, stack_ref));
653 }
654}
655
656void RememberForGcArgumentVisitor::FixupReferences() {
657 // Fixup any references which may have changed.
658 for (const auto& pair : references_) {
659 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700660 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700661 }
662}
663
664
Ian Rogers848871b2013-08-05 10:56:33 -0700665// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700666extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700667 mirror::Object* receiver,
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800668 Thread* self, mirror::ArtMethod** sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800670 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -0700671 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800672 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700673 ScopedObjectAccessUnchecked soa(env);
674 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800675 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700676
677 // Compute details about the called method (avoid GCs)
678 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700680 InvokeType invoke_type;
681 const DexFile* dex_file;
682 uint32_t dex_method_idx;
683 if (called->IsRuntimeMethod()) {
684 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
685 const DexFile::CodeItem* code;
686 {
687 MethodHelper mh(caller);
688 dex_file = &mh.GetDexFile();
689 code = mh.GetCodeItem();
690 }
691 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
692 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
693 Instruction::Code instr_code = instr->Opcode();
694 bool is_range;
695 switch (instr_code) {
696 case Instruction::INVOKE_DIRECT:
697 invoke_type = kDirect;
698 is_range = false;
699 break;
700 case Instruction::INVOKE_DIRECT_RANGE:
701 invoke_type = kDirect;
702 is_range = true;
703 break;
704 case Instruction::INVOKE_STATIC:
705 invoke_type = kStatic;
706 is_range = false;
707 break;
708 case Instruction::INVOKE_STATIC_RANGE:
709 invoke_type = kStatic;
710 is_range = true;
711 break;
712 case Instruction::INVOKE_SUPER:
713 invoke_type = kSuper;
714 is_range = false;
715 break;
716 case Instruction::INVOKE_SUPER_RANGE:
717 invoke_type = kSuper;
718 is_range = true;
719 break;
720 case Instruction::INVOKE_VIRTUAL:
721 invoke_type = kVirtual;
722 is_range = false;
723 break;
724 case Instruction::INVOKE_VIRTUAL_RANGE:
725 invoke_type = kVirtual;
726 is_range = true;
727 break;
728 case Instruction::INVOKE_INTERFACE:
729 invoke_type = kInterface;
730 is_range = false;
731 break;
732 case Instruction::INVOKE_INTERFACE_RANGE:
733 invoke_type = kInterface;
734 is_range = true;
735 break;
736 default:
737 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
738 // Avoid used uninitialized warnings.
739 invoke_type = kDirect;
740 is_range = false;
741 }
742 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
743
744 } else {
745 invoke_type = kStatic;
746 dex_file = &MethodHelper(called).GetDexFile();
747 dex_method_idx = called->GetDexMethodIndex();
748 }
749 uint32_t shorty_len;
750 const char* shorty =
751 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700752 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700753 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800754 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800755 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700756 // Resolve method filling in dex cache.
757 if (called->IsRuntimeMethod()) {
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800758 SirtRef<mirror::Object> sirt_receiver(soa.Self(), virtual_or_interface ? receiver : nullptr);
Ian Rogers848871b2013-08-05 10:56:33 -0700759 called = linker->ResolveMethod(dex_method_idx, caller, invoke_type);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800760 receiver = sirt_receiver.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700761 }
762 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800763 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700764 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800765 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
766 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800767 if (virtual_or_interface) {
768 // Refine called method based on receiver.
769 CHECK(receiver != nullptr) << invoke_type;
770 if (invoke_type == kVirtual) {
771 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
772 } else {
773 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
774 }
Ian Rogers83883d72013-10-21 21:07:24 -0700775 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
776 // of the sharpened method.
777 if (called->GetDexCacheResolvedMethods() == caller->GetDexCacheResolvedMethods()) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100778 caller->GetDexCacheResolvedMethods()->Set<false>(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700779 } else {
780 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000781 // the caller's dex file. Since we get here only if the original called was a runtime
782 // method, we've got the correct dex_file and a dex_method_idx from above.
783 DCHECK(&MethodHelper(caller).GetDexFile() == dex_file);
Ian Rogers83883d72013-10-21 21:07:24 -0700784 uint32_t method_index =
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000785 MethodHelper(called).FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700786 if (method_index != DexFile::kDexNoIndex) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100787 caller->GetDexCacheResolvedMethods()->Set<false>(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700788 }
789 }
790 }
Ian Rogers848871b2013-08-05 10:56:33 -0700791 // Ensure that the called method's class is initialized.
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800792 SirtRef<mirror::Class> called_class(soa.Self(), called->GetDeclaringClass());
Ian Rogers848871b2013-08-05 10:56:33 -0700793 linker->EnsureInitialized(called_class, true, true);
794 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800795 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700796 } else if (called_class->IsInitializing()) {
797 if (invoke_type == kStatic) {
798 // Class is still initializing, go to oat and grab code (trampoline must be left in place
799 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800800 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700801 } else {
802 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800803 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700804 }
805 } else {
806 DCHECK(called_class->IsErroneous());
807 }
808 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800809 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700810 // Fixup any locally saved objects may have moved during a GC.
811 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700812 // Place called method in callee-save frame to be placed as first argument to quick method.
813 *sp = called;
814 return code;
815}
816
Andreas Gampec147b002014-03-06 18:11:06 -0800817
818
819/*
820 * This class uses a couple of observations to unite the different calling conventions through
821 * a few constants.
822 *
823 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
824 * possible alignment.
825 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
826 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
827 * when we have to split things
828 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
829 * and we can use Int handling directly.
830 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
831 * necessary when widening. Also, widening of Ints will take place implicitly, and the
832 * extension should be compatible with Aarch64, which mandates copying the available bits
833 * into LSB and leaving the rest unspecified.
834 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
835 * the stack.
836 * 6) There is only little endian.
837 *
838 *
839 * Actual work is supposed to be done in a delegate of the template type. The interface is as
840 * follows:
841 *
842 * void PushGpr(uintptr_t): Add a value for the next GPR
843 *
844 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
845 * padding, that is, think the architecture is 32b and aligns 64b.
846 *
847 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
848 * split this if necessary. The current state will have aligned, if
849 * necessary.
850 *
851 * void PushStack(uintptr_t): Push a value to the stack.
852 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700853 * uintptr_t PushSirt(mirror::Object* ref): Add a reference to the Sirt. This _will_ have nullptr,
854 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800855 * Must return the jobject, that is, the reference to the
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700856 * entry in the Sirt (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800857 *
858 */
859template <class T> class BuildGenericJniFrameStateMachine {
860 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800861#if defined(__arm__)
862 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800863 static constexpr bool kNativeSoftFloatAbi = true;
864 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800865 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
866
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800867 static constexpr size_t kRegistersNeededForLong = 2;
868 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800869 static constexpr bool kMultiRegistersAligned = true;
870 static constexpr bool kMultiRegistersWidened = false;
871 static constexpr bool kAlignLongOnStack = true;
872 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000873#elif defined(__aarch64__)
874 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
875 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
876 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
877
878 static constexpr size_t kRegistersNeededForLong = 1;
879 static constexpr size_t kRegistersNeededForDouble = 1;
880 static constexpr bool kMultiRegistersAligned = false;
881 static constexpr bool kMultiRegistersWidened = false;
882 static constexpr bool kAlignLongOnStack = false;
883 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800884#elif defined(__mips__)
885 // TODO: These are all dummy values!
886 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
887 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
888 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
889
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800890 static constexpr size_t kRegistersNeededForLong = 2;
891 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800892 static constexpr bool kMultiRegistersAligned = true;
893 static constexpr bool kMultiRegistersWidened = true;
894 static constexpr bool kAlignLongOnStack = false;
895 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800896#elif defined(__i386__)
897 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800898 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800899 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
900 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
901
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800902 static constexpr size_t kRegistersNeededForLong = 2;
903 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800904 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
905 static constexpr bool kMultiRegistersWidened = false;
906 static constexpr bool kAlignLongOnStack = false;
907 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800908#elif defined(__x86_64__)
909 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
910 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
911 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
912
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800913 static constexpr size_t kRegistersNeededForLong = 1;
914 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800915 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800916 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800917 static constexpr bool kAlignLongOnStack = false;
918 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800919#else
920#error "Unsupported architecture"
921#endif
922
Andreas Gampec147b002014-03-06 18:11:06 -0800923 public:
924 explicit BuildGenericJniFrameStateMachine(T* delegate) : gpr_index_(kNumNativeGprArgs),
925 fpr_index_(kNumNativeFprArgs),
926 stack_entries_(0),
927 delegate_(delegate) {
928 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
929 // the next register is even; counting down is just to make the compiler happy...
930 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
931 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
932 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800933
Andreas Gampec147b002014-03-06 18:11:06 -0800934 virtual ~BuildGenericJniFrameStateMachine() {}
935
936 bool HavePointerGpr() {
937 return gpr_index_ > 0;
938 }
939
940 void AdvancePointer(void* val) {
941 if (HavePointerGpr()) {
942 gpr_index_--;
943 PushGpr(reinterpret_cast<uintptr_t>(val));
944 } else {
945 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
946 PushStack(reinterpret_cast<uintptr_t>(val));
947 gpr_index_ = 0;
948 }
949 }
950
951
952 bool HaveSirtGpr() {
953 return gpr_index_ > 0;
954 }
955
956 void AdvanceSirt(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700957 uintptr_t sirtRef = PushSirt(ptr);
Andreas Gampec147b002014-03-06 18:11:06 -0800958 if (HaveSirtGpr()) {
959 gpr_index_--;
960 PushGpr(sirtRef);
961 } else {
962 stack_entries_++;
963 PushStack(sirtRef);
964 gpr_index_ = 0;
965 }
966 }
967
968
969 bool HaveIntGpr() {
970 return gpr_index_ > 0;
971 }
972
973 void AdvanceInt(uint32_t val) {
974 if (HaveIntGpr()) {
975 gpr_index_--;
976 PushGpr(val);
977 } else {
978 stack_entries_++;
979 PushStack(val);
980 gpr_index_ = 0;
981 }
982 }
983
984
985 bool HaveLongGpr() {
986 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
987 }
988
989 bool LongGprNeedsPadding() {
990 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
991 kAlignLongOnStack && // and when it needs alignment
992 (gpr_index_ & 1) == 1; // counter is odd, see constructor
993 }
994
995 bool LongStackNeedsPadding() {
996 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
997 kAlignLongOnStack && // and when it needs 8B alignment
998 (stack_entries_ & 1) == 1; // counter is odd
999 }
1000
1001 void AdvanceLong(uint64_t val) {
1002 if (HaveLongGpr()) {
1003 if (LongGprNeedsPadding()) {
1004 PushGpr(0);
1005 gpr_index_--;
1006 }
1007 if (kRegistersNeededForLong == 1) {
1008 PushGpr(static_cast<uintptr_t>(val));
1009 } else {
1010 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1011 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1012 }
1013 gpr_index_ -= kRegistersNeededForLong;
1014 } else {
1015 if (LongStackNeedsPadding()) {
1016 PushStack(0);
1017 stack_entries_++;
1018 }
1019 if (kRegistersNeededForLong == 1) {
1020 PushStack(static_cast<uintptr_t>(val));
1021 stack_entries_++;
1022 } else {
1023 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1024 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1025 stack_entries_ += 2;
1026 }
1027 gpr_index_ = 0;
1028 }
1029 }
1030
1031
1032 bool HaveFloatFpr() {
1033 return fpr_index_ > 0;
1034 }
1035
Andreas Gampec147b002014-03-06 18:11:06 -08001036 template <typename U, typename V> V convert(U in) {
1037 CHECK_LE(sizeof(U), sizeof(V));
1038 union { U u; V v; } tmp;
1039 tmp.u = in;
1040 return tmp.v;
1041 }
1042
1043 void AdvanceFloat(float val) {
1044 if (kNativeSoftFloatAbi) {
1045 AdvanceInt(convert<float, uint32_t>(val));
1046 } else {
1047 if (HaveFloatFpr()) {
1048 fpr_index_--;
1049 if (kRegistersNeededForDouble == 1) {
1050 if (kMultiRegistersWidened) {
1051 PushFpr8(convert<double, uint64_t>(val));
1052 } else {
1053 // No widening, just use the bits.
1054 PushFpr8(convert<float, uint64_t>(val));
1055 }
1056 } else {
1057 PushFpr4(val);
1058 }
1059 } else {
1060 stack_entries_++;
1061 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1062 // Need to widen before storing: Note the "double" in the template instantiation.
1063 PushStack(convert<double, uintptr_t>(val));
1064 } else {
1065 PushStack(convert<float, uintptr_t>(val));
1066 }
1067 fpr_index_ = 0;
1068 }
1069 }
1070 }
1071
1072
1073 bool HaveDoubleFpr() {
1074 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1075 }
1076
1077 bool DoubleFprNeedsPadding() {
1078 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1079 kAlignDoubleOnStack && // and when it needs alignment
1080 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1081 }
1082
1083 bool DoubleStackNeedsPadding() {
1084 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1085 kAlignDoubleOnStack && // and when it needs 8B alignment
1086 (stack_entries_ & 1) == 1; // counter is odd
1087 }
1088
1089 void AdvanceDouble(uint64_t val) {
1090 if (kNativeSoftFloatAbi) {
1091 AdvanceLong(val);
1092 } else {
1093 if (HaveDoubleFpr()) {
1094 if (DoubleFprNeedsPadding()) {
1095 PushFpr4(0);
1096 fpr_index_--;
1097 }
1098 PushFpr8(val);
1099 fpr_index_ -= kRegistersNeededForDouble;
1100 } else {
1101 if (DoubleStackNeedsPadding()) {
1102 PushStack(0);
1103 stack_entries_++;
1104 }
1105 if (kRegistersNeededForDouble == 1) {
1106 PushStack(static_cast<uintptr_t>(val));
1107 stack_entries_++;
1108 } else {
1109 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1110 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1111 stack_entries_ += 2;
1112 }
1113 fpr_index_ = 0;
1114 }
1115 }
1116 }
1117
1118 uint32_t getStackEntries() {
1119 return stack_entries_;
1120 }
1121
1122 uint32_t getNumberOfUsedGprs() {
1123 return kNumNativeGprArgs - gpr_index_;
1124 }
1125
1126 uint32_t getNumberOfUsedFprs() {
1127 return kNumNativeFprArgs - fpr_index_;
1128 }
1129
1130 private:
1131 void PushGpr(uintptr_t val) {
1132 delegate_->PushGpr(val);
1133 }
1134 void PushFpr4(float val) {
1135 delegate_->PushFpr4(val);
1136 }
1137 void PushFpr8(uint64_t val) {
1138 delegate_->PushFpr8(val);
1139 }
1140 void PushStack(uintptr_t val) {
1141 delegate_->PushStack(val);
1142 }
1143 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1144 return delegate_->PushSirt(ref);
1145 }
1146
1147 uint32_t gpr_index_; // Number of free GPRs
1148 uint32_t fpr_index_; // Number of free FPRs
1149 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1150 // extended
1151 T* delegate_; // What Push implementation gets called
1152};
1153
1154class ComputeGenericJniFrameSize FINAL {
1155 public:
1156 ComputeGenericJniFrameSize() : num_sirt_references_(0), num_stack_entries_(0) {}
1157
Andreas Gampec147b002014-03-06 18:11:06 -08001158 uint32_t GetStackSize() {
1159 return num_stack_entries_ * sizeof(uintptr_t);
1160 }
1161
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001162 // WARNING: After this, *sp won't be pointing to the method anymore!
1163 void ComputeLayout(mirror::ArtMethod*** m, bool is_static, const char* shorty, uint32_t shorty_len,
1164 void* sp, StackIndirectReferenceTable** table, uint32_t* sirt_entries,
1165 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr,
1166 void** code_return, size_t* overall_size)
Andreas Gampec147b002014-03-06 18:11:06 -08001167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1168 ComputeAll(is_static, shorty, shorty_len);
1169
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001170 mirror::ArtMethod* method = **m;
1171
Andreas Gampec147b002014-03-06 18:11:06 -08001172 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
Andreas Gampec147b002014-03-06 18:11:06 -08001173
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001174 // First, fix up the layout of the callee-save frame.
1175 // We have to squeeze in the Sirt, and relocate the method pointer.
1176
1177 // "Free" the slot for the method.
1178 sp8 += kPointerSize;
1179
1180 // Add the Sirt.
Andreas Gampec147b002014-03-06 18:11:06 -08001181 *sirt_entries = num_sirt_references_;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001182 size_t sirt_size = StackIndirectReferenceTable::GetAlignedSirtSize(num_sirt_references_);
1183 sp8 -= sirt_size;
Andreas Gampec147b002014-03-06 18:11:06 -08001184 *table = reinterpret_cast<StackIndirectReferenceTable*>(sp8);
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001185 (*table)->SetNumberOfReferences(num_sirt_references_);
Andreas Gampec147b002014-03-06 18:11:06 -08001186
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001187 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1188 sp8 -= kPointerSize;
1189 uint8_t* method_pointer = sp8;
1190 *(reinterpret_cast<mirror::ArtMethod**>(method_pointer)) = method;
1191 *m = reinterpret_cast<mirror::ArtMethod**>(method_pointer);
1192
1193 // Reference cookie and padding
1194 sp8 -= 8;
1195 // Store Sirt size
1196 *reinterpret_cast<uint32_t*>(sp8) = static_cast<uint32_t>(sirt_size & 0xFFFFFFFF);
1197
1198 // Next comes the native call stack.
Andreas Gampec147b002014-03-06 18:11:06 -08001199 sp8 -= GetStackSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001200 // Now align the call stack below. This aligns by 16, as AArch64 seems to require.
Andreas Gampec147b002014-03-06 18:11:06 -08001201 uintptr_t mask = ~0x0F;
1202 sp8 = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(sp8) & mask);
1203 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
1204
1205 // put fprs and gprs below
1206 // Assumption is OK right now, as we have soft-float arm
1207 size_t fregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeFprArgs;
1208 sp8 -= fregs * sizeof(uintptr_t);
1209 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1210 size_t iregs = BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize>::kNumNativeGprArgs;
1211 sp8 -= iregs * sizeof(uintptr_t);
1212 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1213
1214 // reserve space for the code pointer
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001215 sp8 -= kPointerSize;
Andreas Gampec147b002014-03-06 18:11:06 -08001216 *code_return = reinterpret_cast<void*>(sp8);
1217
1218 *overall_size = reinterpret_cast<uint8_t*>(sp) - sp8;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001219
1220 // The new SP is stored at the end of the alloca, so it can be immediately popped
1221 sp8 = reinterpret_cast<uint8_t*>(sp) - 5 * KB;
1222 *(reinterpret_cast<uint8_t**>(sp8)) = method_pointer;
Andreas Gampec147b002014-03-06 18:11:06 -08001223 }
1224
1225 void ComputeSirtOffset() { } // nothing to do, static right now
1226
1227 void ComputeAll(bool is_static, const char* shorty, uint32_t shorty_len)
1228 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1229 BuildGenericJniFrameStateMachine<ComputeGenericJniFrameSize> sm(this);
1230
1231 // JNIEnv
1232 sm.AdvancePointer(nullptr);
1233
1234 // Class object or this as first argument
1235 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678));
1236
1237 for (uint32_t i = 1; i < shorty_len; ++i) {
1238 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1239 switch (cur_type_) {
1240 case Primitive::kPrimNot:
1241 sm.AdvanceSirt(reinterpret_cast<mirror::Object*>(0x12345678));
1242 break;
1243
1244 case Primitive::kPrimBoolean:
1245 case Primitive::kPrimByte:
1246 case Primitive::kPrimChar:
1247 case Primitive::kPrimShort:
1248 case Primitive::kPrimInt:
1249 sm.AdvanceInt(0);
1250 break;
1251 case Primitive::kPrimFloat:
1252 sm.AdvanceFloat(0);
1253 break;
1254 case Primitive::kPrimDouble:
1255 sm.AdvanceDouble(0);
1256 break;
1257 case Primitive::kPrimLong:
1258 sm.AdvanceLong(0);
1259 break;
1260 default:
1261 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1262 }
1263 }
1264
1265 num_stack_entries_ = sm.getStackEntries();
1266 }
1267
1268 void PushGpr(uintptr_t /* val */) {
1269 // not optimizing registers, yet
1270 }
1271
1272 void PushFpr4(float /* val */) {
1273 // not optimizing registers, yet
1274 }
1275
1276 void PushFpr8(uint64_t /* val */) {
1277 // not optimizing registers, yet
1278 }
1279
1280 void PushStack(uintptr_t /* val */) {
1281 // counting is already done in the superclass
1282 }
1283
1284 uintptr_t PushSirt(mirror::Object* /* ptr */) {
1285 num_sirt_references_++;
1286 return reinterpret_cast<uintptr_t>(nullptr);
1287 }
1288
1289 private:
1290 uint32_t num_sirt_references_;
1291 uint32_t num_stack_entries_;
1292};
1293
1294// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1295// of transitioning into native code.
1296class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001297 public:
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001298 BuildGenericJniFrameVisitor(mirror::ArtMethod*** sp, bool is_static, const char* shorty,
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001299 uint32_t shorty_len, Thread* self) :
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001300 QuickArgumentVisitor(*sp, is_static, shorty, shorty_len), sm_(this) {
Andreas Gampec147b002014-03-06 18:11:06 -08001301 ComputeGenericJniFrameSize fsc;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001302 fsc.ComputeLayout(sp, is_static, shorty, shorty_len, *sp, &sirt_, &sirt_expected_refs_,
1303 &cur_stack_arg_, &cur_gpr_reg_, &cur_fpr_reg_, &code_return_,
1304 &alloca_used_size_);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001305 sirt_number_of_references_ = 0;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001306 cur_sirt_entry_ = reinterpret_cast<StackReference<mirror::Object>*>(GetFirstSirtEntry());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001307
1308 // jni environment is always first argument
Andreas Gampec147b002014-03-06 18:11:06 -08001309 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001310
1311 if (is_static) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001312 sm_.AdvanceSirt((**sp)->GetDeclaringClass());
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001313 }
1314 }
1315
Ian Rogers9758f792014-03-13 09:02:55 -07001316 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001317
Ian Rogers9758f792014-03-13 09:02:55 -07001318 void FinalizeSirt(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001319
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001320 jobject GetFirstSirtEntry() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1321 return reinterpret_cast<jobject>(sirt_->GetStackReference(0));
Andreas Gampec147b002014-03-06 18:11:06 -08001322 }
1323
1324 void PushGpr(uintptr_t val) {
1325 *cur_gpr_reg_ = val;
1326 cur_gpr_reg_++;
1327 }
1328
1329 void PushFpr4(float val) {
1330 *cur_fpr_reg_ = val;
1331 cur_fpr_reg_++;
1332 }
1333
1334 void PushFpr8(uint64_t val) {
1335 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1336 *tmp = val;
1337 cur_fpr_reg_ += 2;
1338 }
1339
1340 void PushStack(uintptr_t val) {
1341 *cur_stack_arg_ = val;
1342 cur_stack_arg_++;
1343 }
1344
1345 uintptr_t PushSirt(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001346 uintptr_t tmp;
1347 if (ref == nullptr) {
1348 *cur_sirt_entry_ = StackReference<mirror::Object>();
1349 tmp = reinterpret_cast<uintptr_t>(nullptr);
1350 } else {
1351 *cur_sirt_entry_ = StackReference<mirror::Object>::FromMirrorPtr(ref);
1352 tmp = reinterpret_cast<uintptr_t>(cur_sirt_entry_);
1353 }
1354 cur_sirt_entry_++;
Andreas Gampec147b002014-03-06 18:11:06 -08001355 sirt_number_of_references_++;
1356 return tmp;
1357 }
1358
1359 // Size of the part of the alloca that we actually need.
1360 size_t GetAllocaUsedSize() {
1361 return alloca_used_size_;
1362 }
1363
1364 void* GetCodeReturn() {
1365 return code_return_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001366 }
1367
1368 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001369 uint32_t sirt_number_of_references_;
1370 StackReference<mirror::Object>* cur_sirt_entry_;
Andreas Gampec147b002014-03-06 18:11:06 -08001371 StackIndirectReferenceTable* sirt_;
1372 uint32_t sirt_expected_refs_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001373 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001374 uint32_t* cur_fpr_reg_;
1375 uintptr_t* cur_stack_arg_;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001376 // StackReference<mirror::Object>* top_of_sirt_;
Andreas Gampec147b002014-03-06 18:11:06 -08001377 void* code_return_;
1378 size_t alloca_used_size_;
1379
1380 BuildGenericJniFrameStateMachine<BuildGenericJniFrameVisitor> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001381
1382 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1383};
1384
Ian Rogers9758f792014-03-13 09:02:55 -07001385void BuildGenericJniFrameVisitor::Visit() {
1386 Primitive::Type type = GetParamPrimitiveType();
1387 switch (type) {
1388 case Primitive::kPrimLong: {
1389 jlong long_arg;
1390 if (IsSplitLongOrDouble()) {
1391 long_arg = ReadSplitLongParam();
1392 } else {
1393 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1394 }
1395 sm_.AdvanceLong(long_arg);
1396 break;
1397 }
1398 case Primitive::kPrimDouble: {
1399 uint64_t double_arg;
1400 if (IsSplitLongOrDouble()) {
1401 // Read into union so that we don't case to a double.
1402 double_arg = ReadSplitLongParam();
1403 } else {
1404 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1405 }
1406 sm_.AdvanceDouble(double_arg);
1407 break;
1408 }
1409 case Primitive::kPrimNot: {
1410 StackReference<mirror::Object>* stack_ref =
1411 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
1412 sm_.AdvanceSirt(stack_ref->AsMirrorPtr());
1413 break;
1414 }
1415 case Primitive::kPrimFloat:
1416 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1417 break;
1418 case Primitive::kPrimBoolean: // Fall-through.
1419 case Primitive::kPrimByte: // Fall-through.
1420 case Primitive::kPrimChar: // Fall-through.
1421 case Primitive::kPrimShort: // Fall-through.
1422 case Primitive::kPrimInt: // Fall-through.
1423 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1424 break;
1425 case Primitive::kPrimVoid:
1426 LOG(FATAL) << "UNREACHABLE";
1427 break;
1428 }
1429}
1430
1431void BuildGenericJniFrameVisitor::FinalizeSirt(Thread* self) {
1432 // Initialize padding entries.
1433 while (sirt_number_of_references_ < sirt_expected_refs_) {
1434 *cur_sirt_entry_ = StackReference<mirror::Object>();
1435 cur_sirt_entry_++;
1436 sirt_number_of_references_++;
1437 }
1438 sirt_->SetNumberOfReferences(sirt_expected_refs_);
1439 DCHECK_NE(sirt_expected_refs_, 0U);
1440 // Install Sirt.
1441 self->PushSirt(sirt_);
1442}
1443
Andreas Gampe90546832014-03-12 18:07:19 -07001444extern "C" void* artFindNativeMethod();
1445
Andreas Gampead615172014-04-04 16:20:13 -07001446uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1447 if (lock != nullptr) {
1448 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1449 } else {
1450 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1451 }
1452}
1453
1454void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1455 if (lock != nullptr) {
1456 JniMethodEndSynchronized(cookie, lock, self);
1457 } else {
1458 JniMethodEnd(cookie, self);
1459 }
1460}
1461
Andreas Gampec147b002014-03-06 18:11:06 -08001462/*
1463 * Initializes an alloca region assumed to be directly below sp for a native call:
1464 * Create a Sirt and call stack and fill a mini stack with values to be pushed to registers.
1465 * The final element on the stack is a pointer to the native code.
1466 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001467 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
1468 * We need to fix this, as the Sirt needs to go into the callee-save frame.
1469 *
Andreas Gampec147b002014-03-06 18:11:06 -08001470 * The return of this function denotes:
1471 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1472 * 2) An error, if the value is negative.
1473 */
1474extern "C" ssize_t artQuickGenericJniTrampoline(Thread* self, mirror::ArtMethod** sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001475 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001476 mirror::ArtMethod* called = *sp;
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001477 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001478
1479 // run the visitor
1480 MethodHelper mh(called);
Andreas Gampec147b002014-03-06 18:11:06 -08001481
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001482 BuildGenericJniFrameVisitor visitor(&sp, called->IsStatic(), mh.GetShorty(), mh.GetShortyLength(),
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001483 self);
1484 visitor.VisitArguments();
1485 visitor.FinalizeSirt(self);
1486
1487 // fix up managed-stack things in Thread
1488 self->SetTopOfStack(sp, 0);
1489
Ian Rogerse0dcd462014-03-08 15:21:04 -08001490 self->VerifyStack();
1491
Andreas Gampe90546832014-03-12 18:07:19 -07001492 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001493 uint32_t cookie;
1494 if (called->IsSynchronized()) {
1495 cookie = JniMethodStartSynchronized(visitor.GetFirstSirtEntry(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001496 if (self->IsExceptionPending()) {
1497 self->PopSirt();
Andreas Gampec147b002014-03-06 18:11:06 -08001498 // A negative value denotes an error.
1499 return -1;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001500 }
1501 } else {
1502 cookie = JniMethodStart(self);
1503 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001504 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001505 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001506
Andreas Gampe90546832014-03-12 18:07:19 -07001507 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001508 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001509
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001510 // There are two cases for the content of nativeCode:
1511 // 1) Pointer to the native function.
1512 // 2) Pointer to the trampoline for native code binding.
1513 // In the second case, we need to execute the binding and continue with the actual native function
1514 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001515 DCHECK(nativeCode != nullptr);
1516 if (nativeCode == GetJniDlsymLookupStub()) {
1517 nativeCode = artFindNativeMethod();
1518
1519 if (nativeCode == nullptr) {
1520 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001521
1522 // End JNI, as the assembly will move to deliver the exception.
1523 jobject lock = called->IsSynchronized() ? visitor.GetFirstSirtEntry() : nullptr;
1524 if (mh.GetShorty()[0] == 'L') {
1525 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1526 } else {
1527 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1528 }
1529
Andreas Gampe90546832014-03-12 18:07:19 -07001530 return -1;
1531 }
1532 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001533 }
1534
Andreas Gampe90546832014-03-12 18:07:19 -07001535 // Store the native code pointer in the stack at the right location.
Andreas Gampec147b002014-03-06 18:11:06 -08001536 uintptr_t* code_pointer = reinterpret_cast<uintptr_t*>(visitor.GetCodeReturn());
Andreas Gampec147b002014-03-06 18:11:06 -08001537 *code_pointer = reinterpret_cast<uintptr_t>(nativeCode);
1538
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001539 // 5K reserved, window_size + frame pointer used.
Andreas Gampe90546832014-03-12 18:07:19 -07001540 size_t window_size = visitor.GetAllocaUsedSize();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001541 return (5 * KB) - window_size - kPointerSize;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001542}
1543
1544/*
1545 * Is called after the native JNI code. Responsible for cleanup (SIRT, saved state) and
1546 * unlocking.
1547 */
1548extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, mirror::ArtMethod** sp,
1549 jvalue result, uint64_t result_f)
1550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1551 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
1552 mirror::ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08001553 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001554
Andreas Gampead615172014-04-04 16:20:13 -07001555 jobject lock = nullptr;
1556 if (called->IsSynchronized()) {
1557 StackIndirectReferenceTable* table =
1558 reinterpret_cast<StackIndirectReferenceTable*>(
1559 reinterpret_cast<uint8_t*>(sp) + kPointerSize);
1560 lock = reinterpret_cast<jobject>(table->GetStackReference(0));
1561 }
1562
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563 MethodHelper mh(called);
1564 char return_shorty_char = mh.GetShorty()[0];
1565
1566 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001567 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001568 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001569 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001570
1571 switch (return_shorty_char) {
1572 case 'F': // Fall-through.
1573 case 'D':
1574 return result_f;
1575 case 'Z':
1576 return result.z;
1577 case 'B':
1578 return result.b;
1579 case 'C':
1580 return result.c;
1581 case 'S':
1582 return result.s;
1583 case 'I':
1584 return result.i;
1585 case 'J':
1586 return result.j;
1587 case 'V':
1588 return 0;
1589 default:
1590 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1591 return 0;
1592 }
1593 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001594}
1595
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001596template<InvokeType type, bool access_check>
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001597static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1598 mirror::ArtMethod* caller_method,
1599 Thread* self, mirror::ArtMethod** sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001600
1601template<InvokeType type, bool access_check>
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001602static uint64_t artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
1603 mirror::ArtMethod* caller_method,
1604 Thread* self, mirror::ArtMethod** sp) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001605 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1606 type);
1607 if (UNLIKELY(method == nullptr)) {
1608 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1609 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1610 uint32_t shorty_len;
1611 const char* shorty =
1612 dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
1613 {
1614 // Remember the args in case a GC happens in FindMethodFromCode.
1615 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1616 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1617 visitor.VisitArguments();
1618 method = FindMethodFromCode<type, access_check>(method_idx, this_object, caller_method, self);
1619 visitor.FixupReferences();
1620 }
1621
1622 if (UNLIKELY(method == NULL)) {
1623 CHECK(self->IsExceptionPending());
1624 return 0; // failure
1625 }
1626 }
1627 DCHECK(!self->IsExceptionPending());
1628 const void* code = method->GetEntryPointFromQuickCompiledCode();
1629
1630 // When we return, the caller will branch to this address, so it had better not be 0!
1631 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1632 << MethodHelper(method).GetDexFile().GetLocation();
1633#ifdef __LP64__
1634 UNIMPLEMENTED(FATAL);
1635 return 0;
1636#else
1637 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1638 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1639 uint64_t result = ((code_uint << 32) | method_uint);
1640 return result;
1641#endif
1642}
1643
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001644// Explicit artInvokeCommon template function declarations to please analysis tool.
1645#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1646 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Nicolas Geoffray0bf8b9c2014-04-04 09:55:27 +01001647 uint64_t artInvokeCommon<type, access_check>(uint32_t method_idx, \
1648 mirror::Object* this_object, \
1649 mirror::ArtMethod* caller_method, \
1650 Thread* self, mirror::ArtMethod** sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001651
1652EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1653EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1654EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1655EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1656EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1657EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1658EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1659EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1660EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1661EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1662#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1663
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001664
1665// See comments in runtime_support_asm.S
1666extern "C" uint64_t artInvokeInterfaceTrampolineWithAccessCheck(uint32_t method_idx,
1667 mirror::Object* this_object,
1668 mirror::ArtMethod* caller_method,
1669 Thread* self,
1670 mirror::ArtMethod** sp)
1671 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1672 return artInvokeCommon<kInterface, true>(method_idx, this_object, caller_method, self, sp);
1673}
1674
1675
1676extern "C" uint64_t artInvokeDirectTrampolineWithAccessCheck(uint32_t method_idx,
1677 mirror::Object* this_object,
1678 mirror::ArtMethod* caller_method,
1679 Thread* self,
1680 mirror::ArtMethod** sp)
1681 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1682 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method, self, sp);
1683}
1684
1685extern "C" uint64_t artInvokeStaticTrampolineWithAccessCheck(uint32_t method_idx,
1686 mirror::Object* this_object,
1687 mirror::ArtMethod* caller_method,
1688 Thread* self,
1689 mirror::ArtMethod** sp)
1690 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1691 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method, self, sp);
1692}
1693
1694extern "C" uint64_t artInvokeSuperTrampolineWithAccessCheck(uint32_t method_idx,
1695 mirror::Object* this_object,
1696 mirror::ArtMethod* caller_method,
1697 Thread* self,
1698 mirror::ArtMethod** sp)
1699 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1700 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method, self, sp);
1701}
1702
1703extern "C" uint64_t artInvokeVirtualTrampolineWithAccessCheck(uint32_t method_idx,
1704 mirror::Object* this_object,
1705 mirror::ArtMethod* caller_method,
1706 Thread* self,
1707 mirror::ArtMethod** sp)
1708 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1709 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method, self, sp);
1710}
1711
1712// Determine target of interface dispatch. This object is known non-null.
1713extern "C" uint64_t artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
1714 mirror::Object* this_object,
1715 mirror::ArtMethod* caller_method,
1716 Thread* self, mirror::ArtMethod** sp)
1717 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1718 mirror::ArtMethod* method;
1719 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1720 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1721 if (UNLIKELY(method == NULL)) {
1722 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1723 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1724 caller_method);
1725 return 0; // Failure.
1726 }
1727 } else {
1728 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsAndArgs);
1729 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
1730 // Determine method index from calling dex instruction.
1731#if defined(__arm__)
1732 // On entry the stack pointed by sp is:
1733 // | argN | |
1734 // | ... | |
1735 // | arg4 | |
1736 // | arg3 spill | | Caller's frame
1737 // | arg2 spill | |
1738 // | arg1 spill | |
1739 // | Method* | ---
1740 // | LR |
1741 // | ... | callee saves
1742 // | R3 | arg3
1743 // | R2 | arg2
1744 // | R1 | arg1
1745 // | R0 |
1746 // | Method* | <- sp
1747 DCHECK_EQ(48U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1748 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp) + kPointerSize);
1749 uintptr_t caller_pc = regs[10];
1750#elif defined(__i386__)
1751 // On entry the stack pointed by sp is:
1752 // | argN | |
1753 // | ... | |
1754 // | arg4 | |
1755 // | arg3 spill | | Caller's frame
1756 // | arg2 spill | |
1757 // | arg1 spill | |
1758 // | Method* | ---
1759 // | Return |
1760 // | EBP,ESI,EDI | callee saves
1761 // | EBX | arg3
1762 // | EDX | arg2
1763 // | ECX | arg1
1764 // | EAX/Method* | <- sp
1765 DCHECK_EQ(32U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1766 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1767 uintptr_t caller_pc = regs[7];
1768#elif defined(__mips__)
1769 // On entry the stack pointed by sp is:
1770 // | argN | |
1771 // | ... | |
1772 // | arg4 | |
1773 // | arg3 spill | | Caller's frame
1774 // | arg2 spill | |
1775 // | arg1 spill | |
1776 // | Method* | ---
1777 // | RA |
1778 // | ... | callee saves
1779 // | A3 | arg3
1780 // | A2 | arg2
1781 // | A1 | arg1
1782 // | A0/Method* | <- sp
1783 DCHECK_EQ(64U, Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes());
1784 uintptr_t* regs = reinterpret_cast<uintptr_t*>(reinterpret_cast<byte*>(sp));
1785 uintptr_t caller_pc = regs[15];
1786#else
1787 UNIMPLEMENTED(FATAL);
1788 uintptr_t caller_pc = 0;
1789#endif
1790 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
1791 const DexFile::CodeItem* code = MethodHelper(caller_method).GetCodeItem();
1792 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1793 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1794 Instruction::Code instr_code = instr->Opcode();
1795 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1796 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1797 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1798 uint32_t dex_method_idx;
1799 if (instr_code == Instruction::INVOKE_INTERFACE) {
1800 dex_method_idx = instr->VRegB_35c();
1801 } else {
1802 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1803 dex_method_idx = instr->VRegB_3rc();
1804 }
1805
1806 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1807 uint32_t shorty_len;
1808 const char* shorty =
1809 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
1810 {
1811 // Remember the args in case a GC happens in FindMethodFromCode.
1812 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1813 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1814 visitor.VisitArguments();
1815 method = FindMethodFromCode<kInterface, false>(dex_method_idx, this_object, caller_method,
1816 self);
1817 visitor.FixupReferences();
1818 }
1819
1820 if (UNLIKELY(method == nullptr)) {
1821 CHECK(self->IsExceptionPending());
1822 return 0; // Failure.
1823 }
1824 }
1825 const void* code = method->GetEntryPointFromQuickCompiledCode();
1826
1827 // When we return, the caller will branch to this address, so it had better not be 0!
1828 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method) << " location: "
1829 << MethodHelper(method).GetDexFile().GetLocation();
1830#ifdef __LP64__
1831 UNIMPLEMENTED(FATAL);
1832 return 0;
1833#else
1834 uint32_t method_uint = reinterpret_cast<uint32_t>(method);
1835 uint64_t code_uint = reinterpret_cast<uint32_t>(code);
1836 uint64_t result = ((code_uint << 32) | method_uint);
1837 return result;
1838#endif
1839}
1840
Ian Rogers848871b2013-08-05 10:56:33 -07001841} // namespace art