1 //===-- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer -------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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 MIPS assembly language. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "InstPrinter/MipsInstPrinter.h" 16 #include "MCTargetDesc/MipsBaseInfo.h" 17 #include "MCTargetDesc/MipsMCNaCl.h" 18 #include "Mips.h" 19 #include "MipsAsmPrinter.h" 20 #include "MipsInstrInfo.h" 21 #include "MipsMCInstLower.h" 22 #include "MipsTargetStreamer.h" 23 #include "llvm/ADT/SmallString.h" 24 #include "llvm/ADT/StringExtras.h" 25 #include "llvm/ADT/Twine.h" 26 #include "llvm/CodeGen/MachineConstantPool.h" 27 #include "llvm/CodeGen/MachineFrameInfo.h" 28 #include "llvm/CodeGen/MachineFunctionPass.h" 29 #include "llvm/CodeGen/MachineInstr.h" 30 #include "llvm/CodeGen/MachineJumpTableInfo.h" 31 #include "llvm/CodeGen/MachineMemOperand.h" 32 #include "llvm/IR/BasicBlock.h" 33 #include "llvm/IR/DataLayout.h" 34 #include "llvm/IR/InlineAsm.h" 35 #include "llvm/IR/Instructions.h" 36 #include "llvm/IR/Mangler.h" 37 #include "llvm/MC/MCAsmInfo.h" 38 #include "llvm/MC/MCContext.h" 39 #include "llvm/MC/MCELFStreamer.h" 40 #include "llvm/MC/MCExpr.h" 41 #include "llvm/MC/MCInst.h" 42 #include "llvm/MC/MCSection.h" 43 #include "llvm/MC/MCSectionELF.h" 44 #include "llvm/MC/MCSymbol.h" 45 #include "llvm/Support/ELF.h" 46 #include "llvm/Support/TargetRegistry.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include "llvm/Target/TargetLoweringObjectFile.h" 49 #include "llvm/Target/TargetOptions.h" 50 #include <string> 51 52 using namespace llvm; 53 54 #define DEBUG_TYPE "mips-asm-printer" 55 56 MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() { 57 return static_cast<MipsTargetStreamer &>(*OutStreamer.getTargetStreamer()); 58 } 59 60 bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) { 61 // Initialize TargetLoweringObjectFile. 62 if (Subtarget->allowMixed16_32()) 63 const_cast<TargetLoweringObjectFile&>(getObjFileLowering()) 64 .Initialize(OutContext, TM); 65 MipsFI = MF.getInfo<MipsFunctionInfo>(); 66 if (Subtarget->inMips16Mode()) 67 for (std::map< 68 const char *, 69 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator 70 it = MipsFI->StubsNeeded.begin(); 71 it != MipsFI->StubsNeeded.end(); ++it) { 72 const char *Symbol = it->first; 73 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second; 74 if (StubsNeeded.find(Symbol) == StubsNeeded.end()) 75 StubsNeeded[Symbol] = Signature; 76 } 77 MCP = MF.getConstantPool(); 78 79 // In NaCl, all indirect jump targets must be aligned to bundle size. 80 if (Subtarget->isTargetNaCl()) 81 NaClAlignIndirectJumpTargets(MF); 82 83 AsmPrinter::runOnMachineFunction(MF); 84 return true; 85 } 86 87 bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) { 88 MCOp = MCInstLowering.LowerOperand(MO); 89 return MCOp.isValid(); 90 } 91 92 #include "MipsGenMCPseudoLowering.inc" 93 94 void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) { 95 if (MI->isDebugValue()) { 96 SmallString<128> Str; 97 raw_svector_ostream OS(Str); 98 99 PrintDebugValueComment(MI, OS); 100 return; 101 } 102 103 // If we just ended a constant pool, mark it as such. 104 if (InConstantPool && MI->getOpcode() != Mips::CONSTPOOL_ENTRY) { 105 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 106 InConstantPool = false; 107 } 108 if (MI->getOpcode() == Mips::CONSTPOOL_ENTRY) { 109 // CONSTPOOL_ENTRY - This instruction represents a floating 110 //constant pool in the function. The first operand is the ID# 111 // for this instruction, the second is the index into the 112 // MachineConstantPool that this is, the third is the size in 113 // bytes of this constant pool entry. 114 // The required alignment is specified on the basic block holding this MI. 115 // 116 unsigned LabelId = (unsigned)MI->getOperand(0).getImm(); 117 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex(); 118 119 // If this is the first entry of the pool, mark it. 120 if (!InConstantPool) { 121 OutStreamer.EmitDataRegion(MCDR_DataRegion); 122 InConstantPool = true; 123 } 124 125 OutStreamer.EmitLabel(GetCPISymbol(LabelId)); 126 127 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx]; 128 if (MCPE.isMachineConstantPoolEntry()) 129 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal); 130 else 131 EmitGlobalConstant(MCPE.Val.ConstVal); 132 return; 133 } 134 135 136 MachineBasicBlock::const_instr_iterator I = MI; 137 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end(); 138 139 do { 140 // Do any auto-generated pseudo lowerings. 141 if (emitPseudoExpansionLowering(OutStreamer, &*I)) 142 continue; 143 144 // The inMips16Mode() test is not permanent. 145 // Some instructions are marked as pseudo right now which 146 // would make the test fail for the wrong reason but 147 // that will be fixed soon. We need this here because we are 148 // removing another test for this situation downstream in the 149 // callchain. 150 // 151 if (I->isPseudo() && !Subtarget->inMips16Mode() 152 && !isLongBranchPseudo(I->getOpcode())) 153 llvm_unreachable("Pseudo opcode found in EmitInstruction()"); 154 155 MCInst TmpInst0; 156 MCInstLowering.Lower(I, TmpInst0); 157 EmitToStreamer(OutStreamer, TmpInst0); 158 } while ((++I != E) && I->isInsideBundle()); // Delay slot check 159 } 160 161 //===----------------------------------------------------------------------===// 162 // 163 // Mips Asm Directives 164 // 165 // -- Frame directive "frame Stackpointer, Stacksize, RARegister" 166 // Describe the stack frame. 167 // 168 // -- Mask directives "(f)mask bitmask, offset" 169 // Tells the assembler which registers are saved and where. 170 // bitmask - contain a little endian bitset indicating which registers are 171 // saved on function prologue (e.g. with a 0x80000000 mask, the 172 // assembler knows the register 31 (RA) is saved at prologue. 173 // offset - the position before stack pointer subtraction indicating where 174 // the first saved register on prologue is located. (e.g. with a 175 // 176 // Consider the following function prologue: 177 // 178 // .frame $fp,48,$ra 179 // .mask 0xc0000000,-8 180 // addiu $sp, $sp, -48 181 // sw $ra, 40($sp) 182 // sw $fp, 36($sp) 183 // 184 // With a 0xc0000000 mask, the assembler knows the register 31 (RA) and 185 // 30 (FP) are saved at prologue. As the save order on prologue is from 186 // left to right, RA is saved first. A -8 offset means that after the 187 // stack pointer subtration, the first register in the mask (RA) will be 188 // saved at address 48-8=40. 189 // 190 //===----------------------------------------------------------------------===// 191 192 //===----------------------------------------------------------------------===// 193 // Mask directives 194 //===----------------------------------------------------------------------===// 195 196 // Create a bitmask with all callee saved registers for CPU or Floating Point 197 // registers. For CPU registers consider RA, GP and FP for saving if necessary. 198 void MipsAsmPrinter::printSavedRegsBitmask() { 199 // CPU and FPU Saved Registers Bitmasks 200 unsigned CPUBitmask = 0, FPUBitmask = 0; 201 int CPUTopSavedRegOff, FPUTopSavedRegOff; 202 203 // Set the CPU and FPU Bitmasks 204 const MachineFrameInfo *MFI = MF->getFrameInfo(); 205 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); 206 // size of stack area to which FP callee-saved regs are saved. 207 unsigned CPURegSize = Mips::GPR32RegClass.getSize(); 208 unsigned FGR32RegSize = Mips::FGR32RegClass.getSize(); 209 unsigned AFGR64RegSize = Mips::AFGR64RegClass.getSize(); 210 bool HasAFGR64Reg = false; 211 unsigned CSFPRegsSize = 0; 212 unsigned i, e = CSI.size(); 213 214 // Set FPU Bitmask. 215 for (i = 0; i != e; ++i) { 216 unsigned Reg = CSI[i].getReg(); 217 if (Mips::GPR32RegClass.contains(Reg)) 218 break; 219 220 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg); 221 if (Mips::AFGR64RegClass.contains(Reg)) { 222 FPUBitmask |= (3 << RegNum); 223 CSFPRegsSize += AFGR64RegSize; 224 HasAFGR64Reg = true; 225 continue; 226 } 227 228 FPUBitmask |= (1 << RegNum); 229 CSFPRegsSize += FGR32RegSize; 230 } 231 232 // Set CPU Bitmask. 233 for (; i != e; ++i) { 234 unsigned Reg = CSI[i].getReg(); 235 unsigned RegNum = TM.getRegisterInfo()->getEncodingValue(Reg); 236 CPUBitmask |= (1 << RegNum); 237 } 238 239 // FP Regs are saved right below where the virtual frame pointer points to. 240 FPUTopSavedRegOff = FPUBitmask ? 241 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0; 242 243 // CPU Regs are saved below FP Regs. 244 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0; 245 246 MipsTargetStreamer &TS = getTargetStreamer(); 247 // Print CPUBitmask 248 TS.emitMask(CPUBitmask, CPUTopSavedRegOff); 249 250 // Print FPUBitmask 251 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff); 252 } 253 254 //===----------------------------------------------------------------------===// 255 // Frame and Set directives 256 //===----------------------------------------------------------------------===// 257 258 /// Frame Directive 259 void MipsAsmPrinter::emitFrameDirective() { 260 const TargetRegisterInfo &RI = *TM.getRegisterInfo(); 261 262 unsigned stackReg = RI.getFrameRegister(*MF); 263 unsigned returnReg = RI.getRARegister(); 264 unsigned stackSize = MF->getFrameInfo()->getStackSize(); 265 266 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg); 267 } 268 269 /// Emit Set directives. 270 const char *MipsAsmPrinter::getCurrentABIString() const { 271 switch (Subtarget->getTargetABI()) { 272 case MipsSubtarget::O32: return "abi32"; 273 case MipsSubtarget::N32: return "abiN32"; 274 case MipsSubtarget::N64: return "abi64"; 275 case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64 276 default: llvm_unreachable("Unknown Mips ABI"); 277 } 278 } 279 280 void MipsAsmPrinter::EmitFunctionEntryLabel() { 281 MipsTargetStreamer &TS = getTargetStreamer(); 282 283 // NaCl sandboxing requires that indirect call instructions are masked. 284 // This means that function entry points should be bundle-aligned. 285 if (Subtarget->isTargetNaCl()) 286 EmitAlignment(std::max(MF->getAlignment(), MIPS_NACL_BUNDLE_ALIGN)); 287 288 if (Subtarget->inMicroMipsMode()) 289 TS.emitDirectiveSetMicroMips(); 290 else 291 TS.emitDirectiveSetNoMicroMips(); 292 293 if (Subtarget->inMips16Mode()) 294 TS.emitDirectiveSetMips16(); 295 else 296 TS.emitDirectiveSetNoMips16(); 297 298 TS.emitDirectiveEnt(*CurrentFnSym); 299 OutStreamer.EmitLabel(CurrentFnSym); 300 } 301 302 /// EmitFunctionBodyStart - Targets can override this to emit stuff before 303 /// the first basic block in the function. 304 void MipsAsmPrinter::EmitFunctionBodyStart() { 305 MipsTargetStreamer &TS = getTargetStreamer(); 306 307 MCInstLowering.Initialize(&MF->getContext()); 308 309 bool IsNakedFunction = 310 MF->getFunction()-> 311 getAttributes().hasAttribute(AttributeSet::FunctionIndex, 312 Attribute::Naked); 313 if (!IsNakedFunction) 314 emitFrameDirective(); 315 316 if (!IsNakedFunction) 317 printSavedRegsBitmask(); 318 319 if (!Subtarget->inMips16Mode()) { 320 TS.emitDirectiveSetNoReorder(); 321 TS.emitDirectiveSetNoMacro(); 322 TS.emitDirectiveSetNoAt(); 323 } 324 } 325 326 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after 327 /// the last basic block in the function. 328 void MipsAsmPrinter::EmitFunctionBodyEnd() { 329 MipsTargetStreamer &TS = getTargetStreamer(); 330 331 // There are instruction for this macros, but they must 332 // always be at the function end, and we can't emit and 333 // break with BB logic. 334 if (!Subtarget->inMips16Mode()) { 335 TS.emitDirectiveSetAt(); 336 TS.emitDirectiveSetMacro(); 337 TS.emitDirectiveSetReorder(); 338 } 339 TS.emitDirectiveEnd(CurrentFnSym->getName()); 340 // Make sure to terminate any constant pools that were at the end 341 // of the function. 342 if (!InConstantPool) 343 return; 344 InConstantPool = false; 345 OutStreamer.EmitDataRegion(MCDR_DataRegionEnd); 346 } 347 348 /// isBlockOnlyReachableByFallthough - Return true if the basic block has 349 /// exactly one predecessor and the control transfer mechanism between 350 /// the predecessor and this block is a fall-through. 351 bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock* 352 MBB) const { 353 // The predecessor has to be immediately before this block. 354 const MachineBasicBlock *Pred = *MBB->pred_begin(); 355 356 // If the predecessor is a switch statement, assume a jump table 357 // implementation, so it is not a fall through. 358 if (const BasicBlock *bb = Pred->getBasicBlock()) 359 if (isa<SwitchInst>(bb->getTerminator())) 360 return false; 361 362 // If this is a landing pad, it isn't a fall through. If it has no preds, 363 // then nothing falls through to it. 364 if (MBB->isLandingPad() || MBB->pred_empty()) 365 return false; 366 367 // If there isn't exactly one predecessor, it can't be a fall through. 368 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI; 369 ++PI2; 370 371 if (PI2 != MBB->pred_end()) 372 return false; 373 374 // The predecessor has to be immediately before this block. 375 if (!Pred->isLayoutSuccessor(MBB)) 376 return false; 377 378 // If the block is completely empty, then it definitely does fall through. 379 if (Pred->empty()) 380 return true; 381 382 // Otherwise, check the last instruction. 383 // Check if the last terminator is an unconditional branch. 384 MachineBasicBlock::const_iterator I = Pred->end(); 385 while (I != Pred->begin() && !(--I)->isTerminator()) ; 386 387 return !I->isBarrier(); 388 } 389 390 // Print out an operand for an inline asm expression. 391 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 392 unsigned AsmVariant,const char *ExtraCode, 393 raw_ostream &O) { 394 // Does this asm operand have a single letter operand modifier? 395 if (ExtraCode && ExtraCode[0]) { 396 if (ExtraCode[1] != 0) return true; // Unknown modifier. 397 398 const MachineOperand &MO = MI->getOperand(OpNum); 399 switch (ExtraCode[0]) { 400 default: 401 // See if this is a generic print operand 402 return AsmPrinter::PrintAsmOperand(MI,OpNum,AsmVariant,ExtraCode,O); 403 case 'X': // hex const int 404 if ((MO.getType()) != MachineOperand::MO_Immediate) 405 return true; 406 O << "0x" << StringRef(utohexstr(MO.getImm())).lower(); 407 return false; 408 case 'x': // hex const int (low 16 bits) 409 if ((MO.getType()) != MachineOperand::MO_Immediate) 410 return true; 411 O << "0x" << StringRef(utohexstr(MO.getImm() & 0xffff)).lower(); 412 return false; 413 case 'd': // decimal const int 414 if ((MO.getType()) != MachineOperand::MO_Immediate) 415 return true; 416 O << MO.getImm(); 417 return false; 418 case 'm': // decimal const int minus 1 419 if ((MO.getType()) != MachineOperand::MO_Immediate) 420 return true; 421 O << MO.getImm() - 1; 422 return false; 423 case 'z': { 424 // $0 if zero, regular printing otherwise 425 if (MO.getType() != MachineOperand::MO_Immediate) 426 return true; 427 int64_t Val = MO.getImm(); 428 if (Val) 429 O << Val; 430 else 431 O << "$0"; 432 return false; 433 } 434 case 'D': // Second part of a double word register operand 435 case 'L': // Low order register of a double word register operand 436 case 'M': // High order register of a double word register operand 437 { 438 if (OpNum == 0) 439 return true; 440 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1); 441 if (!FlagsOP.isImm()) 442 return true; 443 unsigned Flags = FlagsOP.getImm(); 444 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags); 445 // Number of registers represented by this operand. We are looking 446 // for 2 for 32 bit mode and 1 for 64 bit mode. 447 if (NumVals != 2) { 448 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) { 449 unsigned Reg = MO.getReg(); 450 O << '$' << MipsInstPrinter::getRegisterName(Reg); 451 return false; 452 } 453 return true; 454 } 455 456 unsigned RegOp = OpNum; 457 if (!Subtarget->isGP64bit()){ 458 // Endianess reverses which register holds the high or low value 459 // between M and L. 460 switch(ExtraCode[0]) { 461 case 'M': 462 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum; 463 break; 464 case 'L': 465 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1; 466 break; 467 case 'D': // Always the second part 468 RegOp = OpNum + 1; 469 } 470 if (RegOp >= MI->getNumOperands()) 471 return true; 472 const MachineOperand &MO = MI->getOperand(RegOp); 473 if (!MO.isReg()) 474 return true; 475 unsigned Reg = MO.getReg(); 476 O << '$' << MipsInstPrinter::getRegisterName(Reg); 477 return false; 478 } 479 } 480 case 'w': 481 // Print MSA registers for the 'f' constraint 482 // In LLVM, the 'w' modifier doesn't need to do anything. 483 // We can just call printOperand as normal. 484 break; 485 } 486 } 487 488 printOperand(MI, OpNum, O); 489 return false; 490 } 491 492 bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 493 unsigned OpNum, unsigned AsmVariant, 494 const char *ExtraCode, 495 raw_ostream &O) { 496 int Offset = 0; 497 // Currently we are expecting either no ExtraCode or 'D' 498 if (ExtraCode) { 499 if (ExtraCode[0] == 'D') 500 Offset = 4; 501 else 502 return true; // Unknown modifier. 503 } 504 505 const MachineOperand &MO = MI->getOperand(OpNum); 506 assert(MO.isReg() && "unexpected inline asm memory operand"); 507 O << Offset << "($" << MipsInstPrinter::getRegisterName(MO.getReg()) << ")"; 508 509 return false; 510 } 511 512 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum, 513 raw_ostream &O) { 514 const DataLayout *DL = TM.getDataLayout(); 515 const MachineOperand &MO = MI->getOperand(opNum); 516 bool closeP = false; 517 518 if (MO.getTargetFlags()) 519 closeP = true; 520 521 switch(MO.getTargetFlags()) { 522 case MipsII::MO_GPREL: O << "%gp_rel("; break; 523 case MipsII::MO_GOT_CALL: O << "%call16("; break; 524 case MipsII::MO_GOT: O << "%got("; break; 525 case MipsII::MO_ABS_HI: O << "%hi("; break; 526 case MipsII::MO_ABS_LO: O << "%lo("; break; 527 case MipsII::MO_TLSGD: O << "%tlsgd("; break; 528 case MipsII::MO_GOTTPREL: O << "%gottprel("; break; 529 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break; 530 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break; 531 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break; 532 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break; 533 case MipsII::MO_GOT_DISP: O << "%got_disp("; break; 534 case MipsII::MO_GOT_PAGE: O << "%got_page("; break; 535 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break; 536 } 537 538 switch (MO.getType()) { 539 case MachineOperand::MO_Register: 540 O << '$' 541 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower(); 542 break; 543 544 case MachineOperand::MO_Immediate: 545 O << MO.getImm(); 546 break; 547 548 case MachineOperand::MO_MachineBasicBlock: 549 O << *MO.getMBB()->getSymbol(); 550 return; 551 552 case MachineOperand::MO_GlobalAddress: 553 O << *getSymbol(MO.getGlobal()); 554 break; 555 556 case MachineOperand::MO_BlockAddress: { 557 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress()); 558 O << BA->getName(); 559 break; 560 } 561 562 case MachineOperand::MO_ConstantPoolIndex: 563 O << DL->getPrivateGlobalPrefix() << "CPI" 564 << getFunctionNumber() << "_" << MO.getIndex(); 565 if (MO.getOffset()) 566 O << "+" << MO.getOffset(); 567 break; 568 569 default: 570 llvm_unreachable("<unknown operand type>"); 571 } 572 573 if (closeP) O << ")"; 574 } 575 576 void MipsAsmPrinter::printUnsignedImm(const MachineInstr *MI, int opNum, 577 raw_ostream &O) { 578 const MachineOperand &MO = MI->getOperand(opNum); 579 if (MO.isImm()) 580 O << (unsigned short int)MO.getImm(); 581 else 582 printOperand(MI, opNum, O); 583 } 584 585 void MipsAsmPrinter::printUnsignedImm8(const MachineInstr *MI, int opNum, 586 raw_ostream &O) { 587 const MachineOperand &MO = MI->getOperand(opNum); 588 if (MO.isImm()) 589 O << (unsigned short int)(unsigned char)MO.getImm(); 590 else 591 printOperand(MI, opNum, O); 592 } 593 594 void MipsAsmPrinter:: 595 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) { 596 // Load/Store memory operands -- imm($reg) 597 // If PIC target the target is loaded as the 598 // pattern lw $25,%call16($28) 599 printOperand(MI, opNum+1, O); 600 O << "("; 601 printOperand(MI, opNum, O); 602 O << ")"; 603 } 604 605 void MipsAsmPrinter:: 606 printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) { 607 // when using stack locations for not load/store instructions 608 // print the same way as all normal 3 operand instructions. 609 printOperand(MI, opNum, O); 610 O << ", "; 611 printOperand(MI, opNum+1, O); 612 return; 613 } 614 615 void MipsAsmPrinter:: 616 printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O, 617 const char *Modifier) { 618 const MachineOperand &MO = MI->getOperand(opNum); 619 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm()); 620 } 621 622 void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) { 623 // TODO: Need to add -mabicalls and -mno-abicalls flags. 624 // Currently we assume that -mabicalls is the default. 625 bool IsABICalls = true; 626 if (IsABICalls) { 627 getTargetStreamer().emitDirectiveAbiCalls(); 628 Reloc::Model RM = Subtarget->getRelocationModel(); 629 // FIXME: This condition should be a lot more complicated that it is here. 630 // Ideally it should test for properties of the ABI and not the ABI 631 // itself. 632 // For the moment, I'm only correcting enough to make MIPS-IV work. 633 if (RM == Reloc::Static && !Subtarget->isABI_N64()) 634 getTargetStreamer().emitDirectiveOptionPic0(); 635 } 636 637 // Tell the assembler which ABI we are using 638 std::string SectionName = std::string(".mdebug.") + getCurrentABIString(); 639 OutStreamer.SwitchSection(OutContext.getELFSection( 640 SectionName, ELF::SHT_PROGBITS, 0, SectionKind::getDataRel())); 641 642 // NaN: At the moment we only support: 643 // 1. .nan legacy (default) 644 // 2. .nan 2008 645 Subtarget->isNaN2008() ? getTargetStreamer().emitDirectiveNaN2008() 646 : getTargetStreamer().emitDirectiveNaNLegacy(); 647 648 // TODO: handle O64 ABI 649 650 if (Subtarget->isABI_EABI()) { 651 if (Subtarget->isGP32bit()) 652 OutStreamer.SwitchSection( 653 OutContext.getELFSection(".gcc_compiled_long32", ELF::SHT_PROGBITS, 0, 654 SectionKind::getDataRel())); 655 else 656 OutStreamer.SwitchSection( 657 OutContext.getELFSection(".gcc_compiled_long64", ELF::SHT_PROGBITS, 0, 658 SectionKind::getDataRel())); 659 } 660 } 661 662 void MipsAsmPrinter::EmitJal(MCSymbol *Symbol) { 663 MCInst I; 664 I.setOpcode(Mips::JAL); 665 I.addOperand( 666 MCOperand::CreateExpr(MCSymbolRefExpr::Create(Symbol, OutContext))); 667 OutStreamer.EmitInstruction(I, getSubtargetInfo()); 668 } 669 670 void MipsAsmPrinter::EmitInstrReg(unsigned Opcode, unsigned Reg) { 671 MCInst I; 672 I.setOpcode(Opcode); 673 I.addOperand(MCOperand::CreateReg(Reg)); 674 OutStreamer.EmitInstruction(I, getSubtargetInfo()); 675 } 676 677 void MipsAsmPrinter::EmitInstrRegReg(unsigned Opcode, unsigned Reg1, 678 unsigned Reg2) { 679 MCInst I; 680 // 681 // Because of the current td files for Mips32, the operands for MTC1 682 // appear backwards from their normal assembly order. It's not a trivial 683 // change to fix this in the td file so we adjust for it here. 684 // 685 if (Opcode == Mips::MTC1) { 686 unsigned Temp = Reg1; 687 Reg1 = Reg2; 688 Reg2 = Temp; 689 } 690 I.setOpcode(Opcode); 691 I.addOperand(MCOperand::CreateReg(Reg1)); 692 I.addOperand(MCOperand::CreateReg(Reg2)); 693 OutStreamer.EmitInstruction(I, getSubtargetInfo()); 694 } 695 696 void MipsAsmPrinter::EmitInstrRegRegReg(unsigned Opcode, unsigned Reg1, 697 unsigned Reg2, unsigned Reg3) { 698 MCInst I; 699 I.setOpcode(Opcode); 700 I.addOperand(MCOperand::CreateReg(Reg1)); 701 I.addOperand(MCOperand::CreateReg(Reg2)); 702 I.addOperand(MCOperand::CreateReg(Reg3)); 703 OutStreamer.EmitInstruction(I, getSubtargetInfo()); 704 } 705 706 void MipsAsmPrinter::EmitMovFPIntPair(unsigned MovOpc, unsigned Reg1, 707 unsigned Reg2, unsigned FPReg1, 708 unsigned FPReg2, bool LE) { 709 if (!LE) { 710 unsigned temp = Reg1; 711 Reg1 = Reg2; 712 Reg2 = temp; 713 } 714 EmitInstrRegReg(MovOpc, Reg1, FPReg1); 715 EmitInstrRegReg(MovOpc, Reg2, FPReg2); 716 } 717 718 void MipsAsmPrinter::EmitSwapFPIntParams(Mips16HardFloatInfo::FPParamVariant PV, 719 bool LE, bool ToFP) { 720 using namespace Mips16HardFloatInfo; 721 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1; 722 switch (PV) { 723 case FSig: 724 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12); 725 break; 726 case FFSig: 727 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE); 728 break; 729 case FDSig: 730 EmitInstrRegReg(MovOpc, Mips::A0, Mips::F12); 731 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE); 732 break; 733 case DSig: 734 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE); 735 break; 736 case DDSig: 737 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE); 738 EmitMovFPIntPair(MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE); 739 break; 740 case DFSig: 741 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE); 742 EmitInstrRegReg(MovOpc, Mips::A2, Mips::F14); 743 break; 744 case NoSig: 745 return; 746 } 747 } 748 749 void 750 MipsAsmPrinter::EmitSwapFPIntRetval(Mips16HardFloatInfo::FPReturnVariant RV, 751 bool LE) { 752 using namespace Mips16HardFloatInfo; 753 unsigned MovOpc = Mips::MFC1; 754 switch (RV) { 755 case FRet: 756 EmitInstrRegReg(MovOpc, Mips::V0, Mips::F0); 757 break; 758 case DRet: 759 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE); 760 break; 761 case CFRet: 762 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE); 763 break; 764 case CDRet: 765 EmitMovFPIntPair(MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE); 766 EmitMovFPIntPair(MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE); 767 break; 768 case NoFPRet: 769 break; 770 } 771 } 772 773 void MipsAsmPrinter::EmitFPCallStub( 774 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) { 775 MCSymbol *MSymbol = OutContext.GetOrCreateSymbol(StringRef(Symbol)); 776 using namespace Mips16HardFloatInfo; 777 bool LE = Subtarget->isLittle(); 778 // 779 // .global xxxx 780 // 781 OutStreamer.EmitSymbolAttribute(MSymbol, MCSA_Global); 782 const char *RetType; 783 // 784 // make the comment field identifying the return and parameter 785 // types of the floating point stub 786 // # Stub function to call rettype xxxx (params) 787 // 788 switch (Signature->RetSig) { 789 case FRet: 790 RetType = "float"; 791 break; 792 case DRet: 793 RetType = "double"; 794 break; 795 case CFRet: 796 RetType = "complex"; 797 break; 798 case CDRet: 799 RetType = "double complex"; 800 break; 801 case NoFPRet: 802 RetType = ""; 803 break; 804 } 805 const char *Parms; 806 switch (Signature->ParamSig) { 807 case FSig: 808 Parms = "float"; 809 break; 810 case FFSig: 811 Parms = "float, float"; 812 break; 813 case FDSig: 814 Parms = "float, double"; 815 break; 816 case DSig: 817 Parms = "double"; 818 break; 819 case DDSig: 820 Parms = "double, double"; 821 break; 822 case DFSig: 823 Parms = "double, float"; 824 break; 825 case NoSig: 826 Parms = ""; 827 break; 828 } 829 OutStreamer.AddComment("\t# Stub function to call " + Twine(RetType) + " " + 830 Twine(Symbol) + " (" + Twine(Parms) + ")"); 831 // 832 // probably not necessary but we save and restore the current section state 833 // 834 OutStreamer.PushSection(); 835 // 836 // .section mips16.call.fpxxxx,"ax",@progbits 837 // 838 const MCSectionELF *M = OutContext.getELFSection( 839 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS, 840 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR, SectionKind::getText()); 841 OutStreamer.SwitchSection(M, nullptr); 842 // 843 // .align 2 844 // 845 OutStreamer.EmitValueToAlignment(4); 846 MipsTargetStreamer &TS = getTargetStreamer(); 847 // 848 // .set nomips16 849 // .set nomicromips 850 // 851 TS.emitDirectiveSetNoMips16(); 852 TS.emitDirectiveSetNoMicroMips(); 853 // 854 // .ent __call_stub_fp_xxxx 855 // .type __call_stub_fp_xxxx,@function 856 // __call_stub_fp_xxxx: 857 // 858 std::string x = "__call_stub_fp_" + std::string(Symbol); 859 MCSymbol *Stub = OutContext.GetOrCreateSymbol(StringRef(x)); 860 TS.emitDirectiveEnt(*Stub); 861 MCSymbol *MType = 862 OutContext.GetOrCreateSymbol("__call_stub_fp_" + Twine(Symbol)); 863 OutStreamer.EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction); 864 OutStreamer.EmitLabel(Stub); 865 // 866 // we just handle non pic for now. these function will not be 867 // called otherwise. when the full stub generation is moved here 868 // we need to deal with pic. 869 // 870 if (Subtarget->getRelocationModel() == Reloc::PIC_) 871 llvm_unreachable("should not be here if we are compiling pic"); 872 TS.emitDirectiveSetReorder(); 873 // 874 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement 875 // stubs without raw text but this current patch is for compiler generated 876 // functions and they all return some value. 877 // The calling sequence for non pic is different in that case and we need 878 // to implement %lo and %hi in order to handle the case of no return value 879 // See the corresponding method in Mips16HardFloat for details. 880 // 881 // mov the return address to S2. 882 // we have no stack space to store it and we are about to make another call. 883 // We need to make sure that the enclosing function knows to save S2 884 // This should have already been handled. 885 // 886 // Mov $18, $31 887 888 EmitInstrRegRegReg(Mips::ADDu, Mips::S2, Mips::RA, Mips::ZERO); 889 890 EmitSwapFPIntParams(Signature->ParamSig, LE, true); 891 892 // Jal xxxx 893 // 894 EmitJal(MSymbol); 895 896 // fix return values 897 EmitSwapFPIntRetval(Signature->RetSig, LE); 898 // 899 // do the return 900 // if (Signature->RetSig == NoFPRet) 901 // llvm_unreachable("should not be any stubs here with no return value"); 902 // else 903 EmitInstrReg(Mips::JR, Mips::S2); 904 905 MCSymbol *Tmp = OutContext.CreateTempSymbol(); 906 OutStreamer.EmitLabel(Tmp); 907 const MCSymbolRefExpr *E = MCSymbolRefExpr::Create(Stub, OutContext); 908 const MCSymbolRefExpr *T = MCSymbolRefExpr::Create(Tmp, OutContext); 909 const MCExpr *T_min_E = MCBinaryExpr::CreateSub(T, E, OutContext); 910 OutStreamer.EmitELFSize(Stub, T_min_E); 911 TS.emitDirectiveEnd(x); 912 OutStreamer.PopSection(); 913 } 914 915 void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) { 916 // Emit needed stubs 917 // 918 for (std::map< 919 const char *, 920 const llvm::Mips16HardFloatInfo::FuncSignature *>::const_iterator 921 it = StubsNeeded.begin(); 922 it != StubsNeeded.end(); ++it) { 923 const char *Symbol = it->first; 924 const llvm::Mips16HardFloatInfo::FuncSignature *Signature = it->second; 925 EmitFPCallStub(Symbol, Signature); 926 } 927 // return to the text section 928 OutStreamer.SwitchSection(OutContext.getObjectFileInfo()->getTextSection()); 929 } 930 931 void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI, 932 raw_ostream &OS) { 933 // TODO: implement 934 } 935 936 // Align all targets of indirect branches on bundle size. Used only if target 937 // is NaCl. 938 void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) { 939 // Align all blocks that are jumped to through jump table. 940 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) { 941 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables(); 942 for (unsigned I = 0; I < JT.size(); ++I) { 943 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs; 944 945 for (unsigned J = 0; J < MBBs.size(); ++J) 946 MBBs[J]->setAlignment(MIPS_NACL_BUNDLE_ALIGN); 947 } 948 } 949 950 // If basic block address is taken, block can be target of indirect branch. 951 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 952 MBB != E; ++MBB) { 953 if (MBB->hasAddressTaken()) 954 MBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN); 955 } 956 } 957 958 bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const { 959 return (Opcode == Mips::LONG_BRANCH_LUi 960 || Opcode == Mips::LONG_BRANCH_ADDiu 961 || Opcode == Mips::LONG_BRANCH_DADDiu); 962 } 963 964 // Force static initialization. 965 extern "C" void LLVMInitializeMipsAsmPrinter() { 966 RegisterAsmPrinter<MipsAsmPrinter> X(TheMipsTarget); 967 RegisterAsmPrinter<MipsAsmPrinter> Y(TheMipselTarget); 968 RegisterAsmPrinter<MipsAsmPrinter> A(TheMips64Target); 969 RegisterAsmPrinter<MipsAsmPrinter> B(TheMips64elTarget); 970 } 971