blob: e0aab75ddf7834ddde59d7b56e181d3af6545676 [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"
Andreas Gamped58342c2014-06-05 14:18:08 -070024#include "instruction_set.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "interpreter/interpreter.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
Andreas Gampec200a4a2014-06-16 18:39:09 -0700232 + StackArgumentStartFromShorty(is_static, shorty, shorty_len)),
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) {
235 COMPILE_ASSERT(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0), knum_of_quick_fpr_arg_unexpected);
236 COMPILE_ASSERT(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
237 kdouble_align_unexpected);
238 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
239 // next register is even.
240 COMPILE_ASSERT(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
241 knum_quick_fpr_args_not_even);
242 }
Ian Rogers848871b2013-08-05 10:56:33 -0700243
244 virtual ~QuickArgumentVisitor() {}
245
246 virtual void Visit() = 0;
247
Ian Rogers936b37f2014-02-14 00:52:24 -0800248 Primitive::Type GetParamPrimitiveType() const {
249 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700250 }
251
Ian Rogers13735952014-10-08 12:43:28 -0700252 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800253 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800254 Primitive::Type type = GetParamPrimitiveType();
255 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800256 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
257 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
258 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
259 }
260 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000261 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700263 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800264 }
265 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800266 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800267 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
268 }
269 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700270 }
271
272 bool IsSplitLongOrDouble() const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000273 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) || (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800274 return is_split_long_or_double_;
275 } else {
276 return false; // An optimization for when GPR and FPRs are 64bit.
277 }
Ian Rogers848871b2013-08-05 10:56:33 -0700278 }
279
Ian Rogers936b37f2014-02-14 00:52:24 -0800280 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700281 return GetParamPrimitiveType() == Primitive::kPrimNot;
282 }
283
Ian Rogers936b37f2014-02-14 00:52:24 -0800284 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700285 Primitive::Type type = GetParamPrimitiveType();
286 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
287 }
288
289 uint64_t ReadSplitLongParam() const {
290 DCHECK(IsSplitLongOrDouble());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800291 // Read low half from register.
Ian Rogers848871b2013-08-05 10:56:33 -0700292 uint64_t low_half = *reinterpret_cast<uint32_t*>(GetParamAddress());
Zheng Xu5667fdb2014-10-23 18:29:55 +0800293 // Read high half from the stack. As current stack_index_ indexes the argument, the high part
294 // index should be (stack_index_ + 1).
295 uint64_t high_half = *reinterpret_cast<uint32_t*>(stack_args_
296 + (stack_index_ + 1) * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700297 return (low_half & 0xffffffffULL) | (high_half << 32);
298 }
299
300 void VisitArguments() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800301 // (a) 'stack_args_' should point to the first method's argument
302 // (b) whatever the argument type it is, the 'stack_index_' should
303 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800304 gpr_index_ = 0;
305 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800306 if (kQuickDoubleRegAlignedFloatBackFilled) {
307 fpr_double_index_ = 0;
308 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800309 stack_index_ = 0;
310 if (!is_static_) { // Handle this.
311 cur_type_ = Primitive::kPrimNot;
312 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700313 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800314 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800315 if (kNumQuickGprArgs > 0) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800316 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800317 }
Ian Rogers848871b2013-08-05 10:56:33 -0700318 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800319 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
320 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
321 switch (cur_type_) {
322 case Primitive::kPrimNot:
323 case Primitive::kPrimBoolean:
324 case Primitive::kPrimByte:
325 case Primitive::kPrimChar:
326 case Primitive::kPrimShort:
327 case Primitive::kPrimInt:
328 is_split_long_or_double_ = false;
329 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800330 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800331 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800332 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800333 }
334 break;
335 case Primitive::kPrimFloat:
336 is_split_long_or_double_ = false;
337 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800338 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800339 if (kQuickSoftFloatAbi) {
340 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800341 gpr_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800342 }
343 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800344 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800345 fpr_index_++;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800346 if (kQuickDoubleRegAlignedFloatBackFilled) {
347 // Double should not overlap with float.
348 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
349 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
350 // Float should not overlap with double.
351 if (fpr_index_ % 2 == 0) {
352 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
353 }
354 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800355 }
356 }
357 break;
358 case Primitive::kPrimDouble:
359 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800360 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000361 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800362 ((gpr_index_ + 1) == kNumQuickGprArgs);
Ian Rogers936b37f2014-02-14 00:52:24 -0800363 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800364 if (kBytesStackArgLocation == 4) {
365 stack_index_+= 2;
366 } else {
367 CHECK_EQ(kBytesStackArgLocation, 8U);
368 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800369 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700370 if (gpr_index_ < kNumQuickGprArgs) {
371 gpr_index_++;
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000372 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700373 if (gpr_index_ < kNumQuickGprArgs) {
374 gpr_index_++;
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700375 }
376 }
377 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800378 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000379 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800380 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800381 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700382 if (kBytesStackArgLocation == 4) {
383 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800384 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700385 CHECK_EQ(kBytesStackArgLocation, 8U);
386 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800387 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800388 if (kQuickDoubleRegAlignedFloatBackFilled) {
389 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
390 fpr_double_index_ += 2;
391 // Float should not overlap with double.
392 if (fpr_index_ % 2 == 0) {
393 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
394 }
395 }
396 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
397 fpr_index_++;
398 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
399 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
400 fpr_index_++;
401 }
402 }
403 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800404 }
405 break;
406 default:
407 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
408 }
Ian Rogers848871b2013-08-05 10:56:33 -0700409 }
410 }
411
412 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800413 static size_t StackArgumentStartFromShorty(bool is_static, const char* shorty,
414 uint32_t shorty_len) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800415 // 'stack_args_' points to the first method's argument
416 return sizeof(StackReference<mirror::ArtMethod>); // Skip StackReference<ArtMethod>.
Ian Rogers848871b2013-08-05 10:56:33 -0700417 }
418
Andreas Gampec200a4a2014-06-16 18:39:09 -0700419 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700420 const bool is_static_;
421 const char* const shorty_;
422 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700423
424 private:
Ian Rogers13735952014-10-08 12:43:28 -0700425 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
426 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
427 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800428 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800429 // Index into spilled FPRs.
430 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
431 // holds a higher register number.
432 uint32_t fpr_index_;
433 // Index into spilled FPRs for aligned double.
434 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
435 // terms of singles, may be behind fpr_index.
436 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800437 uint32_t stack_index_; // Index into arguments on the stack.
438 // The current type of argument during VisitArguments.
439 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700440 // Does a 64bit parameter straddle the register and stack arguments?
441 bool is_split_long_or_double_;
442};
443
444// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800445class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700446 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700447 BuildQuickShadowFrameVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
448 const char* shorty, uint32_t shorty_len, ShadowFrame* sf,
449 size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700450 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700451
Ian Rogers9758f792014-03-13 09:02:55 -0700452 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700453
454 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800455 ShadowFrame* const sf_;
456 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700457
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700458 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700459};
460
Andreas Gampec200a4a2014-06-16 18:39:09 -0700461void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700462 Primitive::Type type = GetParamPrimitiveType();
463 switch (type) {
464 case Primitive::kPrimLong: // Fall-through.
465 case Primitive::kPrimDouble:
466 if (IsSplitLongOrDouble()) {
467 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
468 } else {
469 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
470 }
471 ++cur_reg_;
472 break;
473 case Primitive::kPrimNot: {
474 StackReference<mirror::Object>* stack_ref =
475 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
476 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
477 }
478 break;
479 case Primitive::kPrimBoolean: // Fall-through.
480 case Primitive::kPrimByte: // Fall-through.
481 case Primitive::kPrimChar: // Fall-through.
482 case Primitive::kPrimShort: // Fall-through.
483 case Primitive::kPrimInt: // Fall-through.
484 case Primitive::kPrimFloat:
485 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
486 break;
487 case Primitive::kPrimVoid:
488 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700489 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700490 }
491 ++cur_reg_;
492}
493
Brian Carlstromea46f952013-07-30 01:26:50 -0700494extern "C" uint64_t artQuickToInterpreterBridge(mirror::ArtMethod* method, Thread* self,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700495 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700496 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
497 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
498 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700499 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700500
501 if (method->IsAbstract()) {
502 ThrowAbstractMethodError(method);
503 return 0;
504 } else {
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800505 DCHECK(!method->IsNative()) << PrettyMethod(method);
Andreas Gampec200a4a2014-06-16 18:39:09 -0700506 const char* old_cause = self->StartAssertNoThreadSuspension(
507 "Building interpreter shadow frame");
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700508 const DexFile::CodeItem* code_item = method->GetCodeItem();
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800509 DCHECK(code_item != nullptr) << PrettyMethod(method);
Ian Rogers848871b2013-08-05 10:56:33 -0700510 uint16_t num_regs = code_item->registers_size_;
511 void* memory = alloca(ShadowFrame::ComputeSize(num_regs));
Andreas Gampec200a4a2014-06-16 18:39:09 -0700512 // No last shadow coming from quick.
513 ShadowFrame* shadow_frame(ShadowFrame::Create(num_regs, nullptr, method, 0, memory));
Ian Rogers848871b2013-08-05 10:56:33 -0700514 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 uint32_t shorty_len = 0;
516 const char* shorty = method->GetShorty(&shorty_len);
517 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800518 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700519 shadow_frame_builder.VisitArguments();
520 // Push a transition back into managed code onto the linked list in thread.
521 ManagedStack fragment;
522 self->PushManagedStackFragment(&fragment);
523 self->PushShadowFrame(shadow_frame);
524 self->EndAssertNoThreadSuspension(old_cause);
525
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700526 StackHandleScope<1> hs(self);
527 MethodHelper mh(hs.NewHandle(method));
528 if (mh.Get()->IsStatic() && !mh.Get()->GetDeclaringClass()->IsInitialized()) {
Ian Rogers848871b2013-08-05 10:56:33 -0700529 // Ensure static method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700530 StackHandleScope<1> hs(self);
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700531 Handle<mirror::Class> h_class(hs.NewHandle(mh.Get()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
Hiroshi Yamauchi5ad97da2014-10-06 10:46:14 -0700533 DCHECK(Thread::Current()->IsExceptionPending()) << PrettyMethod(mh.Get());
Ian Rogers848871b2013-08-05 10:56:33 -0700534 self->PopManagedStackFragment(fragment);
535 return 0;
536 }
537 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700538 JValue result = interpreter::EnterInterpreterFromEntryPoint(self, &mh, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700539 // Pop transition.
540 self->PopManagedStackFragment(fragment);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800541 // No need to restore the args since the method has already been run by the interpreter.
Ian Rogers848871b2013-08-05 10:56:33 -0700542 return result.GetJ();
543 }
544}
545
546// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
547// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800548class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700549 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700550 BuildQuickArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
551 const char* shorty, uint32_t shorty_len,
552 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700553 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700554
Ian Rogers9758f792014-03-13 09:02:55 -0700555 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700556
Ian Rogers9758f792014-03-13 09:02:55 -0700557 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800558
Ian Rogers848871b2013-08-05 10:56:33 -0700559 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700560 ScopedObjectAccessUnchecked* const soa_;
561 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800562 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700563 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700564
Ian Rogers848871b2013-08-05 10:56:33 -0700565 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
566};
567
Ian Rogers9758f792014-03-13 09:02:55 -0700568void BuildQuickArgumentVisitor::Visit() {
569 jvalue val;
570 Primitive::Type type = GetParamPrimitiveType();
571 switch (type) {
572 case Primitive::kPrimNot: {
573 StackReference<mirror::Object>* stack_ref =
574 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
575 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
576 references_.push_back(std::make_pair(val.l, stack_ref));
577 break;
578 }
579 case Primitive::kPrimLong: // Fall-through.
580 case Primitive::kPrimDouble:
581 if (IsSplitLongOrDouble()) {
582 val.j = ReadSplitLongParam();
583 } else {
584 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
585 }
586 break;
587 case Primitive::kPrimBoolean: // Fall-through.
588 case Primitive::kPrimByte: // Fall-through.
589 case Primitive::kPrimChar: // Fall-through.
590 case Primitive::kPrimShort: // Fall-through.
591 case Primitive::kPrimInt: // Fall-through.
592 case Primitive::kPrimFloat:
593 val.i = *reinterpret_cast<jint*>(GetParamAddress());
594 break;
595 case Primitive::kPrimVoid:
596 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700597 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700598 }
599 args_->push_back(val);
600}
601
602void BuildQuickArgumentVisitor::FixupReferences() {
603 // Fixup any references which may have changed.
604 for (const auto& pair : references_) {
605 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700606 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700607 }
608}
609
Ian Rogers848871b2013-08-05 10:56:33 -0700610// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
611// which is responsible for recording callee save registers. We explicitly place into jobjects the
612// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
613// field within the proxy object, which will box the primitive arguments and deal with error cases.
Brian Carlstromea46f952013-07-30 01:26:50 -0700614extern "C" uint64_t artQuickProxyInvokeHandler(mirror::ArtMethod* proxy_method,
Ian Rogers848871b2013-08-05 10:56:33 -0700615 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700616 Thread* self, StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700617 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromd3633d52013-08-20 21:06:26 -0700618 DCHECK(proxy_method->IsProxyMethod()) << PrettyMethod(proxy_method);
619 DCHECK(receiver->GetClass()->IsProxyClass()) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700620 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
621 const char* old_cause =
622 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
623 // Register the top of the managed stack, making stack crawlable.
Jeff Haof0a3f092014-07-24 16:26:09 -0700624 DCHECK_EQ(sp->AsMirrorPtr(), proxy_method) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700625 DCHECK_EQ(proxy_method->GetFrameSizeInBytes(),
Brian Carlstromd3633d52013-08-20 21:06:26 -0700626 Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs)->GetFrameSizeInBytes())
627 << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700628 self->VerifyStack();
629 // Start new JNI local reference state.
630 JNIEnvExt* env = self->GetJniEnv();
631 ScopedObjectAccessUnchecked soa(env);
632 ScopedJniEnvLocalRefState env_state(env);
633 // Create local ref. copies of proxy method and the receiver.
634 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
635
636 // Placing arguments into args vector and remove the receiver.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700637 mirror::ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy();
638 CHECK(!non_proxy_method->IsStatic()) << PrettyMethod(proxy_method) << " "
Andreas Gampec200a4a2014-06-16 18:39:09 -0700639 << PrettyMethod(non_proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700640 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700641 uint32_t shorty_len = 0;
642 const char* shorty = proxy_method->GetShorty(&shorty_len);
643 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700644
Ian Rogers848871b2013-08-05 10:56:33 -0700645 local_ref_visitor.VisitArguments();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700646 DCHECK_GT(args.size(), 0U) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700647 args.erase(args.begin());
648
649 // Convert proxy method into expected interface method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 mirror::ArtMethod* interface_method = proxy_method->FindOverriddenMethod();
Brian Carlstromd3633d52013-08-20 21:06:26 -0700651 DCHECK(interface_method != NULL) << PrettyMethod(proxy_method);
Ian Rogers848871b2013-08-05 10:56:33 -0700652 DCHECK(!interface_method->IsProxyMethod()) << PrettyMethod(interface_method);
653 jobject interface_method_jobj = soa.AddLocalReference<jobject>(interface_method);
654
655 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
656 // that performs allocations.
657 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700658 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800659 // Restore references which might have moved.
660 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700661 return result.GetJ();
662}
663
664// Read object references held in arguments from quick frames and place in a JNI local references,
665// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800666class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700667 public:
Andreas Gampecf4035a2014-05-28 22:43:01 -0700668 RememberForGcArgumentVisitor(StackReference<mirror::ArtMethod>* sp, bool is_static,
669 const char* shorty, uint32_t shorty_len,
670 ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700671 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700672
Ian Rogers9758f792014-03-13 09:02:55 -0700673 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700674
Ian Rogers9758f792014-03-13 09:02:55 -0700675 void FixupReferences() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700676
677 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700678 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800679 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700680 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
681
Mathieu Chartier590fee92013-09-13 13:46:47 -0700682 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700683};
684
Ian Rogers9758f792014-03-13 09:02:55 -0700685void RememberForGcArgumentVisitor::Visit() {
686 if (IsParamAReference()) {
687 StackReference<mirror::Object>* stack_ref =
688 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
689 jobject reference =
690 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
691 references_.push_back(std::make_pair(reference, stack_ref));
692 }
693}
694
695void RememberForGcArgumentVisitor::FixupReferences() {
696 // Fixup any references which may have changed.
697 for (const auto& pair : references_) {
698 pair.second->Assign(soa_->Decode<mirror::Object*>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700699 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700700 }
701}
702
Ian Rogers848871b2013-08-05 10:56:33 -0700703// Lazily resolve a method for quick. Called by stub code.
Brian Carlstromea46f952013-07-30 01:26:50 -0700704extern "C" const void* artQuickResolutionTrampoline(mirror::ArtMethod* called,
Ian Rogers848871b2013-08-05 10:56:33 -0700705 mirror::Object* receiver,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700706 Thread* self,
707 StackReference<mirror::ArtMethod>* sp)
Ian Rogers848871b2013-08-05 10:56:33 -0700708 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700709 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700710 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800711 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -0700712 ScopedObjectAccessUnchecked soa(env);
713 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800714 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -0700715
716 // Compute details about the called method (avoid GCs)
717 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700718 mirror::ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogers848871b2013-08-05 10:56:33 -0700719 InvokeType invoke_type;
720 const DexFile* dex_file;
721 uint32_t dex_method_idx;
722 if (called->IsRuntimeMethod()) {
723 uint32_t dex_pc = caller->ToDexPc(QuickArgumentVisitor::GetCallingPc(sp));
724 const DexFile::CodeItem* code;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700725 dex_file = caller->GetDexFile();
726 code = caller->GetCodeItem();
Ian Rogers848871b2013-08-05 10:56:33 -0700727 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
728 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
729 Instruction::Code instr_code = instr->Opcode();
730 bool is_range;
731 switch (instr_code) {
732 case Instruction::INVOKE_DIRECT:
733 invoke_type = kDirect;
734 is_range = false;
735 break;
736 case Instruction::INVOKE_DIRECT_RANGE:
737 invoke_type = kDirect;
738 is_range = true;
739 break;
740 case Instruction::INVOKE_STATIC:
741 invoke_type = kStatic;
742 is_range = false;
743 break;
744 case Instruction::INVOKE_STATIC_RANGE:
745 invoke_type = kStatic;
746 is_range = true;
747 break;
748 case Instruction::INVOKE_SUPER:
749 invoke_type = kSuper;
750 is_range = false;
751 break;
752 case Instruction::INVOKE_SUPER_RANGE:
753 invoke_type = kSuper;
754 is_range = true;
755 break;
756 case Instruction::INVOKE_VIRTUAL:
757 invoke_type = kVirtual;
758 is_range = false;
759 break;
760 case Instruction::INVOKE_VIRTUAL_RANGE:
761 invoke_type = kVirtual;
762 is_range = true;
763 break;
764 case Instruction::INVOKE_INTERFACE:
765 invoke_type = kInterface;
766 is_range = false;
767 break;
768 case Instruction::INVOKE_INTERFACE_RANGE:
769 invoke_type = kInterface;
770 is_range = true;
771 break;
772 default:
773 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(NULL);
774 // Avoid used uninitialized warnings.
775 invoke_type = kDirect;
776 is_range = false;
777 }
778 dex_method_idx = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
Ian Rogers848871b2013-08-05 10:56:33 -0700779 } else {
780 invoke_type = kStatic;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700781 dex_file = called->GetDexFile();
Ian Rogers848871b2013-08-05 10:56:33 -0700782 dex_method_idx = called->GetDexMethodIndex();
783 }
784 uint32_t shorty_len;
785 const char* shorty =
786 dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700787 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -0700788 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800789 self->EndAssertNoThreadSuspension(old_cause);
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800790 bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -0700791 // Resolve method filling in dex cache.
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700792 if (UNLIKELY(called->IsRuntimeMethod())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700793 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700794 mirror::Object* dummy = nullptr;
795 HandleWrapper<mirror::Object> h_receiver(
796 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
797 called = linker->ResolveMethod(self, dex_method_idx, &caller, invoke_type);
Ian Rogers848871b2013-08-05 10:56:33 -0700798 }
799 const void* code = NULL;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800800 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -0700801 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -0800802 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
803 << PrettyMethod(called) << " " << invoke_type;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800804 if (virtual_or_interface) {
805 // Refine called method based on receiver.
806 CHECK(receiver != nullptr) << invoke_type;
Mingyao Yangf4867782014-05-05 11:55:02 -0700807
808 mirror::ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -0800809 if (invoke_type == kVirtual) {
810 called = receiver->GetClass()->FindVirtualMethodForVirtual(called);
811 } else {
812 called = receiver->GetClass()->FindVirtualMethodForInterface(called);
813 }
Mingyao Yangf4867782014-05-05 11:55:02 -0700814
815 CHECK(called != nullptr) << PrettyMethod(orig_called) << " "
816 << PrettyTypeOf(receiver) << " "
817 << invoke_type << " " << orig_called->GetVtableIndex();
818
Ian Rogers83883d72013-10-21 21:07:24 -0700819 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
820 // of the sharpened method.
Andreas Gampe58a5af82014-07-31 16:23:49 -0700821 if (called->HasSameDexCacheResolvedMethods(caller)) {
822 caller->SetDexCacheResolvedMethod(called->GetDexMethodIndex(), called);
Ian Rogers83883d72013-10-21 21:07:24 -0700823 } else {
824 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000825 // the caller's dex file. Since we get here only if the original called was a runtime
826 // method, we've got the correct dex_file and a dex_method_idx from above.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700827 DCHECK_EQ(caller->GetDexFile(), dex_file);
828 StackHandleScope<1> hs(self);
829 MethodHelper mh(hs.NewHandle(called));
830 uint32_t method_index = mh.FindDexMethodIndexInOtherDexFile(*dex_file, dex_method_idx);
Ian Rogers83883d72013-10-21 21:07:24 -0700831 if (method_index != DexFile::kDexNoIndex) {
Andreas Gampe58a5af82014-07-31 16:23:49 -0700832 caller->SetDexCacheResolvedMethod(method_index, called);
Ian Rogers83883d72013-10-21 21:07:24 -0700833 }
834 }
835 }
Ian Rogers848871b2013-08-05 10:56:33 -0700836 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700837 StackHandleScope<1> hs(soa.Self());
838 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700839 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -0700840 if (LIKELY(called_class->IsInitialized())) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800841 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700842 } else if (called_class->IsInitializing()) {
843 if (invoke_type == kStatic) {
844 // Class is still initializing, go to oat and grab code (trampoline must be left in place
845 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -0800846 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700847 } else {
848 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800849 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -0700850 }
851 } else {
852 DCHECK(called_class->IsErroneous());
853 }
854 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800855 CHECK_EQ(code == NULL, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700856 // Fixup any locally saved objects may have moved during a GC.
857 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700858 // Place called method in callee-save frame to be placed as first argument to quick method.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700859 sp->Assign(called);
Ian Rogers848871b2013-08-05 10:56:33 -0700860 return code;
861}
862
Andreas Gampec147b002014-03-06 18:11:06 -0800863/*
864 * This class uses a couple of observations to unite the different calling conventions through
865 * a few constants.
866 *
867 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
868 * possible alignment.
869 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
870 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
871 * when we have to split things
872 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
873 * and we can use Int handling directly.
874 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
875 * necessary when widening. Also, widening of Ints will take place implicitly, and the
876 * extension should be compatible with Aarch64, which mandates copying the available bits
877 * into LSB and leaving the rest unspecified.
878 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
879 * the stack.
880 * 6) There is only little endian.
881 *
882 *
883 * Actual work is supposed to be done in a delegate of the template type. The interface is as
884 * follows:
885 *
886 * void PushGpr(uintptr_t): Add a value for the next GPR
887 *
888 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
889 * padding, that is, think the architecture is 32b and aligns 64b.
890 *
891 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
892 * split this if necessary. The current state will have aligned, if
893 * necessary.
894 *
895 * void PushStack(uintptr_t): Push a value to the stack.
896 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700897 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700898 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -0800899 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700900 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -0800901 *
902 */
Andreas Gampec200a4a2014-06-16 18:39:09 -0700903template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -0800904 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800905#if defined(__arm__)
906 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -0800907 static constexpr bool kNativeSoftFloatAbi = true;
908 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800909 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
910
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800911 static constexpr size_t kRegistersNeededForLong = 2;
912 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800913 static constexpr bool kMultiRegistersAligned = true;
914 static constexpr bool kMultiRegistersWidened = false;
915 static constexpr bool kAlignLongOnStack = true;
916 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000917#elif defined(__aarch64__)
918 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
919 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
920 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
921
922 static constexpr size_t kRegistersNeededForLong = 1;
923 static constexpr size_t kRegistersNeededForDouble = 1;
924 static constexpr bool kMultiRegistersAligned = false;
925 static constexpr bool kMultiRegistersWidened = false;
926 static constexpr bool kAlignLongOnStack = false;
927 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800928#elif defined(__mips__)
929 // TODO: These are all dummy values!
930 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
931 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
932 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
933
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800934 static constexpr size_t kRegistersNeededForLong = 2;
935 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -0800936 static constexpr bool kMultiRegistersAligned = true;
937 static constexpr bool kMultiRegistersWidened = true;
938 static constexpr bool kAlignLongOnStack = false;
939 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800940#elif defined(__i386__)
941 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -0800942 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800943 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
944 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
945
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800946 static constexpr size_t kRegistersNeededForLong = 2;
947 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700948 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampec147b002014-03-06 18:11:06 -0800949 static constexpr bool kMultiRegistersWidened = false;
950 static constexpr bool kAlignLongOnStack = false;
951 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800952#elif defined(__x86_64__)
953 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
954 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
955 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
956
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800957 static constexpr size_t kRegistersNeededForLong = 1;
958 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -0800959 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe7a0e5042014-03-07 13:03:19 -0800960 static constexpr bool kMultiRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -0800961 static constexpr bool kAlignLongOnStack = false;
962 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800963#else
964#error "Unsupported architecture"
965#endif
966
Andreas Gampec147b002014-03-06 18:11:06 -0800967 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700968 explicit BuildNativeCallFrameStateMachine(T* delegate)
969 : gpr_index_(kNumNativeGprArgs),
970 fpr_index_(kNumNativeFprArgs),
971 stack_entries_(0),
972 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -0800973 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
974 // the next register is even; counting down is just to make the compiler happy...
Zheng Xu5667fdb2014-10-23 18:29:55 +0800975 COMPILE_ASSERT(kNumNativeGprArgs % 2 == 0U, knum_native_gpr_args_not_even);
976 COMPILE_ASSERT(kNumNativeFprArgs % 2 == 0U, knum_native_fpr_args_not_even);
Andreas Gampec147b002014-03-06 18:11:06 -0800977 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800978
Andreas Gampec200a4a2014-06-16 18:39:09 -0700979 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -0800980
Ian Rogers1428dce2014-10-21 15:02:15 -0700981 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -0800982 return gpr_index_ > 0;
983 }
984
Andreas Gampec200a4a2014-06-16 18:39:09 -0700985 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -0800986 if (HavePointerGpr()) {
987 gpr_index_--;
988 PushGpr(reinterpret_cast<uintptr_t>(val));
989 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700990 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -0800991 PushStack(reinterpret_cast<uintptr_t>(val));
992 gpr_index_ = 0;
993 }
994 }
995
Ian Rogers1428dce2014-10-21 15:02:15 -0700996 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -0800997 return gpr_index_ > 0;
998 }
999
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001000 void AdvanceHandleScope(mirror::Object* ptr) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1001 uintptr_t handle = PushHandle(ptr);
1002 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001003 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001004 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001005 } else {
1006 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001007 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001008 gpr_index_ = 0;
1009 }
1010 }
1011
Ian Rogers1428dce2014-10-21 15:02:15 -07001012 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001013 return gpr_index_ > 0;
1014 }
1015
1016 void AdvanceInt(uint32_t val) {
1017 if (HaveIntGpr()) {
1018 gpr_index_--;
1019 PushGpr(val);
1020 } else {
1021 stack_entries_++;
1022 PushStack(val);
1023 gpr_index_ = 0;
1024 }
1025 }
1026
Ian Rogers1428dce2014-10-21 15:02:15 -07001027 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001028 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1029 }
1030
Ian Rogers1428dce2014-10-21 15:02:15 -07001031 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001032 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1033 kAlignLongOnStack && // and when it needs alignment
1034 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1035 }
1036
Ian Rogers1428dce2014-10-21 15:02:15 -07001037 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001038 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1039 kAlignLongOnStack && // and when it needs 8B alignment
1040 (stack_entries_ & 1) == 1; // counter is odd
1041 }
1042
1043 void AdvanceLong(uint64_t val) {
1044 if (HaveLongGpr()) {
1045 if (LongGprNeedsPadding()) {
1046 PushGpr(0);
1047 gpr_index_--;
1048 }
1049 if (kRegistersNeededForLong == 1) {
1050 PushGpr(static_cast<uintptr_t>(val));
1051 } else {
1052 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1053 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1054 }
1055 gpr_index_ -= kRegistersNeededForLong;
1056 } else {
1057 if (LongStackNeedsPadding()) {
1058 PushStack(0);
1059 stack_entries_++;
1060 }
1061 if (kRegistersNeededForLong == 1) {
1062 PushStack(static_cast<uintptr_t>(val));
1063 stack_entries_++;
1064 } else {
1065 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1066 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1067 stack_entries_ += 2;
1068 }
1069 gpr_index_ = 0;
1070 }
1071 }
1072
Ian Rogers1428dce2014-10-21 15:02:15 -07001073 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001074 return fpr_index_ > 0;
1075 }
1076
Andreas Gampec147b002014-03-06 18:11:06 -08001077 void AdvanceFloat(float val) {
1078 if (kNativeSoftFloatAbi) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001079 AdvanceInt(bit_cast<float, uint32_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001080 } else {
1081 if (HaveFloatFpr()) {
1082 fpr_index_--;
1083 if (kRegistersNeededForDouble == 1) {
1084 if (kMultiRegistersWidened) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001085 PushFpr8(bit_cast<double, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001086 } else {
1087 // No widening, just use the bits.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001088 PushFpr8(bit_cast<float, uint64_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001089 }
1090 } else {
1091 PushFpr4(val);
1092 }
1093 } else {
1094 stack_entries_++;
1095 if (kRegistersNeededForDouble == 1 && kMultiRegistersWidened) {
1096 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001097 // Note: We need to jump through those hoops to make the compiler happy.
1098 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
1099 PushStack(static_cast<uintptr_t>(bit_cast<double, uint64_t>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001100 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001101 PushStack(bit_cast<float, uintptr_t>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001102 }
1103 fpr_index_ = 0;
1104 }
1105 }
1106 }
1107
Ian Rogers1428dce2014-10-21 15:02:15 -07001108 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001109 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1110 }
1111
Ian Rogers1428dce2014-10-21 15:02:15 -07001112 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001113 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1114 kAlignDoubleOnStack && // and when it needs alignment
1115 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1116 }
1117
Ian Rogers1428dce2014-10-21 15:02:15 -07001118 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001119 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1120 kAlignDoubleOnStack && // and when it needs 8B alignment
1121 (stack_entries_ & 1) == 1; // counter is odd
1122 }
1123
1124 void AdvanceDouble(uint64_t val) {
1125 if (kNativeSoftFloatAbi) {
1126 AdvanceLong(val);
1127 } else {
1128 if (HaveDoubleFpr()) {
1129 if (DoubleFprNeedsPadding()) {
1130 PushFpr4(0);
1131 fpr_index_--;
1132 }
1133 PushFpr8(val);
1134 fpr_index_ -= kRegistersNeededForDouble;
1135 } else {
1136 if (DoubleStackNeedsPadding()) {
1137 PushStack(0);
1138 stack_entries_++;
1139 }
1140 if (kRegistersNeededForDouble == 1) {
1141 PushStack(static_cast<uintptr_t>(val));
1142 stack_entries_++;
1143 } else {
1144 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1145 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1146 stack_entries_ += 2;
1147 }
1148 fpr_index_ = 0;
1149 }
1150 }
1151 }
1152
Ian Rogers1428dce2014-10-21 15:02:15 -07001153 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001154 return stack_entries_;
1155 }
1156
Ian Rogers1428dce2014-10-21 15:02:15 -07001157 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001158 return kNumNativeGprArgs - gpr_index_;
1159 }
1160
Ian Rogers1428dce2014-10-21 15:02:15 -07001161 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001162 return kNumNativeFprArgs - fpr_index_;
1163 }
1164
1165 private:
1166 void PushGpr(uintptr_t val) {
1167 delegate_->PushGpr(val);
1168 }
1169 void PushFpr4(float val) {
1170 delegate_->PushFpr4(val);
1171 }
1172 void PushFpr8(uint64_t val) {
1173 delegate_->PushFpr8(val);
1174 }
1175 void PushStack(uintptr_t val) {
1176 delegate_->PushStack(val);
1177 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001178 uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1179 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001180 }
1181
1182 uint32_t gpr_index_; // Number of free GPRs
1183 uint32_t fpr_index_; // Number of free FPRs
1184 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1185 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001186 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001187};
1188
Andreas Gampec200a4a2014-06-16 18:39:09 -07001189// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1190// in subclasses.
1191//
1192// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1193// them with handles.
1194class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001195 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001196 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1197
1198 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001199
Ian Rogers1428dce2014-10-21 15:02:15 -07001200 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001201 return num_stack_entries_ * sizeof(uintptr_t);
1202 }
1203
Ian Rogers1428dce2014-10-21 15:02:15 -07001204 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001205 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001206 // Align by kStackAlignment.
1207 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001208 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001209 }
1210
Ian Rogers1428dce2014-10-21 15:02:15 -07001211 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1212 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001213 // Assumption is OK right now, as we have soft-float arm
1214 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1215 sp8 -= fregs * sizeof(uintptr_t);
1216 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1217 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1218 sp8 -= iregs * sizeof(uintptr_t);
1219 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1220 return sp8;
1221 }
Andreas Gampec147b002014-03-06 18:11:06 -08001222
Andreas Gampec200a4a2014-06-16 18:39:09 -07001223 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001224 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001225 // Native call stack.
1226 sp8 = LayoutCallStack(sp8);
1227 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001228
Andreas Gampec200a4a2014-06-16 18:39:09 -07001229 // Put fprs and gprs below.
1230 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001231
Andreas Gampec200a4a2014-06-16 18:39:09 -07001232 // Return the new bottom.
1233 return sp8;
1234 }
1235
1236 virtual void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm)
1237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {}
1238
1239 void Walk(const char* shorty, uint32_t shorty_len) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1240 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1241
1242 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001243
1244 for (uint32_t i = 1; i < shorty_len; ++i) {
1245 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1246 switch (cur_type_) {
1247 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001248 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001249 sm.AdvanceHandleScope(
1250 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001251 break;
1252
1253 case Primitive::kPrimBoolean:
1254 case Primitive::kPrimByte:
1255 case Primitive::kPrimChar:
1256 case Primitive::kPrimShort:
1257 case Primitive::kPrimInt:
1258 sm.AdvanceInt(0);
1259 break;
1260 case Primitive::kPrimFloat:
1261 sm.AdvanceFloat(0);
1262 break;
1263 case Primitive::kPrimDouble:
1264 sm.AdvanceDouble(0);
1265 break;
1266 case Primitive::kPrimLong:
1267 sm.AdvanceLong(0);
1268 break;
1269 default:
1270 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
1271 }
1272 }
1273
Ian Rogers1428dce2014-10-21 15:02:15 -07001274 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001275 }
1276
1277 void PushGpr(uintptr_t /* val */) {
1278 // not optimizing registers, yet
1279 }
1280
1281 void PushFpr4(float /* val */) {
1282 // not optimizing registers, yet
1283 }
1284
1285 void PushFpr8(uint64_t /* val */) {
1286 // not optimizing registers, yet
1287 }
1288
1289 void PushStack(uintptr_t /* val */) {
1290 // counting is already done in the superclass
1291 }
1292
Andreas Gampec200a4a2014-06-16 18:39:09 -07001293 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001294 return reinterpret_cast<uintptr_t>(nullptr);
1295 }
1296
Andreas Gampec200a4a2014-06-16 18:39:09 -07001297 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001298 uint32_t num_stack_entries_;
1299};
1300
Andreas Gampec200a4a2014-06-16 18:39:09 -07001301class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001302 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001303 ComputeGenericJniFrameSize() : num_handle_scope_references_(0) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001304
Andreas Gampec200a4a2014-06-16 18:39:09 -07001305 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1306 // is at *m = sp. Will update to point to the bottom of the save frame.
1307 //
1308 // Note: assumes ComputeAll() has been run before.
Ian Rogers59c07062014-10-10 13:03:39 -07001309 void LayoutCalleeSaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1310 HandleScope** handle_scope)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001311 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1312 mirror::ArtMethod* method = (*m)->AsMirrorPtr();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001313
Andreas Gampec200a4a2014-06-16 18:39:09 -07001314 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1315
1316 // First, fix up the layout of the callee-save frame.
1317 // We have to squeeze in the HandleScope, and relocate the method pointer.
1318
1319 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001320 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001321
1322 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001323 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
1324 size_t scope_and_method = handle_scope_size + sizeof(StackReference<mirror::ArtMethod>);
1325
1326 sp8 -= scope_and_method;
1327 // Align by kStackAlignment.
1328 sp8 = reinterpret_cast<uint8_t*>(RoundDown(
1329 reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
1330
1331 uint8_t* sp8_table = sp8 + sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers59c07062014-10-10 13:03:39 -07001332 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1333 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001334
1335 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1336 uint8_t* method_pointer = sp8;
1337 StackReference<mirror::ArtMethod>* new_method_ref =
1338 reinterpret_cast<StackReference<mirror::ArtMethod>*>(method_pointer);
1339 new_method_ref->Assign(method);
1340 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001341 }
1342
Andreas Gampec200a4a2014-06-16 18:39:09 -07001343 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001344 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001345 // Reference cookie and padding
1346 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001347 }
1348
Andreas Gampec200a4a2014-06-16 18:39:09 -07001349 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1350 // Returns the new bottom. Note: this may be unaligned.
Ian Rogers59c07062014-10-10 13:03:39 -07001351 uint8_t* LayoutJNISaveFrame(Thread* self, StackReference<mirror::ArtMethod>** m, void* sp,
1352 HandleScope** handle_scope)
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001353 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001354 // First, fix up the layout of the callee-save frame.
1355 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001356 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001357
1358 // The bottom of the callee-save frame is now where the method is, *m.
1359 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1360
1361 // Add space for cookie.
1362 LayoutCookie(&sp8);
1363
1364 return sp8;
1365 }
1366
1367 // WARNING: After this, *sp won't be pointing to the method anymore!
Ian Rogers59c07062014-10-10 13:03:39 -07001368 uint8_t* ComputeLayout(Thread* self, StackReference<mirror::ArtMethod>** m,
1369 bool is_static, const char* shorty, uint32_t shorty_len,
1370 HandleScope** handle_scope,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001371 uintptr_t** start_stack, uintptr_t** start_gpr, uint32_t** start_fpr)
1372 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1373 Walk(shorty, shorty_len);
1374
1375 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001376 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001377
1378 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1379
1380 // Return the new bottom.
1381 return sp8;
1382 }
1383
1384 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1385
1386 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1387 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
1388 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1389
1390 private:
1391 uint32_t num_handle_scope_references_;
1392};
1393
1394uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1395 num_handle_scope_references_++;
1396 return reinterpret_cast<uintptr_t>(nullptr);
1397}
1398
1399void ComputeGenericJniFrameSize::WalkHeader(
1400 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
1401 // JNIEnv
1402 sm->AdvancePointer(nullptr);
1403
1404 // Class object or this as first argument
1405 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1406}
1407
1408// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1409// the template requirements of BuildGenericJniFrameStateMachine.
1410class FillNativeCall {
1411 public:
1412 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1413 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1414
1415 virtual ~FillNativeCall() {}
1416
1417 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1418 cur_gpr_reg_ = gpr_regs;
1419 cur_fpr_reg_ = fpr_regs;
1420 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001421 }
1422
1423 void PushGpr(uintptr_t val) {
1424 *cur_gpr_reg_ = val;
1425 cur_gpr_reg_++;
1426 }
1427
1428 void PushFpr4(float val) {
1429 *cur_fpr_reg_ = val;
1430 cur_fpr_reg_++;
1431 }
1432
1433 void PushFpr8(uint64_t val) {
1434 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1435 *tmp = val;
1436 cur_fpr_reg_ += 2;
1437 }
1438
1439 void PushStack(uintptr_t val) {
1440 *cur_stack_arg_ = val;
1441 cur_stack_arg_++;
1442 }
1443
Andreas Gampec200a4a2014-06-16 18:39:09 -07001444 virtual uintptr_t PushHandle(mirror::Object* ref) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1445 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
1446 return 0U;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001447 }
1448
1449 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001450 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001451 uint32_t* cur_fpr_reg_;
1452 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001453};
Andreas Gampec147b002014-03-06 18:11:06 -08001454
Andreas Gampec200a4a2014-06-16 18:39:09 -07001455// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1456// of transitioning into native code.
1457class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1458 public:
Ian Rogers59c07062014-10-10 13:03:39 -07001459 BuildGenericJniFrameVisitor(Thread* self, bool is_static, const char* shorty, uint32_t shorty_len,
1460 StackReference<mirror::ArtMethod>** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001461 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
1462 jni_call_(nullptr, nullptr, nullptr, nullptr), sm_(&jni_call_) {
1463 ComputeGenericJniFrameSize fsc;
1464 uintptr_t* start_gpr_reg;
1465 uint32_t* start_fpr_reg;
1466 uintptr_t* start_stack_arg;
Ian Rogers59c07062014-10-10 13:03:39 -07001467 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, is_static, shorty, shorty_len,
1468 &handle_scope_,
1469 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001470 &start_gpr_reg, &start_fpr_reg);
1471
Andreas Gampec200a4a2014-06-16 18:39:09 -07001472 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1473
1474 // jni environment is always first argument
1475 sm_.AdvancePointer(self->GetJniEnv());
1476
1477 if (is_static) {
1478 sm_.AdvanceHandleScope((*sp)->AsMirrorPtr()->GetDeclaringClass());
1479 }
1480 }
1481
1482 void Visit() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) OVERRIDE;
1483
1484 void FinalizeHandleScope(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1485
1486 StackReference<mirror::Object>* GetFirstHandleScopeEntry()
1487 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1488 return handle_scope_->GetHandle(0).GetReference();
1489 }
1490
Ian Rogers1428dce2014-10-21 15:02:15 -07001491 jobject GetFirstHandleScopeJObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001492 return handle_scope_->GetHandle(0).ToJObject();
1493 }
1494
Ian Rogers1428dce2014-10-21 15:02:15 -07001495 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001496 return bottom_of_used_area_;
1497 }
1498
1499 private:
1500 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1501 class FillJniCall FINAL : public FillNativeCall {
1502 public:
1503 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
1504 HandleScope* handle_scope) : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1505 handle_scope_(handle_scope), cur_entry_(0) {}
1506
1507 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
1508
1509 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1510 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1511 handle_scope_ = scope;
1512 cur_entry_ = 0U;
1513 }
1514
1515 void ResetRemainingScopeSlots() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1516 // Initialize padding entries.
1517 size_t expected_slots = handle_scope_->NumberOfReferences();
1518 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001519 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001520 }
1521 DCHECK_NE(cur_entry_, 0U);
1522 }
1523
1524 private:
1525 HandleScope* handle_scope_;
1526 size_t cur_entry_;
1527 };
1528
1529 HandleScope* handle_scope_;
1530 FillJniCall jni_call_;
1531 void* bottom_of_used_area_;
1532
1533 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001534
1535 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1536};
1537
Andreas Gampec200a4a2014-06-16 18:39:09 -07001538uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1539 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001540 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001541 h.Assign(ref);
1542 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
1543 cur_entry_++;
1544 return tmp;
1545}
1546
Ian Rogers9758f792014-03-13 09:02:55 -07001547void BuildGenericJniFrameVisitor::Visit() {
1548 Primitive::Type type = GetParamPrimitiveType();
1549 switch (type) {
1550 case Primitive::kPrimLong: {
1551 jlong long_arg;
1552 if (IsSplitLongOrDouble()) {
1553 long_arg = ReadSplitLongParam();
1554 } else {
1555 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
1556 }
1557 sm_.AdvanceLong(long_arg);
1558 break;
1559 }
1560 case Primitive::kPrimDouble: {
1561 uint64_t double_arg;
1562 if (IsSplitLongOrDouble()) {
1563 // Read into union so that we don't case to a double.
1564 double_arg = ReadSplitLongParam();
1565 } else {
1566 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
1567 }
1568 sm_.AdvanceDouble(double_arg);
1569 break;
1570 }
1571 case Primitive::kPrimNot: {
1572 StackReference<mirror::Object>* stack_ref =
1573 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001574 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07001575 break;
1576 }
1577 case Primitive::kPrimFloat:
1578 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
1579 break;
1580 case Primitive::kPrimBoolean: // Fall-through.
1581 case Primitive::kPrimByte: // Fall-through.
1582 case Primitive::kPrimChar: // Fall-through.
1583 case Primitive::kPrimShort: // Fall-through.
1584 case Primitive::kPrimInt: // Fall-through.
1585 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
1586 break;
1587 case Primitive::kPrimVoid:
1588 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001589 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07001590 }
1591}
1592
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001593void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001594 // Clear out rest of the scope.
1595 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001596 // Install HandleScope.
1597 self->PushHandleScope(handle_scope_);
Ian Rogers9758f792014-03-13 09:02:55 -07001598}
1599
Ian Rogers04c31d22014-07-07 21:44:06 -07001600#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001601extern "C" void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001602#else
1603extern "C" void* artFindNativeMethod(Thread* self);
1604#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001605
Andreas Gampead615172014-04-04 16:20:13 -07001606uint64_t artQuickGenericJniEndJNIRef(Thread* self, uint32_t cookie, jobject l, jobject lock) {
1607 if (lock != nullptr) {
1608 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
1609 } else {
1610 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
1611 }
1612}
1613
1614void artQuickGenericJniEndJNINonRef(Thread* self, uint32_t cookie, jobject lock) {
1615 if (lock != nullptr) {
1616 JniMethodEndSynchronized(cookie, lock, self);
1617 } else {
1618 JniMethodEnd(cookie, self);
1619 }
1620}
1621
Andreas Gampec147b002014-03-06 18:11:06 -08001622/*
1623 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001624 * 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 -08001625 * The final element on the stack is a pointer to the native code.
1626 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001627 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001628 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001629 *
Andreas Gampec147b002014-03-06 18:11:06 -08001630 * The return of this function denotes:
1631 * 1) How many bytes of the alloca can be released, if the value is non-negative.
1632 * 2) An error, if the value is negative.
1633 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001634extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self,
1635 StackReference<mirror::ArtMethod>* sp)
Andreas Gampe2da88232014-02-27 12:26:20 -08001636 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampecf4035a2014-05-28 22:43:01 -07001637 mirror::ArtMethod* called = sp->AsMirrorPtr();
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001638 DCHECK(called->IsNative()) << PrettyMethod(called, true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001639 uint32_t shorty_len = 0;
1640 const char* shorty = called->GetShorty(&shorty_len);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001641
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001642 // Run the visitor and update sp.
Ian Rogers59c07062014-10-10 13:03:39 -07001643 BuildGenericJniFrameVisitor visitor(self, called->IsStatic(), shorty, shorty_len, &sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001644 visitor.VisitArguments();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001645 visitor.FinalizeHandleScope(self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001646
Andreas Gampec200a4a2014-06-16 18:39:09 -07001647 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001648 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001649
Ian Rogerse0dcd462014-03-08 15:21:04 -08001650 self->VerifyStack();
1651
Andreas Gampe90546832014-03-12 18:07:19 -07001652 // Start JNI, save the cookie.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001653 uint32_t cookie;
1654 if (called->IsSynchronized()) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001655 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001656 if (self->IsExceptionPending()) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001657 self->PopHandleScope();
Andreas Gampec147b002014-03-06 18:11:06 -08001658 // A negative value denotes an error.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001659 return GetTwoWordFailureValue();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001660 }
1661 } else {
1662 cookie = JniMethodStart(self);
1663 }
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001664 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Ian Rogerse0dcd462014-03-08 15:21:04 -08001665 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001666
Andreas Gampe90546832014-03-12 18:07:19 -07001667 // Retrieve the stored native code.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001668 const void* nativeCode = called->GetNativeMethod();
Andreas Gampe90546832014-03-12 18:07:19 -07001669
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07001670 // There are two cases for the content of nativeCode:
1671 // 1) Pointer to the native function.
1672 // 2) Pointer to the trampoline for native code binding.
1673 // In the second case, we need to execute the binding and continue with the actual native function
1674 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07001675 DCHECK(nativeCode != nullptr);
1676 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07001677#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07001678 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07001679#else
1680 nativeCode = artFindNativeMethod(self);
1681#endif
Andreas Gampe90546832014-03-12 18:07:19 -07001682
1683 if (nativeCode == nullptr) {
1684 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07001685
1686 // End JNI, as the assembly will move to deliver the exception.
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001687 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001688 if (shorty[0] == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001689 artQuickGenericJniEndJNIRef(self, cookie, nullptr, lock);
1690 } else {
1691 artQuickGenericJniEndJNINonRef(self, cookie, lock);
1692 }
1693
Andreas Gampec200a4a2014-06-16 18:39:09 -07001694 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07001695 }
1696 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001697 }
1698
Andreas Gampec200a4a2014-06-16 18:39:09 -07001699 // Return native code addr(lo) and bottom of alloca address(hi).
1700 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
1701 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001702}
1703
1704/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001705 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001706 * unlocking.
1707 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001708extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self, jvalue result, uint64_t result_f)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001709 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001710 StackReference<mirror::ArtMethod>* sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001711 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Andreas Gampecf4035a2014-05-28 22:43:01 -07001712 mirror::ArtMethod* called = sp->AsMirrorPtr();
Ian Rogerse0dcd462014-03-08 15:21:04 -08001713 uint32_t cookie = *(sp32 - 1);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001714
Andreas Gampead615172014-04-04 16:20:13 -07001715 jobject lock = nullptr;
1716 if (called->IsSynchronized()) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001717 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp)
1718 + sizeof(StackReference<mirror::ArtMethod>));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001719 lock = table->GetHandle(0).ToJObject();
Andreas Gampead615172014-04-04 16:20:13 -07001720 }
1721
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001722 char return_shorty_char = called->GetShorty()[0];
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001723
1724 if (return_shorty_char == 'L') {
Andreas Gampead615172014-04-04 16:20:13 -07001725 return artQuickGenericJniEndJNIRef(self, cookie, result.l, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001726 } else {
Andreas Gampead615172014-04-04 16:20:13 -07001727 artQuickGenericJniEndJNINonRef(self, cookie, lock);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001728
1729 switch (return_shorty_char) {
Nicolas Geoffray54accbc2014-08-13 03:40:45 +01001730 case 'F': {
1731 if (kRuntimeISA == kX86) {
1732 // Convert back the result to float.
1733 double d = bit_cast<uint64_t, double>(result_f);
1734 return bit_cast<float, uint32_t>(static_cast<float>(d));
1735 } else {
1736 return result_f;
1737 }
1738 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001739 case 'D':
1740 return result_f;
1741 case 'Z':
1742 return result.z;
1743 case 'B':
1744 return result.b;
1745 case 'C':
1746 return result.c;
1747 case 'S':
1748 return result.s;
1749 case 'I':
1750 return result.i;
1751 case 'J':
1752 return result.j;
1753 case 'V':
1754 return 0;
1755 default:
1756 LOG(FATAL) << "Unexpected return shorty character " << return_shorty_char;
1757 return 0;
1758 }
1759 }
Andreas Gampe2da88232014-02-27 12:26:20 -08001760}
1761
Andreas Gamped58342c2014-06-05 14:18:08 -07001762// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
1763// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07001764//
Andreas Gamped58342c2014-06-05 14:18:08 -07001765// It is valid to use this, as at the usage points here (returns from C functions) we are assuming
1766// to hold the mutator lock (see SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001767
1768template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001769static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001770 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001771 Thread* self, StackReference<mirror::ArtMethod>* sp);
Andreas Gampe51f76352014-05-21 08:28:48 -07001772
1773template<InvokeType type, bool access_check>
Andreas Gamped58342c2014-06-05 14:18:08 -07001774static TwoWordReturn artInvokeCommon(uint32_t method_idx, mirror::Object* this_object,
Andreas Gampe51f76352014-05-21 08:28:48 -07001775 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001776 Thread* self, StackReference<mirror::ArtMethod>* sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001777 ScopedQuickEntrypointChecks sqec(self);
1778 DCHECK_EQ(sp->AsMirrorPtr(), Runtime::Current()->GetCalleeSaveMethod(Runtime::kRefsAndArgs));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001779 mirror::ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check,
1780 type);
1781 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001782 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
1783 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001784 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001785 {
1786 // Remember the args in case a GC happens in FindMethodFromCode.
1787 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1788 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
1789 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001790 method = FindMethodFromCode<type, access_check>(method_idx, &this_object, &caller_method,
1791 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001792 visitor.FixupReferences();
1793 }
1794
1795 if (UNLIKELY(method == NULL)) {
1796 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001797 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001798 }
1799 }
1800 DCHECK(!self->IsExceptionPending());
1801 const void* code = method->GetEntryPointFromQuickCompiledCode();
1802
1803 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001804 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1805 << " location: "
1806 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001807
Andreas Gamped58342c2014-06-05 14:18:08 -07001808 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1809 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001810}
1811
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001812// Explicit artInvokeCommon template function declarations to please analysis tool.
1813#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
1814 template SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) \
Andreas Gamped58342c2014-06-05 14:18:08 -07001815 TwoWordReturn artInvokeCommon<type, access_check>(uint32_t method_idx, \
Andreas Gampe51f76352014-05-21 08:28:48 -07001816 mirror::Object* this_object, \
1817 mirror::ArtMethod* caller_method, \
Andreas Gampecf4035a2014-05-28 22:43:01 -07001818 Thread* self, \
1819 StackReference<mirror::ArtMethod>* sp) \
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01001820
1821EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
1822EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
1823EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
1824EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
1825EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
1826EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
1827EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
1828EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
1829EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
1830EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
1831#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
1832
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001833// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07001834extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
1835 uint32_t method_idx, mirror::Object* this_object,
1836 mirror::ArtMethod* caller_method, Thread* self,
1837 StackReference<mirror::ArtMethod>* sp)
1838 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1839 return artInvokeCommon<kInterface, true>(method_idx, this_object,
1840 caller_method, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001841}
1842
Andreas Gampec200a4a2014-06-16 18:39:09 -07001843extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
1844 uint32_t method_idx, mirror::Object* this_object,
1845 mirror::ArtMethod* caller_method, Thread* self,
1846 StackReference<mirror::ArtMethod>* sp)
1847 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1848 return artInvokeCommon<kDirect, true>(method_idx, this_object, caller_method,
1849 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001850}
1851
Andreas Gampec200a4a2014-06-16 18:39:09 -07001852extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
1853 uint32_t method_idx, mirror::Object* this_object,
1854 mirror::ArtMethod* caller_method, Thread* self,
1855 StackReference<mirror::ArtMethod>* sp)
1856 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1857 return artInvokeCommon<kStatic, true>(method_idx, this_object, caller_method,
1858 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001859}
1860
Andreas Gampec200a4a2014-06-16 18:39:09 -07001861extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
1862 uint32_t method_idx, mirror::Object* this_object,
1863 mirror::ArtMethod* caller_method, Thread* self,
1864 StackReference<mirror::ArtMethod>* sp)
1865 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1866 return artInvokeCommon<kSuper, true>(method_idx, this_object, caller_method,
1867 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001868}
1869
Andreas Gampec200a4a2014-06-16 18:39:09 -07001870extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
1871 uint32_t method_idx, mirror::Object* this_object,
1872 mirror::ArtMethod* caller_method, Thread* self,
1873 StackReference<mirror::ArtMethod>* sp)
1874 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1875 return artInvokeCommon<kVirtual, true>(method_idx, this_object, caller_method,
1876 self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001877}
1878
1879// Determine target of interface dispatch. This object is known non-null.
Andreas Gamped58342c2014-06-05 14:18:08 -07001880extern "C" TwoWordReturn artInvokeInterfaceTrampoline(mirror::ArtMethod* interface_method,
Andreas Gampe51f76352014-05-21 08:28:48 -07001881 mirror::Object* this_object,
1882 mirror::ArtMethod* caller_method,
Andreas Gampecf4035a2014-05-28 22:43:01 -07001883 Thread* self,
1884 StackReference<mirror::ArtMethod>* sp)
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001885 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001886 ScopedQuickEntrypointChecks sqec(self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001887 mirror::ArtMethod* method;
1888 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
1889 method = this_object->GetClass()->FindVirtualMethodForInterface(interface_method);
1890 if (UNLIKELY(method == NULL)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001891 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(interface_method, this_object,
1892 caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07001893 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001894 }
1895 } else {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001896 DCHECK(interface_method == Runtime::Current()->GetResolutionMethod());
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001897
1898 // Find the caller PC.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001899 constexpr size_t pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA, Runtime::kRefsAndArgs);
Ian Rogers13735952014-10-08 12:43:28 -07001900 uintptr_t caller_pc = *reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) + pc_offset);
Alexei Zavjalov41c507a2014-05-15 16:02:46 +07001901
1902 // Map the caller PC to a dex PC.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001903 uint32_t dex_pc = caller_method->ToDexPc(caller_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001904 const DexFile::CodeItem* code = caller_method->GetCodeItem();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001905 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1906 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1907 Instruction::Code instr_code = instr->Opcode();
1908 CHECK(instr_code == Instruction::INVOKE_INTERFACE ||
1909 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
1910 << "Unexpected call into interface trampoline: " << instr->DumpString(NULL);
1911 uint32_t dex_method_idx;
1912 if (instr_code == Instruction::INVOKE_INTERFACE) {
1913 dex_method_idx = instr->VRegB_35c();
1914 } else {
1915 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
1916 dex_method_idx = instr->VRegB_3rc();
1917 }
1918
Andreas Gampec200a4a2014-06-16 18:39:09 -07001919 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
1920 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001921 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001922 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
1923 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001924 {
1925 // Remember the args in case a GC happens in FindMethodFromCode.
1926 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
1927 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
1928 visitor.VisitArguments();
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001929 method = FindMethodFromCode<kInterface, false>(dex_method_idx, &this_object, &caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001930 self);
1931 visitor.FixupReferences();
1932 }
1933
1934 if (UNLIKELY(method == nullptr)) {
1935 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07001936 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001937 }
1938 }
1939 const void* code = method->GetEntryPointFromQuickCompiledCode();
1940
1941 // When we return, the caller will branch to this address, so it had better not be 0!
Andreas Gampec200a4a2014-06-16 18:39:09 -07001942 DCHECK(code != nullptr) << "Code was NULL in method: " << PrettyMethod(method)
1943 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07001944
Andreas Gamped58342c2014-06-05 14:18:08 -07001945 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
1946 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001947}
1948
Ian Rogers848871b2013-08-05 10:56:33 -07001949} // namespace art