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 | |
| 17 | #include "jni_compiler.h" |
| 18 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 20 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 21 | |
| 22 | #include "assembler.h" |
| 23 | #include "calling_convention.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 24 | #include "class_linker.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 25 | #include "compiled_method.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 26 | #include "constants.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 27 | #include "jni_internal.h" |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 28 | #include "logging.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "macros.h" |
| 30 | #include "managed_register.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 31 | #include "thread.h" |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 32 | #include "UniquePtr.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 36 | JniCompiler::JniCompiler(InstructionSet instruction_set) { |
| 37 | if (instruction_set == kThumb2) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 38 | // currently only ARM code generation is supported |
| 39 | instruction_set_ = kArm; |
| 40 | } else { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 41 | instruction_set_ = instruction_set; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
| 45 | JniCompiler::~JniCompiler() {} |
| 46 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 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 | // |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 52 | CompiledMethod* JniCompiler::Compile(uint32_t access_flags, uint32_t method_idx, |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 53 | const ClassLoader* class_loader, const DexFile& dex_file) { |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 54 | CHECK((access_flags & kAccNative) != 0); |
| 55 | const bool is_static = (access_flags & kAccStatic) != 0; |
| 56 | const bool is_synchronized = (access_flags & kAccSynchronized) != 0; |
| 57 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 58 | // Calling conventions used to iterate over parameters to method |
| 59 | UniquePtr<JniCallingConvention> jni_conv( |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 60 | JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set_)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 61 | UniquePtr<ManagedRuntimeCallingConvention> mr_conv( |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 62 | ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set_)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 63 | |
| 64 | // Assembler that holds generated instructions |
| 65 | UniquePtr<Assembler> jni_asm(Assembler::Create(instruction_set_)); |
| 66 | #define __ jni_asm-> |
| 67 | |
| 68 | // Offsets into data structures |
| 69 | // TODO: if cross compiling these offsets are for the host not the target |
| 70 | const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions)); |
| 71 | const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter)); |
| 72 | const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit)); |
| 73 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 74 | // 1. Build the frame saving all callee saves |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 75 | const size_t frame_size(jni_conv->FrameSize()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 76 | const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters(); |
| 77 | __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 78 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 79 | // 2. Set up the StackIndirectReferenceTable |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 80 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 81 | jni_conv->ResetIterator(FrameOffset(0)); |
| 82 | __ StoreImmediateToFrame(jni_conv->SirtNumRefsOffset(), |
| 83 | jni_conv->ReferenceCount(), |
| 84 | mr_conv->InterproceduralScratchRegister()); |
| 85 | __ CopyRawPtrFromThread(jni_conv->SirtLinkOffset(), |
| 86 | Thread::TopSirtOffset(), |
| 87 | mr_conv->InterproceduralScratchRegister()); |
| 88 | __ StoreStackOffsetToThread(Thread::TopSirtOffset(), |
| 89 | jni_conv->SirtOffset(), |
| 90 | mr_conv->InterproceduralScratchRegister()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 91 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 92 | // 3. Place incoming reference arguments into SIRT |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 93 | jni_conv->Next(); // Skip JNIEnv* |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 94 | // 3.5. Create Class argument for static methods out of passed method |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 95 | if (is_static) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 96 | FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 97 | // Check sirt offset is within frame |
| 98 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 99 | __ LoadRef(jni_conv->InterproceduralScratchRegister(), |
| 100 | mr_conv->MethodRegister(), Method::DeclaringClassOffset()); |
| 101 | __ VerifyObject(jni_conv->InterproceduralScratchRegister(), false); |
| 102 | __ StoreRef(sirt_offset, jni_conv->InterproceduralScratchRegister()); |
| 103 | jni_conv->Next(); // in SIRT so move to next argument |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 104 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 105 | while (mr_conv->HasNext()) { |
| 106 | CHECK(jni_conv->HasNext()); |
| 107 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 108 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 109 | // References need placing in SIRT and the entry value passing |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 110 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 111 | // Compute SIRT entry, note null is placed in the SIRT but its boxed value |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | // must be NULL |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 113 | FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 114 | // Check SIRT offset is within frame and doesn't run into the saved segment state |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 115 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 116 | CHECK_NE(sirt_offset.Uint32Value(), |
| 117 | jni_conv->SavedLocalReferenceCookieOffset().Uint32Value()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 118 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 119 | bool input_on_stack = mr_conv->IsCurrentParamOnStack(); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 120 | CHECK(input_in_reg || input_on_stack); |
| 121 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 122 | if (input_in_reg) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 123 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 124 | __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull()); |
| 125 | __ StoreRef(sirt_offset, in_reg); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 126 | } else if (input_on_stack) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 127 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 128 | __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull()); |
| 129 | __ CopyRef(sirt_offset, in_off, |
| 130 | mr_conv->InterproceduralScratchRegister()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 133 | mr_conv->Next(); |
| 134 | jni_conv->Next(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 137 | // 4. Transition from being in managed to native code. Save the top_of_managed_stack_ |
| 138 | // so that the managed stack can be crawled while in native code. Clear the corresponding |
| 139 | // PC value that has no meaning for the this frame. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 140 | __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 141 | __ StoreImmediateToThread(Thread::TopOfManagedStackPcOffset(), 0, |
| 142 | mr_conv->InterproceduralScratchRegister()); |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 143 | ChangeThreadState(jni_asm.get(), Thread::kNative, |
| 144 | mr_conv->InterproceduralScratchRegister(), |
| 145 | ManagedRegister::NoRegister(), FrameOffset(0), 0); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 146 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 147 | // 5. Move frame down to allow space for out going args. Do for as short a |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 148 | // time as possible to aid profiling.. |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 149 | const size_t out_arg_size = jni_conv->OutArgSize(); |
| 150 | __ IncreaseFrameSize(out_arg_size); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 151 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 152 | // 6. Acquire lock for synchronized methods. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 153 | if (is_synchronized) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 154 | // Compute arguments in registers to preserve |
| 155 | mr_conv->ResetIterator(FrameOffset(frame_size + out_arg_size)); |
| 156 | std::vector<ManagedRegister> live_argument_regs; |
| 157 | while (mr_conv->HasNext()) { |
| 158 | if (mr_conv->IsCurrentParamInRegister()) { |
| 159 | live_argument_regs.push_back(mr_conv->CurrentParamRegister()); |
| 160 | } |
| 161 | mr_conv->Next(); |
| 162 | } |
| 163 | |
| 164 | // Copy arguments to preserve to callee save registers |
| 165 | CHECK_LE(live_argument_regs.size(), callee_save_regs.size()); |
| 166 | for (size_t i = 0; i < live_argument_regs.size(); i++) { |
| 167 | __ Move(callee_save_regs.at(i), live_argument_regs.at(i)); |
| 168 | } |
| 169 | |
| 170 | // Get SIRT entry for 1st argument (jclass or this) to be 1st argument to |
| 171 | // monitor enter |
| 172 | mr_conv->ResetIterator(FrameOffset(frame_size + out_arg_size)); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 173 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
| 174 | jni_conv->Next(); // Skip JNIEnv* |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 175 | if (is_static) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 176 | FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 177 | if (jni_conv->IsCurrentParamOnStack()) { |
| 178 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 179 | __ CreateSirtEntry(out_off, sirt_offset, |
| 180 | mr_conv->InterproceduralScratchRegister(), |
| 181 | false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 182 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 183 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 184 | __ CreateSirtEntry(out_reg, sirt_offset, |
| 185 | ManagedRegister::NoRegister(), false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 186 | } |
| 187 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 188 | CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size, |
| 189 | out_arg_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 190 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 191 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 192 | // Generate JNIEnv* in place and leave a copy in jni_fns_register |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 193 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 194 | ManagedRegister jni_fns_register = |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 195 | jni_conv->InterproceduralScratchRegister(); |
| 196 | __ LoadRawPtrFromThread(jni_fns_register, Thread::JniEnvOffset()); |
| 197 | SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_fns_register); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 198 | |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 199 | // Call JNIEnv->MonitorEnter(object) |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 200 | __ LoadRawPtr(jni_fns_register, jni_fns_register, functions); |
| 201 | __ Call(jni_fns_register, monitor_enter, |
| 202 | jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 203 | |
| 204 | // Check for exceptions |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 205 | __ ExceptionPoll(jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 206 | |
| 207 | // Restore live arguments |
| 208 | for (size_t i = 0; i < live_argument_regs.size(); i++) { |
| 209 | __ Move(live_argument_regs.at(i), callee_save_regs.at(i)); |
| 210 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 213 | // 7. Iterate over arguments placing values from managed calling convention in |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 214 | // to the convention required for a native call (shuffling). For references |
| 215 | // place an index/pointer to the reference after checking whether it is |
| 216 | // NULL (which must be encoded as NULL). |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 217 | // Note: we do this prior to materializing the JNIEnv* and static's jclass to |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 218 | // give as many free registers for the shuffle as possible |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 219 | mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size)); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 220 | uint32_t args_count = 0; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 221 | while (mr_conv->HasNext()) { |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 222 | args_count++; |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 223 | mr_conv->Next(); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 224 | } |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 225 | |
| 226 | // Do a backward pass over arguments, so that the generated code will be "mov |
| 227 | // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3." |
| 228 | // TODO: A reverse iterator to improve readability. |
| 229 | for (uint32_t i = 0; i < args_count; ++i) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 230 | mr_conv->ResetIterator(FrameOffset(frame_size + out_arg_size)); |
| 231 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
| 232 | jni_conv->Next(); // Skip JNIEnv* |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 233 | if (is_static) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 234 | jni_conv->Next(); // Skip Class for now |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 235 | } |
| 236 | for (uint32_t j = 0; j < args_count - i - 1; ++j) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 237 | mr_conv->Next(); |
| 238 | jni_conv->Next(); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 239 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 240 | CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size, out_arg_size); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 243 | if (is_static) { |
| 244 | // Create argument for Class |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 245 | mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 246 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
| 247 | jni_conv->Next(); // Skip JNIEnv* |
| 248 | FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 249 | if (jni_conv->IsCurrentParamOnStack()) { |
| 250 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 251 | __ CreateSirtEntry(out_off, sirt_offset, |
| 252 | mr_conv->InterproceduralScratchRegister(), |
| 253 | false); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 254 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 255 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 256 | __ CreateSirtEntry(out_reg, sirt_offset, |
| 257 | ManagedRegister::NoRegister(), false); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 258 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 259 | } |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 260 | // 8. Create 1st argument, the JNI environment ptr and save the top of the local reference table |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 261 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 262 | // Register that will hold local indirect reference table |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 263 | if (jni_conv->IsCurrentParamInRegister()) { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 264 | ManagedRegister jni_env = jni_conv->CurrentParamRegister(); |
| 265 | DCHECK(!jni_env.Equals(jni_conv->InterproceduralScratchRegister())); |
| 266 | __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset()); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 267 | // Frame[saved_local_ref_cookie_offset] = env->local_ref_cookie |
| 268 | __ Copy(jni_conv->SavedLocalReferenceCookieOffset(), |
| 269 | jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 270 | jni_conv->InterproceduralScratchRegister(), 4); |
| 271 | // env->local_ref_cookie = env->locals.segment_state |
| 272 | __ Copy(jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 273 | jni_env, JNIEnvExt::SegmentStateOffset(), |
| 274 | jni_conv->InterproceduralScratchRegister(), 4); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 275 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 276 | FrameOffset jni_env = jni_conv->CurrentParamStackOffset(); |
| 277 | __ CopyRawPtrFromThread(jni_env, Thread::JniEnvOffset(), |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 278 | jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 279 | // Frame[saved_local_ref_cookie_offset] = env->local_ref_cookie |
| 280 | __ Copy(jni_conv->SavedLocalReferenceCookieOffset(), |
| 281 | jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 282 | jni_conv->InterproceduralScratchRegister(), 4); |
| 283 | // env->local_ref_cookie = env->locals.segment_state |
| 284 | __ Copy(jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 285 | jni_env, JNIEnvExt::SegmentStateOffset(), |
| 286 | jni_conv->InterproceduralScratchRegister(), 4); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 289 | // 9. Plant call to native code associated with method |
Ian Rogers | ad42e13 | 2011-09-17 20:23:33 -0700 | [diff] [blame] | 290 | if (!jni_conv->IsMethodRegisterClobberedPreCall()) { |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 291 | // Method register shouldn't have been crushed by setting up outgoing |
| 292 | // arguments |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 293 | __ Call(mr_conv->MethodRegister(), Method::NativeMethodOffset(), |
| 294 | mr_conv->InterproceduralScratchRegister()); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 295 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 296 | __ Call(jni_conv->MethodStackOffset(), Method::NativeMethodOffset(), |
| 297 | mr_conv->InterproceduralScratchRegister()); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 298 | } |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 299 | |
| 300 | // 10. Release lock for synchronized methods. |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 301 | if (is_synchronized) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 302 | mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 303 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
| 304 | jni_conv->Next(); // Skip JNIEnv* |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 305 | // Save return value |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 306 | FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation(); |
| 307 | if (jni_conv->SizeOfReturnValue() != 0) { |
| 308 | FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation(); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 309 | CHECK_LT(return_save_location.Uint32Value(), frame_size+out_arg_size); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 310 | __ Store(return_save_location, jni_conv->ReturnRegister(), |
| 311 | jni_conv->SizeOfReturnValue()); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 312 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 313 | // Get SIRT entry for 1st argument |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 314 | if (is_static) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 315 | FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 316 | if (jni_conv->IsCurrentParamOnStack()) { |
| 317 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 318 | __ CreateSirtEntry(out_off, sirt_offset, |
| 319 | mr_conv->InterproceduralScratchRegister(), |
| 320 | false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 321 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 322 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 323 | __ CreateSirtEntry(out_reg, sirt_offset, |
| 324 | ManagedRegister::NoRegister(), false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 325 | } |
| 326 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 327 | CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size, |
| 328 | out_arg_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 329 | } |
| 330 | // Generate JNIEnv* in place and leave a copy in jni_env_register |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 331 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 332 | ManagedRegister jni_env_register = |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 333 | jni_conv->InterproceduralScratchRegister(); |
| 334 | __ LoadRawPtrFromThread(jni_env_register, Thread::JniEnvOffset()); |
| 335 | SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_env_register); |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 336 | // Call JNIEnv->MonitorExit(object) |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 337 | __ LoadRawPtr(jni_env_register, jni_env_register, functions); |
| 338 | __ Call(jni_env_register, monitor_exit, |
| 339 | jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 340 | // Reload return value |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 341 | if (jni_conv->SizeOfReturnValue() != 0) { |
| 342 | __ Load(jni_conv->ReturnRegister(), return_save_location, |
| 343 | jni_conv->SizeOfReturnValue()); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 344 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 347 | // 11. Release outgoing argument area |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 348 | __ DecreaseFrameSize(out_arg_size); |
| 349 | mr_conv->ResetIterator(FrameOffset(frame_size)); |
| 350 | jni_conv->ResetIterator(FrameOffset(0)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 351 | |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 352 | // 12. Transition from being in native to managed code, possibly entering a |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 353 | // safepoint |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 354 | // Don't clobber result |
| 355 | CHECK(!jni_conv->InterproceduralScratchRegister().Equals(jni_conv->ReturnRegister())); |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 356 | // Location to preserve result on slow path, ensuring its within the frame |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 357 | FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation(); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 358 | CHECK(return_save_location.Uint32Value() < frame_size || |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 359 | jni_conv->SizeOfReturnValue() == 0); |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 360 | ChangeThreadState(jni_asm.get(), Thread::kRunnable, |
| 361 | jni_conv->InterproceduralScratchRegister(), |
| 362 | jni_conv->ReturnRegister(), return_save_location, |
| 363 | jni_conv->SizeOfReturnValue()); |
| 364 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 365 | // 13. Place result in correct register possibly loading from indirect |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 366 | // reference table |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 367 | if (jni_conv->IsReturnAReference()) { |
| 368 | __ IncreaseFrameSize(out_arg_size); |
| 369 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 370 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 371 | jni_conv->Next(); // Skip Thread* argument |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 372 | // Pass result as arg2 |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 373 | SetNativeParameter(jni_asm.get(), jni_conv.get(), |
| 374 | jni_conv->ReturnRegister()); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 375 | |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 376 | // Pass Thread* |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 377 | jni_conv->ResetIterator(FrameOffset(out_arg_size)); |
| 378 | if (jni_conv->IsCurrentParamInRegister()) { |
| 379 | __ GetCurrentThread(jni_conv->CurrentParamRegister()); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 380 | __ Call(jni_conv->CurrentParamRegister(), |
| 381 | Offset(OFFSETOF_MEMBER(Thread, pDecodeJObjectInThread)), |
| 382 | jni_conv->InterproceduralScratchRegister()); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 383 | } else { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 384 | __ GetCurrentThread(jni_conv->CurrentParamStackOffset(), |
| 385 | jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 386 | __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pDecodeJObjectInThread)), |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 387 | jni_conv->InterproceduralScratchRegister()); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 390 | __ DecreaseFrameSize(out_arg_size); |
| 391 | jni_conv->ResetIterator(FrameOffset(0)); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 392 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 393 | __ Move(mr_conv->ReturnRegister(), jni_conv->ReturnRegister()); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 394 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 395 | // 14. Restore segment state and remove SIRT from thread |
Ian Rogers | 5a7a74a | 2011-09-26 16:32:29 -0700 | [diff] [blame] | 396 | { |
| 397 | ManagedRegister jni_env = jni_conv->InterproceduralScratchRegister(); |
| 398 | __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset()); |
| 399 | // env->locals.segment_state = env->local_ref_cookie |
| 400 | __ Copy(jni_env, JNIEnvExt::SegmentStateOffset(), |
| 401 | jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 402 | jni_conv->ReturnScratchRegister(), 4); |
| 403 | // env->local_ref_cookie = Frame[saved_local_ref_cookie_offset] |
| 404 | __ Copy(jni_env, JNIEnvExt::LocalRefCookieOffset(), |
| 405 | jni_conv->SavedLocalReferenceCookieOffset(), |
| 406 | jni_conv->ReturnScratchRegister(), 4); |
| 407 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 408 | __ CopyRawPtrToThread(Thread::TopSirtOffset(), jni_conv->SirtLinkOffset(), |
| 409 | jni_conv->InterproceduralScratchRegister()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 410 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 411 | // 15. Check for pending exception and forward if there |
| 412 | __ ExceptionPoll(jni_conv->InterproceduralScratchRegister()); |
| 413 | |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 414 | // 16. Remove activation |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 415 | if (is_synchronized) { |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 416 | __ RemoveFrame(frame_size, callee_save_regs); |
| 417 | } else { |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 418 | // no need to restore callee save registers because we didn't |
| 419 | // clobber them while locking the monitor. |
Ian Rogers | bdb0391 | 2011-09-14 00:55:44 -0700 | [diff] [blame] | 420 | __ RemoveFrame(frame_size, std::vector<ManagedRegister>()); |
| 421 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 422 | |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 423 | // 17. Finalize code generation |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 424 | __ EmitSlowPaths(); |
| 425 | size_t cs = __ CodeSize(); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 426 | std::vector<uint8_t> managed_code(cs); |
| 427 | MemoryRegion code(&managed_code[0], managed_code.size()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 428 | __ FinalizeInstructions(code); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 429 | return new CompiledMethod(instruction_set_, |
| 430 | managed_code, |
| 431 | frame_size, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 432 | jni_conv->CoreSpillMask(), |
| 433 | jni_conv->FpSpillMask()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 434 | #undef __ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 437 | void JniCompiler::SetNativeParameter(Assembler* jni_asm, |
| 438 | JniCallingConvention* jni_conv, |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 439 | ManagedRegister in_reg) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 440 | #define __ jni_asm-> |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 441 | if (jni_conv->IsCurrentParamOnStack()) { |
| 442 | FrameOffset dest = jni_conv->CurrentParamStackOffset(); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 443 | __ StoreRawPtr(dest, in_reg); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 444 | } else { |
| 445 | if (!jni_conv->CurrentParamRegister().Equals(in_reg)) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 446 | __ Move(jni_conv->CurrentParamRegister(), in_reg); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 449 | #undef __ |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 452 | // Copy a single parameter from the managed to the JNI calling convention |
| 453 | void JniCompiler::CopyParameter(Assembler* jni_asm, |
| 454 | ManagedRuntimeCallingConvention* mr_conv, |
| 455 | JniCallingConvention* jni_conv, |
| 456 | size_t frame_size, size_t out_arg_size) { |
| 457 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 458 | bool output_in_reg = jni_conv->IsCurrentParamInRegister(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 459 | FrameOffset sirt_offset(0); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 460 | bool null_allowed = false; |
| 461 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 462 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 463 | // input may be in register, on stack or both - but not none! |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 464 | CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 465 | if (output_in_reg) { // output shouldn't straddle registers and stack |
| 466 | CHECK(!jni_conv->IsCurrentParamOnStack()); |
| 467 | } else { |
| 468 | CHECK(jni_conv->IsCurrentParamOnStack()); |
| 469 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 470 | // References need placing in SIRT and the entry address passing |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 471 | if (ref_param) { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 472 | null_allowed = mr_conv->IsCurrentArgPossiblyNull(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 473 | // Compute SIRT offset. Note null is placed in the SIRT but the jobject |
| 474 | // passed to the native code must be null (not a pointer into the SIRT |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 475 | // as with regular references). |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 476 | sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 477 | // Check SIRT offset is within frame. |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 478 | CHECK_LT(sirt_offset.Uint32Value(), (frame_size + out_arg_size)); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 479 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 480 | #define __ jni_asm-> |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 481 | if (input_in_reg && output_in_reg) { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 482 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 483 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 484 | if (ref_param) { |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 485 | __ CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 486 | } else { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 487 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 488 | // regular non-straddling move |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 489 | __ Move(out_reg, in_reg); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 490 | } else { |
| 491 | UNIMPLEMENTED(FATAL); // we currently don't expect to see this case |
| 492 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 493 | } |
| 494 | } else if (!input_in_reg && !output_in_reg) { |
| 495 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 496 | if (ref_param) { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 497 | __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(), |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 498 | null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 499 | } else { |
| 500 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 501 | size_t param_size = mr_conv->CurrentParamSize(); |
| 502 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 503 | __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 504 | } |
| 505 | } else if (!input_in_reg && output_in_reg) { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 506 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 507 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 508 | // Check that incoming stack arguments are above the current stack frame. |
| 509 | CHECK_GT(in_off.Uint32Value(), frame_size); |
| 510 | if (ref_param) { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 511 | __ CreateSirtEntry(out_reg, sirt_offset, ManagedRegister::NoRegister(), null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 512 | } else { |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 513 | size_t param_size = mr_conv->CurrentParamSize(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 514 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 515 | __ Load(out_reg, in_off, param_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 516 | } |
| 517 | } else { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 518 | CHECK(input_in_reg && !output_in_reg); |
| 519 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 520 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 521 | // Check outgoing argument is within frame |
| 522 | CHECK_LT(out_off.Uint32Value(), frame_size); |
| 523 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 524 | // TODO: recycle value in in_reg rather than reload from SIRT |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 525 | __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(), |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 526 | null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 527 | } else { |
| 528 | size_t param_size = mr_conv->CurrentParamSize(); |
| 529 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 530 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 531 | // regular non-straddling store |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 532 | __ Store(out_off, in_reg, param_size); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 533 | } else { |
| 534 | // store where input straddles registers and stack |
| 535 | CHECK_EQ(param_size, 8u); |
| 536 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
Ian Rogers | dc51b79 | 2011-09-22 20:41:37 -0700 | [diff] [blame] | 537 | __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 538 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
Ian Rogers | 2c8f653 | 2011-09-02 17:16:34 -0700 | [diff] [blame] | 541 | #undef __ |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Ian Rogers | e5de95b | 2011-09-18 20:31:38 -0700 | [diff] [blame] | 544 | void JniCompiler::ChangeThreadState(Assembler* jni_asm, Thread::State new_state, |
| 545 | ManagedRegister scratch, ManagedRegister return_reg, |
| 546 | FrameOffset return_save_location, |
| 547 | size_t return_size) { |
| 548 | /* |
| 549 | * This code mirrors that of Thread::SetState where detail is given on why |
| 550 | * barriers occur when they do. |
| 551 | */ |
| 552 | #define __ jni_asm-> |
| 553 | if (new_state == Thread::kRunnable) { |
| 554 | /* |
| 555 | * Change our status to Thread::kRunnable. The transition requires |
| 556 | * that we check for pending suspension, because the VM considers |
| 557 | * us to be "asleep" in all other states, and another thread could |
| 558 | * be performing a GC now. |
| 559 | */ |
| 560 | __ StoreImmediateToThread(Thread::StateOffset(), Thread::kRunnable, scratch); |
| 561 | __ MemoryBarrier(scratch); |
| 562 | __ SuspendPoll(scratch, return_reg, return_save_location, return_size); |
| 563 | } else { |
| 564 | /* |
| 565 | * Not changing to Thread::kRunnable. No additional work required. |
| 566 | */ |
| 567 | __ MemoryBarrier(scratch); |
| 568 | __ StoreImmediateToThread(Thread::StateOffset(), new_state, scratch); |
| 569 | } |
| 570 | #undef __ |
| 571 | } |
| 572 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 573 | } // namespace art |