blob: cd34d8880dcf8885120fd87ef7b0c5d449fa8862 [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"
Ian Rogers848871b2013-08-05 10:56:33 -070024#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080025#include "method_reference.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 |
Zheng Xu5667fdb2014-10-23 18:29:55 +080053 // | ... | 4x6 bytes callee saves
54 // | R3 |
55 // | R2 |
56 // | R1 |
57 // | S15 |
58 // | : |
59 // | S0 |
60 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070061 // | Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000062 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080063 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
64 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
65 static constexpr size_t kNumQuickGprArgs = 3;
66 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080067 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080068 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
69 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
70 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
71 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
72 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
73 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080074 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000075 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080076 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000077#elif defined(__aarch64__)
78 // The callee save frame is pointed to by SP.
79 // | argN | |
80 // | ... | |
81 // | arg4 | |
82 // | arg3 spill | | Caller's frame
83 // | arg2 spill | |
84 // | arg1 spill | |
85 // | Method* | ---
86 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080087 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000088 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080089 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000090 // | X7 |
91 // | : |
92 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080093 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000094 // | : |
95 // | D0 |
96 // | | padding
97 // | Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000098 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +000099 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800100 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
102 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800103 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800104 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
105 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
106 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
107 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
108 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
109 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000111 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800113#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700114 // The callee save frame is pointed to by SP.
115 // | argN | |
116 // | ... | |
117 // | arg4 | |
118 // | arg3 spill | | Caller's frame
119 // | arg2 spill | |
120 // | arg1 spill | |
121 // | Method* | ---
122 // | RA |
123 // | ... | callee saves
124 // | A3 | arg3
125 // | A2 | arg2
126 // | A1 | arg1
127 // | A0/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000128 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800129 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800130 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800131 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
132 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800133 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800134 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
135 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
136 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800137 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000138 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800140#elif defined(__mips__) && defined(__LP64__)
141 // The callee save frame is pointed to by SP.
142 // | argN | |
143 // | ... | |
144 // | arg4 | |
145 // | arg3 spill | | Caller's frame
146 // | arg2 spill | |
147 // | arg1 spill | |
148 // | Method* | ---
149 // | RA |
150 // | ... | callee saves
151 // | F7 | f_arg7
152 // | F6 | f_arg6
153 // | F5 | f_arg5
154 // | F6 | f_arg6
155 // | F5 | f_arg5
156 // | F4 | f_arg4
157 // | F3 | f_arg3
158 // | F2 | f_arg2
159 // | F1 | f_arg1
160 // | F0 | f_arg0
161 // | A7 | arg7
162 // | A6 | arg6
163 // | A5 | arg5
164 // | A4 | arg4
165 // | A3 | arg3
166 // | A2 | arg2
167 // | A1 | arg1
168 // | | padding
169 // | A0/Method* | <- sp
170 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
171 static constexpr bool kAlignPairRegister = false;
172 static constexpr bool kQuickSoftFloatAbi = false;
173 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
174 // These values are set to zeros because GPR and FPR register
175 // assignments for Mips64 are interleaved, which the current VisitArguments()
176 // function does not support.
177 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
178 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
179 static constexpr bool kGprFprLockstep = true;
180
181 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
182 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
184 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
185 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
186 }
Ian Rogers848871b2013-08-05 10:56:33 -0700187#elif defined(__i386__)
188 // The callee save frame is pointed to by SP.
189 // | argN | |
190 // | ... | |
191 // | arg4 | |
192 // | arg3 spill | | Caller's frame
193 // | arg2 spill | |
194 // | arg1 spill | |
195 // | Method* | ---
196 // | Return |
197 // | EBP,ESI,EDI | callee saves
198 // | EBX | arg3
199 // | EDX | arg2
200 // | ECX | arg1
201 // | EAX/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000202 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800203 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800204 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800205 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
206 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800207 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800208 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
209 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
210 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800211 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000212 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800213 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800215 // The callee save frame is pointed to by SP.
216 // | argN | |
217 // | ... | |
218 // | reg. arg spills | | Caller's frame
219 // | Method* | ---
220 // | Return |
221 // | R15 | callee save
222 // | R14 | callee save
223 // | R13 | callee save
224 // | R12 | callee save
225 // | R9 | arg5
226 // | R8 | arg4
227 // | RSI/R6 | arg1
228 // | RBP/R5 | callee save
229 // | RBX/R3 | callee save
230 // | RDX/R2 | arg2
231 // | RCX/R1 | arg3
232 // | XMM7 | float arg 8
233 // | XMM6 | float arg 7
234 // | XMM5 | float arg 6
235 // | XMM4 | float arg 5
236 // | XMM3 | float arg 4
237 // | XMM2 | float arg 3
238 // | XMM1 | float arg 2
239 // | XMM0 | float arg 1
240 // | Padding |
241 // | RDI/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000242 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800243 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800244 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700245 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700246 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800247 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700249 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
250 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800251 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
252 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000253 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
254 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
255 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
256 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
257 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700259 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
260 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800261 }
262 }
Ian Rogers848871b2013-08-05 10:56:33 -0700263#else
264#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700265#endif
266
Ian Rogers936b37f2014-02-14 00:52:24 -0800267 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100268 // Special handling for proxy methods. Proxy methods are instance methods so the
269 // 'this' object is the 1st argument. They also have the same frame layout as the
270 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
271 // 1st GPR.
272 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
273 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
274 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
275 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
276 CHECK_GT(kNumQuickGprArgs, 0u);
277 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
278 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
279 GprIndexToGprOffset(kThisGprIndex);
280 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
281 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
282 }
283
Andreas Gampecf4035a2014-05-28 22:43:01 -0700284 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700286 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700287 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700288 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700289 }
290
Ian Rogers936b37f2014-02-14 00:52:24 -0800291 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700292 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700294 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700295 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700296 return *reinterpret_cast<uintptr_t*>(lr);
297 }
298
Andreas Gampec200a4a2014-06-16 18:39:09 -0700299 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
300 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
301 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700302 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
303 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
304 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700305 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800306 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
307 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800308 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
309 "Number of Quick FPR arguments unexpected");
310 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
311 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800312 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
313 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800314 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
315 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800316 }
Ian Rogers848871b2013-08-05 10:56:33 -0700317
318 virtual ~QuickArgumentVisitor() {}
319
320 virtual void Visit() = 0;
321
Ian Rogers936b37f2014-02-14 00:52:24 -0800322 Primitive::Type GetParamPrimitiveType() const {
323 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700324 }
325
Ian Rogers13735952014-10-08 12:43:28 -0700326 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800327 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800328 Primitive::Type type = GetParamPrimitiveType();
329 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800330 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
331 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
332 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
333 }
334 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000335 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800336 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700337 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800338 }
339 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800340 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800341 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
342 }
343 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700344 }
345
346 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000347 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800348 return is_split_long_or_double_;
349 } else {
350 return false; // An optimization for when GPR and FPRs are 64bit.
351 }
Ian Rogers848871b2013-08-05 10:56:33 -0700352 }
353
Ian Rogers936b37f2014-02-14 00:52:24 -0800354 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700355 return GetParamPrimitiveType() == Primitive::kPrimNot;
356 }
357
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700359 Primitive::Type type = GetParamPrimitiveType();
360 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
361 }
362
363 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000364 // The splitted long is always available through the stack.
365 return *reinterpret_cast<uint64_t*>(stack_args_
366 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700367 }
368
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800369 void IncGprIndex() {
370 gpr_index_++;
371 if (kGprFprLockstep) {
372 fpr_index_++;
373 }
374 }
375
376 void IncFprIndex() {
377 fpr_index_++;
378 if (kGprFprLockstep) {
379 gpr_index_++;
380 }
381 }
382
Ian Rogers848871b2013-08-05 10:56:33 -0700383 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800384 // (a) 'stack_args_' should point to the first method's argument
385 // (b) whatever the argument type it is, the 'stack_index_' should
386 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 gpr_index_ = 0;
388 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800389 if (kQuickDoubleRegAlignedFloatBackFilled) {
390 fpr_double_index_ = 0;
391 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800392 stack_index_ = 0;
393 if (!is_static_) { // Handle this.
394 cur_type_ = Primitive::kPrimNot;
395 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700396 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800397 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800398 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800399 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 }
Ian Rogers848871b2013-08-05 10:56:33 -0700401 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800402 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
403 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
404 switch (cur_type_) {
405 case Primitive::kPrimNot:
406 case Primitive::kPrimBoolean:
407 case Primitive::kPrimByte:
408 case Primitive::kPrimChar:
409 case Primitive::kPrimShort:
410 case Primitive::kPrimInt:
411 is_split_long_or_double_ = false;
412 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800413 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800414 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800415 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800416 }
417 break;
418 case Primitive::kPrimFloat:
419 is_split_long_or_double_ = false;
420 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800422 if (kQuickSoftFloatAbi) {
423 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800424 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 }
426 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800427 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800428 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800429 if (kQuickDoubleRegAlignedFloatBackFilled) {
430 // Double should not overlap with float.
431 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
432 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
433 // Float should not overlap with double.
434 if (fpr_index_ % 2 == 0) {
435 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
436 }
437 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800438 }
439 }
440 break;
441 case Primitive::kPrimDouble:
442 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800443 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000444 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
445 // Currently, this is only for ARM, where the first available parameter register
446 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800447 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000448 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000449 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800450 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800451 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800452 if (kBytesStackArgLocation == 4) {
453 stack_index_+= 2;
454 } else {
455 CHECK_EQ(kBytesStackArgLocation, 8U);
456 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800457 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700458 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800459 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000460 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700461 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800462 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700463 }
464 }
465 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800466 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000467 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800468 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800469 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700470 if (kBytesStackArgLocation == 4) {
471 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800472 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700473 CHECK_EQ(kBytesStackArgLocation, 8U);
474 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800475 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800476 if (kQuickDoubleRegAlignedFloatBackFilled) {
477 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
478 fpr_double_index_ += 2;
479 // Float should not overlap with double.
480 if (fpr_index_ % 2 == 0) {
481 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
482 }
483 }
484 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800485 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800486 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
487 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800488 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 }
490 }
491 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 }
493 break;
494 default:
495 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
496 }
Ian Rogers848871b2013-08-05 10:56:33 -0700497 }
498 }
499
Andreas Gampec200a4a2014-06-16 18:39:09 -0700500 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700501 const bool is_static_;
502 const char* const shorty_;
503 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700504
505 private:
Ian Rogers13735952014-10-08 12:43:28 -0700506 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
507 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
508 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800509 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800510 // Index into spilled FPRs.
511 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
512 // holds a higher register number.
513 uint32_t fpr_index_;
514 // Index into spilled FPRs for aligned double.
515 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
516 // terms of singles, may be behind fpr_index.
517 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800518 uint32_t stack_index_; // Index into arguments on the stack.
519 // The current type of argument during VisitArguments.
520 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700521 // Does a 64bit parameter straddle the register and stack arguments?
522 bool is_split_long_or_double_;
523};
524
Sebastien Hertza836bc92014-11-25 16:30:53 +0100525// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
526// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
527extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
528 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
529 return QuickArgumentVisitor::GetProxyThisObject(sp);
530}
531
Ian Rogers848871b2013-08-05 10:56:33 -0700532// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800533class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700534 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700535 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
536 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
537 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700538 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700539
Ian Rogers9758f792014-03-13 09:02:55 -0700540 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700541
542 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800543 ShadowFrame* const sf_;
544 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700545
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700546 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700547};
548
Andreas Gampec200a4a2014-06-16 18:39:09 -0700549void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700550 Primitive::Type type = GetParamPrimitiveType();
551 switch (type) {
552 case Primitive::kPrimLong: // Fall-through.
553 case Primitive::kPrimDouble:
554 if (IsSplitLongOrDouble()) {
555 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
556 } else {
557 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
558 }
559 ++cur_reg_;
560 break;
561 case Primitive::kPrimNot: {
562 StackReference<mirror::Object>* stack_ref =
563 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
564 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
565 }
566 break;
567 case Primitive::kPrimBoolean: // Fall-through.
568 case Primitive::kPrimByte: // Fall-through.
569 case Primitive::kPrimChar: // Fall-through.
570 case Primitive::kPrimShort: // Fall-through.
571 case Primitive::kPrimInt: // Fall-through.
572 case Primitive::kPrimFloat:
573 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
574 break;
575 case Primitive::kPrimVoid:
576 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700577 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700578 }
579 ++cur_reg_;
580}
581
Brian Carlstromea46f952013-07-30 01:26:50 -0700582extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700583 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
585 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
586 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700587 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700588
589 if (method->IsAbstract()) {
590 ThrowAbstractMethodError(method);
591 return 0;
592 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800593 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700594 const char* old_cause = self->StartAssertNoThreadSuspension(
595 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700596 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800597 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700598 uint16_t num_regs = code_item->registers_size_;
599 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700600 // No last shadow coming from quick.
601 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700602 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 uint32_t shorty_len = 0;
604 const char* shorty = method->GetShorty(&shorty_len);
605 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800606 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700607 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800608 const bool needs_initialization =
609 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700610 // Push a transition back into managed code onto the linked list in thread.
611 ManagedStack fragment;
612 self->PushManagedStackFragment(&fragment);
613 self->PushShadowFrame(shadow_frame);
614 self->EndAssertNoThreadSuspension(old_cause);
615
Ian Rogerse94652f2014-12-02 11:13:19 -0800616 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700617 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800618 StackHandleScope<1> hs(self);
619 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700620 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800621 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700622 self->PopManagedStackFragment(fragment);
623 return 0;
624 }
625 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800626 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700627 // Pop transition.
628 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800629 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700630 return result.GetJ();
631 }
632}
633
634// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
635// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800636class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700637 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700638 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
639 const char* shorty, uint32_t shorty_len,
640 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700641 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700642
Ian Rogers9758f792014-03-13 09:02:55 -0700643 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700644
Ian Rogers9758f792014-03-13 09:02:55 -0700645 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800646
Ian Rogers848871b2013-08-05 10:56:33 -0700647 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700648 ScopedObjectAccessUnchecked* const soa_;
649 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800650 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700651 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700652
Ian Rogers848871b2013-08-05 10:56:33 -0700653 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
654};
655
Ian Rogers9758f792014-03-13 09:02:55 -0700656void BuildQuickArgumentVisitor::Visit() {
657 jvalue val;
658 Primitive::Type type = GetParamPrimitiveType();
659 switch (type) {
660 case Primitive::kPrimNot: {
661 StackReference<mirror::Object>* stack_ref =
662 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
663 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
664 references_.push_back(std::make_pair(val.l, stack_ref));
665 break;
666 }
667 case Primitive::kPrimLong: // Fall-through.
668 case Primitive::kPrimDouble:
669 if (IsSplitLongOrDouble()) {
670 val.j = ReadSplitLongParam();
671 } else {
672 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
673 }
674 break;
675 case Primitive::kPrimBoolean: // Fall-through.
676 case Primitive::kPrimByte: // Fall-through.
677 case Primitive::kPrimChar: // Fall-through.
678 case Primitive::kPrimShort: // Fall-through.
679 case Primitive::kPrimInt: // Fall-through.
680 case Primitive::kPrimFloat:
681 val.i = *reinterpret_cast<jint*>(GetParamAddress());
682 break;
683 case Primitive::kPrimVoid:
684 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700685 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700686 }
687 args_->push_back(val);
688}
689
690void BuildQuickArgumentVisitor::FixupReferences() {
691 // Fixup any references which may have changed.
692 for (const auto& pair : references_) {
693 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700694 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700695 }
696}
697
Ian Rogers848871b2013-08-05 10:56:33 -0700698// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
699// which is responsible for recording callee save registers. We explicitly place into jobjects the
700// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
701// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700702extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700703 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700704 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700705 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700706 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
707 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700708 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
709 const char* old_cause =
710 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
711 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700712 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700713 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700714 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
715 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700716 self->VerifyStack();
717 // Start new JNI local reference state.
718 JNIEnvExt* env = self->GetJniEnv();
719 ScopedObjectAccessUnchecked soa(env);
720 ScopedJniEnvLocalRefState env_state(env);
721 // Create local ref. copies of proxy method and the receiver.
722 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
723
724 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700725 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
726 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700727 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700728 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700729 uint32_t shorty_len = 0;
730 const char* shorty = proxy_method->GetShorty(&shorty_len);
731 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700732
Ian Rogers848871b2013-08-05 10:56:33 -0700733 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700734 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700735 args.erase(args.begin());
736
737 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700738 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800739 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700740 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
741 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
742
743 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
744 // that performs allocations.
745 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700746 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800747 // Restore references which might have moved.
748 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700749 return result.GetJ();
750}
751
752// Read object references held in arguments from quick frames and place in a JNI local references,
753// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800754class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700755 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700756 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
757 const char* shorty, uint32_t shorty_len,
758 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700759 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700760
Ian Rogers9758f792014-03-13 09:02:55 -0700761 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700762
Ian Rogers9758f792014-03-13 09:02:55 -0700763 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700764
765 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700766 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800767 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700768 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
769
Mathieu Chartier590fee92013-09-13 13:46:47 -0700770 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700771};
772
Ian Rogers9758f792014-03-13 09:02:55 -0700773void RememberForGcArgumentVisitor::Visit() {
774 if (IsParamAReference()) {
775 StackReference<mirror::Object>* stack_ref =
776 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
777 jobject reference =
778 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
779 references_.push_back(std::make_pair(reference, stack_ref));
780 }
781}
782
783void RememberForGcArgumentVisitor::FixupReferences() {
784 // Fixup any references which may have changed.
785 for (const auto& pair : references_) {
786 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700787 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700788 }
789}
790
Ian Rogers848871b2013-08-05 10:56:33 -0700791// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700792extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700793 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700794 Thread* self,
795 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700796 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700797 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700798 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800799 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700800 ScopedObjectAccessUnchecked soa(env);
801 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800802 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700803
804 // Compute details about the called method (avoid GCs)
805 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700806 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700807 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800808 MethodReference called_method(nullptr, 0);
809 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
810 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700811 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
812 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800813 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700814 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700815 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
816 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
817 Instruction::Code instr_code = instr->Opcode();
818 bool is_range;
819 switch (instr_code) {
820 case Instruction::INVOKE_DIRECT:
821 invoke_type = kDirect;
822 is_range = false;
823 break;
824 case Instruction::INVOKE_DIRECT_RANGE:
825 invoke_type = kDirect;
826 is_range = true;
827 break;
828 case Instruction::INVOKE_STATIC:
829 invoke_type = kStatic;
830 is_range = false;
831 break;
832 case Instruction::INVOKE_STATIC_RANGE:
833 invoke_type = kStatic;
834 is_range = true;
835 break;
836 case Instruction::INVOKE_SUPER:
837 invoke_type = kSuper;
838 is_range = false;
839 break;
840 case Instruction::INVOKE_SUPER_RANGE:
841 invoke_type = kSuper;
842 is_range = true;
843 break;
844 case Instruction::INVOKE_VIRTUAL:
845 invoke_type = kVirtual;
846 is_range = false;
847 break;
848 case Instruction::INVOKE_VIRTUAL_RANGE:
849 invoke_type = kVirtual;
850 is_range = true;
851 break;
852 case Instruction::INVOKE_INTERFACE:
853 invoke_type = kInterface;
854 is_range = false;
855 break;
856 case Instruction::INVOKE_INTERFACE_RANGE:
857 invoke_type = kInterface;
858 is_range = true;
859 break;
860 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800861 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
862 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700863 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800864 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700865 } else {
866 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800867 called_method.dex_file = called->GetDexFile();
868 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700869 }
870 uint32_t shorty_len;
871 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800872 called_method.dex_file->GetMethodShorty(
873 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700874 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700875 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800876 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800877 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700878 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800879 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700880 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700881 mirror::Object* dummy = nullptr;
882 HandleWrapper<mirror::Object> h_receiver(
883 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800884 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
885 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700886 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800887 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800888 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700889 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800890 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
891 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800892 if (virtual_or_interface) {
893 // Refine called method based on receiver.
894 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700895
896 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800897 if (invoke_type == kVirtual) {
898 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
899 } else {
900 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
901 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700902
903 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
904 << PrettyTypeOf(receiver) << " "
905 << invoke_type << " " << orig_called->GetVtableIndex();
906
Ian Rogers83883d72013-10-21 21:07:24 -0700907 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
Ian Rogerse0a02da2014-12-02 14:10:53 -0800908 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800909 // Note, called_method.dex_method_index references the dex method before the
910 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
911 // about the name and signature.
912 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800913 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700914 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000915 // the caller's dex file. Since we get here only if the original called was a runtime
916 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800917 DCHECK(!called_method_known_on_entry);
918 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
919 const DexFile* caller_dex_file = called_method.dex_file;
920 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
921 update_dex_cache_method_index =
922 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
923 caller_method_name_and_sig_index);
924 }
925 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
926 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
927 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700928 }
929 }
Ian Rogers848871b2013-08-05 10:56:33 -0700930 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700931 StackHandleScope<1> hs(soa.Self());
932 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700933 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700934 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800935 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700936 } else if (called_class->IsInitializing()) {
937 if (invoke_type == kStatic) {
938 // Class is still initializing, go to oat and grab code (trampoline must be left in place
939 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800940 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700941 } else {
942 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800943 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700944 }
945 } else {
946 DCHECK(called_class->IsErroneous());
947 }
948 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800949 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700950 // Fixup any locally saved objects may have moved during a GC.
951 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700952 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700953 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700954 return code;
955}
956
Andreas Gampec147b002014-03-06 18:11:06 -0800957/*
958 * This class uses a couple of observations to unite the different calling conventions through
959 * a few constants.
960 *
961 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
962 * possible alignment.
963 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
964 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
965 * when we have to split things
966 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
967 * and we can use Int handling directly.
968 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
969 * necessary when widening. Also, widening of Ints will take place implicitly, and the
970 * extension should be compatible with Aarch64, which mandates copying the available bits
971 * into LSB and leaving the rest unspecified.
972 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
973 * the stack.
974 * 6) There is only little endian.
975 *
976 *
977 * Actual work is supposed to be done in a delegate of the template type. The interface is as
978 * follows:
979 *
980 * void PushGpr(uintptr_t): Add a value for the next GPR
981 *
982 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
983 * padding, that is, think the architecture is 32b and aligns 64b.
984 *
985 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
986 * split this if necessary. The current state will have aligned, if
987 * necessary.
988 *
989 * void PushStack(uintptr_t): Push a value to the stack.
990 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700991 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700992 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800993 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700994 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800995 *
996 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700997template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800998 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800999#if defined(__arm__)
1000 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001001 static constexpr bool kNativeSoftFloatAbi = true;
1002 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001003 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1004
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001005 static constexpr size_t kRegistersNeededForLong = 2;
1006 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001007 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001008 static constexpr bool kMultiFPRegistersWidened = false;
1009 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001010 static constexpr bool kAlignLongOnStack = true;
1011 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001012#elif defined(__aarch64__)
1013 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1014 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1015 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1016
1017 static constexpr size_t kRegistersNeededForLong = 1;
1018 static constexpr size_t kRegistersNeededForDouble = 1;
1019 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001020 static constexpr bool kMultiFPRegistersWidened = false;
1021 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001022 static constexpr bool kAlignLongOnStack = false;
1023 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001024#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001025 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001026 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1027 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001028
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001029 static constexpr size_t kRegistersNeededForLong = 2;
1030 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001031 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001032 static constexpr bool kMultiFPRegistersWidened = true;
1033 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001034 static constexpr bool kAlignLongOnStack = true;
1035 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001036#elif defined(__mips__) && defined(__LP64__)
1037 // Let the code prepare GPRs only and we will load the FPRs with same data.
1038 static constexpr bool kNativeSoftFloatAbi = true;
1039 static constexpr size_t kNumNativeGprArgs = 8;
1040 static constexpr size_t kNumNativeFprArgs = 0;
1041
1042 static constexpr size_t kRegistersNeededForLong = 1;
1043 static constexpr size_t kRegistersNeededForDouble = 1;
1044 static constexpr bool kMultiRegistersAligned = false;
1045 static constexpr bool kMultiFPRegistersWidened = false;
1046 static constexpr bool kMultiGPRegistersWidened = true;
1047 static constexpr bool kAlignLongOnStack = false;
1048 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001049#elif defined(__i386__)
1050 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001051 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001052 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1053 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1054
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001055 static constexpr size_t kRegistersNeededForLong = 2;
1056 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001057 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001058 static constexpr bool kMultiFPRegistersWidened = false;
1059 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001060 static constexpr bool kAlignLongOnStack = false;
1061 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001062#elif defined(__x86_64__)
1063 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1064 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1065 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1066
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001067 static constexpr size_t kRegistersNeededForLong = 1;
1068 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001069 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001070 static constexpr bool kMultiFPRegistersWidened = false;
1071 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001072 static constexpr bool kAlignLongOnStack = false;
1073 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001074#else
1075#error "Unsupported architecture"
1076#endif
1077
Andreas Gampec147b002014-03-06 18:11:06 -08001078 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001079 explicit BuildNativeCallFrameStateMachine(T* delegate)
1080 : gpr_index_(kNumNativeGprArgs),
1081 fpr_index_(kNumNativeFprArgs),
1082 stack_entries_(0),
1083 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001084 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1085 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001086 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1087 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001088 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001089
Andreas Gampec200a4a2014-06-16 18:39:09 -07001090 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001091
Ian Rogers1428dce2014-10-21 15:02:15 -07001092 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001093 return gpr_index_ > 0;
1094 }
1095
Andreas Gampec200a4a2014-06-16 18:39:09 -07001096 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001097 if (HavePointerGpr()) {
1098 gpr_index_--;
1099 PushGpr(reinterpret_cast<uintptr_t>(val));
1100 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001101 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001102 PushStack(reinterpret_cast<uintptr_t>(val));
1103 gpr_index_ = 0;
1104 }
1105 }
1106
Ian Rogers1428dce2014-10-21 15:02:15 -07001107 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001108 return gpr_index_ > 0;
1109 }
1110
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001111 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1112 uintptr_t handle = PushHandle(ptr);
1113 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001114 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001115 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001116 } else {
1117 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001118 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001119 gpr_index_ = 0;
1120 }
1121 }
1122
Ian Rogers1428dce2014-10-21 15:02:15 -07001123 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001124 return gpr_index_ > 0;
1125 }
1126
1127 void AdvanceInt(uint32_t val) {
1128 if (HaveIntGpr()) {
1129 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001130 if (kMultiGPRegistersWidened) {
1131 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1132 PushGpr(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1133 } else {
1134 PushGpr(val);
1135 }
Andreas Gampec147b002014-03-06 18:11:06 -08001136 } else {
1137 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001138 if (kMultiGPRegistersWidened) {
1139 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1140 PushStack(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1141 } else {
1142 PushStack(val);
1143 }
Andreas Gampec147b002014-03-06 18:11:06 -08001144 gpr_index_ = 0;
1145 }
1146 }
1147
Ian Rogers1428dce2014-10-21 15:02:15 -07001148 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001149 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1150 }
1151
Ian Rogers1428dce2014-10-21 15:02:15 -07001152 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001153 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1154 kAlignLongOnStack && // and when it needs alignment
1155 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1156 }
1157
Ian Rogers1428dce2014-10-21 15:02:15 -07001158 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001159 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1160 kAlignLongOnStack && // and when it needs 8B alignment
1161 (stack_entries_ & 1) == 1; // counter is odd
1162 }
1163
1164 void AdvanceLong(uint64_t val) {
1165 if (HaveLongGpr()) {
1166 if (LongGprNeedsPadding()) {
1167 PushGpr(0);
1168 gpr_index_--;
1169 }
1170 if (kRegistersNeededForLong == 1) {
1171 PushGpr(static_cast<uintptr_t>(val));
1172 } else {
1173 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1174 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1175 }
1176 gpr_index_ -= kRegistersNeededForLong;
1177 } else {
1178 if (LongStackNeedsPadding()) {
1179 PushStack(0);
1180 stack_entries_++;
1181 }
1182 if (kRegistersNeededForLong == 1) {
1183 PushStack(static_cast<uintptr_t>(val));
1184 stack_entries_++;
1185 } else {
1186 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1187 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1188 stack_entries_ += 2;
1189 }
1190 gpr_index_ = 0;
1191 }
1192 }
1193
Ian Rogers1428dce2014-10-21 15:02:15 -07001194 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001195 return fpr_index_ > 0;
1196 }
1197
Andreas Gampec147b002014-03-06 18:11:06 -08001198 void AdvanceFloat(float val) {
1199 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001200 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001201 } else {
1202 if (HaveFloatFpr()) {
1203 fpr_index_--;
1204 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001205 if (kMultiFPRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001206 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001207 } else {
1208 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001209 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001210 }
1211 } else {
1212 PushFpr4(val);
1213 }
1214 } else {
1215 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001216 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001217 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001218 // Note: We need to jump through those hoops to make the compiler happy.
1219 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1220 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001221 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001222 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001223 }
1224 fpr_index_ = 0;
1225 }
1226 }
1227 }
1228
Ian Rogers1428dce2014-10-21 15:02:15 -07001229 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001230 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1231 }
1232
Ian Rogers1428dce2014-10-21 15:02:15 -07001233 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001234 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1235 kAlignDoubleOnStack && // and when it needs alignment
1236 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1237 }
1238
Ian Rogers1428dce2014-10-21 15:02:15 -07001239 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001240 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1241 kAlignDoubleOnStack && // and when it needs 8B alignment
1242 (stack_entries_ & 1) == 1; // counter is odd
1243 }
1244
1245 void AdvanceDouble(uint64_t val) {
1246 if (kNativeSoftFloatAbi) {
1247 AdvanceLong(val);
1248 } else {
1249 if (HaveDoubleFpr()) {
1250 if (DoubleFprNeedsPadding()) {
1251 PushFpr4(0);
1252 fpr_index_--;
1253 }
1254 PushFpr8(val);
1255 fpr_index_ -= kRegistersNeededForDouble;
1256 } else {
1257 if (DoubleStackNeedsPadding()) {
1258 PushStack(0);
1259 stack_entries_++;
1260 }
1261 if (kRegistersNeededForDouble == 1) {
1262 PushStack(static_cast<uintptr_t>(val));
1263 stack_entries_++;
1264 } else {
1265 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1266 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1267 stack_entries_ += 2;
1268 }
1269 fpr_index_ = 0;
1270 }
1271 }
1272 }
1273
Ian Rogers1428dce2014-10-21 15:02:15 -07001274 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001275 return stack_entries_;
1276 }
1277
Ian Rogers1428dce2014-10-21 15:02:15 -07001278 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001279 return kNumNativeGprArgs - gpr_index_;
1280 }
1281
Ian Rogers1428dce2014-10-21 15:02:15 -07001282 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001283 return kNumNativeFprArgs - fpr_index_;
1284 }
1285
1286 private:
1287 void PushGpr(uintptr_t val) {
1288 delegate_->PushGpr(val);
1289 }
1290 void PushFpr4(float val) {
1291 delegate_->PushFpr4(val);
1292 }
1293 void PushFpr8(uint64_t val) {
1294 delegate_->PushFpr8(val);
1295 }
1296 void PushStack(uintptr_t val) {
1297 delegate_->PushStack(val);
1298 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001299 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1300 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001301 }
1302
1303 uint32_t gpr_index_; // Number of free GPRs
1304 uint32_t fpr_index_; // Number of free FPRs
1305 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1306 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001307 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001308};
1309
Andreas Gampec200a4a2014-06-16 18:39:09 -07001310// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1311// in subclasses.
1312//
1313// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1314// them with handles.
1315class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001316 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001317 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1318
1319 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001320
Ian Rogers1428dce2014-10-21 15:02:15 -07001321 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001322 return num_stack_entries_ * sizeof(uintptr_t);
1323 }
1324
Ian Rogers1428dce2014-10-21 15:02:15 -07001325 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001326 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001327 // Align by kStackAlignment.
1328 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001329 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001330 }
1331
Ian Rogers1428dce2014-10-21 15:02:15 -07001332 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1333 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001334 // Assumption is OK right now, as we have soft-float arm
1335 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1336 sp8 -= fregs * sizeof(uintptr_t);
1337 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1338 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1339 sp8 -= iregs * sizeof(uintptr_t);
1340 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1341 return sp8;
1342 }
Andreas Gampec147b002014-03-06 18:11:06 -08001343
Andreas Gampec200a4a2014-06-16 18:39:09 -07001344 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001345 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001346 // Native call stack.
1347 sp8 = LayoutCallStack(sp8);
1348 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001349
Andreas Gampec200a4a2014-06-16 18:39:09 -07001350 // Put fprs and gprs below.
1351 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001352
Andreas Gampec200a4a2014-06-16 18:39:09 -07001353 // Return the new bottom.
1354 return sp8;
1355 }
1356
1357 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1359 UNUSED(sm);
1360 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001361
1362 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1363 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1364
1365 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001366
1367 for (uint32_t i = 1; i < shorty_len; ++i) {
1368 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1369 switch (cur_type_) {
1370 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001371 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001372 sm.AdvanceHandleScope(
1373 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001374 break;
1375
1376 case Primitive::kPrimBoolean:
1377 case Primitive::kPrimByte:
1378 case Primitive::kPrimChar:
1379 case Primitive::kPrimShort:
1380 case Primitive::kPrimInt:
1381 sm.AdvanceInt(0);
1382 break;
1383 case Primitive::kPrimFloat:
1384 sm.AdvanceFloat(0);
1385 break;
1386 case Primitive::kPrimDouble:
1387 sm.AdvanceDouble(0);
1388 break;
1389 case Primitive::kPrimLong:
1390 sm.AdvanceLong(0);
1391 break;
1392 default:
1393 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001394 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001395 }
1396 }
1397
Ian Rogers1428dce2014-10-21 15:02:15 -07001398 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001399 }
1400
1401 void PushGpr(uintptr_t /* val */) {
1402 // not optimizing registers, yet
1403 }
1404
1405 void PushFpr4(float /* val */) {
1406 // not optimizing registers, yet
1407 }
1408
1409 void PushFpr8(uint64_t /* val */) {
1410 // not optimizing registers, yet
1411 }
1412
1413 void PushStack(uintptr_t /* val */) {
1414 // counting is already done in the superclass
1415 }
1416
Andreas Gampec200a4a2014-06-16 18:39:09 -07001417 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001418 return reinterpret_cast<uintptr_t>(nullptr);
1419 }
1420
Andreas Gampec200a4a2014-06-16 18:39:09 -07001421 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001422 uint32_t num_stack_entries_;
1423};
1424
Andreas Gampec200a4a2014-06-16 18:39:09 -07001425class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001426 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001427 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001428
Andreas Gampec200a4a2014-06-16 18:39:09 -07001429 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1430 // is at *m = sp. Will update to point to the bottom of the save frame.
1431 //
1432 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001433 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1434 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001435 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1436 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001437
Andreas Gampec200a4a2014-06-16 18:39:09 -07001438 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1439
1440 // First, fix up the layout of the callee-save frame.
1441 // We have to squeeze in the HandleScope, and relocate the method pointer.
1442
1443 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001444 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001445
1446 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001447 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1448 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1449
1450 sp8 -= scope_and_method;
1451 // Align by kStackAlignment.
1452 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1453 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1454
1455 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001456 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1457 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001458
1459 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1460 uint8_t* method_pointer = sp8;
1461 StackReference<mirror::ArtMethod>* new_method_ref =
1462 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1463 new_method_ref->Assign(method);
1464 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001465 }
1466
Andreas Gampec200a4a2014-06-16 18:39:09 -07001467 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001468 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001469 // Reference cookie and padding
1470 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001471 }
1472
Andreas Gampec200a4a2014-06-16 18:39:09 -07001473 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1474 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001475 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1476 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001477 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001478 // First, fix up the layout of the callee-save frame.
1479 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001480 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001481
1482 // The bottom of the callee-save frame is now where the method is, *m.
1483 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1484
1485 // Add space for cookie.
1486 LayoutCookie(&sp8);
1487
1488 return sp8;
1489 }
1490
1491 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001492 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001493 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001494 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1495 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1496 Walk(shorty, shorty_len);
1497
1498 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001499 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001500
1501 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1502
1503 // Return the new bottom.
1504 return sp8;
1505 }
1506
1507 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1508
1509 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1510 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1511 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1512
1513 private:
1514 uint32_t num_handle_scope_references_;
1515};
1516
1517uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1518 num_handle_scope_references_++;
1519 return reinterpret_cast<uintptr_t>(nullptr);
1520}
1521
1522void ComputeGenericJniFrameSize::WalkHeader(
1523 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1524 // JNIEnv
1525 sm->AdvancePointer(nullptr);
1526
1527 // Class object or this as first argument
1528 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1529}
1530
1531// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1532// the template requirements of BuildGenericJniFrameStateMachine.
1533class FillNativeCall {
1534 public:
1535 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1536 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1537
1538 virtual ~FillNativeCall() {}
1539
1540 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1541 cur_gpr_reg_ = gpr_regs;
1542 cur_fpr_reg_ = fpr_regs;
1543 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001544 }
1545
1546 void PushGpr(uintptr_t val) {
1547 *cur_gpr_reg_ = val;
1548 cur_gpr_reg_++;
1549 }
1550
1551 void PushFpr4(float val) {
1552 *cur_fpr_reg_ = val;
1553 cur_fpr_reg_++;
1554 }
1555
1556 void PushFpr8(uint64_t val) {
1557 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1558 *tmp = val;
1559 cur_fpr_reg_ += 2;
1560 }
1561
1562 void PushStack(uintptr_t val) {
1563 *cur_stack_arg_ = val;
1564 cur_stack_arg_++;
1565 }
1566
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001567 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001568 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001569 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001570 }
1571
1572 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001573 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001574 uint32_t* cur_fpr_reg_;
1575 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001576};
Andreas Gampec147b002014-03-06 18:11:06 -08001577
Andreas Gampec200a4a2014-06-16 18:39:09 -07001578// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1579// of transitioning into native code.
1580class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1581 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001582 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1583 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001584 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1585 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1586 ComputeGenericJniFrameSize fsc;
1587 uintptr_t* start_gpr_reg;
1588 uint32_t* start_fpr_reg;
1589 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001590 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001591 &handle_scope_,
1592 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001593 &start_gpr_reg, &start_fpr_reg);
1594
Andreas Gampec200a4a2014-06-16 18:39:09 -07001595 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1596
1597 // jni environment is always first argument
1598 sm_.AdvancePointer(self->GetJniEnv());
1599
1600 if (is_static) {
1601 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1602 }
1603 }
1604
1605 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1606
1607 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1608
1609 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1611 return handle_scope_->GetHandle(0).GetReference();
1612 }
1613
Ian Rogers1428dce2014-10-21 15:02:15 -07001614 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001615 return handle_scope_->GetHandle(0).ToJObject();
1616 }
1617
Ian Rogers1428dce2014-10-21 15:02:15 -07001618 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001619 return bottom_of_used_area_;
1620 }
1621
1622 private:
1623 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1624 class FillJniCall FINAL : public FillNativeCall {
1625 public:
1626 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1627 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1628 handle_scope_(handle_scope), cur_entry_(0) {}
1629
1630 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1631
1632 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1633 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1634 handle_scope_ = scope;
1635 cur_entry_ = 0U;
1636 }
1637
1638 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1639 // Initialize padding entries.
1640 size_t expected_slots = handle_scope_->NumberOfReferences();
1641 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001642 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001643 }
1644 DCHECK_NE(cur_entry_, 0U);
1645 }
1646
1647 private:
1648 HandleScope* handle_scope_;
1649 size_t cur_entry_;
1650 };
1651
1652 HandleScope* handle_scope_;
1653 FillJniCall jni_call_;
1654 void* bottom_of_used_area_;
1655
1656 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001657
1658 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1659};
1660
Andreas Gampec200a4a2014-06-16 18:39:09 -07001661uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1662 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001663 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001664 h.Assign(ref);
1665 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1666 cur_entry_++;
1667 return tmp;
1668}
1669
Ian Rogers9758f792014-03-13 09:02:55 -07001670void BuildGenericJniFrameVisitor::Visit() {
1671 Primitive::Type type = GetParamPrimitiveType();
1672 switch (type) {
1673 case Primitive::kPrimLong: {
1674 jlong long_arg;
1675 if (IsSplitLongOrDouble()) {
1676 long_arg = ReadSplitLongParam();
1677 } else {
1678 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1679 }
1680 sm_.AdvanceLong(long_arg);
1681 break;
1682 }
1683 case Primitive::kPrimDouble: {
1684 uint64_t double_arg;
1685 if (IsSplitLongOrDouble()) {
1686 // Read into union so that we don't case to a double.
1687 double_arg = ReadSplitLongParam();
1688 } else {
1689 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1690 }
1691 sm_.AdvanceDouble(double_arg);
1692 break;
1693 }
1694 case Primitive::kPrimNot: {
1695 StackReference<mirror::Object>* stack_ref =
1696 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001697 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001698 break;
1699 }
1700 case Primitive::kPrimFloat:
1701 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1702 break;
1703 case Primitive::kPrimBoolean: // Fall-through.
1704 case Primitive::kPrimByte: // Fall-through.
1705 case Primitive::kPrimChar: // Fall-through.
1706 case Primitive::kPrimShort: // Fall-through.
1707 case Primitive::kPrimInt: // Fall-through.
1708 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1709 break;
1710 case Primitive::kPrimVoid:
1711 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001712 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001713 }
1714}
1715
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001716void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001717 // Clear out rest of the scope.
1718 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001719 // Install HandleScope.
1720 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001721}
1722
Ian Rogers04c31d22014-07-07 21:44:06 -07001723#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001724extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001725#else
1726extern "C" void* artFindNativeMethod(Thread* self);
1727#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001728
Andreas Gampead615172014-04-04 16:20:13 -07001729uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1730 if (lock != nullptr) {
1731 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1732 } else {
1733 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1734 }
1735}
1736
1737void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1738 if (lock != nullptr) {
1739 JniMethodEndSynchronized(cookie, lock, self);
1740 } else {
1741 JniMethodEnd(cookie, self);
1742 }
1743}
1744
Andreas Gampec147b002014-03-06 18:11:06 -08001745/*
1746 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001747 * 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 -08001748 * The final element on the stack is a pointer to the native code.
1749 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001750 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001751 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001752 *
Andreas Gampec147b002014-03-06 18:11:06 -08001753 * The return of this function denotes:
1754 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1755 * 2) An error, if the value is negative.
1756 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001757extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1758 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001759 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001760 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001761 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001762 uint32_t shorty_len = 0;
1763 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001764
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001765 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001766 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001767 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001768 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001769
Andreas Gampec200a4a2014-06-16 18:39:09 -07001770 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001771 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001772
Ian Rogerse0dcd462014-03-08 15:21:04 -08001773 self->VerifyStack();
1774
Andreas Gampe90546832014-03-12 18:07:19 -07001775 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001776 uint32_t cookie;
1777 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001778 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001779 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001780 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001781 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001783 }
1784 } else {
1785 cookie = JniMethodStart(self);
1786 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001787 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001788 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001789
Andreas Gampe90546832014-03-12 18:07:19 -07001790 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001791 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001792
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001793 // There are two cases for the content of nativeCode:
1794 // 1) Pointer to the native function.
1795 // 2) Pointer to the trampoline for native code binding.
1796 // In the second case, we need to execute the binding and continue with the actual native function
1797 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001798 DCHECK(nativeCode != nullptr);
1799 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001800#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001801 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001802#else
1803 nativeCode = artFindNativeMethod(self);
1804#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001805
1806 if (nativeCode == nullptr) {
1807 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001808
1809 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001810 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001811 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001812 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1813 } else {
1814 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1815 }
1816
Andreas Gampec200a4a2014-06-16 18:39:09 -07001817 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001818 }
1819 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001820 }
1821
Andreas Gampec200a4a2014-06-16 18:39:09 -07001822 // Return native code addr(lo) and bottom of alloca address(hi).
1823 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1824 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001825}
1826
1827/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001828 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001829 * unlocking.
1830 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001831extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001832 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001833 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001834 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001835 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001836 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001837
Andreas Gampead615172014-04-04 16:20:13 -07001838 jobject lock = nullptr;
1839 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001840 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1841 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001842 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001843 }
1844
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001845 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001846
1847 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001848 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001849 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001850 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001851
1852 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001853 case 'F': {
1854 if (kRuntimeISA == kX86) {
1855 // Convert back the result to float.
1856 double d = bit_cast<uint64_t, double>(result_f);
1857 return bit_cast<float, uint32_t>(static_cast<float>(d));
1858 } else {
1859 return result_f;
1860 }
1861 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001862 case 'D':
1863 return result_f;
1864 case 'Z':
1865 return result.z;
1866 case 'B':
1867 return result.b;
1868 case 'C':
1869 return result.c;
1870 case 'S':
1871 return result.s;
1872 case 'I':
1873 return result.i;
1874 case 'J':
1875 return result.j;
1876 case 'V':
1877 return 0;
1878 default:
1879 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1880 return 0;
1881 }
1882 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001883}
1884
Andreas Gamped58342c2014-06-05 14:18:08 -07001885// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1886// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001887//
Andreas Gamped58342c2014-06-05 14:18:08 -07001888// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1889// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001890
1891template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001892static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001893 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001894 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001895
1896template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001897static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001898 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001899 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001900 ScopedQuickEntrypointChecks sqec(self);
1901 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001902 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1903 type);
1904 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001905 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1906 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001907 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001908 {
1909 // Remember the args in case a GC happens in FindMethodFromCode.
1910 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1911 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1912 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001913 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1914 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001915 visitor.FixupReferences();
1916 }
1917
Ian Rogerse0a02da2014-12-02 14:10:53 -08001918 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001919 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001920 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001921 }
1922 }
1923 DCHECK(!self->IsExceptionPending());
1924 const void* code = method->GetEntryPointFromQuickCompiledCode();
1925
1926 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001927 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001928 << " location: "
1929 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001930
Andreas Gamped58342c2014-06-05 14:18:08 -07001931 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1932 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001933}
1934
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001935// Explicit artInvokeCommon template function declarations to please analysis tool.
1936#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1937 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001938 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001939 mirror::Object* this_object, \
1940 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001941 Thread* self, \
1942 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001943
1944EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1945EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1946EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1947EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1948EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1949EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1950EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1951EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1952EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1953EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1954#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1955
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001956// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001957extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1958 uint32_t method_idx, mirror::Object* this_object,
1959 mirror::ArtMethod* caller_method, Thread* self,
1960 StackReference<mirror::ArtMethod>* sp)
1961 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1962 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1963 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001964}
1965
Andreas Gampec200a4a2014-06-16 18:39:09 -07001966extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1967 uint32_t method_idx, mirror::Object* this_object,
1968 mirror::ArtMethod* caller_method, Thread* self,
1969 StackReference<mirror::ArtMethod>* sp)
1970 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1971 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1972 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001973}
1974
Andreas Gampec200a4a2014-06-16 18:39:09 -07001975extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1976 uint32_t method_idx, mirror::Object* this_object,
1977 mirror::ArtMethod* caller_method, Thread* self,
1978 StackReference<mirror::ArtMethod>* sp)
1979 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1980 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1981 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001982}
1983
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1985 uint32_t method_idx, mirror::Object* this_object,
1986 mirror::ArtMethod* caller_method, Thread* self,
1987 StackReference<mirror::ArtMethod>* sp)
1988 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1989 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1990 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001991}
1992
Andreas Gampec200a4a2014-06-16 18:39:09 -07001993extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1994 uint32_t method_idx, mirror::Object* this_object,
1995 mirror::ArtMethod* caller_method, Thread* self,
1996 StackReference<mirror::ArtMethod>* sp)
1997 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1998 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1999 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002000}
2001
2002// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07002003extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07002004 mirror::Object* this_object,
2005 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002006 Thread* self,
2007 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002008 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002009 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002010 mirror::ArtMethod* method;
2011 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2012 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002013 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002014 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2015 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002016 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002017 }
2018 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002019 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002020
2021 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002022 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07002023 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002024
2025 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002026 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002027 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002028 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2029 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2030 Instruction::Code instr_code = instr->Opcode();
2031 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2032 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08002033 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002034 uint32_t dex_method_idx;
2035 if (instr_code == Instruction::INVOKE_INTERFACE) {
2036 dex_method_idx = instr->VRegB_35c();
2037 } else {
2038 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2039 dex_method_idx = instr->VRegB_3rc();
2040 }
2041
Andreas Gampec200a4a2014-06-16 18:39:09 -07002042 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2043 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002044 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002045 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2046 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002047 {
2048 // Remember the args in case a GC happens in FindMethodFromCode.
2049 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2050 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2051 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002052 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002053 self);
2054 visitor.FixupReferences();
2055 }
2056
2057 if (UNLIKELY(method == nullptr)) {
2058 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002059 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002060 }
2061 }
2062 const void* code = method->GetEntryPointFromQuickCompiledCode();
2063
2064 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002065 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002066 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002067
Andreas Gamped58342c2014-06-05 14:18:08 -07002068 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2069 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002070}
2071
Ian Rogers848871b2013-08-05 10:56:33 -07002072} // namespace art