Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: irogers@google.com (Ian Rogers) |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | |
| 4 | #include "jni_compiler.h" |
| 5 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 6 | #include <sys/mman.h> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7 | #include <vector> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 8 | |
| 9 | #include "assembler.h" |
| 10 | #include "calling_convention.h" |
| 11 | #include "jni_internal.h" |
| 12 | #include "macros.h" |
| 13 | #include "managed_register.h" |
| 14 | #include "logging.h" |
| 15 | #include "thread.h" |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 16 | |
| 17 | namespace art { |
| 18 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 19 | static Object* DecodeJObjectInThread(Thread* thread, jobject obj) { |
| 20 | return thread->DecodeJObject(obj); |
| 21 | } |
| 22 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 23 | // Generate the JNI bridge for the given method, general contract: |
| 24 | // - Arguments are in the managed runtime format, either on stack or in |
| 25 | // registers, a reference to the method object is supplied as part of this |
| 26 | // convention. |
| 27 | // |
| 28 | void JniCompiler::Compile(Assembler* jni_asm, Method* native_method) { |
| 29 | CHECK(native_method->IsNative()); |
| 30 | JniCallingConvention jni_conv(native_method); |
| 31 | ManagedRuntimeCallingConvention mr_conv(native_method); |
| 32 | const bool is_static = native_method->IsStatic(); |
Elliott Hughes | 69f5bc6 | 2011-08-24 09:26:14 -0700 | [diff] [blame] | 33 | static Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions)); |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 34 | static Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter)); |
| 35 | static Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 36 | |
| 37 | // 1. Build the frame |
| 38 | const size_t frame_size(jni_conv.FrameSize()); |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 39 | const std::vector<ManagedRegister>& spill_regs = jni_conv.RegsToSpillPreCall(); |
| 40 | jni_asm->BuildFrame(frame_size, mr_conv.MethodRegister(), spill_regs); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 41 | |
| 42 | // 2. Save callee save registers that aren't callee save in the native code |
| 43 | // TODO: implement computing the difference of the callee saves |
| 44 | // and saving |
| 45 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 46 | // 3. Set up the StackIndirectReferenceTable |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 47 | mr_conv.ResetIterator(FrameOffset(frame_size)); |
| 48 | jni_conv.ResetIterator(FrameOffset(0)); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 49 | jni_asm->StoreImmediateToFrame(jni_conv.SirtNumRefsOffset(), |
| 50 | jni_conv.ReferenceCount(), |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 51 | mr_conv.InterproceduralScratchRegister()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 52 | jni_asm->CopyRawPtrFromThread(jni_conv.SirtLinkOffset(), |
| 53 | Thread::TopSirtOffset(), |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 54 | mr_conv.InterproceduralScratchRegister()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 55 | jni_asm->StoreStackOffsetToThread(Thread::TopSirtOffset(), |
| 56 | jni_conv.SirtOffset(), |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 57 | mr_conv.InterproceduralScratchRegister()); |
| 58 | |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 59 | // 4. Place incoming reference arguments into SIRT |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 60 | jni_conv.Next(); // Skip JNIEnv* |
| 61 | // 4.5. Create Class argument for static methods out of passed method |
| 62 | if (is_static) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 63 | FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset(); |
| 64 | // Check sirt offset is within frame |
| 65 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 66 | jni_asm->LoadRef(jni_conv.InterproceduralScratchRegister(), |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 67 | mr_conv.MethodRegister(), Method::DeclaringClassOffset()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 68 | jni_asm->VerifyObject(jni_conv.InterproceduralScratchRegister(), false); |
| 69 | jni_asm->StoreRef(sirt_offset, jni_conv.InterproceduralScratchRegister()); |
| 70 | jni_conv.Next(); // in SIRT so move to next argument |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 71 | } |
| 72 | while (mr_conv.HasNext()) { |
| 73 | CHECK(jni_conv.HasNext()); |
| 74 | bool ref_param = jni_conv.IsCurrentParamAReference(); |
| 75 | CHECK(!ref_param || mr_conv.IsCurrentParamAReference()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 76 | // References need placing in SIRT and the entry value passing |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 77 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 78 | // 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] | 79 | // must be NULL |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 80 | FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset(); |
| 81 | // Check SIRT offset is within frame |
| 82 | CHECK_LT(sirt_offset.Uint32Value(), frame_size); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 83 | bool input_in_reg = mr_conv.IsCurrentParamInRegister(); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 84 | bool input_on_stack = mr_conv.IsCurrentParamOnStack(); |
| 85 | CHECK(input_in_reg || input_on_stack); |
| 86 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 87 | if (input_in_reg) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 88 | ManagedRegister in_reg = mr_conv.CurrentParamRegister(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 89 | jni_asm->VerifyObject(in_reg, mr_conv.IsCurrentArgPossiblyNull()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 90 | jni_asm->StoreRef(sirt_offset, in_reg); |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 91 | } else if (input_on_stack) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 92 | FrameOffset in_off = mr_conv.CurrentParamStackOffset(); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 93 | jni_asm->VerifyObject(in_off, mr_conv.IsCurrentArgPossiblyNull()); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 94 | jni_asm->CopyRef(sirt_offset, in_off, |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 95 | mr_conv.InterproceduralScratchRegister()); |
| 96 | } |
| 97 | } |
| 98 | mr_conv.Next(); |
| 99 | jni_conv.Next(); |
| 100 | } |
| 101 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 102 | // 5. Transition from being in managed to native code |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 103 | // TODO: ensure the transition to native follow a store fence. |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 104 | jni_asm->StoreStackPointerToThread(Thread::TopOfManagedStackOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 105 | jni_asm->StoreImmediateToThread(Thread::StateOffset(), Thread::kNative, |
| 106 | mr_conv.InterproceduralScratchRegister()); |
| 107 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 108 | // 6. 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] | 109 | // time as possible to aid profiling.. |
| 110 | const size_t out_arg_size = jni_conv.OutArgSize(); |
| 111 | jni_asm->IncreaseFrameSize(out_arg_size); |
| 112 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 113 | // 7. Acquire lock for synchronized methods. |
| 114 | if (native_method->IsSynchronized()) { |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 115 | // TODO: preserve incoming arguments in registers |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 116 | mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 117 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
| 118 | jni_conv.Next(); // Skip JNIEnv* |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 119 | // Get SIRT entry for 1st argument |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 120 | if (is_static) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 121 | FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 122 | if (jni_conv.IsCurrentParamOnStack()) { |
| 123 | FrameOffset out_off = jni_conv.CurrentParamStackOffset(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 124 | jni_asm->CreateSirtEntry(out_off, sirt_offset, |
| 125 | mr_conv.InterproceduralScratchRegister(), |
| 126 | false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 127 | } else { |
| 128 | ManagedRegister out_reg = jni_conv.CurrentParamRegister(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 129 | jni_asm->CreateSirtEntry(out_reg, sirt_offset, |
| 130 | ManagedRegister::NoRegister(), false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 131 | } |
| 132 | } else { |
| 133 | CopyParameter(jni_asm, &mr_conv, &jni_conv, frame_size, out_arg_size); |
| 134 | } |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 135 | // Generate JNIEnv* in place and leave a copy in jni_fns_register |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 136 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 137 | ManagedRegister jni_fns_register = |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 138 | jni_conv.InterproceduralScratchRegister(); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 139 | jni_asm->LoadRawPtrFromThread(jni_fns_register, Thread::JniEnvOffset()); |
| 140 | SetNativeParameter(jni_asm, &jni_conv, jni_fns_register); |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 141 | // Call JNIEnv->MonitorEnter(object) |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 142 | jni_asm->LoadRawPtr(jni_fns_register, jni_fns_register, functions); |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 143 | jni_asm->Call(jni_fns_register, monitor_enter, |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 144 | jni_conv.InterproceduralScratchRegister()); |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 145 | jni_asm->FillFromSpillArea(spill_regs, out_arg_size); |
Elliott Hughes | 40ef99e | 2011-08-11 17:44:34 -0700 | [diff] [blame] | 146 | jni_asm->ExceptionPoll(jni_conv.InterproceduralScratchRegister()); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 149 | // 8. Iterate over arguments placing values from managed calling convention in |
| 150 | // to the convention required for a native call (shuffling). For references |
| 151 | // place an index/pointer to the reference after checking whether it is |
| 152 | // NULL (which must be encoded as NULL). |
| 153 | // NB. we do this prior to materializing the JNIEnv* and static's jclass to |
| 154 | // give as many free registers for the shuffle as possible |
| 155 | mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size)); |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 156 | uint32_t args_count = 0; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 157 | while (mr_conv.HasNext()) { |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 158 | args_count++; |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 159 | mr_conv.Next(); |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 160 | } |
Shih-wei Liao | 82da44b | 2011-09-01 00:38:04 -0700 | [diff] [blame] | 161 | |
| 162 | // Do a backward pass over arguments, so that the generated code will be "mov |
| 163 | // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3." |
| 164 | // TODO: A reverse iterator to improve readability. |
| 165 | for (uint32_t i = 0; i < args_count; ++i) { |
| 166 | mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 167 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
| 168 | jni_conv.Next(); // Skip JNIEnv* |
| 169 | if (is_static) { |
| 170 | jni_conv.Next(); // Skip Class for now |
| 171 | } |
| 172 | for (uint32_t j = 0; j < args_count - i - 1; ++j) { |
| 173 | mr_conv.Next(); |
| 174 | jni_conv.Next(); |
| 175 | } |
| 176 | CopyParameter(jni_asm, &mr_conv, &jni_conv, frame_size, out_arg_size); |
| 177 | } |
| 178 | |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 179 | if (is_static) { |
| 180 | // Create argument for Class |
| 181 | mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 182 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
| 183 | jni_conv.Next(); // Skip JNIEnv* |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 184 | FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 185 | if (jni_conv.IsCurrentParamOnStack()) { |
| 186 | FrameOffset out_off = jni_conv.CurrentParamStackOffset(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 187 | jni_asm->CreateSirtEntry(out_off, sirt_offset, |
| 188 | mr_conv.InterproceduralScratchRegister(), |
| 189 | false); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 190 | } else { |
| 191 | ManagedRegister out_reg = jni_conv.CurrentParamRegister(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 192 | jni_asm->CreateSirtEntry(out_reg, sirt_offset, |
| 193 | ManagedRegister::NoRegister(), false); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 194 | } |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 195 | } |
| 196 | // 9. Create 1st argument, the JNI environment ptr |
| 197 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
| 198 | if (jni_conv.IsCurrentParamInRegister()) { |
| 199 | jni_asm->LoadRawPtrFromThread(jni_conv.CurrentParamRegister(), |
| 200 | Thread::JniEnvOffset()); |
| 201 | } else { |
| 202 | jni_asm->CopyRawPtrFromThread(jni_conv.CurrentParamStackOffset(), |
| 203 | Thread::JniEnvOffset(), |
| 204 | jni_conv.InterproceduralScratchRegister()); |
| 205 | } |
| 206 | |
| 207 | // 10. Plant call to native code associated with method |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 208 | if (!jni_conv.IsOutArgRegister(mr_conv.MethodRegister())) { |
| 209 | // Method register shouldn't have been crushed by setting up outgoing |
| 210 | // arguments |
| 211 | jni_asm->Call(mr_conv.MethodRegister(), Method::NativeMethodOffset(), |
| 212 | mr_conv.InterproceduralScratchRegister()); |
| 213 | } else { |
| 214 | jni_asm->Call(jni_conv.MethodStackOffset(), Method::NativeMethodOffset(), |
| 215 | mr_conv.InterproceduralScratchRegister()); |
| 216 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 217 | // 11. Release lock for synchronized methods. |
| 218 | if (native_method->IsSynchronized()) { |
| 219 | mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size)); |
| 220 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
| 221 | jni_conv.Next(); // Skip JNIEnv* |
| 222 | // Save return value |
| 223 | FrameOffset return_save_location = jni_conv.ReturnValueSaveLocation(); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 224 | if (jni_conv.SizeOfReturnValue() != 0) { |
| 225 | FrameOffset return_save_location = jni_conv.ReturnValueSaveLocation(); |
| 226 | CHECK_LT(return_save_location.Uint32Value(), frame_size+out_arg_size); |
| 227 | jni_asm->Store(return_save_location, jni_conv.ReturnRegister(), |
| 228 | jni_conv.SizeOfReturnValue()); |
| 229 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 230 | // Get SIRT entry for 1st argument |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 231 | if (is_static) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 232 | FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 233 | if (jni_conv.IsCurrentParamOnStack()) { |
| 234 | FrameOffset out_off = jni_conv.CurrentParamStackOffset(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 235 | jni_asm->CreateSirtEntry(out_off, sirt_offset, |
| 236 | mr_conv.InterproceduralScratchRegister(), |
| 237 | false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 238 | } else { |
| 239 | ManagedRegister out_reg = jni_conv.CurrentParamRegister(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 240 | jni_asm->CreateSirtEntry(out_reg, sirt_offset, |
| 241 | ManagedRegister::NoRegister(), false); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 242 | } |
| 243 | } else { |
| 244 | CopyParameter(jni_asm, &mr_conv, &jni_conv, frame_size, out_arg_size); |
| 245 | } |
| 246 | // Generate JNIEnv* in place and leave a copy in jni_env_register |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 247 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 248 | ManagedRegister jni_env_register = |
| 249 | jni_conv.InterproceduralScratchRegister(); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 250 | jni_asm->LoadRawPtrFromThread(jni_env_register, Thread::JniEnvOffset()); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 251 | SetNativeParameter(jni_asm, &jni_conv, jni_env_register); |
Ian Rogers | a04d397 | 2011-08-17 11:33:44 -0700 | [diff] [blame] | 252 | // Call JNIEnv->MonitorExit(object) |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 253 | jni_asm->LoadRawPtr(jni_env_register, jni_env_register, functions); |
| 254 | jni_asm->Call(jni_env_register, monitor_exit, |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 255 | jni_conv.InterproceduralScratchRegister()); |
| 256 | // Reload return value |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 257 | if (jni_conv.SizeOfReturnValue() != 0) { |
| 258 | jni_asm->Load(jni_conv.ReturnRegister(), return_save_location, |
| 259 | jni_conv.SizeOfReturnValue()); |
| 260 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 263 | // 12. Release outgoing argument area |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 264 | jni_asm->DecreaseFrameSize(out_arg_size); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 265 | mr_conv.ResetIterator(FrameOffset(frame_size)); |
| 266 | jni_conv.ResetIterator(FrameOffset(0)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 267 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 268 | // 13. Transition from being in native to managed code, possibly entering a |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 269 | // safepoint |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 270 | CHECK(!jni_conv.InterproceduralScratchRegister() |
| 271 | .Equals(jni_conv.ReturnRegister())); // don't clobber result |
| 272 | // Location to preserve result on slow path, ensuring its within the frame |
| 273 | FrameOffset return_save_location = jni_conv.ReturnValueSaveLocation(); |
Ian Rogers | 4ff5dad | 2011-09-07 13:56:27 -0700 | [diff] [blame] | 274 | CHECK(return_save_location.Uint32Value() < frame_size || |
| 275 | jni_conv.SizeOfReturnValue() == 0); |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 276 | jni_asm->SuspendPoll(jni_conv.InterproceduralScratchRegister(), |
| 277 | jni_conv.ReturnRegister(), return_save_location, |
| 278 | jni_conv.SizeOfReturnValue()); |
| 279 | jni_asm->ExceptionPoll(jni_conv.InterproceduralScratchRegister()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 280 | jni_asm->StoreImmediateToThread(Thread::StateOffset(), Thread::kRunnable, |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 281 | jni_conv.InterproceduralScratchRegister()); |
| 282 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 283 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 284 | // 14. Place result in correct register possibly loading from indirect |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 285 | // reference table |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 286 | if (jni_conv.IsReturnAReference()) { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 287 | jni_asm->IncreaseFrameSize(out_arg_size); |
| 288 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 289 | |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 290 | jni_conv.Next(); // Skip Thread* argument |
| 291 | // Pass result as arg2 |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 292 | SetNativeParameter(jni_asm, &jni_conv, jni_conv.ReturnRegister()); |
| 293 | |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 294 | // Pass Thread* |
| 295 | jni_conv.ResetIterator(FrameOffset(out_arg_size)); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 296 | if (jni_conv.IsCurrentParamInRegister()) { |
| 297 | jni_asm->GetCurrentThread(jni_conv.CurrentParamRegister()); |
| 298 | } else { |
| 299 | jni_asm->GetCurrentThread(jni_conv.CurrentParamStackOffset(), |
| 300 | jni_conv.InterproceduralScratchRegister()); |
| 301 | } |
| 302 | |
| 303 | jni_asm->Call(reinterpret_cast<uintptr_t>(DecodeJObjectInThread), |
| 304 | jni_conv.InterproceduralScratchRegister()); |
| 305 | |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 306 | jni_asm->DecreaseFrameSize(out_arg_size); |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 307 | jni_conv.ResetIterator(FrameOffset(0)); |
| 308 | } |
| 309 | jni_asm->Move(mr_conv.ReturnRegister(), jni_conv.ReturnRegister()); |
| 310 | |
| 311 | // 15. Remove SIRT from thread |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 312 | jni_asm->CopyRawPtrToThread(Thread::TopSirtOffset(), jni_conv.SirtLinkOffset(), |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 313 | jni_conv.InterproceduralScratchRegister()); |
| 314 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 315 | // 16. Remove activation |
Ian Rogers | 0d666d8 | 2011-08-14 16:03:46 -0700 | [diff] [blame] | 316 | jni_asm->RemoveFrame(frame_size, spill_regs); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 317 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 318 | // 17. Finalize code generation |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 319 | jni_asm->EmitSlowPaths(); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 320 | size_t cs = jni_asm->CodeSize(); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 321 | ByteArray* managed_code = ByteArray::Alloc(cs); |
| 322 | CHECK(managed_code != NULL); |
| 323 | MemoryRegion code(managed_code->GetData(), managed_code->GetLength()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 324 | jni_asm->FinalizeInstructions(code); |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 325 | native_method->SetCode(managed_code, jni_asm->GetInstructionSet()); |
Shih-wei Liao | d11af15 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 326 | native_method->SetFrameSizeInBytes(frame_size); |
| 327 | native_method->SetReturnPcOffsetInBytes(jni_conv.ReturnPcOffset()); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Shih-wei Liao | 668512a | 2011-09-01 14:18:34 -0700 | [diff] [blame] | 330 | void JniCompiler::SetNativeParameter(Assembler *jni_asm, |
| 331 | JniCallingConvention *jni_conv, |
| 332 | ManagedRegister in_reg) { |
| 333 | if (jni_conv->IsCurrentParamOnStack()) { |
| 334 | FrameOffset dest = jni_conv->CurrentParamStackOffset(); |
| 335 | jni_asm->StoreRawPtr(dest, in_reg); |
| 336 | } else { |
| 337 | if (!jni_conv->CurrentParamRegister().Equals(in_reg)) { |
| 338 | jni_asm->Move(jni_conv->CurrentParamRegister(), in_reg); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 343 | // Copy a single parameter from the managed to the JNI calling convention |
| 344 | void JniCompiler::CopyParameter(Assembler* jni_asm, |
| 345 | ManagedRuntimeCallingConvention* mr_conv, |
| 346 | JniCallingConvention* jni_conv, |
| 347 | size_t frame_size, size_t out_arg_size) { |
| 348 | bool input_in_reg = mr_conv->IsCurrentParamInRegister(); |
| 349 | bool output_in_reg = jni_conv->IsCurrentParamInRegister(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 350 | FrameOffset sirt_offset(0); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 351 | bool null_allowed = false; |
| 352 | bool ref_param = jni_conv->IsCurrentParamAReference(); |
| 353 | CHECK(!ref_param || mr_conv->IsCurrentParamAReference()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 354 | // input may be in register, on stack or both - but not none! |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 355 | CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 356 | if (output_in_reg) { // output shouldn't straddle registers and stack |
| 357 | CHECK(!jni_conv->IsCurrentParamOnStack()); |
| 358 | } else { |
| 359 | CHECK(jni_conv->IsCurrentParamOnStack()); |
| 360 | } |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 361 | // References need placing in SIRT and the entry address passing |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 362 | if (ref_param) { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 363 | null_allowed = mr_conv->IsCurrentArgPossiblyNull(); |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 364 | // Compute SIRT offset. Note null is placed in the SIRT but the jobject |
| 365 | // 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] | 366 | // as with regular references). |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 367 | sirt_offset = jni_conv->CurrentParamSirtEntryOffset(); |
| 368 | // Check SIRT offset is within frame. |
| 369 | CHECK_LT(sirt_offset.Uint32Value(), (frame_size+out_arg_size)); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 370 | } |
| 371 | if (input_in_reg && output_in_reg) { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 372 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 373 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 374 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 375 | jni_asm->CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 376 | } else { |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 377 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 378 | // regular non-straddling move |
| 379 | jni_asm->Move(out_reg, in_reg); |
| 380 | } else { |
| 381 | UNIMPLEMENTED(FATAL); // we currently don't expect to see this case |
| 382 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 383 | } |
| 384 | } else if (!input_in_reg && !output_in_reg) { |
| 385 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 386 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 387 | jni_asm->CreateSirtEntry(out_off, sirt_offset, |
| 388 | mr_conv->InterproceduralScratchRegister(), |
| 389 | null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 390 | } else { |
| 391 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 392 | size_t param_size = mr_conv->CurrentParamSize(); |
| 393 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 394 | jni_asm->Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), |
| 395 | param_size); |
| 396 | } |
| 397 | } else if (!input_in_reg && output_in_reg) { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 398 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 399 | ManagedRegister out_reg = jni_conv->CurrentParamRegister(); |
| 400 | // Check that incoming stack arguments are above the current stack frame. |
| 401 | CHECK_GT(in_off.Uint32Value(), frame_size); |
| 402 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 403 | jni_asm->CreateSirtEntry(out_reg, sirt_offset, |
| 404 | ManagedRegister::NoRegister(), null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 405 | } else { |
| 406 | unsigned int param_size = mr_conv->CurrentParamSize(); |
| 407 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
| 408 | jni_asm->Load(out_reg, in_off, param_size); |
| 409 | } |
| 410 | } else { |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 411 | CHECK(input_in_reg && !output_in_reg); |
| 412 | ManagedRegister in_reg = mr_conv->CurrentParamRegister(); |
| 413 | FrameOffset out_off = jni_conv->CurrentParamStackOffset(); |
| 414 | // Check outgoing argument is within frame |
| 415 | CHECK_LT(out_off.Uint32Value(), frame_size); |
| 416 | if (ref_param) { |
Ian Rogers | 408f79a | 2011-08-23 18:22:33 -0700 | [diff] [blame] | 417 | // TODO: recycle value in in_reg rather than reload from SIRT |
| 418 | jni_asm->CreateSirtEntry(out_off, sirt_offset, |
| 419 | mr_conv->InterproceduralScratchRegister(), |
| 420 | null_allowed); |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 421 | } else { |
| 422 | size_t param_size = mr_conv->CurrentParamSize(); |
| 423 | CHECK_EQ(param_size, jni_conv->CurrentParamSize()); |
Ian Rogers | 7a99c11 | 2011-09-07 12:48:27 -0700 | [diff] [blame] | 424 | if (!mr_conv->IsCurrentParamOnStack()) { |
| 425 | // regular non-straddling store |
| 426 | jni_asm->Store(out_off, in_reg, param_size); |
| 427 | } else { |
| 428 | // store where input straddles registers and stack |
| 429 | CHECK_EQ(param_size, 8u); |
| 430 | FrameOffset in_off = mr_conv->CurrentParamStackOffset(); |
| 431 | jni_asm->StoreSpanning(out_off, in_reg, in_off, |
| 432 | mr_conv->InterproceduralScratchRegister()); |
| 433 | } |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 438 | JniCompiler::JniCompiler() {} |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 439 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 440 | JniCompiler::~JniCompiler() {} |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 441 | |
| 442 | } // namespace art |