blob: 133052d07b55d0543e6bea3162f11676ba44a8b5 [file] [log] [blame]
jeffhao7fbee072012-08-24 17:56:54 -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
17#include <stdint.h>
18
19#include <algorithm>
20
21#include "asm_support.h"
22#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"
jeffhao7fbee072012-08-24 17:56:54 -070027#include "oat/utils/mips/assembler_mips.h"
28#include "oat/utils/assembler.h"
jeffhao7fbee072012-08-24 17:56:54 -070029
30namespace art {
31namespace mips {
32// Creates a function which invokes a managed method with an array of
33// arguments.
34//
35// At the time of call, the environment looks something like this:
36//
37// A0 = method pointer
38// A1 = receiver pointer or NULL for static methods
39// A2 = (managed) thread pointer
40// A3 = argument array or NULL for no argument methods
41// [SP] = JValue* result or NULL for void returns
42//
43// As the JNI call has already transitioned the thread into the
44// "running" state the remaining responsibilities of this routine are
45// to save the native register value and restore the managed thread
46// register and transfer arguments from the array into register and on
47// the stack, if needed. On return, the thread register must be
48// shuffled and the return value must be store into the result JValue.
49CompiledInvokeStub* CreateInvokeStub(bool is_static, const char* shorty, uint32_t shorty_len) {
50 UniquePtr<MipsAssembler> assembler(down_cast<MipsAssembler*>(Assembler::Create(kMips)));
51#define __ assembler->
52 size_t num_arg_array_bytes = NumArgArrayBytes(shorty, shorty_len);
53 // Size of frame = spill of R4,R9/LR + Method* + possible receiver + arg array size
54 // Note, space is left in the frame to flush arguments in registers back to out locations.
55 size_t unpadded_frame_size = (4 * kPointerSize) +
56 (is_static ? 0 : kPointerSize) +
57 num_arg_array_bytes;
58 size_t frame_size = RoundUp(unpadded_frame_size, kStackAlignment);
59
60 // Setup frame and spill S0 (rSUSPEND), S1 (rSELF), and RA
61 __ AddConstant(SP, SP, -frame_size);
62 __ StoreToOffset(kStoreWord, RA, SP, frame_size - 4);
63 __ StoreToOffset(kStoreWord, S1, SP, frame_size - 8);
64 __ StoreToOffset(kStoreWord, S0, SP, frame_size - 12);
65
66 // Move the managed thread pointer into S1.
67 __ Move(S1, A2);
68
69 // Reset S0 to suspend check interval
70 __ LoadImmediate(S0, SUSPEND_CHECK_INTERVAL);
71
72 // Can either get 3 or 2 arguments into registers
73 size_t reg_bytes = (is_static ? 3 : 2) * kPointerSize;
74 if (num_arg_array_bytes <= reg_bytes) {
75 reg_bytes = num_arg_array_bytes;
76 }
77
jeffhao07030602012-09-26 14:33:14 -070078 // Method* at bottom of frame is null thereby terminating managed stack crawls
79 __ StoreToOffset(kStoreWord, ZERO, SP, 0);
80
jeffhao7fbee072012-08-24 17:56:54 -070081 // Copy values onto the stack.
82 size_t src_offset = 0;
83 size_t dst_offset = (is_static ? 1 : 2) * kPointerSize;
84 for (size_t i = 1; i < shorty_len; ++i) {
85 switch (shorty[i]) {
86 case 'D':
87 case 'J':
88 // Move both pointers 64 bits.
89 __ LoadFromOffset(kLoadWord, T9, A3, src_offset);
90 src_offset += kPointerSize;
91 __ StoreToOffset(kStoreWord, T9, SP, dst_offset);
92 dst_offset += kPointerSize;
93
94 __ LoadFromOffset(kLoadWord, T9, A3, src_offset);
95 src_offset += kPointerSize;
96 __ StoreToOffset(kStoreWord, T9, SP, dst_offset);
97 dst_offset += kPointerSize;
98 break;
99 default:
100 // Move the source pointer sizeof(JValue) and the destination pointer 32 bits.
101 __ LoadFromOffset(kLoadWord, T9, A3, src_offset);
102 src_offset += sizeof(JValue);
103 __ StoreToOffset(kStoreWord, T9, SP, dst_offset);
104 dst_offset += kPointerSize;
105 break;
106 }
107 }
108
109 // Move all the register arguments into place.
110 dst_offset = (is_static ? 1 : 2) * kPointerSize;
111 if (is_static) {
112 if (reg_bytes > 0 && num_arg_array_bytes > 0) {
113 __ LoadFromOffset(kLoadWord, A1, SP, dst_offset + 0);
114 if (reg_bytes > 4 && num_arg_array_bytes > 4) {
115 __ LoadFromOffset(kLoadWord, A2, SP, dst_offset + 4);
116 if (reg_bytes > 8 && num_arg_array_bytes > 8) {
117 __ LoadFromOffset(kLoadWord, A3, SP, dst_offset + 8);
118 }
119 }
120 }
121 } else {
122 if (reg_bytes > 0 && num_arg_array_bytes > 0) {
123 __ LoadFromOffset(kLoadWord, A2, SP, dst_offset + 0);
124 if (reg_bytes > 4 && num_arg_array_bytes > 4) {
125 __ LoadFromOffset(kLoadWord, A3, SP, dst_offset + 4);
126 }
127 }
128 }
129
130 // Load the code pointer we are about to call.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 __ LoadFromOffset(kLoadWord, T9, A0, mirror::AbstractMethod::GetCodeOffset().Int32Value());
jeffhao7fbee072012-08-24 17:56:54 -0700132
133 // Do the call.
134 __ Jalr(T9);
135
136 // If the method returns a value, store it to the result pointer.
137 if (shorty[0] != 'V') {
138 // Load the result JValue pointer of the stub caller's out args.
jeffhao07030602012-09-26 14:33:14 -0700139 __ LoadFromOffset(kLoadWord, T9, SP, frame_size + 16);
jeffhao7fbee072012-08-24 17:56:54 -0700140 switch (shorty[0]) {
141 case 'D':
142 __ StoreDToOffset(D0, T9, 0);
143 break;
144 case 'F':
145 __ StoreFToOffset(F0, T9, 0);
146 break;
147 case 'J':
148 __ StoreToOffset(kStoreWord, V0, T9, 0);
149 __ StoreToOffset(kStoreWord, V1, T9, 4);
150 break;
151 default:
152 __ StoreToOffset(kStoreWord, V0, T9, 0);
153 }
154 }
155
156 // Restore frame and spill regs
157 __ LoadFromOffset(kLoadWord, S0, SP, frame_size - 12);
158 __ LoadFromOffset(kLoadWord, S1, SP, frame_size - 8);
159 __ LoadFromOffset(kLoadWord, RA, SP, frame_size - 4);
160 __ AddConstant(SP, SP, frame_size);
161
162 __ Jr(RA);
163
164 // TODO: store native_entry in the stub table
165 std::vector<uint8_t> code(assembler->CodeSize());
166 MemoryRegion region(&code[0], code.size());
167 assembler->FinalizeInstructions(region);
168 return new CompiledInvokeStub(kMips, code);
169#undef __
170}
171} // namespace mips
172} // namespace art
173
Ian Rogers1212a022013-03-04 10:48:41 -0800174extern "C" art::CompiledInvokeStub* ArtCreateMipsInvokeStub(art::CompilerDriver& /*compiler*/, bool is_static,
jeffhao7fbee072012-08-24 17:56:54 -0700175 const char* shorty, uint32_t shorty_len) {
176 return art::mips::CreateInvokeStub(is_static, shorty, shorty_len);
177}