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