blob: 49b7cd1aa58008059853b8cdf120da0f7cc057d3 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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 */
16
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "jni_compiler.h"
18
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include <algorithm>
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include <vector>
Dave Allison65fcc2c2014-04-28 13:45:27 -070022#include <fstream>
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
24#include "base/logging.h"
25#include "base/macros.h"
26#include "calling_convention.h"
27#include "class_linker.h"
28#include "compiled_method.h"
29#include "dex_file-inl.h"
30#include "driver/compiler_driver.h"
Ian Rogers166db042013-07-26 12:05:57 -070031#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070032#include "jni_env_ext.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070033#include "mirror/art_method.h"
Ian Rogers166db042013-07-26 12:05:57 -070034#include "utils/assembler.h"
35#include "utils/managed_register.h"
36#include "utils/arm/managed_register_arm.h"
Serban Constantinescu75b91132014-04-09 18:39:10 +010037#include "utils/arm64/managed_register_arm64.h"
Ian Rogers166db042013-07-26 12:05:57 -070038#include "utils/mips/managed_register_mips.h"
Maja Gagic6ea651f2015-02-24 16:55:04 +010039#include "utils/mips64/managed_register_mips64.h"
Ian Rogers166db042013-07-26 12:05:57 -070040#include "utils/x86/managed_register_x86.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070041#include "thread.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070042
43#define __ jni_asm->
44
45namespace art {
46
47static void CopyParameter(Assembler* jni_asm,
48 ManagedRuntimeCallingConvention* mr_conv,
49 JniCallingConvention* jni_conv,
50 size_t frame_size, size_t out_arg_size);
51static void SetNativeParameter(Assembler* jni_asm,
52 JniCallingConvention* jni_conv,
53 ManagedRegister in_reg);
54
55// Generate the JNI bridge for the given method, general contract:
56// - Arguments are in the managed runtime format, either on stack or in
57// registers, a reference to the method object is supplied as part of this
58// convention.
59//
Ian Rogers72d32622014-05-06 16:20:11 -070060CompiledMethod* ArtJniCompileMethodInternal(CompilerDriver* driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 uint32_t access_flags, uint32_t method_idx,
62 const DexFile& dex_file) {
63 const bool is_native = (access_flags & kAccNative) != 0;
64 CHECK(is_native);
65 const bool is_static = (access_flags & kAccStatic) != 0;
66 const bool is_synchronized = (access_flags & kAccSynchronized) != 0;
67 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Ian Rogers72d32622014-05-06 16:20:11 -070068 InstructionSet instruction_set = driver->GetInstructionSet();
Andreas Gampeaf13ad92014-04-11 12:07:48 -070069 const bool is_64_bit_target = Is64BitInstructionSet(instruction_set);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 // Calling conventions used to iterate over parameters to method
Ian Rogers700a4022014-05-19 16:49:03 -070071 std::unique_ptr<JniCallingConvention> main_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 JniCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
73 bool reference_return = main_jni_conv->IsReturnAReference();
74
Ian Rogers700a4022014-05-19 16:49:03 -070075 std::unique_ptr<ManagedRuntimeCallingConvention> mr_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 ManagedRuntimeCallingConvention::Create(is_static, is_synchronized, shorty, instruction_set));
77
78 // Calling conventions to call into JNI method "end" possibly passing a returned reference, the
79 // method and the current thread.
Serban Constantinescu75b91132014-04-09 18:39:10 +010080 const char* jni_end_shorty;
81 if (reference_return && is_synchronized) {
82 jni_end_shorty = "ILL";
83 } else if (reference_return) {
84 jni_end_shorty = "IL";
85 } else if (is_synchronized) {
86 jni_end_shorty = "VL";
87 } else {
88 jni_end_shorty = "V";
89 }
90
Ian Rogers700a4022014-05-19 16:49:03 -070091 std::unique_ptr<JniCallingConvention> end_jni_conv(
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 JniCallingConvention::Create(is_static, is_synchronized, jni_end_shorty, instruction_set));
93
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 // Assembler that holds generated instructions
Ian Rogers700a4022014-05-19 16:49:03 -070095 std::unique_ptr<Assembler> jni_asm(Assembler::Create(instruction_set));
Brian Carlstrom7940e442013-07-12 13:46:57 -070096
97 // Offsets into data structures
98 // TODO: if cross compiling these offsets are for the host not the target
99 const Offset functions(OFFSETOF_MEMBER(JNIEnvExt, functions));
100 const Offset monitor_enter(OFFSETOF_MEMBER(JNINativeInterface, MonitorEnter));
101 const Offset monitor_exit(OFFSETOF_MEMBER(JNINativeInterface, MonitorExit));
102
103 // 1. Build the frame saving all callee saves
104 const size_t frame_size(main_jni_conv->FrameSize());
105 const std::vector<ManagedRegister>& callee_save_regs = main_jni_conv->CalleeSaveRegisters();
106 __ BuildFrame(frame_size, mr_conv->MethodRegister(), callee_save_regs, mr_conv->EntrySpills());
David Srbeckydd973932015-04-07 20:29:48 +0100107 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 // 2. Set up the HandleScope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 mr_conv->ResetIterator(FrameOffset(frame_size));
111 main_jni_conv->ResetIterator(FrameOffset(0));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 __ StoreImmediateToFrame(main_jni_conv->HandleScopeNumRefsOffset(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 main_jni_conv->ReferenceCount(),
114 mr_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100115
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700116 if (is_64_bit_target) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 __ CopyRawPtrFromThread64(main_jni_conv->HandleScopeLinkOffset(),
118 Thread::TopHandleScopeOffset<8>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100119 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700120 __ StoreStackOffsetToThread64(Thread::TopHandleScopeOffset<8>(),
121 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100122 mr_conv->InterproceduralScratchRegister());
123 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700124 __ CopyRawPtrFromThread32(main_jni_conv->HandleScopeLinkOffset(),
125 Thread::TopHandleScopeOffset<4>(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100126 mr_conv->InterproceduralScratchRegister());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700127 __ StoreStackOffsetToThread32(Thread::TopHandleScopeOffset<4>(),
128 main_jni_conv->HandleScopeOffset(),
Serban Constantinescu75b91132014-04-09 18:39:10 +0100129 mr_conv->InterproceduralScratchRegister());
130 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700131
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700132 // 3. Place incoming reference arguments into handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 main_jni_conv->Next(); // Skip JNIEnv*
134 // 3.5. Create Class argument for static methods out of passed method
135 if (is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700136 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
137 // Check handle scope offset is within frame
138 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800139 // TODO: Insert the read barrier for this load.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 __ LoadRef(main_jni_conv->InterproceduralScratchRegister(),
Brian Carlstromea46f952013-07-30 01:26:50 -0700141 mr_conv->MethodRegister(), mirror::ArtMethod::DeclaringClassOffset());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700142 __ VerifyObject(main_jni_conv->InterproceduralScratchRegister(), false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700143 __ StoreRef(handle_scope_offset, main_jni_conv->InterproceduralScratchRegister());
144 main_jni_conv->Next(); // in handle scope so move to next argument
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 }
146 while (mr_conv->HasNext()) {
147 CHECK(main_jni_conv->HasNext());
148 bool ref_param = main_jni_conv->IsCurrentParamAReference();
149 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700150 // References need placing in handle scope and the entry value passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 // Compute handle scope entry, note null is placed in the handle scope but its boxed value
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 // must be NULL
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700154 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
155 // Check handle scope offset is within frame and doesn't run into the saved segment state
156 CHECK_LT(handle_scope_offset.Uint32Value(), frame_size);
157 CHECK_NE(handle_scope_offset.Uint32Value(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 main_jni_conv->SavedLocalReferenceCookieOffset().Uint32Value());
159 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
160 bool input_on_stack = mr_conv->IsCurrentParamOnStack();
161 CHECK(input_in_reg || input_on_stack);
162
163 if (input_in_reg) {
164 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
165 __ VerifyObject(in_reg, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700166 __ StoreRef(handle_scope_offset, in_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700167 } else if (input_on_stack) {
168 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
169 __ VerifyObject(in_off, mr_conv->IsCurrentArgPossiblyNull());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700170 __ CopyRef(handle_scope_offset, in_off,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 mr_conv->InterproceduralScratchRegister());
172 }
173 }
174 mr_conv->Next();
175 main_jni_conv->Next();
176 }
177
178 // 4. Write out the end of the quick frames.
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700179 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100180 __ StoreStackPointerToThread64(Thread::TopOfManagedStackOffset<8>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100181 } else {
182 __ StoreStackPointerToThread32(Thread::TopOfManagedStackOffset<4>());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100183 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184
185 // 5. Move frame down to allow space for out going args.
186 const size_t main_out_arg_size = main_jni_conv->OutArgSize();
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100187 size_t current_out_arg_size = main_out_arg_size;
188 __ IncreaseFrameSize(main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 // 6. Call into appropriate JniMethodStart passing Thread* so that transition out of Runnable
191 // can occur. The result is the saved JNI local state that is restored by the exit call. We
192 // abuse the JNI calling convention here, that is guaranteed to support passing 2 pointer
193 // arguments.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100194 ThreadOffset<4> jni_start32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStartSynchronized)
195 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodStart);
196 ThreadOffset<8> jni_start64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStartSynchronized)
197 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodStart);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700198 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 FrameOffset locked_object_handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200 if (is_synchronized) {
201 // Pass object for locking.
202 main_jni_conv->Next(); // Skip JNIEnv.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700203 locked_object_handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
205 if (main_jni_conv->IsCurrentParamOnStack()) {
206 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700207 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208 mr_conv->InterproceduralScratchRegister(),
209 false);
210 } else {
211 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700212 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700213 ManagedRegister::NoRegister(), false);
214 }
215 main_jni_conv->Next();
216 }
217 if (main_jni_conv->IsCurrentParamInRegister()) {
218 __ GetCurrentThread(main_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700219 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100220 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start64),
221 main_jni_conv->InterproceduralScratchRegister());
222 } else {
223 __ Call(main_jni_conv->CurrentParamRegister(), Offset(jni_start32),
224 main_jni_conv->InterproceduralScratchRegister());
225 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 } else {
227 __ GetCurrentThread(main_jni_conv->CurrentParamStackOffset(),
228 main_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700229 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100230 __ CallFromThread64(jni_start64, main_jni_conv->InterproceduralScratchRegister());
231 } else {
232 __ CallFromThread32(jni_start32, main_jni_conv->InterproceduralScratchRegister());
233 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700234 }
235 if (is_synchronized) { // Check for exceptions from monitor enter.
236 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), main_out_arg_size);
237 }
238 FrameOffset saved_cookie_offset = main_jni_conv->SavedLocalReferenceCookieOffset();
239 __ Store(saved_cookie_offset, main_jni_conv->IntReturnRegister(), 4);
240
241 // 7. Iterate over arguments placing values from managed calling convention in
242 // to the convention required for a native call (shuffling). For references
243 // place an index/pointer to the reference after checking whether it is
244 // NULL (which must be encoded as NULL).
245 // Note: we do this prior to materializing the JNIEnv* and static's jclass to
246 // give as many free registers for the shuffle as possible
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100247 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 uint32_t args_count = 0;
249 while (mr_conv->HasNext()) {
250 args_count++;
251 mr_conv->Next();
252 }
253
254 // Do a backward pass over arguments, so that the generated code will be "mov
255 // R2, R3; mov R1, R2" instead of "mov R1, R2; mov R2, R3."
256 // TODO: A reverse iterator to improve readability.
257 for (uint32_t i = 0; i < args_count; ++i) {
258 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
259 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
260 main_jni_conv->Next(); // Skip JNIEnv*.
261 if (is_static) {
262 main_jni_conv->Next(); // Skip Class for now.
263 }
264 // Skip to the argument we're interested in.
265 for (uint32_t j = 0; j < args_count - i - 1; ++j) {
266 mr_conv->Next();
267 main_jni_conv->Next();
268 }
269 CopyParameter(jni_asm.get(), mr_conv.get(), main_jni_conv.get(), frame_size, main_out_arg_size);
270 }
271 if (is_static) {
272 // Create argument for Class
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100273 mr_conv->ResetIterator(FrameOffset(frame_size + main_out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700274 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
275 main_jni_conv->Next(); // Skip JNIEnv*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700276 FrameOffset handle_scope_offset = main_jni_conv->CurrentParamHandleScopeEntryOffset();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700277 if (main_jni_conv->IsCurrentParamOnStack()) {
278 FrameOffset out_off = main_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700279 __ CreateHandleScopeEntry(out_off, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280 mr_conv->InterproceduralScratchRegister(),
281 false);
282 } else {
283 ManagedRegister out_reg = main_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700284 __ CreateHandleScopeEntry(out_reg, handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 ManagedRegister::NoRegister(), false);
286 }
287 }
288
289 // 8. Create 1st argument, the JNI environment ptr.
290 main_jni_conv->ResetIterator(FrameOffset(main_out_arg_size));
291 // Register that will hold local indirect reference table
292 if (main_jni_conv->IsCurrentParamInRegister()) {
293 ManagedRegister jni_env = main_jni_conv->CurrentParamRegister();
294 DCHECK(!jni_env.Equals(main_jni_conv->InterproceduralScratchRegister()));
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700295 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100296 __ LoadRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>());
297 } else {
298 __ LoadRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>());
299 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 } else {
301 FrameOffset jni_env = main_jni_conv->CurrentParamStackOffset();
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700302 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100303 __ CopyRawPtrFromThread64(jni_env, Thread::JniEnvOffset<8>(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700304 main_jni_conv->InterproceduralScratchRegister());
Serban Constantinescu75b91132014-04-09 18:39:10 +0100305 } else {
306 __ CopyRawPtrFromThread32(jni_env, Thread::JniEnvOffset<4>(),
307 main_jni_conv->InterproceduralScratchRegister());
308 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 }
310
311 // 9. Plant call to native code associated with method.
Mathieu Chartier2d721012014-11-10 11:08:06 -0800312 MemberOffset jni_entrypoint_offset = mirror::ArtMethod::EntryPointFromJniOffset(
313 InstructionSetPointerSize(instruction_set));
314 __ Call(main_jni_conv->MethodStackOffset(), jni_entrypoint_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700315 mr_conv->InterproceduralScratchRegister());
316
317 // 10. Fix differences in result widths.
Andreas Gamped1104322014-05-01 14:38:56 -0700318 if (main_jni_conv->RequiresSmallResultTypeExtension()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319 if (main_jni_conv->GetReturnType() == Primitive::kPrimByte ||
320 main_jni_conv->GetReturnType() == Primitive::kPrimShort) {
321 __ SignExtend(main_jni_conv->ReturnRegister(),
322 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
323 } else if (main_jni_conv->GetReturnType() == Primitive::kPrimBoolean ||
324 main_jni_conv->GetReturnType() == Primitive::kPrimChar) {
325 __ ZeroExtend(main_jni_conv->ReturnRegister(),
326 Primitive::ComponentSize(main_jni_conv->GetReturnType()));
327 }
328 }
329
330 // 11. Save return value
331 FrameOffset return_save_location = main_jni_conv->ReturnValueSaveLocation();
332 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
Maja Gagic6ea651f2015-02-24 16:55:04 +0100333 if ((instruction_set == kMips || instruction_set == kMips64) &&
334 main_jni_conv->GetReturnType() == Primitive::kPrimDouble &&
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 return_save_location.Uint32Value() % 8 != 0) {
336 // Ensure doubles are 8-byte aligned for MIPS
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700337 return_save_location = FrameOffset(return_save_location.Uint32Value() + kMipsPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700338 }
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100339 CHECK_LT(return_save_location.Uint32Value(), frame_size + main_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700340 __ Store(return_save_location, main_jni_conv->ReturnRegister(), main_jni_conv->SizeOfReturnValue());
341 }
342
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100343 // Increase frame size for out args if needed by the end_jni_conv.
344 const size_t end_out_arg_size = end_jni_conv->OutArgSize();
345 if (end_out_arg_size > current_out_arg_size) {
346 size_t out_arg_size_diff = end_out_arg_size - current_out_arg_size;
347 current_out_arg_size = end_out_arg_size;
348 __ IncreaseFrameSize(out_arg_size_diff);
349 saved_cookie_offset = FrameOffset(saved_cookie_offset.SizeValue() + out_arg_size_diff);
350 locked_object_handle_scope_offset =
351 FrameOffset(locked_object_handle_scope_offset.SizeValue() + out_arg_size_diff);
352 return_save_location = FrameOffset(return_save_location.SizeValue() + out_arg_size_diff);
353 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 // thread.
355 end_jni_conv->ResetIterator(FrameOffset(end_out_arg_size));
Serban Constantinescu75b91132014-04-09 18:39:10 +0100356 ThreadOffset<4> jni_end32(-1);
357 ThreadOffset<8> jni_end64(-1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 if (reference_return) {
359 // Pass result.
Serban Constantinescu75b91132014-04-09 18:39:10 +0100360 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReferenceSynchronized)
361 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndWithReference);
362 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReferenceSynchronized)
363 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndWithReference);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364 SetNativeParameter(jni_asm.get(), end_jni_conv.get(), end_jni_conv->ReturnRegister());
365 end_jni_conv->Next();
366 } else {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100367 jni_end32 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEndSynchronized)
368 : QUICK_ENTRYPOINT_OFFSET(4, pJniMethodEnd);
369 jni_end64 = is_synchronized ? QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEndSynchronized)
370 : QUICK_ENTRYPOINT_OFFSET(8, pJniMethodEnd);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 }
372 // Pass saved local reference state.
373 if (end_jni_conv->IsCurrentParamOnStack()) {
374 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
375 __ Copy(out_off, saved_cookie_offset, end_jni_conv->InterproceduralScratchRegister(), 4);
376 } else {
377 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
378 __ Load(out_reg, saved_cookie_offset, 4);
379 }
380 end_jni_conv->Next();
381 if (is_synchronized) {
382 // Pass object for unlocking.
383 if (end_jni_conv->IsCurrentParamOnStack()) {
384 FrameOffset out_off = end_jni_conv->CurrentParamStackOffset();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700385 __ CreateHandleScopeEntry(out_off, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 end_jni_conv->InterproceduralScratchRegister(),
387 false);
388 } else {
389 ManagedRegister out_reg = end_jni_conv->CurrentParamRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700390 __ CreateHandleScopeEntry(out_reg, locked_object_handle_scope_offset,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700391 ManagedRegister::NoRegister(), false);
392 }
393 end_jni_conv->Next();
394 }
395 if (end_jni_conv->IsCurrentParamInRegister()) {
396 __ GetCurrentThread(end_jni_conv->CurrentParamRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700397 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100398 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end64),
399 end_jni_conv->InterproceduralScratchRegister());
400 } else {
401 __ Call(end_jni_conv->CurrentParamRegister(), Offset(jni_end32),
402 end_jni_conv->InterproceduralScratchRegister());
403 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700404 } else {
405 __ GetCurrentThread(end_jni_conv->CurrentParamStackOffset(),
406 end_jni_conv->InterproceduralScratchRegister());
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700407 if (is_64_bit_target) {
Serban Constantinescu75b91132014-04-09 18:39:10 +0100408 __ CallFromThread64(ThreadOffset<8>(jni_end64), end_jni_conv->InterproceduralScratchRegister());
409 } else {
410 __ CallFromThread32(ThreadOffset<4>(jni_end32), end_jni_conv->InterproceduralScratchRegister());
411 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700412 }
413
414 // 13. Reload return value
415 if (main_jni_conv->SizeOfReturnValue() != 0 && !reference_return) {
416 __ Load(mr_conv->ReturnRegister(), return_save_location, mr_conv->SizeOfReturnValue());
417 }
418
419 // 14. Move frame up now we're done with the out arg space.
Vladimir Marko4e24b9d2014-07-24 17:01:58 +0100420 __ DecreaseFrameSize(current_out_arg_size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700421
422 // 15. Process pending exceptions from JNI call or monitor exit.
423 __ ExceptionPoll(main_jni_conv->InterproceduralScratchRegister(), 0);
424
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700425 // 16. Remove activation - need to restore callee save registers since the GC may have changed
Brian Carlstrom7940e442013-07-12 13:46:57 -0700426 // them.
David Srbeckydd973932015-04-07 20:29:48 +0100427 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Mathieu Chartier8770e5c2013-10-16 14:49:01 -0700428 __ RemoveFrame(frame_size, callee_save_regs);
David Srbeckydd973932015-04-07 20:29:48 +0100429 DCHECK_EQ(jni_asm->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430
431 // 17. Finalize code generation
432 __ EmitSlowPaths();
433 size_t cs = __ CodeSize();
434 std::vector<uint8_t> managed_code(cs);
435 MemoryRegion code(&managed_code[0], managed_code.size());
436 __ FinalizeInstructions(code);
David Srbecky8c578312015-04-07 19:46:22 +0100437
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800438 return CompiledMethod::SwapAllocCompiledMethodCFI(driver,
439 instruction_set,
440 ArrayRef<const uint8_t>(managed_code),
441 frame_size,
442 main_jni_conv->CoreSpillMask(),
443 main_jni_conv->FpSpillMask(),
David Srbeckydd973932015-04-07 20:29:48 +0100444 ArrayRef<const uint8_t>(*jni_asm->cfi().data()));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445}
446
447// Copy a single parameter from the managed to the JNI calling convention
448static void CopyParameter(Assembler* jni_asm,
449 ManagedRuntimeCallingConvention* mr_conv,
450 JniCallingConvention* jni_conv,
451 size_t frame_size, size_t out_arg_size) {
452 bool input_in_reg = mr_conv->IsCurrentParamInRegister();
453 bool output_in_reg = jni_conv->IsCurrentParamInRegister();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700454 FrameOffset handle_scope_offset(0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700455 bool null_allowed = false;
456 bool ref_param = jni_conv->IsCurrentParamAReference();
457 CHECK(!ref_param || mr_conv->IsCurrentParamAReference());
458 // input may be in register, on stack or both - but not none!
459 CHECK(input_in_reg || mr_conv->IsCurrentParamOnStack());
460 if (output_in_reg) { // output shouldn't straddle registers and stack
461 CHECK(!jni_conv->IsCurrentParamOnStack());
462 } else {
463 CHECK(jni_conv->IsCurrentParamOnStack());
464 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700465 // References need placing in handle scope and the entry address passing
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 if (ref_param) {
467 null_allowed = mr_conv->IsCurrentArgPossiblyNull();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700468 // Compute handle scope offset. Note null is placed in the handle scope but the jobject
469 // passed to the native code must be null (not a pointer into the handle scope
Brian Carlstrom7940e442013-07-12 13:46:57 -0700470 // as with regular references).
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700471 handle_scope_offset = jni_conv->CurrentParamHandleScopeEntryOffset();
472 // Check handle scope offset is within frame.
473 CHECK_LT(handle_scope_offset.Uint32Value(), (frame_size + out_arg_size));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700474 }
475 if (input_in_reg && output_in_reg) {
476 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
477 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
478 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700479 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, in_reg, null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700480 } else {
481 if (!mr_conv->IsCurrentParamOnStack()) {
482 // regular non-straddling move
483 __ Move(out_reg, in_reg, mr_conv->CurrentParamSize());
484 } else {
485 UNIMPLEMENTED(FATAL); // we currently don't expect to see this case
486 }
487 }
488 } else if (!input_in_reg && !output_in_reg) {
489 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
490 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700491 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 null_allowed);
493 } else {
494 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
495 size_t param_size = mr_conv->CurrentParamSize();
496 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
497 __ Copy(out_off, in_off, mr_conv->InterproceduralScratchRegister(), param_size);
498 }
499 } else if (!input_in_reg && output_in_reg) {
500 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
501 ManagedRegister out_reg = jni_conv->CurrentParamRegister();
502 // Check that incoming stack arguments are above the current stack frame.
503 CHECK_GT(in_off.Uint32Value(), frame_size);
504 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700505 __ CreateHandleScopeEntry(out_reg, handle_scope_offset, ManagedRegister::NoRegister(), null_allowed);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 } else {
507 size_t param_size = mr_conv->CurrentParamSize();
508 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
509 __ Load(out_reg, in_off, param_size);
510 }
511 } else {
512 CHECK(input_in_reg && !output_in_reg);
513 ManagedRegister in_reg = mr_conv->CurrentParamRegister();
514 FrameOffset out_off = jni_conv->CurrentParamStackOffset();
515 // Check outgoing argument is within frame
516 CHECK_LT(out_off.Uint32Value(), frame_size);
517 if (ref_param) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700518 // TODO: recycle value in in_reg rather than reload from handle scope
519 __ CreateHandleScopeEntry(out_off, handle_scope_offset, mr_conv->InterproceduralScratchRegister(),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700520 null_allowed);
521 } else {
522 size_t param_size = mr_conv->CurrentParamSize();
523 CHECK_EQ(param_size, jni_conv->CurrentParamSize());
524 if (!mr_conv->IsCurrentParamOnStack()) {
525 // regular non-straddling store
526 __ Store(out_off, in_reg, param_size);
527 } else {
528 // store where input straddles registers and stack
529 CHECK_EQ(param_size, 8u);
530 FrameOffset in_off = mr_conv->CurrentParamStackOffset();
531 __ StoreSpanning(out_off, in_reg, in_off, mr_conv->InterproceduralScratchRegister());
532 }
533 }
534 }
535}
536
537static void SetNativeParameter(Assembler* jni_asm,
538 JniCallingConvention* jni_conv,
539 ManagedRegister in_reg) {
540 if (jni_conv->IsCurrentParamOnStack()) {
541 FrameOffset dest = jni_conv->CurrentParamStackOffset();
542 __ StoreRawPtr(dest, in_reg);
543 } else {
544 if (!jni_conv->CurrentParamRegister().Equals(in_reg)) {
545 __ Move(jni_conv->CurrentParamRegister(), in_reg, jni_conv->CurrentParamSize());
546 }
547 }
548}
549
Andreas Gampe53c913b2014-08-12 23:19:23 -0700550CompiledMethod* ArtQuickJniCompileMethod(CompilerDriver* compiler, uint32_t access_flags,
551 uint32_t method_idx, const DexFile& dex_file) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 return ArtJniCompileMethodInternal(compiler, access_flags, method_idx, dex_file);
553}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700554
555} // namespace art