1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "SparcInstrInfo.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.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, "Number of machine instrs printed");
41 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
42 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
43 : AsmPrinter(O, TM, T) {
46 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
53 virtual const char *getPassName() const {
54 return "Sparc Assembly Printer";
57 void printOperand(const MachineInstr *MI, int opNum);
58 void printMemOperand(const MachineInstr *MI, int opNum,
59 const char *Modifier = 0);
60 void printCCOperand(const MachineInstr *MI, int opNum);
62 bool printInstruction(const MachineInstr *MI); // autogenerated.
63 bool runOnMachineFunction(MachineFunction &F);
64 bool doInitialization(Module &M);
65 bool doFinalization(Module &M);
67 } // end of anonymous namespace
69 #include "SparcGenAsmWriter.inc"
71 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
72 /// assembly code for a MachineFunction to the given output stream,
73 /// using the given target machine description. This should work
74 /// regardless of whether the function is in SSA form.
76 FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
78 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
81 /// runOnMachineFunction - This uses the printInstruction()
82 /// method to print assembly for each instruction.
84 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
85 SetupMachineFunction(MF);
87 // Print out constants referenced by the function
88 EmitConstantPool(MF.getConstantPool());
90 // BBNumber is used here so that a given Printer will never give two
91 // BBs the same name. (If you have a better way, please let me know!)
92 static unsigned BBNumber = 0;
95 // What's my mangled name?
96 CurrentFnName = Mang->getValueName(MF.getFunction());
98 // Print out the label for the function.
99 const Function *F = MF.getFunction();
100 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
102 O << "\t.globl\t" << CurrentFnName << "\n";
103 O << "\t.type\t" << CurrentFnName << ", #function\n";
104 O << CurrentFnName << ":\n";
106 // Number each basic block so that we can consistently refer to them
107 // in PC-relative references.
108 // FIXME: Why not use the MBB numbers?
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, true);
123 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
125 // Print the assembly for the instruction.
126 printInstruction(II);
131 // We didn't modify anything.
135 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
136 const MachineOperand &MO = MI->getOperand (opNum);
137 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
138 bool CloseParen = false;
139 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
142 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
143 && !MO.isRegister() && !MO.isImmediate()) {
147 switch (MO.getType()) {
148 case MachineOperand::MO_Register:
149 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
150 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
152 O << "%reg" << MO.getReg();
155 case MachineOperand::MO_Immediate:
156 O << (int)MO.getImm();
158 case MachineOperand::MO_MachineBasicBlock:
159 printBasicBlockLabel(MO.getMBB());
161 case MachineOperand::MO_GlobalAddress:
162 O << Mang->getValueName(MO.getGlobal());
164 case MachineOperand::MO_ExternalSymbol:
165 O << MO.getSymbolName();
167 case MachineOperand::MO_ConstantPoolIndex:
168 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
172 O << "<unknown operand type>"; abort (); break;
174 if (CloseParen) O << ")";
177 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
178 const char *Modifier) {
179 printOperand(MI, opNum);
181 // If this is an ADD operand, emit it like normal operands.
182 if (Modifier && !strcmp(Modifier, "arith")) {
184 printOperand(MI, opNum+1);
188 if (MI->getOperand(opNum+1).isRegister() &&
189 MI->getOperand(opNum+1).getReg() == SP::G0)
190 return; // don't print "+%g0"
191 if (MI->getOperand(opNum+1).isImmediate() &&
192 MI->getOperand(opNum+1).getImm() == 0)
193 return; // don't print "+0"
196 if (MI->getOperand(opNum+1).isGlobalAddress() ||
197 MI->getOperand(opNum+1).isConstantPoolIndex()) {
199 printOperand(MI, opNum+1);
202 printOperand(MI, opNum+1);
206 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
207 int CC = (int)MI->getOperand(opNum).getImm();
208 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
213 bool SparcAsmPrinter::doInitialization(Module &M) {
214 Mang = new Mangler(M);
215 return false; // success
218 bool SparcAsmPrinter::doFinalization(Module &M) {
219 const TargetData *TD = TM.getTargetData();
221 // Print out module-level global variables here.
222 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
224 if (I->hasInitializer()) { // External global require no code
225 // Check to see if this is a special global used by LLVM, if so, emit it.
226 if (EmitSpecialLLVMGlobal(I))
230 std::string name = Mang->getValueName(I);
231 Constant *C = I->getInitializer();
232 unsigned Size = TD->getABITypeSize(C->getType());
233 unsigned Align = TD->getPreferredAlignment(I);
235 if (C->isNullValue() && (I->hasCommonLinkage() ||
236 I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
237 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
238 SwitchToDataSection(".data", I);
239 if (I->hasInternalLinkage())
240 O << "\t.local " << name << "\n";
242 O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType())
246 switch (I->getLinkage()) {
247 case GlobalValue::CommonLinkage:
248 case GlobalValue::LinkOnceLinkage:
249 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
250 // Nonnull linkonce -> weak
251 O << "\t.weak " << name << "\n";
252 SwitchToDataSection("", I);
253 O << "\t.section\t\".llvm.linkonce.d." << name
254 << "\",\"aw\",@progbits\n";
257 case GlobalValue::AppendingLinkage:
258 // FIXME: appending linkage variables should go into a section of
259 // their name or something. For now, just emit them as external.
260 case GlobalValue::ExternalLinkage:
261 // If external or appending, declare as a global symbol
262 O << "\t.globl " << name << "\n";
264 case GlobalValue::InternalLinkage:
265 if (C->isNullValue())
266 SwitchToDataSection(".bss", I);
268 SwitchToDataSection(".data", I);
270 case GlobalValue::GhostLinkage:
271 cerr << "Should not have any unmaterialized functions!\n";
273 case GlobalValue::DLLImportLinkage:
274 cerr << "DLLImport linkage is not supported by this target!\n";
276 case GlobalValue::DLLExportLinkage:
277 cerr << "DLLExport linkage is not supported by this target!\n";
280 assert(0 && "Unknown linkage type!");
283 O << "\t.align " << Align << "\n";
284 O << "\t.type " << name << ",#object\n";
285 O << "\t.size " << name << "," << Size << "\n";
287 EmitGlobalConstant(C);
291 return AsmPrinter::doFinalization(M);