e0cafcca7fe8f473858bb7e2db622d346148984f
[oota-llvm.git] / lib / Target / Sparc / AsmPrinter / SparcAsmPrinter.cpp
1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format SPARC assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Sparc.h"
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"
26 using namespace llvm;
27
28 namespace {
29   class SparcAsmPrinter : public AsmPrinter {
30   public:
31     explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
32                              MCContext &Ctx, MCStreamer &Streamer,
33                              const MCAsmInfo *T)
34       : AsmPrinter(O, TM, Ctx, Streamer, T) {}
35
36     virtual const char *getPassName() const {
37       return "Sparc Assembly Printer";
38     }
39
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);
44
45     virtual void EmitInstruction(const MachineInstr *MI) {
46       printInstruction(MI);
47       O << '\n';
48     }
49     void printInstruction(const MachineInstr *MI);  // autogenerated.
50     static const char *getRegisterName(unsigned RegNo);
51
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);
56
57     bool printGetPCX(const MachineInstr *MI, unsigned OpNo);
58   };
59 } // end of anonymous namespace
60
61 #include "SparcGenAsmWriter.inc"
62
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()) {
67     O << "%hi(";
68     CloseParen = true;
69   } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
70              !MO.isReg() && !MO.isImm()) {
71     O << "%lo(";
72     CloseParen = true;
73   }
74   switch (MO.getType()) {
75   case MachineOperand::MO_Register:
76     O << "%" << LowercaseString(getRegisterName(MO.getReg()));
77     break;
78
79   case MachineOperand::MO_Immediate:
80     O << (int)MO.getImm();
81     break;
82   case MachineOperand::MO_MachineBasicBlock:
83     O << *MO.getMBB()->getSymbol(OutContext);
84     return;
85   case MachineOperand::MO_GlobalAddress:
86     O << *GetGlobalValueSymbol(MO.getGlobal());
87     break;
88   case MachineOperand::MO_ExternalSymbol:
89     O << MO.getSymbolName();
90     break;
91   case MachineOperand::MO_ConstantPoolIndex:
92     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
93       << MO.getIndex();
94     break;
95   default:
96     llvm_unreachable("<unknown operand type>");
97   }
98   if (CloseParen) O << ")";
99 }
100
101 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
102                                       const char *Modifier) {
103   printOperand(MI, opNum);
104
105   // If this is an ADD operand, emit it like normal operands.
106   if (Modifier && !strcmp(Modifier, "arith")) {
107     O << ", ";
108     printOperand(MI, opNum+1);
109     return;
110   }
111
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"
118
119   O << "+";
120   if (MI->getOperand(opNum+1).isGlobal() ||
121       MI->getOperand(opNum+1).isCPI()) {
122     O << "%lo(";
123     printOperand(MI, opNum+1);
124     O << ")";
125   } else {
126     printOperand(MI, opNum+1);
127   }
128 }
129
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()));
139     break;
140   }
141
142   unsigned bbNum = MI->getParent()->getNumber();
143
144   O << '\n' << ".LLGETPCH" << bbNum << ":\n";
145   O << "\tcall\t.LLGETPC" << bbNum << '\n' ;
146
147   O << "\t  sethi\t"
148     << "%hi(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "  
149     << operand << '\n' ;
150
151   O << ".LLGETPC" << bbNum << ":\n" ;
152   O << "\tor\t" << operand  
153     << ", %lo(_GLOBAL_OFFSET_TABLE_+(.-.LLGETPCH" << bbNum << ")), "
154     << operand << '\n';
155   O << "\tadd\t" << operand << ", %o7, " << operand << '\n'; 
156   
157   return true;
158 }
159
160 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
161   int CC = (int)MI->getOperand(opNum).getImm();
162   O << SPARCCondCodeToString((SPCC::CondCodes)CC);
163 }
164
165 /// PrintAsmOperand - Print out an operand for an inline asm expression.
166 ///
167 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
168                                       unsigned AsmVariant,
169                                       const char *ExtraCode) {
170   if (ExtraCode && ExtraCode[0]) {
171     if (ExtraCode[1] != 0) return true; // Unknown modifier.
172
173     switch (ExtraCode[0]) {
174     default: return true;  // Unknown modifier.
175     case 'r':
176      break;
177     }
178   }
179
180   printOperand(MI, OpNo);
181
182   return false;
183 }
184
185 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
186                                             unsigned OpNo,
187                                             unsigned AsmVariant,
188                                             const char *ExtraCode) {
189   if (ExtraCode && ExtraCode[0])
190     return true;  // Unknown modifier
191
192   O << '[';
193   printMemOperand(MI, OpNo);
194   O << ']';
195
196   return false;
197 }
198
199 // Force static initialization.
200 extern "C" void LLVMInitializeSparcAsmPrinter() { 
201   RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
202   RegisterAsmPrinter<SparcAsmPrinter> Y(TheSparcV9Target);
203 }