blob: 34f0802444c3a0d3a21bad83f3112deb4cb966d6 [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "base/logging.h"
26#include "base/macros.h"
27#include "calling_convention.h"
28#include "class_linker.h"
29#include "compiled_method.h"
30#include "dex_file-inl.h"
31#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010032#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070033#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070034#include "jni_env_ext.h"
Ian Rogers166db042013-07-26 12:05:57 -070035#include "utils/assembler.h"
36#include "utils/managed_register.h"
37#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010038#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070039#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010040#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070041#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070043
44#define __ jni_asm->
45
46namespace art {
47
48static void CopyParameter(Assembler* jni_asm,
49 ManagedRuntimeCallingConvention* mr_conv,
50 JniCallingConvention* jni_conv,
51 size_t frame_size, size_t out_arg_size);
52static void SetNativeParameter(Assembler* jni_asm,
53 JniCallingConvention* jni_conv,
54 ManagedRegister in_reg);
55
56// Generate the JNI bridge for the given method, general contract:
57// - Arguments are in the managed runtime format, either on stack or in
58// registers, a reference to the method object is supplied as part of this
59// convention.
60//
Ian Rogers72d32622014-05-06 16:20:11 -070061CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 uint32_t access_flags, uint32_t method_idx,
63 const DexFile& dex_file) {
64 const bool is_native = (access_flags & kAccNative) != 0;
65 CHECK(is_native);
66 const bool is_static = (access_flags & kAccStatic) != 0;
67 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
68 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070069 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020070 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070071 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070073 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
75 bool reference_return = main_jni_conv->IsReturnAReference();
76
Ian Rogers700a4022014-05-19 16:49:03 -070077 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
79
80 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
81 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010082 const char* jni_end_shorty;
83 if (reference_return && is_synchronized) {
84 jni_end_shorty = "ILL";
85 } else if (reference_return) {
86 jni_end_shorty = "IL";
87 } else if (is_synchronized) {
88 jni_end_shorty = "VL";
89 } else {
90 jni_end_shorty = "V";
91 }
92
Ian Rogers700a4022014-05-19 16:49:03 -070093 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
95
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 // Assembler that holds generated instructions
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020097 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set, instruction_set_features));
David Srbecky8363c772015-05-28 16:12:43 +010098 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GetGenerateDebugInfo());
Brian Carlstrom7940e442013-07-12 13:46:57 -070099
100 // Offsets into data structures
101 // TODO: if cross compiling these offsets are for the host not the target
102 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
103 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
104 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
105
106 // 1. Build the frame saving all callee saves
107 const size_t frame_size(main_jni_conv->FrameSize());
108 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
109 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100110 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 mr_conv->ResetIterator(FrameOffset(frame_size));
114 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 main_jni_conv->ReferenceCount(),
117 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100118
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700119 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700121 Thread::TopHandleScopeOffset<8>(),
122 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 main_jni_conv->HandleScopeOffset(),
125 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100126 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 Thread::TopHandleScopeOffset<4>(),
129 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700131 main_jni_conv->HandleScopeOffset(),
132 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100133 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 main_jni_conv->Next(); // Skip JNIEnv*
137 // 3.5. Create Class argument for static methods out of passed method
138 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700139 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
140 // Check handle scope offset is within frame
141 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Roland Levillain4d027112015-07-01 15:41:14 +0100142 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700143 // Note this LoadRef() does not include read barrier. It will be handled below.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700145 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700146 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700147 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
148 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 }
150 while (mr_conv->HasNext()) {
151 CHECK(main_jni_conv->HasNext());
152 bool ref_param = main_jni_conv->IsCurrentParamAReference();
153 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700157 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700159 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
161 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
163 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
164 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
165 CHECK(input_in_reg || input_on_stack);
166
167 if (input_in_reg) {
168 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
169 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 } else if (input_on_stack) {
172 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
173 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700174 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 mr_conv->InterproceduralScratchRegister());
176 }
177 }
178 mr_conv->Next();
179 main_jni_conv->Next();
180 }
181
182 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700183 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100184 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100185 } else {
186 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100187 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
189 // 5. Move frame down to allow space for out going args.
190 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100191 size_t current_out_arg_size = main_out_arg_size;
192 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700193
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700194 // Call the read barrier for the declaring class loaded from the method for a static call.
195 // Note that we always have outgoing param space available for at least two params.
196 if (kUseReadBarrier && is_static) {
197 ThreadOffset<4> read_barrier32 = QUICK_ENTRYPOINT_OFFSET(4, pReadBarrierJni);
198 ThreadOffset<8> read_barrier64 = QUICK_ENTRYPOINT_OFFSET(8, pReadBarrierJni);
199 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
200 main_jni_conv->Next(); // Skip JNIEnv.
201 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
202 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
203 // Pass the handle for the class as the first argument.
204 if (main_jni_conv->IsCurrentParamOnStack()) {
205 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
206 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
207 mr_conv->InterproceduralScratchRegister(),
208 false);
209 } else {
210 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
211 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
212 ManagedRegister::NoRegister(), false);
213 }
214 main_jni_conv->Next();
215 // Pass the current thread as the second argument and call.
216 if (main_jni_conv->IsCurrentParamInRegister()) {
217 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
218 if (is_64_bit_target) {
219 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier64),
220 main_jni_conv->InterproceduralScratchRegister());
221 } else {
222 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier32),
223 main_jni_conv->InterproceduralScratchRegister());
224 }
225 } else {
226 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
227 main_jni_conv->InterproceduralScratchRegister());
228 if (is_64_bit_target) {
229 __ CallFromThread64(read_barrier64, main_jni_conv->InterproceduralScratchRegister());
230 } else {
231 __ CallFromThread32(read_barrier32, main_jni_conv->InterproceduralScratchRegister());
232 }
233 }
234 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
235 }
236
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
238 // can occur. The result is the saved JNI local state that is restored by the exit call. We
239 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
240 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100241 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
242 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
243 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
244 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700246 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 if (is_synchronized) {
248 // Pass object for locking.
249 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700250 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
252 if (main_jni_conv->IsCurrentParamOnStack()) {
253 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700254 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 mr_conv->InterproceduralScratchRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 } else {
257 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700258 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 ManagedRegister::NoRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 }
261 main_jni_conv->Next();
262 }
263 if (main_jni_conv->IsCurrentParamInRegister()) {
264 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700265 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100266 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100268 } else {
269 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100271 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 } else {
273 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
274 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700275 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100276 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
277 } else {
278 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
279 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
281 if (is_synchronized) { // Check for exceptions from monitor enter.
282 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
283 }
284 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
285 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
286
287 // 7. Iterate over arguments placing values from managed calling convention in
288 // to the convention required for a native call (shuffling). For references
289 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700290 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100293 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 uint32_t args_count = 0;
295 while (mr_conv->HasNext()) {
296 args_count++;
297 mr_conv->Next();
298 }
299
300 // Do a backward pass over arguments, so that the generated code will be "mov
301 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
302 // TODO: A reverse iterator to improve readability.
303 for (uint32_t i = 0; i < args_count; ++i) {
304 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
305 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
306 main_jni_conv->Next(); // Skip JNIEnv*.
307 if (is_static) {
308 main_jni_conv->Next(); // Skip Class for now.
309 }
310 // Skip to the argument we're interested in.
311 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
312 mr_conv->Next();
313 main_jni_conv->Next();
314 }
315 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
316 }
317 if (is_static) {
318 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100319 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
321 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700322 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 if (main_jni_conv->IsCurrentParamOnStack()) {
324 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700325 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 mr_conv->InterproceduralScratchRegister(),
327 false);
328 } else {
329 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700330 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 ManagedRegister::NoRegister(), false);
332 }
333 }
334
335 // 8. Create 1st argument, the JNI environment ptr.
336 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
337 // Register that will hold local indirect reference table
338 if (main_jni_conv->IsCurrentParamInRegister()) {
339 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
340 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700341 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100342 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
343 } else {
344 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
345 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 } else {
347 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700348 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100349 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100351 } else {
352 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100354 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 }
356
357 // 9. Plant call to native code associated with method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset(
Mathieu Chartier2d721012014-11-10 11:08:06 -0800359 InstructionSetPointerSize(instruction_set));
360 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 mr_conv->InterproceduralScratchRegister());
362
363 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700364 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
366 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
367 __ SignExtend(main_jni_conv->ReturnRegister(),
368 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
369 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
370 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
371 __ ZeroExtend(main_jni_conv->ReturnRegister(),
372 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
373 }
374 }
375
376 // 11. Save return value
377 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
378 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100379 if ((instruction_set == kMips || instruction_set == kMips64) &&
380 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 return_save_location.Uint32Value() % 8 != 0) {
382 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700383 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100385 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
387 }
388
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100389 // Increase frame size for out args if needed by the end_jni_conv.
390 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
391 if (end_out_arg_size > current_out_arg_size) {
392 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
393 current_out_arg_size = end_out_arg_size;
394 __ IncreaseFrameSize(out_arg_size_diff);
395 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
396 locked_object_handle_scope_offset =
397 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
398 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
399 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 // thread.
401 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100402 ThreadOffset<4> jni_end32(-1);
403 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 if (reference_return) {
405 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100406 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
407 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
408 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
409 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
411 end_jni_conv->Next();
412 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100413 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
414 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
415 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
416 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 }
418 // Pass saved local reference state.
419 if (end_jni_conv->IsCurrentParamOnStack()) {
420 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
421 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
422 } else {
423 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
424 __ Load(out_reg, saved_cookie_offset, 4);
425 }
426 end_jni_conv->Next();
427 if (is_synchronized) {
428 // Pass object for unlocking.
429 if (end_jni_conv->IsCurrentParamOnStack()) {
430 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700431 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 end_jni_conv->InterproceduralScratchRegister(),
433 false);
434 } else {
435 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700436 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 ManagedRegister::NoRegister(), false);
438 }
439 end_jni_conv->Next();
440 }
441 if (end_jni_conv->IsCurrentParamInRegister()) {
442 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700443 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100444 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
445 end_jni_conv->InterproceduralScratchRegister());
446 } else {
447 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
448 end_jni_conv->InterproceduralScratchRegister());
449 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 } else {
451 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
452 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700453 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100454 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
455 } else {
456 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
457 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 }
459
460 // 13. Reload return value
461 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
462 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
463 }
464
465 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100466 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467
468 // 15. Process pending exceptions from JNI call or monitor exit.
469 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
470
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700471 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100473 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700474 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100475 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476
477 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000478 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700479 size_t cs = __ CodeSize();
480 std::vector<uint8_t> managed_code(cs);
481 MemoryRegion code(&managed_code[0], managed_code.size());
482 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100483
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100484 return CompiledMethod::SwapAllocCompiledMethod(driver,
485 instruction_set,
486 ArrayRef<const uint8_t>(managed_code),
487 frame_size,
488 main_jni_conv->CoreSpillMask(),
489 main_jni_conv->FpSpillMask(),
490 nullptr, // src_mapping_table.
491 ArrayRef<const uint8_t>(), // mapping_table.
492 ArrayRef<const uint8_t>(), // vmap_table.
493 ArrayRef<const uint8_t>(), // native_gc_map.
494 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
495 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496}
497
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498// Copy a single parameter from the managed to the JNI calling convention.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499static void CopyParameter(Assembler* jni_asm,
500 ManagedRuntimeCallingConvention* mr_conv,
501 JniCallingConvention* jni_conv,
502 size_t frame_size, size_t out_arg_size) {
503 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
504 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700505 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 bool null_allowed = false;
507 bool ref_param = jni_conv->IsCurrentParamAReference();
508 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
509 // input may be in register, on stack or both - but not none!
510 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
511 if (output_in_reg) { // output shouldn't straddle registers and stack
512 CHECK(!jni_conv->IsCurrentParamOnStack());
513 } else {
514 CHECK(jni_conv->IsCurrentParamOnStack());
515 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700516 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 if (ref_param) {
518 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700519 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
520 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700522 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
523 // Check handle scope offset is within frame.
524 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 }
526 if (input_in_reg && output_in_reg) {
527 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
528 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
529 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700530 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 } else {
532 if (!mr_conv->IsCurrentParamOnStack()) {
533 // regular non-straddling move
534 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
535 } else {
536 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
537 }
538 }
539 } else if (!input_in_reg && !output_in_reg) {
540 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
541 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700542 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 null_allowed);
544 } else {
545 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
546 size_t param_size = mr_conv->CurrentParamSize();
547 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
548 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
549 }
550 } else if (!input_in_reg && output_in_reg) {
551 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
552 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
553 // Check that incoming stack arguments are above the current stack frame.
554 CHECK_GT(in_off.Uint32Value(), frame_size);
555 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700556 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 } else {
558 size_t param_size = mr_conv->CurrentParamSize();
559 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
560 __ Load(out_reg, in_off, param_size);
561 }
562 } else {
563 CHECK(input_in_reg && !output_in_reg);
564 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
565 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
566 // Check outgoing argument is within frame
567 CHECK_LT(out_off.Uint32Value(), frame_size);
568 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700569 // TODO: recycle value in in_reg rather than reload from handle scope
570 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 null_allowed);
572 } else {
573 size_t param_size = mr_conv->CurrentParamSize();
574 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
575 if (!mr_conv->IsCurrentParamOnStack()) {
576 // regular non-straddling store
577 __ Store(out_off, in_reg, param_size);
578 } else {
579 // store where input straddles registers and stack
580 CHECK_EQ(param_size, 8u);
581 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
582 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
583 }
584 }
585 }
586}
587
588static void SetNativeParameter(Assembler* jni_asm,
589 JniCallingConvention* jni_conv,
590 ManagedRegister in_reg) {
591 if (jni_conv->IsCurrentParamOnStack()) {
592 FrameOffset dest = jni_conv->CurrentParamStackOffset();
593 __ StoreRawPtr(dest, in_reg);
594 } else {
595 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
596 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
597 }
598 }
599}
600
Andreas Gampe53c913b2014-08-12 23:19:23 -0700601CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
602 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
604}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700605
606} // namespace art