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) {
49 void printInstruction(const MachineInstr *MI); // autogenerated.
50 static const char *getRegisterName(unsigned RegNo);
52 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53 unsigned AsmVariant, const char *ExtraCode);
54 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
55 unsigned AsmVariant, const char *ExtraCode);
57 bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
59 } // end of anonymous namespace
61 #include "SparcGenAsmWriter.inc"
63 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
64 const MachineOperand &MO = MI->getOperand (opNum);
65 bool CloseParen = false;
66 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
69 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
70 !MO.isReg() && !MO.isImm()) {
74 switch (MO.getType()) {
75 case MachineOperand::MO_Register:
76 O << "%" << LowercaseString(getRegisterName(MO.getReg()));
79 case MachineOperand::MO_Immediate:
80 O << (int)MO.getImm();
82 case MachineOperand::MO_MachineBasicBlock:
83 O << *MO.getMBB()->getSymbol(OutContext);
85 case MachineOperand::MO_GlobalAddress:
86 O << *GetGlobalValueSymbol(MO.getGlobal());
88 case MachineOperand::MO_ExternalSymbol:
89 O << MO.getSymbolName();
91 case MachineOperand::MO_ConstantPoolIndex:
92 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
96 llvm_unreachable("<unknown operand type>");
98 if (CloseParen) O << ")";
101 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
102 const char *Modifier) {
103 printOperand(MI, opNum);
105 // If this is an ADD operand, emit it like normal operands.
106 if (Modifier && !strcmp(Modifier, "arith")) {
108 printOperand(MI, opNum+1);
112 if (MI->getOperand(opNum+1).isReg() &&
113 MI->getOperand(opNum+1).getReg() == SP::G0)
114 return; // don't print "+%g0"
115 if (MI->getOperand(opNum+1).isImm() &&
116 MI->getOperand(opNum+1).getImm() == 0)
117 return; // don't print "+0"
120 if (MI->getOperand(opNum+1).isGlobal() ||
121 MI->getOperand(opNum+1).isCPI()) {
123 printOperand(MI, opNum+1);
126 printOperand(MI, opNum+1);
130 bool SparcAsmPrinter::printGetPCX(const MachineInstr *MI, unsigned opNum) {
131 std::string operand = "";
132 const MachineOperand &MO = MI->getOperand(opNum);
133 switch (MO.getType()) {
134 default: assert(0 && "Operand is not a register ");
135 case MachineOperand::MO_Register:
136 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
137 "Operand is not a physical register ");
138 operand = "%" + LowercaseString(getRegisterName(MO.getReg()));
142 unsigned bbNum = MI->getParent()->getNumber();
144 O << '\n' << ".LLGETPCH" << bbNum << ":\n";
145 O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
148 << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
151 O << ".LLGETPC" << bbNum << ":\n" ;
152 O << "\tor\t" << operand
153 << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
155 O << "\tadd\t" << operand << ", %o7, " << operand << '\n';
160 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
161 int CC = (int)MI->getOperand(opNum).getImm();
162 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
165 /// PrintAsmOperand - Print out an operand for an inline asm expression.
167 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
169 const char *ExtraCode) {
170 if (ExtraCode && ExtraCode[0]) {
171 if (ExtraCode[1] != 0) return true; // Unknown modifier.
173 switch (ExtraCode[0]) {
174 default: return true; // Unknown modifier.
180 printOperand(MI, OpNo);
185 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
188 const char *ExtraCode) {
189 if (ExtraCode && ExtraCode[0])
190 return true; // Unknown modifier
193 printMemOperand(MI, OpNo);
199 // Force static initialization.
200 extern "C" void LLVMInitializeSparcAsmPrinter() {
201 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);