blob: 3695ff11d7e1fae686cecb7552df57382ae9f245 [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070023#include "gc/accounting/card_table-inl.h"
Andreas Gamped58342c2014-06-05 14:18:08 -070024#include "instruction_set.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "interpreter/interpreter.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070028#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033
34namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080038 // Number of bytes for each out register in the caller method's frame.
39 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070040 // Frame size in bytes of a callee-save frame for RefsAndArgs.
41 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
42 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070043#if defined(__arm__)
44 // The callee save frame is pointed to by SP.
45 // | argN | |
46 // | ... | |
47 // | arg4 | |
48 // | arg3 spill | | Caller's frame
49 // | arg2 spill | |
50 // | arg1 spill | |
51 // | Method* | ---
52 // | LR |
53 // | ... | callee saves
54 // | R3 | arg3
55 // | R2 | arg2
56 // | R1 | arg1
Ian Rogers936b37f2014-02-14 00:52:24 -080057 // | R0 | padding
Ian Rogers848871b2013-08-05 10:56:33 -070058 // | Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -080059 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
60 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
61 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +080062 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
63 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
64 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
65 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
66 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
67 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080068 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000069 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080070 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000071#elif defined(__aarch64__)
72 // The callee save frame is pointed to by SP.
73 // | argN | |
74 // | ... | |
75 // | arg4 | |
76 // | arg3 spill | | Caller's frame
77 // | arg2 spill | |
78 // | arg1 spill | |
79 // | Method* | ---
80 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080081 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000082 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080083 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000084 // | X7 |
85 // | : |
86 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080087 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000088 // | : |
89 // | D0 |
90 // | | padding
91 // | Method* | <- sp
92 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
93 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
94 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +080095 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
96 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
97 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
98 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
99 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
100 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000102 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103 }
Ian Rogers848871b2013-08-05 10:56:33 -0700104#elif defined(__mips__)
105 // The callee save frame is pointed to by SP.
106 // | argN | |
107 // | ... | |
108 // | arg4 | |
109 // | arg3 spill | | Caller's frame
110 // | arg2 spill | |
111 // | arg1 spill | |
112 // | Method* | ---
113 // | RA |
114 // | ... | callee saves
115 // | A3 | arg3
116 // | A2 | arg2
117 // | A1 | arg1
118 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800119 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
120 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
121 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800122 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
123 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
124 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800125 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000126 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800127 }
Ian Rogers848871b2013-08-05 10:56:33 -0700128#elif defined(__i386__)
129 // The callee save frame is pointed to by SP.
130 // | argN | |
131 // | ... | |
132 // | arg4 | |
133 // | arg3 spill | | Caller's frame
134 // | arg2 spill | |
135 // | arg1 spill | |
136 // | Method* | ---
137 // | Return |
138 // | EBP,ESI,EDI | callee saves
139 // | EBX | arg3
140 // | EDX | arg2
141 // | ECX | arg1
142 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800143 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
144 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
145 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800146 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
147 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
148 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800149 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000150 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800151 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800153 // The callee save frame is pointed to by SP.
154 // | argN | |
155 // | ... | |
156 // | reg. arg spills | | Caller's frame
157 // | Method* | ---
158 // | Return |
159 // | R15 | callee save
160 // | R14 | callee save
161 // | R13 | callee save
162 // | R12 | callee save
163 // | R9 | arg5
164 // | R8 | arg4
165 // | RSI/R6 | arg1
166 // | RBP/R5 | callee save
167 // | RBX/R3 | callee save
168 // | RDX/R2 | arg2
169 // | RCX/R1 | arg3
170 // | XMM7 | float arg 8
171 // | XMM6 | float arg 7
172 // | XMM5 | float arg 6
173 // | XMM4 | float arg 5
174 // | XMM3 | float arg 4
175 // | XMM2 | float arg 3
176 // | XMM1 | float arg 2
177 // | XMM0 | float arg 1
178 // | Padding |
179 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800180 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700181 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700182 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800186 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
187 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000188 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
189 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
190 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
191 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
192 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800193 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700194 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
195 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800196 }
197 }
Ian Rogers848871b2013-08-05 10:56:33 -0700198#else
199#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700200#endif
201
Ian Rogers936b37f2014-02-14 00:52:24 -0800202 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700203 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800204 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700205 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700206 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700207 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700208 }
209
Ian Rogers936b37f2014-02-14 00:52:24 -0800210 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700211 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700213 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700214 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700215 return *reinterpret_cast<uintptr_t*>(lr);
216 }
217
Andreas Gampec200a4a2014-06-16 18:39:09 -0700218 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
219 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
220 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700221 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
222 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
223 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Andreas Gampec200a4a2014-06-16 18:39:09 -0700224 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
225 gpr_index_(0), fpr_index_(0), stack_index_(0), cur_type_(Primitive::kPrimVoid),
226 is_split_long_or_double_(false) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700227
228 virtual ~QuickArgumentVisitor() {}
229
230 virtual void Visit() = 0;
231
Ian Rogers936b37f2014-02-14 00:52:24 -0800232 Primitive::Type GetParamPrimitiveType() const {
233 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700234 }
235
Ian Rogers13735952014-10-08 12:43:28 -0700236 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800237 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800238 Primitive::Type type = GetParamPrimitiveType();
239 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800240 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000241 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800242 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700243 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800244 }
245 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800246 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800247 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
248 }
249 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700250 }
251
252 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000253 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 return is_split_long_or_double_;
255 } else {
256 return false; // An optimization for when GPR and FPRs are 64bit.
257 }
Ian Rogers848871b2013-08-05 10:56:33 -0700258 }
259
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700261 return GetParamPrimitiveType() == Primitive::kPrimNot;
262 }
263
Ian Rogers936b37f2014-02-14 00:52:24 -0800264 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700265 Primitive::Type type = GetParamPrimitiveType();
266 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
267 }
268
269 uint64_t ReadSplitLongParam() const {
270 DCHECK(IsSplitLongOrDouble());
271 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
272 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_);
273 return (low_half & 0xffffffffULL) | (high_half << 32);
274 }
275
276 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700277 // This implementation doesn't support reg-spill area for hard float
278 // ABI targets such as x86_64 and aarch64. So, for those targets whose
279 // 'kQuickSoftFloatAbi' is 'false':
280 // (a) 'stack_args_' should point to the first method's argument
281 // (b) whatever the argument type it is, the 'stack_index_' should
282 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800283 gpr_index_ = 0;
284 fpr_index_ = 0;
285 stack_index_ = 0;
286 if (!is_static_) { // Handle this.
287 cur_type_ = Primitive::kPrimNot;
288 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700289 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700290 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == 0) {
291 stack_index_++;
292 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800293 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 }
Ian Rogers848871b2013-08-05 10:56:33 -0700296 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800297 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
298 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
299 switch (cur_type_) {
300 case Primitive::kPrimNot:
301 case Primitive::kPrimBoolean:
302 case Primitive::kPrimByte:
303 case Primitive::kPrimChar:
304 case Primitive::kPrimShort:
305 case Primitive::kPrimInt:
306 is_split_long_or_double_ = false;
307 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700308 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
309 stack_index_++;
310 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800311 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800312 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800313 }
314 break;
315 case Primitive::kPrimFloat:
316 is_split_long_or_double_ = false;
317 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800318 if (kQuickSoftFloatAbi) {
319 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 gpr_index_++;
321 } else {
322 stack_index_++;
323 }
324 } else {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800325 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800326 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800327 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700328 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800329 }
330 break;
331 case Primitive::kPrimDouble:
332 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800333 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000334 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800335 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800336 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700337 if (!kQuickSoftFloatAbi || kNumQuickGprArgs == gpr_index_) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800338 if (kBytesStackArgLocation == 4) {
339 stack_index_+= 2;
340 } else {
341 CHECK_EQ(kBytesStackArgLocation, 8U);
342 stack_index_++;
343 }
344 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700345 if (gpr_index_ < kNumQuickGprArgs) {
346 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000347 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700348 if (gpr_index_ < kNumQuickGprArgs) {
349 gpr_index_++;
350 } else if (kQuickSoftFloatAbi) {
351 stack_index_++;
352 }
353 }
354 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800355 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000356 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800357 ((fpr_index_ + 1) == kNumQuickFprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 Visit();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800359 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800360 fpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000361 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800362 if ((kNumQuickFprArgs != 0) && (fpr_index_ + 1 < kNumQuickFprArgs + 1)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 fpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800364 }
365 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700366 }
367 if (kBytesStackArgLocation == 4) {
368 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800369 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700370 CHECK_EQ(kBytesStackArgLocation, 8U);
371 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800372 }
373 }
374 break;
375 default:
376 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
377 }
Ian Rogers848871b2013-08-05 10:56:33 -0700378 }
379 }
380
381 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800382 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
383 uint32_t shorty_len) {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800384 if (kQuickSoftFloatAbi) {
385 CHECK_EQ(kNumQuickFprArgs, 0U);
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000386 return (kNumQuickGprArgs * GetBytesPerGprSpillLocation(kRuntimeISA))
Andreas Gampecf4035a2014-05-28 22:43:01 -0700387 + sizeof(StackReference<mirror::ArtMethod>) /* StackReference<ArtMethod> */;
Ian Rogers936b37f2014-02-14 00:52:24 -0800388 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700389 // For now, there is no reg-spill area for the targets with
390 // hard float ABI. So, the offset pointing to the first method's
391 // parameter ('this' for non-static methods) should be returned.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700392 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700393 }
Ian Rogers848871b2013-08-05 10:56:33 -0700394 }
395
Andreas Gampec200a4a2014-06-16 18:39:09 -0700396 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700397 const bool is_static_;
398 const char* const shorty_;
399 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700400
401 private:
Ian Rogers13735952014-10-08 12:43:28 -0700402 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
403 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
404 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800405 uint32_t gpr_index_; // Index into spilled GPRs.
406 uint32_t fpr_index_; // Index into spilled FPRs.
407 uint32_t stack_index_; // Index into arguments on the stack.
408 // The current type of argument during VisitArguments.
409 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700410 // Does a 64bit parameter straddle the register and stack arguments?
411 bool is_split_long_or_double_;
412};
413
414// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800415class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700416 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700417 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
418 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
419 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700420 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700421
Ian Rogers9758f792014-03-13 09:02:55 -0700422 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700423
424 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 ShadowFrame* const sf_;
426 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700427
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700428 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700429};
430
Andreas Gampec200a4a2014-06-16 18:39:09 -0700431void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700432 Primitive::Type type = GetParamPrimitiveType();
433 switch (type) {
434 case Primitive::kPrimLong: // Fall-through.
435 case Primitive::kPrimDouble:
436 if (IsSplitLongOrDouble()) {
437 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
438 } else {
439 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
440 }
441 ++cur_reg_;
442 break;
443 case Primitive::kPrimNot: {
444 StackReference<mirror::Object>* stack_ref =
445 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
446 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
447 }
448 break;
449 case Primitive::kPrimBoolean: // Fall-through.
450 case Primitive::kPrimByte: // Fall-through.
451 case Primitive::kPrimChar: // Fall-through.
452 case Primitive::kPrimShort: // Fall-through.
453 case Primitive::kPrimInt: // Fall-through.
454 case Primitive::kPrimFloat:
455 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
456 break;
457 case Primitive::kPrimVoid:
458 LOG(FATAL) << "UNREACHABLE";
459 break;
460 }
461 ++cur_reg_;
462}
463
Brian Carlstromea46f952013-07-30 01:26:50 -0700464extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700465 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700466 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
467 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
468 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700469 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700470
471 if (method->IsAbstract()) {
472 ThrowAbstractMethodError(method);
473 return 0;
474 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800475 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700476 const char* old_cause = self->StartAssertNoThreadSuspension(
477 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800479 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700480 uint16_t num_regs = code_item->registers_size_;
481 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700482 // No last shadow coming from quick.
483 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700484 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485 uint32_t shorty_len = 0;
486 const char* shorty = method->GetShorty(&shorty_len);
487 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800488 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700489 shadow_frame_builder.VisitArguments();
490 // Push a transition back into managed code onto the linked list in thread.
491 ManagedStack fragment;
492 self->PushManagedStackFragment(&fragment);
493 self->PushShadowFrame(shadow_frame);
494 self->EndAssertNoThreadSuspension(old_cause);
495
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700496 StackHandleScope<1> hs(self);
497 MethodHelper mh(hs.NewHandle(method));
498 if (mh.Get()->IsStatic() && !mh.Get()->GetDeclaringClass()->IsInitialized()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700499 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700500 StackHandleScope<1> hs(self);
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700501 Handle<mirror::Class> h_class(hs.NewHandle(mh.Get()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700502 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700503 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(mh.Get());
Ian Rogers848871b2013-08-05 10:56:33 -0700504 self->PopManagedStackFragment(fragment);
505 return 0;
506 }
507 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700508 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, &mh, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700509 // Pop transition.
510 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800511 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700512 return result.GetJ();
513 }
514}
515
516// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
517// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800518class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700519 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700520 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
521 const char* shorty, uint32_t shorty_len,
522 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700523 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700524
Ian Rogers9758f792014-03-13 09:02:55 -0700525 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700526
Ian Rogers9758f792014-03-13 09:02:55 -0700527 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800528
Ian Rogers848871b2013-08-05 10:56:33 -0700529 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700530 ScopedObjectAccessUnchecked* const soa_;
531 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800532 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700533 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700534
Ian Rogers848871b2013-08-05 10:56:33 -0700535 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
536};
537
Ian Rogers9758f792014-03-13 09:02:55 -0700538void BuildQuickArgumentVisitor::Visit() {
539 jvalue val;
540 Primitive::Type type = GetParamPrimitiveType();
541 switch (type) {
542 case Primitive::kPrimNot: {
543 StackReference<mirror::Object>* stack_ref =
544 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
545 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
546 references_.push_back(std::make_pair(val.l, stack_ref));
547 break;
548 }
549 case Primitive::kPrimLong: // Fall-through.
550 case Primitive::kPrimDouble:
551 if (IsSplitLongOrDouble()) {
552 val.j = ReadSplitLongParam();
553 } else {
554 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
555 }
556 break;
557 case Primitive::kPrimBoolean: // Fall-through.
558 case Primitive::kPrimByte: // Fall-through.
559 case Primitive::kPrimChar: // Fall-through.
560 case Primitive::kPrimShort: // Fall-through.
561 case Primitive::kPrimInt: // Fall-through.
562 case Primitive::kPrimFloat:
563 val.i = *reinterpret_cast<jint*>(GetParamAddress());
564 break;
565 case Primitive::kPrimVoid:
566 LOG(FATAL) << "UNREACHABLE";
567 val.j = 0;
568 break;
569 }
570 args_->push_back(val);
571}
572
573void BuildQuickArgumentVisitor::FixupReferences() {
574 // Fixup any references which may have changed.
575 for (const auto& pair : references_) {
576 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700577 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700578 }
579}
580
Ian Rogers848871b2013-08-05 10:56:33 -0700581// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
582// which is responsible for recording callee save registers. We explicitly place into jobjects the
583// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
584// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700585extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700586 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700587 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700588 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700589 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
590 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700591 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
592 const char* old_cause =
593 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
594 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700595 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700596 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700597 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
598 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700599 self->VerifyStack();
600 // Start new JNI local reference state.
601 JNIEnvExt* env = self->GetJniEnv();
602 ScopedObjectAccessUnchecked soa(env);
603 ScopedJniEnvLocalRefState env_state(env);
604 // Create local ref. copies of proxy method and the receiver.
605 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
606
607 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700608 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
609 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700610 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700611 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700612 uint32_t shorty_len = 0;
613 const char* shorty = proxy_method->GetShorty(&shorty_len);
614 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700615
Ian Rogers848871b2013-08-05 10:56:33 -0700616 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700617 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700618 args.erase(args.begin());
619
620 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700621 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700622 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700623 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
624 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
625
626 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
627 // that performs allocations.
628 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700629 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800630 // Restore references which might have moved.
631 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700632 return result.GetJ();
633}
634
635// Read object references held in arguments from quick frames and place in a JNI local references,
636// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800637class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700638 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700639 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
640 const char* shorty, uint32_t shorty_len,
641 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700642 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700643
Ian Rogers9758f792014-03-13 09:02:55 -0700644 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700645
Ian Rogers9758f792014-03-13 09:02:55 -0700646 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700647
648 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700649 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800650 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700651 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
652
Mathieu Chartier590fee92013-09-13 13:46:47 -0700653 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700654};
655
Ian Rogers9758f792014-03-13 09:02:55 -0700656void RememberForGcArgumentVisitor::Visit() {
657 if (IsParamAReference()) {
658 StackReference<mirror::Object>* stack_ref =
659 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
660 jobject reference =
661 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
662 references_.push_back(std::make_pair(reference, stack_ref));
663 }
664}
665
666void RememberForGcArgumentVisitor::FixupReferences() {
667 // Fixup any references which may have changed.
668 for (const auto& pair : references_) {
669 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700670 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700671 }
672}
673
Ian Rogers848871b2013-08-05 10:56:33 -0700674// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700675extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700676 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700677 Thread* self,
678 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700680 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700681 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800682 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700683 ScopedObjectAccessUnchecked soa(env);
684 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800685 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700686
687 // Compute details about the called method (avoid GCs)
688 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700689 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700690 InvokeType invoke_type;
691 const DexFile* dex_file;
692 uint32_t dex_method_idx;
693 if (called->IsRuntimeMethod()) {
694 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
695 const DexFile::CodeItem* code;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700696 dex_file = caller->GetDexFile();
697 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700698 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
699 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
700 Instruction::Code instr_code = instr->Opcode();
701 bool is_range;
702 switch (instr_code) {
703 case Instruction::INVOKE_DIRECT:
704 invoke_type = kDirect;
705 is_range = false;
706 break;
707 case Instruction::INVOKE_DIRECT_RANGE:
708 invoke_type = kDirect;
709 is_range = true;
710 break;
711 case Instruction::INVOKE_STATIC:
712 invoke_type = kStatic;
713 is_range = false;
714 break;
715 case Instruction::INVOKE_STATIC_RANGE:
716 invoke_type = kStatic;
717 is_range = true;
718 break;
719 case Instruction::INVOKE_SUPER:
720 invoke_type = kSuper;
721 is_range = false;
722 break;
723 case Instruction::INVOKE_SUPER_RANGE:
724 invoke_type = kSuper;
725 is_range = true;
726 break;
727 case Instruction::INVOKE_VIRTUAL:
728 invoke_type = kVirtual;
729 is_range = false;
730 break;
731 case Instruction::INVOKE_VIRTUAL_RANGE:
732 invoke_type = kVirtual;
733 is_range = true;
734 break;
735 case Instruction::INVOKE_INTERFACE:
736 invoke_type = kInterface;
737 is_range = false;
738 break;
739 case Instruction::INVOKE_INTERFACE_RANGE:
740 invoke_type = kInterface;
741 is_range = true;
742 break;
743 default:
744 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
745 // Avoid used uninitialized warnings.
746 invoke_type = kDirect;
747 is_range = false;
748 }
749 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700750 } else {
751 invoke_type = kStatic;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700752 dex_file = called->GetDexFile();
Ian Rogers848871b2013-08-05 10:56:33 -0700753 dex_method_idx = called->GetDexMethodIndex();
754 }
755 uint32_t shorty_len;
756 const char* shorty =
757 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700758 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700759 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800760 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800761 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700762 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700763 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700764 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700765 mirror::Object* dummy = nullptr;
766 HandleWrapper<mirror::Object> h_receiver(
767 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
768 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700769 }
770 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800771 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700772 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800773 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
774 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800775 if (virtual_or_interface) {
776 // Refine called method based on receiver.
777 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700778
779 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800780 if (invoke_type == kVirtual) {
781 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
782 } else {
783 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
784 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700785
786 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
787 << PrettyTypeOf(receiver) << " "
788 << invoke_type << " " << orig_called->GetVtableIndex();
789
Ian Rogers83883d72013-10-21 21:07:24 -0700790 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
791 // of the sharpened method.
Andreas Gampe58a5af82014-07-31 16:23:49 -0700792 if (called->HasSameDexCacheResolvedMethods(caller)) {
793 caller->SetDexCacheResolvedMethod(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700794 } else {
795 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000796 // the caller's dex file. Since we get here only if the original called was a runtime
797 // method, we've got the correct dex_file and a dex_method_idx from above.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700798 DCHECK_EQ(caller->GetDexFile(), dex_file);
799 StackHandleScope<1> hs(self);
800 MethodHelper mh(hs.NewHandle(called));
801 uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700802 if (method_index != DexFile::kDexNoIndex) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700803 caller->SetDexCacheResolvedMethod(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700804 }
805 }
806 }
Ian Rogers848871b2013-08-05 10:56:33 -0700807 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700808 StackHandleScope<1> hs(soa.Self());
809 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700810 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700811 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800812 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700813 } else if (called_class->IsInitializing()) {
814 if (invoke_type == kStatic) {
815 // Class is still initializing, go to oat and grab code (trampoline must be left in place
816 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800817 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700818 } else {
819 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800820 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700821 }
822 } else {
823 DCHECK(called_class->IsErroneous());
824 }
825 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800826 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700827 // Fixup any locally saved objects may have moved during a GC.
828 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700829 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700830 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700831 return code;
832}
833
Andreas Gampec147b002014-03-06 18:11:06 -0800834/*
835 * This class uses a couple of observations to unite the different calling conventions through
836 * a few constants.
837 *
838 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
839 * possible alignment.
840 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
841 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
842 * when we have to split things
843 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
844 * and we can use Int handling directly.
845 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
846 * necessary when widening. Also, widening of Ints will take place implicitly, and the
847 * extension should be compatible with Aarch64, which mandates copying the available bits
848 * into LSB and leaving the rest unspecified.
849 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
850 * the stack.
851 * 6) There is only little endian.
852 *
853 *
854 * Actual work is supposed to be done in a delegate of the template type. The interface is as
855 * follows:
856 *
857 * void PushGpr(uintptr_t): Add a value for the next GPR
858 *
859 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
860 * padding, that is, think the architecture is 32b and aligns 64b.
861 *
862 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
863 * split this if necessary. The current state will have aligned, if
864 * necessary.
865 *
866 * void PushStack(uintptr_t): Push a value to the stack.
867 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700868 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700869 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800870 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700871 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800872 *
873 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700874template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800875 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800876#if defined(__arm__)
877 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800878 static constexpr bool kNativeSoftFloatAbi = true;
879 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800880 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
881
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800882 static constexpr size_t kRegistersNeededForLong = 2;
883 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800884 static constexpr bool kMultiRegistersAligned = true;
885 static constexpr bool kMultiRegistersWidened = false;
886 static constexpr bool kAlignLongOnStack = true;
887 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000888#elif defined(__aarch64__)
889 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
890 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
891 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
892
893 static constexpr size_t kRegistersNeededForLong = 1;
894 static constexpr size_t kRegistersNeededForDouble = 1;
895 static constexpr bool kMultiRegistersAligned = false;
896 static constexpr bool kMultiRegistersWidened = false;
897 static constexpr bool kAlignLongOnStack = false;
898 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800899#elif defined(__mips__)
900 // TODO: These are all dummy values!
901 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
902 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
903 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
904
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800905 static constexpr size_t kRegistersNeededForLong = 2;
906 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800907 static constexpr bool kMultiRegistersAligned = true;
908 static constexpr bool kMultiRegistersWidened = true;
909 static constexpr bool kAlignLongOnStack = false;
910 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800911#elif defined(__i386__)
912 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800913 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800914 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
915 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
916
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800917 static constexpr size_t kRegistersNeededForLong = 2;
918 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700919 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800920 static constexpr bool kMultiRegistersWidened = false;
921 static constexpr bool kAlignLongOnStack = false;
922 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800923#elif defined(__x86_64__)
924 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
925 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
926 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
927
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800928 static constexpr size_t kRegistersNeededForLong = 1;
929 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800930 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800931 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800932 static constexpr bool kAlignLongOnStack = false;
933 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800934#else
935#error "Unsupported architecture"
936#endif
937
Andreas Gampec147b002014-03-06 18:11:06 -0800938 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700939 explicit BuildNativeCallFrameStateMachine(T* delegate)
940 : gpr_index_(kNumNativeGprArgs),
941 fpr_index_(kNumNativeFprArgs),
942 stack_entries_(0),
943 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800944 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
945 // the next register is even; counting down is just to make the compiler happy...
946 CHECK_EQ(kNumNativeGprArgs % 2, 0U);
947 CHECK_EQ(kNumNativeFprArgs % 2, 0U);
948 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800949
Andreas Gampec200a4a2014-06-16 18:39:09 -0700950 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800951
952 bool HavePointerGpr() {
953 return gpr_index_ > 0;
954 }
955
Andreas Gampec200a4a2014-06-16 18:39:09 -0700956 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800957 if (HavePointerGpr()) {
958 gpr_index_--;
959 PushGpr(reinterpret_cast<uintptr_t>(val));
960 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700961 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800962 PushStack(reinterpret_cast<uintptr_t>(val));
963 gpr_index_ = 0;
964 }
965 }
966
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700967 bool HaveHandleScopeGpr() {
Andreas Gampec147b002014-03-06 18:11:06 -0800968 return gpr_index_ > 0;
969 }
970
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700971 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
972 uintptr_t handle = PushHandle(ptr);
973 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800974 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700975 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800976 } else {
977 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700978 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -0800979 gpr_index_ = 0;
980 }
981 }
982
Andreas Gampec147b002014-03-06 18:11:06 -0800983 bool HaveIntGpr() {
984 return gpr_index_ > 0;
985 }
986
987 void AdvanceInt(uint32_t val) {
988 if (HaveIntGpr()) {
989 gpr_index_--;
990 PushGpr(val);
991 } else {
992 stack_entries_++;
993 PushStack(val);
994 gpr_index_ = 0;
995 }
996 }
997
Andreas Gampec147b002014-03-06 18:11:06 -0800998 bool HaveLongGpr() {
999 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1000 }
1001
1002 bool LongGprNeedsPadding() {
1003 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1004 kAlignLongOnStack && // and when it needs alignment
1005 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1006 }
1007
1008 bool LongStackNeedsPadding() {
1009 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1010 kAlignLongOnStack && // and when it needs 8B alignment
1011 (stack_entries_ & 1) == 1; // counter is odd
1012 }
1013
1014 void AdvanceLong(uint64_t val) {
1015 if (HaveLongGpr()) {
1016 if (LongGprNeedsPadding()) {
1017 PushGpr(0);
1018 gpr_index_--;
1019 }
1020 if (kRegistersNeededForLong == 1) {
1021 PushGpr(static_cast<uintptr_t>(val));
1022 } else {
1023 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1024 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1025 }
1026 gpr_index_ -= kRegistersNeededForLong;
1027 } else {
1028 if (LongStackNeedsPadding()) {
1029 PushStack(0);
1030 stack_entries_++;
1031 }
1032 if (kRegistersNeededForLong == 1) {
1033 PushStack(static_cast<uintptr_t>(val));
1034 stack_entries_++;
1035 } else {
1036 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1037 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1038 stack_entries_ += 2;
1039 }
1040 gpr_index_ = 0;
1041 }
1042 }
1043
Andreas Gampec147b002014-03-06 18:11:06 -08001044 bool HaveFloatFpr() {
1045 return fpr_index_ > 0;
1046 }
1047
Andreas Gampec147b002014-03-06 18:11:06 -08001048 void AdvanceFloat(float val) {
1049 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001050 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001051 } else {
1052 if (HaveFloatFpr()) {
1053 fpr_index_--;
1054 if (kRegistersNeededForDouble == 1) {
1055 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001056 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001057 } else {
1058 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001059 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001060 }
1061 } else {
1062 PushFpr4(val);
1063 }
1064 } else {
1065 stack_entries_++;
1066 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1067 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001068 // Note: We need to jump through those hoops to make the compiler happy.
1069 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1070 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001071 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001072 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001073 }
1074 fpr_index_ = 0;
1075 }
1076 }
1077 }
1078
Andreas Gampec147b002014-03-06 18:11:06 -08001079 bool HaveDoubleFpr() {
1080 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1081 }
1082
1083 bool DoubleFprNeedsPadding() {
1084 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1085 kAlignDoubleOnStack && // and when it needs alignment
1086 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1087 }
1088
1089 bool DoubleStackNeedsPadding() {
1090 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1091 kAlignDoubleOnStack && // and when it needs 8B alignment
1092 (stack_entries_ & 1) == 1; // counter is odd
1093 }
1094
1095 void AdvanceDouble(uint64_t val) {
1096 if (kNativeSoftFloatAbi) {
1097 AdvanceLong(val);
1098 } else {
1099 if (HaveDoubleFpr()) {
1100 if (DoubleFprNeedsPadding()) {
1101 PushFpr4(0);
1102 fpr_index_--;
1103 }
1104 PushFpr8(val);
1105 fpr_index_ -= kRegistersNeededForDouble;
1106 } else {
1107 if (DoubleStackNeedsPadding()) {
1108 PushStack(0);
1109 stack_entries_++;
1110 }
1111 if (kRegistersNeededForDouble == 1) {
1112 PushStack(static_cast<uintptr_t>(val));
1113 stack_entries_++;
1114 } else {
1115 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1116 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1117 stack_entries_ += 2;
1118 }
1119 fpr_index_ = 0;
1120 }
1121 }
1122 }
1123
1124 uint32_t getStackEntries() {
1125 return stack_entries_;
1126 }
1127
1128 uint32_t getNumberOfUsedGprs() {
1129 return kNumNativeGprArgs - gpr_index_;
1130 }
1131
1132 uint32_t getNumberOfUsedFprs() {
1133 return kNumNativeFprArgs - fpr_index_;
1134 }
1135
1136 private:
1137 void PushGpr(uintptr_t val) {
1138 delegate_->PushGpr(val);
1139 }
1140 void PushFpr4(float val) {
1141 delegate_->PushFpr4(val);
1142 }
1143 void PushFpr8(uint64_t val) {
1144 delegate_->PushFpr8(val);
1145 }
1146 void PushStack(uintptr_t val) {
1147 delegate_->PushStack(val);
1148 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001149 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1150 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001151 }
1152
1153 uint32_t gpr_index_; // Number of free GPRs
1154 uint32_t fpr_index_; // Number of free FPRs
1155 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1156 // extended
1157 T* delegate_; // What Push implementation gets called
1158};
1159
Andreas Gampec200a4a2014-06-16 18:39:09 -07001160// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1161// in subclasses.
1162//
1163// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1164// them with handles.
1165class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001166 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001167 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1168
1169 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001170
Andreas Gampec147b002014-03-06 18:11:06 -08001171 uint32_t GetStackSize() {
1172 return num_stack_entries_ * sizeof(uintptr_t);
1173 }
1174
Andreas Gampec200a4a2014-06-16 18:39:09 -07001175 uint8_t* LayoutCallStack(uint8_t* sp8) {
Andreas Gampec147b002014-03-06 18:11:06 -08001176 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001177 // Align by kStackAlignment.
1178 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001179 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001180 }
1181
Andreas Gampec200a4a2014-06-16 18:39:09 -07001182 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr) {
1183 // Assumption is OK right now, as we have soft-float arm
1184 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1185 sp8 -= fregs * sizeof(uintptr_t);
1186 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1187 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1188 sp8 -= iregs * sizeof(uintptr_t);
1189 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1190 return sp8;
1191 }
Andreas Gampec147b002014-03-06 18:11:06 -08001192
Andreas Gampec200a4a2014-06-16 18:39:09 -07001193 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
1194 uint32_t** start_fpr) {
1195 // Native call stack.
1196 sp8 = LayoutCallStack(sp8);
1197 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001198
Andreas Gampec200a4a2014-06-16 18:39:09 -07001199 // Put fprs and gprs below.
1200 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001201
Andreas Gampec200a4a2014-06-16 18:39:09 -07001202 // Return the new bottom.
1203 return sp8;
1204 }
1205
1206 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1207 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {}
1208
1209 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1210 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1211
1212 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001213
1214 for (uint32_t i = 1; i < shorty_len; ++i) {
1215 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1216 switch (cur_type_) {
1217 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001218 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001219 sm.AdvanceHandleScope(
1220 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001221 break;
1222
1223 case Primitive::kPrimBoolean:
1224 case Primitive::kPrimByte:
1225 case Primitive::kPrimChar:
1226 case Primitive::kPrimShort:
1227 case Primitive::kPrimInt:
1228 sm.AdvanceInt(0);
1229 break;
1230 case Primitive::kPrimFloat:
1231 sm.AdvanceFloat(0);
1232 break;
1233 case Primitive::kPrimDouble:
1234 sm.AdvanceDouble(0);
1235 break;
1236 case Primitive::kPrimLong:
1237 sm.AdvanceLong(0);
1238 break;
1239 default:
1240 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1241 }
1242 }
1243
1244 num_stack_entries_ = sm.getStackEntries();
1245 }
1246
1247 void PushGpr(uintptr_t /* val */) {
1248 // not optimizing registers, yet
1249 }
1250
1251 void PushFpr4(float /* val */) {
1252 // not optimizing registers, yet
1253 }
1254
1255 void PushFpr8(uint64_t /* val */) {
1256 // not optimizing registers, yet
1257 }
1258
1259 void PushStack(uintptr_t /* val */) {
1260 // counting is already done in the superclass
1261 }
1262
Andreas Gampec200a4a2014-06-16 18:39:09 -07001263 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001264 return reinterpret_cast<uintptr_t>(nullptr);
1265 }
1266
Andreas Gampec200a4a2014-06-16 18:39:09 -07001267 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001268 uint32_t num_stack_entries_;
1269};
1270
Andreas Gampec200a4a2014-06-16 18:39:09 -07001271class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001272 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001273 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001274
Andreas Gampec200a4a2014-06-16 18:39:09 -07001275 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1276 // is at *m = sp. Will update to point to the bottom of the save frame.
1277 //
1278 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001279 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1280 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1282 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001283
Andreas Gampec200a4a2014-06-16 18:39:09 -07001284 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1285
1286 // First, fix up the layout of the callee-save frame.
1287 // We have to squeeze in the HandleScope, and relocate the method pointer.
1288
1289 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001290 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001291
1292 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001293 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1294 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1295
1296 sp8 -= scope_and_method;
1297 // Align by kStackAlignment.
1298 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1299 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1300
1301 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001302 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1303 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001304
1305 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1306 uint8_t* method_pointer = sp8;
1307 StackReference<mirror::ArtMethod>* new_method_ref =
1308 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1309 new_method_ref->Assign(method);
1310 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001311 }
1312
Andreas Gampec200a4a2014-06-16 18:39:09 -07001313 // Adds space for the cookie. Note: may leave stack unaligned.
1314 void LayoutCookie(uint8_t** sp) {
1315 // Reference cookie and padding
1316 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001317 }
1318
Andreas Gampec200a4a2014-06-16 18:39:09 -07001319 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1320 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001321 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1322 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001323 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001324 // First, fix up the layout of the callee-save frame.
1325 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001326 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001327
1328 // The bottom of the callee-save frame is now where the method is, *m.
1329 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1330
1331 // Add space for cookie.
1332 LayoutCookie(&sp8);
1333
1334 return sp8;
1335 }
1336
1337 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001338 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
1339 bool is_static, const char* shorty, uint32_t shorty_len,
1340 HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001341 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1342 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1343 Walk(shorty, shorty_len);
1344
1345 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001346 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001347
1348 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1349
1350 // Return the new bottom.
1351 return sp8;
1352 }
1353
1354 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1355
1356 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1357 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1359
1360 private:
1361 uint32_t num_handle_scope_references_;
1362};
1363
1364uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1365 num_handle_scope_references_++;
1366 return reinterpret_cast<uintptr_t>(nullptr);
1367}
1368
1369void ComputeGenericJniFrameSize::WalkHeader(
1370 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1371 // JNIEnv
1372 sm->AdvancePointer(nullptr);
1373
1374 // Class object or this as first argument
1375 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1376}
1377
1378// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1379// the template requirements of BuildGenericJniFrameStateMachine.
1380class FillNativeCall {
1381 public:
1382 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1383 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1384
1385 virtual ~FillNativeCall() {}
1386
1387 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1388 cur_gpr_reg_ = gpr_regs;
1389 cur_fpr_reg_ = fpr_regs;
1390 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001391 }
1392
1393 void PushGpr(uintptr_t val) {
1394 *cur_gpr_reg_ = val;
1395 cur_gpr_reg_++;
1396 }
1397
1398 void PushFpr4(float val) {
1399 *cur_fpr_reg_ = val;
1400 cur_fpr_reg_++;
1401 }
1402
1403 void PushFpr8(uint64_t val) {
1404 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1405 *tmp = val;
1406 cur_fpr_reg_ += 2;
1407 }
1408
1409 void PushStack(uintptr_t val) {
1410 *cur_stack_arg_ = val;
1411 cur_stack_arg_++;
1412 }
1413
Andreas Gampec200a4a2014-06-16 18:39:09 -07001414 virtual uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1415 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1416 return 0U;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001417 }
1418
1419 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001420 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001421 uint32_t* cur_fpr_reg_;
1422 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001423};
Andreas Gampec147b002014-03-06 18:11:06 -08001424
Andreas Gampec200a4a2014-06-16 18:39:09 -07001425// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1426// of transitioning into native code.
1427class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1428 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001429 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1430 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001431 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1432 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1433 ComputeGenericJniFrameSize fsc;
1434 uintptr_t* start_gpr_reg;
1435 uint32_t* start_fpr_reg;
1436 uintptr_t* start_stack_arg;
Ian Rogers59c07062014-10-10 13:03:39 -07001437 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, is_static, shorty, shorty_len,
1438 &handle_scope_,
1439 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001440 &start_gpr_reg, &start_fpr_reg);
1441
Andreas Gampec200a4a2014-06-16 18:39:09 -07001442 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1443
1444 // jni environment is always first argument
1445 sm_.AdvancePointer(self->GetJniEnv());
1446
1447 if (is_static) {
1448 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1449 }
1450 }
1451
1452 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1453
1454 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1455
1456 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1457 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1458 return handle_scope_->GetHandle(0).GetReference();
1459 }
1460
1461 jobject GetFirstHandleScopeJObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1462 return handle_scope_->GetHandle(0).ToJObject();
1463 }
1464
1465 void* GetBottomOfUsedArea() {
1466 return bottom_of_used_area_;
1467 }
1468
1469 private:
1470 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1471 class FillJniCall FINAL : public FillNativeCall {
1472 public:
1473 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1474 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1475 handle_scope_(handle_scope), cur_entry_(0) {}
1476
1477 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1478
1479 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1480 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1481 handle_scope_ = scope;
1482 cur_entry_ = 0U;
1483 }
1484
1485 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1486 // Initialize padding entries.
1487 size_t expected_slots = handle_scope_->NumberOfReferences();
1488 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001489 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001490 }
1491 DCHECK_NE(cur_entry_, 0U);
1492 }
1493
1494 private:
1495 HandleScope* handle_scope_;
1496 size_t cur_entry_;
1497 };
1498
1499 HandleScope* handle_scope_;
1500 FillJniCall jni_call_;
1501 void* bottom_of_used_area_;
1502
1503 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001504
1505 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1506};
1507
Andreas Gampec200a4a2014-06-16 18:39:09 -07001508uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1509 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001510 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001511 h.Assign(ref);
1512 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1513 cur_entry_++;
1514 return tmp;
1515}
1516
Ian Rogers9758f792014-03-13 09:02:55 -07001517void BuildGenericJniFrameVisitor::Visit() {
1518 Primitive::Type type = GetParamPrimitiveType();
1519 switch (type) {
1520 case Primitive::kPrimLong: {
1521 jlong long_arg;
1522 if (IsSplitLongOrDouble()) {
1523 long_arg = ReadSplitLongParam();
1524 } else {
1525 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1526 }
1527 sm_.AdvanceLong(long_arg);
1528 break;
1529 }
1530 case Primitive::kPrimDouble: {
1531 uint64_t double_arg;
1532 if (IsSplitLongOrDouble()) {
1533 // Read into union so that we don't case to a double.
1534 double_arg = ReadSplitLongParam();
1535 } else {
1536 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1537 }
1538 sm_.AdvanceDouble(double_arg);
1539 break;
1540 }
1541 case Primitive::kPrimNot: {
1542 StackReference<mirror::Object>* stack_ref =
1543 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001544 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001545 break;
1546 }
1547 case Primitive::kPrimFloat:
1548 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1549 break;
1550 case Primitive::kPrimBoolean: // Fall-through.
1551 case Primitive::kPrimByte: // Fall-through.
1552 case Primitive::kPrimChar: // Fall-through.
1553 case Primitive::kPrimShort: // Fall-through.
1554 case Primitive::kPrimInt: // Fall-through.
1555 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1556 break;
1557 case Primitive::kPrimVoid:
1558 LOG(FATAL) << "UNREACHABLE";
1559 break;
1560 }
1561}
1562
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001563void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001564 // Clear out rest of the scope.
1565 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001566 // Install HandleScope.
1567 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001568}
1569
Ian Rogers04c31d22014-07-07 21:44:06 -07001570#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001571extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001572#else
1573extern "C" void* artFindNativeMethod(Thread* self);
1574#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001575
Andreas Gampead615172014-04-04 16:20:13 -07001576uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1577 if (lock != nullptr) {
1578 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1579 } else {
1580 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1581 }
1582}
1583
1584void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1585 if (lock != nullptr) {
1586 JniMethodEndSynchronized(cookie, lock, self);
1587 } else {
1588 JniMethodEnd(cookie, self);
1589 }
1590}
1591
Andreas Gampec147b002014-03-06 18:11:06 -08001592/*
1593 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001594 * 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 -08001595 * The final element on the stack is a pointer to the native code.
1596 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001597 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001598 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001599 *
Andreas Gampec147b002014-03-06 18:11:06 -08001600 * The return of this function denotes:
1601 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1602 * 2) An error, if the value is negative.
1603 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001604extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1605 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001606 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001607 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001608 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001609 uint32_t shorty_len = 0;
1610 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001611
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001612 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001613 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001614 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001615 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001616
Andreas Gampec200a4a2014-06-16 18:39:09 -07001617 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001618 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001619
Ian Rogerse0dcd462014-03-08 15:21:04 -08001620 self->VerifyStack();
1621
Andreas Gampe90546832014-03-12 18:07:19 -07001622 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001623 uint32_t cookie;
1624 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001625 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001626 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001627 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001628 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001629 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001630 }
1631 } else {
1632 cookie = JniMethodStart(self);
1633 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001634 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001635 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001636
Andreas Gampe90546832014-03-12 18:07:19 -07001637 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001638 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001639
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001640 // There are two cases for the content of nativeCode:
1641 // 1) Pointer to the native function.
1642 // 2) Pointer to the trampoline for native code binding.
1643 // In the second case, we need to execute the binding and continue with the actual native function
1644 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001645 DCHECK(nativeCode != nullptr);
1646 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001647#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001648 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001649#else
1650 nativeCode = artFindNativeMethod(self);
1651#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001652
1653 if (nativeCode == nullptr) {
1654 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001655
1656 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001657 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001658 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001659 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1660 } else {
1661 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1662 }
1663
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001665 }
1666 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001667 }
1668
Andreas Gampec200a4a2014-06-16 18:39:09 -07001669 // Return native code addr(lo) and bottom of alloca address(hi).
1670 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1671 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001672}
1673
1674/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001675 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001676 * unlocking.
1677 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001678extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001679 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001680 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001681 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001682 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001683 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001684
Andreas Gampead615172014-04-04 16:20:13 -07001685 jobject lock = nullptr;
1686 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001687 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1688 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001689 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001690 }
1691
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001692 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001693
1694 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001695 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001696 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001697 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001698
1699 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001700 case 'F': {
1701 if (kRuntimeISA == kX86) {
1702 // Convert back the result to float.
1703 double d = bit_cast<uint64_t, double>(result_f);
1704 return bit_cast<float, uint32_t>(static_cast<float>(d));
1705 } else {
1706 return result_f;
1707 }
1708 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001709 case 'D':
1710 return result_f;
1711 case 'Z':
1712 return result.z;
1713 case 'B':
1714 return result.b;
1715 case 'C':
1716 return result.c;
1717 case 'S':
1718 return result.s;
1719 case 'I':
1720 return result.i;
1721 case 'J':
1722 return result.j;
1723 case 'V':
1724 return 0;
1725 default:
1726 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1727 return 0;
1728 }
1729 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001730}
1731
Andreas Gamped58342c2014-06-05 14:18:08 -07001732// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1733// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001734//
Andreas Gamped58342c2014-06-05 14:18:08 -07001735// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1736// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001737
1738template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001739static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001740 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001741 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001742
1743template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001744static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001745 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001746 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001747 ScopedQuickEntrypointChecks sqec(self);
1748 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001749 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1750 type);
1751 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001752 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1753 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001754 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001755 {
1756 // Remember the args in case a GC happens in FindMethodFromCode.
1757 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1758 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1759 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001760 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1761 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001762 visitor.FixupReferences();
1763 }
1764
1765 if (UNLIKELY(method == NULL)) {
1766 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001767 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001768 }
1769 }
1770 DCHECK(!self->IsExceptionPending());
1771 const void* code = method->GetEntryPointFromQuickCompiledCode();
1772
1773 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001774 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1775 << " location: "
1776 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001777
Andreas Gamped58342c2014-06-05 14:18:08 -07001778 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1779 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001780}
1781
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001782// Explicit artInvokeCommon template function declarations to please analysis tool.
1783#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1784 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001785 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001786 mirror::Object* this_object, \
1787 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001788 Thread* self, \
1789 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001790
1791EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1792EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1793EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1794EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1795EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1796EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1797EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1798EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1799EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1800EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1801#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1802
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001803// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001804extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1805 uint32_t method_idx, mirror::Object* this_object,
1806 mirror::ArtMethod* caller_method, Thread* self,
1807 StackReference<mirror::ArtMethod>* sp)
1808 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1809 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1810 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001811}
1812
Andreas Gampec200a4a2014-06-16 18:39:09 -07001813extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1814 uint32_t method_idx, mirror::Object* this_object,
1815 mirror::ArtMethod* caller_method, Thread* self,
1816 StackReference<mirror::ArtMethod>* sp)
1817 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1818 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1819 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001820}
1821
Andreas Gampec200a4a2014-06-16 18:39:09 -07001822extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1823 uint32_t method_idx, mirror::Object* this_object,
1824 mirror::ArtMethod* caller_method, Thread* self,
1825 StackReference<mirror::ArtMethod>* sp)
1826 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1827 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1828 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001829}
1830
Andreas Gampec200a4a2014-06-16 18:39:09 -07001831extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1832 uint32_t method_idx, mirror::Object* this_object,
1833 mirror::ArtMethod* caller_method, Thread* self,
1834 StackReference<mirror::ArtMethod>* sp)
1835 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1836 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1837 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001838}
1839
Andreas Gampec200a4a2014-06-16 18:39:09 -07001840extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1841 uint32_t method_idx, mirror::Object* this_object,
1842 mirror::ArtMethod* caller_method, Thread* self,
1843 StackReference<mirror::ArtMethod>* sp)
1844 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1845 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1846 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001847}
1848
1849// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001850extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001851 mirror::Object* this_object,
1852 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001853 Thread* self,
1854 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001855 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001856 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001857 mirror::ArtMethod* method;
1858 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1859 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1860 if (UNLIKELY(method == NULL)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001861 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1862 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001863 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001864 }
1865 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001866 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001867
1868 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001869 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07001870 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001871
1872 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001873 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001874 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001875 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1876 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1877 Instruction::Code instr_code = instr->Opcode();
1878 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1879 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1880 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1881 uint32_t dex_method_idx;
1882 if (instr_code == Instruction::INVOKE_INTERFACE) {
1883 dex_method_idx = instr->VRegB_35c();
1884 } else {
1885 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1886 dex_method_idx = instr->VRegB_3rc();
1887 }
1888
Andreas Gampec200a4a2014-06-16 18:39:09 -07001889 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1890 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001891 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001892 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1893 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001894 {
1895 // Remember the args in case a GC happens in FindMethodFromCode.
1896 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1897 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1898 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001899 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001900 self);
1901 visitor.FixupReferences();
1902 }
1903
1904 if (UNLIKELY(method == nullptr)) {
1905 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001906 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001907 }
1908 }
1909 const void* code = method->GetEntryPointFromQuickCompiledCode();
1910
1911 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001912 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1913 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001914
Andreas Gamped58342c2014-06-05 14:18:08 -07001915 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1916 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001917}
1918
Ian Rogers848871b2013-08-05 10:56:33 -07001919} // namespace art