xref: /aosp_15_r20/art/compiler/jni/quick/jni_compiler.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "jni_compiler.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <algorithm>
20*795d594fSAndroid Build Coastguard Worker #include <fstream>
21*795d594fSAndroid Build Coastguard Worker #include <ios>
22*795d594fSAndroid Build Coastguard Worker #include <memory>
23*795d594fSAndroid Build Coastguard Worker #include <vector>
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker #include "art_method.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/arena_allocator.h"
27*795d594fSAndroid Build Coastguard Worker #include "base/arena_containers.h"
28*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"  // For VLOG.
29*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
30*795d594fSAndroid Build Coastguard Worker #include "base/memory_region.h"
31*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
32*795d594fSAndroid Build Coastguard Worker #include "base/utils.h"
33*795d594fSAndroid Build Coastguard Worker #include "calling_convention.h"
34*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
35*795d594fSAndroid Build Coastguard Worker #include "dwarf/debug_frame_opcode_writer.h"
36*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
37*795d594fSAndroid Build Coastguard Worker #include "entrypoints/quick/quick_entrypoints.h"
38*795d594fSAndroid Build Coastguard Worker #include "instrumentation.h"
39*795d594fSAndroid Build Coastguard Worker #include "jni/jni_env_ext.h"
40*795d594fSAndroid Build Coastguard Worker #include "jni/local_reference_table.h"
41*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
42*795d594fSAndroid Build Coastguard Worker #include "thread.h"
43*795d594fSAndroid Build Coastguard Worker #include "utils/arm/managed_register_arm.h"
44*795d594fSAndroid Build Coastguard Worker #include "utils/arm64/managed_register_arm64.h"
45*795d594fSAndroid Build Coastguard Worker #include "utils/assembler.h"
46*795d594fSAndroid Build Coastguard Worker #include "utils/jni_macro_assembler.h"
47*795d594fSAndroid Build Coastguard Worker #include "utils/managed_register.h"
48*795d594fSAndroid Build Coastguard Worker #include "utils/x86/managed_register_x86.h"
49*795d594fSAndroid Build Coastguard Worker 
50*795d594fSAndroid Build Coastguard Worker #define __ jni_asm->
51*795d594fSAndroid Build Coastguard Worker 
52*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
53*795d594fSAndroid Build Coastguard Worker 
54*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
55*795d594fSAndroid Build Coastguard Worker static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
56*795d594fSAndroid Build Coastguard Worker                                JniCallingConvention* jni_conv,
57*795d594fSAndroid Build Coastguard Worker                                ManagedRegister in_reg);
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
60*795d594fSAndroid Build Coastguard Worker static void CallDecodeReferenceResult(JNIMacroAssembler<kPointerSize>* jni_asm,
61*795d594fSAndroid Build Coastguard Worker                                       JniCallingConvention* jni_conv,
62*795d594fSAndroid Build Coastguard Worker                                       ManagedRegister mr_return_reg,
63*795d594fSAndroid Build Coastguard Worker                                       size_t main_out_arg_size);
64*795d594fSAndroid Build Coastguard Worker 
65*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
GetMacroAssembler(ArenaAllocator * allocator,InstructionSet isa,const InstructionSetFeatures * features)66*795d594fSAndroid Build Coastguard Worker static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
67*795d594fSAndroid Build Coastguard Worker     ArenaAllocator* allocator, InstructionSet isa, const InstructionSetFeatures* features) {
68*795d594fSAndroid Build Coastguard Worker   return JNIMacroAssembler<kPointerSize>::Create(allocator, isa, features);
69*795d594fSAndroid Build Coastguard Worker }
70*795d594fSAndroid Build Coastguard Worker 
71*795d594fSAndroid Build Coastguard Worker 
72*795d594fSAndroid Build Coastguard Worker // Generate the JNI bridge for the given method, general contract:
73*795d594fSAndroid Build Coastguard Worker // - Arguments are in the managed runtime format, either on stack or in
74*795d594fSAndroid Build Coastguard Worker //   registers, a reference to the method object is supplied as part of this
75*795d594fSAndroid Build Coastguard Worker //   convention.
76*795d594fSAndroid Build Coastguard Worker //
77*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
ArtJniCompileMethodInternal(const CompilerOptions & compiler_options,std::string_view shorty,uint32_t access_flags,ArenaAllocator * allocator)78*795d594fSAndroid Build Coastguard Worker static JniCompiledMethod ArtJniCompileMethodInternal(const CompilerOptions& compiler_options,
79*795d594fSAndroid Build Coastguard Worker                                                      std::string_view shorty,
80*795d594fSAndroid Build Coastguard Worker                                                      uint32_t access_flags,
81*795d594fSAndroid Build Coastguard Worker                                                      ArenaAllocator* allocator) {
82*795d594fSAndroid Build Coastguard Worker   constexpr size_t kRawPointerSize = static_cast<size_t>(kPointerSize);
83*795d594fSAndroid Build Coastguard Worker   CHECK_NE(access_flags & kAccNative, 0u);
84*795d594fSAndroid Build Coastguard Worker   const bool is_static = (access_flags & kAccStatic) != 0;
85*795d594fSAndroid Build Coastguard Worker   const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
86*795d594fSAndroid Build Coastguard Worker   const bool is_fast_native = (access_flags & kAccFastNative) != 0u;
87*795d594fSAndroid Build Coastguard Worker   const bool is_critical_native = (access_flags & kAccCriticalNative) != 0u;
88*795d594fSAndroid Build Coastguard Worker 
89*795d594fSAndroid Build Coastguard Worker   InstructionSet instruction_set = compiler_options.GetInstructionSet();
90*795d594fSAndroid Build Coastguard Worker   const InstructionSetFeatures* instruction_set_features =
91*795d594fSAndroid Build Coastguard Worker       compiler_options.GetInstructionSetFeatures();
92*795d594fSAndroid Build Coastguard Worker   bool emit_read_barrier = compiler_options.EmitReadBarrier();
93*795d594fSAndroid Build Coastguard Worker   bool is_debuggable = compiler_options.GetDebuggable();
94*795d594fSAndroid Build Coastguard Worker   bool needs_entry_exit_hooks = is_debuggable && compiler_options.IsJitCompiler();
95*795d594fSAndroid Build Coastguard Worker   // We don't support JITing stubs for critical native methods in debuggable runtimes yet.
96*795d594fSAndroid Build Coastguard Worker   // TODO(mythria): Add support required for calling method entry / exit hooks from critical native
97*795d594fSAndroid Build Coastguard Worker   // methods.
98*795d594fSAndroid Build Coastguard Worker   DCHECK_IMPLIES(needs_entry_exit_hooks, !is_critical_native);
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker   // The fast-path for decoding a reference skips CheckJNI checks, so we do not inline the
101*795d594fSAndroid Build Coastguard Worker   // decoding in debug build or for debuggable apps (both cases enable CheckJNI by default).
102*795d594fSAndroid Build Coastguard Worker   bool inline_decode_reference = !kIsDebugBuild && !is_debuggable;
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   // When  walking the stack the top frame doesn't have a pc associated with it. We then depend on
105*795d594fSAndroid Build Coastguard Worker   // the invariant that we don't have JITed code when AOT code is available. In debuggable runtimes
106*795d594fSAndroid Build Coastguard Worker   // this invariant doesn't hold. So we tag the SP for JITed code to indentify if we are executing
107*795d594fSAndroid Build Coastguard Worker   // JITed code or AOT code. Since tagging involves additional instructions we tag only in
108*795d594fSAndroid Build Coastguard Worker   // debuggable runtimes.
109*795d594fSAndroid Build Coastguard Worker   bool should_tag_sp = needs_entry_exit_hooks;
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker   VLOG(jni) << "JniCompile: shorty=\"" << shorty
112*795d594fSAndroid Build Coastguard Worker             << "\", access_flags=0x" << std::hex << access_flags
113*795d594fSAndroid Build Coastguard Worker             << (is_static ? " static" : "")
114*795d594fSAndroid Build Coastguard Worker             << (is_synchronized ? " synchronized" : "")
115*795d594fSAndroid Build Coastguard Worker             << (is_fast_native ? " @FastNative" : "")
116*795d594fSAndroid Build Coastguard Worker             << (is_critical_native ? " @CriticalNative" : "");
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
119*795d594fSAndroid Build Coastguard Worker     // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive.
120*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(is_fast_native && is_critical_native)) {
121*795d594fSAndroid Build Coastguard Worker       LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative, \""
122*795d594fSAndroid Build Coastguard Worker                  << shorty << "\", 0x" << std::hex << access_flags;
123*795d594fSAndroid Build Coastguard Worker     }
124*795d594fSAndroid Build Coastguard Worker 
125*795d594fSAndroid Build Coastguard Worker     // @CriticalNative - extra checks:
126*795d594fSAndroid Build Coastguard Worker     // -- Don't allow virtual criticals
127*795d594fSAndroid Build Coastguard Worker     // -- Don't allow synchronized criticals
128*795d594fSAndroid Build Coastguard Worker     // -- Don't allow any objects as parameter or return value
129*795d594fSAndroid Build Coastguard Worker     if (UNLIKELY(is_critical_native)) {
130*795d594fSAndroid Build Coastguard Worker       CHECK(is_static)
131*795d594fSAndroid Build Coastguard Worker           << "@CriticalNative functions cannot be virtual since that would "
132*795d594fSAndroid Build Coastguard Worker           << "require passing a reference parameter (this), which is illegal, \""
133*795d594fSAndroid Build Coastguard Worker           << shorty << "\", 0x" << std::hex << access_flags;
134*795d594fSAndroid Build Coastguard Worker       CHECK(!is_synchronized)
135*795d594fSAndroid Build Coastguard Worker           << "@CriticalNative functions cannot be synchronized since that would "
136*795d594fSAndroid Build Coastguard Worker           << "require passing a (class and/or this) reference parameter, which is illegal, \""
137*795d594fSAndroid Build Coastguard Worker           << shorty << "\", 0x" << std::hex << access_flags;
138*795d594fSAndroid Build Coastguard Worker       for (char c : shorty) {
139*795d594fSAndroid Build Coastguard Worker         CHECK_NE(Primitive::kPrimNot, Primitive::GetType(c))
140*795d594fSAndroid Build Coastguard Worker             << "@CriticalNative methods' shorty types must not have illegal references, \""
141*795d594fSAndroid Build Coastguard Worker             << shorty << "\", 0x" << std::hex << access_flags;
142*795d594fSAndroid Build Coastguard Worker       }
143*795d594fSAndroid Build Coastguard Worker     }
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker 
146*795d594fSAndroid Build Coastguard Worker   // Calling conventions used to iterate over parameters to method
147*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JniCallingConvention> main_jni_conv =
148*795d594fSAndroid Build Coastguard Worker       JniCallingConvention::Create(allocator,
149*795d594fSAndroid Build Coastguard Worker                                    is_static,
150*795d594fSAndroid Build Coastguard Worker                                    is_synchronized,
151*795d594fSAndroid Build Coastguard Worker                                    is_fast_native,
152*795d594fSAndroid Build Coastguard Worker                                    is_critical_native,
153*795d594fSAndroid Build Coastguard Worker                                    shorty,
154*795d594fSAndroid Build Coastguard Worker                                    instruction_set);
155*795d594fSAndroid Build Coastguard Worker   bool reference_return = main_jni_conv->IsReturnAReference();
156*795d594fSAndroid Build Coastguard Worker 
157*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
158*795d594fSAndroid Build Coastguard Worker       ManagedRuntimeCallingConvention::Create(
159*795d594fSAndroid Build Coastguard Worker           allocator, is_static, is_synchronized, shorty, instruction_set));
160*795d594fSAndroid Build Coastguard Worker 
161*795d594fSAndroid Build Coastguard Worker   // Assembler that holds generated instructions
162*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
163*795d594fSAndroid Build Coastguard Worker       GetMacroAssembler<kPointerSize>(allocator, instruction_set, instruction_set_features);
164*795d594fSAndroid Build Coastguard Worker   jni_asm->cfi().SetEnabled(compiler_options.GenerateAnyDebugInfo());
165*795d594fSAndroid Build Coastguard Worker   jni_asm->SetEmitRunTimeChecksInDebugMode(compiler_options.EmitRunTimeChecksInDebugMode());
166*795d594fSAndroid Build Coastguard Worker 
167*795d594fSAndroid Build Coastguard Worker   // 1. Build and register the native method frame.
168*795d594fSAndroid Build Coastguard Worker 
169*795d594fSAndroid Build Coastguard Worker   // 1.1. Build the frame saving all callee saves, Method*, and PC return address.
170*795d594fSAndroid Build Coastguard Worker   //      For @CriticalNative, this includes space for out args, otherwise just the managed frame.
171*795d594fSAndroid Build Coastguard Worker   const size_t managed_frame_size = main_jni_conv->FrameSize();
172*795d594fSAndroid Build Coastguard Worker   const size_t main_out_arg_size = main_jni_conv->OutFrameSize();
173*795d594fSAndroid Build Coastguard Worker   size_t current_frame_size = is_critical_native ? main_out_arg_size : managed_frame_size;
174*795d594fSAndroid Build Coastguard Worker   ManagedRegister method_register =
175*795d594fSAndroid Build Coastguard Worker       is_critical_native ? ManagedRegister::NoRegister() : mr_conv->MethodRegister();
176*795d594fSAndroid Build Coastguard Worker   ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
177*795d594fSAndroid Build Coastguard Worker   __ BuildFrame(current_frame_size, method_register, callee_save_regs);
178*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
179*795d594fSAndroid Build Coastguard Worker 
180*795d594fSAndroid Build Coastguard Worker   // 1.2. Check if we need to go to the slow path to emit the read barrier
181*795d594fSAndroid Build Coastguard Worker   //      for the declaring class in the method for a static call.
182*795d594fSAndroid Build Coastguard Worker   //      Skip this for @CriticalNative because we're not passing a `jclass` to the native method.
183*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> jclass_read_barrier_slow_path;
184*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> jclass_read_barrier_return;
185*795d594fSAndroid Build Coastguard Worker   if (emit_read_barrier && is_static && LIKELY(!is_critical_native)) {
186*795d594fSAndroid Build Coastguard Worker     jclass_read_barrier_slow_path = __ CreateLabel();
187*795d594fSAndroid Build Coastguard Worker     jclass_read_barrier_return = __ CreateLabel();
188*795d594fSAndroid Build Coastguard Worker 
189*795d594fSAndroid Build Coastguard Worker     // Check if gc_is_marking is set -- if it's not, we don't need a read barrier.
190*795d594fSAndroid Build Coastguard Worker     __ TestGcMarking(jclass_read_barrier_slow_path.get(), JNIMacroUnaryCondition::kNotZero);
191*795d594fSAndroid Build Coastguard Worker 
192*795d594fSAndroid Build Coastguard Worker     // If marking, the slow path returns after the check.
193*795d594fSAndroid Build Coastguard Worker     __ Bind(jclass_read_barrier_return.get());
194*795d594fSAndroid Build Coastguard Worker   }
195*795d594fSAndroid Build Coastguard Worker 
196*795d594fSAndroid Build Coastguard Worker   // 1.3 Spill reference register arguments.
197*795d594fSAndroid Build Coastguard Worker   constexpr FrameOffset kInvalidReferenceOffset =
198*795d594fSAndroid Build Coastguard Worker       JNIMacroAssembler<kPointerSize>::kInvalidReferenceOffset;
199*795d594fSAndroid Build Coastguard Worker   ArenaVector<ArgumentLocation> src_args(allocator->Adapter());
200*795d594fSAndroid Build Coastguard Worker   ArenaVector<ArgumentLocation> dest_args(allocator->Adapter());
201*795d594fSAndroid Build Coastguard Worker   ArenaVector<FrameOffset> refs(allocator->Adapter());
202*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
203*795d594fSAndroid Build Coastguard Worker     mr_conv->ResetIterator(FrameOffset(current_frame_size));
204*795d594fSAndroid Build Coastguard Worker     for (; mr_conv->HasNext(); mr_conv->Next()) {
205*795d594fSAndroid Build Coastguard Worker       if (mr_conv->IsCurrentParamInRegister() && mr_conv->IsCurrentParamAReference()) {
206*795d594fSAndroid Build Coastguard Worker         // Spill the reference as raw data.
207*795d594fSAndroid Build Coastguard Worker         src_args.emplace_back(mr_conv->CurrentParamRegister(), kObjectReferenceSize);
208*795d594fSAndroid Build Coastguard Worker         dest_args.emplace_back(mr_conv->CurrentParamStackOffset(), kObjectReferenceSize);
209*795d594fSAndroid Build Coastguard Worker         refs.push_back(kInvalidReferenceOffset);
210*795d594fSAndroid Build Coastguard Worker       }
211*795d594fSAndroid Build Coastguard Worker     }
212*795d594fSAndroid Build Coastguard Worker     __ MoveArguments(ArrayRef<ArgumentLocation>(dest_args),
213*795d594fSAndroid Build Coastguard Worker                      ArrayRef<ArgumentLocation>(src_args),
214*795d594fSAndroid Build Coastguard Worker                      ArrayRef<FrameOffset>(refs));
215*795d594fSAndroid Build Coastguard Worker   }
216*795d594fSAndroid Build Coastguard Worker 
217*795d594fSAndroid Build Coastguard Worker   // 1.4. Write out the end of the quick frames. After this, we can walk the stack.
218*795d594fSAndroid Build Coastguard Worker   // NOTE: @CriticalNative does not need to store the stack pointer to the thread
219*795d594fSAndroid Build Coastguard Worker   //       because garbage collections are disabled within the execution of a
220*795d594fSAndroid Build Coastguard Worker   //       @CriticalNative method.
221*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
222*795d594fSAndroid Build Coastguard Worker     __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>(), should_tag_sp);
223*795d594fSAndroid Build Coastguard Worker   }
224*795d594fSAndroid Build Coastguard Worker 
225*795d594fSAndroid Build Coastguard Worker   // 1.5. Call any method entry hooks if required.
226*795d594fSAndroid Build Coastguard Worker   // For critical native methods, we don't JIT stubs in debuggable runtimes (see
227*795d594fSAndroid Build Coastguard Worker   // OptimizingCompiler::JitCompile).
228*795d594fSAndroid Build Coastguard Worker   // TODO(mythria): Add support to call method entry / exit hooks for critical native methods too.
229*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> method_entry_hook_slow_path;
230*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> method_entry_hook_return;
231*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(needs_entry_exit_hooks)) {
232*795d594fSAndroid Build Coastguard Worker     uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
233*795d594fSAndroid Build Coastguard Worker     int offset = instrumentation::Instrumentation::HaveMethodEntryListenersOffset().Int32Value();
234*795d594fSAndroid Build Coastguard Worker     method_entry_hook_slow_path = __ CreateLabel();
235*795d594fSAndroid Build Coastguard Worker     method_entry_hook_return = __ CreateLabel();
236*795d594fSAndroid Build Coastguard Worker     __ TestByteAndJumpIfNotZero(address + offset, method_entry_hook_slow_path.get());
237*795d594fSAndroid Build Coastguard Worker     __ Bind(method_entry_hook_return.get());
238*795d594fSAndroid Build Coastguard Worker   }
239*795d594fSAndroid Build Coastguard Worker 
240*795d594fSAndroid Build Coastguard Worker   // 2. Lock the object (if synchronized) and transition out of Runnable (if normal native).
241*795d594fSAndroid Build Coastguard Worker 
242*795d594fSAndroid Build Coastguard Worker   // 2.1. Lock the synchronization object (`this` or class) for synchronized methods.
243*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_synchronized)) {
244*795d594fSAndroid Build Coastguard Worker     // We are using a custom calling convention for locking where the assembly thunk gets
245*795d594fSAndroid Build Coastguard Worker     // the object to lock in a register (even on x86), it can use callee-save registers
246*795d594fSAndroid Build Coastguard Worker     // as temporaries (they were saved above) and must preserve argument registers.
247*795d594fSAndroid Build Coastguard Worker     ManagedRegister to_lock = main_jni_conv->LockingArgumentRegister();
248*795d594fSAndroid Build Coastguard Worker     if (is_static) {
249*795d594fSAndroid Build Coastguard Worker       // Pass the declaring class. It was already marked if needed.
250*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(ArtMethod::DeclaringClassOffset().SizeValue(), 0u);
251*795d594fSAndroid Build Coastguard Worker       __ Load(to_lock, method_register, MemberOffset(0u), kObjectReferenceSize);
252*795d594fSAndroid Build Coastguard Worker     } else {
253*795d594fSAndroid Build Coastguard Worker       // Pass the `this` argument.
254*795d594fSAndroid Build Coastguard Worker       mr_conv->ResetIterator(FrameOffset(current_frame_size));
255*795d594fSAndroid Build Coastguard Worker       if (mr_conv->IsCurrentParamInRegister()) {
256*795d594fSAndroid Build Coastguard Worker         __ Move(to_lock, mr_conv->CurrentParamRegister(), kObjectReferenceSize);
257*795d594fSAndroid Build Coastguard Worker       } else {
258*795d594fSAndroid Build Coastguard Worker         __ Load(to_lock, mr_conv->CurrentParamStackOffset(), kObjectReferenceSize);
259*795d594fSAndroid Build Coastguard Worker       }
260*795d594fSAndroid Build Coastguard Worker     }
261*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniLockObject));
262*795d594fSAndroid Build Coastguard Worker   }
263*795d594fSAndroid Build Coastguard Worker 
264*795d594fSAndroid Build Coastguard Worker   // 2.2. Transition from Runnable to Suspended.
265*795d594fSAndroid Build Coastguard Worker   // Managed callee-saves were already saved, so these registers are now available.
266*795d594fSAndroid Build Coastguard Worker   ArrayRef<const ManagedRegister> callee_save_scratch_regs = UNLIKELY(is_critical_native)
267*795d594fSAndroid Build Coastguard Worker       ? ArrayRef<const ManagedRegister>()
268*795d594fSAndroid Build Coastguard Worker       : main_jni_conv->CalleeSaveScratchRegisters();
269*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> transition_to_native_slow_path;
270*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> transition_to_native_resume;
271*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native && !is_fast_native)) {
272*795d594fSAndroid Build Coastguard Worker     transition_to_native_slow_path = __ CreateLabel();
273*795d594fSAndroid Build Coastguard Worker     transition_to_native_resume = __ CreateLabel();
274*795d594fSAndroid Build Coastguard Worker     __ TryToTransitionFromRunnableToNative(transition_to_native_slow_path.get(),
275*795d594fSAndroid Build Coastguard Worker                                            callee_save_scratch_regs);
276*795d594fSAndroid Build Coastguard Worker     __ Bind(transition_to_native_resume.get());
277*795d594fSAndroid Build Coastguard Worker   }
278*795d594fSAndroid Build Coastguard Worker 
279*795d594fSAndroid Build Coastguard Worker   // 3. Push local reference frame.
280*795d594fSAndroid Build Coastguard Worker   // Skip this for @CriticalNative methods, they cannot use any references.
281*795d594fSAndroid Build Coastguard Worker   ManagedRegister jni_env_reg = ManagedRegister::NoRegister();
282*795d594fSAndroid Build Coastguard Worker   ManagedRegister previous_state_reg = ManagedRegister::NoRegister();
283*795d594fSAndroid Build Coastguard Worker   ManagedRegister current_state_reg = ManagedRegister::NoRegister();
284*795d594fSAndroid Build Coastguard Worker   ManagedRegister callee_save_temp = ManagedRegister::NoRegister();
285*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
286*795d594fSAndroid Build Coastguard Worker     // To pop the local reference frame later, we shall need the JNI environment pointer
287*795d594fSAndroid Build Coastguard Worker     // as well as the cookie, so we preserve them across calls in callee-save registers.
288*795d594fSAndroid Build Coastguard Worker     CHECK_GE(callee_save_scratch_regs.size(), 3u);  // At least 3 for each supported architecture.
289*795d594fSAndroid Build Coastguard Worker     jni_env_reg = callee_save_scratch_regs[0];
290*795d594fSAndroid Build Coastguard Worker     constexpr size_t kLRTSegmentStateSize = sizeof(jni::LRTSegmentState);
291*795d594fSAndroid Build Coastguard Worker     previous_state_reg = __ CoreRegisterWithSize(callee_save_scratch_regs[1], kLRTSegmentStateSize);
292*795d594fSAndroid Build Coastguard Worker     current_state_reg = __ CoreRegisterWithSize(callee_save_scratch_regs[2], kLRTSegmentStateSize);
293*795d594fSAndroid Build Coastguard Worker     if (callee_save_scratch_regs.size() >= 4) {
294*795d594fSAndroid Build Coastguard Worker       callee_save_temp = callee_save_scratch_regs[3];
295*795d594fSAndroid Build Coastguard Worker     }
296*795d594fSAndroid Build Coastguard Worker     const MemberOffset previous_state_offset = JNIEnvExt::LrtPreviousStateOffset(kPointerSize);
297*795d594fSAndroid Build Coastguard Worker 
298*795d594fSAndroid Build Coastguard Worker     // Load the JNI environment pointer.
299*795d594fSAndroid Build Coastguard Worker     __ LoadRawPtrFromThread(jni_env_reg, Thread::JniEnvOffset<kPointerSize>());
300*795d594fSAndroid Build Coastguard Worker 
301*795d594fSAndroid Build Coastguard Worker     // Load the local reference frame states.
302*795d594fSAndroid Build Coastguard Worker     __ LoadLocalReferenceTableStates(jni_env_reg, previous_state_reg, current_state_reg);
303*795d594fSAndroid Build Coastguard Worker 
304*795d594fSAndroid Build Coastguard Worker     // Store the current state as the previous state (push the LRT frame).
305*795d594fSAndroid Build Coastguard Worker     __ Store(jni_env_reg, previous_state_offset, current_state_reg, kLRTSegmentStateSize);
306*795d594fSAndroid Build Coastguard Worker   }
307*795d594fSAndroid Build Coastguard Worker 
308*795d594fSAndroid Build Coastguard Worker   // 4. Make the main native call.
309*795d594fSAndroid Build Coastguard Worker 
310*795d594fSAndroid Build Coastguard Worker   // 4.1. Move frame down to allow space for out going args.
311*795d594fSAndroid Build Coastguard Worker   size_t current_out_arg_size = main_out_arg_size;
312*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_critical_native)) {
313*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(main_out_arg_size, current_frame_size);
314*795d594fSAndroid Build Coastguard Worker   } else {
315*795d594fSAndroid Build Coastguard Worker     __ IncreaseFrameSize(main_out_arg_size);
316*795d594fSAndroid Build Coastguard Worker     current_frame_size += main_out_arg_size;
317*795d594fSAndroid Build Coastguard Worker   }
318*795d594fSAndroid Build Coastguard Worker 
319*795d594fSAndroid Build Coastguard Worker   // 4.2. Fill arguments except the `JNIEnv*`.
320*795d594fSAndroid Build Coastguard Worker   // Note: Non-null reference arguments in registers may point to the from-space if we
321*795d594fSAndroid Build Coastguard Worker   // took the slow-path for locking or transition to Native. However, we only need to
322*795d594fSAndroid Build Coastguard Worker   // compare them with null to construct `jobject`s, so we can still use them.
323*795d594fSAndroid Build Coastguard Worker   src_args.clear();
324*795d594fSAndroid Build Coastguard Worker   dest_args.clear();
325*795d594fSAndroid Build Coastguard Worker   refs.clear();
326*795d594fSAndroid Build Coastguard Worker   mr_conv->ResetIterator(FrameOffset(current_frame_size));
327*795d594fSAndroid Build Coastguard Worker   main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
328*795d594fSAndroid Build Coastguard Worker   bool check_method_not_clobbered = false;
329*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_critical_native)) {
330*795d594fSAndroid Build Coastguard Worker     // Move the method pointer to the hidden argument register.
331*795d594fSAndroid Build Coastguard Worker     // TODO: Pass this as the last argument, not first. Change ARM assembler
332*795d594fSAndroid Build Coastguard Worker     // not to expect all register destinations at the beginning.
333*795d594fSAndroid Build Coastguard Worker     src_args.emplace_back(mr_conv->MethodRegister(), kRawPointerSize);
334*795d594fSAndroid Build Coastguard Worker     dest_args.emplace_back(main_jni_conv->HiddenArgumentRegister(), kRawPointerSize);
335*795d594fSAndroid Build Coastguard Worker     refs.push_back(kInvalidReferenceOffset);
336*795d594fSAndroid Build Coastguard Worker   } else {
337*795d594fSAndroid Build Coastguard Worker     main_jni_conv->Next();    // Skip JNIEnv*.
338*795d594fSAndroid Build Coastguard Worker     FrameOffset method_offset(current_out_arg_size + mr_conv->MethodStackOffset().SizeValue());
339*795d594fSAndroid Build Coastguard Worker     if (main_jni_conv->IsCurrentParamOnStack()) {
340*795d594fSAndroid Build Coastguard Worker       // This is for x86 only. The method shall not be clobbered by argument moves
341*795d594fSAndroid Build Coastguard Worker       // because all arguments are passed on the stack to the native method.
342*795d594fSAndroid Build Coastguard Worker       check_method_not_clobbered = true;
343*795d594fSAndroid Build Coastguard Worker       DCHECK(callee_save_temp.IsNoRegister());
344*795d594fSAndroid Build Coastguard Worker     } else if (!is_static) {
345*795d594fSAndroid Build Coastguard Worker       // The method shall not be available in the `jclass` argument register.
346*795d594fSAndroid Build Coastguard Worker       // Make sure it is available in `callee_save_temp` for the call below.
347*795d594fSAndroid Build Coastguard Worker       // (The old method register can be clobbered by argument moves.)
348*795d594fSAndroid Build Coastguard Worker       DCHECK(!callee_save_temp.IsNoRegister());
349*795d594fSAndroid Build Coastguard Worker       ManagedRegister new_method_reg = __ CoreRegisterWithSize(callee_save_temp, kRawPointerSize);
350*795d594fSAndroid Build Coastguard Worker       DCHECK(!method_register.IsNoRegister());
351*795d594fSAndroid Build Coastguard Worker       __ Move(new_method_reg, method_register, kRawPointerSize);
352*795d594fSAndroid Build Coastguard Worker       method_register = new_method_reg;
353*795d594fSAndroid Build Coastguard Worker     }
354*795d594fSAndroid Build Coastguard Worker     if (is_static) {
355*795d594fSAndroid Build Coastguard Worker       // For static methods, move/load the method to the `jclass` argument.
356*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(ArtMethod::DeclaringClassOffset().SizeValue(), 0u);
357*795d594fSAndroid Build Coastguard Worker       if (method_register.IsNoRegister()) {
358*795d594fSAndroid Build Coastguard Worker         DCHECK(main_jni_conv->IsCurrentParamInRegister());
359*795d594fSAndroid Build Coastguard Worker         src_args.emplace_back(method_offset, kRawPointerSize);
360*795d594fSAndroid Build Coastguard Worker       } else {
361*795d594fSAndroid Build Coastguard Worker         src_args.emplace_back(method_register, kRawPointerSize);
362*795d594fSAndroid Build Coastguard Worker       }
363*795d594fSAndroid Build Coastguard Worker       if (main_jni_conv->IsCurrentParamInRegister()) {
364*795d594fSAndroid Build Coastguard Worker         // The `jclass` argument becomes the new method register needed for the call.
365*795d594fSAndroid Build Coastguard Worker         method_register = main_jni_conv->CurrentParamRegister();
366*795d594fSAndroid Build Coastguard Worker         dest_args.emplace_back(method_register, kRawPointerSize);
367*795d594fSAndroid Build Coastguard Worker       } else {
368*795d594fSAndroid Build Coastguard Worker         dest_args.emplace_back(main_jni_conv->CurrentParamStackOffset(), kRawPointerSize);
369*795d594fSAndroid Build Coastguard Worker       }
370*795d594fSAndroid Build Coastguard Worker       refs.push_back(kInvalidReferenceOffset);
371*795d594fSAndroid Build Coastguard Worker       main_jni_conv->Next();
372*795d594fSAndroid Build Coastguard Worker     }
373*795d594fSAndroid Build Coastguard Worker   }
374*795d594fSAndroid Build Coastguard Worker   // Move normal arguments to their locations.
375*795d594fSAndroid Build Coastguard Worker   for (; mr_conv->HasNext(); mr_conv->Next(), main_jni_conv->Next()) {
376*795d594fSAndroid Build Coastguard Worker     DCHECK(main_jni_conv->HasNext());
377*795d594fSAndroid Build Coastguard Worker     static_assert(kObjectReferenceSize == 4u);
378*795d594fSAndroid Build Coastguard Worker     bool is_reference = mr_conv->IsCurrentParamAReference();
379*795d594fSAndroid Build Coastguard Worker     size_t src_size = mr_conv->CurrentParamSize();
380*795d594fSAndroid Build Coastguard Worker     size_t dest_size = main_jni_conv->CurrentParamSize();
381*795d594fSAndroid Build Coastguard Worker     src_args.push_back(mr_conv->IsCurrentParamInRegister()
382*795d594fSAndroid Build Coastguard Worker         ? ArgumentLocation(mr_conv->CurrentParamRegister(), src_size)
383*795d594fSAndroid Build Coastguard Worker         : ArgumentLocation(mr_conv->CurrentParamStackOffset(), src_size));
384*795d594fSAndroid Build Coastguard Worker     dest_args.push_back(main_jni_conv->IsCurrentParamInRegister()
385*795d594fSAndroid Build Coastguard Worker         ? ArgumentLocation(main_jni_conv->CurrentParamRegister(), dest_size)
386*795d594fSAndroid Build Coastguard Worker         : ArgumentLocation(main_jni_conv->CurrentParamStackOffset(), dest_size));
387*795d594fSAndroid Build Coastguard Worker     refs.push_back(is_reference ? mr_conv->CurrentParamStackOffset() : kInvalidReferenceOffset);
388*795d594fSAndroid Build Coastguard Worker   }
389*795d594fSAndroid Build Coastguard Worker   DCHECK(!main_jni_conv->HasNext());
390*795d594fSAndroid Build Coastguard Worker   DCHECK_IMPLIES(check_method_not_clobbered,
391*795d594fSAndroid Build Coastguard Worker                  std::all_of(dest_args.begin(),
392*795d594fSAndroid Build Coastguard Worker                              dest_args.end(),
393*795d594fSAndroid Build Coastguard Worker                              [](const ArgumentLocation& loc) { return !loc.IsRegister(); }));
394*795d594fSAndroid Build Coastguard Worker   __ MoveArguments(ArrayRef<ArgumentLocation>(dest_args),
395*795d594fSAndroid Build Coastguard Worker                    ArrayRef<ArgumentLocation>(src_args),
396*795d594fSAndroid Build Coastguard Worker                    ArrayRef<FrameOffset>(refs));
397*795d594fSAndroid Build Coastguard Worker 
398*795d594fSAndroid Build Coastguard Worker   // 4.3. Create 1st argument, the JNI environment ptr.
399*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
400*795d594fSAndroid Build Coastguard Worker     main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
401*795d594fSAndroid Build Coastguard Worker     if (main_jni_conv->IsCurrentParamInRegister()) {
402*795d594fSAndroid Build Coastguard Worker       ManagedRegister jni_env_arg = main_jni_conv->CurrentParamRegister();
403*795d594fSAndroid Build Coastguard Worker       __ Move(jni_env_arg, jni_env_reg, kRawPointerSize);
404*795d594fSAndroid Build Coastguard Worker     } else {
405*795d594fSAndroid Build Coastguard Worker       FrameOffset jni_env_arg_offset = main_jni_conv->CurrentParamStackOffset();
406*795d594fSAndroid Build Coastguard Worker       __ Store(jni_env_arg_offset, jni_env_reg, kRawPointerSize);
407*795d594fSAndroid Build Coastguard Worker     }
408*795d594fSAndroid Build Coastguard Worker   }
409*795d594fSAndroid Build Coastguard Worker 
410*795d594fSAndroid Build Coastguard Worker   // 4.4. Plant call to native code associated with method.
411*795d594fSAndroid Build Coastguard Worker   MemberOffset jni_entrypoint_offset =
412*795d594fSAndroid Build Coastguard Worker       ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set));
413*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_critical_native)) {
414*795d594fSAndroid Build Coastguard Worker     if (main_jni_conv->UseTailCall()) {
415*795d594fSAndroid Build Coastguard Worker       __ Jump(main_jni_conv->HiddenArgumentRegister(), jni_entrypoint_offset);
416*795d594fSAndroid Build Coastguard Worker     } else {
417*795d594fSAndroid Build Coastguard Worker       __ Call(main_jni_conv->HiddenArgumentRegister(), jni_entrypoint_offset);
418*795d594fSAndroid Build Coastguard Worker     }
419*795d594fSAndroid Build Coastguard Worker   } else {
420*795d594fSAndroid Build Coastguard Worker     DCHECK(method_register.IsRegister());
421*795d594fSAndroid Build Coastguard Worker     __ Call(method_register, jni_entrypoint_offset);
422*795d594fSAndroid Build Coastguard Worker     // We shall not need the method register anymore. And we may clobber it below
423*795d594fSAndroid Build Coastguard Worker     // if it's the `callee_save_temp`, so clear it here to make sure it's not used.
424*795d594fSAndroid Build Coastguard Worker     method_register = ManagedRegister::NoRegister();
425*795d594fSAndroid Build Coastguard Worker   }
426*795d594fSAndroid Build Coastguard Worker 
427*795d594fSAndroid Build Coastguard Worker   // 4.5. Fix differences in result widths.
428*795d594fSAndroid Build Coastguard Worker   if (main_jni_conv->RequiresSmallResultTypeExtension()) {
429*795d594fSAndroid Build Coastguard Worker     DCHECK(main_jni_conv->HasSmallReturnType());
430*795d594fSAndroid Build Coastguard Worker     CHECK_IMPLIES(is_critical_native, !main_jni_conv->UseTailCall());
431*795d594fSAndroid Build Coastguard Worker     if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
432*795d594fSAndroid Build Coastguard Worker         main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
433*795d594fSAndroid Build Coastguard Worker       __ SignExtend(main_jni_conv->ReturnRegister(),
434*795d594fSAndroid Build Coastguard Worker                     Primitive::ComponentSize(main_jni_conv->GetReturnType()));
435*795d594fSAndroid Build Coastguard Worker     } else {
436*795d594fSAndroid Build Coastguard Worker       CHECK(main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
437*795d594fSAndroid Build Coastguard Worker             main_jni_conv->GetReturnType() == Primitive::kPrimChar);
438*795d594fSAndroid Build Coastguard Worker       __ ZeroExtend(main_jni_conv->ReturnRegister(),
439*795d594fSAndroid Build Coastguard Worker                     Primitive::ComponentSize(main_jni_conv->GetReturnType()));
440*795d594fSAndroid Build Coastguard Worker     }
441*795d594fSAndroid Build Coastguard Worker   }
442*795d594fSAndroid Build Coastguard Worker 
443*795d594fSAndroid Build Coastguard Worker   // 4.6. Move the JNI return register into the managed return register (if they don't match).
444*795d594fSAndroid Build Coastguard Worker   if (main_jni_conv->SizeOfReturnValue() != 0) {
445*795d594fSAndroid Build Coastguard Worker     ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister();
446*795d594fSAndroid Build Coastguard Worker     ManagedRegister mr_return_reg = mr_conv->ReturnRegister();
447*795d594fSAndroid Build Coastguard Worker 
448*795d594fSAndroid Build Coastguard Worker     // Check if the JNI return register matches the managed return register.
449*795d594fSAndroid Build Coastguard Worker     // If they differ, only then do we have to do anything about it.
450*795d594fSAndroid Build Coastguard Worker     // Otherwise the return value is already in the right place when we return.
451*795d594fSAndroid Build Coastguard Worker     if (!jni_return_reg.Equals(mr_return_reg)) {
452*795d594fSAndroid Build Coastguard Worker       CHECK_IMPLIES(is_critical_native, !main_jni_conv->UseTailCall());
453*795d594fSAndroid Build Coastguard Worker       // This is typically only necessary on ARM32 due to native being softfloat
454*795d594fSAndroid Build Coastguard Worker       // while managed is hardfloat.
455*795d594fSAndroid Build Coastguard Worker       // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0.
456*795d594fSAndroid Build Coastguard Worker       __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue());
457*795d594fSAndroid Build Coastguard Worker     } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) {
458*795d594fSAndroid Build Coastguard Worker       // Check that if the return value is passed on the stack for some reason,
459*795d594fSAndroid Build Coastguard Worker       // that the size matches.
460*795d594fSAndroid Build Coastguard Worker       CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue());
461*795d594fSAndroid Build Coastguard Worker     }
462*795d594fSAndroid Build Coastguard Worker   }
463*795d594fSAndroid Build Coastguard Worker 
464*795d594fSAndroid Build Coastguard Worker   // 5. Transition to Runnable (if normal native).
465*795d594fSAndroid Build Coastguard Worker 
466*795d594fSAndroid Build Coastguard Worker   // 5.1. Try transitioning to Runnable with a fast-path implementation.
467*795d594fSAndroid Build Coastguard Worker   //      If fast-path fails, make a slow-path call to `JniMethodEnd()`.
468*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> transition_to_runnable_slow_path;
469*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> transition_to_runnable_resume;
470*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native && !is_fast_native)) {
471*795d594fSAndroid Build Coastguard Worker     transition_to_runnable_slow_path = __ CreateLabel();
472*795d594fSAndroid Build Coastguard Worker     transition_to_runnable_resume = __ CreateLabel();
473*795d594fSAndroid Build Coastguard Worker     __ TryToTransitionFromNativeToRunnable(transition_to_runnable_slow_path.get(),
474*795d594fSAndroid Build Coastguard Worker                                            main_jni_conv->ArgumentScratchRegisters(),
475*795d594fSAndroid Build Coastguard Worker                                            mr_conv->ReturnRegister());
476*795d594fSAndroid Build Coastguard Worker     __ Bind(transition_to_runnable_resume.get());
477*795d594fSAndroid Build Coastguard Worker   }
478*795d594fSAndroid Build Coastguard Worker 
479*795d594fSAndroid Build Coastguard Worker   // 5.2. For methods that return a reference, do an exception check before decoding the reference.
480*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> exception_slow_path =
481*795d594fSAndroid Build Coastguard Worker       LIKELY(!is_critical_native) ? __ CreateLabel() : nullptr;
482*795d594fSAndroid Build Coastguard Worker   if (reference_return) {
483*795d594fSAndroid Build Coastguard Worker     DCHECK(!is_critical_native);
484*795d594fSAndroid Build Coastguard Worker     __ ExceptionPoll(exception_slow_path.get());
485*795d594fSAndroid Build Coastguard Worker   }
486*795d594fSAndroid Build Coastguard Worker 
487*795d594fSAndroid Build Coastguard Worker   // 5.3. For @FastNative that returns a reference, do an early suspend check so that we
488*795d594fSAndroid Build Coastguard Worker   //      do not need to encode the decoded reference in a stack map.
489*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> suspend_check_slow_path =
490*795d594fSAndroid Build Coastguard Worker       UNLIKELY(is_fast_native) ? __ CreateLabel() : nullptr;
491*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> suspend_check_resume =
492*795d594fSAndroid Build Coastguard Worker       UNLIKELY(is_fast_native) ? __ CreateLabel() : nullptr;
493*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_fast_native) && reference_return) {
494*795d594fSAndroid Build Coastguard Worker     __ SuspendCheck(suspend_check_slow_path.get());
495*795d594fSAndroid Build Coastguard Worker     __ Bind(suspend_check_resume.get());
496*795d594fSAndroid Build Coastguard Worker   }
497*795d594fSAndroid Build Coastguard Worker 
498*795d594fSAndroid Build Coastguard Worker   // 5.4 For methods with reference return, decode the `jobject`, either directly
499*795d594fSAndroid Build Coastguard Worker   //     or with a call to `JniDecodeReferenceResult()`.
500*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> decode_reference_slow_path;
501*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> decode_reference_resume;
502*795d594fSAndroid Build Coastguard Worker   if (reference_return) {
503*795d594fSAndroid Build Coastguard Worker     DCHECK(!is_critical_native);
504*795d594fSAndroid Build Coastguard Worker     if (inline_decode_reference) {
505*795d594fSAndroid Build Coastguard Worker       // Decode local and JNI transition references in the main path.
506*795d594fSAndroid Build Coastguard Worker       decode_reference_slow_path = __ CreateLabel();
507*795d594fSAndroid Build Coastguard Worker       decode_reference_resume = __ CreateLabel();
508*795d594fSAndroid Build Coastguard Worker       __ DecodeJNITransitionOrLocalJObject(mr_conv->ReturnRegister(),
509*795d594fSAndroid Build Coastguard Worker                                            decode_reference_slow_path.get(),
510*795d594fSAndroid Build Coastguard Worker                                            decode_reference_resume.get());
511*795d594fSAndroid Build Coastguard Worker       __ Bind(decode_reference_resume.get());
512*795d594fSAndroid Build Coastguard Worker     } else {
513*795d594fSAndroid Build Coastguard Worker       CallDecodeReferenceResult<kPointerSize>(
514*795d594fSAndroid Build Coastguard Worker           jni_asm.get(), main_jni_conv.get(), mr_conv->ReturnRegister(), main_out_arg_size);
515*795d594fSAndroid Build Coastguard Worker     }
516*795d594fSAndroid Build Coastguard Worker   }  // if (!is_critical_native)
517*795d594fSAndroid Build Coastguard Worker 
518*795d594fSAndroid Build Coastguard Worker   // 6. Pop local reference frame.
519*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
520*795d594fSAndroid Build Coastguard Worker     __ StoreLocalReferenceTableStates(jni_env_reg, previous_state_reg, current_state_reg);
521*795d594fSAndroid Build Coastguard Worker     // For x86, the `callee_save_temp` is not valid, so let's simply change it to one
522*795d594fSAndroid Build Coastguard Worker     // of the callee save registers that we don't need anymore for all architectures.
523*795d594fSAndroid Build Coastguard Worker     callee_save_temp = current_state_reg;
524*795d594fSAndroid Build Coastguard Worker   }
525*795d594fSAndroid Build Coastguard Worker 
526*795d594fSAndroid Build Coastguard Worker   // 7. Return from the JNI stub.
527*795d594fSAndroid Build Coastguard Worker 
528*795d594fSAndroid Build Coastguard Worker   // 7.1. Move frame up now we're done with the out arg space.
529*795d594fSAndroid Build Coastguard Worker   //      @CriticalNative remove out args together with the frame in RemoveFrame().
530*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
531*795d594fSAndroid Build Coastguard Worker     __ DecreaseFrameSize(current_out_arg_size);
532*795d594fSAndroid Build Coastguard Worker     current_frame_size -= current_out_arg_size;
533*795d594fSAndroid Build Coastguard Worker   }
534*795d594fSAndroid Build Coastguard Worker 
535*795d594fSAndroid Build Coastguard Worker   // 7.2 Unlock the synchronization object for synchronized methods.
536*795d594fSAndroid Build Coastguard Worker   //     Do this before exception poll to avoid extra unlocking in the exception slow path.
537*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_synchronized)) {
538*795d594fSAndroid Build Coastguard Worker     ManagedRegister to_lock = main_jni_conv->LockingArgumentRegister();
539*795d594fSAndroid Build Coastguard Worker     mr_conv->ResetIterator(FrameOffset(current_frame_size));
540*795d594fSAndroid Build Coastguard Worker     if (is_static) {
541*795d594fSAndroid Build Coastguard Worker       // Pass the declaring class.
542*795d594fSAndroid Build Coastguard Worker       DCHECK(method_register.IsNoRegister());  // TODO: Preserve the method in `callee_save_temp`.
543*795d594fSAndroid Build Coastguard Worker       ManagedRegister temp = __ CoreRegisterWithSize(callee_save_temp, kRawPointerSize);
544*795d594fSAndroid Build Coastguard Worker       FrameOffset method_offset = mr_conv->MethodStackOffset();
545*795d594fSAndroid Build Coastguard Worker       __ Load(temp, method_offset, kRawPointerSize);
546*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(ArtMethod::DeclaringClassOffset().SizeValue(), 0u);
547*795d594fSAndroid Build Coastguard Worker       __ LoadGcRootWithoutReadBarrier(to_lock, temp, MemberOffset(0u));
548*795d594fSAndroid Build Coastguard Worker     } else {
549*795d594fSAndroid Build Coastguard Worker       // Pass the `this` argument from its spill slot.
550*795d594fSAndroid Build Coastguard Worker       __ LoadStackReference(to_lock, mr_conv->CurrentParamStackOffset());
551*795d594fSAndroid Build Coastguard Worker     }
552*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniUnlockObject));
553*795d594fSAndroid Build Coastguard Worker   }
554*795d594fSAndroid Build Coastguard Worker 
555*795d594fSAndroid Build Coastguard Worker   // 7.3. Process pending exceptions from JNI call or monitor exit.
556*795d594fSAndroid Build Coastguard Worker   //      @CriticalNative methods do not need exception poll in the stub.
557*795d594fSAndroid Build Coastguard Worker   //      Methods with reference return emit the exception poll earlier.
558*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native) && !reference_return) {
559*795d594fSAndroid Build Coastguard Worker     __ ExceptionPoll(exception_slow_path.get());
560*795d594fSAndroid Build Coastguard Worker   }
561*795d594fSAndroid Build Coastguard Worker 
562*795d594fSAndroid Build Coastguard Worker   // 7.4. For @FastNative, we never transitioned out of runnable, so there is no transition back.
563*795d594fSAndroid Build Coastguard Worker   //      Perform a suspend check if there is a flag raised, unless we have done that above
564*795d594fSAndroid Build Coastguard Worker   //      for reference return.
565*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_fast_native) && !reference_return) {
566*795d594fSAndroid Build Coastguard Worker     __ SuspendCheck(suspend_check_slow_path.get());
567*795d594fSAndroid Build Coastguard Worker     __ Bind(suspend_check_resume.get());
568*795d594fSAndroid Build Coastguard Worker   }
569*795d594fSAndroid Build Coastguard Worker 
570*795d594fSAndroid Build Coastguard Worker   // 7.5. Check if method exit hooks needs to be called
571*795d594fSAndroid Build Coastguard Worker   // For critical native methods, we don't JIT stubs in debuggable runtimes.
572*795d594fSAndroid Build Coastguard Worker   // TODO(mythria): Add support to call method entry / exit hooks for critical native methods too.
573*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> method_exit_hook_slow_path;
574*795d594fSAndroid Build Coastguard Worker   std::unique_ptr<JNIMacroLabel> method_exit_hook_return;
575*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(needs_entry_exit_hooks)) {
576*795d594fSAndroid Build Coastguard Worker     uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
577*795d594fSAndroid Build Coastguard Worker     int offset = instrumentation::Instrumentation::RunExitHooksOffset().Int32Value();
578*795d594fSAndroid Build Coastguard Worker     method_exit_hook_slow_path = __ CreateLabel();
579*795d594fSAndroid Build Coastguard Worker     method_exit_hook_return = __ CreateLabel();
580*795d594fSAndroid Build Coastguard Worker     __ TestByteAndJumpIfNotZero(address + offset, method_exit_hook_slow_path.get());
581*795d594fSAndroid Build Coastguard Worker     __ Bind(method_exit_hook_return.get());
582*795d594fSAndroid Build Coastguard Worker   }
583*795d594fSAndroid Build Coastguard Worker 
584*795d594fSAndroid Build Coastguard Worker   // 7.6. Remove activation - need to restore callee save registers since the GC
585*795d594fSAndroid Build Coastguard Worker   //      may have changed them.
586*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
587*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native) || !main_jni_conv->UseTailCall()) {
588*795d594fSAndroid Build Coastguard Worker     // We expect the compiled method to possibly be suspended during its
589*795d594fSAndroid Build Coastguard Worker     // execution, except in the case of a CriticalNative method.
590*795d594fSAndroid Build Coastguard Worker     bool may_suspend = !is_critical_native;
591*795d594fSAndroid Build Coastguard Worker     __ RemoveFrame(current_frame_size, callee_save_regs, may_suspend);
592*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
593*795d594fSAndroid Build Coastguard Worker   }
594*795d594fSAndroid Build Coastguard Worker 
595*795d594fSAndroid Build Coastguard Worker   // 8. Emit slow paths.
596*795d594fSAndroid Build Coastguard Worker 
597*795d594fSAndroid Build Coastguard Worker   // 8.1. Read barrier slow path for the declaring class in the method for a static call.
598*795d594fSAndroid Build Coastguard Worker   //      Skip this for @CriticalNative because we're not passing a `jclass` to the native method.
599*795d594fSAndroid Build Coastguard Worker   if (emit_read_barrier && is_static && !is_critical_native) {
600*795d594fSAndroid Build Coastguard Worker     __ Bind(jclass_read_barrier_slow_path.get());
601*795d594fSAndroid Build Coastguard Worker 
602*795d594fSAndroid Build Coastguard Worker     // Construct slow path for read barrier:
603*795d594fSAndroid Build Coastguard Worker     //
604*795d594fSAndroid Build Coastguard Worker     // For baker read barrier, do a fast check whether the class is already marked.
605*795d594fSAndroid Build Coastguard Worker     //
606*795d594fSAndroid Build Coastguard Worker     // Call into the runtime's `art_jni_read_barrier` and have it fix up
607*795d594fSAndroid Build Coastguard Worker     // the class address if it was moved.
608*795d594fSAndroid Build Coastguard Worker     //
609*795d594fSAndroid Build Coastguard Worker     // The entrypoint preserves the method register and argument registers.
610*795d594fSAndroid Build Coastguard Worker 
611*795d594fSAndroid Build Coastguard Worker     if (kUseBakerReadBarrier) {
612*795d594fSAndroid Build Coastguard Worker       // We enter the slow path with the method register unclobbered and callee-save
613*795d594fSAndroid Build Coastguard Worker       // registers already spilled, so we can use callee-save scratch registers.
614*795d594fSAndroid Build Coastguard Worker       method_register = mr_conv->MethodRegister();
615*795d594fSAndroid Build Coastguard Worker       ManagedRegister temp = __ CoreRegisterWithSize(
616*795d594fSAndroid Build Coastguard Worker           main_jni_conv->CalleeSaveScratchRegisters()[0], kObjectReferenceSize);
617*795d594fSAndroid Build Coastguard Worker       // Load the declaring class reference.
618*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(ArtMethod::DeclaringClassOffset().SizeValue(), 0u);
619*795d594fSAndroid Build Coastguard Worker       __ LoadGcRootWithoutReadBarrier(temp, method_register, MemberOffset(0u));
620*795d594fSAndroid Build Coastguard Worker       // Return to main path if the class object is marked.
621*795d594fSAndroid Build Coastguard Worker       __ TestMarkBit(temp, jclass_read_barrier_return.get(), JNIMacroUnaryCondition::kNotZero);
622*795d594fSAndroid Build Coastguard Worker     }
623*795d594fSAndroid Build Coastguard Worker 
624*795d594fSAndroid Build Coastguard Worker     ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
625*795d594fSAndroid Build Coastguard Worker                                                                       pJniReadBarrier);
626*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(read_barrier);
627*795d594fSAndroid Build Coastguard Worker 
628*795d594fSAndroid Build Coastguard Worker     // Return to main path.
629*795d594fSAndroid Build Coastguard Worker     __ Jump(jclass_read_barrier_return.get());
630*795d594fSAndroid Build Coastguard Worker   }
631*795d594fSAndroid Build Coastguard Worker 
632*795d594fSAndroid Build Coastguard Worker   // 8.2. Slow path for transition to Native.
633*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native && !is_fast_native)) {
634*795d594fSAndroid Build Coastguard Worker     __ Bind(transition_to_native_slow_path.get());
635*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
636*795d594fSAndroid Build Coastguard Worker     __ Jump(transition_to_native_resume.get());
637*795d594fSAndroid Build Coastguard Worker   }
638*795d594fSAndroid Build Coastguard Worker 
639*795d594fSAndroid Build Coastguard Worker   // 8.3. Slow path for transition to Runnable.
640*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native && !is_fast_native)) {
641*795d594fSAndroid Build Coastguard Worker     __ Bind(transition_to_runnable_slow_path.get());
642*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
643*795d594fSAndroid Build Coastguard Worker     __ Jump(transition_to_runnable_resume.get());
644*795d594fSAndroid Build Coastguard Worker   }
645*795d594fSAndroid Build Coastguard Worker 
646*795d594fSAndroid Build Coastguard Worker   // 8.4. Exception poll slow path(s).
647*795d594fSAndroid Build Coastguard Worker   if (LIKELY(!is_critical_native)) {
648*795d594fSAndroid Build Coastguard Worker     __ Bind(exception_slow_path.get());
649*795d594fSAndroid Build Coastguard Worker     if (reference_return) {
650*795d594fSAndroid Build Coastguard Worker       // We performed the exception check early, so we need to adjust SP and pop IRT frame.
651*795d594fSAndroid Build Coastguard Worker       if (main_out_arg_size != 0) {
652*795d594fSAndroid Build Coastguard Worker         jni_asm->cfi().AdjustCFAOffset(main_out_arg_size);
653*795d594fSAndroid Build Coastguard Worker         __ DecreaseFrameSize(main_out_arg_size);
654*795d594fSAndroid Build Coastguard Worker       }
655*795d594fSAndroid Build Coastguard Worker       __ StoreLocalReferenceTableStates(jni_env_reg, previous_state_reg, current_state_reg);
656*795d594fSAndroid Build Coastguard Worker     }
657*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(current_frame_size));
658*795d594fSAndroid Build Coastguard Worker     __ DeliverPendingException();
659*795d594fSAndroid Build Coastguard Worker   }
660*795d594fSAndroid Build Coastguard Worker 
661*795d594fSAndroid Build Coastguard Worker   // 8.5 Slow path for decoding the `jobject`.
662*795d594fSAndroid Build Coastguard Worker   if (reference_return && inline_decode_reference) {
663*795d594fSAndroid Build Coastguard Worker     __ Bind(decode_reference_slow_path.get());
664*795d594fSAndroid Build Coastguard Worker     if (main_out_arg_size != 0) {
665*795d594fSAndroid Build Coastguard Worker       jni_asm->cfi().AdjustCFAOffset(main_out_arg_size);
666*795d594fSAndroid Build Coastguard Worker     }
667*795d594fSAndroid Build Coastguard Worker     CallDecodeReferenceResult<kPointerSize>(
668*795d594fSAndroid Build Coastguard Worker         jni_asm.get(), main_jni_conv.get(), mr_conv->ReturnRegister(), main_out_arg_size);
669*795d594fSAndroid Build Coastguard Worker     __ Jump(decode_reference_resume.get());
670*795d594fSAndroid Build Coastguard Worker     if (main_out_arg_size != 0) {
671*795d594fSAndroid Build Coastguard Worker       jni_asm->cfi().AdjustCFAOffset(-main_out_arg_size);
672*795d594fSAndroid Build Coastguard Worker     }
673*795d594fSAndroid Build Coastguard Worker   }
674*795d594fSAndroid Build Coastguard Worker 
675*795d594fSAndroid Build Coastguard Worker   // 8.6. Suspend check slow path.
676*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(is_fast_native)) {
677*795d594fSAndroid Build Coastguard Worker     __ Bind(suspend_check_slow_path.get());
678*795d594fSAndroid Build Coastguard Worker     if (reference_return && main_out_arg_size != 0) {
679*795d594fSAndroid Build Coastguard Worker       jni_asm->cfi().AdjustCFAOffset(main_out_arg_size);
680*795d594fSAndroid Build Coastguard Worker       __ DecreaseFrameSize(main_out_arg_size);
681*795d594fSAndroid Build Coastguard Worker     }
682*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pTestSuspend));
683*795d594fSAndroid Build Coastguard Worker     if (reference_return) {
684*795d594fSAndroid Build Coastguard Worker       // Suspend check entry point overwrites top of managed stack and leaves it clobbered.
685*795d594fSAndroid Build Coastguard Worker       // We need to restore the top for subsequent runtime call to `JniDecodeReferenceResult()`.
686*795d594fSAndroid Build Coastguard Worker       __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>(), should_tag_sp);
687*795d594fSAndroid Build Coastguard Worker     }
688*795d594fSAndroid Build Coastguard Worker     if (reference_return && main_out_arg_size != 0) {
689*795d594fSAndroid Build Coastguard Worker       __ IncreaseFrameSize(main_out_arg_size);
690*795d594fSAndroid Build Coastguard Worker     }
691*795d594fSAndroid Build Coastguard Worker     __ Jump(suspend_check_resume.get());
692*795d594fSAndroid Build Coastguard Worker     if (reference_return && main_out_arg_size != 0) {
693*795d594fSAndroid Build Coastguard Worker       jni_asm->cfi().AdjustCFAOffset(-main_out_arg_size);
694*795d594fSAndroid Build Coastguard Worker     }
695*795d594fSAndroid Build Coastguard Worker   }
696*795d594fSAndroid Build Coastguard Worker 
697*795d594fSAndroid Build Coastguard Worker   // 8.7. Method entry / exit hooks slow paths.
698*795d594fSAndroid Build Coastguard Worker   if (UNLIKELY(needs_entry_exit_hooks)) {
699*795d594fSAndroid Build Coastguard Worker     __ Bind(method_entry_hook_slow_path.get());
700*795d594fSAndroid Build Coastguard Worker     // Use Jni specific method entry hook that saves all the arguments. We have only saved the
701*795d594fSAndroid Build Coastguard Worker     // callee save registers at this point. So go through Jni specific stub that saves the rest
702*795d594fSAndroid Build Coastguard Worker     // of the live registers.
703*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEntryHook));
704*795d594fSAndroid Build Coastguard Worker     __ ExceptionPoll(exception_slow_path.get());
705*795d594fSAndroid Build Coastguard Worker     __ Jump(method_entry_hook_return.get());
706*795d594fSAndroid Build Coastguard Worker 
707*795d594fSAndroid Build Coastguard Worker     __ Bind(method_exit_hook_slow_path.get());
708*795d594fSAndroid Build Coastguard Worker     // Method exit hooks is called just before tearing down the frame. So there are no live
709*795d594fSAndroid Build Coastguard Worker     // registers and we can directly call the method exit hook and don't need a Jni specific
710*795d594fSAndroid Build Coastguard Worker     // entrypoint.
711*795d594fSAndroid Build Coastguard Worker     __ Move(mr_conv->ArgumentRegisterForMethodExitHook(), managed_frame_size);
712*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(QUICK_ENTRYPOINT_OFFSET(kPointerSize, pMethodExitHook));
713*795d594fSAndroid Build Coastguard Worker     __ Jump(method_exit_hook_return.get());
714*795d594fSAndroid Build Coastguard Worker   }
715*795d594fSAndroid Build Coastguard Worker 
716*795d594fSAndroid Build Coastguard Worker   // 9. Finalize code generation.
717*795d594fSAndroid Build Coastguard Worker   __ FinalizeCode();
718*795d594fSAndroid Build Coastguard Worker   size_t cs = __ CodeSize();
719*795d594fSAndroid Build Coastguard Worker   std::vector<uint8_t> managed_code(cs);
720*795d594fSAndroid Build Coastguard Worker   MemoryRegion code(&managed_code[0], managed_code.size());
721*795d594fSAndroid Build Coastguard Worker   __ CopyInstructions(code);
722*795d594fSAndroid Build Coastguard Worker 
723*795d594fSAndroid Build Coastguard Worker   return JniCompiledMethod(instruction_set,
724*795d594fSAndroid Build Coastguard Worker                            std::move(managed_code),
725*795d594fSAndroid Build Coastguard Worker                            managed_frame_size,
726*795d594fSAndroid Build Coastguard Worker                            main_jni_conv->CoreSpillMask(),
727*795d594fSAndroid Build Coastguard Worker                            main_jni_conv->FpSpillMask(),
728*795d594fSAndroid Build Coastguard Worker                            ArrayRef<const uint8_t>(*jni_asm->cfi().data()));
729*795d594fSAndroid Build Coastguard Worker }
730*795d594fSAndroid Build Coastguard Worker 
731*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
SetNativeParameter(JNIMacroAssembler<kPointerSize> * jni_asm,JniCallingConvention * jni_conv,ManagedRegister in_reg)732*795d594fSAndroid Build Coastguard Worker static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
733*795d594fSAndroid Build Coastguard Worker                                JniCallingConvention* jni_conv,
734*795d594fSAndroid Build Coastguard Worker                                ManagedRegister in_reg) {
735*795d594fSAndroid Build Coastguard Worker   if (jni_conv->IsCurrentParamOnStack()) {
736*795d594fSAndroid Build Coastguard Worker     FrameOffset dest = jni_conv->CurrentParamStackOffset();
737*795d594fSAndroid Build Coastguard Worker     __ StoreRawPtr(dest, in_reg);
738*795d594fSAndroid Build Coastguard Worker   } else {
739*795d594fSAndroid Build Coastguard Worker     if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
740*795d594fSAndroid Build Coastguard Worker       __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
741*795d594fSAndroid Build Coastguard Worker     }
742*795d594fSAndroid Build Coastguard Worker   }
743*795d594fSAndroid Build Coastguard Worker }
744*795d594fSAndroid Build Coastguard Worker 
745*795d594fSAndroid Build Coastguard Worker template <PointerSize kPointerSize>
CallDecodeReferenceResult(JNIMacroAssembler<kPointerSize> * jni_asm,JniCallingConvention * jni_conv,ManagedRegister mr_return_reg,size_t main_out_arg_size)746*795d594fSAndroid Build Coastguard Worker static void CallDecodeReferenceResult(JNIMacroAssembler<kPointerSize>* jni_asm,
747*795d594fSAndroid Build Coastguard Worker                                       JniCallingConvention* jni_conv,
748*795d594fSAndroid Build Coastguard Worker                                       ManagedRegister mr_return_reg,
749*795d594fSAndroid Build Coastguard Worker                                       size_t main_out_arg_size) {
750*795d594fSAndroid Build Coastguard Worker   // We abuse the JNI calling convention here, that is guaranteed to support passing
751*795d594fSAndroid Build Coastguard Worker   // two pointer arguments, `JNIEnv*` and `jclass`/`jobject`.
752*795d594fSAndroid Build Coastguard Worker   jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
753*795d594fSAndroid Build Coastguard Worker   ThreadOffset<kPointerSize> jni_decode_reference_result =
754*795d594fSAndroid Build Coastguard Worker       QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniDecodeReferenceResult);
755*795d594fSAndroid Build Coastguard Worker   // Pass result.
756*795d594fSAndroid Build Coastguard Worker   SetNativeParameter(jni_asm, jni_conv, mr_return_reg);
757*795d594fSAndroid Build Coastguard Worker   jni_conv->Next();
758*795d594fSAndroid Build Coastguard Worker   if (jni_conv->IsCurrentParamInRegister()) {
759*795d594fSAndroid Build Coastguard Worker     __ GetCurrentThread(jni_conv->CurrentParamRegister());
760*795d594fSAndroid Build Coastguard Worker     __ Call(jni_conv->CurrentParamRegister(), Offset(jni_decode_reference_result));
761*795d594fSAndroid Build Coastguard Worker   } else {
762*795d594fSAndroid Build Coastguard Worker     __ GetCurrentThread(jni_conv->CurrentParamStackOffset());
763*795d594fSAndroid Build Coastguard Worker     __ CallFromThread(jni_decode_reference_result);
764*795d594fSAndroid Build Coastguard Worker   }
765*795d594fSAndroid Build Coastguard Worker   // Note: If the native ABI returns the pointer in a register different from
766*795d594fSAndroid Build Coastguard Worker   // `mr_return_register`, the `JniDecodeReferenceResult` entrypoint must be
767*795d594fSAndroid Build Coastguard Worker   // a stub that moves the result to `mr_return_register`.
768*795d594fSAndroid Build Coastguard Worker }
769*795d594fSAndroid Build Coastguard Worker 
ArtQuickJniCompileMethod(const CompilerOptions & compiler_options,std::string_view shorty,uint32_t access_flags,ArenaAllocator * allocator)770*795d594fSAndroid Build Coastguard Worker JniCompiledMethod ArtQuickJniCompileMethod(const CompilerOptions& compiler_options,
771*795d594fSAndroid Build Coastguard Worker                                            std::string_view shorty,
772*795d594fSAndroid Build Coastguard Worker                                            uint32_t access_flags,
773*795d594fSAndroid Build Coastguard Worker                                            ArenaAllocator* allocator) {
774*795d594fSAndroid Build Coastguard Worker   if (Is64BitInstructionSet(compiler_options.GetInstructionSet())) {
775*795d594fSAndroid Build Coastguard Worker     return ArtJniCompileMethodInternal<PointerSize::k64>(
776*795d594fSAndroid Build Coastguard Worker         compiler_options, shorty, access_flags, allocator);
777*795d594fSAndroid Build Coastguard Worker   } else {
778*795d594fSAndroid Build Coastguard Worker     return ArtJniCompileMethodInternal<PointerSize::k32>(
779*795d594fSAndroid Build Coastguard Worker         compiler_options, shorty, access_flags, allocator);
780*795d594fSAndroid Build Coastguard Worker   }
781*795d594fSAndroid Build Coastguard Worker }
782*795d594fSAndroid Build Coastguard Worker 
783*795d594fSAndroid Build Coastguard Worker }  // namespace art
784