SparcAsmPrinter.cpp revision aafce77b17d340aace52bcd49d1944109d82f14a
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> 35using namespace llvm; 36 37STATISTIC(EmittedInsts, "Number of machine instrs printed"); 38 39namespace { 40 struct VISIBILITY_HIDDEN SparcAsmPrinter : public AsmPrinter { 41 SparcAsmPrinter(std::ostream &O, TargetMachine &TM, const TargetAsmInfo *T) 42 : AsmPrinter(O, TM, T) { 43 } 44 45 /// We name each basic block in a Function with a unique number, so 46 /// that we can consistently refer to them later. This is cleared 47 /// at the beginning of each call to runOnMachineFunction(). 48 /// 49 typedef std::map<const Value *, unsigned> ValueMapTy; 50 ValueMapTy NumberForBB; 51 52 virtual const char *getPassName() const { 53 return "Sparc Assembly Printer"; 54 } 55 56 void printOperand(const MachineInstr *MI, int opNum); 57 void printMemOperand(const MachineInstr *MI, int opNum, 58 const char *Modifier = 0); 59 void printCCOperand(const MachineInstr *MI, int opNum); 60 61 bool printInstruction(const MachineInstr *MI); // autogenerated. 62 bool runOnMachineFunction(MachineFunction &F); 63 bool doInitialization(Module &M); 64 bool doFinalization(Module &M); 65 }; 66} // end of anonymous namespace 67 68#include "SparcGenAsmWriter.inc" 69 70/// createSparcCodePrinterPass - Returns a pass that prints the SPARC 71/// assembly code for a MachineFunction to the given output stream, 72/// using the given target machine description. This should work 73/// regardless of whether the function is in SSA form. 74/// 75FunctionPass *llvm::createSparcCodePrinterPass(std::ostream &o, 76 TargetMachine &tm) { 77 return new SparcAsmPrinter(o, tm, tm.getTargetAsmInfo()); 78} 79 80/// runOnMachineFunction - This uses the printInstruction() 81/// method to print assembly for each instruction. 82/// 83bool SparcAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 84 SetupMachineFunction(MF); 85 86 // Print out constants referenced by the function 87 EmitConstantPool(MF.getConstantPool()); 88 89 // BBNumber is used here so that a given Printer will never give two 90 // BBs the same name. (If you have a better way, please let me know!) 91 static unsigned BBNumber = 0; 92 93 O << "\n\n"; 94 // What's my mangled name? 95 CurrentFnName = Mang->getValueName(MF.getFunction()); 96 97 // Print out the label for the function. 98 const Function *F = MF.getFunction(); 99 SwitchToTextSection(getSectionForFunction(*F).c_str(), F); 100 EmitAlignment(4, F); 101 O << "\t.globl\t" << CurrentFnName << "\n"; 102 O << "\t.type\t" << CurrentFnName << ", #function\n"; 103 O << CurrentFnName << ":\n"; 104 105 // Number each basic block so that we can consistently refer to them 106 // in PC-relative references. 107 // FIXME: Why not use the MBB numbers? 108 NumberForBB.clear(); 109 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 110 I != E; ++I) { 111 NumberForBB[I->getBasicBlock()] = BBNumber++; 112 } 113 114 // Print out code for the function. 115 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 116 I != E; ++I) { 117 // Print a label for the basic block. 118 if (I != MF.begin()) { 119 printBasicBlockLabel(I, true, true); 120 O << '\n'; 121 } 122 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 123 II != E; ++II) { 124 // Print the assembly for the instruction. 125 printInstruction(II); 126 ++EmittedInsts; 127 } 128 } 129 130 // We didn't modify anything. 131 return false; 132} 133 134void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 135 const MachineOperand &MO = MI->getOperand (opNum); 136 const TargetRegisterInfo &RI = *TM.getRegisterInfo(); 137 bool CloseParen = false; 138 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) { 139 O << "%hi("; 140 CloseParen = true; 141 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) 142 && !MO.isRegister() && !MO.isImmediate()) { 143 O << "%lo("; 144 CloseParen = true; 145 } 146 switch (MO.getType()) { 147 case MachineOperand::MO_Register: 148 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) 149 O << "%" << LowercaseString (RI.get(MO.getReg()).AsmName); 150 else 151 O << "%reg" << MO.getReg(); 152 break; 153 154 case MachineOperand::MO_Immediate: 155 O << (int)MO.getImm(); 156 break; 157 case MachineOperand::MO_MachineBasicBlock: 158 printBasicBlockLabel(MO.getMBB()); 159 return; 160 case MachineOperand::MO_GlobalAddress: 161 O << Mang->getValueName(MO.getGlobal()); 162 break; 163 case MachineOperand::MO_ExternalSymbol: 164 O << MO.getSymbolName(); 165 break; 166 case MachineOperand::MO_ConstantPoolIndex: 167 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" 168 << MO.getIndex(); 169 break; 170 default: 171 O << "<unknown operand type>"; abort (); break; 172 } 173 if (CloseParen) O << ")"; 174} 175 176void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, 177 const char *Modifier) { 178 printOperand(MI, opNum); 179 180 // If this is an ADD operand, emit it like normal operands. 181 if (Modifier && !strcmp(Modifier, "arith")) { 182 O << ", "; 183 printOperand(MI, opNum+1); 184 return; 185 } 186 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).getImm() == 0) 192 return; // don't print "+0" 193 194 O << "+"; 195 if (MI->getOperand(opNum+1).isGlobalAddress() || 196 MI->getOperand(opNum+1).isConstantPoolIndex()) { 197 O << "%lo("; 198 printOperand(MI, opNum+1); 199 O << ")"; 200 } else { 201 printOperand(MI, opNum+1); 202 } 203} 204 205void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { 206 int CC = (int)MI->getOperand(opNum).getImm(); 207 O << SPARCCondCodeToString((SPCC::CondCodes)CC); 208} 209 210 211 212bool SparcAsmPrinter::doInitialization(Module &M) { 213 Mang = new Mangler(M); 214 return false; // success 215} 216 217bool SparcAsmPrinter::doFinalization(Module &M) { 218 const TargetData *TD = TM.getTargetData(); 219 220 // Print out module-level global variables here. 221 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 222 I != E; ++I) 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)) 226 continue; 227 228 O << "\n\n"; 229 std::string name = Mang->getValueName(I); 230 Constant *C = I->getInitializer(); 231 unsigned Size = TD->getABITypeSize(C->getType()); 232 unsigned Align = TD->getPreferredAlignment(I); 233 234 if (C->isNullValue() && (I->hasCommonLinkage() || 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"; 240 241 O << "\t.comm " << name << "," << TD->getABITypeSize(C->getType()) 242 << "," << Align; 243 O << "\n"; 244 } else { 245 switch (I->getLinkage()) { 246 case GlobalValue::CommonLinkage: 247 case GlobalValue::LinkOnceLinkage: 248 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 249 // Nonnull linkonce -> weak 250 O << "\t.weak " << name << "\n"; 251 SwitchToDataSection("", I); 252 O << "\t.section\t\".llvm.linkonce.d." << name 253 << "\",\"aw\",@progbits\n"; 254 break; 255 256 case GlobalValue::AppendingLinkage: 257 // FIXME: appending linkage variables should go into a section of 258 // their name or something. For now, just emit them as external. 259 case GlobalValue::ExternalLinkage: 260 // If external or appending, declare as a global symbol 261 O << "\t.globl " << name << "\n"; 262 // FALL THROUGH 263 case GlobalValue::InternalLinkage: 264 if (C->isNullValue()) 265 SwitchToDataSection(".bss", I); 266 else 267 SwitchToDataSection(".data", I); 268 break; 269 case GlobalValue::GhostLinkage: 270 cerr << "Should not have any unmaterialized functions!\n"; 271 abort(); 272 case GlobalValue::DLLImportLinkage: 273 cerr << "DLLImport linkage is not supported by this target!\n"; 274 abort(); 275 case GlobalValue::DLLExportLinkage: 276 cerr << "DLLExport linkage is not supported by this target!\n"; 277 abort(); 278 default: 279 assert(0 && "Unknown linkage type!"); 280 } 281 282 O << "\t.align " << Align << "\n"; 283 O << "\t.type " << name << ",#object\n"; 284 O << "\t.size " << name << "," << Size << "\n"; 285 O << name << ":\n"; 286 EmitGlobalConstant(C); 287 } 288 } 289 290 return AsmPrinter::doFinalization(M); 291} 292