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 "SparcTargetMachine.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetRegistry.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/MathExtras.h"
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
46 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
51 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
55 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
56 const MCAsmInfo *T, bool V)
57 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
59 virtual const char *getPassName() const {
60 return "Sparc Assembly Printer";
63 void PrintGlobalVariable(const GlobalVariable *GVar);
64 void printOperand(const MachineInstr *MI, int opNum);
65 void printMemOperand(const MachineInstr *MI, int opNum,
66 const char *Modifier = 0);
67 void printCCOperand(const MachineInstr *MI, int opNum);
69 void printInstruction(const MachineInstr *MI); // autogenerated.
70 bool runOnMachineFunction(MachineFunction &F);
71 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
73 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
74 unsigned AsmVariant, const char *ExtraCode);
76 } // end of anonymous namespace
78 #include "SparcGenAsmWriter.inc"
81 /// runOnMachineFunction - This uses the printInstruction()
82 /// method to print assembly for each instruction.
84 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!)
97 // Print out the label for the function.
98 const Function *F = MF.getFunction();
99 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
100 EmitAlignment(MF.getAlignment(), F);
101 O << "\t.globl\t" << CurrentFnName << '\n';
103 printVisibility(CurrentFnName, F->getVisibility());
105 O << "\t.type\t" << CurrentFnName << ", #function\n";
106 O << CurrentFnName << ":\n";
107 // Emit pre-function debug information.
108 DW->BeginFunction(&MF);
110 // Number each basic block so that we can consistently refer to them
111 // in PC-relative references.
112 // FIXME: Why not use the MBB numbers?
114 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116 NumberForBB[I->getBasicBlock()] = BBNumber++;
119 // Print out code for the function.
120 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
122 // Print a label for the basic block.
123 if (I != MF.begin()) {
124 printBasicBlockLabel(I, true, true);
127 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
129 // Print the assembly for the instruction.
130 printInstruction(II);
135 // Emit post-function debug information.
136 DW->EndFunction(&MF);
138 // We didn't modify anything.
142 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
143 const MachineOperand &MO = MI->getOperand (opNum);
144 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
145 bool CloseParen = false;
146 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
149 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
150 !MO.isReg() && !MO.isImm()) {
154 switch (MO.getType()) {
155 case MachineOperand::MO_Register:
156 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
157 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
159 O << "%reg" << MO.getReg();
162 case MachineOperand::MO_Immediate:
163 O << (int)MO.getImm();
165 case MachineOperand::MO_MachineBasicBlock:
166 printBasicBlockLabel(MO.getMBB());
168 case MachineOperand::MO_GlobalAddress:
169 O << Mang->getMangledName(MO.getGlobal());
171 case MachineOperand::MO_ExternalSymbol:
172 O << MO.getSymbolName();
174 case MachineOperand::MO_ConstantPoolIndex:
175 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
179 llvm_unreachable("<unknown operand type>");
181 if (CloseParen) O << ")";
184 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
185 const char *Modifier) {
186 printOperand(MI, opNum);
188 // If this is an ADD operand, emit it like normal operands.
189 if (Modifier && !strcmp(Modifier, "arith")) {
191 printOperand(MI, opNum+1);
195 if (MI->getOperand(opNum+1).isReg() &&
196 MI->getOperand(opNum+1).getReg() == SP::G0)
197 return; // don't print "+%g0"
198 if (MI->getOperand(opNum+1).isImm() &&
199 MI->getOperand(opNum+1).getImm() == 0)
200 return; // don't print "+0"
203 if (MI->getOperand(opNum+1).isGlobal() ||
204 MI->getOperand(opNum+1).isCPI()) {
206 printOperand(MI, opNum+1);
209 printOperand(MI, opNum+1);
213 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
214 int CC = (int)MI->getOperand(opNum).getImm();
215 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
218 void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
219 const TargetData *TD = TM.getTargetData();
221 if (!GVar->hasInitializer())
222 return; // External global require no code
224 // Check to see if this is a special global used by LLVM, if so, emit it.
225 if (EmitSpecialLLVMGlobal(GVar))
229 std::string name = Mang->getMangledName(GVar);
230 Constant *C = GVar->getInitializer();
231 unsigned Size = TD->getTypeAllocSize(C->getType());
232 unsigned Align = TD->getPreferredAlignment(GVar);
234 printVisibility(name, GVar->getVisibility());
236 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
239 if (C->isNullValue() && !GVar->hasSection()) {
240 if (!GVar->isThreadLocal() &&
241 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
242 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
244 if (GVar->hasLocalLinkage())
245 O << "\t.local " << name << '\n';
247 O << MAI->getCOMMDirective() << name << ',' << Size;
248 if (MAI->getCOMMDirectiveTakesAlignment())
249 O << ',' << (1 << Align);
256 switch (GVar->getLinkage()) {
257 case GlobalValue::CommonLinkage:
258 case GlobalValue::LinkOnceAnyLinkage:
259 case GlobalValue::LinkOnceODRLinkage:
260 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
261 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
262 // Nonnull linkonce -> weak
263 O << "\t.weak " << name << '\n';
265 case GlobalValue::AppendingLinkage:
266 // FIXME: appending linkage variables should go into a section of
267 // their name or something. For now, just emit them as external.
268 case GlobalValue::ExternalLinkage:
269 // If external or appending, declare as a global symbol
270 O << MAI->getGlobalDirective() << name << '\n';
272 case GlobalValue::PrivateLinkage:
273 case GlobalValue::LinkerPrivateLinkage:
274 case GlobalValue::InternalLinkage:
276 case GlobalValue::GhostLinkage:
277 llvm_unreachable("Should not have any unmaterialized functions!");
278 case GlobalValue::DLLImportLinkage:
279 llvm_unreachable("DLLImport linkage is not supported by this target!");
280 case GlobalValue::DLLExportLinkage:
281 llvm_unreachable("DLLExport linkage is not supported by this target!");
283 llvm_unreachable("Unknown linkage type!");
286 EmitAlignment(Align, GVar);
288 if (MAI->hasDotTypeDotSizeDirective()) {
289 O << "\t.type " << name << ",#object\n";
290 O << "\t.size " << name << ',' << Size << '\n';
294 EmitGlobalConstant(C);
297 /// PrintAsmOperand - Print out an operand for an inline asm expression.
299 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
301 const char *ExtraCode) {
302 if (ExtraCode && ExtraCode[0]) {
303 if (ExtraCode[1] != 0) return true; // Unknown modifier.
305 switch (ExtraCode[0]) {
306 default: return true; // Unknown modifier.
312 printOperand(MI, OpNo);
317 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
320 const char *ExtraCode) {
321 if (ExtraCode && ExtraCode[0])
322 return true; // Unknown modifier
325 printMemOperand(MI, OpNo);
331 // Force static initialization.
332 extern "C" void LLVMInitializeSparcAsmPrinter() {
333 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget);