blob: 458b1531915c6639f730b53ac5d9fd3cb34127e5 [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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#include "Sparc.h"
16#include "SparcInstrInfo.h"
17#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000020#include "llvm/CodeGen/AsmPrinter.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstr.h"
Jim Laskeya6211dc2006-09-06 18:34:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Mangler.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/MathExtras.h"
32#include <cctype>
Chris Lattner158e1f52006-02-05 05:50:24 +000033using namespace llvm;
34
35namespace {
Chris Lattner700b8732006-12-06 17:46:33 +000036 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000037
Jim Laskeya6211dc2006-09-06 18:34:40 +000038 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Jim Laskey261779b2006-09-07 22:06:40 +000039 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000040 : AsmPrinter(O, TM, T) {
41 }
Chris Lattner158e1f52006-02-05 05:50:24 +000042
43 /// We name each basic block in a Function with a unique number, so
44 /// that we can consistently refer to them later. This is cleared
45 /// at the beginning of each call to runOnMachineFunction().
46 ///
47 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
49
50 virtual const char *getPassName() const {
51 return "Sparc Assembly Printer";
52 }
53
54 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000055 void printMemOperand(const MachineInstr *MI, int opNum,
56 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000057 void printCCOperand(const MachineInstr *MI, int opNum);
58
59 bool printInstruction(const MachineInstr *MI); // autogenerated.
60 bool runOnMachineFunction(MachineFunction &F);
61 bool doInitialization(Module &M);
62 bool doFinalization(Module &M);
63 };
64} // end of anonymous namespace
65
66#include "SparcGenAsmWriter.inc"
67
68/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
69/// assembly code for a MachineFunction to the given output stream,
70/// using the given target machine description. This should work
71/// regardless of whether the function is in SSA form.
72///
73FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
74 TargetMachine &tm) {
Jim Laskey261779b2006-09-07 22:06:40 +000075 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattner158e1f52006-02-05 05:50:24 +000076}
77
78/// runOnMachineFunction - This uses the printMachineInstruction()
79/// method to print assembly for each instruction.
80///
81bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
82 SetupMachineFunction(MF);
83
84 // Print out constants referenced by the function
85 EmitConstantPool(MF.getConstantPool());
86
87 // BBNumber is used here so that a given Printer will never give two
88 // BBs the same name. (If you have a better way, please let me know!)
89 static unsigned BBNumber = 0;
90
91 O << "\n\n";
92 // What's my mangled name?
93 CurrentFnName = Mang->getValueName(MF.getFunction());
94
Chris Lattnerd4d255a2006-10-05 02:48:40 +000095 // Print out the label for the function.
96 const Function *F = MF.getFunction();
97 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
98 EmitAlignment(4, F);
Chris Lattner158e1f52006-02-05 05:50:24 +000099 O << "\t.globl\t" << CurrentFnName << "\n";
100 O << "\t.type\t" << CurrentFnName << ", #function\n";
101 O << CurrentFnName << ":\n";
102
103 // Number each basic block so that we can consistently refer to them
104 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000105 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000106 NumberForBB.clear();
107 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
108 I != E; ++I) {
109 NumberForBB[I->getBasicBlock()] = BBNumber++;
110 }
111
112 // Print out code for the function.
113 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
114 I != E; ++I) {
115 // Print a label for the basic block.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000116 if (I != MF.begin()) {
117 printBasicBlockLabel(I, true);
118 O << '\n';
119 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000120 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
121 II != E; ++II) {
122 // Print the assembly for the instruction.
123 O << "\t";
124 printInstruction(II);
125 ++EmittedInsts;
126 }
127 }
128
129 // We didn't modify anything.
130 return false;
131}
132
133void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
134 const MachineOperand &MO = MI->getOperand (opNum);
135 const MRegisterInfo &RI = *TM.getRegisterInfo();
136 bool CloseParen = false;
137 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
138 O << "%hi(";
139 CloseParen = true;
140 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
141 && !MO.isRegister() && !MO.isImmediate()) {
142 O << "%lo(";
143 CloseParen = true;
144 }
145 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000146 case MachineOperand::MO_Register:
Chris Lattner158e1f52006-02-05 05:50:24 +0000147 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
148 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
149 else
150 O << "%reg" << MO.getReg();
151 break;
152
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000153 case MachineOperand::MO_Immediate:
Chris Lattner158e1f52006-02-05 05:50:24 +0000154 O << (int)MO.getImmedValue();
155 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000156 case MachineOperand::MO_MachineBasicBlock:
157 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattner158e1f52006-02-05 05:50:24 +0000158 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000159 case MachineOperand::MO_GlobalAddress:
160 O << Mang->getValueName(MO.getGlobal());
161 break;
162 case MachineOperand::MO_ExternalSymbol:
163 O << MO.getSymbolName();
164 break;
165 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskeya6211dc2006-09-06 18:34:40 +0000166 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner158e1f52006-02-05 05:50:24 +0000167 << MO.getConstantPoolIndex();
168 break;
169 default:
170 O << "<unknown operand type>"; abort (); break;
171 }
172 if (CloseParen) O << ")";
173}
174
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000175void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
176 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000177 printOperand(MI, opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000178
179 // If this is an ADD operand, emit it like normal operands.
180 if (Modifier && !strcmp(Modifier, "arith")) {
181 O << ", ";
182 printOperand(MI, opNum+1);
183 return;
184 }
185
Chris Lattner10b71c02006-05-04 18:05:43 +0000186 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000187 MI->getOperand(opNum+1).getReg() == SP::G0)
188 return; // don't print "+%g0"
Chris Lattner10b71c02006-05-04 18:05:43 +0000189 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000190 MI->getOperand(opNum+1).getImmedValue() == 0)
191 return; // don't print "+0"
192
193 O << "+";
Chris Lattner10b71c02006-05-04 18:05:43 +0000194 if (MI->getOperand(opNum+1).isGlobalAddress() ||
195 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000196 O << "%lo(";
197 printOperand(MI, opNum+1);
198 O << ")";
199 } else {
200 printOperand(MI, opNum+1);
201 }
202}
203
204void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
205 int CC = (int)MI->getOperand(opNum).getImmedValue();
206 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
207}
208
209
210
211bool SparcAsmPrinter::doInitialization(Module &M) {
212 Mang = new Mangler(M);
213 return false; // success
214}
215
216bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000217 const TargetData *TD = TM.getTargetData();
Chris Lattner158e1f52006-02-05 05:50:24 +0000218
219 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000220 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
221 I != E; ++I)
Chris Lattner158e1f52006-02-05 05:50:24 +0000222 if (I->hasInitializer()) { // External global require no code
Chris Lattnere363fdf2006-03-09 06:14:35 +0000223 // Check to see if this is a special global used by LLVM, if so, emit it.
224 if (EmitSpecialLLVMGlobal(I))
225 continue;
226
Chris Lattner158e1f52006-02-05 05:50:24 +0000227 O << "\n\n";
228 std::string name = Mang->getValueName(I);
229 Constant *C = I->getInitializer();
Owen Anderson20a631f2006-05-03 01:29:57 +0000230 unsigned Size = TD->getTypeSize(C->getType());
231 unsigned Align = TD->getTypeAlignment(C->getType());
Chris Lattner158e1f52006-02-05 05:50:24 +0000232
233 if (C->isNullValue() &&
234 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
235 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000236 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000237 if (I->hasInternalLinkage())
238 O << "\t.local " << name << "\n";
239
Owen Anderson20a631f2006-05-03 01:29:57 +0000240 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
241 << "," << (unsigned)TD->getTypeAlignment(C->getType());
Chris Lattner158e1f52006-02-05 05:50:24 +0000242 O << "\n";
243 } else {
244 switch (I->getLinkage()) {
245 case GlobalValue::LinkOnceLinkage:
246 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
247 // Nonnull linkonce -> weak
248 O << "\t.weak " << name << "\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000249 SwitchToDataSection("", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000250 O << "\t.section\t\".llvm.linkonce.d." << name
251 << "\",\"aw\",@progbits\n";
252 break;
253
254 case GlobalValue::AppendingLinkage:
255 // FIXME: appending linkage variables should go into a section of
256 // their name or something. For now, just emit them as external.
257 case GlobalValue::ExternalLinkage:
258 // If external or appending, declare as a global symbol
259 O << "\t.globl " << name << "\n";
260 // FALL THROUGH
261 case GlobalValue::InternalLinkage:
262 if (C->isNullValue())
Chris Lattner8488ba22006-05-09 04:59:56 +0000263 SwitchToDataSection(".bss", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000264 else
Chris Lattner8488ba22006-05-09 04:59:56 +0000265 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000266 break;
267 case GlobalValue::GhostLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000268 cerr << "Should not have any unmaterialized functions!\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000269 abort();
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000270 case GlobalValue::DLLImportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000271 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000272 abort();
273 case GlobalValue::DLLExportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000274 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000275 abort();
276 default:
277 assert(0 && "Unknown linkage type!");
Chris Lattner158e1f52006-02-05 05:50:24 +0000278 }
279
280 O << "\t.align " << Align << "\n";
281 O << "\t.type " << name << ",#object\n";
282 O << "\t.size " << name << "," << Size << "\n";
Chris Lattner81cf22d2006-12-06 06:13:25 +0000283 O << name << ":\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000284 EmitGlobalConstant(C);
285 }
286 }
287
288 AsmPrinter::doFinalization(M);
289 return false; // success
290}