1 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/ADT/Statistic.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/MathExtras.h"
36 STATISTIC(EmittedInsts, "Number of machine instrs printed");
39 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter {
40 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T)
41 : AsmPrinter(O, TM, T) {
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 virtual const char *getPassName() const {
52 return "Sparc Assembly Printer";
55 void printOperand(const MachineInstr *MI, int opNum);
56 void printMemOperand(const MachineInstr *MI, int opNum,
57 const char *Modifier = 0);
58 void printCCOperand(const MachineInstr *MI, int opNum);
60 bool printInstruction(const MachineInstr *MI); // autogenerated.
61 bool runOnMachineFunction(MachineFunction &F);
62 bool doInitialization(Module &M);
63 bool doFinalization(Module &M);
65 } // end of anonymous namespace
67 #include "SparcGenAsmWriter.inc"
69 /// createSparcCodePrinterPass - Returns a pass that prints the SPARC
70 /// assembly code for a MachineFunction to the given output stream,
71 /// using the given target machine description. This should work
72 /// regardless of whether the function is in SSA form.
74 FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o,
76 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo());
79 /// runOnMachineFunction - This uses the printMachineInstruction()
80 /// method to print assembly for each instruction.
82 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
83 SetupMachineFunction(MF);
85 // Print out constants referenced by the function
86 EmitConstantPool(MF.getConstantPool());
88 // BBNumber is used here so that a given Printer will never give two
89 // BBs the same name. (If you have a better way, please let me know!)
90 static unsigned BBNumber = 0;
93 // What's my mangled name?
94 CurrentFnName = Mang->getValueName(MF.getFunction());
96 // Print out the label for the function.
97 const Function *F = MF.getFunction();
98 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
100 O << "\t.globl\t" << CurrentFnName << "\n";
101 O << "\t.type\t" << CurrentFnName << ", #function\n";
102 O << CurrentFnName << ":\n";
104 // Number each basic block so that we can consistently refer to them
105 // in PC-relative references.
106 // FIXME: Why not use the MBB numbers?
108 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
110 NumberForBB[I->getBasicBlock()] = BBNumber++;
113 // Print out code for the function.
114 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
116 // Print a label for the basic block.
117 if (I != MF.begin()) {
118 printBasicBlockLabel(I, true);
121 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
123 // Print the assembly for the instruction.
125 printInstruction(II);
130 // We didn't modify anything.
134 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
135 const MachineOperand &MO = MI->getOperand (opNum);
136 const MRegisterInfo &RI = *TM.getRegisterInfo();
137 bool CloseParen = false;
138 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
141 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri)
142 && !MO.isRegister() && !MO.isImmediate()) {
146 switch (MO.getType()) {
147 case MachineOperand::MO_Register:
148 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
149 O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
151 O << "%reg" << MO.getReg();
154 case MachineOperand::MO_Immediate:
155 O << (int)MO.getImmedValue();
157 case MachineOperand::MO_MachineBasicBlock:
158 printBasicBlockLabel(MO.getMachineBasicBlock());
160 case MachineOperand::MO_GlobalAddress:
161 O << Mang->getValueName(MO.getGlobal());
163 case MachineOperand::MO_ExternalSymbol:
164 O << MO.getSymbolName();
166 case MachineOperand::MO_ConstantPoolIndex:
167 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
168 << MO.getConstantPoolIndex();
171 O << "<unknown operand type>"; abort (); break;
173 if (CloseParen) O << ")";
176 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
177 const char *Modifier) {
178 printOperand(MI, opNum);
180 // If this is an ADD operand, emit it like normal operands.
181 if (Modifier && !strcmp(Modifier, "arith")) {
183 printOperand(MI, opNum+1);
187 if (MI->getOperand(opNum+1).isRegister() &&
188 MI->getOperand(opNum+1).getReg() == SP::G0)
189 return; // don't print "+%g0"
190 if (MI->getOperand(opNum+1).isImmediate() &&
191 MI->getOperand(opNum+1).getImmedValue() == 0)
192 return; // don't print "+0"
195 if (MI->getOperand(opNum+1).isGlobalAddress() ||
196 MI->getOperand(opNum+1).isConstantPoolIndex()) {
198 printOperand(MI, opNum+1);
201 printOperand(MI, opNum+1);
205 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) {
206 int CC = (int)MI->getOperand(opNum).getImmedValue();
207 O << SPARCCondCodeToString((SPCC::CondCodes)CC);
212 bool SparcAsmPrinter::doInitialization(Module &M) {
213 Mang = new Mangler(M);
214 return false; // success
217 bool SparcAsmPrinter::doFinalization(Module &M) {
218 const TargetData *TD = TM.getTargetData();
220 // Print out module-level global variables here.
221 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
223 if (I->hasInitializer()) { // External global require no code
224 // Check to see if this is a special global used by LLVM, if so, emit it.
225 if (EmitSpecialLLVMGlobal(I))
229 std::string name = Mang->getValueName(I);
230 Constant *C = I->getInitializer();
231 unsigned Size = TD->getTypeSize(C->getType());
232 unsigned Align = TD->getPrefTypeAlignment(C->getType());
234 if (C->isNullValue() &&
235 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
236 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
237 SwitchToDataSection(".data", I);
238 if (I->hasInternalLinkage())
239 O << "\t.local " << name << "\n";
241 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
245 switch (I->getLinkage()) {
246 case GlobalValue::LinkOnceLinkage:
247 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
248 // Nonnull linkonce -> weak
249 O << "\t.weak " << name << "\n";
250 SwitchToDataSection("", I);
251 O << "\t.section\t\".llvm.linkonce.d." << name
252 << "\",\"aw\",@progbits\n";
255 case GlobalValue::AppendingLinkage:
256 // FIXME: appending linkage variables should go into a section of
257 // their name or something. For now, just emit them as external.
258 case GlobalValue::ExternalLinkage:
259 // If external or appending, declare as a global symbol
260 O << "\t.globl " << name << "\n";
262 case GlobalValue::InternalLinkage:
263 if (C->isNullValue())
264 SwitchToDataSection(".bss", I);
266 SwitchToDataSection(".data", I);
268 case GlobalValue::GhostLinkage:
269 cerr << "Should not have any unmaterialized functions!\n";
271 case GlobalValue::DLLImportLinkage:
272 cerr << "DLLImport linkage is not supported by this target!\n";
274 case GlobalValue::DLLExportLinkage:
275 cerr << "DLLExport linkage is not supported by this target!\n";
278 assert(0 && "Unknown linkage type!");
281 O << "\t.align " << Align << "\n";
282 O << "\t.type " << name << ",#object\n";
283 O << "\t.size " << name << "," << Size << "\n";
285 EmitGlobalConstant(C);
289 AsmPrinter::doFinalization(M);
290 return false; // success