SparcAsmPrinter.cpp revision 4fca01731a86dbbd758eaf94e4c7edfa36d38db7
1//===-- SparcV8AsmPrinter.cpp - SparcV8 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 V8 assembly language. 12// 13//===----------------------------------------------------------------------===// 14 15#include "SparcV8.h" 16#include "SparcV8InstrInfo.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/TargetMachine.h" 26#include "llvm/Support/Mangler.h" 27#include "llvm/ADT/Statistic.h" 28#include "llvm/ADT/StringExtras.h" 29#include "llvm/Support/CommandLine.h" 30#include "llvm/Support/MathExtras.h" 31#include <cctype> 32using namespace llvm; 33 34namespace { 35 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); 36 37 struct SparcV8AsmPrinter : public AsmPrinter { 38 SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { 39 Data16bitsDirective = "\t.half\t"; 40 Data32bitsDirective = "\t.word\t"; 41 Data64bitsDirective = 0; // .xword is only supported by V9. 42 ZeroDirective = 0; // no .zero or .space! 43 CommentString = "!"; 44 ConstantPoolSection = "\t.section \".rodata\",#alloc\n"; 45 } 46 47 /// We name each basic block in a Function with a unique number, so 48 /// that we can consistently refer to them later. This is cleared 49 /// at the beginning of each call to runOnMachineFunction(). 50 /// 51 typedef std::map<const Value *, unsigned> ValueMapTy; 52 ValueMapTy NumberForBB; 53 54 virtual const char *getPassName() const { 55 return "SparcV8 Assembly Printer"; 56 } 57 58 void printOperand(const MachineInstr *MI, int opNum); 59 void printMemOperand(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); 64 }; 65} // end of anonymous namespace 66 67#include "SparcV8GenAsmWriter.inc" 68 69/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 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. 73/// 74FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, 75 TargetMachine &tm) { 76 return new SparcV8AsmPrinter(o, tm); 77} 78 79/// runOnMachineFunction - This uses the printMachineInstruction() 80/// method to print assembly for each instruction. 81/// 82bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 83 SetupMachineFunction(MF); 84 85 // Print out constants referenced by the function 86 EmitConstantPool(MF.getConstantPool()); 87 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; 91 92 O << "\n\n"; 93 // What's my mangled name? 94 CurrentFnName = Mang->getValueName(MF.getFunction()); 95 96 // Print out labels for the function. 97 O << "\t.text\n"; 98 O << "\t.align 16\n"; 99 O << "\t.globl\t" << CurrentFnName << "\n"; 100 O << "\t.type\t" << CurrentFnName << ", #function\n"; 101 O << CurrentFnName << ":\n"; 102 103 // Number each basic block so that we can consistently refer to them 104 // in PC-relative references. 105 NumberForBB.clear(); 106 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 107 I != E; ++I) { 108 NumberForBB[I->getBasicBlock()] = BBNumber++; 109 } 110 111 // Print out code for the function. 112 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 113 I != E; ++I) { 114 // Print a label for the basic block. 115 if (I != MF.begin()) 116 O << ".LBB" << Mang->getValueName(MF.getFunction ()) 117 << "_" << I->getNumber () << ":\t! " 118 << I->getBasicBlock ()->getName () << "\n"; 119 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 120 II != E; ++II) { 121 // Print the assembly for the instruction. 122 O << "\t"; 123 printInstruction(II); 124 ++EmittedInsts; 125 } 126 } 127 128 // We didn't modify anything. 129 return false; 130} 131 132void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 133 const MachineOperand &MO = MI->getOperand (opNum); 134 const MRegisterInfo &RI = *TM.getRegisterInfo(); 135 bool CloseParen = false; 136 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { 137 O << "%hi("; 138 CloseParen = true; 139 } else if ((MI->getOpcode() == V8::ORri || MI->getOpcode() == V8::ADDri) 140 && !MO.isRegister() && !MO.isImmediate()) { 141 O << "%lo("; 142 CloseParen = true; 143 } 144 switch (MO.getType()) { 145 case MachineOperand::MO_VirtualRegister: 146 if (Value *V = MO.getVRegValueOrNull()) { 147 O << "<" << V->getName() << ">"; 148 break; 149 } 150 // FALLTHROUGH 151 case MachineOperand::MO_MachineRegister: 152 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) 153 O << "%" << LowercaseString (RI.get(MO.getReg()).Name); 154 else 155 O << "%reg" << MO.getReg(); 156 break; 157 158 case MachineOperand::MO_SignExtendedImmed: 159 case MachineOperand::MO_UnextendedImmed: 160 O << (int)MO.getImmedValue(); 161 break; 162 case MachineOperand::MO_MachineBasicBlock: { 163 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); 164 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) 165 << "_" << MBBOp->getNumber () << "\t! " 166 << MBBOp->getBasicBlock ()->getName (); 167 return; 168 } 169 case MachineOperand::MO_PCRelativeDisp: 170 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; 171 abort (); 172 return; 173 case MachineOperand::MO_GlobalAddress: 174 O << Mang->getValueName(MO.getGlobal()); 175 break; 176 case MachineOperand::MO_ExternalSymbol: 177 O << MO.getSymbolName(); 178 break; 179 case MachineOperand::MO_ConstantPoolIndex: 180 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" 181 << MO.getConstantPoolIndex(); 182 break; 183 default: 184 O << "<unknown operand type>"; abort (); break; 185 } 186 if (CloseParen) O << ")"; 187} 188 189void SparcV8AsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) { 190 printOperand(MI, opNum); 191 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType(); 192 193 if ((OpTy == MachineOperand::MO_VirtualRegister || 194 OpTy == MachineOperand::MO_MachineRegister) && 195 MI->getOperand(opNum+1).getReg() == V8::G0) 196 return; // don't print "+%g0" 197 if ((OpTy == MachineOperand::MO_SignExtendedImmed || 198 OpTy == MachineOperand::MO_UnextendedImmed) && 199 MI->getOperand(opNum+1).getImmedValue() == 0) 200 return; // don't print "+0" 201 202 O << "+"; 203 if (OpTy == MachineOperand::MO_GlobalAddress || 204 OpTy == MachineOperand::MO_ConstantPoolIndex) { 205 O << "%lo("; 206 printOperand(MI, opNum+1); 207 O << ")"; 208 } else { 209 printOperand(MI, opNum+1); 210 } 211} 212 213 214bool SparcV8AsmPrinter::doInitialization(Module &M) { 215 Mang = new Mangler(M); 216 return false; // success 217} 218 219bool SparcV8AsmPrinter::doFinalization(Module &M) { 220 const TargetData &TD = TM.getTargetData(); 221 222 // Print out module-level global variables here. 223 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 224 if (I->hasInitializer()) { // External global require no code 225 O << "\n\n"; 226 std::string name = Mang->getValueName(I); 227 Constant *C = I->getInitializer(); 228 unsigned Size = TD.getTypeSize(C->getType()); 229 unsigned Align = TD.getTypeAlignment(C->getType()); 230 231 if (C->isNullValue() && 232 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || 233 I->hasWeakLinkage() /* FIXME: Verify correct */)) { 234 SwitchSection(".data", I); 235 if (I->hasInternalLinkage()) 236 O << "\t.local " << name << "\n"; 237 238 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) 239 << "," << (unsigned)TD.getTypeAlignment(C->getType()); 240 O << "\t\t! "; 241 WriteAsOperand(O, I, true, true, &M); 242 O << "\n"; 243 } else { 244 switch (I->getLinkage()) { 245 case GlobalValue::LinkOnceLinkage: 246 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 247 // Nonnull linkonce -> weak 248 O << "\t.weak " << name << "\n"; 249 SwitchSection("", I); 250 O << "\t.section\t\".llvm.linkonce.d." << name 251 << "\",\"aw\",@progbits\n"; 252 break; 253 254 case GlobalValue::AppendingLinkage: 255 // FIXME: appending linkage variables should go into a section of 256 // their name or something. For now, just emit them as external. 257 case GlobalValue::ExternalLinkage: 258 // If external or appending, declare as a global symbol 259 O << "\t.globl " << name << "\n"; 260 // FALL THROUGH 261 case GlobalValue::InternalLinkage: 262 if (C->isNullValue()) 263 SwitchSection(".bss", I); 264 else 265 SwitchSection(".data", I); 266 break; 267 case GlobalValue::GhostLinkage: 268 std::cerr << "Should not have any unmaterialized functions!\n"; 269 abort(); 270 } 271 272 O << "\t.align " << Align << "\n"; 273 O << "\t.type " << name << ",#object\n"; 274 O << "\t.size " << name << "," << Size << "\n"; 275 O << name << ":\t\t\t\t! "; 276 WriteAsOperand(O, I, true, true, &M); 277 O << " = "; 278 WriteAsOperand(O, C, false, false, &M); 279 O << "\n"; 280 EmitGlobalConstant(C); 281 } 282 } 283 284 AsmPrinter::doFinalization(M); 285 return false; // success 286} 287