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) {
90 SetupMachineFunction(MF);
92 // Print out constants referenced by the function
93 EmitConstantPool(MF.getConstantPool());
95 // BBNumber is used here so that a given Printer will never give two
96 // BBs the same name. (If you have a better way, please let me know!)
99 emitFunctionHeader(MF);
102 // Emit pre-function debug information.
103 DW->BeginFunction(&MF);
105 // Number each basic block so that we can consistently refer to them
106 // in PC-relative references.
107 // FIXME: Why not use the MBB numbers?
109 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
111 NumberForBB[I->getBasicBlock()] = BBNumber++;
114 // Print out code for the function.
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
117 // Print a label for the basic block.
118 if (I != MF.begin()) {
119 EmitBasicBlockStart(I);
121 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
123 // Print the assembly for the instruction.
124 processDebugLoc(II, true);
125 printInstruction(II);
130 processDebugLoc(II, false);
135 // Emit post-function debug information.
136 DW->EndFunction(&MF);
138 // We didn't modify anything.
139 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
143 void SparcAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
144 const Function *F = MF.getFunction();
145 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
146 EmitAlignment(MF.getAlignment(), F);
148 switch (F->getLinkage()) {
149 default: llvm_unreachable("Unknown linkage type");
150 case Function::PrivateLinkage:
151 case Function::InternalLinkage:
152 // Function is internal.
154 case Function::DLLExportLinkage:
155 case Function::ExternalLinkage:
156 // Function is externally visible
157 O << "\t.global\t" << *CurrentFnSym << '\n';
159 case Function::LinkerPrivateLinkage:
160 case Function::LinkOnceAnyLinkage:
161 case Function::LinkOnceODRLinkage:
162 case Function::WeakAnyLinkage:
163 case Function::WeakODRLinkage:
165 O << "\t.weak\t" << *CurrentFnSym << '\n';
169 printVisibility(CurrentFnSym, F->getVisibility());
171 O << "\t.type\t" << *CurrentFnSym << ", #function\n";
172 O << *CurrentFnSym << ":\n";
176 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
177 const MachineOperand &MO = MI->getOperand (opNum);
178 bool CloseParen = false;
179 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
182 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
183 !MO.isReg() && !MO.isImm()) {
187 switch (MO.getType()) {
188 case MachineOperand::MO_Register:
189 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
192 case MachineOperand::MO_Immediate:
193 O << (int)MO.getImm();
195 case MachineOperand::MO_MachineBasicBlock:
196 O << *GetMBBSymbol(MO.getMBB()->getNumber());
198 case MachineOperand::MO_GlobalAddress:
199 O << *GetGlobalValueSymbol(MO.getGlobal());
201 case MachineOperand::MO_ExternalSymbol:
202 O << MO.getSymbolName();
204 case MachineOperand::MO_ConstantPoolIndex:
205 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
209 llvm_unreachable("<unknown operand type>");
211 if (CloseParen) O << ")";
214 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
215 const char *Modifier) {
216 printOperand(MI, opNum);
218 // If this is an ADD operand, emit it like normal operands.
219 if (Modifier && !strcmp(Modifier, "arith")) {
221 printOperand(MI, opNum+1);
225 if (MI->getOperand(opNum+1).isReg() &&
226 MI->getOperand(opNum+1).getReg() == SP::G0)
227 return; // don't print "+%g0"
228 if (MI->getOperand(opNum+1).isImm() &&
229 MI->getOperand(opNum+1).getImm() == 0)
230 return; // don't print "+0"
233 if (MI->getOperand(opNum+1).isGlobal() ||
234 MI->getOperand(opNum+1).isCPI()) {
236 printOperand(MI, opNum+1);
239 printOperand(MI, opNum+1);
243 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
244 std::string operand = "";
245 const MachineOperand &MO = MI->getOperand(opNum);
246 switch (MO.getType()) {
247 default: assert(0 && "Operand is not a register ");
248 case MachineOperand::MO_Register:
249 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
250 "Operand is not a physical register ");
251 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
255 unsigned bbNum = NumberForBB[MI->getParent()->getBasicBlock()];
257 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
258 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
261 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
264 O << ".LLGETPC" << bbNum << ":\n" ;
265 O << "\tor\t" << operand
266 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
268 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
273 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
274 int CC = (int)MI->getOperand(opNum).getImm();
275 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
278 /// PrintAsmOperand - Print out an operand for an inline asm expression.
280 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
282 const char *ExtraCode) {
283 if (ExtraCode && ExtraCode[0]) {
284 if (ExtraCode[1] != 0) return true; // Unknown modifier.
286 switch (ExtraCode[0]) {
287 default: return true; // Unknown modifier.
293 printOperand(MI, OpNo);
298 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
301 const char *ExtraCode) {
302 if (ExtraCode && ExtraCode[0])
303 return true; // Unknown modifier
306 printMemOperand(MI, OpNo);
312 // Force static initialization.
313 extern "C" void LLVMInitializeSparcAsmPrinter() {
314 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);