blob: ff11cdf03917fcd59925e8b74715bd1f3b59ba70 [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"
Jim Laskeya6211dc2006-09-06 18:34:40 +000027#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner5e693ed2009-07-28 03:13:23 +000028#include "llvm/Target/TargetLoweringObjectFile.h"
Daniel Dunbare8338102009-07-15 20:24:03 +000029#include "llvm/Target/TargetRegistry.h"
Torok Edwinfa040022009-07-08 19:04:27 +000030#include "llvm/Support/ErrorHandling.h"
David Greenea31f96c2009-07-14 20:18:05 +000031#include "llvm/Support/FormattedStream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000032#include "llvm/Support/Mangler.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/ADT/StringExtras.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/MathExtras.h"
37#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000038#include <cstring>
Owen Anderson36b92ca2008-07-10 01:44:27 +000039#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000040using namespace llvm;
41
Chris Lattner1ef9cd42006-12-19 22:59:26 +000042STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000043
Chris Lattner1ef9cd42006-12-19 22:59:26 +000044namespace {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000045 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Chris Lattner158e1f52006-02-05 05:50:24 +000046 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
49 ///
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
Owen Andersonc043d132009-06-26 21:45:04 +000052 unsigned BBNumber;
Bill Wendlingc5437ea2009-02-24 08:30:20 +000053 public:
David Greenea31f96c2009-07-14 20:18:05 +000054 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Daniel Dunbar75c12e12009-07-01 01:48:54 +000055 const TargetAsmInfo *T, bool V)
56 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
Chris Lattner158e1f52006-02-05 05:50:24 +000057
58 virtual const char *getPassName() const {
59 return "Sparc Assembly Printer";
60 }
61
Chris Lattner100865e2009-07-21 18:38:57 +000062 void PrintGlobalVariable(const GlobalVariable *GVar);
Chris Lattner158e1f52006-02-05 05:50:24 +000063 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000064 void printMemOperand(const MachineInstr *MI, int opNum,
65 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000066 void printCCOperand(const MachineInstr *MI, int opNum);
67
Chris Lattnerb94284b2009-08-08 01:32:19 +000068 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner158e1f52006-02-05 05:50:24 +000069 bool runOnMachineFunction(MachineFunction &F);
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
Chris Lattner3773afe2009-06-19 15:48:10 +000079
Evan Cheng32e53472008-02-02 08:39:46 +000080/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000081/// method to print assembly for each instruction.
82///
83bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendlingc5437ea2009-02-24 08:30:20 +000084 this->MF = &MF;
85
Chris Lattner158e1f52006-02-05 05:50:24 +000086 SetupMachineFunction(MF);
87
88 // Print out constants referenced by the function
89 EmitConstantPool(MF.getConstantPool());
90
91 // BBNumber is used here so that a given Printer will never give two
92 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner158e1f52006-02-05 05:50:24 +000093
94 O << "\n\n";
Chris Lattner158e1f52006-02-05 05:50:24 +000095
Chris Lattnerd4d255a2006-10-05 02:48:40 +000096 // Print out the label for the function.
97 const Function *F = MF.getFunction();
Chris Lattner50343292009-07-29 05:09:30 +000098 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Bill Wendling31ceb1b2009-06-30 22:38:32 +000099 EmitAlignment(MF.getAlignment(), F);
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000100 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikoved473292008-08-08 18:25:07 +0000101
102 printVisibility(CurrentFnName, F->getVisibility());
103
Chris Lattner158e1f52006-02-05 05:50:24 +0000104 O << "\t.type\t" << CurrentFnName << ", #function\n";
105 O << CurrentFnName << ":\n";
106
107 // Number each basic block so that we can consistently refer to them
108 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000109 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000110 NumberForBB.clear();
111 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
112 I != E; ++I) {
113 NumberForBB[I->getBasicBlock()] = BBNumber++;
114 }
115
116 // Print out code for the function.
117 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 I != E; ++I) {
119 // Print a label for the basic block.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000120 if (I != MF.begin()) {
Evan Chengc7990652008-02-28 00:43:03 +0000121 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000122 O << '\n';
123 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000124 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
125 II != E; ++II) {
126 // Print the assembly for the instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000127 printInstruction(II);
128 ++EmittedInsts;
129 }
130 }
131
132 // We didn't modify anything.
133 return false;
134}
135
136void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
137 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000138 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000139 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000140 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000141 O << "%hi(";
142 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000143 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
144 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000145 O << "%lo(";
146 CloseParen = true;
147 }
148 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000149 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000150 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendlingc24ea4f2008-02-26 21:11:01 +0000151 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Chris Lattner158e1f52006-02-05 05:50:24 +0000152 else
153 O << "%reg" << MO.getReg();
154 break;
155
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000156 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000157 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000158 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000159 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000160 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000161 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000162 case MachineOperand::MO_GlobalAddress:
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000163 O << Mang->getMangledName(MO.getGlobal());
Chris Lattner158e1f52006-02-05 05:50:24 +0000164 break;
165 case MachineOperand::MO_ExternalSymbol:
166 O << MO.getSymbolName();
167 break;
168 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000169 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000170 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000171 break;
172 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000173 llvm_unreachable("<unknown operand type>");
Chris Lattner158e1f52006-02-05 05:50:24 +0000174 }
175 if (CloseParen) O << ")";
176}
177
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000178void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
179 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000180 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000181
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000182 // If this is an ADD operand, emit it like normal operands.
183 if (Modifier && !strcmp(Modifier, "arith")) {
184 O << ", ";
185 printOperand(MI, opNum+1);
186 return;
187 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000188
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000189 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000190 MI->getOperand(opNum+1).getReg() == SP::G0)
191 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000192 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000193 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000194 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000195
Chris Lattner158e1f52006-02-05 05:50:24 +0000196 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000197 if (MI->getOperand(opNum+1).isGlobal() ||
198 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000199 O << "%lo(";
200 printOperand(MI, opNum+1);
201 O << ")";
202 } else {
203 printOperand(MI, opNum+1);
204 }
205}
206
207void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000208 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000209 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
210}
211
Chris Lattner100865e2009-07-21 18:38:57 +0000212void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000213 const TargetData *TD = TM.getTargetData();
214
215 if (!GVar->hasInitializer())
216 return; // External global require no code
217
218 // Check to see if this is a special global used by LLVM, if so, emit it.
219 if (EmitSpecialLLVMGlobal(GVar))
220 return;
221
222 O << "\n\n";
Chris Lattner8c9a96b2009-07-14 18:17:16 +0000223 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000224 Constant *C = GVar->getInitializer();
Duncan Sandsaf9eaa82009-05-09 07:06:46 +0000225 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000226 unsigned Align = TD->getPreferredAlignment(GVar);
227
Anton Korobeynikoved473292008-08-08 18:25:07 +0000228 printVisibility(name, GVar->getVisibility());
229
Chris Lattner50343292009-07-29 05:09:30 +0000230 SwitchToSection(getObjFileLowering().SectionForGlobal(GVar, Mang, TM));
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000231
232 if (C->isNullValue() && !GVar->hasSection()) {
233 if (!GVar->isThreadLocal() &&
Duncan Sands12da8ce2009-03-07 15:45:40 +0000234 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000235 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
236
Rafael Espindola6de96a12009-01-15 20:18:42 +0000237 if (GVar->hasLocalLinkage())
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000238 O << "\t.local " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000239
240 O << TAI->getCOMMDirective() << name << ',' << Size;
241 if (TAI->getCOMMDirectiveTakesAlignment())
242 O << ',' << (1 << Align);
243
244 O << '\n';
245 return;
246 }
247 }
248
249 switch (GVar->getLinkage()) {
Duncan Sands4581beb2009-03-11 20:14:15 +0000250 case GlobalValue::CommonLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +0000251 case GlobalValue::LinkOnceAnyLinkage:
252 case GlobalValue::LinkOnceODRLinkage:
253 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
254 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000255 // Nonnull linkonce -> weak
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000256 O << "\t.weak " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000257 break;
258 case GlobalValue::AppendingLinkage:
259 // FIXME: appending linkage variables should go into a section of
260 // their name or something. For now, just emit them as external.
261 case GlobalValue::ExternalLinkage:
262 // If external or appending, declare as a global symbol
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000263 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000264 // FALL THROUGH
Rafael Espindola6de96a12009-01-15 20:18:42 +0000265 case GlobalValue::PrivateLinkage:
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +0000266 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000267 case GlobalValue::InternalLinkage:
268 break;
269 case GlobalValue::GhostLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000270 llvm_unreachable("Should not have any unmaterialized functions!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000271 case GlobalValue::DLLImportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000272 llvm_unreachable("DLLImport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000273 case GlobalValue::DLLExportLinkage:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000274 llvm_unreachable("DLLExport linkage is not supported by this target!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000275 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +0000276 llvm_unreachable("Unknown linkage type!");
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000277 }
278
Anton Korobeynikovadb93532008-08-07 09:53:38 +0000279 EmitAlignment(Align, GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000280
281 if (TAI->hasDotTypeDotSizeDirective()) {
282 O << "\t.type " << name << ",#object\n";
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000283 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000284 }
285
286 O << name << ":\n";
287 EmitGlobalConstant(C);
288}
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000289
290/// PrintAsmOperand - Print out an operand for an inline asm expression.
291///
292bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
293 unsigned AsmVariant,
294 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000295 if (ExtraCode && ExtraCode[0]) {
296 if (ExtraCode[1] != 0) return true; // Unknown modifier.
297
298 switch (ExtraCode[0]) {
299 default: return true; // Unknown modifier.
300 case 'r':
301 break;
302 }
303 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000304
305 printOperand(MI, OpNo);
306
307 return false;
308}
309
310bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
311 unsigned OpNo,
312 unsigned AsmVariant,
313 const char *ExtraCode) {
314 if (ExtraCode && ExtraCode[0])
315 return true; // Unknown modifier
316
317 O << '[';
318 printMemOperand(MI, OpNo);
319 O << ']';
320
321 return false;
322}
Douglas Gregor1b731d52009-06-16 20:12:29 +0000323
Bob Wilson5a495fe2009-06-23 23:59:40 +0000324// Force static initialization.
Daniel Dunbare8338102009-07-15 20:24:03 +0000325extern "C" void LLVMInitializeSparcAsmPrinter() {
Daniel Dunbar5680b4f2009-07-25 06:49:55 +0000326 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);
Daniel Dunbare8338102009-07-15 20:24:03 +0000327}