1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
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 // This file implements the ARMMCCodeEmitter class.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/ARMAddressingModes.h"
14 #include "MCTargetDesc/ARMBaseInfo.h"
15 #include "MCTargetDesc/ARMFixupKinds.h"
16 #include "MCTargetDesc/ARMMCExpr.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstrDesc.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCRegisterInfo.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/EndianStream.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/MathExtras.h"
35 #include "llvm/TargetParser/Triple.h"
42 #define DEBUG_TYPE "mccodeemitter"
44 STATISTIC(MCNumEmitted
, "Number of MC instructions emitted.");
45 STATISTIC(MCNumCPRelocations
, "Number of constant pool relocations created.");
49 class ARMMCCodeEmitter
: public MCCodeEmitter
{
50 const MCInstrInfo
&MCII
;
55 ARMMCCodeEmitter(const MCInstrInfo
&mcii
, MCContext
&ctx
, bool IsLittle
)
56 : MCII(mcii
), CTX(ctx
), IsLittleEndian(IsLittle
) {
58 ARMMCCodeEmitter(const ARMMCCodeEmitter
&) = delete;
59 ARMMCCodeEmitter
&operator=(const ARMMCCodeEmitter
&) = delete;
60 ~ARMMCCodeEmitter() override
= default;
62 bool isThumb(const MCSubtargetInfo
&STI
) const {
63 return STI
.hasFeature(ARM::ModeThumb
);
66 bool isThumb2(const MCSubtargetInfo
&STI
) const {
67 return isThumb(STI
) && STI
.hasFeature(ARM::FeatureThumb2
);
70 bool isTargetMachO(const MCSubtargetInfo
&STI
) const {
71 const Triple
&TT
= STI
.getTargetTriple();
72 return TT
.isOSBinFormatMachO();
75 unsigned getMachineSoImmOpValue(unsigned SoImm
) const;
77 // getBinaryCodeForInstr - TableGen'erated function for getting the
78 // binary encoding for an instruction.
79 uint64_t getBinaryCodeForInstr(const MCInst
&MI
,
80 SmallVectorImpl
<MCFixup
> &Fixups
,
81 const MCSubtargetInfo
&STI
) const;
83 /// getMachineOpValue - Return binary encoding of operand. If the machine
84 /// operand requires relocation, record the relocation and return zero.
85 unsigned getMachineOpValue(const MCInst
&MI
,const MCOperand
&MO
,
86 SmallVectorImpl
<MCFixup
> &Fixups
,
87 const MCSubtargetInfo
&STI
) const;
89 /// getHiLoImmOpValue - Return the encoding for either the hi / low 16-bit, or
90 /// high/middle-high/middle-low/low 8 bits of the specified operand. This is
91 /// used for operands with :lower16:, :upper16: :lower0_7:, :lower8_15:,
92 /// :higher0_7:, and :higher8_15: prefixes.
93 uint32_t getHiLoImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
94 SmallVectorImpl
<MCFixup
> &Fixups
,
95 const MCSubtargetInfo
&STI
) const;
97 bool EncodeAddrModeOpValues(const MCInst
&MI
, unsigned OpIdx
,
98 unsigned &Reg
, unsigned &Imm
,
99 SmallVectorImpl
<MCFixup
> &Fixups
,
100 const MCSubtargetInfo
&STI
) const;
102 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
103 /// BL branch target.
104 uint32_t getThumbBLTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
105 SmallVectorImpl
<MCFixup
> &Fixups
,
106 const MCSubtargetInfo
&STI
) const;
108 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
109 /// BLX branch target.
110 uint32_t getThumbBLXTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
111 SmallVectorImpl
<MCFixup
> &Fixups
,
112 const MCSubtargetInfo
&STI
) const;
114 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
115 uint32_t getThumbBRTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
116 SmallVectorImpl
<MCFixup
> &Fixups
,
117 const MCSubtargetInfo
&STI
) const;
119 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
120 uint32_t getThumbBCCTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
121 SmallVectorImpl
<MCFixup
> &Fixups
,
122 const MCSubtargetInfo
&STI
) const;
124 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
125 uint32_t getThumbCBTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
126 SmallVectorImpl
<MCFixup
> &Fixups
,
127 const MCSubtargetInfo
&STI
) const;
129 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
131 uint32_t getBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
132 SmallVectorImpl
<MCFixup
> &Fixups
,
133 const MCSubtargetInfo
&STI
) const;
135 /// getThumbBranchTargetOpValue - Return encoding info for 24-bit
136 /// immediate Thumb2 direct branch target.
137 uint32_t getThumbBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
138 SmallVectorImpl
<MCFixup
> &Fixups
,
139 const MCSubtargetInfo
&STI
) const;
141 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
143 uint32_t getARMBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
144 SmallVectorImpl
<MCFixup
> &Fixups
,
145 const MCSubtargetInfo
&STI
) const;
146 uint32_t getARMBLTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
147 SmallVectorImpl
<MCFixup
> &Fixups
,
148 const MCSubtargetInfo
&STI
) const;
149 uint32_t getARMBLXTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
150 SmallVectorImpl
<MCFixup
> &Fixups
,
151 const MCSubtargetInfo
&STI
) const;
153 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
154 /// ADR label target.
155 uint32_t getAdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
156 SmallVectorImpl
<MCFixup
> &Fixups
,
157 const MCSubtargetInfo
&STI
) const;
158 uint32_t getThumbAdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
159 SmallVectorImpl
<MCFixup
> &Fixups
,
160 const MCSubtargetInfo
&STI
) const;
161 uint32_t getT2AdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
162 SmallVectorImpl
<MCFixup
> &Fixups
,
163 const MCSubtargetInfo
&STI
) const;
165 uint32_t getITMaskOpValue(const MCInst
&MI
, unsigned OpIdx
,
166 SmallVectorImpl
<MCFixup
> &Fixups
,
167 const MCSubtargetInfo
&STI
) const;
169 /// getMVEShiftImmOpValue - Return encoding info for the 'sz:imm5'
171 uint32_t getMVEShiftImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
172 SmallVectorImpl
<MCFixup
> &Fixups
,
173 const MCSubtargetInfo
&STI
) const;
175 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
177 uint32_t getAddrModeImm12OpValue(const MCInst
&MI
, unsigned OpIdx
,
178 SmallVectorImpl
<MCFixup
> &Fixups
,
179 const MCSubtargetInfo
&STI
) const;
181 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
182 uint32_t getThumbAddrModeRegRegOpValue(const MCInst
&MI
, unsigned OpIdx
,
183 SmallVectorImpl
<MCFixup
> &Fixups
,
184 const MCSubtargetInfo
&STI
) const;
186 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
188 uint32_t getT2AddrModeImm8s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
189 SmallVectorImpl
<MCFixup
> &Fixups
,
190 const MCSubtargetInfo
&STI
) const;
192 /// getT2AddrModeImm7s4OpValue - Return encoding info for 'reg +/- imm7<<2'
194 uint32_t getT2AddrModeImm7s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
195 SmallVectorImpl
<MCFixup
> &Fixups
,
196 const MCSubtargetInfo
&STI
) const;
198 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
200 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
201 SmallVectorImpl
<MCFixup
> &Fixups
,
202 const MCSubtargetInfo
&STI
) const;
204 /// getT2ScaledImmOpValue - Return encoding info for '+/- immX<<Y'
206 template<unsigned Bits
, unsigned Shift
>
207 uint32_t getT2ScaledImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
208 SmallVectorImpl
<MCFixup
> &Fixups
,
209 const MCSubtargetInfo
&STI
) const;
211 /// getMveAddrModeRQOpValue - Return encoding info for 'reg, vreg'
213 uint32_t getMveAddrModeRQOpValue(const MCInst
&MI
, unsigned OpIdx
,
214 SmallVectorImpl
<MCFixup
> &Fixups
,
215 const MCSubtargetInfo
&STI
) const;
217 /// getMveAddrModeQOpValue - Return encoding info for 'reg +/- imm7<<{shift}'
220 uint32_t getMveAddrModeQOpValue(const MCInst
&MI
, unsigned OpIdx
,
221 SmallVectorImpl
<MCFixup
> &Fixups
,
222 const MCSubtargetInfo
&STI
) const;
224 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
225 /// operand as needed by load/store instructions.
226 uint32_t getLdStSORegOpValue(const MCInst
&MI
, unsigned OpIdx
,
227 SmallVectorImpl
<MCFixup
> &Fixups
,
228 const MCSubtargetInfo
&STI
) const;
230 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
231 uint32_t getLdStmModeOpValue(const MCInst
&MI
, unsigned OpIdx
,
232 SmallVectorImpl
<MCFixup
> &Fixups
,
233 const MCSubtargetInfo
&STI
) const {
234 ARM_AM::AMSubMode Mode
= (ARM_AM::AMSubMode
)MI
.getOperand(OpIdx
).getImm();
236 default: llvm_unreachable("Unknown addressing sub-mode!");
237 case ARM_AM::da
: return 0;
238 case ARM_AM::ia
: return 1;
239 case ARM_AM::db
: return 2;
240 case ARM_AM::ib
: return 3;
244 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
246 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc
) const {
248 case ARM_AM::no_shift
:
249 case ARM_AM::lsl
: return 0;
250 case ARM_AM::lsr
: return 1;
251 case ARM_AM::asr
: return 2;
253 case ARM_AM::rrx
: return 3;
255 llvm_unreachable("Invalid ShiftOpc!");
259 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
260 uint32_t getAddrMode2OffsetOpValue(const MCInst
&MI
, unsigned OpIdx
,
261 SmallVectorImpl
<MCFixup
> &Fixups
,
262 const MCSubtargetInfo
&STI
) const;
264 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
265 uint32_t getPostIdxRegOpValue(const MCInst
&MI
, unsigned OpIdx
,
266 SmallVectorImpl
<MCFixup
> &Fixups
,
267 const MCSubtargetInfo
&STI
) const;
269 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
270 uint32_t getAddrMode3OffsetOpValue(const MCInst
&MI
, unsigned OpIdx
,
271 SmallVectorImpl
<MCFixup
> &Fixups
,
272 const MCSubtargetInfo
&STI
) const;
274 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
275 uint32_t getAddrMode3OpValue(const MCInst
&MI
, unsigned OpIdx
,
276 SmallVectorImpl
<MCFixup
> &Fixups
,
277 const MCSubtargetInfo
&STI
) const;
279 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
281 uint32_t getAddrModeThumbSPOpValue(const MCInst
&MI
, unsigned OpIdx
,
282 SmallVectorImpl
<MCFixup
> &Fixups
,
283 const MCSubtargetInfo
&STI
) const;
285 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
286 uint32_t getAddrModeISOpValue(const MCInst
&MI
, unsigned OpIdx
,
287 SmallVectorImpl
<MCFixup
> &Fixups
,
288 const MCSubtargetInfo
&STI
) const;
290 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
291 uint32_t getAddrModePCOpValue(const MCInst
&MI
, unsigned OpIdx
,
292 SmallVectorImpl
<MCFixup
> &Fixups
,
293 const MCSubtargetInfo
&STI
) const;
295 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand.
296 uint32_t getAddrMode5OpValue(const MCInst
&MI
, unsigned OpIdx
,
297 SmallVectorImpl
<MCFixup
> &Fixups
,
298 const MCSubtargetInfo
&STI
) const;
300 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand.
301 uint32_t getAddrMode5FP16OpValue(const MCInst
&MI
, unsigned OpIdx
,
302 SmallVectorImpl
<MCFixup
> &Fixups
,
303 const MCSubtargetInfo
&STI
) const;
305 /// getCCOutOpValue - Return encoding of the 's' bit.
306 unsigned getCCOutOpValue(const MCInst
&MI
, unsigned Op
,
307 SmallVectorImpl
<MCFixup
> &Fixups
,
308 const MCSubtargetInfo
&STI
) const {
309 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
311 return MI
.getOperand(Op
).getReg() == ARM::CPSR
;
314 unsigned getModImmOpValue(const MCInst
&MI
, unsigned Op
,
315 SmallVectorImpl
<MCFixup
> &Fixups
,
316 const MCSubtargetInfo
&ST
) const {
317 const MCOperand
&MO
= MI
.getOperand(Op
);
319 // Support for fixups (MCFixup)
321 const MCExpr
*Expr
= MO
.getExpr();
322 // Fixups resolve to plain values that need to be encoded.
323 MCFixupKind Kind
= MCFixupKind(ARM::fixup_arm_mod_imm
);
324 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
328 // Immediate is already in its encoded format
332 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
333 unsigned getT2SOImmOpValue(const MCInst
&MI
, unsigned Op
,
334 SmallVectorImpl
<MCFixup
> &Fixups
,
335 const MCSubtargetInfo
&STI
) const {
336 const MCOperand
&MO
= MI
.getOperand(Op
);
338 // Support for fixups (MCFixup)
340 const MCExpr
*Expr
= MO
.getExpr();
341 // Fixups resolve to plain values that need to be encoded.
342 MCFixupKind Kind
= MCFixupKind(ARM::fixup_t2_so_imm
);
343 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
346 unsigned SoImm
= MO
.getImm();
347 unsigned Encoded
= ARM_AM::getT2SOImmVal(SoImm
);
348 assert(Encoded
!= ~0U && "Not a Thumb2 so_imm value?");
352 unsigned getT2AddrModeSORegOpValue(const MCInst
&MI
, unsigned OpNum
,
353 SmallVectorImpl
<MCFixup
> &Fixups
,
354 const MCSubtargetInfo
&STI
) const;
355 template<unsigned Bits
, unsigned Shift
>
356 unsigned getT2AddrModeImmOpValue(const MCInst
&MI
, unsigned OpNum
,
357 SmallVectorImpl
<MCFixup
> &Fixups
,
358 const MCSubtargetInfo
&STI
) const;
359 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst
&MI
, unsigned OpNum
,
360 SmallVectorImpl
<MCFixup
> &Fixups
,
361 const MCSubtargetInfo
&STI
) const;
363 /// getSORegOpValue - Return an encoded so_reg shifted register value.
364 unsigned getSORegRegOpValue(const MCInst
&MI
, unsigned Op
,
365 SmallVectorImpl
<MCFixup
> &Fixups
,
366 const MCSubtargetInfo
&STI
) const;
367 unsigned getSORegImmOpValue(const MCInst
&MI
, unsigned Op
,
368 SmallVectorImpl
<MCFixup
> &Fixups
,
369 const MCSubtargetInfo
&STI
) const;
370 unsigned getT2SORegOpValue(const MCInst
&MI
, unsigned Op
,
371 SmallVectorImpl
<MCFixup
> &Fixups
,
372 const MCSubtargetInfo
&STI
) const;
374 unsigned getNEONVcvtImm32OpValue(const MCInst
&MI
, unsigned Op
,
375 SmallVectorImpl
<MCFixup
> &Fixups
,
376 const MCSubtargetInfo
&STI
) const {
377 return 64 - MI
.getOperand(Op
).getImm();
380 unsigned getBitfieldInvertedMaskOpValue(const MCInst
&MI
, unsigned Op
,
381 SmallVectorImpl
<MCFixup
> &Fixups
,
382 const MCSubtargetInfo
&STI
) const;
384 unsigned getRegisterListOpValue(const MCInst
&MI
, unsigned Op
,
385 SmallVectorImpl
<MCFixup
> &Fixups
,
386 const MCSubtargetInfo
&STI
) const;
387 unsigned getAddrMode6AddressOpValue(const MCInst
&MI
, unsigned Op
,
388 SmallVectorImpl
<MCFixup
> &Fixups
,
389 const MCSubtargetInfo
&STI
) const;
390 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst
&MI
, unsigned Op
,
391 SmallVectorImpl
<MCFixup
> &Fixups
,
392 const MCSubtargetInfo
&STI
) const;
393 unsigned getAddrMode6DupAddressOpValue(const MCInst
&MI
, unsigned Op
,
394 SmallVectorImpl
<MCFixup
> &Fixups
,
395 const MCSubtargetInfo
&STI
) const;
396 unsigned getAddrMode6OffsetOpValue(const MCInst
&MI
, unsigned Op
,
397 SmallVectorImpl
<MCFixup
> &Fixups
,
398 const MCSubtargetInfo
&STI
) const;
400 unsigned getShiftRight8Imm(const MCInst
&MI
, unsigned Op
,
401 SmallVectorImpl
<MCFixup
> &Fixups
,
402 const MCSubtargetInfo
&STI
) const;
403 unsigned getShiftRight16Imm(const MCInst
&MI
, unsigned Op
,
404 SmallVectorImpl
<MCFixup
> &Fixups
,
405 const MCSubtargetInfo
&STI
) const;
406 unsigned getShiftRight32Imm(const MCInst
&MI
, unsigned Op
,
407 SmallVectorImpl
<MCFixup
> &Fixups
,
408 const MCSubtargetInfo
&STI
) const;
409 unsigned getShiftRight64Imm(const MCInst
&MI
, unsigned Op
,
410 SmallVectorImpl
<MCFixup
> &Fixups
,
411 const MCSubtargetInfo
&STI
) const;
413 unsigned getThumbSRImmOpValue(const MCInst
&MI
, unsigned Op
,
414 SmallVectorImpl
<MCFixup
> &Fixups
,
415 const MCSubtargetInfo
&STI
) const;
417 unsigned NEONThumb2DataIPostEncoder(const MCInst
&MI
,
418 unsigned EncodedValue
,
419 const MCSubtargetInfo
&STI
) const;
420 unsigned NEONThumb2LoadStorePostEncoder(const MCInst
&MI
,
421 unsigned EncodedValue
,
422 const MCSubtargetInfo
&STI
) const;
423 unsigned NEONThumb2DupPostEncoder(const MCInst
&MI
,
424 unsigned EncodedValue
,
425 const MCSubtargetInfo
&STI
) const;
426 unsigned NEONThumb2V8PostEncoder(const MCInst
&MI
,
427 unsigned EncodedValue
,
428 const MCSubtargetInfo
&STI
) const;
430 unsigned VFPThumb2PostEncoder(const MCInst
&MI
,
431 unsigned EncodedValue
,
432 const MCSubtargetInfo
&STI
) const;
434 uint32_t getPowerTwoOpValue(const MCInst
&MI
, unsigned OpIdx
,
435 SmallVectorImpl
<MCFixup
> &Fixups
,
436 const MCSubtargetInfo
&STI
) const;
438 void encodeInstruction(const MCInst
&MI
, SmallVectorImpl
<char> &CB
,
439 SmallVectorImpl
<MCFixup
> &Fixups
,
440 const MCSubtargetInfo
&STI
) const override
;
442 template <bool isNeg
, ARM::Fixups fixup
>
443 uint32_t getBFTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
444 SmallVectorImpl
<MCFixup
> &Fixups
,
445 const MCSubtargetInfo
&STI
) const;
447 uint32_t getBFAfterTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
448 SmallVectorImpl
<MCFixup
> &Fixups
,
449 const MCSubtargetInfo
&STI
) const;
451 uint32_t getVPTMaskOpValue(const MCInst
&MI
, unsigned OpIdx
,
452 SmallVectorImpl
<MCFixup
> &Fixups
,
453 const MCSubtargetInfo
&STI
) const;
454 uint32_t getRestrictedCondCodeOpValue(const MCInst
&MI
, unsigned OpIdx
,
455 SmallVectorImpl
<MCFixup
> &Fixups
,
456 const MCSubtargetInfo
&STI
) const;
457 template <unsigned size
>
458 uint32_t getMVEPairVectorIndexOpValue(const MCInst
&MI
, unsigned OpIdx
,
459 SmallVectorImpl
<MCFixup
> &Fixups
,
460 const MCSubtargetInfo
&STI
) const;
463 } // end anonymous namespace
465 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
466 /// instructions, and rewrite them to their Thumb2 form if we are currently in
468 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst
&MI
,
469 unsigned EncodedValue
,
470 const MCSubtargetInfo
&STI
) const {
472 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
473 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
475 unsigned Bit24
= EncodedValue
& 0x01000000;
476 unsigned Bit28
= Bit24
<< 4;
477 EncodedValue
&= 0xEFFFFFFF;
478 EncodedValue
|= Bit28
;
479 EncodedValue
|= 0x0F000000;
485 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
486 /// instructions, and rewrite them to their Thumb2 form if we are currently in
488 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst
&MI
,
489 unsigned EncodedValue
,
490 const MCSubtargetInfo
&STI
) const {
492 EncodedValue
&= 0xF0FFFFFF;
493 EncodedValue
|= 0x09000000;
499 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
500 /// instructions, and rewrite them to their Thumb2 form if we are currently in
502 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst
&MI
,
503 unsigned EncodedValue
,
504 const MCSubtargetInfo
&STI
) const {
506 EncodedValue
&= 0x00FFFFFF;
507 EncodedValue
|= 0xEE000000;
513 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
514 /// if we are in Thumb2.
515 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst
&MI
,
516 unsigned EncodedValue
,
517 const MCSubtargetInfo
&STI
) const {
519 EncodedValue
|= 0xC000000; // Set bits 27-26
525 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
526 /// them to their Thumb2 form if we are currently in Thumb2 mode.
527 unsigned ARMMCCodeEmitter::
528 VFPThumb2PostEncoder(const MCInst
&MI
, unsigned EncodedValue
,
529 const MCSubtargetInfo
&STI
) const {
531 EncodedValue
&= 0x0FFFFFFF;
532 EncodedValue
|= 0xE0000000;
537 /// getMachineOpValue - Return binary encoding of operand. If the machine
538 /// operand requires relocation, record the relocation and return zero.
539 unsigned ARMMCCodeEmitter::
540 getMachineOpValue(const MCInst
&MI
, const MCOperand
&MO
,
541 SmallVectorImpl
<MCFixup
> &Fixups
,
542 const MCSubtargetInfo
&STI
) const {
544 MCRegister Reg
= MO
.getReg();
545 unsigned RegNo
= CTX
.getRegisterInfo()->getEncodingValue(Reg
);
547 // In NEON, Q registers are encoded as 2x their register number,
548 // because they're using the same indices as the D registers they
549 // overlap. In MVE, there are no 64-bit vector instructions, so
550 // the encodings all refer to Q-registers by their literal
553 if (STI
.hasFeature(ARM::HasMVEIntegerOps
))
559 case ARM::Q0
: case ARM::Q1
: case ARM::Q2
: case ARM::Q3
:
560 case ARM::Q4
: case ARM::Q5
: case ARM::Q6
: case ARM::Q7
:
561 case ARM::Q8
: case ARM::Q9
: case ARM::Q10
: case ARM::Q11
:
562 case ARM::Q12
: case ARM::Q13
: case ARM::Q14
: case ARM::Q15
:
565 } else if (MO
.isImm()) {
566 return static_cast<unsigned>(MO
.getImm());
567 } else if (MO
.isDFPImm()) {
568 return static_cast<unsigned>(APFloat(bit_cast
<double>(MO
.getDFPImm()))
574 llvm_unreachable("Unable to encode MCOperand!");
577 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
578 bool ARMMCCodeEmitter::
579 EncodeAddrModeOpValues(const MCInst
&MI
, unsigned OpIdx
, unsigned &Reg
,
580 unsigned &Imm
, SmallVectorImpl
<MCFixup
> &Fixups
,
581 const MCSubtargetInfo
&STI
) const {
582 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
583 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
585 Reg
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
587 int32_t SImm
= MO1
.getImm();
590 // Special value for #-0
591 if (SImm
== INT32_MIN
) {
596 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
606 /// getBranchTargetOpValue - Helper function to get the branch target operand,
607 /// which is either an immediate or requires a fixup.
608 static uint32_t getBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
610 SmallVectorImpl
<MCFixup
> &Fixups
,
611 const MCSubtargetInfo
&STI
) {
612 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
614 // If the destination is an immediate, we have nothing to do.
615 if (MO
.isImm()) return MO
.getImm();
616 assert(MO
.isExpr() && "Unexpected branch target type!");
617 const MCExpr
*Expr
= MO
.getExpr();
618 MCFixupKind Kind
= MCFixupKind(FixupKind
);
619 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
621 // All of the information is in the fixup.
625 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
626 // determined by negating them and XOR'ing them with bit 23.
627 static int32_t encodeThumbBLOffset(int32_t offset
) {
629 uint32_t S
= (offset
& 0x800000) >> 23;
630 uint32_t J1
= (offset
& 0x400000) >> 22;
631 uint32_t J2
= (offset
& 0x200000) >> 21;
644 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
645 uint32_t ARMMCCodeEmitter::
646 getThumbBLTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
647 SmallVectorImpl
<MCFixup
> &Fixups
,
648 const MCSubtargetInfo
&STI
) const {
649 const MCOperand MO
= MI
.getOperand(OpIdx
);
651 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_bl
,
653 return encodeThumbBLOffset(MO
.getImm());
656 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
657 /// BLX branch target.
658 uint32_t ARMMCCodeEmitter::
659 getThumbBLXTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
660 SmallVectorImpl
<MCFixup
> &Fixups
,
661 const MCSubtargetInfo
&STI
) const {
662 const MCOperand MO
= MI
.getOperand(OpIdx
);
664 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_blx
,
666 return encodeThumbBLOffset(MO
.getImm());
669 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
670 uint32_t ARMMCCodeEmitter::
671 getThumbBRTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
672 SmallVectorImpl
<MCFixup
> &Fixups
,
673 const MCSubtargetInfo
&STI
) const {
674 const MCOperand MO
= MI
.getOperand(OpIdx
);
676 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_br
,
678 return (MO
.getImm() >> 1);
681 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
682 uint32_t ARMMCCodeEmitter::
683 getThumbBCCTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
684 SmallVectorImpl
<MCFixup
> &Fixups
,
685 const MCSubtargetInfo
&STI
) const {
686 const MCOperand MO
= MI
.getOperand(OpIdx
);
688 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_bcc
,
690 return (MO
.getImm() >> 1);
693 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
694 uint32_t ARMMCCodeEmitter::
695 getThumbCBTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
696 SmallVectorImpl
<MCFixup
> &Fixups
,
697 const MCSubtargetInfo
&STI
) const {
698 const MCOperand MO
= MI
.getOperand(OpIdx
);
700 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_cb
, Fixups
, STI
);
701 return (MO
.getImm() >> 1);
704 /// Return true if this branch has a non-always predication
705 static bool HasConditionalBranch(const MCInst
&MI
) {
706 int NumOp
= MI
.getNumOperands();
708 for (int i
= 0; i
< NumOp
-1; ++i
) {
709 const MCOperand
&MCOp1
= MI
.getOperand(i
);
710 const MCOperand
&MCOp2
= MI
.getOperand(i
+ 1);
711 if (MCOp1
.isImm() && MCOp2
.isReg() &&
712 (!MCOp2
.getReg() || MCOp2
.getReg() == ARM::CPSR
)) {
713 if (ARMCC::CondCodes(MCOp1
.getImm()) != ARMCC::AL
)
721 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
723 uint32_t ARMMCCodeEmitter::
724 getBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
725 SmallVectorImpl
<MCFixup
> &Fixups
,
726 const MCSubtargetInfo
&STI
) const {
727 // FIXME: This really, really shouldn't use TargetMachine. We don't want
728 // coupling between MC and TM anywhere we can help it.
731 ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_t2_condbranch
, Fixups
, STI
);
732 return getARMBranchTargetOpValue(MI
, OpIdx
, Fixups
, STI
);
735 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
737 uint32_t ARMMCCodeEmitter::
738 getARMBranchTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
739 SmallVectorImpl
<MCFixup
> &Fixups
,
740 const MCSubtargetInfo
&STI
) const {
741 const MCOperand MO
= MI
.getOperand(OpIdx
);
743 if (HasConditionalBranch(MI
))
744 return ::getBranchTargetOpValue(MI
, OpIdx
,
745 ARM::fixup_arm_condbranch
, Fixups
, STI
);
746 return ::getBranchTargetOpValue(MI
, OpIdx
,
747 ARM::fixup_arm_uncondbranch
, Fixups
, STI
);
750 return MO
.getImm() >> 2;
753 uint32_t ARMMCCodeEmitter::
754 getARMBLTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
755 SmallVectorImpl
<MCFixup
> &Fixups
,
756 const MCSubtargetInfo
&STI
) const {
757 const MCOperand MO
= MI
.getOperand(OpIdx
);
759 if (HasConditionalBranch(MI
))
760 return ::getBranchTargetOpValue(MI
, OpIdx
,
761 ARM::fixup_arm_condbl
, Fixups
, STI
);
762 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_uncondbl
, Fixups
, STI
);
765 return MO
.getImm() >> 2;
768 uint32_t ARMMCCodeEmitter::
769 getARMBLXTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
770 SmallVectorImpl
<MCFixup
> &Fixups
,
771 const MCSubtargetInfo
&STI
) const {
772 const MCOperand MO
= MI
.getOperand(OpIdx
);
774 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_blx
, Fixups
, STI
);
776 return MO
.getImm() >> 1;
779 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
780 /// immediate branch target.
781 uint32_t ARMMCCodeEmitter::getThumbBranchTargetOpValue(
782 const MCInst
&MI
, unsigned OpIdx
, SmallVectorImpl
<MCFixup
> &Fixups
,
783 const MCSubtargetInfo
&STI
) const {
785 const MCOperand MO
= MI
.getOperand(OpIdx
);
788 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_t2_uncondbranch
, Fixups
, STI
);
790 Val
= MO
.getImm() >> 1;
792 bool I
= (Val
& 0x800000);
793 bool J1
= (Val
& 0x400000);
794 bool J2
= (Val
& 0x200000);
808 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
809 /// ADR label target.
810 uint32_t ARMMCCodeEmitter::
811 getAdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
812 SmallVectorImpl
<MCFixup
> &Fixups
,
813 const MCSubtargetInfo
&STI
) const {
814 const MCOperand MO
= MI
.getOperand(OpIdx
);
816 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_adr_pcrel_12
,
818 int64_t offset
= MO
.getImm();
819 uint32_t Val
= 0x2000;
822 if (offset
== INT32_MIN
) {
825 } else if (offset
< 0) {
828 SoImmVal
= ARM_AM::getSOImmVal(offset
);
832 SoImmVal
= ARM_AM::getSOImmVal(offset
);
835 SoImmVal
= ARM_AM::getSOImmVal(offset
);
839 SoImmVal
= ARM_AM::getSOImmVal(offset
);
843 assert(SoImmVal
!= -1 && "Not a valid so_imm value!");
849 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
851 uint32_t ARMMCCodeEmitter::
852 getT2AdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
853 SmallVectorImpl
<MCFixup
> &Fixups
,
854 const MCSubtargetInfo
&STI
) const {
855 const MCOperand MO
= MI
.getOperand(OpIdx
);
857 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_t2_adr_pcrel_12
,
859 int32_t Val
= MO
.getImm();
860 if (Val
== INT32_MIN
)
869 /// getITMaskOpValue - Return the architectural encoding of an IT
870 /// predication mask, given the MCOperand format.
871 uint32_t ARMMCCodeEmitter::
872 getITMaskOpValue(const MCInst
&MI
, unsigned OpIdx
,
873 SmallVectorImpl
<MCFixup
> &Fixups
,
874 const MCSubtargetInfo
&STI
) const {
875 const MCOperand MaskMO
= MI
.getOperand(OpIdx
);
876 assert(MaskMO
.isImm() && "Unexpected operand type!");
878 unsigned Mask
= MaskMO
.getImm();
880 // IT masks are encoded as a sequence of replacement low-order bits
881 // for the condition code. So if the low bit of the starting
882 // condition code is 1, then we have to flip all the bits above the
883 // terminating bit (which is the lowest 1 bit).
884 assert(OpIdx
> 0 && "IT mask appears first!");
885 const MCOperand CondMO
= MI
.getOperand(OpIdx
-1);
886 assert(CondMO
.isImm() && "Unexpected operand type!");
887 if (CondMO
.getImm() & 1) {
888 unsigned LowBit
= Mask
& -Mask
;
889 unsigned BitsAboveLowBit
= 0xF & (-LowBit
<< 1);
890 Mask
^= BitsAboveLowBit
;
896 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
898 uint32_t ARMMCCodeEmitter::
899 getThumbAdrLabelOpValue(const MCInst
&MI
, unsigned OpIdx
,
900 SmallVectorImpl
<MCFixup
> &Fixups
,
901 const MCSubtargetInfo
&STI
) const {
902 const MCOperand MO
= MI
.getOperand(OpIdx
);
904 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_thumb_adr_pcrel_10
,
909 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
911 uint32_t ARMMCCodeEmitter::
912 getThumbAddrModeRegRegOpValue(const MCInst
&MI
, unsigned OpIdx
,
913 SmallVectorImpl
<MCFixup
> &,
914 const MCSubtargetInfo
&STI
) const {
918 const MCOperand
&MO1
= MI
.getOperand(OpIdx
);
919 const MCOperand
&MO2
= MI
.getOperand(OpIdx
+ 1);
920 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(MO1
.getReg());
921 unsigned Rm
= CTX
.getRegisterInfo()->getEncodingValue(MO2
.getReg());
922 return (Rm
<< 3) | Rn
;
925 /// getMVEShiftImmOpValue - Return encoding info for the 'sz:imm5'
928 ARMMCCodeEmitter::getMVEShiftImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
929 SmallVectorImpl
<MCFixup
> &Fixups
,
930 const MCSubtargetInfo
&STI
) const {
932 // The value we are trying to encode is an immediate between either the
933 // range of [1-7] or [1-15] depending on whether we are dealing with the
934 // u8/s8 or the u16/s16 variants respectively.
935 // This value is encoded as follows, if ShiftImm is the value within those
936 // ranges then the encoding szimm5 = ShiftImm + size, where size is either 8
939 unsigned Size
, ShiftImm
;
940 switch(MI
.getOpcode()) {
941 case ARM::MVE_VSHLL_imms16bh
:
942 case ARM::MVE_VSHLL_imms16th
:
943 case ARM::MVE_VSHLL_immu16bh
:
944 case ARM::MVE_VSHLL_immu16th
:
947 case ARM::MVE_VSHLL_imms8bh
:
948 case ARM::MVE_VSHLL_imms8th
:
949 case ARM::MVE_VSHLL_immu8bh
:
950 case ARM::MVE_VSHLL_immu8th
:
954 llvm_unreachable("Use of operand not supported by this instruction");
956 ShiftImm
= MI
.getOperand(OpIdx
).getImm();
957 return Size
+ ShiftImm
;
960 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
961 uint32_t ARMMCCodeEmitter::
962 getAddrModeImm12OpValue(const MCInst
&MI
, unsigned OpIdx
,
963 SmallVectorImpl
<MCFixup
> &Fixups
,
964 const MCSubtargetInfo
&STI
) const {
966 // {12} = (U)nsigned (add == '1', sub == '0')
968 unsigned Reg
= 0, Imm12
= 0;
970 // If The first operand isn't a register, we have a label reference.
971 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
973 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
975 isAdd
= EncodeAddrModeOpValues(MI
, OpIdx
, Reg
, Imm12
, Fixups
, STI
);
976 } else if (MO1
.isExpr()) {
977 assert(!isThumb(STI
) && !isThumb2(STI
) &&
978 "Thumb mode requires different encoding");
979 Reg
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
980 isAdd
= false; // 'U' bit is set as part of the fixup.
981 MCFixupKind Kind
= MCFixupKind(ARM::fixup_arm_ldst_abs_12
);
982 Fixups
.push_back(MCFixup::create(0, MO1
.getExpr(), Kind
, MI
.getLoc()));
984 } else if (MO
.isExpr()) {
985 Reg
= CTX
.getRegisterInfo()->getEncodingValue(ARM::PC
); // Rn is PC.
986 isAdd
= false; // 'U' bit is set as part of the fixup.
989 Kind
= MCFixupKind(ARM::fixup_t2_ldst_pcrel_12
);
991 Kind
= MCFixupKind(ARM::fixup_arm_ldst_pcrel_12
);
992 Fixups
.push_back(MCFixup::create(0, MO
.getExpr(), Kind
, MI
.getLoc()));
994 ++MCNumCPRelocations
;
997 int32_t Offset
= MO
.getImm();
998 if (Offset
== INT32_MIN
) {
1001 } else if (Offset
< 0) {
1007 uint32_t Binary
= Imm12
& 0xfff;
1008 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1010 Binary
|= (1 << 12);
1011 Binary
|= (Reg
<< 13);
1015 template<unsigned Bits
, unsigned Shift
>
1016 uint32_t ARMMCCodeEmitter::
1017 getT2ScaledImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
1018 SmallVectorImpl
<MCFixup
> &Fixups
,
1019 const MCSubtargetInfo
&STI
) const {
1020 // FIXME: The immediate operand should have already been encoded like this
1021 // before ever getting here. The encoder method should just need to combine
1022 // the MI operands for the register and the offset into a single
1023 // representation for the complex operand in the .td file. This isn't just
1024 // style, unfortunately. As-is, we can't represent the distinct encoding
1027 // {Bits} = (U)nsigned (add == '1', sub == '0')
1028 // {(Bits-1)-0} = immediate
1029 int32_t Imm
= MI
.getOperand(OpIdx
).getImm();
1030 bool isAdd
= Imm
>= 0;
1032 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1034 Imm
= -(uint32_t)Imm
;
1038 uint32_t Binary
= Imm
& ((1U << Bits
) - 1);
1039 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1041 Binary
|= (1U << Bits
);
1045 /// getMveAddrModeRQOpValue - Return encoding info for 'reg, vreg'
1047 uint32_t ARMMCCodeEmitter::
1048 getMveAddrModeRQOpValue(const MCInst
&MI
, unsigned OpIdx
,
1049 SmallVectorImpl
<MCFixup
> &Fixups
,
1050 const MCSubtargetInfo
&STI
) const {
1053 const MCOperand
&M0
= MI
.getOperand(OpIdx
);
1054 const MCOperand
&M1
= MI
.getOperand(OpIdx
+ 1);
1056 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(M0
.getReg());
1057 unsigned Qm
= CTX
.getRegisterInfo()->getEncodingValue(M1
.getReg());
1059 assert(Qm
< 8 && "Qm is supposed to be encodable in 3 bits");
1061 return (Rn
<< 3) | Qm
;
1064 /// getMveAddrModeRQOpValue - Return encoding info for 'reg, vreg'
1067 uint32_t ARMMCCodeEmitter::
1068 getMveAddrModeQOpValue(const MCInst
&MI
, unsigned OpIdx
,
1069 SmallVectorImpl
<MCFixup
> &Fixups
,
1070 const MCSubtargetInfo
&STI
) const {
1073 const MCOperand
&M0
= MI
.getOperand(OpIdx
);
1074 const MCOperand
&M1
= MI
.getOperand(OpIdx
+ 1);
1076 unsigned Qm
= CTX
.getRegisterInfo()->getEncodingValue(M0
.getReg());
1077 int32_t Imm
= M1
.getImm();
1079 bool isAdd
= Imm
>= 0;
1084 Imm
= -(uint32_t)Imm
;
1091 assert(Qm
< 8 && "Qm is supposed to be encodable in 3 bits");
1093 return (Qm
<< 8) | Imm
;
1096 /// getT2AddrModeImm8s4OpValue - Return encoding info for
1097 /// 'reg +/- imm8<<2' operand.
1098 uint32_t ARMMCCodeEmitter::
1099 getT2AddrModeImm8s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
1100 SmallVectorImpl
<MCFixup
> &Fixups
,
1101 const MCSubtargetInfo
&STI
) const {
1103 // {8} = (U)nsigned (add == '1', sub == '0')
1107 // If The first operand isn't a register, we have a label reference.
1108 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1110 Reg
= CTX
.getRegisterInfo()->getEncodingValue(ARM::PC
); // Rn is PC.
1112 isAdd
= false ; // 'U' bit is set as part of the fixup.
1114 assert(MO
.isExpr() && "Unexpected machine operand type!");
1115 const MCExpr
*Expr
= MO
.getExpr();
1116 MCFixupKind Kind
= MCFixupKind(ARM::fixup_t2_pcrel_10
);
1117 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
1119 ++MCNumCPRelocations
;
1121 isAdd
= EncodeAddrModeOpValues(MI
, OpIdx
, Reg
, Imm8
, Fixups
, STI
);
1123 // FIXME: The immediate operand should have already been encoded like this
1124 // before ever getting here. The encoder method should just need to combine
1125 // the MI operands for the register and the offset into a single
1126 // representation for the complex operand in the .td file. This isn't just
1127 // style, unfortunately. As-is, we can't represent the distinct encoding
1129 assert(((Imm8
& 0x3) == 0) && "Not a valid immediate!");
1130 uint32_t Binary
= (Imm8
>> 2) & 0xff;
1131 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1134 Binary
|= (Reg
<< 9);
1138 /// getT2AddrModeImm7s4OpValue - Return encoding info for
1139 /// 'reg +/- imm7<<2' operand.
1141 ARMMCCodeEmitter::getT2AddrModeImm7s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
1142 SmallVectorImpl
<MCFixup
> &Fixups
,
1143 const MCSubtargetInfo
&STI
) const {
1145 // {7} = (A)dd (add == '1', sub == '0')
1148 // If The first operand isn't a register, we have a label reference.
1149 bool isAdd
= EncodeAddrModeOpValues(MI
, OpIdx
, Reg
, Imm7
, Fixups
, STI
);
1151 // FIXME: The immediate operand should have already been encoded like this
1152 // before ever getting here. The encoder method should just need to combine
1153 // the MI operands for the register and the offset into a single
1154 // representation for the complex operand in the .td file. This isn't just
1155 // style, unfortunately. As-is, we can't represent the distinct encoding
1157 uint32_t Binary
= (Imm7
>> 2) & 0xff;
1158 // Immediate is always encoded as positive. The 'A' bit controls add vs sub.
1161 Binary
|= (Reg
<< 8);
1165 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
1166 /// 'reg + imm8<<2' operand.
1167 uint32_t ARMMCCodeEmitter::
1168 getT2AddrModeImm0_1020s4OpValue(const MCInst
&MI
, unsigned OpIdx
,
1169 SmallVectorImpl
<MCFixup
> &Fixups
,
1170 const MCSubtargetInfo
&STI
) const {
1173 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1174 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1175 unsigned Reg
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1176 unsigned Imm8
= MO1
.getImm();
1177 return (Reg
<< 8) | Imm8
;
1180 uint32_t ARMMCCodeEmitter::getHiLoImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
1181 SmallVectorImpl
<MCFixup
> &Fixups
,
1182 const MCSubtargetInfo
&STI
) const {
1183 // {20-16} = imm{15-12}
1184 // {11-0} = imm{11-0}
1185 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1187 // Hi / lo bits already extracted during earlier passes.
1188 return static_cast<unsigned>(MO
.getImm());
1190 // Handle :upper16:, :lower16:, :upper8_15:, :upper0_7:, :lower8_15:
1191 // :lower0_7: assembly prefixes.
1192 const MCExpr
*E
= MO
.getExpr();
1194 if (E
->getKind() == MCExpr::Target
) {
1195 const ARMMCExpr
*ARM16Expr
= cast
<ARMMCExpr
>(E
);
1196 E
= ARM16Expr
->getSubExpr();
1198 if (const MCConstantExpr
*MCE
= dyn_cast
<MCConstantExpr
>(E
)) {
1199 const int64_t Value
= MCE
->getValue();
1200 if (Value
> UINT32_MAX
)
1201 report_fatal_error("constant value truncated (limited to 32-bit)");
1203 switch (ARM16Expr
->getKind()) {
1204 case ARMMCExpr::VK_ARM_HI16
:
1205 return (int32_t(Value
) & 0xffff0000) >> 16;
1206 case ARMMCExpr::VK_ARM_LO16
:
1207 return (int32_t(Value
) & 0x0000ffff);
1209 case ARMMCExpr::VK_ARM_HI_8_15
:
1210 return (int32_t(Value
) & 0xff000000) >> 24;
1211 case ARMMCExpr::VK_ARM_HI_0_7
:
1212 return (int32_t(Value
) & 0x00ff0000) >> 16;
1213 case ARMMCExpr::VK_ARM_LO_8_15
:
1214 return (int32_t(Value
) & 0x0000ff00) >> 8;
1215 case ARMMCExpr::VK_ARM_LO_0_7
:
1216 return (int32_t(Value
) & 0x000000ff);
1218 default: llvm_unreachable("Unsupported ARMFixup");
1222 switch (ARM16Expr
->getKind()) {
1223 default: llvm_unreachable("Unsupported ARMFixup");
1224 case ARMMCExpr::VK_ARM_HI16
:
1225 Kind
= MCFixupKind(isThumb(STI
) ? ARM::fixup_t2_movt_hi16
1226 : ARM::fixup_arm_movt_hi16
);
1228 case ARMMCExpr::VK_ARM_LO16
:
1229 Kind
= MCFixupKind(isThumb(STI
) ? ARM::fixup_t2_movw_lo16
1230 : ARM::fixup_arm_movw_lo16
);
1232 case ARMMCExpr::VK_ARM_HI_8_15
:
1234 llvm_unreachable(":upper_8_15: not supported in Arm state");
1235 Kind
= MCFixupKind(ARM::fixup_arm_thumb_upper_8_15
);
1237 case ARMMCExpr::VK_ARM_HI_0_7
:
1239 llvm_unreachable(":upper_0_7: not supported in Arm state");
1240 Kind
= MCFixupKind(ARM::fixup_arm_thumb_upper_0_7
);
1242 case ARMMCExpr::VK_ARM_LO_8_15
:
1244 llvm_unreachable(":lower_8_15: not supported in Arm state");
1245 Kind
= MCFixupKind(ARM::fixup_arm_thumb_lower_8_15
);
1247 case ARMMCExpr::VK_ARM_LO_0_7
:
1249 llvm_unreachable(":lower_0_7: not supported in Arm state");
1250 Kind
= MCFixupKind(ARM::fixup_arm_thumb_lower_0_7
);
1254 Fixups
.push_back(MCFixup::create(0, E
, Kind
, MI
.getLoc()));
1257 // If the expression doesn't have :upper16:, :lower16: on it, it's just a
1258 // plain immediate expression, previously those evaluated to the lower 16 bits
1259 // of the expression regardless of whether we have a movt or a movw, but that
1260 // led to misleadingly results. This is disallowed in the AsmParser in
1261 // validateInstruction() so this should never happen. The same holds for
1262 // thumb1 :upper8_15:, :upper0_7:, lower8_15: or :lower0_7: with movs or adds.
1263 llvm_unreachable("expression without :upper16:, :lower16:, :upper8_15:,"
1264 ":upper0_7:, lower8_15: or :lower0_7:");
1267 uint32_t ARMMCCodeEmitter::
1268 getLdStSORegOpValue(const MCInst
&MI
, unsigned OpIdx
,
1269 SmallVectorImpl
<MCFixup
> &Fixups
,
1270 const MCSubtargetInfo
&STI
) const {
1271 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1272 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+1);
1273 const MCOperand
&MO2
= MI
.getOperand(OpIdx
+2);
1274 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1275 unsigned Rm
= CTX
.getRegisterInfo()->getEncodingValue(MO1
.getReg());
1276 unsigned ShImm
= ARM_AM::getAM2Offset(MO2
.getImm());
1277 bool isAdd
= ARM_AM::getAM2Op(MO2
.getImm()) == ARM_AM::add
;
1278 ARM_AM::ShiftOpc ShOp
= ARM_AM::getAM2ShiftOpc(MO2
.getImm());
1279 unsigned SBits
= getShiftOp(ShOp
);
1281 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
1282 // amount. However, it would be an easy mistake to make so check here.
1283 assert((ShImm
& ~0x1f) == 0 && "Out of range shift amount");
1292 uint32_t Binary
= Rm
;
1294 Binary
|= SBits
<< 5;
1295 Binary
|= ShImm
<< 7;
1301 uint32_t ARMMCCodeEmitter::
1302 getAddrMode2OffsetOpValue(const MCInst
&MI
, unsigned OpIdx
,
1303 SmallVectorImpl
<MCFixup
> &Fixups
,
1304 const MCSubtargetInfo
&STI
) const {
1305 // {13} 1 == imm12, 0 == Rm
1308 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1309 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+1);
1310 unsigned Imm
= MO1
.getImm();
1311 bool isAdd
= ARM_AM::getAM2Op(Imm
) == ARM_AM::add
;
1312 bool isReg
= MO
.getReg().isValid();
1313 uint32_t Binary
= ARM_AM::getAM2Offset(Imm
);
1314 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1316 ARM_AM::ShiftOpc ShOp
= ARM_AM::getAM2ShiftOpc(Imm
);
1317 Binary
<<= 7; // Shift amount is bits [11:7]
1318 Binary
|= getShiftOp(ShOp
) << 5; // Shift type is bits [6:5]
1319 Binary
|= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg()); // Rm is bits [3:0]
1321 return Binary
| (isAdd
<< 12) | (isReg
<< 13);
1324 uint32_t ARMMCCodeEmitter::
1325 getPostIdxRegOpValue(const MCInst
&MI
, unsigned OpIdx
,
1326 SmallVectorImpl
<MCFixup
> &Fixups
,
1327 const MCSubtargetInfo
&STI
) const {
1330 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1331 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+1);
1332 bool isAdd
= MO1
.getImm() != 0;
1333 return CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg()) | (isAdd
<< 4);
1336 uint32_t ARMMCCodeEmitter::
1337 getAddrMode3OffsetOpValue(const MCInst
&MI
, unsigned OpIdx
,
1338 SmallVectorImpl
<MCFixup
> &Fixups
,
1339 const MCSubtargetInfo
&STI
) const {
1340 // {9} 1 == imm8, 0 == Rm
1342 // {7-4} imm7_4/zero
1344 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1345 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+1);
1346 unsigned Imm
= MO1
.getImm();
1347 bool isAdd
= ARM_AM::getAM3Op(Imm
) == ARM_AM::add
;
1348 bool isImm
= !MO
.getReg().isValid();
1349 uint32_t Imm8
= ARM_AM::getAM3Offset(Imm
);
1350 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1352 Imm8
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1353 return Imm8
| (isAdd
<< 8) | (isImm
<< 9);
1356 uint32_t ARMMCCodeEmitter::
1357 getAddrMode3OpValue(const MCInst
&MI
, unsigned OpIdx
,
1358 SmallVectorImpl
<MCFixup
> &Fixups
,
1359 const MCSubtargetInfo
&STI
) const {
1360 // {13} 1 == imm8, 0 == Rm
1363 // {7-4} imm7_4/zero
1365 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1366 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+1);
1367 const MCOperand
&MO2
= MI
.getOperand(OpIdx
+2);
1369 // If The first operand isn't a register, we have a label reference.
1371 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(ARM::PC
); // Rn is PC.
1373 assert(MO
.isExpr() && "Unexpected machine operand type!");
1374 const MCExpr
*Expr
= MO
.getExpr();
1375 MCFixupKind Kind
= MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled
);
1376 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
1378 ++MCNumCPRelocations
;
1379 return (Rn
<< 9) | (1 << 13);
1381 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1382 unsigned Imm
= MO2
.getImm();
1383 bool isAdd
= ARM_AM::getAM3Op(Imm
) == ARM_AM::add
;
1384 bool isImm
= !MO1
.getReg().isValid();
1385 uint32_t Imm8
= ARM_AM::getAM3Offset(Imm
);
1386 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1388 Imm8
= CTX
.getRegisterInfo()->getEncodingValue(MO1
.getReg());
1389 return (Rn
<< 9) | Imm8
| (isAdd
<< 8) | (isImm
<< 13);
1392 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1393 uint32_t ARMMCCodeEmitter::
1394 getAddrModeThumbSPOpValue(const MCInst
&MI
, unsigned OpIdx
,
1395 SmallVectorImpl
<MCFixup
> &Fixups
,
1396 const MCSubtargetInfo
&STI
) const {
1399 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1400 assert(MI
.getOperand(OpIdx
).getReg() == ARM::SP
&&
1401 "Unexpected base register!");
1403 // The immediate is already shifted for the implicit zeroes, so no change
1405 return MO1
.getImm() & 0xff;
1408 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1409 uint32_t ARMMCCodeEmitter::
1410 getAddrModeISOpValue(const MCInst
&MI
, unsigned OpIdx
,
1411 SmallVectorImpl
<MCFixup
> &Fixups
,
1412 const MCSubtargetInfo
&STI
) const {
1416 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1417 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1418 unsigned Rn
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1419 unsigned Imm5
= MO1
.getImm();
1420 return ((Imm5
& 0x1f) << 3) | Rn
;
1423 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1424 uint32_t ARMMCCodeEmitter::
1425 getAddrModePCOpValue(const MCInst
&MI
, unsigned OpIdx
,
1426 SmallVectorImpl
<MCFixup
> &Fixups
,
1427 const MCSubtargetInfo
&STI
) const {
1428 const MCOperand MO
= MI
.getOperand(OpIdx
);
1430 return ::getBranchTargetOpValue(MI
, OpIdx
, ARM::fixup_arm_thumb_cp
, Fixups
, STI
);
1431 return (MO
.getImm() >> 2);
1434 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand.
1435 uint32_t ARMMCCodeEmitter::
1436 getAddrMode5OpValue(const MCInst
&MI
, unsigned OpIdx
,
1437 SmallVectorImpl
<MCFixup
> &Fixups
,
1438 const MCSubtargetInfo
&STI
) const {
1440 // {8} = (U)nsigned (add == '1', sub == '0')
1444 // If The first operand isn't a register, we have a label reference.
1445 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1447 Reg
= CTX
.getRegisterInfo()->getEncodingValue(ARM::PC
); // Rn is PC.
1449 isAdd
= false; // 'U' bit is handled as part of the fixup.
1451 assert(MO
.isExpr() && "Unexpected machine operand type!");
1452 const MCExpr
*Expr
= MO
.getExpr();
1455 Kind
= MCFixupKind(ARM::fixup_t2_pcrel_10
);
1457 Kind
= MCFixupKind(ARM::fixup_arm_pcrel_10
);
1458 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
1460 ++MCNumCPRelocations
;
1462 EncodeAddrModeOpValues(MI
, OpIdx
, Reg
, Imm8
, Fixups
, STI
);
1463 isAdd
= ARM_AM::getAM5Op(Imm8
) == ARM_AM::add
;
1466 uint32_t Binary
= ARM_AM::getAM5Offset(Imm8
);
1467 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1470 Binary
|= (Reg
<< 9);
1474 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand.
1475 uint32_t ARMMCCodeEmitter::
1476 getAddrMode5FP16OpValue(const MCInst
&MI
, unsigned OpIdx
,
1477 SmallVectorImpl
<MCFixup
> &Fixups
,
1478 const MCSubtargetInfo
&STI
) const {
1480 // {8} = (U)nsigned (add == '1', sub == '0')
1484 // If The first operand isn't a register, we have a label reference.
1485 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1487 Reg
= CTX
.getRegisterInfo()->getEncodingValue(ARM::PC
); // Rn is PC.
1489 isAdd
= false; // 'U' bit is handled as part of the fixup.
1491 assert(MO
.isExpr() && "Unexpected machine operand type!");
1492 const MCExpr
*Expr
= MO
.getExpr();
1495 Kind
= MCFixupKind(ARM::fixup_t2_pcrel_9
);
1497 Kind
= MCFixupKind(ARM::fixup_arm_pcrel_9
);
1498 Fixups
.push_back(MCFixup::create(0, Expr
, Kind
, MI
.getLoc()));
1500 ++MCNumCPRelocations
;
1502 EncodeAddrModeOpValues(MI
, OpIdx
, Reg
, Imm8
, Fixups
, STI
);
1503 isAdd
= ARM_AM::getAM5Op(Imm8
) == ARM_AM::add
;
1506 uint32_t Binary
= ARM_AM::getAM5Offset(Imm8
);
1507 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1510 Binary
|= (Reg
<< 9);
1514 unsigned ARMMCCodeEmitter::
1515 getSORegRegOpValue(const MCInst
&MI
, unsigned OpIdx
,
1516 SmallVectorImpl
<MCFixup
> &Fixups
,
1517 const MCSubtargetInfo
&STI
) const {
1518 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1519 // shifted. The second is Rs, the amount to shift by, and the third specifies
1520 // the type of the shift.
1528 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1529 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1530 const MCOperand
&MO2
= MI
.getOperand(OpIdx
+ 2);
1531 ARM_AM::ShiftOpc SOpc
= ARM_AM::getSORegShOp(MO2
.getImm());
1534 unsigned Binary
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1536 // Encode the shift opcode.
1538 MCRegister Rs
= MO1
.getReg();
1540 // Set shift operand (bit[7:4]).
1546 default: llvm_unreachable("Unknown shift opc!");
1547 case ARM_AM::lsl
: SBits
= 0x1; break;
1548 case ARM_AM::lsr
: SBits
= 0x3; break;
1549 case ARM_AM::asr
: SBits
= 0x5; break;
1550 case ARM_AM::ror
: SBits
= 0x7; break;
1554 Binary
|= SBits
<< 4;
1556 // Encode the shift operation Rs.
1557 // Encode Rs bit[11:8].
1558 assert(ARM_AM::getSORegOffset(MO2
.getImm()) == 0);
1559 return Binary
| (CTX
.getRegisterInfo()->getEncodingValue(Rs
) << ARMII::RegRsShift
);
1562 unsigned ARMMCCodeEmitter::
1563 getSORegImmOpValue(const MCInst
&MI
, unsigned OpIdx
,
1564 SmallVectorImpl
<MCFixup
> &Fixups
,
1565 const MCSubtargetInfo
&STI
) const {
1566 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1567 // shifted. The second is the amount to shift by.
1574 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1575 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1576 ARM_AM::ShiftOpc SOpc
= ARM_AM::getSORegShOp(MO1
.getImm());
1579 unsigned Binary
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1581 // Encode the shift opcode.
1584 // Set shift operand (bit[6:4]).
1589 // RRX - 110 and bit[11:8] clear.
1591 default: llvm_unreachable("Unknown shift opc!");
1592 case ARM_AM::lsl
: SBits
= 0x0; break;
1593 case ARM_AM::lsr
: SBits
= 0x2; break;
1594 case ARM_AM::asr
: SBits
= 0x4; break;
1595 case ARM_AM::ror
: SBits
= 0x6; break;
1601 // Encode shift_imm bit[11:7].
1602 Binary
|= SBits
<< 4;
1603 unsigned Offset
= ARM_AM::getSORegOffset(MO1
.getImm());
1604 assert(Offset
< 32 && "Offset must be in range 0-31!");
1605 return Binary
| (Offset
<< 7);
1609 unsigned ARMMCCodeEmitter::
1610 getT2AddrModeSORegOpValue(const MCInst
&MI
, unsigned OpNum
,
1611 SmallVectorImpl
<MCFixup
> &Fixups
,
1612 const MCSubtargetInfo
&STI
) const {
1613 const MCOperand
&MO1
= MI
.getOperand(OpNum
);
1614 const MCOperand
&MO2
= MI
.getOperand(OpNum
+1);
1615 const MCOperand
&MO3
= MI
.getOperand(OpNum
+2);
1617 // Encoded as [Rn, Rm, imm].
1618 // FIXME: Needs fixup support.
1619 unsigned Value
= CTX
.getRegisterInfo()->getEncodingValue(MO1
.getReg());
1621 Value
|= CTX
.getRegisterInfo()->getEncodingValue(MO2
.getReg());
1623 Value
|= MO3
.getImm();
1628 template<unsigned Bits
, unsigned Shift
>
1629 unsigned ARMMCCodeEmitter::
1630 getT2AddrModeImmOpValue(const MCInst
&MI
, unsigned OpNum
,
1631 SmallVectorImpl
<MCFixup
> &Fixups
,
1632 const MCSubtargetInfo
&STI
) const {
1633 const MCOperand
&MO1
= MI
.getOperand(OpNum
);
1634 const MCOperand
&MO2
= MI
.getOperand(OpNum
+1);
1636 // FIXME: Needs fixup support.
1637 unsigned Value
= CTX
.getRegisterInfo()->getEncodingValue(MO1
.getReg());
1639 // If the immediate is B bits long, we need B+1 bits in order
1640 // to represent the (inverse of the) sign bit.
1641 Value
<<= (Bits
+ 1);
1642 int32_t tmp
= (int32_t)MO2
.getImm();
1643 if (tmp
== INT32_MIN
) { // represents subtracting zero rather than adding it
1645 } else if (tmp
< 0) {
1648 Value
|= (1U << Bits
); // Set the ADD bit
1650 Value
|= (tmp
>> Shift
) & ((1U << Bits
) - 1);
1654 unsigned ARMMCCodeEmitter::
1655 getT2AddrModeImm8OffsetOpValue(const MCInst
&MI
, unsigned OpNum
,
1656 SmallVectorImpl
<MCFixup
> &Fixups
,
1657 const MCSubtargetInfo
&STI
) const {
1658 const MCOperand
&MO1
= MI
.getOperand(OpNum
);
1660 // FIXME: Needs fixup support.
1662 auto tmp
= static_cast<uint32_t>(MO1
.getImm());
1663 if (static_cast<int32_t>(tmp
) < 0)
1666 Value
|= 256; // Set the ADD bit
1671 unsigned ARMMCCodeEmitter::
1672 getT2SORegOpValue(const MCInst
&MI
, unsigned OpIdx
,
1673 SmallVectorImpl
<MCFixup
> &Fixups
,
1674 const MCSubtargetInfo
&STI
) const {
1675 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1676 // shifted. The second is the amount to shift by.
1683 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
1684 const MCOperand
&MO1
= MI
.getOperand(OpIdx
+ 1);
1685 ARM_AM::ShiftOpc SOpc
= ARM_AM::getSORegShOp(MO1
.getImm());
1688 unsigned Binary
= CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1690 // Encode the shift opcode.
1692 // Set shift operand (bit[6:4]).
1698 default: llvm_unreachable("Unknown shift opc!");
1699 case ARM_AM::lsl
: SBits
= 0x0; break;
1700 case ARM_AM::lsr
: SBits
= 0x2; break;
1701 case ARM_AM::asr
: SBits
= 0x4; break;
1702 case ARM_AM::rrx
: [[fallthrough
]];
1703 case ARM_AM::ror
: SBits
= 0x6; break;
1706 Binary
|= SBits
<< 4;
1707 if (SOpc
== ARM_AM::rrx
)
1710 // Encode shift_imm bit[11:7].
1711 return Binary
| ARM_AM::getSORegOffset(MO1
.getImm()) << 7;
1714 unsigned ARMMCCodeEmitter::
1715 getBitfieldInvertedMaskOpValue(const MCInst
&MI
, unsigned Op
,
1716 SmallVectorImpl
<MCFixup
> &Fixups
,
1717 const MCSubtargetInfo
&STI
) const {
1718 // 10 bits. lower 5 bits are the lsb of the mask, high five bits are the
1720 const MCOperand
&MO
= MI
.getOperand(Op
);
1721 uint32_t v
= ~MO
.getImm();
1722 uint32_t lsb
= llvm::countr_zero(v
);
1723 uint32_t msb
= llvm::Log2_32(v
);
1724 assert(v
!= 0 && lsb
< 32 && msb
< 32 && "Illegal bitfield mask!");
1725 return lsb
| (msb
<< 5);
1728 unsigned ARMMCCodeEmitter::
1729 getRegisterListOpValue(const MCInst
&MI
, unsigned Op
,
1730 SmallVectorImpl
<MCFixup
> &Fixups
,
1731 const MCSubtargetInfo
&STI
) const {
1732 // VLDM/VSTM/VSCCLRM:
1734 // {7-0} = Number of registers
1737 // {15-0} = Bitfield of GPRs.
1738 MCRegister Reg
= MI
.getOperand(Op
).getReg();
1739 bool SPRRegs
= ARMMCRegisterClasses
[ARM::SPRRegClassID
].contains(Reg
);
1740 bool DPRRegs
= ARMMCRegisterClasses
[ARM::DPRRegClassID
].contains(Reg
);
1742 unsigned Binary
= 0;
1744 if (SPRRegs
|| DPRRegs
|| Reg
== ARM::VPR
) {
1745 // VLDM/VSTM/VSCCLRM
1746 unsigned RegNo
= CTX
.getRegisterInfo()->getEncodingValue(Reg
);
1747 unsigned NumRegs
= (MI
.getNumOperands() - Op
) & 0xff;
1748 Binary
|= (RegNo
& 0x1f) << 8;
1750 if (MI
.getOpcode() == ARM::VSCCLRMD
)
1753 else if (MI
.getOpcode() == ARM::VSCCLRMS
) {
1754 // The register list can contain both S registers and D registers, with D
1755 // registers counting as two registers. VPR doesn't count towards the
1756 // number of registers.
1758 for (unsigned I
= Op
, E
= MI
.getNumOperands(); I
< E
; ++I
) {
1759 Reg
= MI
.getOperand(I
).getReg();
1760 if (ARMMCRegisterClasses
[ARM::SPRRegClassID
].contains(Reg
))
1762 else if (ARMMCRegisterClasses
[ARM::DPRRegClassID
].contains(Reg
))
1769 Binary
|= NumRegs
* 2;
1771 const MCRegisterInfo
&MRI
= *CTX
.getRegisterInfo();
1772 assert(is_sorted(drop_begin(MI
, Op
),
1773 [&](const MCOperand
&LHS
, const MCOperand
&RHS
) {
1774 return MRI
.getEncodingValue(LHS
.getReg()) <
1775 MRI
.getEncodingValue(RHS
.getReg());
1777 for (unsigned I
= Op
, E
= MI
.getNumOperands(); I
< E
; ++I
) {
1778 unsigned RegNo
= MRI
.getEncodingValue(MI
.getOperand(I
).getReg());
1779 Binary
|= 1 << RegNo
;
1786 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1787 /// with the alignment operand.
1788 unsigned ARMMCCodeEmitter::
1789 getAddrMode6AddressOpValue(const MCInst
&MI
, unsigned Op
,
1790 SmallVectorImpl
<MCFixup
> &Fixups
,
1791 const MCSubtargetInfo
&STI
) const {
1792 const MCOperand
&Reg
= MI
.getOperand(Op
);
1793 const MCOperand
&Imm
= MI
.getOperand(Op
+ 1);
1795 unsigned RegNo
= CTX
.getRegisterInfo()->getEncodingValue(Reg
.getReg());
1798 switch (Imm
.getImm()) {
1802 case 8: Align
= 0x01; break;
1803 case 16: Align
= 0x02; break;
1804 case 32: Align
= 0x03; break;
1807 return RegNo
| (Align
<< 4);
1810 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1811 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1812 unsigned ARMMCCodeEmitter::
1813 getAddrMode6OneLane32AddressOpValue(const MCInst
&MI
, unsigned Op
,
1814 SmallVectorImpl
<MCFixup
> &Fixups
,
1815 const MCSubtargetInfo
&STI
) const {
1816 const MCOperand
&Reg
= MI
.getOperand(Op
);
1817 const MCOperand
&Imm
= MI
.getOperand(Op
+ 1);
1819 unsigned RegNo
= CTX
.getRegisterInfo()->getEncodingValue(Reg
.getReg());
1822 switch (Imm
.getImm()) {
1826 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1827 case 2: Align
= 0x00; break;
1828 case 4: Align
= 0x03; break;
1831 return RegNo
| (Align
<< 4);
1835 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1836 /// alignment operand for use in VLD-dup instructions. This is the same as
1837 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1838 /// different for VLD4-dup.
1839 unsigned ARMMCCodeEmitter::
1840 getAddrMode6DupAddressOpValue(const MCInst
&MI
, unsigned Op
,
1841 SmallVectorImpl
<MCFixup
> &Fixups
,
1842 const MCSubtargetInfo
&STI
) const {
1843 const MCOperand
&Reg
= MI
.getOperand(Op
);
1844 const MCOperand
&Imm
= MI
.getOperand(Op
+ 1);
1846 unsigned RegNo
= CTX
.getRegisterInfo()->getEncodingValue(Reg
.getReg());
1849 switch (Imm
.getImm()) {
1853 case 8: Align
= 0x01; break;
1854 case 16: Align
= 0x03; break;
1857 return RegNo
| (Align
<< 4);
1860 unsigned ARMMCCodeEmitter::
1861 getAddrMode6OffsetOpValue(const MCInst
&MI
, unsigned Op
,
1862 SmallVectorImpl
<MCFixup
> &Fixups
,
1863 const MCSubtargetInfo
&STI
) const {
1864 const MCOperand
&MO
= MI
.getOperand(Op
);
1867 return CTX
.getRegisterInfo()->getEncodingValue(MO
.getReg());
1870 unsigned ARMMCCodeEmitter::
1871 getShiftRight8Imm(const MCInst
&MI
, unsigned Op
,
1872 SmallVectorImpl
<MCFixup
> &Fixups
,
1873 const MCSubtargetInfo
&STI
) const {
1874 return 8 - MI
.getOperand(Op
).getImm();
1877 unsigned ARMMCCodeEmitter::
1878 getShiftRight16Imm(const MCInst
&MI
, unsigned Op
,
1879 SmallVectorImpl
<MCFixup
> &Fixups
,
1880 const MCSubtargetInfo
&STI
) const {
1881 return 16 - MI
.getOperand(Op
).getImm();
1884 unsigned ARMMCCodeEmitter::
1885 getShiftRight32Imm(const MCInst
&MI
, unsigned Op
,
1886 SmallVectorImpl
<MCFixup
> &Fixups
,
1887 const MCSubtargetInfo
&STI
) const {
1888 return 32 - MI
.getOperand(Op
).getImm();
1891 unsigned ARMMCCodeEmitter::
1892 getShiftRight64Imm(const MCInst
&MI
, unsigned Op
,
1893 SmallVectorImpl
<MCFixup
> &Fixups
,
1894 const MCSubtargetInfo
&STI
) const {
1895 return 64 - MI
.getOperand(Op
).getImm();
1898 void ARMMCCodeEmitter::encodeInstruction(const MCInst
&MI
,
1899 SmallVectorImpl
<char> &CB
,
1900 SmallVectorImpl
<MCFixup
> &Fixups
,
1901 const MCSubtargetInfo
&STI
) const {
1902 // Pseudo instructions don't get encoded.
1903 const MCInstrDesc
&Desc
= MCII
.get(MI
.getOpcode());
1904 uint64_t TSFlags
= Desc
.TSFlags
;
1905 if ((TSFlags
& ARMII::FormMask
) == ARMII::Pseudo
)
1909 if (Desc
.getSize() == 2 || Desc
.getSize() == 4)
1910 Size
= Desc
.getSize();
1912 llvm_unreachable("Unexpected instruction size!");
1915 IsLittleEndian
? llvm::endianness::little
: llvm::endianness::big
;
1916 uint32_t Binary
= getBinaryCodeForInstr(MI
, Fixups
, STI
);
1918 support::endian::write
<uint16_t>(CB
, Binary
, Endian
);
1919 } else if (isThumb(STI
)) {
1920 // Thumb 32-bit wide instructions need to emit the high order halfword
1922 support::endian::write
<uint16_t>(CB
, Binary
>> 16, Endian
);
1923 support::endian::write
<uint16_t>(CB
, Binary
& 0xffff, Endian
);
1925 support::endian::write
<uint32_t>(CB
, Binary
, Endian
);
1927 ++MCNumEmitted
; // Keep track of the # of mi's emitted.
1930 template <bool isNeg
, ARM::Fixups fixup
>
1932 ARMMCCodeEmitter::getBFTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
1933 SmallVectorImpl
<MCFixup
> &Fixups
,
1934 const MCSubtargetInfo
&STI
) const {
1935 const MCOperand MO
= MI
.getOperand(OpIdx
);
1937 return ::getBranchTargetOpValue(MI
, OpIdx
, fixup
, Fixups
, STI
);
1938 return isNeg
? -(MO
.getImm() >> 1) : (MO
.getImm() >> 1);
1942 ARMMCCodeEmitter::getBFAfterTargetOpValue(const MCInst
&MI
, unsigned OpIdx
,
1943 SmallVectorImpl
<MCFixup
> &Fixups
,
1944 const MCSubtargetInfo
&STI
) const {
1945 const MCOperand MO
= MI
.getOperand(OpIdx
);
1946 const MCOperand BranchMO
= MI
.getOperand(0);
1949 assert(BranchMO
.isExpr());
1950 const MCExpr
*DiffExpr
= MCBinaryExpr::createSub(
1951 MO
.getExpr(), BranchMO
.getExpr(), CTX
);
1952 MCFixupKind Kind
= MCFixupKind(ARM::fixup_bfcsel_else_target
);
1953 Fixups
.push_back(llvm::MCFixup::create(0, DiffExpr
, Kind
, MI
.getLoc()));
1957 assert(MO
.isImm() && BranchMO
.isImm());
1958 int Diff
= MO
.getImm() - BranchMO
.getImm();
1959 assert(Diff
== 4 || Diff
== 2);
1964 uint32_t ARMMCCodeEmitter::getVPTMaskOpValue(const MCInst
&MI
, unsigned OpIdx
,
1965 SmallVectorImpl
<MCFixup
> &Fixups
,
1966 const MCSubtargetInfo
&STI
)const {
1967 const MCOperand MO
= MI
.getOperand(OpIdx
);
1968 assert(MO
.isImm() && "Unexpected operand type!");
1970 int Value
= MO
.getImm();
1973 // VPT Masks are actually encoded as a series of invert/don't invert bits,
1974 // rather than true/false bits.
1975 unsigned PrevBit
= 0;
1976 for (int i
= 3; i
>= 0; --i
) {
1977 unsigned Bit
= (Value
>> i
) & 1;
1979 // Check if we are at the end of the mask.
1980 if ((Value
& ~(~0U << i
)) == 0) {
1985 // Convert the bit in the mask based on the previous bit.
1995 uint32_t ARMMCCodeEmitter::getRestrictedCondCodeOpValue(
1996 const MCInst
&MI
, unsigned OpIdx
, SmallVectorImpl
<MCFixup
> &Fixups
,
1997 const MCSubtargetInfo
&STI
) const {
1999 const MCOperand MO
= MI
.getOperand(OpIdx
);
2000 assert(MO
.isImm() && "Unexpected operand type!");
2002 switch (MO
.getImm()) {
2004 assert(0 && "Unexpected Condition!");
2023 uint32_t ARMMCCodeEmitter::
2024 getPowerTwoOpValue(const MCInst
&MI
, unsigned OpIdx
,
2025 SmallVectorImpl
<MCFixup
> &Fixups
,
2026 const MCSubtargetInfo
&STI
) const {
2027 const MCOperand
&MO
= MI
.getOperand(OpIdx
);
2028 assert(MO
.isImm() && "Unexpected operand type!");
2029 return llvm::countr_zero((uint64_t)MO
.getImm());
2032 template <unsigned start
>
2033 uint32_t ARMMCCodeEmitter::
2034 getMVEPairVectorIndexOpValue(const MCInst
&MI
, unsigned OpIdx
,
2035 SmallVectorImpl
<MCFixup
> &Fixups
,
2036 const MCSubtargetInfo
&STI
) const {
2037 const MCOperand MO
= MI
.getOperand(OpIdx
);
2038 assert(MO
.isImm() && "Unexpected operand type!");
2040 int Value
= MO
.getImm();
2041 return Value
- start
;
2044 #include "ARMGenMCCodeEmitter.inc"
2046 MCCodeEmitter
*llvm::createARMLEMCCodeEmitter(const MCInstrInfo
&MCII
,
2048 return new ARMMCCodeEmitter(MCII
, Ctx
, true);
2051 MCCodeEmitter
*llvm::createARMBEMCCodeEmitter(const MCInstrInfo
&MCII
,
2053 return new ARMMCCodeEmitter(MCII
, Ctx
, false);