blob: 06ae46934d51cac67af28f422959a218a4888ac5 [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 Brukman103f0c32003-09-05 22:59:31 +000020#include "Support/Statistic.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000021#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000022#include "SparcV9CodeEmitter.h"
Misha Brukmancfd67c92003-08-29 04:22:54 +000023#include "Config/alloca.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000024
Misha Brukman103f0c32003-09-05 22:59:31 +000025namespace {
26 Statistic<> OverwrittenCalls("call-ovwr", "Number of over-written calls");
27 Statistic<> UnmodifiedCalls("call-skip", "Number of unmodified calls");
28 Statistic<> CallbackCalls("callback", "Number CompilationCallback() calls");
29}
30
Brian Gaekee69f7272003-08-14 06:04:59 +000031bool UltraSparc::addPassesToEmitMachineCode(FunctionPassManager &PM,
Misha Brukman3de36f52003-05-27 20:07:58 +000032 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000033 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000034 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000035 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000036 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000037 return false;
38}
39
Misha Brukmanf86aaa82003-06-02 04:12:39 +000040namespace {
41 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000042 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000043 MachineCodeEmitter &MCE;
44
Misha Brukman0897c602003-08-06 16:20:22 +000045 /// LazyCodeGenMap - Keep track of call sites for functions that are to be
46 /// lazily resolved.
47 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000048 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000049
Misha Brukman0897c602003-08-06 16:20:22 +000050 /// LazyResolverMap - Keep track of the lazy resolver created for a
51 /// particular function so that we can reuse them if necessary.
52 ///
Misha Brukmana2196c12003-06-04 20:01:13 +000053 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukman0897c602003-08-06 16:20:22 +000054
55 public:
56 enum CallType { ShortCall, FarCall };
57
58 private:
59 /// We need to keep track of whether we used a simple call or a far call
60 /// (many instructions) in sequence. This means we need to keep track of
61 /// what type of stub we generate.
62 static std::map<uint64_t, CallType> LazyCallFlavor;
63
Misha Brukmanf86aaa82003-06-02 04:12:39 +000064 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000065 JITResolver(SparcV9CodeEmitter &V9,
66 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
67 uint64_t getLazyResolver(Function *F);
68 uint64_t addFunctionReference(uint64_t Address, Function *F);
Misha Brukman0897c602003-08-06 16:20:22 +000069 void deleteFunctionReference(uint64_t Address);
70 void addCallFlavor(uint64_t Address, CallType Flavor) {
71 LazyCallFlavor[Address] = Flavor;
72 }
Misha Brukmana2196c12003-06-04 20:01:13 +000073
74 // Utility functions for accessing data from static callback
75 uint64_t getCurrentPCValue() {
76 return MCE.getCurrentPCValue();
77 }
78 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
79 return SparcV9.getBinaryCodeForInstr(MI);
80 }
81
Misha Brukmanf47d9c22003-06-05 20:52:06 +000082 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
83
Misha Brukmanf86aaa82003-06-02 04:12:39 +000084 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000085 uint64_t emitStubForFunction(Function *F);
Misha Brukman103f0c32003-09-05 22:59:31 +000086 static void SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
87 uint64_t &FPRS, uint64_t &CCR);
88 static void RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
89 uint64_t &FPRS, uint64_t &CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +000090 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000091 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000092
Misha Brukmanf86aaa82003-06-02 04:12:39 +000093 };
94
95 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000096 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000097}
98
99/// addFunctionReference - This method is called when we need to emit the
100/// address of a function that has not yet been emitted, so we don't know the
101/// address. Instead, we emit a call to the CompilationCallback method, and
102/// keep track of where we are.
103///
Misha Brukmana2196c12003-06-04 20:01:13 +0000104uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000105 LazyCodeGenMap[Address] = F;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000106 return (intptr_t)&JITResolver::CompilationCallback;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000107}
108
Misha Brukman0897c602003-08-06 16:20:22 +0000109/// deleteFunctionReference - If we are emitting a far call, we already added a
110/// reference to the function, but it is now incorrect, since the address to the
111/// JIT resolver is too far away to be a simple call instruction. This is used
112/// to remove the address from the map.
113///
114void JITResolver::deleteFunctionReference(uint64_t Address) {
115 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
116 assert(I != LazyCodeGenMap.end() && "Not in map!");
117 LazyCodeGenMap.erase(I);
118}
119
Misha Brukmana2196c12003-06-04 20:01:13 +0000120uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
121 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000122 assert(I != LazyCodeGenMap.end() && "Not in map!");
123 Function *F = I->second;
124 LazyCodeGenMap.erase(I);
125 return MCE.forceCompilationOf(F);
126}
127
Misha Brukmana2196c12003-06-04 20:01:13 +0000128uint64_t JITResolver::getLazyResolver(Function *F) {
129 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000130 if (I != LazyResolverMap.end() && I->first == F) return I->second;
131
132//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
133
Misha Brukmana2196c12003-06-04 20:01:13 +0000134 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000135 LazyResolverMap.insert(I, std::make_pair(F, Stub));
136 return Stub;
137}
138
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000139uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
140
Misha Brukmanfad49292003-08-15 00:26:50 +0000141 static const unsigned
Misha Brukman0870e972003-08-06 22:19:18 +0000142 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
143 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000144
Misha Brukman0897c602003-08-06 16:20:22 +0000145 MachineInstr* BinaryCode[] = {
146 //
Misha Brukman0870e972003-08-06 22:19:18 +0000147 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000148 //
Misha Brukman0870e972003-08-06 22:19:18 +0000149 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
150 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
151 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
152 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
153 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
154 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
155 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
156 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000157 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000158 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
159 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
160 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
161 // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
162 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
163 // nop ;; delay slot
164 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000165 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000166
Misha Brukman0897c602003-08-06 16:20:22 +0000167 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
168 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
169 delete BinaryCode[i];
170 Addr += 4;
171 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000172
173 return Addr;
174}
175
Misha Brukman103f0c32003-09-05 22:59:31 +0000176void JITResolver::SaveRegisters(uint64_t DoubleFP[], uint64_t &FSR,
177 uint64_t &FPRS, uint64_t &CCR) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000178#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000179
Misha Brukmancfd67c92003-08-29 04:22:54 +0000180#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000181 __asm__ __volatile__ (// Save condition-code registers
182 "stx %%fsr, %0;\n\t"
183 "rd %%fprs, %1;\n\t"
184 "rd %%ccr, %2;\n\t"
185 : "=m"(FSR), "=r"(FPRS), "=r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000186#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000187
188 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000189 __asm__ __volatile__ (// Save Single/Double FP registers, part 1
190 "std %%f0, %0;\n\t" "std %%f2, %1;\n\t"
191 "std %%f4, %2;\n\t" "std %%f6, %3;\n\t"
192 "std %%f8, %4;\n\t" "std %%f10, %5;\n\t"
193 "std %%f12, %6;\n\t" "std %%f14, %7;\n\t"
194 "std %%f16, %8;\n\t" "std %%f18, %9;\n\t"
195 "std %%f20, %10;\n\t" "std %%f22, %11;\n\t"
196 "std %%f24, %12;\n\t" "std %%f26, %13;\n\t"
197 "std %%f28, %14;\n\t" "std %%f30, %15;\n\t"
198 : "=m"(DoubleFP[ 0]), "=m"(DoubleFP[ 1]),
199 "=m"(DoubleFP[ 2]), "=m"(DoubleFP[ 3]),
200 "=m"(DoubleFP[ 4]), "=m"(DoubleFP[ 5]),
201 "=m"(DoubleFP[ 6]), "=m"(DoubleFP[ 7]),
202 "=m"(DoubleFP[ 8]), "=m"(DoubleFP[ 9]),
203 "=m"(DoubleFP[10]), "=m"(DoubleFP[11]),
204 "=m"(DoubleFP[12]), "=m"(DoubleFP[13]),
205 "=m"(DoubleFP[14]), "=m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000206
Misha Brukmancfd67c92003-08-29 04:22:54 +0000207 __asm__ __volatile__ (// Save Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000208 "std %%f32, %0;\n\t" "std %%f34, %1;\n\t"
Misha Brukman15d1d572003-08-15 16:15:28 +0000209 "std %%f36, %2;\n\t" "std %%f38, %3;\n\t"
Misha Brukmanfad49292003-08-15 00:26:50 +0000210 "std %%f40, %4;\n\t" "std %%f42, %5;\n\t"
211 "std %%f44, %6;\n\t" "std %%f46, %7;\n\t"
212 "std %%f48, %8;\n\t" "std %%f50, %9;\n\t"
213 "std %%f52, %10;\n\t" "std %%f54, %11;\n\t"
214 "std %%f56, %12;\n\t" "std %%f58, %13;\n\t"
215 "std %%f60, %14;\n\t" "std %%f62, %15;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000216 : "=m"(DoubleFP[16]), "=m"(DoubleFP[17]),
217 "=m"(DoubleFP[18]), "=m"(DoubleFP[19]),
218 "=m"(DoubleFP[20]), "=m"(DoubleFP[21]),
219 "=m"(DoubleFP[22]), "=m"(DoubleFP[23]),
220 "=m"(DoubleFP[24]), "=m"(DoubleFP[25]),
221 "=m"(DoubleFP[26]), "=m"(DoubleFP[27]),
222 "=m"(DoubleFP[28]), "=m"(DoubleFP[29]),
223 "=m"(DoubleFP[30]), "=m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000224#endif
Misha Brukmancfd67c92003-08-29 04:22:54 +0000225}
Misha Brukmanfad49292003-08-15 00:26:50 +0000226
Misha Brukmanfad49292003-08-15 00:26:50 +0000227
Misha Brukman103f0c32003-09-05 22:59:31 +0000228void JITResolver::RestoreRegisters(uint64_t DoubleFP[], uint64_t &FSR,
229 uint64_t &FPRS, uint64_t &CCR)
230{
Misha Brukmanfad49292003-08-15 00:26:50 +0000231#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukmanfad49292003-08-15 00:26:50 +0000232
Misha Brukmancfd67c92003-08-29 04:22:54 +0000233#if 0
Misha Brukmanfad49292003-08-15 00:26:50 +0000234 __asm__ __volatile__ (// Restore condition-code registers
235 "ldx %0, %%fsr;\n\t"
236 "wr %1, 0, %%fprs;\n\t"
237 "wr %2, 0, %%ccr;\n\t"
238 :: "m"(FSR), "r"(FPRS), "r"(CCR));
Misha Brukmancfd67c92003-08-29 04:22:54 +0000239#endif
Misha Brukmanfad49292003-08-15 00:26:50 +0000240
241 // GCC says: `asm' only allows up to thirty parameters!
Misha Brukmancfd67c92003-08-29 04:22:54 +0000242 __asm__ __volatile__ (// Restore Single/Double FP registers, part 1
243 "ldd %0, %%f0;\n\t" "ldd %1, %%f2;\n\t"
244 "ldd %2, %%f4;\n\t" "ldd %3, %%f6;\n\t"
245 "ldd %4, %%f8;\n\t" "ldd %5, %%f10;\n\t"
246 "ldd %6, %%f12;\n\t" "ldd %7, %%f14;\n\t"
247 "ldd %8, %%f16;\n\t" "ldd %9, %%f18;\n\t"
248 "ldd %10, %%f20;\n\t" "ldd %11, %%f22;\n\t"
249 "ldd %12, %%f24;\n\t" "ldd %13, %%f26;\n\t"
250 "ldd %14, %%f28;\n\t" "ldd %15, %%f30;\n\t"
251 :: "m"(DoubleFP[0]), "m"(DoubleFP[1]),
252 "m"(DoubleFP[2]), "m"(DoubleFP[3]),
253 "m"(DoubleFP[4]), "m"(DoubleFP[5]),
254 "m"(DoubleFP[6]), "m"(DoubleFP[7]),
255 "m"(DoubleFP[8]), "m"(DoubleFP[9]),
256 "m"(DoubleFP[10]), "m"(DoubleFP[11]),
257 "m"(DoubleFP[12]), "m"(DoubleFP[13]),
258 "m"(DoubleFP[14]), "m"(DoubleFP[15]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000259
Misha Brukmancfd67c92003-08-29 04:22:54 +0000260 __asm__ __volatile__ (// Restore Double FP registers, part 2
Misha Brukmanfad49292003-08-15 00:26:50 +0000261 "ldd %0, %%f32;\n\t" "ldd %1, %%f34;\n\t"
262 "ldd %2, %%f36;\n\t" "ldd %3, %%f38;\n\t"
263 "ldd %4, %%f40;\n\t" "ldd %5, %%f42;\n\t"
264 "ldd %6, %%f44;\n\t" "ldd %7, %%f46;\n\t"
265 "ldd %8, %%f48;\n\t" "ldd %9, %%f50;\n\t"
266 "ldd %10, %%f52;\n\t" "ldd %11, %%f54;\n\t"
267 "ldd %12, %%f56;\n\t" "ldd %13, %%f58;\n\t"
268 "ldd %14, %%f60;\n\t" "ldd %15, %%f62;\n\t"
Misha Brukmancfd67c92003-08-29 04:22:54 +0000269 :: "m"(DoubleFP[16]), "m"(DoubleFP[17]),
270 "m"(DoubleFP[18]), "m"(DoubleFP[19]),
271 "m"(DoubleFP[20]), "m"(DoubleFP[21]),
272 "m"(DoubleFP[22]), "m"(DoubleFP[23]),
273 "m"(DoubleFP[24]), "m"(DoubleFP[25]),
274 "m"(DoubleFP[26]), "m"(DoubleFP[27]),
275 "m"(DoubleFP[28]), "m"(DoubleFP[29]),
276 "m"(DoubleFP[30]), "m"(DoubleFP[31]));
Misha Brukmanfad49292003-08-15 00:26:50 +0000277#endif
278}
279
Misha Brukmancfd67c92003-08-29 04:22:54 +0000280void JITResolver::CompilationCallback() {
281 // Local space to save double registers
282 uint64_t DoubleFP[32];
Misha Brukman103f0c32003-09-05 22:59:31 +0000283 uint64_t FSR, FPRS, CCR;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000284
Misha Brukman103f0c32003-09-05 22:59:31 +0000285 SaveRegisters(DoubleFP, FSR, FPRS, CCR);
286 ++CallbackCalls;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000287
288 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
Misha Brukman103f0c32003-09-05 22:59:31 +0000289 uint64_t CameFrom1 = (uint64_t)(intptr_t)__builtin_return_address(1);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000290 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000291 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanfad49292003-08-15 00:26:50 +0000292 register int64_t returnAddr = 0;
Misha Brukman0897c602003-08-06 16:20:22 +0000293#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000294 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
295 DEBUG(std::cerr << "Read i7 (return addr) = "
296 << std::hex << returnAddr << ", value: "
297 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000298#endif
299
Misha Brukman103f0c32003-09-05 22:59:31 +0000300 // If we can rewrite the ORIGINAL caller, we eliminate the whole need for a
301 // trampoline function stub!!
302 unsigned OrigCallInst = *((unsigned*)(intptr_t)CameFrom1);
303 int64_t OrigTarget = (Target-CameFrom1) >> 2;
304 if ((OrigCallInst & (1 << 30)) &&
305 (OrigTarget <= (1 << 30) && OrigTarget >= -(1 << 30)))
306 {
307 // The original call instruction was CALL <immed>, which means we can
308 // overwrite it directly, since the offset will fit into 30 bits
309 MachineInstr *C = BuildMI(V9::CALL, 1).addSImm(OrigTarget);
310 *((unsigned*)(intptr_t)CameFrom1)=TheJITResolver->getBinaryCodeForInstr(*C);
311 delete C;
312 ++OverwrittenCalls;
313 } else {
314 ++UnmodifiedCalls;
315 }
316
Misha Brukman0897c602003-08-06 16:20:22 +0000317 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000318 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000319
Misha Brukman0897c602003-08-06 16:20:22 +0000320 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000321
Misha Brukman0897c602003-08-06 16:20:22 +0000322 // Subtract enough to overwrite up to the 'save' instruction
Misha Brukman0870e972003-08-06 22:19:18 +0000323 // This depends on whether we made a short call (1 instruction) or the
Misha Brukmanfad49292003-08-15 00:26:50 +0000324 // farCall (7 instructions)
Misha Brukman0870e972003-08-06 22:19:18 +0000325 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
Misha Brukman0897c602003-08-06 16:20:22 +0000326 uint64_t CodeBegin = CameFrom - Offset;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000327
328 // FIXME FIXME FIXME FIXME: __builtin_frame_address doesn't work if frame
329 // pointer elimination has been performed. Having a variable sized alloca
Misha Brukman103f0c32003-09-05 22:59:31 +0000330 // disables frame pointer elimination currently, even if it's dead. This is
331 // a gross hack.
Misha Brukmancfd67c92003-08-29 04:22:54 +0000332 alloca(42+Offset);
333 // FIXME FIXME FIXME FIXME
Misha Brukman0897c602003-08-06 16:20:22 +0000334
335 // Make sure that what we're about to overwrite is indeed "save"
Misha Brukman103f0c32003-09-05 22:59:31 +0000336 MachineInstr *SV =BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
Misha Brukman0897c602003-08-06 16:20:22 +0000337 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
338 delete SV;
339 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
Misha Brukmancfd67c92003-08-29 04:22:54 +0000340 if (CodeInMem != SaveInst) {
341 std::cerr << "About to overwrite smthg not a save instr!";
342 abort();
343 }
Misha Brukman103f0c32003-09-05 22:59:31 +0000344 DEBUG(std::cerr << "Emitting a jump to 0x" << std::hex << Target << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000345
Misha Brukman103f0c32003-09-05 22:59:31 +0000346 // If the target function is close enough to fit into the 19bit disp of
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000347 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman103f0c32003-09-05 22:59:31 +0000348 int64_t BranchTarget = (Target-CodeBegin) >> 2;
349 if (BranchTarget >= (1 << 19) || BranchTarget <= -(1 << 19)) {
350 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
351 } else {
352 // ba <target>
353 MachineInstr *I = BuildMI(V9::BA, 1).addSImm(BranchTarget);
354 *((unsigned*)(intptr_t)CodeBegin) =
355 TheJITResolver->getBinaryCodeForInstr(*I);
356 CodeBegin += 4;
357 delete I;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000358
Misha Brukman103f0c32003-09-05 22:59:31 +0000359 // nop
360 I = BuildMI(V9::NOP, 0);
361 *((unsigned*)(intptr_t)CodeBegin) =
362 TheJITResolver->getBinaryCodeForInstr(*I);
363 delete I;
364 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000365
Misha Brukman103f0c32003-09-05 22:59:31 +0000366 RestoreRegisters(DoubleFP, FSR, FPRS, CCR);
Misha Brukmancfd67c92003-08-29 04:22:54 +0000367
Misha Brukman103f0c32003-09-05 22:59:31 +0000368 // Change the return address to reexecute the restore, then the jump. However,
Misha Brukmanfad49292003-08-15 00:26:50 +0000369 // we can't just modify %i7 here, because we return to the function that will
370 // restore the floating-point registers for us. Thus, we just return the value
371 // we want it to be, and the parent will take care of setting %i7 correctly.
Misha Brukman103f0c32003-09-05 22:59:31 +0000372 DEBUG(std::cerr << "Callback returning to: 0x"
Misha Brukman0897c602003-08-06 16:20:22 +0000373 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmancfd67c92003-08-29 04:22:54 +0000374#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
375 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
376#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000377}
378
379/// emitStubForFunction - This method is used by the JIT when it needs to emit
380/// the address of a function for a function whose code has not yet been
381/// generated. In order to do this, it generates a stub which jumps to the lazy
382/// function compiler, which will eventually get fixed to call the function
383/// directly.
384///
Misha Brukmana2196c12003-06-04 20:01:13 +0000385uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmancfd67c92003-08-29 04:22:54 +0000386 MCE.startFunctionStub(*F, 44);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000387
Misha Brukman8f122222003-06-06 00:26:11 +0000388 DEBUG(std::cerr << "Emitting stub at addr: 0x"
389 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000390
Misha Brukman0897c602003-08-06 16:20:22 +0000391 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
392
393 // restore %g0, 0, %g0
394 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
395 .addMReg(g0, MOTy::Def);
396 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
397 delete R;
398
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000399 // save %sp, -192, %sp
400 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
401 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
402 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000403
404 int64_t CurrPC = MCE.getCurrentPCValue();
405 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
406 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman103f0c32003-09-05 22:59:31 +0000407 if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
408 // Since this is a far call, the actual address of the call is shifted
409 // by the number of instructions it takes to calculate the exact address
Misha Brukman0897c602003-08-06 16:20:22 +0000410 deleteFunctionReference(CurrPC);
411 SparcV9.emitFarCall(Addr, F);
Misha Brukman103f0c32003-09-05 22:59:31 +0000412 } else {
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000413 // call CallTarget ;; invoke the callback
414 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
415 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
416 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000417
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000418 // nop ;; call delay slot
419 MachineInstr *Nop = BuildMI(V9::NOP, 0);
420 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
421 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000422
423 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000424 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000425
426 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000427 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000428}
429
430
Misha Brukmana2196c12003-06-04 20:01:13 +0000431SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
432 MachineCodeEmitter &M): TM(tm), MCE(M)
433{
434 TheJITResolver = new JITResolver(*this, M);
435}
436
437SparcV9CodeEmitter::~SparcV9CodeEmitter() {
438 delete TheJITResolver;
439}
440
441void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000442 // Output the constant in big endian byte order...
443 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000444 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000445 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000446 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000447 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000448}
449
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000450unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000451SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukmanfad49292003-08-15 00:26:50 +0000452 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000453 const TargetRegInfo &RI = TM.getRegInfo();
454 unsigned regClass, regType = RI.getRegType(fakeReg);
455 // At least map fakeReg into its class
456 fakeReg = RI.getClassRegNum(fakeReg, regClass);
457
Misha Brukman9cedd432003-07-03 18:36:47 +0000458 switch (regClass) {
459 case UltraSparcRegInfo::IntRegClassID: {
460 // Sparc manual, p31
461 static const unsigned IntRegMap[] = {
462 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
463 8, 9, 10, 11, 12, 13, 15,
464 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
465 16, 17, 18, 19, 20, 21, 22, 23,
466 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
467 24, 25, 26, 27, 28, 29, 30, 31,
468 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
469 0, 1, 2, 3, 4, 5, 6, 7,
470 // "o6"
471 14
472 };
473
474 return IntRegMap[fakeReg];
475 break;
476 }
477 case UltraSparcRegInfo::FloatRegClassID: {
478 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000479 if (regType == UltraSparcRegInfo::FPSingleRegType) {
480 // only numbered 0-31, hence can already fit into 5 bits (and 6)
481 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
482 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
483 // FIXME: This assumes that we only have 5-bit register fiels!
484 // From Sparc Manual, page 40.
485 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
486 fakeReg |= (fakeReg >> 5) & 1;
487 fakeReg &= 0x1f;
488 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
489 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000490 return fakeReg;
491 }
492 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000493 /* xcc, icc, ccr */
494 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000495
Misha Brukmandfbfc572003-07-16 20:30:40 +0000496 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
497 && "CC register out of bounds for IntCCReg map");
498 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
499 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000500 }
501 case UltraSparcRegInfo::FloatCCRegClassID: {
502 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
503 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
504 return fakeReg;
505 }
506 default:
507 assert(0 && "Invalid unified register number in getRegType");
508 return fakeReg;
509 }
510}
511
512
Misha Brukman07d45162003-07-15 19:09:43 +0000513// WARNING: if the call used the delay slot to do meaningful work, that's not
514// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000515inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukmanfad49292003-08-15 00:26:50 +0000516 static const unsigned o6 = SparcIntRegClass::o6,
Misha Brukman0870e972003-08-06 22:19:18 +0000517 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0,
518 g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
Misha Brukman07d45162003-07-15 19:09:43 +0000519
Misha Brukman0897c602003-08-06 16:20:22 +0000520 MachineInstr* BinaryCode[] = {
Misha Brukman0897c602003-08-06 16:20:22 +0000521 //
Misha Brukman0870e972003-08-06 22:19:18 +0000522 // Get address to branch into %g1, using %g5 as a temporary
Misha Brukman0897c602003-08-06 16:20:22 +0000523 //
Misha Brukman0870e972003-08-06 22:19:18 +0000524 // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
525 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
526 // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %1
527 BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
Misha Brukmanfad49292003-08-15 00:26:50 +0000528 // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
Misha Brukman0870e972003-08-06 22:19:18 +0000529 BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
530 // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
531 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000532 // or %g5, %g1, %g1 ;; get upper word (in %g5) into %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000533 BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
534 // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
535 BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
Misha Brukmanfad49292003-08-15 00:26:50 +0000536 // jmpl %g1, %g0, %o7 ;; indirect call on %g1
Misha Brukman0870e972003-08-06 22:19:18 +0000537 BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(o7),
Misha Brukmanfad49292003-08-15 00:26:50 +0000538 // nop ;; delay slot
Misha Brukman0870e972003-08-06 22:19:18 +0000539 BuildMI(V9::NOP, 0)
Misha Brukman0897c602003-08-06 16:20:22 +0000540 };
Misha Brukman07d45162003-07-15 19:09:43 +0000541
Misha Brukman0897c602003-08-06 16:20:22 +0000542 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
543 // This is where we save the return address in the LazyResolverMap!!
Misha Brukman0870e972003-08-06 22:19:18 +0000544 if (i == 6 && F != 0) { // Do this right before the JMPL
Misha Brukman0897c602003-08-06 16:20:22 +0000545 uint64_t CurrPC = MCE.getCurrentPCValue();
546 TheJITResolver->addFunctionReference(CurrPC, F);
547 // Remember that this is a far call, to subtract appropriate offset later
548 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
549 }
Misha Brukman07d45162003-07-15 19:09:43 +0000550
Misha Brukman0897c602003-08-06 16:20:22 +0000551 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
552 delete BinaryCode[i];
553 }
Misha Brukman07d45162003-07-15 19:09:43 +0000554}
555
556
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000557int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
558 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000559 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
560 // or things that get fixed up later by the JIT.
561
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000562 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000563 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000564 abort();
565 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000566 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000567 Value *V = MO.getVRegValue();
568 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000569 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000570 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000571 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000572 } else if (const Constant *C = dyn_cast<Constant>(V)) {
573 if (ConstantMap.find(C) != ConstantMap.end()) {
574 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000575 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000576 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000577 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000578 abort();
579 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000580 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
581 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000582 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000583 // external function calls, etc.?
584 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000585 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000586 if (F->isExternal()) {
587 // Sparc backend broken: this MO should be `ExternalSymbol'
588 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
589 } else {
590 rv = (int64_t)MCE.getGlobalValueAddress(F);
591 }
592 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000593 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000594 // Function has not yet been code generated!
595 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
596 // Delayed resolution...
597 rv = TheJITResolver->getLazyResolver(F);
598 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000599 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000600 }
601 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000602 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000603 if (rv == 0) {
604 if (Constant *C = ConstantPointerRef::get(GV)) {
605 if (ConstantMap.find(C) != ConstantMap.end()) {
606 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
607 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000608 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000609 << ", " << *V << " not found in ConstantMap!\n";
610 abort();
611 }
612 }
613 }
Misha Brukman0870e972003-08-06 22:19:18 +0000614 DEBUG(std::cerr << "Global addr: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000615 }
616 // The real target of the call is Addr = PC + (rv * 4)
617 // So undo that: give the instruction (Addr - PC) / 4
618 if (MI.getOpcode() == V9::CALL) {
619 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000620 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
Misha Brukman0870e972003-08-06 22:19:18 +0000621 << "curr PC: 0x" << std::hex << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000622 int64_t CallInstTarget = (rv - CurrPC) >> 2;
623 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
624 DEBUG(std::cerr << "Making far call!\n");
625 // addresss is out of bounds for the 30-bit call,
626 // make an indirect jump-and-link
627 emitFarCall(rv);
628 // this invalidates the instruction so that the call with an incorrect
629 // address will not be emitted
630 rv = 0;
631 } else {
632 // The call fits into 30 bits, so just return the corrected address
633 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000634 }
Misha Brukman8f122222003-06-06 00:26:11 +0000635 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000636 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000637 } else {
638 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
639 abort();
640 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000641 } else if (MO.isPhysicalRegister() ||
642 MO.getType() == MachineOperand::MO_CCRegister)
643 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000644 // This is necessary because the Sparc backend doesn't actually lay out
645 // registers in the real fashion -- it skips those that it chooses not to
646 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000647 unsigned fakeReg = MO.getAllocatedRegNum();
648 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
649 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000650 << realRegByClass << " (LLC: "
651 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000652 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000653 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000654 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000655 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000656 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000657 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000658 rv = (int64_t)
659 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000660 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000661 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000662 // Duplicate code of the above case for VirtualRegister, BasicBlock...
663 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000664 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000665 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000666 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000667 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000668 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000669 // Sparc backend doesn't generate this (yet...)
670 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
671 abort();
672 } else if (MO.isFrameIndex()) {
673 // Sparc backend doesn't generate this (yet...)
674 int FrameIndex = MO.getFrameIndex();
675 std::cerr << "ERROR: Frame index unhandled.\n";
676 abort();
677 } else if (MO.isConstantPoolIndex()) {
678 // Sparc backend doesn't generate this (yet...)
679 std::cerr << "ERROR: Constant Pool index unhandled.\n";
680 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000681 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000682 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000683 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000684 }
685
686 // Finally, deal with the various bitfield-extracting functions that
687 // are used in SPARC assembly. (Some of these make no sense in combination
688 // with some of the above; we'll trust that the instruction selector
689 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000690 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000691 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000692 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000693 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000694 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000695 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000696 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000697 return rv >> 42;
698 } else { // (unadorned) val
699 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000700 }
701}
702
703unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
704 Val >>= bit;
705 return (Val & 1);
706}
707
Misha Brukman3de36f52003-05-27 20:07:58 +0000708bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000709 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000710 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000711 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000712 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000713
Misha Brukmana2196c12003-06-04 20:01:13 +0000714 // The Sparc backend does not use MachineConstantPool;
715 // instead, it has its own constant pool implementation.
716 // We create a new MachineConstantPool here to be compatible with the emitter.
717 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000718 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
719 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
720 E = pool.end(); I != E; ++I)
721 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000722 Constant *C = (Constant*)*I;
723 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000724 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000725 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000726 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000727 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000728
Misha Brukman3de36f52003-05-27 20:07:58 +0000729 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
730 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000731 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000732
Misha Brukman9cedd432003-07-03 18:36:47 +0000733 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000734 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000735
736 // Resolve branches to BasicBlocks for the entire function
737 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
738 long Location = BBLocations[BBRefs[i].first];
739 unsigned *Ref = BBRefs[i].second.first;
740 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000741 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
742 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000743 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
744 MachineOperand &op = MI->getOperand(ii);
745 if (op.isPCRelativeDisp()) {
746 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000747 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000748 // Location is the target of the branch
749 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000750 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000751 // Save the flags.
752 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
753 if (op.opLoBits32()) { loBits32=true; }
754 if (op.opHiBits32()) { hiBits32=true; }
755 if (op.opLoBits64()) { loBits64=true; }
756 if (op.opHiBits64()) { hiBits64=true; }
757 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
758 branchTarget);
759 if (loBits32) { MI->setOperandLo32(ii); }
760 else if (hiBits32) { MI->setOperandHi32(ii); }
761 else if (loBits64) { MI->setOperandLo64(ii); }
762 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000763 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000764 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
765 *Ref = fixedInstr;
766 break;
767 }
768 }
769 }
770 BBRefs.clear();
771 BBLocations.clear();
772
Misha Brukman3de36f52003-05-27 20:07:58 +0000773 return false;
774}
775
776void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000777 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000778 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000779 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
780 unsigned binCode = getBinaryCodeForInstr(**I);
781 if (binCode == (1 << 30)) {
782 // this is an invalid call: the addr is out of bounds. that means a code
783 // sequence has already been emitted, and this is a no-op
784 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
785 } else {
786 emitWord(binCode);
787 }
788 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000789}
790
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000791void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
792 bool isPCRelative)
793{
794 if (isPCRelative) { // must be a call, this is a major hack!
795 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000796 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
797 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000798 // The real target of the call is Addr = PC + (target * 4)
799 // CurByte is the PC, Addr we just received
800 return (void*) (((long)Addr - (long)CurByte) >> 2);
801 } else {
802 if (Function *F = dyn_cast<Function>(V)) {
803 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000804 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000805 cast<Function>(V));
806 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000807 return
808 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000809
810 } else if (Constant *C = ConstantPointerRef::get(V)) {
811 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000812 return (void*)
813 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000814 } else {
815 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
816 << ", " << *V << " not found in ConstantMap!\n";
817 abort();
818 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000819 } else {
820 std::cerr << "Unhandled global: " << *V << "\n";
821 abort();
822 }
823 }
824 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000825 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000826 }
827}
828
Misha Brukman3de36f52003-05-27 20:07:58 +0000829#include "SparcV9CodeEmitter.inc"