1//===- MipsMCCodeEmitter.h - Convert Mips Code to Machine Code --*- 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// This file defines the MipsMCCodeEmitter class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_MIPS_MCTARGETDESC_MIPSMCCODEEMITTER_H
14#define LLVM_LIB_TARGET_MIPS_MCTARGETDESC_MIPSMCCODEEMITTER_H
15
16#include "llvm/MC/MCCodeEmitter.h"
17#include <cstdint>
18
19namespace llvm {
20
21class MCContext;
22class MCExpr;
23class MCFixup;
24class MCInst;
25class MCInstrInfo;
26class MCOperand;
27class MCSubtargetInfo;
28class raw_ostream;
29
30class MipsMCCodeEmitter : public MCCodeEmitter {
31 const MCInstrInfo &MCII;
32 MCContext &Ctx;
33 bool IsLittleEndian;
34
35 bool isMicroMips(const MCSubtargetInfo &STI) const;
36 bool isMips32r6(const MCSubtargetInfo &STI) const;
37 bool isNanoMips(const MCSubtargetInfo &STI) const;
38
39public:
40 MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_, bool IsLittle)
41 : MCII(mcii), Ctx(Ctx_), IsLittleEndian(IsLittle) {}
42 MipsMCCodeEmitter(const MipsMCCodeEmitter &) = delete;
43 MipsMCCodeEmitter &operator=(const MipsMCCodeEmitter &) = delete;
44 ~MipsMCCodeEmitter() override = default;
45
46 void EmitByte(unsigned char C, raw_ostream &OS) const;
47
48 void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
49 SmallVectorImpl<MCFixup> &Fixups,
50 const MCSubtargetInfo &STI) const override;
51
52 // getBinaryCodeForInstr - TableGen'erated function for getting the
53 // binary encoding for an instruction.
54 uint64_t getBinaryCodeForInstr(const MCInst &MI,
55 SmallVectorImpl<MCFixup> &Fixups,
56 const MCSubtargetInfo &STI) const;
57
58 // getJumpTargetOpValue - Return binary encoding of the jump
59 // target operand. If the machine operand requires relocation,
60 // record the relocation and return zero.
61 unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups,
63 const MCSubtargetInfo &STI) const;
64
65 // getBranchJumpOpValueMM - Return binary encoding of the microMIPS jump
66 // target operand. If the machine operand requires relocation,
67 // record the relocation and return zero.
68 unsigned getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo,
69 SmallVectorImpl<MCFixup> &Fixups,
70 const MCSubtargetInfo &STI) const;
71
72 // getUImm5Lsl2Encoding - Return binary encoding of the microMIPS jump
73 // target operand.
74 unsigned getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo,
75 SmallVectorImpl<MCFixup> &Fixups,
76 const MCSubtargetInfo &STI) const;
77
78 unsigned getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
81
82 unsigned getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo,
83 SmallVectorImpl<MCFixup> &Fixups,
84 const MCSubtargetInfo &STI) const;
85
86 // getSImm9AddiuspValue - Return binary encoding of the microMIPS addiusp
87 // instruction immediate operand.
88 unsigned getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
89 SmallVectorImpl<MCFixup> &Fixups,
90 const MCSubtargetInfo &STI) const;
91
92 // getBranchTargetOpValue - Return binary encoding of the branch
93 // target operand. If the machine operand requires relocation,
94 // record the relocation and return zero.
95 unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo,
96 SmallVectorImpl<MCFixup> &Fixups,
97 const MCSubtargetInfo &STI) const;
98
99 // getBranchTargetOpValue1SImm16 - Return binary encoding of the branch
100 // target operand. If the machine operand requires relocation,
101 // record the relocation and return zero.
102 unsigned getBranchTargetOpValue1SImm16(const MCInst &MI, unsigned OpNo,
103 SmallVectorImpl<MCFixup> &Fixups,
104 const MCSubtargetInfo &STI) const;
105
106 // getBranchTargetOpValueMMR6 - Return binary encoding of the branch
107 // target operand. If the machine operand requires relocation,
108 // record the relocation and return zero.
109 unsigned getBranchTargetOpValueMMR6(const MCInst &MI, unsigned OpNo,
110 SmallVectorImpl<MCFixup> &Fixups,
111 const MCSubtargetInfo &STI) const;
112
113 // getBranchTargetOpValueLsl2MMR6 - Return binary encoding of the branch
114 // target operand. If the machine operand requires relocation,
115 // record the relocation and return zero.
116 unsigned getBranchTargetOpValueLsl2MMR6(const MCInst &MI, unsigned OpNo,
117 SmallVectorImpl<MCFixup> &Fixups,
118 const MCSubtargetInfo &STI) const;
119
120 // getBranchTarget7OpValue - Return binary encoding of the microMIPS branch
121 // target operand. If the machine operand requires relocation,
122 // record the relocation and return zero.
123 unsigned getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo,
124 SmallVectorImpl<MCFixup> &Fixups,
125 const MCSubtargetInfo &STI) const;
126
127 // getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS
128 // 10-bit branch target operand. If the machine operand requires relocation,
129 // record the relocation and return zero.
130 unsigned getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo,
131 SmallVectorImpl<MCFixup> &Fixups,
132 const MCSubtargetInfo &STI) const;
133
134 // getBranchTargetOpValue - Return binary encoding of the microMIPS branch
135 // target operand. If the machine operand requires relocation,
136 // record the relocation and return zero.
137 unsigned getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo,
138 SmallVectorImpl<MCFixup> &Fixups,
139 const MCSubtargetInfo &STI) const;
140
141 // getBranchTarget21OpValue - Return binary encoding of the branch
142 // offset operand. If the machine operand requires relocation,
143 // record the relocation and return zero.
144 unsigned getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo,
145 SmallVectorImpl<MCFixup> &Fixups,
146 const MCSubtargetInfo &STI) const;
147
148 // getBranchTarget21OpValueMM - Return binary encoding of the branch
149 // offset operand for microMIPS. If the machine operand requires
150 // relocation,record the relocation and return zero.
151 unsigned getBranchTarget21OpValueMM(const MCInst &MI, unsigned OpNo,
152 SmallVectorImpl<MCFixup> &Fixups,
153 const MCSubtargetInfo &STI) const;
154
155 // getBranchTarget26OpValue - Return binary encoding of the branch
156 // offset operand. If the machine operand requires relocation,
157 // record the relocation and return zero.
158 unsigned getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo,
159 SmallVectorImpl<MCFixup> &Fixups,
160 const MCSubtargetInfo &STI) const;
161
162 // getBranchTarget26OpValueMM - Return binary encoding of the branch
163 // offset operand. If the machine operand requires relocation,
164 // record the relocation and return zero.
165 unsigned getBranchTarget26OpValueMM(const MCInst &MI, unsigned OpNo,
166 SmallVectorImpl<MCFixup> &Fixups,
167 const MCSubtargetInfo &STI) const;
168
169 // getJumpOffset16OpValue - Return binary encoding of the jump
170 // offset operand. If the machine operand requires relocation,
171 // record the relocation and return zero.
172 unsigned getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo,
173 SmallVectorImpl<MCFixup> &Fixups,
174 const MCSubtargetInfo &STI) const;
175
176 // getMachineOpValue - Return binary encoding of operand. If the machin
177 // operand requires relocation, record the relocation and return zero.
178 unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
179 SmallVectorImpl<MCFixup> &Fixups,
180 const MCSubtargetInfo &STI) const;
181 unsigned getImmOpValue(const MCInst &MI, const MCOperand &MO,
182 SmallVectorImpl<MCFixup> &Fixups,
183 const MCSubtargetInfo &STI) const;
184
185 template <unsigned Shift>
186 unsigned getScaledImmOpValue(const MCInst &MI, unsigned OpNo,
187 SmallVectorImpl<MCFixup> &Fixups,
188 const MCSubtargetInfo &STI) const;
189 template <unsigned FromEnd, unsigned Shift = 0>
190 unsigned getOperandFromEndValue(const MCInst &MI, unsigned OpNo,
191 SmallVectorImpl<MCFixup> &Fixups,
192 const MCSubtargetInfo &STI) const;
193 unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo,
194 SmallVectorImpl<MCFixup> &Fixups,
195 const MCSubtargetInfo &STI) const;
196
197 /// Subtract Offset then encode as a N-bit unsigned integer.
198 template <unsigned Bits, int Offset>
199 unsigned getUImmWithOffsetEncoding(const MCInst &MI, unsigned OpNo,
200 SmallVectorImpl<MCFixup> &Fixups,
201 const MCSubtargetInfo &STI) const;
202
203 unsigned getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo,
204 SmallVectorImpl<MCFixup> &Fixups,
205 const MCSubtargetInfo &STI) const;
206
207 unsigned getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo,
208 SmallVectorImpl<MCFixup> &Fixups,
209 const MCSubtargetInfo &STI) const;
210
211 unsigned getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo,
212 SmallVectorImpl<MCFixup> &Fixups,
213 const MCSubtargetInfo &STI) const;
214 unsigned getUImm4AndValue(const MCInst &MI, unsigned OpNo,
215 SmallVectorImpl<MCFixup> &Fixups,
216 const MCSubtargetInfo &STI) const;
217
218 unsigned getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo,
219 SmallVectorImpl<MCFixup> &Fixups,
220 const MCSubtargetInfo &STI) const;
221 unsigned getMovePRegSingleOpValue(const MCInst &MI, unsigned OpNo,
222 SmallVectorImpl<MCFixup> &Fixups,
223 const MCSubtargetInfo &STI) const;
224
225 unsigned getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo,
226 SmallVectorImpl<MCFixup> &Fixups,
227 const MCSubtargetInfo &STI) const;
228
229 unsigned getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
230 const MCSubtargetInfo &STI) const;
231
232 unsigned getRegisterListOpValue(const MCInst &MI, unsigned OpNo,
233 SmallVectorImpl<MCFixup> &Fixups,
234 const MCSubtargetInfo &STI) const;
235
236 unsigned getRegisterListOpValue16(const MCInst &MI, unsigned OpNo,
237 SmallVectorImpl<MCFixup> &Fixups,
238 const MCSubtargetInfo &STI) const;
239
240private:
241 void LowerCompactBranch(MCInst& Inst) const;
242};
243
244} // end namespace llvm
245
246#endif // LLVM_LIB_TARGET_MIPS_MCTARGETDESC_MIPSMCCODEEMITTER_H
247