blob: d7ca70c2effbbbb60d2fadc97d6ac12c78e87103 [file] [log] [blame]
Misha Brukmana9f7f6e2003-05-30 20:17:33 +00001//===-- SparcV9CodeEmitter.cpp - --------===//
2//
3//
4//===----------------------------------------------------------------------===//
5
Misha Brukmanf86aaa82003-06-02 04:12:39 +00006#include "llvm/Constants.h"
7#include "llvm/Function.h"
8#include "llvm/GlobalVariable.h"
Misha Brukman3de36f52003-05-27 20:07:58 +00009#include "llvm/PassManager.h"
10#include "llvm/CodeGen/MachineCodeEmitter.h"
Misha Brukmana2196c12003-06-04 20:01:13 +000011#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000012#include "llvm/CodeGen/MachineFunctionInfo.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000013#include "llvm/CodeGen/MachineFunctionPass.h"
14#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmana9f7f6e2003-05-30 20:17:33 +000015#include "llvm/Target/TargetMachine.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000016#include "llvm/Target/TargetData.h"
Misha Brukman8f122222003-06-06 00:26:11 +000017#include "Support/Statistic.h"
Misha Brukmanf86aaa82003-06-02 04:12:39 +000018#include "Support/hash_set"
Misha Brukman3de36f52003-05-27 20:07:58 +000019#include "SparcInternals.h"
Misha Brukman0cc640e2003-05-27 21:45:05 +000020#include "SparcV9CodeEmitter.h"
Misha Brukman3de36f52003-05-27 20:07:58 +000021
22bool UltraSparc::addPassesToEmitMachineCode(PassManager &PM,
23 MachineCodeEmitter &MCE) {
Misha Brukman8f122222003-06-06 00:26:11 +000024 MachineCodeEmitter *M = &MCE;
Misha Brukmande07be32003-06-06 04:41:22 +000025 DEBUG(M = MachineCodeEmitter::createFilePrinterEmitter(MCE));
Misha Brukmana2196c12003-06-04 20:01:13 +000026 PM.add(new SparcV9CodeEmitter(*this, *M));
Misha Brukmandcbe7122003-05-31 06:26:06 +000027 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Misha Brukman3de36f52003-05-27 20:07:58 +000028 return false;
29}
30
Misha Brukmanf86aaa82003-06-02 04:12:39 +000031namespace {
32 class JITResolver {
Misha Brukmana2196c12003-06-04 20:01:13 +000033 SparcV9CodeEmitter &SparcV9;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000034 MachineCodeEmitter &MCE;
35
36 // LazyCodeGenMap - Keep track of call sites for functions that are to be
37 // lazily resolved.
Misha Brukmana2196c12003-06-04 20:01:13 +000038 std::map<uint64_t, Function*> LazyCodeGenMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000039
40 // LazyResolverMap - Keep track of the lazy resolver created for a
41 // particular function so that we can reuse them if necessary.
Misha Brukmana2196c12003-06-04 20:01:13 +000042 std::map<Function*, uint64_t> LazyResolverMap;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000043 public:
Misha Brukmana2196c12003-06-04 20:01:13 +000044 JITResolver(SparcV9CodeEmitter &V9,
45 MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
46 uint64_t getLazyResolver(Function *F);
47 uint64_t addFunctionReference(uint64_t Address, Function *F);
48
49 // Utility functions for accessing data from static callback
50 uint64_t getCurrentPCValue() {
51 return MCE.getCurrentPCValue();
52 }
53 unsigned getBinaryCodeForInstr(MachineInstr &MI) {
54 return SparcV9.getBinaryCodeForInstr(MI);
55 }
56
Misha Brukmanf47d9c22003-06-05 20:52:06 +000057 inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
58
Misha Brukmanf86aaa82003-06-02 04:12:39 +000059 private:
Misha Brukmana2196c12003-06-04 20:01:13 +000060 uint64_t emitStubForFunction(Function *F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +000061 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000062 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000063
Misha Brukmanf86aaa82003-06-02 04:12:39 +000064 };
65
66 JITResolver *TheJITResolver;
67}
68
69/// addFunctionReference - This method is called when we need to emit the
70/// address of a function that has not yet been emitted, so we don't know the
71/// address. Instead, we emit a call to the CompilationCallback method, and
72/// keep track of where we are.
73///
Misha Brukmana2196c12003-06-04 20:01:13 +000074uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukmanf86aaa82003-06-02 04:12:39 +000075 LazyCodeGenMap[Address] = F;
76 return (intptr_t)&JITResolver::CompilationCallback;
77}
78
Misha Brukmana2196c12003-06-04 20:01:13 +000079uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
80 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +000081 assert(I != LazyCodeGenMap.end() && "Not in map!");
82 Function *F = I->second;
83 LazyCodeGenMap.erase(I);
84 return MCE.forceCompilationOf(F);
85}
86
Misha Brukmana2196c12003-06-04 20:01:13 +000087uint64_t JITResolver::getLazyResolver(Function *F) {
88 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +000089 if (I != LazyResolverMap.end() && I->first == F) return I->second;
90
91//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
92
Misha Brukmana2196c12003-06-04 20:01:13 +000093 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +000094 LazyResolverMap.insert(I, std::make_pair(F, Stub));
95 return Stub;
96}
97
Misha Brukmanf47d9c22003-06-05 20:52:06 +000098uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
99
100 static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
101 i7 = SparcIntRegClass::i7,
102 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
103
104 //
105 // Save %i1, %i2 to the stack so we can form a 64-bit constant in %i2
106 //
107
108 // stx %i1, [%sp + 2119] ;; save %i1 to the stack, used as temp
109 MachineInstr *STX = BuildMI(V9::STXi, 3).addReg(i1).addReg(o6).addSImm(2119);
110 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*STX);
111 delete STX;
112 Addr += 4;
113
114 // stx %i2, [%sp + 2127] ;; save %i2 to the stack
115 STX = BuildMI(V9::STXi, 3).addReg(i2).addReg(o6).addSImm(2127);
116 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*STX);
117 delete STX;
118 Addr += 4;
119
120 //
121 // Get address to branch into %i2, using %i1 as a temporary
122 //
123
124 // sethi %uhi(Target), %i1 ;; get upper 22 bits of Target into %i1
125 MachineInstr *SH = BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(i1);
126 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SH);
127 delete SH;
128 Addr += 4;
129
130 // or %i1, %ulo(Target), %i1 ;; get 10 lower bits of upper word into %1
131 MachineInstr *OR = BuildMI(V9::ORi, 3)
132 .addReg(i1).addSImm((Target >> 32) & 0x03ff).addReg(i1);
133 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR);
134 delete OR;
135 Addr += 4;
136
137 // sllx %i1, 32, %i1 ;; shift those 10 bits to the upper word
138 MachineInstr *SL = BuildMI(V9::SLLXi6, 3).addReg(i1).addSImm(32).addReg(i1);
139 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SL);
140 delete SL;
141 Addr += 4;
142
143 // sethi %hi(Target), %i2 ;; extract bits 10-31 into the dest reg
144 SH = BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(i2);
145 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SH);
146 delete SH;
147 Addr += 4;
148
149 // or %i1, %i2, %i2 ;; get upper word (in %i1) into %i2
150 OR = BuildMI(V9::ORr, 3).addReg(i1).addReg(i2).addReg(i2);
151 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR);
152 delete OR;
153 Addr += 4;
154
155 // or %i2, %lo(Target), %i2 ;; get lowest 10 bits of Target into %i2
156 OR = BuildMI(V9::ORi, 3).addReg(i2).addSImm(Target & 0x03ff).addReg(i2);
157 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR);
158 delete OR;
159 Addr += 4;
160
161 // ldx [%sp + 2119], %i1 ;; restore %i1 -> 2119 = BIAS(2047) + 72
162 MachineInstr *LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2119).addReg(i1);
163 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*LDX);
164 delete LDX;
165 Addr += 4;
166
167 // jmpl %i2, %g0, %g0 ;; indirect branch on %i2
168 MachineInstr *J = BuildMI(V9::JMPLRETr, 3).addReg(i2).addReg(g0).addReg(g0);
169 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*J);
170 delete J;
171 Addr += 4;
172
173 // ldx [%sp + 2127], %i2 ;; restore %i2 -> 2127 = BIAS(2047) + 80
174 LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2127).addReg(i2);
175 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*LDX);
176 delete LDX;
177 Addr += 4;
178
179 return Addr;
180}
181
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000182void JITResolver::CompilationCallback() {
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000183 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
184 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000185 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000186
187 // Rewrite the call target... so that we don't fault every time we execute
188 // the call.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000189#if 0
Misha Brukmana2196c12003-06-04 20:01:13 +0000190 int64_t RealCallTarget = (int64_t)
191 ((NewVal - TheJITResolver->getCurrentPCValue()) >> 4);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000192 if (RealCallTarget >= (1<<22) || RealCallTarget <= -(1<<22)) {
193 std::cerr << "Address out of bounds for 22bit BA: " << RealCallTarget<<"\n";
194 abort();
195 }
196#endif
197
198 //uint64_t CurrPC = TheJITResolver->getCurrentPCValue();
199 // we will insert 9 instructions before we do the actual jump
200 //int64_t NewTarget = (NewVal - 9*4 - InstAddr) >> 2;
201
202 static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
203 i7 = SparcIntRegClass::i7, o6 = SparcIntRegClass::o6,
204 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0;
205
206 // Subtract 4 to overwrite the 'save' that's there now
207 uint64_t InstAddr = CameFrom-4;
208
209 InstAddr = TheJITResolver->insertFarJumpAtAddr(Target, InstAddr);
210
211 // CODE SHOULD NEVER GO PAST THIS LOAD!! The real function should return to
212 // the original caller, not here!!
213
214 // FIXME: add call 0 to make sure?!?
215
216 // =============== THE REAL STUB ENDS HERE =========================
217
218 // What follows below is one-time restore code, because this callback may be
219 // changing registers in unpredictible ways. However, since it is executed
220 // only once per function (after the function is resolved, the callback is no
221 // longer in the path), this has to be done only once.
222 //
223 // Thus, it is after the regular stub code. The call back returns to THIS
224 // point, but every other call to the target function will execute the code
225 // above. Hence, this code is one-time use.
226
227 uint64_t OneTimeRestore = InstAddr;
228
229 // restore %g0, 0, %g0
230 //MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
231 // .addMReg(g0, MOTy::Def);
232 //*((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*R);
233 //delete R;
234
235 // FIXME: BuildMI() above crashes. Encode the instruction directly.
236 // restore %g0, 0, %g0
237 *((unsigned*)(intptr_t)InstAddr) = 0x81e82000U;
238 InstAddr += 4;
239
240 InstAddr = TheJITResolver->insertFarJumpAtAddr(Target, InstAddr);
241
242 // FIXME: if the target function is close enough to fit into the 19bit disp of
243 // BA, we should use this version, as its much cheaper to generate.
244 /*
245 MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(RealCallTarget);
246 *((unsigned*)(intptr_t)InstAddr) = TheJITResolver->getBinaryCodeForInstr(*MI);
Misha Brukmana2196c12003-06-04 20:01:13 +0000247 delete MI;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000248 InstAddr += 4;
249
250 // Add another NOP
251 MachineInstr *Nop = BuildMI(V9::NOP, 0);
252 *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*Nop);
253 delete Nop;
254 InstAddr += 4;
255
256 MachineInstr *BA = BuildMI(V9::BA, 1).addSImm(RealCallTarget-2);
257 *((unsigned*)(intptr_t)InstAddr) = TheJITResolver->getBinaryCodeForInstr(*BA);
258 delete BA;
259 */
260
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000261 // Change the return address to reexecute the call instruction...
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000262 // The return address is really %o7, but will disappear after this function
263 // returns, and the register windows are rotated away.
264#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
265 __asm__ __volatile__ ("or %%g0, %0, %%i7" : : "r" (OneTimeRestore-8));
266#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000267}
268
269/// emitStubForFunction - This method is used by the JIT when it needs to emit
270/// the address of a function for a function whose code has not yet been
271/// generated. In order to do this, it generates a stub which jumps to the lazy
272/// function compiler, which will eventually get fixed to call the function
273/// directly.
274///
Misha Brukmana2196c12003-06-04 20:01:13 +0000275uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000276 MCE.startFunctionStub(*F, 6);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000277
Misha Brukman8f122222003-06-06 00:26:11 +0000278 DEBUG(std::cerr << "Emitting stub at addr: 0x"
279 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000280
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000281 unsigned o6 = SparcIntRegClass::o6;
282 // save %sp, -192, %sp
283 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
284 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
285 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000286
287 int64_t CurrPC = MCE.getCurrentPCValue();
288 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000289
Misha Brukmana2196c12003-06-04 20:01:13 +0000290 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000291 if (CallTarget >= (1 << 30) || CallTarget <= -(1 << 30)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000292 std::cerr << "Call target beyond 30 bit limit of CALL: "
293 << CallTarget << "\n";
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000294 abort();
295 }
296 // call CallTarget ;; invoke the callback
297 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
Misha Brukmana2196c12003-06-04 20:01:13 +0000298 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
299 delete Call;
300
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000301 // nop ;; call delay slot
Misha Brukmana2196c12003-06-04 20:01:13 +0000302 MachineInstr *Nop = BuildMI(V9::NOP, 0);
303 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
304 delete Nop;
305
306 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
307 return (intptr_t)MCE.finishFunctionStub(*F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000308}
309
310
Misha Brukmana2196c12003-06-04 20:01:13 +0000311SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
312 MachineCodeEmitter &M): TM(tm), MCE(M)
313{
314 TheJITResolver = new JITResolver(*this, M);
315}
316
317SparcV9CodeEmitter::~SparcV9CodeEmitter() {
318 delete TheJITResolver;
319}
320
321void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000322 // Output the constant in big endian byte order...
323 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000324 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000325 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000326 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000327 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000328}
329
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000330unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000331SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukman9cedd432003-07-03 18:36:47 +0000332 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000333 const TargetRegInfo &RI = TM.getRegInfo();
334 unsigned regClass, regType = RI.getRegType(fakeReg);
335 // At least map fakeReg into its class
336 fakeReg = RI.getClassRegNum(fakeReg, regClass);
337
Misha Brukman9cedd432003-07-03 18:36:47 +0000338 switch (regClass) {
339 case UltraSparcRegInfo::IntRegClassID: {
340 // Sparc manual, p31
341 static const unsigned IntRegMap[] = {
342 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
343 8, 9, 10, 11, 12, 13, 15,
344 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
345 16, 17, 18, 19, 20, 21, 22, 23,
346 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
347 24, 25, 26, 27, 28, 29, 30, 31,
348 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
349 0, 1, 2, 3, 4, 5, 6, 7,
350 // "o6"
351 14
352 };
353
354 return IntRegMap[fakeReg];
355 break;
356 }
357 case UltraSparcRegInfo::FloatRegClassID: {
358 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000359 if (regType == UltraSparcRegInfo::FPSingleRegType) {
360 // only numbered 0-31, hence can already fit into 5 bits (and 6)
361 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
362 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
363 // FIXME: This assumes that we only have 5-bit register fiels!
364 // From Sparc Manual, page 40.
365 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
366 fakeReg |= (fakeReg >> 5) & 1;
367 fakeReg &= 0x1f;
368 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
369 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000370 return fakeReg;
371 }
372 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000373 /* xcc, icc, ccr */
374 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000375
Misha Brukmandfbfc572003-07-16 20:30:40 +0000376 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
377 && "CC register out of bounds for IntCCReg map");
378 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
379 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000380 }
381 case UltraSparcRegInfo::FloatCCRegClassID: {
382 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
383 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
384 return fakeReg;
385 }
386 default:
387 assert(0 && "Invalid unified register number in getRegType");
388 return fakeReg;
389 }
390}
391
392
Misha Brukman07d45162003-07-15 19:09:43 +0000393// WARNING: if the call used the delay slot to do meaningful work, that's not
394// being accounted for, and the behavior will be incorrect!!
395inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target) {
396 static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
397 i7 = SparcIntRegClass::i7,
398 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
399
400 //
401 // Save %i1, %i2 to the stack so we can form a 64-bit constant in %i2
402 //
403
404 // stx %i1, [%sp + 2119] ;; save %i1 to the stack, used as temp
405 MachineInstr *STX = BuildMI(V9::STXi, 3).addReg(i1).addReg(o6).addSImm(2119);
406 emitWord(getBinaryCodeForInstr(*STX));
407 delete STX;
408
409 // stx %i2, [%sp + 2127] ;; save %i2 to the stack
410 STX = BuildMI(V9::STXi, 3).addReg(i2).addReg(o6).addSImm(2127);
411 emitWord(getBinaryCodeForInstr(*STX));
412 delete STX;
413
414 //
415 // Get address to branch into %i2, using %i1 as a temporary
416 //
417
418 // sethi %uhi(Target), %i1 ;; get upper 22 bits of Target into %i1
419 MachineInstr *SH = BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(i1);
420 emitWord(getBinaryCodeForInstr(*SH));
421 delete SH;
422
423 // or %i1, %ulo(Target), %i1 ;; get 10 lower bits of upper word into %1
424 MachineInstr *OR = BuildMI(V9::ORi, 3)
425 .addReg(i1).addSImm((Target >> 32) & 0x03ff).addReg(i1);
426 emitWord(getBinaryCodeForInstr(*OR));
427 delete OR;
428
429 // sllx %i1, 32, %i1 ;; shift those 10 bits to the upper word
430 MachineInstr *SL = BuildMI(V9::SLLXi6, 3).addReg(i1).addSImm(32).addReg(i1);
431 emitWord(getBinaryCodeForInstr(*SL));
432 delete SL;
433
434 // sethi %hi(Target), %i2 ;; extract bits 10-31 into the dest reg
435 SH = BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(i2);
436 emitWord(getBinaryCodeForInstr(*SH));
437 delete SH;
438
439 // or %i1, %i2, %i2 ;; get upper word (in %i1) into %i2
440 OR = BuildMI(V9::ORr, 3).addReg(i1).addReg(i2).addReg(i2);
441 emitWord(getBinaryCodeForInstr(*OR));
442 delete OR;
443
444 // or %i2, %lo(Target), %i2 ;; get lowest 10 bits of Target into %i2
445 OR = BuildMI(V9::ORi, 3).addReg(i2).addSImm(Target & 0x03ff).addReg(i2);
446 emitWord(getBinaryCodeForInstr(*OR));
447 delete OR;
448
449 // ldx [%sp + 2119], %i1 ;; restore %i1 -> 2119 = BIAS(2047) + 72
450 MachineInstr *LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2119).addReg(i1);
451 emitWord(getBinaryCodeForInstr(*LDX));
452 delete LDX;
453
454 // jmpl %i2, %g0, %07 ;; indirect call on %i2
455 MachineInstr *J = BuildMI(V9::JMPLRETr, 3).addReg(i2).addReg(g0).addReg(07);
456 emitWord(getBinaryCodeForInstr(*J));
457 delete J;
458
459 // ldx [%sp + 2127], %i2 ;; restore %i2 -> 2127 = BIAS(2047) + 80
460 LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2127).addReg(i2);
461 emitWord(getBinaryCodeForInstr(*LDX));
462 delete LDX;
463}
464
465
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000466int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
467 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000468 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
469 // or things that get fixed up later by the JIT.
470
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000471 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000472 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000473 abort();
474 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000475 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000476 Value *V = MO.getVRegValue();
477 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000478 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000479 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000480 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000481 } else if (const Constant *C = dyn_cast<Constant>(V)) {
482 if (ConstantMap.find(C) != ConstantMap.end()) {
483 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000484 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000485 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000486 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000487 abort();
488 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000489 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
490 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000491 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000492 // external function calls, etc.?
493 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000494 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000495 if (F->isExternal()) {
496 // Sparc backend broken: this MO should be `ExternalSymbol'
497 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
498 } else {
499 rv = (int64_t)MCE.getGlobalValueAddress(F);
500 }
501 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000502 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000503 // Function has not yet been code generated!
504 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
505 // Delayed resolution...
506 rv = TheJITResolver->getLazyResolver(F);
507 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000508 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000509 }
510 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000511 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000512 if (rv == 0) {
513 if (Constant *C = ConstantPointerRef::get(GV)) {
514 if (ConstantMap.find(C) != ConstantMap.end()) {
515 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
516 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000517 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000518 << ", " << *V << " not found in ConstantMap!\n";
519 abort();
520 }
521 }
522 }
523 DEBUG(std::cerr << "Global addr: " << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000524 }
525 // The real target of the call is Addr = PC + (rv * 4)
526 // So undo that: give the instruction (Addr - PC) / 4
527 if (MI.getOpcode() == V9::CALL) {
528 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000529 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
530 << "curr PC: 0x" << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000531 int64_t CallInstTarget = (rv - CurrPC) >> 2;
532 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
533 DEBUG(std::cerr << "Making far call!\n");
534 // addresss is out of bounds for the 30-bit call,
535 // make an indirect jump-and-link
536 emitFarCall(rv);
537 // this invalidates the instruction so that the call with an incorrect
538 // address will not be emitted
539 rv = 0;
540 } else {
541 // The call fits into 30 bits, so just return the corrected address
542 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000543 }
Misha Brukman8f122222003-06-06 00:26:11 +0000544 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000545 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000546 } else {
547 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
548 abort();
549 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000550 } else if (MO.isPhysicalRegister() ||
551 MO.getType() == MachineOperand::MO_CCRegister)
552 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000553 // This is necessary because the Sparc backend doesn't actually lay out
554 // registers in the real fashion -- it skips those that it chooses not to
555 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000556 unsigned fakeReg = MO.getAllocatedRegNum();
557 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
558 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000559 << realRegByClass << " (LLC: "
560 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000561 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000562 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000563 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000564 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000565 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000566 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000567 rv = (int64_t)
568 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000569 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000570 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000571 // Duplicate code of the above case for VirtualRegister, BasicBlock...
572 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000573 DEBUG(std::cerr << "Saving reference to MBB\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000574 BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000575 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000576 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000577 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000578 // Sparc backend doesn't generate this (yet...)
579 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
580 abort();
581 } else if (MO.isFrameIndex()) {
582 // Sparc backend doesn't generate this (yet...)
583 int FrameIndex = MO.getFrameIndex();
584 std::cerr << "ERROR: Frame index unhandled.\n";
585 abort();
586 } else if (MO.isConstantPoolIndex()) {
587 // Sparc backend doesn't generate this (yet...)
588 std::cerr << "ERROR: Constant Pool index unhandled.\n";
589 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000590 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000591 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000592 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000593 }
594
595 // Finally, deal with the various bitfield-extracting functions that
596 // are used in SPARC assembly. (Some of these make no sense in combination
597 // with some of the above; we'll trust that the instruction selector
598 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000599 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000600 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000601 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000602 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000603 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000604 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000605 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000606 return rv >> 42;
607 } else { // (unadorned) val
608 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000609 }
610}
611
612unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
613 Val >>= bit;
614 return (Val & 1);
615}
616
Misha Brukman3de36f52003-05-27 20:07:58 +0000617bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000618 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000619 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000620 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000621 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000622
Misha Brukmana2196c12003-06-04 20:01:13 +0000623 // The Sparc backend does not use MachineConstantPool;
624 // instead, it has its own constant pool implementation.
625 // We create a new MachineConstantPool here to be compatible with the emitter.
626 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000627 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
628 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
629 E = pool.end(); I != E; ++I)
630 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000631 Constant *C = (Constant*)*I;
632 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000633 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000634 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000635 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000636 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000637
Misha Brukman3de36f52003-05-27 20:07:58 +0000638 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
639 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000640 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000641
Misha Brukman9cedd432003-07-03 18:36:47 +0000642 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000643 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000644
645 // Resolve branches to BasicBlocks for the entire function
646 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
647 long Location = BBLocations[BBRefs[i].first];
648 unsigned *Ref = BBRefs[i].second.first;
649 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000650 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
651 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000652 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
653 MachineOperand &op = MI->getOperand(ii);
654 if (op.isPCRelativeDisp()) {
655 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000656 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000657 // Location is the target of the branch
658 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000659 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000660 // Save the flags.
661 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
662 if (op.opLoBits32()) { loBits32=true; }
663 if (op.opHiBits32()) { hiBits32=true; }
664 if (op.opLoBits64()) { loBits64=true; }
665 if (op.opHiBits64()) { hiBits64=true; }
666 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
667 branchTarget);
668 if (loBits32) { MI->setOperandLo32(ii); }
669 else if (hiBits32) { MI->setOperandHi32(ii); }
670 else if (loBits64) { MI->setOperandLo64(ii); }
671 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000672 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000673 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
674 *Ref = fixedInstr;
675 break;
676 }
677 }
678 }
679 BBRefs.clear();
680 BBLocations.clear();
681
Misha Brukman3de36f52003-05-27 20:07:58 +0000682 return false;
683}
684
685void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000686 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000687 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000688 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
689 unsigned binCode = getBinaryCodeForInstr(**I);
690 if (binCode == (1 << 30)) {
691 // this is an invalid call: the addr is out of bounds. that means a code
692 // sequence has already been emitted, and this is a no-op
693 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
694 } else {
695 emitWord(binCode);
696 }
697 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000698}
699
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000700void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
701 bool isPCRelative)
702{
703 if (isPCRelative) { // must be a call, this is a major hack!
704 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000705 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
706 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000707 // The real target of the call is Addr = PC + (target * 4)
708 // CurByte is the PC, Addr we just received
709 return (void*) (((long)Addr - (long)CurByte) >> 2);
710 } else {
711 if (Function *F = dyn_cast<Function>(V)) {
712 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000713 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000714 cast<Function>(V));
715 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000716 return
717 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000718
719 } else if (Constant *C = ConstantPointerRef::get(V)) {
720 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000721 return (void*)
722 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000723 } else {
724 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
725 << ", " << *V << " not found in ConstantMap!\n";
726 abort();
727 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000728 } else {
729 std::cerr << "Unhandled global: " << *V << "\n";
730 abort();
731 }
732 }
733 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000734 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000735 }
736}
737
738
Misha Brukman3de36f52003-05-27 20:07:58 +0000739#include "SparcV9CodeEmitter.inc"