blob: c9718872ec7f1e53eae8a4aee8ccbcc54b3b7920 [file] [log] [blame]
Chris Lattner556d89d2003-08-01 22:19:03 +00001//===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00002//
Chris Lattner556d89d2003-08-01 22:19:03 +00003// FIXME: document
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00004//
5//===----------------------------------------------------------------------===//
6
Misha Brukmanf86aaa82003-06-02 04:12:39 +00007#include "llvm/Constants.h"
8#include "llvm/Function.h"
9#include "llvm/GlobalVariable.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000010#include "llvm/PassManager.h"
11#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukmana2196c12003-06-04 20:01:13 +000012#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000013#include "llvm/CodeGen/MachineFunctionInfo.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000014#include "llvm/CodeGen/MachineFunctionPass.h"
15#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000016#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000017#include "llvm/Target/TargetData.h"
Chris Lattner556d89d2003-08-01 22:19:03 +000018#include "Support/Debug.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000019#include "Support/hash_set"
Misha Brukman3de36f52003-05-27 20:07:58 +000020#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000021#include "SparcV9CodeEmitter.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000022
Brian Gaekee69f7272003-08-14 06:04:59 +000023bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000024 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000025 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000026 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000027 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000028 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000029 return false;
30}
31
Misha Brukmanf86aaa82003-06-02 04:12:39 +000032namespace {
33 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000034 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000035 MachineCodeEmitter &MCE;
36
Misha Brukman0897c602003-08-06 16:20:22 +000037 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
38 /// lazily resolved.
39 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000040 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000041
Misha Brukman0897c602003-08-06 16:20:22 +000042 /// LazyResolverMap - Keep track of the lazy resolver created for a
43 /// particular function so that we can reuse them if necessary.
44 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000045 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000046
47 public:
48 enum CallType { ShortCall, FarCall };
49
50 private:
51 /// We need to keep track of whether we used a simple call or a far call
52 /// (many instructions) in sequence. This means we need to keep track of
53 /// what type of stub we generate.
54 static std::map<uint64_t, CallType> LazyCallFlavor;
55
Misha Brukmanf86aaa82003-06-02 04:12:39 +000056 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000057 JITResolver(SparcV9CodeEmitter &V9,
58 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
59 uint64_t getLazyResolver(Function *F);
60 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000061 void deleteFunctionReference(uint64_t Address);
62 void addCallFlavor(uint64_t Address, CallType Flavor) {
63 LazyCallFlavor[Address] = Flavor;
64 }
Misha Brukmana2196c12003-06-04 20:01:13 +000065
66 // Utility functions for accessing data from static callback
67 uint64_t getCurrentPCValue() {
68 return MCE.getCurrentPCValue();
69 }
70 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
71 return SparcV9.getBinaryCodeForInstr(MI);
72 }
73
Misha Brukmanf47d9c22003-06-05 20:52:06 +000074 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
75
Misha Brukmanf86aaa82003-06-02 04:12:39 +000076 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000077 uint64_t emitStubForFunction(Function *F);
Misha Brukmanfad49292003-08-15 00:26:50 +000078 static void SaveRestoreRegisters();
79 static uint64_t CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000080 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000081
Misha Brukmanf86aaa82003-06-02 04:12:39 +000082 };
83
84 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000085 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000086}
87
88/// addFunctionReference - This method is called when we need to emit the
89/// address of a function that has not yet been emitted, so we don't know the
90/// address. Instead, we emit a call to the CompilationCallback method, and
91/// keep track of where we are.
92///
Misha Brukmana2196c12003-06-04 20:01:13 +000093uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +000094 LazyCodeGenMap[Address] = F;
Misha Brukmanfad49292003-08-15 00:26:50 +000095 return (intptr_t)&JITResolver::SaveRestoreRegisters;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000096}
97
Misha Brukman0897c602003-08-06 16:20:22 +000098/// deleteFunctionReference - If we are emitting a far call, we already added a
99/// reference to the function, but it is now incorrect, since the address to the
100/// JIT resolver is too far away to be a simple call instruction. This is used
101/// to remove the address from the map.
102///
103void JITResolver::deleteFunctionReference(uint64_t Address) {
104 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
105 assert(I != LazyCodeGenMap.end() && "Not in map!");
106 LazyCodeGenMap.erase(I);
107}
108
Misha Brukmana2196c12003-06-04 20:01:13 +0000109uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
110 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000111 assert(I != LazyCodeGenMap.end() && "Not in map!");
112 Function *F = I->second;
113 LazyCodeGenMap.erase(I);
114 return MCE.forceCompilationOf(F);
115}
116
Misha Brukmana2196c12003-06-04 20:01:13 +0000117uint64_t JITResolver::getLazyResolver(Function *F) {
118 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000119 if (I != LazyResolverMap.end() && I->first == F) return I->second;
120
121//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
122
Misha Brukmana2196c12003-06-04 20:01:13 +0000123 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000124 LazyResolverMap.insert(I, std::make_pair(F, Stub));
125 return Stub;
126}
127
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000128uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
129
Misha Brukmanfad49292003-08-15 00:26:50 +0000130 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000131 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
132 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000133
Misha Brukman0897c602003-08-06 16:20:22 +0000134 MachineInstr* BinaryCode[] = {
135 //
Misha Brukman0870e972003-08-06 22:19:18 +0000136 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000137 //
Misha Brukman0870e972003-08-06 22:19:18 +0000138 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
139 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
140 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
141 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
142 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
143 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
144 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
145 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000146 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000147 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
148 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
149 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
150 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
151 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
152 // nop ;; delay slot
153 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000154 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000155
Misha Brukman0897c602003-08-06 16:20:22 +0000156 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
157 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
158 delete BinaryCode[i];
159 Addr += 4;
160 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000161
162 return Addr;
163}
164
Misha Brukmanfad49292003-08-15 00:26:50 +0000165void JITResolver::SaveRestoreRegisters() {
166 uint32_t SingleFP[32];
167 uint64_t DoubleFP[16];
168 // FIXME: uint128_t QuadFloatRegs[..];
169 uint64_t CCR, FSR, FPRS, g1, g5;
170
171#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
172 __asm__ __volatile__ (// Save g1 and g5
173 "stx %%g1, %0;\n\t" "stx %%g5, %1;\n\t"
174 : "=m"(g1), "=m"(g5));
175
176 __asm__ __volatile__ (// Save condition-code registers
177 "stx %%fsr, %0;\n\t"
178 "rd %%fprs, %1;\n\t"
179 "rd %%ccr, %2;\n\t"
180 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
181
182 // GCC says: `asm' only allows up to thirty parameters!
183 __asm__ __volatile__ (// Save Single FP registers, part 1
184 "st %%f0, %0;\n\t" "st %%f1, %1;\n\t"
185 "st %%f2, %2;\n\t" "st %%f3, %3;\n\t"
186 "st %%f4, %4;\n\t" "st %%f5, %5;\n\t"
187 "st %%f6, %6;\n\t" "st %%f7, %7;\n\t"
188 "st %%f8, %8;\n\t" "st %%f9, %9;\n\t"
189 "st %%f10, %10;\n\t" "st %%f11, %11;\n\t"
190 "st %%f12, %12;\n\t" "st %%f13, %13;\n\t"
191 "st %%f14, %14;\n\t" "st %%f15, %15;\n\t"
192 : "=m"(SingleFP[ 0]), "=m"(SingleFP[ 1]),
193 "=m"(SingleFP[ 2]), "=m"(SingleFP[ 3]),
194 "=m"(SingleFP[ 4]), "=m"(SingleFP[ 5]),
195 "=m"(SingleFP[ 6]), "=m"(SingleFP[ 7]),
196 "=m"(SingleFP[ 8]), "=m"(SingleFP[ 9]),
197 "=m"(SingleFP[10]), "=m"(SingleFP[11]),
198 "=m"(SingleFP[12]), "=m"(SingleFP[13]),
199 "=m"(SingleFP[14]), "=m"(SingleFP[15]));
200
201 __asm__ __volatile__ (// Save Single FP registers, part 2
202 "st %%f16, %0;\n\t" "st %%f17, %1;\n\t"
203 "st %%f18, %2;\n\t" "st %%f19, %3;\n\t"
204 "st %%f20, %4;\n\t" "st %%f21, %5;\n\t"
205 "st %%f22, %6;\n\t" "st %%f23, %7;\n\t"
206 "st %%f24, %8;\n\t" "st %%f25, %9;\n\t"
207 "st %%f26, %10;\n\t" "st %%f27, %11;\n\t"
208 "st %%f28, %12;\n\t" "st %%f29, %13;\n\t"
209 "st %%f30, %14;\n\t" "st %%f31, %15;\n\t"
210 : "=m"(SingleFP[16]), "=m"(SingleFP[17]),
211 "=m"(SingleFP[18]), "=m"(SingleFP[19]),
212 "=m"(SingleFP[20]), "=m"(SingleFP[21]),
213 "=m"(SingleFP[22]), "=m"(SingleFP[23]),
214 "=m"(SingleFP[24]), "=m"(SingleFP[25]),
215 "=m"(SingleFP[26]), "=m"(SingleFP[27]),
216 "=m"(SingleFP[28]), "=m"(SingleFP[29]),
217 "=m"(SingleFP[30]), "=m"(SingleFP[31]));
218
219 __asm__ __volatile__ (// Save Double FP registers
220 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
221 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
222 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
223 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
224 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
225 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
226 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
227 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
228 : "=m"(DoubleFP[32/2-16]), "=m"(DoubleFP[34/2-16]),
229 "=m"(DoubleFP[36/2-16]), "=m"(DoubleFP[38/2-16]),
230 "=m"(DoubleFP[40/2-16]), "=m"(DoubleFP[42/2-16]),
231 "=m"(DoubleFP[44/2-16]), "=m"(DoubleFP[46/2-16]),
232 "=m"(DoubleFP[48/2-16]), "=m"(DoubleFP[50/2-16]),
233 "=m"(DoubleFP[52/2-16]), "=m"(DoubleFP[54/2-16]),
234 "=m"(DoubleFP[56/2-16]), "=m"(DoubleFP[58/2-16]),
235 "=m"(DoubleFP[60/2-16]), "=m"(DoubleFP[62/2-16]));
236#endif
237
238 // Resolve the function call
239 register uint64_t restoreAddr = CompilationCallback();
240
241#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
242 // Set the return address to re-execute the `restore' instruction
243 __asm__ __volatile__ ("or %%o0, %%g0, %%i7;\n\t"
244
245 // Restore g1 and g5
246 "ldx %0, %%g1;\n\t" "ldx %1, %%g5;\n\t"
247 :: "m"(g1), "m"(g5));
248
249 __asm__ __volatile__ (// Restore condition-code registers
250 "ldx %0, %%fsr;\n\t"
251 "wr %1, 0, %%fprs;\n\t"
252 "wr %2, 0, %%ccr;\n\t"
253 :: "m"(FSR), "r"(FPRS), "r"(CCR));
254
255 // GCC says: `asm' only allows up to thirty parameters!
256 __asm__ __volatile__ (// Restore Single FP registers, part 1
257 "ld %0, %%f0;\n\t" "ld %1, %%f1;\n\t"
258 "ld %2, %%f2;\n\t" "ld %3, %%f3;\n\t"
259 "ld %4, %%f4;\n\t" "ld %5, %%f5;\n\t"
260 "ld %6, %%f6;\n\t" "ld %7, %%f7;\n\t"
261 "ld %8, %%f8;\n\t" "ld %9, %%f9;\n\t"
262 "ld %10, %%f10;\n\t" "ld %11, %%f11;\n\t"
263 "ld %12, %%f12;\n\t" "ld %13, %%f13;\n\t"
264 "ld %14, %%f14;\n\t" "ld %15, %%f15;\n\t"
265 :: "m"(SingleFP[0]), "m"(SingleFP[1]),
266 "m"(SingleFP[2]), "m"(SingleFP[3]),
267 "m"(SingleFP[4]), "m"(SingleFP[5]),
268 "m"(SingleFP[6]), "m"(SingleFP[7]),
269 "m"(SingleFP[8]), "m"(SingleFP[9]),
270 "m"(SingleFP[10]), "m"(SingleFP[11]),
271 "m"(SingleFP[12]), "m"(SingleFP[13]),
272 "m"(SingleFP[14]), "m"(SingleFP[15]));
273
274 __asm__ __volatile__ (// Restore Single FP registers, part 2
275 "ld %0, %%f16;\n\t" "ld %1, %%f17;\n\t"
276 "ld %2, %%f18;\n\t" "ld %3, %%f19;\n\t"
277 "ld %4, %%f20;\n\t" "ld %5, %%f21;\n\t"
278 "ld %6, %%f22;\n\t" "ld %7, %%f23;\n\t"
279 "ld %8, %%f24;\n\t" "ld %9, %%f25;\n\t"
280 "ld %10, %%f26;\n\t" "ld %11, %%f27;\n\t"
281 "ld %12, %%f28;\n\t" "ld %13, %%f29;\n\t"
282 "ld %14, %%f30;\n\t" "ld %15, %%f31;\n\t"
283 :: "m"(SingleFP[16]), "m"(SingleFP[17]),
284 "m"(SingleFP[18]), "m"(SingleFP[19]),
285 "m"(SingleFP[20]), "m"(SingleFP[21]),
286 "m"(SingleFP[22]), "m"(SingleFP[23]),
287 "m"(SingleFP[24]), "m"(SingleFP[25]),
288 "m"(SingleFP[26]), "m"(SingleFP[27]),
289 "m"(SingleFP[28]), "m"(SingleFP[29]),
290 "m"(SingleFP[30]), "m"(SingleFP[31]));
291
292 __asm__ __volatile__ (// Restore Double FP registers
293 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
294 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
295 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
296 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
297 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
298 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
299 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
300 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
301 :: "m"(DoubleFP[32/2-16]), "m"(DoubleFP[34/2-16]),
302 "m"(DoubleFP[36/2-16]), "m"(DoubleFP[38/2-16]),
303 "m"(DoubleFP[40/2-16]), "m"(DoubleFP[42/2-16]),
304 "m"(DoubleFP[44/2-16]), "m"(DoubleFP[46/2-16]),
305 "m"(DoubleFP[48/2-16]), "m"(DoubleFP[50/2-16]),
306 "m"(DoubleFP[52/2-16]), "m"(DoubleFP[54/2-16]),
307 "m"(DoubleFP[56/2-16]), "m"(DoubleFP[58/2-16]),
308 "m"(DoubleFP[60/2-16]), "m"(DoubleFP[62/2-16]));
309#endif
310}
311
312uint64_t JITResolver::CompilationCallback() {
313 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000314 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000315 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000316 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000317#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000318 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
319 DEBUG(std::cerr << "Read i7 (return addr) = "
320 << std::hex << returnAddr << ", value: "
321 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000322#endif
323
Misha Brukman0897c602003-08-06 16:20:22 +0000324 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000325 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000326
Misha Brukman0897c602003-08-06 16:20:22 +0000327 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000328
Misha Brukman0897c602003-08-06 16:20:22 +0000329 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000330 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000331 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000332 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000333 uint64_t CodeBegin = CameFrom - Offset;
334
335 // Make sure that what we're about to overwrite is indeed "save"
336 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
337 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
338 delete SV;
339 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
340 assert(CodeInMem == SaveInst && "About to overwrite smthg not a save instr!");
341 DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n");
342 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000343
344 // FIXME: if the target function is close enough to fit into the 19bit disp of
345 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman0897c602003-08-06 16:20:22 +0000346#if 0
347 uint64_t InstAddr = CodeBegin;
348 // ba <target>
349 MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target);
350 *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI);
351 InstAddr += 4;
Misha Brukmana2196c12003-06-04 20:01:13 +0000352 delete MI;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000353
Misha Brukman0897c602003-08-06 16:20:22 +0000354 // nop
355 MI = BuildMI(V9::NOP, 0);
356 *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop);
357 delete MI;
358#endif
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000359
Misha Brukmanfad49292003-08-15 00:26:50 +0000360 // Change the return address to reexecute the restore, then the jump However,
361 // we can't just modify %i7 here, because we return to the function that will
362 // restore the floating-point registers for us. Thus, we just return the value
363 // we want it to be, and the parent will take care of setting %i7 correctly.
364 DEBUG(std::cerr << "Callback returning the addr of restore inst: "
Misha Brukman0897c602003-08-06 16:20:22 +0000365 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000366 return CameFrom - Offset - 12; // 8 because of call+delay, 4 more to restore
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000367}
368
369/// emitStubForFunction - This method is used by the JIT when it needs to emit
370/// the address of a function for a function whose code has not yet been
371/// generated. In order to do this, it generates a stub which jumps to the lazy
372/// function compiler, which will eventually get fixed to call the function
373/// directly.
374///
Misha Brukmana2196c12003-06-04 20:01:13 +0000375uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000376 MCE.startFunctionStub(*F, 20);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000377
Misha Brukman8f122222003-06-06 00:26:11 +0000378 DEBUG(std::cerr << "Emitting stub at addr: 0x"
379 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000380
Misha Brukman0897c602003-08-06 16:20:22 +0000381 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
382
383 // restore %g0, 0, %g0
384 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
385 .addMReg(g0, MOTy::Def);
386 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
387 delete R;
388
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000389 // save %sp, -192, %sp
390 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
391 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
392 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000393
394 int64_t CurrPC = MCE.getCurrentPCValue();
395 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
396 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman0897c602003-08-06 16:20:22 +0000397 //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
Misha Brukman0870e972003-08-06 22:19:18 +0000398 // Since this is a far call, the actual address of the call is shifted
399 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000400 deleteFunctionReference(CurrPC);
401 SparcV9.emitFarCall(Addr, F);
402#if 0
Misha Brukman0870e972003-08-06 22:19:18 +0000403 else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000404 // call CallTarget ;; invoke the callback
405 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
406 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
407 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000408
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000409 // nop ;; call delay slot
410 MachineInstr *Nop = BuildMI(V9::NOP, 0);
411 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
412 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000413
414 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000415 }
Misha Brukman0897c602003-08-06 16:20:22 +0000416#endif
Misha Brukmana2196c12003-06-04 20:01:13 +0000417
418 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000419 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000420}
421
422
Misha Brukmana2196c12003-06-04 20:01:13 +0000423SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
424 MachineCodeEmitter &M): TM(tm), MCE(M)
425{
426 TheJITResolver = new JITResolver(*this, M);
427}
428
429SparcV9CodeEmitter::~SparcV9CodeEmitter() {
430 delete TheJITResolver;
431}
432
433void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000434 // Output the constant in big endian byte order...
435 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000436 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000437 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000438 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000439 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000440}
441
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000442unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000443SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000444 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000445 const TargetRegInfo &RI = TM.getRegInfo();
446 unsigned regClass, regType = RI.getRegType(fakeReg);
447 // At least map fakeReg into its class
448 fakeReg = RI.getClassRegNum(fakeReg, regClass);
449
Misha Brukman9cedd432003-07-03 18:36:47 +0000450 switch (regClass) {
451 case UltraSparcRegInfo::IntRegClassID: {
452 // Sparc manual, p31
453 static const unsigned IntRegMap[] = {
454 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
455 8, 9, 10, 11, 12, 13, 15,
456 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
457 16, 17, 18, 19, 20, 21, 22, 23,
458 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
459 24, 25, 26, 27, 28, 29, 30, 31,
460 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
461 0, 1, 2, 3, 4, 5, 6, 7,
462 // "o6"
463 14
464 };
465
466 return IntRegMap[fakeReg];
467 break;
468 }
469 case UltraSparcRegInfo::FloatRegClassID: {
470 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000471 if (regType == UltraSparcRegInfo::FPSingleRegType) {
472 // only numbered 0-31, hence can already fit into 5 bits (and 6)
473 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
474 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
475 // FIXME: This assumes that we only have 5-bit register fiels!
476 // From Sparc Manual, page 40.
477 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
478 fakeReg |= (fakeReg >> 5) & 1;
479 fakeReg &= 0x1f;
480 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
481 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000482 return fakeReg;
483 }
484 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000485 /* xcc, icc, ccr */
486 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000487
Misha Brukmandfbfc572003-07-16 20:30:40 +0000488 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
489 && "CC register out of bounds for IntCCReg map");
490 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
491 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000492 }
493 case UltraSparcRegInfo::FloatCCRegClassID: {
494 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
495 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
496 return fakeReg;
497 }
498 default:
499 assert(0 && "Invalid unified register number in getRegType");
500 return fakeReg;
501 }
502}
503
504
Misha Brukman07d45162003-07-15 19:09:43 +0000505// WARNING: if the call used the delay slot to do meaningful work, that's not
506// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000507inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000508 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000509 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
510 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000511
Misha Brukman0897c602003-08-06 16:20:22 +0000512 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000513 //
Misha Brukman0870e972003-08-06 22:19:18 +0000514 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000515 //
Misha Brukman0870e972003-08-06 22:19:18 +0000516 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
517 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
518 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
519 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000520 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000521 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
522 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
523 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000524 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000525 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
526 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
527 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000528 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000529 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000530 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000531 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000532 };
Misha Brukman07d45162003-07-15 19:09:43 +0000533
Misha Brukman0897c602003-08-06 16:20:22 +0000534 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
535 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000536 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000537 uint64_t CurrPC = MCE.getCurrentPCValue();
538 TheJITResolver->addFunctionReference(CurrPC, F);
539 // Remember that this is a far call, to subtract appropriate offset later
540 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
541 }
Misha Brukman07d45162003-07-15 19:09:43 +0000542
Misha Brukman0897c602003-08-06 16:20:22 +0000543 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
544 delete BinaryCode[i];
545 }
Misha Brukman07d45162003-07-15 19:09:43 +0000546}
547
548
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000549int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
550 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000551 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
552 // or things that get fixed up later by the JIT.
553
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000554 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000555 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000556 abort();
557 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000558 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000559 Value *V = MO.getVRegValue();
560 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000561 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000562 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000563 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000564 } else if (const Constant *C = dyn_cast<Constant>(V)) {
565 if (ConstantMap.find(C) != ConstantMap.end()) {
566 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000567 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000568 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000569 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000570 abort();
571 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000572 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
573 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000574 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000575 // external function calls, etc.?
576 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000577 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000578 if (F->isExternal()) {
579 // Sparc backend broken: this MO should be `ExternalSymbol'
580 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
581 } else {
582 rv = (int64_t)MCE.getGlobalValueAddress(F);
583 }
584 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000585 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000586 // Function has not yet been code generated!
587 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
588 // Delayed resolution...
589 rv = TheJITResolver->getLazyResolver(F);
590 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000591 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000592 }
593 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000594 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000595 if (rv == 0) {
596 if (Constant *C = ConstantPointerRef::get(GV)) {
597 if (ConstantMap.find(C) != ConstantMap.end()) {
598 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
599 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000600 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000601 << ", " << *V << " not found in ConstantMap!\n";
602 abort();
603 }
604 }
605 }
Misha Brukman0870e972003-08-06 22:19:18 +0000606 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000607 }
608 // The real target of the call is Addr = PC + (rv * 4)
609 // So undo that: give the instruction (Addr - PC) / 4
610 if (MI.getOpcode() == V9::CALL) {
611 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000612 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000613 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000614 int64_t CallInstTarget = (rv - CurrPC) >> 2;
615 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
616 DEBUG(std::cerr << "Making far call!\n");
617 // addresss is out of bounds for the 30-bit call,
618 // make an indirect jump-and-link
619 emitFarCall(rv);
620 // this invalidates the instruction so that the call with an incorrect
621 // address will not be emitted
622 rv = 0;
623 } else {
624 // The call fits into 30 bits, so just return the corrected address
625 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000626 }
Misha Brukman8f122222003-06-06 00:26:11 +0000627 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000628 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000629 } else {
630 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
631 abort();
632 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000633 } else if (MO.isPhysicalRegister() ||
634 MO.getType() == MachineOperand::MO_CCRegister)
635 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000636 // This is necessary because the Sparc backend doesn't actually lay out
637 // registers in the real fashion -- it skips those that it chooses not to
638 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000639 unsigned fakeReg = MO.getAllocatedRegNum();
640 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
641 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000642 << realRegByClass << " (LLC: "
643 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000644 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000645 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000646 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000647 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000648 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000649 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000650 rv = (int64_t)
651 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000652 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000653 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000654 // Duplicate code of the above case for VirtualRegister, BasicBlock...
655 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000656 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000657 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000658 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000659 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000660 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000661 // Sparc backend doesn't generate this (yet...)
662 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
663 abort();
664 } else if (MO.isFrameIndex()) {
665 // Sparc backend doesn't generate this (yet...)
666 int FrameIndex = MO.getFrameIndex();
667 std::cerr << "ERROR: Frame index unhandled.\n";
668 abort();
669 } else if (MO.isConstantPoolIndex()) {
670 // Sparc backend doesn't generate this (yet...)
671 std::cerr << "ERROR: Constant Pool index unhandled.\n";
672 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000673 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000674 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000675 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000676 }
677
678 // Finally, deal with the various bitfield-extracting functions that
679 // are used in SPARC assembly. (Some of these make no sense in combination
680 // with some of the above; we'll trust that the instruction selector
681 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000682 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000683 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000684 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000685 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000686 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000687 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000688 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000689 return rv >> 42;
690 } else { // (unadorned) val
691 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000692 }
693}
694
695unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
696 Val >>= bit;
697 return (Val & 1);
698}
699
Misha Brukman3de36f52003-05-27 20:07:58 +0000700bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000701 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000702 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000703 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000704 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000705
Misha Brukmana2196c12003-06-04 20:01:13 +0000706 // The Sparc backend does not use MachineConstantPool;
707 // instead, it has its own constant pool implementation.
708 // We create a new MachineConstantPool here to be compatible with the emitter.
709 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000710 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
711 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
712 E = pool.end(); I != E; ++I)
713 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000714 Constant *C = (Constant*)*I;
715 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000716 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000717 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000718 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000719 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000720
Misha Brukman3de36f52003-05-27 20:07:58 +0000721 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
722 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000723 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000724
Misha Brukman9cedd432003-07-03 18:36:47 +0000725 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000726 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000727
728 // Resolve branches to BasicBlocks for the entire function
729 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
730 long Location = BBLocations[BBRefs[i].first];
731 unsigned *Ref = BBRefs[i].second.first;
732 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000733 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
734 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000735 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
736 MachineOperand &op = MI->getOperand(ii);
737 if (op.isPCRelativeDisp()) {
738 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000739 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000740 // Location is the target of the branch
741 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000742 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000743 // Save the flags.
744 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
745 if (op.opLoBits32()) { loBits32=true; }
746 if (op.opHiBits32()) { hiBits32=true; }
747 if (op.opLoBits64()) { loBits64=true; }
748 if (op.opHiBits64()) { hiBits64=true; }
749 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
750 branchTarget);
751 if (loBits32) { MI->setOperandLo32(ii); }
752 else if (hiBits32) { MI->setOperandHi32(ii); }
753 else if (loBits64) { MI->setOperandLo64(ii); }
754 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000755 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000756 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
757 *Ref = fixedInstr;
758 break;
759 }
760 }
761 }
762 BBRefs.clear();
763 BBLocations.clear();
764
Misha Brukman3de36f52003-05-27 20:07:58 +0000765 return false;
766}
767
768void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000769 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000770 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000771 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
772 unsigned binCode = getBinaryCodeForInstr(**I);
773 if (binCode == (1 << 30)) {
774 // this is an invalid call: the addr is out of bounds. that means a code
775 // sequence has already been emitted, and this is a no-op
776 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
777 } else {
778 emitWord(binCode);
779 }
780 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000781}
782
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000783void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
784 bool isPCRelative)
785{
786 if (isPCRelative) { // must be a call, this is a major hack!
787 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000788 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
789 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000790 // The real target of the call is Addr = PC + (target * 4)
791 // CurByte is the PC, Addr we just received
792 return (void*) (((long)Addr - (long)CurByte) >> 2);
793 } else {
794 if (Function *F = dyn_cast<Function>(V)) {
795 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000796 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000797 cast<Function>(V));
798 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000799 return
800 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000801
802 } else if (Constant *C = ConstantPointerRef::get(V)) {
803 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000804 return (void*)
805 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000806 } else {
807 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
808 << ", " << *V << " not found in ConstantMap!\n";
809 abort();
810 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000811 } else {
812 std::cerr << "Unhandled global: " << *V << "\n";
813 abort();
814 }
815 }
816 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000817 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000818 }
819}
820
Misha Brukman3de36f52003-05-27 20:07:58 +0000821#include "SparcV9CodeEmitter.inc"