blob: af7c4d9cd1e401147abe175c850e106eef423a7a [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"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000021#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineInstr.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
Owen Anderson93719642008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000030#include "llvm/ADT/Statistic.h"
31#include "llvm/ADT/StringExtras.h"
32#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/MathExtras.h"
34#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000035#include <cstring>
Owen Anderson36b92ca2008-07-10 01:44:27 +000036#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000037using namespace llvm;
38
Chris Lattner1ef9cd42006-12-19 22:59:26 +000039STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000040
Chris Lattner1ef9cd42006-12-19 22:59:26 +000041namespace {
Jim Laskeya6211dc2006-09-06 18:34:40 +000042 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Owen Anderson93719642008-08-21 00:14:44 +000043 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000044 : AsmPrinter(O, TM, T) {
45 }
Chris Lattner158e1f52006-02-05 05:50:24 +000046
47 /// 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;
53
54 virtual const char *getPassName() const {
55 return "Sparc Assembly Printer";
56 }
57
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +000058 void printModuleLevelGV(const GlobalVariable* GVar);
Chris Lattner158e1f52006-02-05 05:50:24 +000059 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000060 void printMemOperand(const MachineInstr *MI, int opNum,
61 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000062 void printCCOperand(const MachineInstr *MI, int opNum);
63
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 bool runOnMachineFunction(MachineFunction &F);
66 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
Anton Korobeynikov3db21732008-10-10 10:15:03 +000068 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69 unsigned AsmVariant, const char *ExtraCode);
70 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
71 unsigned AsmVariant, const char *ExtraCode);
Chris Lattner158e1f52006-02-05 05:50:24 +000072 };
73} // end of anonymous namespace
74
75#include "SparcGenAsmWriter.inc"
76
77/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
78/// assembly code for a MachineFunction to the given output stream,
79/// using the given target machine description. This should work
80/// regardless of whether the function is in SSA form.
81///
Owen Anderson93719642008-08-21 00:14:44 +000082FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Chris Lattner158e1f52006-02-05 05:50:24 +000083 TargetMachine &tm) {
Jim Laskey261779b2006-09-07 22:06:40 +000084 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattner158e1f52006-02-05 05:50:24 +000085}
86
Evan Cheng32e53472008-02-02 08:39:46 +000087/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000088/// method to print assembly for each instruction.
89///
90bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
91 SetupMachineFunction(MF);
92
93 // Print out constants referenced by the function
94 EmitConstantPool(MF.getConstantPool());
95
96 // BBNumber is used here so that a given Printer will never give two
97 // BBs the same name. (If you have a better way, please let me know!)
98 static unsigned BBNumber = 0;
99
100 O << "\n\n";
101 // What's my mangled name?
102 CurrentFnName = Mang->getValueName(MF.getFunction());
103
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000104 // Print out the label for the function.
105 const Function *F = MF.getFunction();
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000106 SwitchToSection(TAI->SectionForGlobal(F));
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000107 EmitAlignment(4, F);
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000108 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikoved473292008-08-08 18:25:07 +0000109
110 printVisibility(CurrentFnName, F->getVisibility());
111
Chris Lattner158e1f52006-02-05 05:50:24 +0000112 O << "\t.type\t" << CurrentFnName << ", #function\n";
113 O << CurrentFnName << ":\n";
114
115 // Number each basic block so that we can consistently refer to them
116 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000117 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000118 NumberForBB.clear();
119 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
120 I != E; ++I) {
121 NumberForBB[I->getBasicBlock()] = BBNumber++;
122 }
123
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 I != E; ++I) {
127 // Print a label for the basic block.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000128 if (I != MF.begin()) {
Evan Chengc7990652008-02-28 00:43:03 +0000129 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000130 O << '\n';
131 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
133 II != E; ++II) {
134 // Print the assembly for the instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000135 printInstruction(II);
136 ++EmittedInsts;
137 }
138 }
139
140 // We didn't modify anything.
141 return false;
142}
143
144void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
145 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000146 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000147 bool CloseParen = false;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000148 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000149 O << "%hi(";
150 CloseParen = true;
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000151 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
152 !MO.isReg() && !MO.isImm()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000153 O << "%lo(";
154 CloseParen = true;
155 }
156 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000157 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000158 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendlingc24ea4f2008-02-26 21:11:01 +0000159 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Chris Lattner158e1f52006-02-05 05:50:24 +0000160 else
161 O << "%reg" << MO.getReg();
162 break;
163
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000164 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000165 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000166 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000167 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000168 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000169 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000170 case MachineOperand::MO_GlobalAddress:
171 O << Mang->getValueName(MO.getGlobal());
172 break;
173 case MachineOperand::MO_ExternalSymbol:
174 O << MO.getSymbolName();
175 break;
176 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000177 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000178 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000179 break;
180 default:
181 O << "<unknown operand type>"; abort (); break;
182 }
183 if (CloseParen) O << ")";
184}
185
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000186void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
187 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000188 printOperand(MI, opNum);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000189
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000190 // If this is an ADD operand, emit it like normal operands.
191 if (Modifier && !strcmp(Modifier, "arith")) {
192 O << ", ";
193 printOperand(MI, opNum+1);
194 return;
195 }
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000196
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000197 if (MI->getOperand(opNum+1).isReg() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000198 MI->getOperand(opNum+1).getReg() == SP::G0)
199 return; // don't print "+%g0"
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000200 if (MI->getOperand(opNum+1).isImm() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000201 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000202 return; // don't print "+0"
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000203
Chris Lattner158e1f52006-02-05 05:50:24 +0000204 O << "+";
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000205 if (MI->getOperand(opNum+1).isGlobal() ||
206 MI->getOperand(opNum+1).isCPI()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000207 O << "%lo(";
208 printOperand(MI, opNum+1);
209 O << ")";
210 } else {
211 printOperand(MI, opNum+1);
212 }
213}
214
215void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000216 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000217 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
218}
219
Chris Lattner158e1f52006-02-05 05:50:24 +0000220bool SparcAsmPrinter::doInitialization(Module &M) {
221 Mang = new Mangler(M);
222 return false; // success
223}
224
225bool SparcAsmPrinter::doFinalization(Module &M) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000226 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000227 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
228 I != E; ++I)
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000229 printModuleLevelGV(I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000230
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000231 O << '\n';
Chris Lattner158e1f52006-02-05 05:50:24 +0000232
Dan Gohmancf0a5342007-07-25 19:33:14 +0000233 return AsmPrinter::doFinalization(M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000234}
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000235
236void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
237 const TargetData *TD = TM.getTargetData();
238
239 if (!GVar->hasInitializer())
240 return; // External global require no code
241
242 // Check to see if this is a special global used by LLVM, if so, emit it.
243 if (EmitSpecialLLVMGlobal(GVar))
244 return;
245
246 O << "\n\n";
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000247 std::string name = Mang->getValueName(GVar);
248 Constant *C = GVar->getInitializer();
Duncan Sandsdc020f92009-01-12 20:38:59 +0000249 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000250 unsigned Align = TD->getPreferredAlignment(GVar);
251
Anton Korobeynikoved473292008-08-08 18:25:07 +0000252 printVisibility(name, GVar->getVisibility());
253
Anton Korobeynikov076e9052008-09-24 22:14:23 +0000254 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000255
256 if (C->isNullValue() && !GVar->hasSection()) {
257 if (!GVar->isThreadLocal() &&
Duncan Sands08d91172008-09-29 11:25:42 +0000258 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000259 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
260
261 if (GVar->hasInternalLinkage())
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000262 O << "\t.local " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000263
264 O << TAI->getCOMMDirective() << name << ',' << Size;
265 if (TAI->getCOMMDirectiveTakesAlignment())
266 O << ',' << (1 << Align);
267
268 O << '\n';
269 return;
270 }
271 }
272
273 switch (GVar->getLinkage()) {
274 case GlobalValue::CommonLinkage:
275 case GlobalValue::LinkOnceLinkage:
276 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
277 // Nonnull linkonce -> weak
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000278 O << "\t.weak " << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000279 break;
280 case GlobalValue::AppendingLinkage:
281 // FIXME: appending linkage variables should go into a section of
282 // their name or something. For now, just emit them as external.
283 case GlobalValue::ExternalLinkage:
284 // If external or appending, declare as a global symbol
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000285 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000286 // FALL THROUGH
287 case GlobalValue::InternalLinkage:
288 break;
289 case GlobalValue::GhostLinkage:
290 cerr << "Should not have any unmaterialized functions!\n";
291 abort();
292 case GlobalValue::DLLImportLinkage:
293 cerr << "DLLImport linkage is not supported by this target!\n";
294 abort();
295 case GlobalValue::DLLExportLinkage:
296 cerr << "DLLExport linkage is not supported by this target!\n";
297 abort();
298 default:
299 assert(0 && "Unknown linkage type!");
300 }
301
Anton Korobeynikovadb93532008-08-07 09:53:38 +0000302 EmitAlignment(Align, GVar);
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000303
304 if (TAI->hasDotTypeDotSizeDirective()) {
305 O << "\t.type " << name << ",#object\n";
Anton Korobeynikovadcf3f92008-08-07 09:53:13 +0000306 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov1a11e8a2008-08-07 09:51:25 +0000307 }
308
309 O << name << ":\n";
310 EmitGlobalConstant(C);
311}
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000312
313/// PrintAsmOperand - Print out an operand for an inline asm expression.
314///
315bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
316 unsigned AsmVariant,
317 const char *ExtraCode) {
Anton Korobeynikovb80b4852008-10-10 20:29:50 +0000318 if (ExtraCode && ExtraCode[0]) {
319 if (ExtraCode[1] != 0) return true; // Unknown modifier.
320
321 switch (ExtraCode[0]) {
322 default: return true; // Unknown modifier.
323 case 'r':
324 break;
325 }
326 }
Anton Korobeynikov3db21732008-10-10 10:15:03 +0000327
328 printOperand(MI, OpNo);
329
330 return false;
331}
332
333bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
334 unsigned OpNo,
335 unsigned AsmVariant,
336 const char *ExtraCode) {
337 if (ExtraCode && ExtraCode[0])
338 return true; // Unknown modifier
339
340 O << '[';
341 printMemOperand(MI, OpNo);
342 O << ']';
343
344 return false;
345}