blob: 70ee04246a51fad3047689163aa94013c827ec80 [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
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800157 // | F4 | f_arg4
158 // | F3 | f_arg3
159 // | F2 | f_arg2
160 // | F1 | f_arg1
161 // | F0 | f_arg0
162 // | A7 | arg7
163 // | A6 | arg6
164 // | A5 | arg5
165 // | A4 | arg4
166 // | A3 | arg3
167 // | A2 | arg2
168 // | A1 | arg1
169 // | | padding
170 // | A0/Method* | <- sp
171 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800172 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800173 static constexpr bool kAlignPairRegister = false;
174 static constexpr bool kQuickSoftFloatAbi = false;
175 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
176 // These values are set to zeros because GPR and FPR register
177 // assignments for Mips64 are interleaved, which the current VisitArguments()
178 // function does not support.
179 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
180 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
181 static constexpr bool kGprFprLockstep = true;
182
183 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
186 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
187 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
188 }
Ian Rogers848871b2013-08-05 10:56:33 -0700189#elif defined(__i386__)
190 // The callee save frame is pointed to by SP.
191 // | argN | |
192 // | ... | |
193 // | arg4 | |
194 // | arg3 spill | | Caller's frame
195 // | arg2 spill | |
196 // | arg1 spill | |
197 // | Method* | ---
198 // | Return |
199 // | EBP,ESI,EDI | callee saves
200 // | EBX | arg3
201 // | EDX | arg2
202 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000203 // | XMM3 | float arg 4
204 // | XMM2 | float arg 3
205 // | XMM1 | float arg 2
206 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700207 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500208 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000209 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000210 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800211 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800212 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000213 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800214 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000215 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
216 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
217 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800218 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000219 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800220 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800221#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800222 // The callee save frame is pointed to by SP.
223 // | argN | |
224 // | ... | |
225 // | reg. arg spills | | Caller's frame
226 // | Method* | ---
227 // | Return |
228 // | R15 | callee save
229 // | R14 | callee save
230 // | R13 | callee save
231 // | R12 | callee save
232 // | R9 | arg5
233 // | R8 | arg4
234 // | RSI/R6 | arg1
235 // | RBP/R5 | callee save
236 // | RBX/R3 | callee save
237 // | RDX/R2 | arg2
238 // | RCX/R1 | arg3
239 // | XMM7 | float arg 8
240 // | XMM6 | float arg 7
241 // | XMM5 | float arg 6
242 // | XMM4 | float arg 5
243 // | XMM3 | float arg 4
244 // | XMM2 | float arg 3
245 // | XMM1 | float arg 2
246 // | XMM0 | float arg 1
247 // | Padding |
248 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500249 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000250 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800251 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800252 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700253 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700254 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800255 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800256 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700257 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
260 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000261 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
262 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
263 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
264 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
265 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800266 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700267 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
268 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800269 }
270 }
Ian Rogers848871b2013-08-05 10:56:33 -0700271#else
272#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700273#endif
274
Ian Rogers936b37f2014-02-14 00:52:24 -0800275 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100276 // Special handling for proxy methods. Proxy methods are instance methods so the
277 // 'this' object is the 1st argument. They also have the same frame layout as the
278 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
279 // 1st GPR.
280 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
282 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
283 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
284 CHECK_GT(kNumQuickGprArgs, 0u);
285 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
286 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
287 GprIndexToGprOffset(kThisGprIndex);
288 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
289 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
290 }
291
Andreas Gampecf4035a2014-05-28 22:43:01 -0700292 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700294 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700295 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700296 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700297 }
298
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700300 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700302 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700303 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700304 return *reinterpret_cast<uintptr_t*>(lr);
305 }
306
Andreas Gampec200a4a2014-06-16 18:39:09 -0700307 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
308 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
309 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700310 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
311 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
312 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700313 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800314 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
315 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800316 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
317 "Number of Quick FPR arguments unexpected");
318 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
319 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800320 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
321 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800322 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
323 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800324 }
Ian Rogers848871b2013-08-05 10:56:33 -0700325
326 virtual ~QuickArgumentVisitor() {}
327
328 virtual void Visit() = 0;
329
Ian Rogers936b37f2014-02-14 00:52:24 -0800330 Primitive::Type GetParamPrimitiveType() const {
331 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700332 }
333
Ian Rogers13735952014-10-08 12:43:28 -0700334 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800335 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800336 Primitive::Type type = GetParamPrimitiveType();
337 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800338 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
339 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
340 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
341 }
342 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000343 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800344 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700345 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800346 }
347 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800348 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800349 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
350 }
351 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700352 }
353
354 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000355 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800356 return is_split_long_or_double_;
357 } else {
358 return false; // An optimization for when GPR and FPRs are 64bit.
359 }
Ian Rogers848871b2013-08-05 10:56:33 -0700360 }
361
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700363 return GetParamPrimitiveType() == Primitive::kPrimNot;
364 }
365
Ian Rogers936b37f2014-02-14 00:52:24 -0800366 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700367 Primitive::Type type = GetParamPrimitiveType();
368 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
369 }
370
371 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000372 // The splitted long is always available through the stack.
373 return *reinterpret_cast<uint64_t*>(stack_args_
374 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700375 }
376
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800377 void IncGprIndex() {
378 gpr_index_++;
379 if (kGprFprLockstep) {
380 fpr_index_++;
381 }
382 }
383
384 void IncFprIndex() {
385 fpr_index_++;
386 if (kGprFprLockstep) {
387 gpr_index_++;
388 }
389 }
390
Ian Rogers848871b2013-08-05 10:56:33 -0700391 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800392 // (a) 'stack_args_' should point to the first method's argument
393 // (b) whatever the argument type it is, the 'stack_index_' should
394 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 gpr_index_ = 0;
396 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800397 if (kQuickDoubleRegAlignedFloatBackFilled) {
398 fpr_double_index_ = 0;
399 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 stack_index_ = 0;
401 if (!is_static_) { // Handle this.
402 cur_type_ = Primitive::kPrimNot;
403 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700404 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800405 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800406 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800407 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800408 }
Ian Rogers848871b2013-08-05 10:56:33 -0700409 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800410 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
411 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
412 switch (cur_type_) {
413 case Primitive::kPrimNot:
414 case Primitive::kPrimBoolean:
415 case Primitive::kPrimByte:
416 case Primitive::kPrimChar:
417 case Primitive::kPrimShort:
418 case Primitive::kPrimInt:
419 is_split_long_or_double_ = false;
420 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800422 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800423 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800424 }
425 break;
426 case Primitive::kPrimFloat:
427 is_split_long_or_double_ = false;
428 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800429 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800430 if (kQuickSoftFloatAbi) {
431 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800432 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 }
434 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800435 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800436 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800437 if (kQuickDoubleRegAlignedFloatBackFilled) {
438 // Double should not overlap with float.
439 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
440 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
441 // Float should not overlap with double.
442 if (fpr_index_ % 2 == 0) {
443 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
444 }
445 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 }
447 }
448 break;
449 case Primitive::kPrimDouble:
450 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800451 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000452 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
453 // Currently, this is only for ARM, where the first available parameter register
454 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800455 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000456 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000457 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800458 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500459 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
460 // We don't want to split this. Pass over this register.
461 gpr_index_++;
462 is_split_long_or_double_ = false;
463 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800464 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800465 if (kBytesStackArgLocation == 4) {
466 stack_index_+= 2;
467 } else {
468 CHECK_EQ(kBytesStackArgLocation, 8U);
469 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800470 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700471 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800472 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000473 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700474 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800475 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700476 }
477 }
478 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800479 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000480 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800481 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800482 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700483 if (kBytesStackArgLocation == 4) {
484 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800485 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700486 CHECK_EQ(kBytesStackArgLocation, 8U);
487 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800488 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 if (kQuickDoubleRegAlignedFloatBackFilled) {
490 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
491 fpr_double_index_ += 2;
492 // Float should not overlap with double.
493 if (fpr_index_ % 2 == 0) {
494 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
495 }
496 }
497 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800498 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800499 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
500 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800501 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800502 }
503 }
504 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800505 }
506 break;
507 default:
508 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
509 }
Ian Rogers848871b2013-08-05 10:56:33 -0700510 }
511 }
512
Andreas Gampec200a4a2014-06-16 18:39:09 -0700513 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700514 const bool is_static_;
515 const char* const shorty_;
516 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700517
518 private:
Ian Rogers13735952014-10-08 12:43:28 -0700519 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
520 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
521 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800522 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800523 // Index into spilled FPRs.
524 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
525 // holds a higher register number.
526 uint32_t fpr_index_;
527 // Index into spilled FPRs for aligned double.
528 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
529 // terms of singles, may be behind fpr_index.
530 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800531 uint32_t stack_index_; // Index into arguments on the stack.
532 // The current type of argument during VisitArguments.
533 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700534 // Does a 64bit parameter straddle the register and stack arguments?
535 bool is_split_long_or_double_;
536};
537
Sebastien Hertza836bc92014-11-25 16:30:53 +0100538// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
539// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
540extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
542 return QuickArgumentVisitor::GetProxyThisObject(sp);
543}
544
Ian Rogers848871b2013-08-05 10:56:33 -0700545// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800546class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700547 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700548 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
549 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
550 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700551 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700552
Ian Rogers9758f792014-03-13 09:02:55 -0700553 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700554
555 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800556 ShadowFrame* const sf_;
557 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700558
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700559 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700560};
561
Andreas Gampec200a4a2014-06-16 18:39:09 -0700562void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700563 Primitive::Type type = GetParamPrimitiveType();
564 switch (type) {
565 case Primitive::kPrimLong: // Fall-through.
566 case Primitive::kPrimDouble:
567 if (IsSplitLongOrDouble()) {
568 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
569 } else {
570 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
571 }
572 ++cur_reg_;
573 break;
574 case Primitive::kPrimNot: {
575 StackReference<mirror::Object>* stack_ref =
576 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
577 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
578 }
579 break;
580 case Primitive::kPrimBoolean: // Fall-through.
581 case Primitive::kPrimByte: // Fall-through.
582 case Primitive::kPrimChar: // Fall-through.
583 case Primitive::kPrimShort: // Fall-through.
584 case Primitive::kPrimInt: // Fall-through.
585 case Primitive::kPrimFloat:
586 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
587 break;
588 case Primitive::kPrimVoid:
589 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700590 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700591 }
592 ++cur_reg_;
593}
594
Brian Carlstromea46f952013-07-30 01:26:50 -0700595extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700596 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700597 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
598 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
599 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700600 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700601
602 if (method->IsAbstract()) {
603 ThrowAbstractMethodError(method);
604 return 0;
605 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800606 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700607 const char* old_cause = self->StartAssertNoThreadSuspension(
608 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700609 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800610 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700611 uint16_t num_regs = code_item->registers_size_;
612 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700613 // No last shadow coming from quick.
614 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700615 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700616 uint32_t shorty_len = 0;
617 const char* shorty = method->GetShorty(&shorty_len);
618 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800619 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700620 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800621 const bool needs_initialization =
622 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700623 // Push a transition back into managed code onto the linked list in thread.
624 ManagedStack fragment;
625 self->PushManagedStackFragment(&fragment);
626 self->PushShadowFrame(shadow_frame);
627 self->EndAssertNoThreadSuspension(old_cause);
628
Ian Rogerse94652f2014-12-02 11:13:19 -0800629 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700630 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800631 StackHandleScope<1> hs(self);
632 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700633 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800634 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700635 self->PopManagedStackFragment(fragment);
636 return 0;
637 }
638 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800639 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700640 // Pop transition.
641 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800642 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700643 return result.GetJ();
644 }
645}
646
647// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
648// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800649class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700650 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700651 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
652 const char* shorty, uint32_t shorty_len,
653 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700654 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700655
Ian Rogers9758f792014-03-13 09:02:55 -0700656 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700657
Ian Rogers9758f792014-03-13 09:02:55 -0700658 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800659
Ian Rogers848871b2013-08-05 10:56:33 -0700660 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700661 ScopedObjectAccessUnchecked* const soa_;
662 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800663 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700664 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700665
Ian Rogers848871b2013-08-05 10:56:33 -0700666 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
667};
668
Ian Rogers9758f792014-03-13 09:02:55 -0700669void BuildQuickArgumentVisitor::Visit() {
670 jvalue val;
671 Primitive::Type type = GetParamPrimitiveType();
672 switch (type) {
673 case Primitive::kPrimNot: {
674 StackReference<mirror::Object>* stack_ref =
675 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
676 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
677 references_.push_back(std::make_pair(val.l, stack_ref));
678 break;
679 }
680 case Primitive::kPrimLong: // Fall-through.
681 case Primitive::kPrimDouble:
682 if (IsSplitLongOrDouble()) {
683 val.j = ReadSplitLongParam();
684 } else {
685 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
686 }
687 break;
688 case Primitive::kPrimBoolean: // Fall-through.
689 case Primitive::kPrimByte: // Fall-through.
690 case Primitive::kPrimChar: // Fall-through.
691 case Primitive::kPrimShort: // Fall-through.
692 case Primitive::kPrimInt: // Fall-through.
693 case Primitive::kPrimFloat:
694 val.i = *reinterpret_cast<jint*>(GetParamAddress());
695 break;
696 case Primitive::kPrimVoid:
697 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700698 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700699 }
700 args_->push_back(val);
701}
702
703void BuildQuickArgumentVisitor::FixupReferences() {
704 // Fixup any references which may have changed.
705 for (const auto& pair : references_) {
706 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700707 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700708 }
709}
710
Ian Rogers848871b2013-08-05 10:56:33 -0700711// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
712// which is responsible for recording callee save registers. We explicitly place into jobjects the
713// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
714// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700715extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700716 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700717 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700718 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700719 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
720 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700721 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
722 const char* old_cause =
723 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
724 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700725 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700726 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700727 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
728 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700729 self->VerifyStack();
730 // Start new JNI local reference state.
731 JNIEnvExt* env = self->GetJniEnv();
732 ScopedObjectAccessUnchecked soa(env);
733 ScopedJniEnvLocalRefState env_state(env);
734 // Create local ref. copies of proxy method and the receiver.
735 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
736
737 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700738 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
739 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700740 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700741 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700742 uint32_t shorty_len = 0;
743 const char* shorty = proxy_method->GetShorty(&shorty_len);
744 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700745
Ian Rogers848871b2013-08-05 10:56:33 -0700746 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700747 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700748 args.erase(args.begin());
749
750 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700751 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800752 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700753 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
754 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
755
756 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
757 // that performs allocations.
758 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700759 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800760 // Restore references which might have moved.
761 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700762 return result.GetJ();
763}
764
765// Read object references held in arguments from quick frames and place in a JNI local references,
766// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800767class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700768 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700769 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
770 const char* shorty, uint32_t shorty_len,
771 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700772 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700773
Ian Rogers9758f792014-03-13 09:02:55 -0700774 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700775
Ian Rogers9758f792014-03-13 09:02:55 -0700776 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700777
778 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700779 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800780 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700781 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
782
Mathieu Chartier590fee92013-09-13 13:46:47 -0700783 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700784};
785
Ian Rogers9758f792014-03-13 09:02:55 -0700786void RememberForGcArgumentVisitor::Visit() {
787 if (IsParamAReference()) {
788 StackReference<mirror::Object>* stack_ref =
789 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
790 jobject reference =
791 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
792 references_.push_back(std::make_pair(reference, stack_ref));
793 }
794}
795
796void RememberForGcArgumentVisitor::FixupReferences() {
797 // Fixup any references which may have changed.
798 for (const auto& pair : references_) {
799 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700800 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700801 }
802}
803
Ian Rogers848871b2013-08-05 10:56:33 -0700804// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700805extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700806 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700807 Thread* self,
808 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700809 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700810 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700811 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800812 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700813 ScopedObjectAccessUnchecked soa(env);
814 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800815 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700816
817 // Compute details about the called method (avoid GCs)
818 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700819 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700820 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800821 MethodReference called_method(nullptr, 0);
822 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
823 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700824 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
825 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800826 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700827 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700828 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
829 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
830 Instruction::Code instr_code = instr->Opcode();
831 bool is_range;
832 switch (instr_code) {
833 case Instruction::INVOKE_DIRECT:
834 invoke_type = kDirect;
835 is_range = false;
836 break;
837 case Instruction::INVOKE_DIRECT_RANGE:
838 invoke_type = kDirect;
839 is_range = true;
840 break;
841 case Instruction::INVOKE_STATIC:
842 invoke_type = kStatic;
843 is_range = false;
844 break;
845 case Instruction::INVOKE_STATIC_RANGE:
846 invoke_type = kStatic;
847 is_range = true;
848 break;
849 case Instruction::INVOKE_SUPER:
850 invoke_type = kSuper;
851 is_range = false;
852 break;
853 case Instruction::INVOKE_SUPER_RANGE:
854 invoke_type = kSuper;
855 is_range = true;
856 break;
857 case Instruction::INVOKE_VIRTUAL:
858 invoke_type = kVirtual;
859 is_range = false;
860 break;
861 case Instruction::INVOKE_VIRTUAL_RANGE:
862 invoke_type = kVirtual;
863 is_range = true;
864 break;
865 case Instruction::INVOKE_INTERFACE:
866 invoke_type = kInterface;
867 is_range = false;
868 break;
869 case Instruction::INVOKE_INTERFACE_RANGE:
870 invoke_type = kInterface;
871 is_range = true;
872 break;
873 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800874 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
875 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700876 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800877 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700878 } else {
879 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800880 called_method.dex_file = called->GetDexFile();
881 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700882 }
883 uint32_t shorty_len;
884 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800885 called_method.dex_file->GetMethodShorty(
886 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700887 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700888 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800889 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800890 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700891 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800892 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700893 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700894 mirror::Object* dummy = nullptr;
895 HandleWrapper<mirror::Object> h_receiver(
896 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800897 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
898 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700899 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800900 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800901 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700902 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800903 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
904 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800905 if (virtual_or_interface) {
906 // Refine called method based on receiver.
907 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700908
909 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800910 if (invoke_type == kVirtual) {
911 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
912 } else {
913 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
914 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700915
916 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
917 << PrettyTypeOf(receiver) << " "
918 << invoke_type << " " << orig_called->GetVtableIndex();
919
Ian Rogers83883d72013-10-21 21:07:24 -0700920 // 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 -0800921 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800922 // Note, called_method.dex_method_index references the dex method before the
923 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
924 // about the name and signature.
925 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800926 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700927 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000928 // the caller's dex file. Since we get here only if the original called was a runtime
929 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800930 DCHECK(!called_method_known_on_entry);
931 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
932 const DexFile* caller_dex_file = called_method.dex_file;
933 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
934 update_dex_cache_method_index =
935 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
936 caller_method_name_and_sig_index);
937 }
938 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
939 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
940 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700941 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800942 } else if (invoke_type == kStatic) {
943 const auto called_dex_method_idx = called->GetDexMethodIndex();
944 // For static invokes, we may dispatch to the static method in the superclass but resolve
945 // using the subclass. To prevent getting slow paths on each invoke, we force set the
946 // resolved method for the super class dex method index if we are in the same dex file.
947 // b/19175856
948 if (called->GetDexFile() == called_method.dex_file &&
949 called_method.dex_method_index != called_dex_method_idx) {
950 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called);
951 }
Ian Rogers83883d72013-10-21 21:07:24 -0700952 }
Ian Rogers848871b2013-08-05 10:56:33 -0700953 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700954 StackHandleScope<1> hs(soa.Self());
955 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700956 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700957 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800958 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700959 } else if (called_class->IsInitializing()) {
960 if (invoke_type == kStatic) {
961 // Class is still initializing, go to oat and grab code (trampoline must be left in place
962 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800963 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700964 } else {
965 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800966 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700967 }
968 } else {
969 DCHECK(called_class->IsErroneous());
970 }
971 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800972 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700973 // Fixup any locally saved objects may have moved during a GC.
974 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700975 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700976 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700977 return code;
978}
979
Andreas Gampec147b002014-03-06 18:11:06 -0800980/*
981 * This class uses a couple of observations to unite the different calling conventions through
982 * a few constants.
983 *
984 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
985 * possible alignment.
986 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
987 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
988 * when we have to split things
989 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
990 * and we can use Int handling directly.
991 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
992 * necessary when widening. Also, widening of Ints will take place implicitly, and the
993 * extension should be compatible with Aarch64, which mandates copying the available bits
994 * into LSB and leaving the rest unspecified.
995 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
996 * the stack.
997 * 6) There is only little endian.
998 *
999 *
1000 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1001 * follows:
1002 *
1003 * void PushGpr(uintptr_t): Add a value for the next GPR
1004 *
1005 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1006 * padding, that is, think the architecture is 32b and aligns 64b.
1007 *
1008 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1009 * split this if necessary. The current state will have aligned, if
1010 * necessary.
1011 *
1012 * void PushStack(uintptr_t): Push a value to the stack.
1013 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001014 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001015 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001016 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001017 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001018 *
1019 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001020template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001021 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001022#if defined(__arm__)
1023 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001024 static constexpr bool kNativeSoftFloatAbi = true;
1025 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001026 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1027
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001028 static constexpr size_t kRegistersNeededForLong = 2;
1029 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001030 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001031 static constexpr bool kMultiFPRegistersWidened = false;
1032 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001033 static constexpr bool kAlignLongOnStack = true;
1034 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001035#elif defined(__aarch64__)
1036 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1037 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1038 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1039
1040 static constexpr size_t kRegistersNeededForLong = 1;
1041 static constexpr size_t kRegistersNeededForDouble = 1;
1042 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001043 static constexpr bool kMultiFPRegistersWidened = false;
1044 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001045 static constexpr bool kAlignLongOnStack = false;
1046 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001047#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001048 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001049 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1050 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001051
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001052 static constexpr size_t kRegistersNeededForLong = 2;
1053 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001054 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001055 static constexpr bool kMultiFPRegistersWidened = true;
1056 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001057 static constexpr bool kAlignLongOnStack = true;
1058 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001059#elif defined(__mips__) && defined(__LP64__)
1060 // Let the code prepare GPRs only and we will load the FPRs with same data.
1061 static constexpr bool kNativeSoftFloatAbi = true;
1062 static constexpr size_t kNumNativeGprArgs = 8;
1063 static constexpr size_t kNumNativeFprArgs = 0;
1064
1065 static constexpr size_t kRegistersNeededForLong = 1;
1066 static constexpr size_t kRegistersNeededForDouble = 1;
1067 static constexpr bool kMultiRegistersAligned = false;
1068 static constexpr bool kMultiFPRegistersWidened = false;
1069 static constexpr bool kMultiGPRegistersWidened = true;
1070 static constexpr bool kAlignLongOnStack = false;
1071 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001072#elif defined(__i386__)
1073 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001074 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001075 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1076 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1077
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001078 static constexpr size_t kRegistersNeededForLong = 2;
1079 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001080 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001081 static constexpr bool kMultiFPRegistersWidened = false;
1082 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001083 static constexpr bool kAlignLongOnStack = false;
1084 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001085#elif defined(__x86_64__)
1086 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1087 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1088 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1089
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001090 static constexpr size_t kRegistersNeededForLong = 1;
1091 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001092 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001093 static constexpr bool kMultiFPRegistersWidened = false;
1094 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001095 static constexpr bool kAlignLongOnStack = false;
1096 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001097#else
1098#error "Unsupported architecture"
1099#endif
1100
Andreas Gampec147b002014-03-06 18:11:06 -08001101 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001102 explicit BuildNativeCallFrameStateMachine(T* delegate)
1103 : gpr_index_(kNumNativeGprArgs),
1104 fpr_index_(kNumNativeFprArgs),
1105 stack_entries_(0),
1106 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001107 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1108 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001109 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1110 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001111 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001112
Andreas Gampec200a4a2014-06-16 18:39:09 -07001113 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001114
Ian Rogers1428dce2014-10-21 15:02:15 -07001115 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001116 return gpr_index_ > 0;
1117 }
1118
Andreas Gampec200a4a2014-06-16 18:39:09 -07001119 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001120 if (HavePointerGpr()) {
1121 gpr_index_--;
1122 PushGpr(reinterpret_cast<uintptr_t>(val));
1123 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001124 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001125 PushStack(reinterpret_cast<uintptr_t>(val));
1126 gpr_index_ = 0;
1127 }
1128 }
1129
Ian Rogers1428dce2014-10-21 15:02:15 -07001130 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001131 return gpr_index_ > 0;
1132 }
1133
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001134 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1135 uintptr_t handle = PushHandle(ptr);
1136 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001137 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001138 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001139 } else {
1140 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001141 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001142 gpr_index_ = 0;
1143 }
1144 }
1145
Ian Rogers1428dce2014-10-21 15:02:15 -07001146 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001147 return gpr_index_ > 0;
1148 }
1149
1150 void AdvanceInt(uint32_t val) {
1151 if (HaveIntGpr()) {
1152 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001153 if (kMultiGPRegistersWidened) {
1154 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1155 PushGpr(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1156 } else {
1157 PushGpr(val);
1158 }
Andreas Gampec147b002014-03-06 18:11:06 -08001159 } else {
1160 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001161 if (kMultiGPRegistersWidened) {
1162 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
1163 PushStack(static_cast<int64_t>(bit_cast<uint32_t, int32_t>(val)));
1164 } else {
1165 PushStack(val);
1166 }
Andreas Gampec147b002014-03-06 18:11:06 -08001167 gpr_index_ = 0;
1168 }
1169 }
1170
Ian Rogers1428dce2014-10-21 15:02:15 -07001171 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001172 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1173 }
1174
Ian Rogers1428dce2014-10-21 15:02:15 -07001175 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001176 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1177 kAlignLongOnStack && // and when it needs alignment
1178 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1179 }
1180
Ian Rogers1428dce2014-10-21 15:02:15 -07001181 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001182 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1183 kAlignLongOnStack && // and when it needs 8B alignment
1184 (stack_entries_ & 1) == 1; // counter is odd
1185 }
1186
1187 void AdvanceLong(uint64_t val) {
1188 if (HaveLongGpr()) {
1189 if (LongGprNeedsPadding()) {
1190 PushGpr(0);
1191 gpr_index_--;
1192 }
1193 if (kRegistersNeededForLong == 1) {
1194 PushGpr(static_cast<uintptr_t>(val));
1195 } else {
1196 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1197 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1198 }
1199 gpr_index_ -= kRegistersNeededForLong;
1200 } else {
1201 if (LongStackNeedsPadding()) {
1202 PushStack(0);
1203 stack_entries_++;
1204 }
1205 if (kRegistersNeededForLong == 1) {
1206 PushStack(static_cast<uintptr_t>(val));
1207 stack_entries_++;
1208 } else {
1209 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1210 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1211 stack_entries_ += 2;
1212 }
1213 gpr_index_ = 0;
1214 }
1215 }
1216
Ian Rogers1428dce2014-10-21 15:02:15 -07001217 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001218 return fpr_index_ > 0;
1219 }
1220
Andreas Gampec147b002014-03-06 18:11:06 -08001221 void AdvanceFloat(float val) {
1222 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001223 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001224 } else {
1225 if (HaveFloatFpr()) {
1226 fpr_index_--;
1227 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001228 if (kMultiFPRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001229 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001230 } else {
1231 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001232 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001233 }
1234 } else {
1235 PushFpr4(val);
1236 }
1237 } else {
1238 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001239 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001240 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001241 // Note: We need to jump through those hoops to make the compiler happy.
1242 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1243 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001244 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001245 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001246 }
1247 fpr_index_ = 0;
1248 }
1249 }
1250 }
1251
Ian Rogers1428dce2014-10-21 15:02:15 -07001252 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001253 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1254 }
1255
Ian Rogers1428dce2014-10-21 15:02:15 -07001256 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001257 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1258 kAlignDoubleOnStack && // and when it needs alignment
1259 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1260 }
1261
Ian Rogers1428dce2014-10-21 15:02:15 -07001262 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001263 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1264 kAlignDoubleOnStack && // and when it needs 8B alignment
1265 (stack_entries_ & 1) == 1; // counter is odd
1266 }
1267
1268 void AdvanceDouble(uint64_t val) {
1269 if (kNativeSoftFloatAbi) {
1270 AdvanceLong(val);
1271 } else {
1272 if (HaveDoubleFpr()) {
1273 if (DoubleFprNeedsPadding()) {
1274 PushFpr4(0);
1275 fpr_index_--;
1276 }
1277 PushFpr8(val);
1278 fpr_index_ -= kRegistersNeededForDouble;
1279 } else {
1280 if (DoubleStackNeedsPadding()) {
1281 PushStack(0);
1282 stack_entries_++;
1283 }
1284 if (kRegistersNeededForDouble == 1) {
1285 PushStack(static_cast<uintptr_t>(val));
1286 stack_entries_++;
1287 } else {
1288 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1289 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1290 stack_entries_ += 2;
1291 }
1292 fpr_index_ = 0;
1293 }
1294 }
1295 }
1296
Ian Rogers1428dce2014-10-21 15:02:15 -07001297 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001298 return stack_entries_;
1299 }
1300
Ian Rogers1428dce2014-10-21 15:02:15 -07001301 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001302 return kNumNativeGprArgs - gpr_index_;
1303 }
1304
Ian Rogers1428dce2014-10-21 15:02:15 -07001305 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001306 return kNumNativeFprArgs - fpr_index_;
1307 }
1308
1309 private:
1310 void PushGpr(uintptr_t val) {
1311 delegate_->PushGpr(val);
1312 }
1313 void PushFpr4(float val) {
1314 delegate_->PushFpr4(val);
1315 }
1316 void PushFpr8(uint64_t val) {
1317 delegate_->PushFpr8(val);
1318 }
1319 void PushStack(uintptr_t val) {
1320 delegate_->PushStack(val);
1321 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001322 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1323 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001324 }
1325
1326 uint32_t gpr_index_; // Number of free GPRs
1327 uint32_t fpr_index_; // Number of free FPRs
1328 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1329 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001330 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001331};
1332
Andreas Gampec200a4a2014-06-16 18:39:09 -07001333// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1334// in subclasses.
1335//
1336// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1337// them with handles.
1338class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001339 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001340 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1341
1342 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001343
Ian Rogers1428dce2014-10-21 15:02:15 -07001344 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001345 return num_stack_entries_ * sizeof(uintptr_t);
1346 }
1347
Ian Rogers1428dce2014-10-21 15:02:15 -07001348 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001349 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001350 // Align by kStackAlignment.
1351 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001352 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001353 }
1354
Ian Rogers1428dce2014-10-21 15:02:15 -07001355 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1356 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001357 // Assumption is OK right now, as we have soft-float arm
1358 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1359 sp8 -= fregs * sizeof(uintptr_t);
1360 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1361 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1362 sp8 -= iregs * sizeof(uintptr_t);
1363 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1364 return sp8;
1365 }
Andreas Gampec147b002014-03-06 18:11:06 -08001366
Andreas Gampec200a4a2014-06-16 18:39:09 -07001367 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001368 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001369 // Native call stack.
1370 sp8 = LayoutCallStack(sp8);
1371 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001372
Andreas Gampec200a4a2014-06-16 18:39:09 -07001373 // Put fprs and gprs below.
1374 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001375
Andreas Gampec200a4a2014-06-16 18:39:09 -07001376 // Return the new bottom.
1377 return sp8;
1378 }
1379
1380 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001381 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1382 UNUSED(sm);
1383 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001384
1385 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1386 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1387
1388 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001389
1390 for (uint32_t i = 1; i < shorty_len; ++i) {
1391 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1392 switch (cur_type_) {
1393 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001394 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001395 sm.AdvanceHandleScope(
1396 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001397 break;
1398
1399 case Primitive::kPrimBoolean:
1400 case Primitive::kPrimByte:
1401 case Primitive::kPrimChar:
1402 case Primitive::kPrimShort:
1403 case Primitive::kPrimInt:
1404 sm.AdvanceInt(0);
1405 break;
1406 case Primitive::kPrimFloat:
1407 sm.AdvanceFloat(0);
1408 break;
1409 case Primitive::kPrimDouble:
1410 sm.AdvanceDouble(0);
1411 break;
1412 case Primitive::kPrimLong:
1413 sm.AdvanceLong(0);
1414 break;
1415 default:
1416 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001417 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001418 }
1419 }
1420
Ian Rogers1428dce2014-10-21 15:02:15 -07001421 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001422 }
1423
1424 void PushGpr(uintptr_t /* val */) {
1425 // not optimizing registers, yet
1426 }
1427
1428 void PushFpr4(float /* val */) {
1429 // not optimizing registers, yet
1430 }
1431
1432 void PushFpr8(uint64_t /* val */) {
1433 // not optimizing registers, yet
1434 }
1435
1436 void PushStack(uintptr_t /* val */) {
1437 // counting is already done in the superclass
1438 }
1439
Andreas Gampec200a4a2014-06-16 18:39:09 -07001440 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001441 return reinterpret_cast<uintptr_t>(nullptr);
1442 }
1443
Andreas Gampec200a4a2014-06-16 18:39:09 -07001444 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001445 uint32_t num_stack_entries_;
1446};
1447
Andreas Gampec200a4a2014-06-16 18:39:09 -07001448class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001449 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001450 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001451
Andreas Gampec200a4a2014-06-16 18:39:09 -07001452 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1453 // is at *m = sp. Will update to point to the bottom of the save frame.
1454 //
1455 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001456 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1457 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001458 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1459 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001460
Andreas Gampec200a4a2014-06-16 18:39:09 -07001461 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1462
1463 // First, fix up the layout of the callee-save frame.
1464 // We have to squeeze in the HandleScope, and relocate the method pointer.
1465
1466 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001467 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001468
1469 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001470 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1471 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1472
1473 sp8 -= scope_and_method;
1474 // Align by kStackAlignment.
1475 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1476 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1477
1478 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001479 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1480 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001481
1482 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1483 uint8_t* method_pointer = sp8;
1484 StackReference<mirror::ArtMethod>* new_method_ref =
1485 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1486 new_method_ref->Assign(method);
1487 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001488 }
1489
Andreas Gampec200a4a2014-06-16 18:39:09 -07001490 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001492 // Reference cookie and padding
1493 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001494 }
1495
Andreas Gampec200a4a2014-06-16 18:39:09 -07001496 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1497 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001498 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1499 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001501 // First, fix up the layout of the callee-save frame.
1502 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001503 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001504
1505 // The bottom of the callee-save frame is now where the method is, *m.
1506 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1507
1508 // Add space for cookie.
1509 LayoutCookie(&sp8);
1510
1511 return sp8;
1512 }
1513
1514 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001515 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001516 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001517 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1519 Walk(shorty, shorty_len);
1520
1521 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001522 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001523
1524 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1525
1526 // Return the new bottom.
1527 return sp8;
1528 }
1529
1530 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1531
1532 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1533 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1534 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1535
1536 private:
1537 uint32_t num_handle_scope_references_;
1538};
1539
1540uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1541 num_handle_scope_references_++;
1542 return reinterpret_cast<uintptr_t>(nullptr);
1543}
1544
1545void ComputeGenericJniFrameSize::WalkHeader(
1546 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1547 // JNIEnv
1548 sm->AdvancePointer(nullptr);
1549
1550 // Class object or this as first argument
1551 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1552}
1553
1554// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1555// the template requirements of BuildGenericJniFrameStateMachine.
1556class FillNativeCall {
1557 public:
1558 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1559 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1560
1561 virtual ~FillNativeCall() {}
1562
1563 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1564 cur_gpr_reg_ = gpr_regs;
1565 cur_fpr_reg_ = fpr_regs;
1566 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001567 }
1568
1569 void PushGpr(uintptr_t val) {
1570 *cur_gpr_reg_ = val;
1571 cur_gpr_reg_++;
1572 }
1573
1574 void PushFpr4(float val) {
1575 *cur_fpr_reg_ = val;
1576 cur_fpr_reg_++;
1577 }
1578
1579 void PushFpr8(uint64_t val) {
1580 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1581 *tmp = val;
1582 cur_fpr_reg_ += 2;
1583 }
1584
1585 void PushStack(uintptr_t val) {
1586 *cur_stack_arg_ = val;
1587 cur_stack_arg_++;
1588 }
1589
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001590 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001591 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001592 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001593 }
1594
1595 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001596 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001597 uint32_t* cur_fpr_reg_;
1598 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001599};
Andreas Gampec147b002014-03-06 18:11:06 -08001600
Andreas Gampec200a4a2014-06-16 18:39:09 -07001601// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1602// of transitioning into native code.
1603class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1604 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001605 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1606 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001607 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1608 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1609 ComputeGenericJniFrameSize fsc;
1610 uintptr_t* start_gpr_reg;
1611 uint32_t* start_fpr_reg;
1612 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001613 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001614 &handle_scope_,
1615 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001616 &start_gpr_reg, &start_fpr_reg);
1617
Andreas Gampec200a4a2014-06-16 18:39:09 -07001618 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1619
1620 // jni environment is always first argument
1621 sm_.AdvancePointer(self->GetJniEnv());
1622
1623 if (is_static) {
1624 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1625 }
1626 }
1627
1628 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1629
1630 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1631
1632 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1633 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1634 return handle_scope_->GetHandle(0).GetReference();
1635 }
1636
Ian Rogers1428dce2014-10-21 15:02:15 -07001637 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001638 return handle_scope_->GetHandle(0).ToJObject();
1639 }
1640
Ian Rogers1428dce2014-10-21 15:02:15 -07001641 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 return bottom_of_used_area_;
1643 }
1644
1645 private:
1646 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1647 class FillJniCall FINAL : public FillNativeCall {
1648 public:
1649 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1650 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1651 handle_scope_(handle_scope), cur_entry_(0) {}
1652
1653 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1654
1655 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1656 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1657 handle_scope_ = scope;
1658 cur_entry_ = 0U;
1659 }
1660
1661 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1662 // Initialize padding entries.
1663 size_t expected_slots = handle_scope_->NumberOfReferences();
1664 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001665 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001666 }
1667 DCHECK_NE(cur_entry_, 0U);
1668 }
1669
1670 private:
1671 HandleScope* handle_scope_;
1672 size_t cur_entry_;
1673 };
1674
1675 HandleScope* handle_scope_;
1676 FillJniCall jni_call_;
1677 void* bottom_of_used_area_;
1678
1679 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001680
1681 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1682};
1683
Andreas Gampec200a4a2014-06-16 18:39:09 -07001684uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1685 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001686 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001687 h.Assign(ref);
1688 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1689 cur_entry_++;
1690 return tmp;
1691}
1692
Ian Rogers9758f792014-03-13 09:02:55 -07001693void BuildGenericJniFrameVisitor::Visit() {
1694 Primitive::Type type = GetParamPrimitiveType();
1695 switch (type) {
1696 case Primitive::kPrimLong: {
1697 jlong long_arg;
1698 if (IsSplitLongOrDouble()) {
1699 long_arg = ReadSplitLongParam();
1700 } else {
1701 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1702 }
1703 sm_.AdvanceLong(long_arg);
1704 break;
1705 }
1706 case Primitive::kPrimDouble: {
1707 uint64_t double_arg;
1708 if (IsSplitLongOrDouble()) {
1709 // Read into union so that we don't case to a double.
1710 double_arg = ReadSplitLongParam();
1711 } else {
1712 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1713 }
1714 sm_.AdvanceDouble(double_arg);
1715 break;
1716 }
1717 case Primitive::kPrimNot: {
1718 StackReference<mirror::Object>* stack_ref =
1719 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001720 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001721 break;
1722 }
1723 case Primitive::kPrimFloat:
1724 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1725 break;
1726 case Primitive::kPrimBoolean: // Fall-through.
1727 case Primitive::kPrimByte: // Fall-through.
1728 case Primitive::kPrimChar: // Fall-through.
1729 case Primitive::kPrimShort: // Fall-through.
1730 case Primitive::kPrimInt: // Fall-through.
1731 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1732 break;
1733 case Primitive::kPrimVoid:
1734 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001735 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001736 }
1737}
1738
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001739void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001740 // Clear out rest of the scope.
1741 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001742 // Install HandleScope.
1743 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001744}
1745
Ian Rogers04c31d22014-07-07 21:44:06 -07001746#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001747extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001748#else
1749extern "C" void* artFindNativeMethod(Thread* self);
1750#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001751
Andreas Gampead615172014-04-04 16:20:13 -07001752uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1753 if (lock != nullptr) {
1754 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1755 } else {
1756 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1757 }
1758}
1759
1760void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1761 if (lock != nullptr) {
1762 JniMethodEndSynchronized(cookie, lock, self);
1763 } else {
1764 JniMethodEnd(cookie, self);
1765 }
1766}
1767
Andreas Gampec147b002014-03-06 18:11:06 -08001768/*
1769 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001770 * 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 -08001771 * The final element on the stack is a pointer to the native code.
1772 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001773 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001774 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001775 *
Andreas Gampec147b002014-03-06 18:11:06 -08001776 * The return of this function denotes:
1777 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1778 * 2) An error, if the value is negative.
1779 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001780extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1781 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001782 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001783 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001784 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001785 uint32_t shorty_len = 0;
1786 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001787
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001788 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001789 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001790 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001791 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001792
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001794 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001795
Ian Rogerse0dcd462014-03-08 15:21:04 -08001796 self->VerifyStack();
1797
Andreas Gampe90546832014-03-12 18:07:19 -07001798 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001799 uint32_t cookie;
1800 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001801 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001802 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001803 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001804 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001805 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001806 }
1807 } else {
1808 cookie = JniMethodStart(self);
1809 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001810 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001811 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001812
Andreas Gampe90546832014-03-12 18:07:19 -07001813 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001814 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001815
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001816 // There are two cases for the content of nativeCode:
1817 // 1) Pointer to the native function.
1818 // 2) Pointer to the trampoline for native code binding.
1819 // In the second case, we need to execute the binding and continue with the actual native function
1820 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001821 DCHECK(nativeCode != nullptr);
1822 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001823#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001824 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001825#else
1826 nativeCode = artFindNativeMethod(self);
1827#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001828
1829 if (nativeCode == nullptr) {
1830 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001831
1832 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001833 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001834 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001835 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1836 } else {
1837 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1838 }
1839
Andreas Gampec200a4a2014-06-16 18:39:09 -07001840 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001841 }
1842 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001843 }
1844
Andreas Gampec200a4a2014-06-16 18:39:09 -07001845 // Return native code addr(lo) and bottom of alloca address(hi).
1846 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1847 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001848}
1849
1850/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001851 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001852 * unlocking.
1853 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001854extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001855 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001856 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001857 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001858 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001859 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001860
Andreas Gampead615172014-04-04 16:20:13 -07001861 jobject lock = nullptr;
1862 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001863 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1864 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001865 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001866 }
1867
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001868 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001869
1870 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001871 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001872 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001873 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001874
1875 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001876 case 'F': {
1877 if (kRuntimeISA == kX86) {
1878 // Convert back the result to float.
1879 double d = bit_cast<uint64_t, double>(result_f);
1880 return bit_cast<float, uint32_t>(static_cast<float>(d));
1881 } else {
1882 return result_f;
1883 }
1884 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001885 case 'D':
1886 return result_f;
1887 case 'Z':
1888 return result.z;
1889 case 'B':
1890 return result.b;
1891 case 'C':
1892 return result.c;
1893 case 'S':
1894 return result.s;
1895 case 'I':
1896 return result.i;
1897 case 'J':
1898 return result.j;
1899 case 'V':
1900 return 0;
1901 default:
1902 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1903 return 0;
1904 }
1905 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001906}
1907
Andreas Gamped58342c2014-06-05 14:18:08 -07001908// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1909// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001910//
Andreas Gamped58342c2014-06-05 14:18:08 -07001911// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1912// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001913
1914template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001915static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001916 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001917 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001918
1919template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001920static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001921 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001922 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001923 ScopedQuickEntrypointChecks sqec(self);
1924 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001925 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1926 type);
1927 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001928 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1929 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001930 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001931 {
1932 // Remember the args in case a GC happens in FindMethodFromCode.
1933 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1934 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1935 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001936 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1937 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001938 visitor.FixupReferences();
1939 }
1940
Ian Rogerse0a02da2014-12-02 14:10:53 -08001941 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001942 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001943 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001944 }
1945 }
1946 DCHECK(!self->IsExceptionPending());
1947 const void* code = method->GetEntryPointFromQuickCompiledCode();
1948
1949 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001950 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001951 << " location: "
1952 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001953
Andreas Gamped58342c2014-06-05 14:18:08 -07001954 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1955 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001956}
1957
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001958// Explicit artInvokeCommon template function declarations to please analysis tool.
1959#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1960 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001961 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001962 mirror::Object* this_object, \
1963 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001964 Thread* self, \
1965 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001966
1967EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1968EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1969EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1970EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1971EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1972EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1973EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1974EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1975EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1976EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1977#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1978
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001979// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001980extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1981 uint32_t method_idx, mirror::Object* this_object,
1982 mirror::ArtMethod* caller_method, Thread* self,
1983 StackReference<mirror::ArtMethod>* sp)
1984 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1985 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1986 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001987}
1988
Andreas Gampec200a4a2014-06-16 18:39:09 -07001989extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1990 uint32_t method_idx, mirror::Object* this_object,
1991 mirror::ArtMethod* caller_method, Thread* self,
1992 StackReference<mirror::ArtMethod>* sp)
1993 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1994 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1995 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001996}
1997
Andreas Gampec200a4a2014-06-16 18:39:09 -07001998extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1999 uint32_t method_idx, mirror::Object* this_object,
2000 mirror::ArtMethod* caller_method, Thread* self,
2001 StackReference<mirror::ArtMethod>* sp)
2002 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2003 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
2004 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002005}
2006
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
2008 uint32_t method_idx, mirror::Object* this_object,
2009 mirror::ArtMethod* caller_method, Thread* self,
2010 StackReference<mirror::ArtMethod>* sp)
2011 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2012 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
2013 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002014}
2015
Andreas Gampec200a4a2014-06-16 18:39:09 -07002016extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
2017 uint32_t method_idx, mirror::Object* this_object,
2018 mirror::ArtMethod* caller_method, Thread* self,
2019 StackReference<mirror::ArtMethod>* sp)
2020 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2021 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
2022 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002023}
2024
2025// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07002026extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07002027 mirror::Object* this_object,
2028 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002029 Thread* self,
2030 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002031 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002032 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002033 mirror::ArtMethod* method;
2034 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2035 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002036 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002037 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2038 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002039 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002040 }
2041 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002042 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002043
2044 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002045 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07002046 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002047
2048 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002049 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002050 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002051 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2052 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2053 Instruction::Code instr_code = instr->Opcode();
2054 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2055 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08002056 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002057 uint32_t dex_method_idx;
2058 if (instr_code == Instruction::INVOKE_INTERFACE) {
2059 dex_method_idx = instr->VRegB_35c();
2060 } else {
2061 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2062 dex_method_idx = instr->VRegB_3rc();
2063 }
2064
Andreas Gampec200a4a2014-06-16 18:39:09 -07002065 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2066 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002067 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2069 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002070 {
2071 // Remember the args in case a GC happens in FindMethodFromCode.
2072 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2073 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2074 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002075 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002076 self);
2077 visitor.FixupReferences();
2078 }
2079
2080 if (UNLIKELY(method == nullptr)) {
2081 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002082 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002083 }
2084 }
2085 const void* code = method->GetEntryPointFromQuickCompiledCode();
2086
2087 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002088 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002089 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002090
Andreas Gamped58342c2014-06-05 14:18:08 -07002091 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2092 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002093}
2094
Ian Rogers848871b2013-08-05 10:56:33 -07002095} // namespace art