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/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Target/TargetRegistry.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Support/FormattedStream.h"
29 class SparcAsmPrinter : public AsmPrinter {
31 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
32 MCContext &Ctx, MCStreamer &Streamer,
34 : AsmPrinter(O, TM, Ctx, Streamer, T) {}
36 virtual const char *getPassName() const {
37 return "Sparc Assembly Printer";
40 void printOperand(const MachineInstr *MI, int opNum);
41 void printMemOperand(const MachineInstr *MI, int opNum,
42 const char *Modifier = 0);
43 void printCCOperand(const MachineInstr *MI, int opNum);
45 virtual void EmitInstruction(const MachineInstr *MI) {
48 void printInstruction(const MachineInstr *MI); // autogenerated.
49 static const char *getRegisterName(unsigned RegNo);
51 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52 unsigned AsmVariant, const char *ExtraCode);
53 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
56 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
58 } // end of anonymous namespace
60 #include "SparcGenAsmWriter.inc"
62 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
63 const MachineOperand &MO = MI->getOperand (opNum);
64 bool CloseParen = false;
65 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
68 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
69 !MO.isReg() && !MO.isImm()) {
73 switch (MO.getType()) {
74 case MachineOperand::MO_Register:
75 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
78 case MachineOperand::MO_Immediate:
79 O << (int)MO.getImm();
81 case MachineOperand::MO_MachineBasicBlock:
82 O << *MO.getMBB()->getSymbol(OutContext);
84 case MachineOperand::MO_GlobalAddress:
85 O << *GetGlobalValueSymbol(MO.getGlobal());
87 case MachineOperand::MO_ExternalSymbol:
88 O << MO.getSymbolName();
90 case MachineOperand::MO_ConstantPoolIndex:
91 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
95 llvm_unreachable("<unknown operand type>");
97 if (CloseParen) O << ")";
100 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
101 const char *Modifier) {
102 printOperand(MI, opNum);
104 // If this is an ADD operand, emit it like normal operands.
105 if (Modifier && !strcmp(Modifier, "arith")) {
107 printOperand(MI, opNum+1);
111 if (MI->getOperand(opNum+1).isReg() &&
112 MI->getOperand(opNum+1).getReg() == SP::G0)
113 return; // don't print "+%g0"
114 if (MI->getOperand(opNum+1).isImm() &&
115 MI->getOperand(opNum+1).getImm() == 0)
116 return; // don't print "+0"
119 if (MI->getOperand(opNum+1).isGlobal() ||
120 MI->getOperand(opNum+1).isCPI()) {
122 printOperand(MI, opNum+1);
125 printOperand(MI, opNum+1);
129 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
130 std::string operand = "";
131 const MachineOperand &MO = MI->getOperand(opNum);
132 switch (MO.getType()) {
133 default: assert(0 && "Operand is not a register ");
134 case MachineOperand::MO_Register:
135 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
136 "Operand is not a physical register ");
137 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
141 unsigned bbNum = MI->getParent()->getNumber();
143 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
144 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
147 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
150 O << ".LLGETPC" << bbNum << ":\n" ;
151 O << "\tor\t" << operand
152 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
154 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
159 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
160 int CC = (int)MI->getOperand(opNum).getImm();
161 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
164 /// PrintAsmOperand - Print out an operand for an inline asm expression.
166 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
168 const char *ExtraCode) {
169 if (ExtraCode && ExtraCode[0]) {
170 if (ExtraCode[1] != 0) return true; // Unknown modifier.
172 switch (ExtraCode[0]) {
173 default: return true; // Unknown modifier.
179 printOperand(MI, OpNo);
184 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
187 const char *ExtraCode) {
188 if (ExtraCode && ExtraCode[0])
189 return true; // Unknown modifier
192 printMemOperand(MI, OpNo);
198 // Force static initialization.
199 extern "C" void LLVMInitializeSparcAsmPrinter() {
200 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);