Squelch a bogus warning
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9CodeEmitter.cpp
1 //===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // SPARC-specific backend for emitting machine code to memory.
11 //
12 // This module also contains the code for lazily resolving the targets
13 // of call instructions, including the callback used to redirect calls
14 // to functions for which the code has not yet been generated into the
15 // JIT compiler.
16 //
17 // This file #includes SparcV9CodeEmitter.inc, which contains the code
18 // for getBinaryCodeForInstr(), a method that converts a MachineInstr
19 // into the corresponding binary machine code word.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "llvm/Constants.h"
24 #include "llvm/Function.h"
25 #include "llvm/GlobalVariable.h"
26 #include "llvm/PassManager.h"
27 #include "llvm/CodeGen/MachineCodeEmitter.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Support/Debug.h"
34 #include "SparcV9Internals.h"
35 #include "SparcV9TargetMachine.h"
36 #include "SparcV9RegInfo.h"
37 #include "SparcV9CodeEmitter.h"
38 #include "SparcV9Relocations.h"
39 #include "MachineFunctionInfo.h"
40 using namespace llvm;
41
42 bool SparcV9TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
43                                                       MachineCodeEmitter &MCE) {
44   PM.add(new SparcV9CodeEmitter(*this, MCE));
45   PM.add(createSparcV9MachineCodeDestructionPass());
46   return false;
47 }
48
49 SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
50                                        MachineCodeEmitter &M): TM(tm), MCE(M) {}
51
52 void SparcV9CodeEmitter::emitWord(unsigned Val) {
53   MCE.emitWord(Val);
54 }
55
56 unsigned 
57 SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
58                                   MachineInstr &MI) {
59   const SparcV9RegInfo &RI = *TM.getRegInfo();
60   unsigned regClass, regType = RI.getRegType(fakeReg);
61   // At least map fakeReg into its class
62   fakeReg = RI.getClassRegNum(fakeReg, regClass);
63
64   switch (regClass) {
65   case SparcV9RegInfo::IntRegClassID: {
66     // SparcV9 manual, p31
67     static const unsigned IntRegMap[] = {
68       // "o0", "o1", "o2", "o3", "o4", "o5",       "o7",
69       8, 9, 10, 11, 12, 13, 15,
70       // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
71       16, 17, 18, 19, 20, 21, 22, 23,
72       // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7",
73       24, 25, 26, 27, 28, 29, 30, 31,
74       // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", 
75       0, 1, 2, 3, 4, 5, 6, 7,
76       // "o6"
77       14
78     }; 
79  
80     return IntRegMap[fakeReg];
81     break;
82   }
83   case SparcV9RegInfo::FloatRegClassID: {
84     DEBUG(std::cerr << "FP reg: " << fakeReg << "\n");
85     if (regType == SparcV9RegInfo::FPSingleRegType) {
86       // only numbered 0-31, hence can already fit into 5 bits (and 6)
87       DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n");
88     } else if (regType == SparcV9RegInfo::FPDoubleRegType) {
89       // FIXME: This assumes that we only have 5-bit register fields!
90       // From SparcV9 Manual, page 40.
91       // The bit layout becomes: b[4], b[3], b[2], b[1], b[5]
92       fakeReg |= (fakeReg >> 5) & 1;
93       fakeReg &= 0x1f;
94       DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");      
95     }
96     return fakeReg;
97   }
98   case SparcV9RegInfo::IntCCRegClassID: {
99     /*                                   xcc, icc, ccr */
100     static const unsigned IntCCReg[] = {  6,   4,   2 };
101     
102     assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0])
103              && "CC register out of bounds for IntCCReg map");      
104     DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n");
105     return IntCCReg[fakeReg];
106   }
107   case SparcV9RegInfo::FloatCCRegClassID: {
108     /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */
109     DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n");
110     return fakeReg;
111   }
112   case SparcV9RegInfo::SpecialRegClassID: {
113     // Currently only "special" reg is %fsr, which is encoded as 1 in
114     // instructions and 0 in SparcV9SpecialRegClass.
115     static const unsigned SpecialReg[] = {  1 };
116     assert(fakeReg < sizeof(SpecialReg)/sizeof(SpecialReg[0])
117              && "Special register out of bounds for SpecialReg map");      
118     DEBUG(std::cerr << "Special reg: " << SpecialReg[fakeReg] << "\n");
119     return SpecialReg[fakeReg];
120   }
121   default:
122     assert(0 && "Invalid unified register number in getRealRegNum");
123     return fakeReg;
124   }
125 }
126
127
128
129 int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI,
130                                               MachineOperand &MO) {
131   int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
132                   // or things that get fixed up later by the JIT.
133   if (MO.isPCRelativeDisp() || MO.isGlobalAddress()) {
134     DEBUG(std::cerr << "PCRelativeDisp: ");
135     Value *V = MO.getVRegValue();
136     if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
137       DEBUG(std::cerr << "Saving reference to BB (VReg)\n");
138       unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
139       BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
140     } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
141       // The real target of the branch is CI = PC + (rv * 4)
142       // So undo that: give the instruction (CI - PC) / 4
143       rv = (CI->getRawValue() - MCE.getCurrentPCValue()) / 4;
144     } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
145       unsigned Reloc = 0;
146       if (MI.getOpcode() == V9::CALL) {
147         Reloc = V9::reloc_pcrel_call;
148       } else if (MI.getOpcode() == V9::SETHI) {
149         if (MO.isHiBits64())
150           Reloc = V9::reloc_sethi_hh;
151         else if (MO.isHiBits32())
152           Reloc = V9::reloc_sethi_lm;
153         else
154           assert(0 && "Unknown relocation!");
155       } else if (MI.getOpcode() == V9::ORi) {
156         if (MO.isLoBits32())
157           Reloc = V9::reloc_or_lo;
158         else if (MO.isLoBits64())
159           Reloc = V9::reloc_or_hm;
160         else
161           assert(0 && "Unknown relocation!");
162       } else {
163         assert(0 && "Unknown relocation!");
164       }
165
166       MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(), Reloc, GV));
167       rv = 0;
168     } else {
169       std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
170       abort();
171     }
172   } else if (MO.isRegister() || MO.getType() == MachineOperand::MO_CCRegister)
173   {
174     // This is necessary because the SparcV9 backend doesn't actually lay out
175     // registers in the real fashion -- it skips those that it chooses not to
176     // allocate, i.e. those that are the FP, SP, etc.
177     unsigned fakeReg = MO.getReg();
178     unsigned realRegByClass = getRealRegNum(fakeReg, MI);
179     DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => "
180                     << realRegByClass << " (LLC: " 
181                     << TM.getRegInfo()->getUnifiedRegName(fakeReg) << ")\n");
182     rv = realRegByClass;
183   } else if (MO.isImmediate()) {
184     rv = MO.getImmedValue();
185     DEBUG(std::cerr << "immed: " << rv << "\n");
186   } else if (MO.isMachineBasicBlock()) {
187     // Duplicate code of the above case for VirtualRegister, BasicBlock... 
188     // It should really hit this case, but SparcV9 backend uses VRegs instead
189     DEBUG(std::cerr << "Saving reference to MBB\n");
190     const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
191     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
192     BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
193   } else if (MO.isExternalSymbol()) {
194     // SparcV9 backend doesn't generate this (yet...)
195     std::cerr << "ERROR: External symbol unhandled: " << MO << "\n";
196     abort();
197   } else if (MO.isFrameIndex()) {
198     // SparcV9 backend doesn't generate this (yet...)
199     int FrameIndex = MO.getFrameIndex();
200     std::cerr << "ERROR: Frame index unhandled.\n";
201     abort();
202   } else if (MO.isConstantPoolIndex()) {
203     unsigned Index = MO.getConstantPoolIndex();
204     rv = MCE.getConstantPoolEntryAddress(Index);
205   } else {
206     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
207     abort();
208   }
209
210   // Finally, deal with the various bitfield-extracting functions that
211   // are used in SPARC assembly. (Some of these make no sense in combination
212   // with some of the above; we'll trust that the instruction selector
213   // will not produce nonsense, and not check for valid combinations here.)
214   if (MO.isLoBits32()) {          // %lo(val) == %lo() in SparcV9 ABI doc
215     return rv & 0x03ff;
216   } else if (MO.isHiBits32()) {   // %lm(val) == %hi() in SparcV9 ABI doc
217     return (rv >> 10) & 0x03fffff;
218   } else if (MO.isLoBits64()) {   // %hm(val) == %ulo() in SparcV9 ABI doc
219     return (rv >> 32) & 0x03ff;
220   } else if (MO.isHiBits64()) {   // %hh(val) == %uhi() in SparcV9 ABI doc
221     return rv >> 42;
222   } else {                        // (unadorned) val
223     return rv;
224   }
225 }
226
227 unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
228   Val >>= bit;
229   return (Val & 1);
230 }
231
232 bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
233   MCE.startFunction(MF);
234   DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName()
235             << ", address: " << "0x" << std::hex 
236             << (long)MCE.getCurrentPCValue() << "\n");
237
238   MCE.emitConstantPool(MF.getConstantPool());
239   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
240     emitBasicBlock(*I);
241   MCE.finishFunction(MF);
242
243   DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
244
245   // Resolve branches to BasicBlocks for the entire function
246   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
247     long Location = BBLocations[BBRefs[i].first];
248     unsigned *Ref = BBRefs[i].second.first;
249     MachineInstr *MI = BBRefs[i].second.second;
250     DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
251                     << " in instr: " << std::dec << *MI);
252     for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
253       MachineOperand &op = MI->getOperand(ii);
254       if (op.isPCRelativeDisp()) {
255         // the instruction's branch target is made such that it branches to
256         // PC + (branchTarget * 4), so undo that arithmetic here:
257         // Location is the target of the branch
258         // Ref is the location of the instruction, and hence the PC
259         int64_t branchTarget = (Location - (long)Ref) >> 2;
260         // Save the flags.
261         bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false;   
262         if (op.isLoBits32()) { loBits32=true; }
263         if (op.isHiBits32()) { hiBits32=true; }
264         if (op.isLoBits64()) { loBits64=true; }
265         if (op.isHiBits64()) { hiBits64=true; }
266         MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
267                                    branchTarget);
268         if (loBits32) { MI->getOperand(ii).markLo32(); }
269         else if (hiBits32) { MI->getOperand(ii).markHi32(); }
270         else if (loBits64) { MI->getOperand(ii).markLo64(); }
271         else if (hiBits64) { MI->getOperand(ii).markHi64(); }
272         DEBUG(std::cerr << "Rewrote BB ref: ");
273         unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI);
274         MCE.emitWordAt (fixedInstr, Ref);
275         break;
276       }
277     }
278   }
279   BBRefs.clear();
280   BBLocations.clear();
281
282   return false;
283 }
284
285 void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
286   currBB = MBB.getBasicBlock();
287   BBLocations[currBB] = MCE.getCurrentPCValue();
288   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I)
289     if (I->getOpcode() != V9::RDCCR) {
290       emitWord(getBinaryCodeForInstr(*I));
291     } else {
292       // FIXME: The tblgen produced code emitter cannot deal with the fact that
293       // machine operand #0 of the RDCCR instruction should be ignored.  This is
294       // really a bug in the representation of the RDCCR instruction (which has
295       // no need to explicitly represent the CCR dest), but we hack around it
296       // here.
297       unsigned RegNo = getMachineOpValue(*I, I->getOperand(1));
298       RegNo &= (1<<5)-1;
299       emitWord((RegNo << 25) | 2168487936U);
300     }
301 }
302
303 #include "SparcV9CodeEmitter.inc"
304