blob: 9da1ab070b94509415db6dd2642d446ea2685186 [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07002
3#include "jni_compiler.h"
4
Ian Rogersb033c752011-07-20 12:22:35 -07005#include <sys/mman.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07006#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007
8#include "assembler.h"
9#include "calling_convention.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070010#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "jni_internal.h"
12#include "macros.h"
13#include "managed_register.h"
14#include "logging.h"
15#include "thread.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070016#include "UniquePtr.h"
Ian Rogersb033c752011-07-20 12:22:35 -070017
18namespace art {
19
Shih-wei Liao31384c52011-09-06 15:27:45 -070020namespace arm {
21ByteArray* CreateJniStub();
22}
23
24namespace x86 {
25ByteArray* CreateJniStub();
26}
27
28JniCompiler::JniCompiler(InstructionSet insns) : jni_stub_(NULL) {
Ian Rogers2c8f6532011-09-02 17:16:34 -070029 if (insns == kThumb2) {
30 // currently only ARM code generation is supported
31 instruction_set_ = kArm;
32 } else {
33 instruction_set_ = insns;
34 }
35}
36
37JniCompiler::~JniCompiler() {}
38
39// Return value helper for jobject return types
Shih-wei Liao668512a2011-09-01 14:18:34 -070040static Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
41 return thread->DecodeJObject(obj);
42}
43
Ian Rogersb033c752011-07-20 12:22:35 -070044// Generate the JNI bridge for the given method, general contract:
45// - Arguments are in the managed runtime format, either on stack or in
46// registers, a reference to the method object is supplied as part of this
47// convention.
48//
Ian Rogers2c8f6532011-09-02 17:16:34 -070049void JniCompiler::Compile(Method* native_method) {
Ian Rogersb033c752011-07-20 12:22:35 -070050 CHECK(native_method->IsNative());
Ian Rogers2c8f6532011-09-02 17:16:34 -070051
52 // Calling conventions used to iterate over parameters to method
53 UniquePtr<JniCallingConvention> jni_conv(
54 JniCallingConvention::Create(native_method, instruction_set_));
55 UniquePtr<ManagedRuntimeCallingConvention> mr_conv(
56 ManagedRuntimeCallingConvention::Create(native_method, instruction_set_));
57
58 // Assembler that holds generated instructions
59 UniquePtr<Assembler> jni_asm(Assembler::Create(instruction_set_));
60#define __ jni_asm->
61
62 // Offsets into data structures
63 // TODO: if cross compiling these offsets are for the host not the target
64 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
65 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
66 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
67
68 // Cache of IsStatic as we call it often enough
Ian Rogersb033c752011-07-20 12:22:35 -070069 const bool is_static = native_method->IsStatic();
Ian Rogers2c8f6532011-09-02 17:16:34 -070070
Shih-wei Liao31384c52011-09-06 15:27:45 -070071 // 0. native_method->RegisterNative(jni_stub_ stuff). Note that jni_stub_ will invoke dlsym.
72 if (jni_stub_ == NULL) {
73 if (instruction_set_ == kArm) {
74 jni_stub_ = arm::CreateJniStub();
75 } else {
76 jni_stub_ = x86::CreateJniStub();
77 }
78 }
79 native_method->RegisterNative(jni_stub_->GetData());
80 // TODO: Need to make sure that the stub is copied into the image. I.e.,
81 // ByteArray* needs to be reachable either as a root or from the object graph.
Ian Rogersb033c752011-07-20 12:22:35 -070082
83 // 1. Build the frame
Ian Rogers2c8f6532011-09-02 17:16:34 -070084 const size_t frame_size(jni_conv->FrameSize());
85 const std::vector<ManagedRegister>& spill_regs = jni_conv->RegsToSpillPreCall();
86 __ BuildFrame(frame_size, mr_conv->MethodRegister(), spill_regs);
Ian Rogersb033c752011-07-20 12:22:35 -070087
88 // 2. Save callee save registers that aren't callee save in the native code
89 // TODO: implement computing the difference of the callee saves
90 // and saving
91
Ian Rogers408f79a2011-08-23 18:22:33 -070092 // 3. Set up the StackIndirectReferenceTable
Ian Rogers2c8f6532011-09-02 17:16:34 -070093 mr_conv->ResetIterator(FrameOffset(frame_size));
94 jni_conv->ResetIterator(FrameOffset(0));
95 __ StoreImmediateToFrame(jni_conv->SirtNumRefsOffset(),
96 jni_conv->ReferenceCount(),
97 mr_conv->InterproceduralScratchRegister());
98 __ CopyRawPtrFromThread(jni_conv->SirtLinkOffset(),
99 Thread::TopSirtOffset(),
100 mr_conv->InterproceduralScratchRegister());
101 __ StoreStackOffsetToThread(Thread::TopSirtOffset(),
102 jni_conv->SirtOffset(),
103 mr_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700104
Ian Rogers408f79a2011-08-23 18:22:33 -0700105 // 4. Place incoming reference arguments into SIRT
Ian Rogers2c8f6532011-09-02 17:16:34 -0700106 jni_conv->Next(); // Skip JNIEnv*
Ian Rogersb033c752011-07-20 12:22:35 -0700107 // 4.5. Create Class argument for static methods out of passed method
108 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700109 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -0700110 // Check sirt offset is within frame
111 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700112 __ LoadRef(jni_conv->InterproceduralScratchRegister(),
113 mr_conv->MethodRegister(), Method::DeclaringClassOffset());
114 __ VerifyObject(jni_conv->InterproceduralScratchRegister(), false);
115 __ StoreRef(sirt_offset, jni_conv->InterproceduralScratchRegister());
116 jni_conv->Next(); // in SIRT so move to next argument
Ian Rogersb033c752011-07-20 12:22:35 -0700117 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700118 while (mr_conv->HasNext()) {
119 CHECK(jni_conv->HasNext());
120 bool ref_param = jni_conv->IsCurrentParamAReference();
121 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Ian Rogers408f79a2011-08-23 18:22:33 -0700122 // References need placing in SIRT and the entry value passing
Ian Rogersb033c752011-07-20 12:22:35 -0700123 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700124 // Compute SIRT entry, note null is placed in the SIRT but its boxed value
Ian Rogersb033c752011-07-20 12:22:35 -0700125 // must be NULL
Ian Rogers2c8f6532011-09-02 17:16:34 -0700126 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -0700127 // Check SIRT offset is within frame
128 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700129 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
130 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700131 CHECK(input_in_reg || input_on_stack);
132
Ian Rogersb033c752011-07-20 12:22:35 -0700133 if (input_in_reg) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700134 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
135 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
136 __ StoreRef(sirt_offset, in_reg);
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700137 } else if (input_on_stack) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700138 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
139 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
140 __ CopyRef(sirt_offset, in_off,
141 mr_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700142 }
143 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700144 mr_conv->Next();
145 jni_conv->Next();
Ian Rogersb033c752011-07-20 12:22:35 -0700146 }
147
Ian Rogersdf20fe02011-07-20 20:34:16 -0700148 // 5. Transition from being in managed to native code
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700149 // TODO: ensure the transition to native follow a store fence.
Ian Rogers2c8f6532011-09-02 17:16:34 -0700150 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset());
151 __ StoreImmediateToThread(Thread::StateOffset(), Thread::kNative,
152 mr_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700153
Ian Rogersdf20fe02011-07-20 20:34:16 -0700154 // 6. Move frame down to allow space for out going args. Do for as short a
Ian Rogersb033c752011-07-20 12:22:35 -0700155 // time as possible to aid profiling..
Ian Rogers2c8f6532011-09-02 17:16:34 -0700156 const size_t out_arg_size = jni_conv->OutArgSize();
157 __ IncreaseFrameSize(out_arg_size);
Ian Rogersb033c752011-07-20 12:22:35 -0700158
Ian Rogersdf20fe02011-07-20 20:34:16 -0700159 // 7. Acquire lock for synchronized methods.
160 if (native_method->IsSynchronized()) {
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700161 // TODO: preserve incoming arguments in registers
Ian Rogers2c8f6532011-09-02 17:16:34 -0700162 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
163 jni_conv->ResetIterator(FrameOffset(out_arg_size));
164 jni_conv->Next(); // Skip JNIEnv*
Ian Rogers408f79a2011-08-23 18:22:33 -0700165 // Get SIRT entry for 1st argument
Ian Rogersdf20fe02011-07-20 20:34:16 -0700166 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700167 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
168 if (jni_conv->IsCurrentParamOnStack()) {
169 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
170 __ CreateSirtEntry(out_off, sirt_offset,
171 mr_conv->InterproceduralScratchRegister(),
172 false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700173 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700174 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
175 __ CreateSirtEntry(out_reg, sirt_offset,
176 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700177 }
178 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700179 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size,
180 out_arg_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700181 }
Shih-wei Liao668512a2011-09-01 14:18:34 -0700182 // Generate JNIEnv* in place and leave a copy in jni_fns_register
Ian Rogers2c8f6532011-09-02 17:16:34 -0700183 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700184 ManagedRegister jni_fns_register =
Ian Rogers2c8f6532011-09-02 17:16:34 -0700185 jni_conv->InterproceduralScratchRegister();
186 __ LoadRawPtrFromThread(jni_fns_register, Thread::JniEnvOffset());
187 SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_fns_register);
Ian Rogersa04d3972011-08-17 11:33:44 -0700188 // Call JNIEnv->MonitorEnter(object)
Ian Rogers2c8f6532011-09-02 17:16:34 -0700189 __ LoadRawPtr(jni_fns_register, jni_fns_register, functions);
190 __ Call(jni_fns_register, monitor_enter,
191 jni_conv->InterproceduralScratchRegister());
192 __ FillFromSpillArea(spill_regs, out_arg_size);
193 __ ExceptionPoll(jni_conv->InterproceduralScratchRegister());
Ian Rogersdf20fe02011-07-20 20:34:16 -0700194 }
195
Ian Rogersb033c752011-07-20 12:22:35 -0700196 // 8. Iterate over arguments placing values from managed calling convention in
197 // to the convention required for a native call (shuffling). For references
198 // place an index/pointer to the reference after checking whether it is
199 // NULL (which must be encoded as NULL).
200 // NB. we do this prior to materializing the JNIEnv* and static's jclass to
201 // give as many free registers for the shuffle as possible
Ian Rogers2c8f6532011-09-02 17:16:34 -0700202 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700203 uint32_t args_count = 0;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700204 while (mr_conv->HasNext()) {
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700205 args_count++;
Ian Rogers2c8f6532011-09-02 17:16:34 -0700206 mr_conv->Next();
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700207 }
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700208
209 // Do a backward pass over arguments, so that the generated code will be "mov
210 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
211 // TODO: A reverse iterator to improve readability.
212 for (uint32_t i = 0; i < args_count; ++i) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700213 mr_conv->ResetIterator(FrameOffset(frame_size + out_arg_size));
214 jni_conv->ResetIterator(FrameOffset(out_arg_size));
215 jni_conv->Next(); // Skip JNIEnv*
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700216 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700217 jni_conv->Next(); // Skip Class for now
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700218 }
219 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700220 mr_conv->Next();
221 jni_conv->Next();
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700222 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700223 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size,
224 out_arg_size);
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700225 }
226
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700227 if (is_static) {
228 // Create argument for Class
Ian Rogers2c8f6532011-09-02 17:16:34 -0700229 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
230 jni_conv->ResetIterator(FrameOffset(out_arg_size));
231 jni_conv->Next(); // Skip JNIEnv*
232 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
233 if (jni_conv->IsCurrentParamOnStack()) {
234 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
235 __ CreateSirtEntry(out_off, sirt_offset,
236 mr_conv->InterproceduralScratchRegister(),
237 false);
Ian Rogersb033c752011-07-20 12:22:35 -0700238 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700239 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
240 __ CreateSirtEntry(out_reg, sirt_offset,
241 ManagedRegister::NoRegister(), false);
Ian Rogersb033c752011-07-20 12:22:35 -0700242 }
Ian Rogersb033c752011-07-20 12:22:35 -0700243 }
244 // 9. Create 1st argument, the JNI environment ptr
Ian Rogers2c8f6532011-09-02 17:16:34 -0700245 jni_conv->ResetIterator(FrameOffset(out_arg_size));
246 if (jni_conv->IsCurrentParamInRegister()) {
247 __ LoadRawPtrFromThread(jni_conv->CurrentParamRegister(),
248 Thread::JniEnvOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700249 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700250 __ CopyRawPtrFromThread(jni_conv->CurrentParamStackOffset(),
251 Thread::JniEnvOffset(),
252 jni_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700253 }
254
255 // 10. Plant call to native code associated with method
Ian Rogers2c8f6532011-09-02 17:16:34 -0700256 if (!jni_conv->IsOutArgRegister(mr_conv->MethodRegister())) {
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700257 // Method register shouldn't have been crushed by setting up outgoing
258 // arguments
Ian Rogers2c8f6532011-09-02 17:16:34 -0700259 __ Call(mr_conv->MethodRegister(), Method::NativeMethodOffset(),
260 mr_conv->InterproceduralScratchRegister());
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700261 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700262 __ Call(jni_conv->MethodStackOffset(), Method::NativeMethodOffset(),
263 mr_conv->InterproceduralScratchRegister());
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700264 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700265 // 11. Release lock for synchronized methods.
266 if (native_method->IsSynchronized()) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700267 mr_conv->ResetIterator(FrameOffset(frame_size+out_arg_size));
268 jni_conv->ResetIterator(FrameOffset(out_arg_size));
269 jni_conv->Next(); // Skip JNIEnv*
Ian Rogersdf20fe02011-07-20 20:34:16 -0700270 // Save return value
Ian Rogers2c8f6532011-09-02 17:16:34 -0700271 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
272 if (jni_conv->SizeOfReturnValue() != 0) {
273 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700274 CHECK_LT(return_save_location.Uint32Value(), frame_size+out_arg_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700275 __ Store(return_save_location, jni_conv->ReturnRegister(),
276 jni_conv->SizeOfReturnValue());
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700277 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700278 // Get SIRT entry for 1st argument
Ian Rogersdf20fe02011-07-20 20:34:16 -0700279 if (is_static) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700280 FrameOffset sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
281 if (jni_conv->IsCurrentParamOnStack()) {
282 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
283 __ CreateSirtEntry(out_off, sirt_offset,
284 mr_conv->InterproceduralScratchRegister(),
285 false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700286 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700287 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
288 __ CreateSirtEntry(out_reg, sirt_offset,
289 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700290 }
291 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700292 CopyParameter(jni_asm.get(), mr_conv.get(), jni_conv.get(), frame_size,
293 out_arg_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700294 }
295 // Generate JNIEnv* in place and leave a copy in jni_env_register
Ian Rogers2c8f6532011-09-02 17:16:34 -0700296 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700297 ManagedRegister jni_env_register =
Ian Rogers2c8f6532011-09-02 17:16:34 -0700298 jni_conv->InterproceduralScratchRegister();
299 __ LoadRawPtrFromThread(jni_env_register, Thread::JniEnvOffset());
300 SetNativeParameter(jni_asm.get(), jni_conv.get(), jni_env_register);
Ian Rogersa04d3972011-08-17 11:33:44 -0700301 // Call JNIEnv->MonitorExit(object)
Ian Rogers2c8f6532011-09-02 17:16:34 -0700302 __ LoadRawPtr(jni_env_register, jni_env_register, functions);
303 __ Call(jni_env_register, monitor_exit,
304 jni_conv->InterproceduralScratchRegister());
Ian Rogersdf20fe02011-07-20 20:34:16 -0700305 // Reload return value
Ian Rogers2c8f6532011-09-02 17:16:34 -0700306 if (jni_conv->SizeOfReturnValue() != 0) {
307 __ Load(jni_conv->ReturnRegister(), return_save_location,
308 jni_conv->SizeOfReturnValue());
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700309 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700310 }
311
Shih-wei Liao668512a2011-09-01 14:18:34 -0700312 // 12. Release outgoing argument area
Ian Rogers2c8f6532011-09-02 17:16:34 -0700313 __ DecreaseFrameSize(out_arg_size);
314 mr_conv->ResetIterator(FrameOffset(frame_size));
315 jni_conv->ResetIterator(FrameOffset(0));
Ian Rogersb033c752011-07-20 12:22:35 -0700316
Shih-wei Liao668512a2011-09-01 14:18:34 -0700317 // 13. Transition from being in native to managed code, possibly entering a
Ian Rogersb033c752011-07-20 12:22:35 -0700318 // safepoint
Ian Rogers2c8f6532011-09-02 17:16:34 -0700319 CHECK(!jni_conv->InterproceduralScratchRegister()
320 .Equals(jni_conv->ReturnRegister())); // don't clobber result
Ian Rogers45a76cb2011-07-21 22:00:15 -0700321 // Location to preserve result on slow path, ensuring its within the frame
Ian Rogers2c8f6532011-09-02 17:16:34 -0700322 FrameOffset return_save_location = jni_conv->ReturnValueSaveLocation();
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700323 CHECK(return_save_location.Uint32Value() < frame_size ||
Ian Rogers2c8f6532011-09-02 17:16:34 -0700324 jni_conv->SizeOfReturnValue() == 0);
325 __ SuspendPoll(jni_conv->InterproceduralScratchRegister(),
326 jni_conv->ReturnRegister(), return_save_location,
327 jni_conv->SizeOfReturnValue());
328 __ ExceptionPoll(jni_conv->InterproceduralScratchRegister());
329 __ StoreImmediateToThread(Thread::StateOffset(), Thread::kRunnable,
330 jni_conv->InterproceduralScratchRegister());
Ian Rogers45a76cb2011-07-21 22:00:15 -0700331
Ian Rogersb033c752011-07-20 12:22:35 -0700332
Shih-wei Liao668512a2011-09-01 14:18:34 -0700333 // 14. Place result in correct register possibly loading from indirect
Ian Rogers408f79a2011-08-23 18:22:33 -0700334 // reference table
Ian Rogers2c8f6532011-09-02 17:16:34 -0700335 if (jni_conv->IsReturnAReference()) {
336 __ IncreaseFrameSize(out_arg_size);
337 jni_conv->ResetIterator(FrameOffset(out_arg_size));
Ian Rogersb033c752011-07-20 12:22:35 -0700338
Ian Rogers2c8f6532011-09-02 17:16:34 -0700339 jni_conv->Next(); // Skip Thread* argument
Ian Rogers7a99c112011-09-07 12:48:27 -0700340 // Pass result as arg2
Ian Rogers2c8f6532011-09-02 17:16:34 -0700341 SetNativeParameter(jni_asm.get(), jni_conv.get(),
342 jni_conv->ReturnRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700343
Ian Rogers7a99c112011-09-07 12:48:27 -0700344 // Pass Thread*
Ian Rogers2c8f6532011-09-02 17:16:34 -0700345 jni_conv->ResetIterator(FrameOffset(out_arg_size));
346 if (jni_conv->IsCurrentParamInRegister()) {
347 __ GetCurrentThread(jni_conv->CurrentParamRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700348 } else {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700349 __ GetCurrentThread(jni_conv->CurrentParamStackOffset(),
350 jni_conv->InterproceduralScratchRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700351 }
352
Ian Rogers2c8f6532011-09-02 17:16:34 -0700353 __ Call(reinterpret_cast<uintptr_t>(DecodeJObjectInThread),
354 jni_conv->InterproceduralScratchRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700355
Ian Rogers2c8f6532011-09-02 17:16:34 -0700356 __ DecreaseFrameSize(out_arg_size);
357 jni_conv->ResetIterator(FrameOffset(0));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700358 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700359 __ Move(mr_conv->ReturnRegister(), jni_conv->ReturnRegister());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700360
361 // 15. Remove SIRT from thread
Ian Rogers2c8f6532011-09-02 17:16:34 -0700362 __ CopyRawPtrToThread(Thread::TopSirtOffset(), jni_conv->SirtLinkOffset(),
363 jni_conv->InterproceduralScratchRegister());
Ian Rogersb033c752011-07-20 12:22:35 -0700364
Shih-wei Liao668512a2011-09-01 14:18:34 -0700365 // 16. Remove activation
Ian Rogers2c8f6532011-09-02 17:16:34 -0700366 __ RemoveFrame(frame_size, spill_regs);
Ian Rogersb033c752011-07-20 12:22:35 -0700367
Shih-wei Liao668512a2011-09-01 14:18:34 -0700368 // 17. Finalize code generation
Ian Rogers2c8f6532011-09-02 17:16:34 -0700369 __ EmitSlowPaths();
370 size_t cs = __ CodeSize();
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700371 ByteArray* managed_code = ByteArray::Alloc(cs);
372 CHECK(managed_code != NULL);
373 MemoryRegion code(managed_code->GetData(), managed_code->GetLength());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700374 __ FinalizeInstructions(code);
375 native_method->SetCode(managed_code, instruction_set_);
Shih-wei Liaod11af152011-08-23 16:02:11 -0700376 native_method->SetFrameSizeInBytes(frame_size);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700377 native_method->SetReturnPcOffsetInBytes(jni_conv->ReturnPcOffset());
378#undef __
Ian Rogersb033c752011-07-20 12:22:35 -0700379}
380
Ian Rogers2c8f6532011-09-02 17:16:34 -0700381void JniCompiler::SetNativeParameter(Assembler* jni_asm,
382 JniCallingConvention* jni_conv,
Shih-wei Liao668512a2011-09-01 14:18:34 -0700383 ManagedRegister in_reg) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700384#define __ jni_asm->
Shih-wei Liao668512a2011-09-01 14:18:34 -0700385 if (jni_conv->IsCurrentParamOnStack()) {
386 FrameOffset dest = jni_conv->CurrentParamStackOffset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700387 __ StoreRawPtr(dest, in_reg);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700388 } else {
389 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700390 __ Move(jni_conv->CurrentParamRegister(), in_reg);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700391 }
392 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700393#undef __
Shih-wei Liao668512a2011-09-01 14:18:34 -0700394}
395
Ian Rogersdf20fe02011-07-20 20:34:16 -0700396// Copy a single parameter from the managed to the JNI calling convention
397void JniCompiler::CopyParameter(Assembler* jni_asm,
398 ManagedRuntimeCallingConvention* mr_conv,
399 JniCallingConvention* jni_conv,
400 size_t frame_size, size_t out_arg_size) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700401
Ian Rogersdf20fe02011-07-20 20:34:16 -0700402 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
403 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Ian Rogers408f79a2011-08-23 18:22:33 -0700404 FrameOffset sirt_offset(0);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700405 bool null_allowed = false;
406 bool ref_param = jni_conv->IsCurrentParamAReference();
407 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Ian Rogers7a99c112011-09-07 12:48:27 -0700408 // input may be in register, on stack or both - but not none!
Ian Rogersdf20fe02011-07-20 20:34:16 -0700409 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
Ian Rogers7a99c112011-09-07 12:48:27 -0700410 if (output_in_reg) { // output shouldn't straddle registers and stack
411 CHECK(!jni_conv->IsCurrentParamOnStack());
412 } else {
413 CHECK(jni_conv->IsCurrentParamOnStack());
414 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700415 // References need placing in SIRT and the entry address passing
Ian Rogersdf20fe02011-07-20 20:34:16 -0700416 if (ref_param) {
Ian Rogers7a99c112011-09-07 12:48:27 -0700417 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Ian Rogers408f79a2011-08-23 18:22:33 -0700418 // Compute SIRT offset. Note null is placed in the SIRT but the jobject
419 // passed to the native code must be null (not a pointer into the SIRT
Ian Rogersdf20fe02011-07-20 20:34:16 -0700420 // as with regular references).
Ian Rogers408f79a2011-08-23 18:22:33 -0700421 sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
422 // Check SIRT offset is within frame.
423 CHECK_LT(sirt_offset.Uint32Value(), (frame_size+out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700424 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700425#define __ jni_asm->
Ian Rogersdf20fe02011-07-20 20:34:16 -0700426 if (input_in_reg && output_in_reg) {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700427 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
428 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
429 if (ref_param) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700430 __ CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700431 } else {
Ian Rogers7a99c112011-09-07 12:48:27 -0700432 if (!mr_conv->IsCurrentParamOnStack()) {
433 // regular non-straddling move
Ian Rogers2c8f6532011-09-02 17:16:34 -0700434 __ Move(out_reg, in_reg);
Ian Rogers7a99c112011-09-07 12:48:27 -0700435 } else {
436 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
437 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700438 }
439 } else if (!input_in_reg && !output_in_reg) {
440 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
441 if (ref_param) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700442 __ CreateSirtEntry(out_off, sirt_offset,
443 mr_conv->InterproceduralScratchRegister(),
444 null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700445 } else {
446 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
447 size_t param_size = mr_conv->CurrentParamSize();
448 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700449 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(),
450 param_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700451 }
452 } else if (!input_in_reg && output_in_reg) {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700453 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
454 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
455 // Check that incoming stack arguments are above the current stack frame.
456 CHECK_GT(in_off.Uint32Value(), frame_size);
457 if (ref_param) {
Ian Rogers2c8f6532011-09-02 17:16:34 -0700458 __ CreateSirtEntry(out_reg, sirt_offset,
459 ManagedRegister::NoRegister(), null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700460 } else {
461 unsigned int param_size = mr_conv->CurrentParamSize();
462 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700463 __ Load(out_reg, in_off, param_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700464 }
465 } else {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700466 CHECK(input_in_reg && !output_in_reg);
467 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
468 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
469 // Check outgoing argument is within frame
470 CHECK_LT(out_off.Uint32Value(), frame_size);
471 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700472 // TODO: recycle value in in_reg rather than reload from SIRT
Ian Rogers2c8f6532011-09-02 17:16:34 -0700473 __ CreateSirtEntry(out_off, sirt_offset,
474 mr_conv->InterproceduralScratchRegister(),
475 null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700476 } else {
477 size_t param_size = mr_conv->CurrentParamSize();
478 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers7a99c112011-09-07 12:48:27 -0700479 if (!mr_conv->IsCurrentParamOnStack()) {
480 // regular non-straddling store
Ian Rogers2c8f6532011-09-02 17:16:34 -0700481 __ Store(out_off, in_reg, param_size);
Ian Rogers7a99c112011-09-07 12:48:27 -0700482 } else {
483 // store where input straddles registers and stack
484 CHECK_EQ(param_size, 8u);
485 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
Ian Rogers2c8f6532011-09-02 17:16:34 -0700486 __ StoreSpanning(out_off, in_reg, in_off,
487 mr_conv->InterproceduralScratchRegister());
Ian Rogers7a99c112011-09-07 12:48:27 -0700488 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700489 }
490 }
Ian Rogers2c8f6532011-09-02 17:16:34 -0700491#undef __
Ian Rogersdf20fe02011-07-20 20:34:16 -0700492}
493
Ian Rogersb033c752011-07-20 12:22:35 -0700494} // namespace art