blob: 9fd65c89a4aaa6d3909de6bf3900c56eb465d695 [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
jeffhao07030602012-09-26 14:33:14 -0700129 __ AddConstant(SP, SP, -48);
130 __ StoreToOffset(kStoreWord, RA, SP, 44);
jeffhao4f8f04a2012-10-02 18:10:35 -0700131 __ StoreToOffset(kStoreWord, FP, SP, 40);
132 __ StoreToOffset(kStoreWord, S7, SP, 36);
133 __ StoreToOffset(kStoreWord, S6, SP, 32);
134 __ StoreToOffset(kStoreWord, S5, SP, 28);
135 __ StoreToOffset(kStoreWord, S4, SP, 24);
136 __ StoreToOffset(kStoreWord, S3, SP, 20);
137 __ StoreToOffset(kStoreWord, S2, SP, 16);
jeffhao7fbee072012-08-24 17:56:54 -0700138
jeffhao07030602012-09-26 14:33:14 -0700139 // A0 is the Method* already
jeffhao7fbee072012-08-24 17:56:54 -0700140 __ Move(A1, S1); // Pass Thread::Current() in A1
141 __ Move(A2, SP); // Pass SP in A2
142 // Call to throw AbstractMethodError
143 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
144 __ Jr(T9); // Leaf call to routine that never returns
145
146 __ Break();
147#else // ART_USE_LLVM_COMPILER
148 // R0 is the Method* already
149 __ Move(A1, S1); // Pass Thread::Current() in A1
150 // Call to throw AbstractMethodError
151 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pThrowAbstractMethodErrorFromCode));
152 __ Jr(T9); // Leaf call to routine that never returns
153
154 __ Break();
155#endif // ART_USE_LLVM_COMPILER
156
157 assembler->EmitSlowPaths();
158
159 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700160 Thread* self = Thread::Current();
161 SirtRef<ByteArray> abstract_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700162 CHECK(abstract_stub.get() != NULL);
163 MemoryRegion code(abstract_stub->GetData(), abstract_stub->GetLength());
164 assembler->FinalizeInstructions(code);
165
166 return abstract_stub.get();
167}
168
169ByteArray* CreateJniDlsymLookupStub() {
170 UniquePtr<MipsAssembler> assembler(static_cast<MipsAssembler*>(Assembler::Create(kMips)));
171
172 // Build frame and save argument registers and RA.
173 __ AddConstant(SP, SP, -32);
174 __ StoreToOffset(kStoreWord, RA, SP, 28);
175 __ StoreToOffset(kStoreWord, A3, SP, 24);
176 __ StoreToOffset(kStoreWord, A2, SP, 20);
177 __ StoreToOffset(kStoreWord, A1, SP, 16);
178 __ StoreToOffset(kStoreWord, A0, SP, 12);
179
180 __ Move(A0, S1); // Pass Thread::Current() in A0
181 __ LoadFromOffset(kLoadWord, T9, S1, ENTRYPOINT_OFFSET(pFindNativeMethod));
182 __ Jalr(T9); // Call FindNativeMethod
183
184 // Restore frame, argument registers, and RA.
185 __ LoadFromOffset(kLoadWord, A0, SP, 12);
186 __ LoadFromOffset(kLoadWord, A1, SP, 16);
187 __ LoadFromOffset(kLoadWord, A2, SP, 20);
188 __ LoadFromOffset(kLoadWord, A3, SP, 24);
189 __ LoadFromOffset(kLoadWord, RA, SP, 28);
190 __ AddConstant(SP, SP, 32);
191
192 Label no_native_code_found;
193 __ EmitBranch(V0, ZERO, &no_native_code_found, true);
194 __ Jr(V0); // If V0 != 0 tail call method's code
195 __ Bind(&no_native_code_found, false);
196 __ Jr(RA); // Return to caller to handle exception
197
198 assembler->EmitSlowPaths();
199
200 size_t cs = assembler->CodeSize();
Ian Rogers50b35e22012-10-04 10:09:15 -0700201 Thread* self = Thread::Current();
202 SirtRef<ByteArray> jni_stub(self, ByteArray::Alloc(self, cs));
jeffhao7fbee072012-08-24 17:56:54 -0700203 CHECK(jni_stub.get() != NULL);
204 MemoryRegion code(jni_stub->GetData(), jni_stub->GetLength());
205 assembler->FinalizeInstructions(code);
206
207 return jni_stub.get();
208}
209
210} // namespace mips
211} // namespace art