blob: 6e8c68b658e7301f1532cd8fbe5abeafd58a0205 [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"
Owen Andersoncb371882008-08-21 00:14:44 +000029#include "llvm/Support/raw_ostream.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000030#include "llvm/ADT/Statistic.h"
31#include "llvm/ADT/StringExtras.h"
32#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000033#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000034#include <cctype>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000035#include <cstring>
Owen Andersonc0078482008-07-10 01:44:27 +000036#include <map>
Brian Gaeke4acfd032004-03-04 06:00:41 +000037using namespace llvm;
38
Chris Lattner95b2c7d2006-12-19 22:59:26 +000039STATISTIC(EmittedInsts, "Number of machine instrs printed");
Brian Gaeke4acfd032004-03-04 06:00:41 +000040
Chris Lattner95b2c7d2006-12-19 22:59:26 +000041namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000042 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
Owen Andersoncb371882008-08-21 00:14:44 +000043 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
Jim Laskey563321a2006-09-06 18:34:40 +000044 : AsmPrinter(O, TM, T) {
45 }
Brian Gaeke4acfd032004-03-04 06:00:41 +000046
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
50 ///
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
53
Brian Gaeke4acfd032004-03-04 06:00:41 +000054 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000055 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000056 }
57
Anton Korobeynikov5b794b92008-08-07 09:51:25 +000058 void printModuleLevelGV(const GlobalVariable* GVar);
Brian Gaeke446ae112004-06-15 19:52:59 +000059 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000060 void printMemOperand(const MachineInstr *MI, int opNum,
61 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000062 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000063
Chris Lattner994b7352005-12-16 06:34:17 +000064 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000065 bool runOnMachineFunction(MachineFunction &F);
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///
Owen Andersoncb371882008-08-21 00:14:44 +000078FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
Chris Lattner7c90f732006-02-05 05:50:24 +000079 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
Evan Cheng4eecdeb2008-02-02 08:39:46 +000083/// runOnMachineFunction - This uses the printInstruction()
Brian Gaeke4acfd032004-03-04 06:00:41 +000084/// method to print assembly for each instruction.
85///
Chris Lattner7c90f732006-02-05 05:50:24 +000086bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000087 SetupMachineFunction(MF);
88
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000089 // Print out constants referenced by the function
90 EmitConstantPool(MF.getConstantPool());
91
Brian Gaeke4acfd032004-03-04 06:00:41 +000092 // 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
Chris Lattner29bd9e12006-10-05 02:48:40 +0000100 // Print out the label for the function.
101 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000102 SwitchToSection(TAI->SectionForGlobal(F));
Chris Lattner29bd9e12006-10-05 02:48:40 +0000103 EmitAlignment(4, F);
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000104 O << "\t.globl\t" << CurrentFnName << '\n';
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000105
106 printVisibility(CurrentFnName, F->getVisibility());
107
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000108 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000109 O << CurrentFnName << ":\n";
110
111 // Number each basic block so that we can consistently refer to them
112 // in PC-relative references.
Chris Lattner29bd9e12006-10-05 02:48:40 +0000113 // FIXME: Why not use the MBB numbers?
Brian Gaeke4acfd032004-03-04 06:00:41 +0000114 NumberForBB.clear();
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116 I != E; ++I) {
117 NumberForBB[I->getBasicBlock()] = BBNumber++;
118 }
119
120 // Print out code for the function.
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 I != E; ++I) {
123 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000124 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000125 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000126 O << '\n';
127 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000128 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000129 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000130 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000131 printInstruction(II);
Chris Lattner1dbed162005-12-17 07:04:29 +0000132 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000133 }
134 }
135
136 // We didn't modify anything.
137 return false;
138}
139
Chris Lattner7c90f732006-02-05 05:50:24 +0000140void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000141 const MachineOperand &MO = MI->getOperand (opNum);
Dan Gohman6f0d0242008-02-10 18:45:23 +0000142 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000143 bool CloseParen = false;
Chris Lattner7c90f732006-02-05 05:50:24 +0000144 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000145 O << "%hi(";
146 CloseParen = true;
Chris Lattner7c90f732006-02-05 05:50:24 +0000147 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
Chris Lattner4fca0172006-01-15 09:26:27 +0000148 && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000149 O << "%lo(";
150 CloseParen = true;
151 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000152 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000153 case MachineOperand::MO_Register:
Dan Gohman6f0d0242008-02-10 18:45:23 +0000154 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
Bill Wendling74ab84c2008-02-26 21:11:01 +0000155 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000156 else
157 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000158 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000159
Chris Lattner63b3d712006-05-04 17:21:20 +0000160 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000161 O << (int)MO.getImm();
Brian Gaeke446ae112004-06-15 19:52:59 +0000162 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000163 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000164 printBasicBlockLabel(MO.getMBB());
Brian Gaeke09c13092004-06-17 19:39:23 +0000165 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000166 case MachineOperand::MO_GlobalAddress:
167 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000168 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000169 case MachineOperand::MO_ExternalSymbol:
170 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000171 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000172 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000173 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000174 << MO.getIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000175 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000176 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000177 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000178 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000179 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000180}
181
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000182void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
183 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000184 printOperand(MI, opNum);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000185
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000186 // If this is an ADD operand, emit it like normal operands.
187 if (Modifier && !strcmp(Modifier, "arith")) {
188 O << ", ";
189 printOperand(MI, opNum+1);
190 return;
191 }
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000192
Chris Lattner2d90ac72006-05-04 18:05:43 +0000193 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000194 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000195 return; // don't print "+%g0"
Chris Lattner2d90ac72006-05-04 18:05:43 +0000196 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000197 MI->getOperand(opNum+1).getImm() == 0)
Chris Lattner76acc872005-12-18 02:37:35 +0000198 return; // don't print "+0"
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000199
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000200 O << "+";
Chris Lattner2d90ac72006-05-04 18:05:43 +0000201 if (MI->getOperand(opNum+1).isGlobalAddress() ||
202 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000203 O << "%lo(";
204 printOperand(MI, opNum+1);
205 O << ")";
206 } else {
207 printOperand(MI, opNum+1);
208 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000209}
210
Chris Lattner7c90f732006-02-05 05:50:24 +0000211void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000212 int CC = (int)MI->getOperand(opNum).getImm();
Chris Lattner7c90f732006-02-05 05:50:24 +0000213 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000214}
215
216
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000217
Chris Lattner7c90f732006-02-05 05:50:24 +0000218bool SparcAsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000219 Mang = new Mangler(M);
220 return false; // success
221}
222
Chris Lattner7c90f732006-02-05 05:50:24 +0000223bool SparcAsmPrinter::doFinalization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000224 // Print out module-level global variables here.
Chris Lattner04f96742006-03-09 06:14:35 +0000225 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
226 I != E; ++I)
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000227 printModuleLevelGV(I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000228
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000229 O << '\n';
Brian Gaeke4acfd032004-03-04 06:00:41 +0000230
Dan Gohmanb8275a32007-07-25 19:33:14 +0000231 return AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000232}
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000233
234void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
235 const TargetData *TD = TM.getTargetData();
236
237 if (!GVar->hasInitializer())
238 return; // External global require no code
239
240 // Check to see if this is a special global used by LLVM, if so, emit it.
241 if (EmitSpecialLLVMGlobal(GVar))
242 return;
243
244 O << "\n\n";
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000245 std::string name = Mang->getValueName(GVar);
246 Constant *C = GVar->getInitializer();
247 unsigned Size = TD->getABITypeSize(C->getType());
248 unsigned Align = TD->getPreferredAlignment(GVar);
249
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000250 printVisibility(name, GVar->getVisibility());
251
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000252 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000253
254 if (C->isNullValue() && !GVar->hasSection()) {
255 if (!GVar->isThreadLocal() &&
Duncan Sands5df31862008-09-29 11:25:42 +0000256 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000257 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
258
259 if (GVar->hasInternalLinkage())
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000260 O << "\t.local " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000261
262 O << TAI->getCOMMDirective() << name << ',' << Size;
263 if (TAI->getCOMMDirectiveTakesAlignment())
264 O << ',' << (1 << Align);
265
266 O << '\n';
267 return;
268 }
269 }
270
271 switch (GVar->getLinkage()) {
272 case GlobalValue::CommonLinkage:
273 case GlobalValue::LinkOnceLinkage:
274 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
275 // Nonnull linkonce -> weak
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000276 O << "\t.weak " << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000277 break;
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
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000283 O << TAI->getGlobalDirective() << name << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000284 // FALL THROUGH
285 case GlobalValue::InternalLinkage:
286 break;
287 case GlobalValue::GhostLinkage:
288 cerr << "Should not have any unmaterialized functions!\n";
289 abort();
290 case GlobalValue::DLLImportLinkage:
291 cerr << "DLLImport linkage is not supported by this target!\n";
292 abort();
293 case GlobalValue::DLLExportLinkage:
294 cerr << "DLLExport linkage is not supported by this target!\n";
295 abort();
296 default:
297 assert(0 && "Unknown linkage type!");
298 }
299
Anton Korobeynikovfcd99bb2008-08-07 09:53:38 +0000300 EmitAlignment(Align, GVar);
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000301
302 if (TAI->hasDotTypeDotSizeDirective()) {
303 O << "\t.type " << name << ",#object\n";
Anton Korobeynikov2a166e92008-08-07 09:53:13 +0000304 O << "\t.size " << name << ',' << Size << '\n';
Anton Korobeynikov5b794b92008-08-07 09:51:25 +0000305 }
306
307 O << name << ":\n";
308 EmitGlobalConstant(C);
309}