blob: 1cbe5d92e990083e5bdd74c096b90d2b37417d7d [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 */
Carl Shapiro9b9ba282011-08-14 15:30:39 -070016
Elliott Hughes77405792012-03-15 15:22:12 -070017#include <stdint.h>
Carl Shapiro9b9ba282011-08-14 15:30:39 -070018
19#include <algorithm>
20
Ian Rogers4a510d82011-10-09 14:30:24 -070021#include "asm_support.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070022#include "compiled_method.h"
Ian Rogers1212a022013-03-04 10:48:41 -080023#include "compiler/driver/compiler_driver.h"
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070024#include "invoke_arg_array_builder.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070025#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "mirror/abstract_method.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027#include "oat/utils/arm/assembler_arm.h"
28#include "oat/utils/assembler.h"
Carl Shapiro9b9ba282011-08-14 15:30:39 -070029
Carl Shapiro9b9ba282011-08-14 15:30:39 -070030namespace art {
Ian Rogers2c8f6532011-09-02 17:16:34 -070031namespace arm {
Carl Shapiro9b9ba282011-08-14 15:30:39 -070032
33// Creates a function which invokes a managed method with an array of
34// arguments.
35//
36// At the time of call, the environment looks something like this:
37//
38// R0 = method pointer
39// R1 = receiver pointer or NULL for static methods
40// R2 = (managed) thread pointer
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070041// R3 = argument array or NULL for no argument methods
42// [SP] = JValue* result or NULL for void returns
Carl Shapiro9b9ba282011-08-14 15:30:39 -070043//
44// As the JNI call has already transitioned the thread into the
45// "running" state the remaining responsibilities of this routine are
46// to save the native register value and restore the managed thread
47// register and transfer arguments from the array into register and on
48// the stack, if needed. On return, the thread register must be
49// shuffled and the return value must be store into the result JValue.
Elliott Hughes46f060a2012-03-09 17:36:50 -080050CompiledInvokeStub* CreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len) {
51 UniquePtr<ArmAssembler> assembler(down_cast<ArmAssembler*>(Assembler::Create(kArm)));
Ian Rogers2c8f6532011-09-02 17:16:34 -070052#define __ assembler->
Ian Rogers45619fc2012-02-29 11:15:25 -080053 size_t num_arg_array_bytes = NumArgArrayBytes(shorty, shorty_len);
Ian Rogersb5d09b22012-03-06 22:14:17 -080054 // Size of frame = spill of R4,R9/LR + Method* + possible receiver + arg array size
55 // Note, space is left in the frame to flush arguments in registers back to out locations.
buzbeec1f45042011-09-21 16:03:19 -070056 size_t unpadded_frame_size = (4 * kPointerSize) +
Ian Rogers0571d352011-11-03 19:51:38 -070057 (is_static ? 0 : kPointerSize) +
58 num_arg_array_bytes;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070059 size_t frame_size = RoundUp(unpadded_frame_size, kStackAlignment);
Carl Shapiro9b9ba282011-08-14 15:30:39 -070060
buzbeec1f45042011-09-21 16:03:19 -070061 // Spill R4,R9 and LR
62 RegList save = (1 << R9) | (1 << R4);
Carl Shapiro9b9ba282011-08-14 15:30:39 -070063 __ PushList(save | (1 << LR));
64
Carl Shapiro9b9ba282011-08-14 15:30:39 -070065 // Move the managed thread pointer into R9.
66 __ mov(R9, ShifterOperand(R2));
67
Ian Rogersae675992011-10-09 17:10:22 -070068 // Reset R4 to suspend check interval
Ian Rogers4a510d82011-10-09 14:30:24 -070069 __ LoadImmediate(R4, SUSPEND_CHECK_INTERVAL);
70
buzbeec1f45042011-09-21 16:03:19 -070071 // Move frame down for arguments less 3 pushed values above
72 __ AddConstant(SP, -frame_size + (3 * kPointerSize));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070073
74 // Can either get 3 or 2 arguments into registers
Ian Rogers0571d352011-11-03 19:51:38 -070075 size_t reg_bytes = (is_static ? 3 : 2) * kPointerSize;
Brian Carlstromfd2ec542012-05-02 15:08:57 -070076 if (num_arg_array_bytes <= reg_bytes) {
Ian Rogers0571d352011-11-03 19:51:38 -070077 reg_bytes = num_arg_array_bytes;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070078 }
79
Ian Rogersed8952f2011-08-19 17:11:22 -070080 // Method* at bottom of frame is null thereby terminating managed stack crawls
81 __ LoadImmediate(IP, 0, AL);
82 __ StoreToOffset(kStoreWord, IP, SP, 0);
83
Elliott Hughes77405792012-03-15 15:22:12 -070084 // Copy values onto the stack.
85 size_t src_offset = 0;
86 size_t dst_offset = (is_static ? 1 : 2) * kPointerSize;
87 for (size_t i = 1; i < shorty_len; ++i) {
88 switch (shorty[i]) {
89 case 'D':
90 case 'J':
91 // Move both pointers 64 bits.
92 __ LoadFromOffset(kLoadWord, IP, R3, src_offset);
93 src_offset += kPointerSize;
94 __ StoreToOffset(kStoreWord, IP, SP, dst_offset);
95 dst_offset += kPointerSize;
Ian Rogerscdd1d2d2011-08-18 09:58:17 -070096
Elliott Hughes77405792012-03-15 15:22:12 -070097 __ LoadFromOffset(kLoadWord, IP, R3, src_offset);
98 src_offset += kPointerSize;
99 __ StoreToOffset(kStoreWord, IP, SP, dst_offset);
100 dst_offset += kPointerSize;
101 break;
102 default:
103 // Move the source pointer sizeof(JValue) and the destination pointer 32 bits.
104 __ LoadFromOffset(kLoadWord, IP, R3, src_offset);
105 src_offset += sizeof(JValue);
106 __ StoreToOffset(kStoreWord, IP, SP, dst_offset);
107 dst_offset += kPointerSize;
108 break;
109 }
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700110 }
111
112 // Move all the register arguments into place.
Elliott Hughes77405792012-03-15 15:22:12 -0700113 dst_offset = (is_static ? 1 : 2) * kPointerSize;
Ian Rogers0571d352011-11-03 19:51:38 -0700114 if (is_static) {
Elliott Hughes77405792012-03-15 15:22:12 -0700115 if (reg_bytes > 0 && num_arg_array_bytes > 0) {
116 __ LoadFromOffset(kLoadWord, R1, SP, dst_offset + 0);
117 if (reg_bytes > 4 && num_arg_array_bytes > 4) {
118 __ LoadFromOffset(kLoadWord, R2, SP, dst_offset + 4);
119 if (reg_bytes > 8 && num_arg_array_bytes > 8) {
120 __ LoadFromOffset(kLoadWord, R3, SP, dst_offset + 8);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700121 }
122 }
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700123 }
124 } else {
Elliott Hughes77405792012-03-15 15:22:12 -0700125 if (reg_bytes > 0 && num_arg_array_bytes > 0) {
126 __ LoadFromOffset(kLoadWord, R2, SP, dst_offset + 0);
127 if (reg_bytes > 4 && num_arg_array_bytes > 4) {
128 __ LoadFromOffset(kLoadWord, R3, SP, dst_offset + 4);
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700129 }
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700130 }
131 }
132
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700133 // Load the code pointer we are about to call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 __ LoadFromOffset(kLoadWord, IP, R0, mirror::AbstractMethod::GetCodeOffset().Int32Value());
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700135
136 // Do the call.
137 __ blx(IP);
138
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700139 // If the method returns a value, store it to the result pointer.
Ian Rogers0571d352011-11-03 19:51:38 -0700140 if (shorty[0] != 'V') {
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700141 // Load the result JValue pointer of the stub caller's out args.
142 __ LoadFromOffset(kLoadWord, IP, SP, frame_size);
Ian Rogers0571d352011-11-03 19:51:38 -0700143 StoreOperandType type = (shorty[0] == 'J' || shorty[0] == 'D') ? kStoreWordPair : kStoreWord;
144 __ StoreToOffset(type, R0, IP, 0);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700145 }
146
buzbeec1f45042011-09-21 16:03:19 -0700147 // Remove the frame less the spilled R4, R9 and LR
148 __ AddConstant(SP, frame_size - (3 * kPointerSize));
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700149
buzbeec1f45042011-09-21 16:03:19 -0700150 // Pop R4, R9 and the LR into PC
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700151 __ PopList(save | (1 << PC));
Ian Rogers2c8f6532011-09-02 17:16:34 -0700152 // TODO: store native_entry in the stub table
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700153 std::vector<uint8_t> code(assembler->CodeSize());
154 MemoryRegion region(&code[0], code.size());
Ian Rogers2c8f6532011-09-02 17:16:34 -0700155 assembler->FinalizeInstructions(region);
Logan Chien598c5132012-04-28 22:00:44 +0800156 return new CompiledInvokeStub(kArm, code);
Ian Rogers2c8f6532011-09-02 17:16:34 -0700157#undef __
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700158}
159
Ian Rogers2c8f6532011-09-02 17:16:34 -0700160} // namespace arm
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700161} // namespace art
Elliott Hughes46f060a2012-03-09 17:36:50 -0800162
Ian Rogers1212a022013-03-04 10:48:41 -0800163extern "C" art::CompiledInvokeStub* ArtCreateArmInvokeStub(art::CompilerDriver& /*compiler*/, bool is_static,
Ian Rogers57b86d42012-03-27 16:05:41 -0700164 const char* shorty, uint32_t shorty_len) {
Elliott Hughes46f060a2012-03-09 17:36:50 -0800165 return art::arm::CreateInvokeStub(is_static, shorty, shorty_len);
166}