blob: f99f6a879297de5de31c81339099d5093fbde929 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "jni_compiler.h"
18
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <algorithm>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070022#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010025#include "base/arena_allocator.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070026#include "base/enums.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070027#include "base/logging.h"
28#include "base/macros.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070029#include "memory_region.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "calling_convention.h"
31#include "class_linker.h"
32#include "compiled_method.h"
33#include "dex_file-inl.h"
34#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010035#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070036#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070037#include "jni_env_ext.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070038#include "debug/dwarf/debug_frame_opcode_writer.h"
Ian Rogers166db042013-07-26 12:05:57 -070039#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070040#include "utils/jni_macro_assembler.h"
Ian Rogers166db042013-07-26 12:05:57 -070041#include "utils/managed_register.h"
42#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010043#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070044#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010045#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070046#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070047#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070048
49#define __ jni_asm->
50
51namespace art {
52
Andreas Gampe3b165bc2016-08-01 22:07:04 -070053template <PointerSize kPointerSize>
54static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 ManagedRuntimeCallingConvention* mr_conv,
56 JniCallingConvention* jni_conv,
57 size_t frame_size, size_t out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070058template <PointerSize kPointerSize>
59static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 JniCallingConvention* jni_conv,
61 ManagedRegister in_reg);
62
Andreas Gampe3b165bc2016-08-01 22:07:04 -070063template <PointerSize kPointerSize>
64static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
65 ArenaAllocator* arena, InstructionSet isa, const InstructionSetFeatures* features) {
66 return JNIMacroAssembler<kPointerSize>::Create(arena, isa, features);
67}
68
Brian Carlstrom7940e442013-07-12 13:46:57 -070069// Generate the JNI bridge for the given method, general contract:
70// - Arguments are in the managed runtime format, either on stack or in
71// registers, a reference to the method object is supplied as part of this
72// convention.
73//
Andreas Gampe3b165bc2016-08-01 22:07:04 -070074template <PointerSize kPointerSize>
75static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
76 uint32_t access_flags,
77 uint32_t method_idx,
78 const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 const bool is_native = (access_flags & kAccNative) != 0;
80 CHECK(is_native);
81 const bool is_static = (access_flags & kAccStatic) != 0;
82 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
83 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070084 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020085 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Vladimir Marko93205e32016-04-13 11:59:46 +010086
87 ArenaPool pool;
88 ArenaAllocator arena(&pool);
89
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<JniCallingConvention> main_jni_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010092 JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 bool reference_return = main_jni_conv->IsReturnAReference();
94
Ian Rogers700a4022014-05-19 16:49:03 -070095 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +010096 ManagedRuntimeCallingConvention::Create(
97 &arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070098
99 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
100 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100101 const char* jni_end_shorty;
102 if (reference_return && is_synchronized) {
103 jni_end_shorty = "ILL";
104 } else if (reference_return) {
105 jni_end_shorty = "IL";
106 } else if (is_synchronized) {
107 jni_end_shorty = "VL";
108 } else {
109 jni_end_shorty = "V";
110 }
111
Vladimir Marko93205e32016-04-13 11:59:46 +0100112 std::unique_ptr<JniCallingConvention> end_jni_conv(JniCallingConvention::Create(
113 &arena, is_static, is_synchronized, jni_end_shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 // Assembler that holds generated instructions
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700116 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
117 GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000118 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GenerateAnyDebugInfo());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119
120 // Offsets into data structures
121 // TODO: if cross compiling these offsets are for the host not the target
122 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
123 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
124 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
125
126 // 1. Build the frame saving all callee saves
127 const size_t frame_size(main_jni_conv->FrameSize());
Vladimir Marko32248382016-05-19 10:37:24 +0100128 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100130 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 mr_conv->ResetIterator(FrameOffset(frame_size));
134 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700135 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 main_jni_conv->ReferenceCount(),
137 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100138
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700139 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
140 Thread::TopHandleScopeOffset<kPointerSize>(),
141 mr_conv->InterproceduralScratchRegister());
142 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
143 main_jni_conv->HandleScopeOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 mr_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700146 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 main_jni_conv->Next(); // Skip JNIEnv*
148 // 3.5. Create Class argument for static methods out of passed method
149 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
151 // Check handle scope offset is within frame
152 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Roland Levillain4d027112015-07-01 15:41:14 +0100153 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700154 // Note this LoadRef() does not include read barrier. It will be handled below.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700156 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700158 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
159 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 }
161 while (mr_conv->HasNext()) {
162 CHECK(main_jni_conv->HasNext());
163 bool ref_param = main_jni_conv->IsCurrentParamAReference();
164 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700168 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700170 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700171 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
172 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
174 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
175 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
176 CHECK(input_in_reg || input_on_stack);
177
178 if (input_in_reg) {
179 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
180 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700181 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 } else if (input_on_stack) {
183 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
184 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 mr_conv->InterproceduralScratchRegister());
187 }
188 }
189 mr_conv->Next();
190 main_jni_conv->Next();
191 }
192
193 // 4. Write out the end of the quick frames.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700194 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195
196 // 5. Move frame down to allow space for out going args.
197 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100198 size_t current_out_arg_size = main_out_arg_size;
199 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700201 // Call the read barrier for the declaring class loaded from the method for a static call.
202 // Note that we always have outgoing param space available for at least two params.
203 if (kUseReadBarrier && is_static) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700204 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
205 pReadBarrierJni);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700206 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
207 main_jni_conv->Next(); // Skip JNIEnv.
208 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
209 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
210 // Pass the handle for the class as the first argument.
211 if (main_jni_conv->IsCurrentParamOnStack()) {
212 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
213 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
214 mr_conv->InterproceduralScratchRegister(),
215 false);
216 } else {
217 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
218 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
219 ManagedRegister::NoRegister(), false);
220 }
221 main_jni_conv->Next();
222 // Pass the current thread as the second argument and call.
223 if (main_jni_conv->IsCurrentParamInRegister()) {
224 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700225 __ Call(main_jni_conv->CurrentParamRegister(),
226 Offset(read_barrier),
227 main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700228 } else {
229 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
230 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700231 __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700232 }
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.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700240 ThreadOffset<kPointerSize> jni_start =
Andreas Gampe542451c2016-07-26 09:02:02 -0700241 is_synchronized
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700242 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
243 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 mr_conv->InterproceduralScratchRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 } else {
256 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700257 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 ManagedRegister::NoRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 }
260 main_jni_conv->Next();
261 }
262 if (main_jni_conv->IsCurrentParamInRegister()) {
263 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700264 __ Call(main_jni_conv->CurrentParamRegister(),
265 Offset(jni_start),
266 main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 } else {
268 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
269 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700270 __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 }
272 if (is_synchronized) { // Check for exceptions from monitor enter.
273 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
274 }
275 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
276 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
277
278 // 7. Iterate over arguments placing values from managed calling convention in
279 // to the convention required for a native call (shuffling). For references
280 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700281 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700282 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700283 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100284 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 uint32_t args_count = 0;
286 while (mr_conv->HasNext()) {
287 args_count++;
288 mr_conv->Next();
289 }
290
291 // Do a backward pass over arguments, so that the generated code will be "mov
292 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
293 // TODO: A reverse iterator to improve readability.
294 for (uint32_t i = 0; i < args_count; ++i) {
295 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
296 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
297 main_jni_conv->Next(); // Skip JNIEnv*.
298 if (is_static) {
299 main_jni_conv->Next(); // Skip Class for now.
300 }
301 // Skip to the argument we're interested in.
302 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
303 mr_conv->Next();
304 main_jni_conv->Next();
305 }
306 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
307 }
308 if (is_static) {
309 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100310 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
312 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700313 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 if (main_jni_conv->IsCurrentParamOnStack()) {
315 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700316 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317 mr_conv->InterproceduralScratchRegister(),
318 false);
319 } else {
320 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700321 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 ManagedRegister::NoRegister(), false);
323 }
324 }
325
326 // 8. Create 1st argument, the JNI environment ptr.
327 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
328 // Register that will hold local indirect reference table
329 if (main_jni_conv->IsCurrentParamInRegister()) {
330 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
331 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700332 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700333 } else {
334 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700335 __ CopyRawPtrFromThread(jni_env,
336 Thread::JniEnvOffset<kPointerSize>(),
337 main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 }
339
340 // 9. Plant call to native code associated with method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset(
Mathieu Chartier2d721012014-11-10 11:08:06 -0800342 InstructionSetPointerSize(instruction_set));
343 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 mr_conv->InterproceduralScratchRegister());
345
346 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700347 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
349 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
350 __ SignExtend(main_jni_conv->ReturnRegister(),
351 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
352 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
353 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
354 __ ZeroExtend(main_jni_conv->ReturnRegister(),
355 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
356 }
357 }
358
359 // 11. Save return value
360 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
361 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100362 if ((instruction_set == kMips || instruction_set == kMips64) &&
363 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 return_save_location.Uint32Value() % 8 != 0) {
365 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampe542451c2016-07-26 09:02:02 -0700366 return_save_location = FrameOffset(return_save_location.Uint32Value()
367 + static_cast<size_t>(kMipsPointerSize));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100369 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700370 __ Store(return_save_location,
371 main_jni_conv->ReturnRegister(),
372 main_jni_conv->SizeOfReturnValue());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700373 }
374
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100375 // Increase frame size for out args if needed by the end_jni_conv.
376 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
377 if (end_out_arg_size > current_out_arg_size) {
378 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
379 current_out_arg_size = end_out_arg_size;
380 __ IncreaseFrameSize(out_arg_size_diff);
381 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
382 locked_object_handle_scope_offset =
383 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
384 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 // thread.
387 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700388 ThreadOffset<kPointerSize> jni_end(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 if (reference_return) {
390 // Pass result.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700391 jni_end = is_synchronized
392 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
393 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
395 end_jni_conv->Next();
396 } else {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700397 jni_end = is_synchronized
398 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
399 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 }
401 // Pass saved local reference state.
402 if (end_jni_conv->IsCurrentParamOnStack()) {
403 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
404 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
405 } else {
406 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
407 __ Load(out_reg, saved_cookie_offset, 4);
408 }
409 end_jni_conv->Next();
410 if (is_synchronized) {
411 // Pass object for unlocking.
412 if (end_jni_conv->IsCurrentParamOnStack()) {
413 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700414 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700415 end_jni_conv->InterproceduralScratchRegister(),
416 false);
417 } else {
418 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700419 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 ManagedRegister::NoRegister(), false);
421 }
422 end_jni_conv->Next();
423 }
424 if (end_jni_conv->IsCurrentParamInRegister()) {
425 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700426 __ Call(end_jni_conv->CurrentParamRegister(),
427 Offset(jni_end),
428 end_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700429 } else {
430 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
431 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700432 __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 }
434
435 // 13. Reload return value
436 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
437 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
438 }
439
440 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100441 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442
443 // 15. Process pending exceptions from JNI call or monitor exit.
444 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
445
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700446 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700447 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100448 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700449 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100450 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700451
452 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000453 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 size_t cs = __ CodeSize();
455 std::vector<uint8_t> managed_code(cs);
456 MemoryRegion code(&managed_code[0], managed_code.size());
457 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100458
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100459 return CompiledMethod::SwapAllocCompiledMethod(driver,
460 instruction_set,
461 ArrayRef<const uint8_t>(managed_code),
462 frame_size,
463 main_jni_conv->CoreSpillMask(),
464 main_jni_conv->FpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100465 ArrayRef<const SrcMapElem>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100466 ArrayRef<const uint8_t>(), // vmap_table.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100467 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
468 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469}
470
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700471// Copy a single parameter from the managed to the JNI calling convention.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700472template <PointerSize kPointerSize>
473static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 ManagedRuntimeCallingConvention* mr_conv,
475 JniCallingConvention* jni_conv,
476 size_t frame_size, size_t out_arg_size) {
477 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
478 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700479 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 bool null_allowed = false;
481 bool ref_param = jni_conv->IsCurrentParamAReference();
482 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
483 // input may be in register, on stack or both - but not none!
484 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
485 if (output_in_reg) { // output shouldn't straddle registers and stack
486 CHECK(!jni_conv->IsCurrentParamOnStack());
487 } else {
488 CHECK(jni_conv->IsCurrentParamOnStack());
489 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700490 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700491 if (ref_param) {
492 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700493 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
494 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700495 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700496 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
497 // Check handle scope offset is within frame.
498 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700499 }
500 if (input_in_reg && output_in_reg) {
501 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
502 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
503 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700504 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700505 } else {
506 if (!mr_conv->IsCurrentParamOnStack()) {
507 // regular non-straddling move
508 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
509 } else {
510 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
511 }
512 }
513 } else if (!input_in_reg && !output_in_reg) {
514 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
515 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700516 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 null_allowed);
518 } else {
519 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
520 size_t param_size = mr_conv->CurrentParamSize();
521 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
522 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
523 }
524 } else if (!input_in_reg && output_in_reg) {
525 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
526 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
527 // Check that incoming stack arguments are above the current stack frame.
528 CHECK_GT(in_off.Uint32Value(), frame_size);
529 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700530 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700531 } else {
532 size_t param_size = mr_conv->CurrentParamSize();
533 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
534 __ Load(out_reg, in_off, param_size);
535 }
536 } else {
537 CHECK(input_in_reg && !output_in_reg);
538 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
539 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
540 // Check outgoing argument is within frame
541 CHECK_LT(out_off.Uint32Value(), frame_size);
542 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700543 // TODO: recycle value in in_reg rather than reload from handle scope
544 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 null_allowed);
546 } else {
547 size_t param_size = mr_conv->CurrentParamSize();
548 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
549 if (!mr_conv->IsCurrentParamOnStack()) {
550 // regular non-straddling store
551 __ Store(out_off, in_reg, param_size);
552 } else {
553 // store where input straddles registers and stack
554 CHECK_EQ(param_size, 8u);
555 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
556 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
557 }
558 }
559 }
560}
561
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700562template <PointerSize kPointerSize>
563static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 JniCallingConvention* jni_conv,
565 ManagedRegister in_reg) {
566 if (jni_conv->IsCurrentParamOnStack()) {
567 FrameOffset dest = jni_conv->CurrentParamStackOffset();
568 __ StoreRawPtr(dest, in_reg);
569 } else {
570 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
571 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
572 }
573 }
574}
575
Andreas Gampe53c913b2014-08-12 23:19:23 -0700576CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
577 uint32_t method_idx, const DexFile& dex_file) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700578 if (Is64BitInstructionSet(compiler->GetInstructionSet())) {
579 return ArtJniCompileMethodInternal<PointerSize::k64>(
580 compiler, access_flags, method_idx, dex_file);
581 } else {
582 return ArtJniCompileMethodInternal<PointerSize::k32>(
583 compiler, access_flags, method_idx, dex_file);
584 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700585}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700586
587} // namespace art