PPCAsmPrinter.cpp revision 12585baf1a086f7d54e0ac963e3b0e7b6792b80c
1//===-- PPC32AsmPrinter.cpp - Print machine instrs to PowerPC assembly ----===// 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 PowerPC assembly language. This printer is 12// the output mechanism used by `llc'. 13// 14// Documentation at http://developer.apple.com/documentation/DeveloperTools/ 15// Reference/Assembler/ASMIntroduction/chapter_1_section_1.html 16// 17//===----------------------------------------------------------------------===// 18 19#define DEBUG_TYPE "asmprinter" 20#include "PowerPC.h" 21#include "PPC32TargetMachine.h" 22#include "llvm/Constants.h" 23#include "llvm/DerivedTypes.h" 24#include "llvm/Module.h" 25#include "llvm/Assembly/Writer.h" 26#include "llvm/CodeGen/AsmPrinter.h" 27#include "llvm/CodeGen/MachineConstantPool.h" 28#include "llvm/CodeGen/MachineFunctionPass.h" 29#include "llvm/CodeGen/MachineInstr.h" 30#include "llvm/CodeGen/ValueTypes.h" 31#include "llvm/Support/Mangler.h" 32#include "Support/CommandLine.h" 33#include "Support/Debug.h" 34#include "Support/Statistic.h" 35#include "Support/StringExtras.h" 36#include <set> 37using namespace llvm; 38 39namespace { 40 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); 41 42 struct PPC32AsmPrinter : public AsmPrinter { 43 std::set<std::string> FnStubs, GVStubs, LinkOnceStubs; 44 std::set<std::string> Strings; 45 46 PPC32AsmPrinter(std::ostream &O, TargetMachine &TM) 47 : AsmPrinter(O, TM), LabelNumber(0) { 48 CommentString = ";"; 49 GlobalPrefix = "_"; 50 ZeroDirective = "\t.space\t"; // ".space N" emits N zeros. 51 Data64bitsDirective = 0; // we can't emit a 64-bit unit 52 AlignmentIsInBytes = false; // Alignment is by power of 2. 53 } 54 55 /// Unique incrementer for label values for referencing Global values. 56 /// 57 unsigned LabelNumber; 58 59 virtual const char *getPassName() const { 60 return "PPC32 Assembly Printer"; 61 } 62 63 PPC32TargetMachine &getTM() { 64 return static_cast<PPC32TargetMachine&>(TM); 65 } 66 67 /// printInstruction - This method is automatically generated by tablegen 68 /// from the instruction set description. This method returns true if the 69 /// machine instruction was sufficiently described to print it, otherwise it 70 /// returns false. 71 bool printInstruction(const MachineInstr *MI); 72 73 void printMachineInstruction(const MachineInstr *MI); 74 void printOp(const MachineOperand &MO, bool LoadAddrOp = false); 75 void printImmOp(const MachineOperand &MO, unsigned ArgType); 76 77 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){ 78 const MachineOperand &MO = MI->getOperand(OpNo); 79 if (MO.getType() == MachineOperand::MO_MachineRegister) { 80 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??"); 81 O << LowercaseString(TM.getRegisterInfo()->get(MO.getReg()).Name); 82 } else if (MO.isImmediate()) { 83 O << MO.getImmedValue(); 84 } else { 85 printOp(MO); 86 } 87 } 88 89 void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo, 90 MVT::ValueType VT) { 91 unsigned char value = MI->getOperand(OpNo).getImmedValue(); 92 assert(value <= 31 && "Invalid u5imm argument!"); 93 O << (unsigned int)value; 94 } 95 void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo, 96 MVT::ValueType VT) { 97 O << (unsigned short)MI->getOperand(OpNo).getImmedValue(); 98 } 99 100 void printConstantPool(MachineConstantPool *MCP); 101 bool runOnMachineFunction(MachineFunction &F); 102 bool doFinalization(Module &M); 103 }; 104} // end of anonymous namespace 105 106/// createPPC32AsmPrinterPass - Returns a pass that prints the PPC 107/// assembly code for a MachineFunction to the given output stream, 108/// using the given target machine description. This should work 109/// regardless of whether the function is in SSA form or not. 110/// 111FunctionPass *llvm::createPPC32AsmPrinter(std::ostream &o, TargetMachine &tm) { 112 return new PPC32AsmPrinter(o, tm); 113} 114 115// Include the auto-generated portion of the assembly writer 116#include "PowerPCGenAsmWriter.inc" 117 118/// printConstantPool - Print to the current output stream assembly 119/// representations of the constants in the constant pool MCP. This is 120/// used to print out constants which have been "spilled to memory" by 121/// the code generator. 122/// 123void PPC32AsmPrinter::printConstantPool(MachineConstantPool *MCP) { 124 const std::vector<Constant*> &CP = MCP->getConstants(); 125 const TargetData &TD = TM.getTargetData(); 126 127 if (CP.empty()) return; 128 129 for (unsigned i = 0, e = CP.size(); i != e; ++i) { 130 O << "\t.const\n"; 131 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType())); 132 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString 133 << *CP[i] << "\n"; 134 emitGlobalConstant(CP[i]); 135 } 136} 137 138/// runOnMachineFunction - This uses the printMachineInstruction() 139/// method to print assembly for each instruction. 140/// 141bool PPC32AsmPrinter::runOnMachineFunction(MachineFunction &MF) { 142 setupMachineFunction(MF); 143 O << "\n\n"; 144 145 // Print out constants referenced by the function 146 printConstantPool(MF.getConstantPool()); 147 148 // Print out labels for the function. 149 O << "\t.text\n"; 150 emitAlignment(2); 151 O << "\t.globl\t" << CurrentFnName << "\n"; 152 O << CurrentFnName << ":\n"; 153 154 // Print out code for the function. 155 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); 156 I != E; ++I) { 157 // Print a label for the basic block. 158 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t" 159 << CommentString << " " << I->getBasicBlock()->getName() << "\n"; 160 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); 161 II != E; ++II) { 162 // Print the assembly for the instruction. 163 O << "\t"; 164 printMachineInstruction(II); 165 } 166 } 167 ++LabelNumber; 168 169 // We didn't modify anything. 170 return false; 171} 172 173void PPC32AsmPrinter::printOp(const MachineOperand &MO, 174 bool LoadAddrOp /* = false */) { 175 const MRegisterInfo &RI = *TM.getRegisterInfo(); 176 int new_symbol; 177 178 switch (MO.getType()) { 179 case MachineOperand::MO_VirtualRegister: 180 if (Value *V = MO.getVRegValueOrNull()) { 181 O << "<" << V->getName() << ">"; 182 return; 183 } 184 // FALLTHROUGH 185 case MachineOperand::MO_MachineRegister: 186 case MachineOperand::MO_CCRegister: 187 O << LowercaseString(RI.get(MO.getReg()).Name); 188 return; 189 190 case MachineOperand::MO_SignExtendedImmed: 191 case MachineOperand::MO_UnextendedImmed: 192 std::cerr << "printOp() does not handle immediate values\n"; 193 abort(); 194 return; 195 196 case MachineOperand::MO_PCRelativeDisp: 197 std::cerr << "Shouldn't use addPCDisp() when building PPC MachineInstrs"; 198 abort(); 199 return; 200 201 case MachineOperand::MO_MachineBasicBlock: { 202 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); 203 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) 204 << "_" << MBBOp->getNumber() << "\t; " 205 << MBBOp->getBasicBlock()->getName(); 206 return; 207 } 208 209 case MachineOperand::MO_ConstantPoolIndex: 210 O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); 211 return; 212 213 case MachineOperand::MO_ExternalSymbol: 214 O << MO.getSymbolName(); 215 return; 216 217 case MachineOperand::MO_GlobalAddress: { 218 GlobalValue *GV = MO.getGlobal(); 219 std::string Name = Mang->getValueName(GV); 220 221 // Dynamically-resolved functions need a stub for the function. Be 222 // wary however not to output $stub for external functions whose addresses 223 // are taken. Those should be emitted as $non_lazy_ptr below. 224 Function *F = dyn_cast<Function>(GV); 225 if (F && F->isExternal() && !LoadAddrOp && 226 getTM().CalledFunctions.count(F)) { 227 FnStubs.insert(Name); 228 O << "L" << Name << "$stub"; 229 return; 230 } 231 232 // External global variables need a non-lazily-resolved stub 233 if (GV->isExternal() && getTM().AddressTaken.count(GV)) { 234 GVStubs.insert(Name); 235 O << "L" << Name << "$non_lazy_ptr"; 236 return; 237 } 238 239 if (F && LoadAddrOp && getTM().AddressTaken.count(GV)) { 240 LinkOnceStubs.insert(Name); 241 O << "L" << Name << "$non_lazy_ptr"; 242 return; 243 } 244 245 O << Mang->getValueName(GV); 246 return; 247 } 248 249 default: 250 O << "<unknown operand type: " << MO.getType() << ">"; 251 return; 252 } 253} 254 255void PPC32AsmPrinter::printImmOp(const MachineOperand &MO, unsigned ArgType) { 256 int Imm = MO.getImmedValue(); 257 if (ArgType == PPCII::Simm16 || ArgType == PPCII::Disimm16) { 258 O << (short)Imm; 259 } else { 260 O << Imm; 261 } 262} 263 264/// printMachineInstruction -- Print out a single PowerPC MI in Darwin syntax to 265/// the current output stream. 266/// 267void PPC32AsmPrinter::printMachineInstruction(const MachineInstr *MI) { 268 ++EmittedInsts; 269 if (printInstruction(MI)) 270 return; // Printer was automatically generated 271 272 unsigned Opcode = MI->getOpcode(); 273 const TargetInstrInfo &TII = *TM.getInstrInfo(); 274 const TargetInstrDescriptor &Desc = TII.get(Opcode); 275 unsigned i; 276 277 unsigned ArgCount = MI->getNumOperands(); 278 unsigned ArgType[] = { 279 (Desc.TSFlags >> PPCII::Arg0TypeShift) & PPCII::ArgTypeMask, 280 (Desc.TSFlags >> PPCII::Arg1TypeShift) & PPCII::ArgTypeMask, 281 (Desc.TSFlags >> PPCII::Arg2TypeShift) & PPCII::ArgTypeMask, 282 (Desc.TSFlags >> PPCII::Arg3TypeShift) & PPCII::ArgTypeMask, 283 (Desc.TSFlags >> PPCII::Arg4TypeShift) & PPCII::ArgTypeMask 284 }; 285 assert(((Desc.TSFlags & PPCII::VMX) == 0) && 286 "Instruction requires VMX support"); 287 assert(((Desc.TSFlags & PPCII::PPC64) == 0) && 288 "Instruction requires 64 bit support"); 289 290 // CALLpcrel and CALLindirect are handled specially here to print only the 291 // appropriate number of args that the assembler expects. This is because 292 // may have many arguments appended to record the uses of registers that are 293 // holding arguments to the called function. 294 if (Opcode == PPC::COND_BRANCH) { 295 std::cerr << "Error: untranslated conditional branch psuedo instruction!\n"; 296 abort(); 297 } else if (Opcode == PPC::IMPLICIT_DEF) { 298 --EmittedInsts; // Not an actual machine instruction 299 O << "; IMPLICIT DEF "; 300 printOp(MI->getOperand(0)); 301 O << "\n"; 302 return; 303 } else if (Opcode == PPC::CALLpcrel) { 304 O << TII.getName(Opcode) << " "; 305 printOp(MI->getOperand(0)); 306 O << "\n"; 307 return; 308 } else if (Opcode == PPC::CALLindirect) { 309 O << TII.getName(Opcode) << " "; 310 printImmOp(MI->getOperand(0), ArgType[0]); 311 O << ", "; 312 printImmOp(MI->getOperand(1), ArgType[0]); 313 O << "\n"; 314 return; 315 } else if (Opcode == PPC::MovePCtoLR) { 316 ++EmittedInsts; // Actually two machine instructions 317 // FIXME: should probably be converted to cout.width and cout.fill 318 O << "bl \"L0000" << LabelNumber << "$pb\"\n"; 319 O << "\"L0000" << LabelNumber << "$pb\":\n"; 320 O << "\tmflr "; 321 printOp(MI->getOperand(0)); 322 O << "\n"; 323 return; 324 } 325 326 O << TII.getName(Opcode) << " "; 327 if (Opcode == PPC::LOADHiAddr) { 328 printOp(MI->getOperand(0)); 329 O << ", "; 330 if (MI->getOperand(1).getReg() == PPC::R0) 331 O << "0"; 332 else 333 printOp(MI->getOperand(1)); 334 O << ", ha16(" ; 335 printOp(MI->getOperand(2), true /* LoadAddrOp */); 336 O << "-\"L0000" << LabelNumber << "$pb\")\n"; 337 } else if (ArgCount == 3 && (MI->getOperand(2).isConstantPoolIndex() 338 || MI->getOperand(2).isGlobalAddress())) { 339 printOp(MI->getOperand(0)); 340 O << ", lo16("; 341 printOp(MI->getOperand(2), true /* LoadAddrOp */); 342 O << "-\"L0000" << LabelNumber << "$pb\")"; 343 O << "("; 344 if (MI->getOperand(1).getReg() == PPC::R0) 345 O << "0"; 346 else 347 printOp(MI->getOperand(1)); 348 O << ")\n"; 349 } else if (ArgCount == 3 && ArgType[1] == PPCII::Disimm16) { 350 printOp(MI->getOperand(0)); 351 O << ", "; 352 printImmOp(MI->getOperand(1), ArgType[1]); 353 O << "("; 354 if (MI->getOperand(2).hasAllocatedReg() && 355 MI->getOperand(2).getReg() == PPC::R0) 356 O << "0"; 357 else 358 printOp(MI->getOperand(2)); 359 O << ")\n"; 360 } else { 361 for (i = 0; i < ArgCount; ++i) { 362 // addi and friends 363 if (i == 1 && ArgCount == 3 && ArgType[2] == PPCII::Simm16 && 364 MI->getOperand(1).hasAllocatedReg() && 365 MI->getOperand(1).getReg() == PPC::R0) { 366 O << "0"; 367 // for long branch support, bc $+8 368 } else if (i == 1 && ArgCount == 2 && MI->getOperand(1).isImmediate() && 369 TII.isBranch(MI->getOpcode())) { 370 O << "$+8"; 371 assert(8 == MI->getOperand(i).getImmedValue() 372 && "branch off PC not to pc+8?"); 373 //printOp(MI->getOperand(i)); 374 } else if (MI->getOperand(i).isImmediate()) { 375 printImmOp(MI->getOperand(i), ArgType[i]); 376 } else { 377 printOp(MI->getOperand(i)); 378 } 379 if (ArgCount - 1 == i) 380 O << "\n"; 381 else 382 O << ", "; 383 } 384 } 385 return; 386} 387 388// SwitchSection - Switch to the specified section of the executable if we are 389// not already in it! 390// 391static void SwitchSection(std::ostream &OS, std::string &CurSection, 392 const char *NewSection) { 393 if (CurSection != NewSection) { 394 CurSection = NewSection; 395 if (!CurSection.empty()) 396 OS << "\t" << NewSection << "\n"; 397 } 398} 399 400bool PPC32AsmPrinter::doFinalization(Module &M) { 401 const TargetData &TD = TM.getTargetData(); 402 std::string CurSection; 403 404 // Print out module-level global variables here. 405 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) 406 if (I->hasInitializer()) { // External global require no code 407 O << "\n\n"; 408 std::string name = Mang->getValueName(I); 409 Constant *C = I->getInitializer(); 410 unsigned Size = TD.getTypeSize(C->getType()); 411 unsigned Align = TD.getTypeAlignmentShift(C->getType()); 412 413 if (C->isNullValue() && /* FIXME: Verify correct */ 414 (I->hasInternalLinkage() || I->hasWeakLinkage())) { 415 SwitchSection(O, CurSection, ".data"); 416 if (I->hasInternalLinkage()) 417 O << ".lcomm " << name << "," << TD.getTypeSize(C->getType()) 418 << "," << Align; 419 else 420 O << ".comm " << name << "," << TD.getTypeSize(C->getType()); 421 O << "\t\t; "; 422 WriteAsOperand(O, I, true, true, &M); 423 O << "\n"; 424 } else { 425 switch (I->getLinkage()) { 426 case GlobalValue::LinkOnceLinkage: 427 O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n" 428 << ".weak_definition " << name << '\n' 429 << ".private_extern " << name << '\n' 430 << ".section __DATA,__datacoal_nt,coalesced,no_toc\n"; 431 LinkOnceStubs.insert(name); 432 break; 433 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. 434 // Nonnull linkonce -> weak 435 O << "\t.weak " << name << "\n"; 436 SwitchSection(O, CurSection, ""); 437 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; 438 break; 439 case GlobalValue::AppendingLinkage: 440 // FIXME: appending linkage variables should go into a section of 441 // their name or something. For now, just emit them as external. 442 case GlobalValue::ExternalLinkage: 443 // If external or appending, declare as a global symbol 444 O << "\t.globl " << name << "\n"; 445 // FALL THROUGH 446 case GlobalValue::InternalLinkage: 447 SwitchSection(O, CurSection, ".data"); 448 break; 449 } 450 451 emitAlignment(Align); 452 O << name << ":\t\t\t\t; "; 453 WriteAsOperand(O, I, true, true, &M); 454 O << " = "; 455 WriteAsOperand(O, C, false, false, &M); 456 O << "\n"; 457 emitGlobalConstant(C); 458 } 459 } 460 461 // Output stubs for dynamically-linked functions 462 for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end(); 463 i != e; ++i) 464 { 465 O << ".data\n"; 466 O << ".section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n"; 467 emitAlignment(2); 468 O << "L" << *i << "$stub:\n"; 469 O << "\t.indirect_symbol " << *i << "\n"; 470 O << "\tmflr r0\n"; 471 O << "\tbcl 20,31,L0$" << *i << "\n"; 472 O << "L0$" << *i << ":\n"; 473 O << "\tmflr r11\n"; 474 O << "\taddis r11,r11,ha16(L" << *i << "$lazy_ptr-L0$" << *i << ")\n"; 475 O << "\tmtlr r0\n"; 476 O << "\tlwzu r12,lo16(L" << *i << "$lazy_ptr-L0$" << *i << ")(r11)\n"; 477 O << "\tmtctr r12\n"; 478 O << "\tbctr\n"; 479 O << ".data\n"; 480 O << ".lazy_symbol_pointer\n"; 481 O << "L" << *i << "$lazy_ptr:\n"; 482 O << "\t.indirect_symbol " << *i << "\n"; 483 O << "\t.long dyld_stub_binding_helper\n"; 484 } 485 486 O << "\n"; 487 488 // Output stubs for external global variables 489 if (GVStubs.begin() != GVStubs.end()) 490 O << ".data\n.non_lazy_symbol_pointer\n"; 491 for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end(); 492 i != e; ++i) { 493 O << "L" << *i << "$non_lazy_ptr:\n"; 494 O << "\t.indirect_symbol " << *i << "\n"; 495 O << "\t.long\t0\n"; 496 } 497 498 // Output stubs for link-once variables 499 if (LinkOnceStubs.begin() != LinkOnceStubs.end()) 500 O << ".data\n.align 2\n"; 501 for (std::set<std::string>::iterator i = LinkOnceStubs.begin(), 502 e = LinkOnceStubs.end(); i != e; ++i) { 503 O << "L" << *i << "$non_lazy_ptr:\n" 504 << "\t.long\t" << *i << '\n'; 505 } 506 507 AsmPrinter::doFinalization(M); 508 return false; // success 509} 510