blob: 4565a997e274472a363dfc6b2b1bfe81cc288d58 [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"
20#include "llvm/Assembly/Writer.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/TargetMachine.h"
26#include "llvm/Support/Mangler.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/StringExtras.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/MathExtras.h"
31#include <cctype>
32#include <iostream>
33using namespace llvm;
34
35namespace {
36 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37
38 struct SparcAsmPrinter : public AsmPrinter {
39 SparcAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
40 Data16bitsDirective = "\t.half\t";
41 Data32bitsDirective = "\t.word\t";
42 Data64bitsDirective = 0; // .xword is only supported by V9.
Chris Lattner6db414e2006-02-15 07:07:14 +000043 ZeroDirective = "\t.skip\t";
Chris Lattner158e1f52006-02-05 05:50:24 +000044 CommentString = "!";
45 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
46 }
47
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
51 ///
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
54
55 virtual const char *getPassName() const {
56 return "Sparc Assembly Printer";
57 }
58
59 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);
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///
78FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
79 TargetMachine &tm) {
80 return new SparcAsmPrinter(o, tm);
81}
82
83/// runOnMachineFunction - This uses the printMachineInstruction()
84/// 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 labels for the function.
101 O << "\t.text\n";
102 O << "\t.align 16\n";
103 O << "\t.globl\t" << CurrentFnName << "\n";
104 O << "\t.type\t" << CurrentFnName << ", #function\n";
105 O << CurrentFnName << ":\n";
106
107 // Number each basic block so that we can consistently refer to them
108 // in PC-relative references.
109 NumberForBB.clear();
110 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
111 I != E; ++I) {
112 NumberForBB[I->getBasicBlock()] = BBNumber++;
113 }
114
115 // Print out code for the function.
116 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
117 I != E; ++I) {
118 // Print a label for the basic block.
119 if (I != MF.begin())
120 O << ".LBB" << Mang->getValueName(MF.getFunction ())
121 << "_" << I->getNumber () << ":\t! "
122 << I->getBasicBlock ()->getName () << "\n";
123 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
124 II != E; ++II) {
125 // Print the assembly for the instruction.
126 O << "\t";
127 printInstruction(II);
128 ++EmittedInsts;
129 }
130 }
131
132 // We didn't modify anything.
133 return false;
134}
135
136void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
137 const MachineOperand &MO = MI->getOperand (opNum);
138 const MRegisterInfo &RI = *TM.getRegisterInfo();
139 bool CloseParen = false;
140 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
141 O << "%hi(";
142 CloseParen = true;
143 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
144 && !MO.isRegister() && !MO.isImmediate()) {
145 O << "%lo(";
146 CloseParen = true;
147 }
148 switch (MO.getType()) {
149 case MachineOperand::MO_VirtualRegister:
150 if (Value *V = MO.getVRegValueOrNull()) {
151 O << "<" << V->getName() << ">";
152 break;
153 }
154 // FALLTHROUGH
155 case MachineOperand::MO_MachineRegister:
156 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
157 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
158 else
159 O << "%reg" << MO.getReg();
160 break;
161
162 case MachineOperand::MO_SignExtendedImmed:
163 case MachineOperand::MO_UnextendedImmed:
164 O << (int)MO.getImmedValue();
165 break;
166 case MachineOperand::MO_MachineBasicBlock: {
167 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
168 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
169 << "_" << MBBOp->getNumber () << "\t! "
170 << MBBOp->getBasicBlock ()->getName ();
171 return;
172 }
173 case MachineOperand::MO_PCRelativeDisp:
174 std::cerr << "Shouldn't use addPCDisp() when building Sparc MachineInstrs";
175 abort ();
176 return;
177 case MachineOperand::MO_GlobalAddress:
178 O << Mang->getValueName(MO.getGlobal());
179 break;
180 case MachineOperand::MO_ExternalSymbol:
181 O << MO.getSymbolName();
182 break;
183 case MachineOperand::MO_ConstantPoolIndex:
184 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
185 << MO.getConstantPoolIndex();
186 break;
187 default:
188 O << "<unknown operand type>"; abort (); break;
189 }
190 if (CloseParen) O << ")";
191}
192
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000193void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
194 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000195 printOperand(MI, opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000196
197 // If this is an ADD operand, emit it like normal operands.
198 if (Modifier && !strcmp(Modifier, "arith")) {
199 O << ", ";
200 printOperand(MI, opNum+1);
201 return;
202 }
203
Chris Lattner158e1f52006-02-05 05:50:24 +0000204 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
205
206 if ((OpTy == MachineOperand::MO_VirtualRegister ||
207 OpTy == MachineOperand::MO_MachineRegister) &&
208 MI->getOperand(opNum+1).getReg() == SP::G0)
209 return; // don't print "+%g0"
210 if ((OpTy == MachineOperand::MO_SignExtendedImmed ||
211 OpTy == MachineOperand::MO_UnextendedImmed) &&
212 MI->getOperand(opNum+1).getImmedValue() == 0)
213 return; // don't print "+0"
214
215 O << "+";
216 if (OpTy == MachineOperand::MO_GlobalAddress ||
217 OpTy == MachineOperand::MO_ConstantPoolIndex) {
218 O << "%lo(";
219 printOperand(MI, opNum+1);
220 O << ")";
221 } else {
222 printOperand(MI, opNum+1);
223 }
224}
225
226void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
227 int CC = (int)MI->getOperand(opNum).getImmedValue();
228 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
229}
230
231
232
233bool SparcAsmPrinter::doInitialization(Module &M) {
234 Mang = new Mangler(M);
235 return false; // success
236}
237
238bool SparcAsmPrinter::doFinalization(Module &M) {
239 const TargetData &TD = TM.getTargetData();
240
241 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000242 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
243 I != E; ++I)
Chris Lattner158e1f52006-02-05 05:50:24 +0000244 if (I->hasInitializer()) { // External global require no code
Chris Lattnere363fdf2006-03-09 06:14:35 +0000245 // Check to see if this is a special global used by LLVM, if so, emit it.
246 if (EmitSpecialLLVMGlobal(I))
247 continue;
248
Chris Lattner158e1f52006-02-05 05:50:24 +0000249 O << "\n\n";
250 std::string name = Mang->getValueName(I);
251 Constant *C = I->getInitializer();
252 unsigned Size = TD.getTypeSize(C->getType());
253 unsigned Align = TD.getTypeAlignment(C->getType());
254
255 if (C->isNullValue() &&
256 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
257 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
258 SwitchSection(".data", I);
259 if (I->hasInternalLinkage())
260 O << "\t.local " << name << "\n";
261
262 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
263 << "," << (unsigned)TD.getTypeAlignment(C->getType());
264 O << "\t\t! ";
265 WriteAsOperand(O, I, true, true, &M);
266 O << "\n";
267 } else {
268 switch (I->getLinkage()) {
269 case GlobalValue::LinkOnceLinkage:
270 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
271 // Nonnull linkonce -> weak
272 O << "\t.weak " << name << "\n";
273 SwitchSection("", I);
274 O << "\t.section\t\".llvm.linkonce.d." << name
275 << "\",\"aw\",@progbits\n";
276 break;
277
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
283 O << "\t.globl " << name << "\n";
284 // FALL THROUGH
285 case GlobalValue::InternalLinkage:
286 if (C->isNullValue())
287 SwitchSection(".bss", I);
288 else
289 SwitchSection(".data", I);
290 break;
291 case GlobalValue::GhostLinkage:
292 std::cerr << "Should not have any unmaterialized functions!\n";
293 abort();
294 }
295
296 O << "\t.align " << Align << "\n";
297 O << "\t.type " << name << ",#object\n";
298 O << "\t.size " << name << "," << Size << "\n";
299 O << name << ":\t\t\t\t! ";
300 WriteAsOperand(O, I, true, true, &M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000301 O << "\n";
302 EmitGlobalConstant(C);
303 }
304 }
305
306 AsmPrinter::doFinalization(M);
307 return false; // success
308}