blob: 0de7fb032bb28ff27aad7ebc3a336b1d24728aae [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"
Owen Anderson07000c62006-05-12 06:33:49 +000025#include "llvm/Target/TargetData.h"
Brian Gaeke4acfd032004-03-04 06:00:41 +000026#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Mangler.h"
Brian Gaeke74dfcf12004-09-02 02:37:43 +000028#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/StringExtras.h"
30#include "llvm/Support/CommandLine.h"
Jim Laskeyb8df7c22005-08-17 20:04:34 +000031#include "llvm/Support/MathExtras.h"
Brian Gaekea8b00ca2004-03-06 05:30:21 +000032#include <cctype>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000033#include <iostream>
Brian Gaeke4acfd032004-03-04 06:00:41 +000034using namespace llvm;
35
36namespace {
37 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Chris Lattner7c90f732006-02-05 05:50:24 +000039 struct SparcAsmPrinter : public AsmPrinter {
40 SparcAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000041 Data16bitsDirective = "\t.half\t";
42 Data32bitsDirective = "\t.word\t";
Chris Lattner379e6c02005-12-18 23:36:45 +000043 Data64bitsDirective = 0; // .xword is only supported by V9.
Chris Lattner45090472006-02-15 07:07:14 +000044 ZeroDirective = "\t.skip\t";
Chris Lattner7a48e502005-12-18 23:35:05 +000045 CommentString = "!";
46 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000047 }
Brian Gaeke4acfd032004-03-04 06:00:41 +000048
49 /// We name each basic block in a Function with a unique number, so
50 /// that we can consistently refer to them later. This is cleared
51 /// at the beginning of each call to runOnMachineFunction().
52 ///
53 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
55
Brian Gaeke4acfd032004-03-04 06:00:41 +000056 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000057 return "Sparc Assembly Printer";
Brian Gaeke4acfd032004-03-04 06:00:41 +000058 }
59
Brian Gaeke446ae112004-06-15 19:52:59 +000060 void printOperand(const MachineInstr *MI, int opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +000061 void printMemOperand(const MachineInstr *MI, int opNum,
62 const char *Modifier = 0);
Chris Lattner7c90f732006-02-05 05:50:24 +000063 void printCCOperand(const MachineInstr *MI, int opNum);
Chris Lattner6788faa2006-01-31 06:49:09 +000064
Chris Lattner994b7352005-12-16 06:34:17 +000065 bool printInstruction(const MachineInstr *MI); // autogenerated.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000066 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke4acfd032004-03-04 06:00:41 +000067 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 };
70} // end of anonymous namespace
71
Chris Lattner7c90f732006-02-05 05:50:24 +000072#include "SparcGenAsmWriter.inc"
Chris Lattner994b7352005-12-16 06:34:17 +000073
Chris Lattner7c90f732006-02-05 05:50:24 +000074/// createSparcCodePrinterPass - Returns a pass that prints the SPARC
Brian Gaeke4acfd032004-03-04 06:00:41 +000075/// assembly code for a MachineFunction to the given output stream,
76/// using the given target machine description. This should work
77/// regardless of whether the function is in SSA form.
78///
Chris Lattner7c90f732006-02-05 05:50:24 +000079FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
80 TargetMachine &tm) {
81 return new SparcAsmPrinter(o, tm);
Brian Gaeke4acfd032004-03-04 06:00:41 +000082}
83
Brian Gaeke4acfd032004-03-04 06:00:41 +000084/// runOnMachineFunction - This uses the printMachineInstruction()
85/// method to print assembly for each instruction.
86///
Chris Lattner7c90f732006-02-05 05:50:24 +000087bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner1dbed162005-12-17 07:04:29 +000088 SetupMachineFunction(MF);
89
Chris Lattnerb5e9eb62005-12-17 07:11:43 +000090 // Print out constants referenced by the function
91 EmitConstantPool(MF.getConstantPool());
92
Brian Gaeke4acfd032004-03-04 06:00:41 +000093 // BBNumber is used here so that a given Printer will never give two
94 // BBs the same name. (If you have a better way, please let me know!)
95 static unsigned BBNumber = 0;
96
97 O << "\n\n";
98 // What's my mangled name?
99 CurrentFnName = Mang->getValueName(MF.getFunction());
100
Brian Gaeke4acfd032004-03-04 06:00:41 +0000101 // Print out labels for the function.
Chris Lattner4632d7a2006-05-09 04:59:56 +0000102 SwitchToTextSection(".text", MF.getFunction());
103 EmitAlignment(4, MF.getFunction());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000104 O << "\t.globl\t" << CurrentFnName << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000105 O << "\t.type\t" << CurrentFnName << ", #function\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000106 O << CurrentFnName << ":\n";
107
108 // Number each basic block so that we can consistently refer to them
109 // in PC-relative references.
110 NumberForBB.clear();
111 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
112 I != E; ++I) {
113 NumberForBB[I->getBasicBlock()] = BBNumber++;
114 }
115
116 // Print out code for the function.
117 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 I != E; ++I) {
119 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000120 if (I != MF.begin()) {
121 printBasicBlockLabel(I, true);
122 O << '\n';
123 }
Brian Gaeke4acfd032004-03-04 06:00:41 +0000124 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukman27177f82005-04-22 18:06:01 +0000125 II != E; ++II) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000126 // Print the assembly for the instruction.
Chris Lattner0d8fcd32005-12-17 06:54:41 +0000127 O << "\t";
128 printInstruction(II);
Chris Lattner1dbed162005-12-17 07:04:29 +0000129 ++EmittedInsts;
Brian Gaeke4acfd032004-03-04 06:00:41 +0000130 }
131 }
132
133 // We didn't modify anything.
134 return false;
135}
136
Chris Lattner7c90f732006-02-05 05:50:24 +0000137void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000138 const MachineOperand &MO = MI->getOperand (opNum);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000139 const MRegisterInfo &RI = *TM.getRegisterInfo();
Brian Gaeke446ae112004-06-15 19:52:59 +0000140 bool CloseParen = false;
Chris Lattner7c90f732006-02-05 05:50:24 +0000141 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000142 O << "%hi(";
143 CloseParen = true;
Chris Lattner7c90f732006-02-05 05:50:24 +0000144 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
Chris Lattner4fca0172006-01-15 09:26:27 +0000145 && !MO.isRegister() && !MO.isImmediate()) {
Brian Gaeke446ae112004-06-15 19:52:59 +0000146 O << "%lo(";
147 CloseParen = true;
148 }
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000149 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000150 case MachineOperand::MO_Register:
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000151 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaekea8b00ca2004-03-06 05:30:21 +0000152 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000153 else
154 O << "%reg" << MO.getReg();
Brian Gaeke446ae112004-06-15 19:52:59 +0000155 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000156
Chris Lattner63b3d712006-05-04 17:21:20 +0000157 case MachineOperand::MO_Immediate:
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000158 O << (int)MO.getImmedValue();
Brian Gaeke446ae112004-06-15 19:52:59 +0000159 break;
Nate Begeman37efe672006-04-22 18:53:45 +0000160 case MachineOperand::MO_MachineBasicBlock:
161 printBasicBlockLabel(MO.getMachineBasicBlock());
Brian Gaeke09c13092004-06-17 19:39:23 +0000162 return;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000163 case MachineOperand::MO_GlobalAddress:
164 O << Mang->getValueName(MO.getGlobal());
Brian Gaeke446ae112004-06-15 19:52:59 +0000165 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000166 case MachineOperand::MO_ExternalSymbol:
167 O << MO.getSymbolName();
Brian Gaeke446ae112004-06-15 19:52:59 +0000168 break;
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000169 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnerb5e9eb62005-12-17 07:11:43 +0000170 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
171 << MO.getConstantPoolIndex();
Brian Gaeke8a0ae9e2004-06-27 22:50:44 +0000172 break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000173 default:
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000174 O << "<unknown operand type>"; abort (); break;
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000175 }
Brian Gaeke446ae112004-06-15 19:52:59 +0000176 if (CloseParen) O << ")";
Brian Gaeke62aa28a2004-03-05 08:39:09 +0000177}
178
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000179void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
180 const char *Modifier) {
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000181 printOperand(MI, opNum);
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000182
183 // If this is an ADD operand, emit it like normal operands.
184 if (Modifier && !strcmp(Modifier, "arith")) {
185 O << ", ";
186 printOperand(MI, opNum+1);
187 return;
188 }
189
Chris Lattner76acc872005-12-18 02:37:35 +0000190 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
191
Chris Lattner2d90ac72006-05-04 18:05:43 +0000192 if (MI->getOperand(opNum+1).isRegister() &&
Chris Lattner7c90f732006-02-05 05:50:24 +0000193 MI->getOperand(opNum+1).getReg() == SP::G0)
Chris Lattner76acc872005-12-18 02:37:35 +0000194 return; // don't print "+%g0"
Chris Lattner2d90ac72006-05-04 18:05:43 +0000195 if (MI->getOperand(opNum+1).isImmediate() &&
Chris Lattner76acc872005-12-18 02:37:35 +0000196 MI->getOperand(opNum+1).getImmedValue() == 0)
197 return; // don't print "+0"
198
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000199 O << "+";
Chris Lattner2d90ac72006-05-04 18:05:43 +0000200 if (MI->getOperand(opNum+1).isGlobalAddress() ||
201 MI->getOperand(opNum+1).isConstantPoolIndex()) {
Chris Lattnere1389ad2005-12-18 02:27:00 +0000202 O << "%lo(";
203 printOperand(MI, opNum+1);
204 O << ")";
205 } else {
206 printOperand(MI, opNum+1);
207 }
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000208}
209
Chris Lattner7c90f732006-02-05 05:50:24 +0000210void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
Chris Lattner6788faa2006-01-31 06:49:09 +0000211 int CC = (int)MI->getOperand(opNum).getImmedValue();
Chris Lattner7c90f732006-02-05 05:50:24 +0000212 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
Chris Lattner6788faa2006-01-31 06:49:09 +0000213}
214
215
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000216
Chris Lattner7c90f732006-02-05 05:50:24 +0000217bool SparcAsmPrinter::doInitialization(Module &M) {
Brian Gaeke4acfd032004-03-04 06:00:41 +0000218 Mang = new Mangler(M);
219 return false; // success
220}
221
Chris Lattner7c90f732006-02-05 05:50:24 +0000222bool SparcAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000223 const TargetData *TD = TM.getTargetData();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000224
225 // Print out module-level global variables here.
Chris Lattner04f96742006-03-09 06:14:35 +0000226 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
227 I != E; ++I)
Brian Gaeke4acfd032004-03-04 06:00:41 +0000228 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000229 // Check to see if this is a special global used by LLVM, if so, emit it.
230 if (EmitSpecialLLVMGlobal(I))
231 continue;
232
Brian Gaeke4acfd032004-03-04 06:00:41 +0000233 O << "\n\n";
234 std::string name = Mang->getValueName(I);
235 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000236 unsigned Size = TD->getTypeSize(C->getType());
237 unsigned Align = TD->getTypeAlignment(C->getType());
Brian Gaeke4acfd032004-03-04 06:00:41 +0000238
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000239 if (C->isNullValue() &&
Brian Gaeke4acfd032004-03-04 06:00:41 +0000240 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
241 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000242 SwitchToDataSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000243 if (I->hasInternalLinkage())
244 O << "\t.local " << name << "\n";
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000245
Owen Andersona69571c2006-05-03 01:29:57 +0000246 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
247 << "," << (unsigned)TD->getTypeAlignment(C->getType());
Brian Gaeke79db7402004-03-16 22:37:12 +0000248 O << "\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000249 WriteAsOperand(O, I, true, true, &M);
250 O << "\n";
251 } else {
252 switch (I->getLinkage()) {
253 case GlobalValue::LinkOnceLinkage:
254 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
255 // Nonnull linkonce -> weak
256 O << "\t.weak " << name << "\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000257 SwitchToDataSection("", I);
Chris Lattner1dbed162005-12-17 07:04:29 +0000258 O << "\t.section\t\".llvm.linkonce.d." << name
259 << "\",\"aw\",@progbits\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000260 break;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000261
Brian Gaeke4acfd032004-03-04 06:00:41 +0000262 case GlobalValue::AppendingLinkage:
263 // FIXME: appending linkage variables should go into a section of
264 // their name or something. For now, just emit them as external.
265 case GlobalValue::ExternalLinkage:
266 // If external or appending, declare as a global symbol
267 O << "\t.globl " << name << "\n";
268 // FALL THROUGH
269 case GlobalValue::InternalLinkage:
270 if (C->isNullValue())
Chris Lattner4632d7a2006-05-09 04:59:56 +0000271 SwitchToDataSection(".bss", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000272 else
Chris Lattner4632d7a2006-05-09 04:59:56 +0000273 SwitchToDataSection(".data", I);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000274 break;
Misha Brukmanc11c44f2004-11-19 21:49:19 +0000275 case GlobalValue::GhostLinkage:
276 std::cerr << "Should not have any unmaterialized functions!\n";
277 abort();
Brian Gaeke4acfd032004-03-04 06:00:41 +0000278 }
279
280 O << "\t.align " << Align << "\n";
Brian Gaeke54cc3c22004-03-16 22:52:04 +0000281 O << "\t.type " << name << ",#object\n";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000282 O << "\t.size " << name << "," << Size << "\n";
Brian Gaeke79db7402004-03-16 22:37:12 +0000283 O << name << ":\t\t\t\t! ";
Brian Gaeke4acfd032004-03-04 06:00:41 +0000284 WriteAsOperand(O, I, true, true, &M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000285 O << "\n";
Chris Lattner967abf32005-12-17 07:17:08 +0000286 EmitGlobalConstant(C);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000287 }
288 }
289
Chris Lattner1dbed162005-12-17 07:04:29 +0000290 AsmPrinter::doFinalization(M);
Brian Gaeke4acfd032004-03-04 06:00:41 +0000291 return false; // success
292}