blob: 168034641b89d478fa1a8fee6f070508d0b82251 [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.
Chris Lattnere34788c2009-09-09 20:34:59 +0000130 processDebugLoc(II->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 printInstruction(II);
132 ++EmittedInsts;
133 }
134 }
135
Richard Penningtonb246f532009-09-08 12:47:30 +0000136 // Emit post-function debug information.
137 DW->EndFunction(&MF);
138
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 // 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:
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000170 O << Mang->getMangledName(MO.getGlobal());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 break;
172 case MachineOperand::MO_ExternalSymbol:
173 O << MO.getSymbolName();
174 break;
175 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000176 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000177 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 break;
179 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000180 llvm_unreachable("<unknown operand type>");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182 if (CloseParen) O << ")";
183}
184
185void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
186 const char *Modifier) {
187 printOperand(MI, opNum);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 // If this is an ADD operand, emit it like normal operands.
190 if (Modifier && !strcmp(Modifier, "arith")) {
191 O << ", ";
192 printOperand(MI, opNum+1);
193 return;
194 }
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000195
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000196 if (MI->getOperand(opNum+1).isReg() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 MI->getOperand(opNum+1).getReg() == SP::G0)
198 return; // don't print "+%g0"
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000199 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000200 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000201 return; // don't print "+0"
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000202
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 O << "+";
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000204 if (MI->getOperand(opNum+1).isGlobal() ||
205 MI->getOperand(opNum+1).isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 O << "%lo(";
207 printOperand(MI, opNum+1);
208 O << ")";
209 } else {
210 printOperand(MI, opNum+1);
211 }
212}
213
214void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000215 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
217}
218
Chris Lattnerae982212009-07-21 18:38:57 +0000219void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000220 const TargetData *TD = TM.getTargetData();
221
222 if (!GVar->hasInitializer())
223 return; // External global require no code
224
225 // Check to see if this is a special global used by LLVM, if so, emit it.
226 if (EmitSpecialLLVMGlobal(GVar))
227 return;
228
229 O << "\n\n";
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000230 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000231 Constant *C = GVar->getInitializer();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000232 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000233 unsigned Align = TD->getPreferredAlignment(GVar);
234
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000235 printVisibility(name, GVar->getVisibility());
236
Chris Lattner73266f92009-08-19 05:49:37 +0000237 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
238 TM));
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000239
240 if (C->isNullValue() && !GVar->hasSection()) {
241 if (!GVar->isThreadLocal() &&
Duncan Sands19d161f2009-03-07 15:45:40 +0000242 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000243 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
244
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000245 if (GVar->hasLocalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000246 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000247
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000248 O << MAI->getCOMMDirective() << name << ',' << Size;
249 if (MAI->getCOMMDirectiveTakesAlignment())
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000250 O << ',' << (1 << Align);
251
252 O << '\n';
253 return;
254 }
255 }
256
257 switch (GVar->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000258 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000259 case GlobalValue::LinkOnceAnyLinkage:
260 case GlobalValue::LinkOnceODRLinkage:
261 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
262 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000263 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000264 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000265 break;
266 case GlobalValue::AppendingLinkage:
267 // FIXME: appending linkage variables should go into a section of
268 // their name or something. For now, just emit them as external.
269 case GlobalValue::ExternalLinkage:
270 // If external or appending, declare as a global symbol
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000271 O << MAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000272 // FALL THROUGH
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000273 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000274 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000275 case GlobalValue::InternalLinkage:
276 break;
277 case GlobalValue::GhostLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000278 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000279 case GlobalValue::DLLImportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000280 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000281 case GlobalValue::DLLExportLinkage:
Edwin Törökbd448e32009-07-14 16:55:14 +0000282 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000283 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000284 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000285 }
286
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000287 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000288
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000289 if (MAI->hasDotTypeDotSizeDirective()) {
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000290 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000291 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a895b2008-08-07 09:51:25 +0000292 }
293
294 O << name << ":\n";
295 EmitGlobalConstant(C);
296}
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000297
298/// PrintAsmOperand - Print out an operand for an inline asm expression.
299///
300bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
301 unsigned AsmVariant,
302 const char *ExtraCode) {
Anton Korobeynikov678468b2008-10-10 20:29:50 +0000303 if (ExtraCode && ExtraCode[0]) {
304 if (ExtraCode[1] != 0) return true; // Unknown modifier.
305
306 switch (ExtraCode[0]) {
307 default: return true; // Unknown modifier.
308 case 'r':
309 break;
310 }
311 }
Anton Korobeynikov2ffb4732008-10-10 10:15:03 +0000312
313 printOperand(MI, OpNo);
314
315 return false;
316}
317
318bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
319 unsigned OpNo,
320 unsigned AsmVariant,
321 const char *ExtraCode) {
322 if (ExtraCode && ExtraCode[0])
323 return true; // Unknown modifier
324
325 O << '[';
326 printMemOperand(MI, OpNo);
327 O << ']';
328
329 return false;
330}
Douglas Gregor1dc5ff42009-06-16 20:12:29 +0000331
Bob Wilsonebbc1c42009-06-23 23:59:40 +0000332// Force static initialization.
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000333extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000334 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbarfe5939f2009-07-15 20:24:03 +0000335}