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/TargetAsmInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Support/Mangler.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/MathExtras.h"
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40 struct VISIBILITY_HIDDEN SparcTargetAsmInfo : public TargetAsmInfo {
41 SparcTargetAsmInfo() {
42 Data16bitsDirective = "\t.half\t";
43 Data32bitsDirective = "\t.word\t";
44 Data64bitsDirective = 0; // .xword is only supported by V9.
45 ZeroDirective = "\t.skip\t";
47 ConstantPoolSection = "\t.section \".rodata\",#alloc\n";
51 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
52 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, TargetAsmInfo *T)
53 : AsmPrinter(O, TM, T) {
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().
60 typedef std::map<const Value *, unsigned> ValueMapTy;
61 ValueMapTy NumberForBB;
63 virtual const char *getPassName() const {
64 return "Sparc Assembly Printer";
67 void printOperand(const MachineInstr *MI, int opNum);
68 void printMemOperand(const MachineInstr *MI, int opNum,
69 const char *Modifier = 0);
70 void printCCOperand(const MachineInstr *MI, int opNum);
72 bool printInstruction(const MachineInstr *MI); // autogenerated.
73 bool runOnMachineFunction(MachineFunction &F);
74 bool doInitialization(Module &M);
75 bool doFinalization(Module &M);
77 } // end of anonymous namespace
79 #include "SparcGenAsmWriter.inc"
81 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
82 /// 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.
86 FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
88 SparcTargetAsmInfo *TAI = new SparcTargetAsmInfo();
89 return new SparcAsmPrinter(o, tm, TAI);
92 /// runOnMachineFunction - This uses the printMachineInstruction()
93 /// method to print assembly for each instruction.
95 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
96 SetupMachineFunction(MF);
98 // Print out constants referenced by the function
99 EmitConstantPool(MF.getConstantPool());
101 // 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;
106 // What's my mangled name?
107 CurrentFnName = Mang->getValueName(MF.getFunction());
109 // Print out labels for the function.
110 SwitchToTextSection(".text", MF.getFunction());
111 EmitAlignment(4, MF.getFunction());
112 O << "\t.globl\t" << CurrentFnName << "\n";
113 O << "\t.type\t" << CurrentFnName << ", #function\n";
114 O << CurrentFnName << ":\n";
116 // Number each basic block so that we can consistently refer to them
117 // in PC-relative references.
119 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
121 NumberForBB[I->getBasicBlock()] = BBNumber++;
124 // Print out code for the function.
125 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127 // Print a label for the basic block.
128 if (I != MF.begin()) {
129 printBasicBlockLabel(I, true);
132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
134 // Print the assembly for the instruction.
136 printInstruction(II);
141 // We didn't modify anything.
145 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
146 const MachineOperand &MO = MI->getOperand (opNum);
147 const MRegisterInfo &RI = *TM.getRegisterInfo();
148 bool CloseParen = false;
149 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
152 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
153 && !MO.isRegister() && !MO.isImmediate()) {
157 switch (MO.getType()) {
158 case MachineOperand::MO_Register:
159 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
160 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
162 O << "%reg" << MO.getReg();
165 case MachineOperand::MO_Immediate:
166 O << (int)MO.getImmedValue();
168 case MachineOperand::MO_MachineBasicBlock:
169 printBasicBlockLabel(MO.getMachineBasicBlock());
171 case MachineOperand::MO_GlobalAddress:
172 O << Mang->getValueName(MO.getGlobal());
174 case MachineOperand::MO_ExternalSymbol:
175 O << MO.getSymbolName();
177 case MachineOperand::MO_ConstantPoolIndex:
178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
179 << MO.getConstantPoolIndex();
182 O << "<unknown operand type>"; abort (); break;
184 if (CloseParen) O << ")";
187 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
188 const char *Modifier) {
189 printOperand(MI, opNum);
191 // If this is an ADD operand, emit it like normal operands.
192 if (Modifier && !strcmp(Modifier, "arith")) {
194 printOperand(MI, opNum+1);
198 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType();
200 if (MI->getOperand(opNum+1).isRegister() &&
201 MI->getOperand(opNum+1).getReg() == SP::G0)
202 return; // don't print "+%g0"
203 if (MI->getOperand(opNum+1).isImmediate() &&
204 MI->getOperand(opNum+1).getImmedValue() == 0)
205 return; // don't print "+0"
208 if (MI->getOperand(opNum+1).isGlobalAddress() ||
209 MI->getOperand(opNum+1).isConstantPoolIndex()) {
211 printOperand(MI, opNum+1);
214 printOperand(MI, opNum+1);
218 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
219 int CC = (int)MI->getOperand(opNum).getImmedValue();
220 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
225 bool SparcAsmPrinter::doInitialization(Module &M) {
226 Mang = new Mangler(M);
227 return false; // success
230 bool SparcAsmPrinter::doFinalization(Module &M) {
231 const TargetData *TD = TM.getTargetData();
233 // Print out module-level global variables here.
234 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
236 if (I->hasInitializer()) { // External global require no code
237 // Check to see if this is a special global used by LLVM, if so, emit it.
238 if (EmitSpecialLLVMGlobal(I))
242 std::string name = Mang->getValueName(I);
243 Constant *C = I->getInitializer();
244 unsigned Size = TD->getTypeSize(C->getType());
245 unsigned Align = TD->getTypeAlignment(C->getType());
247 if (C->isNullValue() &&
248 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
249 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
250 SwitchToDataSection(".data", I);
251 if (I->hasInternalLinkage())
252 O << "\t.local " << name << "\n";
254 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
255 << "," << (unsigned)TD->getTypeAlignment(C->getType());
257 WriteAsOperand(O, I, true, true, &M);
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";
265 SwitchToDataSection("", I);
266 O << "\t.section\t\".llvm.linkonce.d." << name
267 << "\",\"aw\",@progbits\n";
270 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";
277 case GlobalValue::InternalLinkage:
278 if (C->isNullValue())
279 SwitchToDataSection(".bss", I);
281 SwitchToDataSection(".data", I);
283 case GlobalValue::GhostLinkage:
284 std::cerr << "Should not have any unmaterialized functions!\n";
288 O << "\t.align " << Align << "\n";
289 O << "\t.type " << name << ",#object\n";
290 O << "\t.size " << name << "," << Size << "\n";
291 O << name << ":\t\t\t\t! ";
292 WriteAsOperand(O, I, true, true, &M);
294 EmitGlobalConstant(C);
298 AsmPrinter::doFinalization(M);
299 return false; // success