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/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   virtual void emitDirectiveSetMicroMips();
28   virtual void emitDirectiveSetNoMicroMips();
29   virtual void emitDirectiveSetMips16();
30   virtual void emitDirectiveSetNoMips16();
31 
32   virtual void emitDirectiveSetReorder();
33   virtual void emitDirectiveSetNoReorder();
34   virtual void emitDirectiveSetMacro();
35   virtual void emitDirectiveSetNoMacro();
36   virtual void emitDirectiveSetMsa();
37   virtual void emitDirectiveSetNoMsa();
38   virtual void emitDirectiveSetAt();
39   virtual void emitDirectiveSetAtWithArg(unsigned RegNo);
40   virtual void emitDirectiveSetNoAt();
41   virtual void emitDirectiveEnd(StringRef Name);
42 
43   virtual void emitDirectiveEnt(const MCSymbol &Symbol);
44   virtual void emitDirectiveAbiCalls();
45   virtual void emitDirectiveNaN2008();
46   virtual void emitDirectiveNaNLegacy();
47   virtual void emitDirectiveOptionPic0();
48   virtual void emitDirectiveOptionPic2();
49   virtual void emitDirectiveInsn();
50   virtual void emitFrame(unsigned StackReg, unsigned StackSize,
51                          unsigned ReturnReg);
52   virtual void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff);
53   virtual void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff);
54 
55   virtual void emitDirectiveSetArch(StringRef Arch);
56   virtual void emitDirectiveSetMips0();
57   virtual void emitDirectiveSetMips1();
58   virtual void emitDirectiveSetMips2();
59   virtual void emitDirectiveSetMips3();
60   virtual void emitDirectiveSetMips4();
61   virtual void emitDirectiveSetMips5();
62   virtual void emitDirectiveSetMips32();
63   virtual void emitDirectiveSetMips32R2();
64   virtual void emitDirectiveSetMips32R3();
65   virtual void emitDirectiveSetMips32R5();
66   virtual void emitDirectiveSetMips32R6();
67   virtual void emitDirectiveSetMips64();
68   virtual void emitDirectiveSetMips64R2();
69   virtual void emitDirectiveSetMips64R3();
70   virtual void emitDirectiveSetMips64R5();
71   virtual void emitDirectiveSetMips64R6();
72   virtual void emitDirectiveSetDsp();
73   virtual void emitDirectiveSetNoDsp();
74   virtual void emitDirectiveSetPop();
75   virtual void emitDirectiveSetPush();
76   virtual void emitDirectiveSetSoftFloat();
77   virtual void emitDirectiveSetHardFloat();
78 
79   // PIC support
80   virtual void emitDirectiveCpLoad(unsigned RegNo);
81   virtual void emitDirectiveCpRestore(SmallVector<MCInst, 3> &StoreInsts,
82                                       int Offset);
83   virtual void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
84                                     const MCSymbol &Sym, bool IsReg);
85   virtual void emitDirectiveCpreturn(unsigned SaveLocation,
86                                      bool SaveLocationIsRegister);
87 
88   // FP abiflags directives
89   virtual void emitDirectiveModuleFP();
90   virtual void emitDirectiveModuleOddSPReg();
91   virtual void emitDirectiveModuleSoftFloat();
92   virtual void emitDirectiveModuleHardFloat();
93   virtual void emitDirectiveSetFp(MipsABIFlagsSection::FpABIKind Value);
94   virtual void emitDirectiveSetOddSPReg();
95   virtual void emitDirectiveSetNoOddSPReg();
96 
forbidModuleDirective()97   void forbidModuleDirective() { ModuleDirectiveAllowed = false; }
reallowModuleDirective()98   void reallowModuleDirective() { ModuleDirectiveAllowed = true; }
isModuleDirectiveAllowed()99   bool isModuleDirectiveAllowed() { return ModuleDirectiveAllowed; }
100 
101   // This method enables template classes to set internal abi flags
102   // structure values.
103   template <class PredicateLibrary>
updateABIInfo(const PredicateLibrary & P)104   void updateABIInfo(const PredicateLibrary &P) {
105     ABI = P.getABI();
106     ABIFlagsSection.setAllFromPredicates(P);
107   }
108 
getABIFlagsSection()109   MipsABIFlagsSection &getABIFlagsSection() { return ABIFlagsSection; }
getABI()110   const MipsABIInfo &getABI() const {
111     assert(ABI.hasValue() && "ABI hasn't been set!");
112     return *ABI;
113   }
114 
115 protected:
116   llvm::Optional<MipsABIInfo> ABI;
117   MipsABIFlagsSection ABIFlagsSection;
118 
119   bool GPRInfoSet;
120   unsigned GPRBitMask;
121   int GPROffset;
122 
123   bool FPRInfoSet;
124   unsigned FPRBitMask;
125   int FPROffset;
126 
127   bool FrameInfoSet;
128   int FrameOffset;
129   unsigned FrameReg;
130   unsigned ReturnReg;
131 
132 private:
133   bool ModuleDirectiveAllowed;
134 };
135 
136 // This part is for ascii assembly output
137 class MipsTargetAsmStreamer : public MipsTargetStreamer {
138   formatted_raw_ostream &OS;
139 
140 public:
141   MipsTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
142   void emitDirectiveSetMicroMips() override;
143   void emitDirectiveSetNoMicroMips() override;
144   void emitDirectiveSetMips16() override;
145   void emitDirectiveSetNoMips16() override;
146 
147   void emitDirectiveSetReorder() override;
148   void emitDirectiveSetNoReorder() override;
149   void emitDirectiveSetMacro() override;
150   void emitDirectiveSetNoMacro() override;
151   void emitDirectiveSetMsa() override;
152   void emitDirectiveSetNoMsa() override;
153   void emitDirectiveSetAt() override;
154   void emitDirectiveSetAtWithArg(unsigned RegNo) override;
155   void emitDirectiveSetNoAt() override;
156   void emitDirectiveEnd(StringRef Name) override;
157 
158   void emitDirectiveEnt(const MCSymbol &Symbol) override;
159   void emitDirectiveAbiCalls() override;
160   void emitDirectiveNaN2008() override;
161   void emitDirectiveNaNLegacy() override;
162   void emitDirectiveOptionPic0() override;
163   void emitDirectiveOptionPic2() override;
164   void emitDirectiveInsn() override;
165   void emitFrame(unsigned StackReg, unsigned StackSize,
166                  unsigned ReturnReg) override;
167   void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) override;
168   void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) override;
169 
170   void emitDirectiveSetArch(StringRef Arch) override;
171   void emitDirectiveSetMips0() override;
172   void emitDirectiveSetMips1() override;
173   void emitDirectiveSetMips2() override;
174   void emitDirectiveSetMips3() override;
175   void emitDirectiveSetMips4() override;
176   void emitDirectiveSetMips5() override;
177   void emitDirectiveSetMips32() override;
178   void emitDirectiveSetMips32R2() override;
179   void emitDirectiveSetMips32R3() override;
180   void emitDirectiveSetMips32R5() override;
181   void emitDirectiveSetMips32R6() override;
182   void emitDirectiveSetMips64() override;
183   void emitDirectiveSetMips64R2() override;
184   void emitDirectiveSetMips64R3() override;
185   void emitDirectiveSetMips64R5() override;
186   void emitDirectiveSetMips64R6() override;
187   void emitDirectiveSetDsp() override;
188   void emitDirectiveSetNoDsp() override;
189   void emitDirectiveSetPop() override;
190   void emitDirectiveSetPush() override;
191   void emitDirectiveSetSoftFloat() override;
192   void emitDirectiveSetHardFloat() override;
193 
194   // PIC support
195   void emitDirectiveCpLoad(unsigned RegNo) override;
196   void emitDirectiveCpRestore(SmallVector<MCInst, 3> &StoreInsts,
197                               int Offset) override;
198   void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
199                             const MCSymbol &Sym, bool IsReg) override;
200   void emitDirectiveCpreturn(unsigned SaveLocation,
201                              bool SaveLocationIsRegister) override;
202 
203   // FP abiflags directives
204   void emitDirectiveModuleFP() override;
205   void emitDirectiveModuleOddSPReg() override;
206   void emitDirectiveModuleSoftFloat() override;
207   void emitDirectiveModuleHardFloat() override;
208   void emitDirectiveSetFp(MipsABIFlagsSection::FpABIKind Value) override;
209   void emitDirectiveSetOddSPReg() override;
210   void emitDirectiveSetNoOddSPReg() override;
211 };
212 
213 // This part is for ELF object output
214 class MipsTargetELFStreamer : public MipsTargetStreamer {
215   bool MicroMipsEnabled;
216   const MCSubtargetInfo &STI;
217   bool Pic;
218 
219 public:
isMicroMipsEnabled()220   bool isMicroMipsEnabled() const { return MicroMipsEnabled; }
221   MCELFStreamer &getStreamer();
222   MipsTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
223 
224   void emitLabel(MCSymbol *Symbol) override;
225   void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
226   void finish() override;
227 
228   void emitDirectiveSetMicroMips() override;
229   void emitDirectiveSetNoMicroMips() override;
230   void emitDirectiveSetMips16() override;
231 
232   void emitDirectiveSetNoReorder() 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   // PIC support
248   void emitDirectiveCpLoad(unsigned RegNo) override;
249   void emitDirectiveCpRestore(SmallVector<MCInst, 3> &StoreInsts,
250                               int Offset) override;
251   void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
252                             const MCSymbol &Sym, bool IsReg) override;
253   void emitDirectiveCpreturn(unsigned SaveLocation,
254                              bool SaveLocationIsRegister) override;
255 
256   void emitMipsAbiFlags();
257 };
258 }
259 #endif
260