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