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 "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/MC/MCAsmInfo.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetRegistry.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/ADT/StringExtras.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/MathExtras.h"
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46 class SparcAsmPrinter : public AsmPrinter {
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
55 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56 const MCAsmInfo *T, bool V)
57 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
59 virtual const char *getPassName() const {
60 return "Sparc Assembly Printer";
63 void printOperand(const MachineInstr *MI, int opNum);
64 void printMemOperand(const MachineInstr *MI, int opNum,
65 const char *Modifier = 0);
66 void printCCOperand(const MachineInstr *MI, int opNum);
68 void printInstruction(const MachineInstr *MI); // autogenerated.
69 static const char *getRegisterName(unsigned RegNo);
71 bool runOnMachineFunction(MachineFunction &F);
72 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
74 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode);
77 void emitFunctionHeader(const MachineFunction &MF);
78 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
80 } // end of anonymous namespace
82 #include "SparcGenAsmWriter.inc"
84 /// runOnMachineFunction - This uses the printInstruction()
85 /// method to print assembly for each instruction.
87 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
88 SetupMachineFunction(MF);
90 // Print out constants referenced by the function
91 EmitConstantPool(MF.getConstantPool());
93 // BBNumber is used here so that a given Printer will never give two
94 // BBs the same name. (If you have a better way, please let me know!)
97 emitFunctionHeader(MF);
100 // Emit pre-function debug information.
101 DW->BeginFunction(&MF);
103 // Number each basic block so that we can consistently refer to them
104 // in PC-relative references.
105 // FIXME: Why not use the MBB numbers?
107 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
109 NumberForBB[I->getBasicBlock()] = BBNumber++;
112 // Print out code for the function.
113 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
115 // Print a label for the basic block.
116 if (I != MF.begin()) {
117 EmitBasicBlockStart(I);
119 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
121 // Print the assembly for the instruction.
122 processDebugLoc(II, true);
123 printInstruction(II);
128 processDebugLoc(II, false);
133 // Emit post-function debug information.
134 DW->EndFunction(&MF);
136 // We didn't modify anything.
137 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
141 void SparcAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
142 const Function *F = MF.getFunction();
143 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
144 EmitAlignment(MF.getAlignment(), F);
146 switch (F->getLinkage()) {
147 default: llvm_unreachable("Unknown linkage type");
148 case Function::PrivateLinkage:
149 case Function::InternalLinkage:
150 // Function is internal.
152 case Function::DLLExportLinkage:
153 case Function::ExternalLinkage:
154 // Function is externally visible
155 O << "\t.global\t" << *CurrentFnSym << '\n';
157 case Function::LinkerPrivateLinkage:
158 case Function::LinkOnceAnyLinkage:
159 case Function::LinkOnceODRLinkage:
160 case Function::WeakAnyLinkage:
161 case Function::WeakODRLinkage:
163 O << "\t.weak\t" << *CurrentFnSym << '\n';
167 printVisibility(CurrentFnSym, F->getVisibility());
169 O << "\t.type\t" << *CurrentFnSym << ", #function\n";
170 O << *CurrentFnSym << ":\n";
174 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
175 const MachineOperand &MO = MI->getOperand (opNum);
176 bool CloseParen = false;
177 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
180 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
181 !MO.isReg() && !MO.isImm()) {
185 switch (MO.getType()) {
186 case MachineOperand::MO_Register:
187 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
190 case MachineOperand::MO_Immediate:
191 O << (int)MO.getImm();
193 case MachineOperand::MO_MachineBasicBlock:
194 O << *GetMBBSymbol(MO.getMBB()->getNumber());
196 case MachineOperand::MO_GlobalAddress:
197 O << *GetGlobalValueSymbol(MO.getGlobal());
199 case MachineOperand::MO_ExternalSymbol:
200 O << MO.getSymbolName();
202 case MachineOperand::MO_ConstantPoolIndex:
203 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
207 llvm_unreachable("<unknown operand type>");
209 if (CloseParen) O << ")";
212 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
213 const char *Modifier) {
214 printOperand(MI, opNum);
216 // If this is an ADD operand, emit it like normal operands.
217 if (Modifier && !strcmp(Modifier, "arith")) {
219 printOperand(MI, opNum+1);
223 if (MI->getOperand(opNum+1).isReg() &&
224 MI->getOperand(opNum+1).getReg() == SP::G0)
225 return; // don't print "+%g0"
226 if (MI->getOperand(opNum+1).isImm() &&
227 MI->getOperand(opNum+1).getImm() == 0)
228 return; // don't print "+0"
231 if (MI->getOperand(opNum+1).isGlobal() ||
232 MI->getOperand(opNum+1).isCPI()) {
234 printOperand(MI, opNum+1);
237 printOperand(MI, opNum+1);
241 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
242 std::string operand = "";
243 const MachineOperand &MO = MI->getOperand(opNum);
244 switch (MO.getType()) {
245 default: assert(0 && "Operand is not a register ");
246 case MachineOperand::MO_Register:
247 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
248 "Operand is not a physical register ");
249 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
253 unsigned bbNum = NumberForBB[MI->getParent()->getBasicBlock()];
255 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
256 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
259 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
262 O << ".LLGETPC" << bbNum << ":\n" ;
263 O << "\tor\t" << operand
264 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
266 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
271 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
272 int CC = (int)MI->getOperand(opNum).getImm();
273 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
276 /// PrintAsmOperand - Print out an operand for an inline asm expression.
278 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
280 const char *ExtraCode) {
281 if (ExtraCode && ExtraCode[0]) {
282 if (ExtraCode[1] != 0) return true; // Unknown modifier.
284 switch (ExtraCode[0]) {
285 default: return true; // Unknown modifier.
291 printOperand(MI, OpNo);
296 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
299 const char *ExtraCode) {
300 if (ExtraCode && ExtraCode[0])
301 return true; // Unknown modifier
304 printMemOperand(MI, OpNo);
310 // Force static initialization.
311 extern "C" void LLVMInitializeSparcAsmPrinter() {
312 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);