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