blob: c4919fbc6fe9c1f15a050abeb6883d8d66694d8b [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
Brian Carlstromfc7120c2012-08-27 13:43:25 -070017#include <algorithm>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070018#include <vector>
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070019
Elliott Hughes07ed66b2012-12-12 18:34:25 -080020#include "base/logging.h"
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/macros.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "calling_convention.h"
Ian Rogers0571d352011-11-03 19:51:38 -070023#include "class_linker.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070024#include "compiled_method.h"
Ian Rogers1212a022013-03-04 10:48:41 -080025#include "compiler/driver/compiler_driver.h"
Elliott Hughes3e778f72012-05-21 15:29:52 -070026#include "disassembler.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "jni_internal.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070028#include "oat/runtime/oat_support_entrypoints.h"
29#include "oat/utils/assembler.h"
30#include "oat/utils/managed_register.h"
Elliott Hughes3e778f72012-05-21 15:29:52 -070031#include "oat/utils/arm/managed_register_arm.h"
jeffhao7fbee072012-08-24 17:56:54 -070032#include "oat/utils/mips/managed_register_mips.h"
Elliott Hughes3e778f72012-05-21 15:29:52 -070033#include "oat/utils/x86/managed_register_x86.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "thread.h"
Ian Rogers2c8f6532011-09-02 17:16:34 -070035#include "UniquePtr.h"
Ian Rogersb033c752011-07-20 12:22:35 -070036
Elliott Hughes46f060a2012-03-09 17:36:50 -080037#define __ jni_asm->
38
Ian Rogersb033c752011-07-20 12:22:35 -070039namespace art {
40
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041static void CopyParameter(Assembler* jni_asm,
42 ManagedRuntimeCallingConvention* mr_conv,
43 JniCallingConvention* jni_conv,
44 size_t frame_size, size_t out_arg_size);
45static void SetNativeParameter(Assembler* jni_asm,
46 JniCallingConvention* jni_conv,
47 ManagedRegister in_reg);
48
49// Generate the JNI bridge for the given method, general contract:
50// - Arguments are in the managed runtime format, either on stack or in
51// registers, a reference to the method object is supplied as part of this
52// convention.
53//
Ian Rogers1212a022013-03-04 10:48:41 -080054CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver& compiler,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 uint32_t access_flags, uint32_t method_idx,
56 const DexFile& dex_file) {
57 const bool is_native = (access_flags & kAccNative) != 0;
58 CHECK(is_native);
59 const bool is_static = (access_flags & kAccStatic) != 0;
60 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
61 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
62 InstructionSet instruction_set = compiler.GetInstructionSet();
63 if (instruction_set == kThumb2) {
64 instruction_set = kArm;
Ian Rogers2c8f6532011-09-02 17:16:34 -070065 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 // Calling conventions used to iterate over parameters to method
Brian Carlstromfc7120c2012-08-27 13:43:25 -070067 UniquePtr<JniCallingConvention> main_jni_conv(
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
Brian Carlstromfc7120c2012-08-27 13:43:25 -070069 bool reference_return = main_jni_conv->IsReturnAReference();
70
Ian Rogers00f7d0e2012-07-19 15:28:27 -070071 UniquePtr<ManagedRuntimeCallingConvention> mr_conv(
72 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
73
Brian Carlstromfc7120c2012-08-27 13:43:25 -070074 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
75 // method and the current thread.
76 size_t jni_end_arg_count = 0;
77 if (reference_return) { jni_end_arg_count++; }
78 if (is_synchronized) { jni_end_arg_count++; }
79 const char* jni_end_shorty = jni_end_arg_count == 0 ? "I"
80 : (jni_end_arg_count == 1 ? "II" : "III");
81 UniquePtr<JniCallingConvention> end_jni_conv(
82 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
83
84
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 // Assembler that holds generated instructions
86 UniquePtr<Assembler> jni_asm(Assembler::Create(instruction_set));
87 bool should_disassemble = false;
88
89 // Offsets into data structures
90 // TODO: if cross compiling these offsets are for the host not the target
91 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
92 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
93 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
94
95 // 1. Build the frame saving all callee saves
Brian Carlstromfc7120c2012-08-27 13:43:25 -070096 const size_t frame_size(main_jni_conv->FrameSize());
97 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
99
100 // 2. Set up the StackIndirectReferenceTable
101 mr_conv->ResetIterator(FrameOffset(frame_size));
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700102 main_jni_conv->ResetIterator(FrameOffset(0));
103 __ StoreImmediateToFrame(main_jni_conv->SirtNumRefsOffset(),
104 main_jni_conv->ReferenceCount(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 mr_conv->InterproceduralScratchRegister());
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700106 __ CopyRawPtrFromThread(main_jni_conv->SirtLinkOffset(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700107 Thread::TopSirtOffset(),
108 mr_conv->InterproceduralScratchRegister());
109 __ StoreStackOffsetToThread(Thread::TopSirtOffset(),
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700110 main_jni_conv->SirtOffset(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 mr_conv->InterproceduralScratchRegister());
112
113 // 3. Place incoming reference arguments into SIRT
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700114 main_jni_conv->Next(); // Skip JNIEnv*
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 // 3.5. Create Class argument for static methods out of passed method
116 if (is_static) {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700117 FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 // Check sirt offset is within frame
119 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700120 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 mr_conv->MethodRegister(), mirror::AbstractMethod::DeclaringClassOffset());
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700122 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
123 __ StoreRef(sirt_offset, main_jni_conv->InterproceduralScratchRegister());
124 main_jni_conv->Next(); // in SIRT so move to next argument
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 }
126 while (mr_conv->HasNext()) {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700127 CHECK(main_jni_conv->HasNext());
128 bool ref_param = main_jni_conv->IsCurrentParamAReference();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
130 // References need placing in SIRT and the entry value passing
131 if (ref_param) {
132 // Compute SIRT entry, note null is placed in the SIRT but its boxed value
133 // must be NULL
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700134 FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 // Check SIRT offset is within frame and doesn't run into the saved segment state
136 CHECK_LT(sirt_offset.Uint32Value(), frame_size);
137 CHECK_NE(sirt_offset.Uint32Value(),
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700138 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
140 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
141 CHECK(input_in_reg || input_on_stack);
142
143 if (input_in_reg) {
144 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
145 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
146 __ StoreRef(sirt_offset, in_reg);
147 } else if (input_on_stack) {
148 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
149 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
150 __ CopyRef(sirt_offset, in_off,
151 mr_conv->InterproceduralScratchRegister());
152 }
153 }
154 mr_conv->Next();
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700155 main_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700156 }
157
158 // 4. Write out the end of the quick frames.
159 __ StoreStackPointerToThread(Thread::TopOfManagedStackOffset());
160 __ StoreImmediateToThread(Thread::TopOfManagedStackPcOffset(), 0,
161 mr_conv->InterproceduralScratchRegister());
162
163 // 5. Move frame down to allow space for out going args.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700164 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
165 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
166 const size_t max_out_arg_size = std::max(main_out_arg_size, end_out_arg_size);
167 __ IncreaseFrameSize(max_out_arg_size);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700168
169
170 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
171 // can occur. The result is the saved JNI local state that is restored by the exit call. We
172 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
173 // arguments.
174 uintptr_t jni_start = is_synchronized ? ENTRYPOINT_OFFSET(pJniMethodStartSynchronized)
175 : ENTRYPOINT_OFFSET(pJniMethodStart);
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700176 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 FrameOffset locked_object_sirt_offset(0);
178 if (is_synchronized) {
179 // Pass object for locking.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700180 main_jni_conv->Next(); // Skip JNIEnv.
181 locked_object_sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset();
182 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
183 if (main_jni_conv->IsCurrentParamOnStack()) {
184 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700185 __ CreateSirtEntry(out_off, locked_object_sirt_offset,
186 mr_conv->InterproceduralScratchRegister(),
187 false);
188 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700189 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 __ CreateSirtEntry(out_reg, locked_object_sirt_offset,
191 ManagedRegister::NoRegister(), false);
192 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700193 main_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700194 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700195 if (main_jni_conv->IsCurrentParamInRegister()) {
196 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
197 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start),
198 main_jni_conv->InterproceduralScratchRegister());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700200 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
201 main_jni_conv->InterproceduralScratchRegister());
202 __ Call(ThreadOffset(jni_start), main_jni_conv->InterproceduralScratchRegister());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700203 }
204 if (is_synchronized) { // Check for exceptions from monitor enter.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700205 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700206 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700207 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
208 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209
210 // 7. Iterate over arguments placing values from managed calling convention in
211 // to the convention required for a native call (shuffling). For references
212 // place an index/pointer to the reference after checking whether it is
213 // NULL (which must be encoded as NULL).
214 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
215 // give as many free registers for the shuffle as possible
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700216 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700217 uint32_t args_count = 0;
218 while (mr_conv->HasNext()) {
219 args_count++;
220 mr_conv->Next();
221 }
222
223 // Do a backward pass over arguments, so that the generated code will be "mov
224 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
225 // TODO: A reverse iterator to improve readability.
226 for (uint32_t i = 0; i < args_count; ++i) {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700227 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
228 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
229 main_jni_conv->Next(); // Skip JNIEnv*.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 if (is_static) {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700231 main_jni_conv->Next(); // Skip Class for now.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700232 }
233 // Skip to the argument we're interested in.
234 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
235 mr_conv->Next();
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700236 main_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700238 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700239 }
240 if (is_static) {
241 // Create argument for Class
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700242 mr_conv->ResetIterator(FrameOffset(frame_size+main_out_arg_size));
243 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
244 main_jni_conv->Next(); // Skip JNIEnv*
245 FrameOffset sirt_offset = main_jni_conv->CurrentParamSirtEntryOffset();
246 if (main_jni_conv->IsCurrentParamOnStack()) {
247 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700248 __ CreateSirtEntry(out_off, sirt_offset,
249 mr_conv->InterproceduralScratchRegister(),
250 false);
251 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700252 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 __ CreateSirtEntry(out_reg, sirt_offset,
254 ManagedRegister::NoRegister(), false);
255 }
256 }
257
258 // 8. Create 1st argument, the JNI environment ptr.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700259 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700260 // Register that will hold local indirect reference table
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700261 if (main_jni_conv->IsCurrentParamInRegister()) {
262 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
263 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700264 __ LoadRawPtrFromThread(jni_env, Thread::JniEnvOffset());
265 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700266 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700267 __ CopyRawPtrFromThread(jni_env, Thread::JniEnvOffset(),
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700268 main_jni_conv->InterproceduralScratchRegister());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700269 }
270
271 // 9. Plant call to native code associated with method.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 __ Call(main_jni_conv->MethodStackOffset(), mirror::AbstractMethod::NativeMethodOffset(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700273 mr_conv->InterproceduralScratchRegister());
274
275 // 10. Fix differences in result widths.
276 if (instruction_set == kX86) {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700277 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
278 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
279 __ SignExtend(main_jni_conv->ReturnRegister(),
280 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
281 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
282 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
283 __ ZeroExtend(main_jni_conv->ReturnRegister(),
284 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 }
286 }
287
288 // 11. Save return value
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700289 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
290 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
jeffhao07030602012-09-26 14:33:14 -0700291 if (instruction_set == kMips && main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
292 return_save_location.Uint32Value() % 8 != 0) {
293 // Ensure doubles are 8-byte aligned for MIPS
294 return_save_location = FrameOffset(return_save_location.Uint32Value() + kPointerSize);
295 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700296 CHECK_LT(return_save_location.Uint32Value(), frame_size+main_out_arg_size);
297 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700298 }
299
300 // 12. Call into JNI method end possibly passing a returned reference, the method and the current
301 // thread.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700302 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700303 uintptr_t jni_end;
304 if (reference_return) {
305 // Pass result.
306 jni_end = is_synchronized ? ENTRYPOINT_OFFSET(pJniMethodEndWithReferenceSynchronized)
307 : ENTRYPOINT_OFFSET(pJniMethodEndWithReference);
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700308 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
309 end_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700310 } else {
311 jni_end = is_synchronized ? ENTRYPOINT_OFFSET(pJniMethodEndSynchronized)
312 : ENTRYPOINT_OFFSET(pJniMethodEnd);
313 }
314 // Pass saved local reference state.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700315 if (end_jni_conv->IsCurrentParamOnStack()) {
316 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
317 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700318 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700319 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 __ Load(out_reg, saved_cookie_offset, 4);
321 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700322 end_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 if (is_synchronized) {
324 // Pass object for unlocking.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700325 if (end_jni_conv->IsCurrentParamOnStack()) {
326 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700327 __ CreateSirtEntry(out_off, locked_object_sirt_offset,
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700328 end_jni_conv->InterproceduralScratchRegister(),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700329 false);
330 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700331 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 __ CreateSirtEntry(out_reg, locked_object_sirt_offset,
333 ManagedRegister::NoRegister(), false);
334 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700335 end_jni_conv->Next();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 }
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700337 if (end_jni_conv->IsCurrentParamInRegister()) {
338 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
339 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end),
340 end_jni_conv->InterproceduralScratchRegister());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 } else {
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700342 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
343 end_jni_conv->InterproceduralScratchRegister());
344 __ Call(ThreadOffset(jni_end), end_jni_conv->InterproceduralScratchRegister());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 }
346
347 // 13. Reload return value
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700348 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700349 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
350 }
351
352 // 14. Move frame up now we're done with the out arg space.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700353 __ DecreaseFrameSize(max_out_arg_size);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700354
355 // 15. Process pending exceptions from JNI call or monitor exit.
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700356 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357
358 // 16. Remove activation - no need to restore callee save registers because we didn't clobber
359 // them.
360 __ RemoveFrame(frame_size, std::vector<ManagedRegister>());
361
362 // 17. Finalize code generation
363 __ EmitSlowPaths();
364 size_t cs = __ CodeSize();
365 std::vector<uint8_t> managed_code(cs);
366 MemoryRegion code(&managed_code[0], managed_code.size());
367 __ FinalizeInstructions(code);
368 if (should_disassemble) {
369 UniquePtr<Disassembler> disassembler(Disassembler::Create(instruction_set));
370 disassembler->Dump(LOG(INFO), &managed_code[0], &managed_code[managed_code.size()]);
371 }
372 return new CompiledMethod(instruction_set,
373 managed_code,
374 frame_size,
Brian Carlstromfc7120c2012-08-27 13:43:25 -0700375 main_jni_conv->CoreSpillMask(),
376 main_jni_conv->FpSpillMask());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700377}
378
Elliott Hughes46f060a2012-03-09 17:36:50 -0800379// Copy a single parameter from the managed to the JNI calling convention
380static void CopyParameter(Assembler* jni_asm,
381 ManagedRuntimeCallingConvention* mr_conv,
382 JniCallingConvention* jni_conv,
383 size_t frame_size, size_t out_arg_size) {
384 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
385 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
386 FrameOffset sirt_offset(0);
387 bool null_allowed = false;
388 bool ref_param = jni_conv->IsCurrentParamAReference();
389 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
390 // input may be in register, on stack or both - but not none!
391 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
392 if (output_in_reg) { // output shouldn't straddle registers and stack
393 CHECK(!jni_conv->IsCurrentParamOnStack());
394 } else {
395 CHECK(jni_conv->IsCurrentParamOnStack());
396 }
397 // References need placing in SIRT and the entry address passing
398 if (ref_param) {
399 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
400 // Compute SIRT offset. Note null is placed in the SIRT but the jobject
401 // passed to the native code must be null (not a pointer into the SIRT
402 // as with regular references).
403 sirt_offset = jni_conv->CurrentParamSirtEntryOffset();
404 // Check SIRT offset is within frame.
405 CHECK_LT(sirt_offset.Uint32Value(), (frame_size + out_arg_size));
406 }
407 if (input_in_reg && output_in_reg) {
408 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
409 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
410 if (ref_param) {
411 __ CreateSirtEntry(out_reg, sirt_offset, in_reg, null_allowed);
412 } else {
413 if (!mr_conv->IsCurrentParamOnStack()) {
414 // regular non-straddling move
Ian Rogersb5d09b22012-03-06 22:14:17 -0800415 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
Elliott Hughes46f060a2012-03-09 17:36:50 -0800416 } else {
417 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
418 }
419 }
420 } else if (!input_in_reg && !output_in_reg) {
421 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
422 if (ref_param) {
423 __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(),
424 null_allowed);
425 } else {
426 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
427 size_t param_size = mr_conv->CurrentParamSize();
428 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
429 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
430 }
431 } else if (!input_in_reg && output_in_reg) {
432 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
433 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
434 // Check that incoming stack arguments are above the current stack frame.
435 CHECK_GT(in_off.Uint32Value(), frame_size);
436 if (ref_param) {
437 __ CreateSirtEntry(out_reg, sirt_offset, ManagedRegister::NoRegister(), null_allowed);
438 } else {
439 size_t param_size = mr_conv->CurrentParamSize();
440 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
441 __ Load(out_reg, in_off, param_size);
442 }
443 } else {
444 CHECK(input_in_reg && !output_in_reg);
445 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
446 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
447 // Check outgoing argument is within frame
448 CHECK_LT(out_off.Uint32Value(), frame_size);
449 if (ref_param) {
450 // TODO: recycle value in in_reg rather than reload from SIRT
451 __ CreateSirtEntry(out_off, sirt_offset, mr_conv->InterproceduralScratchRegister(),
452 null_allowed);
453 } else {
454 size_t param_size = mr_conv->CurrentParamSize();
455 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
456 if (!mr_conv->IsCurrentParamOnStack()) {
457 // regular non-straddling store
458 __ Store(out_off, in_reg, param_size);
459 } else {
460 // store where input straddles registers and stack
461 CHECK_EQ(param_size, 8u);
462 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
463 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
464 }
465 }
466 }
467}
468
469static void SetNativeParameter(Assembler* jni_asm,
470 JniCallingConvention* jni_conv,
471 ManagedRegister in_reg) {
472 if (jni_conv->IsCurrentParamOnStack()) {
473 FrameOffset dest = jni_conv->CurrentParamStackOffset();
474 __ StoreRawPtr(dest, in_reg);
475 } else {
476 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800477 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
Elliott Hughes46f060a2012-03-09 17:36:50 -0800478 }
479 }
480}
Ian Rogers2c8f6532011-09-02 17:16:34 -0700481
Ian Rogersb033c752011-07-20 12:22:35 -0700482} // namespace art
Elliott Hughes46f060a2012-03-09 17:36:50 -0800483
Ian Rogers1212a022013-03-04 10:48:41 -0800484extern "C" art::CompiledMethod* ArtQuickJniCompileMethod(art::CompilerDriver& compiler,
Brian Carlstrom00bc1dc2013-02-01 15:56:27 -0800485 uint32_t access_flags, uint32_t method_idx,
486 const art::DexFile& dex_file) {
Ian Rogers57b86d42012-03-27 16:05:41 -0700487 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
Elliott Hughes46f060a2012-03-09 17:36:50 -0800488}