blob: d092c3f1f69ba6a7bb2d1bfc07530f4a09241d47 [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>
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070020#include <ios>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070023#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010026#include "base/arena_allocator.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "base/logging.h"
29#include "base/macros.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070030#include "memory_region.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "calling_convention.h"
32#include "class_linker.h"
33#include "compiled_method.h"
34#include "dex_file-inl.h"
35#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010036#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070037#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070038#include "jni_env_ext.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070039#include "debug/dwarf/debug_frame_opcode_writer.h"
Ian Rogers166db042013-07-26 12:05:57 -070040#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070041#include "utils/jni_macro_assembler.h"
Ian Rogers166db042013-07-26 12:05:57 -070042#include "utils/managed_register.h"
43#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010044#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070045#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010046#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070047#include "utils/x86/managed_register_x86.h"
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070048#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070049#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
51#define __ jni_asm->
52
53namespace art {
54
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070055using JniOptimizationFlags = Compiler::JniOptimizationFlags;
56
Andreas Gampe3b165bc2016-08-01 22:07:04 -070057template <PointerSize kPointerSize>
58static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 ManagedRuntimeCallingConvention* mr_conv,
60 JniCallingConvention* jni_conv,
61 size_t frame_size, size_t out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070062template <PointerSize kPointerSize>
63static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 JniCallingConvention* jni_conv,
65 ManagedRegister in_reg);
66
Andreas Gampe3b165bc2016-08-01 22:07:04 -070067template <PointerSize kPointerSize>
68static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
69 ArenaAllocator* arena, InstructionSet isa, const InstructionSetFeatures* features) {
70 return JNIMacroAssembler<kPointerSize>::Create(arena, isa, features);
71}
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073// Generate the JNI bridge for the given method, general contract:
74// - Arguments are in the managed runtime format, either on stack or in
75// registers, a reference to the method object is supplied as part of this
76// convention.
77//
Andreas Gampe3b165bc2016-08-01 22:07:04 -070078template <PointerSize kPointerSize>
79static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
80 uint32_t access_flags,
81 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070082 const DexFile& dex_file,
83 JniOptimizationFlags optimization_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 const bool is_native = (access_flags & kAccNative) != 0;
85 CHECK(is_native);
86 const bool is_static = (access_flags & kAccStatic) != 0;
87 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
88 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070089 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020090 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Vladimir Marko93205e32016-04-13 11:59:46 +010091
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070092 // i.e. if the method was annotated with @FastNative
93 const bool is_fast_native =
94 (static_cast<uint32_t>(optimization_flags) & Compiler::kFastNative) != 0;
95
96 VLOG(jni) << "JniCompile: Method :: "
97 << art::PrettyMethod(method_idx, dex_file, /* with signature */ true)
98 << " :: access_flags = " << std::hex << access_flags << std::dec;
99
100 if (UNLIKELY(is_fast_native)) {
101 VLOG(jni) << "JniCompile: Fast native method detected :: "
102 << art::PrettyMethod(method_idx, dex_file, /* with signature */ true);
103 }
104
Vladimir Marko93205e32016-04-13 11:59:46 +0100105 ArenaPool pool;
106 ArenaAllocator arena(&pool);
107
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -0700109 std::unique_ptr<JniCallingConvention> main_jni_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +0100110 JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 bool reference_return = main_jni_conv->IsReturnAReference();
112
Ian Rogers700a4022014-05-19 16:49:03 -0700113 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +0100114 ManagedRuntimeCallingConvention::Create(
115 &arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116
117 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
118 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100119 const char* jni_end_shorty;
120 if (reference_return && is_synchronized) {
121 jni_end_shorty = "ILL";
122 } else if (reference_return) {
123 jni_end_shorty = "IL";
124 } else if (is_synchronized) {
125 jni_end_shorty = "VL";
126 } else {
127 jni_end_shorty = "V";
128 }
129
Vladimir Marko93205e32016-04-13 11:59:46 +0100130 std::unique_ptr<JniCallingConvention> end_jni_conv(JniCallingConvention::Create(
131 &arena, is_static, is_synchronized, jni_end_shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 // Assembler that holds generated instructions
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700134 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
135 GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000136 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GenerateAnyDebugInfo());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137
138 // Offsets into data structures
139 // TODO: if cross compiling these offsets are for the host not the target
140 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
141 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
142 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
143
144 // 1. Build the frame saving all callee saves
145 const size_t frame_size(main_jni_conv->FrameSize());
Vladimir Marko32248382016-05-19 10:37:24 +0100146 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100148 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 mr_conv->ResetIterator(FrameOffset(frame_size));
152 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700153 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 main_jni_conv->ReferenceCount(),
155 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100156
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700157 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
158 Thread::TopHandleScopeOffset<kPointerSize>(),
159 mr_conv->InterproceduralScratchRegister());
160 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
161 main_jni_conv->HandleScopeOffset(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700162 mr_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 main_jni_conv->Next(); // Skip JNIEnv*
166 // 3.5. Create Class argument for static methods out of passed method
167 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700168 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
169 // Check handle scope offset is within frame
170 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Roland Levillain4d027112015-07-01 15:41:14 +0100171 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700172 // Note this LoadRef() does not include read barrier. It will be handled below.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700176 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
177 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 }
179 while (mr_conv->HasNext()) {
180 CHECK(main_jni_conv->HasNext());
181 bool ref_param = main_jni_conv->IsCurrentParamAReference();
182 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700183 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700185 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700186 // must be null.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700187 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700188 // Check handle scope offset is within frame and doesn't run into the saved segment state.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700189 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
190 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
192 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
193 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
194 CHECK(input_in_reg || input_on_stack);
195
196 if (input_in_reg) {
197 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
198 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 } else if (input_on_stack) {
201 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
202 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 mr_conv->InterproceduralScratchRegister());
205 }
206 }
207 mr_conv->Next();
208 main_jni_conv->Next();
209 }
210
211 // 4. Write out the end of the quick frames.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700212 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213
214 // 5. Move frame down to allow space for out going args.
215 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100216 size_t current_out_arg_size = main_out_arg_size;
217 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700219 // Call the read barrier for the declaring class loaded from the method for a static call.
220 // Note that we always have outgoing param space available for at least two params.
221 if (kUseReadBarrier && is_static) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700222 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
223 pReadBarrierJni);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700224 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
225 main_jni_conv->Next(); // Skip JNIEnv.
226 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
227 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
228 // Pass the handle for the class as the first argument.
229 if (main_jni_conv->IsCurrentParamOnStack()) {
230 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
231 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
232 mr_conv->InterproceduralScratchRegister(),
233 false);
234 } else {
235 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
236 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
237 ManagedRegister::NoRegister(), false);
238 }
239 main_jni_conv->Next();
240 // Pass the current thread as the second argument and call.
241 if (main_jni_conv->IsCurrentParamInRegister()) {
242 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700243 __ Call(main_jni_conv->CurrentParamRegister(),
244 Offset(read_barrier),
245 main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700246 } else {
247 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
248 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700249 __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700250 }
251 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
252 }
253
Brian Carlstrom7940e442013-07-12 13:46:57 -0700254 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
255 // can occur. The result is the saved JNI local state that is restored by the exit call. We
256 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
257 // arguments.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700258 ThreadOffset<kPointerSize> jni_start =
Andreas Gampe542451c2016-07-26 09:02:02 -0700259 is_synchronized
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700260 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700261 : (is_fast_native
262 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart)
263 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
264
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700266 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700267 if (is_synchronized) {
268 // Pass object for locking.
269 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700270 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
272 if (main_jni_conv->IsCurrentParamOnStack()) {
273 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700274 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 mr_conv->InterproceduralScratchRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276 } else {
277 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700278 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 ManagedRegister::NoRegister(), false);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 }
281 main_jni_conv->Next();
282 }
283 if (main_jni_conv->IsCurrentParamInRegister()) {
284 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700285 __ Call(main_jni_conv->CurrentParamRegister(),
286 Offset(jni_start),
287 main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288 } else {
289 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
290 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700291 __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700292 }
293 if (is_synchronized) { // Check for exceptions from monitor enter.
294 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
295 }
296 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
297 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
298
299 // 7. Iterate over arguments placing values from managed calling convention in
300 // to the convention required for a native call (shuffling). For references
301 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700302 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700303 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700304 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100305 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 uint32_t args_count = 0;
307 while (mr_conv->HasNext()) {
308 args_count++;
309 mr_conv->Next();
310 }
311
312 // Do a backward pass over arguments, so that the generated code will be "mov
313 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
314 // TODO: A reverse iterator to improve readability.
315 for (uint32_t i = 0; i < args_count; ++i) {
316 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
317 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
318 main_jni_conv->Next(); // Skip JNIEnv*.
319 if (is_static) {
320 main_jni_conv->Next(); // Skip Class for now.
321 }
322 // Skip to the argument we're interested in.
323 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
324 mr_conv->Next();
325 main_jni_conv->Next();
326 }
327 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
328 }
329 if (is_static) {
330 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100331 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
333 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700334 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 if (main_jni_conv->IsCurrentParamOnStack()) {
336 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700337 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 mr_conv->InterproceduralScratchRegister(),
339 false);
340 } else {
341 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700342 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700343 ManagedRegister::NoRegister(), false);
344 }
345 }
346
347 // 8. Create 1st argument, the JNI environment ptr.
348 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
349 // Register that will hold local indirect reference table
350 if (main_jni_conv->IsCurrentParamInRegister()) {
351 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
352 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700353 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 } else {
355 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700356 __ CopyRawPtrFromThread(jni_env,
357 Thread::JniEnvOffset<kPointerSize>(),
358 main_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700359 }
360
361 // 9. Plant call to native code associated with method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700362 MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset(
Mathieu Chartier2d721012014-11-10 11:08:06 -0800363 InstructionSetPointerSize(instruction_set));
364 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365 mr_conv->InterproceduralScratchRegister());
366
367 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700368 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
370 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
371 __ SignExtend(main_jni_conv->ReturnRegister(),
372 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
373 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
374 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
375 __ ZeroExtend(main_jni_conv->ReturnRegister(),
376 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
377 }
378 }
379
380 // 11. Save return value
381 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
382 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100383 if ((instruction_set == kMips || instruction_set == kMips64) &&
384 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700385 return_save_location.Uint32Value() % 8 != 0) {
386 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 return_save_location = FrameOffset(return_save_location.Uint32Value()
388 + static_cast<size_t>(kMipsPointerSize));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100390 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700391 __ Store(return_save_location,
392 main_jni_conv->ReturnRegister(),
393 main_jni_conv->SizeOfReturnValue());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 }
395
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100396 // Increase frame size for out args if needed by the end_jni_conv.
397 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
398 if (end_out_arg_size > current_out_arg_size) {
399 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
400 current_out_arg_size = end_out_arg_size;
401 __ IncreaseFrameSize(out_arg_size_diff);
402 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
403 locked_object_handle_scope_offset =
404 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
405 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
406 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700407 // thread.
408 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700409
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700410 ThreadOffset<kPointerSize> jni_end(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700411 if (reference_return) {
412 // Pass result.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700413 jni_end = is_synchronized
414 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
415 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700416 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
417 end_jni_conv->Next();
418 } else {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700419 jni_end = is_synchronized
420 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700421 : (is_fast_native
422 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd)
423 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
425 // Pass saved local reference state.
426 if (end_jni_conv->IsCurrentParamOnStack()) {
427 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
428 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
429 } else {
430 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
431 __ Load(out_reg, saved_cookie_offset, 4);
432 }
433 end_jni_conv->Next();
434 if (is_synchronized) {
435 // Pass object for unlocking.
436 if (end_jni_conv->IsCurrentParamOnStack()) {
437 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700438 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 end_jni_conv->InterproceduralScratchRegister(),
440 false);
441 } else {
442 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 ManagedRegister::NoRegister(), false);
445 }
446 end_jni_conv->Next();
447 }
448 if (end_jni_conv->IsCurrentParamInRegister()) {
449 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700450 __ Call(end_jni_conv->CurrentParamRegister(),
451 Offset(jni_end),
452 end_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 } else {
454 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
455 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700456 __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 }
458
459 // 13. Reload return value
460 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
461 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
462 }
463
464 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100465 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466
467 // 15. Process pending exceptions from JNI call or monitor exit.
468 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
469
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700470 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100472 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700473 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100474 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700475
476 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000477 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 size_t cs = __ CodeSize();
479 std::vector<uint8_t> managed_code(cs);
480 MemoryRegion code(&managed_code[0], managed_code.size());
481 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100482
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100483 return CompiledMethod::SwapAllocCompiledMethod(driver,
484 instruction_set,
485 ArrayRef<const uint8_t>(managed_code),
486 frame_size,
487 main_jni_conv->CoreSpillMask(),
488 main_jni_conv->FpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100489 ArrayRef<const SrcMapElem>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100490 ArrayRef<const uint8_t>(), // vmap_table.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100491 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
492 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493}
494
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700495// Copy a single parameter from the managed to the JNI calling convention.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700496template <PointerSize kPointerSize>
497static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 ManagedRuntimeCallingConvention* mr_conv,
499 JniCallingConvention* jni_conv,
500 size_t frame_size, size_t out_arg_size) {
501 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
502 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700503 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 bool null_allowed = false;
505 bool ref_param = jni_conv->IsCurrentParamAReference();
506 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
507 // input may be in register, on stack or both - but not none!
508 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
509 if (output_in_reg) { // output shouldn't straddle registers and stack
510 CHECK(!jni_conv->IsCurrentParamOnStack());
511 } else {
512 CHECK(jni_conv->IsCurrentParamOnStack());
513 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700514 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 if (ref_param) {
516 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700517 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
518 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700520 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
521 // Check handle scope offset is within frame.
522 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700523 }
524 if (input_in_reg && output_in_reg) {
525 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
526 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
527 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700528 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 } else {
530 if (!mr_conv->IsCurrentParamOnStack()) {
531 // regular non-straddling move
532 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
533 } else {
534 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
535 }
536 }
537 } else if (!input_in_reg && !output_in_reg) {
538 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
539 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700540 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 null_allowed);
542 } else {
543 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
544 size_t param_size = mr_conv->CurrentParamSize();
545 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
546 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
547 }
548 } else if (!input_in_reg && output_in_reg) {
549 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
550 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
551 // Check that incoming stack arguments are above the current stack frame.
552 CHECK_GT(in_off.Uint32Value(), frame_size);
553 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700554 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700555 } else {
556 size_t param_size = mr_conv->CurrentParamSize();
557 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
558 __ Load(out_reg, in_off, param_size);
559 }
560 } else {
561 CHECK(input_in_reg && !output_in_reg);
562 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
563 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
564 // Check outgoing argument is within frame
565 CHECK_LT(out_off.Uint32Value(), frame_size);
566 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700567 // TODO: recycle value in in_reg rather than reload from handle scope
568 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700569 null_allowed);
570 } else {
571 size_t param_size = mr_conv->CurrentParamSize();
572 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
573 if (!mr_conv->IsCurrentParamOnStack()) {
574 // regular non-straddling store
575 __ Store(out_off, in_reg, param_size);
576 } else {
577 // store where input straddles registers and stack
578 CHECK_EQ(param_size, 8u);
579 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
580 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
581 }
582 }
583 }
584}
585
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700586template <PointerSize kPointerSize>
587static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700588 JniCallingConvention* jni_conv,
589 ManagedRegister in_reg) {
590 if (jni_conv->IsCurrentParamOnStack()) {
591 FrameOffset dest = jni_conv->CurrentParamStackOffset();
592 __ StoreRawPtr(dest, in_reg);
593 } else {
594 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
595 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
596 }
597 }
598}
599
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700600CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler,
601 uint32_t access_flags,
602 uint32_t method_idx,
603 const DexFile& dex_file,
604 Compiler::JniOptimizationFlags optimization_flags) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700605 if (Is64BitInstructionSet(compiler->GetInstructionSet())) {
606 return ArtJniCompileMethodInternal<PointerSize::k64>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700607 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700608 } else {
609 return ArtJniCompileMethodInternal<PointerSize::k32>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700610 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700611 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700613
614} // namespace art