blob: a06303d23e188b8faf2e9b380569087073ae3a16 [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 Yamauchi1cc71eb2015-05-07 10:47:27 -0700141 // Note this LoadRef() already includes the heap poisoning negation.
142 // Note this LoadRef() does not include read barrier. It will be handled below.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700144 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
147 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 }
149 while (mr_conv->HasNext()) {
150 CHECK(main_jni_conv->HasNext());
151 bool ref_param = main_jni_conv->IsCurrentParamAReference();
152 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700155 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700156 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700157 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700158 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700159 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
160 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
162 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
163 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
164 CHECK(input_in_reg || input_on_stack);
165
166 if (input_in_reg) {
167 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
168 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 } else if (input_on_stack) {
171 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
172 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700173 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 mr_conv->InterproceduralScratchRegister());
175 }
176 }
177 mr_conv->Next();
178 main_jni_conv->Next();
179 }
180
181 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700182 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100183 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100184 } else {
185 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100186 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
188 // 5. Move frame down to allow space for out going args.
189 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100190 size_t current_out_arg_size = main_out_arg_size;
191 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700193 // Call the read barrier for the declaring class loaded from the method for a static call.
194 // Note that we always have outgoing param space available for at least two params.
195 if (kUseReadBarrier && is_static) {
196 ThreadOffset<4> read_barrier32 = QUICK_ENTRYPOINT_OFFSET(4, pReadBarrierJni);
197 ThreadOffset<8> read_barrier64 = QUICK_ENTRYPOINT_OFFSET(8, pReadBarrierJni);
198 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
199 main_jni_conv->Next(); // Skip JNIEnv.
200 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
201 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
202 // Pass the handle for the class as the first argument.
203 if (main_jni_conv->IsCurrentParamOnStack()) {
204 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
205 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
206 mr_conv->InterproceduralScratchRegister(),
207 false);
208 } else {
209 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
210 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
211 ManagedRegister::NoRegister(), false);
212 }
213 main_jni_conv->Next();
214 // Pass the current thread as the second argument and call.
215 if (main_jni_conv->IsCurrentParamInRegister()) {
216 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
217 if (is_64_bit_target) {
218 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier64),
219 main_jni_conv->InterproceduralScratchRegister());
220 } else {
221 __ Call(main_jni_conv->CurrentParamRegister(), Offset(read_barrier32),
222 main_jni_conv->InterproceduralScratchRegister());
223 }
224 } else {
225 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
226 main_jni_conv->InterproceduralScratchRegister());
227 if (is_64_bit_target) {
228 __ CallFromThread64(read_barrier64, main_jni_conv->InterproceduralScratchRegister());
229 } else {
230 __ CallFromThread32(read_barrier32, main_jni_conv->InterproceduralScratchRegister());
231 }
232 }
233 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
234 }
235
Brian Carlstrom7940e442013-07-12 13:46:57 -0700236 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
237 // can occur. The result is the saved JNI local state that is restored by the exit call. We
238 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
239 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100240 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
241 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
242 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
243 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700245 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 if (is_synchronized) {
247 // Pass object for locking.
248 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700249 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
251 if (main_jni_conv->IsCurrentParamOnStack()) {
252 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700253 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 mr_conv->InterproceduralScratchRegister(),
255 false);
256 } else {
257 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700258 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 ManagedRegister::NoRegister(), false);
260 }
261 main_jni_conv->Next();
262 }
263 if (main_jni_conv->IsCurrentParamInRegister()) {
264 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700265 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100266 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
267 main_jni_conv->InterproceduralScratchRegister());
268 } else {
269 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
270 main_jni_conv->InterproceduralScratchRegister());
271 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272 } else {
273 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
274 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700275 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100276 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
277 } else {
278 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
279 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
281 if (is_synchronized) { // Check for exceptions from monitor enter.
282 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
283 }
284 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
285 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
286
287 // 7. Iterate over arguments placing values from managed calling convention in
288 // to the convention required for a native call (shuffling). For references
289 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700290 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100293 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 uint32_t args_count = 0;
295 while (mr_conv->HasNext()) {
296 args_count++;
297 mr_conv->Next();
298 }
299
300 // Do a backward pass over arguments, so that the generated code will be "mov
301 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
302 // TODO: A reverse iterator to improve readability.
303 for (uint32_t i = 0; i < args_count; ++i) {
304 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
305 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
306 main_jni_conv->Next(); // Skip JNIEnv*.
307 if (is_static) {
308 main_jni_conv->Next(); // Skip Class for now.
309 }
310 // Skip to the argument we're interested in.
311 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
312 mr_conv->Next();
313 main_jni_conv->Next();
314 }
315 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
316 }
317 if (is_static) {
318 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100319 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
321 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700322 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 if (main_jni_conv->IsCurrentParamOnStack()) {
324 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700325 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 mr_conv->InterproceduralScratchRegister(),
327 false);
328 } else {
329 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700330 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 ManagedRegister::NoRegister(), false);
332 }
333 }
334
335 // 8. Create 1st argument, the JNI environment ptr.
336 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
337 // Register that will hold local indirect reference table
338 if (main_jni_conv->IsCurrentParamInRegister()) {
339 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
340 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700341 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100342 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
343 } else {
344 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
345 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 } else {
347 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700348 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100349 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100351 } else {
352 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
353 main_jni_conv->InterproceduralScratchRegister());
354 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 }
356
357 // 9. Plant call to native code associated with method.
Mathieu Chartier2d721012014-11-10 11:08:06 -0800358 MemberOffset jni_entrypoint_offset = mirror::ArtMethod::EntryPointFromJniOffset(
359 InstructionSetPointerSize(instruction_set));
360 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 mr_conv->InterproceduralScratchRegister());
362
363 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700364 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
366 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
367 __ SignExtend(main_jni_conv->ReturnRegister(),
368 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
369 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
370 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
371 __ ZeroExtend(main_jni_conv->ReturnRegister(),
372 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
373 }
374 }
375
376 // 11. Save return value
377 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
378 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100379 if ((instruction_set == kMips || instruction_set == kMips64) &&
380 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 return_save_location.Uint32Value() % 8 != 0) {
382 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700383 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100385 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
387 }
388
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100389 // Increase frame size for out args if needed by the end_jni_conv.
390 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
391 if (end_out_arg_size > current_out_arg_size) {
392 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
393 current_out_arg_size = end_out_arg_size;
394 __ IncreaseFrameSize(out_arg_size_diff);
395 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
396 locked_object_handle_scope_offset =
397 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
398 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
399 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 // thread.
401 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100402 ThreadOffset<4> jni_end32(-1);
403 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 if (reference_return) {
405 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100406 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
407 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
408 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
409 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
411 end_jni_conv->Next();
412 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100413 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
414 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
415 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
416 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700417 }
418 // Pass saved local reference state.
419 if (end_jni_conv->IsCurrentParamOnStack()) {
420 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
421 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
422 } else {
423 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
424 __ Load(out_reg, saved_cookie_offset, 4);
425 }
426 end_jni_conv->Next();
427 if (is_synchronized) {
428 // Pass object for unlocking.
429 if (end_jni_conv->IsCurrentParamOnStack()) {
430 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700431 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 end_jni_conv->InterproceduralScratchRegister(),
433 false);
434 } else {
435 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700436 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700437 ManagedRegister::NoRegister(), false);
438 }
439 end_jni_conv->Next();
440 }
441 if (end_jni_conv->IsCurrentParamInRegister()) {
442 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700443 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100444 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
445 end_jni_conv->InterproceduralScratchRegister());
446 } else {
447 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
448 end_jni_conv->InterproceduralScratchRegister());
449 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700450 } else {
451 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
452 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700453 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100454 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
455 } else {
456 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
457 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700458 }
459
460 // 13. Reload return value
461 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
462 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
463 }
464
465 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100466 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467
468 // 15. Process pending exceptions from JNI call or monitor exit.
469 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
470
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700471 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100473 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700474 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100475 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476
477 // 17. Finalize code generation
478 __ EmitSlowPaths();
479 size_t cs = __ CodeSize();
480 std::vector<uint8_t> managed_code(cs);
481 MemoryRegion code(&managed_code[0], managed_code.size());
482 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100483
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100484 return CompiledMethod::SwapAllocCompiledMethod(driver,
485 instruction_set,
486 ArrayRef<const uint8_t>(managed_code),
487 frame_size,
488 main_jni_conv->CoreSpillMask(),
489 main_jni_conv->FpSpillMask(),
490 nullptr, // src_mapping_table.
491 ArrayRef<const uint8_t>(), // mapping_table.
492 ArrayRef<const uint8_t>(), // vmap_table.
493 ArrayRef<const uint8_t>(), // native_gc_map.
494 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
495 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700496}
497
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498// Copy a single parameter from the managed to the JNI calling convention.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499static void CopyParameter(Assembler* jni_asm,
500 ManagedRuntimeCallingConvention* mr_conv,
501 JniCallingConvention* jni_conv,
502 size_t frame_size, size_t out_arg_size) {
503 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
504 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700505 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 bool null_allowed = false;
507 bool ref_param = jni_conv->IsCurrentParamAReference();
508 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
509 // input may be in register, on stack or both - but not none!
510 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
511 if (output_in_reg) { // output shouldn't straddle registers and stack
512 CHECK(!jni_conv->IsCurrentParamOnStack());
513 } else {
514 CHECK(jni_conv->IsCurrentParamOnStack());
515 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700516 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 if (ref_param) {
518 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700519 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
520 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700521 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700522 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
523 // Check handle scope offset is within frame.
524 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 }
526 if (input_in_reg && output_in_reg) {
527 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
528 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
529 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700530 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 } else {
532 if (!mr_conv->IsCurrentParamOnStack()) {
533 // regular non-straddling move
534 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
535 } else {
536 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
537 }
538 }
539 } else if (!input_in_reg && !output_in_reg) {
540 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
541 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700542 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700543 null_allowed);
544 } else {
545 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
546 size_t param_size = mr_conv->CurrentParamSize();
547 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
548 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
549 }
550 } else if (!input_in_reg && output_in_reg) {
551 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
552 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
553 // Check that incoming stack arguments are above the current stack frame.
554 CHECK_GT(in_off.Uint32Value(), frame_size);
555 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700556 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700557 } else {
558 size_t param_size = mr_conv->CurrentParamSize();
559 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
560 __ Load(out_reg, in_off, param_size);
561 }
562 } else {
563 CHECK(input_in_reg && !output_in_reg);
564 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
565 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
566 // Check outgoing argument is within frame
567 CHECK_LT(out_off.Uint32Value(), frame_size);
568 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700569 // TODO: recycle value in in_reg rather than reload from handle scope
570 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700571 null_allowed);
572 } else {
573 size_t param_size = mr_conv->CurrentParamSize();
574 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
575 if (!mr_conv->IsCurrentParamOnStack()) {
576 // regular non-straddling store
577 __ Store(out_off, in_reg, param_size);
578 } else {
579 // store where input straddles registers and stack
580 CHECK_EQ(param_size, 8u);
581 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
582 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
583 }
584 }
585 }
586}
587
588static void SetNativeParameter(Assembler* jni_asm,
589 JniCallingConvention* jni_conv,
590 ManagedRegister in_reg) {
591 if (jni_conv->IsCurrentParamOnStack()) {
592 FrameOffset dest = jni_conv->CurrentParamStackOffset();
593 __ StoreRawPtr(dest, in_reg);
594 } else {
595 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
596 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
597 }
598 }
599}
600
Andreas Gampe53c913b2014-08-12 23:19:23 -0700601CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
602 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
604}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700605
606} // namespace art