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