blob: fec11da9a548097d96cadb089421b20b7530255d [file] [log] [blame]
Ian Rogersb033c752011-07-20 12:22:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: irogers@google.com (Ian Rogers)
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003
4#include "jni_compiler.h"
5
Ian Rogersb033c752011-07-20 12:22:35 -07006#include <sys/mman.h>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07007#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008
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 Rogersb033c752011-07-20 12:22:35 -070016
17namespace art {
18
Shih-wei Liao668512a2011-09-01 14:18:34 -070019static Object* DecodeJObjectInThread(Thread* thread, jobject obj) {
20 return thread->DecodeJObject(obj);
21}
22
Ian Rogersb033c752011-07-20 12:22:35 -070023// 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//
28void 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 Hughes69f5bc62011-08-24 09:26:14 -070033 static Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
Ian Rogersa04d3972011-08-17 11:33:44 -070034 static Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
35 static Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
Ian Rogersb033c752011-07-20 12:22:35 -070036
37 // 1. Build the frame
38 const size_t frame_size(jni_conv.FrameSize());
Ian Rogers0d666d82011-08-14 16:03:46 -070039 const std::vector<ManagedRegister>& spill_regs = jni_conv.RegsToSpillPreCall();
40 jni_asm->BuildFrame(frame_size, mr_conv.MethodRegister(), spill_regs);
Ian Rogersb033c752011-07-20 12:22:35 -070041
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 Rogers408f79a2011-08-23 18:22:33 -070046 // 3. Set up the StackIndirectReferenceTable
Ian Rogersb033c752011-07-20 12:22:35 -070047 mr_conv.ResetIterator(FrameOffset(frame_size));
48 jni_conv.ResetIterator(FrameOffset(0));
Ian Rogers408f79a2011-08-23 18:22:33 -070049 jni_asm->StoreImmediateToFrame(jni_conv.SirtNumRefsOffset(),
50 jni_conv.ReferenceCount(),
Ian Rogersb033c752011-07-20 12:22:35 -070051 mr_conv.InterproceduralScratchRegister());
Ian Rogers408f79a2011-08-23 18:22:33 -070052 jni_asm->CopyRawPtrFromThread(jni_conv.SirtLinkOffset(),
53 Thread::TopSirtOffset(),
Ian Rogersb033c752011-07-20 12:22:35 -070054 mr_conv.InterproceduralScratchRegister());
Ian Rogers408f79a2011-08-23 18:22:33 -070055 jni_asm->StoreStackOffsetToThread(Thread::TopSirtOffset(),
56 jni_conv.SirtOffset(),
Ian Rogersb033c752011-07-20 12:22:35 -070057 mr_conv.InterproceduralScratchRegister());
58
Ian Rogers408f79a2011-08-23 18:22:33 -070059 // 4. Place incoming reference arguments into SIRT
Ian Rogersb033c752011-07-20 12:22:35 -070060 jni_conv.Next(); // Skip JNIEnv*
61 // 4.5. Create Class argument for static methods out of passed method
62 if (is_static) {
Ian Rogers408f79a2011-08-23 18:22:33 -070063 FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset();
64 // Check sirt offset is within frame
65 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogersb033c752011-07-20 12:22:35 -070066 jni_asm->LoadRef(jni_conv.InterproceduralScratchRegister(),
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070067 mr_conv.MethodRegister(), Method::DeclaringClassOffset());
Ian Rogers408f79a2011-08-23 18:22:33 -070068 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 Rogersb033c752011-07-20 12:22:35 -070071 }
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 Rogers408f79a2011-08-23 18:22:33 -070076 // References need placing in SIRT and the entry value passing
Ian Rogersb033c752011-07-20 12:22:35 -070077 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -070078 // Compute SIRT entry, note null is placed in the SIRT but its boxed value
Ian Rogersb033c752011-07-20 12:22:35 -070079 // must be NULL
Ian Rogers408f79a2011-08-23 18:22:33 -070080 FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset();
81 // Check SIRT offset is within frame
82 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Ian Rogersb033c752011-07-20 12:22:35 -070083 bool input_in_reg = mr_conv.IsCurrentParamInRegister();
Shih-wei Liao5381cf92011-07-27 00:28:04 -070084 bool input_on_stack = mr_conv.IsCurrentParamOnStack();
85 CHECK(input_in_reg || input_on_stack);
86
Ian Rogersb033c752011-07-20 12:22:35 -070087 if (input_in_reg) {
Ian Rogersb033c752011-07-20 12:22:35 -070088 ManagedRegister in_reg = mr_conv.CurrentParamRegister();
Ian Rogers7a99c112011-09-07 12:48:27 -070089 jni_asm->VerifyObject(in_reg, mr_conv.IsCurrentArgPossiblyNull());
Ian Rogers408f79a2011-08-23 18:22:33 -070090 jni_asm->StoreRef(sirt_offset, in_reg);
Shih-wei Liao5381cf92011-07-27 00:28:04 -070091 } else if (input_on_stack) {
Ian Rogersb033c752011-07-20 12:22:35 -070092 FrameOffset in_off = mr_conv.CurrentParamStackOffset();
Ian Rogers7a99c112011-09-07 12:48:27 -070093 jni_asm->VerifyObject(in_off, mr_conv.IsCurrentArgPossiblyNull());
Ian Rogers408f79a2011-08-23 18:22:33 -070094 jni_asm->CopyRef(sirt_offset, in_off,
Ian Rogersb033c752011-07-20 12:22:35 -070095 mr_conv.InterproceduralScratchRegister());
96 }
97 }
98 mr_conv.Next();
99 jni_conv.Next();
100 }
101
Ian Rogersdf20fe02011-07-20 20:34:16 -0700102 // 5. Transition from being in managed to native code
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700103 // TODO: ensure the transition to native follow a store fence.
Ian Rogers45a76cb2011-07-21 22:00:15 -0700104 jni_asm->StoreStackPointerToThread(Thread::TopOfManagedStackOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700105 jni_asm->StoreImmediateToThread(Thread::StateOffset(), Thread::kNative,
106 mr_conv.InterproceduralScratchRegister());
107
Ian Rogersdf20fe02011-07-20 20:34:16 -0700108 // 6. Move frame down to allow space for out going args. Do for as short a
Ian Rogersb033c752011-07-20 12:22:35 -0700109 // 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 Rogersdf20fe02011-07-20 20:34:16 -0700113 // 7. Acquire lock for synchronized methods.
114 if (native_method->IsSynchronized()) {
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700115 // TODO: preserve incoming arguments in registers
Ian Rogersdf20fe02011-07-20 20:34:16 -0700116 mr_conv.ResetIterator(FrameOffset(frame_size+out_arg_size));
117 jni_conv.ResetIterator(FrameOffset(out_arg_size));
118 jni_conv.Next(); // Skip JNIEnv*
Ian Rogers408f79a2011-08-23 18:22:33 -0700119 // Get SIRT entry for 1st argument
Ian Rogersdf20fe02011-07-20 20:34:16 -0700120 if (is_static) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700121 FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700122 if (jni_conv.IsCurrentParamOnStack()) {
123 FrameOffset out_off = jni_conv.CurrentParamStackOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -0700124 jni_asm->CreateSirtEntry(out_off, sirt_offset,
125 mr_conv.InterproceduralScratchRegister(),
126 false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700127 } else {
128 ManagedRegister out_reg = jni_conv.CurrentParamRegister();
Ian Rogers408f79a2011-08-23 18:22:33 -0700129 jni_asm->CreateSirtEntry(out_reg, sirt_offset,
130 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700131 }
132 } else {
133 CopyParameter(jni_asm, &mr_conv, &jni_conv, frame_size, out_arg_size);
134 }
Shih-wei Liao668512a2011-09-01 14:18:34 -0700135 // Generate JNIEnv* in place and leave a copy in jni_fns_register
Ian Rogers0d666d82011-08-14 16:03:46 -0700136 jni_conv.ResetIterator(FrameOffset(out_arg_size));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700137 ManagedRegister jni_fns_register =
Ian Rogersdf20fe02011-07-20 20:34:16 -0700138 jni_conv.InterproceduralScratchRegister();
Shih-wei Liao668512a2011-09-01 14:18:34 -0700139 jni_asm->LoadRawPtrFromThread(jni_fns_register, Thread::JniEnvOffset());
140 SetNativeParameter(jni_asm, &jni_conv, jni_fns_register);
Ian Rogersa04d3972011-08-17 11:33:44 -0700141 // Call JNIEnv->MonitorEnter(object)
Shih-wei Liao668512a2011-09-01 14:18:34 -0700142 jni_asm->LoadRawPtr(jni_fns_register, jni_fns_register, functions);
Ian Rogersa04d3972011-08-17 11:33:44 -0700143 jni_asm->Call(jni_fns_register, monitor_enter,
Ian Rogersdf20fe02011-07-20 20:34:16 -0700144 jni_conv.InterproceduralScratchRegister());
Ian Rogers0d666d82011-08-14 16:03:46 -0700145 jni_asm->FillFromSpillArea(spill_regs, out_arg_size);
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700146 jni_asm->ExceptionPoll(jni_conv.InterproceduralScratchRegister());
Ian Rogersdf20fe02011-07-20 20:34:16 -0700147 }
148
Ian Rogersb033c752011-07-20 12:22:35 -0700149 // 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 Liao82da44b2011-09-01 00:38:04 -0700156 uint32_t args_count = 0;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700157 while (mr_conv.HasNext()) {
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700158 args_count++;
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700159 mr_conv.Next();
Carl Shapiroe2d373e2011-07-25 15:20:06 -0700160 }
Shih-wei Liao82da44b2011-09-01 00:38:04 -0700161
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 Shapiroe2d373e2011-07-25 15:20:06 -0700179 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 Rogers408f79a2011-08-23 18:22:33 -0700184 FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset();
Ian Rogersb033c752011-07-20 12:22:35 -0700185 if (jni_conv.IsCurrentParamOnStack()) {
186 FrameOffset out_off = jni_conv.CurrentParamStackOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -0700187 jni_asm->CreateSirtEntry(out_off, sirt_offset,
188 mr_conv.InterproceduralScratchRegister(),
189 false);
Ian Rogersb033c752011-07-20 12:22:35 -0700190 } else {
191 ManagedRegister out_reg = jni_conv.CurrentParamRegister();
Ian Rogers408f79a2011-08-23 18:22:33 -0700192 jni_asm->CreateSirtEntry(out_reg, sirt_offset,
193 ManagedRegister::NoRegister(), false);
Ian Rogersb033c752011-07-20 12:22:35 -0700194 }
Ian Rogersb033c752011-07-20 12:22:35 -0700195 }
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 Shapiroe2d373e2011-07-25 15:20:06 -0700208 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 Rogersdf20fe02011-07-20 20:34:16 -0700217 // 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 Rogers4ff5dad2011-09-07 13:56:27 -0700224 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 Rogers408f79a2011-08-23 18:22:33 -0700230 // Get SIRT entry for 1st argument
Ian Rogersdf20fe02011-07-20 20:34:16 -0700231 if (is_static) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700232 FrameOffset sirt_offset = jni_conv.CurrentParamSirtEntryOffset();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700233 if (jni_conv.IsCurrentParamOnStack()) {
234 FrameOffset out_off = jni_conv.CurrentParamStackOffset();
Ian Rogers408f79a2011-08-23 18:22:33 -0700235 jni_asm->CreateSirtEntry(out_off, sirt_offset,
236 mr_conv.InterproceduralScratchRegister(),
237 false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700238 } else {
239 ManagedRegister out_reg = jni_conv.CurrentParamRegister();
Ian Rogers408f79a2011-08-23 18:22:33 -0700240 jni_asm->CreateSirtEntry(out_reg, sirt_offset,
241 ManagedRegister::NoRegister(), false);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700242 }
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 Rogers0d666d82011-08-14 16:03:46 -0700247 jni_conv.ResetIterator(FrameOffset(out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700248 ManagedRegister jni_env_register =
249 jni_conv.InterproceduralScratchRegister();
Ian Rogersdf20fe02011-07-20 20:34:16 -0700250 jni_asm->LoadRawPtrFromThread(jni_env_register, Thread::JniEnvOffset());
Shih-wei Liao668512a2011-09-01 14:18:34 -0700251 SetNativeParameter(jni_asm, &jni_conv, jni_env_register);
Ian Rogersa04d3972011-08-17 11:33:44 -0700252 // Call JNIEnv->MonitorExit(object)
Shih-wei Liao668512a2011-09-01 14:18:34 -0700253 jni_asm->LoadRawPtr(jni_env_register, jni_env_register, functions);
254 jni_asm->Call(jni_env_register, monitor_exit,
Ian Rogersdf20fe02011-07-20 20:34:16 -0700255 jni_conv.InterproceduralScratchRegister());
256 // Reload return value
Ian Rogers4ff5dad2011-09-07 13:56:27 -0700257 if (jni_conv.SizeOfReturnValue() != 0) {
258 jni_asm->Load(jni_conv.ReturnRegister(), return_save_location,
259 jni_conv.SizeOfReturnValue());
260 }
Ian Rogersdf20fe02011-07-20 20:34:16 -0700261 }
262
Shih-wei Liao668512a2011-09-01 14:18:34 -0700263 // 12. Release outgoing argument area
Ian Rogersb033c752011-07-20 12:22:35 -0700264 jni_asm->DecreaseFrameSize(out_arg_size);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700265 mr_conv.ResetIterator(FrameOffset(frame_size));
266 jni_conv.ResetIterator(FrameOffset(0));
Ian Rogersb033c752011-07-20 12:22:35 -0700267
Shih-wei Liao668512a2011-09-01 14:18:34 -0700268 // 13. Transition from being in native to managed code, possibly entering a
Ian Rogersb033c752011-07-20 12:22:35 -0700269 // safepoint
Ian Rogers45a76cb2011-07-21 22:00:15 -0700270 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 Rogers4ff5dad2011-09-07 13:56:27 -0700274 CHECK(return_save_location.Uint32Value() < frame_size ||
275 jni_conv.SizeOfReturnValue() == 0);
Ian Rogers45a76cb2011-07-21 22:00:15 -0700276 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 Rogersb033c752011-07-20 12:22:35 -0700280 jni_asm->StoreImmediateToThread(Thread::StateOffset(), Thread::kRunnable,
Ian Rogers45a76cb2011-07-21 22:00:15 -0700281 jni_conv.InterproceduralScratchRegister());
282
Ian Rogersb033c752011-07-20 12:22:35 -0700283
Shih-wei Liao668512a2011-09-01 14:18:34 -0700284 // 14. Place result in correct register possibly loading from indirect
Ian Rogers408f79a2011-08-23 18:22:33 -0700285 // reference table
Ian Rogersb033c752011-07-20 12:22:35 -0700286 if (jni_conv.IsReturnAReference()) {
Ian Rogers7a99c112011-09-07 12:48:27 -0700287 jni_asm->IncreaseFrameSize(out_arg_size);
288 jni_conv.ResetIterator(FrameOffset(out_arg_size));
Ian Rogersb033c752011-07-20 12:22:35 -0700289
Ian Rogers7a99c112011-09-07 12:48:27 -0700290 jni_conv.Next(); // Skip Thread* argument
291 // Pass result as arg2
Shih-wei Liao668512a2011-09-01 14:18:34 -0700292 SetNativeParameter(jni_asm, &jni_conv, jni_conv.ReturnRegister());
293
Ian Rogers7a99c112011-09-07 12:48:27 -0700294 // Pass Thread*
295 jni_conv.ResetIterator(FrameOffset(out_arg_size));
Shih-wei Liao668512a2011-09-01 14:18:34 -0700296 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 Rogers7a99c112011-09-07 12:48:27 -0700306 jni_asm->DecreaseFrameSize(out_arg_size);
Shih-wei Liao668512a2011-09-01 14:18:34 -0700307 jni_conv.ResetIterator(FrameOffset(0));
308 }
309 jni_asm->Move(mr_conv.ReturnRegister(), jni_conv.ReturnRegister());
310
311 // 15. Remove SIRT from thread
Ian Rogers408f79a2011-08-23 18:22:33 -0700312 jni_asm->CopyRawPtrToThread(Thread::TopSirtOffset(), jni_conv.SirtLinkOffset(),
Ian Rogersb033c752011-07-20 12:22:35 -0700313 jni_conv.InterproceduralScratchRegister());
314
Shih-wei Liao668512a2011-09-01 14:18:34 -0700315 // 16. Remove activation
Ian Rogers0d666d82011-08-14 16:03:46 -0700316 jni_asm->RemoveFrame(frame_size, spill_regs);
Ian Rogersb033c752011-07-20 12:22:35 -0700317
Shih-wei Liao668512a2011-09-01 14:18:34 -0700318 // 17. Finalize code generation
Ian Rogers45a76cb2011-07-21 22:00:15 -0700319 jni_asm->EmitSlowPaths();
Ian Rogersb033c752011-07-20 12:22:35 -0700320 size_t cs = jni_asm->CodeSize();
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700321 ByteArray* managed_code = ByteArray::Alloc(cs);
322 CHECK(managed_code != NULL);
323 MemoryRegion code(managed_code->GetData(), managed_code->GetLength());
Ian Rogersb033c752011-07-20 12:22:35 -0700324 jni_asm->FinalizeInstructions(code);
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700325 native_method->SetCode(managed_code, jni_asm->GetInstructionSet());
Shih-wei Liaod11af152011-08-23 16:02:11 -0700326 native_method->SetFrameSizeInBytes(frame_size);
327 native_method->SetReturnPcOffsetInBytes(jni_conv.ReturnPcOffset());
Ian Rogersb033c752011-07-20 12:22:35 -0700328}
329
Shih-wei Liao668512a2011-09-01 14:18:34 -0700330void 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 Rogersdf20fe02011-07-20 20:34:16 -0700343// Copy a single parameter from the managed to the JNI calling convention
344void 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 Rogers408f79a2011-08-23 18:22:33 -0700350 FrameOffset sirt_offset(0);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700351 bool null_allowed = false;
352 bool ref_param = jni_conv->IsCurrentParamAReference();
353 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Ian Rogers7a99c112011-09-07 12:48:27 -0700354 // input may be in register, on stack or both - but not none!
Ian Rogersdf20fe02011-07-20 20:34:16 -0700355 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
Ian Rogers7a99c112011-09-07 12:48:27 -0700356 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 Rogers408f79a2011-08-23 18:22:33 -0700361 // References need placing in SIRT and the entry address passing
Ian Rogersdf20fe02011-07-20 20:34:16 -0700362 if (ref_param) {
Ian Rogers7a99c112011-09-07 12:48:27 -0700363 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Ian Rogers408f79a2011-08-23 18:22:33 -0700364 // 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 Rogersdf20fe02011-07-20 20:34:16 -0700366 // as with regular references).
Ian Rogers408f79a2011-08-23 18:22:33 -0700367 sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
368 // Check SIRT offset is within frame.
369 CHECK_LT(sirt_offset.Uint32Value(), (frame_size+out_arg_size));
Ian Rogersdf20fe02011-07-20 20:34:16 -0700370 }
371 if (input_in_reg && output_in_reg) {
Ian Rogersdf20fe02011-07-20 20:34:16 -0700372 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
373 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
374 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700375 jni_asm->CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700376 } else {
Ian Rogers7a99c112011-09-07 12:48:27 -0700377 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 Rogersdf20fe02011-07-20 20:34:16 -0700383 }
384 } else if (!input_in_reg && !output_in_reg) {
385 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
386 if (ref_param) {
Ian Rogers408f79a2011-08-23 18:22:33 -0700387 jni_asm->CreateSirtEntry(out_off, sirt_offset,
388 mr_conv->InterproceduralScratchRegister(),
389 null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700390 } 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 Rogersdf20fe02011-07-20 20:34:16 -0700398 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 Rogers408f79a2011-08-23 18:22:33 -0700403 jni_asm->CreateSirtEntry(out_reg, sirt_offset,
404 ManagedRegister::NoRegister(), null_allowed);
Ian Rogersdf20fe02011-07-20 20:34:16 -0700405 } 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 Rogersdf20fe02011-07-20 20:34:16 -0700411 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 Rogers408f79a2011-08-23 18:22:33 -0700417 // 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 Rogersdf20fe02011-07-20 20:34:16 -0700421 } else {
422 size_t param_size = mr_conv->CurrentParamSize();
423 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
Ian Rogers7a99c112011-09-07 12:48:27 -0700424 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 Rogersdf20fe02011-07-20 20:34:16 -0700434 }
435 }
436}
437
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700438JniCompiler::JniCompiler() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700439
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700440JniCompiler::~JniCompiler() {}
Ian Rogersb033c752011-07-20 12:22:35 -0700441
442} // namespace art