blob: 9ccdff3a17fe1ca043a75ea7aacf10ede3d603e8 [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>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <fstream>
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070021#include <ios>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include <vector>
24
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"
Andreas Gampe57943812017-12-06 21:39:13 -080028#include "base/logging.h" // For VLOG.
Brian Carlstrom7940e442013-07-12 13:46:57 -070029#include "base/macros.h"
David Sehr3215fff2018-04-03 17:10:12 -070030#include "base/malloc_arena_pool.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032#include "calling_convention.h"
33#include "class_linker.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "debug/dwarf/debug_frame_opcode_writer.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070036#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010037#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070038#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070039#include "jni_env_ext.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "memory_region.h"
41#include "thread.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070042#include "utils/arm/managed_register_arm.h"
43#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070044#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045#include "utils/jni_macro_assembler.h"
Ian Rogers166db042013-07-26 12:05:57 -070046#include "utils/managed_register.h"
Ian Rogers166db042013-07-26 12:05:57 -070047#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010048#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070049#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
51#define __ jni_asm->
52
53namespace art {
54
Andreas Gampe3b165bc2016-08-01 22:07:04 -070055template <PointerSize kPointerSize>
56static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 ManagedRuntimeCallingConvention* mr_conv,
58 JniCallingConvention* jni_conv,
59 size_t frame_size, size_t out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070060template <PointerSize kPointerSize>
61static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 JniCallingConvention* jni_conv,
63 ManagedRegister in_reg);
64
Andreas Gampe3b165bc2016-08-01 22:07:04 -070065template <PointerSize kPointerSize>
66static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
Vladimir Markoe764d2e2017-10-05 14:35:55 +010067 ArenaAllocator* allocator, InstructionSet isa, const InstructionSetFeatures* features) {
68 return JNIMacroAssembler<kPointerSize>::Create(allocator, isa, features);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070069}
70
Igor Murashkinaf1e2992016-10-12 17:44:50 -070071enum class JniEntrypoint {
72 kStart,
73 kEnd
74};
75
76template <PointerSize kPointerSize>
77static ThreadOffset<kPointerSize> GetJniEntrypointThreadOffset(JniEntrypoint which,
78 bool reference_return,
79 bool is_synchronized,
80 bool is_fast_native) {
81 if (which == JniEntrypoint::kStart) { // JniMethodStart
82 ThreadOffset<kPointerSize> jni_start =
83 is_synchronized
84 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
85 : (is_fast_native
86 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart)
87 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
88
89 return jni_start;
90 } else { // JniMethodEnd
91 ThreadOffset<kPointerSize> jni_end(-1);
92 if (reference_return) {
93 // Pass result.
94 jni_end = is_synchronized
95 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
96 : (is_fast_native
97 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEndWithReference)
98 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference));
99 } else {
100 jni_end = is_synchronized
101 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
102 : (is_fast_native
103 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd)
104 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
105 }
106
107 return jni_end;
108 }
109}
110
111
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112// Generate the JNI bridge for the given method, general contract:
113// - Arguments are in the managed runtime format, either on stack or in
114// registers, a reference to the method object is supplied as part of this
115// convention.
116//
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700117template <PointerSize kPointerSize>
Vladimir Marko7bdc6e72017-11-28 12:37:13 +0000118static JniCompiledMethod ArtJniCompileMethodInternal(CompilerDriver* driver,
119 uint32_t access_flags,
120 uint32_t method_idx,
121 const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 const bool is_native = (access_flags & kAccNative) != 0;
123 CHECK(is_native);
124 const bool is_static = (access_flags & kAccStatic) != 0;
125 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
126 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -0700127 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200128 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Vladimir Marko93205e32016-04-13 11:59:46 +0100129
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700130 // i.e. if the method was annotated with @FastNative
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100131 const bool is_fast_native = (access_flags & kAccFastNative) != 0u;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700132
133 // i.e. if the method was annotated with @CriticalNative
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100134 bool is_critical_native = (access_flags & kAccCriticalNative) != 0u;
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700135
136 VLOG(jni) << "JniCompile: Method :: "
David Sehr709b0702016-10-13 09:12:37 -0700137 << dex_file.PrettyMethod(method_idx, /* with signature */ true)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700138 << " :: access_flags = " << std::hex << access_flags << std::dec;
139
140 if (UNLIKELY(is_fast_native)) {
141 VLOG(jni) << "JniCompile: Fast native method detected :: "
David Sehr709b0702016-10-13 09:12:37 -0700142 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700143 }
144
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700145 if (UNLIKELY(is_critical_native)) {
146 VLOG(jni) << "JniCompile: Critical native method detected :: "
David Sehr709b0702016-10-13 09:12:37 -0700147 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700148 }
149
150 if (kIsDebugBuild) {
151 // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive.
152 if (UNLIKELY(is_fast_native && is_critical_native)) {
153 LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative"
David Sehr709b0702016-10-13 09:12:37 -0700154 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700155 }
156
157 // @CriticalNative - extra checks:
158 // -- Don't allow virtual criticals
159 // -- Don't allow synchronized criticals
160 // -- Don't allow any objects as parameter or return value
161 if (UNLIKELY(is_critical_native)) {
162 CHECK(is_static)
163 << "@CriticalNative functions cannot be virtual since that would"
164 << "require passing a reference parameter (this), which is illegal "
David Sehr709b0702016-10-13 09:12:37 -0700165 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700166 CHECK(!is_synchronized)
167 << "@CriticalNative functions cannot be synchronized since that would"
168 << "require passing a (class and/or this) reference parameter, which is illegal "
David Sehr709b0702016-10-13 09:12:37 -0700169 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700170 for (size_t i = 0; i < strlen(shorty); ++i) {
171 CHECK_NE(Primitive::kPrimNot, Primitive::GetType(shorty[i]))
172 << "@CriticalNative methods' shorty types must not have illegal references "
David Sehr709b0702016-10-13 09:12:37 -0700173 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700174 }
175 }
176 }
177
David Sehr3215fff2018-04-03 17:10:12 -0700178 MallocArenaPool pool;
Vladimir Marko69d310e2017-10-09 14:12:23 +0100179 ArenaAllocator allocator(&pool);
Vladimir Marko93205e32016-04-13 11:59:46 +0100180
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 // Calling conventions used to iterate over parameters to method
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700182 std::unique_ptr<JniCallingConvention> main_jni_conv =
Vladimir Marko69d310e2017-10-09 14:12:23 +0100183 JniCallingConvention::Create(&allocator,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700184 is_static,
185 is_synchronized,
186 is_critical_native,
187 shorty,
188 instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 bool reference_return = main_jni_conv->IsReturnAReference();
190
Ian Rogers700a4022014-05-19 16:49:03 -0700191 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +0100192 ManagedRuntimeCallingConvention::Create(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100193 &allocator, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194
195 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
196 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100197 const char* jni_end_shorty;
198 if (reference_return && is_synchronized) {
199 jni_end_shorty = "ILL";
200 } else if (reference_return) {
201 jni_end_shorty = "IL";
202 } else if (is_synchronized) {
203 jni_end_shorty = "VL";
204 } else {
205 jni_end_shorty = "V";
206 }
207
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700208 std::unique_ptr<JniCallingConvention> end_jni_conv(
Vladimir Marko69d310e2017-10-09 14:12:23 +0100209 JniCallingConvention::Create(&allocator,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700210 is_static,
211 is_synchronized,
212 is_critical_native,
213 jni_end_shorty,
214 instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700215
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 // Assembler that holds generated instructions
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700217 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
Vladimir Marko69d310e2017-10-09 14:12:23 +0100218 GetMacroAssembler<kPointerSize>(&allocator, instruction_set, instruction_set_features);
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100219 const CompilerOptions& compiler_options = driver->GetCompilerOptions();
220 jni_asm->cfi().SetEnabled(compiler_options.GenerateAnyDebugInfo());
221 jni_asm->SetEmitRunTimeChecksInDebugMode(compiler_options.EmitRunTimeChecksInDebugMode());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222
223 // Offsets into data structures
224 // TODO: if cross compiling these offsets are for the host not the target
225 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
226 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
227 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
228
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700229 // 1. Build the frame saving all callee saves, Method*, and PC return address.
230 const size_t frame_size(main_jni_conv->FrameSize()); // Excludes outgoing args.
Vladimir Marko32248382016-05-19 10:37:24 +0100231 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100233 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700235 if (LIKELY(!is_critical_native)) {
236 // NOTE: @CriticalNative methods don't have a HandleScope
237 // because they can't have any reference parameters or return values.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100238
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700239 // 2. Set up the HandleScope
240 mr_conv->ResetIterator(FrameOffset(frame_size));
241 main_jni_conv->ResetIterator(FrameOffset(0));
242 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
243 main_jni_conv->ReferenceCount(),
244 mr_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700246 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
247 Thread::TopHandleScopeOffset<kPointerSize>(),
248 mr_conv->InterproceduralScratchRegister());
249 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
250 main_jni_conv->HandleScopeOffset(),
251 mr_conv->InterproceduralScratchRegister());
252
253 // 3. Place incoming reference arguments into handle scope
254 main_jni_conv->Next(); // Skip JNIEnv*
255 // 3.5. Create Class argument for static methods out of passed method
256 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700257 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700258 // Check handle scope offset is within frame
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700259 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700260 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
261 // Note this LoadRef() does not include read barrier. It will be handled below.
262 //
263 // scratchRegister = *method[DeclaringClassOffset()];
264 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
265 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
266 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
267 // *handleScopeOffset = scratchRegister
268 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
269 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700270 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700271 // Place every reference into the handle scope (ignore other parameters).
272 while (mr_conv->HasNext()) {
273 CHECK(main_jni_conv->HasNext());
274 bool ref_param = main_jni_conv->IsCurrentParamAReference();
275 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
276 // References need placing in handle scope and the entry value passing
277 if (ref_param) {
278 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
279 // must be null.
280 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
281 // Check handle scope offset is within frame and doesn't run into the saved segment state.
282 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
283 CHECK_NE(handle_scope_offset.Uint32Value(),
284 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
285 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
286 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
287 CHECK(input_in_reg || input_on_stack);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700288
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700289 if (input_in_reg) {
290 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
291 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
292 __ StoreRef(handle_scope_offset, in_reg);
293 } else if (input_on_stack) {
294 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
295 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
296 __ CopyRef(handle_scope_offset, in_off,
297 mr_conv->InterproceduralScratchRegister());
298 }
299 }
300 mr_conv->Next();
301 main_jni_conv->Next();
302 }
303
304 // 4. Write out the end of the quick frames.
305 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
306
307 // NOTE: @CriticalNative does not need to store the stack pointer to the thread
308 // because garbage collections are disabled within the execution of a
309 // @CriticalNative method.
310 // (TODO: We could probably disable it for @FastNative too).
311 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312
313 // 5. Move frame down to allow space for out going args.
314 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100315 size_t current_out_arg_size = main_out_arg_size;
316 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700318 // Call the read barrier for the declaring class loaded from the method for a static call.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700319 // Skip this for @CriticalNative because we didn't build a HandleScope to begin with.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700320 // Note that we always have outgoing param space available for at least two params.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700321 if (kUseReadBarrier && is_static && !is_critical_native) {
Igor Murashkinae7ff922016-10-06 14:59:19 -0700322 const bool kReadBarrierFastPath =
Vladimir Marko33bff252017-11-01 14:35:42 +0000323 (instruction_set != InstructionSet::kMips) && (instruction_set != InstructionSet::kMips64);
Igor Murashkinae7ff922016-10-06 14:59:19 -0700324 std::unique_ptr<JNIMacroLabel> skip_cold_path_label;
325 if (kReadBarrierFastPath) {
326 skip_cold_path_label = __ CreateLabel();
327 // Fast path for supported targets.
328 //
329 // Check if gc_is_marking is set -- if it's not, we don't need
330 // a read barrier so skip it.
331 __ LoadFromThread(main_jni_conv->InterproceduralScratchRegister(),
332 Thread::IsGcMarkingOffset<kPointerSize>(),
333 Thread::IsGcMarkingSize());
334 // Jump over the slow path if gc is marking is false.
335 __ Jump(skip_cold_path_label.get(),
336 JNIMacroUnaryCondition::kZero,
337 main_jni_conv->InterproceduralScratchRegister());
338 }
339
340 // Construct slow path for read barrier:
341 //
342 // Call into the runtime's ReadBarrierJni and have it fix up
343 // the object address if it was moved.
344
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700345 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
346 pReadBarrierJni);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700347 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
348 main_jni_conv->Next(); // Skip JNIEnv.
349 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
350 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
351 // Pass the handle for the class as the first argument.
352 if (main_jni_conv->IsCurrentParamOnStack()) {
353 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
354 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
355 mr_conv->InterproceduralScratchRegister(),
356 false);
357 } else {
358 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
359 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
360 ManagedRegister::NoRegister(), false);
361 }
362 main_jni_conv->Next();
363 // Pass the current thread as the second argument and call.
364 if (main_jni_conv->IsCurrentParamInRegister()) {
365 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700366 __ Call(main_jni_conv->CurrentParamRegister(),
367 Offset(read_barrier),
368 main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700369 } else {
370 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
371 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700372 __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700373 }
374 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700375
376 if (kReadBarrierFastPath) {
377 __ Bind(skip_cold_path_label.get());
378 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700379 }
380
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
382 // can occur. The result is the saved JNI local state that is restored by the exit call. We
383 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
384 // arguments.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700385 FrameOffset locked_object_handle_scope_offset(0xBEEFDEAD);
386 if (LIKELY(!is_critical_native)) {
387 // Skip this for @CriticalNative methods. They do not call JniMethodStart.
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700388 ThreadOffset<kPointerSize> jni_start(
389 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kStart,
390 reference_return,
391 is_synchronized,
392 is_fast_native).SizeValue());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700394 locked_object_handle_scope_offset = FrameOffset(0);
395 if (is_synchronized) {
396 // Pass object for locking.
397 main_jni_conv->Next(); // Skip JNIEnv.
398 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
399 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
400 if (main_jni_conv->IsCurrentParamOnStack()) {
401 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
402 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
403 mr_conv->InterproceduralScratchRegister(), false);
404 } else {
405 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
406 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
407 ManagedRegister::NoRegister(), false);
408 }
409 main_jni_conv->Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700410 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700411 if (main_jni_conv->IsCurrentParamInRegister()) {
412 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
413 __ Call(main_jni_conv->CurrentParamRegister(),
414 Offset(jni_start),
415 main_jni_conv->InterproceduralScratchRegister());
416 } else {
417 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
418 main_jni_conv->InterproceduralScratchRegister());
419 __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister());
420 }
421 if (is_synchronized) { // Check for exceptions from monitor enter.
422 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
423 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700424 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700425
426 // Store into stack_frame[saved_cookie_offset] the return value of JniMethodStart.
427 FrameOffset saved_cookie_offset(
428 FrameOffset(0xDEADBEEFu)); // @CriticalNative - use obviously bad value for debugging
429 if (LIKELY(!is_critical_native)) {
430 saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
431 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4 /* sizeof cookie */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433
434 // 7. Iterate over arguments placing values from managed calling convention in
435 // to the convention required for a native call (shuffling). For references
436 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700437 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700439 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100440 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 uint32_t args_count = 0;
442 while (mr_conv->HasNext()) {
443 args_count++;
444 mr_conv->Next();
445 }
446
447 // Do a backward pass over arguments, so that the generated code will be "mov
448 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
449 // TODO: A reverse iterator to improve readability.
450 for (uint32_t i = 0; i < args_count; ++i) {
451 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
452 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700453
454 // Skip the extra JNI parameters for now.
455 if (LIKELY(!is_critical_native)) {
456 main_jni_conv->Next(); // Skip JNIEnv*.
457 if (is_static) {
458 main_jni_conv->Next(); // Skip Class for now.
459 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 }
461 // Skip to the argument we're interested in.
462 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
463 mr_conv->Next();
464 main_jni_conv->Next();
465 }
466 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
467 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700468 if (is_static && !is_critical_native) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700469 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100470 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
472 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700473 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 if (main_jni_conv->IsCurrentParamOnStack()) {
475 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700476 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 mr_conv->InterproceduralScratchRegister(),
478 false);
479 } else {
480 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700481 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700482 ManagedRegister::NoRegister(), false);
483 }
484 }
485
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700486 // Set the iterator back to the incoming Method*.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700488 if (LIKELY(!is_critical_native)) {
489 // 8. Create 1st argument, the JNI environment ptr.
490 // Register that will hold local indirect reference table
491 if (main_jni_conv->IsCurrentParamInRegister()) {
492 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
493 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
494 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
495 } else {
496 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
497 __ CopyRawPtrFromThread(jni_env,
498 Thread::JniEnvOffset<kPointerSize>(),
499 main_jni_conv->InterproceduralScratchRegister());
500 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 }
502
503 // 9. Plant call to native code associated with method.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700504 MemberOffset jni_entrypoint_offset =
505 ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set));
506 // FIXME: Not sure if MethodStackOffset will work here. What does it even do?
507 __ Call(main_jni_conv->MethodStackOffset(),
508 jni_entrypoint_offset,
509 // XX: Why not the jni conv scratch register?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 mr_conv->InterproceduralScratchRegister());
511
512 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700513 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700514 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
515 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
516 __ SignExtend(main_jni_conv->ReturnRegister(),
517 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
518 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
519 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
520 __ ZeroExtend(main_jni_conv->ReturnRegister(),
521 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
522 }
523 }
524
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700525 // 11. Process return value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
527 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700528 if (LIKELY(!is_critical_native)) {
529 // For normal JNI, store the return value on the stack because the call to
530 // JniMethodEnd will clobber the return value. It will be restored in (13).
Vladimir Marko33bff252017-11-01 14:35:42 +0000531 if ((instruction_set == InstructionSet::kMips ||
532 instruction_set == InstructionSet::kMips64) &&
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700533 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
534 return_save_location.Uint32Value() % 8 != 0) {
535 // Ensure doubles are 8-byte aligned for MIPS
536 return_save_location = FrameOffset(return_save_location.Uint32Value()
537 + static_cast<size_t>(kMipsPointerSize));
538 // TODO: refactor this into the JniCallingConvention code
539 // as a return value alignment requirement.
540 }
541 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
542 __ Store(return_save_location,
543 main_jni_conv->ReturnRegister(),
544 main_jni_conv->SizeOfReturnValue());
545 } else {
546 // For @CriticalNative only,
547 // move the JNI return register into the managed return register (if they don't match).
548 ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister();
549 ManagedRegister mr_return_reg = mr_conv->ReturnRegister();
550
551 // Check if the JNI return register matches the managed return register.
552 // If they differ, only then do we have to do anything about it.
553 // Otherwise the return value is already in the right place when we return.
554 if (!jni_return_reg.Equals(mr_return_reg)) {
555 // This is typically only necessary on ARM32 due to native being softfloat
556 // while managed is hardfloat.
557 // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0.
558 __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue());
559 } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) {
560 // Sanity check: If the return value is passed on the stack for some reason,
561 // then make sure the size matches.
562 CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue());
563 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700564 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 }
566
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100567 // Increase frame size for out args if needed by the end_jni_conv.
568 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
569 if (end_out_arg_size > current_out_arg_size) {
570 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
571 current_out_arg_size = end_out_arg_size;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700572 // TODO: This is redundant for @CriticalNative but we need to
573 // conditionally do __DecreaseFrameSize below.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100574 __ IncreaseFrameSize(out_arg_size_diff);
575 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
576 locked_object_handle_scope_offset =
577 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
578 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
579 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580 // thread.
581 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700582
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700583 if (LIKELY(!is_critical_native)) {
584 // 12. Call JniMethodEnd
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700585 ThreadOffset<kPointerSize> jni_end(
586 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kEnd,
587 reference_return,
588 is_synchronized,
589 is_fast_native).SizeValue());
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700590 if (reference_return) {
591 // Pass result.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700592 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
593 end_jni_conv->Next();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700594 }
595 // Pass saved local reference state.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700596 if (end_jni_conv->IsCurrentParamOnStack()) {
597 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700598 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700599 } else {
600 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700601 __ Load(out_reg, saved_cookie_offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700602 }
603 end_jni_conv->Next();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700604 if (is_synchronized) {
605 // Pass object for unlocking.
606 if (end_jni_conv->IsCurrentParamOnStack()) {
607 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
608 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
609 end_jni_conv->InterproceduralScratchRegister(),
610 false);
611 } else {
612 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
613 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
614 ManagedRegister::NoRegister(), false);
615 }
616 end_jni_conv->Next();
617 }
618 if (end_jni_conv->IsCurrentParamInRegister()) {
619 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
620 __ Call(end_jni_conv->CurrentParamRegister(),
621 Offset(jni_end),
622 end_jni_conv->InterproceduralScratchRegister());
623 } else {
624 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
625 end_jni_conv->InterproceduralScratchRegister());
626 __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister());
627 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700628
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700629 // 13. Reload return value
630 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
631 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
632 // NIT: If it's @CriticalNative then we actually only need to do this IF
633 // the calling convention's native return register doesn't match the managed convention's
634 // return register.
635 }
636 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637
638 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100639 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700640
641 // 15. Process pending exceptions from JNI call or monitor exit.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700642 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0 /* stack_adjust */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700644 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100646 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Roland Levillain0d127e12017-07-05 17:01:11 +0100647 // We expect the compiled method to possibly be suspended during its
648 // execution, except in the case of a CriticalNative method.
649 bool may_suspend = !is_critical_native;
650 __ RemoveFrame(frame_size, callee_save_regs, may_suspend);
David Srbeckydd973932015-04-07 20:29:48 +0100651 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700652
653 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000654 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 size_t cs = __ CodeSize();
656 std::vector<uint8_t> managed_code(cs);
657 MemoryRegion code(&managed_code[0], managed_code.size());
658 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100659
Vladimir Marko7bdc6e72017-11-28 12:37:13 +0000660 return JniCompiledMethod(instruction_set,
661 std::move(managed_code),
662 frame_size,
663 main_jni_conv->CoreSpillMask(),
664 main_jni_conv->FpSpillMask(),
665 ArrayRef<const uint8_t>(*jni_asm->cfi().data()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666}
667
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700668// Copy a single parameter from the managed to the JNI calling convention.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700669template <PointerSize kPointerSize>
670static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700671 ManagedRuntimeCallingConvention* mr_conv,
672 JniCallingConvention* jni_conv,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700673 size_t frame_size,
674 size_t out_arg_size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700675 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
676 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700677 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 bool null_allowed = false;
679 bool ref_param = jni_conv->IsCurrentParamAReference();
680 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
681 // input may be in register, on stack or both - but not none!
682 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
683 if (output_in_reg) { // output shouldn't straddle registers and stack
684 CHECK(!jni_conv->IsCurrentParamOnStack());
685 } else {
686 CHECK(jni_conv->IsCurrentParamOnStack());
687 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700689 if (ref_param) {
690 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700691 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
692 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700693 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700694 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
695 // Check handle scope offset is within frame.
696 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697 }
698 if (input_in_reg && output_in_reg) {
699 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
700 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
701 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700702 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703 } else {
704 if (!mr_conv->IsCurrentParamOnStack()) {
705 // regular non-straddling move
706 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
707 } else {
708 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
709 }
710 }
711 } else if (!input_in_reg && !output_in_reg) {
712 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
713 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700714 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700715 null_allowed);
716 } else {
717 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
718 size_t param_size = mr_conv->CurrentParamSize();
719 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
720 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
721 }
722 } else if (!input_in_reg && output_in_reg) {
723 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
724 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
725 // Check that incoming stack arguments are above the current stack frame.
726 CHECK_GT(in_off.Uint32Value(), frame_size);
727 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700728 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 } else {
730 size_t param_size = mr_conv->CurrentParamSize();
731 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
732 __ Load(out_reg, in_off, param_size);
733 }
734 } else {
735 CHECK(input_in_reg && !output_in_reg);
736 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
737 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
738 // Check outgoing argument is within frame
739 CHECK_LT(out_off.Uint32Value(), frame_size);
740 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700741 // TODO: recycle value in in_reg rather than reload from handle scope
742 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 null_allowed);
744 } else {
745 size_t param_size = mr_conv->CurrentParamSize();
746 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
747 if (!mr_conv->IsCurrentParamOnStack()) {
748 // regular non-straddling store
749 __ Store(out_off, in_reg, param_size);
750 } else {
751 // store where input straddles registers and stack
752 CHECK_EQ(param_size, 8u);
753 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
754 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
755 }
756 }
757 }
758}
759
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700760template <PointerSize kPointerSize>
761static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 JniCallingConvention* jni_conv,
763 ManagedRegister in_reg) {
764 if (jni_conv->IsCurrentParamOnStack()) {
765 FrameOffset dest = jni_conv->CurrentParamStackOffset();
766 __ StoreRawPtr(dest, in_reg);
767 } else {
768 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
769 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
770 }
771 }
772}
773
Vladimir Marko7bdc6e72017-11-28 12:37:13 +0000774JniCompiledMethod ArtQuickJniCompileMethod(CompilerDriver* compiler,
775 uint32_t access_flags,
776 uint32_t method_idx,
777 const DexFile& dex_file) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700778 if (Is64BitInstructionSet(compiler->GetInstructionSet())) {
779 return ArtJniCompileMethodInternal<PointerSize::k64>(
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100780 compiler, access_flags, method_idx, dex_file);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700781 } else {
782 return ArtJniCompileMethodInternal<PointerSize::k32>(
Vladimir Markob0a6aee2017-10-27 10:34:04 +0100783 compiler, access_flags, method_idx, dex_file);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700784 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700785}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700786
787} // namespace art