blob: 9691308092728fd38bb12fc5c32128e0bf8979ef [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)
33 // | 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 | junk on call to UnresolvedDirectMethodTrampolineFromCode, holds result Method*
41 // | Method* | Callee save Method* set up by UnresolvedDirectMethodTrampolineFromCode
42 // Save callee saves and ready frame for exception delivery
43 __ AddConstant(SP, SP, -64);
44 __ StoreToOffset(kStoreWord, RA, SP, 60);
jeffhao4f8f04a2012-10-02 18:10:35 -070045 __ StoreToOffset(kStoreWord, FP, SP, 56);
46 __ StoreToOffset(kStoreWord, S7, SP, 52);
47 __ StoreToOffset(kStoreWord, S6, SP, 48);
48 __ StoreToOffset(kStoreWord, S5, SP, 44);
49 __ StoreToOffset(kStoreWord, S4, SP, 40);
50 __ StoreToOffset(kStoreWord, S3, SP, 36);
51 __ StoreToOffset(kStoreWord, S2, SP, 32);
52 __ StoreToOffset(kStoreWord, A3, SP, 28);
53 __ StoreToOffset(kStoreWord, A2, SP, 24);
54 __ StoreToOffset(kStoreWord, A1, SP, 20);
jeffhao7fbee072012-08-24 17:56:54 -070055
56 __ LoadFromOffset(kLoadWord, T9, S1,
57 ENTRYPOINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode));
58 __ Move(A2, S1); // Pass Thread::Current() in A2
59 __ LoadImmediate(A3, type); // Pass is_static
60 __ Move(A1, SP); // Pass SP for Method** callee_addr
61 __ Jalr(T9); // Call to unresolved direct method trampoline (method_idx, sp, Thread*, is_static)
62
63 // Restore registers which may have been modified by GC
jeffhao4f8f04a2012-10-02 18:10:35 -070064 __ LoadFromOffset(kLoadWord, A1, SP, 20);
65 __ LoadFromOffset(kLoadWord, A2, SP, 24);
66 __ LoadFromOffset(kLoadWord, A3, SP, 28);
67 __ LoadFromOffset(kLoadWord, S2, SP, 32);
68 __ LoadFromOffset(kLoadWord, S3, SP, 36);
69 __ LoadFromOffset(kLoadWord, S4, SP, 40);
70 __ LoadFromOffset(kLoadWord, S5, SP, 44);
71 __ LoadFromOffset(kLoadWord, S6, SP, 48);
72 __ LoadFromOffset(kLoadWord, S7, SP, 52);
73 __ LoadFromOffset(kLoadWord, FP, SP, 56);
jeffhao7fbee072012-08-24 17:56:54 -070074 __ LoadFromOffset(kLoadWord, RA, SP, 60);
75 __ AddConstant(SP, SP, 64);
76
77 __ Move(A0, V0); // Put returned Method* into A0
78 __ Jr(V0); // Leaf call to method's code
79
80 __ Break();
81#else // ART_USE_LLVM_COMPILER
82 // Build frame and save argument registers and RA.
83 __ AddConstant(SP, SP, -32);
84 __ StoreToOffset(kStoreWord, RA, SP, 28);
85 __ StoreToOffset(kStoreWord, A3, SP, 24);
86 __ StoreToOffset(kStoreWord, A2, SP, 20);
87 __ StoreToOffset(kStoreWord, A1, SP, 16);
88 __ StoreToOffset(kStoreWord, A0, SP, 12);
89
90 __ LoadFromOffset(kLoadWord, T9, S1,
91 ENTRYPOINT_OFFSET(pUnresolvedDirectMethodTrampolineFromCode));
jeffhao0ac41d52012-08-27 13:07:54 -070092 __ Move(A2, S1); // Pass Thread::Current() in A2
jeffhao7fbee072012-08-24 17:56:54 -070093 __ LoadImmediate(A3, type); // Pass is_static
jeffhao0ac41d52012-08-27 13:07:54 -070094 __ Move(A1, SP); // Pass SP for Method** callee_addr
jeffhao7fbee072012-08-24 17:56:54 -070095 __ Jalr(T9); // Call to unresolved direct method trampoline (callee, callee_addr, Thread*, is_static)
96
97 // Restore frame, argument registers, and RA.
98 __ LoadFromOffset(kLoadWord, A0, SP, 12);
99 __ LoadFromOffset(kLoadWord, A1, SP, 16);
100 __ LoadFromOffset(kLoadWord, A2, SP, 20);
101 __ LoadFromOffset(kLoadWord, A3, SP, 24);
102 __ LoadFromOffset(kLoadWord, RA, SP, 28);
103 __ AddConstant(SP, SP, 32);
104
105 Label resolve_fail;
106 __ EmitBranch(V0, ZERO, &resolve_fail, true);
107 __ Jr(V0); // If V0 != 0 tail call method's code
108 __ Bind(&resolve_fail, false);
109 __ Jr(RA); // Return to caller to handle exception
110#endif // ART_USE_LLVM_COMPILER
111
112 assembler->EmitSlowPaths();
113
114 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700115 Thread* self = Thread::Current();
116 SirtRef<ByteArray> resolution_trampoline(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700117 CHECK(resolution_trampoline.get() != NULL);
118 MemoryRegion code(resolution_trampoline->GetData(), resolution_trampoline->GetLength());
119 assembler->FinalizeInstructions(code);
120
121 return resolution_trampoline.get();
122}
123
Mathieu Chartier66f19252012-09-18 08:57:04 -0700124typedef void (*ThrowAme)(AbstractMethod*, Thread*);
jeffhao7fbee072012-08-24 17:56:54 -0700125
126ByteArray* CreateAbstractMethodErrorStub() {
127 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
128#if !defined(ART_USE_LLVM_COMPILER)
129 // Save callee saves and ready frame for exception delivery
jeffhao07030602012-09-26 14:33:14 -0700130 __ AddConstant(SP, SP, -48);
131 __ StoreToOffset(kStoreWord, RA, SP, 44);
jeffhao4f8f04a2012-10-02 18:10:35 -0700132 __ StoreToOffset(kStoreWord, FP, SP, 40);
133 __ StoreToOffset(kStoreWord, S7, SP, 36);
134 __ StoreToOffset(kStoreWord, S6, SP, 32);
135 __ StoreToOffset(kStoreWord, S5, SP, 28);
136 __ StoreToOffset(kStoreWord, S4, SP, 24);
137 __ StoreToOffset(kStoreWord, S3, SP, 20);
138 __ StoreToOffset(kStoreWord, S2, SP, 16);
jeffhao7fbee072012-08-24 17:56:54 -0700139
jeffhao07030602012-09-26 14:33:14 -0700140 // A0 is the Method* already
jeffhao7fbee072012-08-24 17:56:54 -0700141 __ Move(A1, S1); // Pass Thread::Current() in A1
142 __ Move(A2, SP); // Pass SP in A2
143 // Call to throw AbstractMethodError
144 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
145 __ Jr(T9); // Leaf call to routine that never returns
146
147 __ Break();
148#else // ART_USE_LLVM_COMPILER
149 // R0 is the Method* already
150 __ Move(A1, S1); // Pass Thread::Current() in A1
151 // Call to throw AbstractMethodError
152 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
153 __ Jr(T9); // Leaf call to routine that never returns
154
155 __ Break();
156#endif // ART_USE_LLVM_COMPILER
157
158 assembler->EmitSlowPaths();
159
160 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700161 Thread* self = Thread::Current();
162 SirtRef<ByteArray> abstract_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700163 CHECK(abstract_stub.get() != NULL);
164 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
165 assembler->FinalizeInstructions(code);
166
167 return abstract_stub.get();
168}
169
170ByteArray* CreateJniDlsymLookupStub() {
171 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
172
173 // Build frame and save argument registers and RA.
174 __ AddConstant(SP, SP, -32);
175 __ StoreToOffset(kStoreWord, RA, SP, 28);
176 __ StoreToOffset(kStoreWord, A3, SP, 24);
177 __ StoreToOffset(kStoreWord, A2, SP, 20);
178 __ StoreToOffset(kStoreWord, A1, SP, 16);
179 __ StoreToOffset(kStoreWord, A0, SP, 12);
180
181 __ Move(A0, S1); // Pass Thread::Current() in A0
182 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pFindNativeMethod));
183 __ Jalr(T9); // Call FindNativeMethod
184
185 // Restore frame, argument registers, and RA.
186 __ LoadFromOffset(kLoadWord, A0, SP, 12);
187 __ LoadFromOffset(kLoadWord, A1, SP, 16);
188 __ LoadFromOffset(kLoadWord, A2, SP, 20);
189 __ LoadFromOffset(kLoadWord, A3, SP, 24);
190 __ LoadFromOffset(kLoadWord, RA, SP, 28);
191 __ AddConstant(SP, SP, 32);
192
193 Label no_native_code_found;
194 __ EmitBranch(V0, ZERO, &no_native_code_found, true);
195 __ Jr(V0); // If V0 != 0 tail call method's code
196 __ Bind(&no_native_code_found, false);
197 __ Jr(RA); // Return to caller to handle exception
198
199 assembler->EmitSlowPaths();
200
201 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700202 Thread* self = Thread::Current();
203 SirtRef<ByteArray> jni_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700204 CHECK(jni_stub.get() != NULL);
205 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
206 assembler->FinalizeInstructions(code);
207
208 return jni_stub.get();
209}
210
211} // namespace mips
212} // namespace art