LLVM API Documentation
00001 //===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains a printer that converts from our internal representation 00011 // of machine-dependent LLVM code to GAS-format SPARC assembly language. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "asm-printer" 00016 #include "Sparc.h" 00017 #include "SparcInstrInfo.h" 00018 #include "SparcTargetMachine.h" 00019 #include "llvm/Constants.h" 00020 #include "llvm/DerivedTypes.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/CodeGen/AsmPrinter.h" 00023 #include "llvm/CodeGen/DwarfWriter.h" 00024 #include "llvm/CodeGen/MachineFunctionPass.h" 00025 #include "llvm/CodeGen/MachineConstantPool.h" 00026 #include "llvm/CodeGen/MachineInstr.h" 00027 #include "llvm/MC/MCStreamer.h" 00028 #include "llvm/Target/TargetAsmInfo.h" 00029 #include "llvm/Target/TargetLoweringObjectFile.h" 00030 #include "llvm/Target/TargetRegistry.h" 00031 #include "llvm/ADT/Statistic.h" 00032 #include "llvm/ADT/StringExtras.h" 00033 #include "llvm/Support/CommandLine.h" 00034 #include "llvm/Support/ErrorHandling.h" 00035 #include "llvm/Support/FormattedStream.h" 00036 #include "llvm/Support/Mangler.h" 00037 #include "llvm/Support/MathExtras.h" 00038 #include <cctype> 00039 #include <cstring> 00040 #include <map> 00041 using namespace llvm; 00042 00043 STATISTIC(EmittedInsts, "Number of machine instrs printed"); 00044 00045 namespace { 00046 class VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter { 00047 /// We name each basic block in a Function with a unique number, so 00048 /// that we can consistently refer to them later. This is cleared 00049 /// at the beginning of each call to runOnMachineFunction(). 00050 /// 00051 typedef std::map<const Value *, unsigned> ValueMapTy; 00052 ValueMapTy NumberForBB; 00053 unsigned BBNumber; 00054 public: 00055 explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, 00056 const TargetAsmInfo *T, bool V) 00057 : AsmPrinter(O, TM, T, V), BBNumber(0) {} 00058 00059 virtual const char *getPassName() const { 00060 return "Sparc Assembly Printer"; 00061 } 00062 00063 void PrintGlobalVariable(const GlobalVariable *GVar); 00064 void printOperand(const MachineInstr *MI, int opNum); 00065 void printMemOperand(const MachineInstr *MI, int opNum, 00066 const char *Modifier = 0); 00067 void printCCOperand(const MachineInstr *MI, int opNum); 00068 00069 void printInstruction(const MachineInstr *MI); // autogenerated. 00070 bool runOnMachineFunction(MachineFunction &F); 00071 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00072 unsigned AsmVariant, const char *ExtraCode); 00073 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo, 00074 unsigned AsmVariant, const char *ExtraCode); 00075 }; 00076 } // end of anonymous namespace 00077 00078 #include "SparcGenAsmWriter.inc" 00079 00080 00081 /// runOnMachineFunction - This uses the printInstruction() 00082 /// method to print assembly for each instruction. 00083 /// 00084 bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 00085 this->MF = &MF; 00086 00087 SetupMachineFunction(MF); 00088 00089 // Print out constants referenced by the function 00090 EmitConstantPool(MF.getConstantPool()); 00091 00092 // BBNumber is used here so that a given Printer will never give two 00093 // BBs the same name. (If you have a better way, please let me know!) 00094 00095 O << "\n\n"; 00096 00097 // Print out the label for the function. 00098 const Function *F = MF.getFunction(); 00099 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM)); 00100 EmitAlignment(MF.getAlignment(), F); 00101 O << "\t.globl\t" << CurrentFnName << '\n'; 00102 00103 printVisibility(CurrentFnName, F->getVisibility()); 00104 00105 O << "\t.type\t" << CurrentFnName << ", #function\n"; 00106 O << CurrentFnName << ":\n"; 00107 00108 // Number each basic block so that we can consistently refer to them 00109 // in PC-relative references. 00110 // FIXME: Why not use the MBB numbers? 00111 NumberForBB.clear(); 00112 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00113 I != E; ++I) { 00114 NumberForBB[I->getBasicBlock()] = BBNumber++; 00115 } 00116 00117 // Print out code for the function. 00118 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 00119 I != E; ++I) { 00120 // Print a label for the basic block. 00121 if (I != MF.begin()) { 00122 printBasicBlockLabel(I, true, true); 00123 O << '\n'; 00124 } 00125 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 00126 II != E; ++II) { 00127 // Print the assembly for the instruction. 00128 printInstruction(II); 00129 ++EmittedInsts; 00130 } 00131 } 00132 00133 // We didn't modify anything. 00134 return false; 00135 } 00136 00137 void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 00138 const MachineOperand &MO = MI->getOperand (opNum); 00139 const TargetRegisterInfo &RI = *TM.getRegisterInfo(); 00140 bool CloseParen = false; 00141 if (MI->getOpcode() == SP::SETHIi && !MO.isReg() && !MO.isImm()) { 00142 O << "%hi("; 00143 CloseParen = true; 00144 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) && 00145 !MO.isReg() && !MO.isImm()) { 00146 O << "%lo("; 00147 CloseParen = true; 00148 } 00149 switch (MO.getType()) { 00150 case MachineOperand::MO_Register: 00151 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 00152 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName); 00153 else 00154 O << "%reg" << MO.getReg(); 00155 break; 00156 00157 case MachineOperand::MO_Immediate: 00158 O << (int)MO.getImm(); 00159 break; 00160 case MachineOperand::MO_MachineBasicBlock: 00161 printBasicBlockLabel(MO.getMBB()); 00162 return; 00163 case MachineOperand::MO_GlobalAddress: 00164 O << Mang->getMangledName(MO.getGlobal()); 00165 break; 00166 case MachineOperand::MO_ExternalSymbol: 00167 O << MO.getSymbolName(); 00168 break; 00169 case MachineOperand::MO_ConstantPoolIndex: 00170 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" 00171 << MO.getIndex(); 00172 break; 00173 default: 00174 llvm_unreachable("<unknown operand type>"); 00175 } 00176 if (CloseParen) O << ")"; 00177 } 00178 00179 void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, 00180 const char *Modifier) { 00181 printOperand(MI, opNum); 00182 00183 // If this is an ADD operand, emit it like normal operands. 00184 if (Modifier && !strcmp(Modifier, "arith")) { 00185 O << ", "; 00186 printOperand(MI, opNum+1); 00187 return; 00188 } 00189 00190 if (MI->getOperand(opNum+1).isReg() && 00191 MI->getOperand(opNum+1).getReg() == SP::G0) 00192 return; // don't print "+%g0" 00193 if (MI->getOperand(opNum+1).isImm() && 00194 MI->getOperand(opNum+1).getImm() == 0) 00195 return; // don't print "+0" 00196 00197 O << "+"; 00198 if (MI->getOperand(opNum+1).isGlobal() || 00199 MI->getOperand(opNum+1).isCPI()) { 00200 O << "%lo("; 00201 printOperand(MI, opNum+1); 00202 O << ")"; 00203 } else { 00204 printOperand(MI, opNum+1); 00205 } 00206 } 00207 00208 void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { 00209 int CC = (int)MI->getOperand(opNum).getImm(); 00210 O << SPARCCondCodeToString((SPCC::CondCodes)CC); 00211 } 00212 00213 void SparcAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) { 00214 const TargetData *TD = TM.getTargetData(); 00215 00216 if (!GVar->hasInitializer()) 00217 return; // External global require no code 00218 00219 // Check to see if this is a special global used by LLVM, if so, emit it. 00220 if (EmitSpecialLLVMGlobal(GVar)) 00221 return; 00222 00223 O << "\n\n"; 00224 std::string name = Mang->getMangledName(GVar); 00225 Constant *C = GVar->getInitializer(); 00226 unsigned Size = TD->getTypeAllocSize(C->getType()); 00227 unsigned Align = TD->getPreferredAlignment(GVar); 00228 00229 printVisibility(name, GVar->getVisibility()); 00230 00231 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang, 00232 TM)); 00233 00234 if (C->isNullValue() && !GVar->hasSection()) { 00235 if (!GVar->isThreadLocal() && 00236 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) { 00237 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 00238 00239 if (GVar->hasLocalLinkage()) 00240 O << "\t.local " << name << '\n'; 00241 00242 O << TAI->getCOMMDirective() << name << ',' << Size; 00243 if (TAI->getCOMMDirectiveTakesAlignment()) 00244 O << ',' << (1 << Align); 00245 00246 O << '\n'; 00247 return; 00248 } 00249 } 00250 00251 switch (GVar->getLinkage()) { 00252 case GlobalValue::CommonLinkage: 00253 case GlobalValue::LinkOnceAnyLinkage: 00254 case GlobalValue::LinkOnceODRLinkage: 00255 case GlobalValue::WeakAnyLinkage: // FIXME: Verify correct for weak. 00256 case GlobalValue::WeakODRLinkage: // FIXME: Verify correct for weak. 00257 // Nonnull linkonce -> weak 00258 O << "\t.weak " << name << '\n'; 00259 break; 00260 case GlobalValue::AppendingLinkage: 00261 // FIXME: appending linkage variables should go into a section of 00262 // their name or something. For now, just emit them as external. 00263 case GlobalValue::ExternalLinkage: 00264 // If external or appending, declare as a global symbol 00265 O << TAI->getGlobalDirective() << name << '\n'; 00266 // FALL THROUGH 00267 case GlobalValue::PrivateLinkage: 00268 case GlobalValue::LinkerPrivateLinkage: 00269 case GlobalValue::InternalLinkage: 00270 break; 00271 case GlobalValue::GhostLinkage: 00272 llvm_unreachable("Should not have any unmaterialized functions!"); 00273 case GlobalValue::DLLImportLinkage: 00274 llvm_unreachable("DLLImport linkage is not supported by this target!"); 00275 case GlobalValue::DLLExportLinkage: 00276 llvm_unreachable("DLLExport linkage is not supported by this target!"); 00277 default: 00278 llvm_unreachable("Unknown linkage type!"); 00279 } 00280 00281 EmitAlignment(Align, GVar); 00282 00283 if (TAI->hasDotTypeDotSizeDirective()) { 00284 O << "\t.type " << name << ",#object\n"; 00285 O << "\t.size " << name << ',' << Size << '\n'; 00286 } 00287 00288 O << name << ":\n"; 00289 EmitGlobalConstant(C); 00290 } 00291 00292 /// PrintAsmOperand - Print out an operand for an inline asm expression. 00293 /// 00294 bool SparcAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, 00295 unsigned AsmVariant, 00296 const char *ExtraCode) { 00297 if (ExtraCode && ExtraCode[0]) { 00298 if (ExtraCode[1] != 0) return true; // Unknown modifier. 00299 00300 switch (ExtraCode[0]) { 00301 default: return true; // Unknown modifier. 00302 case 'r': 00303 break; 00304 } 00305 } 00306 00307 printOperand(MI, OpNo); 00308 00309 return false; 00310 } 00311 00312 bool SparcAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 00313 unsigned OpNo, 00314 unsigned AsmVariant, 00315 const char *ExtraCode) { 00316 if (ExtraCode && ExtraCode[0]) 00317 return true; // Unknown modifier 00318 00319 O << '['; 00320 printMemOperand(MI, OpNo); 00321 O << ']'; 00322 00323 return false; 00324 } 00325 00326 // Force static initialization. 00327 extern "C" void LLVMInitializeSparcAsmPrinter() { 00328 RegisterAsmPrinter<SparcAsmPrinter> X(TheSparcTarget); 00329 }