blob: 61707f5556fb10bffc3236b5ee09b2dd438d2617 [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,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000052 const TargetAsmInfo *T, CodeGenOpt::Level OL,
53 bool V)
Bill Wendling58ed5d22009-04-29 00:15:41 +000054 : AsmPrinter(O, TM, T, OL, V) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055
56 virtual const char *getPassName() const {
57 return "Sparc Assembly Printer";
58 }
59
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +000060 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 void printOperand(const MachineInstr *MI, int opNum);
62 void printMemOperand(const MachineInstr *MI, int opNum,
63 const char *Modifier = 0);
64 void printCCOperand(const MachineInstr *MI, int opNum);
65
66 bool printInstruction(const MachineInstr *MI); // autogenerated.
67 bool runOnMachineFunction(MachineFunction &F);
68 bool doInitialization(Module &M);
69 bool doFinalization(Module &M);
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +000070 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
71 unsigned AsmVariant, const char *ExtraCode);
72 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 };
75} // end of anonymous namespace
76
77#include "SparcGenAsmWriter.inc"
78
79/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
80/// assembly code for a MachineFunction to the given output stream,
81/// using the given target machine description. This should work
82/// regardless of whether the function is in SSA form.
83///
Owen Anderson847b99b2008-08-21 00:14:44 +000084FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Bill Wendling4f405312009-02-24 08:30:20 +000085 TargetMachine &tm,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000086 CodeGenOpt::Level OptLevel,
Bill Wendling58ed5d22009-04-29 00:15:41 +000087 bool verbose) {
88 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
Evan Cheng8b988692008-02-02 08:39:46 +000091/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092/// method to print assembly for each instruction.
93///
94bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000095 this->MF = &MF;
96
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 SetupMachineFunction(MF);
98
99 // Print out constants referenced by the function
100 EmitConstantPool(MF.getConstantPool());
101
102 // BBNumber is used here so that a given Printer will never give two
103 // BBs the same name. (If you have a better way, please let me know!)
104 static unsigned BBNumber = 0;
105
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));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 EmitAlignment(4, 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:
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000175 {
176 const GlobalValue *GV = MO.getGlobal();
177 O << Mang->getValueName(GV);
178 }
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:
Evan Cheng477013c2007-10-14 05:57:21 +0000184 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000185 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 break;
187 default:
188 O << "<unknown operand type>"; abort (); break;
189 }
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000228 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return false; // success
230}
231
232bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Print out module-level global variables here.
234 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
235 I != E; ++I)
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000236 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000238 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239
Dan Gohman4a558a32007-07-25 19:33:14 +0000240 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241}
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000242
243void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
244 const TargetData *TD = TM.getTargetData();
245
246 if (!GVar->hasInitializer())
247 return; // External global require no code
248
249 // Check to see if this is a special global used by LLVM, if so, emit it.
250 if (EmitSpecialLLVMGlobal(GVar))
251 return;
252
253 O << "\n\n";
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000254 std::string name = Mang->getValueName(GVar);
255 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000256 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000257 unsigned Align = TD->getPreferredAlignment(GVar);
258
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000259 printVisibility(name, GVar->getVisibility());
260
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000261 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000262
263 if (C->isNullValue() && !GVar->hasSection()) {
264 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000265 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000266 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
267
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000268 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000269 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000270
271 O << TAI->getCOMMDirective() << name << ',' << Size;
272 if (TAI->getCOMMDirectiveTakesAlignment())
273 O << ',' << (1 << Align);
274
275 O << '\n';
276 return;
277 }
278 }
279
280 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000281 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000282 case GlobalValue::LinkOnceAnyLinkage:
283 case GlobalValue::LinkOnceODRLinkage:
284 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
285 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000286 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000287 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000288 break;
289 case GlobalValue::AppendingLinkage:
290 // FIXME: appending linkage variables should go into a section of
291 // their name or something. For now, just emit them as external.
292 case GlobalValue::ExternalLinkage:
293 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000294 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000295 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000296 case GlobalValue::PrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000297 case GlobalValue::InternalLinkage:
298 break;
299 case GlobalValue::GhostLinkage:
300 cerr << "Should not have any unmaterialized functions!\n";
301 abort();
302 case GlobalValue::DLLImportLinkage:
303 cerr << "DLLImport linkage is not supported by this target!\n";
304 abort();
305 case GlobalValue::DLLExportLinkage:
306 cerr << "DLLExport linkage is not supported by this target!\n";
307 abort();
308 default:
309 assert(0 && "Unknown linkage type!");
310 }
311
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000312 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000313
314 if (TAI->hasDotTypeDotSizeDirective()) {
315 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000316 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000317 }
318
319 O << name << ":\n";
320 EmitGlobalConstant(C);
321}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000322
323/// PrintAsmOperand - Print out an operand for an inline asm expression.
324///
325bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
326 unsigned AsmVariant,
327 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000328 if (ExtraCode && ExtraCode[0]) {
329 if (ExtraCode[1] != 0) return true; // Unknown modifier.
330
331 switch (ExtraCode[0]) {
332 default: return true; // Unknown modifier.
333 case 'r':
334 break;
335 }
336 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000337
338 printOperand(MI, OpNo);
339
340 return false;
341}
342
343bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
344 unsigned OpNo,
345 unsigned AsmVariant,
346 const char *ExtraCode) {
347 if (ExtraCode && ExtraCode[0])
348 return true; // Unknown modifier
349
350 O << '[';
351 printMemOperand(MI, OpNo);
352 O << ']';
353
354 return false;
355}