blob: 024e6924e70490e930c3b91929695bb09a8433ee [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"
Devang Patel9d683022009-06-25 00:47:42 +000022#include "llvm/MDNode.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000023#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendling0f4c5812009-02-18 23:12:06 +000024#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineInstr.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000028#include "llvm/Target/TargetAsmInfo.h"
Torok Edwinfa040022009-07-08 19:04:27 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000030#include "llvm/Support/Mangler.h"
Owen Anderson93719642008-08-21 00:14:44 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000032#include "llvm/ADT/Statistic.h"
33#include "llvm/ADT/StringExtras.h"
34#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/MathExtras.h"
36#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000037#include <cstring>
Owen Anderson36b92ca2008-07-10 01:44:27 +000038#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000039using namespace llvm;
40
Chris Lattner1ef9cd42006-12-19 22:59:26 +000041STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000042
Chris Lattner1ef9cd42006-12-19 22:59:26 +000043namespace {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000044 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Chris Lattner158e1f52006-02-05 05:50:24 +000045 /// We name each basic block in a Function with a unique number, so
46 /// that we can consistently refer to them later. This is cleared
47 /// at the beginning of each call to runOnMachineFunction().
48 ///
49 typedef std::map<const Value *, unsigned> ValueMapTy;
50 ValueMapTy NumberForBB;
Owen Andersonc043d132009-06-26 21:45:04 +000051 unsigned BBNumber;
Bill Wendlingc5437ea2009-02-24 08:30:20 +000052 public:
Bill Wendling084669a2009-04-29 00:15:41 +000053 explicit SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000054 const TargetAsmInfo *T, bool V)
55 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000056
57 virtual const char *getPassName() const {
58 return "Sparc Assembly Printer";
59 }
60
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +000061 void printModuleLevelGV(const GlobalVariable* GVar);
Chris Lattner158e1f52006-02-05 05:50:24 +000062 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000063 void printMemOperand(const MachineInstr *MI, int opNum,
64 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000065 void printCCOperand(const MachineInstr *MI, int opNum);
66
67 bool printInstruction(const MachineInstr *MI); // autogenerated.
68 bool runOnMachineFunction(MachineFunction &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
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
80/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
81/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
84///
Owen Anderson93719642008-08-21 00:14:44 +000085FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Bill Wendlingc5437ea2009-02-24 08:30:20 +000086 TargetMachine &tm,
Bill Wendling084669a2009-04-29 00:15:41 +000087 bool verbose) {
Daniel Dunbar75c12e12009-07-01 01:48:54 +000088 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Chris Lattner158e1f52006-02-05 05:50:24 +000089}
90
Chris Lattner3773afe2009-06-19 15:48:10 +000091
Evan Cheng32e53472008-02-02 08:39:46 +000092/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000093/// method to print assembly for each instruction.
94///
95bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000096 this->MF = &MF;
97
Chris Lattner158e1f52006-02-05 05:50:24 +000098 SetupMachineFunction(MF);
99
100 // Print out constants referenced by the function
101 EmitConstantPool(MF.getConstantPool());
102
103 // BBNumber is used here so that a given Printer will never give two
104 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner158e1f52006-02-05 05:50:24 +0000105
106 O << "\n\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000107
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000108 // Print out the label for the function.
109 const Function *F = MF.getFunction();
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000110 SwitchToSection(TAI->SectionForGlobal(F));
Bill Wendling31ceb1b2009-06-30 22:38:32 +0000111 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000112 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikoved473292008-08-08 18:25:07 +0000113
114 printVisibility(CurrentFnName, F->getVisibility());
115
Chris Lattner158e1f52006-02-05 05:50:24 +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.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000121 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000122 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.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000132 if (I != MF.begin()) {
Evan Chengc7990652008-02-28 00:43:03 +0000133 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000134 O << '\n';
135 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000136 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137 II != E; ++II) {
138 // Print the assembly for the instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +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 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:
Evan Chengcdf36092007-10-14 05:57:21 +0000181 O << TAI->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 Lattner158e1f52006-02-05 05:50:24 +0000224bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000225 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Chris Lattner158e1f52006-02-05 05:50:24 +0000226 return false; // success
227}
228
229bool SparcAsmPrinter::doFinalization(Module &M) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000230 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000231 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
232 I != E; ++I)
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000233 printModuleLevelGV(I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000234
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000235 O << '\n';
Chris Lattner158e1f52006-02-05 05:50:24 +0000236
Dan Gohmancf0a5342007-07-25 19:33:14 +0000237 return AsmPrinter::doFinalization(M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000238}
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000239
240void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
241 const TargetData *TD = TM.getTargetData();
242
243 if (!GVar->hasInitializer())
244 return; // External global require no code
245
246 // Check to see if this is a special global used by LLVM, if so, emit it.
247 if (EmitSpecialLLVMGlobal(GVar))
248 return;
249
250 O << "\n\n";
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000251 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000252 Constant *C = GVar->getInitializer();
Devang Patel2cc6d182009-06-26 02:26:12 +0000253 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patel9d683022009-06-25 00:47:42 +0000254 return;
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000255 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000256 unsigned Align = TD->getPreferredAlignment(GVar);
257
Anton Korobeynikoved473292008-08-08 18:25:07 +0000258 printVisibility(name, GVar->getVisibility());
259
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000260 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000261
262 if (C->isNullValue() && !GVar->hasSection()) {
263 if (!GVar->isThreadLocal() &&
Duncan Sands12da8ce2009-03-07 15:45:40 +0000264 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000265 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
266
Rafael Espindola6de96a12009-01-15 20:18:42 +0000267 if (GVar->hasLocalLinkage())
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000268 O << "\t.local " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000269
270 O << TAI->getCOMMDirective() << name << ',' << Size;
271 if (TAI->getCOMMDirectiveTakesAlignment())
272 O << ',' << (1 << Align);
273
274 O << '\n';
275 return;
276 }
277 }
278
279 switch (GVar->getLinkage()) {
Duncan Sands4581beb2009-03-11 20:14:15 +0000280 case GlobalValue::CommonLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +0000281 case GlobalValue::LinkOnceAnyLinkage:
282 case GlobalValue::LinkOnceODRLinkage:
283 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
284 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000285 // Nonnull linkonce -> weak
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000286 O << "\t.weak " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000287 break;
288 case GlobalValue::AppendingLinkage:
289 // FIXME: appending linkage variables should go into a section of
290 // their name or something. For now, just emit them as external.
291 case GlobalValue::ExternalLinkage:
292 // If external or appending, declare as a global symbol
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000293 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000294 // FALL THROUGH
Rafael Espindola6de96a12009-01-15 20:18:42 +0000295 case GlobalValue::PrivateLinkage:
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000296 case GlobalValue::InternalLinkage:
297 break;
298 case GlobalValue::GhostLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000299 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000300 case GlobalValue::DLLImportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000301 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000302 case GlobalValue::DLLExportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000303 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000304 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000305 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000306 }
307
Anton Korobeynikovadb93532008-08-07 09:53:38 +0000308 EmitAlignment(Align, GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000309
310 if (TAI->hasDotTypeDotSizeDirective()) {
311 O << "\t.type " << name << ",#object\n";
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000312 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000313 }
314
315 O << name << ":\n";
316 EmitGlobalConstant(C);
317}
Anton Korobeynikov3db21732008-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 Korobeynikovb80b4852008-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 Korobeynikov3db21732008-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}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000352
Chris Lattner3773afe2009-06-19 15:48:10 +0000353namespace {
354 static struct Register {
355 Register() {
356 SparcTargetMachine::registerAsmPrinter(createSparcCodePrinterPass);
357 }
358 } Registrator;
359}
360
Bob Wilson5a495fe2009-06-23 23:59:40 +0000361// Force static initialization.
362extern "C" void LLVMInitializeSparcAsmPrinter() { }