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/Support/Mangler.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/MathExtras.h"
40 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
44 /// We name each basic block in a Function with a unique number, so
45 /// that we can consistently refer to them later. This is cleared
46 /// at the beginning of each call to runOnMachineFunction().
48 typedef std::map<const Value *, unsigned> ValueMapTy;
49 ValueMapTy NumberForBB;
51 explicit SparcAsmPrinter(raw_ostream &O, TargetMachine &TM,
52 const TargetAsmInfo *T, CodeGenOpt::Level OL,
54 : AsmPrinter(O, TM, T, OL, V) {}
56 virtual const char *getPassName() const {
57 return "Sparc Assembly Printer";
60 void printModuleLevelGV(const GlobalVariable* GVar);
61 void printOperand(const MachineInstr *MI, int opNum);
62 void printMemOperand(const MachineInstr *MI, int opNum,
63 const char *Modifier = 0);
64 void printCCOperand(const MachineInstr *MI, int opNum);
66 bool printInstruction(const MachineInstr *MI); // autogenerated.
67 bool runOnMachineFunction(MachineFunction &F);
68 bool doInitialization(Module &M);
69 bool doFinalization(Module &M);
70 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
71 unsigned AsmVariant, const char *ExtraCode);
72 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
73 unsigned AsmVariant, const char *ExtraCode);
75 } // end of anonymous namespace
77 #include "SparcGenAsmWriter.inc"
79 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
80 /// assembly code for a MachineFunction to the given output stream,
81 /// using the given target machine description. This should work
82 /// regardless of whether the function is in SSA form.
84 FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
86 CodeGenOpt::Level OptLevel,
88 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, 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!)
105 static unsigned BBNumber = 0;
109 // Print out the label for the function.
110 const Function *F = MF.getFunction();
111 SwitchToSection(TAI->SectionForGlobal(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:
177 const GlobalValue *GV = MO.getGlobal();
178 O << Mang->getValueName(GV);
181 case MachineOperand::MO_ExternalSymbol:
182 O << MO.getSymbolName();
184 case MachineOperand::MO_ConstantPoolIndex:
185 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
189 O << "<unknown operand type>"; abort (); break;
191 if (CloseParen) O << ")";
194 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
195 const char *Modifier) {
196 printOperand(MI, opNum);
198 // If this is an ADD operand, emit it like normal operands.
199 if (Modifier && !strcmp(Modifier, "arith")) {
201 printOperand(MI, opNum+1);
205 if (MI->getOperand(opNum+1).isReg() &&
206 MI->getOperand(opNum+1).getReg() == SP::G0)
207 return; // don't print "+%g0"
208 if (MI->getOperand(opNum+1).isImm() &&
209 MI->getOperand(opNum+1).getImm() == 0)
210 return; // don't print "+0"
213 if (MI->getOperand(opNum+1).isGlobal() ||
214 MI->getOperand(opNum+1).isCPI()) {
216 printOperand(MI, opNum+1);
219 printOperand(MI, opNum+1);
223 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
224 int CC = (int)MI->getOperand(opNum).getImm();
225 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
228 bool SparcAsmPrinter::doInitialization(Module &M) {
229 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
230 return false; // success
233 bool SparcAsmPrinter::doFinalization(Module &M) {
234 // Print out module-level global variables here.
235 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
237 printModuleLevelGV(I);
241 return AsmPrinter::doFinalization(M);
244 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
245 const TargetData *TD = TM.getTargetData();
247 if (!GVar->hasInitializer())
248 return; // External global require no code
250 // Check to see if this is a special global used by LLVM, if so, emit it.
251 if (EmitSpecialLLVMGlobal(GVar))
255 std::string name = Mang->getValueName(GVar);
256 Constant *C = GVar->getInitializer();
257 if (isa<MDNode>(C) || isa<MDString>(C))
259 unsigned Size = TD->getTypeAllocSize(C->getType());
260 unsigned Align = TD->getPreferredAlignment(GVar);
262 printVisibility(name, GVar->getVisibility());
264 SwitchToSection(TAI->SectionForGlobal(GVar));
266 if (C->isNullValue() && !GVar->hasSection()) {
267 if (!GVar->isThreadLocal() &&
268 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
269 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
271 if (GVar->hasLocalLinkage())
272 O << "\t.local " << name << '\n';
274 O << TAI->getCOMMDirective() << name << ',' << Size;
275 if (TAI->getCOMMDirectiveTakesAlignment())
276 O << ',' << (1 << Align);
283 switch (GVar->getLinkage()) {
284 case GlobalValue::CommonLinkage:
285 case GlobalValue::LinkOnceAnyLinkage:
286 case GlobalValue::LinkOnceODRLinkage:
287 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak.
288 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak.
289 // Nonnull linkonce -> weak
290 O << "\t.weak " << name << '\n';
292 case GlobalValue::AppendingLinkage:
293 // FIXME: appending linkage variables should go into a section of
294 // their name or something. For now, just emit them as external.
295 case GlobalValue::ExternalLinkage:
296 // If external or appending, declare as a global symbol
297 O << TAI->getGlobalDirective() << name << '\n';
299 case GlobalValue::PrivateLinkage:
300 case GlobalValue::InternalLinkage:
302 case GlobalValue::GhostLinkage:
303 cerr << "Should not have any unmaterialized functions!\n";
305 case GlobalValue::DLLImportLinkage:
306 cerr << "DLLImport linkage is not supported by this target!\n";
308 case GlobalValue::DLLExportLinkage:
309 cerr << "DLLExport linkage is not supported by this target!\n";
312 assert(0 && "Unknown linkage type!");
315 EmitAlignment(Align, GVar);
317 if (TAI->hasDotTypeDotSizeDirective()) {
318 O << "\t.type " << name << ",#object\n";
319 O << "\t.size " << name << ',' << Size << '\n';
323 EmitGlobalConstant(C);
326 /// PrintAsmOperand - Print out an operand for an inline asm expression.
328 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
330 const char *ExtraCode) {
331 if (ExtraCode && ExtraCode[0]) {
332 if (ExtraCode[1] != 0) return true; // Unknown modifier.
334 switch (ExtraCode[0]) {
335 default: return true; // Unknown modifier.
341 printOperand(MI, OpNo);
346 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
349 const char *ExtraCode) {
350 if (ExtraCode && ExtraCode[0])
351 return true; // Unknown modifier
354 printMemOperand(MI, OpNo);
361 static struct Register {
363 SparcTargetMachine::registerAsmPrinter(createSparcCodePrinterPass);
368 // Force static initialization.
369 extern "C" void LLVMInitializeSparcAsmPrinter() { }