1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to GAS-format SPARC assembly language.
13 //===----------------------------------------------------------------------===//
16 #include "SparcInstrInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/MathExtras.h"
36 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38 struct SparcAsmPrinter : public AsmPrinter {
39 SparcAsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
40 Data16bitsDirective = "\t.half\t";
41 Data32bitsDirective = "\t.word\t";
42 Data64bitsDirective = 0; // .xword is only supported by V9.
43 ZeroDirective = "\t.skip\t";
45 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
55 virtual const char *getPassName() const {
56 return "Sparc Assembly Printer";
59 void printOperand(const MachineInstr *MI, int opNum);
60 void printMemOperand(const MachineInstr *MI, int opNum,
61 const char *Modifier = 0);
62 void printCCOperand(const MachineInstr *MI, int opNum);
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 bool runOnMachineFunction(MachineFunction &F);
66 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
69 } // end of anonymous namespace
71 #include "SparcGenAsmWriter.inc"
73 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
74 /// 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.
78 FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
80 return new SparcAsmPrinter(o, tm);
83 /// runOnMachineFunction - This uses the printMachineInstruction()
84 /// method to print assembly for each instruction.
86 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
87 SetupMachineFunction(MF);
89 // Print out constants referenced by the function
90 EmitConstantPool(MF.getConstantPool());
92 // 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;
97 // What's my mangled name?
98 CurrentFnName = Mang->getValueName(MF.getFunction());
100 // Print out labels for the function.
102 O << "\t.align 16\n";
103 O << "\t.globl\t" << CurrentFnName << "\n";
104 O << "\t.type\t" << CurrentFnName << ", #function\n";
105 O << CurrentFnName << ":\n";
107 // Number each basic block so that we can consistently refer to them
108 // in PC-relative references.
110 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
112 NumberForBB[I->getBasicBlock()] = BBNumber++;
115 // Print out code for the function.
116 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
118 // Print a label for the basic block.
119 if (I != MF.begin()) {
120 printBasicBlockLabel(I, true);
123 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
125 // Print the assembly for the instruction.
127 printInstruction(II);
132 // We didn't modify anything.
136 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
137 const MachineOperand &MO = MI->getOperand (opNum);
138 const MRegisterInfo &RI = *TM.getRegisterInfo();
139 bool CloseParen = false;
140 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
143 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
144 && !MO.isRegister() && !MO.isImmediate()) {
148 switch (MO.getType()) {
149 case MachineOperand::MO_VirtualRegister:
150 if (Value *V = MO.getVRegValueOrNull()) {
151 O << "<" << V->getName() << ">";
155 case MachineOperand::MO_MachineRegister:
156 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
157 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
159 O << "%reg" << MO.getReg();
162 case MachineOperand::MO_SignExtendedImmed:
163 case MachineOperand::MO_UnextendedImmed:
164 O << (int)MO.getImmedValue();
166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMachineBasicBlock());
169 case MachineOperand::MO_PCRelativeDisp:
170 std::cerr << "Shouldn't use addPCDisp() when building Sparc MachineInstrs";
173 case MachineOperand::MO_GlobalAddress:
174 O << Mang->getValueName(MO.getGlobal());
176 case MachineOperand::MO_ExternalSymbol:
177 O << MO.getSymbolName();
179 case MachineOperand::MO_ConstantPoolIndex:
180 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
181 << MO.getConstantPoolIndex();
184 O << "<unknown operand type>"; abort (); break;
186 if (CloseParen) O << ")";
189 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
190 const char *Modifier) {
191 printOperand(MI, opNum);
193 // If this is an ADD operand, emit it like normal operands.
194 if (Modifier && !strcmp(Modifier, "arith")) {
196 printOperand(MI, opNum+1);
200 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
202 if ((OpTy == MachineOperand::MO_VirtualRegister ||
203 OpTy == MachineOperand::MO_MachineRegister) &&
204 MI->getOperand(opNum+1).getReg() == SP::G0)
205 return; // don't print "+%g0"
206 if ((OpTy == MachineOperand::MO_SignExtendedImmed ||
207 OpTy == MachineOperand::MO_UnextendedImmed) &&
208 MI->getOperand(opNum+1).getImmedValue() == 0)
209 return; // don't print "+0"
212 if (OpTy == MachineOperand::MO_GlobalAddress ||
213 OpTy == MachineOperand::MO_ConstantPoolIndex) {
215 printOperand(MI, opNum+1);
218 printOperand(MI, opNum+1);
222 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
223 int CC = (int)MI->getOperand(opNum).getImmedValue();
224 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
229 bool SparcAsmPrinter::doInitialization(Module &M) {
230 Mang = new Mangler(M);
231 return false; // success
234 bool SparcAsmPrinter::doFinalization(Module &M) {
235 const TargetData *TD = TM.getTargetData();
237 // Print out module-level global variables here.
238 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
240 if (I->hasInitializer()) { // External global require no code
241 // Check to see if this is a special global used by LLVM, if so, emit it.
242 if (EmitSpecialLLVMGlobal(I))
246 std::string name = Mang->getValueName(I);
247 Constant *C = I->getInitializer();
248 unsigned Size = TD->getTypeSize(C->getType());
249 unsigned Align = TD->getTypeAlignment(C->getType());
251 if (C->isNullValue() &&
252 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
253 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
254 SwitchSection(".data", I);
255 if (I->hasInternalLinkage())
256 O << "\t.local " << name << "\n";
258 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
259 << "," << (unsigned)TD->getTypeAlignment(C->getType());
261 WriteAsOperand(O, I, true, true, &M);
264 switch (I->getLinkage()) {
265 case GlobalValue::LinkOnceLinkage:
266 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
267 // Nonnull linkonce -> weak
268 O << "\t.weak " << name << "\n";
269 SwitchSection("", I);
270 O << "\t.section\t\".llvm.linkonce.d." << name
271 << "\",\"aw\",@progbits\n";
274 case GlobalValue::AppendingLinkage:
275 // FIXME: appending linkage variables should go into a section of
276 // their name or something. For now, just emit them as external.
277 case GlobalValue::ExternalLinkage:
278 // If external or appending, declare as a global symbol
279 O << "\t.globl " << name << "\n";
281 case GlobalValue::InternalLinkage:
282 if (C->isNullValue())
283 SwitchSection(".bss", I);
285 SwitchSection(".data", I);
287 case GlobalValue::GhostLinkage:
288 std::cerr << "Should not have any unmaterialized functions!\n";
292 O << "\t.align " << Align << "\n";
293 O << "\t.type " << name << ",#object\n";
294 O << "\t.size " << name << "," << Size << "\n";
295 O << name << ":\t\t\t\t! ";
296 WriteAsOperand(O, I, true, true, &M);
298 EmitGlobalConstant(C);
302 AsmPrinter::doFinalization(M);
303 return false; // success