blob: 15e7fb02b2b773546395e571063fa5751bee60ed [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattnere880ec92009-06-19 15:48:10 +000018#include "SparcTargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
22#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000027#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000028#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000030#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000031#include "llvm/Target/TargetRegistry.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
Chris Lattner73266f92009-08-19 05:49:37 +000035#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
37#include "llvm/Support/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/MathExtras.h"
39#include <cctype>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000040#include <cstring>
Owen Anderson80045bf2008-07-10 01:44:27 +000041#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
44STATISTIC(EmittedInsts, "Number of machine instrs printed");
45
46namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000047 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
51 ///
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
Owen Andersond2d689e2009-06-26 21:45:04 +000054 unsigned BBNumber;
Bill Wendling4f405312009-02-24 08:30:20 +000055 public:
David Greene302008d2009-07-14 20:18:05 +000056 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000057 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000058 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
60 virtual const char *getPassName() const {
61 return "Sparc Assembly Printer";
62 }
63
Chris Lattnerae982212009-07-21 18:38:57 +000064 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 void printOperand(const MachineInstr *MI, int opNum);
66 void printMemOperand(const MachineInstr *MI, int opNum,
67 const char *Modifier = 0);
68 void printCCOperand(const MachineInstr *MI, int opNum);
69
Chris Lattnerddb259a2009-08-08 01:32:19 +000070 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner92221692009-09-13 20:08:00 +000071 const char *getRegisterName(unsigned RegNo) const;
72
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +000074 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode);
76 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
77 unsigned AsmVariant, const char *ExtraCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 };
79} // end of anonymous namespace
80
81#include "SparcGenAsmWriter.inc"
82
Chris Lattnere880ec92009-06-19 15:48:10 +000083
Evan Cheng8b988692008-02-02 08:39:46 +000084/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085/// method to print assembly for each instruction.
86///
87bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000088 this->MF = &MF;
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 SetupMachineFunction(MF);
91
92 // Print out constants referenced by the function
93 EmitConstantPool(MF.getConstantPool());
94
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!)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097
98 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
100 // Print out the label for the function.
101 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +0000102 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Bill Wendling25a8ae32009-06-30 22:38:32 +0000103 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000104 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000105
106 printVisibility(CurrentFnName, F->getVisibility());
107
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 O << "\t.type\t" << CurrentFnName << ", #function\n";
109 O << CurrentFnName << ":\n";
Richard Penningtonb246f532009-09-08 12:47:30 +0000110 // Emit pre-function debug information.
111 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
113 // Number each basic block so that we can consistently refer to them
114 // in PC-relative references.
115 // FIXME: Why not use the MBB numbers?
116 NumberForBB.clear();
117 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 I != E; ++I) {
119 NumberForBB[I->getBasicBlock()] = BBNumber++;
120 }
121
122 // Print out code for the function.
123 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
124 I != E; ++I) {
125 // Print a label for the basic block.
126 if (I != MF.begin()) {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000127 EmitBasicBlockStart(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 O << '\n';
129 }
130 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
131 II != E; ++II) {
132 // Print the assembly for the instruction.
Chris Lattnere34788c2009-09-09 20:34:59 +0000133 processDebugLoc(II->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 printInstruction(II);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000135
136 if (VerboseAsm && !II->getDebugLoc().isUnknown())
137 EmitComments(*II);
138 O << '\n';
139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 ++EmittedInsts;
141 }
142 }
143
Richard Penningtonb246f532009-09-08 12:47:30 +0000144 // Emit post-function debug information.
145 DW->EndFunction(&MF);
146
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 // We didn't modify anything.
148 return false;
149}
150
151void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
152 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000153 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000155 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 O << "%hi(";
157 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000158 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
159 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 O << "%lo(";
161 CloseParen = true;
162 }
163 switch (MO.getType()) {
164 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000165 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000166 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 else
168 O << "%reg" << MO.getReg();
169 break;
170
171 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000172 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 break;
174 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000175 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 return;
177 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000178 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 break;
180 case MachineOperand::MO_ExternalSymbol:
181 O << MO.getSymbolName();
182 break;
183 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000184 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000185 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 break;
187 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000188 llvm_unreachable("<unknown operand type>");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 }
190 if (CloseParen) O << ")";
191}
192
193void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
194 const char *Modifier) {
195 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 // If this is an ADD operand, emit it like normal operands.
198 if (Modifier && !strcmp(Modifier, "arith")) {
199 O << ", ";
200 printOperand(MI, opNum+1);
201 return;
202 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000203
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000204 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 MI->getOperand(opNum+1).getReg() == SP::G0)
206 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000207 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000208 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000210
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000212 if (MI->getOperand(opNum+1).isGlobal() ||
213 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 O << "%lo(";
215 printOperand(MI, opNum+1);
216 O << ")";
217 } else {
218 printOperand(MI, opNum+1);
219 }
220}
221
222void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000223 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
225}
226
Chris Lattnerae982212009-07-21 18:38:57 +0000227void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000228 const TargetData *TD = TM.getTargetData();
229
230 if (!GVar->hasInitializer())
231 return; // External global require no code
232
233 // Check to see if this is a special global used by LLVM, if so, emit it.
234 if (EmitSpecialLLVMGlobal(GVar))
235 return;
236
237 O << "\n\n";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000238 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000239 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000240 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000241 unsigned Align = TD->getPreferredAlignment(GVar);
242
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000243 printVisibility(name, GVar->getVisibility());
244
Chris Lattner73266f92009-08-19 05:49:37 +0000245 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
246 TM));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000247
248 if (C->isNullValue() && !GVar->hasSection()) {
249 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000250 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000251 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
252
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000253 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000254 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000255
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000256 O << MAI->getCOMMDirective() << name << ',' << Size;
257 if (MAI->getCOMMDirectiveTakesAlignment())
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000258 O << ',' << (1 << Align);
259
260 O << '\n';
261 return;
262 }
263 }
264
265 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000266 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000267 case GlobalValue::LinkOnceAnyLinkage:
268 case GlobalValue::LinkOnceODRLinkage:
269 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
270 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000271 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000272 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000273 break;
274 case GlobalValue::AppendingLinkage:
275 // FIXME: appending linkage variables should go into a section of
276 // their name or something. For now, just emit them as external.
277 case GlobalValue::ExternalLinkage:
278 // If external or appending, declare as a global symbol
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000279 O << MAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000280 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000281 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000282 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000283 case GlobalValue::InternalLinkage:
284 break;
285 case GlobalValue::GhostLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000286 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000287 case GlobalValue::DLLImportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000288 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000289 case GlobalValue::DLLExportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000290 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000291 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000292 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000293 }
294
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000295 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000296
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000297 if (MAI->hasDotTypeDotSizeDirective()) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000298 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000299 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000300 }
301
302 O << name << ":\n";
303 EmitGlobalConstant(C);
304}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000305
306/// PrintAsmOperand - Print out an operand for an inline asm expression.
307///
308bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
309 unsigned AsmVariant,
310 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000311 if (ExtraCode && ExtraCode[0]) {
312 if (ExtraCode[1] != 0) return true; // Unknown modifier.
313
314 switch (ExtraCode[0]) {
315 default: return true; // Unknown modifier.
316 case 'r':
317 break;
318 }
319 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000320
321 printOperand(MI, OpNo);
322
323 return false;
324}
325
326bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
327 unsigned OpNo,
328 unsigned AsmVariant,
329 const char *ExtraCode) {
330 if (ExtraCode && ExtraCode[0])
331 return true; // Unknown modifier
332
333 O << '[';
334 printMemOperand(MI, OpNo);
335 O << ']';
336
337 return false;
338}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000339
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000340// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000341extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000342 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000343}