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 {
48 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
49 const MCAsmInfo *T, bool V)
50 : AsmPrinter(O, TM, T, V) {}
52 virtual const char *getPassName() const {
53 return "Sparc Assembly Printer";
56 void printOperand(const MachineInstr *MI, int opNum);
57 void printMemOperand(const MachineInstr *MI, int opNum,
58 const char *Modifier = 0);
59 void printCCOperand(const MachineInstr *MI, int opNum);
61 void printInstruction(const MachineInstr *MI); // autogenerated.
62 static const char *getRegisterName(unsigned RegNo);
64 bool runOnMachineFunction(MachineFunction &F);
65 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
66 unsigned AsmVariant, const char *ExtraCode);
67 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
68 unsigned AsmVariant, const char *ExtraCode);
70 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
72 } // end of anonymous namespace
74 #include "SparcGenAsmWriter.inc"
76 /// runOnMachineFunction - This uses the printInstruction()
77 /// method to print assembly for each instruction.
79 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
80 SetupMachineFunction(MF);
84 // Print out code for the function.
85 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
87 // Print a label for the basic block.
89 EmitBasicBlockStart(I);
91 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
93 // Print the assembly for the instruction.
94 processDebugLoc(II, true);
100 processDebugLoc(II, false);
105 // Emit post-function debug information.
106 DW->EndFunction(&MF);
108 // We didn't modify anything.
109 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
114 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
115 const MachineOperand &MO = MI->getOperand (opNum);
116 bool CloseParen = false;
117 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
120 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
121 !MO.isReg() && !MO.isImm()) {
125 switch (MO.getType()) {
126 case MachineOperand::MO_Register:
127 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
130 case MachineOperand::MO_Immediate:
131 O << (int)MO.getImm();
133 case MachineOperand::MO_MachineBasicBlock:
134 O << *MO.getMBB()->getSymbol(OutContext);
136 case MachineOperand::MO_GlobalAddress:
137 O << *GetGlobalValueSymbol(MO.getGlobal());
139 case MachineOperand::MO_ExternalSymbol:
140 O << MO.getSymbolName();
142 case MachineOperand::MO_ConstantPoolIndex:
143 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
147 llvm_unreachable("<unknown operand type>");
149 if (CloseParen) O << ")";
152 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
153 const char *Modifier) {
154 printOperand(MI, opNum);
156 // If this is an ADD operand, emit it like normal operands.
157 if (Modifier && !strcmp(Modifier, "arith")) {
159 printOperand(MI, opNum+1);
163 if (MI->getOperand(opNum+1).isReg() &&
164 MI->getOperand(opNum+1).getReg() == SP::G0)
165 return; // don't print "+%g0"
166 if (MI->getOperand(opNum+1).isImm() &&
167 MI->getOperand(opNum+1).getImm() == 0)
168 return; // don't print "+0"
171 if (MI->getOperand(opNum+1).isGlobal() ||
172 MI->getOperand(opNum+1).isCPI()) {
174 printOperand(MI, opNum+1);
177 printOperand(MI, opNum+1);
181 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
182 std::string operand = "";
183 const MachineOperand &MO = MI->getOperand(opNum);
184 switch (MO.getType()) {
185 default: assert(0 && "Operand is not a register ");
186 case MachineOperand::MO_Register:
187 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
188 "Operand is not a physical register ");
189 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
193 unsigned bbNum = MI->getParent()->getNumber();
195 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
196 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
199 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
202 O << ".LLGETPC" << bbNum << ":\n" ;
203 O << "\tor\t" << operand
204 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
206 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
211 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
212 int CC = (int)MI->getOperand(opNum).getImm();
213 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
216 /// PrintAsmOperand - Print out an operand for an inline asm expression.
218 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
220 const char *ExtraCode) {
221 if (ExtraCode && ExtraCode[0]) {
222 if (ExtraCode[1] != 0) return true; // Unknown modifier.
224 switch (ExtraCode[0]) {
225 default: return true; // Unknown modifier.
231 printOperand(MI, OpNo);
236 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
239 const char *ExtraCode) {
240 if (ExtraCode && ExtraCode[0])
241 return true; // Unknown modifier
244 printMemOperand(MI, OpNo);
250 // Force static initialization.
251 extern "C" void LLVMInitializeSparcAsmPrinter() {
252 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);