a0edc6f85d8164f9ae9f6a863652cd41cdc3e162
[oota-llvm.git] / lib / Target / Mips / MipsTargetStreamer.h
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 MIPSTARGETSTREAMER_H
11 #define MIPSTARGETSTREAMER_H
12
13 #include "llvm/MC/MCELFStreamer.h"
14 #include "llvm/MC/MCStreamer.h"
15
16 namespace llvm {
17 struct Elf_Internal_ABIFlags_v0 {
18   // Version of flags structure.
19   uint16_t version;
20   // The level of the ISA: 1-5, 32, 64.
21   uint8_t isa_level;
22   // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
23   uint8_t isa_rev;
24   // The size of general purpose registers.
25   uint8_t gpr_size;
26   // The size of co-processor 1 registers.
27   uint8_t cpr1_size;
28   // The size of co-processor 2 registers.
29   uint8_t cpr2_size;
30   // The floating-point ABI.
31   uint8_t fp_abi;
32   // Processor-specific extension.
33   uint32_t isa_ext;
34   // Mask of ASEs used.
35   uint32_t ases;
36   // Mask of general flags.
37   uint32_t flags1;
38   uint32_t flags2;
39
40   Elf_Internal_ABIFlags_v0()
41       : version(0), isa_level(0), isa_rev(0), gpr_size(0), cpr1_size(0),
42         cpr2_size(0), fp_abi(0), isa_ext(0), ases(0), flags1(0), flags2(0) {}
43 };
44
45 // Values for the xxx_size bytes of an ABI flags structure.
46 enum {
47   AFL_REG_NONE = 0x00, // No registers.
48   AFL_REG_32 = 0x01,   // 32-bit registers.
49   AFL_REG_64 = 0x02,   // 64-bit registers.
50   AFL_REG_128 = 0x03   // 128-bit registers.
51 };
52
53 // Masks for the ases word of an ABI flags structure.
54 enum {
55   AFL_ASE_DSP = 0x00000001,       // DSP ASE.
56   AFL_ASE_DSPR2 = 0x00000002,     // DSP R2 ASE.
57   AFL_ASE_EVA = 0x00000004,       // Enhanced VA Scheme.
58   AFL_ASE_MCU = 0x00000008,       // MCU (MicroController) ASE.
59   AFL_ASE_MDMX = 0x00000010,      // MDMX ASE.
60   AFL_ASE_MIPS3D = 0x00000020,    // MIPS-3D ASE.
61   AFL_ASE_MT = 0x00000040,        // MT ASE.
62   AFL_ASE_SMARTMIPS = 0x00000080, // SmartMIPS ASE.
63   AFL_ASE_VIRT = 0x00000100,      // VZ ASE.
64   AFL_ASE_MSA = 0x00000200,       // MSA ASE.
65   AFL_ASE_MIPS16 = 0x00000400,    // MIPS16 ASE.
66   AFL_ASE_MICROMIPS = 0x00000800, // MICROMIPS ASE.
67   AFL_ASE_XPA = 0x00001000        // XPA ASE.
68 };
69
70 // Values for the isa_ext word of an ABI flags structure.
71 enum {
72   AFL_EXT_XLR = 1,          // RMI Xlr instruction.
73   AFL_EXT_OCTEON2 = 2,      // Cavium Networks Octeon2.
74   AFL_EXT_OCTEONP = 3,      // Cavium Networks OcteonP.
75   AFL_EXT_LOONGSON_3A = 4,  // Loongson 3A.
76   AFL_EXT_OCTEON = 5,       // Cavium Networks Octeon.
77   AFL_EXT_5900 = 6,         // MIPS R5900 instruction.
78   AFL_EXT_4650 = 7,         // MIPS R4650 instruction.
79   AFL_EXT_4010 = 8,         // LSI R4010 instruction.
80   AFL_EXT_4100 = 9,         // NEC VR4100 instruction.
81   AFL_EXT_3900 = 10,        // Toshiba R3900 instruction.
82   AFL_EXT_10000 = 11,       // MIPS R10000 instruction.
83   AFL_EXT_SB1 = 12,         // Broadcom SB-1 instruction.
84   AFL_EXT_4111 = 13,        // NEC VR4111/VR4181 instruction.
85   AFL_EXT_4120 = 14,        // NEC VR4120 instruction.
86   AFL_EXT_5400 = 15,        // NEC VR5400 instruction.
87   AFL_EXT_5500 = 16,        // NEC VR5500 instruction.
88   AFL_EXT_LOONGSON_2E = 17, // ST Microelectronics Loongson 2E.
89   AFL_EXT_LOONGSON_2F = 18  // ST Microelectronics Loongson 2F.
90 };
91
92 // Values for the fp_abi word of an ABI flags structure.
93 enum {
94   Val_GNU_MIPS_ABI_FP_DOUBLE = 1,
95   Val_GNU_MIPS_ABI_FP_XX = 5,
96   Val_GNU_MIPS_ABI_FP_64 = 6
97 };
98
99 class MipsTargetStreamer : public MCTargetStreamer {
100 public:
101   MipsTargetStreamer(MCStreamer &S);
102   virtual void emitDirectiveSetMicroMips();
103   virtual void emitDirectiveSetNoMicroMips();
104   virtual void emitDirectiveSetMips16();
105   virtual void emitDirectiveSetNoMips16();
106
107   virtual void emitDirectiveSetReorder();
108   virtual void emitDirectiveSetNoReorder();
109   virtual void emitDirectiveSetMacro();
110   virtual void emitDirectiveSetNoMacro();
111   virtual void emitDirectiveSetAt();
112   virtual void emitDirectiveSetNoAt();
113   virtual void emitDirectiveEnd(StringRef Name);
114
115   virtual void emitDirectiveEnt(const MCSymbol &Symbol);
116   virtual void emitDirectiveAbiCalls();
117   virtual void emitDirectiveNaN2008();
118   virtual void emitDirectiveNaNLegacy();
119   virtual void emitDirectiveOptionPic0();
120   virtual void emitDirectiveOptionPic2();
121   virtual void emitFrame(unsigned StackReg, unsigned StackSize,
122                          unsigned ReturnReg);
123   virtual void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff);
124   virtual void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff);
125
126   virtual void emitDirectiveSetMips32R2();
127   virtual void emitDirectiveSetMips64();
128   virtual void emitDirectiveSetMips64R2();
129   virtual void emitDirectiveSetDsp();
130
131   // PIC support
132   virtual void emitDirectiveCpload(unsigned RegNo);
133   virtual void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
134                                     const MCSymbol &Sym, bool IsReg);
135   // ABI Flags
136   virtual void emitDirectiveModule(unsigned Value, bool is32BitAbi){};
137   virtual void emitDirectiveSetFp(unsigned Value, bool is32BitAbi){};
138   virtual void emitMipsAbiFlags(){};
139   void setCanHaveModuleDir(bool Can) { canHaveModuleDirective = Can; }
140   bool getCanHaveModuleDir() { return canHaveModuleDirective; }
141
142   void setVersion(uint16_t Version) { MipsABIFlags.version = Version; }
143   void setISALevel(uint8_t Level) { MipsABIFlags.isa_level = Level; }
144   void setISARev(uint8_t Rev) { MipsABIFlags.isa_rev = Rev; }
145   void setGprSize(uint8_t Size) { MipsABIFlags.gpr_size = Size; }
146   void setCpr1Size(uint8_t Size) { MipsABIFlags.cpr1_size = Size; }
147   void setCpr2Size(uint8_t Size) { MipsABIFlags.cpr2_size = Size; }
148   void setFpABI(uint8_t Abi) { MipsABIFlags.fp_abi = Abi; }
149   void setIsaExt(uint32_t IsaExt) { MipsABIFlags.isa_ext = IsaExt; }
150   void setASEs(uint32_t Ases) { MipsABIFlags.ases = Ases; }
151   void setFlags1(uint32_t Flags) { MipsABIFlags.flags1 = Flags; }
152   void setFlags2(uint32_t Flags) { MipsABIFlags.flags2 = Flags; }
153
154   uint8_t getFPAbi() { return MipsABIFlags.fp_abi; }
155   // This method enables template classes to set internal abi flags
156   // structure values.
157   template <class PredicateLibrary>
158   void updateABIInfo(const PredicateLibrary &P) {
159     setVersion(0); // Version, default value is 0.
160
161     if (P.hasMips64()) { // isa_level
162       setISALevel(64);
163       if (P.hasMips64r6())
164         setISARev(6);
165       else if (P.hasMips64r2())
166         setISARev(2);
167       else
168         setISARev(1);
169     } else if (P.hasMips32()) {
170       setISALevel(32);
171       if (P.hasMips32r6())
172         setISARev(6);
173       else if (P.hasMips32r2())
174         setISARev(2);
175       else
176         setISARev(1);
177     } else {
178       setISARev(0);
179       if (P.hasMips5())
180         setISALevel(5);
181       else if (P.hasMips4())
182         setISALevel(4);
183       else if (P.hasMips3())
184         setISALevel(3);
185       else if (P.hasMips2())
186         setISALevel(2);
187       else if (P.hasMips1())
188         setISALevel(1);
189       else
190         llvm_unreachable("Unknown ISA level!");
191     }
192
193     if (P.isGP64bit()) // GPR size.
194       setGprSize(AFL_REG_64);
195     else
196       setGprSize(AFL_REG_32);
197
198     // TODO: check for MSA128 value.
199     if (P.mipsSEUsesSoftFloat())
200       setCpr1Size(AFL_REG_NONE);
201     else if (P.isFP64bit())
202       setCpr1Size(AFL_REG_64);
203     else
204       setCpr1Size(AFL_REG_32);
205     setCpr2Size(AFL_REG_NONE); // Default value.
206
207     // Set ASE.
208     unsigned AseFlags = 0;
209     if (P.hasDSP())
210       AseFlags |= AFL_ASE_DSP;
211     if (P.hasDSPR2())
212       AseFlags |= AFL_ASE_DSPR2;
213     if (P.hasMSA())
214       AseFlags |= AFL_ASE_MSA;
215     if (P.inMicroMipsMode())
216       AseFlags |= AFL_ASE_MICROMIPS;
217     if (P.inMips16Mode())
218       AseFlags |= AFL_ASE_MIPS16;
219
220     if (P.isABI_N32() || P.isABI_N64())
221       setFpABI(Val_GNU_MIPS_ABI_FP_DOUBLE);
222     else if (P.isABI_O32()) {
223       if (P.isFP64bit())
224         setFpABI(Val_GNU_MIPS_ABI_FP_64);
225       else if (P.isABI_FPXX())
226         setFpABI(Val_GNU_MIPS_ABI_FP_XX);
227       else
228         setFpABI(Val_GNU_MIPS_ABI_FP_DOUBLE);
229     } else
230       setFpABI(0); // Default value.
231   }
232
233 protected:
234   Elf_Internal_ABIFlags_v0 MipsABIFlags;
235
236 private:
237   bool canHaveModuleDirective;
238 };
239
240 // This part is for ascii assembly output
241 class MipsTargetAsmStreamer : public MipsTargetStreamer {
242   formatted_raw_ostream &OS;
243
244 public:
245   MipsTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS);
246   void emitDirectiveSetMicroMips() override;
247   void emitDirectiveSetNoMicroMips() override;
248   void emitDirectiveSetMips16() override;
249   void emitDirectiveSetNoMips16() override;
250
251   void emitDirectiveSetReorder() override;
252   void emitDirectiveSetNoReorder() override;
253   void emitDirectiveSetMacro() override;
254   void emitDirectiveSetNoMacro() override;
255   void emitDirectiveSetAt() override;
256   void emitDirectiveSetNoAt() override;
257   void emitDirectiveEnd(StringRef Name) override;
258
259   void emitDirectiveEnt(const MCSymbol &Symbol) override;
260   void emitDirectiveAbiCalls() override;
261   void emitDirectiveNaN2008() override;
262   void emitDirectiveNaNLegacy() override;
263   void emitDirectiveOptionPic0() override;
264   void emitDirectiveOptionPic2() override;
265   void emitFrame(unsigned StackReg, unsigned StackSize,
266                  unsigned ReturnReg) override;
267   void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) override;
268   void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) override;
269
270   void emitDirectiveSetMips32R2() override;
271   void emitDirectiveSetMips64() override;
272   void emitDirectiveSetMips64R2() override;
273   void emitDirectiveSetDsp() override;
274
275   // PIC support
276   virtual void emitDirectiveCpload(unsigned RegNo);
277   void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
278                             const MCSymbol &Sym, bool IsReg) override;
279
280   // ABI Flags
281   void emitDirectiveModule(unsigned Value, bool is32BitAbi) override;
282   void emitDirectiveSetFp(unsigned Value, bool is32BitAbi) override;
283   void emitMipsAbiFlags() override;
284 };
285
286 // This part is for ELF object output
287 class MipsTargetELFStreamer : public MipsTargetStreamer {
288   bool MicroMipsEnabled;
289   const MCSubtargetInfo &STI;
290   bool Pic;
291
292 public:
293   bool isMicroMipsEnabled() const { return MicroMipsEnabled; }
294   MCELFStreamer &getStreamer();
295   MipsTargetELFStreamer(MCStreamer &S, const MCSubtargetInfo &STI);
296
297   void emitLabel(MCSymbol *Symbol) override;
298   void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
299   void finish() override;
300
301   void emitDirectiveSetMicroMips() override;
302   void emitDirectiveSetNoMicroMips() override;
303   void emitDirectiveSetMips16() override;
304   void emitDirectiveSetNoMips16() override;
305
306   void emitDirectiveSetReorder() override;
307   void emitDirectiveSetNoReorder() override;
308   void emitDirectiveSetMacro() override;
309   void emitDirectiveSetNoMacro() override;
310   void emitDirectiveSetAt() override;
311   void emitDirectiveSetNoAt() override;
312   void emitDirectiveEnd(StringRef Name) override;
313
314   void emitDirectiveEnt(const MCSymbol &Symbol) override;
315   void emitDirectiveAbiCalls() override;
316   void emitDirectiveNaN2008() override;
317   void emitDirectiveNaNLegacy() override;
318   void emitDirectiveOptionPic0() override;
319   void emitDirectiveOptionPic2() override;
320   void emitFrame(unsigned StackReg, unsigned StackSize,
321                  unsigned ReturnReg) override;
322   void emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) override;
323   void emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) override;
324
325   void emitDirectiveSetMips32R2() override;
326   void emitDirectiveSetMips64() override;
327   void emitDirectiveSetMips64R2() override;
328   void emitDirectiveSetDsp() override;
329
330   // PIC support
331   virtual void emitDirectiveCpload(unsigned RegNo);
332   void emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset,
333                             const MCSymbol &Sym, bool IsReg) override;
334
335   // ABI Flags
336   void emitMipsAbiFlags() override;
337
338 protected:
339   bool isO32() const { return STI.getFeatureBits() & Mips::FeatureO32; }
340   bool isN32() const { return STI.getFeatureBits() & Mips::FeatureN32; }
341   bool isN64() const { return STI.getFeatureBits() & Mips::FeatureN64; }
342 };
343 }
344 #endif