blob: e4c5b5531774b15c4355b9892dc8431a0f4e63e1 [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +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
Chris Lattner1ef9cd42006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner158e1f52006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Chris Lattner3773afe2009-06-19 15:48:10 +000018#include "SparcTargetMachine.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Module.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000022#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling0f4c5812009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineConstantPool.h"
26#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000027#include "llvm/MC/MCStreamer.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000028#include "llvm/MC/MCAsmInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000029#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbare8338102009-07-15 20:24:03 +000030#include "llvm/Target/TargetRegistry.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/Support/CommandLine.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/FormattedStream.h"
36#include "llvm/Support/Mangler.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000037#include "llvm/Support/MathExtras.h"
38#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000039#include <cstring>
Owen Anderson36b92ca2008-07-10 01:44:27 +000040#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000041using namespace llvm;
42
Chris Lattner1ef9cd42006-12-19 22:59:26 +000043STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000044
Chris Lattner1ef9cd42006-12-19 22:59:26 +000045namespace {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000046 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Chris Lattner158e1f52006-02-05 05:50:24 +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 Andersonc043d132009-06-26 21:45:04 +000053 unsigned BBNumber;
Bill Wendlingc5437ea2009-02-24 08:30:20 +000054 public:
David Greenea31f96c2009-07-14 20:18:05 +000055 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner7b26fce2009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool V)
Daniel Dunbar75c12e12009-07-01 01:48:54 +000057 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000058
59 virtual const char *getPassName() const {
60 return "Sparc Assembly Printer";
61 }
62
Chris Lattner100865e2009-07-21 18:38:57 +000063 void PrintGlobalVariable(const GlobalVariable *GVar);
Chris Lattner158e1f52006-02-05 05:50:24 +000064 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000065 void printMemOperand(const MachineInstr *MI, int opNum,
66 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000067 void printCCOperand(const MachineInstr *MI, int opNum);
68
Chris Lattnerb94284b2009-08-08 01:32:19 +000069 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner158e1f52006-02-05 05:50:24 +000070 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov3db21732008-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);
Chris Lattner158e1f52006-02-05 05:50:24 +000075 };
76} // end of anonymous namespace
77
78#include "SparcGenAsmWriter.inc"
79
Chris Lattner3773afe2009-06-19 15:48:10 +000080
Evan Cheng32e53472008-02-02 08:39:46 +000081/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000082/// method to print assembly for each instruction.
83///
84bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000085 this->MF = &MF;
86
Chris Lattner158e1f52006-02-05 05:50:24 +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!)
Chris Lattner158e1f52006-02-05 05:50:24 +000094
95 O << "\n\n";
Chris Lattner158e1f52006-02-05 05:50:24 +000096
Chris Lattnerd4d255a2006-10-05 02:48:40 +000097 // Print out the label for the function.
98 const Function *F = MF.getFunction();
Chris Lattner4b7dadb2009-08-19 05:49:37 +000099 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Bill Wendling31ceb1b2009-06-30 22:38:32 +0000100 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000101 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikoved473292008-08-08 18:25:07 +0000102
103 printVisibility(CurrentFnName, F->getVisibility());
104
Chris Lattner158e1f52006-02-05 05:50:24 +0000105 O << "\t.type\t" << CurrentFnName << ", #function\n";
106 O << CurrentFnName << ":\n";
Richard Penningtonbd1fc362009-09-08 12:47:30 +0000107 // Emit pre-function debug information.
108 DW->BeginFunction(&MF);
Chris Lattner158e1f52006-02-05 05:50:24 +0000109
110 // Number each basic block so that we can consistently refer to them
111 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000112 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000113 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.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000123 if (I != MF.begin()) {
Evan Chengc7990652008-02-28 00:43:03 +0000124 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000125 O << '\n';
126 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000127 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
128 II != E; ++II) {
129 // Print the assembly for the instruction.
Chris Lattner716a8c92009-09-09 20:34:59 +0000130 processDebugLoc(II->getDebugLoc());
Chris Lattner158e1f52006-02-05 05:50:24 +0000131 printInstruction(II);
Chris Lattner321bc992009-09-09 23:14:36 +0000132
133 if (VerboseAsm && !II->getDebugLoc().isUnknown())
134 EmitComments(*II);
135 O << '\n';
136
Chris Lattner158e1f52006-02-05 05:50:24 +0000137 ++EmittedInsts;
138 }
139 }
140
Richard Penningtonbd1fc362009-09-08 12:47:30 +0000141 // Emit post-function debug information.
142 DW->EndFunction(&MF);
143
Chris Lattner158e1f52006-02-05 05:50:24 +0000144 // 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 Gohman3a4be0f2008-02-10 18:45:23 +0000150 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000151 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000152 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000153 O << "%hi(";
154 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000155 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
156 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000157 O << "%lo(";
158 CloseParen = true;
159 }
160 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000161 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000162 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendlingc24ea4f2008-02-26 21:11:01 +0000163 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Chris Lattner158e1f52006-02-05 05:50:24 +0000164 else
165 O << "%reg" << MO.getReg();
166 break;
167
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000168 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000169 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000170 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000171 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000172 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000173 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000174 case MachineOperand::MO_GlobalAddress:
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000175 O << Mang->getMangledName(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +0000176 break;
177 case MachineOperand::MO_ExternalSymbol:
178 O << MO.getSymbolName();
179 break;
180 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnere9a75a62009-08-22 21:43:10 +0000181 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000182 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000183 break;
184 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000185 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +0000186 }
187 if (CloseParen) O << ")";
188}
189
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000190void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
191 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000192 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000193
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000194 // If this is an ADD operand, emit it like normal operands.
195 if (Modifier && !strcmp(Modifier, "arith")) {
196 O << ", ";
197 printOperand(MI, opNum+1);
198 return;
199 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000200
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000201 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000202 MI->getOperand(opNum+1).getReg() == SP::G0)
203 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000204 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000205 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000206 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000207
Chris Lattner158e1f52006-02-05 05:50:24 +0000208 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000209 if (MI->getOperand(opNum+1).isGlobal() ||
210 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000211 O << "%lo(";
212 printOperand(MI, opNum+1);
213 O << ")";
214 } else {
215 printOperand(MI, opNum+1);
216 }
217}
218
219void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000220 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000221 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
222}
223
Chris Lattner100865e2009-07-21 18:38:57 +0000224void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000225 const TargetData *TD = TM.getTargetData();
226
227 if (!GVar->hasInitializer())
228 return; // External global require no code
229
230 // Check to see if this is a special global used by LLVM, if so, emit it.
231 if (EmitSpecialLLVMGlobal(GVar))
232 return;
233
234 O << "\n\n";
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000235 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000236 Constant *C = GVar->getInitializer();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000237 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000238 unsigned Align = TD->getPreferredAlignment(GVar);
239
Anton Korobeynikoved473292008-08-08 18:25:07 +0000240 printVisibility(name, GVar->getVisibility());
241
Chris Lattner4b7dadb2009-08-19 05:49:37 +0000242 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
243 TM));
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000244
245 if (C->isNullValue() && !GVar->hasSection()) {
246 if (!GVar->isThreadLocal() &&
Duncan Sands12da8ce2009-03-07 15:45:40 +0000247 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000248 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
249
Rafael Espindola6de96a12009-01-15 20:18:42 +0000250 if (GVar->hasLocalLinkage())
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000251 O << "\t.local " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000252
Chris Lattnere9a75a62009-08-22 21:43:10 +0000253 O << MAI->getCOMMDirective() << name << ',' << Size;
254 if (MAI->getCOMMDirectiveTakesAlignment())
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000255 O << ',' << (1 << Align);
256
257 O << '\n';
258 return;
259 }
260 }
261
262 switch (GVar->getLinkage()) {
Duncan Sands4581beb2009-03-11 20:14:15 +0000263 case GlobalValue::CommonLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +0000264 case GlobalValue::LinkOnceAnyLinkage:
265 case GlobalValue::LinkOnceODRLinkage:
266 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
267 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000268 // Nonnull linkonce -> weak
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000269 O << "\t.weak " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000270 break;
271 case GlobalValue::AppendingLinkage:
272 // FIXME: appending linkage variables should go into a section of
273 // their name or something. For now, just emit them as external.
274 case GlobalValue::ExternalLinkage:
275 // If external or appending, declare as a global symbol
Chris Lattnere9a75a62009-08-22 21:43:10 +0000276 O << MAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000277 // FALL THROUGH
Rafael Espindola6de96a12009-01-15 20:18:42 +0000278 case GlobalValue::PrivateLinkage:
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000279 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000280 case GlobalValue::InternalLinkage:
281 break;
282 case GlobalValue::GhostLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000283 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000284 case GlobalValue::DLLImportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000285 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000286 case GlobalValue::DLLExportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000287 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000288 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000289 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000290 }
291
Anton Korobeynikovadb93532008-08-07 09:53:38 +0000292 EmitAlignment(Align, GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000293
Chris Lattnere9a75a62009-08-22 21:43:10 +0000294 if (MAI->hasDotTypeDotSizeDirective()) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000295 O << "\t.type " << name << ",#object\n";
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000296 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000297 }
298
299 O << name << ":\n";
300 EmitGlobalConstant(C);
301}
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000302
303/// PrintAsmOperand - Print out an operand for an inline asm expression.
304///
305bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
306 unsigned AsmVariant,
307 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000308 if (ExtraCode && ExtraCode[0]) {
309 if (ExtraCode[1] != 0) return true; // Unknown modifier.
310
311 switch (ExtraCode[0]) {
312 default: return true; // Unknown modifier.
313 case 'r':
314 break;
315 }
316 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000317
318 printOperand(MI, OpNo);
319
320 return false;
321}
322
323bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
324 unsigned OpNo,
325 unsigned AsmVariant,
326 const char *ExtraCode) {
327 if (ExtraCode && ExtraCode[0])
328 return true; // Unknown modifier
329
330 O << '[';
331 printMemOperand(MI, OpNo);
332 O << ']';
333
334 return false;
335}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000336
Bob Wilson5a495fe2009-06-23 23:59:40 +0000337// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000338extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000339 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000340}