1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/ARMAddressingModes.h"
15 #include "MCTargetDesc/ARMBaseInfo.h"
16 #include "MCTargetDesc/ARMFixupKinds.h"
17 #include "MCTargetDesc/ARMMCExpr.h"
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/Triple.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCFixup.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCRegisterInfo.h"
31 #include "llvm/MC/MCSubtargetInfo.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/raw_ostream.h"
44 #define DEBUG_TYPE "mccodeemitter"
46 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
47 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
51 class ARMMCCodeEmitter : public MCCodeEmitter {
52 const MCInstrInfo &MCII;
57 ARMMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx, bool IsLittle)
58 : MCII(mcii), CTX(ctx), IsLittleEndian(IsLittle) {
60 ARMMCCodeEmitter(const ARMMCCodeEmitter &) = delete;
61 ARMMCCodeEmitter &operator=(const ARMMCCodeEmitter &) = delete;
62 ~ARMMCCodeEmitter() override = default;
64 bool isThumb(const MCSubtargetInfo &STI) const {
65 return STI.getFeatureBits()[ARM::ModeThumb];
68 bool isThumb2(const MCSubtargetInfo &STI) const {
69 return isThumb(STI) && STI.getFeatureBits()[ARM::FeatureThumb2];
72 bool isTargetMachO(const MCSubtargetInfo &STI) const {
73 const Triple &TT = STI.getTargetTriple();
74 return TT.isOSBinFormatMachO();
77 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
79 // getBinaryCodeForInstr - TableGen'erated function for getting the
80 // binary encoding for an instruction.
81 uint64_t getBinaryCodeForInstr(const MCInst &MI,
82 SmallVectorImpl<MCFixup> &Fixups,
83 const MCSubtargetInfo &STI) const;
85 /// getMachineOpValue - Return binary encoding of operand. If the machine
86 /// operand requires relocation, record the relocation and return zero.
87 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
91 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
92 /// the specified operand. This is used for operands with :lower16: and
93 /// :upper16: prefixes.
94 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups,
96 const MCSubtargetInfo &STI) const;
98 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
99 unsigned &Reg, unsigned &Imm,
100 SmallVectorImpl<MCFixup> &Fixups,
101 const MCSubtargetInfo &STI) const;
103 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
104 /// BL branch target.
105 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups,
107 const MCSubtargetInfo &STI) const;
109 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
110 /// BLX branch target.
111 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups,
113 const MCSubtargetInfo &STI) const;
115 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
116 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups,
118 const MCSubtargetInfo &STI) const;
120 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
121 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups,
123 const MCSubtargetInfo &STI) const;
125 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
126 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups,
128 const MCSubtargetInfo &STI) const;
130 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
132 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups,
134 const MCSubtargetInfo &STI) const;
136 /// getThumbBranchTargetOpValue - Return encoding info for 24-bit
137 /// immediate Thumb2 direct branch target.
138 uint32_t getThumbBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups,
140 const MCSubtargetInfo &STI) const;
142 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
144 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups,
146 const MCSubtargetInfo &STI) const;
147 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const;
150 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups,
152 const MCSubtargetInfo &STI) const;
154 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
155 /// ADR label target.
156 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups,
158 const MCSubtargetInfo &STI) const;
159 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups,
161 const MCSubtargetInfo &STI) const;
162 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups,
164 const MCSubtargetInfo &STI) const;
167 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
169 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups,
171 const MCSubtargetInfo &STI) const;
173 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
174 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
175 SmallVectorImpl<MCFixup> &Fixups,
176 const MCSubtargetInfo &STI) const;
178 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
180 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups,
182 const MCSubtargetInfo &STI) const;
184 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
186 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups,
188 const MCSubtargetInfo &STI) const;
190 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
192 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups,
194 const MCSubtargetInfo &STI) const;
197 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
198 /// operand as needed by load/store instructions.
199 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &Fixups,
201 const MCSubtargetInfo &STI) const;
203 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
204 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups,
206 const MCSubtargetInfo &STI) const {
207 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
209 default: llvm_unreachable("Unknown addressing sub-mode!");
210 case ARM_AM::da: return 0;
211 case ARM_AM::ia: return 1;
212 case ARM_AM::db: return 2;
213 case ARM_AM::ib: return 3;
217 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
219 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
221 case ARM_AM::no_shift:
222 case ARM_AM::lsl: return 0;
223 case ARM_AM::lsr: return 1;
224 case ARM_AM::asr: return 2;
226 case ARM_AM::rrx: return 3;
228 llvm_unreachable("Invalid ShiftOpc!");
231 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
232 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
233 SmallVectorImpl<MCFixup> &Fixups,
234 const MCSubtargetInfo &STI) const;
236 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
237 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
238 SmallVectorImpl<MCFixup> &Fixups,
239 const MCSubtargetInfo &STI) const;
241 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
242 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
243 SmallVectorImpl<MCFixup> &Fixups,
244 const MCSubtargetInfo &STI) const;
246 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
247 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
248 SmallVectorImpl<MCFixup> &Fixups,
249 const MCSubtargetInfo &STI) const;
251 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
253 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
254 SmallVectorImpl<MCFixup> &Fixups,
255 const MCSubtargetInfo &STI) const;
257 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
258 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
259 SmallVectorImpl<MCFixup> &Fixups,
260 const MCSubtargetInfo &STI) const;
262 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
263 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
264 SmallVectorImpl<MCFixup> &Fixups,
265 const MCSubtargetInfo &STI) const;
267 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand.
268 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
269 SmallVectorImpl<MCFixup> &Fixups,
270 const MCSubtargetInfo &STI) const;
272 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand.
273 uint32_t getAddrMode5FP16OpValue(const MCInst &MI, unsigned OpIdx,
274 SmallVectorImpl<MCFixup> &Fixups,
275 const MCSubtargetInfo &STI) const;
277 /// getCCOutOpValue - Return encoding of the 's' bit.
278 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups,
280 const MCSubtargetInfo &STI) const {
281 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
283 return MI.getOperand(Op).getReg() == ARM::CPSR;
286 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
287 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups,
289 const MCSubtargetInfo &STI) const {
290 const MCOperand &MO = MI.getOperand(Op);
292 // We expect MO to be an immediate or an expression,
293 // if it is an immediate - that's fine, just encode the value.
294 // Otherwise - create a Fixup.
296 const MCExpr *Expr = MO.getExpr();
297 // In instruction code this value always encoded as lowest 12 bits,
298 // so we don't have to perform any specific adjustments.
299 // Due to requirements of relocatable records we have to use FK_Data_4.
300 // See ARMELFObjectWriter::ExplicitRelSym and
301 // ARMELFObjectWriter::GetRelocTypeInner for more details.
302 MCFixupKind Kind = MCFixupKind(FK_Data_4);
303 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
307 unsigned SoImm = MO.getImm();
308 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
309 assert(SoImmVal != -1 && "Not a valid so_imm value!");
311 // Encode rotate_imm.
312 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
313 << ARMII::SoRotImmShift;
316 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
320 unsigned getModImmOpValue(const MCInst &MI, unsigned Op,
321 SmallVectorImpl<MCFixup> &Fixups,
322 const MCSubtargetInfo &ST) const {
323 const MCOperand &MO = MI.getOperand(Op);
325 // Support for fixups (MCFixup)
327 const MCExpr *Expr = MO.getExpr();
328 // Fixups resolve to plain values that need to be encoded.
329 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_mod_imm);
330 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
334 // Immediate is already in its encoded format
338 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
339 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
340 SmallVectorImpl<MCFixup> &Fixups,
341 const MCSubtargetInfo &STI) const {
342 const MCOperand &MO = MI.getOperand(Op);
344 // Support for fixups (MCFixup)
346 const MCExpr *Expr = MO.getExpr();
347 // Fixups resolve to plain values that need to be encoded.
348 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_so_imm);
349 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
352 unsigned SoImm = MO.getImm();
353 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
354 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
358 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
359 SmallVectorImpl<MCFixup> &Fixups,
360 const MCSubtargetInfo &STI) const;
361 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
362 SmallVectorImpl<MCFixup> &Fixups,
363 const MCSubtargetInfo &STI) const;
364 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
365 SmallVectorImpl<MCFixup> &Fixups,
366 const MCSubtargetInfo &STI) const;
368 /// getSORegOpValue - Return an encoded so_reg shifted register value.
369 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
370 SmallVectorImpl<MCFixup> &Fixups,
371 const MCSubtargetInfo &STI) const;
372 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
373 SmallVectorImpl<MCFixup> &Fixups,
374 const MCSubtargetInfo &STI) const;
375 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
376 SmallVectorImpl<MCFixup> &Fixups,
377 const MCSubtargetInfo &STI) const;
379 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
380 SmallVectorImpl<MCFixup> &Fixups,
381 const MCSubtargetInfo &STI) const {
382 return 64 - MI.getOperand(Op).getImm();
385 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
386 SmallVectorImpl<MCFixup> &Fixups,
387 const MCSubtargetInfo &STI) const;
389 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
390 SmallVectorImpl<MCFixup> &Fixups,
391 const MCSubtargetInfo &STI) const;
392 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
393 SmallVectorImpl<MCFixup> &Fixups,
394 const MCSubtargetInfo &STI) const;
395 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
396 SmallVectorImpl<MCFixup> &Fixups,
397 const MCSubtargetInfo &STI) const;
398 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
399 SmallVectorImpl<MCFixup> &Fixups,
400 const MCSubtargetInfo &STI) const;
401 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
402 SmallVectorImpl<MCFixup> &Fixups,
403 const MCSubtargetInfo &STI) const;
405 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
406 SmallVectorImpl<MCFixup> &Fixups,
407 const MCSubtargetInfo &STI) const;
408 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
409 SmallVectorImpl<MCFixup> &Fixups,
410 const MCSubtargetInfo &STI) const;
411 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
412 SmallVectorImpl<MCFixup> &Fixups,
413 const MCSubtargetInfo &STI) const;
414 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
415 SmallVectorImpl<MCFixup> &Fixups,
416 const MCSubtargetInfo &STI) const;
418 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
419 SmallVectorImpl<MCFixup> &Fixups,
420 const MCSubtargetInfo &STI) const;
422 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
423 unsigned EncodedValue,
424 const MCSubtargetInfo &STI) const;
425 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
426 unsigned EncodedValue,
427 const MCSubtargetInfo &STI) const;
428 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
429 unsigned EncodedValue,
430 const MCSubtargetInfo &STI) const;
431 unsigned NEONThumb2V8PostEncoder(const MCInst &MI,
432 unsigned EncodedValue,
433 const MCSubtargetInfo &STI) const;
435 unsigned VFPThumb2PostEncoder(const MCInst &MI,
436 unsigned EncodedValue,
437 const MCSubtargetInfo &STI) const;
439 void EmitByte(unsigned char C, raw_ostream &OS) const {
443 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
444 // Output the constant in little endian byte order.
445 for (unsigned i = 0; i != Size; ++i) {
446 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
447 EmitByte((Val >> Shift) & 0xff, OS);
451 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
452 SmallVectorImpl<MCFixup> &Fixups,
453 const MCSubtargetInfo &STI) const override;
456 } // end anonymous namespace
458 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
459 /// instructions, and rewrite them to their Thumb2 form if we are currently in
461 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
462 unsigned EncodedValue,
463 const MCSubtargetInfo &STI) const {
465 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
466 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
468 unsigned Bit24 = EncodedValue & 0x01000000;
469 unsigned Bit28 = Bit24 << 4;
470 EncodedValue &= 0xEFFFFFFF;
471 EncodedValue |= Bit28;
472 EncodedValue |= 0x0F000000;
478 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
479 /// instructions, and rewrite them to their Thumb2 form if we are currently in
481 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
482 unsigned EncodedValue,
483 const MCSubtargetInfo &STI) const {
485 EncodedValue &= 0xF0FFFFFF;
486 EncodedValue |= 0x09000000;
492 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
493 /// instructions, and rewrite them to their Thumb2 form if we are currently in
495 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
496 unsigned EncodedValue,
497 const MCSubtargetInfo &STI) const {
499 EncodedValue &= 0x00FFFFFF;
500 EncodedValue |= 0xEE000000;
506 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
507 /// if we are in Thumb2.
508 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI,
509 unsigned EncodedValue,
510 const MCSubtargetInfo &STI) const {
512 EncodedValue |= 0xC000000; // Set bits 27-26
518 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
519 /// them to their Thumb2 form if we are currently in Thumb2 mode.
520 unsigned ARMMCCodeEmitter::
521 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue,
522 const MCSubtargetInfo &STI) const {
524 EncodedValue &= 0x0FFFFFFF;
525 EncodedValue |= 0xE0000000;
530 /// getMachineOpValue - Return binary encoding of operand. If the machine
531 /// operand requires relocation, record the relocation and return zero.
532 unsigned ARMMCCodeEmitter::
533 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
534 SmallVectorImpl<MCFixup> &Fixups,
535 const MCSubtargetInfo &STI) const {
537 unsigned Reg = MO.getReg();
538 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
540 // Q registers are encoded as 2x their register number.
544 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
545 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
546 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
547 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
550 } else if (MO.isImm()) {
551 return static_cast<unsigned>(MO.getImm());
552 } else if (MO.isFPImm()) {
553 return static_cast<unsigned>(APFloat(MO.getFPImm())
554 .bitcastToAPInt().getHiBits(32).getLimitedValue());
557 llvm_unreachable("Unable to encode MCOperand!");
560 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
561 bool ARMMCCodeEmitter::
562 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
563 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups,
564 const MCSubtargetInfo &STI) const {
565 const MCOperand &MO = MI.getOperand(OpIdx);
566 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
568 Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
570 int32_t SImm = MO1.getImm();
573 // Special value for #-0
574 if (SImm == INT32_MIN) {
579 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
589 /// getBranchTargetOpValue - Helper function to get the branch target operand,
590 /// which is either an immediate or requires a fixup.
591 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
593 SmallVectorImpl<MCFixup> &Fixups,
594 const MCSubtargetInfo &STI) {
595 const MCOperand &MO = MI.getOperand(OpIdx);
597 // If the destination is an immediate, we have nothing to do.
598 if (MO.isImm()) return MO.getImm();
599 assert(MO.isExpr() && "Unexpected branch target type!");
600 const MCExpr *Expr = MO.getExpr();
601 MCFixupKind Kind = MCFixupKind(FixupKind);
602 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
604 // All of the information is in the fixup.
608 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
609 // determined by negating them and XOR'ing them with bit 23.
610 static int32_t encodeThumbBLOffset(int32_t offset) {
612 uint32_t S = (offset & 0x800000) >> 23;
613 uint32_t J1 = (offset & 0x400000) >> 22;
614 uint32_t J2 = (offset & 0x200000) >> 21;
627 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
628 uint32_t ARMMCCodeEmitter::
629 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
630 SmallVectorImpl<MCFixup> &Fixups,
631 const MCSubtargetInfo &STI) const {
632 const MCOperand MO = MI.getOperand(OpIdx);
634 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
636 return encodeThumbBLOffset(MO.getImm());
639 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
640 /// BLX branch target.
641 uint32_t ARMMCCodeEmitter::
642 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
643 SmallVectorImpl<MCFixup> &Fixups,
644 const MCSubtargetInfo &STI) const {
645 const MCOperand MO = MI.getOperand(OpIdx);
647 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
649 return encodeThumbBLOffset(MO.getImm());
652 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
653 uint32_t ARMMCCodeEmitter::
654 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
655 SmallVectorImpl<MCFixup> &Fixups,
656 const MCSubtargetInfo &STI) const {
657 const MCOperand MO = MI.getOperand(OpIdx);
659 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
661 return (MO.getImm() >> 1);
664 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
665 uint32_t ARMMCCodeEmitter::
666 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
667 SmallVectorImpl<MCFixup> &Fixups,
668 const MCSubtargetInfo &STI) const {
669 const MCOperand MO = MI.getOperand(OpIdx);
671 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
673 return (MO.getImm() >> 1);
676 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
677 uint32_t ARMMCCodeEmitter::
678 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups,
680 const MCSubtargetInfo &STI) const {
681 const MCOperand MO = MI.getOperand(OpIdx);
683 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups, STI);
684 return (MO.getImm() >> 1);
687 /// Return true if this branch has a non-always predication
688 static bool HasConditionalBranch(const MCInst &MI) {
689 int NumOp = MI.getNumOperands();
691 for (int i = 0; i < NumOp-1; ++i) {
692 const MCOperand &MCOp1 = MI.getOperand(i);
693 const MCOperand &MCOp2 = MI.getOperand(i + 1);
694 if (MCOp1.isImm() && MCOp2.isReg() &&
695 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
696 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
704 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
706 uint32_t ARMMCCodeEmitter::
707 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
708 SmallVectorImpl<MCFixup> &Fixups,
709 const MCSubtargetInfo &STI) const {
710 // FIXME: This really, really shouldn't use TargetMachine. We don't want
711 // coupling between MC and TM anywhere we can help it.
714 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups, STI);
715 return getARMBranchTargetOpValue(MI, OpIdx, Fixups, STI);
718 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
720 uint32_t ARMMCCodeEmitter::
721 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
722 SmallVectorImpl<MCFixup> &Fixups,
723 const MCSubtargetInfo &STI) const {
724 const MCOperand MO = MI.getOperand(OpIdx);
726 if (HasConditionalBranch(MI))
727 return ::getBranchTargetOpValue(MI, OpIdx,
728 ARM::fixup_arm_condbranch, Fixups, STI);
729 return ::getBranchTargetOpValue(MI, OpIdx,
730 ARM::fixup_arm_uncondbranch, Fixups, STI);
733 return MO.getImm() >> 2;
736 uint32_t ARMMCCodeEmitter::
737 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
738 SmallVectorImpl<MCFixup> &Fixups,
739 const MCSubtargetInfo &STI) const {
740 const MCOperand MO = MI.getOperand(OpIdx);
742 if (HasConditionalBranch(MI))
743 return ::getBranchTargetOpValue(MI, OpIdx,
744 ARM::fixup_arm_condbl, Fixups, STI);
745 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups, STI);
748 return MO.getImm() >> 2;
751 uint32_t ARMMCCodeEmitter::
752 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
753 SmallVectorImpl<MCFixup> &Fixups,
754 const MCSubtargetInfo &STI) const {
755 const MCOperand MO = MI.getOperand(OpIdx);
757 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups, STI);
759 return MO.getImm() >> 1;
762 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
763 /// immediate branch target.
764 uint32_t ARMMCCodeEmitter::getThumbBranchTargetOpValue(
765 const MCInst &MI, unsigned OpIdx, SmallVectorImpl<MCFixup> &Fixups,
766 const MCSubtargetInfo &STI) const {
768 const MCOperand MO = MI.getOperand(OpIdx);
771 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups, STI);
773 Val = MO.getImm() >> 1;
775 bool I = (Val & 0x800000);
776 bool J1 = (Val & 0x400000);
777 bool J2 = (Val & 0x200000);
791 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
792 /// ADR label target.
793 uint32_t ARMMCCodeEmitter::
794 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
795 SmallVectorImpl<MCFixup> &Fixups,
796 const MCSubtargetInfo &STI) const {
797 const MCOperand MO = MI.getOperand(OpIdx);
799 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
801 int64_t offset = MO.getImm();
802 uint32_t Val = 0x2000;
805 if (offset == INT32_MIN) {
808 } else if (offset < 0) {
811 SoImmVal = ARM_AM::getSOImmVal(offset);
815 SoImmVal = ARM_AM::getSOImmVal(offset);
818 SoImmVal = ARM_AM::getSOImmVal(offset);
822 SoImmVal = ARM_AM::getSOImmVal(offset);
826 assert(SoImmVal != -1 && "Not a valid so_imm value!");
832 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
834 uint32_t ARMMCCodeEmitter::
835 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
836 SmallVectorImpl<MCFixup> &Fixups,
837 const MCSubtargetInfo &STI) const {
838 const MCOperand MO = MI.getOperand(OpIdx);
840 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
842 int32_t Val = MO.getImm();
843 if (Val == INT32_MIN)
852 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
854 uint32_t ARMMCCodeEmitter::
855 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
856 SmallVectorImpl<MCFixup> &Fixups,
857 const MCSubtargetInfo &STI) const {
858 const MCOperand MO = MI.getOperand(OpIdx);
860 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
865 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
867 uint32_t ARMMCCodeEmitter::
868 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
869 SmallVectorImpl<MCFixup> &,
870 const MCSubtargetInfo &STI) const {
874 const MCOperand &MO1 = MI.getOperand(OpIdx);
875 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
876 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
877 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
878 return (Rm << 3) | Rn;
881 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
882 uint32_t ARMMCCodeEmitter::
883 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
884 SmallVectorImpl<MCFixup> &Fixups,
885 const MCSubtargetInfo &STI) const {
887 // {12} = (U)nsigned (add == '1', sub == '0')
891 // If The first operand isn't a register, we have a label reference.
892 const MCOperand &MO = MI.getOperand(OpIdx);
894 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
898 const MCExpr *Expr = MO.getExpr();
899 isAdd = false ; // 'U' bit is set as part of the fixup.
903 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
905 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
906 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
908 ++MCNumCPRelocations;
911 int32_t Offset = MO.getImm();
912 if (Offset == INT32_MIN) {
915 } else if (Offset < 0) {
922 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups, STI);
924 uint32_t Binary = Imm12 & 0xfff;
925 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
928 Binary |= (Reg << 13);
932 /// getT2Imm8s4OpValue - Return encoding info for
933 /// '+/- imm8<<2' operand.
934 uint32_t ARMMCCodeEmitter::
935 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
936 SmallVectorImpl<MCFixup> &Fixups,
937 const MCSubtargetInfo &STI) const {
938 // FIXME: The immediate operand should have already been encoded like this
939 // before ever getting here. The encoder method should just need to combine
940 // the MI operands for the register and the offset into a single
941 // representation for the complex operand in the .td file. This isn't just
942 // style, unfortunately. As-is, we can't represent the distinct encoding
945 // {8} = (U)nsigned (add == '1', sub == '0')
947 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
948 bool isAdd = Imm8 >= 0;
950 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
952 Imm8 = -(uint32_t)Imm8;
957 uint32_t Binary = Imm8 & 0xff;
958 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
964 /// getT2AddrModeImm8s4OpValue - Return encoding info for
965 /// 'reg +/- imm8<<2' operand.
966 uint32_t ARMMCCodeEmitter::
967 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
968 SmallVectorImpl<MCFixup> &Fixups,
969 const MCSubtargetInfo &STI) const {
971 // {8} = (U)nsigned (add == '1', sub == '0')
975 // If The first operand isn't a register, we have a label reference.
976 const MCOperand &MO = MI.getOperand(OpIdx);
978 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
980 isAdd = false ; // 'U' bit is set as part of the fixup.
982 assert(MO.isExpr() && "Unexpected machine operand type!");
983 const MCExpr *Expr = MO.getExpr();
984 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
985 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
987 ++MCNumCPRelocations;
989 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
991 // FIXME: The immediate operand should have already been encoded like this
992 // before ever getting here. The encoder method should just need to combine
993 // the MI operands for the register and the offset into a single
994 // representation for the complex operand in the .td file. This isn't just
995 // style, unfortunately. As-is, we can't represent the distinct encoding
997 uint32_t Binary = (Imm8 >> 2) & 0xff;
998 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1001 Binary |= (Reg << 9);
1005 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
1006 /// 'reg + imm8<<2' operand.
1007 uint32_t ARMMCCodeEmitter::
1008 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
1009 SmallVectorImpl<MCFixup> &Fixups,
1010 const MCSubtargetInfo &STI) const {
1013 const MCOperand &MO = MI.getOperand(OpIdx);
1014 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1015 unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1016 unsigned Imm8 = MO1.getImm();
1017 return (Reg << 8) | Imm8;
1021 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
1022 SmallVectorImpl<MCFixup> &Fixups,
1023 const MCSubtargetInfo &STI) const {
1024 // {20-16} = imm{15-12}
1025 // {11-0} = imm{11-0}
1026 const MCOperand &MO = MI.getOperand(OpIdx);
1028 // Hi / lo 16 bits already extracted during earlier passes.
1029 return static_cast<unsigned>(MO.getImm());
1031 // Handle :upper16: and :lower16: assembly prefixes.
1032 const MCExpr *E = MO.getExpr();
1034 if (E->getKind() == MCExpr::Target) {
1035 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
1036 E = ARM16Expr->getSubExpr();
1038 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(E)) {
1039 const int64_t Value = MCE->getValue();
1040 if (Value > UINT32_MAX)
1041 report_fatal_error("constant value truncated (limited to 32-bit)");
1043 switch (ARM16Expr->getKind()) {
1044 case ARMMCExpr::VK_ARM_HI16:
1045 return (int32_t(Value) & 0xffff0000) >> 16;
1046 case ARMMCExpr::VK_ARM_LO16:
1047 return (int32_t(Value) & 0x0000ffff);
1048 default: llvm_unreachable("Unsupported ARMFixup");
1052 switch (ARM16Expr->getKind()) {
1053 default: llvm_unreachable("Unsupported ARMFixup");
1054 case ARMMCExpr::VK_ARM_HI16:
1055 Kind = MCFixupKind(isThumb(STI) ? ARM::fixup_t2_movt_hi16
1056 : ARM::fixup_arm_movt_hi16);
1058 case ARMMCExpr::VK_ARM_LO16:
1059 Kind = MCFixupKind(isThumb(STI) ? ARM::fixup_t2_movw_lo16
1060 : ARM::fixup_arm_movw_lo16);
1064 Fixups.push_back(MCFixup::create(0, E, Kind, MI.getLoc()));
1067 // If the expression doesn't have :upper16: or :lower16: on it,
1068 // it's just a plain immediate expression, previously those evaluated to
1069 // the lower 16 bits of the expression regardless of whether
1070 // we have a movt or a movw, but that led to misleadingly results.
1071 // This is disallowed in the AsmParser in validateInstruction()
1072 // so this should never happen.
1073 llvm_unreachable("expression without :upper16: or :lower16:");
1076 uint32_t ARMMCCodeEmitter::
1077 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
1078 SmallVectorImpl<MCFixup> &Fixups,
1079 const MCSubtargetInfo &STI) const {
1080 const MCOperand &MO = MI.getOperand(OpIdx);
1081 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1082 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1083 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1084 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1085 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
1086 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
1087 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
1088 unsigned SBits = getShiftOp(ShOp);
1090 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
1091 // amount. However, it would be an easy mistake to make so check here.
1092 assert((ShImm & ~0x1f) == 0 && "Out of range shift amount");
1101 uint32_t Binary = Rm;
1103 Binary |= SBits << 5;
1104 Binary |= ShImm << 7;
1110 uint32_t ARMMCCodeEmitter::
1111 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1112 SmallVectorImpl<MCFixup> &Fixups,
1113 const MCSubtargetInfo &STI) const {
1114 // {13} 1 == imm12, 0 == Rm
1117 const MCOperand &MO = MI.getOperand(OpIdx);
1118 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1119 unsigned Imm = MO1.getImm();
1120 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
1121 bool isReg = MO.getReg() != 0;
1122 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
1123 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1125 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
1126 Binary <<= 7; // Shift amount is bits [11:7]
1127 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
1128 Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0]
1130 return Binary | (isAdd << 12) | (isReg << 13);
1133 uint32_t ARMMCCodeEmitter::
1134 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
1135 SmallVectorImpl<MCFixup> &Fixups,
1136 const MCSubtargetInfo &STI) const {
1139 const MCOperand &MO = MI.getOperand(OpIdx);
1140 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1141 bool isAdd = MO1.getImm() != 0;
1142 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4);
1145 uint32_t ARMMCCodeEmitter::
1146 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1147 SmallVectorImpl<MCFixup> &Fixups,
1148 const MCSubtargetInfo &STI) const {
1149 // {9} 1 == imm8, 0 == Rm
1151 // {7-4} imm7_4/zero
1153 const MCOperand &MO = MI.getOperand(OpIdx);
1154 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1155 unsigned Imm = MO1.getImm();
1156 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1157 bool isImm = MO.getReg() == 0;
1158 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1159 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1161 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1162 return Imm8 | (isAdd << 8) | (isImm << 9);
1165 uint32_t ARMMCCodeEmitter::
1166 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1167 SmallVectorImpl<MCFixup> &Fixups,
1168 const MCSubtargetInfo &STI) const {
1169 // {13} 1 == imm8, 0 == Rm
1172 // {7-4} imm7_4/zero
1174 const MCOperand &MO = MI.getOperand(OpIdx);
1175 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1176 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1178 // If The first operand isn't a register, we have a label reference.
1180 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1182 assert(MO.isExpr() && "Unexpected machine operand type!");
1183 const MCExpr *Expr = MO.getExpr();
1184 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1185 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
1187 ++MCNumCPRelocations;
1188 return (Rn << 9) | (1 << 13);
1190 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1191 unsigned Imm = MO2.getImm();
1192 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1193 bool isImm = MO1.getReg() == 0;
1194 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1195 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1197 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1198 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1201 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1202 uint32_t ARMMCCodeEmitter::
1203 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1204 SmallVectorImpl<MCFixup> &Fixups,
1205 const MCSubtargetInfo &STI) const {
1208 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1209 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1210 "Unexpected base register!");
1212 // The immediate is already shifted for the implicit zeroes, so no change
1214 return MO1.getImm() & 0xff;
1217 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1218 uint32_t ARMMCCodeEmitter::
1219 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1220 SmallVectorImpl<MCFixup> &Fixups,
1221 const MCSubtargetInfo &STI) const {
1225 const MCOperand &MO = MI.getOperand(OpIdx);
1226 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1227 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1228 unsigned Imm5 = MO1.getImm();
1229 return ((Imm5 & 0x1f) << 3) | Rn;
1232 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1233 uint32_t ARMMCCodeEmitter::
1234 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1235 SmallVectorImpl<MCFixup> &Fixups,
1236 const MCSubtargetInfo &STI) const {
1237 const MCOperand MO = MI.getOperand(OpIdx);
1239 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups, STI);
1240 return (MO.getImm() >> 2);
1243 /// getAddrMode5OpValue - Return encoding info for 'reg +/- (imm8 << 2)' operand.
1244 uint32_t ARMMCCodeEmitter::
1245 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1246 SmallVectorImpl<MCFixup> &Fixups,
1247 const MCSubtargetInfo &STI) const {
1249 // {8} = (U)nsigned (add == '1', sub == '0')
1253 // If The first operand isn't a register, we have a label reference.
1254 const MCOperand &MO = MI.getOperand(OpIdx);
1256 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1258 isAdd = false; // 'U' bit is handled as part of the fixup.
1260 assert(MO.isExpr() && "Unexpected machine operand type!");
1261 const MCExpr *Expr = MO.getExpr();
1264 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1266 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1267 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
1269 ++MCNumCPRelocations;
1271 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1272 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1275 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1276 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1279 Binary |= (Reg << 9);
1283 /// getAddrMode5FP16OpValue - Return encoding info for 'reg +/- (imm8 << 1)' operand.
1284 uint32_t ARMMCCodeEmitter::
1285 getAddrMode5FP16OpValue(const MCInst &MI, unsigned OpIdx,
1286 SmallVectorImpl<MCFixup> &Fixups,
1287 const MCSubtargetInfo &STI) const {
1289 // {8} = (U)nsigned (add == '1', sub == '0')
1293 // If The first operand isn't a register, we have a label reference.
1294 const MCOperand &MO = MI.getOperand(OpIdx);
1296 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1298 isAdd = false; // 'U' bit is handled as part of the fixup.
1300 assert(MO.isExpr() && "Unexpected machine operand type!");
1301 const MCExpr *Expr = MO.getExpr();
1304 Kind = MCFixupKind(ARM::fixup_t2_pcrel_9);
1306 Kind = MCFixupKind(ARM::fixup_arm_pcrel_9);
1307 Fixups.push_back(MCFixup::create(0, Expr, Kind, MI.getLoc()));
1309 ++MCNumCPRelocations;
1311 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1312 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1315 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1316 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1319 Binary |= (Reg << 9);
1323 unsigned ARMMCCodeEmitter::
1324 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1325 SmallVectorImpl<MCFixup> &Fixups,
1326 const MCSubtargetInfo &STI) const {
1327 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1328 // shifted. The second is Rs, the amount to shift by, and the third specifies
1329 // the type of the shift.
1337 const MCOperand &MO = MI.getOperand(OpIdx);
1338 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1339 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1340 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1343 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1345 // Encode the shift opcode.
1347 unsigned Rs = MO1.getReg();
1349 // Set shift operand (bit[7:4]).
1355 default: llvm_unreachable("Unknown shift opc!");
1356 case ARM_AM::lsl: SBits = 0x1; break;
1357 case ARM_AM::lsr: SBits = 0x3; break;
1358 case ARM_AM::asr: SBits = 0x5; break;
1359 case ARM_AM::ror: SBits = 0x7; break;
1363 Binary |= SBits << 4;
1365 // Encode the shift operation Rs.
1366 // Encode Rs bit[11:8].
1367 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1368 return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift);
1371 unsigned ARMMCCodeEmitter::
1372 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1373 SmallVectorImpl<MCFixup> &Fixups,
1374 const MCSubtargetInfo &STI) const {
1375 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1376 // shifted. The second is the amount to shift by.
1383 const MCOperand &MO = MI.getOperand(OpIdx);
1384 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1385 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1388 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1390 // Encode the shift opcode.
1393 // Set shift operand (bit[6:4]).
1398 // RRX - 110 and bit[11:8] clear.
1400 default: llvm_unreachable("Unknown shift opc!");
1401 case ARM_AM::lsl: SBits = 0x0; break;
1402 case ARM_AM::lsr: SBits = 0x2; break;
1403 case ARM_AM::asr: SBits = 0x4; break;
1404 case ARM_AM::ror: SBits = 0x6; break;
1410 // Encode shift_imm bit[11:7].
1411 Binary |= SBits << 4;
1412 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1413 assert(Offset < 32 && "Offset must be in range 0-31!");
1414 return Binary | (Offset << 7);
1418 unsigned ARMMCCodeEmitter::
1419 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1420 SmallVectorImpl<MCFixup> &Fixups,
1421 const MCSubtargetInfo &STI) const {
1422 const MCOperand &MO1 = MI.getOperand(OpNum);
1423 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1424 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1426 // Encoded as [Rn, Rm, imm].
1427 // FIXME: Needs fixup support.
1428 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1430 Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
1432 Value |= MO3.getImm();
1437 unsigned ARMMCCodeEmitter::
1438 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1439 SmallVectorImpl<MCFixup> &Fixups,
1440 const MCSubtargetInfo &STI) const {
1441 const MCOperand &MO1 = MI.getOperand(OpNum);
1442 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1444 // FIXME: Needs fixup support.
1445 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1447 // Even though the immediate is 8 bits long, we need 9 bits in order
1448 // to represent the (inverse of the) sign bit.
1450 int32_t tmp = (int32_t)MO2.getImm();
1454 Value |= 256; // Set the ADD bit
1459 unsigned ARMMCCodeEmitter::
1460 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1461 SmallVectorImpl<MCFixup> &Fixups,
1462 const MCSubtargetInfo &STI) const {
1463 const MCOperand &MO1 = MI.getOperand(OpNum);
1465 // FIXME: Needs fixup support.
1467 int32_t tmp = (int32_t)MO1.getImm();
1471 Value |= 256; // Set the ADD bit
1476 unsigned ARMMCCodeEmitter::
1477 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1478 SmallVectorImpl<MCFixup> &Fixups,
1479 const MCSubtargetInfo &STI) const {
1480 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1481 // shifted. The second is the amount to shift by.
1488 const MCOperand &MO = MI.getOperand(OpIdx);
1489 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1490 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1493 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1495 // Encode the shift opcode.
1497 // Set shift operand (bit[6:4]).
1503 default: llvm_unreachable("Unknown shift opc!");
1504 case ARM_AM::lsl: SBits = 0x0; break;
1505 case ARM_AM::lsr: SBits = 0x2; break;
1506 case ARM_AM::asr: SBits = 0x4; break;
1507 case ARM_AM::rrx: LLVM_FALLTHROUGH;
1508 case ARM_AM::ror: SBits = 0x6; break;
1511 Binary |= SBits << 4;
1512 if (SOpc == ARM_AM::rrx)
1515 // Encode shift_imm bit[11:7].
1516 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1519 unsigned ARMMCCodeEmitter::
1520 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1521 SmallVectorImpl<MCFixup> &Fixups,
1522 const MCSubtargetInfo &STI) const {
1523 // 10 bits. lower 5 bits are the lsb of the mask, high five bits are the
1525 const MCOperand &MO = MI.getOperand(Op);
1526 uint32_t v = ~MO.getImm();
1527 uint32_t lsb = countTrailingZeros(v);
1528 uint32_t msb = (32 - countLeadingZeros (v)) - 1;
1529 assert(v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1530 return lsb | (msb << 5);
1533 unsigned ARMMCCodeEmitter::
1534 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1535 SmallVectorImpl<MCFixup> &Fixups,
1536 const MCSubtargetInfo &STI) const {
1539 // {7-0} = Number of registers
1542 // {15-0} = Bitfield of GPRs.
1543 unsigned Reg = MI.getOperand(Op).getReg();
1544 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1545 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1547 unsigned Binary = 0;
1549 if (SPRRegs || DPRRegs) {
1551 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
1552 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1553 Binary |= (RegNo & 0x1f) << 8;
1557 Binary |= NumRegs * 2;
1559 const MCRegisterInfo &MRI = *CTX.getRegisterInfo();
1560 assert(std::is_sorted(MI.begin() + Op, MI.end(),
1561 [&](const MCOperand &LHS, const MCOperand &RHS) {
1562 return MRI.getEncodingValue(LHS.getReg()) <
1563 MRI.getEncodingValue(RHS.getReg());
1566 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1567 unsigned RegNo = MRI.getEncodingValue(MI.getOperand(I).getReg());
1568 Binary |= 1 << RegNo;
1575 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1576 /// with the alignment operand.
1577 unsigned ARMMCCodeEmitter::
1578 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1579 SmallVectorImpl<MCFixup> &Fixups,
1580 const MCSubtargetInfo &STI) const {
1581 const MCOperand &Reg = MI.getOperand(Op);
1582 const MCOperand &Imm = MI.getOperand(Op + 1);
1584 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1587 switch (Imm.getImm()) {
1591 case 8: Align = 0x01; break;
1592 case 16: Align = 0x02; break;
1593 case 32: Align = 0x03; break;
1596 return RegNo | (Align << 4);
1599 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1600 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1601 unsigned ARMMCCodeEmitter::
1602 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1603 SmallVectorImpl<MCFixup> &Fixups,
1604 const MCSubtargetInfo &STI) const {
1605 const MCOperand &Reg = MI.getOperand(Op);
1606 const MCOperand &Imm = MI.getOperand(Op + 1);
1608 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1611 switch (Imm.getImm()) {
1615 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1616 case 2: Align = 0x00; break;
1617 case 4: Align = 0x03; break;
1620 return RegNo | (Align << 4);
1624 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1625 /// alignment operand for use in VLD-dup instructions. This is the same as
1626 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1627 /// different for VLD4-dup.
1628 unsigned ARMMCCodeEmitter::
1629 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1630 SmallVectorImpl<MCFixup> &Fixups,
1631 const MCSubtargetInfo &STI) const {
1632 const MCOperand &Reg = MI.getOperand(Op);
1633 const MCOperand &Imm = MI.getOperand(Op + 1);
1635 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1638 switch (Imm.getImm()) {
1642 case 8: Align = 0x01; break;
1643 case 16: Align = 0x03; break;
1646 return RegNo | (Align << 4);
1649 unsigned ARMMCCodeEmitter::
1650 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1651 SmallVectorImpl<MCFixup> &Fixups,
1652 const MCSubtargetInfo &STI) const {
1653 const MCOperand &MO = MI.getOperand(Op);
1654 if (MO.getReg() == 0) return 0x0D;
1655 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1658 unsigned ARMMCCodeEmitter::
1659 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1660 SmallVectorImpl<MCFixup> &Fixups,
1661 const MCSubtargetInfo &STI) const {
1662 return 8 - MI.getOperand(Op).getImm();
1665 unsigned ARMMCCodeEmitter::
1666 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1667 SmallVectorImpl<MCFixup> &Fixups,
1668 const MCSubtargetInfo &STI) const {
1669 return 16 - MI.getOperand(Op).getImm();
1672 unsigned ARMMCCodeEmitter::
1673 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1674 SmallVectorImpl<MCFixup> &Fixups,
1675 const MCSubtargetInfo &STI) const {
1676 return 32 - MI.getOperand(Op).getImm();
1679 unsigned ARMMCCodeEmitter::
1680 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1681 SmallVectorImpl<MCFixup> &Fixups,
1682 const MCSubtargetInfo &STI) const {
1683 return 64 - MI.getOperand(Op).getImm();
1686 void ARMMCCodeEmitter::
1687 encodeInstruction(const MCInst &MI, raw_ostream &OS,
1688 SmallVectorImpl<MCFixup> &Fixups,
1689 const MCSubtargetInfo &STI) const {
1690 // Pseudo instructions don't get encoded.
1691 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1692 uint64_t TSFlags = Desc.TSFlags;
1693 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1697 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1698 Size = Desc.getSize();
1700 llvm_unreachable("Unexpected instruction size!");
1702 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
1703 // Thumb 32-bit wide instructions need to emit the high order halfword
1705 if (isThumb(STI) && Size == 4) {
1706 EmitConstant(Binary >> 16, 2, OS);
1707 EmitConstant(Binary & 0xffff, 2, OS);
1709 EmitConstant(Binary, Size, OS);
1710 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1713 #include "ARMGenMCCodeEmitter.inc"
1715 MCCodeEmitter *llvm::createARMLEMCCodeEmitter(const MCInstrInfo &MCII,
1716 const MCRegisterInfo &MRI,
1718 return new ARMMCCodeEmitter(MCII, Ctx, true);
1721 MCCodeEmitter *llvm::createARMBEMCCodeEmitter(const MCInstrInfo &MCII,
1722 const MCRegisterInfo &MRI,
1724 return new ARMMCCodeEmitter(MCII, Ctx, false);