blob: 4311a34494aa40b184a13c764df9d9d4a68a04a7 [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
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "jni_compiler.h"
18
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <algorithm>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070022#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method.h"
Vladimir Markod1ee8092016-04-13 11:59:46 +010025#include "base/arena_allocator.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "base/logging.h"
27#include "base/macros.h"
28#include "calling_convention.h"
29#include "class_linker.h"
30#include "compiled_method.h"
31#include "dex_file-inl.h"
32#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010033#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070034#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070035#include "jni_env_ext.h"
Ian Rogers166db042013-07-26 12:05:57 -070036#include "utils/assembler.h"
37#include "utils/managed_register.h"
38#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010039#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070040#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010041#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070042#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070043#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070044
45#define __ jni_asm->
46
47namespace art {
48
49static void CopyParameter(Assembler* jni_asm,
50 ManagedRuntimeCallingConvention* mr_conv,
51 JniCallingConvention* jni_conv,
52 size_t frame_size, size_t out_arg_size);
53static void SetNativeParameter(Assembler* jni_asm,
54 JniCallingConvention* jni_conv,
55 ManagedRegister in_reg);
56
57// Generate the JNI bridge for the given method, general contract:
58// - Arguments are in the managed runtime format, either on stack or in
59// registers, a reference to the method object is supplied as part of this
60// convention.
61//
Ian Rogers72d32622014-05-06 16:20:11 -070062CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 uint32_t access_flags, uint32_t method_idx,
64 const DexFile& dex_file) {
65 const bool is_native = (access_flags & kAccNative) != 0;
66 CHECK(is_native);
67 const bool is_static = (access_flags & kAccStatic) != 0;
68 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
69 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070070 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020071 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070072 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Vladimir Markod1ee8092016-04-13 11:59:46 +010073
74 ArenaPool pool;
75 ArenaAllocator arena(&pool);
76
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070078 std::unique_ptr<JniCallingConvention> main_jni_conv(
Vladimir Markod1ee8092016-04-13 11:59:46 +010079 JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 bool reference_return = main_jni_conv->IsReturnAReference();
81
Ian Rogers700a4022014-05-19 16:49:03 -070082 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Markod1ee8092016-04-13 11:59:46 +010083 ManagedRuntimeCallingConvention::Create(
84 &arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070085
86 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
87 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010088 const char* jni_end_shorty;
89 if (reference_return && is_synchronized) {
90 jni_end_shorty = "ILL";
91 } else if (reference_return) {
92 jni_end_shorty = "IL";
93 } else if (is_synchronized) {
94 jni_end_shorty = "VL";
95 } else {
96 jni_end_shorty = "V";
97 }
98
Vladimir Markod1ee8092016-04-13 11:59:46 +010099 std::unique_ptr<JniCallingConvention> end_jni_conv(JniCallingConvention::Create(
100 &arena, is_static, is_synchronized, jni_end_shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 // Assembler that holds generated instructions
Vladimir Markod1ee8092016-04-13 11:59:46 +0100103 std::unique_ptr<Assembler> jni_asm(
104 Assembler::Create(&arena, instruction_set, instruction_set_features));
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000105 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GenerateAnyDebugInfo());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106
107 // Offsets into data structures
108 // TODO: if cross compiling these offsets are for the host not the target
109 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
110 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
111 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
112
113 // 1. Build the frame saving all callee saves
114 const size_t frame_size(main_jni_conv->FrameSize());
Vladimir Marko1cd1b032016-05-19 10:37:24 +0100115 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100117 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700120 mr_conv->ResetIterator(FrameOffset(frame_size));
121 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 main_jni_conv->ReferenceCount(),
124 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100125
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700126 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 Thread::TopHandleScopeOffset<8>(),
129 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 main_jni_conv->HandleScopeOffset(),
132 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100133 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700134 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 Thread::TopHandleScopeOffset<4>(),
136 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700137 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138 main_jni_conv->HandleScopeOffset(),
139 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100140 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700142 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 main_jni_conv->Next(); // Skip JNIEnv*
144 // 3.5. Create Class argument for static methods out of passed method
145 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
147 // Check handle scope offset is within frame
148 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Roland Levillain4d027112015-07-01 15:41:14 +0100149 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700150 // Note this LoadRef() does not include read barrier. It will be handled below.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700152 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
155 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 }
157 while (mr_conv->HasNext()) {
158 CHECK(main_jni_conv->HasNext());
159 bool ref_param = main_jni_conv->IsCurrentParamAReference();
160 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700161 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700163 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700164 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
168 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
170 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
171 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
172 CHECK(input_in_reg || input_on_stack);
173
174 if (input_in_reg) {
175 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
176 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 } else if (input_on_stack) {
179 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
180 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 mr_conv->InterproceduralScratchRegister());
183 }
184 }
185 mr_conv->Next();
186 main_jni_conv->Next();
187 }
188
189 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700190 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100191 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100192 } else {
193 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100194 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195
196 // 5. Move frame down to allow space for out going args.
197 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100198 size_t current_out_arg_size = main_out_arg_size;
199 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700201 // Call the read barrier for the declaring class loaded from the method for a static call.
202 // Note that we always have outgoing param space available for at least two params.
203 if (kUseReadBarrier && is_static) {
204 ThreadOffset<4> read_barrier32 = QUICK_ENTRYPOINT_OFFSET(4, pReadBarrierJni);
205 ThreadOffset<8> read_barrier64 = QUICK_ENTRYPOINT_OFFSET(8, pReadBarrierJni);
206 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
207 main_jni_conv->Next(); // Skip JNIEnv.
208 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
209 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
210 // Pass the handle for the class as the first argument.
211 if (main_jni_conv->IsCurrentParamOnStack()) {
212 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
213 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
214 mr_conv->InterproceduralScratchRegister(),
215 false);
216 } else {
217 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
218 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
219 ManagedRegister::NoRegister(), false);
220 }
221 main_jni_conv->Next();
222 // Pass the current thread as the second argument and call.
223 if (main_jni_conv->IsCurrentParamInRegister()) {
224 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
225 if (is_64_bit_target) {
226 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier64),
227 main_jni_conv->InterproceduralScratchRegister());
228 } else {
229 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier32),
230 main_jni_conv->InterproceduralScratchRegister());
231 }
232 } else {
233 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
234 main_jni_conv->InterproceduralScratchRegister());
235 if (is_64_bit_target) {
236 __ CallFromThread64(read_barrier64, main_jni_conv->InterproceduralScratchRegister());
237 } else {
238 __ CallFromThread32(read_barrier32, main_jni_conv->InterproceduralScratchRegister());
239 }
240 }
241 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
242 }
243
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
245 // can occur. The result is the saved JNI local state that is restored by the exit call. We
246 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
247 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100248 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
249 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
250 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
251 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700253 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 if (is_synchronized) {
255 // Pass object for locking.
256 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700257 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700258 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
259 if (main_jni_conv->IsCurrentParamOnStack()) {
260 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700261 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 mr_conv->InterproceduralScratchRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700263 } else {
264 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700265 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 ManagedRegister::NoRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 }
268 main_jni_conv->Next();
269 }
270 if (main_jni_conv->IsCurrentParamInRegister()) {
271 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700272 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100273 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100275 } else {
276 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100278 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 } else {
280 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
281 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700282 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100283 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
284 } else {
285 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
286 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 }
288 if (is_synchronized) { // Check for exceptions from monitor enter.
289 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
290 }
291 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
292 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
293
294 // 7. Iterate over arguments placing values from managed calling convention in
295 // to the convention required for a native call (shuffling). For references
296 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700297 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700299 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100300 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 uint32_t args_count = 0;
302 while (mr_conv->HasNext()) {
303 args_count++;
304 mr_conv->Next();
305 }
306
307 // Do a backward pass over arguments, so that the generated code will be "mov
308 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
309 // TODO: A reverse iterator to improve readability.
310 for (uint32_t i = 0; i < args_count; ++i) {
311 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
312 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
313 main_jni_conv->Next(); // Skip JNIEnv*.
314 if (is_static) {
315 main_jni_conv->Next(); // Skip Class for now.
316 }
317 // Skip to the argument we're interested in.
318 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
319 mr_conv->Next();
320 main_jni_conv->Next();
321 }
322 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
323 }
324 if (is_static) {
325 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100326 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
328 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700329 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 if (main_jni_conv->IsCurrentParamOnStack()) {
331 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700332 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 mr_conv->InterproceduralScratchRegister(),
334 false);
335 } else {
336 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700337 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 ManagedRegister::NoRegister(), false);
339 }
340 }
341
342 // 8. Create 1st argument, the JNI environment ptr.
343 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
344 // Register that will hold local indirect reference table
345 if (main_jni_conv->IsCurrentParamInRegister()) {
346 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
347 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700348 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100349 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
350 } else {
351 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
352 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 } else {
354 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700355 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100356 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700357 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100358 } else {
359 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100361 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 }
363
364 // 9. Plant call to native code associated with method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365 MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset(
Mathieu Chartier2d721012014-11-10 11:08:06 -0800366 InstructionSetPointerSize(instruction_set));
367 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 mr_conv->InterproceduralScratchRegister());
369
370 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700371 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
373 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
374 __ SignExtend(main_jni_conv->ReturnRegister(),
375 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
376 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
377 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
378 __ ZeroExtend(main_jni_conv->ReturnRegister(),
379 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
380 }
381 }
382
383 // 11. Save return value
384 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
385 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100386 if ((instruction_set == kMips || instruction_set == kMips64) &&
387 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 return_save_location.Uint32Value() % 8 != 0) {
389 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700390 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100392 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
394 }
395
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100396 // Increase frame size for out args if needed by the end_jni_conv.
397 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
398 if (end_out_arg_size > current_out_arg_size) {
399 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
400 current_out_arg_size = end_out_arg_size;
401 __ IncreaseFrameSize(out_arg_size_diff);
402 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
403 locked_object_handle_scope_offset =
404 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
405 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
406 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 // thread.
408 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100409 ThreadOffset<4> jni_end32(-1);
410 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 if (reference_return) {
412 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100413 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
414 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
415 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
416 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
418 end_jni_conv->Next();
419 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100420 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
421 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
422 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
423 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
425 // Pass saved local reference state.
426 if (end_jni_conv->IsCurrentParamOnStack()) {
427 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
428 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
429 } else {
430 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
431 __ Load(out_reg, saved_cookie_offset, 4);
432 }
433 end_jni_conv->Next();
434 if (is_synchronized) {
435 // Pass object for unlocking.
436 if (end_jni_conv->IsCurrentParamOnStack()) {
437 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700438 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 end_jni_conv->InterproceduralScratchRegister(),
440 false);
441 } else {
442 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 ManagedRegister::NoRegister(), false);
445 }
446 end_jni_conv->Next();
447 }
448 if (end_jni_conv->IsCurrentParamInRegister()) {
449 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700450 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100451 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
452 end_jni_conv->InterproceduralScratchRegister());
453 } else {
454 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
455 end_jni_conv->InterproceduralScratchRegister());
456 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 } else {
458 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
459 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700460 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100461 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
462 } else {
463 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
464 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 }
466
467 // 13. Reload return value
468 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
469 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
470 }
471
472 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100473 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474
475 // 15. Process pending exceptions from JNI call or monitor exit.
476 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
477
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700478 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100480 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700481 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100482 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700483
484 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000485 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 size_t cs = __ CodeSize();
487 std::vector<uint8_t> managed_code(cs);
488 MemoryRegion code(&managed_code[0], managed_code.size());
489 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100490
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 return CompiledMethod::SwapAllocCompiledMethod(driver,
492 instruction_set,
493 ArrayRef<const uint8_t>(managed_code),
494 frame_size,
495 main_jni_conv->CoreSpillMask(),
496 main_jni_conv->FpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100497 ArrayRef<const SrcMapElem>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100498 ArrayRef<const uint8_t>(), // vmap_table.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100499 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
500 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501}
502
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700503// Copy a single parameter from the managed to the JNI calling convention.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504static void CopyParameter(Assembler* jni_asm,
505 ManagedRuntimeCallingConvention* mr_conv,
506 JniCallingConvention* jni_conv,
507 size_t frame_size, size_t out_arg_size) {
508 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
509 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700510 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700511 bool null_allowed = false;
512 bool ref_param = jni_conv->IsCurrentParamAReference();
513 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
514 // input may be in register, on stack or both - but not none!
515 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
516 if (output_in_reg) { // output shouldn't straddle registers and stack
517 CHECK(!jni_conv->IsCurrentParamOnStack());
518 } else {
519 CHECK(jni_conv->IsCurrentParamOnStack());
520 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700521 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700522 if (ref_param) {
523 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700524 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
525 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700527 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
528 // Check handle scope offset is within frame.
529 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 }
531 if (input_in_reg && output_in_reg) {
532 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
533 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
534 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700535 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 } else {
537 if (!mr_conv->IsCurrentParamOnStack()) {
538 // regular non-straddling move
539 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
540 } else {
541 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
542 }
543 }
544 } else if (!input_in_reg && !output_in_reg) {
545 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
546 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700547 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700548 null_allowed);
549 } else {
550 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
551 size_t param_size = mr_conv->CurrentParamSize();
552 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
553 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
554 }
555 } else if (!input_in_reg && output_in_reg) {
556 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
557 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
558 // Check that incoming stack arguments are above the current stack frame.
559 CHECK_GT(in_off.Uint32Value(), frame_size);
560 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700561 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 } else {
563 size_t param_size = mr_conv->CurrentParamSize();
564 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
565 __ Load(out_reg, in_off, param_size);
566 }
567 } else {
568 CHECK(input_in_reg && !output_in_reg);
569 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
570 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
571 // Check outgoing argument is within frame
572 CHECK_LT(out_off.Uint32Value(), frame_size);
573 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700574 // TODO: recycle value in in_reg rather than reload from handle scope
575 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 null_allowed);
577 } else {
578 size_t param_size = mr_conv->CurrentParamSize();
579 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
580 if (!mr_conv->IsCurrentParamOnStack()) {
581 // regular non-straddling store
582 __ Store(out_off, in_reg, param_size);
583 } else {
584 // store where input straddles registers and stack
585 CHECK_EQ(param_size, 8u);
586 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
587 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
588 }
589 }
590 }
591}
592
593static void SetNativeParameter(Assembler* jni_asm,
594 JniCallingConvention* jni_conv,
595 ManagedRegister in_reg) {
596 if (jni_conv->IsCurrentParamOnStack()) {
597 FrameOffset dest = jni_conv->CurrentParamStackOffset();
598 __ StoreRawPtr(dest, in_reg);
599 } else {
600 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
601 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
602 }
603 }
604}
605
Andreas Gampe53c913b2014-08-12 23:19:23 -0700606CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
607 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
609}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700610
611} // namespace art