blob: 00251ffaccff7924224e52b3516b4e2ad970ed41 [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
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050062 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000063 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080064 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
65 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
66 static constexpr size_t kNumQuickGprArgs = 3;
67 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080068 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080069 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
70 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
71 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
72 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
73 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
74 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080075 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000076 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080077 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000078#elif defined(__aarch64__)
79 // The callee save frame is pointed to by SP.
80 // | argN | |
81 // | ... | |
82 // | arg4 | |
83 // | arg3 spill | | Caller's frame
84 // | arg2 spill | |
85 // | arg1 spill | |
86 // | Method* | ---
87 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080088 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000089 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080090 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000091 // | X7 |
92 // | : |
93 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080094 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000095 // | : |
96 // | D0 |
97 // | | padding
98 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050099 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000100 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000101 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800102 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000103 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
104 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800105 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800106 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
107 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
108 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
109 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
110 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
111 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000113 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800115#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700116 // The callee save frame is pointed to by SP.
117 // | argN | |
118 // | ... | |
119 // | arg4 | |
120 // | arg3 spill | | Caller's frame
121 // | arg2 spill | |
122 // | arg1 spill | |
123 // | Method* | ---
124 // | RA |
125 // | ... | callee saves
126 // | A3 | arg3
127 // | A2 | arg2
128 // | A1 | arg1
129 // | A0/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500130 static constexpr bool kSplitPairAcrossRegisterAndStack = true;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000131 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800132 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800133 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800134 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
135 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800136 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800137 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
Douglas Leungc6d86722014-12-10 16:15:17 -0800138 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 16; // Offset of first GPR arg.
Ian Rogers936b37f2014-02-14 00:52:24 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800140 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000141 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800142 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800143#elif defined(__mips__) && defined(__LP64__)
144 // The callee save frame is pointed to by SP.
145 // | argN | |
146 // | ... | |
147 // | arg4 | |
148 // | arg3 spill | | Caller's frame
149 // | arg2 spill | |
150 // | arg1 spill | |
151 // | Method* | ---
152 // | RA |
153 // | ... | callee saves
154 // | F7 | f_arg7
155 // | F6 | f_arg6
156 // | F5 | f_arg5
157 // | F6 | f_arg6
158 // | F5 | f_arg5
159 // | F4 | f_arg4
160 // | F3 | f_arg3
161 // | F2 | f_arg2
162 // | F1 | f_arg1
163 // | F0 | f_arg0
164 // | A7 | arg7
165 // | A6 | arg6
166 // | A5 | arg5
167 // | A4 | arg4
168 // | A3 | arg3
169 // | A2 | arg2
170 // | A1 | arg1
171 // | | padding
172 // | A0/Method* | <- sp
173 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800174 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800175 static constexpr bool kAlignPairRegister = false;
176 static constexpr bool kQuickSoftFloatAbi = false;
177 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
178 // These values are set to zeros because GPR and FPR register
179 // assignments for Mips64 are interleaved, which the current VisitArguments()
180 // function does not support.
181 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
182 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
183 static constexpr bool kGprFprLockstep = true;
184
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
186 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
187 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
188 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
189 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
190 }
Ian Rogers848871b2013-08-05 10:56:33 -0700191#elif defined(__i386__)
192 // The callee save frame is pointed to by SP.
193 // | argN | |
194 // | ... | |
195 // | arg4 | |
196 // | arg3 spill | | Caller's frame
197 // | arg2 spill | |
198 // | arg1 spill | |
199 // | Method* | ---
200 // | Return |
201 // | EBP,ESI,EDI | callee saves
202 // | EBX | arg3
203 // | EDX | arg2
204 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000205 // | XMM3 | float arg 4
206 // | XMM2 | float arg 3
207 // | XMM1 | float arg 2
208 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700209 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500210 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000211 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000212 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800213 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800214 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000215 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000217 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
218 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
219 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800220 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000221 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800222 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800223#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800224 // The callee save frame is pointed to by SP.
225 // | argN | |
226 // | ... | |
227 // | reg. arg spills | | Caller's frame
228 // | Method* | ---
229 // | Return |
230 // | R15 | callee save
231 // | R14 | callee save
232 // | R13 | callee save
233 // | R12 | callee save
234 // | R9 | arg5
235 // | R8 | arg4
236 // | RSI/R6 | arg1
237 // | RBP/R5 | callee save
238 // | RBX/R3 | callee save
239 // | RDX/R2 | arg2
240 // | RCX/R1 | arg3
241 // | XMM7 | float arg 8
242 // | XMM6 | float arg 7
243 // | XMM5 | float arg 6
244 // | XMM4 | float arg 5
245 // | XMM3 | float arg 4
246 // | XMM2 | float arg 3
247 // | XMM1 | float arg 2
248 // | XMM0 | float arg 1
249 // | Padding |
250 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500251 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000252 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800253 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800254 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700255 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700256 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800257 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700259 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
260 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800261 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
262 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000263 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
264 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
265 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
266 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
267 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800268 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700269 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
270 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800271 }
272 }
Ian Rogers848871b2013-08-05 10:56:33 -0700273#else
274#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700275#endif
276
Ian Rogers936b37f2014-02-14 00:52:24 -0800277 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100278 // Special handling for proxy methods. Proxy methods are instance methods so the
279 // 'this' object is the 1st argument. They also have the same frame layout as the
280 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
281 // 1st GPR.
282 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
283 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
284 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
285 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
286 CHECK_GT(kNumQuickGprArgs, 0u);
287 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
288 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
289 GprIndexToGprOffset(kThisGprIndex);
290 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
291 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
292 }
293
Andreas Gampecf4035a2014-05-28 22:43:01 -0700294 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700296 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700297 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700298 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700299 }
300
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700302 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700304 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700305 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700306 return *reinterpret_cast<uintptr_t*>(lr);
307 }
308
Andreas Gampec200a4a2014-06-16 18:39:09 -0700309 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
310 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
311 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700312 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
313 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
314 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700315 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800316 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
317 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800318 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
319 "Number of Quick FPR arguments unexpected");
320 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
321 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800322 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
323 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800324 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
325 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800326 }
Ian Rogers848871b2013-08-05 10:56:33 -0700327
328 virtual ~QuickArgumentVisitor() {}
329
330 virtual void Visit() = 0;
331
Ian Rogers936b37f2014-02-14 00:52:24 -0800332 Primitive::Type GetParamPrimitiveType() const {
333 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700334 }
335
Ian Rogers13735952014-10-08 12:43:28 -0700336 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800337 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800338 Primitive::Type type = GetParamPrimitiveType();
339 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800340 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
341 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
342 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
343 }
344 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000345 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800346 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700347 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800348 }
349 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800350 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800351 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
352 }
353 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700354 }
355
356 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000357 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 return is_split_long_or_double_;
359 } else {
360 return false; // An optimization for when GPR and FPRs are 64bit.
361 }
Ian Rogers848871b2013-08-05 10:56:33 -0700362 }
363
Ian Rogers936b37f2014-02-14 00:52:24 -0800364 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700365 return GetParamPrimitiveType() == Primitive::kPrimNot;
366 }
367
Ian Rogers936b37f2014-02-14 00:52:24 -0800368 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700369 Primitive::Type type = GetParamPrimitiveType();
370 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
371 }
372
373 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000374 // The splitted long is always available through the stack.
375 return *reinterpret_cast<uint64_t*>(stack_args_
376 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700377 }
378
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800379 void IncGprIndex() {
380 gpr_index_++;
381 if (kGprFprLockstep) {
382 fpr_index_++;
383 }
384 }
385
386 void IncFprIndex() {
387 fpr_index_++;
388 if (kGprFprLockstep) {
389 gpr_index_++;
390 }
391 }
392
Ian Rogers848871b2013-08-05 10:56:33 -0700393 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800394 // (a) 'stack_args_' should point to the first method's argument
395 // (b) whatever the argument type it is, the 'stack_index_' should
396 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800397 gpr_index_ = 0;
398 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800399 if (kQuickDoubleRegAlignedFloatBackFilled) {
400 fpr_double_index_ = 0;
401 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800402 stack_index_ = 0;
403 if (!is_static_) { // Handle this.
404 cur_type_ = Primitive::kPrimNot;
405 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700406 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800407 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800408 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800409 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800410 }
Ian Rogers848871b2013-08-05 10:56:33 -0700411 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800412 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
413 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
414 switch (cur_type_) {
415 case Primitive::kPrimNot:
416 case Primitive::kPrimBoolean:
417 case Primitive::kPrimByte:
418 case Primitive::kPrimChar:
419 case Primitive::kPrimShort:
420 case Primitive::kPrimInt:
421 is_split_long_or_double_ = false;
422 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800423 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800424 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800425 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 }
427 break;
428 case Primitive::kPrimFloat:
429 is_split_long_or_double_ = false;
430 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800431 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800432 if (kQuickSoftFloatAbi) {
433 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800434 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800435 }
436 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800437 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800438 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800439 if (kQuickDoubleRegAlignedFloatBackFilled) {
440 // Double should not overlap with float.
441 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
442 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
443 // Float should not overlap with double.
444 if (fpr_index_ % 2 == 0) {
445 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
446 }
447 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800448 }
449 }
450 break;
451 case Primitive::kPrimDouble:
452 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800453 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000454 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
455 // Currently, this is only for ARM, where the first available parameter register
456 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800457 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000458 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000459 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800460 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500461 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
462 // We don't want to split this. Pass over this register.
463 gpr_index_++;
464 is_split_long_or_double_ = false;
465 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800466 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800467 if (kBytesStackArgLocation == 4) {
468 stack_index_+= 2;
469 } else {
470 CHECK_EQ(kBytesStackArgLocation, 8U);
471 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800472 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700473 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800474 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000475 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700476 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800477 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700478 }
479 }
480 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800481 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000482 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800483 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800484 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700485 if (kBytesStackArgLocation == 4) {
486 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800487 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700488 CHECK_EQ(kBytesStackArgLocation, 8U);
489 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800490 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800491 if (kQuickDoubleRegAlignedFloatBackFilled) {
492 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
493 fpr_double_index_ += 2;
494 // Float should not overlap with double.
495 if (fpr_index_ % 2 == 0) {
496 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
497 }
498 }
499 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800500 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800501 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
502 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800503 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800504 }
505 }
506 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800507 }
508 break;
509 default:
510 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
511 }
Ian Rogers848871b2013-08-05 10:56:33 -0700512 }
513 }
514
Andreas Gampec200a4a2014-06-16 18:39:09 -0700515 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700516 const bool is_static_;
517 const char* const shorty_;
518 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700519
520 private:
Ian Rogers13735952014-10-08 12:43:28 -0700521 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
522 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
523 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800524 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800525 // Index into spilled FPRs.
526 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
527 // holds a higher register number.
528 uint32_t fpr_index_;
529 // Index into spilled FPRs for aligned double.
530 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
531 // terms of singles, may be behind fpr_index.
532 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800533 uint32_t stack_index_; // Index into arguments on the stack.
534 // The current type of argument during VisitArguments.
535 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700536 // Does a 64bit parameter straddle the register and stack arguments?
537 bool is_split_long_or_double_;
538};
539
Sebastien Hertza836bc92014-11-25 16:30:53 +0100540// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
541// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
542extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
543 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
544 return QuickArgumentVisitor::GetProxyThisObject(sp);
545}
546
Ian Rogers848871b2013-08-05 10:56:33 -0700547// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800548class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700549 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700550 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
551 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
552 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700553 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700554
Ian Rogers9758f792014-03-13 09:02:55 -0700555 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700556
557 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800558 ShadowFrame* const sf_;
559 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700560
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700561 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700562};
563
Andreas Gampec200a4a2014-06-16 18:39:09 -0700564void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700565 Primitive::Type type = GetParamPrimitiveType();
566 switch (type) {
567 case Primitive::kPrimLong: // Fall-through.
568 case Primitive::kPrimDouble:
569 if (IsSplitLongOrDouble()) {
570 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
571 } else {
572 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
573 }
574 ++cur_reg_;
575 break;
576 case Primitive::kPrimNot: {
577 StackReference<mirror::Object>* stack_ref =
578 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
579 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
580 }
581 break;
582 case Primitive::kPrimBoolean: // Fall-through.
583 case Primitive::kPrimByte: // Fall-through.
584 case Primitive::kPrimChar: // Fall-through.
585 case Primitive::kPrimShort: // Fall-through.
586 case Primitive::kPrimInt: // Fall-through.
587 case Primitive::kPrimFloat:
588 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
589 break;
590 case Primitive::kPrimVoid:
591 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700592 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700593 }
594 ++cur_reg_;
595}
596
Brian Carlstromea46f952013-07-30 01:26:50 -0700597extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700598 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700599 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
600 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
601 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700602 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700603
604 if (method->IsAbstract()) {
605 ThrowAbstractMethodError(method);
606 return 0;
607 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800608 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700609 const char* old_cause = self->StartAssertNoThreadSuspension(
610 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700611 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800612 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700613 uint16_t num_regs = code_item->registers_size_;
614 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700615 // No last shadow coming from quick.
616 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700617 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700618 uint32_t shorty_len = 0;
619 const char* shorty = method->GetShorty(&shorty_len);
620 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800621 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700622 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800623 const bool needs_initialization =
624 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700625 // Push a transition back into managed code onto the linked list in thread.
626 ManagedStack fragment;
627 self->PushManagedStackFragment(&fragment);
628 self->PushShadowFrame(shadow_frame);
629 self->EndAssertNoThreadSuspension(old_cause);
630
Ian Rogerse94652f2014-12-02 11:13:19 -0800631 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700632 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800633 StackHandleScope<1> hs(self);
634 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700635 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800636 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700637 self->PopManagedStackFragment(fragment);
638 return 0;
639 }
640 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800641 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700642 // Pop transition.
643 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800644 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700645 return result.GetJ();
646 }
647}
648
649// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
650// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800651class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700652 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700653 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
654 const char* shorty, uint32_t shorty_len,
655 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700656 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700657
Ian Rogers9758f792014-03-13 09:02:55 -0700658 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700659
Ian Rogers9758f792014-03-13 09:02:55 -0700660 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800661
Ian Rogers848871b2013-08-05 10:56:33 -0700662 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700663 ScopedObjectAccessUnchecked* const soa_;
664 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800665 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700666 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700667
Ian Rogers848871b2013-08-05 10:56:33 -0700668 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
669};
670
Ian Rogers9758f792014-03-13 09:02:55 -0700671void BuildQuickArgumentVisitor::Visit() {
672 jvalue val;
673 Primitive::Type type = GetParamPrimitiveType();
674 switch (type) {
675 case Primitive::kPrimNot: {
676 StackReference<mirror::Object>* stack_ref =
677 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
678 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
679 references_.push_back(std::make_pair(val.l, stack_ref));
680 break;
681 }
682 case Primitive::kPrimLong: // Fall-through.
683 case Primitive::kPrimDouble:
684 if (IsSplitLongOrDouble()) {
685 val.j = ReadSplitLongParam();
686 } else {
687 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
688 }
689 break;
690 case Primitive::kPrimBoolean: // Fall-through.
691 case Primitive::kPrimByte: // Fall-through.
692 case Primitive::kPrimChar: // Fall-through.
693 case Primitive::kPrimShort: // Fall-through.
694 case Primitive::kPrimInt: // Fall-through.
695 case Primitive::kPrimFloat:
696 val.i = *reinterpret_cast<jint*>(GetParamAddress());
697 break;
698 case Primitive::kPrimVoid:
699 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700700 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700701 }
702 args_->push_back(val);
703}
704
705void BuildQuickArgumentVisitor::FixupReferences() {
706 // Fixup any references which may have changed.
707 for (const auto& pair : references_) {
708 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700709 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700710 }
711}
712
Ian Rogers848871b2013-08-05 10:56:33 -0700713// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
714// which is responsible for recording callee save registers. We explicitly place into jobjects the
715// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
716// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700717extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700718 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700719 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700720 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700721 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
722 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700723 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
724 const char* old_cause =
725 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
726 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700727 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700728 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700729 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
730 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700731 self->VerifyStack();
732 // Start new JNI local reference state.
733 JNIEnvExt* env = self->GetJniEnv();
734 ScopedObjectAccessUnchecked soa(env);
735 ScopedJniEnvLocalRefState env_state(env);
736 // Create local ref. copies of proxy method and the receiver.
737 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
738
739 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700740 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
741 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700742 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700743 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700744 uint32_t shorty_len = 0;
745 const char* shorty = proxy_method->GetShorty(&shorty_len);
746 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700747
Ian Rogers848871b2013-08-05 10:56:33 -0700748 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700749 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700750 args.erase(args.begin());
751
752 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700753 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800754 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700755 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
756 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
757
758 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
759 // that performs allocations.
760 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700761 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800762 // Restore references which might have moved.
763 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700764 return result.GetJ();
765}
766
767// Read object references held in arguments from quick frames and place in a JNI local references,
768// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800769class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700770 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700771 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
772 const char* shorty, uint32_t shorty_len,
773 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700774 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700775
Ian Rogers9758f792014-03-13 09:02:55 -0700776 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700777
Ian Rogers9758f792014-03-13 09:02:55 -0700778 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700779
780 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700781 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800782 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700783 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
784
Mathieu Chartier590fee92013-09-13 13:46:47 -0700785 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700786};
787
Ian Rogers9758f792014-03-13 09:02:55 -0700788void RememberForGcArgumentVisitor::Visit() {
789 if (IsParamAReference()) {
790 StackReference<mirror::Object>* stack_ref =
791 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
792 jobject reference =
793 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
794 references_.push_back(std::make_pair(reference, stack_ref));
795 }
796}
797
798void RememberForGcArgumentVisitor::FixupReferences() {
799 // Fixup any references which may have changed.
800 for (const auto& pair : references_) {
801 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700802 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700803 }
804}
805
Ian Rogers848871b2013-08-05 10:56:33 -0700806// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700807extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700808 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700809 Thread* self,
810 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700811 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700812 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700813 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800814 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700815 ScopedObjectAccessUnchecked soa(env);
816 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800817 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700818
819 // Compute details about the called method (avoid GCs)
820 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700821 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700822 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800823 MethodReference called_method(nullptr, 0);
824 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
825 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700826 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
827 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800828 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700829 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700830 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
831 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
832 Instruction::Code instr_code = instr->Opcode();
833 bool is_range;
834 switch (instr_code) {
835 case Instruction::INVOKE_DIRECT:
836 invoke_type = kDirect;
837 is_range = false;
838 break;
839 case Instruction::INVOKE_DIRECT_RANGE:
840 invoke_type = kDirect;
841 is_range = true;
842 break;
843 case Instruction::INVOKE_STATIC:
844 invoke_type = kStatic;
845 is_range = false;
846 break;
847 case Instruction::INVOKE_STATIC_RANGE:
848 invoke_type = kStatic;
849 is_range = true;
850 break;
851 case Instruction::INVOKE_SUPER:
852 invoke_type = kSuper;
853 is_range = false;
854 break;
855 case Instruction::INVOKE_SUPER_RANGE:
856 invoke_type = kSuper;
857 is_range = true;
858 break;
859 case Instruction::INVOKE_VIRTUAL:
860 invoke_type = kVirtual;
861 is_range = false;
862 break;
863 case Instruction::INVOKE_VIRTUAL_RANGE:
864 invoke_type = kVirtual;
865 is_range = true;
866 break;
867 case Instruction::INVOKE_INTERFACE:
868 invoke_type = kInterface;
869 is_range = false;
870 break;
871 case Instruction::INVOKE_INTERFACE_RANGE:
872 invoke_type = kInterface;
873 is_range = true;
874 break;
875 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800876 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
877 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700878 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800879 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700880 } else {
881 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800882 called_method.dex_file = called->GetDexFile();
883 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700884 }
885 uint32_t shorty_len;
886 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800887 called_method.dex_file->GetMethodShorty(
888 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700889 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700890 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800891 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800892 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700893 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800894 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700895 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700896 mirror::Object* dummy = nullptr;
897 HandleWrapper<mirror::Object> h_receiver(
898 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800899 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
900 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700901 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800902 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800903 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700904 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800905 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
906 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800907 if (virtual_or_interface) {
908 // Refine called method based on receiver.
909 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700910
911 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800912 if (invoke_type == kVirtual) {
913 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
914 } else {
915 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
916 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700917
918 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
919 << PrettyTypeOf(receiver) << " "
920 << invoke_type << " " << orig_called->GetVtableIndex();
921
Ian Rogers83883d72013-10-21 21:07:24 -0700922 // 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 -0800923 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800924 // Note, called_method.dex_method_index references the dex method before the
925 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
926 // about the name and signature.
927 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800928 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700929 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000930 // the caller's dex file. Since we get here only if the original called was a runtime
931 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800932 DCHECK(!called_method_known_on_entry);
933 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
934 const DexFile* caller_dex_file = called_method.dex_file;
935 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
936 update_dex_cache_method_index =
937 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
938 caller_method_name_and_sig_index);
939 }
940 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
941 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
942 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700943 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800944 } else if (invoke_type == kStatic) {
945 const auto called_dex_method_idx = called->GetDexMethodIndex();
946 // For static invokes, we may dispatch to the static method in the superclass but resolve
947 // using the subclass. To prevent getting slow paths on each invoke, we force set the
948 // resolved method for the super class dex method index if we are in the same dex file.
949 // b/19175856
950 if (called->GetDexFile() == called_method.dex_file &&
951 called_method.dex_method_index != called_dex_method_idx) {
952 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called);
953 }
Ian Rogers83883d72013-10-21 21:07:24 -0700954 }
Ian Rogers848871b2013-08-05 10:56:33 -0700955 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700956 StackHandleScope<1> hs(soa.Self());
957 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700958 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700959 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800960 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700961 } else if (called_class->IsInitializing()) {
962 if (invoke_type == kStatic) {
963 // Class is still initializing, go to oat and grab code (trampoline must be left in place
964 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800965 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700966 } else {
967 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800968 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700969 }
970 } else {
971 DCHECK(called_class->IsErroneous());
972 }
973 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800974 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700975 // Fixup any locally saved objects may have moved during a GC.
976 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700977 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700978 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700979 return code;
980}
981
Andreas Gampec147b002014-03-06 18:11:06 -0800982/*
983 * This class uses a couple of observations to unite the different calling conventions through
984 * a few constants.
985 *
986 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
987 * possible alignment.
988 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
989 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
990 * when we have to split things
991 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
992 * and we can use Int handling directly.
993 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
994 * necessary when widening. Also, widening of Ints will take place implicitly, and the
995 * extension should be compatible with Aarch64, which mandates copying the available bits
996 * into LSB and leaving the rest unspecified.
997 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
998 * the stack.
999 * 6) There is only little endian.
1000 *
1001 *
1002 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1003 * follows:
1004 *
1005 * void PushGpr(uintptr_t): Add a value for the next GPR
1006 *
1007 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1008 * padding, that is, think the architecture is 32b and aligns 64b.
1009 *
1010 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1011 * split this if necessary. The current state will have aligned, if
1012 * necessary.
1013 *
1014 * void PushStack(uintptr_t): Push a value to the stack.
1015 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001016 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001017 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001018 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001019 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001020 *
1021 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001022template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001023 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001024#if defined(__arm__)
1025 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001026 static constexpr bool kNativeSoftFloatAbi = true;
1027 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001028 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1029
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001030 static constexpr size_t kRegistersNeededForLong = 2;
1031 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001032 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001033 static constexpr bool kMultiFPRegistersWidened = false;
1034 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001035 static constexpr bool kAlignLongOnStack = true;
1036 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001037#elif defined(__aarch64__)
1038 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1039 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1040 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1041
1042 static constexpr size_t kRegistersNeededForLong = 1;
1043 static constexpr size_t kRegistersNeededForDouble = 1;
1044 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001045 static constexpr bool kMultiFPRegistersWidened = false;
1046 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001047 static constexpr bool kAlignLongOnStack = false;
1048 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001049#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001050 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001051 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1052 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001053
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001054 static constexpr size_t kRegistersNeededForLong = 2;
1055 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001056 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001057 static constexpr bool kMultiFPRegistersWidened = true;
1058 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001059 static constexpr bool kAlignLongOnStack = true;
1060 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001061#elif defined(__mips__) && defined(__LP64__)
1062 // Let the code prepare GPRs only and we will load the FPRs with same data.
1063 static constexpr bool kNativeSoftFloatAbi = true;
1064 static constexpr size_t kNumNativeGprArgs = 8;
1065 static constexpr size_t kNumNativeFprArgs = 0;
1066
1067 static constexpr size_t kRegistersNeededForLong = 1;
1068 static constexpr size_t kRegistersNeededForDouble = 1;
1069 static constexpr bool kMultiRegistersAligned = false;
1070 static constexpr bool kMultiFPRegistersWidened = false;
1071 static constexpr bool kMultiGPRegistersWidened = true;
1072 static constexpr bool kAlignLongOnStack = false;
1073 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001074#elif defined(__i386__)
1075 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001076 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001077 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1078 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1079
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001080 static constexpr size_t kRegistersNeededForLong = 2;
1081 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001082 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001083 static constexpr bool kMultiFPRegistersWidened = false;
1084 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001085 static constexpr bool kAlignLongOnStack = false;
1086 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001087#elif defined(__x86_64__)
1088 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1089 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1090 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1091
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001092 static constexpr size_t kRegistersNeededForLong = 1;
1093 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001094 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001095 static constexpr bool kMultiFPRegistersWidened = false;
1096 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001097 static constexpr bool kAlignLongOnStack = false;
1098 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001099#else
1100#error "Unsupported architecture"
1101#endif
1102
Andreas Gampec147b002014-03-06 18:11:06 -08001103 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001104 explicit BuildNativeCallFrameStateMachine(T* delegate)
1105 : gpr_index_(kNumNativeGprArgs),
1106 fpr_index_(kNumNativeFprArgs),
1107 stack_entries_(0),
1108 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001109 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1110 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001111 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1112 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001113 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001114
Andreas Gampec200a4a2014-06-16 18:39:09 -07001115 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001116
Ian Rogers1428dce2014-10-21 15:02:15 -07001117 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001118 return gpr_index_ > 0;
1119 }
1120
Andreas Gampec200a4a2014-06-16 18:39:09 -07001121 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001122 if (HavePointerGpr()) {
1123 gpr_index_--;
1124 PushGpr(reinterpret_cast<uintptr_t>(val));
1125 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001126 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001127 PushStack(reinterpret_cast<uintptr_t>(val));
1128 gpr_index_ = 0;
1129 }
1130 }
1131
Ian Rogers1428dce2014-10-21 15:02:15 -07001132 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001133 return gpr_index_ > 0;
1134 }
1135
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001136 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1137 uintptr_t handle = PushHandle(ptr);
1138 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001139 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001140 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001141 } else {
1142 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001143 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001144 gpr_index_ = 0;
1145 }
1146 }
1147
Ian Rogers1428dce2014-10-21 15:02:15 -07001148 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001149 return gpr_index_ > 0;
1150 }
1151
1152 void AdvanceInt(uint32_t val) {
1153 if (HaveIntGpr()) {
1154 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001155 if (kMultiGPRegistersWidened) {
1156 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1157 PushGpr(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1158 } else {
1159 PushGpr(val);
1160 }
Andreas Gampec147b002014-03-06 18:11:06 -08001161 } else {
1162 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001163 if (kMultiGPRegistersWidened) {
1164 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1165 PushStack(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1166 } else {
1167 PushStack(val);
1168 }
Andreas Gampec147b002014-03-06 18:11:06 -08001169 gpr_index_ = 0;
1170 }
1171 }
1172
Ian Rogers1428dce2014-10-21 15:02:15 -07001173 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001174 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1175 }
1176
Ian Rogers1428dce2014-10-21 15:02:15 -07001177 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001178 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1179 kAlignLongOnStack && // and when it needs alignment
1180 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1181 }
1182
Ian Rogers1428dce2014-10-21 15:02:15 -07001183 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001184 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1185 kAlignLongOnStack && // and when it needs 8B alignment
1186 (stack_entries_ & 1) == 1; // counter is odd
1187 }
1188
1189 void AdvanceLong(uint64_t val) {
1190 if (HaveLongGpr()) {
1191 if (LongGprNeedsPadding()) {
1192 PushGpr(0);
1193 gpr_index_--;
1194 }
1195 if (kRegistersNeededForLong == 1) {
1196 PushGpr(static_cast<uintptr_t>(val));
1197 } else {
1198 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1199 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1200 }
1201 gpr_index_ -= kRegistersNeededForLong;
1202 } else {
1203 if (LongStackNeedsPadding()) {
1204 PushStack(0);
1205 stack_entries_++;
1206 }
1207 if (kRegistersNeededForLong == 1) {
1208 PushStack(static_cast<uintptr_t>(val));
1209 stack_entries_++;
1210 } else {
1211 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1212 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1213 stack_entries_ += 2;
1214 }
1215 gpr_index_ = 0;
1216 }
1217 }
1218
Ian Rogers1428dce2014-10-21 15:02:15 -07001219 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001220 return fpr_index_ > 0;
1221 }
1222
Andreas Gampec147b002014-03-06 18:11:06 -08001223 void AdvanceFloat(float val) {
1224 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001225 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001226 } else {
1227 if (HaveFloatFpr()) {
1228 fpr_index_--;
1229 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001230 if (kMultiFPRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001231 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001232 } else {
1233 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001234 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001235 }
1236 } else {
1237 PushFpr4(val);
1238 }
1239 } else {
1240 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001241 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001242 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001243 // Note: We need to jump through those hoops to make the compiler happy.
1244 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1245 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001246 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001247 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001248 }
1249 fpr_index_ = 0;
1250 }
1251 }
1252 }
1253
Ian Rogers1428dce2014-10-21 15:02:15 -07001254 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001255 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1256 }
1257
Ian Rogers1428dce2014-10-21 15:02:15 -07001258 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001259 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1260 kAlignDoubleOnStack && // and when it needs alignment
1261 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1262 }
1263
Ian Rogers1428dce2014-10-21 15:02:15 -07001264 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001265 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1266 kAlignDoubleOnStack && // and when it needs 8B alignment
1267 (stack_entries_ & 1) == 1; // counter is odd
1268 }
1269
1270 void AdvanceDouble(uint64_t val) {
1271 if (kNativeSoftFloatAbi) {
1272 AdvanceLong(val);
1273 } else {
1274 if (HaveDoubleFpr()) {
1275 if (DoubleFprNeedsPadding()) {
1276 PushFpr4(0);
1277 fpr_index_--;
1278 }
1279 PushFpr8(val);
1280 fpr_index_ -= kRegistersNeededForDouble;
1281 } else {
1282 if (DoubleStackNeedsPadding()) {
1283 PushStack(0);
1284 stack_entries_++;
1285 }
1286 if (kRegistersNeededForDouble == 1) {
1287 PushStack(static_cast<uintptr_t>(val));
1288 stack_entries_++;
1289 } else {
1290 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1291 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1292 stack_entries_ += 2;
1293 }
1294 fpr_index_ = 0;
1295 }
1296 }
1297 }
1298
Ian Rogers1428dce2014-10-21 15:02:15 -07001299 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001300 return stack_entries_;
1301 }
1302
Ian Rogers1428dce2014-10-21 15:02:15 -07001303 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001304 return kNumNativeGprArgs - gpr_index_;
1305 }
1306
Ian Rogers1428dce2014-10-21 15:02:15 -07001307 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001308 return kNumNativeFprArgs - fpr_index_;
1309 }
1310
1311 private:
1312 void PushGpr(uintptr_t val) {
1313 delegate_->PushGpr(val);
1314 }
1315 void PushFpr4(float val) {
1316 delegate_->PushFpr4(val);
1317 }
1318 void PushFpr8(uint64_t val) {
1319 delegate_->PushFpr8(val);
1320 }
1321 void PushStack(uintptr_t val) {
1322 delegate_->PushStack(val);
1323 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001324 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1325 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001326 }
1327
1328 uint32_t gpr_index_; // Number of free GPRs
1329 uint32_t fpr_index_; // Number of free FPRs
1330 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1331 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001332 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001333};
1334
Andreas Gampec200a4a2014-06-16 18:39:09 -07001335// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1336// in subclasses.
1337//
1338// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1339// them with handles.
1340class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001341 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001342 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1343
1344 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001345
Ian Rogers1428dce2014-10-21 15:02:15 -07001346 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001347 return num_stack_entries_ * sizeof(uintptr_t);
1348 }
1349
Ian Rogers1428dce2014-10-21 15:02:15 -07001350 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001351 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001352 // Align by kStackAlignment.
1353 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001354 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001355 }
1356
Ian Rogers1428dce2014-10-21 15:02:15 -07001357 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1358 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001359 // Assumption is OK right now, as we have soft-float arm
1360 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1361 sp8 -= fregs * sizeof(uintptr_t);
1362 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1363 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1364 sp8 -= iregs * sizeof(uintptr_t);
1365 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1366 return sp8;
1367 }
Andreas Gampec147b002014-03-06 18:11:06 -08001368
Andreas Gampec200a4a2014-06-16 18:39:09 -07001369 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001370 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001371 // Native call stack.
1372 sp8 = LayoutCallStack(sp8);
1373 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001374
Andreas Gampec200a4a2014-06-16 18:39:09 -07001375 // Put fprs and gprs below.
1376 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001377
Andreas Gampec200a4a2014-06-16 18:39:09 -07001378 // Return the new bottom.
1379 return sp8;
1380 }
1381
1382 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1384 UNUSED(sm);
1385 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001386
1387 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1388 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1389
1390 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001391
1392 for (uint32_t i = 1; i < shorty_len; ++i) {
1393 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1394 switch (cur_type_) {
1395 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001396 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001397 sm.AdvanceHandleScope(
1398 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001399 break;
1400
1401 case Primitive::kPrimBoolean:
1402 case Primitive::kPrimByte:
1403 case Primitive::kPrimChar:
1404 case Primitive::kPrimShort:
1405 case Primitive::kPrimInt:
1406 sm.AdvanceInt(0);
1407 break;
1408 case Primitive::kPrimFloat:
1409 sm.AdvanceFloat(0);
1410 break;
1411 case Primitive::kPrimDouble:
1412 sm.AdvanceDouble(0);
1413 break;
1414 case Primitive::kPrimLong:
1415 sm.AdvanceLong(0);
1416 break;
1417 default:
1418 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001419 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001420 }
1421 }
1422
Ian Rogers1428dce2014-10-21 15:02:15 -07001423 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001424 }
1425
1426 void PushGpr(uintptr_t /* val */) {
1427 // not optimizing registers, yet
1428 }
1429
1430 void PushFpr4(float /* val */) {
1431 // not optimizing registers, yet
1432 }
1433
1434 void PushFpr8(uint64_t /* val */) {
1435 // not optimizing registers, yet
1436 }
1437
1438 void PushStack(uintptr_t /* val */) {
1439 // counting is already done in the superclass
1440 }
1441
Andreas Gampec200a4a2014-06-16 18:39:09 -07001442 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001443 return reinterpret_cast<uintptr_t>(nullptr);
1444 }
1445
Andreas Gampec200a4a2014-06-16 18:39:09 -07001446 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001447 uint32_t num_stack_entries_;
1448};
1449
Andreas Gampec200a4a2014-06-16 18:39:09 -07001450class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001451 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001452 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001453
Andreas Gampec200a4a2014-06-16 18:39:09 -07001454 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1455 // is at *m = sp. Will update to point to the bottom of the save frame.
1456 //
1457 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001458 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1459 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001460 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1461 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001462
Andreas Gampec200a4a2014-06-16 18:39:09 -07001463 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1464
1465 // First, fix up the layout of the callee-save frame.
1466 // We have to squeeze in the HandleScope, and relocate the method pointer.
1467
1468 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001469 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001470
1471 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001472 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1473 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1474
1475 sp8 -= scope_and_method;
1476 // Align by kStackAlignment.
1477 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1478 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1479
1480 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001481 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1482 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001483
1484 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1485 uint8_t* method_pointer = sp8;
1486 StackReference<mirror::ArtMethod>* new_method_ref =
1487 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1488 new_method_ref->Assign(method);
1489 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001490 }
1491
Andreas Gampec200a4a2014-06-16 18:39:09 -07001492 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001493 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001494 // Reference cookie and padding
1495 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001496 }
1497
Andreas Gampec200a4a2014-06-16 18:39:09 -07001498 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1499 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001500 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1501 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001503 // First, fix up the layout of the callee-save frame.
1504 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001505 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001506
1507 // The bottom of the callee-save frame is now where the method is, *m.
1508 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1509
1510 // Add space for cookie.
1511 LayoutCookie(&sp8);
1512
1513 return sp8;
1514 }
1515
1516 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001517 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001518 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001519 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1520 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1521 Walk(shorty, shorty_len);
1522
1523 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001524 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001525
1526 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1527
1528 // Return the new bottom.
1529 return sp8;
1530 }
1531
1532 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1533
1534 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1535 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1536 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1537
1538 private:
1539 uint32_t num_handle_scope_references_;
1540};
1541
1542uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1543 num_handle_scope_references_++;
1544 return reinterpret_cast<uintptr_t>(nullptr);
1545}
1546
1547void ComputeGenericJniFrameSize::WalkHeader(
1548 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1549 // JNIEnv
1550 sm->AdvancePointer(nullptr);
1551
1552 // Class object or this as first argument
1553 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1554}
1555
1556// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1557// the template requirements of BuildGenericJniFrameStateMachine.
1558class FillNativeCall {
1559 public:
1560 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1561 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1562
1563 virtual ~FillNativeCall() {}
1564
1565 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1566 cur_gpr_reg_ = gpr_regs;
1567 cur_fpr_reg_ = fpr_regs;
1568 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001569 }
1570
1571 void PushGpr(uintptr_t val) {
1572 *cur_gpr_reg_ = val;
1573 cur_gpr_reg_++;
1574 }
1575
1576 void PushFpr4(float val) {
1577 *cur_fpr_reg_ = val;
1578 cur_fpr_reg_++;
1579 }
1580
1581 void PushFpr8(uint64_t val) {
1582 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1583 *tmp = val;
1584 cur_fpr_reg_ += 2;
1585 }
1586
1587 void PushStack(uintptr_t val) {
1588 *cur_stack_arg_ = val;
1589 cur_stack_arg_++;
1590 }
1591
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001592 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001593 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001594 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001595 }
1596
1597 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001598 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001599 uint32_t* cur_fpr_reg_;
1600 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001601};
Andreas Gampec147b002014-03-06 18:11:06 -08001602
Andreas Gampec200a4a2014-06-16 18:39:09 -07001603// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1604// of transitioning into native code.
1605class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1606 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001607 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1608 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001609 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1610 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1611 ComputeGenericJniFrameSize fsc;
1612 uintptr_t* start_gpr_reg;
1613 uint32_t* start_fpr_reg;
1614 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001615 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001616 &handle_scope_,
1617 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618 &start_gpr_reg, &start_fpr_reg);
1619
Andreas Gampec200a4a2014-06-16 18:39:09 -07001620 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1621
1622 // jni environment is always first argument
1623 sm_.AdvancePointer(self->GetJniEnv());
1624
1625 if (is_static) {
1626 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1627 }
1628 }
1629
1630 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1631
1632 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1633
1634 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1636 return handle_scope_->GetHandle(0).GetReference();
1637 }
1638
Ian Rogers1428dce2014-10-21 15:02:15 -07001639 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001640 return handle_scope_->GetHandle(0).ToJObject();
1641 }
1642
Ian Rogers1428dce2014-10-21 15:02:15 -07001643 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001644 return bottom_of_used_area_;
1645 }
1646
1647 private:
1648 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1649 class FillJniCall FINAL : public FillNativeCall {
1650 public:
1651 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1652 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1653 handle_scope_(handle_scope), cur_entry_(0) {}
1654
1655 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1656
1657 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1658 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1659 handle_scope_ = scope;
1660 cur_entry_ = 0U;
1661 }
1662
1663 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1664 // Initialize padding entries.
1665 size_t expected_slots = handle_scope_->NumberOfReferences();
1666 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001667 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001668 }
1669 DCHECK_NE(cur_entry_, 0U);
1670 }
1671
1672 private:
1673 HandleScope* handle_scope_;
1674 size_t cur_entry_;
1675 };
1676
1677 HandleScope* handle_scope_;
1678 FillJniCall jni_call_;
1679 void* bottom_of_used_area_;
1680
1681 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001682
1683 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1684};
1685
Andreas Gampec200a4a2014-06-16 18:39:09 -07001686uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1687 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001688 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001689 h.Assign(ref);
1690 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1691 cur_entry_++;
1692 return tmp;
1693}
1694
Ian Rogers9758f792014-03-13 09:02:55 -07001695void BuildGenericJniFrameVisitor::Visit() {
1696 Primitive::Type type = GetParamPrimitiveType();
1697 switch (type) {
1698 case Primitive::kPrimLong: {
1699 jlong long_arg;
1700 if (IsSplitLongOrDouble()) {
1701 long_arg = ReadSplitLongParam();
1702 } else {
1703 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1704 }
1705 sm_.AdvanceLong(long_arg);
1706 break;
1707 }
1708 case Primitive::kPrimDouble: {
1709 uint64_t double_arg;
1710 if (IsSplitLongOrDouble()) {
1711 // Read into union so that we don't case to a double.
1712 double_arg = ReadSplitLongParam();
1713 } else {
1714 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1715 }
1716 sm_.AdvanceDouble(double_arg);
1717 break;
1718 }
1719 case Primitive::kPrimNot: {
1720 StackReference<mirror::Object>* stack_ref =
1721 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001722 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001723 break;
1724 }
1725 case Primitive::kPrimFloat:
1726 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1727 break;
1728 case Primitive::kPrimBoolean: // Fall-through.
1729 case Primitive::kPrimByte: // Fall-through.
1730 case Primitive::kPrimChar: // Fall-through.
1731 case Primitive::kPrimShort: // Fall-through.
1732 case Primitive::kPrimInt: // Fall-through.
1733 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1734 break;
1735 case Primitive::kPrimVoid:
1736 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001737 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001738 }
1739}
1740
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001741void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001742 // Clear out rest of the scope.
1743 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001744 // Install HandleScope.
1745 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001746}
1747
Ian Rogers04c31d22014-07-07 21:44:06 -07001748#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001749extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001750#else
1751extern "C" void* artFindNativeMethod(Thread* self);
1752#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001753
Andreas Gampead615172014-04-04 16:20:13 -07001754uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1755 if (lock != nullptr) {
1756 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1757 } else {
1758 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1759 }
1760}
1761
1762void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1763 if (lock != nullptr) {
1764 JniMethodEndSynchronized(cookie, lock, self);
1765 } else {
1766 JniMethodEnd(cookie, self);
1767 }
1768}
1769
Andreas Gampec147b002014-03-06 18:11:06 -08001770/*
1771 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001772 * 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 -08001773 * The final element on the stack is a pointer to the native code.
1774 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001775 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001776 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001777 *
Andreas Gampec147b002014-03-06 18:11:06 -08001778 * The return of this function denotes:
1779 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1780 * 2) An error, if the value is negative.
1781 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1783 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001784 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001785 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001786 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001787 uint32_t shorty_len = 0;
1788 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001789
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001790 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001791 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001792 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001793 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001794
Andreas Gampec200a4a2014-06-16 18:39:09 -07001795 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001796 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001797
Ian Rogerse0dcd462014-03-08 15:21:04 -08001798 self->VerifyStack();
1799
Andreas Gampe90546832014-03-12 18:07:19 -07001800 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001801 uint32_t cookie;
1802 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001803 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001804 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001805 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001806 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001807 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001808 }
1809 } else {
1810 cookie = JniMethodStart(self);
1811 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001812 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001813 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001814
Andreas Gampe90546832014-03-12 18:07:19 -07001815 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001816 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001817
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001818 // There are two cases for the content of nativeCode:
1819 // 1) Pointer to the native function.
1820 // 2) Pointer to the trampoline for native code binding.
1821 // In the second case, we need to execute the binding and continue with the actual native function
1822 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001823 DCHECK(nativeCode != nullptr);
1824 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001825#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001826 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001827#else
1828 nativeCode = artFindNativeMethod(self);
1829#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001830
1831 if (nativeCode == nullptr) {
1832 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001833
1834 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001835 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001836 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001837 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1838 } else {
1839 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1840 }
1841
Andreas Gampec200a4a2014-06-16 18:39:09 -07001842 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001843 }
1844 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001845 }
1846
Andreas Gampec200a4a2014-06-16 18:39:09 -07001847 // Return native code addr(lo) and bottom of alloca address(hi).
1848 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1849 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001850}
1851
1852/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001853 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001854 * unlocking.
1855 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001856extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001857 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001858 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001859 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001860 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001861 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001862
Andreas Gampead615172014-04-04 16:20:13 -07001863 jobject lock = nullptr;
1864 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001865 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1866 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001867 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001868 }
1869
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001870 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001871
1872 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001873 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001874 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001875 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001876
1877 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001878 case 'F': {
1879 if (kRuntimeISA == kX86) {
1880 // Convert back the result to float.
1881 double d = bit_cast<uint64_t, double>(result_f);
1882 return bit_cast<float, uint32_t>(static_cast<float>(d));
1883 } else {
1884 return result_f;
1885 }
1886 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001887 case 'D':
1888 return result_f;
1889 case 'Z':
1890 return result.z;
1891 case 'B':
1892 return result.b;
1893 case 'C':
1894 return result.c;
1895 case 'S':
1896 return result.s;
1897 case 'I':
1898 return result.i;
1899 case 'J':
1900 return result.j;
1901 case 'V':
1902 return 0;
1903 default:
1904 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1905 return 0;
1906 }
1907 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001908}
1909
Andreas Gamped58342c2014-06-05 14:18:08 -07001910// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1911// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001912//
Andreas Gamped58342c2014-06-05 14:18:08 -07001913// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1914// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001915
1916template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001917static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001918 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001919 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001920
1921template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001922static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001923 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001924 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001925 ScopedQuickEntrypointChecks sqec(self);
1926 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001927 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1928 type);
1929 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001930 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1931 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001932 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001933 {
1934 // Remember the args in case a GC happens in FindMethodFromCode.
1935 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1936 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1937 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001938 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1939 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001940 visitor.FixupReferences();
1941 }
1942
Ian Rogerse0a02da2014-12-02 14:10:53 -08001943 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001944 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001945 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001946 }
1947 }
1948 DCHECK(!self->IsExceptionPending());
1949 const void* code = method->GetEntryPointFromQuickCompiledCode();
1950
1951 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001952 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001953 << " location: "
1954 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001955
Andreas Gamped58342c2014-06-05 14:18:08 -07001956 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1957 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001958}
1959
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001960// Explicit artInvokeCommon template function declarations to please analysis tool.
1961#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1962 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001963 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001964 mirror::Object* this_object, \
1965 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001966 Thread* self, \
1967 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001968
1969EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1970EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1971EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1972EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1973EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1974EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1975EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1976EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1977EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1978EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1979#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1980
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001981// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001982extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1983 uint32_t method_idx, mirror::Object* this_object,
1984 mirror::ArtMethod* caller_method, Thread* self,
1985 StackReference<mirror::ArtMethod>* sp)
1986 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1987 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1988 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001989}
1990
Andreas Gampec200a4a2014-06-16 18:39:09 -07001991extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1992 uint32_t method_idx, mirror::Object* this_object,
1993 mirror::ArtMethod* caller_method, Thread* self,
1994 StackReference<mirror::ArtMethod>* sp)
1995 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1996 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1997 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001998}
1999
Andreas Gampec200a4a2014-06-16 18:39:09 -07002000extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
2001 uint32_t method_idx, mirror::Object* this_object,
2002 mirror::ArtMethod* caller_method, Thread* self,
2003 StackReference<mirror::ArtMethod>* sp)
2004 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2005 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
2006 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002007}
2008
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
2010 uint32_t method_idx, mirror::Object* this_object,
2011 mirror::ArtMethod* caller_method, Thread* self,
2012 StackReference<mirror::ArtMethod>* sp)
2013 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2014 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
2015 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002016}
2017
Andreas Gampec200a4a2014-06-16 18:39:09 -07002018extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
2019 uint32_t method_idx, mirror::Object* this_object,
2020 mirror::ArtMethod* caller_method, Thread* self,
2021 StackReference<mirror::ArtMethod>* sp)
2022 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2023 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
2024 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002025}
2026
2027// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07002028extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07002029 mirror::Object* this_object,
2030 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002031 Thread* self,
2032 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002034 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002035 mirror::ArtMethod* method;
2036 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2037 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002038 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002039 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2040 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002041 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002042 }
2043 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002044 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002045
2046 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002047 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07002048 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002049
2050 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002051 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002052 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002053 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2054 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2055 Instruction::Code instr_code = instr->Opcode();
2056 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2057 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08002058 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002059 uint32_t dex_method_idx;
2060 if (instr_code == Instruction::INVOKE_INTERFACE) {
2061 dex_method_idx = instr->VRegB_35c();
2062 } else {
2063 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2064 dex_method_idx = instr->VRegB_3rc();
2065 }
2066
Andreas Gampec200a4a2014-06-16 18:39:09 -07002067 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2068 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002069 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002070 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2071 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002072 {
2073 // Remember the args in case a GC happens in FindMethodFromCode.
2074 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2075 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2076 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002077 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002078 self);
2079 visitor.FixupReferences();
2080 }
2081
2082 if (UNLIKELY(method == nullptr)) {
2083 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002084 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002085 }
2086 }
2087 const void* code = method->GetEntryPointFromQuickCompiledCode();
2088
2089 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002090 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002091 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002092
Andreas Gamped58342c2014-06-05 14:18:08 -07002093 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2094 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002095}
2096
Ian Rogers848871b2013-08-05 10:56:33 -07002097} // namespace art