blob: d0ee4ee47407d9a7a6ede3ba8514fa20acacfb8c [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>
Owen Anderson36b92ca2008-07-10 01:44:27 +000035#include <map>
Chris Lattner158e1f52006-02-05 05:50:24 +000036using namespace llvm;
37
Chris Lattner1ef9cd42006-12-19 22:59:26 +000038STATISTIC(EmittedInsts, "Number of machine instrs printed");
Chris Lattner158e1f52006-02-05 05:50:24 +000039
Chris Lattner1ef9cd42006-12-19 22:59:26 +000040namespace {
Jim Laskeya6211dc2006-09-06 18:34:40 +000041 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Jim Laskey261779b2006-09-07 22:06:40 +000042 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskeya6211dc2006-09-06 18:34:40 +000043 : AsmPrinter(O, TM, T) {
44 }
Chris Lattner158e1f52006-02-05 05:50:24 +000045
46 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
49 ///
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
52
53 virtual const char *getPassName() const {
54 return "Sparc Assembly Printer";
55 }
56
57 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000058 void printMemOperand(const MachineInstr *MI, int opNum,
59 const char *Modifier = 0);
Chris Lattner158e1f52006-02-05 05:50:24 +000060 void printCCOperand(const MachineInstr *MI, int opNum);
61
62 bool printInstruction(const MachineInstr *MI); // autogenerated.
63 bool runOnMachineFunction(MachineFunction &F);
64 bool doInitialization(Module &M);
65 bool doFinalization(Module &M);
66 };
67} // end of anonymous namespace
68
69#include "SparcGenAsmWriter.inc"
70
71/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
72/// assembly code for a MachineFunction to the given output stream,
73/// using the given target machine description. This should work
74/// regardless of whether the function is in SSA form.
75///
76FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
77 TargetMachine &tm) {
Jim Laskey261779b2006-09-07 22:06:40 +000078 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Chris Lattner158e1f52006-02-05 05:50:24 +000079}
80
Evan Cheng32e53472008-02-02 08:39:46 +000081/// runOnMachineFunction - This uses the printInstruction()
Chris Lattner158e1f52006-02-05 05:50:24 +000082/// method to print assembly for each instruction.
83///
84bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
85 SetupMachineFunction(MF);
86
87 // Print out constants referenced by the function
88 EmitConstantPool(MF.getConstantPool());
89
90 // BBNumber is used here so that a given Printer will never give two
91 // BBs the same name. (If you have a better way, please let me know!)
92 static unsigned BBNumber = 0;
93
94 O << "\n\n";
95 // What's my mangled name?
96 CurrentFnName = Mang->getValueName(MF.getFunction());
97
Chris Lattnerd4d255a2006-10-05 02:48:40 +000098 // Print out the label for the function.
99 const Function *F = MF.getFunction();
100 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
101 EmitAlignment(4, F);
Chris Lattner158e1f52006-02-05 05:50:24 +0000102 O << "\t.globl\t" << CurrentFnName << "\n";
103 O << "\t.type\t" << CurrentFnName << ", #function\n";
104 O << CurrentFnName << ":\n";
105
106 // Number each basic block so that we can consistently refer to them
107 // in PC-relative references.
Chris Lattnerd4d255a2006-10-05 02:48:40 +0000108 // FIXME: Why not use the MBB numbers?
Chris Lattner158e1f52006-02-05 05:50:24 +0000109 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()) {
Evan Chengc7990652008-02-28 00:43:03 +0000120 printBasicBlockLabel(I, true, true);
Nate Begemanb9d4f832006-05-02 05:37:32 +0000121 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.
Chris Lattner158e1f52006-02-05 05:50:24 +0000126 printInstruction(II);
127 ++EmittedInsts;
128 }
129 }
130
131 // We didn't modify anything.
132 return false;
133}
134
135void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
136 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000137 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner158e1f52006-02-05 05:50:24 +0000138 bool CloseParen = false;
139 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
140 O << "%hi(";
141 CloseParen = true;
142 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
143 && !MO.isRegister() && !MO.isImmediate()) {
144 O << "%lo(";
145 CloseParen = true;
146 }
147 switch (MO.getType()) {
Chris Lattner10b71c02006-05-04 18:05:43 +0000148 case MachineOperand::MO_Register:
Dan Gohman3a4be0f2008-02-10 18:45:23 +0000149 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendlingc24ea4f2008-02-26 21:11:01 +0000150 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Chris Lattner158e1f52006-02-05 05:50:24 +0000151 else
152 O << "%reg" << MO.getReg();
153 break;
154
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000155 case MachineOperand::MO_Immediate:
Chris Lattner5c463782007-12-30 20:49:49 +0000156 O << (int)MO.getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000157 break;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000158 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnera5bb3702007-12-30 23:10:15 +0000159 printBasicBlockLabel(MO.getMBB());
Chris Lattner158e1f52006-02-05 05:50:24 +0000160 return;
Chris Lattner158e1f52006-02-05 05:50:24 +0000161 case MachineOperand::MO_GlobalAddress:
162 O << Mang->getValueName(MO.getGlobal());
163 break;
164 case MachineOperand::MO_ExternalSymbol:
165 O << MO.getSymbolName();
166 break;
167 case MachineOperand::MO_ConstantPoolIndex:
Evan Chengcdf36092007-10-14 05:57:21 +0000168 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnera5bb3702007-12-30 23:10:15 +0000169 << MO.getIndex();
Chris Lattner158e1f52006-02-05 05:50:24 +0000170 break;
171 default:
172 O << "<unknown operand type>"; abort (); break;
173 }
174 if (CloseParen) O << ")";
175}
176
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000177void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
178 const char *Modifier) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000179 printOperand(MI, opNum);
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000180
181 // If this is an ADD operand, emit it like normal operands.
182 if (Modifier && !strcmp(Modifier, "arith")) {
183 O << ", ";
184 printOperand(MI, opNum+1);
185 return;
186 }
187
Chris Lattner10b71c02006-05-04 18:05:43 +0000188 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner158e1f52006-02-05 05:50:24 +0000189 MI->getOperand(opNum+1).getReg() == SP::G0)
190 return; // don't print "+%g0"
Chris Lattner10b71c02006-05-04 18:05:43 +0000191 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner5c463782007-12-30 20:49:49 +0000192 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner158e1f52006-02-05 05:50:24 +0000193 return; // don't print "+0"
194
195 O << "+";
Chris Lattner10b71c02006-05-04 18:05:43 +0000196 if (MI->getOperand(opNum+1).isGlobalAddress() ||
197 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000198 O << "%lo(";
199 printOperand(MI, opNum+1);
200 O << ")";
201 } else {
202 printOperand(MI, opNum+1);
203 }
204}
205
206void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner5c463782007-12-30 20:49:49 +0000207 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner158e1f52006-02-05 05:50:24 +0000208 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
209}
210
211
212
213bool SparcAsmPrinter::doInitialization(Module &M) {
214 Mang = new Mangler(M);
215 return false; // success
216}
217
218bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Anderson20a631f2006-05-03 01:29:57 +0000219 const TargetData *TD = TM.getTargetData();
Chris Lattner158e1f52006-02-05 05:50:24 +0000220
221 // Print out module-level global variables here.
Chris Lattnere363fdf2006-03-09 06:14:35 +0000222 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
223 I != E; ++I)
Chris Lattner158e1f52006-02-05 05:50:24 +0000224 if (I->hasInitializer()) { // External global require no code
Chris Lattnere363fdf2006-03-09 06:14:35 +0000225 // Check to see if this is a special global used by LLVM, if so, emit it.
226 if (EmitSpecialLLVMGlobal(I))
227 continue;
228
Chris Lattner158e1f52006-02-05 05:50:24 +0000229 O << "\n\n";
230 std::string name = Mang->getValueName(I);
231 Constant *C = I->getInitializer();
Duncan Sands283207a2007-11-05 00:04:43 +0000232 unsigned Size = TD->getABITypeSize(C->getType());
Duncan Sands05837ed2008-01-29 06:23:44 +0000233 unsigned Align = TD->getPreferredAlignment(I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000234
Dale Johannesence4396b2008-05-14 20:12:51 +0000235 if (C->isNullValue() && (I->hasCommonLinkage() ||
236 I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
Chris Lattner158e1f52006-02-05 05:50:24 +0000237 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner8488ba22006-05-09 04:59:56 +0000238 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000239 if (I->hasInternalLinkage())
240 O << "\t.local " << name << "\n";
241
Duncan Sands283207a2007-11-05 00:04:43 +0000242 O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
Chris Lattner50ee0e42007-01-20 22:35:55 +0000243 << "," << Align;
Chris Lattner158e1f52006-02-05 05:50:24 +0000244 O << "\n";
245 } else {
246 switch (I->getLinkage()) {
Dale Johannesence4396b2008-05-14 20:12:51 +0000247 case GlobalValue::CommonLinkage:
Chris Lattner158e1f52006-02-05 05:50:24 +0000248 case GlobalValue::LinkOnceLinkage:
249 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
250 // Nonnull linkonce -> weak
251 O << "\t.weak " << name << "\n";
Chris Lattner8488ba22006-05-09 04:59:56 +0000252 SwitchToDataSection("", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000253 O << "\t.section\t\".llvm.linkonce.d." << name
254 << "\",\"aw\",@progbits\n";
255 break;
256
257 case GlobalValue::AppendingLinkage:
258 // FIXME: appending linkage variables should go into a section of
259 // their name or something. For now, just emit them as external.
260 case GlobalValue::ExternalLinkage:
261 // If external or appending, declare as a global symbol
262 O << "\t.globl " << name << "\n";
263 // FALL THROUGH
264 case GlobalValue::InternalLinkage:
265 if (C->isNullValue())
Chris Lattner8488ba22006-05-09 04:59:56 +0000266 SwitchToDataSection(".bss", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000267 else
Chris Lattner8488ba22006-05-09 04:59:56 +0000268 SwitchToDataSection(".data", I);
Chris Lattner158e1f52006-02-05 05:50:24 +0000269 break;
270 case GlobalValue::GhostLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000271 cerr << "Should not have any unmaterialized functions!\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000272 abort();
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000273 case GlobalValue::DLLImportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000274 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000275 abort();
276 case GlobalValue::DLLExportLinkage:
Bill Wendling9bfb1e12006-12-07 22:21:48 +0000277 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000278 abort();
279 default:
280 assert(0 && "Unknown linkage type!");
Chris Lattner158e1f52006-02-05 05:50:24 +0000281 }
282
283 O << "\t.align " << Align << "\n";
284 O << "\t.type " << name << ",#object\n";
285 O << "\t.size " << name << "," << Size << "\n";
Chris Lattner81cf22d2006-12-06 06:13:25 +0000286 O << name << ":\n";
Chris Lattner158e1f52006-02-05 05:50:24 +0000287 EmitGlobalConstant(C);
288 }
289 }
290
Dan Gohmancf0a5342007-07-25 19:33:14 +0000291 return AsmPrinter::doFinalization(M);
Chris Lattner158e1f52006-02-05 05:50:24 +0000292}