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 | |
| 17 | #include <algorithm> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include "base/logging.h" |
| 21 | #include "base/macros.h" |
| 22 | #include "calling_convention.h" |
| 23 | #include "class_linker.h" |
| 24 | #include "compiled_method.h" |
| 25 | #include "dex_file-inl.h" |
| 26 | #include "driver/compiler_driver.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 27 | #include "entrypoints/quick/quick_entrypoints.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | #include "jni_internal.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 29 | #include "utils/assembler.h" |
| 30 | #include "utils/managed_register.h" |
| 31 | #include "utils/arm/managed_register_arm.h" |
| 32 | #include "utils/mips/managed_register_mips.h" |
| 33 | #include "utils/x86/managed_register_x86.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 34 | #include "thread.h" |
| 35 | #include "UniquePtr.h" |
| 36 | |
| 37 | #define __ jni_asm-> |
| 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | static void CopyParameter(Assembler* jni_asm, |
| 42 | ManagedRuntimeCallingConvention* mr_conv, |
| 43 | JniCallingConvention* jni_conv, |
| 44 | size_t frame_size, size_t out_arg_size); |
| 45 | static void SetNativeParameter(Assembler* jni_asm, |
| 46 | JniCallingConvention* jni_conv, |
| 47 | ManagedRegister in_reg); |
| 48 | |
| 49 | // Generate the JNI bridge for the given method, general contract: |
| 50 | // - Arguments are in the managed runtime format, either on stack or in |
| 51 | // registers, a reference to the method object is supplied as part of this |
| 52 | // convention. |
| 53 | // |
| 54 | CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver& compiler, |
| 55 | uint32_t access_flags, uint32_t method_idx, |
| 56 | const DexFile& dex_file) { |
| 57 | const bool is_native = (access_flags & kAccNative) != 0; |
| 58 | CHECK(is_native); |
| 59 | const bool is_static = (access_flags & kAccStatic) != 0; |
| 60 | const bool is_synchronized = (access_flags & kAccSynchronized) != 0; |
| 61 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
| 62 | InstructionSet instruction_set = compiler.GetInstructionSet(); |
| 63 | if (instruction_set == kThumb2) { |
| 64 | instruction_set = kArm; |
| 65 | } |
| 66 | // Calling conventions used to iterate over parameters to method |
| 67 | UniquePtr<JniCallingConvention> main_jni_conv( |
| 68 | JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set)); |
| 69 | bool reference_return = main_jni_conv->IsReturnAReference(); |
| 70 | |
| 71 | UniquePtr<ManagedRuntimeCallingConvention> mr_conv( |
| 72 | ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set)); |
| 73 | |
| 74 | // Calling conventions to call into JNI method "end" possibly passing a returned reference, the |
| 75 | // method and the current thread. |
| 76 | size_t jni_end_arg_count = 0; |
| 77 | if (reference_return) { jni_end_arg_count++; } |
| 78 | if (is_synchronized) { jni_end_arg_count++; } |
| 79 | const char* jni_end_shorty = jni_end_arg_count == 0 ? "I" |
| 80 | : (jni_end_arg_count == 1 ? "II" : "III"); |
| 81 | UniquePtr<JniCallingConvention> end_jni_conv( |
| 82 | JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set)); |
| 83 | |
| 84 | |
| 85 | // Assembler that holds generated instructions |
| 86 | UniquePtr<Assembler> jni_asm(Assembler::Create(instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 87 | |
| 88 | // Offsets into data structures |
| 89 | // TODO: if cross compiling these offsets are for the host not the target |
| 90 | const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions)); |
| 91 | const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter)); |
| 92 | const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit)); |
| 93 | |
| 94 | // 1. Build the frame saving all callee saves |
| 95 | const size_t frame_size(main_jni_conv->FrameSize()); |
| 96 | const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters(); |
| 97 | __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills()); |
| 98 | |
| 99 | // 2. Set up the StackIndirectReferenceTable |
| 100 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 101 | main_jni_conv->ResetIterator(FrameOffset(0)); |
| 102 | __ StoreImmediateToFrame(main_jni_conv->SirtNumRefsOffset(), |
| 103 | main_jni_conv->ReferenceCount(), |
| 104 | mr_conv->InterproceduralScratchRegister()); |
| 105 | __ CopyRawPtrFromThread(main_jni_conv->SirtLinkOffset(), |
| 106 | Thread::TopSirtOffset(), |
| 107 | mr_conv->InterproceduralScratchRegister()); |
| 108 | __ StoreStackOffsetToThread(Thread::TopSirtOffset(), |
| 109 | main_jni_conv->SirtOffset(), |
| 110 | mr_conv->InterproceduralScratchRegister()); |
| 111 | |
| 112 | // 3. Place incoming reference arguments into SIRT |
| 113 | main_jni_conv->Next(); // Skip JNIEnv* |
| 114 | // 3.5. Create Class argument for static methods out of passed method |
| 115 | if (is_static) { |
| 116 | FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset(); |
| 117 | // Check sirt offset is within frame |
| 118 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
| 119 | __ LoadRef(main_jni_conv->InterproceduralScratchRegister(), |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 120 | mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 121 | __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false); |
| 122 | __ StoreRef(sirt_offset, main_jni_conv->InterproceduralScratchRegister()); |
| 123 | main_jni_conv->Next(); // in SIRT so move to next argument |
| 124 | } |
| 125 | while (mr_conv->HasNext()) { |
| 126 | CHECK(main_jni_conv->HasNext()); |
| 127 | bool ref_param = main_jni_conv->IsCurrentParamAReference(); |
| 128 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
| 129 | // References need placing in SIRT and the entry value passing |
| 130 | if (ref_param) { |
| 131 | // Compute SIRT entry, note null is placed in the SIRT but its boxed value |
| 132 | // must be NULL |
| 133 | FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset(); |
| 134 | // Check SIRT offset is within frame and doesn't run into the saved segment state |
| 135 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
| 136 | CHECK_NE(sirt_offset.Uint32Value(), |
| 137 | main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value()); |
| 138 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 139 | bool input_on_stack = mr_conv->IsCurrentParamOnStack(); |
| 140 | CHECK(input_in_reg || input_on_stack); |
| 141 | |
| 142 | if (input_in_reg) { |
| 143 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 144 | __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull()); |
| 145 | __ StoreRef(sirt_offset, in_reg); |
| 146 | } else if (input_on_stack) { |
| 147 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 148 | __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull()); |
| 149 | __ CopyRef(sirt_offset, in_off, |
| 150 | mr_conv->InterproceduralScratchRegister()); |
| 151 | } |
| 152 | } |
| 153 | mr_conv->Next(); |
| 154 | main_jni_conv->Next(); |
| 155 | } |
| 156 | |
| 157 | // 4. Write out the end of the quick frames. |
| 158 | __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset()); |
| 159 | __ StoreImmediateToThread(Thread::TopOfManagedStackPcOffset(), 0, |
| 160 | mr_conv->InterproceduralScratchRegister()); |
| 161 | |
| 162 | // 5. Move frame down to allow space for out going args. |
| 163 | const size_t main_out_arg_size = main_jni_conv->OutArgSize(); |
| 164 | const size_t end_out_arg_size = end_jni_conv->OutArgSize(); |
| 165 | const size_t max_out_arg_size = std::max(main_out_arg_size, end_out_arg_size); |
| 166 | __ IncreaseFrameSize(max_out_arg_size); |
| 167 | |
| 168 | |
| 169 | // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable |
| 170 | // can occur. The result is the saved JNI local state that is restored by the exit call. We |
| 171 | // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer |
| 172 | // arguments. |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 173 | ThreadOffset jni_start = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(pJniMethodStartSynchronized) |
| 174 | : QUICK_ENTRYPOINT_OFFSET(pJniMethodStart); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 175 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 176 | FrameOffset locked_object_sirt_offset(0); |
| 177 | if (is_synchronized) { |
| 178 | // Pass object for locking. |
| 179 | main_jni_conv->Next(); // Skip JNIEnv. |
| 180 | locked_object_sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset(); |
| 181 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 182 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 183 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
| 184 | __ CreateSirtEntry(out_off, locked_object_sirt_offset, |
| 185 | mr_conv->InterproceduralScratchRegister(), |
| 186 | false); |
| 187 | } else { |
| 188 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
| 189 | __ CreateSirtEntry(out_reg, locked_object_sirt_offset, |
| 190 | ManagedRegister::NoRegister(), false); |
| 191 | } |
| 192 | main_jni_conv->Next(); |
| 193 | } |
| 194 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 195 | __ GetCurrentThread(main_jni_conv->CurrentParamRegister()); |
| 196 | __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start), |
| 197 | main_jni_conv->InterproceduralScratchRegister()); |
| 198 | } else { |
| 199 | __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(), |
| 200 | main_jni_conv->InterproceduralScratchRegister()); |
| 201 | __ Call(ThreadOffset(jni_start), main_jni_conv->InterproceduralScratchRegister()); |
| 202 | } |
| 203 | if (is_synchronized) { // Check for exceptions from monitor enter. |
| 204 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size); |
| 205 | } |
| 206 | FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset(); |
| 207 | __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4); |
| 208 | |
| 209 | // 7. Iterate over arguments placing values from managed calling convention in |
| 210 | // to the convention required for a native call (shuffling). For references |
| 211 | // place an index/pointer to the reference after checking whether it is |
| 212 | // NULL (which must be encoded as NULL). |
| 213 | // Note: we do this prior to materializing the JNIEnv* and static's jclass to |
| 214 | // give as many free registers for the shuffle as possible |
| 215 | mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size)); |
| 216 | uint32_t args_count = 0; |
| 217 | while (mr_conv->HasNext()) { |
| 218 | args_count++; |
| 219 | mr_conv->Next(); |
| 220 | } |
| 221 | |
| 222 | // Do a backward pass over arguments, so that the generated code will be "mov |
| 223 | // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3." |
| 224 | // TODO: A reverse iterator to improve readability. |
| 225 | for (uint32_t i = 0; i < args_count; ++i) { |
| 226 | mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size)); |
| 227 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 228 | main_jni_conv->Next(); // Skip JNIEnv*. |
| 229 | if (is_static) { |
| 230 | main_jni_conv->Next(); // Skip Class for now. |
| 231 | } |
| 232 | // Skip to the argument we're interested in. |
| 233 | for (uint32_t j = 0; j < args_count - i - 1; ++j) { |
| 234 | mr_conv->Next(); |
| 235 | main_jni_conv->Next(); |
| 236 | } |
| 237 | CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size); |
| 238 | } |
| 239 | if (is_static) { |
| 240 | // Create argument for Class |
| 241 | mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size)); |
| 242 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 243 | main_jni_conv->Next(); // Skip JNIEnv* |
| 244 | FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset(); |
| 245 | if (main_jni_conv->IsCurrentParamOnStack()) { |
| 246 | FrameOffset out_off = main_jni_conv->CurrentParamStackOffset(); |
| 247 | __ CreateSirtEntry(out_off, sirt_offset, |
| 248 | mr_conv->InterproceduralScratchRegister(), |
| 249 | false); |
| 250 | } else { |
| 251 | ManagedRegister out_reg = main_jni_conv->CurrentParamRegister(); |
| 252 | __ CreateSirtEntry(out_reg, sirt_offset, |
| 253 | ManagedRegister::NoRegister(), false); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // 8. Create 1st argument, the JNI environment ptr. |
| 258 | main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size)); |
| 259 | // Register that will hold local indirect reference table |
| 260 | if (main_jni_conv->IsCurrentParamInRegister()) { |
| 261 | ManagedRegister jni_env = main_jni_conv->CurrentParamRegister(); |
| 262 | DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister())); |
| 263 | __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset()); |
| 264 | } else { |
| 265 | FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset(); |
| 266 | __ CopyRawPtrFromThread(jni_env, Thread::JniEnvOffset(), |
| 267 | main_jni_conv->InterproceduralScratchRegister()); |
| 268 | } |
| 269 | |
| 270 | // 9. Plant call to native code associated with method. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 271 | __ Call(main_jni_conv->MethodStackOffset(), mirror::ArtMethod::NativeMethodOffset(), |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 272 | mr_conv->InterproceduralScratchRegister()); |
| 273 | |
| 274 | // 10. Fix differences in result widths. |
| 275 | if (instruction_set == kX86) { |
| 276 | if (main_jni_conv->GetReturnType() == Primitive::kPrimByte || |
| 277 | main_jni_conv->GetReturnType() == Primitive::kPrimShort) { |
| 278 | __ SignExtend(main_jni_conv->ReturnRegister(), |
| 279 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 280 | } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean || |
| 281 | main_jni_conv->GetReturnType() == Primitive::kPrimChar) { |
| 282 | __ ZeroExtend(main_jni_conv->ReturnRegister(), |
| 283 | Primitive::ComponentSize(main_jni_conv->GetReturnType())); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // 11. Save return value |
| 288 | FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation(); |
| 289 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
| 290 | if (instruction_set == kMips && main_jni_conv->GetReturnType() == Primitive::kPrimDouble && |
| 291 | return_save_location.Uint32Value() % 8 != 0) { |
| 292 | // Ensure doubles are 8-byte aligned for MIPS |
| 293 | return_save_location = FrameOffset(return_save_location.Uint32Value() + kPointerSize); |
| 294 | } |
| 295 | CHECK_LT(return_save_location.Uint32Value(), frame_size+main_out_arg_size); |
| 296 | __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue()); |
| 297 | } |
| 298 | |
| 299 | // 12. Call into JNI method end possibly passing a returned reference, the method and the current |
| 300 | // thread. |
| 301 | end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size)); |
Ian Rogers | 848871b | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 302 | ThreadOffset jni_end(-1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 303 | if (reference_return) { |
| 304 | // Pass result. |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 305 | jni_end = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(pJniMethodEndWithReferenceSynchronized) |
| 306 | : QUICK_ENTRYPOINT_OFFSET(pJniMethodEndWithReference); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 307 | SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister()); |
| 308 | end_jni_conv->Next(); |
| 309 | } else { |
Ian Rogers | 7655f29 | 2013-07-29 11:07:13 -0700 | [diff] [blame] | 310 | jni_end = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(pJniMethodEndSynchronized) |
| 311 | : QUICK_ENTRYPOINT_OFFSET(pJniMethodEnd); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 312 | } |
| 313 | // Pass saved local reference state. |
| 314 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 315 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
| 316 | __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4); |
| 317 | } else { |
| 318 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
| 319 | __ Load(out_reg, saved_cookie_offset, 4); |
| 320 | } |
| 321 | end_jni_conv->Next(); |
| 322 | if (is_synchronized) { |
| 323 | // Pass object for unlocking. |
| 324 | if (end_jni_conv->IsCurrentParamOnStack()) { |
| 325 | FrameOffset out_off = end_jni_conv->CurrentParamStackOffset(); |
| 326 | __ CreateSirtEntry(out_off, locked_object_sirt_offset, |
| 327 | end_jni_conv->InterproceduralScratchRegister(), |
| 328 | false); |
| 329 | } else { |
| 330 | ManagedRegister out_reg = end_jni_conv->CurrentParamRegister(); |
| 331 | __ CreateSirtEntry(out_reg, locked_object_sirt_offset, |
| 332 | ManagedRegister::NoRegister(), false); |
| 333 | } |
| 334 | end_jni_conv->Next(); |
| 335 | } |
| 336 | if (end_jni_conv->IsCurrentParamInRegister()) { |
| 337 | __ GetCurrentThread(end_jni_conv->CurrentParamRegister()); |
| 338 | __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end), |
| 339 | end_jni_conv->InterproceduralScratchRegister()); |
| 340 | } else { |
| 341 | __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(), |
| 342 | end_jni_conv->InterproceduralScratchRegister()); |
| 343 | __ Call(ThreadOffset(jni_end), end_jni_conv->InterproceduralScratchRegister()); |
| 344 | } |
| 345 | |
| 346 | // 13. Reload return value |
| 347 | if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) { |
| 348 | __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue()); |
| 349 | } |
| 350 | |
| 351 | // 14. Move frame up now we're done with the out arg space. |
| 352 | __ DecreaseFrameSize(max_out_arg_size); |
| 353 | |
| 354 | // 15. Process pending exceptions from JNI call or monitor exit. |
| 355 | __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0); |
| 356 | |
| 357 | // 16. Remove activation - no need to restore callee save registers because we didn't clobber |
| 358 | // them. |
| 359 | __ RemoveFrame(frame_size, std::vector<ManagedRegister>()); |
| 360 | |
| 361 | // 17. Finalize code generation |
| 362 | __ EmitSlowPaths(); |
| 363 | size_t cs = __ CodeSize(); |
| 364 | std::vector<uint8_t> managed_code(cs); |
| 365 | MemoryRegion code(&managed_code[0], managed_code.size()); |
| 366 | __ FinalizeInstructions(code); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 367 | return new CompiledMethod(compiler, |
| 368 | instruction_set, |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 369 | managed_code, |
| 370 | frame_size, |
| 371 | main_jni_conv->CoreSpillMask(), |
| 372 | main_jni_conv->FpSpillMask()); |
| 373 | } |
| 374 | |
| 375 | // Copy a single parameter from the managed to the JNI calling convention |
| 376 | static void CopyParameter(Assembler* jni_asm, |
| 377 | ManagedRuntimeCallingConvention* mr_conv, |
| 378 | JniCallingConvention* jni_conv, |
| 379 | size_t frame_size, size_t out_arg_size) { |
| 380 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 381 | bool output_in_reg = jni_conv->IsCurrentParamInRegister(); |
| 382 | FrameOffset sirt_offset(0); |
| 383 | bool null_allowed = false; |
| 384 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 385 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
| 386 | // input may be in register, on stack or both - but not none! |
| 387 | CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack()); |
| 388 | if (output_in_reg) { // output shouldn't straddle registers and stack |
| 389 | CHECK(!jni_conv->IsCurrentParamOnStack()); |
| 390 | } else { |
| 391 | CHECK(jni_conv->IsCurrentParamOnStack()); |
| 392 | } |
| 393 | // References need placing in SIRT and the entry address passing |
| 394 | if (ref_param) { |
| 395 | null_allowed = mr_conv->IsCurrentArgPossiblyNull(); |
| 396 | // Compute SIRT offset. Note null is placed in the SIRT but the jobject |
| 397 | // passed to the native code must be null (not a pointer into the SIRT |
| 398 | // as with regular references). |
| 399 | sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 400 | // Check SIRT offset is within frame. |
| 401 | CHECK_LT(sirt_offset.Uint32Value(), (frame_size + out_arg_size)); |
| 402 | } |
| 403 | if (input_in_reg && output_in_reg) { |
| 404 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 405 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 406 | if (ref_param) { |
| 407 | __ CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed); |
| 408 | } else { |
| 409 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 410 | // regular non-straddling move |
| 411 | __ Move(out_reg, in_reg, mr_conv->CurrentParamSize()); |
| 412 | } else { |
| 413 | UNIMPLEMENTED(FATAL); // we currently don't expect to see this case |
| 414 | } |
| 415 | } |
| 416 | } else if (!input_in_reg && !output_in_reg) { |
| 417 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 418 | if (ref_param) { |
| 419 | __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(), |
| 420 | null_allowed); |
| 421 | } else { |
| 422 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 423 | size_t param_size = mr_conv->CurrentParamSize(); |
| 424 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 425 | __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size); |
| 426 | } |
| 427 | } else if (!input_in_reg && output_in_reg) { |
| 428 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 429 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 430 | // Check that incoming stack arguments are above the current stack frame. |
| 431 | CHECK_GT(in_off.Uint32Value(), frame_size); |
| 432 | if (ref_param) { |
| 433 | __ CreateSirtEntry(out_reg, sirt_offset, ManagedRegister::NoRegister(), null_allowed); |
| 434 | } else { |
| 435 | size_t param_size = mr_conv->CurrentParamSize(); |
| 436 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 437 | __ Load(out_reg, in_off, param_size); |
| 438 | } |
| 439 | } else { |
| 440 | CHECK(input_in_reg && !output_in_reg); |
| 441 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 442 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 443 | // Check outgoing argument is within frame |
| 444 | CHECK_LT(out_off.Uint32Value(), frame_size); |
| 445 | if (ref_param) { |
| 446 | // TODO: recycle value in in_reg rather than reload from SIRT |
| 447 | __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(), |
| 448 | null_allowed); |
| 449 | } else { |
| 450 | size_t param_size = mr_conv->CurrentParamSize(); |
| 451 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 452 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 453 | // regular non-straddling store |
| 454 | __ Store(out_off, in_reg, param_size); |
| 455 | } else { |
| 456 | // store where input straddles registers and stack |
| 457 | CHECK_EQ(param_size, 8u); |
| 458 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 459 | __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister()); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static void SetNativeParameter(Assembler* jni_asm, |
| 466 | JniCallingConvention* jni_conv, |
| 467 | ManagedRegister in_reg) { |
| 468 | if (jni_conv->IsCurrentParamOnStack()) { |
| 469 | FrameOffset dest = jni_conv->CurrentParamStackOffset(); |
| 470 | __ StoreRawPtr(dest, in_reg); |
| 471 | } else { |
| 472 | if (!jni_conv->CurrentParamRegister().Equals(in_reg)) { |
| 473 | __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize()); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | } // namespace art |
| 479 | |
| 480 | extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver& compiler, |
| 481 | uint32_t access_flags, uint32_t method_idx, |
| 482 | const art::DexFile& dex_file) { |
| 483 | return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file); |
| 484 | } |