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