SparcAsmPrinter.cpp revision 76acc872b3c63c26a83c2832ece6fa9b04786f24
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 = "\t.xword\t"; 42 ZeroDirective = 0; // no .zero or .space! 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 "SparcV8 Assembly Printer"; 54 } 55 56 void printOperand(const MachineInstr *MI, int opNum); 57 void printMemOperand(const MachineInstr *MI, int opNum); 58 bool printInstruction(const MachineInstr *MI); // autogenerated. 59 bool runOnMachineFunction(MachineFunction &F); 60 bool doInitialization(Module &M); 61 bool doFinalization(Module &M); 62 }; 63} // end of anonymous namespace 64 65#include "SparcV8GenAsmWriter.inc" 66 67/// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 68/// assembly code for a MachineFunction to the given output stream, 69/// using the given target machine description. This should work 70/// regardless of whether the function is in SSA form. 71/// 72FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, 73 TargetMachine &tm) { 74 return new SparcV8AsmPrinter(o, tm); 75} 76 77/// runOnMachineFunction - This uses the printMachineInstruction() 78/// method to print assembly for each instruction. 79/// 80bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 81 SetupMachineFunction(MF); 82 83 // Print out constants referenced by the function 84 EmitConstantPool(MF.getConstantPool()); 85 86 // BBNumber is used here so that a given Printer will never give two 87 // BBs the same name. (If you have a better way, please let me know!) 88 static unsigned BBNumber = 0; 89 90 O << "\n\n"; 91 // What's my mangled name? 92 CurrentFnName = Mang->getValueName(MF.getFunction()); 93 94 // Print out labels for the function. 95 O << "\t.text\n"; 96 O << "\t.align 16\n"; 97 O << "\t.globl\t" << CurrentFnName << "\n"; 98 O << "\t.type\t" << CurrentFnName << ", #function\n"; 99 O << CurrentFnName << ":\n"; 100 101 // Number each basic block so that we can consistently refer to them 102 // in PC-relative references. 103 NumberForBB.clear(); 104 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 105 I != E; ++I) { 106 NumberForBB[I->getBasicBlock()] = BBNumber++; 107 } 108 109 // Print out code for the function. 110 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 111 I != E; ++I) { 112 // Print a label for the basic block. 113 O << ".LBB" << Mang->getValueName(MF.getFunction ()) 114 << "_" << I->getNumber () << ":\t! " 115 << I->getBasicBlock ()->getName () << "\n"; 116 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 117 II != E; ++II) { 118 // Print the assembly for the instruction. 119 O << "\t"; 120 printInstruction(II); 121 ++EmittedInsts; 122 } 123 } 124 125 // We didn't modify anything. 126 return false; 127} 128 129void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { 130 const MachineOperand &MO = MI->getOperand (opNum); 131 const MRegisterInfo &RI = *TM.getRegisterInfo(); 132 bool CloseParen = false; 133 if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { 134 O << "%hi("; 135 CloseParen = true; 136 } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate()) 137 { 138 O << "%lo("; 139 CloseParen = true; 140 } 141 switch (MO.getType()) { 142 case MachineOperand::MO_VirtualRegister: 143 if (Value *V = MO.getVRegValueOrNull()) { 144 O << "<" << V->getName() << ">"; 145 break; 146 } 147 // FALLTHROUGH 148 case MachineOperand::MO_MachineRegister: 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_SignExtendedImmed: 156 case MachineOperand::MO_UnextendedImmed: 157 O << (int)MO.getImmedValue(); 158 break; 159 case MachineOperand::MO_MachineBasicBlock: { 160 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); 161 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) 162 << "_" << MBBOp->getNumber () << "\t! " 163 << MBBOp->getBasicBlock ()->getName (); 164 return; 165 } 166 case MachineOperand::MO_PCRelativeDisp: 167 std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; 168 abort (); 169 return; 170 case MachineOperand::MO_GlobalAddress: 171 O << Mang->getValueName(MO.getGlobal()); 172 break; 173 case MachineOperand::MO_ExternalSymbol: 174 O << MO.getSymbolName(); 175 break; 176 case MachineOperand::MO_ConstantPoolIndex: 177 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" 178 << MO.getConstantPoolIndex(); 179 break; 180 default: 181 O << "<unknown operand type>"; abort (); break; 182 } 183 if (CloseParen) O << ")"; 184} 185 186void SparcV8AsmPrinter::printMemOperand(const MachineInstr *MI, int opNum) { 187 printOperand(MI, opNum); 188 MachineOperand::MachineOperandType OpTy = MI->getOperand(opNum+1).getType(); 189 190 if ((OpTy == MachineOperand::MO_VirtualRegister || 191 OpTy == MachineOperand::MO_MachineRegister) && 192 MI->getOperand(opNum+1).getReg() == V8::G0) 193 return; // don't print "+%g0" 194 if ((OpTy == MachineOperand::MO_SignExtendedImmed || 195 OpTy == MachineOperand::MO_UnextendedImmed) && 196 MI->getOperand(opNum+1).getImmedValue() == 0) 197 return; // don't print "+0" 198 199 O << "+"; 200 if (OpTy == MachineOperand::MO_GlobalAddress || 201 OpTy == MachineOperand::MO_ConstantPoolIndex) { 202 O << "%lo("; 203 printOperand(MI, opNum+1); 204 O << ")"; 205 } else { 206 printOperand(MI, opNum+1); 207 } 208} 209 210 211bool SparcV8AsmPrinter::doInitialization(Module &M) { 212 Mang = new Mangler(M); 213 return false; // success 214} 215 216bool SparcV8AsmPrinter::doFinalization(Module &M) { 217 const TargetData &TD = TM.getTargetData(); 218 219 // Print out module-level global variables here. 220 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 221 if (I->hasInitializer()) { // External global require no code 222 O << "\n\n"; 223 std::string name = Mang->getValueName(I); 224 Constant *C = I->getInitializer(); 225 unsigned Size = TD.getTypeSize(C->getType()); 226 unsigned Align = TD.getTypeAlignment(C->getType()); 227 228 if (C->isNullValue() && 229 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || 230 I->hasWeakLinkage() /* FIXME: Verify correct */)) { 231 SwitchSection(".data", I); 232 if (I->hasInternalLinkage()) 233 O << "\t.local " << name << "\n"; 234 235 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) 236 << "," << (unsigned)TD.getTypeAlignment(C->getType()); 237 O << "\t\t! "; 238 WriteAsOperand(O, I, true, true, &M); 239 O << "\n"; 240 } else { 241 switch (I->getLinkage()) { 242 case GlobalValue::LinkOnceLinkage: 243 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 244 // Nonnull linkonce -> weak 245 O << "\t.weak " << name << "\n"; 246 SwitchSection("", I); 247 O << "\t.section\t\".llvm.linkonce.d." << name 248 << "\",\"aw\",@progbits\n"; 249 break; 250 251 case GlobalValue::AppendingLinkage: 252 // FIXME: appending linkage variables should go into a section of 253 // their name or something. For now, just emit them as external. 254 case GlobalValue::ExternalLinkage: 255 // If external or appending, declare as a global symbol 256 O << "\t.globl " << name << "\n"; 257 // FALL THROUGH 258 case GlobalValue::InternalLinkage: 259 if (C->isNullValue()) 260 SwitchSection(".bss", I); 261 else 262 SwitchSection(".data", I); 263 break; 264 case GlobalValue::GhostLinkage: 265 std::cerr << "Should not have any unmaterialized functions!\n"; 266 abort(); 267 } 268 269 O << "\t.align " << Align << "\n"; 270 O << "\t.type " << name << ",#object\n"; 271 O << "\t.size " << name << "," << Size << "\n"; 272 O << name << ":\t\t\t\t! "; 273 WriteAsOperand(O, I, true, true, &M); 274 O << " = "; 275 WriteAsOperand(O, C, false, false, &M); 276 O << "\n"; 277 EmitGlobalConstant(C); 278 } 279 } 280 281 AsmPrinter::doFinalization(M); 282 return false; // success 283} 284