blob: 80976bb25917dde83c65dcf1de92d440a69743de [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:
51 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
52 const TargetAsmInfo *T, bool F)
53 : AsmPrinter(O, TM, T, F) {}
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,
85 bool fast) {
86 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), fast);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087}
88
Evan Cheng8b988692008-02-02 08:39:46 +000089/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090/// method to print assembly for each instruction.
91///
92bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000093 this->MF = &MF;
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 SetupMachineFunction(MF);
96
97 // Print out constants referenced by the function
98 EmitConstantPool(MF.getConstantPool());
99
100 // BBNumber is used here so that a given Printer will never give two
101 // BBs the same name. (If you have a better way, please let me know!)
102 static unsigned BBNumber = 0;
103
104 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 // Print out the label for the function.
107 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000108 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 EmitAlignment(4, F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000110 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000111
112 printVisibility(CurrentFnName, F->getVisibility());
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 O << "\t.type\t" << CurrentFnName << ", #function\n";
115 O << CurrentFnName << ":\n";
116
117 // Number each basic block so that we can consistently refer to them
118 // in PC-relative references.
119 // FIXME: Why not use the MBB numbers?
120 NumberForBB.clear();
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 I != E; ++I) {
123 NumberForBB[I->getBasicBlock()] = BBNumber++;
124 }
125
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
130 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000131 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 O << '\n';
133 }
134 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
135 II != E; ++II) {
136 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 printInstruction(II);
138 ++EmittedInsts;
139 }
140 }
141
142 // We didn't modify anything.
143 return false;
144}
145
146void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
147 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000148 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000150 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 O << "%hi(";
152 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000153 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
154 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 O << "%lo(";
156 CloseParen = true;
157 }
158 switch (MO.getType()) {
159 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000160 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000161 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 else
163 O << "%reg" << MO.getReg();
164 break;
165
166 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000167 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 break;
169 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000170 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 return;
172 case MachineOperand::MO_GlobalAddress:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000173 {
174 const GlobalValue *GV = MO.getGlobal();
175 O << Mang->getValueName(GV);
176 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 case MachineOperand::MO_ExternalSymbol:
179 O << MO.getSymbolName();
180 break;
181 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000182 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000183 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 break;
185 default:
186 O << "<unknown operand type>"; abort (); break;
187 }
188 if (CloseParen) O << ")";
189}
190
191void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
192 const char *Modifier) {
193 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 // If this is an ADD operand, emit it like normal operands.
196 if (Modifier && !strcmp(Modifier, "arith")) {
197 O << ", ";
198 printOperand(MI, opNum+1);
199 return;
200 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000201
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000202 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 MI->getOperand(opNum+1).getReg() == SP::G0)
204 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000205 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000206 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000208
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000210 if (MI->getOperand(opNum+1).isGlobal() ||
211 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 O << "%lo(";
213 printOperand(MI, opNum+1);
214 O << ")";
215 } else {
216 printOperand(MI, opNum+1);
217 }
218}
219
220void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000221 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
223}
224
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000226 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 return false; // success
228}
229
230bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 // Print out module-level global variables here.
232 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
233 I != E; ++I)
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000234 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000236 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
Dan Gohman4a558a32007-07-25 19:33:14 +0000238 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239}
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000240
241void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
242 const TargetData *TD = TM.getTargetData();
243
244 if (!GVar->hasInitializer())
245 return; // External global require no code
246
247 // Check to see if this is a special global used by LLVM, if so, emit it.
248 if (EmitSpecialLLVMGlobal(GVar))
249 return;
250
251 O << "\n\n";
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000252 std::string name = Mang->getValueName(GVar);
253 Constant *C = GVar->getInitializer();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000254 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000255 unsigned Align = TD->getPreferredAlignment(GVar);
256
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000257 printVisibility(name, GVar->getVisibility());
258
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000259 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000260
261 if (C->isNullValue() && !GVar->hasSection()) {
262 if (!GVar->isThreadLocal() &&
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000263 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000264 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
265
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000266 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000267 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000268
269 O << TAI->getCOMMDirective() << name << ',' << Size;
270 if (TAI->getCOMMDirectiveTakesAlignment())
271 O << ',' << (1 << Align);
272
273 O << '\n';
274 return;
275 }
276 }
277
278 switch (GVar->getLinkage()) {
279 case GlobalValue::CommonLinkage:
280 case GlobalValue::LinkOnceLinkage:
281 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
282 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000283 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000284 break;
285 case GlobalValue::AppendingLinkage:
286 // FIXME: appending linkage variables should go into a section of
287 // their name or something. For now, just emit them as external.
288 case GlobalValue::ExternalLinkage:
289 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000290 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000291 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000292 case GlobalValue::PrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000293 case GlobalValue::InternalLinkage:
294 break;
295 case GlobalValue::GhostLinkage:
296 cerr << "Should not have any unmaterialized functions!\n";
297 abort();
298 case GlobalValue::DLLImportLinkage:
299 cerr << "DLLImport linkage is not supported by this target!\n";
300 abort();
301 case GlobalValue::DLLExportLinkage:
302 cerr << "DLLExport linkage is not supported by this target!\n";
303 abort();
304 default:
305 assert(0 && "Unknown linkage type!");
306 }
307
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000308 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000309
310 if (TAI->hasDotTypeDotSizeDirective()) {
311 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000312 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000313 }
314
315 O << name << ":\n";
316 EmitGlobalConstant(C);
317}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000318
319/// PrintAsmOperand - Print out an operand for an inline asm expression.
320///
321bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
322 unsigned AsmVariant,
323 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000324 if (ExtraCode && ExtraCode[0]) {
325 if (ExtraCode[1] != 0) return true; // Unknown modifier.
326
327 switch (ExtraCode[0]) {
328 default: return true; // Unknown modifier.
329 case 'r':
330 break;
331 }
332 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000333
334 printOperand(MI, OpNo);
335
336 return false;
337}
338
339bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
340 unsigned OpNo,
341 unsigned AsmVariant,
342 const char *ExtraCode) {
343 if (ExtraCode && ExtraCode[0])
344 return true; // Unknown modifier
345
346 O << '[';
347 printMemOperand(MI, OpNo);
348 O << ']';
349
350 return false;
351}