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/DwarfWriter.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineInstr.h"
26 #include "llvm/Target/TargetAsmInfo.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/Target/TargetMachine.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 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
44 SparcAsmPrinter(raw_ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
45 : AsmPrinter(O, TM, T) {
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
55 virtual const char *getPassName() const {
56 return "Sparc Assembly Printer";
59 void printModuleLevelGV(const GlobalVariable* GVar);
60 void printOperand(const MachineInstr *MI, int opNum);
61 void printMemOperand(const MachineInstr *MI, int opNum,
62 const char *Modifier = 0);
63 void printCCOperand(const MachineInstr *MI, int opNum);
65 bool printInstruction(const MachineInstr *MI); // autogenerated.
66 bool runOnMachineFunction(MachineFunction &F);
67 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
71 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
72 unsigned AsmVariant, const char *ExtraCode);
74 } // end of anonymous namespace
76 #include "SparcGenAsmWriter.inc"
78 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
79 /// assembly code for a MachineFunction to the given output stream,
80 /// using the given target machine description. This should work
81 /// regardless of whether the function is in SSA form.
83 FunctionPass *llvm::createSparcCodePrinterPass(raw_ostream &o,
85 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
88 /// runOnMachineFunction - This uses the printInstruction()
89 /// method to print assembly for each instruction.
91 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
92 SetupMachineFunction(MF);
94 // Print out constants referenced by the function
95 EmitConstantPool(MF.getConstantPool());
97 // BBNumber is used here so that a given Printer will never give two
98 // BBs the same name. (If you have a better way, please let me know!)
99 static unsigned BBNumber = 0;
103 // Print out the label for the function.
104 const Function *F = MF.getFunction();
105 SwitchToSection(TAI->SectionForGlobal(F));
107 O << "\t.globl\t" << CurrentFnName << '\n';
109 printVisibility(CurrentFnName, F->getVisibility());
111 O << "\t.type\t" << CurrentFnName << ", #function\n";
112 O << CurrentFnName << ":\n";
114 // Number each basic block so that we can consistently refer to them
115 // in PC-relative references.
116 // FIXME: Why not use the MBB numbers?
118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
120 NumberForBB[I->getBasicBlock()] = BBNumber++;
123 // Print out code for the function.
124 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
126 // Print a label for the basic block.
127 if (I != MF.begin()) {
128 printBasicBlockLabel(I, true, true);
131 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
133 // Print the assembly for the instruction.
134 printInstruction(II);
139 // We didn't modify anything.
143 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
144 const MachineOperand &MO = MI->getOperand (opNum);
145 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
146 bool CloseParen = false;
147 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) {
150 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) &&
151 !MO.isReg() && !MO.isImm()) {
155 switch (MO.getType()) {
156 case MachineOperand::MO_Register:
157 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
158 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName);
160 O << "%reg" << MO.getReg();
163 case MachineOperand::MO_Immediate:
164 O << (int)MO.getImm();
166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMBB());
169 case MachineOperand::MO_GlobalAddress:
171 const GlobalValue *GV = MO.getGlobal();
172 O << Mang->getValueName(GV);
175 case MachineOperand::MO_ExternalSymbol:
176 O << MO.getSymbolName();
178 case MachineOperand::MO_ConstantPoolIndex:
179 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
183 O << "<unknown operand type>"; abort (); break;
185 if (CloseParen) O << ")";
188 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
189 const char *Modifier) {
190 printOperand(MI, opNum);
192 // If this is an ADD operand, emit it like normal operands.
193 if (Modifier && !strcmp(Modifier, "arith")) {
195 printOperand(MI, opNum+1);
199 if (MI->getOperand(opNum+1).isReg() &&
200 MI->getOperand(opNum+1).getReg() == SP::G0)
201 return; // don't print "+%g0"
202 if (MI->getOperand(opNum+1).isImm() &&
203 MI->getOperand(opNum+1).getImm() == 0)
204 return; // don't print "+0"
207 if (MI->getOperand(opNum+1).isGlobal() ||
208 MI->getOperand(opNum+1).isCPI()) {
210 printOperand(MI, opNum+1);
213 printOperand(MI, opNum+1);
217 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
218 int CC = (int)MI->getOperand(opNum).getImm();
219 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
222 bool SparcAsmPrinter::doInitialization(Module &M) {
223 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
224 return false; // success
227 bool SparcAsmPrinter::doFinalization(Module &M) {
228 // Print out module-level global variables here.
229 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
231 printModuleLevelGV(I);
235 return AsmPrinter::doFinalization(M);
238 void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
239 const TargetData *TD = TM.getTargetData();
241 if (!GVar->hasInitializer())
242 return; // External global require no code
244 // Check to see if this is a special global used by LLVM, if so, emit it.
245 if (EmitSpecialLLVMGlobal(GVar))
249 std::string name = Mang->getValueName(GVar);
250 Constant *C = GVar->getInitializer();
251 unsigned Size = TD->getTypePaddedSize(C->getType());
252 unsigned Align = TD->getPreferredAlignment(GVar);
254 printVisibility(name, GVar->getVisibility());
256 SwitchToSection(TAI->SectionForGlobal(GVar));
258 if (C->isNullValue() && !GVar->hasSection()) {
259 if (!GVar->isThreadLocal() &&
260 (GVar->hasLocalLinkage() || GVar->mayBeOverridden())) {
261 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
263 if (GVar->hasLocalLinkage())
264 O << "\t.local " << name << '\n';
266 O << TAI->getCOMMDirective() << name << ',' << Size;
267 if (TAI->getCOMMDirectiveTakesAlignment())
268 O << ',' << (1 << Align);
275 switch (GVar->getLinkage()) {
276 case GlobalValue::CommonLinkage:
277 case GlobalValue::LinkOnceLinkage:
278 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
279 // Nonnull linkonce -> weak
280 O << "\t.weak " << name << '\n';
282 case GlobalValue::AppendingLinkage:
283 // FIXME: appending linkage variables should go into a section of
284 // their name or something. For now, just emit them as external.
285 case GlobalValue::ExternalLinkage:
286 // If external or appending, declare as a global symbol
287 O << TAI->getGlobalDirective() << name << '\n';
289 case GlobalValue::PrivateLinkage:
290 case GlobalValue::InternalLinkage:
292 case GlobalValue::GhostLinkage:
293 cerr << "Should not have any unmaterialized functions!\n";
295 case GlobalValue::DLLImportLinkage:
296 cerr << "DLLImport linkage is not supported by this target!\n";
298 case GlobalValue::DLLExportLinkage:
299 cerr << "DLLExport linkage is not supported by this target!\n";
302 assert(0 && "Unknown linkage type!");
305 EmitAlignment(Align, GVar);
307 if (TAI->hasDotTypeDotSizeDirective()) {
308 O << "\t.type " << name << ",#object\n";
309 O << "\t.size " << name << ',' << Size << '\n';
313 EmitGlobalConstant(C);
316 /// PrintAsmOperand - Print out an operand for an inline asm expression.
318 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
320 const char *ExtraCode) {
321 if (ExtraCode && ExtraCode[0]) {
322 if (ExtraCode[1] != 0) return true; // Unknown modifier.
324 switch (ExtraCode[0]) {
325 default: return true; // Unknown modifier.
331 printOperand(MI, OpNo);
336 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
339 const char *ExtraCode) {
340 if (ExtraCode && ExtraCode[0])
341 return true; // Unknown modifier
344 printMemOperand(MI, OpNo);