blob: cbbf4126dd8e2d664acae26c740b5e5f7ade582a [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"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/StringExtras.h"
31#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/MathExtras.h"
33#include <cctype>
Anton Korobeynikov579f0712008-02-20 11:08:44 +000034#include <cstring>
Chris Lattner158e1f52006-02-05 05:50:24 +000035using namespace llvm;
36
Chris Lattner1ef9cd42006-12-19 22:59:26 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000038
Chris Lattner1ef9cd42006-12-19 22:59:26 +000039namespace {
Jim Laskeya6211dc2006-09-06 18:34:40 +000040 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Jim Laskey261779b2006-09-07 22:06:40 +000041 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000042 : AsmPrinter(O, TM, T) {
43 }
Chris Lattner158e1f52006-02-05 05:50:24 +000044
45 /// We name each basic block in a Function with a unique number, so
46 /// that we can consistently refer to them later. This is cleared
47 /// at the beginning of each call to runOnMachineFunction().
48 ///
49 typedef std::map<const Value *, unsigned> ValueMapTy;
50 ValueMapTy NumberForBB;
51
52 virtual const char *getPassName() const {
53 return "Sparc Assembly Printer";
54 }
55
56 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000057 void printMemOperand(const MachineInstr *MI, int opNum,
58 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000059 void printCCOperand(const MachineInstr *MI, int opNum);
60
61 bool printInstruction(const MachineInstr *MI); // autogenerated.
62 bool runOnMachineFunction(MachineFunction &F);
63 bool doInitialization(Module &M);
64 bool doFinalization(Module &M);
65 };
66} // end of anonymous namespace
67
68#include "SparcGenAsmWriter.inc"
69
70/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
71/// assembly code for a MachineFunction to the given output stream,
72/// using the given target machine description. This should work
73/// regardless of whether the function is in SSA form.
74///
75FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
76 TargetMachine &tm) {
Jim Laskey261779b2006-09-07 22:06:40 +000077 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattner158e1f52006-02-05 05:50:24 +000078}
79
Evan Cheng32e53472008-02-02 08:39:46 +000080/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000081/// method to print assembly for each instruction.
82///
83bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
84 SetupMachineFunction(MF);
85
86 // Print out constants referenced by the function
87 EmitConstantPool(MF.getConstantPool());
88
89 // BBNumber is used here so that a given Printer will never give two
90 // BBs the same name. (If you have a better way, please let me know!)
91 static unsigned BBNumber = 0;
92
93 O << "\n\n";
94 // What's my mangled name?
95 CurrentFnName = Mang->getValueName(MF.getFunction());
96
Chris Lattnerd4d255a2006-10-05 02:48:40 +000097 // Print out the label for the function.
98 const Function *F = MF.getFunction();
99 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
100 EmitAlignment(4, F);
Chris Lattner158e1f52006-02-05 05:50:24 +0000101 O << "\t.globl\t" << CurrentFnName << "\n";
102 O << "\t.type\t" << CurrentFnName << ", #function\n";
103 O << CurrentFnName << ":\n";
104
105 // Number each basic block so that we can consistently refer to them
106 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000107 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000108 NumberForBB.clear();
109 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
110 I != E; ++I) {
111 NumberForBB[I->getBasicBlock()] = BBNumber++;
112 }
113
114 // Print out code for the function.
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116 I != E; ++I) {
117 // Print a label for the basic block.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000118 if (I != MF.begin()) {
119 printBasicBlockLabel(I, true);
120 O << '\n';
121 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000122 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
123 II != E; ++II) {
124 // Print the assembly for the instruction.
Chris Lattner158e1f52006-02-05 05:50:24 +0000125 printInstruction(II);
126 ++EmittedInsts;
127 }
128 }
129
130 // We didn't modify anything.
131 return false;
132}
133
134void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
135 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000136 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000137 bool CloseParen = false;
138 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
139 O << "%hi(";
140 CloseParen = true;
141 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
142 && !MO.isRegister() && !MO.isImmediate()) {
143 O << "%lo(";
144 CloseParen = true;
145 }
146 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000147 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000148 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Chris Lattner158e1f52006-02-05 05:50:24 +0000149 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
150 else
151 O << "%reg" << MO.getReg();
152 break;
153
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000154 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000155 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000156 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000157 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000158 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000159 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000160 case MachineOperand::MO_GlobalAddress:
161 O << Mang->getValueName(MO.getGlobal());
162 break;
163 case MachineOperand::MO_ExternalSymbol:
164 O << MO.getSymbolName();
165 break;
166 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000167 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000168 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000169 break;
170 default:
171 O << "<unknown operand type>"; abort (); break;
172 }
173 if (CloseParen) O << ")";
174}
175
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000176void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
177 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000178 printOperand(MI, opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000179
180 // If this is an ADD operand, emit it like normal operands.
181 if (Modifier && !strcmp(Modifier, "arith")) {
182 O << ", ";
183 printOperand(MI, opNum+1);
184 return;
185 }
186
Chris Lattner10b71c02006-05-04 18:05:43 +0000187 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000188 MI->getOperand(opNum+1).getReg() == SP::G0)
189 return; // don't print "+%g0"
Chris Lattner10b71c02006-05-04 18:05:43 +0000190 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000191 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000192 return; // don't print "+0"
193
194 O << "+";
Chris Lattner10b71c02006-05-04 18:05:43 +0000195 if (MI->getOperand(opNum+1).isGlobalAddress() ||
196 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000197 O << "%lo(";
198 printOperand(MI, opNum+1);
199 O << ")";
200 } else {
201 printOperand(MI, opNum+1);
202 }
203}
204
205void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000206 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000207 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
208}
209
210
211
212bool SparcAsmPrinter::doInitialization(Module &M) {
213 Mang = new Mangler(M);
214 return false; // success
215}
216
217bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000218 const TargetData *TD = TM.getTargetData();
Chris Lattner158e1f52006-02-05 05:50:24 +0000219
220 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000221 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
222 I != E; ++I)
Chris Lattner158e1f52006-02-05 05:50:24 +0000223 if (I->hasInitializer()) { // External global require no code
Chris Lattnere363fdf2006-03-09 06:14:35 +0000224 // Check to see if this is a special global used by LLVM, if so, emit it.
225 if (EmitSpecialLLVMGlobal(I))
226 continue;
227
Chris Lattner158e1f52006-02-05 05:50:24 +0000228 O << "\n\n";
229 std::string name = Mang->getValueName(I);
230 Constant *C = I->getInitializer();
Duncan Sands283207a2007-11-05 00:04:43 +0000231 unsigned Size = TD->getABITypeSize(C->getType());
Duncan Sands05837ed2008-01-29 06:23:44 +0000232 unsigned Align = TD->getPreferredAlignment(I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000233
234 if (C->isNullValue() &&
235 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
236 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000237 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000238 if (I->hasInternalLinkage())
239 O << "\t.local " << name << "\n";
240
Duncan Sands283207a2007-11-05 00:04:43 +0000241 O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
Chris Lattner50ee0e42007-01-20 22:35:55 +0000242 << "," << Align;
Chris Lattner158e1f52006-02-05 05:50:24 +0000243 O << "\n";
244 } else {
245 switch (I->getLinkage()) {
246 case GlobalValue::LinkOnceLinkage:
247 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
248 // Nonnull linkonce -> weak
249 O << "\t.weak " << name << "\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000250 SwitchToDataSection("", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000251 O << "\t.section\t\".llvm.linkonce.d." << name
252 << "\",\"aw\",@progbits\n";
253 break;
254
255 case GlobalValue::AppendingLinkage:
256 // FIXME: appending linkage variables should go into a section of
257 // their name or something. For now, just emit them as external.
258 case GlobalValue::ExternalLinkage:
259 // If external or appending, declare as a global symbol
260 O << "\t.globl " << name << "\n";
261 // FALL THROUGH
262 case GlobalValue::InternalLinkage:
263 if (C->isNullValue())
Chris Lattner8488ba22006-05-09 04:59:56 +0000264 SwitchToDataSection(".bss", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000265 else
Chris Lattner8488ba22006-05-09 04:59:56 +0000266 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000267 break;
268 case GlobalValue::GhostLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000269 cerr << "Should not have any unmaterialized functions!\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000270 abort();
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000271 case GlobalValue::DLLImportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000272 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000273 abort();
274 case GlobalValue::DLLExportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000275 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000276 abort();
277 default:
278 assert(0 && "Unknown linkage type!");
Chris Lattner158e1f52006-02-05 05:50:24 +0000279 }
280
281 O << "\t.align " << Align << "\n";
282 O << "\t.type " << name << ",#object\n";
283 O << "\t.size " << name << "," << Size << "\n";
Chris Lattner81cf22d2006-12-06 06:13:25 +0000284 O << name << ":\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000285 EmitGlobalConstant(C);
286 }
287 }
288
Dan Gohmancf0a5342007-07-25 19:33:14 +0000289 return AsmPrinter::doFinalization(M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000290}