blob: 6115bf6fa115c7ce849fe74839bc21c95ff8b0a9 [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"
Devang Patelf667ab42009-06-25 00:47:42 +000022#include "llvm/MDNode.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000024#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000029#include "llvm/Target/TargetRegistry.h"
Edwin Török2b331342009-07-08 19:04:27 +000030#include "llvm/Support/ErrorHandling.h"
David Greene302008d2009-07-14 20:18:05 +000031#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/Mangler.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/MathExtras.h"
37#include <cctype>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000038#include <cstring>
Owen Anderson80045bf2008-07-10 01:44:27 +000039#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
44namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000045 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
49 ///
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
Owen Andersond2d689e2009-06-26 21:45:04 +000052 unsigned BBNumber;
Bill Wendling4f405312009-02-24 08:30:20 +000053 public:
David Greene302008d2009-07-14 20:18:05 +000054 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000055 const TargetAsmInfo *T, bool V)
56 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 virtual const char *getPassName() const {
59 return "Sparc Assembly Printer";
60 }
61
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +000062 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 void printOperand(const MachineInstr *MI, int opNum);
64 void printMemOperand(const MachineInstr *MI, int opNum,
65 const char *Modifier = 0);
66 void printCCOperand(const MachineInstr *MI, int opNum);
67
68 bool printInstruction(const MachineInstr *MI); // autogenerated.
69 bool runOnMachineFunction(MachineFunction &F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 bool doFinalization(Module &M);
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +000071 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
73 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
74 unsigned AsmVariant, const char *ExtraCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 };
76} // end of anonymous namespace
77
78#include "SparcGenAsmWriter.inc"
79
80/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
81/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
84///
David Greene302008d2009-07-14 20:18:05 +000085FunctionPass *llvm::createSparcCodePrinterPass(formatted_raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000086 TargetMachine &tm,
Bill Wendling58ed5d22009-04-29 00:15:41 +000087 bool verbose) {
Daniel Dunbarb10d2222009-07-01 01:48:54 +000088 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
Chris Lattnere880ec92009-06-19 15:48:10 +000091
Evan Cheng8b988692008-02-02 08:39:46 +000092/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093/// method to print assembly for each instruction.
94///
95bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000096 this->MF = &MF;
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 SetupMachineFunction(MF);
99
100 // Print out constants referenced by the function
101 EmitConstantPool(MF.getConstantPool());
102
103 // BBNumber is used here so that a given Printer will never give two
104 // BBs the same name. (If you have a better way, please let me know!)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 // Print out the label for the function.
109 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000110 SwitchToSection(TAI->SectionForGlobal(F));
Bill Wendling25a8ae32009-06-30 22:38:32 +0000111 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000112 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000113
114 printVisibility(CurrentFnName, F->getVisibility());
115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 O << "\t.type\t" << CurrentFnName << ", #function\n";
117 O << CurrentFnName << ":\n";
118
119 // Number each basic block so that we can consistently refer to them
120 // in PC-relative references.
121 // FIXME: Why not use the MBB numbers?
122 NumberForBB.clear();
123 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
124 I != E; ++I) {
125 NumberForBB[I->getBasicBlock()] = BBNumber++;
126 }
127
128 // Print out code for the function.
129 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
130 I != E; ++I) {
131 // Print a label for the basic block.
132 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000133 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 O << '\n';
135 }
136 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137 II != E; ++II) {
138 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 printInstruction(II);
140 ++EmittedInsts;
141 }
142 }
143
144 // We didn't modify anything.
145 return false;
146}
147
148void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
149 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000150 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000152 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 O << "%hi(";
154 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000155 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
156 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 O << "%lo(";
158 CloseParen = true;
159 }
160 switch (MO.getType()) {
161 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000162 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000163 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 else
165 O << "%reg" << MO.getReg();
166 break;
167
168 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000169 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000172 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 return;
174 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000175 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 break;
177 case MachineOperand::MO_ExternalSymbol:
178 O << MO.getSymbolName();
179 break;
180 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000181 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000182 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 break;
184 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000185 llvm_unreachable("<unknown operand type>");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 }
187 if (CloseParen) O << ")";
188}
189
190void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
191 const char *Modifier) {
192 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000193
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 // If this is an ADD operand, emit it like normal operands.
195 if (Modifier && !strcmp(Modifier, "arith")) {
196 O << ", ";
197 printOperand(MI, opNum+1);
198 return;
199 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000200
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000201 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 MI->getOperand(opNum+1).getReg() == SP::G0)
203 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000204 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000205 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000207
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000209 if (MI->getOperand(opNum+1).isGlobal() ||
210 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 O << "%lo(";
212 printOperand(MI, opNum+1);
213 O << ")";
214 } else {
215 printOperand(MI, opNum+1);
216 }
217}
218
219void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000220 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
222}
223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 // Print out module-level global variables here.
226 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
227 I != E; ++I)
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000228 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000230 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231
Dan Gohman4a558a32007-07-25 19:33:14 +0000232 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233}
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000234
235void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
236 const TargetData *TD = TM.getTargetData();
237
238 if (!GVar->hasInitializer())
239 return; // External global require no code
240
241 // Check to see if this is a special global used by LLVM, if so, emit it.
242 if (EmitSpecialLLVMGlobal(GVar))
243 return;
244
245 O << "\n\n";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000246 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000247 Constant *C = GVar->getInitializer();
Devang Patel880595f2009-06-26 02:26:12 +0000248 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patelf667ab42009-06-25 00:47:42 +0000249 return;
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000250 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000251 unsigned Align = TD->getPreferredAlignment(GVar);
252
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000253 printVisibility(name, GVar->getVisibility());
254
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000255 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000256
257 if (C->isNullValue() && !GVar->hasSection()) {
258 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000259 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000260 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
261
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000262 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000263 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000264
265 O << TAI->getCOMMDirective() << name << ',' << Size;
266 if (TAI->getCOMMDirectiveTakesAlignment())
267 O << ',' << (1 << Align);
268
269 O << '\n';
270 return;
271 }
272 }
273
274 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000275 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000276 case GlobalValue::LinkOnceAnyLinkage:
277 case GlobalValue::LinkOnceODRLinkage:
278 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
279 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000280 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000281 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000282 break;
283 case GlobalValue::AppendingLinkage:
284 // FIXME: appending linkage variables should go into a section of
285 // their name or something. For now, just emit them as external.
286 case GlobalValue::ExternalLinkage:
287 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000288 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000289 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000290 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000291 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000292 case GlobalValue::InternalLinkage:
293 break;
294 case GlobalValue::GhostLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000295 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000296 case GlobalValue::DLLImportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000297 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000298 case GlobalValue::DLLExportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000299 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000300 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000301 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000302 }
303
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000304 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000305
306 if (TAI->hasDotTypeDotSizeDirective()) {
307 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000308 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000309 }
310
311 O << name << ":\n";
312 EmitGlobalConstant(C);
313}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000314
315/// PrintAsmOperand - Print out an operand for an inline asm expression.
316///
317bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
318 unsigned AsmVariant,
319 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000320 if (ExtraCode && ExtraCode[0]) {
321 if (ExtraCode[1] != 0) return true; // Unknown modifier.
322
323 switch (ExtraCode[0]) {
324 default: return true; // Unknown modifier.
325 case 'r':
326 break;
327 }
328 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000329
330 printOperand(MI, OpNo);
331
332 return false;
333}
334
335bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
336 unsigned OpNo,
337 unsigned AsmVariant,
338 const char *ExtraCode) {
339 if (ExtraCode && ExtraCode[0])
340 return true; // Unknown modifier
341
342 O << '[';
343 printMemOperand(MI, OpNo);
344 O << ']';
345
346 return false;
347}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000348
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000349// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000350extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000351 TargetRegistry::RegisterAsmPrinter(TheSparcTarget,
352 createSparcCodePrinterPass);
353}