SparcAsmPrinter.cpp revision f5b6a47bb57fb5ffc734416d4d5d993e1a06273b
1//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains a printer that converts from our internal representation 11// of machine-dependent LLVM code to GAS-format SPARC assembly language. 12// 13//===----------------------------------------------------------------------===// 14 15#define DEBUG_TYPE "asm-printer" 16#include "Sparc.h" 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" 33#include <cctype> 34#include <cstring> 35#include <map> 36using namespace llvm; 37 38STATISTIC(EmittedInsts, "Number of machine instrs printed"); 39 40namespace { 41 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter { 42 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) 43 : AsmPrinter(O, TM, T) { 44 } 45 46 /// We name each basic block in a Function with a unique number, so 47 /// that we can consistently refer to them later. This is cleared 48 /// at the beginning of each call to runOnMachineFunction(). 49 /// 50 typedef std::map<const Value *, unsigned> ValueMapTy; 51 ValueMapTy NumberForBB; 52 53 virtual const char *getPassName() const { 54 return "Sparc Assembly Printer"; 55 } 56 57 void printModuleLevelGV(const GlobalVariable* GVar); 58 void printOperand(const MachineInstr *MI, int opNum); 59 void printMemOperand(const MachineInstr *MI, int opNum, 60 const char *Modifier = 0); 61 void printCCOperand(const MachineInstr *MI, int opNum); 62 63 bool printInstruction(const MachineInstr *MI); // autogenerated. 64 bool runOnMachineFunction(MachineFunction &F); 65 std::string getSectionForFunction(const Function &F) const; 66 bool doInitialization(Module &M); 67 bool doFinalization(Module &M); 68 }; 69} // end of anonymous namespace 70 71#include "SparcGenAsmWriter.inc" 72 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. 77/// 78FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o, 79 TargetMachine &tm) { 80 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo()); 81} 82 83// Substitute old hook with new one temporary 84std::string SparcAsmPrinter::getSectionForFunction(const Function &F) const { 85 return TAI->SectionForGlobal(&F); 86} 87 88/// runOnMachineFunction - This uses the printInstruction() 89/// method to print assembly for each instruction. 90/// 91bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 92 SetupMachineFunction(MF); 93 94 // Print out constants referenced by the function 95 EmitConstantPool(MF.getConstantPool()); 96 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; 100 101 O << "\n\n"; 102 // What's my mangled name? 103 CurrentFnName = Mang->getValueName(MF.getFunction()); 104 105 // Print out the label for the function. 106 const Function *F = MF.getFunction(); 107 SwitchToTextSection(getSectionForFunction(*F).c_str(), F); 108 EmitAlignment(4, F); 109 O << "\t.globl\t" << CurrentFnName << '\n'; 110 111 printVisibility(CurrentFnName, F->getVisibility()); 112 113 O << "\t.type\t" << CurrentFnName << ", #function\n"; 114 O << CurrentFnName << ":\n"; 115 116 // Number each basic block so that we can consistently refer to them 117 // in PC-relative references. 118 // FIXME: Why not use the MBB numbers? 119 NumberForBB.clear(); 120 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 121 I != E; ++I) { 122 NumberForBB[I->getBasicBlock()] = BBNumber++; 123 } 124 125 // Print out code for the function. 126 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 127 I != E; ++I) { 128 // Print a label for the basic block. 129 if (I != MF.begin()) { 130 printBasicBlockLabel(I, true, true); 131 O << '\n'; 132 } 133 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 134 II != E; ++II) { 135 // Print the assembly for the instruction. 136 printInstruction(II); 137 ++EmittedInsts; 138 } 139 } 140 141 // We didn't modify anything. 142 return false; 143} 144 145void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 146 const MachineOperand &MO = MI->getOperand (opNum); 147 const TargetRegisterInfo &RI = *TM.getRegisterInfo(); 148 bool CloseParen = false; 149 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) { 150 O << "%hi("; 151 CloseParen = true; 152 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) 153 && !MO.isRegister() && !MO.isImmediate()) { 154 O << "%lo("; 155 CloseParen = true; 156 } 157 switch (MO.getType()) { 158 case MachineOperand::MO_Register: 159 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 160 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName); 161 else 162 O << "%reg" << MO.getReg(); 163 break; 164 165 case MachineOperand::MO_Immediate: 166 O << (int)MO.getImm(); 167 break; 168 case MachineOperand::MO_MachineBasicBlock: 169 printBasicBlockLabel(MO.getMBB()); 170 return; 171 case MachineOperand::MO_GlobalAddress: 172 O << Mang->getValueName(MO.getGlobal()); 173 break; 174 case MachineOperand::MO_ExternalSymbol: 175 O << MO.getSymbolName(); 176 break; 177 case MachineOperand::MO_ConstantPoolIndex: 178 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" 179 << MO.getIndex(); 180 break; 181 default: 182 O << "<unknown operand type>"; abort (); break; 183 } 184 if (CloseParen) O << ")"; 185} 186 187void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, 188 const char *Modifier) { 189 printOperand(MI, opNum); 190 191 // If this is an ADD operand, emit it like normal operands. 192 if (Modifier && !strcmp(Modifier, "arith")) { 193 O << ", "; 194 printOperand(MI, opNum+1); 195 return; 196 } 197 198 if (MI->getOperand(opNum+1).isRegister() && 199 MI->getOperand(opNum+1).getReg() == SP::G0) 200 return; // don't print "+%g0" 201 if (MI->getOperand(opNum+1).isImmediate() && 202 MI->getOperand(opNum+1).getImm() == 0) 203 return; // don't print "+0" 204 205 O << "+"; 206 if (MI->getOperand(opNum+1).isGlobalAddress() || 207 MI->getOperand(opNum+1).isConstantPoolIndex()) { 208 O << "%lo("; 209 printOperand(MI, opNum+1); 210 O << ")"; 211 } else { 212 printOperand(MI, opNum+1); 213 } 214} 215 216void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { 217 int CC = (int)MI->getOperand(opNum).getImm(); 218 O << SPARCCondCodeToString((SPCC::CondCodes)CC); 219} 220 221 222 223bool SparcAsmPrinter::doInitialization(Module &M) { 224 Mang = new Mangler(M); 225 return false; // success 226} 227 228bool SparcAsmPrinter::doFinalization(Module &M) { 229 // Print out module-level global variables here. 230 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 231 I != E; ++I) 232 printModuleLevelGV(I); 233 234 O << '\n'; 235 236 return AsmPrinter::doFinalization(M); 237} 238 239void SparcAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) { 240 const TargetData *TD = TM.getTargetData(); 241 242 if (!GVar->hasInitializer()) 243 return; // External global require no code 244 245 // Check to see if this is a special global used by LLVM, if so, emit it. 246 if (EmitSpecialLLVMGlobal(GVar)) 247 return; 248 249 O << "\n\n"; 250 std::string SectionName = TAI->SectionForGlobal(GVar); 251 std::string name = Mang->getValueName(GVar); 252 Constant *C = GVar->getInitializer(); 253 unsigned Size = TD->getABITypeSize(C->getType()); 254 unsigned Align = TD->getPreferredAlignment(GVar); 255 256 printVisibility(name, GVar->getVisibility()); 257 258 SwitchToDataSection(SectionName.c_str()); 259 260 if (C->isNullValue() && !GVar->hasSection()) { 261 if (!GVar->isThreadLocal() && 262 (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) { 263 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. 264 265 if (GVar->hasInternalLinkage()) 266 O << "\t.local " << name << '\n'; 267 268 O << TAI->getCOMMDirective() << name << ',' << Size; 269 if (TAI->getCOMMDirectiveTakesAlignment()) 270 O << ',' << (1 << Align); 271 272 O << '\n'; 273 return; 274 } 275 } 276 277 switch (GVar->getLinkage()) { 278 case GlobalValue::CommonLinkage: 279 case GlobalValue::LinkOnceLinkage: 280 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 281 // Nonnull linkonce -> weak 282 O << "\t.weak " << name << '\n'; 283 break; 284 case GlobalValue::AppendingLinkage: 285 // FIXME: appending linkage variables should go into a section of 286 // their name or something. For now, just emit them as external. 287 case GlobalValue::ExternalLinkage: 288 // If external or appending, declare as a global symbol 289 O << TAI->getGlobalDirective() << name << '\n'; 290 // FALL THROUGH 291 case GlobalValue::InternalLinkage: 292 break; 293 case GlobalValue::GhostLinkage: 294 cerr << "Should not have any unmaterialized functions!\n"; 295 abort(); 296 case GlobalValue::DLLImportLinkage: 297 cerr << "DLLImport linkage is not supported by this target!\n"; 298 abort(); 299 case GlobalValue::DLLExportLinkage: 300 cerr << "DLLExport linkage is not supported by this target!\n"; 301 abort(); 302 default: 303 assert(0 && "Unknown linkage type!"); 304 } 305 306 EmitAlignment(Align, GVar); 307 308 if (TAI->hasDotTypeDotSizeDirective()) { 309 O << "\t.type " << name << ",#object\n"; 310 O << "\t.size " << name << ',' << Size << '\n'; 311 } 312 313 O << name << ":\n"; 314 EmitGlobalConstant(C); 315} 316