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/MDNode.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/StringExtras.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/MathExtras.h"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
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;
54 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
55 const TargetAsmInfo *T, bool V)
56 : AsmPrinter(O, TM, T, V), BBNumber(0) {}
58 virtual const char *getPassName() const {
59 return "Sparc Assembly Printer";
62 void printModuleLevelGV(const GlobalVariable* GVar);
63 void printOperand(const MachineInstr *MI, int opNum);
64 void printMemOperand(const MachineInstr *MI, int opNum,
65 const char *Modifier = 0);
66 void printCCOperand(const MachineInstr *MI, int opNum);
68 bool printInstruction(const MachineInstr *MI); // autogenerated.
69 bool runOnMachineFunction(MachineFunction &F);
70 bool doFinalization(Module &M);
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"
80 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
81 /// assembly code for a MachineFunction to the given output stream,
82 /// using the given target machine description. This should work
83 /// regardless of whether the function is in SSA form.
85 FunctionPass *llvm::createSparcCodePrinterPass(formatted_raw_ostream &o,
88 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
92 /// runOnMachineFunction - This uses the printInstruction()
93 /// method to print assembly for each instruction.
95 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
98 SetupMachineFunction(MF);
100 // Print out constants referenced by the function
101 EmitConstantPool(MF.getConstantPool());
103 // BBNumber is used here so that a given Printer will never give two
104 // BBs the same name. (If you have a better way, please let me know!)
108 // Print out the label for the function.
109 const Function *F = MF.getFunction();
110 SwitchToSection(TAI->SectionForGlobal(F));
111 EmitAlignment(MF.getAlignment(), F);
112 O << "\t.globl\t" << CurrentFnName << '\n';
114 printVisibility(CurrentFnName, F->getVisibility());
116 O << "\t.type\t" << CurrentFnName << ", #function\n";
117 O << CurrentFnName << ":\n";
119 // Number each basic block so that we can consistently refer to them
120 // in PC-relative references.
121 // FIXME: Why not use the MBB numbers?
123 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
125 NumberForBB[I->getBasicBlock()] = BBNumber++;
128 // Print out code for the function.
129 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
131 // Print a label for the basic block.
132 if (I != MF.begin()) {
133 printBasicBlockLabel(I, true, true);
136 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138 // Print the assembly for the instruction.
139 printInstruction(II);
144 // We didn't modify anything.
148 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
149 const MachineOperand &MO = MI->getOperand (opNum);
150 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
151 bool CloseParen = false;
152 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
155 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
156 !MO.isReg() && !MO.isImm()) {
160 switch (MO.getType()) {
161 case MachineOperand::MO_Register:
162 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
163 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
165 O << "%reg" << MO.getReg();
168 case MachineOperand::MO_Immediate:
169 O << (int)MO.getImm();
171 case MachineOperand::MO_MachineBasicBlock:
172 printBasicBlockLabel(MO.getMBB());
174 case MachineOperand::MO_GlobalAddress:
175 O << Mang->getMangledName(MO.getGlobal());
177 case MachineOperand::MO_ExternalSymbol:
178 O << MO.getSymbolName();
180 case MachineOperand::MO_ConstantPoolIndex:
181 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
185 llvm_unreachable("<unknown operand type>");
187 if (CloseParen) O << ")";
190 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
191 const char *Modifier) {
192 printOperand(MI, opNum);
194 // If this is an ADD operand, emit it like normal operands.
195 if (Modifier && !strcmp(Modifier, "arith")) {
197 printOperand(MI, opNum+1);
201 if (MI->getOperand(opNum+1).isReg() &&
202 MI->getOperand(opNum+1).getReg() == SP::G0)
203 return; // don't print "+%g0"
204 if (MI->getOperand(opNum+1).isImm() &&
205 MI->getOperand(opNum+1).getImm() == 0)
206 return; // don't print "+0"
209 if (MI->getOperand(opNum+1).isGlobal() ||
210 MI->getOperand(opNum+1).isCPI()) {
212 printOperand(MI, opNum+1);
215 printOperand(MI, opNum+1);
219 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
220 int CC = (int)MI->getOperand(opNum).getImm();
221 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
224 bool SparcAsmPrinter::doFinalization(Module &M) {
225 // Print out module-level global variables here.
226 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
228 printModuleLevelGV(I);
232 return AsmPrinter::doFinalization(M);
235 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
236 const TargetData *TD = TM.getTargetData();
238 if (!GVar->hasInitializer())
239 return; // 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(GVar))
246 std::string name = Mang->getMangledName(GVar);
247 Constant *C = GVar->getInitializer();
248 if (isa<MDNode>(C) || isa<MDString>(C))
250 unsigned Size = TD->getTypeAllocSize(C->getType());
251 unsigned Align = TD->getPreferredAlignment(GVar);
253 printVisibility(name, GVar->getVisibility());
255 SwitchToSection(TAI->SectionForGlobal(GVar));
257 if (C->isNullValue() && !GVar->hasSection()) {
258 if (!GVar->isThreadLocal() &&
259 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
260 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
262 if (GVar->hasLocalLinkage())
263 O << "\t.local " << name << '\n';
265 O << TAI->getCOMMDirective() << name << ',' << Size;
266 if (TAI->getCOMMDirectiveTakesAlignment())
267 O << ',' << (1 << Align);
274 switch (GVar->getLinkage()) {
275 case GlobalValue::CommonLinkage:
276 case GlobalValue::LinkOnceAnyLinkage:
277 case GlobalValue::LinkOnceODRLinkage:
278 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
279 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
280 // Nonnull linkonce -> weak
281 O << "\t.weak " << name << '\n';
283 case GlobalValue::AppendingLinkage:
284 // FIXME: appending linkage variables should go into a section of
285 // their name or something. For now, just emit them as external.
286 case GlobalValue::ExternalLinkage:
287 // If external or appending, declare as a global symbol
288 O << TAI->getGlobalDirective() << name << '\n';
290 case GlobalValue::PrivateLinkage:
291 case GlobalValue::LinkerPrivateLinkage:
292 case GlobalValue::InternalLinkage:
294 case GlobalValue::GhostLinkage:
295 llvm_unreachable("Should not have any unmaterialized functions!");
296 case GlobalValue::DLLImportLinkage:
297 llvm_unreachable("DLLImport linkage is not supported by this target!");
298 case GlobalValue::DLLExportLinkage:
299 llvm_unreachable("DLLExport linkage is not supported by this target!");
301 llvm_unreachable("Unknown linkage type!");
304 EmitAlignment(Align, GVar);
306 if (TAI->hasDotTypeDotSizeDirective()) {
307 O << "\t.type " << name << ",#object\n";
308 O << "\t.size " << name << ',' << Size << '\n';
312 EmitGlobalConstant(C);
315 /// PrintAsmOperand - Print out an operand for an inline asm expression.
317 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
319 const char *ExtraCode) {
320 if (ExtraCode && ExtraCode[0]) {
321 if (ExtraCode[1] != 0) return true; // Unknown modifier.
323 switch (ExtraCode[0]) {
324 default: return true; // Unknown modifier.
330 printOperand(MI, OpNo);
335 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
338 const char *ExtraCode) {
339 if (ExtraCode && ExtraCode[0])
340 return true; // Unknown modifier
343 printMemOperand(MI, OpNo);
349 // Force static initialization.
350 extern "C" void LLVMInitializeSparcAsmPrinter() {
351 TargetRegistry::RegisterAsmPrinter(TheSparcTarget,
352 createSparcCodePrinterPass);