blob: c3fe75b3f14fed2d2409cf8744492cbbf6ca82c2 [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"
39#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070040#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041
42#define __ jni_asm->
43
44namespace art {
45
46static void CopyParameter(Assembler* jni_asm,
47 ManagedRuntimeCallingConvention* mr_conv,
48 JniCallingConvention* jni_conv,
49 size_t frame_size, size_t out_arg_size);
50static void SetNativeParameter(Assembler* jni_asm,
51 JniCallingConvention* jni_conv,
52 ManagedRegister in_reg);
53
54// Generate the JNI bridge for the given method, general contract:
55// - Arguments are in the managed runtime format, either on stack or in
56// registers, a reference to the method object is supplied as part of this
57// convention.
58//
Ian Rogers72d32622014-05-06 16:20:11 -070059CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 uint32_t access_flags, uint32_t method_idx,
61 const DexFile& dex_file) {
62 const bool is_native = (access_flags & kAccNative) != 0;
63 CHECK(is_native);
64 const bool is_static = (access_flags & kAccStatic) != 0;
65 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
66 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070067 InstructionSet instruction_set = driver->GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070068 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070070 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
72 bool reference_return = main_jni_conv->IsReturnAReference();
73
Ian Rogers700a4022014-05-19 16:49:03 -070074 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
76
77 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
78 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010079 const char* jni_end_shorty;
80 if (reference_return && is_synchronized) {
81 jni_end_shorty = "ILL";
82 } else if (reference_return) {
83 jni_end_shorty = "IL";
84 } else if (is_synchronized) {
85 jni_end_shorty = "VL";
86 } else {
87 jni_end_shorty = "V";
88 }
89
Ian Rogers700a4022014-05-19 16:49:03 -070090 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
92
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070094 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
Tong Shen547cdfd2014-08-05 01:54:19 -070095 jni_asm->InitializeFrameDescriptionEntry();
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);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700139 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700141 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
142 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 }
144 while (mr_conv->HasNext()) {
145 CHECK(main_jni_conv->HasNext());
146 bool ref_param = main_jni_conv->IsCurrentParamAReference();
147 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700148 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
153 // Check handle scope offset is within frame and doesn't run into the saved segment state
154 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
155 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
157 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
158 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
159 CHECK(input_in_reg || input_on_stack);
160
161 if (input_in_reg) {
162 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
163 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 } else if (input_on_stack) {
166 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
167 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700169 mr_conv->InterproceduralScratchRegister());
170 }
171 }
172 mr_conv->Next();
173 main_jni_conv->Next();
174 }
175
176 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700177 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100178 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100179 } else {
180 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100181 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
183 // 5. Move frame down to allow space for out going args.
184 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100185 size_t current_out_arg_size = main_out_arg_size;
186 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
189 // can occur. The result is the saved JNI local state that is restored by the exit call. We
190 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
191 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100192 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
193 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
194 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
195 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700197 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 if (is_synchronized) {
199 // Pass object for locking.
200 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700201 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700202 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
203 if (main_jni_conv->IsCurrentParamOnStack()) {
204 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700205 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700206 mr_conv->InterproceduralScratchRegister(),
207 false);
208 } else {
209 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700210 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 ManagedRegister::NoRegister(), false);
212 }
213 main_jni_conv->Next();
214 }
215 if (main_jni_conv->IsCurrentParamInRegister()) {
216 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700217 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100218 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
219 main_jni_conv->InterproceduralScratchRegister());
220 } else {
221 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
222 main_jni_conv->InterproceduralScratchRegister());
223 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700224 } else {
225 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
226 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700227 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100228 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
229 } else {
230 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
231 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 }
233 if (is_synchronized) { // Check for exceptions from monitor enter.
234 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
235 }
236 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
237 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
238
239 // 7. Iterate over arguments placing values from managed calling convention in
240 // to the convention required for a native call (shuffling). For references
241 // place an index/pointer to the reference after checking whether it is
242 // NULL (which must be encoded as NULL).
243 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
244 // give as many free registers for the shuffle as possible
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100245 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 uint32_t args_count = 0;
247 while (mr_conv->HasNext()) {
248 args_count++;
249 mr_conv->Next();
250 }
251
252 // Do a backward pass over arguments, so that the generated code will be "mov
253 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
254 // TODO: A reverse iterator to improve readability.
255 for (uint32_t i = 0; i < args_count; ++i) {
256 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
257 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
258 main_jni_conv->Next(); // Skip JNIEnv*.
259 if (is_static) {
260 main_jni_conv->Next(); // Skip Class for now.
261 }
262 // Skip to the argument we're interested in.
263 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
264 mr_conv->Next();
265 main_jni_conv->Next();
266 }
267 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
268 }
269 if (is_static) {
270 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100271 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
273 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700274 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700275 if (main_jni_conv->IsCurrentParamOnStack()) {
276 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700277 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278 mr_conv->InterproceduralScratchRegister(),
279 false);
280 } else {
281 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700283 ManagedRegister::NoRegister(), false);
284 }
285 }
286
287 // 8. Create 1st argument, the JNI environment ptr.
288 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
289 // Register that will hold local indirect reference table
290 if (main_jni_conv->IsCurrentParamInRegister()) {
291 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
292 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700293 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100294 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
295 } else {
296 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
297 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 } else {
299 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700300 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100301 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700302 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100303 } else {
304 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
305 main_jni_conv->InterproceduralScratchRegister());
306 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 }
308
309 // 9. Plant call to native code associated with method.
Mathieu Chartier2d721012014-11-10 11:08:06 -0800310 MemberOffset jni_entrypoint_offset = mirror::ArtMethod::EntryPointFromJniOffset(
311 InstructionSetPointerSize(instruction_set));
312 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 mr_conv->InterproceduralScratchRegister());
314
315 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700316 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
318 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
319 __ SignExtend(main_jni_conv->ReturnRegister(),
320 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
321 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
322 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
323 __ ZeroExtend(main_jni_conv->ReturnRegister(),
324 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
325 }
326 }
327
328 // 11. Save return value
329 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
330 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
331 if (instruction_set == kMips && main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
332 return_save_location.Uint32Value() % 8 != 0) {
333 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700334 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100336 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
338 }
339
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100340 // Increase frame size for out args if needed by the end_jni_conv.
341 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
342 if (end_out_arg_size > current_out_arg_size) {
343 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
344 current_out_arg_size = end_out_arg_size;
345 __ IncreaseFrameSize(out_arg_size_diff);
346 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
347 locked_object_handle_scope_offset =
348 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
349 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
350 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 // thread.
352 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100353 ThreadOffset<4> jni_end32(-1);
354 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 if (reference_return) {
356 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100357 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
358 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
359 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
360 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
362 end_jni_conv->Next();
363 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100364 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
365 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
366 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
367 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 }
369 // Pass saved local reference state.
370 if (end_jni_conv->IsCurrentParamOnStack()) {
371 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
372 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
373 } else {
374 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
375 __ Load(out_reg, saved_cookie_offset, 4);
376 }
377 end_jni_conv->Next();
378 if (is_synchronized) {
379 // Pass object for unlocking.
380 if (end_jni_conv->IsCurrentParamOnStack()) {
381 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700382 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700383 end_jni_conv->InterproceduralScratchRegister(),
384 false);
385 } else {
386 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700387 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700388 ManagedRegister::NoRegister(), false);
389 }
390 end_jni_conv->Next();
391 }
392 if (end_jni_conv->IsCurrentParamInRegister()) {
393 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700394 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100395 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
396 end_jni_conv->InterproceduralScratchRegister());
397 } else {
398 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
399 end_jni_conv->InterproceduralScratchRegister());
400 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 } else {
402 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
403 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700404 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100405 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
406 } else {
407 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
408 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700409 }
410
411 // 13. Reload return value
412 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
413 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
414 }
415
416 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100417 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700418
419 // 15. Process pending exceptions from JNI call or monitor exit.
420 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
421
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700422 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700423 // them.
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700424 __ RemoveFrame(frame_size, callee_save_regs);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425
426 // 17. Finalize code generation
427 __ EmitSlowPaths();
428 size_t cs = __ CodeSize();
429 std::vector<uint8_t> managed_code(cs);
430 MemoryRegion code(&managed_code[0], managed_code.size());
431 __ FinalizeInstructions(code);
Tong Shen547cdfd2014-08-05 01:54:19 -0700432 jni_asm->FinalizeFrameDescriptionEntry();
Ian Rogers72d32622014-05-06 16:20:11 -0700433 return new CompiledMethod(driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700434 instruction_set,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 managed_code,
436 frame_size,
437 main_jni_conv->CoreSpillMask(),
Tong Shen547cdfd2014-08-05 01:54:19 -0700438 main_jni_conv->FpSpillMask(),
439 jni_asm->GetFrameDescriptionEntry());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700440}
441
442// Copy a single parameter from the managed to the JNI calling convention
443static void CopyParameter(Assembler* jni_asm,
444 ManagedRuntimeCallingConvention* mr_conv,
445 JniCallingConvention* jni_conv,
446 size_t frame_size, size_t out_arg_size) {
447 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
448 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700449 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 bool null_allowed = false;
451 bool ref_param = jni_conv->IsCurrentParamAReference();
452 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
453 // input may be in register, on stack or both - but not none!
454 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
455 if (output_in_reg) { // output shouldn't straddle registers and stack
456 CHECK(!jni_conv->IsCurrentParamOnStack());
457 } else {
458 CHECK(jni_conv->IsCurrentParamOnStack());
459 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700460 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700461 if (ref_param) {
462 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700463 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
464 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700465 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700466 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
467 // Check handle scope offset is within frame.
468 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 }
470 if (input_in_reg && output_in_reg) {
471 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
472 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
473 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700474 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475 } else {
476 if (!mr_conv->IsCurrentParamOnStack()) {
477 // regular non-straddling move
478 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
479 } else {
480 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
481 }
482 }
483 } else if (!input_in_reg && !output_in_reg) {
484 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
485 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700486 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 null_allowed);
488 } else {
489 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
490 size_t param_size = mr_conv->CurrentParamSize();
491 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
492 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
493 }
494 } else if (!input_in_reg && output_in_reg) {
495 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
496 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
497 // Check that incoming stack arguments are above the current stack frame.
498 CHECK_GT(in_off.Uint32Value(), frame_size);
499 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700500 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 } else {
502 size_t param_size = mr_conv->CurrentParamSize();
503 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
504 __ Load(out_reg, in_off, param_size);
505 }
506 } else {
507 CHECK(input_in_reg && !output_in_reg);
508 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
509 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
510 // Check outgoing argument is within frame
511 CHECK_LT(out_off.Uint32Value(), frame_size);
512 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700513 // TODO: recycle value in in_reg rather than reload from handle scope
514 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 null_allowed);
516 } else {
517 size_t param_size = mr_conv->CurrentParamSize();
518 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
519 if (!mr_conv->IsCurrentParamOnStack()) {
520 // regular non-straddling store
521 __ Store(out_off, in_reg, param_size);
522 } else {
523 // store where input straddles registers and stack
524 CHECK_EQ(param_size, 8u);
525 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
526 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
527 }
528 }
529 }
530}
531
532static void SetNativeParameter(Assembler* jni_asm,
533 JniCallingConvention* jni_conv,
534 ManagedRegister in_reg) {
535 if (jni_conv->IsCurrentParamOnStack()) {
536 FrameOffset dest = jni_conv->CurrentParamStackOffset();
537 __ StoreRawPtr(dest, in_reg);
538 } else {
539 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
540 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
541 }
542 }
543}
544
Andreas Gampe53c913b2014-08-12 23:19:23 -0700545CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
546 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
548}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700549
550} // namespace art