blob: 20f9f4b0d803949223e71db940b0b4a825b45434 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 <algorithm>
18#include <vector>
19
20#include "base/logging.h"
21#include "base/macros.h"
22#include "calling_convention.h"
23#include "class_linker.h"
24#include "compiled_method.h"
25#include "dex_file-inl.h"
26#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070027#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "jni_internal.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "utils/assembler.h"
30#include "utils/managed_register.h"
31#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010032#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070033#include "utils/mips/managed_register_mips.h"
34#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070035#include "thread.h"
36#include "UniquePtr.h"
37
38#define __ jni_asm->
39
40namespace art {
41
42static void CopyParameter(Assembler* jni_asm,
43 ManagedRuntimeCallingConvention* mr_conv,
44 JniCallingConvention* jni_conv,
45 size_t frame_size, size_t out_arg_size);
46static void SetNativeParameter(Assembler* jni_asm,
47 JniCallingConvention* jni_conv,
48 ManagedRegister in_reg);
49
50// Generate the JNI bridge for the given method, general contract:
51// - Arguments are in the managed runtime format, either on stack or in
52// registers, a reference to the method object is supplied as part of this
53// convention.
54//
Ian Rogers72d32622014-05-06 16:20:11 -070055CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 uint32_t access_flags, uint32_t method_idx,
57 const DexFile& dex_file) {
58 const bool is_native = (access_flags & kAccNative) != 0;
59 CHECK(is_native);
60 const bool is_static = (access_flags & kAccStatic) != 0;
61 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
62 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070063 InstructionSet instruction_set = driver->GetInstructionSet();
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 if (instruction_set == kThumb2) {
65 instruction_set = kArm;
66 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -070067 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 // Calling conventions used to iterate over parameters to method
69 UniquePtr<JniCallingConvention> main_jni_conv(
70 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
71 bool reference_return = main_jni_conv->IsReturnAReference();
72
73 UniquePtr<ManagedRuntimeCallingConvention> mr_conv(
74 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
75
76 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
77 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010078 const char* jni_end_shorty;
79 if (reference_return && is_synchronized) {
80 jni_end_shorty = "ILL";
81 } else if (reference_return) {
82 jni_end_shorty = "IL";
83 } else if (is_synchronized) {
84 jni_end_shorty = "VL";
85 } else {
86 jni_end_shorty = "V";
87 }
88
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 UniquePtr<JniCallingConvention> end_jni_conv(
90 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
91
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 // Assembler that holds generated instructions
93 UniquePtr<Assembler> jni_asm(Assembler::Create(instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070094
95 // Offsets into data structures
96 // TODO: if cross compiling these offsets are for the host not the target
97 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
98 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
99 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
100
101 // 1. Build the frame saving all callee saves
102 const size_t frame_size(main_jni_conv->FrameSize());
103 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
104 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
105
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700106 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 mr_conv->ResetIterator(FrameOffset(frame_size));
108 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 main_jni_conv->ReferenceCount(),
111 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100112
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700113 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
115 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100116 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
118 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100119 mr_conv->InterproceduralScratchRegister());
120 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700121 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
122 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100123 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
125 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100126 mr_conv->InterproceduralScratchRegister());
127 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130 main_jni_conv->Next(); // Skip JNIEnv*
131 // 3.5. Create Class argument for static methods out of passed method
132 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700133 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
134 // Check handle scope offset is within frame
135 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700137 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700139 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
140 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 }
142 while (mr_conv->HasNext()) {
143 CHECK(main_jni_conv->HasNext());
144 bool ref_param = main_jni_conv->IsCurrentParamAReference();
145 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700148 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
151 // Check handle scope offset is within frame and doesn't run into the saved segment state
152 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
153 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
155 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
156 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
157 CHECK(input_in_reg || input_on_stack);
158
159 if (input_in_reg) {
160 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
161 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 } else if (input_on_stack) {
164 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
165 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 mr_conv->InterproceduralScratchRegister());
168 }
169 }
170 mr_conv->Next();
171 main_jni_conv->Next();
172 }
173
174 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700175 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100176 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
177 __ StoreImmediateToThread64(Thread::TopOfManagedStackPcOffset<8>(), 0,
178 mr_conv->InterproceduralScratchRegister());
179 } else {
180 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
181 __ StoreImmediateToThread32(Thread::TopOfManagedStackPcOffset<4>(), 0,
182 mr_conv->InterproceduralScratchRegister());
183 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184
185 // 5. Move frame down to allow space for out going args.
186 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
187 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
188 const size_t max_out_arg_size = std::max(main_out_arg_size, end_out_arg_size);
189 __ IncreaseFrameSize(max_out_arg_size);
190
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
192 // can occur. The result is the saved JNI local state that is restored by the exit call. We
193 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
194 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100195 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
196 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
197 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
198 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700200 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 if (is_synchronized) {
202 // Pass object for locking.
203 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700204 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
206 if (main_jni_conv->IsCurrentParamOnStack()) {
207 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700208 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209 mr_conv->InterproceduralScratchRegister(),
210 false);
211 } else {
212 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 ManagedRegister::NoRegister(), false);
215 }
216 main_jni_conv->Next();
217 }
218 if (main_jni_conv->IsCurrentParamInRegister()) {
219 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700220 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100221 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
222 main_jni_conv->InterproceduralScratchRegister());
223 } else {
224 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
225 main_jni_conv->InterproceduralScratchRegister());
226 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 } else {
228 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
229 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700230 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100231 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
232 } else {
233 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
234 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 }
236 if (is_synchronized) { // Check for exceptions from monitor enter.
237 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
238 }
239 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
240 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
241
242 // 7. Iterate over arguments placing values from managed calling convention in
243 // to the convention required for a native call (shuffling). For references
244 // place an index/pointer to the reference after checking whether it is
245 // NULL (which must be encoded as NULL).
246 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
247 // give as many free registers for the shuffle as possible
248 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
249 uint32_t args_count = 0;
250 while (mr_conv->HasNext()) {
251 args_count++;
252 mr_conv->Next();
253 }
254
255 // Do a backward pass over arguments, so that the generated code will be "mov
256 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
257 // TODO: A reverse iterator to improve readability.
258 for (uint32_t i = 0; i < args_count; ++i) {
259 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
260 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
261 main_jni_conv->Next(); // Skip JNIEnv*.
262 if (is_static) {
263 main_jni_conv->Next(); // Skip Class for now.
264 }
265 // Skip to the argument we're interested in.
266 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
267 mr_conv->Next();
268 main_jni_conv->Next();
269 }
270 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
271 }
272 if (is_static) {
273 // Create argument for Class
274 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
275 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
276 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700277 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 if (main_jni_conv->IsCurrentParamOnStack()) {
279 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700280 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700281 mr_conv->InterproceduralScratchRegister(),
282 false);
283 } else {
284 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700285 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 ManagedRegister::NoRegister(), false);
287 }
288 }
289
290 // 8. Create 1st argument, the JNI environment ptr.
291 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
292 // Register that will hold local indirect reference table
293 if (main_jni_conv->IsCurrentParamInRegister()) {
294 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
295 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700296 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100297 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
298 } else {
299 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
300 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 } else {
302 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700303 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100304 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100306 } else {
307 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
308 main_jni_conv->InterproceduralScratchRegister());
309 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 }
311
312 // 9. Plant call to native code associated with method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700313 __ Call(main_jni_conv->MethodStackOffset(), mirror::ArtMethod::NativeMethodOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 mr_conv->InterproceduralScratchRegister());
315
316 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700317 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
319 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
320 __ SignExtend(main_jni_conv->ReturnRegister(),
321 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
322 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
323 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
324 __ ZeroExtend(main_jni_conv->ReturnRegister(),
325 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
326 }
327 }
328
329 // 11. Save return value
330 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
331 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
332 if (instruction_set == kMips && main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
333 return_save_location.Uint32Value() % 8 != 0) {
334 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700335 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 }
337 CHECK_LT(return_save_location.Uint32Value(), frame_size+main_out_arg_size);
338 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
339 }
340
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 // thread.
342 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100343 ThreadOffset<4> jni_end32(-1);
344 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 if (reference_return) {
346 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100347 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
348 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
349 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
350 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
352 end_jni_conv->Next();
353 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100354 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
355 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
356 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
357 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 }
359 // Pass saved local reference state.
360 if (end_jni_conv->IsCurrentParamOnStack()) {
361 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
362 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
363 } else {
364 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
365 __ Load(out_reg, saved_cookie_offset, 4);
366 }
367 end_jni_conv->Next();
368 if (is_synchronized) {
369 // Pass object for unlocking.
370 if (end_jni_conv->IsCurrentParamOnStack()) {
371 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700372 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 end_jni_conv->InterproceduralScratchRegister(),
374 false);
375 } else {
376 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700377 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378 ManagedRegister::NoRegister(), false);
379 }
380 end_jni_conv->Next();
381 }
382 if (end_jni_conv->IsCurrentParamInRegister()) {
383 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700384 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100385 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
386 end_jni_conv->InterproceduralScratchRegister());
387 } else {
388 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
389 end_jni_conv->InterproceduralScratchRegister());
390 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 } else {
392 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
393 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700394 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100395 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
396 } else {
397 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
398 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700399 }
400
401 // 13. Reload return value
402 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
403 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
404 }
405
406 // 14. Move frame up now we're done with the out arg space.
407 __ DecreaseFrameSize(max_out_arg_size);
408
409 // 15. Process pending exceptions from JNI call or monitor exit.
410 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
411
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700412 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 // them.
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700414 __ RemoveFrame(frame_size, callee_save_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415
416 // 17. Finalize code generation
417 __ EmitSlowPaths();
418 size_t cs = __ CodeSize();
Serban Constantinescu75b91132014-04-09 18:39:10 +0100419 if (instruction_set == kArm64) {
420 // Test that we do not exceed the buffer size.
421 CHECK(cs < arm64::kBufferSizeArm64);
422 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 std::vector<uint8_t> managed_code(cs);
424 MemoryRegion code(&managed_code[0], managed_code.size());
425 __ FinalizeInstructions(code);
Ian Rogers72d32622014-05-06 16:20:11 -0700426 return new CompiledMethod(driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700427 instruction_set,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 managed_code,
429 frame_size,
430 main_jni_conv->CoreSpillMask(),
431 main_jni_conv->FpSpillMask());
432}
433
434// Copy a single parameter from the managed to the JNI calling convention
435static void CopyParameter(Assembler* jni_asm,
436 ManagedRuntimeCallingConvention* mr_conv,
437 JniCallingConvention* jni_conv,
438 size_t frame_size, size_t out_arg_size) {
439 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
440 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700441 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 bool null_allowed = false;
443 bool ref_param = jni_conv->IsCurrentParamAReference();
444 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
445 // input may be in register, on stack or both - but not none!
446 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
447 if (output_in_reg) { // output shouldn't straddle registers and stack
448 CHECK(!jni_conv->IsCurrentParamOnStack());
449 } else {
450 CHECK(jni_conv->IsCurrentParamOnStack());
451 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700452 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 if (ref_param) {
454 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700455 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
456 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700458 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
459 // Check handle scope offset is within frame.
460 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 }
462 if (input_in_reg && output_in_reg) {
463 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
464 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
465 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700466 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 } else {
468 if (!mr_conv->IsCurrentParamOnStack()) {
469 // regular non-straddling move
470 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
471 } else {
472 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
473 }
474 }
475 } else if (!input_in_reg && !output_in_reg) {
476 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
477 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700478 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 null_allowed);
480 } else {
481 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
482 size_t param_size = mr_conv->CurrentParamSize();
483 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
484 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
485 }
486 } else if (!input_in_reg && output_in_reg) {
487 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
488 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
489 // Check that incoming stack arguments are above the current stack frame.
490 CHECK_GT(in_off.Uint32Value(), frame_size);
491 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700492 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 } else {
494 size_t param_size = mr_conv->CurrentParamSize();
495 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
496 __ Load(out_reg, in_off, param_size);
497 }
498 } else {
499 CHECK(input_in_reg && !output_in_reg);
500 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
501 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
502 // Check outgoing argument is within frame
503 CHECK_LT(out_off.Uint32Value(), frame_size);
504 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700505 // TODO: recycle value in in_reg rather than reload from handle scope
506 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700507 null_allowed);
508 } else {
509 size_t param_size = mr_conv->CurrentParamSize();
510 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
511 if (!mr_conv->IsCurrentParamOnStack()) {
512 // regular non-straddling store
513 __ Store(out_off, in_reg, param_size);
514 } else {
515 // store where input straddles registers and stack
516 CHECK_EQ(param_size, 8u);
517 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
518 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
519 }
520 }
521 }
522}
523
524static void SetNativeParameter(Assembler* jni_asm,
525 JniCallingConvention* jni_conv,
526 ManagedRegister in_reg) {
527 if (jni_conv->IsCurrentParamOnStack()) {
528 FrameOffset dest = jni_conv->CurrentParamStackOffset();
529 __ StoreRawPtr(dest, in_reg);
530 } else {
531 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
532 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
533 }
534 }
535}
536
537} // namespace art
538
Ian Rogers72d32622014-05-06 16:20:11 -0700539extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700540 uint32_t access_flags, uint32_t method_idx,
541 const art::DexFile& dex_file) {
542 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
543}