blob: a7a7d12f3f6d84e92562b14a9ce1b2f7893a136e [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Carlstrom578bbdc2011-07-21 14:07:47 -070016
17#include "jni_compiler.h"
18
Ian Rogersb033c752011-07-20 12:22:35 -070019#include <sys/mman.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070020#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021
22#include "assembler.h"
23#include "calling_convention.h"
Ian Rogers0571d352011-11-03 19:51:38 -070024#include "class_linker.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070025#include "compiled_method.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "jni_internal.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028#include "logging.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "macros.h"
30#include "managed_register.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031#include "thread.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070032#include "UniquePtr.h"
Ian Rogersb033c752011-07-20 12:22:35 -070033
34namespace art {
35
Brian Carlstrom3320cf42011-10-04 14:58:28 -070036JniCompiler::JniCompiler(InstructionSet instruction_set) {
37 if (instruction_set == kThumb2) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070038 // currently only ARM code generation is supported
39 instruction_set_ = kArm;
40 } else {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041 instruction_set_ = instruction_set;
Ian Rogers2c8f6532011-09-02 17:16:34 -070042 }
43}
44
45JniCompiler::~JniCompiler() {}
46
Ian Rogersb033c752011-07-20 12:22:35 -070047// 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 Rogers169c9a72011-11-13 20:13:17 -080052CompiledMethod* JniCompiler::Compile(uint32_t access_flags, uint32_t method_idx,
Ian Rogers0571d352011-11-03 19:51:38 -070053 const ClassLoader* class_loader, const DexFile& dex_file) {
Ian Rogers169c9a72011-11-13 20:13:17 -080054 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 Rogers2c8f6532011-09-02 17:16:34 -070058 // Calling conventions used to iterate over parameters to method
59 UniquePtr<JniCallingConvention> jni_conv(
Ian Rogers169c9a72011-11-13 20:13:17 -080060 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set_));
Ian Rogers2c8f6532011-09-02 17:16:34 -070061 UniquePtr<ManagedRuntimeCallingConvention> mr_conv(
Ian Rogers169c9a72011-11-13 20:13:17 -080062 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set_));
Ian Rogers2c8f6532011-09-02 17:16:34 -070063
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 Rogersbdb03912011-09-14 00:55:44 -070074 // 1. Build the frame saving all callee saves
Ian Rogers2c8f6532011-09-02 17:16:34 -070075 const size_t frame_size(jni_conv->FrameSize());
Ian Rogersbdb03912011-09-14 00:55:44 -070076 const std::vector<ManagedRegister>& callee_save_regs = jni_conv->CalleeSaveRegisters();
77 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs);
Ian Rogersb033c752011-07-20 12:22:35 -070078
Ian Rogersbdb03912011-09-14 00:55:44 -070079 // 2. Set up the StackIndirectReferenceTable
Ian Rogers2c8f6532011-09-02 17:16:34 -070080 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 Rogersb033c752011-07-20 12:22:35 -070091
Ian Rogersbdb03912011-09-14 00:55:44 -070092 // 3. Place incoming reference arguments into SIRT
Ian Rogers2c8f6532011-09-02 17:16:34 -070093 jni_conv->Next(); // Skip JNIEnv*
Ian Rogersbdb03912011-09-14 00:55:44 -070094 // 3.5. Create Class argument for static methods out of passed method
Ian Rogersb033c752011-07-20 12:22:35 -070095 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070096 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -070097 // Check sirt offset is within frame
98 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -070099 __ 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 Rogersb033c752011-07-20 12:22:35 -0700104 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700105 while (mr_conv->HasNext()) {
106 CHECK(jni_conv->HasNext());
107 bool ref_param = jni_conv->IsCurrentParamAReference();
108 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Ian Rogers408f79a2011-08-23 18:22:33 -0700109 // References need placing in SIRT and the entry value passing
Ian Rogersb033c752011-07-20 12:22:35 -0700110 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700111 // Compute SIRT entry, note null is placed in the SIRT but its boxed value
Ian Rogersb033c752011-07-20 12:22:35 -0700112 // must be NULL
Ian Rogers2c8f6532011-09-02 17:16:34 -0700113 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700114 // Check SIRT offset is within frame and doesn't run into the saved segment state
Ian Rogers408f79a2011-08-23 18:22:33 -0700115 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700116 CHECK_NE(sirt_offset.Uint32Value(),
117 jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
119 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700120 CHECK(input_in_reg || input_on_stack);
121
Ian Rogersb033c752011-07-20 12:22:35 -0700122 if (input_in_reg) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700123 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
124 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
125 __ StoreRef(sirt_offset, in_reg);
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700126 } else if (input_on_stack) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700127 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
128 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
129 __ CopyRef(sirt_offset, in_off,
130 mr_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700131 }
132 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700133 mr_conv->Next();
134 jni_conv->Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700135 }
136
Ian Rogersbdb03912011-09-14 00:55:44 -0700137 // 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 Rogers2c8f6532011-09-02 17:16:34 -0700140 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset());
Ian Rogersbdb03912011-09-14 00:55:44 -0700141 __ StoreImmediateToThread(Thread::TopOfManagedStackPcOffset(), 0,
142 mr_conv->InterproceduralScratchRegister());
Ian Rogerse5de95b2011-09-18 20:31:38 -0700143 ChangeThreadState(jni_asm.get(), Thread::kNative,
144 mr_conv->InterproceduralScratchRegister(),
145 ManagedRegister::NoRegister(), FrameOffset(0), 0);
Ian Rogersb033c752011-07-20 12:22:35 -0700146
Ian Rogersbdb03912011-09-14 00:55:44 -0700147 // 5. Move frame down to allow space for out going args. Do for as short a
Ian Rogersb033c752011-07-20 12:22:35 -0700148 // time as possible to aid profiling..
Ian Rogers2c8f6532011-09-02 17:16:34 -0700149 const size_t out_arg_size = jni_conv->OutArgSize();
150 __ IncreaseFrameSize(out_arg_size);
Ian Rogersb033c752011-07-20 12:22:35 -0700151
Ian Rogersbdb03912011-09-14 00:55:44 -0700152 // 6. Acquire lock for synchronized methods.
Ian Rogers169c9a72011-11-13 20:13:17 -0800153 if (is_synchronized) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700154 // 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 Rogers2c8f6532011-09-02 17:16:34 -0700173 jni_conv->ResetIterator(FrameOffset(out_arg_size));
174 jni_conv->Next(); // Skip JNIEnv*
Ian Rogersdf20fe02011-07-20 20:34:16 -0700175 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700176 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 Rogersdf20fe02011-07-20 20:34:16 -0700182 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700183 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
184 __ CreateSirtEntry(out_reg, sirt_offset,
185 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700186 }
187 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700188 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size,
189 out_arg_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700190 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700191
Shih-wei Liao668512a2011-09-01 14:18:34 -0700192 // Generate JNIEnv* in place and leave a copy in jni_fns_register
Ian Rogers2c8f6532011-09-02 17:16:34 -0700193 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700194 ManagedRegister jni_fns_register =
Ian Rogers2c8f6532011-09-02 17:16:34 -0700195 jni_conv->InterproceduralScratchRegister();
196 __ LoadRawPtrFromThread(jni_fns_register, Thread::JniEnvOffset());
197 SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_fns_register);
Ian Rogersbdb03912011-09-14 00:55:44 -0700198
Ian Rogersa04d3972011-08-17 11:33:44 -0700199 // Call JNIEnv->MonitorEnter(object)
Ian Rogers2c8f6532011-09-02 17:16:34 -0700200 __ LoadRawPtr(jni_fns_register, jni_fns_register, functions);
201 __ Call(jni_fns_register, monitor_enter,
202 jni_conv->InterproceduralScratchRegister());
Ian Rogersbdb03912011-09-14 00:55:44 -0700203
204 // Check for exceptions
Ian Rogers2c8f6532011-09-02 17:16:34 -0700205 __ ExceptionPoll(jni_conv->InterproceduralScratchRegister());
Ian Rogersbdb03912011-09-14 00:55:44 -0700206
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 Rogersdf20fe02011-07-20 20:34:16 -0700211 }
212
Ian Rogersbdb03912011-09-14 00:55:44 -0700213 // 7. Iterate over arguments placing values from managed calling convention in
Ian Rogersb033c752011-07-20 12:22:35 -0700214 // 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 Hughes20cde902011-10-04 17:37:27 -0700217 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
Ian Rogersb033c752011-07-20 12:22:35 -0700218 // give as many free registers for the shuffle as possible
Ian Rogers2c8f6532011-09-02 17:16:34 -0700219 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700220 uint32_t args_count = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700221 while (mr_conv->HasNext()) {
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700222 args_count++;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700223 mr_conv->Next();
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700224 }
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700225
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 Rogers2c8f6532011-09-02 17:16:34 -0700230 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 Liao82da44b2011-09-01 00:38:04 -0700233 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700234 jni_conv->Next(); // Skip Class for now
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700235 }
236 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700237 mr_conv->Next();
238 jni_conv->Next();
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700239 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700240 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size, out_arg_size);
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700241 }
242
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700243 if (is_static) {
244 // Create argument for Class
Ian Rogers2c8f6532011-09-02 17:16:34 -0700245 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 Rogersb033c752011-07-20 12:22:35 -0700254 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700255 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
256 __ CreateSirtEntry(out_reg, sirt_offset,
257 ManagedRegister::NoRegister(), false);
Ian Rogersb033c752011-07-20 12:22:35 -0700258 }
Ian Rogersb033c752011-07-20 12:22:35 -0700259 }
Ian Rogersdc51b792011-09-22 20:41:37 -0700260 // 8. Create 1st argument, the JNI environment ptr and save the top of the local reference table
Ian Rogers2c8f6532011-09-02 17:16:34 -0700261 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Ian Rogersdc51b792011-09-22 20:41:37 -0700262 // Register that will hold local indirect reference table
Ian Rogers2c8f6532011-09-02 17:16:34 -0700263 if (jni_conv->IsCurrentParamInRegister()) {
Ian Rogersdc51b792011-09-22 20:41:37 -0700264 ManagedRegister jni_env = jni_conv->CurrentParamRegister();
265 DCHECK(!jni_env.Equals(jni_conv->InterproceduralScratchRegister()));
266 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset());
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700267 // 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 Rogersb033c752011-07-20 12:22:35 -0700275 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700276 FrameOffset jni_env = jni_conv->CurrentParamStackOffset();
277 __ CopyRawPtrFromThread(jni_env, Thread::JniEnvOffset(),
Ian Rogers2c8f6532011-09-02 17:16:34 -0700278 jni_conv->InterproceduralScratchRegister());
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700279 // 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 Rogersb033c752011-07-20 12:22:35 -0700287 }
288
Ian Rogersbdb03912011-09-14 00:55:44 -0700289 // 9. Plant call to native code associated with method
Ian Rogersad42e132011-09-17 20:23:33 -0700290 if (!jni_conv->IsMethodRegisterClobberedPreCall()) {
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700291 // Method register shouldn't have been crushed by setting up outgoing
292 // arguments
Ian Rogers2c8f6532011-09-02 17:16:34 -0700293 __ Call(mr_conv->MethodRegister(), Method::NativeMethodOffset(),
294 mr_conv->InterproceduralScratchRegister());
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700295 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700296 __ Call(jni_conv->MethodStackOffset(), Method::NativeMethodOffset(),
297 mr_conv->InterproceduralScratchRegister());
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700298 }
Ian Rogersbdb03912011-09-14 00:55:44 -0700299
300 // 10. Release lock for synchronized methods.
Ian Rogers169c9a72011-11-13 20:13:17 -0800301 if (is_synchronized) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700302 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
303 jni_conv->ResetIterator(FrameOffset(out_arg_size));
304 jni_conv->Next(); // Skip JNIEnv*
Ian Rogersdf20fe02011-07-20 20:34:16 -0700305 // Save return value
Ian Rogers2c8f6532011-09-02 17:16:34 -0700306 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
307 if (jni_conv->SizeOfReturnValue() != 0) {
308 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700309 CHECK_LT(return_save_location.Uint32Value(), frame_size+out_arg_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700310 __ Store(return_save_location, jni_conv->ReturnRegister(),
311 jni_conv->SizeOfReturnValue());
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700312 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700313 // Get SIRT entry for 1st argument
Ian Rogersdf20fe02011-07-20 20:34:16 -0700314 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700315 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 Rogersdf20fe02011-07-20 20:34:16 -0700321 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700322 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
323 __ CreateSirtEntry(out_reg, sirt_offset,
324 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700325 }
326 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700327 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size,
328 out_arg_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700329 }
330 // Generate JNIEnv* in place and leave a copy in jni_env_register
Ian Rogers2c8f6532011-09-02 17:16:34 -0700331 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700332 ManagedRegister jni_env_register =
Ian Rogers2c8f6532011-09-02 17:16:34 -0700333 jni_conv->InterproceduralScratchRegister();
334 __ LoadRawPtrFromThread(jni_env_register, Thread::JniEnvOffset());
335 SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_env_register);
Ian Rogersa04d3972011-08-17 11:33:44 -0700336 // Call JNIEnv->MonitorExit(object)
Ian Rogers2c8f6532011-09-02 17:16:34 -0700337 __ LoadRawPtr(jni_env_register, jni_env_register, functions);
338 __ Call(jni_env_register, monitor_exit,
339 jni_conv->InterproceduralScratchRegister());
Ian Rogersdf20fe02011-07-20 20:34:16 -0700340 // Reload return value
Ian Rogers2c8f6532011-09-02 17:16:34 -0700341 if (jni_conv->SizeOfReturnValue() != 0) {
342 __ Load(jni_conv->ReturnRegister(), return_save_location,
343 jni_conv->SizeOfReturnValue());
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700344 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700345 }
346
Ian Rogersbdb03912011-09-14 00:55:44 -0700347 // 11. Release outgoing argument area
Ian Rogers2c8f6532011-09-02 17:16:34 -0700348 __ DecreaseFrameSize(out_arg_size);
349 mr_conv->ResetIterator(FrameOffset(frame_size));
350 jni_conv->ResetIterator(FrameOffset(0));
Ian Rogersb033c752011-07-20 12:22:35 -0700351
Ian Rogersbdb03912011-09-14 00:55:44 -0700352 // 12. Transition from being in native to managed code, possibly entering a
Ian Rogersb033c752011-07-20 12:22:35 -0700353 // safepoint
Ian Rogerse5de95b2011-09-18 20:31:38 -0700354 // Don't clobber result
355 CHECK(!jni_conv->InterproceduralScratchRegister().Equals(jni_conv->ReturnRegister()));
Ian Rogers45a76cb2011-07-21 22:00:15 -0700356 // Location to preserve result on slow path, ensuring its within the frame
Ian Rogers2c8f6532011-09-02 17:16:34 -0700357 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700358 CHECK(return_save_location.Uint32Value() < frame_size ||
Ian Rogers2c8f6532011-09-02 17:16:34 -0700359 jni_conv->SizeOfReturnValue() == 0);
Ian Rogerse5de95b2011-09-18 20:31:38 -0700360 ChangeThreadState(jni_asm.get(), Thread::kRunnable,
361 jni_conv->InterproceduralScratchRegister(),
362 jni_conv->ReturnRegister(), return_save_location,
363 jni_conv->SizeOfReturnValue());
364
Elliott Hughes726079d2011-10-07 18:43:44 -0700365 // 13. Place result in correct register possibly loading from indirect
Ian Rogers408f79a2011-08-23 18:22:33 -0700366 // reference table
Ian Rogers2c8f6532011-09-02 17:16:34 -0700367 if (jni_conv->IsReturnAReference()) {
368 __ IncreaseFrameSize(out_arg_size);
369 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Ian Rogersb033c752011-07-20 12:22:35 -0700370
Ian Rogers2c8f6532011-09-02 17:16:34 -0700371 jni_conv->Next(); // Skip Thread* argument
Ian Rogers7a99c112011-09-07 12:48:27 -0700372 // Pass result as arg2
Ian Rogers2c8f6532011-09-02 17:16:34 -0700373 SetNativeParameter(jni_asm.get(), jni_conv.get(),
374 jni_conv->ReturnRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700375
Ian Rogers7a99c112011-09-07 12:48:27 -0700376 // Pass Thread*
Ian Rogers2c8f6532011-09-02 17:16:34 -0700377 jni_conv->ResetIterator(FrameOffset(out_arg_size));
378 if (jni_conv->IsCurrentParamInRegister()) {
379 __ GetCurrentThread(jni_conv->CurrentParamRegister());
Brian Carlstrom16192862011-09-12 17:50:06 -0700380 __ Call(jni_conv->CurrentParamRegister(),
381 Offset(OFFSETOF_MEMBER(Thread, pDecodeJObjectInThread)),
382 jni_conv->InterproceduralScratchRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700383 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700384 __ GetCurrentThread(jni_conv->CurrentParamStackOffset(),
385 jni_conv->InterproceduralScratchRegister());
Ian Rogersbdb03912011-09-14 00:55:44 -0700386 __ Call(ThreadOffset(OFFSETOF_MEMBER(Thread, pDecodeJObjectInThread)),
Brian Carlstrom16192862011-09-12 17:50:06 -0700387 jni_conv->InterproceduralScratchRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700388 }
389
Ian Rogers2c8f6532011-09-02 17:16:34 -0700390 __ DecreaseFrameSize(out_arg_size);
391 jni_conv->ResetIterator(FrameOffset(0));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700392 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700393 __ Move(mr_conv->ReturnRegister(), jni_conv->ReturnRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700394
Elliott Hughes726079d2011-10-07 18:43:44 -0700395 // 14. Restore segment state and remove SIRT from thread
Ian Rogers5a7a74a2011-09-26 16:32:29 -0700396 {
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 Rogers2c8f6532011-09-02 17:16:34 -0700408 __ CopyRawPtrToThread(Thread::TopSirtOffset(), jni_conv->SirtLinkOffset(),
409 jni_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700410
Elliott Hughes726079d2011-10-07 18:43:44 -0700411 // 15. Check for pending exception and forward if there
412 __ ExceptionPoll(jni_conv->InterproceduralScratchRegister());
413
Ian Rogerse5de95b2011-09-18 20:31:38 -0700414 // 16. Remove activation
Ian Rogers169c9a72011-11-13 20:13:17 -0800415 if (is_synchronized) {
Ian Rogersbdb03912011-09-14 00:55:44 -0700416 __ RemoveFrame(frame_size, callee_save_regs);
417 } else {
Elliott Hughes726079d2011-10-07 18:43:44 -0700418 // no need to restore callee save registers because we didn't
419 // clobber them while locking the monitor.
Ian Rogersbdb03912011-09-14 00:55:44 -0700420 __ RemoveFrame(frame_size, std::vector<ManagedRegister>());
421 }
Ian Rogersb033c752011-07-20 12:22:35 -0700422
Ian Rogerse5de95b2011-09-18 20:31:38 -0700423 // 17. Finalize code generation
Ian Rogers2c8f6532011-09-02 17:16:34 -0700424 __ EmitSlowPaths();
425 size_t cs = __ CodeSize();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700426 std::vector<uint8_t> managed_code(cs);
427 MemoryRegion code(&managed_code[0], managed_code.size());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700428 __ FinalizeInstructions(code);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700429 return new CompiledMethod(instruction_set_,
430 managed_code,
431 frame_size,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700432 jni_conv->CoreSpillMask(),
433 jni_conv->FpSpillMask());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700434#undef __
Ian Rogersb033c752011-07-20 12:22:35 -0700435}
436
Ian Rogers2c8f6532011-09-02 17:16:34 -0700437void JniCompiler::SetNativeParameter(Assembler* jni_asm,
438 JniCallingConvention* jni_conv,
Shih-wei Liao668512a2011-09-01 14:18:34 -0700439 ManagedRegister in_reg) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700440#define __ jni_asm->
Shih-wei Liao668512a2011-09-01 14:18:34 -0700441 if (jni_conv->IsCurrentParamOnStack()) {
442 FrameOffset dest = jni_conv->CurrentParamStackOffset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700443 __ StoreRawPtr(dest, in_reg);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700444 } else {
445 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700446 __ Move(jni_conv->CurrentParamRegister(), in_reg);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700447 }
448 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700449#undef __
Shih-wei Liao668512a2011-09-01 14:18:34 -0700450}
451
Ian Rogersdf20fe02011-07-20 20:34:16 -0700452// Copy a single parameter from the managed to the JNI calling convention
453void 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 Rogers408f79a2011-08-23 18:22:33 -0700459 FrameOffset sirt_offset(0);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700460 bool null_allowed = false;
461 bool ref_param = jni_conv->IsCurrentParamAReference();
462 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Ian Rogers7a99c112011-09-07 12:48:27 -0700463 // input may be in register, on stack or both - but not none!
Ian Rogersdf20fe02011-07-20 20:34:16 -0700464 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
Ian Rogers7a99c112011-09-07 12:48:27 -0700465 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 Rogers408f79a2011-08-23 18:22:33 -0700470 // References need placing in SIRT and the entry address passing
Ian Rogersdf20fe02011-07-20 20:34:16 -0700471 if (ref_param) {
Ian Rogers7a99c112011-09-07 12:48:27 -0700472 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Ian Rogers408f79a2011-08-23 18:22:33 -0700473 // 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 Rogersdf20fe02011-07-20 20:34:16 -0700475 // as with regular references).
Ian Rogers408f79a2011-08-23 18:22:33 -0700476 sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
477 // Check SIRT offset is within frame.
Ian Rogersdc51b792011-09-22 20:41:37 -0700478 CHECK_LT(sirt_offset.Uint32Value(), (frame_size + out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700479 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700480#define __ jni_asm->
Ian Rogersdf20fe02011-07-20 20:34:16 -0700481 if (input_in_reg && output_in_reg) {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700482 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
483 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
484 if (ref_param) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700485 __ CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700486 } else {
Ian Rogers7a99c112011-09-07 12:48:27 -0700487 if (!mr_conv->IsCurrentParamOnStack()) {
488 // regular non-straddling move
Ian Rogers2c8f6532011-09-02 17:16:34 -0700489 __ Move(out_reg, in_reg);
Ian Rogers7a99c112011-09-07 12:48:27 -0700490 } else {
491 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
492 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700493 }
494 } else if (!input_in_reg && !output_in_reg) {
495 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
496 if (ref_param) {
Ian Rogersdc51b792011-09-22 20:41:37 -0700497 __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(),
Ian Rogers2c8f6532011-09-02 17:16:34 -0700498 null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700499 } 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 Rogersdc51b792011-09-22 20:41:37 -0700503 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700504 }
505 } else if (!input_in_reg && output_in_reg) {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700506 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 Rogersdc51b792011-09-22 20:41:37 -0700511 __ CreateSirtEntry(out_reg, sirt_offset, ManagedRegister::NoRegister(), null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700512 } else {
Ian Rogersdc51b792011-09-22 20:41:37 -0700513 size_t param_size = mr_conv->CurrentParamSize();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700514 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700515 __ Load(out_reg, in_off, param_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700516 }
517 } else {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700518 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 Rogers408f79a2011-08-23 18:22:33 -0700524 // TODO: recycle value in in_reg rather than reload from SIRT
Ian Rogersdc51b792011-09-22 20:41:37 -0700525 __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(),
Ian Rogers2c8f6532011-09-02 17:16:34 -0700526 null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700527 } else {
528 size_t param_size = mr_conv->CurrentParamSize();
529 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers7a99c112011-09-07 12:48:27 -0700530 if (!mr_conv->IsCurrentParamOnStack()) {
531 // regular non-straddling store
Ian Rogers2c8f6532011-09-02 17:16:34 -0700532 __ Store(out_off, in_reg, param_size);
Ian Rogers7a99c112011-09-07 12:48:27 -0700533 } else {
534 // store where input straddles registers and stack
535 CHECK_EQ(param_size, 8u);
536 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
Ian Rogersdc51b792011-09-22 20:41:37 -0700537 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
Ian Rogers7a99c112011-09-07 12:48:27 -0700538 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700539 }
540 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700541#undef __
Ian Rogersdf20fe02011-07-20 20:34:16 -0700542}
543
Ian Rogerse5de95b2011-09-18 20:31:38 -0700544void 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 Rogersb033c752011-07-20 12:22:35 -0700573} // namespace art