SparcAsmPrinter.cpp revision 29bd9e12d4fbd667c56225872b15ca490440da46
1//===-- SparcAsmPrinter.cpp - Sparc LLVM assembly writer ------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 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. 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#include "Sparc.h" 16#include "SparcInstrInfo.h" 17#include "llvm/Constants.h" 18#include "llvm/DerivedTypes.h" 19#include "llvm/Module.h" 20#include "llvm/Assembly/Writer.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 <iostream> 35using namespace llvm; 36 37namespace { 38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); 39 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 printMachineInstruction() 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); 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 O << "\t"; 126 printInstruction(II); 127 ++EmittedInsts; 128 } 129 } 130 131 // We didn't modify anything. 132 return false; 133} 134 135void SparcAsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 136 const MachineOperand &MO = MI->getOperand (opNum); 137 const MRegisterInfo &RI = *TM.getRegisterInfo(); 138 bool CloseParen = false; 139 if (MI->getOpcode() == SP::SETHIi && !MO.isRegister() && !MO.isImmediate()) { 140 O << "%hi("; 141 CloseParen = true; 142 } else if ((MI->getOpcode() == SP::ORri || MI->getOpcode() == SP::ADDri) 143 && !MO.isRegister() && !MO.isImmediate()) { 144 O << "%lo("; 145 CloseParen = true; 146 } 147 switch (MO.getType()) { 148 case MachineOperand::MO_Register: 149 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) 150 O << "%" << LowercaseString (RI.get(MO.getReg()).Name); 151 else 152 O << "%reg" << MO.getReg(); 153 break; 154 155 case MachineOperand::MO_Immediate: 156 O << (int)MO.getImmedValue(); 157 break; 158 case MachineOperand::MO_MachineBasicBlock: 159 printBasicBlockLabel(MO.getMachineBasicBlock()); 160 return; 161 case MachineOperand::MO_GlobalAddress: 162 O << Mang->getValueName(MO.getGlobal()); 163 break; 164 case MachineOperand::MO_ExternalSymbol: 165 O << MO.getSymbolName(); 166 break; 167 case MachineOperand::MO_ConstantPoolIndex: 168 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_" 169 << MO.getConstantPoolIndex(); 170 break; 171 default: 172 O << "<unknown operand type>"; abort (); break; 173 } 174 if (CloseParen) O << ")"; 175} 176 177void SparcAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum, 178 const char *Modifier) { 179 printOperand(MI, opNum); 180 181 // If this is an ADD operand, emit it like normal operands. 182 if (Modifier && !strcmp(Modifier, "arith")) { 183 O << ", "; 184 printOperand(MI, opNum+1); 185 return; 186 } 187 188 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType(); 189 190 if (MI->getOperand(opNum+1).isRegister() && 191 MI->getOperand(opNum+1).getReg() == SP::G0) 192 return; // don't print "+%g0" 193 if (MI->getOperand(opNum+1).isImmediate() && 194 MI->getOperand(opNum+1).getImmedValue() == 0) 195 return; // don't print "+0" 196 197 O << "+"; 198 if (MI->getOperand(opNum+1).isGlobalAddress() || 199 MI->getOperand(opNum+1).isConstantPoolIndex()) { 200 O << "%lo("; 201 printOperand(MI, opNum+1); 202 O << ")"; 203 } else { 204 printOperand(MI, opNum+1); 205 } 206} 207 208void SparcAsmPrinter::printCCOperand(const MachineInstr *MI, int opNum) { 209 int CC = (int)MI->getOperand(opNum).getImmedValue(); 210 O << SPARCCondCodeToString((SPCC::CondCodes)CC); 211} 212 213 214 215bool SparcAsmPrinter::doInitialization(Module &M) { 216 Mang = new Mangler(M); 217 return false; // success 218} 219 220bool SparcAsmPrinter::doFinalization(Module &M) { 221 const TargetData *TD = TM.getTargetData(); 222 223 // Print out module-level global variables here. 224 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); 225 I != E; ++I) 226 if (I->hasInitializer()) { // External global require no code 227 // Check to see if this is a special global used by LLVM, if so, emit it. 228 if (EmitSpecialLLVMGlobal(I)) 229 continue; 230 231 O << "\n\n"; 232 std::string name = Mang->getValueName(I); 233 Constant *C = I->getInitializer(); 234 unsigned Size = TD->getTypeSize(C->getType()); 235 unsigned Align = TD->getTypeAlignment(C->getType()); 236 237 if (C->isNullValue() && 238 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || 239 I->hasWeakLinkage() /* FIXME: Verify correct */)) { 240 SwitchToDataSection(".data", I); 241 if (I->hasInternalLinkage()) 242 O << "\t.local " << name << "\n"; 243 244 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType()) 245 << "," << (unsigned)TD->getTypeAlignment(C->getType()); 246 O << "\t\t! "; 247 WriteAsOperand(O, I, true, true, &M); 248 O << "\n"; 249 } else { 250 switch (I->getLinkage()) { 251 case GlobalValue::LinkOnceLinkage: 252 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 253 // Nonnull linkonce -> weak 254 O << "\t.weak " << name << "\n"; 255 SwitchToDataSection("", I); 256 O << "\t.section\t\".llvm.linkonce.d." << name 257 << "\",\"aw\",@progbits\n"; 258 break; 259 260 case GlobalValue::AppendingLinkage: 261 // FIXME: appending linkage variables should go into a section of 262 // their name or something. For now, just emit them as external. 263 case GlobalValue::ExternalLinkage: 264 // If external or appending, declare as a global symbol 265 O << "\t.globl " << name << "\n"; 266 // FALL THROUGH 267 case GlobalValue::InternalLinkage: 268 if (C->isNullValue()) 269 SwitchToDataSection(".bss", I); 270 else 271 SwitchToDataSection(".data", I); 272 break; 273 case GlobalValue::GhostLinkage: 274 std::cerr << "Should not have any unmaterialized functions!\n"; 275 abort(); 276 case GlobalValue::DLLImportLinkage: 277 std::cerr << "DLLImport linkage is not supported by this target!\n"; 278 abort(); 279 case GlobalValue::DLLExportLinkage: 280 std::cerr << "DLLExport linkage is not supported by this target!\n"; 281 abort(); 282 default: 283 assert(0 && "Unknown linkage type!"); 284 } 285 286 O << "\t.align " << Align << "\n"; 287 O << "\t.type " << name << ",#object\n"; 288 O << "\t.size " << name << "," << Size << "\n"; 289 O << name << ":\t\t\t\t! "; 290 WriteAsOperand(O, I, true, true, &M); 291 O << "\n"; 292 EmitGlobalConstant(C); 293 } 294 } 295 296 AsmPrinter::doFinalization(M); 297 return false; // success 298} 299