blob: f720d47570eda8f46ba407496bc648e4c839ffac [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 {
43 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Owen Anderson847b99b2008-08-21 00:14:44 +000044 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 : AsmPrinter(O, TM, T) {
46 }
47
48 /// 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;
54
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,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 TargetMachine &tm) {
85 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
86}
87
Evan Cheng8b988692008-02-02 08:39:46 +000088/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089/// method to print assembly for each instruction.
90///
91bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 SetupMachineFunction(MF);
93
94 // Print out constants referenced by the function
95 EmitConstantPool(MF.getConstantPool());
96
97 // BBNumber is used here so that a given Printer will never give two
98 // BBs the same name. (If you have a better way, please let me know!)
99 static unsigned BBNumber = 0;
100
101 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 // Print out the label for the function.
104 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000105 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 EmitAlignment(4, F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000107 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000108
109 printVisibility(CurrentFnName, F->getVisibility());
110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 O << "\t.type\t" << CurrentFnName << ", #function\n";
112 O << CurrentFnName << ":\n";
113
114 // Number each basic block so that we can consistently refer to them
115 // in PC-relative references.
116 // FIXME: Why not use the MBB numbers?
117 NumberForBB.clear();
118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
119 I != E; ++I) {
120 NumberForBB[I->getBasicBlock()] = BBNumber++;
121 }
122
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 // Print a label for the basic block.
127 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000128 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 O << '\n';
130 }
131 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
132 II != E; ++II) {
133 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 printInstruction(II);
135 ++EmittedInsts;
136 }
137 }
138
139 // We didn't modify anything.
140 return false;
141}
142
143void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
144 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000145 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000147 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 O << "%hi(";
149 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000150 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
151 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 O << "%lo(";
153 CloseParen = true;
154 }
155 switch (MO.getType()) {
156 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000157 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000158 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 else
160 O << "%reg" << MO.getReg();
161 break;
162
163 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000164 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 break;
166 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000167 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 return;
169 case MachineOperand::MO_GlobalAddress:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000170 {
171 const GlobalValue *GV = MO.getGlobal();
172 O << Mang->getValueName(GV);
173 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 break;
175 case MachineOperand::MO_ExternalSymbol:
176 O << MO.getSymbolName();
177 break;
178 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000179 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000180 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 break;
182 default:
183 O << "<unknown operand type>"; abort (); break;
184 }
185 if (CloseParen) O << ")";
186}
187
188void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
189 const char *Modifier) {
190 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000191
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 // If this is an ADD operand, emit it like normal operands.
193 if (Modifier && !strcmp(Modifier, "arith")) {
194 O << ", ";
195 printOperand(MI, opNum+1);
196 return;
197 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000198
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000199 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 MI->getOperand(opNum+1).getReg() == SP::G0)
201 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000202 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000203 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000207 if (MI->getOperand(opNum+1).isGlobal() ||
208 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 O << "%lo(";
210 printOperand(MI, opNum+1);
211 O << ")";
212 } else {
213 printOperand(MI, opNum+1);
214 }
215}
216
217void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000218 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
220}
221
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000223 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return false; // success
225}
226
227bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 // Print out module-level global variables here.
229 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
230 I != E; ++I)
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000231 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000233 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
Dan Gohman4a558a32007-07-25 19:33:14 +0000235 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236}
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000237
238void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
239 const TargetData *TD = TM.getTargetData();
240
241 if (!GVar->hasInitializer())
242 return; // External global require no code
243
244 // Check to see if this is a special global used by LLVM, if so, emit it.
245 if (EmitSpecialLLVMGlobal(GVar))
246 return;
247
248 O << "\n\n";
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000249 std::string name = Mang->getValueName(GVar);
250 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000251 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000252 unsigned Align = TD->getPreferredAlignment(GVar);
253
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000254 printVisibility(name, GVar->getVisibility());
255
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000256 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000257
258 if (C->isNullValue() && !GVar->hasSection()) {
259 if (!GVar->isThreadLocal() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000260 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000261 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
262
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000263 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000264 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000265
266 O << TAI->getCOMMDirective() << name << ',' << Size;
267 if (TAI->getCOMMDirectiveTakesAlignment())
268 O << ',' << (1 << Align);
269
270 O << '\n';
271 return;
272 }
273 }
274
275 switch (GVar->getLinkage()) {
276 case GlobalValue::CommonLinkage:
277 case GlobalValue::LinkOnceLinkage:
278 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
279 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000280 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000281 break;
282 case GlobalValue::AppendingLinkage:
283 // FIXME: appending linkage variables should go into a section of
284 // their name or something. For now, just emit them as external.
285 case GlobalValue::ExternalLinkage:
286 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000287 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000288 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000289 case GlobalValue::PrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000290 case GlobalValue::InternalLinkage:
291 break;
292 case GlobalValue::GhostLinkage:
293 cerr << "Should not have any unmaterialized functions!\n";
294 abort();
295 case GlobalValue::DLLImportLinkage:
296 cerr << "DLLImport linkage is not supported by this target!\n";
297 abort();
298 case GlobalValue::DLLExportLinkage:
299 cerr << "DLLExport linkage is not supported by this target!\n";
300 abort();
301 default:
302 assert(0 && "Unknown linkage type!");
303 }
304
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000305 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000306
307 if (TAI->hasDotTypeDotSizeDirective()) {
308 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000309 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000310 }
311
312 O << name << ":\n";
313 EmitGlobalConstant(C);
314}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000315
316/// PrintAsmOperand - Print out an operand for an inline asm expression.
317///
318bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
319 unsigned AsmVariant,
320 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000321 if (ExtraCode && ExtraCode[0]) {
322 if (ExtraCode[1] != 0) return true; // Unknown modifier.
323
324 switch (ExtraCode[0]) {
325 default: return true; // Unknown modifier.
326 case 'r':
327 break;
328 }
329 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000330
331 printOperand(MI, OpNo);
332
333 return false;
334}
335
336bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
337 unsigned OpNo,
338 unsigned AsmVariant,
339 const char *ExtraCode) {
340 if (ExtraCode && ExtraCode[0])
341 return true; // Unknown modifier
342
343 O << '[';
344 printMemOperand(MI, OpNo);
345 O << ']';
346
347 return false;
348}