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