1 //===-- SparcV9CodeEmitter.cpp --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // SPARC-specific backend for emitting machine code to memory.
12 // This module also contains the code for lazily resolving the targets of call
13 // instructions, including the callback used to redirect calls to functions for
14 // which the code has not yet been generated into the JIT compiler.
16 // This file #includes SparcV9GenCodeEmitter.inc, which contains the code for
17 // getBinaryCodeForInstr(), a method that converts a MachineInstr into the
18 // corresponding binary machine code word.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Constants.h"
23 #include "llvm/Function.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/CodeGen/MachineCodeEmitter.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Support/Debug.h"
33 #include "SparcV9Internals.h"
34 #include "SparcV9TargetMachine.h"
35 #include "SparcV9RegInfo.h"
36 #include "SparcV9CodeEmitter.h"
37 #include "SparcV9Relocations.h"
38 #include "MachineFunctionInfo.h"
42 bool SparcV9TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
43 MachineCodeEmitter &MCE) {
44 PM.add(new SparcV9CodeEmitter(*this, MCE));
45 PM.add(createSparcV9MachineCodeDestructionPass());
49 SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm,
50 MachineCodeEmitter &M): TM(tm), MCE(M) {}
52 void SparcV9CodeEmitter::emitWord(unsigned Val) {
57 SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg,
59 const SparcV9RegInfo &RI = *TM.getRegInfo();
60 unsigned regClass = 0, regType = RI.getRegType(fakeReg);
61 // At least map fakeReg into its class
62 fakeReg = RI.getClassRegNum(fakeReg, 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,
80 return IntRegMap[fakeReg];
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;
94 DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n");
98 case SparcV9RegInfo::IntCCRegClassID: {
100 static const unsigned IntCCReg[] = { 6, 4, 2 };
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];
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");
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];
122 assert(0 && "Invalid unified register number in getRealRegNum");
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)) {
146 if (MI.getOpcode() == V9::CALL) {
147 Reloc = V9::reloc_pcrel_call;
148 } else if (MI.getOpcode() == V9::SETHI) {
150 Reloc = V9::reloc_sethi_hh;
151 else if (MO.isHiBits32())
152 Reloc = V9::reloc_sethi_lm;
154 assert(0 && "Unknown relocation!");
155 } else if (MI.getOpcode() == V9::ORi) {
157 Reloc = V9::reloc_or_lo;
158 else if (MO.isLoBits64())
159 Reloc = V9::reloc_or_hm;
161 assert(0 && "Unknown relocation!");
163 assert(0 && "Unknown relocation!");
166 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(), Reloc, GV));
169 std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n";
172 } else if (MO.isRegister() || MO.getType() == MachineOperand::MO_CCRegister)
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");
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";
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";
202 } else if (MO.isConstantPoolIndex()) {
203 unsigned Index = MO.getConstantPoolIndex();
204 rv = MCE.getConstantPoolEntryAddress(Index);
206 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
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
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
222 } else { // (unadorned) val
227 unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) {
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");
238 MCE.emitConstantPool(MF.getConstantPool());
239 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
241 MCE.finishFunction(MF);
243 DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n");
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;
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,
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);
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));
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
297 unsigned RegNo = getMachineOpValue(*I, I->getOperand(1));
299 emitWord((RegNo << 25) | 2168487936U);
303 #include "SparcV9GenCodeEmitter.inc"