blob: 424adaa7c3f435ebdf3c8285ba990357d5087c39 [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);
66 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
68 };
69} // end of anonymous namespace
70
71#include "SparcGenAsmWriter.inc"
72
73/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
74/// assembly code for a MachineFunction to the given output stream,
75/// using the given target machine description. This should work
76/// regardless of whether the function is in SSA form.
77///
Owen Anderson847b99b2008-08-21 00:14:44 +000078FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 TargetMachine &tm) {
80 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
81}
82
Evan Cheng8b988692008-02-02 08:39:46 +000083/// runOnMachineFunction - This uses the printInstruction()
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084/// method to print assembly for each instruction.
85///
86bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
87 SetupMachineFunction(MF);
88
89 // Print out constants referenced by the function
90 EmitConstantPool(MF.getConstantPool());
91
92 // BBNumber is used here so that a given Printer will never give two
93 // BBs the same name. (If you have a better way, please let me know!)
94 static unsigned BBNumber = 0;
95
96 O << "\n\n";
97 // What's my mangled name?
98 CurrentFnName = Mang->getValueName(MF.getFunction());
99
100 // Print out the label for the function.
101 const Function *F = MF.getFunction();
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000102 SwitchToSection(TAI->SectionForGlobal(F));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 EmitAlignment(4, F);
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000104 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000105
106 printVisibility(CurrentFnName, F->getVisibility());
107
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 O << "\t.type\t" << CurrentFnName << ", #function\n";
109 O << CurrentFnName << ":\n";
110
111 // Number each basic block so that we can consistently refer to them
112 // in PC-relative references.
113 // FIXME: Why not use the MBB numbers?
114 NumberForBB.clear();
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116 I != E; ++I) {
117 NumberForBB[I->getBasicBlock()] = BBNumber++;
118 }
119
120 // Print out code for the function.
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 I != E; ++I) {
123 // Print a label for the basic block.
124 if (I != MF.begin()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000125 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 O << '\n';
127 }
128 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
129 II != E; ++II) {
130 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 printInstruction(II);
132 ++EmittedInsts;
133 }
134 }
135
136 // We didn't modify anything.
137 return false;
138}
139
140void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
141 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman1e57df32008-02-10 18:45:23 +0000142 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 bool CloseParen = false;
144 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
145 O << "%hi(";
146 CloseParen = true;
147 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
148 && !MO.isRegister() && !MO.isImmediate()) {
149 O << "%lo(";
150 CloseParen = true;
151 }
152 switch (MO.getType()) {
153 case MachineOperand::MO_Register:
Dan Gohman1e57df32008-02-10 18:45:23 +0000154 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling8eeb9792008-02-26 21:11:01 +0000155 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 else
157 O << "%reg" << MO.getReg();
158 break;
159
160 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000161 O << (int)MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 break;
163 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000164 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 return;
166 case MachineOperand::MO_GlobalAddress:
167 O << Mang->getValueName(MO.getGlobal());
168 break;
169 case MachineOperand::MO_ExternalSymbol:
170 O << MO.getSymbolName();
171 break;
172 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng477013c2007-10-14 05:57:21 +0000173 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner6017d482007-12-30 23:10:15 +0000174 << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 break;
176 default:
177 O << "<unknown operand type>"; abort (); break;
178 }
179 if (CloseParen) O << ")";
180}
181
182void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
183 const char *Modifier) {
184 printOperand(MI, opNum);
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 // If this is an ADD operand, emit it like normal operands.
187 if (Modifier && !strcmp(Modifier, "arith")) {
188 O << ", ";
189 printOperand(MI, opNum+1);
190 return;
191 }
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 if (MI->getOperand(opNum+1).isRegister() &&
194 MI->getOperand(opNum+1).getReg() == SP::G0)
195 return; // don't print "+%g0"
196 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattnera96056a2007-12-30 20:49:49 +0000197 MI->getOperand(opNum+1).getImm() == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 return; // don't print "+0"
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 O << "+";
201 if (MI->getOperand(opNum+1).isGlobalAddress() ||
202 MI->getOperand(opNum+1).isConstantPoolIndex()) {
203 O << "%lo(";
204 printOperand(MI, opNum+1);
205 O << ")";
206 } else {
207 printOperand(MI, opNum+1);
208 }
209}
210
211void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000212 int CC = (int)MI->getOperand(opNum).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
214}
215
216
217
218bool SparcAsmPrinter::doInitialization(Module &M) {
219 Mang = new Mangler(M);
220 return false; // success
221}
222
223bool SparcAsmPrinter::doFinalization(Module &M) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 // Print out module-level global variables here.
225 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
226 I != E; ++I)
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000227 printModuleLevelGV(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000229 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230
Dan Gohman4a558a32007-07-25 19:33:14 +0000231 return AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232}
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000233
234void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
235 const TargetData *TD = TM.getTargetData();
236
237 if (!GVar->hasInitializer())
238 return; // External global require no code
239
240 // Check to see if this is a special global used by LLVM, if so, emit it.
241 if (EmitSpecialLLVMGlobal(GVar))
242 return;
243
244 O << "\n\n";
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000245 std::string name = Mang->getValueName(GVar);
246 Constant *C = GVar->getInitializer();
247 unsigned Size = TD->getABITypeSize(C->getType());
248 unsigned Align = TD->getPreferredAlignment(GVar);
249
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +0000250 printVisibility(name, GVar->getVisibility());
251
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000252 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000253
254 if (C->isNullValue() && !GVar->hasSection()) {
255 if (!GVar->isThreadLocal() &&
256 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
257 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
258
259 if (GVar->hasInternalLinkage())
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000260 O << "\t.local " << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000261
262 O << TAI->getCOMMDirective() << name << ',' << Size;
263 if (TAI->getCOMMDirectiveTakesAlignment())
264 O << ',' << (1 << Align);
265
266 O << '\n';
267 return;
268 }
269 }
270
271 switch (GVar->getLinkage()) {
272 case GlobalValue::CommonLinkage:
273 case GlobalValue::LinkOnceLinkage:
274 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
275 // Nonnull linkonce -> weak
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000276 O << "\t.weak " << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000277 break;
278 case GlobalValue::AppendingLinkage:
279 // FIXME: appending linkage variables should go into a section of
280 // their name or something. For now, just emit them as external.
281 case GlobalValue::ExternalLinkage:
282 // If external or appending, declare as a global symbol
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000283 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000284 // FALL THROUGH
285 case GlobalValue::InternalLinkage:
286 break;
287 case GlobalValue::GhostLinkage:
288 cerr << "Should not have any unmaterialized functions!\n";
289 abort();
290 case GlobalValue::DLLImportLinkage:
291 cerr << "DLLImport linkage is not supported by this target!\n";
292 abort();
293 case GlobalValue::DLLExportLinkage:
294 cerr << "DLLExport linkage is not supported by this target!\n";
295 abort();
296 default:
297 assert(0 && "Unknown linkage type!");
298 }
299
Anton Korobeynikovb59b2292008-08-07 09:53:38 +0000300 EmitAlignment(Align, GVar);
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000301
302 if (TAI->hasDotTypeDotSizeDirective()) {
303 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov140c7e72008-08-07 09:53:13 +0000304 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikovdb9a8952008-08-07 09:51:25 +0000305 }
306
307 O << name << ":\n";
308 EmitGlobalConstant(C);
309}