Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 17 | #include "jni_compiler.h" |
| 18 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 19 | #include <algorithm> |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 20 | #include <ios> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 21 | #include <memory> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 22 | #include <vector> |
Dave Allison | 65fcc2c | 2014-04-28 13:45:27 -0700 | [diff] [blame] | 23 | #include <fstream> |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "art_method.h" |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 26 | #include "base/arena_allocator.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 27 | #include "base/enums.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "base/logging.h" |
| 29 | #include "base/macros.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 30 | #include "memory_region.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 31 | #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 Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 36 | #include "driver/compiler_options.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 37 | #include "entrypoints/quick/quick_entrypoints.h" |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 38 | #include "jni_env_ext.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 39 | #include "debug/dwarf/debug_frame_opcode_writer.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 40 | #include "utils/assembler.h" |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 41 | #include "utils/jni_macro_assembler.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 42 | #include "utils/managed_register.h" |
| 43 | #include "utils/arm/managed_register_arm.h" |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 44 | #include "utils/arm64/managed_register_arm64.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 45 | #include "utils/mips/managed_register_mips.h" |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 46 | #include "utils/mips64/managed_register_mips64.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 47 | #include "utils/x86/managed_register_x86.h" |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 48 | #include "utils.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 49 | #include "thread.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 50 | |
| 51 | #define __ jni_asm-> |
| 52 | |
| 53 | namespace art { |
| 54 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 55 | using JniOptimizationFlags = Compiler::JniOptimizationFlags; |
| 56 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 57 | template <PointerSize kPointerSize> |
| 58 | static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 59 | ManagedRuntimeCallingConvention* mr_conv, |
| 60 | JniCallingConvention* jni_conv, |
| 61 | size_t frame_size, size_t out_arg_size); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 62 | template <PointerSize kPointerSize> |
| 63 | static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 64 | JniCallingConvention* jni_conv, |
| 65 | ManagedRegister in_reg); |
| 66 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 67 | template <PointerSize kPointerSize> |
| 68 | static 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 Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 73 | // 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 Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 78 | template <PointerSize kPointerSize> |
| 79 | static CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver, |
| 80 | uint32_t access_flags, |
| 81 | uint32_t method_idx, |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 82 | const DexFile& dex_file, |
| 83 | JniOptimizationFlags optimization_flags) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 84 | 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 Rogers | 72d3262 | 2014-05-06 16:20:11 -0700 | [diff] [blame] | 89 | InstructionSet instruction_set = driver->GetInstructionSet(); |
Goran Jakovljevic | 8c434dc | 2015-08-26 14:39:44 +0200 | [diff] [blame] | 90 | const InstructionSetFeatures* instruction_set_features = driver->GetInstructionSetFeatures(); |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 91 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 92 | // i.e. if the method was annotated with @FastNative |
| 93 | const bool is_fast_native = |
| 94 | (static_cast<uint32_t>(optimization_flags) & Compiler::kFastNative) != 0; |
| 95 | |
| 96 | VLOG(jni) << "JniCompile: Method :: " |
| 97 | << art::PrettyMethod(method_idx, dex_file, /* with signature */ true) |
| 98 | << " :: access_flags = " << std::hex << access_flags << std::dec; |
| 99 | |
| 100 | if (UNLIKELY(is_fast_native)) { |
| 101 | VLOG(jni) << "JniCompile: Fast native method detected :: " |
| 102 | << art::PrettyMethod(method_idx, dex_file, /* with signature */ true); |
| 103 | } |
| 104 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 105 | ArenaPool pool; |
| 106 | ArenaAllocator arena(&pool); |
| 107 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 108 | // Calling conventions used to iterate over parameters to method |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 109 | std::unique_ptr<JniCallingConvention> main_jni_conv( |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 110 | JniCallingConvention::Create(&arena, is_static, is_synchronized, shorty, instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 111 | bool reference_return = main_jni_conv->IsReturnAReference(); |
| 112 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 113 | std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv( |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 114 | ManagedRuntimeCallingConvention::Create( |
| 115 | &arena, is_static, is_synchronized, shorty, instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 116 | |
| 117 | // Calling conventions to call into JNI method "end" possibly passing a returned reference, the |
| 118 | // method and the current thread. |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 119 | const char* jni_end_shorty; |
| 120 | if (reference_return && is_synchronized) { |
| 121 | jni_end_shorty = "ILL"; |
| 122 | } else if (reference_return) { |
| 123 | jni_end_shorty = "IL"; |
| 124 | } else if (is_synchronized) { |
| 125 | jni_end_shorty = "VL"; |
| 126 | } else { |
| 127 | jni_end_shorty = "V"; |
| 128 | } |
| 129 | |
Vladimir Marko | 93205e3 | 2016-04-13 11:59:46 +0100 | [diff] [blame] | 130 | std::unique_ptr<JniCallingConvention> end_jni_conv(JniCallingConvention::Create( |
| 131 | &arena, is_static, is_synchronized, jni_end_shorty, instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 132 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 133 | // Assembler that holds generated instructions |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 134 | std::unique_ptr<JNIMacroAssembler<kPointerSize>> jni_asm = |
| 135 | GetMacroAssembler<kPointerSize>(&arena, instruction_set, instruction_set_features); |
David Srbecky | 5b1c2ca | 2016-01-25 17:32:41 +0000 | [diff] [blame] | 136 | jni_asm->cfi().SetEnabled(driver->GetCompilerOptions().GenerateAnyDebugInfo()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 137 | |
| 138 | // Offsets into data structures |
| 139 | // TODO: if cross compiling these offsets are for the host not the target |
| 140 | const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions)); |
| 141 | const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter)); |
| 142 | const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit)); |
| 143 | |
| 144 | // 1. Build the frame saving all callee saves |
| 145 | const size_t frame_size(main_jni_conv->FrameSize()); |
Vladimir Marko | 3224838 | 2016-05-19 10:37:24 +0100 | [diff] [blame] | 146 | ArrayRef<const ManagedRegister> callee_save_regs = main_jni_conv->CalleeSaveRegisters(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 147 | __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills()); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 148 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 149 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 150 | // 2. Set up the HandleScope |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 151 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 152 | main_jni_conv->ResetIterator(FrameOffset(0)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 153 | __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 154 | main_jni_conv->ReferenceCount(), |
| 155 | mr_conv->InterproceduralScratchRegister()); |
Serban Constantinescu | 75b9113 | 2014-04-09 18:39:10 +0100 | [diff] [blame] | 156 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 157 | __ CopyRawPtrFromThread(main_jni_conv->HandleScopeLinkOffset(), |
| 158 | Thread::TopHandleScopeOffset<kPointerSize>(), |
| 159 | mr_conv->InterproceduralScratchRegister()); |
| 160 | __ StoreStackOffsetToThread(Thread::TopHandleScopeOffset<kPointerSize>(), |
| 161 | main_jni_conv->HandleScopeOffset(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 162 | mr_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 163 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 164 | // 3. Place incoming reference arguments into handle scope |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 165 | main_jni_conv->Next(); // Skip JNIEnv* |
| 166 | // 3.5. Create Class argument for static methods out of passed method |
| 167 | if (is_static) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 168 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 169 | // Check handle scope offset is within frame |
| 170 | CHECK_LT(handle_scope_offset.Uint32Value(), frame_size); |
Roland Levillain | 4d02711 | 2015-07-01 15:41:14 +0100 | [diff] [blame] | 171 | // Note this LoadRef() doesn't need heap unpoisoning since it's from the ArtMethod. |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 172 | // Note this LoadRef() does not include read barrier. It will be handled below. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 173 | __ LoadRef(main_jni_conv->InterproceduralScratchRegister(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | mr_conv->MethodRegister(), ArtMethod::DeclaringClassOffset(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 175 | __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 176 | __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister()); |
| 177 | main_jni_conv->Next(); // in handle scope so move to next argument |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 178 | } |
| 179 | while (mr_conv->HasNext()) { |
| 180 | CHECK(main_jni_conv->HasNext()); |
| 181 | bool ref_param = main_jni_conv->IsCurrentParamAReference(); |
| 182 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 183 | // References need placing in handle scope and the entry value passing |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 184 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 185 | // Compute handle scope entry, note null is placed in the handle scope but its boxed value |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 186 | // must be null. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 187 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 188 | // Check handle scope offset is within frame and doesn't run into the saved segment state. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 189 | CHECK_LT(handle_scope_offset.Uint32Value(), frame_size); |
| 190 | CHECK_NE(handle_scope_offset.Uint32Value(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 191 | main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value()); |
| 192 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 193 | bool input_on_stack = mr_conv->IsCurrentParamOnStack(); |
| 194 | CHECK(input_in_reg || input_on_stack); |
| 195 | |
| 196 | if (input_in_reg) { |
| 197 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 198 | __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 199 | __ StoreRef(handle_scope_offset, in_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 200 | } else if (input_on_stack) { |
| 201 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 202 | __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 203 | __ CopyRef(handle_scope_offset, in_off, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 204 | mr_conv->InterproceduralScratchRegister()); |
| 205 | } |
| 206 | } |
| 207 | mr_conv->Next(); |
| 208 | main_jni_conv->Next(); |
| 209 | } |
| 210 | |
| 211 | // 4. Write out the end of the quick frames. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 212 | __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset<kPointerSize>()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 213 | |
| 214 | // 5. Move frame down to allow space for out going args. |
| 215 | const size_t main_out_arg_size = main_jni_conv->OutArgSize(); |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 216 | size_t current_out_arg_size = main_out_arg_size; |
| 217 | __ IncreaseFrameSize(main_out_arg_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 218 | |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 219 | // Call the read barrier for the declaring class loaded from the method for a static call. |
| 220 | // Note that we always have outgoing param space available for at least two params. |
| 221 | if (kUseReadBarrier && is_static) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 222 | ThreadOffset<kPointerSize> read_barrier = QUICK_ENTRYPOINT_OFFSET(kPointerSize, |
| 223 | pReadBarrierJni); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 224 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 225 | main_jni_conv->Next(); // Skip JNIEnv. |
| 226 | FrameOffset class_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 227 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 228 | // Pass the handle for the class as the first argument. |
| 229 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 230 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
| 231 | __ CreateHandleScopeEntry(out_off, class_handle_scope_offset, |
| 232 | mr_conv->InterproceduralScratchRegister(), |
| 233 | false); |
| 234 | } else { |
| 235 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
| 236 | __ CreateHandleScopeEntry(out_reg, class_handle_scope_offset, |
| 237 | ManagedRegister::NoRegister(), false); |
| 238 | } |
| 239 | main_jni_conv->Next(); |
| 240 | // Pass the current thread as the second argument and call. |
| 241 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 242 | __ GetCurrentThread(main_jni_conv->CurrentParamRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 243 | __ Call(main_jni_conv->CurrentParamRegister(), |
| 244 | Offset(read_barrier), |
| 245 | main_jni_conv->InterproceduralScratchRegister()); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 246 | } else { |
| 247 | __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(), |
| 248 | main_jni_conv->InterproceduralScratchRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 249 | __ CallFromThread(read_barrier, main_jni_conv->InterproceduralScratchRegister()); |
Hiroshi Yamauchi | 1cc71eb | 2015-05-07 10:47:27 -0700 | [diff] [blame] | 250 | } |
| 251 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); // Reset. |
| 252 | } |
| 253 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 254 | // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable |
| 255 | // can occur. The result is the saved JNI local state that is restored by the exit call. We |
| 256 | // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer |
| 257 | // arguments. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 258 | ThreadOffset<kPointerSize> jni_start = |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 259 | is_synchronized |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 260 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStartSynchronized) |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 261 | : (is_fast_native |
| 262 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastStart) |
| 263 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodStart)); |
| 264 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 265 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 266 | FrameOffset locked_object_handle_scope_offset(0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 267 | if (is_synchronized) { |
| 268 | // Pass object for locking. |
| 269 | main_jni_conv->Next(); // Skip JNIEnv. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 270 | locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 271 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 272 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 273 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 274 | __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 275 | mr_conv->InterproceduralScratchRegister(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 276 | } else { |
| 277 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 278 | __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 279 | ManagedRegister::NoRegister(), false); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 280 | } |
| 281 | main_jni_conv->Next(); |
| 282 | } |
| 283 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 284 | __ GetCurrentThread(main_jni_conv->CurrentParamRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 285 | __ Call(main_jni_conv->CurrentParamRegister(), |
| 286 | Offset(jni_start), |
| 287 | main_jni_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 288 | } else { |
| 289 | __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(), |
| 290 | main_jni_conv->InterproceduralScratchRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 291 | __ CallFromThread(jni_start, main_jni_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 292 | } |
| 293 | if (is_synchronized) { // Check for exceptions from monitor enter. |
| 294 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size); |
| 295 | } |
| 296 | FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset(); |
| 297 | __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4); |
| 298 | |
| 299 | // 7. Iterate over arguments placing values from managed calling convention in |
| 300 | // to the convention required for a native call (shuffling). For references |
| 301 | // place an index/pointer to the reference after checking whether it is |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 302 | // null (which must be encoded as null). |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 303 | // Note: we do this prior to materializing the JNIEnv* and static's jclass to |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 304 | // give as many free registers for the shuffle as possible. |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 305 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 306 | uint32_t args_count = 0; |
| 307 | while (mr_conv->HasNext()) { |
| 308 | args_count++; |
| 309 | mr_conv->Next(); |
| 310 | } |
| 311 | |
| 312 | // Do a backward pass over arguments, so that the generated code will be "mov |
| 313 | // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3." |
| 314 | // TODO: A reverse iterator to improve readability. |
| 315 | for (uint32_t i = 0; i < args_count; ++i) { |
| 316 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
| 317 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 318 | main_jni_conv->Next(); // Skip JNIEnv*. |
| 319 | if (is_static) { |
| 320 | main_jni_conv->Next(); // Skip Class for now. |
| 321 | } |
| 322 | // Skip to the argument we're interested in. |
| 323 | for (uint32_t j = 0; j < args_count - i - 1; ++j) { |
| 324 | mr_conv->Next(); |
| 325 | main_jni_conv->Next(); |
| 326 | } |
| 327 | CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size); |
| 328 | } |
| 329 | if (is_static) { |
| 330 | // Create argument for Class |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 331 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 332 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 333 | main_jni_conv->Next(); // Skip JNIEnv* |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 334 | FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 335 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 336 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 337 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 338 | mr_conv->InterproceduralScratchRegister(), |
| 339 | false); |
| 340 | } else { |
| 341 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 342 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 343 | ManagedRegister::NoRegister(), false); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // 8. Create 1st argument, the JNI environment ptr. |
| 348 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 349 | // Register that will hold local indirect reference table |
| 350 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 351 | ManagedRegister jni_env = main_jni_conv->CurrentParamRegister(); |
| 352 | DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister())); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 353 | __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset<kPointerSize>()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 354 | } else { |
| 355 | FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset(); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 356 | __ CopyRawPtrFromThread(jni_env, |
| 357 | Thread::JniEnvOffset<kPointerSize>(), |
| 358 | main_jni_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // 9. Plant call to native code associated with method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 362 | MemberOffset jni_entrypoint_offset = ArtMethod::EntryPointFromJniOffset( |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 363 | InstructionSetPointerSize(instruction_set)); |
| 364 | __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 365 | mr_conv->InterproceduralScratchRegister()); |
| 366 | |
| 367 | // 10. Fix differences in result widths. |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 368 | if (main_jni_conv->RequiresSmallResultTypeExtension()) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 369 | if (main_jni_conv->GetReturnType() == Primitive::kPrimByte || |
| 370 | main_jni_conv->GetReturnType() == Primitive::kPrimShort) { |
| 371 | __ SignExtend(main_jni_conv->ReturnRegister(), |
| 372 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 373 | } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean || |
| 374 | main_jni_conv->GetReturnType() == Primitive::kPrimChar) { |
| 375 | __ ZeroExtend(main_jni_conv->ReturnRegister(), |
| 376 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // 11. Save return value |
| 381 | FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation(); |
| 382 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
Maja Gagic | 6ea651f | 2015-02-24 16:55:04 +0100 | [diff] [blame] | 383 | if ((instruction_set == kMips || instruction_set == kMips64) && |
| 384 | main_jni_conv->GetReturnType() == Primitive::kPrimDouble && |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 385 | return_save_location.Uint32Value() % 8 != 0) { |
| 386 | // Ensure doubles are 8-byte aligned for MIPS |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 387 | return_save_location = FrameOffset(return_save_location.Uint32Value() |
| 388 | + static_cast<size_t>(kMipsPointerSize)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 389 | } |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 390 | CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 391 | __ Store(return_save_location, |
| 392 | main_jni_conv->ReturnRegister(), |
| 393 | main_jni_conv->SizeOfReturnValue()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 396 | // Increase frame size for out args if needed by the end_jni_conv. |
| 397 | const size_t end_out_arg_size = end_jni_conv->OutArgSize(); |
| 398 | if (end_out_arg_size > current_out_arg_size) { |
| 399 | size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size; |
| 400 | current_out_arg_size = end_out_arg_size; |
| 401 | __ IncreaseFrameSize(out_arg_size_diff); |
| 402 | saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff); |
| 403 | locked_object_handle_scope_offset = |
| 404 | FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff); |
| 405 | return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff); |
| 406 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 407 | // thread. |
| 408 | end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size)); |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 409 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 410 | ThreadOffset<kPointerSize> jni_end(-1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 411 | if (reference_return) { |
| 412 | // Pass result. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 413 | jni_end = is_synchronized |
| 414 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReferenceSynchronized) |
| 415 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndWithReference); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 416 | SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister()); |
| 417 | end_jni_conv->Next(); |
| 418 | } else { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 419 | jni_end = is_synchronized |
| 420 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEndSynchronized) |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 421 | : (is_fast_native |
| 422 | ? QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodFastEnd) |
| 423 | : QUICK_ENTRYPOINT_OFFSET(kPointerSize, pJniMethodEnd)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 424 | } |
| 425 | // Pass saved local reference state. |
| 426 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 427 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
| 428 | __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4); |
| 429 | } else { |
| 430 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
| 431 | __ Load(out_reg, saved_cookie_offset, 4); |
| 432 | } |
| 433 | end_jni_conv->Next(); |
| 434 | if (is_synchronized) { |
| 435 | // Pass object for unlocking. |
| 436 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 437 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 438 | __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 439 | end_jni_conv->InterproceduralScratchRegister(), |
| 440 | false); |
| 441 | } else { |
| 442 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 443 | __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 444 | ManagedRegister::NoRegister(), false); |
| 445 | } |
| 446 | end_jni_conv->Next(); |
| 447 | } |
| 448 | if (end_jni_conv->IsCurrentParamInRegister()) { |
| 449 | __ GetCurrentThread(end_jni_conv->CurrentParamRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 450 | __ Call(end_jni_conv->CurrentParamRegister(), |
| 451 | Offset(jni_end), |
| 452 | end_jni_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 453 | } else { |
| 454 | __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(), |
| 455 | end_jni_conv->InterproceduralScratchRegister()); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 456 | __ CallFromThread(jni_end, end_jni_conv->InterproceduralScratchRegister()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // 13. Reload return value |
| 460 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
| 461 | __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue()); |
| 462 | } |
| 463 | |
| 464 | // 14. Move frame up now we're done with the out arg space. |
Vladimir Marko | 4e24b9d | 2014-07-24 17:01:58 +0100 | [diff] [blame] | 465 | __ DecreaseFrameSize(current_out_arg_size); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 466 | |
| 467 | // 15. Process pending exceptions from JNI call or monitor exit. |
| 468 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0); |
| 469 | |
Mathieu Chartier | 8770e5c | 2013-10-16 14:49:01 -0700 | [diff] [blame] | 470 | // 16. Remove activation - need to restore callee save registers since the GC may have changed |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 471 | // them. |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 472 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Mathieu Chartier | 8770e5c | 2013-10-16 14:49:01 -0700 | [diff] [blame] | 473 | __ RemoveFrame(frame_size, callee_save_regs); |
David Srbecky | dd97393 | 2015-04-07 20:29:48 +0100 | [diff] [blame] | 474 | DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 475 | |
| 476 | // 17. Finalize code generation |
Vladimir Marko | cf93a5c | 2015-06-16 11:33:24 +0000 | [diff] [blame] | 477 | __ FinalizeCode(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 478 | size_t cs = __ CodeSize(); |
| 479 | std::vector<uint8_t> managed_code(cs); |
| 480 | MemoryRegion code(&managed_code[0], managed_code.size()); |
| 481 | __ FinalizeInstructions(code); |
David Srbecky | 8c57831 | 2015-04-07 19:46:22 +0100 | [diff] [blame] | 482 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 483 | return CompiledMethod::SwapAllocCompiledMethod(driver, |
| 484 | instruction_set, |
| 485 | ArrayRef<const uint8_t>(managed_code), |
| 486 | frame_size, |
| 487 | main_jni_conv->CoreSpillMask(), |
| 488 | main_jni_conv->FpSpillMask(), |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 489 | ArrayRef<const SrcMapElem>(), |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 490 | ArrayRef<const uint8_t>(), // vmap_table. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 491 | ArrayRef<const uint8_t>(*jni_asm->cfi().data()), |
| 492 | ArrayRef<const LinkerPatch>()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 495 | // Copy a single parameter from the managed to the JNI calling convention. |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 496 | template <PointerSize kPointerSize> |
| 497 | static void CopyParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 498 | ManagedRuntimeCallingConvention* mr_conv, |
| 499 | JniCallingConvention* jni_conv, |
| 500 | size_t frame_size, size_t out_arg_size) { |
| 501 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 502 | bool output_in_reg = jni_conv->IsCurrentParamInRegister(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 503 | FrameOffset handle_scope_offset(0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 504 | bool null_allowed = false; |
| 505 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 506 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
| 507 | // input may be in register, on stack or both - but not none! |
| 508 | CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack()); |
| 509 | if (output_in_reg) { // output shouldn't straddle registers and stack |
| 510 | CHECK(!jni_conv->IsCurrentParamOnStack()); |
| 511 | } else { |
| 512 | CHECK(jni_conv->IsCurrentParamOnStack()); |
| 513 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 514 | // References need placing in handle scope and the entry address passing. |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 515 | if (ref_param) { |
| 516 | null_allowed = mr_conv->IsCurrentArgPossiblyNull(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 517 | // Compute handle scope offset. Note null is placed in the handle scope but the jobject |
| 518 | // passed to the native code must be null (not a pointer into the handle scope |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 519 | // as with regular references). |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 520 | handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset(); |
| 521 | // Check handle scope offset is within frame. |
| 522 | CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 523 | } |
| 524 | if (input_in_reg && output_in_reg) { |
| 525 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 526 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 527 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 528 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 529 | } else { |
| 530 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 531 | // regular non-straddling move |
| 532 | __ Move(out_reg, in_reg, mr_conv->CurrentParamSize()); |
| 533 | } else { |
| 534 | UNIMPLEMENTED(FATAL); // we currently don't expect to see this case |
| 535 | } |
| 536 | } |
| 537 | } else if (!input_in_reg && !output_in_reg) { |
| 538 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 539 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 540 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 541 | null_allowed); |
| 542 | } else { |
| 543 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 544 | size_t param_size = mr_conv->CurrentParamSize(); |
| 545 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 546 | __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size); |
| 547 | } |
| 548 | } else if (!input_in_reg && output_in_reg) { |
| 549 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 550 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 551 | // Check that incoming stack arguments are above the current stack frame. |
| 552 | CHECK_GT(in_off.Uint32Value(), frame_size); |
| 553 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 554 | __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 555 | } else { |
| 556 | size_t param_size = mr_conv->CurrentParamSize(); |
| 557 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 558 | __ Load(out_reg, in_off, param_size); |
| 559 | } |
| 560 | } else { |
| 561 | CHECK(input_in_reg && !output_in_reg); |
| 562 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 563 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 564 | // Check outgoing argument is within frame |
| 565 | CHECK_LT(out_off.Uint32Value(), frame_size); |
| 566 | if (ref_param) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 567 | // TODO: recycle value in in_reg rather than reload from handle scope |
| 568 | __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 569 | null_allowed); |
| 570 | } else { |
| 571 | size_t param_size = mr_conv->CurrentParamSize(); |
| 572 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 573 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 574 | // regular non-straddling store |
| 575 | __ Store(out_off, in_reg, param_size); |
| 576 | } else { |
| 577 | // store where input straddles registers and stack |
| 578 | CHECK_EQ(param_size, 8u); |
| 579 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 580 | __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister()); |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 586 | template <PointerSize kPointerSize> |
| 587 | static void SetNativeParameter(JNIMacroAssembler<kPointerSize>* jni_asm, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 588 | JniCallingConvention* jni_conv, |
| 589 | ManagedRegister in_reg) { |
| 590 | if (jni_conv->IsCurrentParamOnStack()) { |
| 591 | FrameOffset dest = jni_conv->CurrentParamStackOffset(); |
| 592 | __ StoreRawPtr(dest, in_reg); |
| 593 | } else { |
| 594 | if (!jni_conv->CurrentParamRegister().Equals(in_reg)) { |
| 595 | __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize()); |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 600 | CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, |
| 601 | uint32_t access_flags, |
| 602 | uint32_t method_idx, |
| 603 | const DexFile& dex_file, |
| 604 | Compiler::JniOptimizationFlags optimization_flags) { |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 605 | if (Is64BitInstructionSet(compiler->GetInstructionSet())) { |
| 606 | return ArtJniCompileMethodInternal<PointerSize::k64>( |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 607 | compiler, access_flags, method_idx, dex_file, optimization_flags); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 608 | } else { |
| 609 | return ArtJniCompileMethodInternal<PointerSize::k32>( |
Igor Murashkin | 9d4b6da | 2016-07-29 09:51:58 -0700 | [diff] [blame^] | 610 | compiler, access_flags, method_idx, dex_file, optimization_flags); |
Andreas Gampe | 3b165bc | 2016-08-01 22:07:04 -0700 | [diff] [blame] | 611 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 612 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 613 | |
| 614 | } // namespace art |