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