blob: ac640b4454b32c1a8f271130efe004aaabe59ffc [file] [log] [blame]
Ian Rogers848871b2013-08-05 10:56:33 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "callee_save_frame.h"
Dragos Sbirleabd136a22013-08-13 18:07:04 -070018#include "common_throws.h"
Ian Rogers848871b2013-08-05 10:56:33 -070019#include "dex_file-inl.h"
20#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070021#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070022#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070023#include "gc/accounting/card_table-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070024#include "interpreter/interpreter.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080025#include "method_reference.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070026#include "mirror/art_method-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070027#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070028#include "mirror/dex_cache-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070031#include "runtime.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070032#include "scoped_thread_state_change.h"
Ian Rogers848871b2013-08-05 10:56:33 -070033
34namespace art {
35
36// Visits the arguments as saved to the stack by a Runtime::kRefAndArgs callee save frame.
37class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080038 // Number of bytes for each out register in the caller method's frame.
39 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070040 // Frame size in bytes of a callee-save frame for RefsAndArgs.
41 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
42 GetCalleeSaveFrameSize(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070043#if defined(__arm__)
44 // The callee save frame is pointed to by SP.
45 // | argN | |
46 // | ... | |
47 // | arg4 | |
48 // | arg3 spill | | Caller's frame
49 // | arg2 spill | |
50 // | arg1 spill | |
51 // | Method* | ---
52 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080053 // | ... | 4x6 bytes callee saves
54 // | R3 |
55 // | R2 |
56 // | R1 |
57 // | S15 |
58 // | : |
59 // | S0 |
60 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070061 // | Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000062 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080063 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
64 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
65 static constexpr size_t kNumQuickGprArgs = 3;
66 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Zheng Xub551fdc2014-07-25 11:49:42 +080067 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
68 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
69 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
70 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
71 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
72 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080073 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000074 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080075 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000076#elif defined(__aarch64__)
77 // The callee save frame is pointed to by SP.
78 // | argN | |
79 // | ... | |
80 // | arg4 | |
81 // | arg3 spill | | Caller's frame
82 // | arg2 spill | |
83 // | arg1 spill | |
84 // | Method* | ---
85 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080086 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000087 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080088 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000089 // | X7 |
90 // | : |
91 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080092 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000093 // | : |
94 // | D0 |
95 // | | padding
96 // | Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000097 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +000098 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +080099 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000100 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
101 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +0800102 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
103 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
104 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
105 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
106 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
107 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000109 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 }
Ian Rogers848871b2013-08-05 10:56:33 -0700111#elif defined(__mips__)
112 // The callee save frame is pointed to by SP.
113 // | argN | |
114 // | ... | |
115 // | arg4 | |
116 // | arg3 spill | | Caller's frame
117 // | arg2 spill | |
118 // | arg1 spill | |
119 // | Method* | ---
120 // | RA |
121 // | ... | callee saves
122 // | A3 | arg3
123 // | A2 | arg2
124 // | A1 | arg1
125 // | A0/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000126 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800127 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800128 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800129 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
130 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800131 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
132 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
133 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800134 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000135 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800136 }
Ian Rogers848871b2013-08-05 10:56:33 -0700137#elif defined(__i386__)
138 // The callee save frame is pointed to by SP.
139 // | argN | |
140 // | ... | |
141 // | arg4 | |
142 // | arg3 spill | | Caller's frame
143 // | arg2 spill | |
144 // | arg1 spill | |
145 // | Method* | ---
146 // | Return |
147 // | EBP,ESI,EDI | callee saves
148 // | EBX | arg3
149 // | EDX | arg2
150 // | ECX | arg1
151 // | EAX/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000152 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800153 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800154 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800155 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
156 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800157 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
158 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
159 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800160 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000161 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800162 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800164 // The callee save frame is pointed to by SP.
165 // | argN | |
166 // | ... | |
167 // | reg. arg spills | | Caller's frame
168 // | Method* | ---
169 // | Return |
170 // | R15 | callee save
171 // | R14 | callee save
172 // | R13 | callee save
173 // | R12 | callee save
174 // | R9 | arg5
175 // | R8 | arg4
176 // | RSI/R6 | arg1
177 // | RBP/R5 | callee save
178 // | RBX/R3 | callee save
179 // | RDX/R2 | arg2
180 // | RCX/R1 | arg3
181 // | XMM7 | float arg 8
182 // | XMM6 | float arg 7
183 // | XMM5 | float arg 6
184 // | XMM4 | float arg 5
185 // | XMM3 | float arg 4
186 // | XMM2 | float arg 3
187 // | XMM1 | float arg 2
188 // | XMM0 | float arg 1
189 // | Padding |
190 // | RDI/Method* | <- sp
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000191 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800192 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800193 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700194 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700195 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800196 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700197 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
198 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800199 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
200 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000201 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
202 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
203 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
204 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
205 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800206 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700207 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
208 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800209 }
210 }
Ian Rogers848871b2013-08-05 10:56:33 -0700211#else
212#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700213#endif
214
Ian Rogers936b37f2014-02-14 00:52:24 -0800215 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100216 // Special handling for proxy methods. Proxy methods are instance methods so the
217 // 'this' object is the 1st argument. They also have the same frame layout as the
218 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
219 // 1st GPR.
220 static mirror::Object* GetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
221 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
222 CHECK(sp->AsMirrorPtr()->IsProxyMethod());
223 CHECK_EQ(kQuickCalleeSaveFrame_RefAndArgs_FrameSize, sp->AsMirrorPtr()->GetFrameSizeInBytes());
224 CHECK_GT(kNumQuickGprArgs, 0u);
225 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
226 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
227 GprIndexToGprOffset(kThisGprIndex);
228 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
229 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
230 }
231
Andreas Gampecf4035a2014-05-28 22:43:01 -0700232 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700234 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700235 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700236 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700237 }
238
Ian Rogers936b37f2014-02-14 00:52:24 -0800239 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700240 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800241 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700242 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700243 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700244 return *reinterpret_cast<uintptr_t*>(lr);
245 }
246
Andreas Gampec200a4a2014-06-16 18:39:09 -0700247 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
248 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
249 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700250 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
251 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
252 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700253 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800254 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
255 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800256 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
257 "Number of Quick FPR arguments unexpected");
258 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
259 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800260 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
261 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800262 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
263 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800264 }
Ian Rogers848871b2013-08-05 10:56:33 -0700265
266 virtual ~QuickArgumentVisitor() {}
267
268 virtual void Visit() = 0;
269
Ian Rogers936b37f2014-02-14 00:52:24 -0800270 Primitive::Type GetParamPrimitiveType() const {
271 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700272 }
273
Ian Rogers13735952014-10-08 12:43:28 -0700274 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800275 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800276 Primitive::Type type = GetParamPrimitiveType();
277 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800278 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
279 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
280 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
281 }
282 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000283 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800284 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700285 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800286 }
287 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800288 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800289 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
290 }
291 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700292 }
293
294 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000295 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 return is_split_long_or_double_;
297 } else {
298 return false; // An optimization for when GPR and FPRs are 64bit.
299 }
Ian Rogers848871b2013-08-05 10:56:33 -0700300 }
301
Ian Rogers936b37f2014-02-14 00:52:24 -0800302 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700303 return GetParamPrimitiveType() == Primitive::kPrimNot;
304 }
305
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700307 Primitive::Type type = GetParamPrimitiveType();
308 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
309 }
310
311 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000312 // The splitted long is always available through the stack.
313 return *reinterpret_cast<uint64_t*>(stack_args_
314 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700315 }
316
317 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800318 // (a) 'stack_args_' should point to the first method's argument
319 // (b) whatever the argument type it is, the 'stack_index_' should
320 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800321 gpr_index_ = 0;
322 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800323 if (kQuickDoubleRegAlignedFloatBackFilled) {
324 fpr_double_index_ = 0;
325 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800326 stack_index_ = 0;
327 if (!is_static_) { // Handle this.
328 cur_type_ = Primitive::kPrimNot;
329 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700330 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800331 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800332 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800333 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800334 }
Ian Rogers848871b2013-08-05 10:56:33 -0700335 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800336 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
337 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
338 switch (cur_type_) {
339 case Primitive::kPrimNot:
340 case Primitive::kPrimBoolean:
341 case Primitive::kPrimByte:
342 case Primitive::kPrimChar:
343 case Primitive::kPrimShort:
344 case Primitive::kPrimInt:
345 is_split_long_or_double_ = false;
346 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800347 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800348 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800349 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800350 }
351 break;
352 case Primitive::kPrimFloat:
353 is_split_long_or_double_ = false;
354 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800355 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800356 if (kQuickSoftFloatAbi) {
357 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800358 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800359 }
360 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800361 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800362 fpr_index_++;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800363 if (kQuickDoubleRegAlignedFloatBackFilled) {
364 // Double should not overlap with float.
365 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
366 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
367 // Float should not overlap with double.
368 if (fpr_index_ % 2 == 0) {
369 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
370 }
371 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800372 }
373 }
374 break;
375 case Primitive::kPrimDouble:
376 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800377 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000378 if (cur_type_ == Primitive::kPrimLong && kAlignPairRegister && gpr_index_ == 0) {
379 // Currently, this is only for ARM, where the first available parameter register
380 // is R1. So we skip it, and use R2 instead.
381 gpr_index_++;
382 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000383 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800384 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800385 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800386 if (kBytesStackArgLocation == 4) {
387 stack_index_+= 2;
388 } else {
389 CHECK_EQ(kBytesStackArgLocation, 8U);
390 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800391 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700392 if (gpr_index_ < kNumQuickGprArgs) {
393 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000394 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700395 if (gpr_index_ < kNumQuickGprArgs) {
396 gpr_index_++;
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700397 }
398 }
399 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800400 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000401 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800402 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800403 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700404 if (kBytesStackArgLocation == 4) {
405 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800406 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700407 CHECK_EQ(kBytesStackArgLocation, 8U);
408 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800409 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800410 if (kQuickDoubleRegAlignedFloatBackFilled) {
411 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
412 fpr_double_index_ += 2;
413 // Float should not overlap with double.
414 if (fpr_index_ % 2 == 0) {
415 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
416 }
417 }
418 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
419 fpr_index_++;
420 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
421 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
422 fpr_index_++;
423 }
424 }
425 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 }
427 break;
428 default:
429 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
430 }
Ian Rogers848871b2013-08-05 10:56:33 -0700431 }
432 }
433
Andreas Gampec200a4a2014-06-16 18:39:09 -0700434 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700435 const bool is_static_;
436 const char* const shorty_;
437 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700438
439 private:
Ian Rogers13735952014-10-08 12:43:28 -0700440 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
441 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
442 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800444 // Index into spilled FPRs.
445 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
446 // holds a higher register number.
447 uint32_t fpr_index_;
448 // Index into spilled FPRs for aligned double.
449 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
450 // terms of singles, may be behind fpr_index.
451 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 uint32_t stack_index_; // Index into arguments on the stack.
453 // The current type of argument during VisitArguments.
454 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700455 // Does a 64bit parameter straddle the register and stack arguments?
456 bool is_split_long_or_double_;
457};
458
Sebastien Hertza836bc92014-11-25 16:30:53 +0100459// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
460// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
461extern "C" mirror::Object* artQuickGetProxyThisObject(StackReference<mirror::ArtMethod>* sp)
462 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
463 return QuickArgumentVisitor::GetProxyThisObject(sp);
464}
465
Ian Rogers848871b2013-08-05 10:56:33 -0700466// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800467class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700468 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700469 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
470 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
471 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700472 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700473
Ian Rogers9758f792014-03-13 09:02:55 -0700474 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700475
476 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800477 ShadowFrame* const sf_;
478 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700479
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700480 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700481};
482
Andreas Gampec200a4a2014-06-16 18:39:09 -0700483void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700484 Primitive::Type type = GetParamPrimitiveType();
485 switch (type) {
486 case Primitive::kPrimLong: // Fall-through.
487 case Primitive::kPrimDouble:
488 if (IsSplitLongOrDouble()) {
489 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
490 } else {
491 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
492 }
493 ++cur_reg_;
494 break;
495 case Primitive::kPrimNot: {
496 StackReference<mirror::Object>* stack_ref =
497 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
498 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
499 }
500 break;
501 case Primitive::kPrimBoolean: // Fall-through.
502 case Primitive::kPrimByte: // Fall-through.
503 case Primitive::kPrimChar: // Fall-through.
504 case Primitive::kPrimShort: // Fall-through.
505 case Primitive::kPrimInt: // Fall-through.
506 case Primitive::kPrimFloat:
507 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
508 break;
509 case Primitive::kPrimVoid:
510 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700511 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700512 }
513 ++cur_reg_;
514}
515
Brian Carlstromea46f952013-07-30 01:26:50 -0700516extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700517 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700518 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
519 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
520 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700521 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700522
523 if (method->IsAbstract()) {
524 ThrowAbstractMethodError(method);
525 return 0;
526 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800527 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700528 const char* old_cause = self->StartAssertNoThreadSuspension(
529 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700530 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800531 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700532 uint16_t num_regs = code_item->registers_size_;
533 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700534 // No last shadow coming from quick.
535 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700536 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700537 uint32_t shorty_len = 0;
538 const char* shorty = method->GetShorty(&shorty_len);
539 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800540 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700541 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800542 const bool needs_initialization =
543 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700544 // Push a transition back into managed code onto the linked list in thread.
545 ManagedStack fragment;
546 self->PushManagedStackFragment(&fragment);
547 self->PushShadowFrame(shadow_frame);
548 self->EndAssertNoThreadSuspension(old_cause);
549
Ian Rogerse94652f2014-12-02 11:13:19 -0800550 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700551 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800552 StackHandleScope<1> hs(self);
553 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700554 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800555 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700556 self->PopManagedStackFragment(fragment);
557 return 0;
558 }
559 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800560 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700561 // Pop transition.
562 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800563 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700564 return result.GetJ();
565 }
566}
567
568// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
569// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800570class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700571 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700572 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
573 const char* shorty, uint32_t shorty_len,
574 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700575 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700576
Ian Rogers9758f792014-03-13 09:02:55 -0700577 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700578
Ian Rogers9758f792014-03-13 09:02:55 -0700579 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800580
Ian Rogers848871b2013-08-05 10:56:33 -0700581 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700582 ScopedObjectAccessUnchecked* const soa_;
583 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800584 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700585 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700586
Ian Rogers848871b2013-08-05 10:56:33 -0700587 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
588};
589
Ian Rogers9758f792014-03-13 09:02:55 -0700590void BuildQuickArgumentVisitor::Visit() {
591 jvalue val;
592 Primitive::Type type = GetParamPrimitiveType();
593 switch (type) {
594 case Primitive::kPrimNot: {
595 StackReference<mirror::Object>* stack_ref =
596 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
597 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
598 references_.push_back(std::make_pair(val.l, stack_ref));
599 break;
600 }
601 case Primitive::kPrimLong: // Fall-through.
602 case Primitive::kPrimDouble:
603 if (IsSplitLongOrDouble()) {
604 val.j = ReadSplitLongParam();
605 } else {
606 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
607 }
608 break;
609 case Primitive::kPrimBoolean: // Fall-through.
610 case Primitive::kPrimByte: // Fall-through.
611 case Primitive::kPrimChar: // Fall-through.
612 case Primitive::kPrimShort: // Fall-through.
613 case Primitive::kPrimInt: // Fall-through.
614 case Primitive::kPrimFloat:
615 val.i = *reinterpret_cast<jint*>(GetParamAddress());
616 break;
617 case Primitive::kPrimVoid:
618 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700619 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700620 }
621 args_->push_back(val);
622}
623
624void BuildQuickArgumentVisitor::FixupReferences() {
625 // Fixup any references which may have changed.
626 for (const auto& pair : references_) {
627 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700628 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700629 }
630}
631
Ian Rogers848871b2013-08-05 10:56:33 -0700632// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
633// which is responsible for recording callee save registers. We explicitly place into jobjects the
634// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
635// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700636extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700637 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700638 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700639 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700640 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
641 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700642 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
643 const char* old_cause =
644 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
645 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700646 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700647 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700648 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
649 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700650 self->VerifyStack();
651 // Start new JNI local reference state.
652 JNIEnvExt* env = self->GetJniEnv();
653 ScopedObjectAccessUnchecked soa(env);
654 ScopedJniEnvLocalRefState env_state(env);
655 // Create local ref. copies of proxy method and the receiver.
656 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
657
658 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700659 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
660 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700661 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700662 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700663 uint32_t shorty_len = 0;
664 const char* shorty = proxy_method->GetShorty(&shorty_len);
665 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700666
Ian Rogers848871b2013-08-05 10:56:33 -0700667 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700668 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700669 args.erase(args.begin());
670
671 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800673 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700674 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
675 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
676
677 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
678 // that performs allocations.
679 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700680 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800681 // Restore references which might have moved.
682 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700683 return result.GetJ();
684}
685
686// Read object references held in arguments from quick frames and place in a JNI local references,
687// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800688class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700689 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700690 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
691 const char* shorty, uint32_t shorty_len,
692 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700693 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700694
Ian Rogers9758f792014-03-13 09:02:55 -0700695 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700696
Ian Rogers9758f792014-03-13 09:02:55 -0700697 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700698
699 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700700 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800701 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700702 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
703
Mathieu Chartier590fee92013-09-13 13:46:47 -0700704 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700705};
706
Ian Rogers9758f792014-03-13 09:02:55 -0700707void RememberForGcArgumentVisitor::Visit() {
708 if (IsParamAReference()) {
709 StackReference<mirror::Object>* stack_ref =
710 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
711 jobject reference =
712 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
713 references_.push_back(std::make_pair(reference, stack_ref));
714 }
715}
716
717void RememberForGcArgumentVisitor::FixupReferences() {
718 // Fixup any references which may have changed.
719 for (const auto& pair : references_) {
720 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700721 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700722 }
723}
724
Ian Rogers848871b2013-08-05 10:56:33 -0700725// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700726extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700727 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700728 Thread* self,
729 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700730 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700731 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700732 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800733 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700734 ScopedObjectAccessUnchecked soa(env);
735 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800736 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700737
738 // Compute details about the called method (avoid GCs)
739 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700740 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700741 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800742 MethodReference called_method(nullptr, 0);
743 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
744 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700745 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
746 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800747 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700748 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700749 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
750 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
751 Instruction::Code instr_code = instr->Opcode();
752 bool is_range;
753 switch (instr_code) {
754 case Instruction::INVOKE_DIRECT:
755 invoke_type = kDirect;
756 is_range = false;
757 break;
758 case Instruction::INVOKE_DIRECT_RANGE:
759 invoke_type = kDirect;
760 is_range = true;
761 break;
762 case Instruction::INVOKE_STATIC:
763 invoke_type = kStatic;
764 is_range = false;
765 break;
766 case Instruction::INVOKE_STATIC_RANGE:
767 invoke_type = kStatic;
768 is_range = true;
769 break;
770 case Instruction::INVOKE_SUPER:
771 invoke_type = kSuper;
772 is_range = false;
773 break;
774 case Instruction::INVOKE_SUPER_RANGE:
775 invoke_type = kSuper;
776 is_range = true;
777 break;
778 case Instruction::INVOKE_VIRTUAL:
779 invoke_type = kVirtual;
780 is_range = false;
781 break;
782 case Instruction::INVOKE_VIRTUAL_RANGE:
783 invoke_type = kVirtual;
784 is_range = true;
785 break;
786 case Instruction::INVOKE_INTERFACE:
787 invoke_type = kInterface;
788 is_range = false;
789 break;
790 case Instruction::INVOKE_INTERFACE_RANGE:
791 invoke_type = kInterface;
792 is_range = true;
793 break;
794 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800795 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
796 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700797 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800798 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700799 } else {
800 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800801 called_method.dex_file = called->GetDexFile();
802 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700803 }
804 uint32_t shorty_len;
805 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800806 called_method.dex_file->GetMethodShorty(
807 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700808 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700809 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800810 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800811 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700812 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800813 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700814 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700815 mirror::Object* dummy = nullptr;
816 HandleWrapper<mirror::Object> h_receiver(
817 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800818 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
819 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700820 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800821 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800822 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700823 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800824 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
825 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800826 if (virtual_or_interface) {
827 // Refine called method based on receiver.
828 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700829
830 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800831 if (invoke_type == kVirtual) {
832 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
833 } else {
834 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
835 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700836
837 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
838 << PrettyTypeOf(receiver) << " "
839 << invoke_type << " " << orig_called->GetVtableIndex();
840
Ian Rogers83883d72013-10-21 21:07:24 -0700841 // 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 -0800842 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -0800843 // Note, called_method.dex_method_index references the dex method before the
844 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
845 // about the name and signature.
846 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800847 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700848 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000849 // the caller's dex file. Since we get here only if the original called was a runtime
850 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800851 DCHECK(!called_method_known_on_entry);
852 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
853 const DexFile* caller_dex_file = called_method.dex_file;
854 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
855 update_dex_cache_method_index =
856 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
857 caller_method_name_and_sig_index);
858 }
859 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
860 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
861 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700862 }
863 }
Ian Rogers848871b2013-08-05 10:56:33 -0700864 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700865 StackHandleScope<1> hs(soa.Self());
866 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700867 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700868 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800869 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700870 } else if (called_class->IsInitializing()) {
871 if (invoke_type == kStatic) {
872 // Class is still initializing, go to oat and grab code (trampoline must be left in place
873 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800874 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700875 } else {
876 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800877 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700878 }
879 } else {
880 DCHECK(called_class->IsErroneous());
881 }
882 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800883 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700884 // Fixup any locally saved objects may have moved during a GC.
885 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700886 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700887 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700888 return code;
889}
890
Andreas Gampec147b002014-03-06 18:11:06 -0800891/*
892 * This class uses a couple of observations to unite the different calling conventions through
893 * a few constants.
894 *
895 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
896 * possible alignment.
897 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
898 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
899 * when we have to split things
900 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
901 * and we can use Int handling directly.
902 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
903 * necessary when widening. Also, widening of Ints will take place implicitly, and the
904 * extension should be compatible with Aarch64, which mandates copying the available bits
905 * into LSB and leaving the rest unspecified.
906 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
907 * the stack.
908 * 6) There is only little endian.
909 *
910 *
911 * Actual work is supposed to be done in a delegate of the template type. The interface is as
912 * follows:
913 *
914 * void PushGpr(uintptr_t): Add a value for the next GPR
915 *
916 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
917 * padding, that is, think the architecture is 32b and aligns 64b.
918 *
919 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
920 * split this if necessary. The current state will have aligned, if
921 * necessary.
922 *
923 * void PushStack(uintptr_t): Push a value to the stack.
924 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700925 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700926 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800927 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700928 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800929 *
930 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700931template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800932 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800933#if defined(__arm__)
934 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800935 static constexpr bool kNativeSoftFloatAbi = true;
936 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800937 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
938
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800939 static constexpr size_t kRegistersNeededForLong = 2;
940 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800941 static constexpr bool kMultiRegistersAligned = true;
942 static constexpr bool kMultiRegistersWidened = false;
943 static constexpr bool kAlignLongOnStack = true;
944 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000945#elif defined(__aarch64__)
946 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
947 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
948 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
949
950 static constexpr size_t kRegistersNeededForLong = 1;
951 static constexpr size_t kRegistersNeededForDouble = 1;
952 static constexpr bool kMultiRegistersAligned = false;
953 static constexpr bool kMultiRegistersWidened = false;
954 static constexpr bool kAlignLongOnStack = false;
955 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800956#elif defined(__mips__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800957 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -0700958 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
959 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800960
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800961 static constexpr size_t kRegistersNeededForLong = 2;
962 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800963 static constexpr bool kMultiRegistersAligned = true;
964 static constexpr bool kMultiRegistersWidened = true;
Douglas Leung735b8552014-10-31 12:21:40 -0700965 static constexpr bool kAlignLongOnStack = true;
966 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800967#elif defined(__i386__)
968 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800969 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800970 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
971 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
972
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800973 static constexpr size_t kRegistersNeededForLong = 2;
974 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700975 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800976 static constexpr bool kMultiRegistersWidened = false;
977 static constexpr bool kAlignLongOnStack = false;
978 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800979#elif defined(__x86_64__)
980 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
981 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
982 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
983
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800984 static constexpr size_t kRegistersNeededForLong = 1;
985 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800986 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800987 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800988 static constexpr bool kAlignLongOnStack = false;
989 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800990#else
991#error "Unsupported architecture"
992#endif
993
Andreas Gampec147b002014-03-06 18:11:06 -0800994 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700995 explicit BuildNativeCallFrameStateMachine(T* delegate)
996 : gpr_index_(kNumNativeGprArgs),
997 fpr_index_(kNumNativeFprArgs),
998 stack_entries_(0),
999 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001000 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1001 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001002 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1003 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001004 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001005
Andreas Gampec200a4a2014-06-16 18:39:09 -07001006 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001007
Ian Rogers1428dce2014-10-21 15:02:15 -07001008 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001009 return gpr_index_ > 0;
1010 }
1011
Andreas Gampec200a4a2014-06-16 18:39:09 -07001012 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001013 if (HavePointerGpr()) {
1014 gpr_index_--;
1015 PushGpr(reinterpret_cast<uintptr_t>(val));
1016 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001017 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001018 PushStack(reinterpret_cast<uintptr_t>(val));
1019 gpr_index_ = 0;
1020 }
1021 }
1022
Ian Rogers1428dce2014-10-21 15:02:15 -07001023 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001024 return gpr_index_ > 0;
1025 }
1026
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001027 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1028 uintptr_t handle = PushHandle(ptr);
1029 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001030 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001031 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001032 } else {
1033 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001034 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001035 gpr_index_ = 0;
1036 }
1037 }
1038
Ian Rogers1428dce2014-10-21 15:02:15 -07001039 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001040 return gpr_index_ > 0;
1041 }
1042
1043 void AdvanceInt(uint32_t val) {
1044 if (HaveIntGpr()) {
1045 gpr_index_--;
1046 PushGpr(val);
1047 } else {
1048 stack_entries_++;
1049 PushStack(val);
1050 gpr_index_ = 0;
1051 }
1052 }
1053
Ian Rogers1428dce2014-10-21 15:02:15 -07001054 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001055 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1056 }
1057
Ian Rogers1428dce2014-10-21 15:02:15 -07001058 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001059 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1060 kAlignLongOnStack && // and when it needs alignment
1061 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1062 }
1063
Ian Rogers1428dce2014-10-21 15:02:15 -07001064 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001065 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1066 kAlignLongOnStack && // and when it needs 8B alignment
1067 (stack_entries_ & 1) == 1; // counter is odd
1068 }
1069
1070 void AdvanceLong(uint64_t val) {
1071 if (HaveLongGpr()) {
1072 if (LongGprNeedsPadding()) {
1073 PushGpr(0);
1074 gpr_index_--;
1075 }
1076 if (kRegistersNeededForLong == 1) {
1077 PushGpr(static_cast<uintptr_t>(val));
1078 } else {
1079 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1080 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1081 }
1082 gpr_index_ -= kRegistersNeededForLong;
1083 } else {
1084 if (LongStackNeedsPadding()) {
1085 PushStack(0);
1086 stack_entries_++;
1087 }
1088 if (kRegistersNeededForLong == 1) {
1089 PushStack(static_cast<uintptr_t>(val));
1090 stack_entries_++;
1091 } else {
1092 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1093 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1094 stack_entries_ += 2;
1095 }
1096 gpr_index_ = 0;
1097 }
1098 }
1099
Ian Rogers1428dce2014-10-21 15:02:15 -07001100 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001101 return fpr_index_ > 0;
1102 }
1103
Andreas Gampec147b002014-03-06 18:11:06 -08001104 void AdvanceFloat(float val) {
1105 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001106 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001107 } else {
1108 if (HaveFloatFpr()) {
1109 fpr_index_--;
1110 if (kRegistersNeededForDouble == 1) {
1111 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001112 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001113 } else {
1114 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001115 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001116 }
1117 } else {
1118 PushFpr4(val);
1119 }
1120 } else {
1121 stack_entries_++;
1122 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1123 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001124 // Note: We need to jump through those hoops to make the compiler happy.
1125 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1126 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001127 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001128 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001129 }
1130 fpr_index_ = 0;
1131 }
1132 }
1133 }
1134
Ian Rogers1428dce2014-10-21 15:02:15 -07001135 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001136 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1137 }
1138
Ian Rogers1428dce2014-10-21 15:02:15 -07001139 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001140 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1141 kAlignDoubleOnStack && // and when it needs alignment
1142 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1143 }
1144
Ian Rogers1428dce2014-10-21 15:02:15 -07001145 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001146 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1147 kAlignDoubleOnStack && // and when it needs 8B alignment
1148 (stack_entries_ & 1) == 1; // counter is odd
1149 }
1150
1151 void AdvanceDouble(uint64_t val) {
1152 if (kNativeSoftFloatAbi) {
1153 AdvanceLong(val);
1154 } else {
1155 if (HaveDoubleFpr()) {
1156 if (DoubleFprNeedsPadding()) {
1157 PushFpr4(0);
1158 fpr_index_--;
1159 }
1160 PushFpr8(val);
1161 fpr_index_ -= kRegistersNeededForDouble;
1162 } else {
1163 if (DoubleStackNeedsPadding()) {
1164 PushStack(0);
1165 stack_entries_++;
1166 }
1167 if (kRegistersNeededForDouble == 1) {
1168 PushStack(static_cast<uintptr_t>(val));
1169 stack_entries_++;
1170 } else {
1171 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1172 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1173 stack_entries_ += 2;
1174 }
1175 fpr_index_ = 0;
1176 }
1177 }
1178 }
1179
Ian Rogers1428dce2014-10-21 15:02:15 -07001180 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001181 return stack_entries_;
1182 }
1183
Ian Rogers1428dce2014-10-21 15:02:15 -07001184 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001185 return kNumNativeGprArgs - gpr_index_;
1186 }
1187
Ian Rogers1428dce2014-10-21 15:02:15 -07001188 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001189 return kNumNativeFprArgs - fpr_index_;
1190 }
1191
1192 private:
1193 void PushGpr(uintptr_t val) {
1194 delegate_->PushGpr(val);
1195 }
1196 void PushFpr4(float val) {
1197 delegate_->PushFpr4(val);
1198 }
1199 void PushFpr8(uint64_t val) {
1200 delegate_->PushFpr8(val);
1201 }
1202 void PushStack(uintptr_t val) {
1203 delegate_->PushStack(val);
1204 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001205 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1206 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001207 }
1208
1209 uint32_t gpr_index_; // Number of free GPRs
1210 uint32_t fpr_index_; // Number of free FPRs
1211 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1212 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001213 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001214};
1215
Andreas Gampec200a4a2014-06-16 18:39:09 -07001216// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1217// in subclasses.
1218//
1219// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1220// them with handles.
1221class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001222 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001223 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1224
1225 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001226
Ian Rogers1428dce2014-10-21 15:02:15 -07001227 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001228 return num_stack_entries_ * sizeof(uintptr_t);
1229 }
1230
Ian Rogers1428dce2014-10-21 15:02:15 -07001231 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001232 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001233 // Align by kStackAlignment.
1234 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001235 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001236 }
1237
Ian Rogers1428dce2014-10-21 15:02:15 -07001238 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1239 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001240 // Assumption is OK right now, as we have soft-float arm
1241 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1242 sp8 -= fregs * sizeof(uintptr_t);
1243 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1244 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1245 sp8 -= iregs * sizeof(uintptr_t);
1246 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1247 return sp8;
1248 }
Andreas Gampec147b002014-03-06 18:11:06 -08001249
Andreas Gampec200a4a2014-06-16 18:39:09 -07001250 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001251 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001252 // Native call stack.
1253 sp8 = LayoutCallStack(sp8);
1254 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001255
Andreas Gampec200a4a2014-06-16 18:39:09 -07001256 // Put fprs and gprs below.
1257 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001258
Andreas Gampec200a4a2014-06-16 18:39:09 -07001259 // Return the new bottom.
1260 return sp8;
1261 }
1262
1263 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001264 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1265 UNUSED(sm);
1266 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001267
1268 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1269 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1270
1271 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001272
1273 for (uint32_t i = 1; i < shorty_len; ++i) {
1274 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1275 switch (cur_type_) {
1276 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001277 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001278 sm.AdvanceHandleScope(
1279 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001280 break;
1281
1282 case Primitive::kPrimBoolean:
1283 case Primitive::kPrimByte:
1284 case Primitive::kPrimChar:
1285 case Primitive::kPrimShort:
1286 case Primitive::kPrimInt:
1287 sm.AdvanceInt(0);
1288 break;
1289 case Primitive::kPrimFloat:
1290 sm.AdvanceFloat(0);
1291 break;
1292 case Primitive::kPrimDouble:
1293 sm.AdvanceDouble(0);
1294 break;
1295 case Primitive::kPrimLong:
1296 sm.AdvanceLong(0);
1297 break;
1298 default:
1299 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001300 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001301 }
1302 }
1303
Ian Rogers1428dce2014-10-21 15:02:15 -07001304 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001305 }
1306
1307 void PushGpr(uintptr_t /* val */) {
1308 // not optimizing registers, yet
1309 }
1310
1311 void PushFpr4(float /* val */) {
1312 // not optimizing registers, yet
1313 }
1314
1315 void PushFpr8(uint64_t /* val */) {
1316 // not optimizing registers, yet
1317 }
1318
1319 void PushStack(uintptr_t /* val */) {
1320 // counting is already done in the superclass
1321 }
1322
Andreas Gampec200a4a2014-06-16 18:39:09 -07001323 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001324 return reinterpret_cast<uintptr_t>(nullptr);
1325 }
1326
Andreas Gampec200a4a2014-06-16 18:39:09 -07001327 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001328 uint32_t num_stack_entries_;
1329};
1330
Andreas Gampec200a4a2014-06-16 18:39:09 -07001331class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001332 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001333 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001334
Andreas Gampec200a4a2014-06-16 18:39:09 -07001335 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1336 // is at *m = sp. Will update to point to the bottom of the save frame.
1337 //
1338 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001339 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1340 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001341 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1342 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001343
Andreas Gampec200a4a2014-06-16 18:39:09 -07001344 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1345
1346 // First, fix up the layout of the callee-save frame.
1347 // We have to squeeze in the HandleScope, and relocate the method pointer.
1348
1349 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001350 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001351
1352 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001353 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1354 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1355
1356 sp8 -= scope_and_method;
1357 // Align by kStackAlignment.
1358 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1359 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1360
1361 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001362 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1363 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001364
1365 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1366 uint8_t* method_pointer = sp8;
1367 StackReference<mirror::ArtMethod>* new_method_ref =
1368 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1369 new_method_ref->Assign(method);
1370 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001371 }
1372
Andreas Gampec200a4a2014-06-16 18:39:09 -07001373 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001374 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001375 // Reference cookie and padding
1376 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001377 }
1378
Andreas Gampec200a4a2014-06-16 18:39:09 -07001379 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1380 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001381 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1382 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001383 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001384 // First, fix up the layout of the callee-save frame.
1385 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001386 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001387
1388 // The bottom of the callee-save frame is now where the method is, *m.
1389 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1390
1391 // Add space for cookie.
1392 LayoutCookie(&sp8);
1393
1394 return sp8;
1395 }
1396
1397 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001398 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001399 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001400 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1401 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1402 Walk(shorty, shorty_len);
1403
1404 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001405 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001406
1407 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1408
1409 // Return the new bottom.
1410 return sp8;
1411 }
1412
1413 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1414
1415 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1416 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1417 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1418
1419 private:
1420 uint32_t num_handle_scope_references_;
1421};
1422
1423uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1424 num_handle_scope_references_++;
1425 return reinterpret_cast<uintptr_t>(nullptr);
1426}
1427
1428void ComputeGenericJniFrameSize::WalkHeader(
1429 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1430 // JNIEnv
1431 sm->AdvancePointer(nullptr);
1432
1433 // Class object or this as first argument
1434 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1435}
1436
1437// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1438// the template requirements of BuildGenericJniFrameStateMachine.
1439class FillNativeCall {
1440 public:
1441 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1442 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1443
1444 virtual ~FillNativeCall() {}
1445
1446 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1447 cur_gpr_reg_ = gpr_regs;
1448 cur_fpr_reg_ = fpr_regs;
1449 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001450 }
1451
1452 void PushGpr(uintptr_t val) {
1453 *cur_gpr_reg_ = val;
1454 cur_gpr_reg_++;
1455 }
1456
1457 void PushFpr4(float val) {
1458 *cur_fpr_reg_ = val;
1459 cur_fpr_reg_++;
1460 }
1461
1462 void PushFpr8(uint64_t val) {
1463 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1464 *tmp = val;
1465 cur_fpr_reg_ += 2;
1466 }
1467
1468 void PushStack(uintptr_t val) {
1469 *cur_stack_arg_ = val;
1470 cur_stack_arg_++;
1471 }
1472
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001473 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001474 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001475 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001476 }
1477
1478 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001479 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001480 uint32_t* cur_fpr_reg_;
1481 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001482};
Andreas Gampec147b002014-03-06 18:11:06 -08001483
Andreas Gampec200a4a2014-06-16 18:39:09 -07001484// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1485// of transitioning into native code.
1486class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1487 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001488 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1489 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001490 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1491 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1492 ComputeGenericJniFrameSize fsc;
1493 uintptr_t* start_gpr_reg;
1494 uint32_t* start_fpr_reg;
1495 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001497 &handle_scope_,
1498 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001499 &start_gpr_reg, &start_fpr_reg);
1500
Andreas Gampec200a4a2014-06-16 18:39:09 -07001501 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1502
1503 // jni environment is always first argument
1504 sm_.AdvancePointer(self->GetJniEnv());
1505
1506 if (is_static) {
1507 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1508 }
1509 }
1510
1511 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1512
1513 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1514
1515 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1516 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1517 return handle_scope_->GetHandle(0).GetReference();
1518 }
1519
Ian Rogers1428dce2014-10-21 15:02:15 -07001520 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001521 return handle_scope_->GetHandle(0).ToJObject();
1522 }
1523
Ian Rogers1428dce2014-10-21 15:02:15 -07001524 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001525 return bottom_of_used_area_;
1526 }
1527
1528 private:
1529 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1530 class FillJniCall FINAL : public FillNativeCall {
1531 public:
1532 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1533 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1534 handle_scope_(handle_scope), cur_entry_(0) {}
1535
1536 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1537
1538 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1539 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1540 handle_scope_ = scope;
1541 cur_entry_ = 0U;
1542 }
1543
1544 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1545 // Initialize padding entries.
1546 size_t expected_slots = handle_scope_->NumberOfReferences();
1547 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001548 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001549 }
1550 DCHECK_NE(cur_entry_, 0U);
1551 }
1552
1553 private:
1554 HandleScope* handle_scope_;
1555 size_t cur_entry_;
1556 };
1557
1558 HandleScope* handle_scope_;
1559 FillJniCall jni_call_;
1560 void* bottom_of_used_area_;
1561
1562 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001563
1564 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1565};
1566
Andreas Gampec200a4a2014-06-16 18:39:09 -07001567uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1568 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001569 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001570 h.Assign(ref);
1571 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1572 cur_entry_++;
1573 return tmp;
1574}
1575
Ian Rogers9758f792014-03-13 09:02:55 -07001576void BuildGenericJniFrameVisitor::Visit() {
1577 Primitive::Type type = GetParamPrimitiveType();
1578 switch (type) {
1579 case Primitive::kPrimLong: {
1580 jlong long_arg;
1581 if (IsSplitLongOrDouble()) {
1582 long_arg = ReadSplitLongParam();
1583 } else {
1584 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1585 }
1586 sm_.AdvanceLong(long_arg);
1587 break;
1588 }
1589 case Primitive::kPrimDouble: {
1590 uint64_t double_arg;
1591 if (IsSplitLongOrDouble()) {
1592 // Read into union so that we don't case to a double.
1593 double_arg = ReadSplitLongParam();
1594 } else {
1595 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1596 }
1597 sm_.AdvanceDouble(double_arg);
1598 break;
1599 }
1600 case Primitive::kPrimNot: {
1601 StackReference<mirror::Object>* stack_ref =
1602 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001603 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001604 break;
1605 }
1606 case Primitive::kPrimFloat:
1607 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1608 break;
1609 case Primitive::kPrimBoolean: // Fall-through.
1610 case Primitive::kPrimByte: // Fall-through.
1611 case Primitive::kPrimChar: // Fall-through.
1612 case Primitive::kPrimShort: // Fall-through.
1613 case Primitive::kPrimInt: // Fall-through.
1614 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1615 break;
1616 case Primitive::kPrimVoid:
1617 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001618 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001619 }
1620}
1621
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001622void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001623 // Clear out rest of the scope.
1624 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001625 // Install HandleScope.
1626 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001627}
1628
Ian Rogers04c31d22014-07-07 21:44:06 -07001629#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001630extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001631#else
1632extern "C" void* artFindNativeMethod(Thread* self);
1633#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001634
Andreas Gampead615172014-04-04 16:20:13 -07001635uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1636 if (lock != nullptr) {
1637 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1638 } else {
1639 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1640 }
1641}
1642
1643void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1644 if (lock != nullptr) {
1645 JniMethodEndSynchronized(cookie, lock, self);
1646 } else {
1647 JniMethodEnd(cookie, self);
1648 }
1649}
1650
Andreas Gampec147b002014-03-06 18:11:06 -08001651/*
1652 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001653 * 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 -08001654 * The final element on the stack is a pointer to the native code.
1655 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001656 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001657 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001658 *
Andreas Gampec147b002014-03-06 18:11:06 -08001659 * The return of this function denotes:
1660 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1661 * 2) An error, if the value is negative.
1662 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001663extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1664 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001666 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001667 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001668 uint32_t shorty_len = 0;
1669 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001670
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001671 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001672 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001673 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001674 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001675
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001677 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001678
Ian Rogerse0dcd462014-03-08 15:21:04 -08001679 self->VerifyStack();
1680
Andreas Gampe90546832014-03-12 18:07:19 -07001681 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001682 uint32_t cookie;
1683 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001684 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001685 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001686 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001687 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001688 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001689 }
1690 } else {
1691 cookie = JniMethodStart(self);
1692 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001693 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001694 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001695
Andreas Gampe90546832014-03-12 18:07:19 -07001696 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001697 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001698
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001699 // There are two cases for the content of nativeCode:
1700 // 1) Pointer to the native function.
1701 // 2) Pointer to the trampoline for native code binding.
1702 // In the second case, we need to execute the binding and continue with the actual native function
1703 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001704 DCHECK(nativeCode != nullptr);
1705 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001706#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001707 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001708#else
1709 nativeCode = artFindNativeMethod(self);
1710#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001711
1712 if (nativeCode == nullptr) {
1713 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001714
1715 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001716 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001717 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001718 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1719 } else {
1720 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1721 }
1722
Andreas Gampec200a4a2014-06-16 18:39:09 -07001723 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001724 }
1725 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001726 }
1727
Andreas Gampec200a4a2014-06-16 18:39:09 -07001728 // Return native code addr(lo) and bottom of alloca address(hi).
1729 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1730 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001731}
1732
1733/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001734 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001735 * unlocking.
1736 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001737extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001738 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001739 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001740 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001741 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001742 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001743
Andreas Gampead615172014-04-04 16:20:13 -07001744 jobject lock = nullptr;
1745 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001746 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1747 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001748 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001749 }
1750
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001751 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001752
1753 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001754 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001755 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001756 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001757
1758 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001759 case 'F': {
1760 if (kRuntimeISA == kX86) {
1761 // Convert back the result to float.
1762 double d = bit_cast<uint64_t, double>(result_f);
1763 return bit_cast<float, uint32_t>(static_cast<float>(d));
1764 } else {
1765 return result_f;
1766 }
1767 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001768 case 'D':
1769 return result_f;
1770 case 'Z':
1771 return result.z;
1772 case 'B':
1773 return result.b;
1774 case 'C':
1775 return result.c;
1776 case 'S':
1777 return result.s;
1778 case 'I':
1779 return result.i;
1780 case 'J':
1781 return result.j;
1782 case 'V':
1783 return 0;
1784 default:
1785 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1786 return 0;
1787 }
1788 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001789}
1790
Andreas Gamped58342c2014-06-05 14:18:08 -07001791// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1792// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001793//
Andreas Gamped58342c2014-06-05 14:18:08 -07001794// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1795// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001796
1797template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001798static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001799 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001800 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001801
1802template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001803static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001804 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001805 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001806 ScopedQuickEntrypointChecks sqec(self);
1807 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001808 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1809 type);
1810 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001811 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1812 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001813 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001814 {
1815 // Remember the args in case a GC happens in FindMethodFromCode.
1816 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1817 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1818 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001819 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1820 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001821 visitor.FixupReferences();
1822 }
1823
Ian Rogerse0a02da2014-12-02 14:10:53 -08001824 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001825 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001826 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001827 }
1828 }
1829 DCHECK(!self->IsExceptionPending());
1830 const void* code = method->GetEntryPointFromQuickCompiledCode();
1831
1832 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001833 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001834 << " location: "
1835 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001836
Andreas Gamped58342c2014-06-05 14:18:08 -07001837 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1838 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001839}
1840
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001841// Explicit artInvokeCommon template function declarations to please analysis tool.
1842#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1843 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001844 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001845 mirror::Object* this_object, \
1846 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001847 Thread* self, \
1848 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001849
1850EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1851EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1852EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1853EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1854EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1855EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1856EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1857EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1858EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1859EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1860#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1861
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001862// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001863extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1864 uint32_t method_idx, mirror::Object* this_object,
1865 mirror::ArtMethod* caller_method, Thread* self,
1866 StackReference<mirror::ArtMethod>* sp)
1867 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1868 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1869 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001870}
1871
Andreas Gampec200a4a2014-06-16 18:39:09 -07001872extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1873 uint32_t method_idx, mirror::Object* this_object,
1874 mirror::ArtMethod* caller_method, Thread* self,
1875 StackReference<mirror::ArtMethod>* sp)
1876 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1877 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1878 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001879}
1880
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1882 uint32_t method_idx, mirror::Object* this_object,
1883 mirror::ArtMethod* caller_method, Thread* self,
1884 StackReference<mirror::ArtMethod>* sp)
1885 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1886 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1887 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001888}
1889
Andreas Gampec200a4a2014-06-16 18:39:09 -07001890extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1891 uint32_t method_idx, mirror::Object* this_object,
1892 mirror::ArtMethod* caller_method, Thread* self,
1893 StackReference<mirror::ArtMethod>* sp)
1894 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1895 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1896 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001897}
1898
Andreas Gampec200a4a2014-06-16 18:39:09 -07001899extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1900 uint32_t method_idx, mirror::Object* this_object,
1901 mirror::ArtMethod* caller_method, Thread* self,
1902 StackReference<mirror::ArtMethod>* sp)
1903 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1904 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1905 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001906}
1907
1908// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001909extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001910 mirror::Object* this_object,
1911 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001912 Thread* self,
1913 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001914 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001915 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001916 mirror::ArtMethod* method;
1917 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1918 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001919 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001920 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1921 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001922 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001923 }
1924 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001925 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001926
1927 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001928 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07001929 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001930
1931 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001932 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001933 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001934 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1935 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1936 Instruction::Code instr_code = instr->Opcode();
1937 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1938 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08001939 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001940 uint32_t dex_method_idx;
1941 if (instr_code == Instruction::INVOKE_INTERFACE) {
1942 dex_method_idx = instr->VRegB_35c();
1943 } else {
1944 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1945 dex_method_idx = instr->VRegB_3rc();
1946 }
1947
Andreas Gampec200a4a2014-06-16 18:39:09 -07001948 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1949 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001950 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001951 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1952 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001953 {
1954 // Remember the args in case a GC happens in FindMethodFromCode.
1955 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1956 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1957 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001958 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001959 self);
1960 visitor.FixupReferences();
1961 }
1962
1963 if (UNLIKELY(method == nullptr)) {
1964 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001965 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001966 }
1967 }
1968 const void* code = method->GetEntryPointFromQuickCompiledCode();
1969
1970 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001971 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001972 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001973
Andreas Gamped58342c2014-06-05 14:18:08 -07001974 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1975 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001976}
1977
Ian Rogers848871b2013-08-05 10:56:33 -07001978} // namespace art