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/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/Support/TargetRegistry.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/Mangler.h"
32 class SparcAsmPrinter : public AsmPrinter {
34 explicit SparcAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
35 : AsmPrinter(TM, Streamer) {}
37 virtual const char *getPassName() const {
38 return "Sparc Assembly Printer";
41 void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
42 void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
43 const char *Modifier = 0);
44 void printCCOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
46 virtual void EmitInstruction(const MachineInstr *MI) {
48 raw_svector_ostream OS(Str);
49 printInstruction(MI, OS);
50 OutStreamer.EmitRawText(OS.str());
52 void printInstruction(const MachineInstr *MI, raw_ostream &OS);// autogen'd.
53 static const char *getRegisterName(unsigned RegNo);
55 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode,
58 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
59 unsigned AsmVariant, const char *ExtraCode,
62 bool printGetPCX(const MachineInstr *MI, unsigned OpNo, raw_ostream &OS);
64 virtual bool isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB)
67 virtual MachineLocation getDebugValueLocation(const MachineInstr *MI) const;
69 } // end of anonymous namespace
71 #include "SparcGenAsmWriter.inc"
73 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
75 const MachineOperand &MO = MI->getOperand (opNum);
76 unsigned TF = MO.getTargetFlags();
78 // Verify the target flags.
79 if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
80 if (MI->getOpcode() == SP::CALL)
81 assert(TF == SPII::MO_NO_FLAG &&
82 "Cannot handle target flags on call address");
83 else if (MI->getOpcode() == SP::SETHIi)
84 assert((TF == SPII::MO_HI || TF == SPII::MO_H44 || TF == SPII::MO_HH) &&
85 "Invalid target flags for address operand on sethi");
87 assert((TF == SPII::MO_LO || TF == SPII::MO_M44 || TF == SPII::MO_L44 ||
89 "Invalid target flags for small address operand");
93 bool CloseParen = true;
96 llvm_unreachable("Unknown target flags on operand");
97 case SPII::MO_NO_FLAG:
100 case SPII::MO_LO: O << "%lo("; break;
101 case SPII::MO_HI: O << "%hi("; break;
102 case SPII::MO_H44: O << "%h44("; break;
103 case SPII::MO_M44: O << "%m44("; break;
104 case SPII::MO_L44: O << "%l44("; break;
105 case SPII::MO_HH: O << "%hh("; break;
106 case SPII::MO_HM: O << "%hm("; break;
109 switch (MO.getType()) {
110 case MachineOperand::MO_Register:
111 O << "%" << StringRef(getRegisterName(MO.getReg())).lower();
114 case MachineOperand::MO_Immediate:
115 O << (int)MO.getImm();
117 case MachineOperand::MO_MachineBasicBlock:
118 O << *MO.getMBB()->getSymbol();
120 case MachineOperand::MO_GlobalAddress:
121 O << *Mang->getSymbol(MO.getGlobal());
123 case MachineOperand::MO_BlockAddress:
124 O << GetBlockAddressSymbol(MO.getBlockAddress())->getName();
126 case MachineOperand::MO_ExternalSymbol:
127 O << MO.getSymbolName();
129 case MachineOperand::MO_ConstantPoolIndex:
130 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
134 llvm_unreachable("<unknown operand type>");
136 if (CloseParen) O << ")";
139 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
140 raw_ostream &O, const char *Modifier) {
141 printOperand(MI, opNum, O);
143 // If this is an ADD operand, emit it like normal operands.
144 if (Modifier && !strcmp(Modifier, "arith")) {
146 printOperand(MI, opNum+1, O);
150 if (MI->getOperand(opNum+1).isReg() &&
151 MI->getOperand(opNum+1).getReg() == SP::G0)
152 return; // don't print "+%g0"
153 if (MI->getOperand(opNum+1).isImm() &&
154 MI->getOperand(opNum+1).getImm() == 0)
155 return; // don't print "+0"
158 printOperand(MI, opNum+1, O);
161 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum,
163 std::string operand = "";
164 const MachineOperand &MO = MI->getOperand(opNum);
165 switch (MO.getType()) {
166 default: llvm_unreachable("Operand is not a register");
167 case MachineOperand::MO_Register:
168 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
169 "Operand is not a physical register ");
170 assert(MO.getReg() != SP::O7 &&
171 "%o7 is assigned as destination for getpcx!");
172 operand = "%" + StringRef(getRegisterName(MO.getReg())).lower();
176 unsigned mfNum = MI->getParent()->getParent()->getFunctionNumber();
177 unsigned bbNum = MI->getParent()->getNumber();
179 O << '\n' << ".LLGETPCH" << mfNum << '_' << bbNum << ":\n";
180 O << "\tcall\t.LLGETPC" << mfNum << '_' << bbNum << '\n' ;
183 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
184 << ")), " << operand << '\n' ;
186 O << ".LLGETPC" << mfNum << '_' << bbNum << ":\n" ;
187 O << "\tor\t" << operand
188 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << mfNum << '_' << bbNum
189 << ")), " << operand << '\n';
190 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
195 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum,
197 int CC = (int)MI->getOperand(opNum).getImm();
198 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
201 /// PrintAsmOperand - Print out an operand for an inline asm expression.
203 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
205 const char *ExtraCode,
207 if (ExtraCode && ExtraCode[0]) {
208 if (ExtraCode[1] != 0) return true; // Unknown modifier.
210 switch (ExtraCode[0]) {
212 // See if this is a generic print operand
213 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
219 printOperand(MI, OpNo, O);
224 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
225 unsigned OpNo, unsigned AsmVariant,
226 const char *ExtraCode,
228 if (ExtraCode && ExtraCode[0])
229 return true; // Unknown modifier
232 printMemOperand(MI, OpNo, O);
238 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
239 /// exactly one predecessor and the control transfer mechanism between
240 /// the predecessor and this block is a fall-through.
242 /// This overrides AsmPrinter's implementation to handle delay slots.
243 bool SparcAsmPrinter::
244 isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
245 // If this is a landing pad, it isn't a fall through. If it has no preds,
246 // then nothing falls through to it.
247 if (MBB->isLandingPad() || MBB->pred_empty())
250 // If there isn't exactly one predecessor, it can't be a fall through.
251 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
253 if (PI2 != MBB->pred_end())
256 // The predecessor has to be immediately before this block.
257 const MachineBasicBlock *Pred = *PI;
259 if (!Pred->isLayoutSuccessor(MBB))
262 // Check if the last terminator is an unconditional branch.
263 MachineBasicBlock::const_iterator I = Pred->end();
264 while (I != Pred->begin() && !(--I)->isTerminator())
266 return I == Pred->end() || !I->isBarrier();
269 MachineLocation SparcAsmPrinter::
270 getDebugValueLocation(const MachineInstr *MI) const {
271 assert(MI->getNumOperands() == 4 && "Invalid number of operands!");
272 assert(MI->getOperand(0).isReg() && MI->getOperand(1).isImm() &&
273 "Unexpected MachineOperand types");
274 return MachineLocation(MI->getOperand(0).getReg(),
275 MI->getOperand(1).getImm());
278 // Force static initialization.
279 extern "C" void LLVMInitializeSparcAsmPrinter() {
280 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
281 RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);