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