blob: 45e2fd0ffe5abdb682f80391a12d645abfc176b5 [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"
Ian Rogers166db042013-07-26 12:05:57 -070031#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070032#include "jni_env_ext.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "mirror/art_method.h"
Ian Rogers166db042013-07-26 12:05:57 -070034#include "utils/assembler.h"
35#include "utils/managed_register.h"
36#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010037#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070038#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010039#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070040#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042
43#define __ jni_asm->
44
45namespace art {
46
47static void CopyParameter(Assembler* jni_asm,
48 ManagedRuntimeCallingConvention* mr_conv,
49 JniCallingConvention* jni_conv,
50 size_t frame_size, size_t out_arg_size);
51static void SetNativeParameter(Assembler* jni_asm,
52 JniCallingConvention* jni_conv,
53 ManagedRegister in_reg);
54
55// Generate the JNI bridge for the given method, general contract:
56// - Arguments are in the managed runtime format, either on stack or in
57// registers, a reference to the method object is supplied as part of this
58// convention.
59//
Ian Rogers72d32622014-05-06 16:20:11 -070060CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 uint32_t access_flags, uint32_t method_idx,
62 const DexFile& dex_file) {
63 const bool is_native = (access_flags & kAccNative) != 0;
64 CHECK(is_native);
65 const bool is_static = (access_flags & kAccStatic) != 0;
66 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
67 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070068 InstructionSet instruction_set = driver->GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070069 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070071 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
73 bool reference_return = main_jni_conv->IsReturnAReference();
74
Ian Rogers700a4022014-05-19 16:49:03 -070075 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
77
78 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
79 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010080 const char* jni_end_shorty;
81 if (reference_return && is_synchronized) {
82 jni_end_shorty = "ILL";
83 } else if (reference_return) {
84 jni_end_shorty = "IL";
85 } else if (is_synchronized) {
86 jni_end_shorty = "VL";
87 } else {
88 jni_end_shorty = "V";
89 }
90
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
93
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070095 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070096
97 // Offsets into data structures
98 // TODO: if cross compiling these offsets are for the host not the target
99 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
100 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
101 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
102
103 // 1. Build the frame saving all callee saves
104 const size_t frame_size(main_jni_conv->FrameSize());
105 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
106 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
107
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 mr_conv->ResetIterator(FrameOffset(frame_size));
110 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 main_jni_conv->ReferenceCount(),
113 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100114
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700115 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700116 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
117 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100118 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700119 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
120 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100121 mr_conv->InterproceduralScratchRegister());
122 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700123 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
124 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100125 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700126 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
127 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100128 mr_conv->InterproceduralScratchRegister());
129 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700131 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 main_jni_conv->Next(); // Skip JNIEnv*
133 // 3.5. Create Class argument for static methods out of passed method
134 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
136 // Check handle scope offset is within frame
137 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800138 // TODO: Insert the read barrier for this load.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700140 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700141 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700142 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
143 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 }
145 while (mr_conv->HasNext()) {
146 CHECK(main_jni_conv->HasNext());
147 bool ref_param = main_jni_conv->IsCurrentParamAReference();
148 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700149 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
154 // Check handle scope offset is within frame and doesn't run into the saved segment state
155 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
156 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
158 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
159 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
160 CHECK(input_in_reg || input_on_stack);
161
162 if (input_in_reg) {
163 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
164 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 } else if (input_on_stack) {
167 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
168 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 mr_conv->InterproceduralScratchRegister());
171 }
172 }
173 mr_conv->Next();
174 main_jni_conv->Next();
175 }
176
177 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700178 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100179 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100180 } else {
181 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100182 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183
184 // 5. Move frame down to allow space for out going args.
185 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100186 size_t current_out_arg_size = main_out_arg_size;
187 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
190 // can occur. The result is the saved JNI local state that is restored by the exit call. We
191 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
192 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100193 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
194 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
195 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
196 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700198 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199 if (is_synchronized) {
200 // Pass object for locking.
201 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
204 if (main_jni_conv->IsCurrentParamOnStack()) {
205 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 mr_conv->InterproceduralScratchRegister(),
208 false);
209 } else {
210 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700211 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700212 ManagedRegister::NoRegister(), false);
213 }
214 main_jni_conv->Next();
215 }
216 if (main_jni_conv->IsCurrentParamInRegister()) {
217 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700218 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100219 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
220 main_jni_conv->InterproceduralScratchRegister());
221 } else {
222 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
223 main_jni_conv->InterproceduralScratchRegister());
224 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225 } else {
226 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
227 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700228 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100229 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
230 } else {
231 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
232 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 }
234 if (is_synchronized) { // Check for exceptions from monitor enter.
235 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
236 }
237 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
238 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
239
240 // 7. Iterate over arguments placing values from managed calling convention in
241 // to the convention required for a native call (shuffling). For references
242 // place an index/pointer to the reference after checking whether it is
243 // NULL (which must be encoded as NULL).
244 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
245 // give as many free registers for the shuffle as possible
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100246 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 uint32_t args_count = 0;
248 while (mr_conv->HasNext()) {
249 args_count++;
250 mr_conv->Next();
251 }
252
253 // Do a backward pass over arguments, so that the generated code will be "mov
254 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
255 // TODO: A reverse iterator to improve readability.
256 for (uint32_t i = 0; i < args_count; ++i) {
257 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
258 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
259 main_jni_conv->Next(); // Skip JNIEnv*.
260 if (is_static) {
261 main_jni_conv->Next(); // Skip Class for now.
262 }
263 // Skip to the argument we're interested in.
264 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
265 mr_conv->Next();
266 main_jni_conv->Next();
267 }
268 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
269 }
270 if (is_static) {
271 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100272 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
274 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700275 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 if (main_jni_conv->IsCurrentParamOnStack()) {
277 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 mr_conv->InterproceduralScratchRegister(),
280 false);
281 } else {
282 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700283 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 ManagedRegister::NoRegister(), false);
285 }
286 }
287
288 // 8. Create 1st argument, the JNI environment ptr.
289 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
290 // Register that will hold local indirect reference table
291 if (main_jni_conv->IsCurrentParamInRegister()) {
292 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
293 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700294 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100295 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
296 } else {
297 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
298 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 } else {
300 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700301 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100302 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100304 } else {
305 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
306 main_jni_conv->InterproceduralScratchRegister());
307 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 }
309
310 // 9. Plant call to native code associated with method.
Mathieu Chartier2d721012014-11-10 11:08:06 -0800311 MemberOffset jni_entrypoint_offset = mirror::ArtMethod::EntryPointFromJniOffset(
312 InstructionSetPointerSize(instruction_set));
313 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 mr_conv->InterproceduralScratchRegister());
315
316 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700317 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
319 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
320 __ SignExtend(main_jni_conv->ReturnRegister(),
321 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
322 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
323 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
324 __ ZeroExtend(main_jni_conv->ReturnRegister(),
325 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
326 }
327 }
328
329 // 11. Save return value
330 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
331 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100332 if ((instruction_set == kMips || instruction_set == kMips64) &&
333 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 return_save_location.Uint32Value() % 8 != 0) {
335 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700336 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100338 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
340 }
341
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100342 // Increase frame size for out args if needed by the end_jni_conv.
343 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
344 if (end_out_arg_size > current_out_arg_size) {
345 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
346 current_out_arg_size = end_out_arg_size;
347 __ IncreaseFrameSize(out_arg_size_diff);
348 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
349 locked_object_handle_scope_offset =
350 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
351 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
352 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700353 // thread.
354 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100355 ThreadOffset<4> jni_end32(-1);
356 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 if (reference_return) {
358 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100359 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
360 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
361 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
362 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700363 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
364 end_jni_conv->Next();
365 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100366 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
367 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
368 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
369 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700370 }
371 // Pass saved local reference state.
372 if (end_jni_conv->IsCurrentParamOnStack()) {
373 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
374 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
375 } else {
376 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
377 __ Load(out_reg, saved_cookie_offset, 4);
378 }
379 end_jni_conv->Next();
380 if (is_synchronized) {
381 // Pass object for unlocking.
382 if (end_jni_conv->IsCurrentParamOnStack()) {
383 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700384 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 end_jni_conv->InterproceduralScratchRegister(),
386 false);
387 } else {
388 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700389 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 ManagedRegister::NoRegister(), false);
391 }
392 end_jni_conv->Next();
393 }
394 if (end_jni_conv->IsCurrentParamInRegister()) {
395 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700396 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100397 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
398 end_jni_conv->InterproceduralScratchRegister());
399 } else {
400 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
401 end_jni_conv->InterproceduralScratchRegister());
402 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 } else {
404 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
405 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700406 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100407 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
408 } else {
409 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
410 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 }
412
413 // 13. Reload return value
414 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
415 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
416 }
417
418 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100419 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420
421 // 15. Process pending exceptions from JNI call or monitor exit.
422 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
423
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700424 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 // them.
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700426 __ RemoveFrame(frame_size, callee_save_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427
428 // 17. Finalize code generation
429 __ EmitSlowPaths();
430 size_t cs = __ CodeSize();
431 std::vector<uint8_t> managed_code(cs);
432 MemoryRegion code(&managed_code[0], managed_code.size());
433 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100434
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800435 return CompiledMethod::SwapAllocCompiledMethodCFI(driver,
436 instruction_set,
437 ArrayRef<const uint8_t>(managed_code),
438 frame_size,
439 main_jni_conv->CoreSpillMask(),
440 main_jni_conv->FpSpillMask(),
David Srbecky8c578312015-04-07 19:46:22 +0100441 ArrayRef<const uint8_t>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442}
443
444// Copy a single parameter from the managed to the JNI calling convention
445static void CopyParameter(Assembler* jni_asm,
446 ManagedRuntimeCallingConvention* mr_conv,
447 JniCallingConvention* jni_conv,
448 size_t frame_size, size_t out_arg_size) {
449 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
450 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700451 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700452 bool null_allowed = false;
453 bool ref_param = jni_conv->IsCurrentParamAReference();
454 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
455 // input may be in register, on stack or both - but not none!
456 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
457 if (output_in_reg) { // output shouldn't straddle registers and stack
458 CHECK(!jni_conv->IsCurrentParamOnStack());
459 } else {
460 CHECK(jni_conv->IsCurrentParamOnStack());
461 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700462 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 if (ref_param) {
464 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700465 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
466 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700468 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
469 // Check handle scope offset is within frame.
470 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 }
472 if (input_in_reg && output_in_reg) {
473 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
474 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
475 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700476 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 } else {
478 if (!mr_conv->IsCurrentParamOnStack()) {
479 // regular non-straddling move
480 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
481 } else {
482 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
483 }
484 }
485 } else if (!input_in_reg && !output_in_reg) {
486 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
487 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700488 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 null_allowed);
490 } else {
491 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
492 size_t param_size = mr_conv->CurrentParamSize();
493 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
494 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
495 }
496 } else if (!input_in_reg && output_in_reg) {
497 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
498 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
499 // Check that incoming stack arguments are above the current stack frame.
500 CHECK_GT(in_off.Uint32Value(), frame_size);
501 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700502 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700503 } else {
504 size_t param_size = mr_conv->CurrentParamSize();
505 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
506 __ Load(out_reg, in_off, param_size);
507 }
508 } else {
509 CHECK(input_in_reg && !output_in_reg);
510 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
511 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
512 // Check outgoing argument is within frame
513 CHECK_LT(out_off.Uint32Value(), frame_size);
514 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700515 // TODO: recycle value in in_reg rather than reload from handle scope
516 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 null_allowed);
518 } else {
519 size_t param_size = mr_conv->CurrentParamSize();
520 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
521 if (!mr_conv->IsCurrentParamOnStack()) {
522 // regular non-straddling store
523 __ Store(out_off, in_reg, param_size);
524 } else {
525 // store where input straddles registers and stack
526 CHECK_EQ(param_size, 8u);
527 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
528 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
529 }
530 }
531 }
532}
533
534static void SetNativeParameter(Assembler* jni_asm,
535 JniCallingConvention* jni_conv,
536 ManagedRegister in_reg) {
537 if (jni_conv->IsCurrentParamOnStack()) {
538 FrameOffset dest = jni_conv->CurrentParamStackOffset();
539 __ StoreRawPtr(dest, in_reg);
540 } else {
541 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
542 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
543 }
544 }
545}
546
Andreas Gampe53c913b2014-08-12 23:19:23 -0700547CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
548 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700549 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
550}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700551
552} // namespace art