blob: e608f7ec37a3df5bb60a43c8a7e0bbb68c0410df [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"
22#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner73266f92009-08-19 05:49:37 +000027#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000028#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbarfe5939f2009-07-15 20:24:03 +000030#include "llvm/Target/TargetRegistry.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"
Chris Lattner73266f92009-08-19 05:49:37 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
36#include "llvm/Support/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
38#include <cctype>
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000039#include <cstring>
Owen Anderson80045bf2008-07-10 01:44:27 +000040#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041using namespace llvm;
42
43STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45namespace {
Bill Wendling4f405312009-02-24 08:30:20 +000046 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
50 ///
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
Owen Andersond2d689e2009-06-26 21:45:04 +000053 unsigned BBNumber;
Bill Wendling4f405312009-02-24 08:30:20 +000054 public:
David Greene302008d2009-07-14 20:18:05 +000055 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner621c44d2009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool V)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000057 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 virtual const char *getPassName() const {
60 return "Sparc Assembly Printer";
61 }
62
Chris Lattnerae982212009-07-21 18:38:57 +000063 void PrintGlobalVariable(const GlobalVariable *GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 void printOperand(const MachineInstr *MI, int opNum);
65 void printMemOperand(const MachineInstr *MI, int opNum,
66 const char *Modifier = 0);
67 void printCCOperand(const MachineInstr *MI, int opNum);
68
Chris Lattnerddb259a2009-08-08 01:32:19 +000069 void printInstruction(const MachineInstr *MI); // autogenerated.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 bool runOnMachineFunction(MachineFunction &F);
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
Chris Lattnere880ec92009-06-19 15:48:10 +000080
Evan Cheng8b988692008-02-02 08:39:46 +000081/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082/// method to print assembly for each instruction.
83///
84bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +000085 this->MF = &MF;
86
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 SetupMachineFunction(MF);
88
89 // Print out constants referenced by the function
90 EmitConstantPool(MF.getConstantPool());
91
92 // BBNumber is used here so that a given Printer will never give two
93 // BBs the same name. (If you have a better way, please let me know!)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094
95 O << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096
97 // Print out the label for the function.
98 const Function *F = MF.getFunction();
Chris Lattner73266f92009-08-19 05:49:37 +000099 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Bill Wendling25a8ae32009-06-30 22:38:32 +0000100 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000101 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000102
103 printVisibility(CurrentFnName, F->getVisibility());
104
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 O << "\t.type\t" << CurrentFnName << ", #function\n";
106 O << CurrentFnName << ":\n";
Richard Penningtonb246f532009-09-08 12:47:30 +0000107 // Emit pre-function debug information.
108 DW->BeginFunction(&MF);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 // Number each basic block so that we can consistently refer to them
111 // in PC-relative references.
112 // FIXME: Why not use the MBB numbers?
113 NumberForBB.clear();
114 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
115 I != E; ++I) {
116 NumberForBB[I->getBasicBlock()] = BBNumber++;
117 }
118
119 // Print out code for the function.
120 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
121 I != E; ++I) {
122 // Print a label for the basic block.
123 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000124 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 O << '\n';
126 }
127 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
128 II != E; ++II) {
129 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 printInstruction(II);
131 ++EmittedInsts;
132 }
133 }
134
Richard Penningtonb246f532009-09-08 12:47:30 +0000135 // Emit post-function debug information.
136 DW->EndFunction(&MF);
137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 // We didn't modify anything.
139 return false;
140}
141
142void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
143 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000144 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 bool CloseParen = false;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000146 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 O << "%hi(";
148 CloseParen = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000149 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
150 !MO.isReg() && !MO.isImm()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 O << "%lo(";
152 CloseParen = true;
153 }
154 switch (MO.getType()) {
155 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000156 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000157 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 else
159 O << "%reg" << MO.getReg();
160 break;
161
162 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000163 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 break;
165 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000166 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 return;
168 case MachineOperand::MO_GlobalAddress:
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000169 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 break;
171 case MachineOperand::MO_ExternalSymbol:
172 O << MO.getSymbolName();
173 break;
174 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000175 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000176 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 break;
178 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000179 llvm_unreachable("<unknown operand type>");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 }
181 if (CloseParen) O << ")";
182}
183
184void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
185 const char *Modifier) {
186 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 // If this is an ADD operand, emit it like normal operands.
189 if (Modifier && !strcmp(Modifier, "arith")) {
190 O << ", ";
191 printOperand(MI, opNum+1);
192 return;
193 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000194
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000195 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 MI->getOperand(opNum+1).getReg() == SP::G0)
197 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000198 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000199 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000203 if (MI->getOperand(opNum+1).isGlobal() ||
204 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 O << "%lo(";
206 printOperand(MI, opNum+1);
207 O << ")";
208 } else {
209 printOperand(MI, opNum+1);
210 }
211}
212
213void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000214 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
216}
217
Chris Lattnerae982212009-07-21 18:38:57 +0000218void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000219 const TargetData *TD = TM.getTargetData();
220
221 if (!GVar->hasInitializer())
222 return; // External global require no code
223
224 // Check to see if this is a special global used by LLVM, if so, emit it.
225 if (EmitSpecialLLVMGlobal(GVar))
226 return;
227
228 O << "\n\n";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000229 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000230 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000231 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000232 unsigned Align = TD->getPreferredAlignment(GVar);
233
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000234 printVisibility(name, GVar->getVisibility());
235
Chris Lattner73266f92009-08-19 05:49:37 +0000236 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
237 TM));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000238
239 if (C->isNullValue() && !GVar->hasSection()) {
240 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000241 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000242 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
243
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000244 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000245 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000246
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000247 O << MAI->getCOMMDirective() << name << ',' << Size;
248 if (MAI->getCOMMDirectiveTakesAlignment())
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000249 O << ',' << (1 << Align);
250
251 O << '\n';
252 return;
253 }
254 }
255
256 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000257 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000258 case GlobalValue::LinkOnceAnyLinkage:
259 case GlobalValue::LinkOnceODRLinkage:
260 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
261 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000262 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000263 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000264 break;
265 case GlobalValue::AppendingLinkage:
266 // FIXME: appending linkage variables should go into a section of
267 // their name or something. For now, just emit them as external.
268 case GlobalValue::ExternalLinkage:
269 // If external or appending, declare as a global symbol
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000270 O << MAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000271 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000272 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000273 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000274 case GlobalValue::InternalLinkage:
275 break;
276 case GlobalValue::GhostLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000277 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000278 case GlobalValue::DLLImportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000279 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000280 case GlobalValue::DLLExportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000281 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000282 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000283 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000284 }
285
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000286 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000287
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000288 if (MAI->hasDotTypeDotSizeDirective()) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000289 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000290 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000291 }
292
293 O << name << ":\n";
294 EmitGlobalConstant(C);
295}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000296
297/// PrintAsmOperand - Print out an operand for an inline asm expression.
298///
299bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
300 unsigned AsmVariant,
301 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000302 if (ExtraCode && ExtraCode[0]) {
303 if (ExtraCode[1] != 0) return true; // Unknown modifier.
304
305 switch (ExtraCode[0]) {
306 default: return true; // Unknown modifier.
307 case 'r':
308 break;
309 }
310 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000311
312 printOperand(MI, OpNo);
313
314 return false;
315}
316
317bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
318 unsigned OpNo,
319 unsigned AsmVariant,
320 const char *ExtraCode) {
321 if (ExtraCode && ExtraCode[0])
322 return true; // Unknown modifier
323
324 O << '[';
325 printMemOperand(MI, OpNo);
326 O << ']';
327
328 return false;
329}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000330
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000331// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000332extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000333 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000334}