blob: f720d47570eda8f46ba407496bc648e4c839ffac [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke4acfd032004-03-04 06:00:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke4acfd032004-03-04 06:00:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
Chris Lattner7c90f732006-02-05 05:50:24 +000011// of machine-dependent LLVM code to GAS-format SPARC assembly language.
Brian Gaeke4acfd032004-03-04 06:00:41 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
Chris Lattner1dbed162005-12-17 07:04:29 +000021#include "llvm/CodeGen/AsmPrinter.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000022#include "llvm/CodeGen/DwarfWriter.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineConstantPool.h"
25#include "llvm/CodeGen/MachineInstr.h"
Jim Laskey563321a2006-09-06 18:34:40 +000026#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000027#include "llvm/Target/TargetData.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000030#include "llvm/Support/raw_ostream.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/ADT/StringExtras.h"
33#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000034#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000035#include <cctype>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000036#include <cstring>
Owen Andersonc0078482008-07-10 01:44:27 +000037#include <map>
Brian Gaeke4acfd032004-03-04 06:00:41 +000038using namespace llvm;
39
Chris Lattner95b2c7d2006-12-19 22:59:26 +000040STATISTIC(EmittedInsts, "Number of machine instrs printed");
Brian Gaeke4acfd032004-03-04 06:00:41 +000041
Chris Lattner95b2c7d2006-12-19 22:59:26 +000042namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000043 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Owen Andersoncb371882008-08-21 00:14:44 +000044 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000045 : AsmPrinter(O, TM, T) {
46 }
Brian Gaeke4acfd032004-03-04 06:00:41 +000047
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
51 ///
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
54
Brian Gaeke4acfd032004-03-04 06:00:41 +000055 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000056 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000057 }
58
Anton Korobeynikov5b794b92008-08-07 09:51:25 +000059 void printModuleLevelGV(const GlobalVariable* GVar);
Brian Gaeke446ae112004-06-15 19:52:59 +000060 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000061 void printMemOperand(const MachineInstr *MI, int opNum,
62 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000063 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000064
Chris Lattner994b7352005-12-16 06:34:17 +000065 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000066 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000067 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
Anton Korobeynikovf3693302008-10-10 10:15:03 +000069 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
71 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
Brian Gaeke4acfd032004-03-04 06:00:41 +000073 };
74} // end of anonymous namespace
75
Chris Lattner7c90f732006-02-05 05:50:24 +000076#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000077
Chris Lattner7c90f732006-02-05 05:50:24 +000078/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
Brian Gaeke4acfd032004-03-04 06:00:41 +000079/// assembly code for a MachineFunction to the given output stream,
80/// using the given target machine description. This should work
81/// regardless of whether the function is in SSA form.
82///
Owen Andersoncb371882008-08-21 00:14:44 +000083FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Chris Lattner7c90f732006-02-05 05:50:24 +000084 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000085 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Brian Gaeke4acfd032004-03-04 06:00:41 +000086}
87
Evan Cheng4eecdeb2008-02-02 08:39:46 +000088/// runOnMachineFunction - This uses the printInstruction()
Brian Gaeke4acfd032004-03-04 06:00:41 +000089/// method to print assembly for each instruction.
90///
Chris Lattner7c90f732006-02-05 05:50:24 +000091bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000092 SetupMachineFunction(MF);
93
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000094 // Print out constants referenced by the function
95 EmitConstantPool(MF.getConstantPool());
96
Brian Gaeke4acfd032004-03-04 06:00:41 +000097 // BBNumber is used here so that a given Printer will never give two
98 // BBs the same name. (If you have a better way, please let me know!)
99 static unsigned BBNumber = 0;
100
101 O << "\n\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000102
Chris Lattner29bd9e12006-10-05 02:48:40 +0000103 // Print out the label for the function.
104 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000105 SwitchToSection(TAI->SectionForGlobal(F));
Chris Lattner29bd9e12006-10-05 02:48:40 +0000106 EmitAlignment(4, F);
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000107 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000108
109 printVisibility(CurrentFnName, F->getVisibility());
110
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000111 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000112 O << CurrentFnName << ":\n";
113
114 // Number each basic block so that we can consistently refer to them
115 // in PC-relative references.
Chris Lattner29bd9e12006-10-05 02:48:40 +0000116 // FIXME: Why not use the MBB numbers?
Brian Gaeke4acfd032004-03-04 06:00:41 +0000117 NumberForBB.clear();
118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
119 I != E; ++I) {
120 NumberForBB[I->getBasicBlock()] = BBNumber++;
121 }
122
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 I != E; ++I) {
126 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000127 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000128 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000129 O << '\n';
130 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000131 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000132 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000133 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000134 printInstruction(II);
Chris Lattner1dbed162005-12-17 07:04:29 +0000135 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000136 }
137 }
138
139 // We didn't modify anything.
140 return false;
141}
142
Chris Lattner7c90f732006-02-05 05:50:24 +0000143void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000144 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000145 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000146 bool CloseParen = false;
Dan Gohmand735b802008-10-03 15:45:36 +0000147 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000148 O << "%hi(";
149 CloseParen = true;
Dan Gohmand735b802008-10-03 15:45:36 +0000150 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
151 !MO.isReg() && !MO.isImm()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000152 O << "%lo(";
153 CloseParen = true;
154 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000155 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000156 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000157 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling74ab84c2008-02-26 21:11:01 +0000158 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000159 else
160 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000161 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000162
Chris Lattner63b3d712006-05-04 17:21:20 +0000163 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000164 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +0000165 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000166 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000167 printBasicBlockLabel(MO.getMBB());
Brian Gaeke09c13092004-06-17 19:39:23 +0000168 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000169 case MachineOperand::MO_GlobalAddress:
Rafael Espindolabb46f522009-01-15 20:18:42 +0000170 {
171 const GlobalValue *GV = MO.getGlobal();
172 O << Mang->getValueName(GV);
173 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000174 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000175 case MachineOperand::MO_ExternalSymbol:
176 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000177 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000178 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000179 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000180 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000181 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000182 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000183 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000184 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000185 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000186}
187
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000188void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
189 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000190 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000191
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000192 // If this is an ADD operand, emit it like normal operands.
193 if (Modifier && !strcmp(Modifier, "arith")) {
194 O << ", ";
195 printOperand(MI, opNum+1);
196 return;
197 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000198
Dan Gohmand735b802008-10-03 15:45:36 +0000199 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000200 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000201 return; // don't print "+%g0"
Dan Gohmand735b802008-10-03 15:45:36 +0000202 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000203 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000204 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000205
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000206 O << "+";
Dan Gohmand735b802008-10-03 15:45:36 +0000207 if (MI->getOperand(opNum+1).isGlobal() ||
208 MI->getOperand(opNum+1).isCPI()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000209 O << "%lo(";
210 printOperand(MI, opNum+1);
211 O << ")";
212 } else {
213 printOperand(MI, opNum+1);
214 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000215}
216
Chris Lattner7c90f732006-02-05 05:50:24 +0000217void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000218 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000219 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000220}
221
Chris Lattner7c90f732006-02-05 05:50:24 +0000222bool SparcAsmPrinter::doInitialization(Module &M) {
Rafael Espindolabb46f522009-01-15 20:18:42 +0000223 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000224 return false; // success
225}
226
Chris Lattner7c90f732006-02-05 05:50:24 +0000227bool SparcAsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000228 // Print out module-level global variables here.
Chris Lattner04f96742006-03-09 06:14:35 +0000229 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
230 I != E; ++I)
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000231 printModuleLevelGV(I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000232
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000233 O << '\n';
Brian Gaeke4acfd032004-03-04 06:00:41 +0000234
Dan Gohmanb8275a32007-07-25 19:33:14 +0000235 return AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000236}
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000237
238void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
239 const TargetData *TD = TM.getTargetData();
240
241 if (!GVar->hasInitializer())
242 return; // External global require no code
243
244 // Check to see if this is a special global used by LLVM, if so, emit it.
245 if (EmitSpecialLLVMGlobal(GVar))
246 return;
247
248 O << "\n\n";
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000249 std::string name = Mang->getValueName(GVar);
250 Constant *C = GVar->getInitializer();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000251 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000252 unsigned Align = TD->getPreferredAlignment(GVar);
253
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000254 printVisibility(name, GVar->getVisibility());
255
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000256 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000257
258 if (C->isNullValue() && !GVar->hasSection()) {
259 if (!GVar->isThreadLocal() &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000260 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000261 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
262
Rafael Espindolabb46f522009-01-15 20:18:42 +0000263 if (GVar->hasLocalLinkage())
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000264 O << "\t.local " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000265
266 O << TAI->getCOMMDirective() << name << ',' << Size;
267 if (TAI->getCOMMDirectiveTakesAlignment())
268 O << ',' << (1 << Align);
269
270 O << '\n';
271 return;
272 }
273 }
274
275 switch (GVar->getLinkage()) {
276 case GlobalValue::CommonLinkage:
277 case GlobalValue::LinkOnceLinkage:
278 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
279 // Nonnull linkonce -> weak
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000280 O << "\t.weak " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000281 break;
282 case GlobalValue::AppendingLinkage:
283 // FIXME: appending linkage variables should go into a section of
284 // their name or something. For now, just emit them as external.
285 case GlobalValue::ExternalLinkage:
286 // If external or appending, declare as a global symbol
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000287 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000288 // FALL THROUGH
Rafael Espindolabb46f522009-01-15 20:18:42 +0000289 case GlobalValue::PrivateLinkage:
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000290 case GlobalValue::InternalLinkage:
291 break;
292 case GlobalValue::GhostLinkage:
293 cerr << "Should not have any unmaterialized functions!\n";
294 abort();
295 case GlobalValue::DLLImportLinkage:
296 cerr << "DLLImport linkage is not supported by this target!\n";
297 abort();
298 case GlobalValue::DLLExportLinkage:
299 cerr << "DLLExport linkage is not supported by this target!\n";
300 abort();
301 default:
302 assert(0 && "Unknown linkage type!");
303 }
304
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000305 EmitAlignment(Align, GVar);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000306
307 if (TAI->hasDotTypeDotSizeDirective()) {
308 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000309 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000310 }
311
312 O << name << ":\n";
313 EmitGlobalConstant(C);
314}
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000315
316/// PrintAsmOperand - Print out an operand for an inline asm expression.
317///
318bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
319 unsigned AsmVariant,
320 const char *ExtraCode) {
Anton Korobeynikov4cf5e2e2008-10-10 20:29:50 +0000321 if (ExtraCode && ExtraCode[0]) {
322 if (ExtraCode[1] != 0) return true; // Unknown modifier.
323
324 switch (ExtraCode[0]) {
325 default: return true; // Unknown modifier.
326 case 'r':
327 break;
328 }
329 }
Anton Korobeynikovf3693302008-10-10 10:15:03 +0000330
331 printOperand(MI, OpNo);
332
333 return false;
334}
335
336bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
337 unsigned OpNo,
338 unsigned AsmVariant,
339 const char *ExtraCode) {
340 if (ExtraCode && ExtraCode[0])
341 return true; // Unknown modifier
342
343 O << '[';
344 printMemOperand(MI, OpNo);
345 O << ']';
346
347 return false;
348}