blob: 2e813c808aebf2768968bcd5378d7537c1bac404 [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"
Daniel Mihalyieb076692014-08-22 17:33:31 +020033#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070034
35namespace art {
36
37// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
38class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080039 // Number of bytes for each out register in the caller method's frame.
40 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070041 // Frame size in bytes of a callee-save frame for RefsAndArgs.
42 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
43 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070044#if defined(__arm__)
45 // The callee save frame is pointed to by SP.
46 // | argN | |
47 // | ... | |
48 // | arg4 | |
49 // | arg3 spill | | Caller's frame
50 // | arg2 spill | |
51 // | arg1 spill | |
52 // | Method* | ---
53 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080054 // | ... | 4x6 bytes callee saves
55 // | R3 |
56 // | R2 |
57 // | R1 |
58 // | S15 |
59 // | : |
60 // | S0 |
61 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070062 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050063 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000064 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080065 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
66 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
67 static constexpr size_t kNumQuickGprArgs = 3;
68 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080069 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080070 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
71 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
72 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
73 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
74 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
75 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080076 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000077 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080078 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000079#elif defined(__aarch64__)
80 // The callee save frame is pointed to by SP.
81 // | argN | |
82 // | ... | |
83 // | arg4 | |
84 // | arg3 spill | | Caller's frame
85 // | arg2 spill | |
86 // | arg1 spill | |
87 // | Method* | ---
88 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080089 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000090 // | : |
Zheng Xu69a50302015-04-14 20:04:41 +080091 // | X19 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000092 // | X7 |
93 // | : |
94 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080095 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000096 // | : |
97 // | D0 |
98 // | | padding
99 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500100 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000101 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000102 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800103 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000104 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
105 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800106 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +0800107 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
108 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
109 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
110 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
111 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
112 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000113 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000114 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000115 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800116#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700117 // The callee save frame is pointed to by SP.
118 // | argN | |
119 // | ... | |
120 // | arg4 | |
121 // | arg3 spill | | Caller's frame
122 // | arg2 spill | |
123 // | arg1 spill | |
124 // | Method* | ---
125 // | RA |
126 // | ... | callee saves
127 // | A3 | arg3
128 // | A2 | arg2
129 // | A1 | arg1
130 // | A0/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500131 static constexpr bool kSplitPairAcrossRegisterAndStack = true;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000132 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800133 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800134 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800135 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
136 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800137 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800138 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
Douglas Leungc6d86722014-12-10 16:15:17 -0800139 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 16; // Offset of first GPR arg.
Ian Rogers936b37f2014-02-14 00:52:24 -0800140 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800141 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000142 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800143 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800144#elif defined(__mips__) && defined(__LP64__)
145 // The callee save frame is pointed to by SP.
146 // | argN | |
147 // | ... | |
148 // | arg4 | |
149 // | arg3 spill | | Caller's frame
150 // | arg2 spill | |
151 // | arg1 spill | |
152 // | Method* | ---
153 // | RA |
154 // | ... | callee saves
155 // | F7 | f_arg7
156 // | F6 | f_arg6
157 // | F5 | f_arg5
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800158 // | F4 | f_arg4
159 // | F3 | f_arg3
160 // | F2 | f_arg2
161 // | F1 | f_arg1
162 // | F0 | f_arg0
163 // | A7 | arg7
164 // | A6 | arg6
165 // | A5 | arg5
166 // | A4 | arg4
167 // | A3 | arg3
168 // | A2 | arg2
169 // | A1 | arg1
170 // | | padding
171 // | A0/Method* | <- sp
172 // NOTE: for Mip64, when A0 is skipped, F0 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800173 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800174 static constexpr bool kAlignPairRegister = false;
175 static constexpr bool kQuickSoftFloatAbi = false;
176 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
177 // These values are set to zeros because GPR and FPR register
178 // assignments for Mips64 are interleaved, which the current VisitArguments()
179 // function does not support.
180 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
181 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
182 static constexpr bool kGprFprLockstep = true;
183
184 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F1).
185 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
186 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
187 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
188 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
189 }
Ian Rogers848871b2013-08-05 10:56:33 -0700190#elif defined(__i386__)
191 // The callee save frame is pointed to by SP.
192 // | argN | |
193 // | ... | |
194 // | arg4 | |
195 // | arg3 spill | | Caller's frame
196 // | arg2 spill | |
197 // | arg1 spill | |
198 // | Method* | ---
199 // | Return |
200 // | EBP,ESI,EDI | callee saves
201 // | EBX | arg3
202 // | EDX | arg2
203 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000204 // | XMM3 | float arg 4
205 // | XMM2 | float arg 3
206 // | XMM1 | float arg 2
207 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700208 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500209 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000210 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000211 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800212 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800213 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000214 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800215 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000216 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
217 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
218 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800219 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000220 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800221 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800222#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800223 // The callee save frame is pointed to by SP.
224 // | argN | |
225 // | ... | |
226 // | reg. arg spills | | Caller's frame
227 // | Method* | ---
228 // | Return |
229 // | R15 | callee save
230 // | R14 | callee save
231 // | R13 | callee save
232 // | R12 | callee save
233 // | R9 | arg5
234 // | R8 | arg4
235 // | RSI/R6 | arg1
236 // | RBP/R5 | callee save
237 // | RBX/R3 | callee save
238 // | RDX/R2 | arg2
239 // | RCX/R1 | arg3
240 // | XMM7 | float arg 8
241 // | XMM6 | float arg 7
242 // | XMM5 | float arg 6
243 // | XMM4 | float arg 5
244 // | XMM3 | float arg 4
245 // | XMM2 | float arg 3
246 // | XMM1 | float arg 2
247 // | XMM0 | float arg 1
248 // | Padding |
249 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500250 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000251 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800252 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800253 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700254 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700255 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800256 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700258 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
259 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
261 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000262 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
263 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
264 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
265 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
266 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800267 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700268 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
269 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800270 }
271 }
Ian Rogers848871b2013-08-05 10:56:33 -0700272#else
273#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700274#endif
275
Ian Rogers936b37f2014-02-14 00:52:24 -0800276 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100277 // Special handling for proxy methods. Proxy methods are instance methods so the
278 // 'this' object is the 1st argument. They also have the same frame layout as the
279 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
280 // 1st GPR.
281 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
282 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
283 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
284 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
285 CHECK_GT(kNumQuickGprArgs, 0u);
286 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
287 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
288 GprIndexToGprOffset(kThisGprIndex);
289 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
290 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
291 }
292
Andreas Gampecf4035a2014-05-28 22:43:01 -0700293 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800294 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700295 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700296 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700297 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700298 }
299
Ian Rogers936b37f2014-02-14 00:52:24 -0800300 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700301 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700303 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700304 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700305 return *reinterpret_cast<uintptr_t*>(lr);
306 }
307
Andreas Gampec200a4a2014-06-16 18:39:09 -0700308 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
309 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
310 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700311 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
312 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
313 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700314 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800315 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
316 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800317 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
318 "Number of Quick FPR arguments unexpected");
319 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
320 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800321 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
322 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800323 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
324 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800325 }
Ian Rogers848871b2013-08-05 10:56:33 -0700326
327 virtual ~QuickArgumentVisitor() {}
328
329 virtual void Visit() = 0;
330
Ian Rogers936b37f2014-02-14 00:52:24 -0800331 Primitive::Type GetParamPrimitiveType() const {
332 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700333 }
334
Ian Rogers13735952014-10-08 12:43:28 -0700335 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800336 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800337 Primitive::Type type = GetParamPrimitiveType();
338 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800339 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
340 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
341 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
342 }
343 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000344 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800345 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700346 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800347 }
348 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800349 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800350 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
351 }
352 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700353 }
354
355 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000356 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800357 return is_split_long_or_double_;
358 } else {
359 return false; // An optimization for when GPR and FPRs are 64bit.
360 }
Ian Rogers848871b2013-08-05 10:56:33 -0700361 }
362
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700364 return GetParamPrimitiveType() == Primitive::kPrimNot;
365 }
366
Ian Rogers936b37f2014-02-14 00:52:24 -0800367 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700368 Primitive::Type type = GetParamPrimitiveType();
369 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
370 }
371
372 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000373 // The splitted long is always available through the stack.
374 return *reinterpret_cast<uint64_t*>(stack_args_
375 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700376 }
377
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800378 void IncGprIndex() {
379 gpr_index_++;
380 if (kGprFprLockstep) {
381 fpr_index_++;
382 }
383 }
384
385 void IncFprIndex() {
386 fpr_index_++;
387 if (kGprFprLockstep) {
388 gpr_index_++;
389 }
390 }
391
Ian Rogers848871b2013-08-05 10:56:33 -0700392 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800393 // (a) 'stack_args_' should point to the first method's argument
394 // (b) whatever the argument type it is, the 'stack_index_' should
395 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800396 gpr_index_ = 0;
397 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800398 if (kQuickDoubleRegAlignedFloatBackFilled) {
399 fpr_double_index_ = 0;
400 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800401 stack_index_ = 0;
402 if (!is_static_) { // Handle this.
403 cur_type_ = Primitive::kPrimNot;
404 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700405 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800406 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800407 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800408 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800409 }
Ian Rogers848871b2013-08-05 10:56:33 -0700410 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800411 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
412 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
413 switch (cur_type_) {
414 case Primitive::kPrimNot:
415 case Primitive::kPrimBoolean:
416 case Primitive::kPrimByte:
417 case Primitive::kPrimChar:
418 case Primitive::kPrimShort:
419 case Primitive::kPrimInt:
420 is_split_long_or_double_ = false;
421 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800422 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800423 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800424 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 }
426 break;
427 case Primitive::kPrimFloat:
428 is_split_long_or_double_ = false;
429 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800430 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800431 if (kQuickSoftFloatAbi) {
432 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800433 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800434 }
435 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800436 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800437 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800438 if (kQuickDoubleRegAlignedFloatBackFilled) {
439 // Double should not overlap with float.
440 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
441 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
442 // Float should not overlap with double.
443 if (fpr_index_ % 2 == 0) {
444 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
445 }
446 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800447 }
448 }
449 break;
450 case Primitive::kPrimDouble:
451 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800452 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000453 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
454 // Currently, this is only for ARM, where the first available parameter register
455 // is R1. So we skip it, and use R2 instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800456 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000457 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000458 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800459 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500460 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
461 // We don't want to split this. Pass over this register.
462 gpr_index_++;
463 is_split_long_or_double_ = false;
464 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800465 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800466 if (kBytesStackArgLocation == 4) {
467 stack_index_+= 2;
468 } else {
469 CHECK_EQ(kBytesStackArgLocation, 8U);
470 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800471 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700472 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800473 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000474 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700475 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800476 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700477 }
478 }
479 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800480 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000481 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800482 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800483 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700484 if (kBytesStackArgLocation == 4) {
485 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800486 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700487 CHECK_EQ(kBytesStackArgLocation, 8U);
488 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800489 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800490 if (kQuickDoubleRegAlignedFloatBackFilled) {
491 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
492 fpr_double_index_ += 2;
493 // Float should not overlap with double.
494 if (fpr_index_ % 2 == 0) {
495 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
496 }
497 }
498 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800499 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800500 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
501 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800502 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800503 }
504 }
505 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800506 }
507 break;
508 default:
509 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
510 }
Ian Rogers848871b2013-08-05 10:56:33 -0700511 }
512 }
513
Andreas Gampec200a4a2014-06-16 18:39:09 -0700514 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700515 const bool is_static_;
516 const char* const shorty_;
517 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700518
519 private:
Ian Rogers13735952014-10-08 12:43:28 -0700520 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
521 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
522 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800523 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800524 // Index into spilled FPRs.
525 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
526 // holds a higher register number.
527 uint32_t fpr_index_;
528 // Index into spilled FPRs for aligned double.
529 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
530 // terms of singles, may be behind fpr_index.
531 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800532 uint32_t stack_index_; // Index into arguments on the stack.
533 // The current type of argument during VisitArguments.
534 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700535 // Does a 64bit parameter straddle the register and stack arguments?
536 bool is_split_long_or_double_;
537};
538
Sebastien Hertza836bc92014-11-25 16:30:53 +0100539// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
540// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
541extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
542 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
543 return QuickArgumentVisitor::GetProxyThisObject(sp);
544}
545
Ian Rogers848871b2013-08-05 10:56:33 -0700546// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800547class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700548 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700549 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
550 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
551 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700552 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700553
Ian Rogers9758f792014-03-13 09:02:55 -0700554 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700555
556 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800557 ShadowFrame* const sf_;
558 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700559
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700560 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700561};
562
Andreas Gampec200a4a2014-06-16 18:39:09 -0700563void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700564 Primitive::Type type = GetParamPrimitiveType();
565 switch (type) {
566 case Primitive::kPrimLong: // Fall-through.
567 case Primitive::kPrimDouble:
568 if (IsSplitLongOrDouble()) {
569 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
570 } else {
571 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
572 }
573 ++cur_reg_;
574 break;
575 case Primitive::kPrimNot: {
576 StackReference<mirror::Object>* stack_ref =
577 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
578 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
579 }
580 break;
581 case Primitive::kPrimBoolean: // Fall-through.
582 case Primitive::kPrimByte: // Fall-through.
583 case Primitive::kPrimChar: // Fall-through.
584 case Primitive::kPrimShort: // Fall-through.
585 case Primitive::kPrimInt: // Fall-through.
586 case Primitive::kPrimFloat:
587 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
588 break;
589 case Primitive::kPrimVoid:
590 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700591 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700592 }
593 ++cur_reg_;
594}
595
Brian Carlstromea46f952013-07-30 01:26:50 -0700596extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700597 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700598 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
599 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
600 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700601 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700602
603 if (method->IsAbstract()) {
604 ThrowAbstractMethodError(method);
605 return 0;
606 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800607 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700608 const char* old_cause = self->StartAssertNoThreadSuspension(
609 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700610 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800611 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700612 uint16_t num_regs = code_item->registers_size_;
613 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700614 // No last shadow coming from quick.
615 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700616 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700617 uint32_t shorty_len = 0;
618 const char* shorty = method->GetShorty(&shorty_len);
619 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800620 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700621 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800622 const bool needs_initialization =
623 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700624 // Push a transition back into managed code onto the linked list in thread.
625 ManagedStack fragment;
626 self->PushManagedStackFragment(&fragment);
627 self->PushShadowFrame(shadow_frame);
628 self->EndAssertNoThreadSuspension(old_cause);
629
Ian Rogerse94652f2014-12-02 11:13:19 -0800630 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700631 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800632 StackHandleScope<1> hs(self);
633 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700634 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800635 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700636 self->PopManagedStackFragment(fragment);
637 return 0;
638 }
639 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800640 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700641 // Pop transition.
642 self->PopManagedStackFragment(fragment);
Daniel Mihalyieb076692014-08-22 17:33:31 +0200643
644 // Request a stack deoptimization if needed
645 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
646 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
647 self->SetException(Thread::GetDeoptimizationException());
648 self->SetDeoptimizationReturnValue(result);
649 }
650
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800651 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700652 return result.GetJ();
653 }
654}
655
656// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
657// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800658class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700659 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700660 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
661 const char* shorty, uint32_t shorty_len,
662 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700663 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700664
Ian Rogers9758f792014-03-13 09:02:55 -0700665 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700666
Ian Rogers9758f792014-03-13 09:02:55 -0700667 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800668
Ian Rogers848871b2013-08-05 10:56:33 -0700669 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700670 ScopedObjectAccessUnchecked* const soa_;
671 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800672 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700673 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700674
Ian Rogers848871b2013-08-05 10:56:33 -0700675 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
676};
677
Ian Rogers9758f792014-03-13 09:02:55 -0700678void BuildQuickArgumentVisitor::Visit() {
679 jvalue val;
680 Primitive::Type type = GetParamPrimitiveType();
681 switch (type) {
682 case Primitive::kPrimNot: {
683 StackReference<mirror::Object>* stack_ref =
684 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
685 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
686 references_.push_back(std::make_pair(val.l, stack_ref));
687 break;
688 }
689 case Primitive::kPrimLong: // Fall-through.
690 case Primitive::kPrimDouble:
691 if (IsSplitLongOrDouble()) {
692 val.j = ReadSplitLongParam();
693 } else {
694 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
695 }
696 break;
697 case Primitive::kPrimBoolean: // Fall-through.
698 case Primitive::kPrimByte: // Fall-through.
699 case Primitive::kPrimChar: // Fall-through.
700 case Primitive::kPrimShort: // Fall-through.
701 case Primitive::kPrimInt: // Fall-through.
702 case Primitive::kPrimFloat:
703 val.i = *reinterpret_cast<jint*>(GetParamAddress());
704 break;
705 case Primitive::kPrimVoid:
706 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700707 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700708 }
709 args_->push_back(val);
710}
711
712void BuildQuickArgumentVisitor::FixupReferences() {
713 // Fixup any references which may have changed.
714 for (const auto& pair : references_) {
715 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700716 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700717 }
718}
719
Ian Rogers848871b2013-08-05 10:56:33 -0700720// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
721// which is responsible for recording callee save registers. We explicitly place into jobjects the
722// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
723// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700724extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700725 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700726 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700727 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700728 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
729 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700730 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
731 const char* old_cause =
732 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
733 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700734 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700735 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700736 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
737 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700738 self->VerifyStack();
739 // Start new JNI local reference state.
740 JNIEnvExt* env = self->GetJniEnv();
741 ScopedObjectAccessUnchecked soa(env);
742 ScopedJniEnvLocalRefState env_state(env);
743 // Create local ref. copies of proxy method and the receiver.
744 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
745
746 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700747 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
748 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700749 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700750 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700751 uint32_t shorty_len = 0;
752 const char* shorty = proxy_method->GetShorty(&shorty_len);
753 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700754
Ian Rogers848871b2013-08-05 10:56:33 -0700755 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700756 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700757 args.erase(args.begin());
758
759 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700760 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800761 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700762 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
763 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
764
765 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
766 // that performs allocations.
767 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700768 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800769 // Restore references which might have moved.
770 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700771 return result.GetJ();
772}
773
774// Read object references held in arguments from quick frames and place in a JNI local references,
775// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800776class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700777 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700778 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
779 const char* shorty, uint32_t shorty_len,
780 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700781 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700782
Ian Rogers9758f792014-03-13 09:02:55 -0700783 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700784
Ian Rogers9758f792014-03-13 09:02:55 -0700785 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700786
787 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700788 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800789 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700790 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
791
Mathieu Chartier590fee92013-09-13 13:46:47 -0700792 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700793};
794
Ian Rogers9758f792014-03-13 09:02:55 -0700795void RememberForGcArgumentVisitor::Visit() {
796 if (IsParamAReference()) {
797 StackReference<mirror::Object>* stack_ref =
798 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
799 jobject reference =
800 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
801 references_.push_back(std::make_pair(reference, stack_ref));
802 }
803}
804
805void RememberForGcArgumentVisitor::FixupReferences() {
806 // Fixup any references which may have changed.
807 for (const auto& pair : references_) {
808 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700809 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700810 }
811}
812
Ian Rogers848871b2013-08-05 10:56:33 -0700813// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700814extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700815 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700816 Thread* self,
817 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700818 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700819 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700820 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800821 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700822 ScopedObjectAccessUnchecked soa(env);
823 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800824 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700825
826 // Compute details about the called method (avoid GCs)
827 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700828 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700829 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800830 MethodReference called_method(nullptr, 0);
831 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
832 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700833 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
834 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800835 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700836 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700837 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
838 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
839 Instruction::Code instr_code = instr->Opcode();
840 bool is_range;
841 switch (instr_code) {
842 case Instruction::INVOKE_DIRECT:
843 invoke_type = kDirect;
844 is_range = false;
845 break;
846 case Instruction::INVOKE_DIRECT_RANGE:
847 invoke_type = kDirect;
848 is_range = true;
849 break;
850 case Instruction::INVOKE_STATIC:
851 invoke_type = kStatic;
852 is_range = false;
853 break;
854 case Instruction::INVOKE_STATIC_RANGE:
855 invoke_type = kStatic;
856 is_range = true;
857 break;
858 case Instruction::INVOKE_SUPER:
859 invoke_type = kSuper;
860 is_range = false;
861 break;
862 case Instruction::INVOKE_SUPER_RANGE:
863 invoke_type = kSuper;
864 is_range = true;
865 break;
866 case Instruction::INVOKE_VIRTUAL:
867 invoke_type = kVirtual;
868 is_range = false;
869 break;
870 case Instruction::INVOKE_VIRTUAL_RANGE:
871 invoke_type = kVirtual;
872 is_range = true;
873 break;
874 case Instruction::INVOKE_INTERFACE:
875 invoke_type = kInterface;
876 is_range = false;
877 break;
878 case Instruction::INVOKE_INTERFACE_RANGE:
879 invoke_type = kInterface;
880 is_range = true;
881 break;
882 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800883 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
884 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700885 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800886 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700887 } else {
888 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800889 called_method.dex_file = called->GetDexFile();
890 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700891 }
892 uint32_t shorty_len;
893 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800894 called_method.dex_file->GetMethodShorty(
895 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700896 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700897 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800898 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800899 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700900 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800901 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700902 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700903 mirror::Object* dummy = nullptr;
904 HandleWrapper<mirror::Object> h_receiver(
905 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800906 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
907 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700908 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800909 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800910 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700911 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800912 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
913 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800914 if (virtual_or_interface) {
915 // Refine called method based on receiver.
916 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700917
918 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800919 if (invoke_type == kVirtual) {
920 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
921 } else {
922 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
923 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700924
925 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
926 << PrettyTypeOf(receiver) << " "
927 << invoke_type << " " << orig_called->GetVtableIndex();
928
Ian Rogers83883d72013-10-21 21:07:24 -0700929 // 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 -0800930 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800931 // Note, called_method.dex_method_index references the dex method before the
932 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
933 // about the name and signature.
934 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800935 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700936 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000937 // the caller's dex file. Since we get here only if the original called was a runtime
938 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800939 DCHECK(!called_method_known_on_entry);
940 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
941 const DexFile* caller_dex_file = called_method.dex_file;
942 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
943 update_dex_cache_method_index =
944 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
945 caller_method_name_and_sig_index);
946 }
947 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
948 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
949 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700950 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -0800951 } else if (invoke_type == kStatic) {
952 const auto called_dex_method_idx = called->GetDexMethodIndex();
953 // For static invokes, we may dispatch to the static method in the superclass but resolve
954 // using the subclass. To prevent getting slow paths on each invoke, we force set the
955 // resolved method for the super class dex method index if we are in the same dex file.
956 // b/19175856
957 if (called->GetDexFile() == called_method.dex_file &&
958 called_method.dex_method_index != called_dex_method_idx) {
959 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx, called);
960 }
Ian Rogers83883d72013-10-21 21:07:24 -0700961 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200962
Ian Rogers848871b2013-08-05 10:56:33 -0700963 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700964 StackHandleScope<1> hs(soa.Self());
965 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700966 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700967 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +0200968 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
969 // If we are single-stepping or the called method is deoptimized (by a
970 // breakpoint, for example), then we have to execute the called method
971 // with the interpreter.
972 code = GetQuickToInterpreterBridge();
973 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
974 // If the caller is deoptimized (by a breakpoint, for example), we have to
975 // continue its execution with interpreter when returning from the called
976 // method. Because we do not want to execute the called method with the
977 // interpreter, we wrap its execution into the instrumentation stubs.
978 // When the called method returns, it will execute the instrumentation
979 // exit hook that will determine the need of the interpreter with a call
980 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
981 // it is needed.
982 code = GetQuickInstrumentationEntryPoint();
983 } else {
984 code = called->GetEntryPointFromQuickCompiledCode();
985 }
Ian Rogers848871b2013-08-05 10:56:33 -0700986 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +0200987 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
988 // If we are single-stepping or the called method is deoptimized (by a
989 // breakpoint, for example), then we have to execute the called method
990 // with the interpreter.
991 code = GetQuickToInterpreterBridge();
992 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -0700993 // Class is still initializing, go to oat and grab code (trampoline must be left in place
994 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800995 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700996 } else {
997 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800998 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700999 }
1000 } else {
1001 DCHECK(called_class->IsErroneous());
1002 }
1003 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001004 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001005 // Fixup any locally saved objects may have moved during a GC.
1006 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001007 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -07001008 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001009 return code;
1010}
1011
Andreas Gampec147b002014-03-06 18:11:06 -08001012/*
1013 * This class uses a couple of observations to unite the different calling conventions through
1014 * a few constants.
1015 *
1016 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1017 * possible alignment.
1018 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1019 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1020 * when we have to split things
1021 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1022 * and we can use Int handling directly.
1023 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1024 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1025 * extension should be compatible with Aarch64, which mandates copying the available bits
1026 * into LSB and leaving the rest unspecified.
1027 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1028 * the stack.
1029 * 6) There is only little endian.
1030 *
1031 *
1032 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1033 * follows:
1034 *
1035 * void PushGpr(uintptr_t): Add a value for the next GPR
1036 *
1037 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1038 * padding, that is, think the architecture is 32b and aligns 64b.
1039 *
1040 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1041 * split this if necessary. The current state will have aligned, if
1042 * necessary.
1043 *
1044 * void PushStack(uintptr_t): Push a value to the stack.
1045 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001046 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001047 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001048 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001049 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001050 *
1051 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001052template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001053 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001054#if defined(__arm__)
1055 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001056 static constexpr bool kNativeSoftFloatAbi = true;
1057 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001058 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1059
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001060 static constexpr size_t kRegistersNeededForLong = 2;
1061 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001062 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001063 static constexpr bool kMultiFPRegistersWidened = false;
1064 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001065 static constexpr bool kAlignLongOnStack = true;
1066 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001067#elif defined(__aarch64__)
1068 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1069 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1070 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1071
1072 static constexpr size_t kRegistersNeededForLong = 1;
1073 static constexpr size_t kRegistersNeededForDouble = 1;
1074 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001075 static constexpr bool kMultiFPRegistersWidened = false;
1076 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001077 static constexpr bool kAlignLongOnStack = false;
1078 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001079#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001080 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001081 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1082 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001083
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001084 static constexpr size_t kRegistersNeededForLong = 2;
1085 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001086 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001087 static constexpr bool kMultiFPRegistersWidened = true;
1088 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001089 static constexpr bool kAlignLongOnStack = true;
1090 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001091#elif defined(__mips__) && defined(__LP64__)
1092 // Let the code prepare GPRs only and we will load the FPRs with same data.
1093 static constexpr bool kNativeSoftFloatAbi = true;
1094 static constexpr size_t kNumNativeGprArgs = 8;
1095 static constexpr size_t kNumNativeFprArgs = 0;
1096
1097 static constexpr size_t kRegistersNeededForLong = 1;
1098 static constexpr size_t kRegistersNeededForDouble = 1;
1099 static constexpr bool kMultiRegistersAligned = false;
1100 static constexpr bool kMultiFPRegistersWidened = false;
1101 static constexpr bool kMultiGPRegistersWidened = true;
1102 static constexpr bool kAlignLongOnStack = false;
1103 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001104#elif defined(__i386__)
1105 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001106 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001107 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1108 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1109
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001110 static constexpr size_t kRegistersNeededForLong = 2;
1111 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001112 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001113 static constexpr bool kMultiFPRegistersWidened = false;
1114 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001115 static constexpr bool kAlignLongOnStack = false;
1116 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001117#elif defined(__x86_64__)
1118 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1119 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1120 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1121
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001122 static constexpr size_t kRegistersNeededForLong = 1;
1123 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001124 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001125 static constexpr bool kMultiFPRegistersWidened = false;
1126 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001127 static constexpr bool kAlignLongOnStack = false;
1128 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001129#else
1130#error "Unsupported architecture"
1131#endif
1132
Andreas Gampec147b002014-03-06 18:11:06 -08001133 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001134 explicit BuildNativeCallFrameStateMachine(T* delegate)
1135 : gpr_index_(kNumNativeGprArgs),
1136 fpr_index_(kNumNativeFprArgs),
1137 stack_entries_(0),
1138 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001139 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1140 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001141 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1142 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001143 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001144
Andreas Gampec200a4a2014-06-16 18:39:09 -07001145 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001146
Ian Rogers1428dce2014-10-21 15:02:15 -07001147 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001148 return gpr_index_ > 0;
1149 }
1150
Andreas Gampec200a4a2014-06-16 18:39:09 -07001151 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001152 if (HavePointerGpr()) {
1153 gpr_index_--;
1154 PushGpr(reinterpret_cast<uintptr_t>(val));
1155 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001156 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001157 PushStack(reinterpret_cast<uintptr_t>(val));
1158 gpr_index_ = 0;
1159 }
1160 }
1161
Ian Rogers1428dce2014-10-21 15:02:15 -07001162 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001163 return gpr_index_ > 0;
1164 }
1165
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001166 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1167 uintptr_t handle = PushHandle(ptr);
1168 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001169 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001170 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001171 } else {
1172 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001173 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001174 gpr_index_ = 0;
1175 }
1176 }
1177
Ian Rogers1428dce2014-10-21 15:02:15 -07001178 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001179 return gpr_index_ > 0;
1180 }
1181
1182 void AdvanceInt(uint32_t val) {
1183 if (HaveIntGpr()) {
1184 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001185 if (kMultiGPRegistersWidened) {
1186 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001187 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001188 } else {
1189 PushGpr(val);
1190 }
Andreas Gampec147b002014-03-06 18:11:06 -08001191 } else {
1192 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001193 if (kMultiGPRegistersWidened) {
1194 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001195 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001196 } else {
1197 PushStack(val);
1198 }
Andreas Gampec147b002014-03-06 18:11:06 -08001199 gpr_index_ = 0;
1200 }
1201 }
1202
Ian Rogers1428dce2014-10-21 15:02:15 -07001203 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001204 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1205 }
1206
Ian Rogers1428dce2014-10-21 15:02:15 -07001207 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001208 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1209 kAlignLongOnStack && // and when it needs alignment
1210 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1211 }
1212
Ian Rogers1428dce2014-10-21 15:02:15 -07001213 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001214 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1215 kAlignLongOnStack && // and when it needs 8B alignment
1216 (stack_entries_ & 1) == 1; // counter is odd
1217 }
1218
1219 void AdvanceLong(uint64_t val) {
1220 if (HaveLongGpr()) {
1221 if (LongGprNeedsPadding()) {
1222 PushGpr(0);
1223 gpr_index_--;
1224 }
1225 if (kRegistersNeededForLong == 1) {
1226 PushGpr(static_cast<uintptr_t>(val));
1227 } else {
1228 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1229 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1230 }
1231 gpr_index_ -= kRegistersNeededForLong;
1232 } else {
1233 if (LongStackNeedsPadding()) {
1234 PushStack(0);
1235 stack_entries_++;
1236 }
1237 if (kRegistersNeededForLong == 1) {
1238 PushStack(static_cast<uintptr_t>(val));
1239 stack_entries_++;
1240 } else {
1241 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1242 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1243 stack_entries_ += 2;
1244 }
1245 gpr_index_ = 0;
1246 }
1247 }
1248
Ian Rogers1428dce2014-10-21 15:02:15 -07001249 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001250 return fpr_index_ > 0;
1251 }
1252
Andreas Gampec147b002014-03-06 18:11:06 -08001253 void AdvanceFloat(float val) {
1254 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001255 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001256 } else {
1257 if (HaveFloatFpr()) {
1258 fpr_index_--;
1259 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001260 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001261 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001262 } else {
1263 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001264 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001265 }
1266 } else {
1267 PushFpr4(val);
1268 }
1269 } else {
1270 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001271 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001272 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001273 // Note: We need to jump through those hoops to make the compiler happy.
1274 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001275 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001276 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001277 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001278 }
1279 fpr_index_ = 0;
1280 }
1281 }
1282 }
1283
Ian Rogers1428dce2014-10-21 15:02:15 -07001284 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001285 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1286 }
1287
Ian Rogers1428dce2014-10-21 15:02:15 -07001288 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001289 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1290 kAlignDoubleOnStack && // and when it needs alignment
1291 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1292 }
1293
Ian Rogers1428dce2014-10-21 15:02:15 -07001294 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001295 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1296 kAlignDoubleOnStack && // and when it needs 8B alignment
1297 (stack_entries_ & 1) == 1; // counter is odd
1298 }
1299
1300 void AdvanceDouble(uint64_t val) {
1301 if (kNativeSoftFloatAbi) {
1302 AdvanceLong(val);
1303 } else {
1304 if (HaveDoubleFpr()) {
1305 if (DoubleFprNeedsPadding()) {
1306 PushFpr4(0);
1307 fpr_index_--;
1308 }
1309 PushFpr8(val);
1310 fpr_index_ -= kRegistersNeededForDouble;
1311 } else {
1312 if (DoubleStackNeedsPadding()) {
1313 PushStack(0);
1314 stack_entries_++;
1315 }
1316 if (kRegistersNeededForDouble == 1) {
1317 PushStack(static_cast<uintptr_t>(val));
1318 stack_entries_++;
1319 } else {
1320 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1321 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1322 stack_entries_ += 2;
1323 }
1324 fpr_index_ = 0;
1325 }
1326 }
1327 }
1328
Ian Rogers1428dce2014-10-21 15:02:15 -07001329 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001330 return stack_entries_;
1331 }
1332
Ian Rogers1428dce2014-10-21 15:02:15 -07001333 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001334 return kNumNativeGprArgs - gpr_index_;
1335 }
1336
Ian Rogers1428dce2014-10-21 15:02:15 -07001337 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001338 return kNumNativeFprArgs - fpr_index_;
1339 }
1340
1341 private:
1342 void PushGpr(uintptr_t val) {
1343 delegate_->PushGpr(val);
1344 }
1345 void PushFpr4(float val) {
1346 delegate_->PushFpr4(val);
1347 }
1348 void PushFpr8(uint64_t val) {
1349 delegate_->PushFpr8(val);
1350 }
1351 void PushStack(uintptr_t val) {
1352 delegate_->PushStack(val);
1353 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001354 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1355 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001356 }
1357
1358 uint32_t gpr_index_; // Number of free GPRs
1359 uint32_t fpr_index_; // Number of free FPRs
1360 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1361 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001362 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001363};
1364
Andreas Gampec200a4a2014-06-16 18:39:09 -07001365// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1366// in subclasses.
1367//
1368// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1369// them with handles.
1370class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001371 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001372 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1373
1374 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001375
Ian Rogers1428dce2014-10-21 15:02:15 -07001376 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001377 return num_stack_entries_ * sizeof(uintptr_t);
1378 }
1379
Ian Rogers1428dce2014-10-21 15:02:15 -07001380 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001381 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001382 // Align by kStackAlignment.
1383 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001384 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001385 }
1386
Ian Rogers1428dce2014-10-21 15:02:15 -07001387 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1388 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001389 // Assumption is OK right now, as we have soft-float arm
1390 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1391 sp8 -= fregs * sizeof(uintptr_t);
1392 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1393 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1394 sp8 -= iregs * sizeof(uintptr_t);
1395 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1396 return sp8;
1397 }
Andreas Gampec147b002014-03-06 18:11:06 -08001398
Andreas Gampec200a4a2014-06-16 18:39:09 -07001399 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001400 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001401 // Native call stack.
1402 sp8 = LayoutCallStack(sp8);
1403 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001404
Andreas Gampec200a4a2014-06-16 18:39:09 -07001405 // Put fprs and gprs below.
1406 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001407
Andreas Gampec200a4a2014-06-16 18:39:09 -07001408 // Return the new bottom.
1409 return sp8;
1410 }
1411
1412 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001413 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1414 UNUSED(sm);
1415 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001416
1417 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1418 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1419
1420 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001421
1422 for (uint32_t i = 1; i < shorty_len; ++i) {
1423 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1424 switch (cur_type_) {
1425 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001426 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001427 sm.AdvanceHandleScope(
1428 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001429 break;
1430
1431 case Primitive::kPrimBoolean:
1432 case Primitive::kPrimByte:
1433 case Primitive::kPrimChar:
1434 case Primitive::kPrimShort:
1435 case Primitive::kPrimInt:
1436 sm.AdvanceInt(0);
1437 break;
1438 case Primitive::kPrimFloat:
1439 sm.AdvanceFloat(0);
1440 break;
1441 case Primitive::kPrimDouble:
1442 sm.AdvanceDouble(0);
1443 break;
1444 case Primitive::kPrimLong:
1445 sm.AdvanceLong(0);
1446 break;
1447 default:
1448 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001449 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001450 }
1451 }
1452
Ian Rogers1428dce2014-10-21 15:02:15 -07001453 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001454 }
1455
1456 void PushGpr(uintptr_t /* val */) {
1457 // not optimizing registers, yet
1458 }
1459
1460 void PushFpr4(float /* val */) {
1461 // not optimizing registers, yet
1462 }
1463
1464 void PushFpr8(uint64_t /* val */) {
1465 // not optimizing registers, yet
1466 }
1467
1468 void PushStack(uintptr_t /* val */) {
1469 // counting is already done in the superclass
1470 }
1471
Andreas Gampec200a4a2014-06-16 18:39:09 -07001472 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001473 return reinterpret_cast<uintptr_t>(nullptr);
1474 }
1475
Andreas Gampec200a4a2014-06-16 18:39:09 -07001476 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001477 uint32_t num_stack_entries_;
1478};
1479
Andreas Gampec200a4a2014-06-16 18:39:09 -07001480class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001481 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001482 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001483
Andreas Gampec200a4a2014-06-16 18:39:09 -07001484 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1485 // is at *m = sp. Will update to point to the bottom of the save frame.
1486 //
1487 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001488 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1489 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001490 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1491 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001492
Andreas Gampec200a4a2014-06-16 18:39:09 -07001493 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1494
1495 // First, fix up the layout of the callee-save frame.
1496 // We have to squeeze in the HandleScope, and relocate the method pointer.
1497
1498 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001499 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001500
1501 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001502 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1503 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1504
1505 sp8 -= scope_and_method;
1506 // Align by kStackAlignment.
1507 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1508 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1509
1510 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001511 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1512 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001513
1514 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1515 uint8_t* method_pointer = sp8;
1516 StackReference<mirror::ArtMethod>* new_method_ref =
1517 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1518 new_method_ref->Assign(method);
1519 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001520 }
1521
Andreas Gampec200a4a2014-06-16 18:39:09 -07001522 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001523 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001524 // Reference cookie and padding
1525 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001526 }
1527
Andreas Gampec200a4a2014-06-16 18:39:09 -07001528 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1529 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001530 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1531 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001532 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001533 // First, fix up the layout of the callee-save frame.
1534 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001535 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001536
1537 // The bottom of the callee-save frame is now where the method is, *m.
1538 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1539
1540 // Add space for cookie.
1541 LayoutCookie(&sp8);
1542
1543 return sp8;
1544 }
1545
1546 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001547 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001548 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001549 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1550 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1551 Walk(shorty, shorty_len);
1552
1553 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001554 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001555
1556 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1557
1558 // Return the new bottom.
1559 return sp8;
1560 }
1561
1562 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1563
1564 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1565 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1566 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1567
1568 private:
1569 uint32_t num_handle_scope_references_;
1570};
1571
1572uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1573 num_handle_scope_references_++;
1574 return reinterpret_cast<uintptr_t>(nullptr);
1575}
1576
1577void ComputeGenericJniFrameSize::WalkHeader(
1578 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1579 // JNIEnv
1580 sm->AdvancePointer(nullptr);
1581
1582 // Class object or this as first argument
1583 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1584}
1585
1586// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1587// the template requirements of BuildGenericJniFrameStateMachine.
1588class FillNativeCall {
1589 public:
1590 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1591 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1592
1593 virtual ~FillNativeCall() {}
1594
1595 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1596 cur_gpr_reg_ = gpr_regs;
1597 cur_fpr_reg_ = fpr_regs;
1598 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001599 }
1600
1601 void PushGpr(uintptr_t val) {
1602 *cur_gpr_reg_ = val;
1603 cur_gpr_reg_++;
1604 }
1605
1606 void PushFpr4(float val) {
1607 *cur_fpr_reg_ = val;
1608 cur_fpr_reg_++;
1609 }
1610
1611 void PushFpr8(uint64_t val) {
1612 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1613 *tmp = val;
1614 cur_fpr_reg_ += 2;
1615 }
1616
1617 void PushStack(uintptr_t val) {
1618 *cur_stack_arg_ = val;
1619 cur_stack_arg_++;
1620 }
1621
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001622 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001623 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001624 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001625 }
1626
1627 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001628 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001629 uint32_t* cur_fpr_reg_;
1630 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001631};
Andreas Gampec147b002014-03-06 18:11:06 -08001632
Andreas Gampec200a4a2014-06-16 18:39:09 -07001633// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1634// of transitioning into native code.
1635class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1636 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001637 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1638 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001639 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1640 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1641 ComputeGenericJniFrameSize fsc;
1642 uintptr_t* start_gpr_reg;
1643 uint32_t* start_fpr_reg;
1644 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001645 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001646 &handle_scope_,
1647 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001648 &start_gpr_reg, &start_fpr_reg);
1649
Andreas Gampec200a4a2014-06-16 18:39:09 -07001650 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1651
1652 // jni environment is always first argument
1653 sm_.AdvancePointer(self->GetJniEnv());
1654
1655 if (is_static) {
1656 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1657 }
1658 }
1659
1660 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1661
1662 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1663
1664 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1666 return handle_scope_->GetHandle(0).GetReference();
1667 }
1668
Ian Rogers1428dce2014-10-21 15:02:15 -07001669 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001670 return handle_scope_->GetHandle(0).ToJObject();
1671 }
1672
Ian Rogers1428dce2014-10-21 15:02:15 -07001673 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001674 return bottom_of_used_area_;
1675 }
1676
1677 private:
1678 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1679 class FillJniCall FINAL : public FillNativeCall {
1680 public:
1681 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1682 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1683 handle_scope_(handle_scope), cur_entry_(0) {}
1684
1685 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1686
1687 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1688 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1689 handle_scope_ = scope;
1690 cur_entry_ = 0U;
1691 }
1692
1693 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1694 // Initialize padding entries.
1695 size_t expected_slots = handle_scope_->NumberOfReferences();
1696 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001697 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001698 }
1699 DCHECK_NE(cur_entry_, 0U);
1700 }
1701
1702 private:
1703 HandleScope* handle_scope_;
1704 size_t cur_entry_;
1705 };
1706
1707 HandleScope* handle_scope_;
1708 FillJniCall jni_call_;
1709 void* bottom_of_used_area_;
1710
1711 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001712
1713 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1714};
1715
Andreas Gampec200a4a2014-06-16 18:39:09 -07001716uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1717 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001718 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001719 h.Assign(ref);
1720 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1721 cur_entry_++;
1722 return tmp;
1723}
1724
Ian Rogers9758f792014-03-13 09:02:55 -07001725void BuildGenericJniFrameVisitor::Visit() {
1726 Primitive::Type type = GetParamPrimitiveType();
1727 switch (type) {
1728 case Primitive::kPrimLong: {
1729 jlong long_arg;
1730 if (IsSplitLongOrDouble()) {
1731 long_arg = ReadSplitLongParam();
1732 } else {
1733 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1734 }
1735 sm_.AdvanceLong(long_arg);
1736 break;
1737 }
1738 case Primitive::kPrimDouble: {
1739 uint64_t double_arg;
1740 if (IsSplitLongOrDouble()) {
1741 // Read into union so that we don't case to a double.
1742 double_arg = ReadSplitLongParam();
1743 } else {
1744 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1745 }
1746 sm_.AdvanceDouble(double_arg);
1747 break;
1748 }
1749 case Primitive::kPrimNot: {
1750 StackReference<mirror::Object>* stack_ref =
1751 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001752 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001753 break;
1754 }
1755 case Primitive::kPrimFloat:
1756 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1757 break;
1758 case Primitive::kPrimBoolean: // Fall-through.
1759 case Primitive::kPrimByte: // Fall-through.
1760 case Primitive::kPrimChar: // Fall-through.
1761 case Primitive::kPrimShort: // Fall-through.
1762 case Primitive::kPrimInt: // Fall-through.
1763 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1764 break;
1765 case Primitive::kPrimVoid:
1766 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001767 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001768 }
1769}
1770
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001771void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772 // Clear out rest of the scope.
1773 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001774 // Install HandleScope.
1775 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001776}
1777
Ian Rogers04c31d22014-07-07 21:44:06 -07001778#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001779extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001780#else
1781extern "C" void* artFindNativeMethod(Thread* self);
1782#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001783
Andreas Gampead615172014-04-04 16:20:13 -07001784uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1785 if (lock != nullptr) {
1786 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1787 } else {
1788 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1789 }
1790}
1791
1792void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1793 if (lock != nullptr) {
1794 JniMethodEndSynchronized(cookie, lock, self);
1795 } else {
1796 JniMethodEnd(cookie, self);
1797 }
1798}
1799
Andreas Gampec147b002014-03-06 18:11:06 -08001800/*
1801 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001802 * 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 -08001803 * The final element on the stack is a pointer to the native code.
1804 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001805 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001806 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001807 *
Andreas Gampec147b002014-03-06 18:11:06 -08001808 * The return of this function denotes:
1809 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1810 * 2) An error, if the value is negative.
1811 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001812extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1813 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001815 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001816 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001817 uint32_t shorty_len = 0;
1818 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001819
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001820 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001821 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001822 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001823 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001824
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001826 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001827
Ian Rogerse0dcd462014-03-08 15:21:04 -08001828 self->VerifyStack();
1829
Andreas Gampe90546832014-03-12 18:07:19 -07001830 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001831 uint32_t cookie;
1832 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001833 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001834 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001835 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001836 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001837 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001838 }
1839 } else {
1840 cookie = JniMethodStart(self);
1841 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001842 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001843 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001844
Andreas Gampe90546832014-03-12 18:07:19 -07001845 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001846 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001847
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001848 // There are two cases for the content of nativeCode:
1849 // 1) Pointer to the native function.
1850 // 2) Pointer to the trampoline for native code binding.
1851 // In the second case, we need to execute the binding and continue with the actual native function
1852 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001853 DCHECK(nativeCode != nullptr);
1854 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001855#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001856 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001857#else
1858 nativeCode = artFindNativeMethod(self);
1859#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001860
1861 if (nativeCode == nullptr) {
1862 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001863
1864 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001865 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001866 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001867 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1868 } else {
1869 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1870 }
1871
Andreas Gampec200a4a2014-06-16 18:39:09 -07001872 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001873 }
1874 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001875 }
1876
Andreas Gampec200a4a2014-06-16 18:39:09 -07001877 // Return native code addr(lo) and bottom of alloca address(hi).
1878 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1879 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001880}
1881
1882/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001883 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001884 * unlocking.
1885 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001886extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001887 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001888 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001889 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001890 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001891 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001892
Andreas Gampead615172014-04-04 16:20:13 -07001893 jobject lock = nullptr;
1894 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001895 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1896 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001897 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001898 }
1899
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001900 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001901
1902 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001903 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001904 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001905 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001906
1907 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001908 case 'F': {
1909 if (kRuntimeISA == kX86) {
1910 // Convert back the result to float.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001911 double d = bit_cast<double, uint64_t>(result_f);
1912 return bit_cast<uint32_t, float>(static_cast<float>(d));
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001913 } else {
1914 return result_f;
1915 }
1916 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001917 case 'D':
1918 return result_f;
1919 case 'Z':
1920 return result.z;
1921 case 'B':
1922 return result.b;
1923 case 'C':
1924 return result.c;
1925 case 'S':
1926 return result.s;
1927 case 'I':
1928 return result.i;
1929 case 'J':
1930 return result.j;
1931 case 'V':
1932 return 0;
1933 default:
1934 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1935 return 0;
1936 }
1937 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001938}
1939
Andreas Gamped58342c2014-06-05 14:18:08 -07001940// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1941// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001942//
Andreas Gamped58342c2014-06-05 14:18:08 -07001943// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1944// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001945
1946template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001947static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001948 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001949 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001950
1951template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001952static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001953 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001954 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001955 ScopedQuickEntrypointChecks sqec(self);
1956 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001957 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1958 type);
1959 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001960 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1961 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001962 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001963 {
1964 // Remember the args in case a GC happens in FindMethodFromCode.
1965 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1966 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1967 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001968 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1969 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001970 visitor.FixupReferences();
1971 }
1972
Ian Rogerse0a02da2014-12-02 14:10:53 -08001973 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001974 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001975 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001976 }
1977 }
1978 DCHECK(!self->IsExceptionPending());
1979 const void* code = method->GetEntryPointFromQuickCompiledCode();
1980
1981 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001982 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001983 << " location: "
1984 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001985
Andreas Gamped58342c2014-06-05 14:18:08 -07001986 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1987 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001988}
1989
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001990// Explicit artInvokeCommon template function declarations to please analysis tool.
1991#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1992 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001993 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001994 mirror::Object* this_object, \
1995 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001996 Thread* self, \
1997 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001998
1999EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2000EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2001EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2002EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2003EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2004EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2005EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2006EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2007EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2008EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2009#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2010
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002011// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002012extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
2013 uint32_t method_idx, mirror::Object* this_object,
2014 mirror::ArtMethod* caller_method, Thread* self,
2015 StackReference<mirror::ArtMethod>* sp)
2016 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2017 return artInvokeCommon<kInterface, true>(method_idx, this_object,
2018 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002019}
2020
Andreas Gampec200a4a2014-06-16 18:39:09 -07002021extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
2022 uint32_t method_idx, mirror::Object* this_object,
2023 mirror::ArtMethod* caller_method, Thread* self,
2024 StackReference<mirror::ArtMethod>* sp)
2025 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2026 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
2027 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002028}
2029
Andreas Gampec200a4a2014-06-16 18:39:09 -07002030extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
2031 uint32_t method_idx, mirror::Object* this_object,
2032 mirror::ArtMethod* caller_method, Thread* self,
2033 StackReference<mirror::ArtMethod>* sp)
2034 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2035 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
2036 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002037}
2038
Andreas Gampec200a4a2014-06-16 18:39:09 -07002039extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
2040 uint32_t method_idx, mirror::Object* this_object,
2041 mirror::ArtMethod* caller_method, Thread* self,
2042 StackReference<mirror::ArtMethod>* sp)
2043 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2044 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
2045 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002046}
2047
Andreas Gampec200a4a2014-06-16 18:39:09 -07002048extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
2049 uint32_t method_idx, mirror::Object* this_object,
2050 mirror::ArtMethod* caller_method, Thread* self,
2051 StackReference<mirror::ArtMethod>* sp)
2052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2053 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
2054 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002055}
2056
2057// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07002058extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07002059 mirror::Object* this_object,
2060 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07002061 Thread* self,
2062 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002063 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002064 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002065 mirror::ArtMethod* method;
2066 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
2067 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002068 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002069 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
2070 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002071 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002072 }
2073 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002074 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002075
2076 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002077 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07002078 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07002079
2080 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002081 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002082 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002083 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
2084 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
2085 Instruction::Code instr_code = instr->Opcode();
2086 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2087 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08002088 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002089 uint32_t dex_method_idx;
2090 if (instr_code == Instruction::INVOKE_INTERFACE) {
2091 dex_method_idx = instr->VRegB_35c();
2092 } else {
2093 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2094 dex_method_idx = instr->VRegB_3rc();
2095 }
2096
Andreas Gampec200a4a2014-06-16 18:39:09 -07002097 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2098 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002099 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002100 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2101 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002102 {
2103 // Remember the args in case a GC happens in FindMethodFromCode.
2104 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2105 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2106 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002107 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002108 self);
2109 visitor.FixupReferences();
2110 }
2111
2112 if (UNLIKELY(method == nullptr)) {
2113 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002114 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002115 }
2116 }
2117 const void* code = method->GetEntryPointFromQuickCompiledCode();
2118
2119 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08002120 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002121 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002122
Andreas Gamped58342c2014-06-05 14:18:08 -07002123 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2124 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002125}
2126
Ian Rogers848871b2013-08-05 10:56:33 -07002127} // namespace art