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