blob: cb2ea02837f215a5493cebf4c994d89faa5bc113 [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"
Chris Lattner158e1f52006-02-05 05:50:24 +000029#include "llvm/Support/Mangler.h"
Owen Anderson93719642008-08-21 00:14:44 +000030#include "llvm/Support/raw_ostream.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"
34#include "llvm/Support/MathExtras.h"
35#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000036#include <cstring>
Owen Anderson36b92ca2008-07-10 01:44:27 +000037#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000038using namespace llvm;
39
Chris Lattner1ef9cd42006-12-19 22:59:26 +000040STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000041
Chris Lattner1ef9cd42006-12-19 22:59:26 +000042namespace {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000043 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Chris Lattner158e1f52006-02-05 05:50:24 +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 Wendlingc5437ea2009-02-24 08:30:20 +000050 public:
Bill Wendling084669a2009-04-29 00:15:41 +000051 explicit SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
Bill Wendling026e5d72009-04-29 23:29:43 +000052 const TargetAsmInfo *T, CodeGenOpt::Level OL,
53 bool V)
Bill Wendling084669a2009-04-29 00:15:41 +000054 : AsmPrinter(O, TM, T, OL, V) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000055
56 virtual const char *getPassName() const {
57 return "Sparc Assembly Printer";
58 }
59
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +000060 void printModuleLevelGV(const GlobalVariable* GVar);
Chris Lattner158e1f52006-02-05 05:50:24 +000061 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000062 void printMemOperand(const MachineInstr *MI, int opNum,
63 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000064 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 Korobeynikov3db21732008-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);
Chris Lattner158e1f52006-02-05 05:50:24 +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 Anderson93719642008-08-21 00:14:44 +000084FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Bill Wendlingc5437ea2009-02-24 08:30:20 +000085 TargetMachine &tm,
Bill Wendling026e5d72009-04-29 23:29:43 +000086 CodeGenOpt::Level OptLevel,
Bill Wendling084669a2009-04-29 00:15:41 +000087 bool verbose) {
88 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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!)
105 static unsigned BBNumber = 0;
106
107 O << "\n\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000108
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000109 // Print out the label for the function.
110 const Function *F = MF.getFunction();
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000111 SwitchToSection(TAI->SectionForGlobal(F));
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000112 EmitAlignment(4, F);
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000113 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikoved473292008-08-08 18:25:07 +0000114
115 printVisibility(CurrentFnName, F->getVisibility());
116
Chris Lattner158e1f52006-02-05 05:50:24 +0000117 O << "\t.type\t" << CurrentFnName << ", #function\n";
118 O << CurrentFnName << ":\n";
119
120 // Number each basic block so that we can consistently refer to them
121 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000122 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000123 NumberForBB.clear();
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 NumberForBB[I->getBasicBlock()] = BBNumber++;
127 }
128
129 // Print out code for the function.
130 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
131 I != E; ++I) {
132 // Print a label for the basic block.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000133 if (I != MF.begin()) {
Evan Chengc7990652008-02-28 00:43:03 +0000134 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000135 O << '\n';
136 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138 II != E; ++II) {
139 // Print the assembly for the instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000140 printInstruction(II);
141 ++EmittedInsts;
142 }
143 }
144
145 // We didn't modify anything.
146 return false;
147}
148
149void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
150 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000151 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000152 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000153 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000154 O << "%hi(";
155 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000156 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
157 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000158 O << "%lo(";
159 CloseParen = true;
160 }
161 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000162 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000163 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendlingc24ea4f2008-02-26 21:11:01 +0000164 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Chris Lattner158e1f52006-02-05 05:50:24 +0000165 else
166 O << "%reg" << MO.getReg();
167 break;
168
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000169 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000170 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000171 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000172 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000173 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000174 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000175 case MachineOperand::MO_GlobalAddress:
Rafael Espindola6de96a12009-01-15 20:18:42 +0000176 {
177 const GlobalValue *GV = MO.getGlobal();
178 O << Mang->getValueName(GV);
179 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000180 break;
181 case MachineOperand::MO_ExternalSymbol:
182 O << MO.getSymbolName();
183 break;
184 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000185 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000186 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000187 break;
188 default:
189 O << "<unknown operand type>"; abort (); break;
190 }
191 if (CloseParen) O << ")";
192}
193
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000194void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
195 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000196 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000197
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000198 // If this is an ADD operand, emit it like normal operands.
199 if (Modifier && !strcmp(Modifier, "arith")) {
200 O << ", ";
201 printOperand(MI, opNum+1);
202 return;
203 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000204
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000205 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000206 MI->getOperand(opNum+1).getReg() == SP::G0)
207 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000208 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000209 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000210 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000211
Chris Lattner158e1f52006-02-05 05:50:24 +0000212 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000213 if (MI->getOperand(opNum+1).isGlobal() ||
214 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000215 O << "%lo(";
216 printOperand(MI, opNum+1);
217 O << ")";
218 } else {
219 printOperand(MI, opNum+1);
220 }
221}
222
223void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000224 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000225 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
226}
227
Chris Lattner158e1f52006-02-05 05:50:24 +0000228bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000229 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Chris Lattner158e1f52006-02-05 05:50:24 +0000230 return false; // success
231}
232
233bool SparcAsmPrinter::doFinalization(Module &M) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000234 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000235 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
236 I != E; ++I)
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000237 printModuleLevelGV(I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000238
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000239 O << '\n';
Chris Lattner158e1f52006-02-05 05:50:24 +0000240
Dan Gohmancf0a5342007-07-25 19:33:14 +0000241 return AsmPrinter::doFinalization(M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000242}
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000243
244void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
245 const TargetData *TD = TM.getTargetData();
246
247 if (!GVar->hasInitializer())
248 return; // External global require no code
249
250 // Check to see if this is a special global used by LLVM, if so, emit it.
251 if (EmitSpecialLLVMGlobal(GVar))
252 return;
253
254 O << "\n\n";
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000255 std::string name = Mang->getValueName(GVar);
256 Constant *C = GVar->getInitializer();
Devang Patel2cc6d182009-06-26 02:26:12 +0000257 if (isa<MDNode>(C) || isa<MDString>(C))
Devang Patel9d683022009-06-25 00:47:42 +0000258 return;
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000259 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000260 unsigned Align = TD->getPreferredAlignment(GVar);
261
Anton Korobeynikoved473292008-08-08 18:25:07 +0000262 printVisibility(name, GVar->getVisibility());
263
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000264 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000265
266 if (C->isNullValue() && !GVar->hasSection()) {
267 if (!GVar->isThreadLocal() &&
Duncan Sands12da8ce2009-03-07 15:45:40 +0000268 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000269 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
270
Rafael Espindola6de96a12009-01-15 20:18:42 +0000271 if (GVar->hasLocalLinkage())
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000272 O << "\t.local " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000273
274 O << TAI->getCOMMDirective() << name << ',' << Size;
275 if (TAI->getCOMMDirectiveTakesAlignment())
276 O << ',' << (1 << Align);
277
278 O << '\n';
279 return;
280 }
281 }
282
283 switch (GVar->getLinkage()) {
Duncan Sands4581beb2009-03-11 20:14:15 +0000284 case GlobalValue::CommonLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +0000285 case GlobalValue::LinkOnceAnyLinkage:
286 case GlobalValue::LinkOnceODRLinkage:
287 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
288 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000289 // Nonnull linkonce -> weak
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000290 O << "\t.weak " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000291 break;
292 case GlobalValue::AppendingLinkage:
293 // FIXME: appending linkage variables should go into a section of
294 // their name or something. For now, just emit them as external.
295 case GlobalValue::ExternalLinkage:
296 // If external or appending, declare as a global symbol
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000297 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000298 // FALL THROUGH
Rafael Espindola6de96a12009-01-15 20:18:42 +0000299 case GlobalValue::PrivateLinkage:
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000300 case GlobalValue::InternalLinkage:
301 break;
302 case GlobalValue::GhostLinkage:
303 cerr << "Should not have any unmaterialized functions!\n";
304 abort();
305 case GlobalValue::DLLImportLinkage:
306 cerr << "DLLImport linkage is not supported by this target!\n";
307 abort();
308 case GlobalValue::DLLExportLinkage:
309 cerr << "DLLExport linkage is not supported by this target!\n";
310 abort();
311 default:
312 assert(0 && "Unknown linkage type!");
313 }
314
Anton Korobeynikovadb93532008-08-07 09:53:38 +0000315 EmitAlignment(Align, GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000316
317 if (TAI->hasDotTypeDotSizeDirective()) {
318 O << "\t.type " << name << ",#object\n";
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000319 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000320 }
321
322 O << name << ":\n";
323 EmitGlobalConstant(C);
324}
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000325
326/// PrintAsmOperand - Print out an operand for an inline asm expression.
327///
328bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
329 unsigned AsmVariant,
330 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000331 if (ExtraCode && ExtraCode[0]) {
332 if (ExtraCode[1] != 0) return true; // Unknown modifier.
333
334 switch (ExtraCode[0]) {
335 default: return true; // Unknown modifier.
336 case 'r':
337 break;
338 }
339 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000340
341 printOperand(MI, OpNo);
342
343 return false;
344}
345
346bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
347 unsigned OpNo,
348 unsigned AsmVariant,
349 const char *ExtraCode) {
350 if (ExtraCode && ExtraCode[0])
351 return true; // Unknown modifier
352
353 O << '[';
354 printMemOperand(MI, OpNo);
355 O << ']';
356
357 return false;
358}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000359
Chris Lattner3773afe2009-06-19 15:48:10 +0000360namespace {
361 static struct Register {
362 Register() {
363 SparcTargetMachine::registerAsmPrinter(createSparcCodePrinterPass);
364 }
365 } Registrator;
366}
367
Bob Wilson5a495fe2009-06-23 23:59:40 +0000368// Force static initialization.
369extern "C" void LLVMInitializeSparcAsmPrinter() { }