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