blob: c66a2a62eb86bf605fd1ec566e084331d9d55d23 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "jni_compiler.h"
18
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <algorithm>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <fstream>
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070021#include <ios>
Ian Rogers700a4022014-05-19 16:49:03 -070022#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include <vector>
24
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method.h"
Vladimir Marko93205e32016-04-13 11:59:46 +010026#include "base/arena_allocator.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070027#include "base/enums.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070028#include "base/logging.h"
29#include "base/macros.h"
30#include "calling_convention.h"
31#include "class_linker.h"
32#include "compiled_method.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "debug/dwarf/debug_frame_opcode_writer.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070034#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 Gampe8cf9cb32017-07-19 09:28:38 -070039#include "memory_region.h"
40#include "thread.h"
41#include "utils.h"
42#include "utils/arm/managed_register_arm.h"
43#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070044#include "utils/assembler.h"
Andreas Gampe3b165bc2016-08-01 22:07:04 -070045#include "utils/jni_macro_assembler.h"
Ian Rogers166db042013-07-26 12:05:57 -070046#include "utils/managed_register.h"
Ian Rogers166db042013-07-26 12:05:57 -070047#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010048#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070049#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
51#define __ jni_asm->
52
53namespace art {
54
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
Igor Murashkinaf1e2992016-10-12 17:44:50 -070073enum class JniEntrypoint {
74 kStart,
75 kEnd
76};
77
78template <PointerSize kPointerSize>
79static ThreadOffset<kPointerSize> GetJniEntrypointThreadOffset(JniEntrypoint which,
80 bool reference_return,
81 bool is_synchronized,
82 bool is_fast_native) {
83 if (which == JniEntrypoint::kStart) { // JniMethodStart
84 ThreadOffset<kPointerSize> jni_start =
85 is_synchronized
86 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized)
87 : (is_fast_native
88 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart)
89 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart));
90
91 return jni_start;
92 } else { // JniMethodEnd
93 ThreadOffset<kPointerSize> jni_end(-1);
94 if (reference_return) {
95 // Pass result.
96 jni_end = is_synchronized
97 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized)
98 : (is_fast_native
99 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEndWithReference)
100 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference));
101 } else {
102 jni_end = is_synchronized
103 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized)
104 : (is_fast_native
105 ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd)
106 : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd));
107 }
108
109 return jni_end;
110 }
111}
112
113
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114// Generate the JNI bridge for the given method, general contract:
115// - Arguments are in the managed runtime format, either on stack or in
116// registers, a reference to the method object is supplied as part of this
117// convention.
118//
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700119template <PointerSize kPointerSize>
120static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
121 uint32_t access_flags,
122 uint32_t method_idx,
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700123 const DexFile& dex_file,
124 JniOptimizationFlags optimization_flags) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 const bool is_native = (access_flags & kAccNative) != 0;
126 CHECK(is_native);
127 const bool is_static = (access_flags & kAccStatic) != 0;
128 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
129 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -0700130 InstructionSet instruction_set = driver->GetInstructionSet();
Goran Jakovljevic8c434dc2015-08-26 14:39:44 +0200131 const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures();
Vladimir Marko93205e32016-04-13 11:59:46 +0100132
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700133 // i.e. if the method was annotated with @FastNative
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700134 const bool is_fast_native = (optimization_flags == Compiler::kFastNative);
135
136 // i.e. if the method was annotated with @CriticalNative
137 bool is_critical_native = (optimization_flags == Compiler::kCriticalNative);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700138
139 VLOG(jni) << "JniCompile: Method :: "
David Sehr709b0702016-10-13 09:12:37 -0700140 << dex_file.PrettyMethod(method_idx, /* with signature */ true)
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700141 << " :: access_flags = " << std::hex << access_flags << std::dec;
142
143 if (UNLIKELY(is_fast_native)) {
144 VLOG(jni) << "JniCompile: Fast native method detected :: "
David Sehr709b0702016-10-13 09:12:37 -0700145 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700146 }
147
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700148 if (UNLIKELY(is_critical_native)) {
149 VLOG(jni) << "JniCompile: Critical native method detected :: "
David Sehr709b0702016-10-13 09:12:37 -0700150 << dex_file.PrettyMethod(method_idx, /* with signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700151 }
152
153 if (kIsDebugBuild) {
154 // Don't allow both @FastNative and @CriticalNative. They are mutually exclusive.
155 if (UNLIKELY(is_fast_native && is_critical_native)) {
156 LOG(FATAL) << "JniCompile: Method cannot be both @CriticalNative and @FastNative"
David Sehr709b0702016-10-13 09:12:37 -0700157 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700158 }
159
160 // @CriticalNative - extra checks:
161 // -- Don't allow virtual criticals
162 // -- Don't allow synchronized criticals
163 // -- Don't allow any objects as parameter or return value
164 if (UNLIKELY(is_critical_native)) {
165 CHECK(is_static)
166 << "@CriticalNative functions cannot be virtual since that would"
167 << "require passing a reference parameter (this), which is illegal "
David Sehr709b0702016-10-13 09:12:37 -0700168 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700169 CHECK(!is_synchronized)
170 << "@CriticalNative functions cannot be synchronized since that would"
171 << "require passing a (class and/or this) reference parameter, which is illegal "
David Sehr709b0702016-10-13 09:12:37 -0700172 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700173 for (size_t i = 0; i < strlen(shorty); ++i) {
174 CHECK_NE(Primitive::kPrimNot, Primitive::GetType(shorty[i]))
175 << "@CriticalNative methods' shorty types must not have illegal references "
David Sehr709b0702016-10-13 09:12:37 -0700176 << dex_file.PrettyMethod(method_idx, /* with_signature */ true);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700177 }
178 }
179 }
180
Vladimir Marko93205e32016-04-13 11:59:46 +0100181 ArenaPool pool;
182 ArenaAllocator arena(&pool);
183
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 // Calling conventions used to iterate over parameters to method
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700185 std::unique_ptr<JniCallingConvention> main_jni_conv =
186 JniCallingConvention::Create(&arena,
187 is_static,
188 is_synchronized,
189 is_critical_native,
190 shorty,
191 instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 bool reference_return = main_jni_conv->IsReturnAReference();
193
Ian Rogers700a4022014-05-19 16:49:03 -0700194 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Vladimir Marko93205e32016-04-13 11:59:46 +0100195 ManagedRuntimeCallingConvention::Create(
196 &arena, is_static, is_synchronized, shorty, instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700197
198 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
199 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100200 const char* jni_end_shorty;
201 if (reference_return && is_synchronized) {
202 jni_end_shorty = "ILL";
203 } else if (reference_return) {
204 jni_end_shorty = "IL";
205 } else if (is_synchronized) {
206 jni_end_shorty = "VL";
207 } else {
208 jni_end_shorty = "V";
209 }
210
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700211 std::unique_ptr<JniCallingConvention> end_jni_conv(
212 JniCallingConvention::Create(&arena,
213 is_static,
214 is_synchronized,
215 is_critical_native,
216 jni_end_shorty,
217 instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 // Assembler that holds generated instructions
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700220 std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm =
221 GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features);
Roland Levillain2b03a1f2017-06-06 16:09:59 +0100222 const CompilerOptions& compiler_options = driver->GetCompilerOptions();
223 jni_asm->cfi().SetEnabled(compiler_options.GenerateAnyDebugInfo());
224 jni_asm->SetEmitRunTimeChecksInDebugMode(compiler_options.EmitRunTimeChecksInDebugMode());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700225
226 // Offsets into data structures
227 // TODO: if cross compiling these offsets are for the host not the target
228 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
229 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
230 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
231
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700232 // 1. Build the frame saving all callee saves, Method*, and PC return address.
233 const size_t frame_size(main_jni_conv->FrameSize()); // Excludes outgoing args.
Vladimir Marko32248382016-05-19 10:37:24 +0100234 ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100236 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700238 if (LIKELY(!is_critical_native)) {
239 // NOTE: @CriticalNative methods don't have a HandleScope
240 // because they can't have any reference parameters or return values.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100241
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700242 // 2. Set up the HandleScope
243 mr_conv->ResetIterator(FrameOffset(frame_size));
244 main_jni_conv->ResetIterator(FrameOffset(0));
245 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
246 main_jni_conv->ReferenceCount(),
247 mr_conv->InterproceduralScratchRegister());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700249 __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(),
250 Thread::TopHandleScopeOffset<kPointerSize>(),
251 mr_conv->InterproceduralScratchRegister());
252 __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(),
253 main_jni_conv->HandleScopeOffset(),
254 mr_conv->InterproceduralScratchRegister());
255
256 // 3. Place incoming reference arguments into handle scope
257 main_jni_conv->Next(); // Skip JNIEnv*
258 // 3.5. Create Class argument for static methods out of passed method
259 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700260 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700261 // Check handle scope offset is within frame
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700262 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700263 // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod.
264 // Note this LoadRef() does not include read barrier. It will be handled below.
265 //
266 // scratchRegister = *method[DeclaringClassOffset()];
267 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
268 mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false);
269 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
270 // *handleScopeOffset = scratchRegister
271 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
272 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700273 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700274 // Place every reference into the handle scope (ignore other parameters).
275 while (mr_conv->HasNext()) {
276 CHECK(main_jni_conv->HasNext());
277 bool ref_param = main_jni_conv->IsCurrentParamAReference();
278 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
279 // References need placing in handle scope and the entry value passing
280 if (ref_param) {
281 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
282 // must be null.
283 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
284 // Check handle scope offset is within frame and doesn't run into the saved segment state.
285 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
286 CHECK_NE(handle_scope_offset.Uint32Value(),
287 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
288 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
289 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
290 CHECK(input_in_reg || input_on_stack);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700292 if (input_in_reg) {
293 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
294 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
295 __ StoreRef(handle_scope_offset, in_reg);
296 } else if (input_on_stack) {
297 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
298 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
299 __ CopyRef(handle_scope_offset, in_off,
300 mr_conv->InterproceduralScratchRegister());
301 }
302 }
303 mr_conv->Next();
304 main_jni_conv->Next();
305 }
306
307 // 4. Write out the end of the quick frames.
308 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>());
309
310 // NOTE: @CriticalNative does not need to store the stack pointer to the thread
311 // because garbage collections are disabled within the execution of a
312 // @CriticalNative method.
313 // (TODO: We could probably disable it for @FastNative too).
314 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315
316 // 5. Move frame down to allow space for out going args.
317 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100318 size_t current_out_arg_size = main_out_arg_size;
319 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700321 // Call the read barrier for the declaring class loaded from the method for a static call.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700322 // Skip this for @CriticalNative because we didn't build a HandleScope to begin with.
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700323 // Note that we always have outgoing param space available for at least two params.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700324 if (kUseReadBarrier && is_static && !is_critical_native) {
Igor Murashkinae7ff922016-10-06 14:59:19 -0700325 const bool kReadBarrierFastPath =
326 (instruction_set != kMips) && (instruction_set != kMips64);
327 std::unique_ptr<JNIMacroLabel> skip_cold_path_label;
328 if (kReadBarrierFastPath) {
329 skip_cold_path_label = __ CreateLabel();
330 // Fast path for supported targets.
331 //
332 // Check if gc_is_marking is set -- if it's not, we don't need
333 // a read barrier so skip it.
334 __ LoadFromThread(main_jni_conv->InterproceduralScratchRegister(),
335 Thread::IsGcMarkingOffset<kPointerSize>(),
336 Thread::IsGcMarkingSize());
337 // Jump over the slow path if gc is marking is false.
338 __ Jump(skip_cold_path_label.get(),
339 JNIMacroUnaryCondition::kZero,
340 main_jni_conv->InterproceduralScratchRegister());
341 }
342
343 // Construct slow path for read barrier:
344 //
345 // Call into the runtime's ReadBarrierJni and have it fix up
346 // the object address if it was moved.
347
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700348 ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize,
349 pReadBarrierJni);
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700350 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
351 main_jni_conv->Next(); // Skip JNIEnv.
352 FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
353 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
354 // Pass the handle for the class as the first argument.
355 if (main_jni_conv->IsCurrentParamOnStack()) {
356 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
357 __ CreateHandleScopeEntry(out_off, class_handle_scope_offset,
358 mr_conv->InterproceduralScratchRegister(),
359 false);
360 } else {
361 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
362 __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset,
363 ManagedRegister::NoRegister(), false);
364 }
365 main_jni_conv->Next();
366 // Pass the current thread as the second argument and call.
367 if (main_jni_conv->IsCurrentParamInRegister()) {
368 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700369 __ Call(main_jni_conv->CurrentParamRegister(),
370 Offset(read_barrier),
371 main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700372 } else {
373 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
374 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700375 __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister());
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700376 }
377 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset.
Igor Murashkinae7ff922016-10-06 14:59:19 -0700378
379 if (kReadBarrierFastPath) {
380 __ Bind(skip_cold_path_label.get());
381 }
Hiroshi Yamauchi1cc71eb2015-05-07 10:47:27 -0700382 }
383
Brian Carlstrom7940e442013-07-12 13:46:57 -0700384 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
385 // can occur. The result is the saved JNI local state that is restored by the exit call. We
386 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
387 // arguments.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700388 FrameOffset locked_object_handle_scope_offset(0xBEEFDEAD);
389 if (LIKELY(!is_critical_native)) {
390 // Skip this for @CriticalNative methods. They do not call JniMethodStart.
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700391 ThreadOffset<kPointerSize> jni_start(
392 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kStart,
393 reference_return,
394 is_synchronized,
395 is_fast_native).SizeValue());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700396 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700397 locked_object_handle_scope_offset = FrameOffset(0);
398 if (is_synchronized) {
399 // Pass object for locking.
400 main_jni_conv->Next(); // Skip JNIEnv.
401 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
402 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
403 if (main_jni_conv->IsCurrentParamOnStack()) {
404 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
405 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
406 mr_conv->InterproceduralScratchRegister(), false);
407 } else {
408 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
409 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
410 ManagedRegister::NoRegister(), false);
411 }
412 main_jni_conv->Next();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700414 if (main_jni_conv->IsCurrentParamInRegister()) {
415 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
416 __ Call(main_jni_conv->CurrentParamRegister(),
417 Offset(jni_start),
418 main_jni_conv->InterproceduralScratchRegister());
419 } else {
420 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
421 main_jni_conv->InterproceduralScratchRegister());
422 __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister());
423 }
424 if (is_synchronized) { // Check for exceptions from monitor enter.
425 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
426 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700428
429 // Store into stack_frame[saved_cookie_offset] the return value of JniMethodStart.
430 FrameOffset saved_cookie_offset(
431 FrameOffset(0xDEADBEEFu)); // @CriticalNative - use obviously bad value for debugging
432 if (LIKELY(!is_critical_native)) {
433 saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
434 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4 /* sizeof cookie */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700435 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436
437 // 7. Iterate over arguments placing values from managed calling convention in
438 // to the convention required for a native call (shuffling). For references
439 // place an index/pointer to the reference after checking whether it is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700440 // null (which must be encoded as null).
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700442 // give as many free registers for the shuffle as possible.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100443 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700444 uint32_t args_count = 0;
445 while (mr_conv->HasNext()) {
446 args_count++;
447 mr_conv->Next();
448 }
449
450 // Do a backward pass over arguments, so that the generated code will be "mov
451 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
452 // TODO: A reverse iterator to improve readability.
453 for (uint32_t i = 0; i < args_count; ++i) {
454 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
455 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700456
457 // Skip the extra JNI parameters for now.
458 if (LIKELY(!is_critical_native)) {
459 main_jni_conv->Next(); // Skip JNIEnv*.
460 if (is_static) {
461 main_jni_conv->Next(); // Skip Class for now.
462 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700463 }
464 // Skip to the argument we're interested in.
465 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
466 mr_conv->Next();
467 main_jni_conv->Next();
468 }
469 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
470 }
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700471 if (is_static && !is_critical_native) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700472 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100473 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
475 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700476 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700477 if (main_jni_conv->IsCurrentParamOnStack()) {
478 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700479 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 mr_conv->InterproceduralScratchRegister(),
481 false);
482 } else {
483 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700484 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700485 ManagedRegister::NoRegister(), false);
486 }
487 }
488
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700489 // Set the iterator back to the incoming Method*.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700491 if (LIKELY(!is_critical_native)) {
492 // 8. Create 1st argument, the JNI environment ptr.
493 // Register that will hold local indirect reference table
494 if (main_jni_conv->IsCurrentParamInRegister()) {
495 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
496 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
497 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>());
498 } else {
499 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
500 __ CopyRawPtrFromThread(jni_env,
501 Thread::JniEnvOffset<kPointerSize>(),
502 main_jni_conv->InterproceduralScratchRegister());
503 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 }
505
506 // 9. Plant call to native code associated with method.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700507 MemberOffset jni_entrypoint_offset =
508 ArtMethod::EntryPointFromJniOffset(InstructionSetPointerSize(instruction_set));
509 // FIXME: Not sure if MethodStackOffset will work here. What does it even do?
510 __ Call(main_jni_conv->MethodStackOffset(),
511 jni_entrypoint_offset,
512 // XX: Why not the jni conv scratch register?
Brian Carlstrom7940e442013-07-12 13:46:57 -0700513 mr_conv->InterproceduralScratchRegister());
514
515 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700516 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700517 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
518 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
519 __ SignExtend(main_jni_conv->ReturnRegister(),
520 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
521 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
522 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
523 __ ZeroExtend(main_jni_conv->ReturnRegister(),
524 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
525 }
526 }
527
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700528 // 11. Process return value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
530 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700531 if (LIKELY(!is_critical_native)) {
532 // For normal JNI, store the return value on the stack because the call to
533 // JniMethodEnd will clobber the return value. It will be restored in (13).
534 if ((instruction_set == kMips || instruction_set == kMips64) &&
535 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
536 return_save_location.Uint32Value() % 8 != 0) {
537 // Ensure doubles are 8-byte aligned for MIPS
538 return_save_location = FrameOffset(return_save_location.Uint32Value()
539 + static_cast<size_t>(kMipsPointerSize));
540 // TODO: refactor this into the JniCallingConvention code
541 // as a return value alignment requirement.
542 }
543 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
544 __ Store(return_save_location,
545 main_jni_conv->ReturnRegister(),
546 main_jni_conv->SizeOfReturnValue());
547 } else {
548 // For @CriticalNative only,
549 // move the JNI return register into the managed return register (if they don't match).
550 ManagedRegister jni_return_reg = main_jni_conv->ReturnRegister();
551 ManagedRegister mr_return_reg = mr_conv->ReturnRegister();
552
553 // Check if the JNI return register matches the managed return register.
554 // If they differ, only then do we have to do anything about it.
555 // Otherwise the return value is already in the right place when we return.
556 if (!jni_return_reg.Equals(mr_return_reg)) {
557 // This is typically only necessary on ARM32 due to native being softfloat
558 // while managed is hardfloat.
559 // -- For example VMOV {r0, r1} -> D0; VMOV r0 -> S0.
560 __ Move(mr_return_reg, jni_return_reg, main_jni_conv->SizeOfReturnValue());
561 } else if (jni_return_reg.IsNoRegister() && mr_return_reg.IsNoRegister()) {
562 // Sanity check: If the return value is passed on the stack for some reason,
563 // then make sure the size matches.
564 CHECK_EQ(main_jni_conv->SizeOfReturnValue(), mr_conv->SizeOfReturnValue());
565 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700566 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 }
568
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100569 // Increase frame size for out args if needed by the end_jni_conv.
570 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
571 if (end_out_arg_size > current_out_arg_size) {
572 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
573 current_out_arg_size = end_out_arg_size;
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700574 // TODO: This is redundant for @CriticalNative but we need to
575 // conditionally do __DecreaseFrameSize below.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100576 __ IncreaseFrameSize(out_arg_size_diff);
577 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
578 locked_object_handle_scope_offset =
579 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
580 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
581 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582 // thread.
583 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700584
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700585 if (LIKELY(!is_critical_native)) {
586 // 12. Call JniMethodEnd
Igor Murashkinaf1e2992016-10-12 17:44:50 -0700587 ThreadOffset<kPointerSize> jni_end(
588 GetJniEntrypointThreadOffset<kPointerSize>(JniEntrypoint::kEnd,
589 reference_return,
590 is_synchronized,
591 is_fast_native).SizeValue());
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700592 if (reference_return) {
593 // Pass result.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700594 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
595 end_jni_conv->Next();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700596 }
597 // Pass saved local reference state.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 if (end_jni_conv->IsCurrentParamOnStack()) {
599 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700600 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 } else {
602 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700603 __ Load(out_reg, saved_cookie_offset, 4);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700604 }
605 end_jni_conv->Next();
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700606 if (is_synchronized) {
607 // Pass object for unlocking.
608 if (end_jni_conv->IsCurrentParamOnStack()) {
609 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
610 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
611 end_jni_conv->InterproceduralScratchRegister(),
612 false);
613 } else {
614 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
615 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
616 ManagedRegister::NoRegister(), false);
617 }
618 end_jni_conv->Next();
619 }
620 if (end_jni_conv->IsCurrentParamInRegister()) {
621 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
622 __ Call(end_jni_conv->CurrentParamRegister(),
623 Offset(jni_end),
624 end_jni_conv->InterproceduralScratchRegister());
625 } else {
626 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
627 end_jni_conv->InterproceduralScratchRegister());
628 __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister());
629 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700630
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700631 // 13. Reload return value
632 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
633 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
634 // NIT: If it's @CriticalNative then we actually only need to do this IF
635 // the calling convention's native return register doesn't match the managed convention's
636 // return register.
637 }
638 } // if (!is_critical_native)
Brian Carlstrom7940e442013-07-12 13:46:57 -0700639
640 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100641 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642
643 // 15. Process pending exceptions from JNI call or monitor exit.
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700644 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0 /* stack_adjust */);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700646 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700647 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100648 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700649 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100650 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651
652 // 17. Finalize code generation
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000653 __ FinalizeCode();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 size_t cs = __ CodeSize();
655 std::vector<uint8_t> managed_code(cs);
656 MemoryRegion code(&managed_code[0], managed_code.size());
657 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100658
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100659 return CompiledMethod::SwapAllocCompiledMethod(driver,
660 instruction_set,
661 ArrayRef<const uint8_t>(managed_code),
662 frame_size,
663 main_jni_conv->CoreSpillMask(),
664 main_jni_conv->FpSpillMask(),
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700665 /* method_info */ ArrayRef<const uint8_t>(),
666 /* vmap_table */ ArrayRef<const uint8_t>(),
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100667 ArrayRef<const uint8_t>(*jni_asm->cfi().data()),
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100668 ArrayRef<const linker::LinkerPatch>());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669}
670
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700671// Copy a single parameter from the managed to the JNI calling convention.
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700672template <PointerSize kPointerSize>
673static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700674 ManagedRuntimeCallingConvention* mr_conv,
675 JniCallingConvention* jni_conv,
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700676 size_t frame_size,
677 size_t out_arg_size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
679 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700680 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700681 bool null_allowed = false;
682 bool ref_param = jni_conv->IsCurrentParamAReference();
683 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
684 // input may be in register, on stack or both - but not none!
685 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
686 if (output_in_reg) { // output shouldn't straddle registers and stack
687 CHECK(!jni_conv->IsCurrentParamOnStack());
688 } else {
689 CHECK(jni_conv->IsCurrentParamOnStack());
690 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700691 // References need placing in handle scope and the entry address passing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 if (ref_param) {
693 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700694 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
695 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700696 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700697 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
698 // Check handle scope offset is within frame.
699 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700700 }
701 if (input_in_reg && output_in_reg) {
702 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
703 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
704 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700705 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700706 } else {
707 if (!mr_conv->IsCurrentParamOnStack()) {
708 // regular non-straddling move
709 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
710 } else {
711 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
712 }
713 }
714 } else if (!input_in_reg && !output_in_reg) {
715 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
716 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700717 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700718 null_allowed);
719 } else {
720 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
721 size_t param_size = mr_conv->CurrentParamSize();
722 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
723 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
724 }
725 } else if (!input_in_reg && output_in_reg) {
726 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
727 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
728 // Check that incoming stack arguments are above the current stack frame.
729 CHECK_GT(in_off.Uint32Value(), frame_size);
730 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700731 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700732 } else {
733 size_t param_size = mr_conv->CurrentParamSize();
734 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
735 __ Load(out_reg, in_off, param_size);
736 }
737 } else {
738 CHECK(input_in_reg && !output_in_reg);
739 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
740 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
741 // Check outgoing argument is within frame
742 CHECK_LT(out_off.Uint32Value(), frame_size);
743 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700744 // TODO: recycle value in in_reg rather than reload from handle scope
745 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700746 null_allowed);
747 } else {
748 size_t param_size = mr_conv->CurrentParamSize();
749 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
750 if (!mr_conv->IsCurrentParamOnStack()) {
751 // regular non-straddling store
752 __ Store(out_off, in_reg, param_size);
753 } else {
754 // store where input straddles registers and stack
755 CHECK_EQ(param_size, 8u);
756 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
757 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
758 }
759 }
760 }
761}
762
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700763template <PointerSize kPointerSize>
764static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700765 JniCallingConvention* jni_conv,
766 ManagedRegister in_reg) {
767 if (jni_conv->IsCurrentParamOnStack()) {
768 FrameOffset dest = jni_conv->CurrentParamStackOffset();
769 __ StoreRawPtr(dest, in_reg);
770 } else {
771 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
772 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
773 }
774 }
775}
776
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700777CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler,
778 uint32_t access_flags,
779 uint32_t method_idx,
780 const DexFile& dex_file,
781 Compiler::JniOptimizationFlags optimization_flags) {
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700782 if (Is64BitInstructionSet(compiler->GetInstructionSet())) {
783 return ArtJniCompileMethodInternal<PointerSize::k64>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700784 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700785 } else {
786 return ArtJniCompileMethodInternal<PointerSize::k32>(
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700787 compiler, access_flags, method_idx, dex_file, optimization_flags);
Andreas Gampe3b165bc2016-08-01 22:07:04 -0700788 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700789}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700790
791} // namespace art