blob: 9c940a18122638708cc55e055215e8cb32a91748 [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 "jni_internal.h"
18#include "oat/runtime/oat_support_entrypoints.h"
19#include "oat/runtime/stub.h"
20#include "oat/utils/mips/assembler_mips.h"
21#include "object.h"
22#include "stack_indirect_reference_table.h"
Ian Rogers1f539342012-10-03 21:09:42 -070023#include "sirt_ref.h"
jeffhao7fbee072012-08-24 17:56:54 -070024
25#define __ assembler->
26
27namespace art {
28namespace mips {
29
30ByteArray* MipsCreateResolutionTrampoline(Runtime::TrampolineType type) {
31 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
32#if !defined(ART_USE_LLVM_COMPILER)
jeffhaofa147e22012-10-12 17:03:32 -070033 // | Out args |
34 // | Method* | <- SP on entry
35 // | RA | return address into caller
36 // | ... | callee saves
37 // | A3 | possible argument
38 // | A2 | possible argument
39 // | A1 | possible argument
40 // | A0/Method* | Callee save Method* set up by UnresolvedDirectMethodTrampolineFromCode
jeffhao7fbee072012-08-24 17:56:54 -070041 // Save callee saves and ready frame for exception delivery
jeffhaofa147e22012-10-12 17:03:32 -070042 __ AddConstant(SP, SP, -48);
43 __ StoreToOffset(kStoreWord, RA, SP, 44);
44 __ StoreToOffset(kStoreWord, FP, SP, 40);
45 __ StoreToOffset(kStoreWord, S7, SP, 36);
46 __ StoreToOffset(kStoreWord, S6, SP, 32);
47 __ StoreToOffset(kStoreWord, S5, SP, 28);
48 __ StoreToOffset(kStoreWord, S4, SP, 24);
49 __ StoreToOffset(kStoreWord, S3, SP, 20);
50 __ StoreToOffset(kStoreWord, S2, SP, 16);
51 __ StoreToOffset(kStoreWord, A3, SP, 12);
52 __ StoreToOffset(kStoreWord, A2, SP, 8);
53 __ StoreToOffset(kStoreWord, A1, SP, 4);
jeffhao7fbee072012-08-24 17:56:54 -070054
55 __ LoadFromOffset(kLoadWord, T9, S1,
56 ENTRYPOINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode));
57 __ Move(A2, S1); // Pass Thread::Current() in A2
58 __ LoadImmediate(A3, type); // Pass is_static
59 __ Move(A1, SP); // Pass SP for Method** callee_addr
60 __ Jalr(T9); // Call to unresolved direct method trampoline (method_idx, sp, Thread*, is_static)
61
62 // Restore registers which may have been modified by GC
jeffhaofa147e22012-10-12 17:03:32 -070063 __ LoadFromOffset(kLoadWord, A0, SP, 0);
64 __ LoadFromOffset(kLoadWord, A1, SP, 4);
65 __ LoadFromOffset(kLoadWord, A2, SP, 8);
66 __ LoadFromOffset(kLoadWord, A3, SP, 12);
67 __ LoadFromOffset(kLoadWord, S2, SP, 16);
68 __ LoadFromOffset(kLoadWord, S3, SP, 20);
69 __ LoadFromOffset(kLoadWord, S4, SP, 24);
70 __ LoadFromOffset(kLoadWord, S5, SP, 28);
71 __ LoadFromOffset(kLoadWord, S6, SP, 32);
72 __ LoadFromOffset(kLoadWord, S7, SP, 36);
73 __ LoadFromOffset(kLoadWord, FP, SP, 40);
74 __ LoadFromOffset(kLoadWord, RA, SP, 44);
75 __ AddConstant(SP, SP, 48);
jeffhao7fbee072012-08-24 17:56:54 -070076
jeffhao7fbee072012-08-24 17:56:54 -070077 __ Jr(V0); // Leaf call to method's code
78
79 __ Break();
80#else // ART_USE_LLVM_COMPILER
81 // Build frame and save argument registers and RA.
82 __ AddConstant(SP, SP, -32);
83 __ StoreToOffset(kStoreWord, RA, SP, 28);
84 __ StoreToOffset(kStoreWord, A3, SP, 24);
85 __ StoreToOffset(kStoreWord, A2, SP, 20);
86 __ StoreToOffset(kStoreWord, A1, SP, 16);
87 __ StoreToOffset(kStoreWord, A0, SP, 12);
88
89 __ LoadFromOffset(kLoadWord, T9, S1,
90 ENTRYPOINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode));
jeffhao0ac41d52012-08-27 13:07:54 -070091 __ Move(A2, S1); // Pass Thread::Current() in A2
jeffhao7fbee072012-08-24 17:56:54 -070092 __ LoadImmediate(A3, type); // Pass is_static
jeffhao0ac41d52012-08-27 13:07:54 -070093 __ Move(A1, SP); // Pass SP for Method** callee_addr
jeffhao7fbee072012-08-24 17:56:54 -070094 __ Jalr(T9); // Call to unresolved direct method trampoline (callee, callee_addr, Thread*, is_static)
95
96 // Restore frame, argument registers, and RA.
97 __ LoadFromOffset(kLoadWord, A0, SP, 12);
98 __ LoadFromOffset(kLoadWord, A1, SP, 16);
99 __ LoadFromOffset(kLoadWord, A2, SP, 20);
100 __ LoadFromOffset(kLoadWord, A3, SP, 24);
101 __ LoadFromOffset(kLoadWord, RA, SP, 28);
102 __ AddConstant(SP, SP, 32);
103
104 Label resolve_fail;
105 __ EmitBranch(V0, ZERO, &resolve_fail, true);
106 __ Jr(V0); // If V0 != 0 tail call method's code
107 __ Bind(&resolve_fail, false);
108 __ Jr(RA); // Return to caller to handle exception
109#endif // ART_USE_LLVM_COMPILER
110
111 assembler->EmitSlowPaths();
112
113 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700114 Thread* self = Thread::Current();
115 SirtRef<ByteArray> resolution_trampoline(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700116 CHECK(resolution_trampoline.get() != NULL);
117 MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength());
118 assembler->FinalizeInstructions(code);
119
120 return resolution_trampoline.get();
121}
122
Mathieu Chartier66f19252012-09-18 08:57:04 -0700123typedef void (*ThrowAme)(AbstractMethod*, Thread*);
jeffhao7fbee072012-08-24 17:56:54 -0700124
125ByteArray* CreateAbstractMethodErrorStub() {
126 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
127#if !defined(ART_USE_LLVM_COMPILER)
128 // Save callee saves and ready frame for exception delivery
jeffhao4eb68ed2012-10-17 16:41:07 -0700129 __ AddConstant(SP, SP, -64);
130 __ StoreToOffset(kStoreWord, RA, SP, 60);
131 __ StoreToOffset(kStoreWord, FP, SP, 56);
132 __ StoreToOffset(kStoreWord, S7, SP, 52);
133 __ StoreToOffset(kStoreWord, S6, SP, 48);
134 __ StoreToOffset(kStoreWord, S5, SP, 44);
135 __ StoreToOffset(kStoreWord, S4, SP, 40);
136 __ StoreToOffset(kStoreWord, S3, SP, 36);
137 __ StoreToOffset(kStoreWord, S2, SP, 32);
138 __ StoreToOffset(kStoreWord, S1, SP, 28);
139 __ StoreToOffset(kStoreWord, S0, SP, 24);
jeffhao7fbee072012-08-24 17:56:54 -0700140
jeffhao07030602012-09-26 14:33:14 -0700141 // A0 is the Method* already
jeffhao7fbee072012-08-24 17:56:54 -0700142 __ Move(A1, S1); // Pass Thread::Current() in A1
143 __ Move(A2, SP); // Pass SP in A2
144 // Call to throw AbstractMethodError
145 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
146 __ Jr(T9); // Leaf call to routine that never returns
147
148 __ Break();
149#else // ART_USE_LLVM_COMPILER
150 // R0 is the Method* already
151 __ Move(A1, S1); // Pass Thread::Current() in A1
152 // Call to throw AbstractMethodError
153 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
154 __ Jr(T9); // Leaf call to routine that never returns
155
156 __ Break();
157#endif // ART_USE_LLVM_COMPILER
158
159 assembler->EmitSlowPaths();
160
161 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700162 Thread* self = Thread::Current();
163 SirtRef<ByteArray> abstract_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700164 CHECK(abstract_stub.get() != NULL);
165 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
166 assembler->FinalizeInstructions(code);
167
168 return abstract_stub.get();
169}
170
171ByteArray* CreateJniDlsymLookupStub() {
172 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
173
174 // Build frame and save argument registers and RA.
175 __ AddConstant(SP, SP, -32);
176 __ StoreToOffset(kStoreWord, RA, SP, 28);
177 __ StoreToOffset(kStoreWord, A3, SP, 24);
178 __ StoreToOffset(kStoreWord, A2, SP, 20);
179 __ StoreToOffset(kStoreWord, A1, SP, 16);
180 __ StoreToOffset(kStoreWord, A0, SP, 12);
181
182 __ Move(A0, S1); // Pass Thread::Current() in A0
183 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pFindNativeMethod));
184 __ Jalr(T9); // Call FindNativeMethod
185
186 // Restore frame, argument registers, and RA.
187 __ LoadFromOffset(kLoadWord, A0, SP, 12);
188 __ LoadFromOffset(kLoadWord, A1, SP, 16);
189 __ LoadFromOffset(kLoadWord, A2, SP, 20);
190 __ LoadFromOffset(kLoadWord, A3, SP, 24);
191 __ LoadFromOffset(kLoadWord, RA, SP, 28);
192 __ AddConstant(SP, SP, 32);
193
194 Label no_native_code_found;
195 __ EmitBranch(V0, ZERO, &no_native_code_found, true);
196 __ Jr(V0); // If V0 != 0 tail call method's code
197 __ Bind(&no_native_code_found, false);
198 __ Jr(RA); // Return to caller to handle exception
199
200 assembler->EmitSlowPaths();
201
202 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700203 Thread* self = Thread::Current();
204 SirtRef<ByteArray> jni_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700205 CHECK(jni_stub.get() != NULL);
206 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
207 assembler->FinalizeInstructions(code);
208
209 return jni_stub.get();
210}
211
212} // namespace mips
213} // namespace art