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