blob: 3f77d0e9164a911a01cfc0837eb469870f1f54c0 [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
23bool UltraSparc::addPassesToEmitMachineCode(PassManager &PM,
24 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 Brukmanf86aaa82003-06-02 04:12:39 +000078 static void CompilationCallback();
Misha Brukmana2196c12003-06-04 20:01:13 +000079 uint64_t resolveFunctionReference(uint64_t RetAddr);
Misha Brukmanf47d9c22003-06-05 20:52:06 +000080
Misha Brukmanf86aaa82003-06-02 04:12:39 +000081 };
82
83 JITResolver *TheJITResolver;
Misha Brukman0897c602003-08-06 16:20:22 +000084 std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000085}
86
87/// addFunctionReference - This method is called when we need to emit the
88/// address of a function that has not yet been emitted, so we don't know the
89/// address. Instead, we emit a call to the CompilationCallback method, and
90/// keep track of where we are.
91///
Misha Brukmana2196c12003-06-04 20:01:13 +000092uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +000093 LazyCodeGenMap[Address] = F;
Misha Brukmanf86aaa82003-06-02 04:12:39 +000094 return (intptr_t)&JITResolver::CompilationCallback;
95}
96
Misha Brukman0897c602003-08-06 16:20:22 +000097/// deleteFunctionReference - If we are emitting a far call, we already added a
98/// reference to the function, but it is now incorrect, since the address to the
99/// JIT resolver is too far away to be a simple call instruction. This is used
100/// to remove the address from the map.
101///
102void JITResolver::deleteFunctionReference(uint64_t Address) {
103 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
104 assert(I != LazyCodeGenMap.end() && "Not in map!");
105 LazyCodeGenMap.erase(I);
106}
107
Misha Brukmana2196c12003-06-04 20:01:13 +0000108uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
109 std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000110 assert(I != LazyCodeGenMap.end() && "Not in map!");
111 Function *F = I->second;
112 LazyCodeGenMap.erase(I);
113 return MCE.forceCompilationOf(F);
114}
115
Misha Brukmana2196c12003-06-04 20:01:13 +0000116uint64_t JITResolver::getLazyResolver(Function *F) {
117 std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000118 if (I != LazyResolverMap.end() && I->first == F) return I->second;
119
120//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
121
Misha Brukmana2196c12003-06-04 20:01:13 +0000122 uint64_t Stub = emitStubForFunction(F);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000123 LazyResolverMap.insert(I, std::make_pair(F, Stub));
124 return Stub;
125}
126
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000127uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
128
129 static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
130 i7 = SparcIntRegClass::i7,
131 o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
132
Misha Brukman0897c602003-08-06 16:20:22 +0000133 MachineInstr* BinaryCode[] = {
134 //
135 // Save %i1, %i2 to the stack so we can form a 64-bit constant in %i2
136 //
137 // stx %i1, [%sp + 2119] ;; save %i1 to the stack, used as temp
138 BuildMI(V9::STXi, 3).addReg(i1).addReg(o6).addSImm(2119),
139 // stx %i2, [%sp + 2127] ;; save %i2 to the stack
140 BuildMI(V9::STXi, 3).addReg(i2).addReg(o6).addSImm(2127),
141 //
142 // Get address to branch into %i2, using %i1 as a temporary
143 //
144 // sethi %uhi(Target), %i1 ;; get upper 22 bits of Target into %i1
145 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(i1),
146 // or %i1, %ulo(Target), %i1 ;; get 10 lower bits of upper word into %1
147 BuildMI(V9::ORi, 3).addReg(i1).addSImm((Target >> 32) & 0x03ff).addReg(i1),
148 // sllx %i1, 32, %i1 ;; shift those 10 bits to the upper word
149 BuildMI(V9::SLLXi6, 3).addReg(i1).addSImm(32).addReg(i1),
150 // sethi %hi(Target), %i2 ;; extract bits 10-31 into the dest reg
151 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(i2),
152 // or %i1, %i2, %i2 ;; get upper word (in %i1) into %i2
153 BuildMI(V9::ORr, 3).addReg(i1).addReg(i2).addReg(i2),
154 // or %i2, %lo(Target), %i2 ;; get lowest 10 bits of Target into %i2
155 BuildMI(V9::ORi, 3).addReg(i2).addSImm(Target & 0x03ff).addReg(i2),
156 // ldx [%sp + 2119], %i1 ;; restore %i1 -> 2119 = BIAS(2047) + 72
157 BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2119).addReg(i1),
158 // jmpl %i2, %g0, %g0 ;; indirect branch on %i2
159 BuildMI(V9::JMPLRETr, 3).addReg(i2).addReg(g0).addReg(g0),
160 // ldx [%sp + 2127], %i2 ;; restore %i2 -> 2127 = BIAS(2047) + 80
161 BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2127).addReg(i2)
162 };
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000163
Misha Brukman0897c602003-08-06 16:20:22 +0000164 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
165 *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
166 delete BinaryCode[i];
167 Addr += 4;
168 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000169
170 return Addr;
171}
172
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000173void JITResolver::CompilationCallback() {
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000174 uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
175 int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
Misha Brukman8f122222003-06-06 00:26:11 +0000176 DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
Misha Brukman0897c602003-08-06 16:20:22 +0000177#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
178 register int64_t returnAddr;
179 __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
180 DEBUG(std::cerr << "Read i7 (return addr) = "
181 << std::hex << returnAddr << ", value: "
182 << std::hex << *(unsigned*)returnAddr << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000183#endif
184
Misha Brukman0897c602003-08-06 16:20:22 +0000185 // Rewrite the call target so that we don't fault every time we execute it.
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000186 //
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000187
Misha Brukman0897c602003-08-06 16:20:22 +0000188 static const unsigned o6 = SparcIntRegClass::o6;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000189
Misha Brukman0897c602003-08-06 16:20:22 +0000190 // Subtract enough to overwrite up to the 'save' instruction
191 // This depends on whether we made a short call (1 instruction) to the
192 // farCall (10 instructions)
193 uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 40;
194 uint64_t CodeBegin = CameFrom - Offset;
195
196 // Make sure that what we're about to overwrite is indeed "save"
197 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
198 unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
199 delete SV;
200 unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
201 assert(CodeInMem == SaveInst && "About to overwrite smthg not a save instr!");
202 DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n");
203 TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000204
205 // FIXME: if the target function is close enough to fit into the 19bit disp of
206 // BA, we should use this version, as its much cheaper to generate.
Misha Brukman0897c602003-08-06 16:20:22 +0000207#if 0
208 uint64_t InstAddr = CodeBegin;
209 // ba <target>
210 MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target);
211 *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI);
212 InstAddr += 4;
Misha Brukmana2196c12003-06-04 20:01:13 +0000213 delete MI;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000214
Misha Brukman0897c602003-08-06 16:20:22 +0000215 // nop
216 MI = BuildMI(V9::NOP, 0);
217 *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop);
218 delete MI;
219#endif
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000220
Misha Brukman0897c602003-08-06 16:20:22 +0000221 // Change the return address to reexecute the restore, then the jump
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000222 // The return address is really %o7, but will disappear after this function
223 // returns, and the register windows are rotated away.
224#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
Misha Brukman0897c602003-08-06 16:20:22 +0000225 __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
226 DEBUG(std::cerr << "Callback setting return addr to "
227 << std::hex << (CameFrom-Offset-12) << "\n");
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000228#endif
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000229}
230
231/// emitStubForFunction - This method is used by the JIT when it needs to emit
232/// the address of a function for a function whose code has not yet been
233/// generated. In order to do this, it generates a stub which jumps to the lazy
234/// function compiler, which will eventually get fixed to call the function
235/// directly.
236///
Misha Brukmana2196c12003-06-04 20:01:13 +0000237uint64_t JITResolver::emitStubForFunction(Function *F) {
Misha Brukman0897c602003-08-06 16:20:22 +0000238 MCE.startFunctionStub(*F, 20);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000239
Misha Brukman8f122222003-06-06 00:26:11 +0000240 DEBUG(std::cerr << "Emitting stub at addr: 0x"
241 << std::hex << MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000242
Misha Brukman0897c602003-08-06 16:20:22 +0000243 unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
244
245 // restore %g0, 0, %g0
246 MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
247 .addMReg(g0, MOTy::Def);
248 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
249 delete R;
250
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000251 // save %sp, -192, %sp
252 MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
253 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
254 delete SV;
Misha Brukmana2196c12003-06-04 20:01:13 +0000255
256 int64_t CurrPC = MCE.getCurrentPCValue();
257 int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
258 int64_t CallTarget = (Addr-CurrPC) >> 2;
Misha Brukman0897c602003-08-06 16:20:22 +0000259 //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
260 // Since this is a far call, the actual address of the call is shifted
261 // by the number of instructions it takes to calculate the exact address
262 deleteFunctionReference(CurrPC);
263 SparcV9.emitFarCall(Addr, F);
264#if 0
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000265 } else {
266 // call CallTarget ;; invoke the callback
267 MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
268 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
269 delete Call;
Misha Brukmana2196c12003-06-04 20:01:13 +0000270
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000271 // nop ;; call delay slot
272 MachineInstr *Nop = BuildMI(V9::NOP, 0);
273 SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
274 delete Nop;
Misha Brukman0897c602003-08-06 16:20:22 +0000275
276 addCallFlavor(CurrPC, ShortCall);
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000277 }
Misha Brukman0897c602003-08-06 16:20:22 +0000278#endif
Misha Brukmana2196c12003-06-04 20:01:13 +0000279
280 SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
Misha Brukman0897c602003-08-06 16:20:22 +0000281 return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000282}
283
284
Misha Brukmana2196c12003-06-04 20:01:13 +0000285SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
286 MachineCodeEmitter &M): TM(tm), MCE(M)
287{
288 TheJITResolver = new JITResolver(*this, M);
289}
290
291SparcV9CodeEmitter::~SparcV9CodeEmitter() {
292 delete TheJITResolver;
293}
294
295void SparcV9CodeEmitter::emitWord(unsigned Val) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000296 // Output the constant in big endian byte order...
297 unsigned byteVal;
Misha Brukmana2196c12003-06-04 20:01:13 +0000298 for (int i = 3; i >= 0; --i) {
Misha Brukman3de36f52003-05-27 20:07:58 +0000299 byteVal = Val >> 8*i;
Misha Brukmana2196c12003-06-04 20:01:13 +0000300 MCE.emitByte(byteVal & 255);
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000301 }
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000302}
303
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000304unsigned
Misha Brukman173e2502003-07-14 23:26:03 +0000305SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
Misha Brukman9cedd432003-07-03 18:36:47 +0000306 MachineInstr &MI) {
Misha Brukman173e2502003-07-14 23:26:03 +0000307 const TargetRegInfo &RI = TM.getRegInfo();
308 unsigned regClass, regType = RI.getRegType(fakeReg);
309 // At least map fakeReg into its class
310 fakeReg = RI.getClassRegNum(fakeReg, regClass);
311
Misha Brukman9cedd432003-07-03 18:36:47 +0000312 switch (regClass) {
313 case UltraSparcRegInfo::IntRegClassID: {
314 // Sparc manual, p31
315 static const unsigned IntRegMap[] = {
316 // "o0", "o1", "o2", "o3", "o4", "o5", "o7",
317 8, 9, 10, 11, 12, 13, 15,
318 // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
319 16, 17, 18, 19, 20, 21, 22, 23,
320 // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
321 24, 25, 26, 27, 28, 29, 30, 31,
322 // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
323 0, 1, 2, 3, 4, 5, 6, 7,
324 // "o6"
325 14
326 };
327
328 return IntRegMap[fakeReg];
329 break;
330 }
331 case UltraSparcRegInfo::FloatRegClassID: {
332 DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
Misha Brukman173e2502003-07-14 23:26:03 +0000333 if (regType == UltraSparcRegInfo::FPSingleRegType) {
334 // only numbered 0-31, hence can already fit into 5 bits (and 6)
335 DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
336 } else if (regType == UltraSparcRegInfo::FPDoubleRegType) {
337 // FIXME: This assumes that we only have 5-bit register fiels!
338 // From Sparc Manual, page 40.
339 // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
340 fakeReg |= (fakeReg >> 5) & 1;
341 fakeReg &= 0x1f;
342 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
343 }
Misha Brukman9cedd432003-07-03 18:36:47 +0000344 return fakeReg;
345 }
346 case UltraSparcRegInfo::IntCCRegClassID: {
Misha Brukmandfbfc572003-07-16 20:30:40 +0000347 /* xcc, icc, ccr */
348 static const unsigned IntCCReg[] = { 6, 4, 2 };
Misha Brukman9cedd432003-07-03 18:36:47 +0000349
Misha Brukmandfbfc572003-07-16 20:30:40 +0000350 assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
351 && "CC register out of bounds for IntCCReg map");
352 DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
353 return IntCCReg[fakeReg];
Misha Brukman9cedd432003-07-03 18:36:47 +0000354 }
355 case UltraSparcRegInfo::FloatCCRegClassID: {
356 /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
357 DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
358 return fakeReg;
359 }
360 default:
361 assert(0 && "Invalid unified register number in getRegType");
362 return fakeReg;
363 }
364}
365
366
Misha Brukman07d45162003-07-15 19:09:43 +0000367// WARNING: if the call used the delay slot to do meaningful work, that's not
368// being accounted for, and the behavior will be incorrect!!
Misha Brukman0897c602003-08-06 16:20:22 +0000369inline void SparcV9CodeEmitter::emitFarCall(uint64_t Target, Function *F) {
Misha Brukman07d45162003-07-15 19:09:43 +0000370 static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
Misha Brukmana1f1fea2003-07-29 19:00:58 +0000371 i7 = SparcIntRegClass::i7, o6 = SparcIntRegClass::o6,
372 o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0;
Misha Brukman07d45162003-07-15 19:09:43 +0000373
Misha Brukman0897c602003-08-06 16:20:22 +0000374 MachineInstr* BinaryCode[] = {
375 //
376 // Save %i1, %i2 to the stack so we can form a 64-bit constant in %i2
377 //
378 // stx %i1, [%sp + 2119] ;; save %i1 to the stack, used as temp
379 BuildMI(V9::STXi, 3).addReg(i1).addReg(o6).addSImm(2119),
380 // stx %i2, [%sp + 2127] ;; save %i2 to the stack
381 BuildMI(V9::STXi, 3).addReg(i2).addReg(o6).addSImm(2127),
382 //
383 // Get address to branch into %i2, using %i1 as a temporary
384 //
385 // sethi %uhi(Target), %i1 ;; get upper 22 bits of Target into %i1
386 BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(i1),
387 // or %i1, %ulo(Target), %i1 ;; get 10 lower bits of upper word into %1
388 BuildMI(V9::ORi, 3).addReg(i1).addSImm((Target >> 32) & 0x03ff).addReg(i1),
389 // sllx %i1, 32, %i1 ;; shift those 10 bits to the upper word
390 BuildMI(V9::SLLXi6, 3).addReg(i1).addSImm(32).addReg(i1),
391 // sethi %hi(Target), %i2 ;; extract bits 10-31 into the dest reg
392 BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(i2),
393 // or %i1, %i2, %i2 ;; get upper word (in %i1) into %i2
394 BuildMI(V9::ORr, 3).addReg(i1).addReg(i2).addReg(i2),
395 // or %i2, %lo(Target), %i2 ;; get lowest 10 bits of Target into %i2
396 BuildMI(V9::ORi, 3).addReg(i2).addSImm(Target & 0x03ff).addReg(i2),
397 // ldx [%sp + 2119], %i1 ;; restore %i1 -> 2119 = BIAS(2047) + 72
398 BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2119).addReg(i1),
399 // jmpl %i2, %g0, %o7 ;; indirect call on %i2
400 BuildMI(V9::JMPLRETr, 3).addReg(i2).addReg(g0).addReg(o7),
401 // ldx [%sp + 2127], %i2 ;; restore %i2 -> 2127 = BIAS(2047) + 80
402 BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2127).addReg(i2)
403 };
Misha Brukman07d45162003-07-15 19:09:43 +0000404
Misha Brukman0897c602003-08-06 16:20:22 +0000405 for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
406 // This is where we save the return address in the LazyResolverMap!!
407 if (i == 9 && F != 0) { // Do this right before the JMPL
408 uint64_t CurrPC = MCE.getCurrentPCValue();
409 TheJITResolver->addFunctionReference(CurrPC, F);
410 // Remember that this is a far call, to subtract appropriate offset later
411 TheJITResolver->addCallFlavor(CurrPC, JITResolver::FarCall);
412 }
Misha Brukman07d45162003-07-15 19:09:43 +0000413
Misha Brukman0897c602003-08-06 16:20:22 +0000414 emitWord(getBinaryCodeForInstr(*BinaryCode[i]));
415 delete BinaryCode[i];
416 }
Misha Brukman07d45162003-07-15 19:09:43 +0000417}
418
419
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000420int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
421 MachineOperand &MO) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000422 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
423 // or things that get fixed up later by the JIT.
424
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000425 if (MO.isVirtualRegister()) {
Misha Brukman33394592003-06-06 03:35:37 +0000426 std::cerr << "ERROR: virtual register found in machine code.\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000427 abort();
428 } else if (MO.isPCRelativeDisp()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000429 DEBUG(std::cerr << "PCRelativeDisp: ");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000430 Value *V = MO.getVRegValue();
431 if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000432 DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000433 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000434 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana2196c12003-06-04 20:01:13 +0000435 } else if (const Constant *C = dyn_cast<Constant>(V)) {
436 if (ConstantMap.find(C) != ConstantMap.end()) {
437 rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukman8f122222003-06-06 00:26:11 +0000438 DEBUG(std::cerr << "const: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000439 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000440 std::cerr << "ERROR: constant not in map:" << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000441 abort();
442 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000443 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
444 // same as MO.isGlobalAddress()
Misha Brukman8f122222003-06-06 00:26:11 +0000445 DEBUG(std::cerr << "GlobalValue: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000446 // external function calls, etc.?
447 if (Function *F = dyn_cast<Function>(GV)) {
Misha Brukman8f122222003-06-06 00:26:11 +0000448 DEBUG(std::cerr << "Function: ");
Misha Brukmana2196c12003-06-04 20:01:13 +0000449 if (F->isExternal()) {
450 // Sparc backend broken: this MO should be `ExternalSymbol'
451 rv = (int64_t)MCE.getGlobalValueAddress(F->getName());
452 } else {
453 rv = (int64_t)MCE.getGlobalValueAddress(F);
454 }
455 if (rv == 0) {
Misha Brukman8f122222003-06-06 00:26:11 +0000456 DEBUG(std::cerr << "not yet generated\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000457 // Function has not yet been code generated!
458 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F);
459 // Delayed resolution...
460 rv = TheJITResolver->getLazyResolver(F);
461 } else {
Misha Brukman8f122222003-06-06 00:26:11 +0000462 DEBUG(std::cerr << "already generated: 0x" << std::hex << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000463 }
464 } else {
Misha Brukman33394592003-06-06 03:35:37 +0000465 rv = (int64_t)MCE.getGlobalValueAddress(GV);
Misha Brukmande07be32003-06-06 04:41:22 +0000466 if (rv == 0) {
467 if (Constant *C = ConstantPointerRef::get(GV)) {
468 if (ConstantMap.find(C) != ConstantMap.end()) {
469 rv = MCE.getConstantPoolEntryAddress(ConstantMap[C]);
470 } else {
Misha Brukman8631ac42003-06-06 09:53:28 +0000471 std::cerr << "Constant: 0x" << std::hex << (intptr_t)C
Misha Brukmande07be32003-06-06 04:41:22 +0000472 << ", " << *V << " not found in ConstantMap!\n";
473 abort();
474 }
475 }
476 }
477 DEBUG(std::cerr << "Global addr: " << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000478 }
479 // The real target of the call is Addr = PC + (rv * 4)
480 // So undo that: give the instruction (Addr - PC) / 4
481 if (MI.getOpcode() == V9::CALL) {
482 int64_t CurrPC = MCE.getCurrentPCValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000483 DEBUG(std::cerr << "rv addr: 0x" << std::hex << rv << "\n"
484 << "curr PC: 0x" << CurrPC << "\n");
Misha Brukman07d45162003-07-15 19:09:43 +0000485 int64_t CallInstTarget = (rv - CurrPC) >> 2;
486 if (CallInstTarget >= (1<<29) || CallInstTarget <= -(1<<29)) {
487 DEBUG(std::cerr << "Making far call!\n");
488 // addresss is out of bounds for the 30-bit call,
489 // make an indirect jump-and-link
490 emitFarCall(rv);
491 // this invalidates the instruction so that the call with an incorrect
492 // address will not be emitted
493 rv = 0;
494 } else {
495 // The call fits into 30 bits, so just return the corrected address
496 rv = CallInstTarget;
Misha Brukmana2196c12003-06-04 20:01:13 +0000497 }
Misha Brukman8f122222003-06-06 00:26:11 +0000498 DEBUG(std::cerr << "returning addr: 0x" << rv << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000499 }
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000500 } else {
501 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
502 abort();
503 }
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000504 } else if (MO.isPhysicalRegister() ||
505 MO.getType() == MachineOperand::MO_CCRegister)
506 {
Misha Brukman9cedd432003-07-03 18:36:47 +0000507 // This is necessary because the Sparc backend doesn't actually lay out
508 // registers in the real fashion -- it skips those that it chooses not to
509 // allocate, i.e. those that are the FP, SP, etc.
Misha Brukman173e2502003-07-14 23:26:03 +0000510 unsigned fakeReg = MO.getAllocatedRegNum();
511 unsigned realRegByClass = getRealRegNum(fakeReg, MI);
512 DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
Misha Brukmandfbfc572003-07-16 20:30:40 +0000513 << realRegByClass << " (LLC: "
514 << TM.getRegInfo().getUnifiedRegName(fakeReg) << ")\n");
Misha Brukman9cedd432003-07-03 18:36:47 +0000515 rv = realRegByClass;
Misha Brukman3de36f52003-05-27 20:07:58 +0000516 } else if (MO.isImmediate()) {
Brian Gaekec3eaa892003-06-02 02:13:26 +0000517 rv = MO.getImmedValue();
Misha Brukman8f122222003-06-06 00:26:11 +0000518 DEBUG(std::cerr << "immed: " << rv << "\n");
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000519 } else if (MO.isGlobalAddress()) {
Misha Brukman8f122222003-06-06 00:26:11 +0000520 DEBUG(std::cerr << "GlobalAddress: not PC-relative\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000521 rv = (int64_t)
522 (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()),
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000523 MI, MO.isPCRelative());
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000524 } else if (MO.isMachineBasicBlock()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000525 // Duplicate code of the above case for VirtualRegister, BasicBlock...
526 // It should really hit this case, but Sparc backend uses VRegs instead
Misha Brukman8f122222003-06-06 00:26:11 +0000527 DEBUG(std::cerr << "Saving reference to MBB\n");
Chris Lattner6856d112003-07-26 23:04:00 +0000528 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000529 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000530 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000531 } else if (MO.isExternalSymbol()) {
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000532 // Sparc backend doesn't generate this (yet...)
533 std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
534 abort();
535 } else if (MO.isFrameIndex()) {
536 // Sparc backend doesn't generate this (yet...)
537 int FrameIndex = MO.getFrameIndex();
538 std::cerr << "ERROR: Frame index unhandled.\n";
539 abort();
540 } else if (MO.isConstantPoolIndex()) {
541 // Sparc backend doesn't generate this (yet...)
542 std::cerr << "ERROR: Constant Pool index unhandled.\n";
543 abort();
Misha Brukman3de36f52003-05-27 20:07:58 +0000544 } else {
Misha Brukmana9f7f6e2003-05-30 20:17:33 +0000545 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000546 abort();
Brian Gaekec3eaa892003-06-02 02:13:26 +0000547 }
548
549 // Finally, deal with the various bitfield-extracting functions that
550 // are used in SPARC assembly. (Some of these make no sense in combination
551 // with some of the above; we'll trust that the instruction selector
552 // will not produce nonsense, and not check for valid combinations here.)
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000553 if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000554 return rv & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000555 } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000556 return (rv >> 10) & 0x03fffff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000557 } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000558 return (rv >> 32) & 0x03ff;
Misha Brukmanf47d9c22003-06-05 20:52:06 +0000559 } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc
Brian Gaekec3eaa892003-06-02 02:13:26 +0000560 return rv >> 42;
561 } else { // (unadorned) val
562 return rv;
Misha Brukman3de36f52003-05-27 20:07:58 +0000563 }
564}
565
566unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
567 Val >>= bit;
568 return (Val & 1);
569}
570
Misha Brukman3de36f52003-05-27 20:07:58 +0000571bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000572 MCE.startFunction(MF);
Misha Brukman8f122222003-06-06 00:26:11 +0000573 DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000574 << ", address: " << "0x" << std::hex
Misha Brukman8f122222003-06-06 00:26:11 +0000575 << (long)MCE.getCurrentPCValue() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000576
Misha Brukmana2196c12003-06-04 20:01:13 +0000577 // The Sparc backend does not use MachineConstantPool;
578 // instead, it has its own constant pool implementation.
579 // We create a new MachineConstantPool here to be compatible with the emitter.
580 MachineConstantPool MCP;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000581 const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues();
582 for (hash_set<const Constant*>::const_iterator I = pool.begin(),
583 E = pool.end(); I != E; ++I)
584 {
Misha Brukmana2196c12003-06-04 20:01:13 +0000585 Constant *C = (Constant*)*I;
586 unsigned idx = MCP.getConstantPoolIndex(C);
Misha Brukman9cedd432003-07-03 18:36:47 +0000587 DEBUG(std::cerr << "Constant[" << idx << "] = 0x" << (intptr_t)C << "\n");
Misha Brukmana2196c12003-06-04 20:01:13 +0000588 ConstantMap[C] = idx;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000589 }
Misha Brukmana2196c12003-06-04 20:01:13 +0000590 MCE.emitConstantPool(&MCP);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000591
Misha Brukman3de36f52003-05-27 20:07:58 +0000592 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
593 emitBasicBlock(*I);
Misha Brukmana2196c12003-06-04 20:01:13 +0000594 MCE.finishFunction(MF);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000595
Misha Brukman9cedd432003-07-03 18:36:47 +0000596 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000597 ConstantMap.clear();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000598
599 // Resolve branches to BasicBlocks for the entire function
600 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
601 long Location = BBLocations[BBRefs[i].first];
602 unsigned *Ref = BBRefs[i].second.first;
603 MachineInstr *MI = BBRefs[i].second.second;
Misha Brukman9cedd432003-07-03 18:36:47 +0000604 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
605 << " in instr: " << std::dec << *MI);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000606 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
607 MachineOperand &op = MI->getOperand(ii);
608 if (op.isPCRelativeDisp()) {
609 // the instruction's branch target is made such that it branches to
Misha Brukman9cedd432003-07-03 18:36:47 +0000610 // PC + (branchTarget * 4), so undo that arithmetic here:
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000611 // Location is the target of the branch
612 // Ref is the location of the instruction, and hence the PC
Misha Brukman9cedd432003-07-03 18:36:47 +0000613 int64_t branchTarget = (Location - (long)Ref) >> 2;
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000614 // Save the flags.
615 bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;
616 if (op.opLoBits32()) { loBits32=true; }
617 if (op.opHiBits32()) { hiBits32=true; }
618 if (op.opLoBits64()) { loBits64=true; }
619 if (op.opHiBits64()) { hiBits64=true; }
620 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
621 branchTarget);
622 if (loBits32) { MI->setOperandLo32(ii); }
623 else if (hiBits32) { MI->setOperandHi32(ii); }
624 else if (loBits64) { MI->setOperandLo64(ii); }
625 else if (hiBits64) { MI->setOperandHi64(ii); }
Misha Brukman8f122222003-06-06 00:26:11 +0000626 DEBUG(std::cerr << "Rewrote BB ref: ");
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000627 unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
628 *Ref = fixedInstr;
629 break;
630 }
631 }
632 }
633 BBRefs.clear();
634 BBLocations.clear();
635
Misha Brukman3de36f52003-05-27 20:07:58 +0000636 return false;
637}
638
639void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukman0d603452003-05-27 22:41:44 +0000640 currBB = MBB.getBasicBlock();
Misha Brukmana2196c12003-06-04 20:01:13 +0000641 BBLocations[currBB] = MCE.getCurrentPCValue();
Misha Brukman07d45162003-07-15 19:09:43 +0000642 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
643 unsigned binCode = getBinaryCodeForInstr(**I);
644 if (binCode == (1 << 30)) {
645 // this is an invalid call: the addr is out of bounds. that means a code
646 // sequence has already been emitted, and this is a no-op
647 DEBUG(std::cerr << "Call supressed: already emitted far call.\n");
648 } else {
649 emitWord(binCode);
650 }
651 }
Misha Brukman3de36f52003-05-27 20:07:58 +0000652}
653
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000654void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI,
655 bool isPCRelative)
656{
657 if (isPCRelative) { // must be a call, this is a major hack!
658 // Try looking up the function to see if it is already compiled!
Misha Brukmana2196c12003-06-04 20:01:13 +0000659 if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) {
660 intptr_t CurByte = MCE.getCurrentPCValue();
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000661 // The real target of the call is Addr = PC + (target * 4)
662 // CurByte is the PC, Addr we just received
663 return (void*) (((long)Addr - (long)CurByte) >> 2);
664 } else {
665 if (Function *F = dyn_cast<Function>(V)) {
666 // Function has not yet been code generated!
Misha Brukmana2196c12003-06-04 20:01:13 +0000667 TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(),
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000668 cast<Function>(V));
669 // Delayed resolution...
Misha Brukmaneaaf8ad2003-06-02 05:24:46 +0000670 return
671 (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V));
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000672
673 } else if (Constant *C = ConstantPointerRef::get(V)) {
674 if (ConstantMap.find(C) != ConstantMap.end()) {
Misha Brukmana2196c12003-06-04 20:01:13 +0000675 return (void*)
676 (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000677 } else {
678 std::cerr << "Constant: 0x" << std::hex << &*C << std::dec
679 << ", " << *V << " not found in ConstantMap!\n";
680 abort();
681 }
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000682 } else {
683 std::cerr << "Unhandled global: " << *V << "\n";
684 abort();
685 }
686 }
687 } else {
Misha Brukmana2196c12003-06-04 20:01:13 +0000688 return (void*)(intptr_t)MCE.getGlobalValueAddress(V);
Misha Brukmanf86aaa82003-06-02 04:12:39 +0000689 }
690}
691
692
Misha Brukman3de36f52003-05-27 20:07:58 +0000693#include "SparcV9CodeEmitter.inc"