blob: 13d8c166cc64b5a434d028dff14bb440ecab9f8f [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "jni_compiler.h"
18
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <algorithm>
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070020#include <ios>
Ian Rogers700a4022014-05-19 16:49:03 -070021#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070023#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010026#include "base/arena_allocator.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "base/logging.h"
29#include "base/macros.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070030#include "memory_region.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070031#include "calling_convention.h"
32#include "class_linker.h"
33#include "compiled_method.h"
34#include "dex_file-inl.h"
35#include "driver/compiler_driver.h"
David Srbeckyc6b4dd82015-04-07 20:32:43 +010036#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070037#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070038#include "jni_env_ext.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070039#include "debug/dwarf/debug_frame_opcode_writer.h"
Ian Rogers166db042013-07-26 12:05:57 -070040#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070041#include "utils/jni_macro_assembler.h"
Ian Rogers166db042013-07-26 12:05:57 -070042#include "utils/managed_register.h"
43#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010044#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070045#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010046#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070047#include "utils/x86/managed_register_x86.h"
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070048#include "utils.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070049#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
51#define __ jni_asm->
52
53namespace art {
54
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070055using JniOptimizationFlags = Compiler::JniOptimizationFlags;
56
Andreas Gampe3b165bc2016-08-01 22:07:04 -070057template <PointerSize kPointerSize>
58static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 ManagedRuntimeCallingConvention* mr_conv,
60 JniCallingConvention* jni_conv,
61 size_t frame_size, size_t out_arg_size);
Andreas Gampe3b165bc2016-08-01 22:07:04 -070062template <PointerSize kPointerSize>
63static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 JniCallingConvention* jni_conv,
65 ManagedRegister in_reg);
66
Andreas Gampe3b165bc2016-08-01 22:07:04 -070067template <PointerSize kPointerSize>
68static std::unique_ptr<JNIMacroAssembler<kPointerSize>> GetMacroAssembler(
69 ArenaAllocator* arena, InstructionSet isa, const InstructionSetFeatures* features) {
70 return JNIMacroAssembler<kPointerSize>::Create(arena, isa, features);
71}
72
Brian Carlstrom7940e442013-07-12 13:46:57 -070073// Generate the JNI bridge for the given method, general contract:
74// - Arguments are in the managed runtime format, either on stack or in
75// registers, a reference to the method object is supplied as part of this
76// convention.
77//
Andreas Gampe3b165bc2016-08-01 22:07:04 -070078template <PointerSize kPointerSize>
79static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
80 uint32_t access_flags,
81 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070082 const DexFile& dex_file,
83 JniOptimizationFlags optimization_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 const bool is_native = (access_flags & kAccNative) != 0;
85 CHECK(is_native);
86 const bool is_static = (access_flags & kAccStatic) != 0;
87 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
88 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070089 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +020090 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Vladimir Marko93205e32016-04-13 11:59:46 +010091
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070092 // i.e. if the method was annotated with @FastNative
Igor Murashkin367f3dd2016-09-01 17:00:24 -070093 const bool is_fast_native = (optimization_flags == Compiler::kFastNative);
94
95 // i.e. if the method was annotated with @CriticalNative
96 bool is_critical_native = (optimization_flags == Compiler::kCriticalNative);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070097
98 VLOG(jni) << "JniCompile: Method :: "
99 << art::PrettyMethod(method_idx, dex_file, /* with signature */ true)
100 << " :: access_flags = " << std::hex << access_flags << std::dec;
101
102 if (UNLIKELY(is_fast_native)) {
103 VLOG(jni) << "JniCompile: Fast native method detected :: "
104 << art::PrettyMethod(method_idx, dex_file, /* with signature */ true);
105 }
106
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700107 if (UNLIKELY(is_critical_native)) {
108 VLOG(jni) << "JniCompile: Critical native method detected :: "
109 << art::PrettyMethod(method_idx, dex_file, /* with signature */ true);
110 }
111
112 if (kIsDebugBuild) {
113 // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive.
114 if (UNLIKELY(is_fast_native && is_critical_native)) {
115 LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative"
116 << art::PrettyMethod(method_idx, dex_file, /* with_signature */ true);
117 }
118
119 // @CriticalNative - extra checks:
120 // -- Don't allow virtual criticals
121 // -- Don't allow synchronized criticals
122 // -- Don't allow any objects as parameter or return value
123 if (UNLIKELY(is_critical_native)) {
124 CHECK(is_static)
125 << "@CriticalNative functions cannot be virtual since that would"
126 << "require passing a reference parameter (this), which is illegal "
127 << art::PrettyMethod(method_idx, dex_file, /* with_signature */ true);
128 CHECK(!is_synchronized)
129 << "@CriticalNative functions cannot be synchronized since that would"
130 << "require passing a (class and/or this) reference parameter, which is illegal "
131 << art::PrettyMethod(method_idx, dex_file, /* with_signature */ true);
132 for (size_t i = 0; i < strlen(shorty); ++i) {
133 CHECK_NE(Primitive::kPrimNot, Primitive::GetType(shorty[i]))
134 << "@CriticalNative methods' shorty types must not have illegal references "
135 << art::PrettyMethod(method_idx, dex_file, /* with_signature */ true);
136 }
137 }
138 }
139
Vladimir Marko93205e32016-04-13 11:59:46 +0100140 ArenaPool pool;
141 ArenaAllocator arena(&pool);
142
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 // Calling conventions used to iterate over parameters to method
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700144 std::unique_ptr<JniCallingConvention> main_jni_conv =
145 JniCallingConvention::Create(&arena,
146 is_static,
147 is_synchronized,
148 is_critical_native,
149 shorty,
150 instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 bool reference_return = main_jni_conv->IsReturnAReference();
152
Ian Rogers700a4022014-05-19 16:49:03 -0700153 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +0100154 ManagedRuntimeCallingConvention::Create(
155 &arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156
157 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
158 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100159 const char* jni_end_shorty;
160 if (reference_return && is_synchronized) {
161 jni_end_shorty = "ILL";
162 } else if (reference_return) {
163 jni_end_shorty = "IL";
164 } else if (is_synchronized) {
165 jni_end_shorty = "VL";
166 } else {
167 jni_end_shorty = "V";
168 }
169
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700170 std::unique_ptr<JniCallingConvention> end_jni_conv(
171 JniCallingConvention::Create(&arena,
172 is_static,
173 is_synchronized,
174 is_critical_native,
175 jni_end_shorty,
176 instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 // Assembler that holds generated instructions
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700179 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
180 GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features);
David Srbecky5b1c2ca2016-01-25 17:32:41 +0000181 jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GenerateAnyDebugInfo());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182
183 // Offsets into data structures
184 // TODO: if cross compiling these offsets are for the host not the target
185 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
186 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
187 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
188
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700189 // 1. Build the frame saving all callee saves, Method*, and PC return address.
190 const size_t frame_size(main_jni_conv->FrameSize()); // Excludes outgoing args.
Vladimir Marko32248382016-05-19 10:37:24 +0100191 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100193 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700194
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700195 if (LIKELY(!is_critical_native)) {
196 // NOTE: @CriticalNative methods don't have a HandleScope
197 // because they can't have any reference parameters or return values.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100198
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700199 // 2. Set up the HandleScope
200 mr_conv->ResetIterator(FrameOffset(frame_size));
201 main_jni_conv->ResetIterator(FrameOffset(0));
202 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
203 main_jni_conv->ReferenceCount(),
204 mr_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700206 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
207 Thread::TopHandleScopeOffset<kPointerSize>(),
208 mr_conv->InterproceduralScratchRegister());
209 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
210 main_jni_conv->HandleScopeOffset(),
211 mr_conv->InterproceduralScratchRegister());
212
213 // 3. Place incoming reference arguments into handle scope
214 main_jni_conv->Next(); // Skip JNIEnv*
215 // 3.5. Create Class argument for static methods out of passed method
216 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700217 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700218 // Check handle scope offset is within frame
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700219 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700220 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
221 // Note this LoadRef() does not include read barrier. It will be handled below.
222 //
223 // scratchRegister = *method[DeclaringClassOffset()];
224 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
225 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
226 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
227 // *handleScopeOffset = scratchRegister
228 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
229 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700231 // Place every reference into the handle scope (ignore other parameters).
232 while (mr_conv->HasNext()) {
233 CHECK(main_jni_conv->HasNext());
234 bool ref_param = main_jni_conv->IsCurrentParamAReference();
235 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
236 // References need placing in handle scope and the entry value passing
237 if (ref_param) {
238 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
239 // must be null.
240 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
241 // Check handle scope offset is within frame and doesn't run into the saved segment state.
242 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
243 CHECK_NE(handle_scope_offset.Uint32Value(),
244 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
245 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
246 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
247 CHECK(input_in_reg || input_on_stack);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700249 if (input_in_reg) {
250 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
251 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
252 __ StoreRef(handle_scope_offset, in_reg);
253 } else if (input_on_stack) {
254 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
255 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
256 __ CopyRef(handle_scope_offset, in_off,
257 mr_conv->InterproceduralScratchRegister());
258 }
259 }
260 mr_conv->Next();
261 main_jni_conv->Next();
262 }
263
264 // 4. Write out the end of the quick frames.
265 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
266
267 // NOTE: @CriticalNative does not need to store the stack pointer to the thread
268 // because garbage collections are disabled within the execution of a
269 // @CriticalNative method.
270 // (TODO: We could probably disable it for @FastNative too).
271 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700272
273 // 5. Move frame down to allow space for out going args.
274 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100275 size_t current_out_arg_size = main_out_arg_size;
276 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700278 // Call the read barrier for the declaring class loaded from the method for a static call.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700279 // Skip this for @CriticalNative because we didn't build a HandleScope to begin with.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700280 // Note that we always have outgoing param space available for at least two params.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700281 if (kUseReadBarrier && is_static && !is_critical_native) {
Igor Murashkinae7ff922016-10-06 14:59:19 -0700282 const bool kReadBarrierFastPath =
283 (instruction_set != kMips) && (instruction_set != kMips64);
284 std::unique_ptr<JNIMacroLabel> skip_cold_path_label;
285 if (kReadBarrierFastPath) {
286 skip_cold_path_label = __ CreateLabel();
287 // Fast path for supported targets.
288 //
289 // Check if gc_is_marking is set -- if it's not, we don't need
290 // a read barrier so skip it.
291 __ LoadFromThread(main_jni_conv->InterproceduralScratchRegister(),
292 Thread::IsGcMarkingOffset<kPointerSize>(),
293 Thread::IsGcMarkingSize());
294 // Jump over the slow path if gc is marking is false.
295 __ Jump(skip_cold_path_label.get(),
296 JNIMacroUnaryCondition::kZero,
297 main_jni_conv->InterproceduralScratchRegister());
298 }
299
300 // Construct slow path for read barrier:
301 //
302 // Call into the runtime's ReadBarrierJni and have it fix up
303 // the object address if it was moved.
304
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700305 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
306 pReadBarrierJni);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700307 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
308 main_jni_conv->Next(); // Skip JNIEnv.
309 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
310 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
311 // Pass the handle for the class as the first argument.
312 if (main_jni_conv->IsCurrentParamOnStack()) {
313 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
314 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
315 mr_conv->InterproceduralScratchRegister(),
316 false);
317 } else {
318 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
319 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
320 ManagedRegister::NoRegister(), false);
321 }
322 main_jni_conv->Next();
323 // Pass the current thread as the second argument and call.
324 if (main_jni_conv->IsCurrentParamInRegister()) {
325 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700326 __ Call(main_jni_conv->CurrentParamRegister(),
327 Offset(read_barrier),
328 main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700329 } else {
330 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
331 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700332 __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700333 }
334 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700335
336 if (kReadBarrierFastPath) {
337 __ Bind(skip_cold_path_label.get());
338 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700339 }
340
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
342 // can occur. The result is the saved JNI local state that is restored by the exit call. We
343 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
344 // arguments.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700345 FrameOffset locked_object_handle_scope_offset(0xBEEFDEAD);
346 if (LIKELY(!is_critical_native)) {
347 // Skip this for @CriticalNative methods. They do not call JniMethodStart.
348 ThreadOffset<kPointerSize> jni_start =
349 is_synchronized
350 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
Igor Murashkin6f029d92016-10-12 13:33:43 -0700351 : ((is_fast_native && !reference_return) // TODO: support @FastNative returning obj
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700352 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart)
353 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700354
Brian Carlstrom7940e442013-07-12 13:46:57 -0700355 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700356 locked_object_handle_scope_offset = FrameOffset(0);
357 if (is_synchronized) {
358 // Pass object for locking.
359 main_jni_conv->Next(); // Skip JNIEnv.
360 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
361 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
362 if (main_jni_conv->IsCurrentParamOnStack()) {
363 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
364 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
365 mr_conv->InterproceduralScratchRegister(), false);
366 } else {
367 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
368 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
369 ManagedRegister::NoRegister(), false);
370 }
371 main_jni_conv->Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700372 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700373 if (main_jni_conv->IsCurrentParamInRegister()) {
374 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
375 __ Call(main_jni_conv->CurrentParamRegister(),
376 Offset(jni_start),
377 main_jni_conv->InterproceduralScratchRegister());
378 } else {
379 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
380 main_jni_conv->InterproceduralScratchRegister());
381 __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister());
382 }
383 if (is_synchronized) { // Check for exceptions from monitor enter.
384 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700387
388 // Store into stack_frame[saved_cookie_offset] the return value of JniMethodStart.
389 FrameOffset saved_cookie_offset(
390 FrameOffset(0xDEADBEEFu)); // @CriticalNative - use obviously bad value for debugging
391 if (LIKELY(!is_critical_native)) {
392 saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
393 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4 /* sizeof cookie */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700394 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700395
396 // 7. Iterate over arguments placing values from managed calling convention in
397 // to the convention required for a native call (shuffling). For references
398 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700399 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700400 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700401 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100402 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700403 uint32_t args_count = 0;
404 while (mr_conv->HasNext()) {
405 args_count++;
406 mr_conv->Next();
407 }
408
409 // Do a backward pass over arguments, so that the generated code will be "mov
410 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
411 // TODO: A reverse iterator to improve readability.
412 for (uint32_t i = 0; i < args_count; ++i) {
413 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
414 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700415
416 // Skip the extra JNI parameters for now.
417 if (LIKELY(!is_critical_native)) {
418 main_jni_conv->Next(); // Skip JNIEnv*.
419 if (is_static) {
420 main_jni_conv->Next(); // Skip Class for now.
421 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700422 }
423 // Skip to the argument we're interested in.
424 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
425 mr_conv->Next();
426 main_jni_conv->Next();
427 }
428 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
429 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700430 if (is_static && !is_critical_native) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100432 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
434 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700435 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436 if (main_jni_conv->IsCurrentParamOnStack()) {
437 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700438 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 mr_conv->InterproceduralScratchRegister(),
440 false);
441 } else {
442 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 ManagedRegister::NoRegister(), false);
445 }
446 }
447
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700448 // Set the iterator back to the incoming Method*.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700450 if (LIKELY(!is_critical_native)) {
451 // 8. Create 1st argument, the JNI environment ptr.
452 // Register that will hold local indirect reference table
453 if (main_jni_conv->IsCurrentParamInRegister()) {
454 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
455 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
456 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
457 } else {
458 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
459 __ CopyRawPtrFromThread(jni_env,
460 Thread::JniEnvOffset<kPointerSize>(),
461 main_jni_conv->InterproceduralScratchRegister());
462 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 }
464
465 // 9. Plant call to native code associated with method.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700466 MemberOffset jni_entrypoint_offset =
467 ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set));
468 // FIXME: Not sure if MethodStackOffset will work here. What does it even do?
469 __ Call(main_jni_conv->MethodStackOffset(),
470 jni_entrypoint_offset,
471 // XX: Why not the jni conv scratch register?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 mr_conv->InterproceduralScratchRegister());
473
474 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700475 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700476 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
477 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
478 __ SignExtend(main_jni_conv->ReturnRegister(),
479 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
480 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
481 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
482 __ ZeroExtend(main_jni_conv->ReturnRegister(),
483 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
484 }
485 }
486
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700487 // 11. Process return value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700488 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
489 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700490 if (LIKELY(!is_critical_native)) {
491 // For normal JNI, store the return value on the stack because the call to
492 // JniMethodEnd will clobber the return value. It will be restored in (13).
493 if ((instruction_set == kMips || instruction_set == kMips64) &&
494 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
495 return_save_location.Uint32Value() % 8 != 0) {
496 // Ensure doubles are 8-byte aligned for MIPS
497 return_save_location = FrameOffset(return_save_location.Uint32Value()
498 + static_cast<size_t>(kMipsPointerSize));
499 // TODO: refactor this into the JniCallingConvention code
500 // as a return value alignment requirement.
501 }
502 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
503 __ Store(return_save_location,
504 main_jni_conv->ReturnRegister(),
505 main_jni_conv->SizeOfReturnValue());
506 } else {
507 // For @CriticalNative only,
508 // move the JNI return register into the managed return register (if they don't match).
509 ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister();
510 ManagedRegister mr_return_reg = mr_conv->ReturnRegister();
511
512 // Check if the JNI return register matches the managed return register.
513 // If they differ, only then do we have to do anything about it.
514 // Otherwise the return value is already in the right place when we return.
515 if (!jni_return_reg.Equals(mr_return_reg)) {
516 // This is typically only necessary on ARM32 due to native being softfloat
517 // while managed is hardfloat.
518 // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0.
519 __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue());
520 } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) {
521 // Sanity check: If the return value is passed on the stack for some reason,
522 // then make sure the size matches.
523 CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue());
524 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700525 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526 }
527
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100528 // Increase frame size for out args if needed by the end_jni_conv.
529 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
530 if (end_out_arg_size > current_out_arg_size) {
531 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
532 current_out_arg_size = end_out_arg_size;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700533 // TODO: This is redundant for @CriticalNative but we need to
534 // conditionally do __DecreaseFrameSize below.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100535 __ IncreaseFrameSize(out_arg_size_diff);
536 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
537 locked_object_handle_scope_offset =
538 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
539 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
540 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700541 // thread.
542 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700543
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700544 if (LIKELY(!is_critical_native)) {
545 // 12. Call JniMethodEnd
546 ThreadOffset<kPointerSize> jni_end(-1);
547 if (reference_return) {
548 // Pass result.
549 jni_end = is_synchronized
550 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
551 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference);
552 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
553 end_jni_conv->Next();
554 } else {
555 jni_end = is_synchronized
556 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
557 : (is_fast_native
558 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd)
559 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
560 }
561 // Pass saved local reference state.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700562 if (end_jni_conv->IsCurrentParamOnStack()) {
563 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700564 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700565 } else {
566 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700567 __ Load(out_reg, saved_cookie_offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700568 }
569 end_jni_conv->Next();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700570 if (is_synchronized) {
571 // Pass object for unlocking.
572 if (end_jni_conv->IsCurrentParamOnStack()) {
573 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
574 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
575 end_jni_conv->InterproceduralScratchRegister(),
576 false);
577 } else {
578 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
579 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
580 ManagedRegister::NoRegister(), false);
581 }
582 end_jni_conv->Next();
583 }
584 if (end_jni_conv->IsCurrentParamInRegister()) {
585 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
586 __ Call(end_jni_conv->CurrentParamRegister(),
587 Offset(jni_end),
588 end_jni_conv->InterproceduralScratchRegister());
589 } else {
590 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
591 end_jni_conv->InterproceduralScratchRegister());
592 __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister());
593 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700594
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700595 // 13. Reload return value
596 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
597 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
598 // NIT: If it's @CriticalNative then we actually only need to do this IF
599 // the calling convention's native return register doesn't match the managed convention's
600 // return register.
601 }
602 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700603
604 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100605 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606
607 // 15. Process pending exceptions from JNI call or monitor exit.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700608 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0 /* stack_adjust */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700610 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100612 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700613 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100614 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700615
616 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000617 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700618 size_t cs = __ CodeSize();
619 std::vector<uint8_t> managed_code(cs);
620 MemoryRegion code(&managed_code[0], managed_code.size());
621 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100622
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100623 return CompiledMethod::SwapAllocCompiledMethod(driver,
624 instruction_set,
625 ArrayRef<const uint8_t>(managed_code),
626 frame_size,
627 main_jni_conv->CoreSpillMask(),
628 main_jni_conv->FpSpillMask(),
Vladimir Marko35831e82015-09-11 11:59:18 +0100629 ArrayRef<const SrcMapElem>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100630 ArrayRef<const uint8_t>(), // vmap_table.
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100631 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
632 ArrayRef<const LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633}
634
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700635// Copy a single parameter from the managed to the JNI calling convention.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700636template <PointerSize kPointerSize>
637static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700638 ManagedRuntimeCallingConvention* mr_conv,
639 JniCallingConvention* jni_conv,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700640 size_t frame_size,
641 size_t out_arg_size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
643 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700644 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 bool null_allowed = false;
646 bool ref_param = jni_conv->IsCurrentParamAReference();
647 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
648 // input may be in register, on stack or both - but not none!
649 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
650 if (output_in_reg) { // output shouldn't straddle registers and stack
651 CHECK(!jni_conv->IsCurrentParamOnStack());
652 } else {
653 CHECK(jni_conv->IsCurrentParamOnStack());
654 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700655 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700656 if (ref_param) {
657 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700658 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
659 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700661 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
662 // Check handle scope offset is within frame.
663 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 }
665 if (input_in_reg && output_in_reg) {
666 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
667 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
668 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700669 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700670 } else {
671 if (!mr_conv->IsCurrentParamOnStack()) {
672 // regular non-straddling move
673 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
674 } else {
675 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
676 }
677 }
678 } else if (!input_in_reg && !output_in_reg) {
679 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
680 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700681 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 null_allowed);
683 } else {
684 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
685 size_t param_size = mr_conv->CurrentParamSize();
686 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
687 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
688 }
689 } else if (!input_in_reg && output_in_reg) {
690 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
691 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
692 // Check that incoming stack arguments are above the current stack frame.
693 CHECK_GT(in_off.Uint32Value(), frame_size);
694 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700695 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 } else {
697 size_t param_size = mr_conv->CurrentParamSize();
698 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
699 __ Load(out_reg, in_off, param_size);
700 }
701 } else {
702 CHECK(input_in_reg && !output_in_reg);
703 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
704 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
705 // Check outgoing argument is within frame
706 CHECK_LT(out_off.Uint32Value(), frame_size);
707 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700708 // TODO: recycle value in in_reg rather than reload from handle scope
709 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700710 null_allowed);
711 } else {
712 size_t param_size = mr_conv->CurrentParamSize();
713 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
714 if (!mr_conv->IsCurrentParamOnStack()) {
715 // regular non-straddling store
716 __ Store(out_off, in_reg, param_size);
717 } else {
718 // store where input straddles registers and stack
719 CHECK_EQ(param_size, 8u);
720 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
721 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
722 }
723 }
724 }
725}
726
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700727template <PointerSize kPointerSize>
728static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700729 JniCallingConvention* jni_conv,
730 ManagedRegister in_reg) {
731 if (jni_conv->IsCurrentParamOnStack()) {
732 FrameOffset dest = jni_conv->CurrentParamStackOffset();
733 __ StoreRawPtr(dest, in_reg);
734 } else {
735 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
736 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
737 }
738 }
739}
740
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700741CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler,
742 uint32_t access_flags,
743 uint32_t method_idx,
744 const DexFile& dex_file,
745 Compiler::JniOptimizationFlags optimization_flags) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700746 if (Is64BitInstructionSet(compiler->GetInstructionSet())) {
747 return ArtJniCompileMethodInternal<PointerSize::k64>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700748 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700749 } else {
750 return ArtJniCompileMethodInternal<PointerSize::k32>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700751 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700752 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700754
755} // namespace art