blob: 5cdb4d6e398c38c42531a80e3c3b6de37ea860e1 [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Brian Gaeke4acfd032004-03-04 06:00:41 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Brian Gaeke4acfd032004-03-04 06:00:41 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
Chris Lattner7c90f732006-02-05 05:50:24 +000011// of machine-dependent LLVM code to GAS-format SPARC assembly language.
Brian Gaeke4acfd032004-03-04 06:00:41 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Chris Lattner7c90f732006-02-05 05:50:24 +000016#include "Sparc.h"
17#include "SparcInstrInfo.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000018#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
Chris Lattner1dbed162005-12-17 07:04:29 +000021#include "llvm/CodeGen/AsmPrinter.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineInstr.h"
Jim Laskey563321a2006-09-06 18:34:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000026#include "llvm/Target/TargetData.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000027#include "llvm/Target/TargetMachine.h"
28#include "llvm/Support/Mangler.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/StringExtras.h"
31#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000032#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000033#include <cctype>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000034#include <cstring>
Owen Andersonc0078482008-07-10 01:44:27 +000035#include <map>
Brian Gaeke4acfd032004-03-04 06:00:41 +000036using namespace llvm;
37
Chris Lattner95b2c7d2006-12-19 22:59:26 +000038STATISTIC(EmittedInsts, "Number of machine instrs printed");
Brian Gaeke4acfd032004-03-04 06:00:41 +000039
Chris Lattner95b2c7d2006-12-19 22:59:26 +000040namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000041 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Jim Laskeya0f3d172006-09-07 22:06:40 +000042 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000043 : AsmPrinter(O, TM, T) {
44 }
Brian Gaeke4acfd032004-03-04 06:00:41 +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
Brian Gaeke4acfd032004-03-04 06:00:41 +000053 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000054 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000055 }
56
Anton Korobeynikov5b794b92008-08-07 09:51:25 +000057 void printModuleLevelGV(const GlobalVariable* GVar);
Brian Gaeke446ae112004-06-15 19:52:59 +000058 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000059 void printMemOperand(const MachineInstr *MI, int opNum,
60 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000061 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000062
Chris Lattner994b7352005-12-16 06:34:17 +000063 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000064 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +000065 std::string getSectionForFunction(const Function &F) const;
Brian Gaeke4acfd032004-03-04 06:00:41 +000066 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
68 };
69} // end of anonymous namespace
70
Chris Lattner7c90f732006-02-05 05:50:24 +000071#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000072
Chris Lattner7c90f732006-02-05 05:50:24 +000073/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
Brian Gaeke4acfd032004-03-04 06:00:41 +000074/// 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///
Chris Lattner7c90f732006-02-05 05:50:24 +000078FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
79 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000080 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
Brian Gaeke4acfd032004-03-04 06:00:41 +000081}
82
Anton Korobeynikov5b794b92008-08-07 09:51:25 +000083// Substitute old hook with new one temporary
84std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const {
85 return TAI->SectionForGlobal(&F);
86}
87
Evan Cheng4eecdeb2008-02-02 08:39:46 +000088/// runOnMachineFunction - This uses the printInstruction()
Brian Gaeke4acfd032004-03-04 06:00:41 +000089/// method to print assembly for each instruction.
90///
Chris Lattner7c90f732006-02-05 05:50:24 +000091bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000092 SetupMachineFunction(MF);
93
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000094 // Print out constants referenced by the function
95 EmitConstantPool(MF.getConstantPool());
96
Brian Gaeke4acfd032004-03-04 06:00:41 +000097 // BBNumber is used here so that a given Printer will never give two
98 // BBs the same name. (If you have a better way, please let me know!)
99 static unsigned BBNumber = 0;
100
101 O << "\n\n";
102 // What's my mangled name?
103 CurrentFnName = Mang->getValueName(MF.getFunction());
104
Chris Lattner29bd9e12006-10-05 02:48:40 +0000105 // Print out the label for the function.
106 const Function *F = MF.getFunction();
107 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
108 EmitAlignment(4, F);
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000109 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000110
111 printVisibility(CurrentFnName, F->getVisibility());
112
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000113 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000114 O << CurrentFnName << ":\n";
115
116 // Number each basic block so that we can consistently refer to them
117 // in PC-relative references.
Chris Lattner29bd9e12006-10-05 02:48:40 +0000118 // FIXME: Why not use the MBB numbers?
Brian Gaeke4acfd032004-03-04 06:00:41 +0000119 NumberForBB.clear();
120 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
121 I != E; ++I) {
122 NumberForBB[I->getBasicBlock()] = BBNumber++;
123 }
124
125 // Print out code for the function.
126 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127 I != E; ++I) {
128 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000129 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000130 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000131 O << '\n';
132 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000133 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000134 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000135 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000136 printInstruction(II);
Chris Lattner1dbed162005-12-17 07:04:29 +0000137 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000138 }
139 }
140
141 // We didn't modify anything.
142 return false;
143}
144
Chris Lattner7c90f732006-02-05 05:50:24 +0000145void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000146 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000147 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000148 bool CloseParen = false;
Chris Lattner7c90f732006-02-05 05:50:24 +0000149 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000150 O << "%hi(";
151 CloseParen = true;
Chris Lattner7c90f732006-02-05 05:50:24 +0000152 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
Chris Lattner4fca0172006-01-15 09:26:27 +0000153 && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000154 O << "%lo(";
155 CloseParen = true;
156 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000157 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000158 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000159 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling74ab84c2008-02-26 21:11:01 +0000160 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000161 else
162 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000163 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000164
Chris Lattner63b3d712006-05-04 17:21:20 +0000165 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000166 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +0000167 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000168 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000169 printBasicBlockLabel(MO.getMBB());
Brian Gaeke09c13092004-06-17 19:39:23 +0000170 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000171 case MachineOperand::MO_GlobalAddress:
172 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000173 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000174 case MachineOperand::MO_ExternalSymbol:
175 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000176 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000177 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000179 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000180 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000181 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000182 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000183 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000184 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000185}
186
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000187void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
188 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000189 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000190
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000191 // If this is an ADD operand, emit it like normal operands.
192 if (Modifier && !strcmp(Modifier, "arith")) {
193 O << ", ";
194 printOperand(MI, opNum+1);
195 return;
196 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000197
Chris Lattner2d90ac72006-05-04 18:05:43 +0000198 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000199 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000200 return; // don't print "+%g0"
Chris Lattner2d90ac72006-05-04 18:05:43 +0000201 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000202 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000203 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000204
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000205 O << "+";
Chris Lattner2d90ac72006-05-04 18:05:43 +0000206 if (MI->getOperand(opNum+1).isGlobalAddress() ||
207 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000208 O << "%lo(";
209 printOperand(MI, opNum+1);
210 O << ")";
211 } else {
212 printOperand(MI, opNum+1);
213 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000214}
215
Chris Lattner7c90f732006-02-05 05:50:24 +0000216void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000217 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000218 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000219}
220
221
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000222
Chris Lattner7c90f732006-02-05 05:50:24 +0000223bool SparcAsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000224 Mang = new Mangler(M);
225 return false; // success
226}
227
Chris Lattner7c90f732006-02-05 05:50:24 +0000228bool SparcAsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000229 // Print out module-level global variables here.
Chris Lattner04f96742006-03-09 06:14:35 +0000230 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
231 I != E; ++I)
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000232 printModuleLevelGV(I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000233
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000234 O << '\n';
Brian Gaeke4acfd032004-03-04 06:00:41 +0000235
Dan Gohmanb8275a32007-07-25 19:33:14 +0000236 return AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000237}
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000238
239void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
240 const TargetData *TD = TM.getTargetData();
241
242 if (!GVar->hasInitializer())
243 return; // External global require no code
244
245 // Check to see if this is a special global used by LLVM, if so, emit it.
246 if (EmitSpecialLLVMGlobal(GVar))
247 return;
248
249 O << "\n\n";
250 std::string SectionName = TAI->SectionForGlobal(GVar);
251 std::string name = Mang->getValueName(GVar);
252 Constant *C = GVar->getInitializer();
253 unsigned Size = TD->getABITypeSize(C->getType());
254 unsigned Align = TD->getPreferredAlignment(GVar);
255
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000256 printVisibility(name, GVar->getVisibility());
257
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000258 SwitchToDataSection(SectionName.c_str());
259
260 if (C->isNullValue() && !GVar->hasSection()) {
261 if (!GVar->isThreadLocal() &&
262 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) {
263 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
264
265 if (GVar->hasInternalLinkage())
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000266 O << "\t.local " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000267
268 O << TAI->getCOMMDirective() << name << ',' << Size;
269 if (TAI->getCOMMDirectiveTakesAlignment())
270 O << ',' << (1 << Align);
271
272 O << '\n';
273 return;
274 }
275 }
276
277 switch (GVar->getLinkage()) {
278 case GlobalValue::CommonLinkage:
279 case GlobalValue::LinkOnceLinkage:
280 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
281 // Nonnull linkonce -> weak
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000282 O << "\t.weak " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000283 break;
284 case GlobalValue::AppendingLinkage:
285 // FIXME: appending linkage variables should go into a section of
286 // their name or something. For now, just emit them as external.
287 case GlobalValue::ExternalLinkage:
288 // If external or appending, declare as a global symbol
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000289 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000290 // FALL THROUGH
291 case GlobalValue::InternalLinkage:
292 break;
293 case GlobalValue::GhostLinkage:
294 cerr << "Should not have any unmaterialized functions!\n";
295 abort();
296 case GlobalValue::DLLImportLinkage:
297 cerr << "DLLImport linkage is not supported by this target!\n";
298 abort();
299 case GlobalValue::DLLExportLinkage:
300 cerr << "DLLExport linkage is not supported by this target!\n";
301 abort();
302 default:
303 assert(0 && "Unknown linkage type!");
304 }
305
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000306 EmitAlignment(Align, GVar);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000307
308 if (TAI->hasDotTypeDotSizeDirective()) {
309 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000310 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000311 }
312
313 O << name << ":\n";
314 EmitGlobalConstant(C);
315}