blob: 2bc6f6332e6aac954b15ee4c897bbbbd0e4d669c [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//
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.
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 Lattner7c90f732006-02-05 05:50:24 +000015#include "Sparc.h"
16#include "SparcInstrInfo.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Module.h"
20#include "llvm/Assembly/Writer.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>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000034#include <iostream>
Brian Gaeke4acfd032004-03-04 06:00:41 +000035using namespace llvm;
36
37namespace {
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
Jim Laskey563321a2006-09-06 18:34:40 +000040 struct VISIBILITY_HIDDEN SparcTargetAsmInfo : public TargetAsmInfo {
41 SparcTargetAsmInfo() {
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000042 Data16bitsDirective = "\t.half\t";
43 Data32bitsDirective = "\t.word\t";
Chris Lattner379e6c02005-12-18 23:36:45 +000044 Data64bitsDirective = 0; // .xword is only supported by V9.
Chris Lattner45090472006-02-15 07:07:14 +000045 ZeroDirective = "\t.skip\t";
Chris Lattner7a48e502005-12-18 23:35:05 +000046 CommentString = "!";
47 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000048 }
Jim Laskey563321a2006-09-06 18:34:40 +000049 };
50
51 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
52 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, TargetAsmInfo *T)
53 : AsmPrinter(O, TM, T) {
54 }
Brian Gaeke4acfd032004-03-04 06:00:41 +000055
56 /// We name each basic block in a Function with a unique number, so
57 /// that we can consistently refer to them later. This is cleared
58 /// at the beginning of each call to runOnMachineFunction().
59 ///
60 typedef std::map<const Value *, unsigned> ValueMapTy;
61 ValueMapTy NumberForBB;
62
Brian Gaeke4acfd032004-03-04 06:00:41 +000063 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000064 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000065 }
66
Brian Gaeke446ae112004-06-15 19:52:59 +000067 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000068 void printMemOperand(const MachineInstr *MI, int opNum,
69 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000070 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000071
Chris Lattner994b7352005-12-16 06:34:17 +000072 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000073 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000074 bool doInitialization(Module &M);
75 bool doFinalization(Module &M);
76 };
77} // end of anonymous namespace
78
Chris Lattner7c90f732006-02-05 05:50:24 +000079#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000080
Chris Lattner7c90f732006-02-05 05:50:24 +000081/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
Brian Gaeke4acfd032004-03-04 06:00:41 +000082/// assembly code for a MachineFunction to the given output stream,
83/// using the given target machine description. This should work
84/// regardless of whether the function is in SSA form.
85///
Chris Lattner7c90f732006-02-05 05:50:24 +000086FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
87 TargetMachine &tm) {
Jim Laskey563321a2006-09-06 18:34:40 +000088 SparcTargetAsmInfo *TAI = new SparcTargetAsmInfo();
89 return new SparcAsmPrinter(o, tm, TAI);
Brian Gaeke4acfd032004-03-04 06:00:41 +000090}
91
Brian Gaeke4acfd032004-03-04 06:00:41 +000092/// runOnMachineFunction - This uses the printMachineInstruction()
93/// method to print assembly for each instruction.
94///
Chris Lattner7c90f732006-02-05 05:50:24 +000095bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000096 SetupMachineFunction(MF);
97
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000098 // Print out constants referenced by the function
99 EmitConstantPool(MF.getConstantPool());
100
Brian Gaeke4acfd032004-03-04 06:00:41 +0000101 // BBNumber is used here so that a given Printer will never give two
102 // BBs the same name. (If you have a better way, please let me know!)
103 static unsigned BBNumber = 0;
104
105 O << "\n\n";
106 // What's my mangled name?
107 CurrentFnName = Mang->getValueName(MF.getFunction());
108
Brian Gaeke4acfd032004-03-04 06:00:41 +0000109 // Print out labels for the function.
Chris Lattner4632d7a2006-05-09 04:59:56 +0000110 SwitchToTextSection(".text", MF.getFunction());
111 EmitAlignment(4, MF.getFunction());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000112 O << "\t.globl\t" << CurrentFnName << "\n";
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.
118 NumberForBB.clear();
119 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
120 I != E; ++I) {
121 NumberForBB[I->getBasicBlock()] = BBNumber++;
122 }
123
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 I != E; ++I) {
127 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000128 if (I != MF.begin()) {
129 printBasicBlockLabel(I, true);
130 O << '\n';
131 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000133 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000134 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000135 O << "\t";
136 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);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000147 const MRegisterInfo &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:
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000159 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000160 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
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:
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000166 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000167 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000168 case MachineOperand::MO_MachineBasicBlock:
169 printBasicBlockLabel(MO.getMachineBasicBlock());
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:
Jim Laskey563321a2006-09-06 18:34:40 +0000178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattnerb5e9eb62005-12-17 07:11:43 +0000179 << MO.getConstantPoolIndex();
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);
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000190
191 // 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 }
197
Chris Lattner76acc872005-12-18 02:37:35 +0000198 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
199
Chris Lattner2d90ac72006-05-04 18:05:43 +0000200 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000201 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000202 return; // don't print "+%g0"
Chris Lattner2d90ac72006-05-04 18:05:43 +0000203 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner76acc872005-12-18 02:37:35 +0000204 MI->getOperand(opNum+1).getImmedValue() == 0)
205 return; // don't print "+0"
206
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000207 O << "+";
Chris Lattner2d90ac72006-05-04 18:05:43 +0000208 if (MI->getOperand(opNum+1).isGlobalAddress() ||
209 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000210 O << "%lo(";
211 printOperand(MI, opNum+1);
212 O << ")";
213 } else {
214 printOperand(MI, opNum+1);
215 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000216}
217
Chris Lattner7c90f732006-02-05 05:50:24 +0000218void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner6788faa2006-01-31 06:49:09 +0000219 int CC = (int)MI->getOperand(opNum).getImmedValue();
Chris Lattner7c90f732006-02-05 05:50:24 +0000220 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000221}
222
223
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000224
Chris Lattner7c90f732006-02-05 05:50:24 +0000225bool SparcAsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000226 Mang = new Mangler(M);
227 return false; // success
228}
229
Chris Lattner7c90f732006-02-05 05:50:24 +0000230bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000231 const TargetData *TD = TM.getTargetData();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000232
233 // Print out module-level global variables here.
Chris Lattner04f96742006-03-09 06:14:35 +0000234 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
235 I != E; ++I)
Brian Gaeke4acfd032004-03-04 06:00:41 +0000236 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000237 // Check to see if this is a special global used by LLVM, if so, emit it.
238 if (EmitSpecialLLVMGlobal(I))
239 continue;
240
Brian Gaeke4acfd032004-03-04 06:00:41 +0000241 O << "\n\n";
242 std::string name = Mang->getValueName(I);
243 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000244 unsigned Size = TD->getTypeSize(C->getType());
245 unsigned Align = TD->getTypeAlignment(C->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000246
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000247 if (C->isNullValue() &&
Brian Gaeke4acfd032004-03-04 06:00:41 +0000248 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
249 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000250 SwitchToDataSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000251 if (I->hasInternalLinkage())
252 O << "\t.local " << name << "\n";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000253
Owen Andersona69571c2006-05-03 01:29:57 +0000254 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
255 << "," << (unsigned)TD->getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000256 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000257 WriteAsOperand(O, I, true, true, &M);
258 O << "\n";
259 } else {
260 switch (I->getLinkage()) {
261 case GlobalValue::LinkOnceLinkage:
262 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
263 // Nonnull linkonce -> weak
264 O << "\t.weak " << name << "\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000265 SwitchToDataSection("", I);
Chris Lattner1dbed162005-12-17 07:04:29 +0000266 O << "\t.section\t\".llvm.linkonce.d." << name
267 << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000268 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000269
Brian Gaeke4acfd032004-03-04 06:00:41 +0000270 case GlobalValue::AppendingLinkage:
271 // FIXME: appending linkage variables should go into a section of
272 // their name or something. For now, just emit them as external.
273 case GlobalValue::ExternalLinkage:
274 // If external or appending, declare as a global symbol
275 O << "\t.globl " << name << "\n";
276 // FALL THROUGH
277 case GlobalValue::InternalLinkage:
278 if (C->isNullValue())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000279 SwitchToDataSection(".bss", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000280 else
Chris Lattner4632d7a2006-05-09 04:59:56 +0000281 SwitchToDataSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000282 break;
Misha Brukmanc11c44f2004-11-19 21:49:19 +0000283 case GlobalValue::GhostLinkage:
284 std::cerr << "Should not have any unmaterialized functions!\n";
285 abort();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000286 }
287
288 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000289 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000290 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000291 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000292 WriteAsOperand(O, I, true, true, &M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000293 O << "\n";
Chris Lattner967abf32005-12-17 07:17:08 +0000294 EmitGlobalConstant(C);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000295 }
296 }
297
Chris Lattner1dbed162005-12-17 07:04:29 +0000298 AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000299 return false; // success
300}