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