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);
69 } // end of anonymous namespace
71 #include "SparcGenAsmWriter.inc"
73 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
74 /// assembly code for a MachineFunction to the given output stream,
75 /// using the given target machine description. This should work
76 /// regardless of whether the function is in SSA form.
78 FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
80 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
83 /// runOnMachineFunction - This uses the printInstruction()
84 /// method to print assembly for each instruction.
86 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!)
94 static unsigned BBNumber = 0;
97 // What's my mangled name?
98 CurrentFnName = Mang->getValueName(MF.getFunction());
100 // Print out the label for the function.
101 const Function *F = MF.getFunction();
102 SwitchToSection(TAI->SectionForGlobal(F));
104 O << "\t.globl\t" << CurrentFnName << '\n';
106 printVisibility(CurrentFnName, F->getVisibility());
108 O << "\t.type\t" << CurrentFnName << ", #function\n";
109 O << CurrentFnName << ":\n";
111 // Number each basic block so that we can consistently refer to them
112 // in PC-relative references.
113 // FIXME: Why not use the MBB numbers?
115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
117 NumberForBB[I->getBasicBlock()] = BBNumber++;
120 // Print out code for the function.
121 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
123 // Print a label for the basic block.
124 if (I != MF.begin()) {
125 printBasicBlockLabel(I, true, true);
128 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
130 // Print the assembly for the instruction.
131 printInstruction(II);
136 // We didn't modify anything.
140 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
141 const MachineOperand &MO = MI->getOperand (opNum);
142 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
143 bool CloseParen = false;
144 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
147 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
148 !MO.isReg() && !MO.isImm()) {
152 switch (MO.getType()) {
153 case MachineOperand::MO_Register:
154 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
155 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
157 O << "%reg" << MO.getReg();
160 case MachineOperand::MO_Immediate:
161 O << (int)MO.getImm();
163 case MachineOperand::MO_MachineBasicBlock:
164 printBasicBlockLabel(MO.getMBB());
166 case MachineOperand::MO_GlobalAddress:
167 O << Mang->getValueName(MO.getGlobal());
169 case MachineOperand::MO_ExternalSymbol:
170 O << MO.getSymbolName();
172 case MachineOperand::MO_ConstantPoolIndex:
173 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
177 O << "<unknown operand type>"; abort (); break;
179 if (CloseParen) O << ")";
182 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
183 const char *Modifier) {
184 printOperand(MI, opNum);
186 // If this is an ADD operand, emit it like normal operands.
187 if (Modifier && !strcmp(Modifier, "arith")) {
189 printOperand(MI, opNum+1);
193 if (MI->getOperand(opNum+1).isReg() &&
194 MI->getOperand(opNum+1).getReg() == SP::G0)
195 return; // don't print "+%g0"
196 if (MI->getOperand(opNum+1).isImm() &&
197 MI->getOperand(opNum+1).getImm() == 0)
198 return; // don't print "+0"
201 if (MI->getOperand(opNum+1).isGlobal() ||
202 MI->getOperand(opNum+1).isCPI()) {
204 printOperand(MI, opNum+1);
207 printOperand(MI, opNum+1);
211 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
212 int CC = (int)MI->getOperand(opNum).getImm();
213 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
218 bool SparcAsmPrinter::doInitialization(Module &M) {
219 Mang = new Mangler(M);
220 return false; // success
223 bool SparcAsmPrinter::doFinalization(Module &M) {
224 // Print out module-level global variables here.
225 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
227 printModuleLevelGV(I);
231 return AsmPrinter::doFinalization(M);
234 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
235 const TargetData *TD = TM.getTargetData();
237 if (!GVar->hasInitializer())
238 return; // External global require no code
240 // Check to see if this is a special global used by LLVM, if so, emit it.
241 if (EmitSpecialLLVMGlobal(GVar))
245 std::string name = Mang->getValueName(GVar);
246 Constant *C = GVar->getInitializer();
247 unsigned Size = TD->getABITypeSize(C->getType());
248 unsigned Align = TD->getPreferredAlignment(GVar);
250 printVisibility(name, GVar->getVisibility());
252 SwitchToSection(TAI->SectionForGlobal(GVar));
254 if (C->isNullValue() && !GVar->hasSection()) {
255 if (!GVar->isThreadLocal() &&
256 (GVar->hasInternalLinkage() || GVar->mayBeOverridden())) {
257 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
259 if (GVar->hasInternalLinkage())
260 O << "\t.local " << name << '\n';
262 O << TAI->getCOMMDirective() << name << ',' << Size;
263 if (TAI->getCOMMDirectiveTakesAlignment())
264 O << ',' << (1 << Align);
271 switch (GVar->getLinkage()) {
272 case GlobalValue::CommonLinkage:
273 case GlobalValue::LinkOnceLinkage:
274 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
275 // Nonnull linkonce -> weak
276 O << "\t.weak " << name << '\n';
278 case GlobalValue::AppendingLinkage:
279 // FIXME: appending linkage variables should go into a section of
280 // their name or something. For now, just emit them as external.
281 case GlobalValue::ExternalLinkage:
282 // If external or appending, declare as a global symbol
283 O << TAI->getGlobalDirective() << name << '\n';
285 case GlobalValue::InternalLinkage:
287 case GlobalValue::GhostLinkage:
288 cerr << "Should not have any unmaterialized functions!\n";
290 case GlobalValue::DLLImportLinkage:
291 cerr << "DLLImport linkage is not supported by this target!\n";
293 case GlobalValue::DLLExportLinkage:
294 cerr << "DLLExport linkage is not supported by this target!\n";
297 assert(0 && "Unknown linkage type!");
300 EmitAlignment(Align, GVar);
302 if (TAI->hasDotTypeDotSizeDirective()) {
303 O << "\t.type " << name << ",#object\n";
304 O << "\t.size " << name << ',' << Size << '\n';
308 EmitGlobalConstant(C);