blob: 6f2cb25911a569a578a8c679e343fe0b0368426f [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
24#include "base/logging.h"
25#include "base/macros.h"
26#include "calling_convention.h"
27#include "class_linker.h"
28#include "compiled_method.h"
29#include "dex_file-inl.h"
30#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010031#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070032#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070033#include "jni_env_ext.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070034#include "mirror/art_method.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();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070070 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070072 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070073 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
74 bool reference_return = main_jni_conv->IsReturnAReference();
75
Ian Rogers700a4022014-05-19 16:49:03 -070076 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
78
79 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
80 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010081 const char* jni_end_shorty;
82 if (reference_return && is_synchronized) {
83 jni_end_shorty = "ILL";
84 } else if (reference_return) {
85 jni_end_shorty = "IL";
86 } else if (is_synchronized) {
87 jni_end_shorty = "VL";
88 } else {
89 jni_end_shorty = "V";
90 }
91
Ian Rogers700a4022014-05-19 16:49:03 -070092 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
94
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070096 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
David Srbecky8dc73242015-04-12 11:40:39 +010097 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GetIncludeCFI());
Brian Carlstrom7940e442013-07-12 13:46:57 -070098
99 // Offsets into data structures
100 // TODO: if cross compiling these offsets are for the host not the target
101 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
102 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
103 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
104
105 // 1. Build the frame saving all callee saves
106 const size_t frame_size(main_jni_conv->FrameSize());
107 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
108 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100109 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 mr_conv->ResetIterator(FrameOffset(frame_size));
113 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700114 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 main_jni_conv->ReferenceCount(),
116 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100117
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700118 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
120 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100121 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
123 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100124 mr_conv->InterproceduralScratchRegister());
125 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
127 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100128 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700129 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
130 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100131 mr_conv->InterproceduralScratchRegister());
132 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700134 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 main_jni_conv->Next(); // Skip JNIEnv*
136 // 3.5. Create Class argument for static methods out of passed method
137 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700138 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
139 // Check handle scope offset is within frame
140 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800141 // TODO: Insert the read barrier for this load.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700143 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
146 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 }
148 while (mr_conv->HasNext()) {
149 CHECK(main_jni_conv->HasNext());
150 bool ref_param = main_jni_conv->IsCurrentParamAReference();
151 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700157 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
159 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
161 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
162 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
163 CHECK(input_in_reg || input_on_stack);
164
165 if (input_in_reg) {
166 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
167 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 } else if (input_on_stack) {
170 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
171 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700172 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 mr_conv->InterproceduralScratchRegister());
174 }
175 }
176 mr_conv->Next();
177 main_jni_conv->Next();
178 }
179
180 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700181 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100182 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100183 } else {
184 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100185 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186
187 // 5. Move frame down to allow space for out going args.
188 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100189 size_t current_out_arg_size = main_out_arg_size;
190 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
193 // can occur. The result is the saved JNI local state that is restored by the exit call. We
194 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
195 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100196 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
197 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
198 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
199 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700201 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 if (is_synchronized) {
203 // Pass object for locking.
204 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
207 if (main_jni_conv->IsCurrentParamOnStack()) {
208 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700209 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 mr_conv->InterproceduralScratchRegister(),
211 false);
212 } else {
213 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700214 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215 ManagedRegister::NoRegister(), false);
216 }
217 main_jni_conv->Next();
218 }
219 if (main_jni_conv->IsCurrentParamInRegister()) {
220 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700221 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100222 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
223 main_jni_conv->InterproceduralScratchRegister());
224 } else {
225 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
226 main_jni_conv->InterproceduralScratchRegister());
227 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700228 } else {
229 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
230 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700231 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100232 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
233 } else {
234 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
235 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 }
237 if (is_synchronized) { // Check for exceptions from monitor enter.
238 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
239 }
240 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
241 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
242
243 // 7. Iterate over arguments placing values from managed calling convention in
244 // to the convention required for a native call (shuffling). For references
245 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700246 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700248 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100249 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 uint32_t args_count = 0;
251 while (mr_conv->HasNext()) {
252 args_count++;
253 mr_conv->Next();
254 }
255
256 // Do a backward pass over arguments, so that the generated code will be "mov
257 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
258 // TODO: A reverse iterator to improve readability.
259 for (uint32_t i = 0; i < args_count; ++i) {
260 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
261 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
262 main_jni_conv->Next(); // Skip JNIEnv*.
263 if (is_static) {
264 main_jni_conv->Next(); // Skip Class for now.
265 }
266 // Skip to the argument we're interested in.
267 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
268 mr_conv->Next();
269 main_jni_conv->Next();
270 }
271 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
272 }
273 if (is_static) {
274 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100275 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
277 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 if (main_jni_conv->IsCurrentParamOnStack()) {
280 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700281 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 mr_conv->InterproceduralScratchRegister(),
283 false);
284 } else {
285 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700286 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 ManagedRegister::NoRegister(), false);
288 }
289 }
290
291 // 8. Create 1st argument, the JNI environment ptr.
292 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
293 // Register that will hold local indirect reference table
294 if (main_jni_conv->IsCurrentParamInRegister()) {
295 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
296 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700297 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100298 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
299 } else {
300 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
301 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 } else {
303 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700304 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100305 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100307 } else {
308 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
309 main_jni_conv->InterproceduralScratchRegister());
310 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 }
312
313 // 9. Plant call to native code associated with method.
Mathieu Chartier2d721012014-11-10 11:08:06 -0800314 MemberOffset jni_entrypoint_offset = mirror::ArtMethod::EntryPointFromJniOffset(
315 InstructionSetPointerSize(instruction_set));
316 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 mr_conv->InterproceduralScratchRegister());
318
319 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700320 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
322 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
323 __ SignExtend(main_jni_conv->ReturnRegister(),
324 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
325 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
326 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
327 __ ZeroExtend(main_jni_conv->ReturnRegister(),
328 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
329 }
330 }
331
332 // 11. Save return value
333 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
334 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100335 if ((instruction_set == kMips || instruction_set == kMips64) &&
336 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 return_save_location.Uint32Value() % 8 != 0) {
338 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700339 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100341 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
343 }
344
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100345 // Increase frame size for out args if needed by the end_jni_conv.
346 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
347 if (end_out_arg_size > current_out_arg_size) {
348 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
349 current_out_arg_size = end_out_arg_size;
350 __ IncreaseFrameSize(out_arg_size_diff);
351 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
352 locked_object_handle_scope_offset =
353 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
354 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
355 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356 // thread.
357 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100358 ThreadOffset<4> jni_end32(-1);
359 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 if (reference_return) {
361 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100362 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
363 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
364 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
365 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700366 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
367 end_jni_conv->Next();
368 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100369 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
370 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
371 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
372 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
374 // Pass saved local reference state.
375 if (end_jni_conv->IsCurrentParamOnStack()) {
376 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
377 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
378 } else {
379 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
380 __ Load(out_reg, saved_cookie_offset, 4);
381 }
382 end_jni_conv->Next();
383 if (is_synchronized) {
384 // Pass object for unlocking.
385 if (end_jni_conv->IsCurrentParamOnStack()) {
386 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700387 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 end_jni_conv->InterproceduralScratchRegister(),
389 false);
390 } else {
391 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700392 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 ManagedRegister::NoRegister(), false);
394 }
395 end_jni_conv->Next();
396 }
397 if (end_jni_conv->IsCurrentParamInRegister()) {
398 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700399 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100400 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
401 end_jni_conv->InterproceduralScratchRegister());
402 } else {
403 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
404 end_jni_conv->InterproceduralScratchRegister());
405 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700406 } else {
407 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
408 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700409 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100410 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
411 } else {
412 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
413 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700414 }
415
416 // 13. Reload return value
417 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
418 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
419 }
420
421 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100422 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423
424 // 15. Process pending exceptions from JNI call or monitor exit.
425 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
426
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700427 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700428 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100429 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700430 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100431 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432
433 // 17. Finalize code generation
434 __ EmitSlowPaths();
435 size_t cs = __ CodeSize();
436 std::vector<uint8_t> managed_code(cs);
437 MemoryRegion code(&managed_code[0], managed_code.size());
438 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100439
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100440 return CompiledMethod::SwapAllocCompiledMethod(driver,
441 instruction_set,
442 ArrayRef<const uint8_t>(managed_code),
443 frame_size,
444 main_jni_conv->CoreSpillMask(),
445 main_jni_conv->FpSpillMask(),
446 nullptr, // src_mapping_table.
447 ArrayRef<const uint8_t>(), // mapping_table.
448 ArrayRef<const uint8_t>(), // vmap_table.
449 ArrayRef<const uint8_t>(), // native_gc_map.
450 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
451 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452}
453
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700454// Copy a single parameter from the managed to the JNI calling convention.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455static void CopyParameter(Assembler* jni_asm,
456 ManagedRuntimeCallingConvention* mr_conv,
457 JniCallingConvention* jni_conv,
458 size_t frame_size, size_t out_arg_size) {
459 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
460 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700461 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 bool null_allowed = false;
463 bool ref_param = jni_conv->IsCurrentParamAReference();
464 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
465 // input may be in register, on stack or both - but not none!
466 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
467 if (output_in_reg) { // output shouldn't straddle registers and stack
468 CHECK(!jni_conv->IsCurrentParamOnStack());
469 } else {
470 CHECK(jni_conv->IsCurrentParamOnStack());
471 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700472 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473 if (ref_param) {
474 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700475 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
476 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700478 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
479 // Check handle scope offset is within frame.
480 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 }
482 if (input_in_reg && output_in_reg) {
483 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
484 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
485 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700486 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 } else {
488 if (!mr_conv->IsCurrentParamOnStack()) {
489 // regular non-straddling move
490 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
491 } else {
492 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
493 }
494 }
495 } else if (!input_in_reg && !output_in_reg) {
496 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
497 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700498 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 null_allowed);
500 } else {
501 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
502 size_t param_size = mr_conv->CurrentParamSize();
503 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
504 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
505 }
506 } else if (!input_in_reg && output_in_reg) {
507 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
508 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
509 // Check that incoming stack arguments are above the current stack frame.
510 CHECK_GT(in_off.Uint32Value(), frame_size);
511 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700512 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 } else {
514 size_t param_size = mr_conv->CurrentParamSize();
515 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
516 __ Load(out_reg, in_off, param_size);
517 }
518 } else {
519 CHECK(input_in_reg && !output_in_reg);
520 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
521 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
522 // Check outgoing argument is within frame
523 CHECK_LT(out_off.Uint32Value(), frame_size);
524 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700525 // TODO: recycle value in in_reg rather than reload from handle scope
526 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527 null_allowed);
528 } else {
529 size_t param_size = mr_conv->CurrentParamSize();
530 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
531 if (!mr_conv->IsCurrentParamOnStack()) {
532 // regular non-straddling store
533 __ Store(out_off, in_reg, param_size);
534 } else {
535 // store where input straddles registers and stack
536 CHECK_EQ(param_size, 8u);
537 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
538 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
539 }
540 }
541 }
542}
543
544static void SetNativeParameter(Assembler* jni_asm,
545 JniCallingConvention* jni_conv,
546 ManagedRegister in_reg) {
547 if (jni_conv->IsCurrentParamOnStack()) {
548 FrameOffset dest = jni_conv->CurrentParamStackOffset();
549 __ StoreRawPtr(dest, in_reg);
550 } else {
551 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
552 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
553 }
554 }
555}
556
Andreas Gampe53c913b2014-08-12 23:19:23 -0700557CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
558 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700559 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
560}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700561
562} // namespace art