blob: c38cfaf746daf2eb2abb836766a74e343cf7757c [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"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070031#include "mirror/art_method.h"
Ian Rogers166db042013-07-26 12:05:57 -070032#include "utils/assembler.h"
33#include "utils/managed_register.h"
34#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010035#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070036#include "utils/mips/managed_register_mips.h"
37#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070038#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070039
40#define __ jni_asm->
41
42namespace art {
43
44static void CopyParameter(Assembler* jni_asm,
45 ManagedRuntimeCallingConvention* mr_conv,
46 JniCallingConvention* jni_conv,
47 size_t frame_size, size_t out_arg_size);
48static void SetNativeParameter(Assembler* jni_asm,
49 JniCallingConvention* jni_conv,
50 ManagedRegister in_reg);
51
52// Generate the JNI bridge for the given method, general contract:
53// - Arguments are in the managed runtime format, either on stack or in
54// registers, a reference to the method object is supplied as part of this
55// convention.
56//
Ian Rogers72d32622014-05-06 16:20:11 -070057CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 uint32_t access_flags, uint32_t method_idx,
59 const DexFile& dex_file) {
60 const bool is_native = (access_flags & kAccNative) != 0;
61 CHECK(is_native);
62 const bool is_static = (access_flags & kAccStatic) != 0;
63 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
64 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070065 InstructionSet instruction_set = driver->GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070066 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070068 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
70 bool reference_return = main_jni_conv->IsReturnAReference();
71
Ian Rogers700a4022014-05-19 16:49:03 -070072 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
74
75 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
76 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010077 const char* jni_end_shorty;
78 if (reference_return && is_synchronized) {
79 jni_end_shorty = "ILL";
80 } else if (reference_return) {
81 jni_end_shorty = "IL";
82 } else if (is_synchronized) {
83 jni_end_shorty = "VL";
84 } else {
85 jni_end_shorty = "V";
86 }
87
Ian Rogers700a4022014-05-19 16:49:03 -070088 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
90
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070092 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070093
94 // Offsets into data structures
95 // TODO: if cross compiling these offsets are for the host not the target
96 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
97 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
98 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
99
100 // 1. Build the frame saving all callee saves
101 const size_t frame_size(main_jni_conv->FrameSize());
102 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
103 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
104
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 mr_conv->ResetIterator(FrameOffset(frame_size));
107 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 main_jni_conv->ReferenceCount(),
110 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100111
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700112 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
114 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100115 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
117 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100118 mr_conv->InterproceduralScratchRegister());
119 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
121 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100122 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
124 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100125 mr_conv->InterproceduralScratchRegister());
126 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700127
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700128 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 main_jni_conv->Next(); // Skip JNIEnv*
130 // 3.5. Create Class argument for static methods out of passed method
131 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
133 // Check handle scope offset is within frame
134 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700136 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
139 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 }
141 while (mr_conv->HasNext()) {
142 CHECK(main_jni_conv->HasNext());
143 bool ref_param = main_jni_conv->IsCurrentParamAReference();
144 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
150 // Check handle scope offset is within frame and doesn't run into the saved segment state
151 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
152 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
154 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
155 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
156 CHECK(input_in_reg || input_on_stack);
157
158 if (input_in_reg) {
159 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
160 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 } else if (input_on_stack) {
163 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
164 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 mr_conv->InterproceduralScratchRegister());
167 }
168 }
169 mr_conv->Next();
170 main_jni_conv->Next();
171 }
172
173 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700174 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100175 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
176 __ StoreImmediateToThread64(Thread::TopOfManagedStackPcOffset<8>(), 0,
177 mr_conv->InterproceduralScratchRegister());
178 } else {
179 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
180 __ StoreImmediateToThread32(Thread::TopOfManagedStackPcOffset<4>(), 0,
181 mr_conv->InterproceduralScratchRegister());
182 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183
184 // 5. Move frame down to allow space for out going args.
185 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100186 size_t current_out_arg_size = main_out_arg_size;
187 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
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
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100246 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 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
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100272 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 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 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100335 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700336 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
337 }
338
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100339 // Increase frame size for out args if needed by the end_jni_conv.
340 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
341 if (end_out_arg_size > current_out_arg_size) {
342 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
343 current_out_arg_size = end_out_arg_size;
344 __ IncreaseFrameSize(out_arg_size_diff);
345 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
346 locked_object_handle_scope_offset =
347 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
348 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
349 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 // thread.
351 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100352 ThreadOffset<4> jni_end32(-1);
353 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 if (reference_return) {
355 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100356 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
357 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
358 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
359 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
361 end_jni_conv->Next();
362 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100363 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
364 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
365 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
366 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700367 }
368 // Pass saved local reference state.
369 if (end_jni_conv->IsCurrentParamOnStack()) {
370 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
371 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
372 } else {
373 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
374 __ Load(out_reg, saved_cookie_offset, 4);
375 }
376 end_jni_conv->Next();
377 if (is_synchronized) {
378 // Pass object for unlocking.
379 if (end_jni_conv->IsCurrentParamOnStack()) {
380 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700381 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 end_jni_conv->InterproceduralScratchRegister(),
383 false);
384 } else {
385 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700386 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700387 ManagedRegister::NoRegister(), false);
388 }
389 end_jni_conv->Next();
390 }
391 if (end_jni_conv->IsCurrentParamInRegister()) {
392 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700393 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100394 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
395 end_jni_conv->InterproceduralScratchRegister());
396 } else {
397 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
398 end_jni_conv->InterproceduralScratchRegister());
399 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 } else {
401 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
402 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700403 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100404 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
405 } else {
406 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
407 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700408 }
409
410 // 13. Reload return value
411 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
412 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
413 }
414
415 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100416 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417
418 // 15. Process pending exceptions from JNI call or monitor exit.
419 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
420
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700421 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 // them.
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700423 __ RemoveFrame(frame_size, callee_save_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424
425 // 17. Finalize code generation
426 __ EmitSlowPaths();
427 size_t cs = __ CodeSize();
Serban Constantinescu75b91132014-04-09 18:39:10 +0100428 if (instruction_set == kArm64) {
429 // Test that we do not exceed the buffer size.
430 CHECK(cs < arm64::kBufferSizeArm64);
431 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 std::vector<uint8_t> managed_code(cs);
433 MemoryRegion code(&managed_code[0], managed_code.size());
434 __ FinalizeInstructions(code);
Ian Rogers72d32622014-05-06 16:20:11 -0700435 return new CompiledMethod(driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700436 instruction_set,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 managed_code,
438 frame_size,
439 main_jni_conv->CoreSpillMask(),
440 main_jni_conv->FpSpillMask());
441}
442
443// Copy a single parameter from the managed to the JNI calling convention
444static void CopyParameter(Assembler* jni_asm,
445 ManagedRuntimeCallingConvention* mr_conv,
446 JniCallingConvention* jni_conv,
447 size_t frame_size, size_t out_arg_size) {
448 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
449 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700450 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451 bool null_allowed = false;
452 bool ref_param = jni_conv->IsCurrentParamAReference();
453 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
454 // input may be in register, on stack or both - but not none!
455 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
456 if (output_in_reg) { // output shouldn't straddle registers and stack
457 CHECK(!jni_conv->IsCurrentParamOnStack());
458 } else {
459 CHECK(jni_conv->IsCurrentParamOnStack());
460 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700461 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 if (ref_param) {
463 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700464 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
465 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700467 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
468 // Check handle scope offset is within frame.
469 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 }
471 if (input_in_reg && output_in_reg) {
472 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
473 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
474 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700475 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 } else {
477 if (!mr_conv->IsCurrentParamOnStack()) {
478 // regular non-straddling move
479 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
480 } else {
481 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
482 }
483 }
484 } else if (!input_in_reg && !output_in_reg) {
485 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
486 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700487 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 null_allowed);
489 } else {
490 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
491 size_t param_size = mr_conv->CurrentParamSize();
492 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
493 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
494 }
495 } else if (!input_in_reg && output_in_reg) {
496 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
497 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
498 // Check that incoming stack arguments are above the current stack frame.
499 CHECK_GT(in_off.Uint32Value(), frame_size);
500 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700501 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700502 } else {
503 size_t param_size = mr_conv->CurrentParamSize();
504 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
505 __ Load(out_reg, in_off, param_size);
506 }
507 } else {
508 CHECK(input_in_reg && !output_in_reg);
509 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
510 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
511 // Check outgoing argument is within frame
512 CHECK_LT(out_off.Uint32Value(), frame_size);
513 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700514 // TODO: recycle value in in_reg rather than reload from handle scope
515 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700516 null_allowed);
517 } else {
518 size_t param_size = mr_conv->CurrentParamSize();
519 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
520 if (!mr_conv->IsCurrentParamOnStack()) {
521 // regular non-straddling store
522 __ Store(out_off, in_reg, param_size);
523 } else {
524 // store where input straddles registers and stack
525 CHECK_EQ(param_size, 8u);
526 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
527 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
528 }
529 }
530 }
531}
532
533static void SetNativeParameter(Assembler* jni_asm,
534 JniCallingConvention* jni_conv,
535 ManagedRegister in_reg) {
536 if (jni_conv->IsCurrentParamOnStack()) {
537 FrameOffset dest = jni_conv->CurrentParamStackOffset();
538 __ StoreRawPtr(dest, in_reg);
539 } else {
540 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
541 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
542 }
543 }
544}
545
546} // namespace art
547
Ian Rogers72d32622014-05-06 16:20:11 -0700548extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 uint32_t access_flags, uint32_t method_idx,
550 const art::DexFile& dex_file) {
551 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
552}