1//===- MipsABIFlagsSection.h - Mips ELF ABI Flags Section -------*- 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_MCTARGETDESC_MIPSABIFLAGSSECTION_H
10#define LLVM_LIB_TARGET_MIPS_MCTARGETDESC_MIPSABIFLAGSSECTION_H
11
12#include "llvm/Support/ErrorHandling.h"
13#include "llvm/Support/MipsABIFlags.h"
14#include <cstdint>
15
16namespace llvm {
17
18class MCStreamer;
19class StringRef;
20
21struct MipsABIFlagsSection {
22 // Internal representation of the fp_abi related values used in .module.
23 enum class FpABIKind { ANY, XX, S32, S64, SOFT, SINGLE };
24
25 // Version of flags structure.
26 uint16_t Version = 0;
27 // The level of the ISA: 1-5, 32, 64.
28 uint8_t ISALevel = 0;
29 // The revision of ISA: 0 for MIPS V and below, 1-n otherwise.
30 uint8_t ISARevision = 0;
31 // The size of general purpose registers.
32 Mips::AFL_REG GPRSize = Mips::AFL_REG_NONE;
33 // The size of co-processor 1 registers.
34 Mips::AFL_REG CPR1Size = Mips::AFL_REG_NONE;
35 // The size of co-processor 2 registers.
36 Mips::AFL_REG CPR2Size = Mips::AFL_REG_NONE;
37 // Processor-specific extension.
38 Mips::AFL_EXT ISAExtension = Mips::AFL_EXT_NONE;
39 // Mask of ASEs used.
40 uint32_t ASESet = 0;
41
42 bool OddSPReg = false;
43
44 bool Is32BitABI = false;
45
46protected:
47 // The floating-point ABI.
48 FpABIKind FpABI = FpABIKind::ANY;
49
50public:
51 MipsABIFlagsSection() = default;
52
53 uint16_t getVersionValue() { return (uint16_t)Version; }
54 uint8_t getISALevelValue() { return (uint8_t)ISALevel; }
55 uint8_t getISARevisionValue() { return (uint8_t)ISARevision; }
56 uint8_t getGPRSizeValue() { return (uint8_t)GPRSize; }
57 uint8_t getCPR1SizeValue();
58 uint8_t getCPR2SizeValue() { return (uint8_t)CPR2Size; }
59 uint8_t getFpABIValue();
60 uint32_t getISAExtensionValue() { return (uint32_t)ISAExtension; }
61 uint32_t getASESetValue() { return (uint32_t)ASESet; }
62
63 uint32_t getFlags1Value() {
64 uint32_t Value = 0;
65
66 if (OddSPReg)
67 Value |= (uint32_t)Mips::AFL_FLAGS1_ODDSPREG;
68
69 return Value;
70 }
71
72 uint32_t getFlags2Value() { return 0; }
73
74 FpABIKind getFpABI() { return FpABI; }
75 void setFpABI(FpABIKind Value, bool IsABI32Bit) {
76 FpABI = Value;
77 Is32BitABI = IsABI32Bit;
78 }
79
80 StringRef getFpABIString(FpABIKind Value);
81
82 template <class PredicateLibrary>
83 void setISALevelAndRevisionFromPredicates(const PredicateLibrary &P) {
84 if (P.hasNanoMips()) {
85 ISALevel = 32;
86 ISARevision = 6;
87 } else if (P.hasMips64()) {
88 ISALevel = 64;
89 if (P.hasMips64r6())
90 ISARevision = 6;
91 else if (P.hasMips64r5())
92 ISARevision = 5;
93 else if (P.hasMips64r3())
94 ISARevision = 3;
95 else if (P.hasMips64r2())
96 ISARevision = 2;
97 else
98 ISARevision = 1;
99 } else if (P.hasMips32()) {
100 ISALevel = 32;
101 if (P.hasMips32r6())
102 ISARevision = 6;
103 else if (P.hasMips32r5())
104 ISARevision = 5;
105 else if (P.hasMips32r3())
106 ISARevision = 3;
107 else if (P.hasMips32r2())
108 ISARevision = 2;
109 else
110 ISARevision = 1;
111 } else {
112 ISARevision = 0;
113 if (P.hasMips5())
114 ISALevel = 5;
115 else if (P.hasMips4())
116 ISALevel = 4;
117 else if (P.hasMips3())
118 ISALevel = 3;
119 else if (P.hasMips2())
120 ISALevel = 2;
121 else if (P.hasMips1())
122 ISALevel = 1;
123 else
124 llvm_unreachable("Unknown ISA level!");
125 }
126 }
127
128 template <class PredicateLibrary>
129 void setGPRSizeFromPredicates(const PredicateLibrary &P) {
130 GPRSize = P.isGP64bit() ? Mips::AFL_REG_64 : Mips::AFL_REG_32;
131 }
132
133 template <class PredicateLibrary>
134 void setCPR1SizeFromPredicates(const PredicateLibrary &P) {
135 if (P.useSoftFloat())
136 CPR1Size = Mips::AFL_REG_NONE;
137 else if (P.hasMSA())
138 CPR1Size = Mips::AFL_REG_128;
139 else if (P.isSingleFloat())
140 CPR1Size = Mips::AFL_REG_32;
141 else
142 CPR1Size = P.isFP64bit() ? Mips::AFL_REG_64 : Mips::AFL_REG_32;
143 }
144
145 template <class PredicateLibrary>
146 void setISAExtensionFromPredicates(const PredicateLibrary &P) {
147 if (P.hasCnMipsP())
148 ISAExtension = Mips::AFL_EXT_OCTEONP;
149 else if (P.hasCnMips())
150 ISAExtension = Mips::AFL_EXT_OCTEON;
151 else if (P.isR5900())
152 ISAExtension = Mips::AFL_EXT_5900;
153 else
154 ISAExtension = Mips::AFL_EXT_NONE;
155 }
156
157 template <class PredicateLibrary>
158 void setASESetFromPredicates(const PredicateLibrary &P) {
159 ASESet = 0;
160 if (P.hasDSP())
161 ASESet |= Mips::AFL_ASE_DSP;
162 if (P.hasDSPR2())
163 ASESet |= Mips::AFL_ASE_DSPR2;
164 if (P.hasMSA())
165 ASESet |= Mips::AFL_ASE_MSA;
166 if (P.inMicroMipsMode())
167 ASESet |= Mips::AFL_ASE_MICROMIPS;
168 if (P.inMips16Mode())
169 ASESet |= Mips::AFL_ASE_MIPS16;
170 if (P.hasMT())
171 ASESet |= Mips::AFL_ASE_MT;
172 if (P.hasCRC())
173 ASESet |= Mips::AFL_ASE_CRC;
174 if (P.hasVirt())
175 ASESet |= Mips::AFL_ASE_VIRT;
176 if (P.hasGINV())
177 ASESet |= Mips::AFL_ASE_GINV;
178 }
179
180 template <class PredicateLibrary>
181 void setFpAbiFromPredicates(const PredicateLibrary &P) {
182 Is32BitABI = P.isABI_O32();
183
184 FpABI = FpABIKind::ANY;
185 if (P.useSoftFloat())
186 FpABI = FpABIKind::SOFT;
187 else if (P.isSingleFloat())
188 FpABI = FpABIKind::SINGLE;
189 else if (P.isABI_N32() || P.isABI_N64())
190 FpABI = FpABIKind::S64;
191 else if (P.isABI_O32()) {
192 if (P.isABI_FPXX())
193 FpABI = FpABIKind::XX;
194 else if (P.isFP64bit())
195 FpABI = FpABIKind::S64;
196 else
197 FpABI = FpABIKind::S32;
198 }
199 }
200
201 template <class PredicateLibrary>
202 void setAllFromPredicates(const PredicateLibrary &P) {
203 setISALevelAndRevisionFromPredicates(P);
204 setGPRSizeFromPredicates(P);
205 setCPR1SizeFromPredicates(P);
206 setISAExtensionFromPredicates(P);
207 setASESetFromPredicates(P);
208 setFpAbiFromPredicates(P);
209 OddSPReg = P.useOddSPReg();
210 }
211};
212
213MCStreamer &operator<<(MCStreamer &OS, MipsABIFlagsSection &ABIFlagsSection);
214
215} // end namespace llvm
216
217#endif // LLVM_LIB_TARGET_MIPS_MCTARGETDESC_MIPSABIFLAGSSECTION_H
218