blob: a176a8727cf7092b1e3be8f0f1400de1e8f0f081 [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
Zheng Xu5667fdb2014-10-23 18:29:55 +080062 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
63 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
64 static constexpr size_t kNumQuickGprArgs = 3;
65 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Zheng Xub551fdc2014-07-25 11:49:42 +080066 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
67 arm::ArmCalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
68 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
69 arm::ArmCalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
70 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
71 arm::ArmCalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080072 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000073 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080074 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000075#elif defined(__aarch64__)
76 // The callee save frame is pointed to by SP.
77 // | argN | |
78 // | ... | |
79 // | arg4 | |
80 // | arg3 spill | | Caller's frame
81 // | arg2 spill | |
82 // | arg1 spill | |
83 // | Method* | ---
84 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +080085 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000086 // | : |
Zheng Xub551fdc2014-07-25 11:49:42 +080087 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000088 // | X7 |
89 // | : |
90 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +080091 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +000092 // | : |
93 // | D0 |
94 // | | padding
95 // | Method* | <- sp
96 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +080097 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +000098 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
99 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Zheng Xub551fdc2014-07-25 11:49:42 +0800100 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
101 arm64::Arm64CalleeSaveFpr1Offset(Runtime::kRefsAndArgs); // Offset of first FPR arg.
102 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
103 arm64::Arm64CalleeSaveGpr1Offset(Runtime::kRefsAndArgs); // Offset of first GPR arg.
104 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
105 arm64::Arm64CalleeSaveLrOffset(Runtime::kRefsAndArgs); // Offset of return address.
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000107 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 }
Ian Rogers848871b2013-08-05 10:56:33 -0700109#elif defined(__mips__)
110 // The callee save frame is pointed to by SP.
111 // | argN | |
112 // | ... | |
113 // | arg4 | |
114 // | arg3 spill | | Caller's frame
115 // | arg2 spill | |
116 // | arg1 spill | |
117 // | Method* | ---
118 // | RA |
119 // | ... | callee saves
120 // | A3 | arg3
121 // | A2 | arg2
122 // | A1 | arg1
123 // | A0/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800124 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800125 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800126 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
127 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800128 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
129 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
130 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 60; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800131 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000132 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800133 }
Ian Rogers848871b2013-08-05 10:56:33 -0700134#elif defined(__i386__)
135 // The callee save frame is pointed to by SP.
136 // | argN | |
137 // | ... | |
138 // | arg4 | |
139 // | arg3 spill | | Caller's frame
140 // | arg2 spill | |
141 // | arg1 spill | |
142 // | Method* | ---
143 // | Return |
144 // | EBP,ESI,EDI | callee saves
145 // | EBX | arg3
146 // | EDX | arg2
147 // | ECX | arg1
148 // | EAX/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800149 static constexpr bool kQuickSoftFloatAbi = true; // This is a soft float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800150 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800151 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
152 static constexpr size_t kNumQuickFprArgs = 0; // 0 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800153 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 0; // Offset of first FPR arg.
154 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4; // Offset of first GPR arg.
155 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800156 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000157 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800158 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800160 // The callee save frame is pointed to by SP.
161 // | argN | |
162 // | ... | |
163 // | reg. arg spills | | Caller's frame
164 // | Method* | ---
165 // | Return |
166 // | R15 | callee save
167 // | R14 | callee save
168 // | R13 | callee save
169 // | R12 | callee save
170 // | R9 | arg5
171 // | R8 | arg4
172 // | RSI/R6 | arg1
173 // | RBP/R5 | callee save
174 // | RBX/R3 | callee save
175 // | RDX/R2 | arg2
176 // | RCX/R1 | arg3
177 // | XMM7 | float arg 8
178 // | XMM6 | float arg 7
179 // | XMM5 | float arg 6
180 // | XMM4 | float arg 5
181 // | XMM3 | float arg 4
182 // | XMM2 | float arg 3
183 // | XMM1 | float arg 2
184 // | XMM0 | float arg 1
185 // | Padding |
186 // | RDI/Method* | <- sp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800187 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800188 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700189 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700190 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Ian Rogers936b37f2014-02-14 00:52:24 -0800191 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700192 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
193 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800194 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
195 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000196 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
197 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
198 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
199 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
200 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800201 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700202 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
203 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800204 }
205 }
Ian Rogers848871b2013-08-05 10:56:33 -0700206#else
207#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700208#endif
209
Ian Rogers936b37f2014-02-14 00:52:24 -0800210 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700211 static mirror::ArtMethod* GetCallingMethod(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800212 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700213 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700214 uint8_t* previous_sp = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700215 return reinterpret_cast<StackReference<mirror::ArtMethod>*>(previous_sp)->AsMirrorPtr();
Ian Rogers848871b2013-08-05 10:56:33 -0700216 }
217
Ian Rogers936b37f2014-02-14 00:52:24 -0800218 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700219 static uintptr_t GetCallingPc(StackReference<mirror::ArtMethod>* sp)
Ian Rogers936b37f2014-02-14 00:52:24 -0800220 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700221 DCHECK(sp->AsMirrorPtr()->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700222 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700223 return *reinterpret_cast<uintptr_t*>(lr);
224 }
225
Andreas Gampec200a4a2014-06-16 18:39:09 -0700226 QuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static, const char* shorty,
227 uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
228 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700229 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
230 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
231 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700232 + sizeof(StackReference<mirror::ArtMethod>)), // Skip StackReference<ArtMethod>.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800233 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
234 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800235 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
236 "Number of Quick FPR arguments unexpected");
237 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
238 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800239 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
240 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800241 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
242 "Number of Quick FPR arguments not even");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800243 }
Ian Rogers848871b2013-08-05 10:56:33 -0700244
245 virtual ~QuickArgumentVisitor() {}
246
247 virtual void Visit() = 0;
248
Ian Rogers936b37f2014-02-14 00:52:24 -0800249 Primitive::Type GetParamPrimitiveType() const {
250 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700251 }
252
Ian Rogers13735952014-10-08 12:43:28 -0700253 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800254 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800255 Primitive::Type type = GetParamPrimitiveType();
256 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800257 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
258 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
259 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
260 }
261 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000262 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800263 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700264 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800265 }
266 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800267 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800268 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
269 }
270 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700271 }
272
273 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000274 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800275 return is_split_long_or_double_;
276 } else {
277 return false; // An optimization for when GPR and FPRs are 64bit.
278 }
Ian Rogers848871b2013-08-05 10:56:33 -0700279 }
280
Ian Rogers936b37f2014-02-14 00:52:24 -0800281 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700282 return GetParamPrimitiveType() == Primitive::kPrimNot;
283 }
284
Ian Rogers936b37f2014-02-14 00:52:24 -0800285 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700286 Primitive::Type type = GetParamPrimitiveType();
287 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
288 }
289
290 uint64_t ReadSplitLongParam() const {
291 DCHECK(IsSplitLongOrDouble());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800292 // Read low half from register.
Ian Rogers848871b2013-08-05 10:56:33 -0700293 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800294 // Read high half from the stack. As current stack_index_ indexes the argument, the high part
295 // index should be (stack_index_ + 1).
296 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_
297 + (stack_index_ + 1) * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700298 return (low_half & 0xffffffffULL) | (high_half << 32);
299 }
300
301 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800302 // (a) 'stack_args_' should point to the first method's argument
303 // (b) whatever the argument type it is, the 'stack_index_' should
304 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800305 gpr_index_ = 0;
306 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800307 if (kQuickDoubleRegAlignedFloatBackFilled) {
308 fpr_double_index_ = 0;
309 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800310 stack_index_ = 0;
311 if (!is_static_) { // Handle this.
312 cur_type_ = Primitive::kPrimNot;
313 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700314 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800315 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800316 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800317 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800318 }
Ian Rogers848871b2013-08-05 10:56:33 -0700319 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800320 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
321 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
322 switch (cur_type_) {
323 case Primitive::kPrimNot:
324 case Primitive::kPrimBoolean:
325 case Primitive::kPrimByte:
326 case Primitive::kPrimChar:
327 case Primitive::kPrimShort:
328 case Primitive::kPrimInt:
329 is_split_long_or_double_ = false;
330 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800331 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800332 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800333 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800334 }
335 break;
336 case Primitive::kPrimFloat:
337 is_split_long_or_double_ = false;
338 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800339 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800340 if (kQuickSoftFloatAbi) {
341 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800342 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800343 }
344 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800345 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800346 fpr_index_++;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800347 if (kQuickDoubleRegAlignedFloatBackFilled) {
348 // Double should not overlap with float.
349 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
350 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
351 // Float should not overlap with double.
352 if (fpr_index_ % 2 == 0) {
353 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
354 }
355 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800356 }
357 }
358 break;
359 case Primitive::kPrimDouble:
360 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800361 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000362 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800363 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800364 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800365 if (kBytesStackArgLocation == 4) {
366 stack_index_+= 2;
367 } else {
368 CHECK_EQ(kBytesStackArgLocation, 8U);
369 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800370 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700371 if (gpr_index_ < kNumQuickGprArgs) {
372 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000373 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700374 if (gpr_index_ < kNumQuickGprArgs) {
375 gpr_index_++;
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700376 }
377 }
378 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800379 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000380 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800381 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800382 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700383 if (kBytesStackArgLocation == 4) {
384 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800385 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700386 CHECK_EQ(kBytesStackArgLocation, 8U);
387 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800388 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800389 if (kQuickDoubleRegAlignedFloatBackFilled) {
390 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
391 fpr_double_index_ += 2;
392 // Float should not overlap with double.
393 if (fpr_index_ % 2 == 0) {
394 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
395 }
396 }
397 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
398 fpr_index_++;
399 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
400 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
401 fpr_index_++;
402 }
403 }
404 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800405 }
406 break;
407 default:
408 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
409 }
Ian Rogers848871b2013-08-05 10:56:33 -0700410 }
411 }
412
Andreas Gampec200a4a2014-06-16 18:39:09 -0700413 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700414 const bool is_static_;
415 const char* const shorty_;
416 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700417
418 private:
Ian Rogers13735952014-10-08 12:43:28 -0700419 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
420 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
421 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800422 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800423 // Index into spilled FPRs.
424 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
425 // holds a higher register number.
426 uint32_t fpr_index_;
427 // Index into spilled FPRs for aligned double.
428 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
429 // terms of singles, may be behind fpr_index.
430 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 uint32_t stack_index_; // Index into arguments on the stack.
432 // The current type of argument during VisitArguments.
433 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700434 // Does a 64bit parameter straddle the register and stack arguments?
435 bool is_split_long_or_double_;
436};
437
438// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800439class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700440 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700441 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
442 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
443 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700444 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700445
Ian Rogers9758f792014-03-13 09:02:55 -0700446 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700447
448 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800449 ShadowFrame* const sf_;
450 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700451
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700452 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700453};
454
Andreas Gampec200a4a2014-06-16 18:39:09 -0700455void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700456 Primitive::Type type = GetParamPrimitiveType();
457 switch (type) {
458 case Primitive::kPrimLong: // Fall-through.
459 case Primitive::kPrimDouble:
460 if (IsSplitLongOrDouble()) {
461 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
462 } else {
463 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
464 }
465 ++cur_reg_;
466 break;
467 case Primitive::kPrimNot: {
468 StackReference<mirror::Object>* stack_ref =
469 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
470 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
471 }
472 break;
473 case Primitive::kPrimBoolean: // Fall-through.
474 case Primitive::kPrimByte: // Fall-through.
475 case Primitive::kPrimChar: // Fall-through.
476 case Primitive::kPrimShort: // Fall-through.
477 case Primitive::kPrimInt: // Fall-through.
478 case Primitive::kPrimFloat:
479 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
480 break;
481 case Primitive::kPrimVoid:
482 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700483 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700484 }
485 ++cur_reg_;
486}
487
Brian Carlstromea46f952013-07-30 01:26:50 -0700488extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700489 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700490 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
491 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
492 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700493 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700494
495 if (method->IsAbstract()) {
496 ThrowAbstractMethodError(method);
497 return 0;
498 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800499 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700500 const char* old_cause = self->StartAssertNoThreadSuspension(
501 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700502 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800503 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700504 uint16_t num_regs = code_item->registers_size_;
505 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700506 // No last shadow coming from quick.
507 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700508 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 uint32_t shorty_len = 0;
510 const char* shorty = method->GetShorty(&shorty_len);
511 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800512 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700513 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800514 const bool needs_initialization =
515 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700516 // Push a transition back into managed code onto the linked list in thread.
517 ManagedStack fragment;
518 self->PushManagedStackFragment(&fragment);
519 self->PushShadowFrame(shadow_frame);
520 self->EndAssertNoThreadSuspension(old_cause);
521
Ian Rogerse94652f2014-12-02 11:13:19 -0800522 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700523 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800524 StackHandleScope<1> hs(self);
525 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700526 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Ian Rogerse94652f2014-12-02 11:13:19 -0800527 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(shadow_frame->GetMethod());
Ian Rogers848871b2013-08-05 10:56:33 -0700528 self->PopManagedStackFragment(fragment);
529 return 0;
530 }
531 }
Ian Rogerse94652f2014-12-02 11:13:19 -0800532 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700533 // Pop transition.
534 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800535 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700536 return result.GetJ();
537 }
538}
539
540// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
541// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800542class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700543 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700544 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
545 const char* shorty, uint32_t shorty_len,
546 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700547 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700548
Ian Rogers9758f792014-03-13 09:02:55 -0700549 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700550
Ian Rogers9758f792014-03-13 09:02:55 -0700551 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800552
Ian Rogers848871b2013-08-05 10:56:33 -0700553 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700554 ScopedObjectAccessUnchecked* const soa_;
555 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800556 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700557 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700558
Ian Rogers848871b2013-08-05 10:56:33 -0700559 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
560};
561
Ian Rogers9758f792014-03-13 09:02:55 -0700562void BuildQuickArgumentVisitor::Visit() {
563 jvalue val;
564 Primitive::Type type = GetParamPrimitiveType();
565 switch (type) {
566 case Primitive::kPrimNot: {
567 StackReference<mirror::Object>* stack_ref =
568 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
569 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
570 references_.push_back(std::make_pair(val.l, stack_ref));
571 break;
572 }
573 case Primitive::kPrimLong: // Fall-through.
574 case Primitive::kPrimDouble:
575 if (IsSplitLongOrDouble()) {
576 val.j = ReadSplitLongParam();
577 } else {
578 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
579 }
580 break;
581 case Primitive::kPrimBoolean: // Fall-through.
582 case Primitive::kPrimByte: // Fall-through.
583 case Primitive::kPrimChar: // Fall-through.
584 case Primitive::kPrimShort: // Fall-through.
585 case Primitive::kPrimInt: // Fall-through.
586 case Primitive::kPrimFloat:
587 val.i = *reinterpret_cast<jint*>(GetParamAddress());
588 break;
589 case Primitive::kPrimVoid:
590 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700591 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700592 }
593 args_->push_back(val);
594}
595
596void BuildQuickArgumentVisitor::FixupReferences() {
597 // Fixup any references which may have changed.
598 for (const auto& pair : references_) {
599 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700600 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700601 }
602}
603
Ian Rogers848871b2013-08-05 10:56:33 -0700604// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
605// which is responsible for recording callee save registers. We explicitly place into jobjects the
606// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
607// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700608extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700609 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700610 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700612 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
613 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700614 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
615 const char* old_cause =
616 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
617 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700618 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700619 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700620 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
621 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700622 self->VerifyStack();
623 // Start new JNI local reference state.
624 JNIEnvExt* env = self->GetJniEnv();
625 ScopedObjectAccessUnchecked soa(env);
626 ScopedJniEnvLocalRefState env_state(env);
627 // Create local ref. copies of proxy method and the receiver.
628 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
629
630 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700631 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
632 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700633 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700634 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700635 uint32_t shorty_len = 0;
636 const char* shorty = proxy_method->GetShorty(&shorty_len);
637 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700638
Ian Rogers848871b2013-08-05 10:56:33 -0700639 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700640 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700641 args.erase(args.begin());
642
643 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700644 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Ian Rogerse0a02da2014-12-02 14:10:53 -0800645 DCHECK(interface_method != nullptr) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700646 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
647 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
648
649 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
650 // that performs allocations.
651 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700652 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800653 // Restore references which might have moved.
654 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700655 return result.GetJ();
656}
657
658// Read object references held in arguments from quick frames and place in a JNI local references,
659// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800660class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700661 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700662 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
663 const char* shorty, uint32_t shorty_len,
664 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700665 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700666
Ian Rogers9758f792014-03-13 09:02:55 -0700667 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700668
Ian Rogers9758f792014-03-13 09:02:55 -0700669 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700670
671 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700672 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800673 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700674 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
675
Mathieu Chartier590fee92013-09-13 13:46:47 -0700676 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700677};
678
Ian Rogers9758f792014-03-13 09:02:55 -0700679void RememberForGcArgumentVisitor::Visit() {
680 if (IsParamAReference()) {
681 StackReference<mirror::Object>* stack_ref =
682 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
683 jobject reference =
684 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
685 references_.push_back(std::make_pair(reference, stack_ref));
686 }
687}
688
689void RememberForGcArgumentVisitor::FixupReferences() {
690 // Fixup any references which may have changed.
691 for (const auto& pair : references_) {
692 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700693 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700694 }
695}
696
Ian Rogers848871b2013-08-05 10:56:33 -0700697// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700698extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700699 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700700 Thread* self,
701 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700702 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700703 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700704 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800705 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700706 ScopedObjectAccessUnchecked soa(env);
707 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800708 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700709
710 // Compute details about the called method (avoid GCs)
711 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700712 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700713 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800714 MethodReference called_method(nullptr, 0);
715 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
716 if (!called_method_known_on_entry) {
Ian Rogers848871b2013-08-05 10:56:33 -0700717 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
718 const DexFile::CodeItem* code;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800719 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700720 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700721 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
722 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
723 Instruction::Code instr_code = instr->Opcode();
724 bool is_range;
725 switch (instr_code) {
726 case Instruction::INVOKE_DIRECT:
727 invoke_type = kDirect;
728 is_range = false;
729 break;
730 case Instruction::INVOKE_DIRECT_RANGE:
731 invoke_type = kDirect;
732 is_range = true;
733 break;
734 case Instruction::INVOKE_STATIC:
735 invoke_type = kStatic;
736 is_range = false;
737 break;
738 case Instruction::INVOKE_STATIC_RANGE:
739 invoke_type = kStatic;
740 is_range = true;
741 break;
742 case Instruction::INVOKE_SUPER:
743 invoke_type = kSuper;
744 is_range = false;
745 break;
746 case Instruction::INVOKE_SUPER_RANGE:
747 invoke_type = kSuper;
748 is_range = true;
749 break;
750 case Instruction::INVOKE_VIRTUAL:
751 invoke_type = kVirtual;
752 is_range = false;
753 break;
754 case Instruction::INVOKE_VIRTUAL_RANGE:
755 invoke_type = kVirtual;
756 is_range = true;
757 break;
758 case Instruction::INVOKE_INTERFACE:
759 invoke_type = kInterface;
760 is_range = false;
761 break;
762 case Instruction::INVOKE_INTERFACE_RANGE:
763 invoke_type = kInterface;
764 is_range = true;
765 break;
766 default:
Ian Rogerse0a02da2014-12-02 14:10:53 -0800767 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
768 UNREACHABLE();
Ian Rogers848871b2013-08-05 10:56:33 -0700769 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800770 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700771 } else {
772 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -0800773 called_method.dex_file = called->GetDexFile();
774 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -0700775 }
776 uint32_t shorty_len;
777 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -0800778 called_method.dex_file->GetMethodShorty(
779 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700780 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700781 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800782 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -0800783 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700784 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800785 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700786 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700787 mirror::Object* dummy = nullptr;
788 HandleWrapper<mirror::Object> h_receiver(
789 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -0800790 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
791 called = linker->ResolveMethod(self, called_method.dex_method_index, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700792 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800793 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800794 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700795 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800796 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
797 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800798 if (virtual_or_interface) {
799 // Refine called method based on receiver.
800 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700801
802 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800803 if (invoke_type == kVirtual) {
804 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
805 } else {
806 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
807 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700808
809 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
810 << PrettyTypeOf(receiver) << " "
811 << invoke_type << " " << orig_called->GetVtableIndex();
812
Ian Rogers83883d72013-10-21 21:07:24 -0700813 // 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 -0800814 // of the sharpened method avoiding dirtying the dex cache if possible.
815 uint32_t update_dex_cache_method_index = called_method.dex_method_index;
816 if (!called->HasSameDexCacheResolvedMethods(caller)) {
Ian Rogers83883d72013-10-21 21:07:24 -0700817 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000818 // the caller's dex file. Since we get here only if the original called was a runtime
819 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -0800820 DCHECK(!called_method_known_on_entry);
821 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
822 const DexFile* caller_dex_file = called_method.dex_file;
823 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
824 update_dex_cache_method_index =
825 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
826 caller_method_name_and_sig_index);
827 }
828 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
829 (caller->GetDexCacheResolvedMethod(update_dex_cache_method_index) != called)) {
830 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700831 }
832 }
Ian Rogers848871b2013-08-05 10:56:33 -0700833 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700834 StackHandleScope<1> hs(soa.Self());
835 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700836 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700837 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800838 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700839 } else if (called_class->IsInitializing()) {
840 if (invoke_type == kStatic) {
841 // Class is still initializing, go to oat and grab code (trampoline must be left in place
842 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800843 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700844 } else {
845 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800846 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700847 }
848 } else {
849 DCHECK(called_class->IsErroneous());
850 }
851 }
Ian Rogerse0a02da2014-12-02 14:10:53 -0800852 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700853 // Fixup any locally saved objects may have moved during a GC.
854 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700855 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700856 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700857 return code;
858}
859
Andreas Gampec147b002014-03-06 18:11:06 -0800860/*
861 * This class uses a couple of observations to unite the different calling conventions through
862 * a few constants.
863 *
864 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
865 * possible alignment.
866 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
867 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
868 * when we have to split things
869 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
870 * and we can use Int handling directly.
871 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
872 * necessary when widening. Also, widening of Ints will take place implicitly, and the
873 * extension should be compatible with Aarch64, which mandates copying the available bits
874 * into LSB and leaving the rest unspecified.
875 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
876 * the stack.
877 * 6) There is only little endian.
878 *
879 *
880 * Actual work is supposed to be done in a delegate of the template type. The interface is as
881 * follows:
882 *
883 * void PushGpr(uintptr_t): Add a value for the next GPR
884 *
885 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
886 * padding, that is, think the architecture is 32b and aligns 64b.
887 *
888 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
889 * split this if necessary. The current state will have aligned, if
890 * necessary.
891 *
892 * void PushStack(uintptr_t): Push a value to the stack.
893 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700894 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700895 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800896 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700897 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800898 *
899 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700900template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800901 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800902#if defined(__arm__)
903 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800904 static constexpr bool kNativeSoftFloatAbi = true;
905 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800906 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
907
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800908 static constexpr size_t kRegistersNeededForLong = 2;
909 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800910 static constexpr bool kMultiRegistersAligned = true;
911 static constexpr bool kMultiRegistersWidened = false;
912 static constexpr bool kAlignLongOnStack = true;
913 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000914#elif defined(__aarch64__)
915 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
916 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
917 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
918
919 static constexpr size_t kRegistersNeededForLong = 1;
920 static constexpr size_t kRegistersNeededForDouble = 1;
921 static constexpr bool kMultiRegistersAligned = false;
922 static constexpr bool kMultiRegistersWidened = false;
923 static constexpr bool kAlignLongOnStack = false;
924 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800925#elif defined(__mips__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800926 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -0700927 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
928 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800929
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800930 static constexpr size_t kRegistersNeededForLong = 2;
931 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800932 static constexpr bool kMultiRegistersAligned = true;
933 static constexpr bool kMultiRegistersWidened = true;
Douglas Leung735b8552014-10-31 12:21:40 -0700934 static constexpr bool kAlignLongOnStack = true;
935 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800936#elif defined(__i386__)
937 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800938 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800939 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
940 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
941
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800942 static constexpr size_t kRegistersNeededForLong = 2;
943 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700944 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800945 static constexpr bool kMultiRegistersWidened = false;
946 static constexpr bool kAlignLongOnStack = false;
947 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800948#elif defined(__x86_64__)
949 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
950 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
951 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
952
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800953 static constexpr size_t kRegistersNeededForLong = 1;
954 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800955 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800956 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800957 static constexpr bool kAlignLongOnStack = false;
958 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800959#else
960#error "Unsupported architecture"
961#endif
962
Andreas Gampec147b002014-03-06 18:11:06 -0800963 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700964 explicit BuildNativeCallFrameStateMachine(T* delegate)
965 : gpr_index_(kNumNativeGprArgs),
966 fpr_index_(kNumNativeFprArgs),
967 stack_entries_(0),
968 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800969 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
970 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -0800971 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
972 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -0800973 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800974
Andreas Gampec200a4a2014-06-16 18:39:09 -0700975 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800976
Ian Rogers1428dce2014-10-21 15:02:15 -0700977 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -0800978 return gpr_index_ > 0;
979 }
980
Andreas Gampec200a4a2014-06-16 18:39:09 -0700981 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800982 if (HavePointerGpr()) {
983 gpr_index_--;
984 PushGpr(reinterpret_cast<uintptr_t>(val));
985 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700986 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800987 PushStack(reinterpret_cast<uintptr_t>(val));
988 gpr_index_ = 0;
989 }
990 }
991
Ian Rogers1428dce2014-10-21 15:02:15 -0700992 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -0800993 return gpr_index_ > 0;
994 }
995
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700996 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
997 uintptr_t handle = PushHandle(ptr);
998 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -0800999 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001000 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001001 } else {
1002 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001003 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001004 gpr_index_ = 0;
1005 }
1006 }
1007
Ian Rogers1428dce2014-10-21 15:02:15 -07001008 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001009 return gpr_index_ > 0;
1010 }
1011
1012 void AdvanceInt(uint32_t val) {
1013 if (HaveIntGpr()) {
1014 gpr_index_--;
1015 PushGpr(val);
1016 } else {
1017 stack_entries_++;
1018 PushStack(val);
1019 gpr_index_ = 0;
1020 }
1021 }
1022
Ian Rogers1428dce2014-10-21 15:02:15 -07001023 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001024 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1025 }
1026
Ian Rogers1428dce2014-10-21 15:02:15 -07001027 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001028 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1029 kAlignLongOnStack && // and when it needs alignment
1030 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1031 }
1032
Ian Rogers1428dce2014-10-21 15:02:15 -07001033 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001034 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1035 kAlignLongOnStack && // and when it needs 8B alignment
1036 (stack_entries_ & 1) == 1; // counter is odd
1037 }
1038
1039 void AdvanceLong(uint64_t val) {
1040 if (HaveLongGpr()) {
1041 if (LongGprNeedsPadding()) {
1042 PushGpr(0);
1043 gpr_index_--;
1044 }
1045 if (kRegistersNeededForLong == 1) {
1046 PushGpr(static_cast<uintptr_t>(val));
1047 } else {
1048 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1049 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1050 }
1051 gpr_index_ -= kRegistersNeededForLong;
1052 } else {
1053 if (LongStackNeedsPadding()) {
1054 PushStack(0);
1055 stack_entries_++;
1056 }
1057 if (kRegistersNeededForLong == 1) {
1058 PushStack(static_cast<uintptr_t>(val));
1059 stack_entries_++;
1060 } else {
1061 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1062 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1063 stack_entries_ += 2;
1064 }
1065 gpr_index_ = 0;
1066 }
1067 }
1068
Ian Rogers1428dce2014-10-21 15:02:15 -07001069 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001070 return fpr_index_ > 0;
1071 }
1072
Andreas Gampec147b002014-03-06 18:11:06 -08001073 void AdvanceFloat(float val) {
1074 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001075 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001076 } else {
1077 if (HaveFloatFpr()) {
1078 fpr_index_--;
1079 if (kRegistersNeededForDouble == 1) {
1080 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001081 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001082 } else {
1083 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001084 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001085 }
1086 } else {
1087 PushFpr4(val);
1088 }
1089 } else {
1090 stack_entries_++;
1091 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1092 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001093 // Note: We need to jump through those hoops to make the compiler happy.
1094 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1095 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001096 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001097 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001098 }
1099 fpr_index_ = 0;
1100 }
1101 }
1102 }
1103
Ian Rogers1428dce2014-10-21 15:02:15 -07001104 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001105 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1106 }
1107
Ian Rogers1428dce2014-10-21 15:02:15 -07001108 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001109 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1110 kAlignDoubleOnStack && // and when it needs alignment
1111 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1112 }
1113
Ian Rogers1428dce2014-10-21 15:02:15 -07001114 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001115 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1116 kAlignDoubleOnStack && // and when it needs 8B alignment
1117 (stack_entries_ & 1) == 1; // counter is odd
1118 }
1119
1120 void AdvanceDouble(uint64_t val) {
1121 if (kNativeSoftFloatAbi) {
1122 AdvanceLong(val);
1123 } else {
1124 if (HaveDoubleFpr()) {
1125 if (DoubleFprNeedsPadding()) {
1126 PushFpr4(0);
1127 fpr_index_--;
1128 }
1129 PushFpr8(val);
1130 fpr_index_ -= kRegistersNeededForDouble;
1131 } else {
1132 if (DoubleStackNeedsPadding()) {
1133 PushStack(0);
1134 stack_entries_++;
1135 }
1136 if (kRegistersNeededForDouble == 1) {
1137 PushStack(static_cast<uintptr_t>(val));
1138 stack_entries_++;
1139 } else {
1140 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1141 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1142 stack_entries_ += 2;
1143 }
1144 fpr_index_ = 0;
1145 }
1146 }
1147 }
1148
Ian Rogers1428dce2014-10-21 15:02:15 -07001149 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001150 return stack_entries_;
1151 }
1152
Ian Rogers1428dce2014-10-21 15:02:15 -07001153 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001154 return kNumNativeGprArgs - gpr_index_;
1155 }
1156
Ian Rogers1428dce2014-10-21 15:02:15 -07001157 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001158 return kNumNativeFprArgs - fpr_index_;
1159 }
1160
1161 private:
1162 void PushGpr(uintptr_t val) {
1163 delegate_->PushGpr(val);
1164 }
1165 void PushFpr4(float val) {
1166 delegate_->PushFpr4(val);
1167 }
1168 void PushFpr8(uint64_t val) {
1169 delegate_->PushFpr8(val);
1170 }
1171 void PushStack(uintptr_t val) {
1172 delegate_->PushStack(val);
1173 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001174 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1175 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001176 }
1177
1178 uint32_t gpr_index_; // Number of free GPRs
1179 uint32_t fpr_index_; // Number of free FPRs
1180 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1181 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001182 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001183};
1184
Andreas Gampec200a4a2014-06-16 18:39:09 -07001185// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1186// in subclasses.
1187//
1188// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1189// them with handles.
1190class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001191 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001192 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1193
1194 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001195
Ian Rogers1428dce2014-10-21 15:02:15 -07001196 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001197 return num_stack_entries_ * sizeof(uintptr_t);
1198 }
1199
Ian Rogers1428dce2014-10-21 15:02:15 -07001200 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001201 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001202 // Align by kStackAlignment.
1203 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001204 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001205 }
1206
Ian Rogers1428dce2014-10-21 15:02:15 -07001207 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1208 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001209 // Assumption is OK right now, as we have soft-float arm
1210 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1211 sp8 -= fregs * sizeof(uintptr_t);
1212 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1213 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1214 sp8 -= iregs * sizeof(uintptr_t);
1215 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1216 return sp8;
1217 }
Andreas Gampec147b002014-03-06 18:11:06 -08001218
Andreas Gampec200a4a2014-06-16 18:39:09 -07001219 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001220 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001221 // Native call stack.
1222 sp8 = LayoutCallStack(sp8);
1223 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001224
Andreas Gampec200a4a2014-06-16 18:39:09 -07001225 // Put fprs and gprs below.
1226 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001227
Andreas Gampec200a4a2014-06-16 18:39:09 -07001228 // Return the new bottom.
1229 return sp8;
1230 }
1231
1232 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001233 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1234 UNUSED(sm);
1235 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001236
1237 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1238 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1239
1240 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001241
1242 for (uint32_t i = 1; i < shorty_len; ++i) {
1243 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1244 switch (cur_type_) {
1245 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001246 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001247 sm.AdvanceHandleScope(
1248 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001249 break;
1250
1251 case Primitive::kPrimBoolean:
1252 case Primitive::kPrimByte:
1253 case Primitive::kPrimChar:
1254 case Primitive::kPrimShort:
1255 case Primitive::kPrimInt:
1256 sm.AdvanceInt(0);
1257 break;
1258 case Primitive::kPrimFloat:
1259 sm.AdvanceFloat(0);
1260 break;
1261 case Primitive::kPrimDouble:
1262 sm.AdvanceDouble(0);
1263 break;
1264 case Primitive::kPrimLong:
1265 sm.AdvanceLong(0);
1266 break;
1267 default:
1268 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001269 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001270 }
1271 }
1272
Ian Rogers1428dce2014-10-21 15:02:15 -07001273 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001274 }
1275
1276 void PushGpr(uintptr_t /* val */) {
1277 // not optimizing registers, yet
1278 }
1279
1280 void PushFpr4(float /* val */) {
1281 // not optimizing registers, yet
1282 }
1283
1284 void PushFpr8(uint64_t /* val */) {
1285 // not optimizing registers, yet
1286 }
1287
1288 void PushStack(uintptr_t /* val */) {
1289 // counting is already done in the superclass
1290 }
1291
Andreas Gampec200a4a2014-06-16 18:39:09 -07001292 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001293 return reinterpret_cast<uintptr_t>(nullptr);
1294 }
1295
Andreas Gampec200a4a2014-06-16 18:39:09 -07001296 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001297 uint32_t num_stack_entries_;
1298};
1299
Andreas Gampec200a4a2014-06-16 18:39:09 -07001300class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001301 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001302 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001303
Andreas Gampec200a4a2014-06-16 18:39:09 -07001304 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1305 // is at *m = sp. Will update to point to the bottom of the save frame.
1306 //
1307 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001308 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1309 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001310 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1311 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001312
Andreas Gampec200a4a2014-06-16 18:39:09 -07001313 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1314
1315 // First, fix up the layout of the callee-save frame.
1316 // We have to squeeze in the HandleScope, and relocate the method pointer.
1317
1318 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001319 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001320
1321 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001322 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1323 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1324
1325 sp8 -= scope_and_method;
1326 // Align by kStackAlignment.
1327 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1328 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1329
1330 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001331 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1332 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001333
1334 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1335 uint8_t* method_pointer = sp8;
1336 StackReference<mirror::ArtMethod>* new_method_ref =
1337 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1338 new_method_ref->Assign(method);
1339 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001340 }
1341
Andreas Gampec200a4a2014-06-16 18:39:09 -07001342 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001343 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001344 // Reference cookie and padding
1345 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001346 }
1347
Andreas Gampec200a4a2014-06-16 18:39:09 -07001348 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1349 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001350 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1351 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001352 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001353 // First, fix up the layout of the callee-save frame.
1354 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001355 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001356
1357 // The bottom of the callee-save frame is now where the method is, *m.
1358 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1359
1360 // Add space for cookie.
1361 LayoutCookie(&sp8);
1362
1363 return sp8;
1364 }
1365
1366 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001367 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001368 const char* shorty, uint32_t shorty_len, HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001369 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1371 Walk(shorty, shorty_len);
1372
1373 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001374 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001375
1376 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1377
1378 // Return the new bottom.
1379 return sp8;
1380 }
1381
1382 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1383
1384 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1385 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1386 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1387
1388 private:
1389 uint32_t num_handle_scope_references_;
1390};
1391
1392uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1393 num_handle_scope_references_++;
1394 return reinterpret_cast<uintptr_t>(nullptr);
1395}
1396
1397void ComputeGenericJniFrameSize::WalkHeader(
1398 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1399 // JNIEnv
1400 sm->AdvancePointer(nullptr);
1401
1402 // Class object or this as first argument
1403 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1404}
1405
1406// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1407// the template requirements of BuildGenericJniFrameStateMachine.
1408class FillNativeCall {
1409 public:
1410 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1411 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1412
1413 virtual ~FillNativeCall() {}
1414
1415 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1416 cur_gpr_reg_ = gpr_regs;
1417 cur_fpr_reg_ = fpr_regs;
1418 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001419 }
1420
1421 void PushGpr(uintptr_t val) {
1422 *cur_gpr_reg_ = val;
1423 cur_gpr_reg_++;
1424 }
1425
1426 void PushFpr4(float val) {
1427 *cur_fpr_reg_ = val;
1428 cur_fpr_reg_++;
1429 }
1430
1431 void PushFpr8(uint64_t val) {
1432 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1433 *tmp = val;
1434 cur_fpr_reg_ += 2;
1435 }
1436
1437 void PushStack(uintptr_t val) {
1438 *cur_stack_arg_ = val;
1439 cur_stack_arg_++;
1440 }
1441
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001442 virtual uintptr_t PushHandle(mirror::Object*) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001443 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001444 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001445 }
1446
1447 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001448 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001449 uint32_t* cur_fpr_reg_;
1450 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001451};
Andreas Gampec147b002014-03-06 18:11:06 -08001452
Andreas Gampec200a4a2014-06-16 18:39:09 -07001453// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1454// of transitioning into native code.
1455class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1456 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001457 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1458 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001459 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1460 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1461 ComputeGenericJniFrameSize fsc;
1462 uintptr_t* start_gpr_reg;
1463 uint32_t* start_fpr_reg;
1464 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001465 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001466 &handle_scope_,
1467 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001468 &start_gpr_reg, &start_fpr_reg);
1469
Andreas Gampec200a4a2014-06-16 18:39:09 -07001470 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1471
1472 // jni environment is always first argument
1473 sm_.AdvancePointer(self->GetJniEnv());
1474
1475 if (is_static) {
1476 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1477 }
1478 }
1479
1480 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1481
1482 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1483
1484 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1486 return handle_scope_->GetHandle(0).GetReference();
1487 }
1488
Ian Rogers1428dce2014-10-21 15:02:15 -07001489 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001490 return handle_scope_->GetHandle(0).ToJObject();
1491 }
1492
Ian Rogers1428dce2014-10-21 15:02:15 -07001493 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001494 return bottom_of_used_area_;
1495 }
1496
1497 private:
1498 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1499 class FillJniCall FINAL : public FillNativeCall {
1500 public:
1501 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1502 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1503 handle_scope_(handle_scope), cur_entry_(0) {}
1504
1505 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1506
1507 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1508 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1509 handle_scope_ = scope;
1510 cur_entry_ = 0U;
1511 }
1512
1513 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1514 // Initialize padding entries.
1515 size_t expected_slots = handle_scope_->NumberOfReferences();
1516 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001517 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001518 }
1519 DCHECK_NE(cur_entry_, 0U);
1520 }
1521
1522 private:
1523 HandleScope* handle_scope_;
1524 size_t cur_entry_;
1525 };
1526
1527 HandleScope* handle_scope_;
1528 FillJniCall jni_call_;
1529 void* bottom_of_used_area_;
1530
1531 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001532
1533 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1534};
1535
Andreas Gampec200a4a2014-06-16 18:39:09 -07001536uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1537 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001538 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001539 h.Assign(ref);
1540 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1541 cur_entry_++;
1542 return tmp;
1543}
1544
Ian Rogers9758f792014-03-13 09:02:55 -07001545void BuildGenericJniFrameVisitor::Visit() {
1546 Primitive::Type type = GetParamPrimitiveType();
1547 switch (type) {
1548 case Primitive::kPrimLong: {
1549 jlong long_arg;
1550 if (IsSplitLongOrDouble()) {
1551 long_arg = ReadSplitLongParam();
1552 } else {
1553 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1554 }
1555 sm_.AdvanceLong(long_arg);
1556 break;
1557 }
1558 case Primitive::kPrimDouble: {
1559 uint64_t double_arg;
1560 if (IsSplitLongOrDouble()) {
1561 // Read into union so that we don't case to a double.
1562 double_arg = ReadSplitLongParam();
1563 } else {
1564 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1565 }
1566 sm_.AdvanceDouble(double_arg);
1567 break;
1568 }
1569 case Primitive::kPrimNot: {
1570 StackReference<mirror::Object>* stack_ref =
1571 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001572 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001573 break;
1574 }
1575 case Primitive::kPrimFloat:
1576 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1577 break;
1578 case Primitive::kPrimBoolean: // Fall-through.
1579 case Primitive::kPrimByte: // Fall-through.
1580 case Primitive::kPrimChar: // Fall-through.
1581 case Primitive::kPrimShort: // Fall-through.
1582 case Primitive::kPrimInt: // Fall-through.
1583 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1584 break;
1585 case Primitive::kPrimVoid:
1586 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001587 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001588 }
1589}
1590
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001591void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001592 // Clear out rest of the scope.
1593 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001594 // Install HandleScope.
1595 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001596}
1597
Ian Rogers04c31d22014-07-07 21:44:06 -07001598#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001599extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001600#else
1601extern "C" void* artFindNativeMethod(Thread* self);
1602#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001603
Andreas Gampead615172014-04-04 16:20:13 -07001604uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1605 if (lock != nullptr) {
1606 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1607 } else {
1608 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1609 }
1610}
1611
1612void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1613 if (lock != nullptr) {
1614 JniMethodEndSynchronized(cookie, lock, self);
1615 } else {
1616 JniMethodEnd(cookie, self);
1617 }
1618}
1619
Andreas Gampec147b002014-03-06 18:11:06 -08001620/*
1621 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001622 * 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 -08001623 * The final element on the stack is a pointer to the native code.
1624 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001625 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001626 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001627 *
Andreas Gampec147b002014-03-06 18:11:06 -08001628 * The return of this function denotes:
1629 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1630 * 2) An error, if the value is negative.
1631 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001632extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1633 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001634 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001635 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001636 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001637 uint32_t shorty_len = 0;
1638 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001639
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001640 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001641 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001642 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001643 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001644
Andreas Gampec200a4a2014-06-16 18:39:09 -07001645 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001646 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001647
Ian Rogerse0dcd462014-03-08 15:21:04 -08001648 self->VerifyStack();
1649
Andreas Gampe90546832014-03-12 18:07:19 -07001650 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001651 uint32_t cookie;
1652 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001653 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001654 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001655 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001656 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001657 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001658 }
1659 } else {
1660 cookie = JniMethodStart(self);
1661 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001662 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001663 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001664
Andreas Gampe90546832014-03-12 18:07:19 -07001665 // Retrieve the stored native code.
Mathieu Chartier2d721012014-11-10 11:08:06 -08001666 void* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07001667
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001668 // There are two cases for the content of nativeCode:
1669 // 1) Pointer to the native function.
1670 // 2) Pointer to the trampoline for native code binding.
1671 // In the second case, we need to execute the binding and continue with the actual native function
1672 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001673 DCHECK(nativeCode != nullptr);
1674 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001675#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001676 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001677#else
1678 nativeCode = artFindNativeMethod(self);
1679#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001680
1681 if (nativeCode == nullptr) {
1682 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001683
1684 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001685 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001686 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001687 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1688 } else {
1689 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1690 }
1691
Andreas Gampec200a4a2014-06-16 18:39:09 -07001692 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001693 }
1694 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001695 }
1696
Andreas Gampec200a4a2014-06-16 18:39:09 -07001697 // Return native code addr(lo) and bottom of alloca address(hi).
1698 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1699 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001700}
1701
1702/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001703 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001704 * unlocking.
1705 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001706extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001707 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001708 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001709 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001710 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001711 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001712
Andreas Gampead615172014-04-04 16:20:13 -07001713 jobject lock = nullptr;
1714 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001715 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1716 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001717 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001718 }
1719
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001720 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001721
1722 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001723 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001724 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001725 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001726
1727 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001728 case 'F': {
1729 if (kRuntimeISA == kX86) {
1730 // Convert back the result to float.
1731 double d = bit_cast<uint64_t, double>(result_f);
1732 return bit_cast<float, uint32_t>(static_cast<float>(d));
1733 } else {
1734 return result_f;
1735 }
1736 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001737 case 'D':
1738 return result_f;
1739 case 'Z':
1740 return result.z;
1741 case 'B':
1742 return result.b;
1743 case 'C':
1744 return result.c;
1745 case 'S':
1746 return result.s;
1747 case 'I':
1748 return result.i;
1749 case 'J':
1750 return result.j;
1751 case 'V':
1752 return 0;
1753 default:
1754 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1755 return 0;
1756 }
1757 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001758}
1759
Andreas Gamped58342c2014-06-05 14:18:08 -07001760// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1761// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001762//
Andreas Gamped58342c2014-06-05 14:18:08 -07001763// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1764// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001765
1766template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001767static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001768 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001769 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001770
1771template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001772static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001773 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001774 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001775 ScopedQuickEntrypointChecks sqec(self);
1776 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001777 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1778 type);
1779 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001780 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1781 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001783 {
1784 // Remember the args in case a GC happens in FindMethodFromCode.
1785 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1786 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1787 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001788 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1789 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001790 visitor.FixupReferences();
1791 }
1792
Ian Rogerse0a02da2014-12-02 14:10:53 -08001793 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001794 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001795 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001796 }
1797 }
1798 DCHECK(!self->IsExceptionPending());
1799 const void* code = method->GetEntryPointFromQuickCompiledCode();
1800
1801 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001802 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001803 << " location: "
1804 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001805
Andreas Gamped58342c2014-06-05 14:18:08 -07001806 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1807 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001808}
1809
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001810// Explicit artInvokeCommon template function declarations to please analysis tool.
1811#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1812 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001813 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001814 mirror::Object* this_object, \
1815 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001816 Thread* self, \
1817 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001818
1819EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1820EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1821EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1822EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1823EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1824EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1825EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1826EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1827EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1828EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1829#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1830
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001831// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001832extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1833 uint32_t method_idx, mirror::Object* this_object,
1834 mirror::ArtMethod* caller_method, Thread* self,
1835 StackReference<mirror::ArtMethod>* sp)
1836 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1837 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1838 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001839}
1840
Andreas Gampec200a4a2014-06-16 18:39:09 -07001841extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1842 uint32_t method_idx, mirror::Object* this_object,
1843 mirror::ArtMethod* caller_method, Thread* self,
1844 StackReference<mirror::ArtMethod>* sp)
1845 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1846 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1847 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001848}
1849
Andreas Gampec200a4a2014-06-16 18:39:09 -07001850extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1851 uint32_t method_idx, mirror::Object* this_object,
1852 mirror::ArtMethod* caller_method, Thread* self,
1853 StackReference<mirror::ArtMethod>* sp)
1854 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1855 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1856 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001857}
1858
Andreas Gampec200a4a2014-06-16 18:39:09 -07001859extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1860 uint32_t method_idx, mirror::Object* this_object,
1861 mirror::ArtMethod* caller_method, Thread* self,
1862 StackReference<mirror::ArtMethod>* sp)
1863 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1864 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1865 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001866}
1867
Andreas Gampec200a4a2014-06-16 18:39:09 -07001868extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1869 uint32_t method_idx, mirror::Object* this_object,
1870 mirror::ArtMethod* caller_method, Thread* self,
1871 StackReference<mirror::ArtMethod>* sp)
1872 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1873 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1874 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001875}
1876
1877// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001878extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001879 mirror::Object* this_object,
1880 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001881 Thread* self,
1882 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001883 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001884 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001885 mirror::ArtMethod* method;
1886 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1887 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001888 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001889 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1890 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001891 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001892 }
1893 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001894 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001895
1896 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001897 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07001898 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001899
1900 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001901 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001902 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001903 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1904 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1905 Instruction::Code instr_code = instr->Opcode();
1906 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1907 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
Ian Rogerse0a02da2014-12-02 14:10:53 -08001908 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001909 uint32_t dex_method_idx;
1910 if (instr_code == Instruction::INVOKE_INTERFACE) {
1911 dex_method_idx = instr->VRegB_35c();
1912 } else {
1913 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1914 dex_method_idx = instr->VRegB_3rc();
1915 }
1916
Andreas Gampec200a4a2014-06-16 18:39:09 -07001917 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1918 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001919 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001920 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1921 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001922 {
1923 // Remember the args in case a GC happens in FindMethodFromCode.
1924 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1925 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1926 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001927 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001928 self);
1929 visitor.FixupReferences();
1930 }
1931
1932 if (UNLIKELY(method == nullptr)) {
1933 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001934 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001935 }
1936 }
1937 const void* code = method->GetEntryPointFromQuickCompiledCode();
1938
1939 // When we return, the caller will branch to this address, so it had better not be 0!
Ian Rogerse0a02da2014-12-02 14:10:53 -08001940 DCHECK(code != nullptr) << "Code was null in method: " << PrettyMethod(method)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001941 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001942
Andreas Gamped58342c2014-06-05 14:18:08 -07001943 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1944 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001945}
1946
Ian Rogers848871b2013-08-05 10:56:33 -07001947} // namespace art