1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format SPARC assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "SparcInstrInfo.h"
18 #include "SparcTargetMachine.h"
19 #include "MCTargetDesc/SparcBaseInfo.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineInstr.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/TargetRegistry.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/Mangler.h"
33 class SparcAsmPrinter : public AsmPrinter {
35 explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
36 : AsmPrinter(TM, Streamer) {}
38 virtual const char *getPassName() const {
39 return "Sparc Assembly Printer";
42 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
43 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
44 const char *Modifier = 0);
45 void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
47 virtual void EmitFunctionBodyStart();
48 virtual void EmitInstruction(const MachineInstr *MI) {
50 raw_svector_ostream OS(Str);
51 printInstruction(MI, OS);
52 OutStreamer.EmitRawText(OS.str());
54 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
55 static const char *getRegisterName(unsigned RegNo);
57 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
58 unsigned AsmVariant, const char *ExtraCode,
60 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
61 unsigned AsmVariant, const char *ExtraCode,
64 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
66 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
68 void EmitGlobalRegisterDecl(unsigned reg) {
70 raw_svector_ostream OS(Str);
72 << "%" << StringRef(getRegisterName(reg)).lower()
74 << ((reg == SP::G6 || reg == SP::G7)? "#ignore" : "#scratch");
75 OutStreamer.EmitRawText(OS.str());
79 } // end of anonymous namespace
81 #include "SparcGenAsmWriter.inc"
83 void SparcAsmPrinter::EmitFunctionBodyStart() {
84 if (!TM.getSubtarget<SparcSubtarget>().is64Bit())
87 const MachineRegisterInfo &MRI = MF->getRegInfo();
88 const unsigned globalRegs[] = { SP::G2, SP::G3, SP::G6, SP::G7, 0 };
89 for (unsigned i = 0; globalRegs[i] != 0; ++i) {
90 unsigned reg = globalRegs[i];
91 if (!MRI.isPhysRegUsed(reg))
93 EmitGlobalRegisterDecl(reg);
97 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
99 const MachineOperand &MO = MI->getOperand (opNum);
100 unsigned TF = MO.getTargetFlags();
102 // Verify the target flags.
103 if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
104 if (MI->getOpcode() == SP::CALL)
105 assert(TF == SPII::MO_NO_FLAG &&
106 "Cannot handle target flags on call address");
107 else if (MI->getOpcode() == SP::SETHIi)
108 assert((TF == SPII::MO_HI || TF == SPII::MO_H44 || TF == SPII::MO_HH
109 || TF == SPII::MO_TLS_GD_HI22
110 || TF == SPII::MO_TLS_LDM_HI22
111 || TF == SPII::MO_TLS_LDO_HIX22
112 || TF == SPII::MO_TLS_IE_HI22
113 || TF == SPII::MO_TLS_LE_HIX22) &&
114 "Invalid target flags for address operand on sethi");
115 else if (MI->getOpcode() == SP::TLS_CALL)
116 assert((TF == SPII::MO_NO_FLAG
117 || TF == SPII::MO_TLS_GD_CALL
118 || TF == SPII::MO_TLS_LDM_CALL) &&
119 "Cannot handle target flags on tls call address");
120 else if (MI->getOpcode() == SP::TLS_ADDrr)
121 assert((TF == SPII::MO_TLS_GD_ADD || TF == SPII::MO_TLS_LDM_ADD
122 || TF == SPII::MO_TLS_LDO_ADD || TF == SPII::MO_TLS_IE_ADD) &&
123 "Cannot handle target flags on add for TLS");
124 else if (MI->getOpcode() == SP::TLS_LDrr)
125 assert(TF == SPII::MO_TLS_IE_LD &&
126 "Cannot handle target flags on ld for TLS");
127 else if (MI->getOpcode() == SP::TLS_LDXrr)
128 assert(TF == SPII::MO_TLS_IE_LDX &&
129 "Cannot handle target flags on ldx for TLS");
130 else if (MI->getOpcode() == SP::XORri)
131 assert((TF == SPII::MO_TLS_LDO_LOX10 || TF == SPII::MO_TLS_LE_LOX10) &&
132 "Cannot handle target flags on xor for TLS");
134 assert((TF == SPII::MO_LO || TF == SPII::MO_M44 || TF == SPII::MO_L44
136 || TF == SPII::MO_TLS_GD_LO10
137 || TF == SPII::MO_TLS_LDM_LO10
138 || TF == SPII::MO_TLS_IE_LO10 ) &&
139 "Invalid target flags for small address operand");
143 bool CloseParen = true;
146 llvm_unreachable("Unknown target flags on operand");
147 case SPII::MO_NO_FLAG:
150 case SPII::MO_LO: O << "%lo("; break;
151 case SPII::MO_HI: O << "%hi("; break;
152 case SPII::MO_H44: O << "%h44("; break;
153 case SPII::MO_M44: O << "%m44("; break;
154 case SPII::MO_L44: O << "%l44("; break;
155 case SPII::MO_HH: O << "%hh("; break;
156 case SPII::MO_HM: O << "%hm("; break;
157 case SPII::MO_TLS_GD_HI22: O << "%tgd_hi22("; break;
158 case SPII::MO_TLS_GD_LO10: O << "%tgd_lo10("; break;
159 case SPII::MO_TLS_GD_ADD: O << "%tgd_add("; break;
160 case SPII::MO_TLS_GD_CALL: O << "%tgd_call("; break;
161 case SPII::MO_TLS_LDM_HI22: O << "%tldm_hi22("; break;
162 case SPII::MO_TLS_LDM_LO10: O << "%tldm_lo10("; break;
163 case SPII::MO_TLS_LDM_ADD: O << "%tldm_add("; break;
164 case SPII::MO_TLS_LDM_CALL: O << "%tldm_call("; break;
165 case SPII::MO_TLS_LDO_HIX22: O << "%tldo_hix22("; break;
166 case SPII::MO_TLS_LDO_LOX10: O << "%tldo_lox10("; break;
167 case SPII::MO_TLS_LDO_ADD: O << "%tldo_add("; break;
168 case SPII::MO_TLS_IE_HI22: O << "%tie_hi22("; break;
169 case SPII::MO_TLS_IE_LO10: O << "%tie_lo10("; break;
170 case SPII::MO_TLS_IE_LD: O << "%tie_ld("; break;
171 case SPII::MO_TLS_IE_LDX: O << "%tie_ldx("; break;
172 case SPII::MO_TLS_IE_ADD: O << "%tie_add("; break;
173 case SPII::MO_TLS_LE_HIX22: O << "%tle_hix22("; break;
174 case SPII::MO_TLS_LE_LOX10: O << "%tle_lox10("; break;
177 switch (MO.getType()) {
178 case MachineOperand::MO_Register:
179 O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
182 case MachineOperand::MO_Immediate:
183 O << (int)MO.getImm();
185 case MachineOperand::MO_MachineBasicBlock:
186 O << *MO.getMBB()->getSymbol();
188 case MachineOperand::MO_GlobalAddress:
189 O << *getSymbol(MO.getGlobal());
191 case MachineOperand::MO_BlockAddress:
192 O << GetBlockAddressSymbol(MO.getBlockAddress())->getName();
194 case MachineOperand::MO_ExternalSymbol:
195 O << MO.getSymbolName();
197 case MachineOperand::MO_ConstantPoolIndex:
198 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
202 llvm_unreachable("<unknown operand type>");
204 if (CloseParen) O << ")";
207 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
208 raw_ostream &O, const char *Modifier) {
209 printOperand(MI, opNum, O);
211 // If this is an ADD operand, emit it like normal operands.
212 if (Modifier && !strcmp(Modifier, "arith")) {
214 printOperand(MI, opNum+1, O);
218 if (MI->getOperand(opNum+1).isReg() &&
219 MI->getOperand(opNum+1).getReg() == SP::G0)
220 return; // don't print "+%g0"
221 if (MI->getOperand(opNum+1).isImm() &&
222 MI->getOperand(opNum+1).getImm() == 0)
223 return; // don't print "+0"
226 printOperand(MI, opNum+1, O);
229 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
231 std::string operand = "";
232 const MachineOperand &MO = MI->getOperand(opNum);
233 switch (MO.getType()) {
234 default: llvm_unreachable("Operand is not a register");
235 case MachineOperand::MO_Register:
236 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
237 "Operand is not a physical register ");
238 assert(MO.getReg() != SP::O7 &&
239 "%o7 is assigned as destination for getpcx!");
240 operand = "%" + StringRef(getRegisterName(MO.getReg())).lower();
244 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
245 unsigned bbNum = MI->getParent()->getNumber();
247 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
248 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
251 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
252 << ")), " << operand << '\n' ;
254 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
255 O << "\tor\t" << operand
256 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
257 << ")), " << operand << '\n';
258 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
263 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
265 int CC = (int)MI->getOperand(opNum).getImm();
266 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
269 /// PrintAsmOperand - Print out an operand for an inline asm expression.
271 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
273 const char *ExtraCode,
275 if (ExtraCode && ExtraCode[0]) {
276 if (ExtraCode[1] != 0) return true; // Unknown modifier.
278 switch (ExtraCode[0]) {
280 // See if this is a generic print operand
281 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
287 printOperand(MI, OpNo, O);
292 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
293 unsigned OpNo, unsigned AsmVariant,
294 const char *ExtraCode,
296 if (ExtraCode && ExtraCode[0])
297 return true; // Unknown modifier
300 printMemOperand(MI, OpNo, O);
306 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
307 /// exactly one predecessor and the control transfer mechanism between
308 /// the predecessor and this block is a fall-through.
310 /// This overrides AsmPrinter's implementation to handle delay slots.
311 bool SparcAsmPrinter::
312 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
313 // If this is a landing pad, it isn't a fall through. If it has no preds,
314 // then nothing falls through to it.
315 if (MBB->isLandingPad() || MBB->pred_empty())
318 // If there isn't exactly one predecessor, it can't be a fall through.
319 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
321 if (PI2 != MBB->pred_end())
324 // The predecessor has to be immediately before this block.
325 const MachineBasicBlock *Pred = *PI;
327 if (!Pred->isLayoutSuccessor(MBB))
330 // Check if the last terminator is an unconditional branch.
331 MachineBasicBlock::const_iterator I = Pred->end();
332 while (I != Pred->begin() && !(--I)->isTerminator())
334 return I == Pred->end() || !I->isBarrier();
337 // Force static initialization.
338 extern "C" void LLVMInitializeSparcAsmPrinter() {
339 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
340 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);