| 1 | //===-- MipsTargetStreamer.h - Mips Target Streamer ------------*- C++ -*--===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIB_TARGET_MIPS_MIPSTARGETSTREAMER_H |
| 10 | #define LLVM_LIB_TARGET_MIPS_MIPSTARGETSTREAMER_H |
| 11 | |
| 12 | #include "MCTargetDesc/MipsABIFlagsSection.h" |
| 13 | #include "MCTargetDesc/MipsABIInfo.h" |
| 14 | #include "MCTargetDesc/MipsMCOptions.h" |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/MC/MCELFStreamer.h" |
| 18 | #include "llvm/MC/MCRegisterInfo.h" |
| 19 | #include "llvm/MC/MCStreamer.h" |
| 20 | |
| 21 | namespace llvm { |
| 22 | |
| 23 | class formatted_raw_ostream; |
| 24 | |
| 25 | class MipsTargetStreamer : public MCTargetStreamer { |
| 26 | public: |
| 27 | MipsTargetStreamer(MCStreamer &S); |
| 28 | |
| 29 | virtual void emitGPRel32Value(const MCExpr *); |
| 30 | virtual void emitGPRel64Value(const MCExpr *); |
| 31 | virtual void emitDTPRel32Value(const MCExpr *); |
| 32 | virtual void emitDTPRel64Value(const MCExpr *); |
| 33 | virtual void emitTPRel32Value(const MCExpr *); |
| 34 | virtual void emitTPRel64Value(const MCExpr *); |
| 35 | |
| 36 | virtual void setPic(bool Value) {} |
| 37 | |
| 38 | virtual void emitDirectiveSetMicroMips(); |
| 39 | virtual void emitDirectiveSetNoMicroMips(); |
| 40 | virtual void setUsesMicroMips(); |
| 41 | virtual void emitDirectiveSetMips16(); |
| 42 | virtual void emitDirectiveSetNoMips16(); |
| 43 | |
| 44 | virtual void emitDirectiveSetReorder(); |
| 45 | virtual void emitDirectiveSetNoReorder(); |
| 46 | virtual void emitDirectiveSetMacro(); |
| 47 | virtual void emitDirectiveSetNoMacro(); |
| 48 | virtual void emitDirectiveSetMsa(); |
| 49 | virtual void emitDirectiveSetNoMsa(); |
| 50 | virtual void emitDirectiveSetMt(); |
| 51 | virtual void emitDirectiveSetNoMt(); |
| 52 | virtual void emitDirectiveSetCRC(); |
| 53 | virtual void emitDirectiveSetNoCRC(); |
| 54 | virtual void emitDirectiveSetVirt(); |
| 55 | virtual void emitDirectiveSetNoVirt(); |
| 56 | virtual void emitDirectiveSetGINV(); |
| 57 | virtual void emitDirectiveSetNoGINV(); |
| 58 | virtual void emitDirectiveSetEVA(); |
| 59 | virtual void emitDirectiveSetNoEVA(); |
| 60 | virtual void emitDirectiveSetAt(); |
| 61 | virtual void emitDirectiveSetAtWithArg(unsigned RegNo); |
| 62 | virtual void emitDirectiveSetNoAt(); |
| 63 | virtual void emitDirectiveEnd(StringRef Name); |
| 64 | |
| 65 | virtual void emitDirectiveEnt(const MCSymbol &Symbol); |
| 66 | virtual void emitDirectiveAbiCalls(); |
| 67 | virtual void emitDirectiveNaN2008(); |
| 68 | virtual void emitDirectiveNaNLegacy(); |
| 69 | virtual void emitDirectiveOptionPic0(); |
| 70 | virtual void emitDirectiveOptionPic2(); |
| 71 | virtual void emitDirectiveInsn(); |
| 72 | virtual void emitFrame(MCRegister StackReg, unsigned StackSize, |
| 73 | MCRegister ReturnReg); |
| 74 | virtual void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff); |
| 75 | virtual void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff); |
| 76 | |
| 77 | virtual void emitDirectiveSetArch(StringRef Arch); |
| 78 | virtual void emitDirectiveSetMips0(); |
| 79 | virtual void emitDirectiveSetMips1(); |
| 80 | virtual void emitDirectiveSetMips2(); |
| 81 | virtual void emitDirectiveSetMips3(); |
| 82 | virtual void emitDirectiveSetMips4(); |
| 83 | virtual void emitDirectiveSetMips5(); |
| 84 | virtual void emitDirectiveSetMips32(); |
| 85 | virtual void emitDirectiveSetMips32R2(); |
| 86 | virtual void emitDirectiveSetMips32R3(); |
| 87 | virtual void emitDirectiveSetMips32R5(); |
| 88 | virtual void emitDirectiveSetMips32R6(); |
| 89 | virtual void emitDirectiveSetMips64(); |
| 90 | virtual void emitDirectiveSetMips64R2(); |
| 91 | virtual void emitDirectiveSetMips64R3(); |
| 92 | virtual void emitDirectiveSetMips64R5(); |
| 93 | virtual void emitDirectiveSetMips64R6(); |
| 94 | virtual void emitDirectiveSetDsp(); |
| 95 | virtual void emitDirectiveSetDspr2(); |
| 96 | virtual void emitDirectiveSetNoDsp(); |
| 97 | virtual void emitDirectiveSetMips3D(); |
| 98 | virtual void emitDirectiveSetNoMips3D(); |
| 99 | virtual void emitDirectiveSetPop(); |
| 100 | virtual void emitDirectiveSetPush(); |
| 101 | virtual void emitDirectiveSetSoftFloat(); |
| 102 | virtual void emitDirectiveSetHardFloat(); |
| 103 | |
| 104 | // PIC support |
| 105 | virtual void emitDirectiveCpAdd(MCRegister Reg); |
| 106 | virtual void emitDirectiveCpLoad(MCRegister Reg); |
| 107 | virtual void emitDirectiveCpLocal(MCRegister Reg); |
| 108 | virtual bool emitDirectiveCpRestore(int Offset, |
| 109 | function_ref<MCRegister()> GetATReg, |
| 110 | SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 111 | virtual void emitDirectiveCpsetup(MCRegister Reg, int RegOrOffset, |
| 112 | const MCSymbol &Sym, bool IsReg); |
| 113 | virtual void emitDirectiveCpreturn(unsigned SaveLocation, |
| 114 | bool SaveLocationIsRegister); |
| 115 | |
| 116 | // FP abiflags directives |
| 117 | virtual void emitDirectiveModuleFP(); |
| 118 | virtual void emitDirectiveModuleOddSPReg(); |
| 119 | virtual void emitDirectiveModuleSoftFloat(); |
| 120 | virtual void emitDirectiveModuleHardFloat(); |
| 121 | virtual void emitDirectiveModuleMT(); |
| 122 | virtual void emitDirectiveSetFp(MipsABIFlagsSection::FpABIKind Value); |
| 123 | virtual void emitDirectiveSetOddSPReg(); |
| 124 | virtual void emitDirectiveSetNoOddSPReg(); |
| 125 | virtual void emitDirectiveModuleCRC(); |
| 126 | virtual void emitDirectiveModuleNoCRC(); |
| 127 | virtual void emitDirectiveModuleVirt(); |
| 128 | virtual void emitDirectiveModuleNoVirt(); |
| 129 | virtual void emitDirectiveModuleGINV(); |
| 130 | virtual void emitDirectiveModuleNoGINV(); |
| 131 | |
| 132 | void emitR(unsigned Opcode, MCRegister Reg0, SMLoc IDLoc, |
| 133 | const MCSubtargetInfo *STI); |
| 134 | void emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2, SMLoc IDLoc, |
| 135 | const MCSubtargetInfo *STI); |
| 136 | void emitRX(unsigned Opcode, MCRegister Reg0, MCOperand Op1, SMLoc IDLoc, |
| 137 | const MCSubtargetInfo *STI); |
| 138 | void emitRI(unsigned Opcode, MCRegister Reg0, int32_t Imm, SMLoc IDLoc, |
| 139 | const MCSubtargetInfo *STI); |
| 140 | void emitRR(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, SMLoc IDLoc, |
| 141 | const MCSubtargetInfo *STI); |
| 142 | void emitRRX(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, MCOperand Op2, |
| 143 | SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 144 | void emitRRR(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, |
| 145 | MCRegister Reg2, SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 146 | void emitRRRX(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, |
| 147 | MCRegister Reg2, MCOperand Op3, SMLoc IDLoc, |
| 148 | const MCSubtargetInfo *STI); |
| 149 | void emitRRI(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, int16_t Imm, |
| 150 | SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 151 | void emitRRIII(unsigned Opcode, MCRegister Reg0, MCRegister Reg1, |
| 152 | int16_t Imm0, int16_t Imm1, int16_t Imm2, SMLoc IDLoc, |
| 153 | const MCSubtargetInfo *STI); |
| 154 | void emitAddu(MCRegister DstReg, MCRegister SrcReg, MCRegister TrgReg, |
| 155 | bool Is64Bit, const MCSubtargetInfo *STI); |
| 156 | void emitDSLL(MCRegister DstReg, MCRegister SrcReg, int16_t ShiftAmount, |
| 157 | SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 158 | void emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc, |
| 159 | const MCSubtargetInfo *STI); |
| 160 | void emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 161 | |
| 162 | /// Emit a store instruction with an offset. If the offset is out of range |
| 163 | /// then it will be synthesized using the assembler temporary. |
| 164 | /// |
| 165 | /// GetATReg() is a callback that can be used to obtain the current assembler |
| 166 | /// temporary and is only called when the assembler temporary is required. It |
| 167 | /// must handle the case where no assembler temporary is available (typically |
| 168 | /// by reporting an error). |
| 169 | void emitStoreWithImmOffset(unsigned Opcode, MCRegister SrcReg, |
| 170 | MCRegister BaseReg, int64_t Offset, |
| 171 | function_ref<MCRegister()> GetATReg, SMLoc IDLoc, |
| 172 | const MCSubtargetInfo *STI); |
| 173 | void emitLoadWithImmOffset(unsigned Opcode, MCRegister DstReg, |
| 174 | MCRegister BaseReg, int64_t Offset, |
| 175 | MCRegister TmpReg, SMLoc IDLoc, |
| 176 | const MCSubtargetInfo *STI); |
| 177 | void emitGPRestore(int Offset, SMLoc IDLoc, const MCSubtargetInfo *STI); |
| 178 | |
| 179 | void forbidModuleDirective() { ModuleDirectiveAllowed = false; } |
| 180 | void reallowModuleDirective() { ModuleDirectiveAllowed = true; } |
| 181 | bool isModuleDirectiveAllowed() { return ModuleDirectiveAllowed; } |
| 182 | |
| 183 | // This method enables template classes to set internal abi flags |
| 184 | // structure values. |
| 185 | template <class PredicateLibrary> |
| 186 | void updateABIInfo(const PredicateLibrary &P) { |
| 187 | ABI = P.getABI(); |
| 188 | ABIFlagsSection.setAllFromPredicates(P); |
| 189 | } |
| 190 | |
| 191 | MipsABIFlagsSection &getABIFlagsSection() { return ABIFlagsSection; } |
| 192 | const MipsABIInfo &getABI() const { |
| 193 | assert(ABI && "ABI hasn't been set!" ); |
| 194 | return *ABI; |
| 195 | } |
| 196 | |
| 197 | protected: |
| 198 | std::optional<MipsABIInfo> ABI; |
| 199 | MipsABIFlagsSection ABIFlagsSection; |
| 200 | |
| 201 | bool GPRInfoSet; |
| 202 | unsigned GPRBitMask; |
| 203 | int GPROffset; |
| 204 | |
| 205 | bool FPRInfoSet; |
| 206 | unsigned FPRBitMask; |
| 207 | int FPROffset; |
| 208 | |
| 209 | bool FrameInfoSet; |
| 210 | int FrameOffset; |
| 211 | unsigned FrameReg; |
| 212 | MCRegister GPReg; |
| 213 | unsigned ReturnReg; |
| 214 | |
| 215 | private: |
| 216 | bool ModuleDirectiveAllowed; |
| 217 | }; |
| 218 | |
| 219 | // This part is for ascii assembly output |
| 220 | class MipsTargetAsmStreamer : public MipsTargetStreamer { |
| 221 | formatted_raw_ostream &OS; |
| 222 | |
| 223 | public: |
| 224 | MipsTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS); |
| 225 | |
| 226 | void emitGPRel32Value(const MCExpr *) override; |
| 227 | void emitGPRel64Value(const MCExpr *) override; |
| 228 | void emitDTPRel32Value(const MCExpr *) override; |
| 229 | void emitDTPRel64Value(const MCExpr *) override; |
| 230 | void emitTPRel32Value(const MCExpr *) override; |
| 231 | void emitTPRel64Value(const MCExpr *) override; |
| 232 | |
| 233 | void emitDirectiveSetMicroMips() override; |
| 234 | void emitDirectiveSetNoMicroMips() override; |
| 235 | void emitDirectiveSetMips16() override; |
| 236 | void emitDirectiveSetNoMips16() override; |
| 237 | |
| 238 | void emitDirectiveSetReorder() override; |
| 239 | void emitDirectiveSetNoReorder() override; |
| 240 | void emitDirectiveSetMacro() override; |
| 241 | void emitDirectiveSetNoMacro() override; |
| 242 | void emitDirectiveSetMsa() override; |
| 243 | void emitDirectiveSetNoMsa() override; |
| 244 | void emitDirectiveSetMt() override; |
| 245 | void emitDirectiveSetNoMt() override; |
| 246 | void emitDirectiveSetCRC() override; |
| 247 | void emitDirectiveSetNoCRC() override; |
| 248 | void emitDirectiveSetVirt() override; |
| 249 | void emitDirectiveSetNoVirt() override; |
| 250 | void emitDirectiveSetGINV() override; |
| 251 | void emitDirectiveSetNoGINV() override; |
| 252 | void emitDirectiveSetEVA() override; |
| 253 | void emitDirectiveSetNoEVA() override; |
| 254 | void emitDirectiveSetAt() override; |
| 255 | void emitDirectiveSetAtWithArg(unsigned RegNo) override; |
| 256 | void emitDirectiveSetNoAt() override; |
| 257 | void emitDirectiveEnd(StringRef Name) override; |
| 258 | |
| 259 | void emitDirectiveEnt(const MCSymbol &Symbol) override; |
| 260 | void emitDirectiveAbiCalls() override; |
| 261 | void emitDirectiveNaN2008() override; |
| 262 | void emitDirectiveNaNLegacy() override; |
| 263 | void emitDirectiveOptionPic0() override; |
| 264 | void emitDirectiveOptionPic2() override; |
| 265 | void emitDirectiveInsn() override; |
| 266 | void emitFrame(MCRegister StackReg, unsigned StackSize, |
| 267 | MCRegister ReturnReg) override; |
| 268 | void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) override; |
| 269 | void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) override; |
| 270 | |
| 271 | void emitDirectiveSetArch(StringRef Arch) override; |
| 272 | void emitDirectiveSetMips0() override; |
| 273 | void emitDirectiveSetMips1() override; |
| 274 | void emitDirectiveSetMips2() override; |
| 275 | void emitDirectiveSetMips3() override; |
| 276 | void emitDirectiveSetMips4() override; |
| 277 | void emitDirectiveSetMips5() override; |
| 278 | void emitDirectiveSetMips32() override; |
| 279 | void emitDirectiveSetMips32R2() override; |
| 280 | void emitDirectiveSetMips32R3() override; |
| 281 | void emitDirectiveSetMips32R5() override; |
| 282 | void emitDirectiveSetMips32R6() override; |
| 283 | void emitDirectiveSetMips64() override; |
| 284 | void emitDirectiveSetMips64R2() override; |
| 285 | void emitDirectiveSetMips64R3() override; |
| 286 | void emitDirectiveSetMips64R5() override; |
| 287 | void emitDirectiveSetMips64R6() override; |
| 288 | void emitDirectiveSetDsp() override; |
| 289 | void emitDirectiveSetDspr2() override; |
| 290 | void emitDirectiveSetNoDsp() override; |
| 291 | void emitDirectiveSetMips3D() override; |
| 292 | void emitDirectiveSetNoMips3D() override; |
| 293 | void emitDirectiveSetPop() override; |
| 294 | void emitDirectiveSetPush() override; |
| 295 | void emitDirectiveSetSoftFloat() override; |
| 296 | void emitDirectiveSetHardFloat() override; |
| 297 | |
| 298 | // PIC support |
| 299 | void emitDirectiveCpAdd(MCRegister Reg) override; |
| 300 | void emitDirectiveCpLoad(MCRegister Reg) override; |
| 301 | void emitDirectiveCpLocal(MCRegister Reg) override; |
| 302 | |
| 303 | /// Emit a .cprestore directive. If the offset is out of range then it will |
| 304 | /// be synthesized using the assembler temporary. |
| 305 | /// |
| 306 | /// GetATReg() is a callback that can be used to obtain the current assembler |
| 307 | /// temporary and is only called when the assembler temporary is required. It |
| 308 | /// must handle the case where no assembler temporary is available (typically |
| 309 | /// by reporting an error). |
| 310 | bool emitDirectiveCpRestore(int Offset, function_ref<MCRegister()> GetATReg, |
| 311 | SMLoc IDLoc, const MCSubtargetInfo *STI) override; |
| 312 | void emitDirectiveCpsetup(MCRegister Reg, int RegOrOffset, |
| 313 | const MCSymbol &Sym, bool IsReg) override; |
| 314 | void emitDirectiveCpreturn(unsigned SaveLocation, |
| 315 | bool SaveLocationIsRegister) override; |
| 316 | |
| 317 | // FP abiflags directives |
| 318 | void emitDirectiveModuleFP() override; |
| 319 | void emitDirectiveModuleOddSPReg() override; |
| 320 | void emitDirectiveModuleSoftFloat() override; |
| 321 | void emitDirectiveModuleHardFloat() override; |
| 322 | void emitDirectiveModuleMT() override; |
| 323 | void emitDirectiveModuleCRC() override; |
| 324 | void emitDirectiveModuleNoCRC() override; |
| 325 | void emitDirectiveModuleVirt() override; |
| 326 | void emitDirectiveModuleNoVirt() override; |
| 327 | void emitDirectiveModuleGINV() override; |
| 328 | void emitDirectiveModuleNoGINV() override; |
| 329 | void emitDirectiveSetFp(MipsABIFlagsSection::FpABIKind Value) override; |
| 330 | void emitDirectiveSetOddSPReg() override; |
| 331 | void emitDirectiveSetNoOddSPReg() override; |
| 332 | }; |
| 333 | |
| 334 | // This part is for ELF object output |
| 335 | class MipsTargetELFStreamer : public MipsTargetStreamer { |
| 336 | enum class ISAMode { Standard, MicroMips, Mips16 }; |
| 337 | static ISAMode getISAMode(const MCSubtargetInfo &STI); |
| 338 | |
| 339 | const MipsMCOptions &CLOpts; |
| 340 | ISAMode Mode; |
| 341 | SmallVector<ISAMode, 4> ModeStack; |
| 342 | const MCSubtargetInfo &STI; |
| 343 | bool Pic; |
| 344 | |
| 345 | public: |
| 346 | bool isMicroMipsEnabled() const { return Mode == ISAMode::MicroMips; } |
| 347 | bool isMips16Enabled() const { return Mode == ISAMode::Mips16; } |
| 348 | MCELFStreamer &getStreamer(); |
| 349 | MipsTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI); |
| 350 | |
| 351 | void setPic(bool Value) override { Pic = Value; } |
| 352 | |
| 353 | void emitLabel(MCSymbol *Symbol) override; |
| 354 | void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override; |
| 355 | void finish() override; |
| 356 | |
| 357 | void emitGPRel32Value(const MCExpr *) override; |
| 358 | void emitGPRel64Value(const MCExpr *) override; |
| 359 | void emitDTPRel32Value(const MCExpr *) override; |
| 360 | void emitDTPRel64Value(const MCExpr *) override; |
| 361 | void emitTPRel32Value(const MCExpr *) override; |
| 362 | void emitTPRel64Value(const MCExpr *) override; |
| 363 | |
| 364 | void emitDirectiveSetMicroMips() override; |
| 365 | void emitDirectiveSetNoMicroMips() override; |
| 366 | void emitDirectiveSetPush() override; |
| 367 | void emitDirectiveSetPop() override; |
| 368 | void emitDirectiveSetMips0() override; |
| 369 | void setUsesMicroMips() override; |
| 370 | void emitDirectiveSetMips16() override; |
| 371 | void emitDirectiveSetNoMips16() override; |
| 372 | |
| 373 | void emitDirectiveSetNoReorder() override; |
| 374 | void emitDirectiveEnd(StringRef Name) override; |
| 375 | |
| 376 | void emitDirectiveEnt(const MCSymbol &Symbol) override; |
| 377 | void emitDirectiveAbiCalls() override; |
| 378 | void emitDirectiveNaN2008() override; |
| 379 | void emitDirectiveNaNLegacy() override; |
| 380 | void emitDirectiveOptionPic0() override; |
| 381 | void emitDirectiveOptionPic2() override; |
| 382 | void emitDirectiveInsn() override; |
| 383 | void emitFrame(MCRegister StackReg, unsigned StackSize, |
| 384 | MCRegister ReturnReg) override; |
| 385 | void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) override; |
| 386 | void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) override; |
| 387 | |
| 388 | // PIC support |
| 389 | void emitDirectiveCpAdd(MCRegister Reg) override; |
| 390 | void emitDirectiveCpLoad(MCRegister Reg) override; |
| 391 | void emitDirectiveCpLocal(MCRegister Reg) override; |
| 392 | bool emitDirectiveCpRestore(int Offset, function_ref<MCRegister()> GetATReg, |
| 393 | SMLoc IDLoc, const MCSubtargetInfo *STI) override; |
| 394 | void emitDirectiveCpsetup(MCRegister Reg, int RegOrOffset, |
| 395 | const MCSymbol &Sym, bool IsReg) override; |
| 396 | void emitDirectiveCpreturn(unsigned SaveLocation, |
| 397 | bool SaveLocationIsRegister) override; |
| 398 | |
| 399 | void emitMipsAbiFlags(); |
| 400 | }; |
| 401 | } |
| 402 | #endif |
| 403 | |