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