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/Support/raw_ostream.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/ADT/StringExtras.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/MathExtras.h"
39 STATISTIC(EmittedInsts, "Number of machine instrs printed");
42 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
43 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
44 : AsmPrinter(O, TM, T) {
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;
54 virtual const char *getPassName() const {
55 return "Sparc Assembly Printer";
58 void printModuleLevelGV(const GlobalVariable* GVar);
59 void printOperand(const MachineInstr *MI, int opNum);
60 void printMemOperand(const MachineInstr *MI, int opNum,
61 const char *Modifier = 0);
62 void printCCOperand(const MachineInstr *MI, int opNum);
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 bool runOnMachineFunction(MachineFunction &F);
66 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
68 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69 unsigned AsmVariant, const char *ExtraCode);
70 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
71 unsigned AsmVariant, const char *ExtraCode);
73 } // end of anonymous namespace
75 #include "SparcGenAsmWriter.inc"
77 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
78 /// assembly code for a MachineFunction to the given output stream,
79 /// using the given target machine description. This should work
80 /// regardless of whether the function is in SSA form.
82 FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
84 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
87 /// runOnMachineFunction - This uses the printInstruction()
88 /// method to print assembly for each instruction.
90 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
91 SetupMachineFunction(MF);
93 // Print out constants referenced by the function
94 EmitConstantPool(MF.getConstantPool());
96 // BBNumber is used here so that a given Printer will never give two
97 // BBs the same name. (If you have a better way, please let me know!)
98 static unsigned BBNumber = 0;
101 // What's my mangled name?
102 CurrentFnName = Mang->getValueName(MF.getFunction());
104 // Print out the label for the function.
105 const Function *F = MF.getFunction();
106 SwitchToSection(TAI->SectionForGlobal(F));
108 O << "\t.globl\t" << CurrentFnName << '\n';
110 printVisibility(CurrentFnName, F->getVisibility());
112 O << "\t.type\t" << CurrentFnName << ", #function\n";
113 O << CurrentFnName << ":\n";
115 // Number each basic block so that we can consistently refer to them
116 // in PC-relative references.
117 // FIXME: Why not use the MBB numbers?
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, true);
132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
134 // Print the assembly for the instruction.
135 printInstruction(II);
140 // We didn't modify anything.
144 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
145 const MachineOperand &MO = MI->getOperand (opNum);
146 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
147 bool CloseParen = false;
148 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
151 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
152 !MO.isReg() && !MO.isImm()) {
156 switch (MO.getType()) {
157 case MachineOperand::MO_Register:
158 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
159 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
161 O << "%reg" << MO.getReg();
164 case MachineOperand::MO_Immediate:
165 O << (int)MO.getImm();
167 case MachineOperand::MO_MachineBasicBlock:
168 printBasicBlockLabel(MO.getMBB());
170 case MachineOperand::MO_GlobalAddress:
171 O << Mang->getValueName(MO.getGlobal());
173 case MachineOperand::MO_ExternalSymbol:
174 O << MO.getSymbolName();
176 case MachineOperand::MO_ConstantPoolIndex:
177 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
181 O << "<unknown operand type>"; abort (); break;
183 if (CloseParen) O << ")";
186 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
187 const char *Modifier) {
188 printOperand(MI, opNum);
190 // If this is an ADD operand, emit it like normal operands.
191 if (Modifier && !strcmp(Modifier, "arith")) {
193 printOperand(MI, opNum+1);
197 if (MI->getOperand(opNum+1).isReg() &&
198 MI->getOperand(opNum+1).getReg() == SP::G0)
199 return; // don't print "+%g0"
200 if (MI->getOperand(opNum+1).isImm() &&
201 MI->getOperand(opNum+1).getImm() == 0)
202 return; // don't print "+0"
205 if (MI->getOperand(opNum+1).isGlobal() ||
206 MI->getOperand(opNum+1).isCPI()) {
208 printOperand(MI, opNum+1);
211 printOperand(MI, opNum+1);
215 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
216 int CC = (int)MI->getOperand(opNum).getImm();
217 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
220 bool SparcAsmPrinter::doInitialization(Module &M) {
221 Mang = new Mangler(M);
222 return false; // success
225 bool SparcAsmPrinter::doFinalization(Module &M) {
226 // Print out module-level global variables here.
227 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
229 printModuleLevelGV(I);
233 return AsmPrinter::doFinalization(M);
236 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
237 const TargetData *TD = TM.getTargetData();
239 if (!GVar->hasInitializer())
240 return; // External global require no code
242 // Check to see if this is a special global used by LLVM, if so, emit it.
243 if (EmitSpecialLLVMGlobal(GVar))
247 std::string name = Mang->getValueName(GVar);
248 Constant *C = GVar->getInitializer();
249 unsigned Size = TD->getABITypeSize(C->getType());
250 unsigned Align = TD->getPreferredAlignment(GVar);
252 printVisibility(name, GVar->getVisibility());
254 SwitchToSection(TAI->SectionForGlobal(GVar));
256 if (C->isNullValue() && !GVar->hasSection()) {
257 if (!GVar->isThreadLocal() &&
258 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
259 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
261 if (GVar->hasInternalLinkage())
262 O << "\t.local " << name << '\n';
264 O << TAI->getCOMMDirective() << name << ',' << Size;
265 if (TAI->getCOMMDirectiveTakesAlignment())
266 O << ',' << (1 << Align);
273 switch (GVar->getLinkage()) {
274 case GlobalValue::CommonLinkage:
275 case GlobalValue::LinkOnceLinkage:
276 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
277 // Nonnull linkonce -> weak
278 O << "\t.weak " << name << '\n';
280 case GlobalValue::AppendingLinkage:
281 // FIXME: appending linkage variables should go into a section of
282 // their name or something. For now, just emit them as external.
283 case GlobalValue::ExternalLinkage:
284 // If external or appending, declare as a global symbol
285 O << TAI->getGlobalDirective() << name << '\n';
287 case GlobalValue::InternalLinkage:
289 case GlobalValue::GhostLinkage:
290 cerr << "Should not have any unmaterialized functions!\n";
292 case GlobalValue::DLLImportLinkage:
293 cerr << "DLLImport linkage is not supported by this target!\n";
295 case GlobalValue::DLLExportLinkage:
296 cerr << "DLLExport linkage is not supported by this target!\n";
299 assert(0 && "Unknown linkage type!");
302 EmitAlignment(Align, GVar);
304 if (TAI->hasDotTypeDotSizeDirective()) {
305 O << "\t.type " << name << ",#object\n";
306 O << "\t.size " << name << ',' << Size << '\n';
310 EmitGlobalConstant(C);
313 /// PrintAsmOperand - Print out an operand for an inline asm expression.
315 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
317 const char *ExtraCode) {
318 if (ExtraCode && ExtraCode[0])
319 return true; // Unknown modifier
321 printOperand(MI, OpNo);
326 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
329 const char *ExtraCode) {
330 if (ExtraCode && ExtraCode[0])
331 return true; // Unknown modifier
334 printMemOperand(MI, OpNo);