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 doInitialization(Module &M);
71 bool doFinalization(Module &M);
72 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
74 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
75 unsigned AsmVariant, const char *ExtraCode);
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(formatted_raw_ostream &o,
89 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
93 /// runOnMachineFunction - This uses the printInstruction()
94 /// method to print assembly for each instruction.
96 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
99 SetupMachineFunction(MF);
101 // Print out constants referenced by the function
102 EmitConstantPool(MF.getConstantPool());
104 // BBNumber is used here so that a given Printer will never give two
105 // BBs the same name. (If you have a better way, please let me know!)
109 // Print out the label for the function.
110 const Function *F = MF.getFunction();
111 SwitchToSection(TAI->SectionForGlobal(F));
112 EmitAlignment(MF.getAlignment(), F);
113 O << "\t.globl\t" << CurrentFnName << '\n';
115 printVisibility(CurrentFnName, F->getVisibility());
117 O << "\t.type\t" << CurrentFnName << ", #function\n";
118 O << CurrentFnName << ":\n";
120 // Number each basic block so that we can consistently refer to them
121 // in PC-relative references.
122 // FIXME: Why not use the MBB numbers?
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 NumberForBB[I->getBasicBlock()] = BBNumber++;
129 // Print out code for the function.
130 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
132 // Print a label for the basic block.
133 if (I != MF.begin()) {
134 printBasicBlockLabel(I, true, true);
137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 // Print the assembly for the instruction.
140 printInstruction(II);
145 // We didn't modify anything.
149 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
150 const MachineOperand &MO = MI->getOperand (opNum);
151 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
152 bool CloseParen = false;
153 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
156 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
157 !MO.isReg() && !MO.isImm()) {
161 switch (MO.getType()) {
162 case MachineOperand::MO_Register:
163 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
164 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
166 O << "%reg" << MO.getReg();
169 case MachineOperand::MO_Immediate:
170 O << (int)MO.getImm();
172 case MachineOperand::MO_MachineBasicBlock:
173 printBasicBlockLabel(MO.getMBB());
175 case MachineOperand::MO_GlobalAddress:
176 O << Mang->getMangledName(MO.getGlobal());
178 case MachineOperand::MO_ExternalSymbol:
179 O << MO.getSymbolName();
181 case MachineOperand::MO_ConstantPoolIndex:
182 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
186 llvm_unreachable("<unknown operand type>");
188 if (CloseParen) O << ")";
191 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
192 const char *Modifier) {
193 printOperand(MI, opNum);
195 // If this is an ADD operand, emit it like normal operands.
196 if (Modifier && !strcmp(Modifier, "arith")) {
198 printOperand(MI, opNum+1);
202 if (MI->getOperand(opNum+1).isReg() &&
203 MI->getOperand(opNum+1).getReg() == SP::G0)
204 return; // don't print "+%g0"
205 if (MI->getOperand(opNum+1).isImm() &&
206 MI->getOperand(opNum+1).getImm() == 0)
207 return; // don't print "+0"
210 if (MI->getOperand(opNum+1).isGlobal() ||
211 MI->getOperand(opNum+1).isCPI()) {
213 printOperand(MI, opNum+1);
216 printOperand(MI, opNum+1);
220 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
221 int CC = (int)MI->getOperand(opNum).getImm();
222 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
225 bool SparcAsmPrinter::doInitialization(Module &M) {
226 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix(),
227 TAI->getLessPrivateGlobalPrefix());
228 return false; // success
231 bool SparcAsmPrinter::doFinalization(Module &M) {
232 // Print out module-level global variables here.
233 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
235 printModuleLevelGV(I);
239 return AsmPrinter::doFinalization(M);
242 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
243 const TargetData *TD = TM.getTargetData();
245 if (!GVar->hasInitializer())
246 return; // External global require no code
248 // Check to see if this is a special global used by LLVM, if so, emit it.
249 if (EmitSpecialLLVMGlobal(GVar))
253 std::string name = Mang->getMangledName(GVar);
254 Constant *C = GVar->getInitializer();
255 if (isa<MDNode>(C) || isa<MDString>(C))
257 unsigned Size = TD->getTypeAllocSize(C->getType());
258 unsigned Align = TD->getPreferredAlignment(GVar);
260 printVisibility(name, GVar->getVisibility());
262 SwitchToSection(TAI->SectionForGlobal(GVar));
264 if (C->isNullValue() && !GVar->hasSection()) {
265 if (!GVar->isThreadLocal() &&
266 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
267 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
269 if (GVar->hasLocalLinkage())
270 O << "\t.local " << name << '\n';
272 O << TAI->getCOMMDirective() << name << ',' << Size;
273 if (TAI->getCOMMDirectiveTakesAlignment())
274 O << ',' << (1 << Align);
281 switch (GVar->getLinkage()) {
282 case GlobalValue::CommonLinkage:
283 case GlobalValue::LinkOnceAnyLinkage:
284 case GlobalValue::LinkOnceODRLinkage:
285 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
286 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
287 // Nonnull linkonce -> weak
288 O << "\t.weak " << name << '\n';
290 case GlobalValue::AppendingLinkage:
291 // FIXME: appending linkage variables should go into a section of
292 // their name or something. For now, just emit them as external.
293 case GlobalValue::ExternalLinkage:
294 // If external or appending, declare as a global symbol
295 O << TAI->getGlobalDirective() << name << '\n';
297 case GlobalValue::PrivateLinkage:
298 case GlobalValue::LinkerPrivateLinkage:
299 case GlobalValue::InternalLinkage:
301 case GlobalValue::GhostLinkage:
302 llvm_unreachable("Should not have any unmaterialized functions!");
303 case GlobalValue::DLLImportLinkage:
304 llvm_unreachable("DLLImport linkage is not supported by this target!");
305 case GlobalValue::DLLExportLinkage:
306 llvm_unreachable("DLLExport linkage is not supported by this target!");
308 llvm_unreachable("Unknown linkage type!");
311 EmitAlignment(Align, GVar);
313 if (TAI->hasDotTypeDotSizeDirective()) {
314 O << "\t.type " << name << ",#object\n";
315 O << "\t.size " << name << ',' << Size << '\n';
319 EmitGlobalConstant(C);
322 /// PrintAsmOperand - Print out an operand for an inline asm expression.
324 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
326 const char *ExtraCode) {
327 if (ExtraCode && ExtraCode[0]) {
328 if (ExtraCode[1] != 0) return true; // Unknown modifier.
330 switch (ExtraCode[0]) {
331 default: return true; // Unknown modifier.
337 printOperand(MI, OpNo);
342 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
345 const char *ExtraCode) {
346 if (ExtraCode && ExtraCode[0])
347 return true; // Unknown modifier
350 printMemOperand(MI, OpNo);
356 // Force static initialization.
357 extern "C" void LLVMInitializeSparcAsmPrinter() {
358 TargetRegistry::RegisterAsmPrinter(TheSparcTarget,
359 createSparcCodePrinterPass);