blob: ab1868425c5c55a9476d3c16bb8b1dfd278e7812 [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"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
21#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000022#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/Target/TargetAsmInfo.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetMachine.h"
29#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000030#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/MathExtras.h"
35#include <cctype>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000036#include <cstring>
Owen Anderson80045bf2008-07-10 01:44:27 +000037#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038using namespace llvm;
39
40STATISTIC(EmittedInsts, "Number of machine instrs printed");
41
42namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000043 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 /// We name each basic block in a Function with a unique number, so
45 /// that we can consistently refer to them later. This is cleared
46 /// at the beginning of each call to runOnMachineFunction().
47 ///
48 typedef std::map<const Value *, unsigned> ValueMapTy;
49 ValueMapTy NumberForBB;
Bill Wendling4f405312009-02-24 08:30:20 +000050 public:
Bill Wendling58ed5d22009-04-29 00:15:41 +000051 explicit SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
52 const TargetAsmInfo *T, unsigned OL, bool V)
53 : AsmPrinter(O, TM, T, OL, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 virtual const char *getPassName() const {
56 return "Sparc Assembly Printer";
57 }
58
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +000059 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 void printOperand(const MachineInstr *MI, int opNum);
61 void printMemOperand(const MachineInstr *MI, int opNum,
62 const char *Modifier = 0);
63 void printCCOperand(const MachineInstr *MI, int opNum);
64
65 bool printInstruction(const MachineInstr *MI); // autogenerated.
66 bool runOnMachineFunction(MachineFunction &F);
67 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +000069 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
71 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 };
74} // end of anonymous namespace
75
76#include "SparcGenAsmWriter.inc"
77
78/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
79/// assembly code for a MachineFunction to the given output stream,
80/// using the given target machine description. This should work
81/// regardless of whether the function is in SSA form.
82///
Owen Anderson847b99b2008-08-21 00:14:44 +000083FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000084 TargetMachine &tm,
Bill Wendling58ed5d22009-04-29 00:15:41 +000085 unsigned OptLevel,
86 bool verbose) {
87 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088}
89
Evan Cheng8b988692008-02-02 08:39:46 +000090/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091/// method to print assembly for each instruction.
92///
93bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000094 this->MF = &MF;
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 SetupMachineFunction(MF);
97
98 // Print out constants referenced by the function
99 EmitConstantPool(MF.getConstantPool());
100
101 // BBNumber is used here so that a given Printer will never give two
102 // BBs the same name. (If you have a better way, please let me know!)
103 static unsigned BBNumber = 0;
104
105 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 // Print out the label for the function.
108 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000109 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 EmitAlignment(4, F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000111 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000112
113 printVisibility(CurrentFnName, F->getVisibility());
114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 O << "\t.type\t" << CurrentFnName << ", #function\n";
116 O << CurrentFnName << ":\n";
117
118 // Number each basic block so that we can consistently refer to them
119 // in PC-relative references.
120 // FIXME: Why not use the MBB numbers?
121 NumberForBB.clear();
122 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
123 I != E; ++I) {
124 NumberForBB[I->getBasicBlock()] = BBNumber++;
125 }
126
127 // Print out code for the function.
128 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
129 I != E; ++I) {
130 // Print a label for the basic block.
131 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000132 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 O << '\n';
134 }
135 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
136 II != E; ++II) {
137 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 printInstruction(II);
139 ++EmittedInsts;
140 }
141 }
142
143 // We didn't modify anything.
144 return false;
145}
146
147void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
148 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000149 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000151 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 O << "%hi(";
153 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000154 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
155 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 O << "%lo(";
157 CloseParen = true;
158 }
159 switch (MO.getType()) {
160 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000161 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000162 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 else
164 O << "%reg" << MO.getReg();
165 break;
166
167 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000168 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 break;
170 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000171 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 return;
173 case MachineOperand::MO_GlobalAddress:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000174 {
175 const GlobalValue *GV = MO.getGlobal();
176 O << Mang->getValueName(GV);
177 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
179 case MachineOperand::MO_ExternalSymbol:
180 O << MO.getSymbolName();
181 break;
182 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000183 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000184 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 break;
186 default:
187 O << "<unknown operand type>"; abort (); break;
188 }
189 if (CloseParen) O << ")";
190}
191
192void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
193 const char *Modifier) {
194 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000195
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 // If this is an ADD operand, emit it like normal operands.
197 if (Modifier && !strcmp(Modifier, "arith")) {
198 O << ", ";
199 printOperand(MI, opNum+1);
200 return;
201 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000202
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000203 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 MI->getOperand(opNum+1).getReg() == SP::G0)
205 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000206 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000207 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000209
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000211 if (MI->getOperand(opNum+1).isGlobal() ||
212 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 O << "%lo(";
214 printOperand(MI, opNum+1);
215 O << ")";
216 } else {
217 printOperand(MI, opNum+1);
218 }
219}
220
221void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000222 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
224}
225
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000227 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 return false; // success
229}
230
231bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 // Print out module-level global variables here.
233 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
234 I != E; ++I)
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000235 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000237 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238
Dan Gohman4a558a32007-07-25 19:33:14 +0000239 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240}
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000241
242void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
243 const TargetData *TD = TM.getTargetData();
244
245 if (!GVar->hasInitializer())
246 return; // External global require no code
247
248 // Check to see if this is a special global used by LLVM, if so, emit it.
249 if (EmitSpecialLLVMGlobal(GVar))
250 return;
251
252 O << "\n\n";
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000253 std::string name = Mang->getValueName(GVar);
254 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000255 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000256 unsigned Align = TD->getPreferredAlignment(GVar);
257
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000258 printVisibility(name, GVar->getVisibility());
259
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000260 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000261
262 if (C->isNullValue() && !GVar->hasSection()) {
263 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000264 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000265 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
266
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000267 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000268 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000269
270 O << TAI->getCOMMDirective() << name << ',' << Size;
271 if (TAI->getCOMMDirectiveTakesAlignment())
272 O << ',' << (1 << Align);
273
274 O << '\n';
275 return;
276 }
277 }
278
279 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000280 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000281 case GlobalValue::LinkOnceAnyLinkage:
282 case GlobalValue::LinkOnceODRLinkage:
283 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
284 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000285 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000286 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000287 break;
288 case GlobalValue::AppendingLinkage:
289 // FIXME: appending linkage variables should go into a section of
290 // their name or something. For now, just emit them as external.
291 case GlobalValue::ExternalLinkage:
292 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000293 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000294 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000295 case GlobalValue::PrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000296 case GlobalValue::InternalLinkage:
297 break;
298 case GlobalValue::GhostLinkage:
299 cerr << "Should not have any unmaterialized functions!\n";
300 abort();
301 case GlobalValue::DLLImportLinkage:
302 cerr << "DLLImport linkage is not supported by this target!\n";
303 abort();
304 case GlobalValue::DLLExportLinkage:
305 cerr << "DLLExport linkage is not supported by this target!\n";
306 abort();
307 default:
308 assert(0 && "Unknown linkage type!");
309 }
310
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000311 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000312
313 if (TAI->hasDotTypeDotSizeDirective()) {
314 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000315 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000316 }
317
318 O << name << ":\n";
319 EmitGlobalConstant(C);
320}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000321
322/// PrintAsmOperand - Print out an operand for an inline asm expression.
323///
324bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
325 unsigned AsmVariant,
326 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000327 if (ExtraCode && ExtraCode[0]) {
328 if (ExtraCode[1] != 0) return true; // Unknown modifier.
329
330 switch (ExtraCode[0]) {
331 default: return true; // Unknown modifier.
332 case 'r':
333 break;
334 }
335 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000336
337 printOperand(MI, OpNo);
338
339 return false;
340}
341
342bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
343 unsigned OpNo,
344 unsigned AsmVariant,
345 const char *ExtraCode) {
346 if (ExtraCode && ExtraCode[0])
347 return true; // Unknown modifier
348
349 O << '[';
350 printMemOperand(MI, OpNo);
351 O << ']';
352
353 return false;
354}