blob: da2a31e20d971098a1e7e005b15c557b613c2d4e [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.
Chris Lattner8488ba22006-05-09 04:59:56 +0000101 SwitchToTextSection(".text", MF.getFunction());
102 EmitAlignment(4, MF.getFunction());
Chris Lattner158e1f52006-02-05 05:50:24 +0000103 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.
Nate Begemanb9d4f832006-05-02 05:37:32 +0000119 if (I != MF.begin()) {
120 printBasicBlockLabel(I, true);
121 O << '\n';
122 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000123 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()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000149 case MachineOperand::MO_Register:
Chris Lattner158e1f52006-02-05 05:50:24 +0000150 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
151 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
152 else
153 O << "%reg" << MO.getReg();
154 break;
155
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000156 case MachineOperand::MO_Immediate:
Chris Lattner158e1f52006-02-05 05:50:24 +0000157 O << (int)MO.getImmedValue();
158 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000159 case MachineOperand::MO_MachineBasicBlock:
160 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattner158e1f52006-02-05 05:50:24 +0000161 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000162 case MachineOperand::MO_GlobalAddress:
163 O << Mang->getValueName(MO.getGlobal());
164 break;
165 case MachineOperand::MO_ExternalSymbol:
166 O << MO.getSymbolName();
167 break;
168 case MachineOperand::MO_ConstantPoolIndex:
169 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
170 << MO.getConstantPoolIndex();
171 break;
172 default:
173 O << "<unknown operand type>"; abort (); break;
174 }
175 if (CloseParen) O << ")";
176}
177
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000178void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
179 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000180 printOperand(MI, opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000181
182 // If this is an ADD operand, emit it like normal operands.
183 if (Modifier && !strcmp(Modifier, "arith")) {
184 O << ", ";
185 printOperand(MI, opNum+1);
186 return;
187 }
188
Chris Lattner158e1f52006-02-05 05:50:24 +0000189 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
190
Chris Lattner10b71c02006-05-04 18:05:43 +0000191 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000192 MI->getOperand(opNum+1).getReg() == SP::G0)
193 return; // don't print "+%g0"
Chris Lattner10b71c02006-05-04 18:05:43 +0000194 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000195 MI->getOperand(opNum+1).getImmedValue() == 0)
196 return; // don't print "+0"
197
198 O << "+";
Chris Lattner10b71c02006-05-04 18:05:43 +0000199 if (MI->getOperand(opNum+1).isGlobalAddress() ||
200 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000201 O << "%lo(";
202 printOperand(MI, opNum+1);
203 O << ")";
204 } else {
205 printOperand(MI, opNum+1);
206 }
207}
208
209void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
210 int CC = (int)MI->getOperand(opNum).getImmedValue();
211 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
212}
213
214
215
216bool SparcAsmPrinter::doInitialization(Module &M) {
217 Mang = new Mangler(M);
218 return false; // success
219}
220
221bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000222 const TargetData *TD = TM.getTargetData();
Chris Lattner158e1f52006-02-05 05:50:24 +0000223
224 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000225 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
226 I != E; ++I)
Chris Lattner158e1f52006-02-05 05:50:24 +0000227 if (I->hasInitializer()) { // External global require no code
Chris Lattnere363fdf2006-03-09 06:14:35 +0000228 // Check to see if this is a special global used by LLVM, if so, emit it.
229 if (EmitSpecialLLVMGlobal(I))
230 continue;
231
Chris Lattner158e1f52006-02-05 05:50:24 +0000232 O << "\n\n";
233 std::string name = Mang->getValueName(I);
234 Constant *C = I->getInitializer();
Owen Anderson20a631f2006-05-03 01:29:57 +0000235 unsigned Size = TD->getTypeSize(C->getType());
236 unsigned Align = TD->getTypeAlignment(C->getType());
Chris Lattner158e1f52006-02-05 05:50:24 +0000237
238 if (C->isNullValue() &&
239 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
240 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000241 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000242 if (I->hasInternalLinkage())
243 O << "\t.local " << name << "\n";
244
Owen Anderson20a631f2006-05-03 01:29:57 +0000245 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
246 << "," << (unsigned)TD->getTypeAlignment(C->getType());
Chris Lattner158e1f52006-02-05 05:50:24 +0000247 O << "\t\t! ";
248 WriteAsOperand(O, I, true, true, &M);
249 O << "\n";
250 } else {
251 switch (I->getLinkage()) {
252 case GlobalValue::LinkOnceLinkage:
253 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
254 // Nonnull linkonce -> weak
255 O << "\t.weak " << name << "\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000256 SwitchToDataSection("", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000257 O << "\t.section\t\".llvm.linkonce.d." << name
258 << "\",\"aw\",@progbits\n";
259 break;
260
261 case GlobalValue::AppendingLinkage:
262 // FIXME: appending linkage variables should go into a section of
263 // their name or something. For now, just emit them as external.
264 case GlobalValue::ExternalLinkage:
265 // If external or appending, declare as a global symbol
266 O << "\t.globl " << name << "\n";
267 // FALL THROUGH
268 case GlobalValue::InternalLinkage:
269 if (C->isNullValue())
Chris Lattner8488ba22006-05-09 04:59:56 +0000270 SwitchToDataSection(".bss", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000271 else
Chris Lattner8488ba22006-05-09 04:59:56 +0000272 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000273 break;
274 case GlobalValue::GhostLinkage:
275 std::cerr << "Should not have any unmaterialized functions!\n";
276 abort();
277 }
278
279 O << "\t.align " << Align << "\n";
280 O << "\t.type " << name << ",#object\n";
281 O << "\t.size " << name << "," << Size << "\n";
282 O << name << ":\t\t\t\t! ";
283 WriteAsOperand(O, I, true, true, &M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000284 O << "\n";
285 EmitGlobalConstant(C);
286 }
287 }
288
289 AsmPrinter::doFinalization(M);
290 return false; // success
291}