blob: 3bbb723bcf5698b509bbb345784541663c3dfe26 [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>
Ian Rogers700a4022014-05-19 16:49:03 -070018#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070020#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021
22#include "base/logging.h"
23#include "base/macros.h"
24#include "calling_convention.h"
25#include "class_linker.h"
26#include "compiled_method.h"
27#include "dex_file-inl.h"
28#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070029#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "jni_internal.h"
Ian Rogers166db042013-07-26 12:05:57 -070031#include "utils/assembler.h"
32#include "utils/managed_register.h"
33#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010034#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070035#include "utils/mips/managed_register_mips.h"
36#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070037#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038
39#define __ jni_asm->
40
41namespace art {
42
43static void CopyParameter(Assembler* jni_asm,
44 ManagedRuntimeCallingConvention* mr_conv,
45 JniCallingConvention* jni_conv,
46 size_t frame_size, size_t out_arg_size);
47static void SetNativeParameter(Assembler* jni_asm,
48 JniCallingConvention* jni_conv,
49 ManagedRegister in_reg);
50
51// Generate the JNI bridge for the given method, general contract:
52// - Arguments are in the managed runtime format, either on stack or in
53// registers, a reference to the method object is supplied as part of this
54// convention.
55//
Ian Rogers72d32622014-05-06 16:20:11 -070056CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 uint32_t access_flags, uint32_t method_idx,
58 const DexFile& dex_file) {
59 const bool is_native = (access_flags & kAccNative) != 0;
60 CHECK(is_native);
61 const bool is_static = (access_flags & kAccStatic) != 0;
62 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
63 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070064 InstructionSet instruction_set = driver->GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070065 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070067 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
69 bool reference_return = main_jni_conv->IsReturnAReference();
70
Ian Rogers700a4022014-05-19 16:49:03 -070071 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
73
74 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
75 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010076 const char* jni_end_shorty;
77 if (reference_return && is_synchronized) {
78 jni_end_shorty = "ILL";
79 } else if (reference_return) {
80 jni_end_shorty = "IL";
81 } else if (is_synchronized) {
82 jni_end_shorty = "VL";
83 } else {
84 jni_end_shorty = "V";
85 }
86
Ian Rogers700a4022014-05-19 16:49:03 -070087 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
89
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070092
93 // Offsets into data structures
94 // TODO: if cross compiling these offsets are for the host not the target
95 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
96 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
97 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
98
99 // 1. Build the frame saving all callee saves
100 const size_t frame_size(main_jni_conv->FrameSize());
101 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
102 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
103
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700104 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 mr_conv->ResetIterator(FrameOffset(frame_size));
106 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 main_jni_conv->ReferenceCount(),
109 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100110
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700111 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
113 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100114 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
116 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100117 mr_conv->InterproceduralScratchRegister());
118 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
120 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100121 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
123 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100124 mr_conv->InterproceduralScratchRegister());
125 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700126
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 main_jni_conv->Next(); // Skip JNIEnv*
129 // 3.5. Create Class argument for static methods out of passed method
130 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
132 // Check handle scope offset is within frame
133 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700135 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
138 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 }
140 while (mr_conv->HasNext()) {
141 CHECK(main_jni_conv->HasNext());
142 bool ref_param = main_jni_conv->IsCurrentParamAReference();
143 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700148 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
149 // Check handle scope offset is within frame and doesn't run into the saved segment state
150 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
151 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
153 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
154 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
155 CHECK(input_in_reg || input_on_stack);
156
157 if (input_in_reg) {
158 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
159 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 } else if (input_on_stack) {
162 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
163 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 mr_conv->InterproceduralScratchRegister());
166 }
167 }
168 mr_conv->Next();
169 main_jni_conv->Next();
170 }
171
172 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700173 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100174 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
175 __ StoreImmediateToThread64(Thread::TopOfManagedStackPcOffset<8>(), 0,
176 mr_conv->InterproceduralScratchRegister());
177 } else {
178 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
179 __ StoreImmediateToThread32(Thread::TopOfManagedStackPcOffset<4>(), 0,
180 mr_conv->InterproceduralScratchRegister());
181 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
183 // 5. Move frame down to allow space for out going args.
184 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
185 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
186 const size_t max_out_arg_size = std::max(main_out_arg_size, end_out_arg_size);
187 __ IncreaseFrameSize(max_out_arg_size);
188
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
190 // can occur. The result is the saved JNI local state that is restored by the exit call. We
191 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
192 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100193 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
194 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
195 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
196 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 if (is_synchronized) {
200 // Pass object for locking.
201 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
204 if (main_jni_conv->IsCurrentParamOnStack()) {
205 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 mr_conv->InterproceduralScratchRegister(),
208 false);
209 } else {
210 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700211 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 ManagedRegister::NoRegister(), false);
213 }
214 main_jni_conv->Next();
215 }
216 if (main_jni_conv->IsCurrentParamInRegister()) {
217 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700218 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100219 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
220 main_jni_conv->InterproceduralScratchRegister());
221 } else {
222 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
223 main_jni_conv->InterproceduralScratchRegister());
224 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 } else {
226 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
227 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700228 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100229 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
230 } else {
231 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
232 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 }
234 if (is_synchronized) { // Check for exceptions from monitor enter.
235 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
236 }
237 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
238 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
239
240 // 7. Iterate over arguments placing values from managed calling convention in
241 // to the convention required for a native call (shuffling). For references
242 // place an index/pointer to the reference after checking whether it is
243 // NULL (which must be encoded as NULL).
244 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
245 // give as many free registers for the shuffle as possible
246 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
247 uint32_t args_count = 0;
248 while (mr_conv->HasNext()) {
249 args_count++;
250 mr_conv->Next();
251 }
252
253 // Do a backward pass over arguments, so that the generated code will be "mov
254 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
255 // TODO: A reverse iterator to improve readability.
256 for (uint32_t i = 0; i < args_count; ++i) {
257 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
258 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
259 main_jni_conv->Next(); // Skip JNIEnv*.
260 if (is_static) {
261 main_jni_conv->Next(); // Skip Class for now.
262 }
263 // Skip to the argument we're interested in.
264 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
265 mr_conv->Next();
266 main_jni_conv->Next();
267 }
268 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
269 }
270 if (is_static) {
271 // Create argument for Class
272 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
273 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
274 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 if (main_jni_conv->IsCurrentParamOnStack()) {
277 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 mr_conv->InterproceduralScratchRegister(),
280 false);
281 } else {
282 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700283 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 ManagedRegister::NoRegister(), false);
285 }
286 }
287
288 // 8. Create 1st argument, the JNI environment ptr.
289 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
290 // Register that will hold local indirect reference table
291 if (main_jni_conv->IsCurrentParamInRegister()) {
292 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
293 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700294 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100295 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
296 } else {
297 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
298 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 } else {
300 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700301 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100302 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100304 } else {
305 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
306 main_jni_conv->InterproceduralScratchRegister());
307 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 }
309
310 // 9. Plant call to native code associated with method.
Brian Carlstromea46f952013-07-30 01:26:50 -0700311 __ Call(main_jni_conv->MethodStackOffset(), mirror::ArtMethod::NativeMethodOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 mr_conv->InterproceduralScratchRegister());
313
314 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700315 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700316 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
317 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
318 __ SignExtend(main_jni_conv->ReturnRegister(),
319 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
320 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
321 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
322 __ ZeroExtend(main_jni_conv->ReturnRegister(),
323 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
324 }
325 }
326
327 // 11. Save return value
328 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
329 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
330 if (instruction_set == kMips && main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
331 return_save_location.Uint32Value() % 8 != 0) {
332 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700333 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 }
335 CHECK_LT(return_save_location.Uint32Value(), frame_size+main_out_arg_size);
336 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
337 }
338
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 // thread.
340 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100341 ThreadOffset<4> jni_end32(-1);
342 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 if (reference_return) {
344 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100345 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
346 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
347 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
348 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700349 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
350 end_jni_conv->Next();
351 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100352 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
353 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
354 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
355 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 }
357 // Pass saved local reference state.
358 if (end_jni_conv->IsCurrentParamOnStack()) {
359 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
360 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
361 } else {
362 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
363 __ Load(out_reg, saved_cookie_offset, 4);
364 }
365 end_jni_conv->Next();
366 if (is_synchronized) {
367 // Pass object for unlocking.
368 if (end_jni_conv->IsCurrentParamOnStack()) {
369 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700370 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 end_jni_conv->InterproceduralScratchRegister(),
372 false);
373 } else {
374 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700375 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 ManagedRegister::NoRegister(), false);
377 }
378 end_jni_conv->Next();
379 }
380 if (end_jni_conv->IsCurrentParamInRegister()) {
381 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700382 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100383 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
384 end_jni_conv->InterproceduralScratchRegister());
385 } else {
386 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
387 end_jni_conv->InterproceduralScratchRegister());
388 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 } else {
390 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
391 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700392 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100393 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
394 } else {
395 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
396 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700397 }
398
399 // 13. Reload return value
400 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
401 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
402 }
403
404 // 14. Move frame up now we're done with the out arg space.
405 __ DecreaseFrameSize(max_out_arg_size);
406
407 // 15. Process pending exceptions from JNI call or monitor exit.
408 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
409
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700410 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 // them.
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700412 __ RemoveFrame(frame_size, callee_save_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413
414 // 17. Finalize code generation
415 __ EmitSlowPaths();
416 size_t cs = __ CodeSize();
Serban Constantinescu75b91132014-04-09 18:39:10 +0100417 if (instruction_set == kArm64) {
418 // Test that we do not exceed the buffer size.
419 CHECK(cs < arm64::kBufferSizeArm64);
420 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421 std::vector<uint8_t> managed_code(cs);
422 MemoryRegion code(&managed_code[0], managed_code.size());
423 __ FinalizeInstructions(code);
Ian Rogers72d32622014-05-06 16:20:11 -0700424 return new CompiledMethod(driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700425 instruction_set,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 managed_code,
427 frame_size,
428 main_jni_conv->CoreSpillMask(),
429 main_jni_conv->FpSpillMask());
430}
431
432// Copy a single parameter from the managed to the JNI calling convention
433static void CopyParameter(Assembler* jni_asm,
434 ManagedRuntimeCallingConvention* mr_conv,
435 JniCallingConvention* jni_conv,
436 size_t frame_size, size_t out_arg_size) {
437 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
438 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700439 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440 bool null_allowed = false;
441 bool ref_param = jni_conv->IsCurrentParamAReference();
442 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
443 // input may be in register, on stack or both - but not none!
444 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
445 if (output_in_reg) { // output shouldn't straddle registers and stack
446 CHECK(!jni_conv->IsCurrentParamOnStack());
447 } else {
448 CHECK(jni_conv->IsCurrentParamOnStack());
449 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700450 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 if (ref_param) {
452 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700453 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
454 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700456 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
457 // Check handle scope offset is within frame.
458 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 }
460 if (input_in_reg && output_in_reg) {
461 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
462 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
463 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700464 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 } else {
466 if (!mr_conv->IsCurrentParamOnStack()) {
467 // regular non-straddling move
468 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
469 } else {
470 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
471 }
472 }
473 } else if (!input_in_reg && !output_in_reg) {
474 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
475 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700476 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 null_allowed);
478 } else {
479 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
480 size_t param_size = mr_conv->CurrentParamSize();
481 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
482 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
483 }
484 } else if (!input_in_reg && output_in_reg) {
485 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
486 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
487 // Check that incoming stack arguments are above the current stack frame.
488 CHECK_GT(in_off.Uint32Value(), frame_size);
489 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700490 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 } else {
492 size_t param_size = mr_conv->CurrentParamSize();
493 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
494 __ Load(out_reg, in_off, param_size);
495 }
496 } else {
497 CHECK(input_in_reg && !output_in_reg);
498 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
499 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
500 // Check outgoing argument is within frame
501 CHECK_LT(out_off.Uint32Value(), frame_size);
502 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700503 // TODO: recycle value in in_reg rather than reload from handle scope
504 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 null_allowed);
506 } else {
507 size_t param_size = mr_conv->CurrentParamSize();
508 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
509 if (!mr_conv->IsCurrentParamOnStack()) {
510 // regular non-straddling store
511 __ Store(out_off, in_reg, param_size);
512 } else {
513 // store where input straddles registers and stack
514 CHECK_EQ(param_size, 8u);
515 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
516 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
517 }
518 }
519 }
520}
521
522static void SetNativeParameter(Assembler* jni_asm,
523 JniCallingConvention* jni_conv,
524 ManagedRegister in_reg) {
525 if (jni_conv->IsCurrentParamOnStack()) {
526 FrameOffset dest = jni_conv->CurrentParamStackOffset();
527 __ StoreRawPtr(dest, in_reg);
528 } else {
529 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
530 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
531 }
532 }
533}
534
535} // namespace art
536
Ian Rogers72d32622014-05-06 16:20:11 -0700537extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538 uint32_t access_flags, uint32_t method_idx,
539 const art::DexFile& dex_file) {
540 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
541}