blob: 8fa54883e37ba7c8cb99f95a8a08ce43a408d218 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
15#define DEBUG_TYPE "asm-printer"
16#include "Sparc.h"
17#include "SparcInstrInfo.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
21#include "llvm/CodeGen/AsmPrinter.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineInstr.h"
25#include "llvm/Target/TargetAsmInfo.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikov357a27d2008-02-20 11:08:44 +000035#include <cstring>
Owen Anderson80045bf2008-07-10 01:44:27 +000036#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
39STATISTIC(EmittedInsts, "Number of machine instrs printed");
40
41namespace {
42 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Owen Anderson847b99b2008-08-21 00:14:44 +000043 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 : AsmPrinter(O, TM, T) {
45 }
46
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 Korobeynikovdb9a8952008-08-07 09:51:25 +000058 void printModuleLevelGV(const GlobalVariable* GVar);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 void printOperand(const MachineInstr *MI, int opNum);
60 void printMemOperand(const MachineInstr *MI, int opNum,
61 const char *Modifier = 0);
62 void printCCOperand(const MachineInstr *MI, int opNum);
63
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +000066 std::string getSectionForFunction(const Function &F) const;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 };
70} // end of anonymous namespace
71
72#include "SparcGenAsmWriter.inc"
73
74/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
75/// assembly code for a MachineFunction to the given output stream,
76/// using the given target machine description. This should work
77/// regardless of whether the function is in SSA form.
78///
Owen Anderson847b99b2008-08-21 00:14:44 +000079FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 TargetMachine &tm) {
81 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
82}
83
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +000084// Substitute old hook with new one temporary
85std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const {
86 return TAI->SectionForGlobal(&F);
87}
88
Evan Cheng8b988692008-02-02 08:39:46 +000089/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090/// method to print assembly for each instruction.
91///
92bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
93 SetupMachineFunction(MF);
94
95 // Print out constants referenced by the function
96 EmitConstantPool(MF.getConstantPool());
97
98 // BBNumber is used here so that a given Printer will never give two
99 // BBs the same name. (If you have a better way, please let me know!)
100 static unsigned BBNumber = 0;
101
102 O << "\n\n";
103 // What's my mangled name?
104 CurrentFnName = Mang->getValueName(MF.getFunction());
105
106 // Print out the label for the function.
107 const Function *F = MF.getFunction();
108 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
109 EmitAlignment(4, F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000110 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000111
112 printVisibility(CurrentFnName, F->getVisibility());
113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 O << "\t.type\t" << CurrentFnName << ", #function\n";
115 O << CurrentFnName << ":\n";
116
117 // Number each basic block so that we can consistently refer to them
118 // in PC-relative references.
119 // FIXME: Why not use the MBB numbers?
120 NumberForBB.clear();
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 I != E; ++I) {
123 NumberForBB[I->getBasicBlock()] = BBNumber++;
124 }
125
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
130 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000131 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 O << '\n';
133 }
134 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
135 II != E; ++II) {
136 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 printInstruction(II);
138 ++EmittedInsts;
139 }
140 }
141
142 // We didn't modify anything.
143 return false;
144}
145
146void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
147 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000148 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 bool CloseParen = false;
150 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
151 O << "%hi(";
152 CloseParen = true;
153 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
154 && !MO.isRegister() && !MO.isImmediate()) {
155 O << "%lo(";
156 CloseParen = true;
157 }
158 switch (MO.getType()) {
159 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000160 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000161 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 else
163 O << "%reg" << MO.getReg();
164 break;
165
166 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000167 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 break;
169 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000170 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 return;
172 case MachineOperand::MO_GlobalAddress:
173 O << Mang->getValueName(MO.getGlobal());
174 break;
175 case MachineOperand::MO_ExternalSymbol:
176 O << MO.getSymbolName();
177 break;
178 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000179 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000180 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 break;
182 default:
183 O << "<unknown operand type>"; abort (); break;
184 }
185 if (CloseParen) O << ")";
186}
187
188void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
189 const char *Modifier) {
190 printOperand(MI, opNum);
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000191
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Korobeynikovdb9a8952008-08-07 09:51:25 +0000198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 if (MI->getOperand(opNum+1).isRegister() &&
200 MI->getOperand(opNum+1).getReg() == SP::G0)
201 return; // don't print "+%g0"
202 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000203 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 return; // don't print "+0"
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000205
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 O << "+";
207 if (MI->getOperand(opNum+1).isGlobalAddress() ||
208 MI->getOperand(opNum+1).isConstantPoolIndex()) {
209 O << "%lo(";
210 printOperand(MI, opNum+1);
211 O << ")";
212 } else {
213 printOperand(MI, opNum+1);
214 }
215}
216
217void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000218 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
220}
221
222
223
224bool SparcAsmPrinter::doInitialization(Module &M) {
225 Mang = new Mangler(M);
226 return false; // success
227}
228
229bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Print out module-level global variables here.
231 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
232 I != E; ++I)
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000233 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000235 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236
Dan Gohman4a558a32007-07-25 19:33:14 +0000237 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238}
Anton Korobeynikovdb9a8952008-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";
251 std::string SectionName = TAI->SectionForGlobal(GVar);
252 std::string name = Mang->getValueName(GVar);
253 Constant *C = GVar->getInitializer();
254 unsigned Size = TD->getABITypeSize(C->getType());
255 unsigned Align = TD->getPreferredAlignment(GVar);
256
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000257 printVisibility(name, GVar->getVisibility());
258
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000259 SwitchToDataSection(SectionName.c_str());
260
261 if (C->isNullValue() && !GVar->hasSection()) {
262 if (!GVar->isThreadLocal() &&
263 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
264 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
265
266 if (GVar->hasInternalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000267 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000268
269 O << TAI->getCOMMDirective() << name << ',' << Size;
270 if (TAI->getCOMMDirectiveTakesAlignment())
271 O << ',' << (1 << Align);
272
273 O << '\n';
274 return;
275 }
276 }
277
278 switch (GVar->getLinkage()) {
279 case GlobalValue::CommonLinkage:
280 case GlobalValue::LinkOnceLinkage:
281 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
282 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000283 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000284 break;
285 case GlobalValue::AppendingLinkage:
286 // FIXME: appending linkage variables should go into a section of
287 // their name or something. For now, just emit them as external.
288 case GlobalValue::ExternalLinkage:
289 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000290 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000291 // FALL THROUGH
292 case GlobalValue::InternalLinkage:
293 break;
294 case GlobalValue::GhostLinkage:
295 cerr << "Should not have any unmaterialized functions!\n";
296 abort();
297 case GlobalValue::DLLImportLinkage:
298 cerr << "DLLImport linkage is not supported by this target!\n";
299 abort();
300 case GlobalValue::DLLExportLinkage:
301 cerr << "DLLExport linkage is not supported by this target!\n";
302 abort();
303 default:
304 assert(0 && "Unknown linkage type!");
305 }
306
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000307 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000308
309 if (TAI->hasDotTypeDotSizeDirective()) {
310 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000311 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000312 }
313
314 O << name << ":\n";
315 EmitGlobalConstant(C);
316}