1 //===-- MipsTargetStreamer.cpp - Mips Target Streamer Methods -------------===// 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 provides Mips specific target streamer methods. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MCTargetDesc/MipsABIInfo.h" 15 #include "MipsTargetStreamer.h" 16 #include "InstPrinter/MipsInstPrinter.h" 17 #include "MipsELFStreamer.h" 18 #include "MipsMCExpr.h" 19 #include "MipsMCTargetDesc.h" 20 #include "MipsTargetObjectFile.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCSectionELF.h" 23 #include "llvm/MC/MCSubtargetInfo.h" 24 #include "llvm/MC/MCSymbolELF.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/ELF.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/FormattedStream.h" 29 30 using namespace llvm; 31 32 namespace { 33 static cl::opt<bool> RoundSectionSizes( 34 "mips-round-section-sizes", cl::init(false), 35 cl::desc("Round section sizes up to the section alignment"), cl::Hidden); 36 } // end anonymous namespace 37 38 MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) 39 : MCTargetStreamer(S), ModuleDirectiveAllowed(true) { 40 GPRInfoSet = FPRInfoSet = FrameInfoSet = false; 41 } 42 void MipsTargetStreamer::emitDirectiveSetMicroMips() {} 43 void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {} 44 void MipsTargetStreamer::setUsesMicroMips() {} 45 void MipsTargetStreamer::emitDirectiveSetMips16() {} 46 void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); } 47 void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); } 48 void MipsTargetStreamer::emitDirectiveSetNoReorder() {} 49 void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); } 50 void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); } 51 void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); } 52 void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); } 53 void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); } 54 void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) { 55 forbidModuleDirective(); 56 } 57 void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); } 58 void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {} 59 void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {} 60 void MipsTargetStreamer::emitDirectiveAbiCalls() {} 61 void MipsTargetStreamer::emitDirectiveNaN2008() {} 62 void MipsTargetStreamer::emitDirectiveNaNLegacy() {} 63 void MipsTargetStreamer::emitDirectiveOptionPic0() {} 64 void MipsTargetStreamer::emitDirectiveOptionPic2() {} 65 void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); } 66 void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize, 67 unsigned ReturnReg) {} 68 void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {} 69 void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) { 70 } 71 void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) { 72 forbidModuleDirective(); 73 } 74 void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); } 75 void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); } 76 void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); } 77 void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); } 78 void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); } 79 void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); } 80 void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); } 81 void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); } 82 void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); } 83 void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); } 84 void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); } 85 void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); } 86 void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); } 87 void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); } 88 void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); } 89 void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); } 90 void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); } 91 void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); } 92 void MipsTargetStreamer::emitDirectiveSetSoftFloat() { 93 forbidModuleDirective(); 94 } 95 void MipsTargetStreamer::emitDirectiveSetHardFloat() { 96 forbidModuleDirective(); 97 } 98 void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); } 99 void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); } 100 void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {} 101 bool MipsTargetStreamer::emitDirectiveCpRestore( 102 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, 103 const MCSubtargetInfo *STI) { 104 forbidModuleDirective(); 105 return true; 106 } 107 void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset, 108 const MCSymbol &Sym, bool IsReg) { 109 } 110 void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation, 111 bool SaveLocationIsRegister) {} 112 113 void MipsTargetStreamer::emitDirectiveModuleFP() {} 114 115 void MipsTargetStreamer::emitDirectiveModuleOddSPReg() { 116 if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI) 117 report_fatal_error("+nooddspreg is only valid for O32"); 118 } 119 void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {} 120 void MipsTargetStreamer::emitDirectiveModuleHardFloat() {} 121 void MipsTargetStreamer::emitDirectiveSetFp( 122 MipsABIFlagsSection::FpABIKind Value) { 123 forbidModuleDirective(); 124 } 125 void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); } 126 void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() { 127 forbidModuleDirective(); 128 } 129 130 void MipsTargetStreamer::emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc, 131 const MCSubtargetInfo *STI) { 132 MCInst TmpInst; 133 TmpInst.setOpcode(Opcode); 134 TmpInst.addOperand(MCOperand::createReg(Reg0)); 135 TmpInst.setLoc(IDLoc); 136 getStreamer().EmitInstruction(TmpInst, *STI); 137 } 138 139 void MipsTargetStreamer::emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1, 140 SMLoc IDLoc, const MCSubtargetInfo *STI) { 141 MCInst TmpInst; 142 TmpInst.setOpcode(Opcode); 143 TmpInst.addOperand(MCOperand::createReg(Reg0)); 144 TmpInst.addOperand(Op1); 145 TmpInst.setLoc(IDLoc); 146 getStreamer().EmitInstruction(TmpInst, *STI); 147 } 148 149 void MipsTargetStreamer::emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm, 150 SMLoc IDLoc, const MCSubtargetInfo *STI) { 151 emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, STI); 152 } 153 154 void MipsTargetStreamer::emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, 155 SMLoc IDLoc, const MCSubtargetInfo *STI) { 156 emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, STI); 157 } 158 159 void MipsTargetStreamer::emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2, 160 SMLoc IDLoc, const MCSubtargetInfo *STI) { 161 MCInst TmpInst; 162 TmpInst.setOpcode(Opcode); 163 TmpInst.addOperand(MCOperand::createImm(Imm1)); 164 TmpInst.addOperand(MCOperand::createImm(Imm2)); 165 TmpInst.setLoc(IDLoc); 166 getStreamer().EmitInstruction(TmpInst, *STI); 167 } 168 169 void MipsTargetStreamer::emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1, 170 MCOperand Op2, SMLoc IDLoc, 171 const MCSubtargetInfo *STI) { 172 MCInst TmpInst; 173 TmpInst.setOpcode(Opcode); 174 TmpInst.addOperand(MCOperand::createReg(Reg0)); 175 TmpInst.addOperand(MCOperand::createReg(Reg1)); 176 TmpInst.addOperand(Op2); 177 TmpInst.setLoc(IDLoc); 178 getStreamer().EmitInstruction(TmpInst, *STI); 179 } 180 181 void MipsTargetStreamer::emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, 182 unsigned Reg2, SMLoc IDLoc, 183 const MCSubtargetInfo *STI) { 184 emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc, STI); 185 } 186 187 void MipsTargetStreamer::emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1, 188 int16_t Imm, SMLoc IDLoc, 189 const MCSubtargetInfo *STI) { 190 emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc, STI); 191 } 192 193 void MipsTargetStreamer::emitAddu(unsigned DstReg, unsigned SrcReg, 194 unsigned TrgReg, bool Is64Bit, 195 const MCSubtargetInfo *STI) { 196 emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(), 197 STI); 198 } 199 200 void MipsTargetStreamer::emitDSLL(unsigned DstReg, unsigned SrcReg, 201 int16_t ShiftAmount, SMLoc IDLoc, 202 const MCSubtargetInfo *STI) { 203 if (ShiftAmount >= 32) { 204 emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc, STI); 205 return; 206 } 207 208 emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, STI); 209 } 210 211 void MipsTargetStreamer::emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc, 212 const MCSubtargetInfo *STI) { 213 if (hasShortDelaySlot) 214 emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, STI); 215 else 216 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 217 } 218 219 void MipsTargetStreamer::emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI) { 220 emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); 221 } 222 223 /// Emit the $gp restore operation for .cprestore. 224 void MipsTargetStreamer::emitGPRestore(int Offset, SMLoc IDLoc, 225 const MCSubtargetInfo *STI) { 226 emitLoadWithImmOffset(Mips::LW, Mips::GP, Mips::SP, Offset, Mips::GP, IDLoc, 227 STI); 228 } 229 230 /// Emit a store instruction with an immediate offset. 231 void MipsTargetStreamer::emitStoreWithImmOffset( 232 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, int64_t Offset, 233 function_ref<unsigned()> GetATReg, SMLoc IDLoc, 234 const MCSubtargetInfo *STI) { 235 if (isInt<16>(Offset)) { 236 emitRRI(Opcode, SrcReg, BaseReg, Offset, IDLoc, STI); 237 return; 238 } 239 240 // sw $8, offset($8) => lui $at, %hi(offset) 241 // add $at, $at, $8 242 // sw $8, %lo(offset)($at) 243 244 unsigned ATReg = GetATReg(); 245 if (!ATReg) 246 return; 247 248 unsigned LoOffset = Offset & 0x0000ffff; 249 unsigned HiOffset = (Offset & 0xffff0000) >> 16; 250 251 // If msb of LoOffset is 1(negative number) we must increment HiOffset 252 // to account for the sign-extension of the low part. 253 if (LoOffset & 0x8000) 254 HiOffset++; 255 256 // Generate the base address in ATReg. 257 emitRI(Mips::LUi, ATReg, HiOffset, IDLoc, STI); 258 if (BaseReg != Mips::ZERO) 259 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI); 260 // Emit the store with the adjusted base and offset. 261 emitRRI(Opcode, SrcReg, ATReg, LoOffset, IDLoc, STI); 262 } 263 264 /// Emit a store instruction with an symbol offset. Symbols are assumed to be 265 /// out of range for a simm16 will be expanded to appropriate instructions. 266 void MipsTargetStreamer::emitStoreWithSymOffset( 267 unsigned Opcode, unsigned SrcReg, unsigned BaseReg, MCOperand &HiOperand, 268 MCOperand &LoOperand, unsigned ATReg, SMLoc IDLoc, 269 const MCSubtargetInfo *STI) { 270 // sw $8, sym => lui $at, %hi(sym) 271 // sw $8, %lo(sym)($at) 272 273 // Generate the base address in ATReg. 274 emitRX(Mips::LUi, ATReg, HiOperand, IDLoc, STI); 275 if (BaseReg != Mips::ZERO) 276 emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI); 277 // Emit the store with the adjusted base and offset. 278 emitRRX(Opcode, SrcReg, ATReg, LoOperand, IDLoc, STI); 279 } 280 281 /// Emit a load instruction with an immediate offset. DstReg and TmpReg are 282 /// permitted to be the same register iff DstReg is distinct from BaseReg and 283 /// DstReg is a GPR. It is the callers responsibility to identify such cases 284 /// and pass the appropriate register in TmpReg. 285 void MipsTargetStreamer::emitLoadWithImmOffset(unsigned Opcode, unsigned DstReg, 286 unsigned BaseReg, int64_t Offset, 287 unsigned TmpReg, SMLoc IDLoc, 288 const MCSubtargetInfo *STI) { 289 if (isInt<16>(Offset)) { 290 emitRRI(Opcode, DstReg, BaseReg, Offset, IDLoc, STI); 291 return; 292 } 293 294 // 1) lw $8, offset($9) => lui $8, %hi(offset) 295 // add $8, $8, $9 296 // lw $8, %lo(offset)($9) 297 // 2) lw $8, offset($8) => lui $at, %hi(offset) 298 // add $at, $at, $8 299 // lw $8, %lo(offset)($at) 300 301 unsigned LoOffset = Offset & 0x0000ffff; 302 unsigned HiOffset = (Offset & 0xffff0000) >> 16; 303 304 // If msb of LoOffset is 1(negative number) we must increment HiOffset 305 // to account for the sign-extension of the low part. 306 if (LoOffset & 0x8000) 307 HiOffset++; 308 309 // Generate the base address in TmpReg. 310 emitRI(Mips::LUi, TmpReg, HiOffset, IDLoc, STI); 311 if (BaseReg != Mips::ZERO) 312 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI); 313 // Emit the load with the adjusted base and offset. 314 emitRRI(Opcode, DstReg, TmpReg, LoOffset, IDLoc, STI); 315 } 316 317 /// Emit a load instruction with an symbol offset. Symbols are assumed to be 318 /// out of range for a simm16 will be expanded to appropriate instructions. 319 /// DstReg and TmpReg are permitted to be the same register iff DstReg is a 320 /// GPR. It is the callers responsibility to identify such cases and pass the 321 /// appropriate register in TmpReg. 322 void MipsTargetStreamer::emitLoadWithSymOffset(unsigned Opcode, unsigned DstReg, 323 unsigned BaseReg, 324 MCOperand &HiOperand, 325 MCOperand &LoOperand, 326 unsigned TmpReg, SMLoc IDLoc, 327 const MCSubtargetInfo *STI) { 328 // 1) lw $8, sym => lui $8, %hi(sym) 329 // lw $8, %lo(sym)($8) 330 // 2) ldc1 $f0, sym => lui $at, %hi(sym) 331 // ldc1 $f0, %lo(sym)($at) 332 333 // Generate the base address in TmpReg. 334 emitRX(Mips::LUi, TmpReg, HiOperand, IDLoc, STI); 335 if (BaseReg != Mips::ZERO) 336 emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI); 337 // Emit the load with the adjusted base and offset. 338 emitRRX(Opcode, DstReg, TmpReg, LoOperand, IDLoc, STI); 339 } 340 341 MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S, 342 formatted_raw_ostream &OS) 343 : MipsTargetStreamer(S), OS(OS) {} 344 345 void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() { 346 OS << "\t.set\tmicromips\n"; 347 forbidModuleDirective(); 348 } 349 350 void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() { 351 OS << "\t.set\tnomicromips\n"; 352 forbidModuleDirective(); 353 } 354 355 void MipsTargetAsmStreamer::emitDirectiveSetMips16() { 356 OS << "\t.set\tmips16\n"; 357 forbidModuleDirective(); 358 } 359 360 void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() { 361 OS << "\t.set\tnomips16\n"; 362 MipsTargetStreamer::emitDirectiveSetNoMips16(); 363 } 364 365 void MipsTargetAsmStreamer::emitDirectiveSetReorder() { 366 OS << "\t.set\treorder\n"; 367 MipsTargetStreamer::emitDirectiveSetReorder(); 368 } 369 370 void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() { 371 OS << "\t.set\tnoreorder\n"; 372 forbidModuleDirective(); 373 } 374 375 void MipsTargetAsmStreamer::emitDirectiveSetMacro() { 376 OS << "\t.set\tmacro\n"; 377 MipsTargetStreamer::emitDirectiveSetMacro(); 378 } 379 380 void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() { 381 OS << "\t.set\tnomacro\n"; 382 MipsTargetStreamer::emitDirectiveSetNoMacro(); 383 } 384 385 void MipsTargetAsmStreamer::emitDirectiveSetMsa() { 386 OS << "\t.set\tmsa\n"; 387 MipsTargetStreamer::emitDirectiveSetMsa(); 388 } 389 390 void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() { 391 OS << "\t.set\tnomsa\n"; 392 MipsTargetStreamer::emitDirectiveSetNoMsa(); 393 } 394 395 void MipsTargetAsmStreamer::emitDirectiveSetAt() { 396 OS << "\t.set\tat\n"; 397 MipsTargetStreamer::emitDirectiveSetAt(); 398 } 399 400 void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) { 401 OS << "\t.set\tat=$" << Twine(RegNo) << "\n"; 402 MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo); 403 } 404 405 void MipsTargetAsmStreamer::emitDirectiveSetNoAt() { 406 OS << "\t.set\tnoat\n"; 407 MipsTargetStreamer::emitDirectiveSetNoAt(); 408 } 409 410 void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) { 411 OS << "\t.end\t" << Name << '\n'; 412 } 413 414 void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) { 415 OS << "\t.ent\t" << Symbol.getName() << '\n'; 416 } 417 418 void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; } 419 420 void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; } 421 422 void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() { 423 OS << "\t.nan\tlegacy\n"; 424 } 425 426 void MipsTargetAsmStreamer::emitDirectiveOptionPic0() { 427 OS << "\t.option\tpic0\n"; 428 } 429 430 void MipsTargetAsmStreamer::emitDirectiveOptionPic2() { 431 OS << "\t.option\tpic2\n"; 432 } 433 434 void MipsTargetAsmStreamer::emitDirectiveInsn() { 435 MipsTargetStreamer::emitDirectiveInsn(); 436 OS << "\t.insn\n"; 437 } 438 439 void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize, 440 unsigned ReturnReg) { 441 OS << "\t.frame\t$" 442 << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << "," 443 << StackSize << ",$" 444 << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n'; 445 } 446 447 void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) { 448 OS << "\t.set arch=" << Arch << "\n"; 449 MipsTargetStreamer::emitDirectiveSetArch(Arch); 450 } 451 452 void MipsTargetAsmStreamer::emitDirectiveSetMips0() { 453 OS << "\t.set\tmips0\n"; 454 MipsTargetStreamer::emitDirectiveSetMips0(); 455 } 456 457 void MipsTargetAsmStreamer::emitDirectiveSetMips1() { 458 OS << "\t.set\tmips1\n"; 459 MipsTargetStreamer::emitDirectiveSetMips1(); 460 } 461 462 void MipsTargetAsmStreamer::emitDirectiveSetMips2() { 463 OS << "\t.set\tmips2\n"; 464 MipsTargetStreamer::emitDirectiveSetMips2(); 465 } 466 467 void MipsTargetAsmStreamer::emitDirectiveSetMips3() { 468 OS << "\t.set\tmips3\n"; 469 MipsTargetStreamer::emitDirectiveSetMips3(); 470 } 471 472 void MipsTargetAsmStreamer::emitDirectiveSetMips4() { 473 OS << "\t.set\tmips4\n"; 474 MipsTargetStreamer::emitDirectiveSetMips4(); 475 } 476 477 void MipsTargetAsmStreamer::emitDirectiveSetMips5() { 478 OS << "\t.set\tmips5\n"; 479 MipsTargetStreamer::emitDirectiveSetMips5(); 480 } 481 482 void MipsTargetAsmStreamer::emitDirectiveSetMips32() { 483 OS << "\t.set\tmips32\n"; 484 MipsTargetStreamer::emitDirectiveSetMips32(); 485 } 486 487 void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() { 488 OS << "\t.set\tmips32r2\n"; 489 MipsTargetStreamer::emitDirectiveSetMips32R2(); 490 } 491 492 void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() { 493 OS << "\t.set\tmips32r3\n"; 494 MipsTargetStreamer::emitDirectiveSetMips32R3(); 495 } 496 497 void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() { 498 OS << "\t.set\tmips32r5\n"; 499 MipsTargetStreamer::emitDirectiveSetMips32R5(); 500 } 501 502 void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() { 503 OS << "\t.set\tmips32r6\n"; 504 MipsTargetStreamer::emitDirectiveSetMips32R6(); 505 } 506 507 void MipsTargetAsmStreamer::emitDirectiveSetMips64() { 508 OS << "\t.set\tmips64\n"; 509 MipsTargetStreamer::emitDirectiveSetMips64(); 510 } 511 512 void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() { 513 OS << "\t.set\tmips64r2\n"; 514 MipsTargetStreamer::emitDirectiveSetMips64R2(); 515 } 516 517 void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() { 518 OS << "\t.set\tmips64r3\n"; 519 MipsTargetStreamer::emitDirectiveSetMips64R3(); 520 } 521 522 void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() { 523 OS << "\t.set\tmips64r5\n"; 524 MipsTargetStreamer::emitDirectiveSetMips64R5(); 525 } 526 527 void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() { 528 OS << "\t.set\tmips64r6\n"; 529 MipsTargetStreamer::emitDirectiveSetMips64R6(); 530 } 531 532 void MipsTargetAsmStreamer::emitDirectiveSetDsp() { 533 OS << "\t.set\tdsp\n"; 534 MipsTargetStreamer::emitDirectiveSetDsp(); 535 } 536 537 void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() { 538 OS << "\t.set\tnodsp\n"; 539 MipsTargetStreamer::emitDirectiveSetNoDsp(); 540 } 541 542 void MipsTargetAsmStreamer::emitDirectiveSetPop() { 543 OS << "\t.set\tpop\n"; 544 MipsTargetStreamer::emitDirectiveSetPop(); 545 } 546 547 void MipsTargetAsmStreamer::emitDirectiveSetPush() { 548 OS << "\t.set\tpush\n"; 549 MipsTargetStreamer::emitDirectiveSetPush(); 550 } 551 552 void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() { 553 OS << "\t.set\tsoftfloat\n"; 554 MipsTargetStreamer::emitDirectiveSetSoftFloat(); 555 } 556 557 void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() { 558 OS << "\t.set\thardfloat\n"; 559 MipsTargetStreamer::emitDirectiveSetHardFloat(); 560 } 561 562 // Print a 32 bit hex number with all numbers. 563 static void printHex32(unsigned Value, raw_ostream &OS) { 564 OS << "0x"; 565 for (int i = 7; i >= 0; i--) 566 OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4)); 567 } 568 569 void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask, 570 int CPUTopSavedRegOff) { 571 OS << "\t.mask \t"; 572 printHex32(CPUBitmask, OS); 573 OS << ',' << CPUTopSavedRegOff << '\n'; 574 } 575 576 void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask, 577 int FPUTopSavedRegOff) { 578 OS << "\t.fmask\t"; 579 printHex32(FPUBitmask, OS); 580 OS << "," << FPUTopSavedRegOff << '\n'; 581 } 582 583 void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) { 584 OS << "\t.cpload\t$" 585 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n"; 586 forbidModuleDirective(); 587 } 588 589 bool MipsTargetAsmStreamer::emitDirectiveCpRestore( 590 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, 591 const MCSubtargetInfo *STI) { 592 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI); 593 OS << "\t.cprestore\t" << Offset << "\n"; 594 return true; 595 } 596 597 void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo, 598 int RegOrOffset, 599 const MCSymbol &Sym, 600 bool IsReg) { 601 OS << "\t.cpsetup\t$" 602 << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", "; 603 604 if (IsReg) 605 OS << "$" 606 << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower(); 607 else 608 OS << RegOrOffset; 609 610 OS << ", "; 611 612 OS << Sym.getName(); 613 forbidModuleDirective(); 614 } 615 616 void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation, 617 bool SaveLocationIsRegister) { 618 OS << "\t.cpreturn"; 619 forbidModuleDirective(); 620 } 621 622 void MipsTargetAsmStreamer::emitDirectiveModuleFP() { 623 OS << "\t.module\tfp="; 624 OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n"; 625 } 626 627 void MipsTargetAsmStreamer::emitDirectiveSetFp( 628 MipsABIFlagsSection::FpABIKind Value) { 629 MipsTargetStreamer::emitDirectiveSetFp(Value); 630 631 OS << "\t.set\tfp="; 632 OS << ABIFlagsSection.getFpABIString(Value) << "\n"; 633 } 634 635 void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() { 636 MipsTargetStreamer::emitDirectiveModuleOddSPReg(); 637 638 OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n"; 639 } 640 641 void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() { 642 MipsTargetStreamer::emitDirectiveSetOddSPReg(); 643 OS << "\t.set\toddspreg\n"; 644 } 645 646 void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() { 647 MipsTargetStreamer::emitDirectiveSetNoOddSPReg(); 648 OS << "\t.set\tnooddspreg\n"; 649 } 650 651 void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() { 652 OS << "\t.module\tsoftfloat\n"; 653 } 654 655 void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() { 656 OS << "\t.module\thardfloat\n"; 657 } 658 659 // This part is for ELF object output. 660 MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S, 661 const MCSubtargetInfo &STI) 662 : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) { 663 MCAssembler &MCA = getStreamer().getAssembler(); 664 665 // It's possible that MCObjectFileInfo isn't fully initialized at this point 666 // due to an initialization order problem where LLVMTargetMachine creates the 667 // target streamer before TargetLoweringObjectFile calls 668 // InitializeMCObjectFileInfo. There doesn't seem to be a single place that 669 // covers all cases so this statement covers most cases and direct object 670 // emission must call setPic() once MCObjectFileInfo has been initialized. The 671 // cases we don't handle here are covered by MipsAsmPrinter. 672 Pic = MCA.getContext().getObjectFileInfo()->isPositionIndependent(); 673 674 const FeatureBitset &Features = STI.getFeatureBits(); 675 676 // Set the header flags that we can in the constructor. 677 // FIXME: This is a fairly terrible hack. We set the rest 678 // of these in the destructor. The problem here is two-fold: 679 // 680 // a: Some of the eflags can be set/reset by directives. 681 // b: There aren't any usage paths that initialize the ABI 682 // pointer until after we initialize either an assembler 683 // or the target machine. 684 // We can fix this by making the target streamer construct 685 // the ABI, but this is fraught with wide ranging dependency 686 // issues as well. 687 unsigned EFlags = MCA.getELFHeaderEFlags(); 688 689 // FIXME: Fix a dependency issue by instantiating the ABI object to some 690 // default based off the triple. The triple doesn't describe the target 691 // fully, but any external user of the API that uses the MCTargetStreamer 692 // would otherwise crash on assertion failure. 693 694 ABI = MipsABIInfo( 695 STI.getTargetTriple().getArch() == Triple::ArchType::mipsel || 696 STI.getTargetTriple().getArch() == Triple::ArchType::mips 697 ? MipsABIInfo::O32() 698 : MipsABIInfo::N64()); 699 700 // Architecture 701 if (Features[Mips::FeatureMips64r6]) 702 EFlags |= ELF::EF_MIPS_ARCH_64R6; 703 else if (Features[Mips::FeatureMips64r2] || 704 Features[Mips::FeatureMips64r3] || 705 Features[Mips::FeatureMips64r5]) 706 EFlags |= ELF::EF_MIPS_ARCH_64R2; 707 else if (Features[Mips::FeatureMips64]) 708 EFlags |= ELF::EF_MIPS_ARCH_64; 709 else if (Features[Mips::FeatureMips5]) 710 EFlags |= ELF::EF_MIPS_ARCH_5; 711 else if (Features[Mips::FeatureMips4]) 712 EFlags |= ELF::EF_MIPS_ARCH_4; 713 else if (Features[Mips::FeatureMips3]) 714 EFlags |= ELF::EF_MIPS_ARCH_3; 715 else if (Features[Mips::FeatureMips32r6]) 716 EFlags |= ELF::EF_MIPS_ARCH_32R6; 717 else if (Features[Mips::FeatureMips32r2] || 718 Features[Mips::FeatureMips32r3] || 719 Features[Mips::FeatureMips32r5]) 720 EFlags |= ELF::EF_MIPS_ARCH_32R2; 721 else if (Features[Mips::FeatureMips32]) 722 EFlags |= ELF::EF_MIPS_ARCH_32; 723 else if (Features[Mips::FeatureMips2]) 724 EFlags |= ELF::EF_MIPS_ARCH_2; 725 else 726 EFlags |= ELF::EF_MIPS_ARCH_1; 727 728 // Machine 729 if (Features[Mips::FeatureCnMips]) 730 EFlags |= ELF::EF_MIPS_MACH_OCTEON; 731 732 // Other options. 733 if (Features[Mips::FeatureNaN2008]) 734 EFlags |= ELF::EF_MIPS_NAN2008; 735 736 MCA.setELFHeaderEFlags(EFlags); 737 } 738 739 void MipsTargetELFStreamer::emitLabel(MCSymbol *S) { 740 auto *Symbol = cast<MCSymbolELF>(S); 741 if (!isMicroMipsEnabled()) 742 return; 743 getStreamer().getAssembler().registerSymbol(*Symbol); 744 uint8_t Type = Symbol->getType(); 745 if (Type != ELF::STT_FUNC) 746 return; 747 748 Symbol->setOther(ELF::STO_MIPS_MICROMIPS); 749 } 750 751 void MipsTargetELFStreamer::finish() { 752 MCAssembler &MCA = getStreamer().getAssembler(); 753 const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo(); 754 755 // .bss, .text and .data are always at least 16-byte aligned. 756 MCSection &TextSection = *OFI.getTextSection(); 757 MCA.registerSection(TextSection); 758 MCSection &DataSection = *OFI.getDataSection(); 759 MCA.registerSection(DataSection); 760 MCSection &BSSSection = *OFI.getBSSSection(); 761 MCA.registerSection(BSSSection); 762 763 TextSection.setAlignment(std::max(16u, TextSection.getAlignment())); 764 DataSection.setAlignment(std::max(16u, DataSection.getAlignment())); 765 BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment())); 766 767 if (RoundSectionSizes) { 768 // Make sections sizes a multiple of the alignment. This is useful for 769 // verifying the output of IAS against the output of other assemblers but 770 // it's not necessary to produce a correct object and increases section 771 // size. 772 MCStreamer &OS = getStreamer(); 773 for (MCSection &S : MCA) { 774 MCSectionELF &Section = static_cast<MCSectionELF &>(S); 775 776 unsigned Alignment = Section.getAlignment(); 777 if (Alignment) { 778 OS.SwitchSection(&Section); 779 if (Section.UseCodeAlign()) 780 OS.EmitCodeAlignment(Alignment, Alignment); 781 else 782 OS.EmitValueToAlignment(Alignment, 0, 1, Alignment); 783 } 784 } 785 } 786 787 const FeatureBitset &Features = STI.getFeatureBits(); 788 789 // Update e_header flags. See the FIXME and comment above in 790 // the constructor for a full rundown on this. 791 unsigned EFlags = MCA.getELFHeaderEFlags(); 792 793 // ABI 794 // N64 does not require any ABI bits. 795 if (getABI().IsO32()) 796 EFlags |= ELF::EF_MIPS_ABI_O32; 797 else if (getABI().IsN32()) 798 EFlags |= ELF::EF_MIPS_ABI2; 799 800 if (Features[Mips::FeatureGP64Bit]) { 801 if (getABI().IsO32()) 802 EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */ 803 } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64]) 804 EFlags |= ELF::EF_MIPS_32BITMODE; 805 806 // -mplt is not implemented but we should act as if it was 807 // given. 808 if (!Features[Mips::FeatureNoABICalls]) 809 EFlags |= ELF::EF_MIPS_CPIC; 810 811 if (Pic) 812 EFlags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC; 813 814 MCA.setELFHeaderEFlags(EFlags); 815 816 // Emit all the option records. 817 // At the moment we are only emitting .Mips.options (ODK_REGINFO) and 818 // .reginfo. 819 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer); 820 MEF.EmitMipsOptionRecords(); 821 822 emitMipsAbiFlags(); 823 } 824 825 void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) { 826 auto *Symbol = cast<MCSymbolELF>(S); 827 // If on rhs is micromips symbol then mark Symbol as microMips. 828 if (Value->getKind() != MCExpr::SymbolRef) 829 return; 830 const auto &RhsSym = cast<MCSymbolELF>( 831 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol()); 832 833 if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS)) 834 return; 835 836 Symbol->setOther(ELF::STO_MIPS_MICROMIPS); 837 } 838 839 MCELFStreamer &MipsTargetELFStreamer::getStreamer() { 840 return static_cast<MCELFStreamer &>(Streamer); 841 } 842 843 void MipsTargetELFStreamer::emitDirectiveSetMicroMips() { 844 MicroMipsEnabled = true; 845 forbidModuleDirective(); 846 } 847 848 void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() { 849 MicroMipsEnabled = false; 850 forbidModuleDirective(); 851 } 852 853 void MipsTargetELFStreamer::setUsesMicroMips() { 854 MCAssembler &MCA = getStreamer().getAssembler(); 855 unsigned Flags = MCA.getELFHeaderEFlags(); 856 Flags |= ELF::EF_MIPS_MICROMIPS; 857 MCA.setELFHeaderEFlags(Flags); 858 } 859 860 void MipsTargetELFStreamer::emitDirectiveSetMips16() { 861 MCAssembler &MCA = getStreamer().getAssembler(); 862 unsigned Flags = MCA.getELFHeaderEFlags(); 863 Flags |= ELF::EF_MIPS_ARCH_ASE_M16; 864 MCA.setELFHeaderEFlags(Flags); 865 forbidModuleDirective(); 866 } 867 868 void MipsTargetELFStreamer::emitDirectiveSetNoReorder() { 869 MCAssembler &MCA = getStreamer().getAssembler(); 870 unsigned Flags = MCA.getELFHeaderEFlags(); 871 Flags |= ELF::EF_MIPS_NOREORDER; 872 MCA.setELFHeaderEFlags(Flags); 873 forbidModuleDirective(); 874 } 875 876 void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) { 877 MCAssembler &MCA = getStreamer().getAssembler(); 878 MCContext &Context = MCA.getContext(); 879 MCStreamer &OS = getStreamer(); 880 881 MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0); 882 883 MCSymbol *Sym = Context.getOrCreateSymbol(Name); 884 const MCSymbolRefExpr *ExprRef = 885 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context); 886 887 MCA.registerSection(*Sec); 888 Sec->setAlignment(4); 889 890 OS.PushSection(); 891 892 OS.SwitchSection(Sec); 893 894 OS.EmitValueImpl(ExprRef, 4); 895 896 OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask 897 OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset 898 899 OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask 900 OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset 901 902 OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset 903 OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg 904 OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg 905 906 // The .end directive marks the end of a procedure. Invalidate 907 // the information gathered up until this point. 908 GPRInfoSet = FPRInfoSet = FrameInfoSet = false; 909 910 OS.PopSection(); 911 912 // .end also implicitly sets the size. 913 MCSymbol *CurPCSym = Context.createTempSymbol(); 914 OS.EmitLabel(CurPCSym); 915 const MCExpr *Size = MCBinaryExpr::createSub( 916 MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context), 917 ExprRef, Context); 918 int64_t AbsSize; 919 if (!Size->evaluateAsAbsolute(AbsSize, MCA)) 920 llvm_unreachable("Function size must be evaluatable as absolute"); 921 Size = MCConstantExpr::create(AbsSize, Context); 922 static_cast<MCSymbolELF *>(Sym)->setSize(Size); 923 } 924 925 void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) { 926 GPRInfoSet = FPRInfoSet = FrameInfoSet = false; 927 928 // .ent also acts like an implicit '.type symbol, STT_FUNC' 929 static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC); 930 } 931 932 void MipsTargetELFStreamer::emitDirectiveAbiCalls() { 933 MCAssembler &MCA = getStreamer().getAssembler(); 934 unsigned Flags = MCA.getELFHeaderEFlags(); 935 Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC; 936 MCA.setELFHeaderEFlags(Flags); 937 } 938 939 void MipsTargetELFStreamer::emitDirectiveNaN2008() { 940 MCAssembler &MCA = getStreamer().getAssembler(); 941 unsigned Flags = MCA.getELFHeaderEFlags(); 942 Flags |= ELF::EF_MIPS_NAN2008; 943 MCA.setELFHeaderEFlags(Flags); 944 } 945 946 void MipsTargetELFStreamer::emitDirectiveNaNLegacy() { 947 MCAssembler &MCA = getStreamer().getAssembler(); 948 unsigned Flags = MCA.getELFHeaderEFlags(); 949 Flags &= ~ELF::EF_MIPS_NAN2008; 950 MCA.setELFHeaderEFlags(Flags); 951 } 952 953 void MipsTargetELFStreamer::emitDirectiveOptionPic0() { 954 MCAssembler &MCA = getStreamer().getAssembler(); 955 unsigned Flags = MCA.getELFHeaderEFlags(); 956 // This option overrides other PIC options like -KPIC. 957 Pic = false; 958 Flags &= ~ELF::EF_MIPS_PIC; 959 MCA.setELFHeaderEFlags(Flags); 960 } 961 962 void MipsTargetELFStreamer::emitDirectiveOptionPic2() { 963 MCAssembler &MCA = getStreamer().getAssembler(); 964 unsigned Flags = MCA.getELFHeaderEFlags(); 965 Pic = true; 966 // NOTE: We are following the GAS behaviour here which means the directive 967 // 'pic2' also sets the CPIC bit in the ELF header. This is different from 968 // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and 969 // EF_MIPS_CPIC to be mutually exclusive. 970 Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC; 971 MCA.setELFHeaderEFlags(Flags); 972 } 973 974 void MipsTargetELFStreamer::emitDirectiveInsn() { 975 MipsTargetStreamer::emitDirectiveInsn(); 976 MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer); 977 MEF.createPendingLabelRelocs(); 978 } 979 980 void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize, 981 unsigned ReturnReg_) { 982 MCContext &Context = getStreamer().getAssembler().getContext(); 983 const MCRegisterInfo *RegInfo = Context.getRegisterInfo(); 984 985 FrameInfoSet = true; 986 FrameReg = RegInfo->getEncodingValue(StackReg); 987 FrameOffset = StackSize; 988 ReturnReg = RegInfo->getEncodingValue(ReturnReg_); 989 } 990 991 void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask, 992 int CPUTopSavedRegOff) { 993 GPRInfoSet = true; 994 GPRBitMask = CPUBitmask; 995 GPROffset = CPUTopSavedRegOff; 996 } 997 998 void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask, 999 int FPUTopSavedRegOff) { 1000 FPRInfoSet = true; 1001 FPRBitMask = FPUBitmask; 1002 FPROffset = FPUTopSavedRegOff; 1003 } 1004 1005 void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) { 1006 // .cpload $reg 1007 // This directive expands to: 1008 // lui $gp, %hi(_gp_disp) 1009 // addui $gp, $gp, %lo(_gp_disp) 1010 // addu $gp, $gp, $reg 1011 // when support for position independent code is enabled. 1012 if (!Pic || (getABI().IsN32() || getABI().IsN64())) 1013 return; 1014 1015 // There's a GNU extension controlled by -mno-shared that allows 1016 // locally-binding symbols to be accessed using absolute addresses. 1017 // This is currently not supported. When supported -mno-shared makes 1018 // .cpload expand to: 1019 // lui $gp, %hi(__gnu_local_gp) 1020 // addiu $gp, $gp, %lo(__gnu_local_gp) 1021 1022 StringRef SymName("_gp_disp"); 1023 MCAssembler &MCA = getStreamer().getAssembler(); 1024 MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName); 1025 MCA.registerSymbol(*GP_Disp); 1026 1027 MCInst TmpInst; 1028 TmpInst.setOpcode(Mips::LUi); 1029 TmpInst.addOperand(MCOperand::createReg(Mips::GP)); 1030 const MCExpr *HiSym = MipsMCExpr::create( 1031 MipsMCExpr::MEK_HI, 1032 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None, 1033 MCA.getContext()), 1034 MCA.getContext()); 1035 TmpInst.addOperand(MCOperand::createExpr(HiSym)); 1036 getStreamer().EmitInstruction(TmpInst, STI); 1037 1038 TmpInst.clear(); 1039 1040 TmpInst.setOpcode(Mips::ADDiu); 1041 TmpInst.addOperand(MCOperand::createReg(Mips::GP)); 1042 TmpInst.addOperand(MCOperand::createReg(Mips::GP)); 1043 const MCExpr *LoSym = MipsMCExpr::create( 1044 MipsMCExpr::MEK_LO, 1045 MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None, 1046 MCA.getContext()), 1047 MCA.getContext()); 1048 TmpInst.addOperand(MCOperand::createExpr(LoSym)); 1049 getStreamer().EmitInstruction(TmpInst, STI); 1050 1051 TmpInst.clear(); 1052 1053 TmpInst.setOpcode(Mips::ADDu); 1054 TmpInst.addOperand(MCOperand::createReg(Mips::GP)); 1055 TmpInst.addOperand(MCOperand::createReg(Mips::GP)); 1056 TmpInst.addOperand(MCOperand::createReg(RegNo)); 1057 getStreamer().EmitInstruction(TmpInst, STI); 1058 1059 forbidModuleDirective(); 1060 } 1061 1062 bool MipsTargetELFStreamer::emitDirectiveCpRestore( 1063 int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, 1064 const MCSubtargetInfo *STI) { 1065 MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI); 1066 // .cprestore offset 1067 // When PIC mode is enabled and the O32 ABI is used, this directive expands 1068 // to: 1069 // sw $gp, offset($sp) 1070 // and adds a corresponding LW after every JAL. 1071 1072 // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it 1073 // is used in non-PIC mode. 1074 if (!Pic || (getABI().IsN32() || getABI().IsN64())) 1075 return true; 1076 1077 // Store the $gp on the stack. 1078 emitStoreWithImmOffset(Mips::SW, Mips::GP, Mips::SP, Offset, GetATReg, IDLoc, 1079 STI); 1080 return true; 1081 } 1082 1083 void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo, 1084 int RegOrOffset, 1085 const MCSymbol &Sym, 1086 bool IsReg) { 1087 // Only N32 and N64 emit anything for .cpsetup iff PIC is set. 1088 if (!Pic || !(getABI().IsN32() || getABI().IsN64())) 1089 return; 1090 1091 forbidModuleDirective(); 1092 1093 MCAssembler &MCA = getStreamer().getAssembler(); 1094 MCInst Inst; 1095 1096 // Either store the old $gp in a register or on the stack 1097 if (IsReg) { 1098 // move $save, $gpreg 1099 emitRRR(Mips::OR64, RegOrOffset, Mips::GP, Mips::ZERO, SMLoc(), &STI); 1100 } else { 1101 // sd $gpreg, offset($sp) 1102 emitRRI(Mips::SD, Mips::GP, Mips::SP, RegOrOffset, SMLoc(), &STI); 1103 } 1104 1105 if (getABI().IsN32()) { 1106 MCSymbol *GPSym = MCA.getContext().getOrCreateSymbol("__gnu_local_gp"); 1107 const MipsMCExpr *HiExpr = MipsMCExpr::create( 1108 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(GPSym, MCA.getContext()), 1109 MCA.getContext()); 1110 const MipsMCExpr *LoExpr = MipsMCExpr::create( 1111 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(GPSym, MCA.getContext()), 1112 MCA.getContext()); 1113 1114 // lui $gp, %hi(__gnu_local_gp) 1115 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI); 1116 1117 // addiu $gp, $gp, %lo(__gnu_local_gp) 1118 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr), 1119 SMLoc(), &STI); 1120 1121 return; 1122 } 1123 1124 const MipsMCExpr *HiExpr = MipsMCExpr::createGpOff( 1125 MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(&Sym, MCA.getContext()), 1126 MCA.getContext()); 1127 const MipsMCExpr *LoExpr = MipsMCExpr::createGpOff( 1128 MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(&Sym, MCA.getContext()), 1129 MCA.getContext()); 1130 1131 // lui $gp, %hi(%neg(%gp_rel(funcSym))) 1132 emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI); 1133 1134 // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym))) 1135 emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr), 1136 SMLoc(), &STI); 1137 1138 // daddu $gp, $gp, $funcreg 1139 emitRRR(Mips::DADDu, Mips::GP, Mips::GP, RegNo, SMLoc(), &STI); 1140 } 1141 1142 void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation, 1143 bool SaveLocationIsRegister) { 1144 // Only N32 and N64 emit anything for .cpreturn iff PIC is set. 1145 if (!Pic || !(getABI().IsN32() || getABI().IsN64())) 1146 return; 1147 1148 MCInst Inst; 1149 // Either restore the old $gp from a register or on the stack 1150 if (SaveLocationIsRegister) { 1151 Inst.setOpcode(Mips::OR); 1152 Inst.addOperand(MCOperand::createReg(Mips::GP)); 1153 Inst.addOperand(MCOperand::createReg(SaveLocation)); 1154 Inst.addOperand(MCOperand::createReg(Mips::ZERO)); 1155 } else { 1156 Inst.setOpcode(Mips::LD); 1157 Inst.addOperand(MCOperand::createReg(Mips::GP)); 1158 Inst.addOperand(MCOperand::createReg(Mips::SP)); 1159 Inst.addOperand(MCOperand::createImm(SaveLocation)); 1160 } 1161 getStreamer().EmitInstruction(Inst, STI); 1162 1163 forbidModuleDirective(); 1164 } 1165 1166 void MipsTargetELFStreamer::emitMipsAbiFlags() { 1167 MCAssembler &MCA = getStreamer().getAssembler(); 1168 MCContext &Context = MCA.getContext(); 1169 MCStreamer &OS = getStreamer(); 1170 MCSectionELF *Sec = Context.getELFSection( 1171 ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, ""); 1172 MCA.registerSection(*Sec); 1173 Sec->setAlignment(8); 1174 OS.SwitchSection(Sec); 1175 1176 OS << ABIFlagsSection; 1177 } 1178