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 #define DEBUG_TYPE "mccodeemitter"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMCExpr.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Support/raw_ostream.h"
31 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
32 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
35 class ARMMCCodeEmitter : public MCCodeEmitter {
36 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
37 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
38 const MCInstrInfo &MCII;
39 const MCSubtargetInfo &STI;
42 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
44 : MCII(mcii), STI(sti) {
47 ~ARMMCCodeEmitter() {}
49 bool isThumb() const {
50 // FIXME: Can tablegen auto-generate this?
51 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
53 bool isThumb2() const {
54 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
56 bool isTargetDarwin() const {
57 Triple TT(STI.getTargetTriple());
58 Triple::OSType OS = TT.getOS();
59 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
62 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64 // getBinaryCodeForInstr - TableGen'erated function for getting the
65 // binary encoding for an instruction.
66 unsigned getBinaryCodeForInstr(const MCInst &MI,
67 SmallVectorImpl<MCFixup> &Fixups) const;
69 /// getMachineOpValue - Return binary encoding of operand. If the machine
70 /// operand requires relocation, record the relocation and return zero.
71 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
72 SmallVectorImpl<MCFixup> &Fixups) const;
74 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
75 /// the specified operand. This is used for operands with :lower16: and
76 /// :upper16: prefixes.
77 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
78 SmallVectorImpl<MCFixup> &Fixups) const;
80 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
81 unsigned &Reg, unsigned &Imm,
82 SmallVectorImpl<MCFixup> &Fixups) const;
84 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
86 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
89 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
90 /// BLX branch target.
91 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
94 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
95 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
98 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
99 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
102 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
103 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
106 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
108 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
111 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
112 /// immediate Thumb2 direct branch target.
113 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
116 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
118 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const;
121 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
122 /// ADR label target.
123 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
125 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
126 SmallVectorImpl<MCFixup> &Fixups) const;
127 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
131 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
133 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
136 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
137 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups)const;
140 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
142 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
146 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
147 /// operand as needed by load/store instructions.
148 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
151 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
152 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const {
154 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
156 default: assert(0 && "Unknown addressing sub-mode!");
157 case ARM_AM::da: return 0;
158 case ARM_AM::ia: return 1;
159 case ARM_AM::db: return 2;
160 case ARM_AM::ib: return 3;
163 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
165 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
167 default: llvm_unreachable("Unknown shift opc!");
168 case ARM_AM::no_shift:
169 case ARM_AM::lsl: return 0;
170 case ARM_AM::lsr: return 1;
171 case ARM_AM::asr: return 2;
173 case ARM_AM::rrx: return 3;
178 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
179 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
182 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
183 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
186 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
187 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
190 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
191 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
194 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
196 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
199 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
200 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &Fixups) const;
203 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
204 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
207 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
208 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
209 SmallVectorImpl<MCFixup> &Fixups) const;
211 /// getCCOutOpValue - Return encoding of the 's' bit.
212 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
213 SmallVectorImpl<MCFixup> &Fixups) const {
214 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
216 return MI.getOperand(Op).getReg() == ARM::CPSR;
219 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
220 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
221 SmallVectorImpl<MCFixup> &Fixups) const {
222 unsigned SoImm = MI.getOperand(Op).getImm();
223 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
224 assert(SoImmVal != -1 && "Not a valid so_imm value!");
226 // Encode rotate_imm.
227 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
228 << ARMII::SoRotImmShift;
231 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
235 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
236 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const {
238 unsigned SoImm = MI.getOperand(Op).getImm();
239 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
240 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
244 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
245 SmallVectorImpl<MCFixup> &Fixups) const;
246 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
247 SmallVectorImpl<MCFixup> &Fixups) const;
248 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
249 SmallVectorImpl<MCFixup> &Fixups) const;
250 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
251 SmallVectorImpl<MCFixup> &Fixups) const;
253 /// getSORegOpValue - Return an encoded so_reg shifted register value.
254 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const;
256 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const;
259 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const {
261 switch (MI.getOperand(Op).getImm()) {
262 default: assert (0 && "Not a valid rot_imm value!");
270 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const {
272 return MI.getOperand(Op).getImm() - 1;
275 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const {
277 return 64 - MI.getOperand(Op).getImm();
280 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
281 SmallVectorImpl<MCFixup> &Fixups) const;
283 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
286 unsigned getSsatBitPosValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
289 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
293 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
295 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
297 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
309 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
310 unsigned EncodedValue) const;
311 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
312 unsigned EncodedValue) const;
313 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
314 unsigned EncodedValue) const;
316 unsigned VFPThumb2PostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const;
319 void EmitByte(unsigned char C, raw_ostream &OS) const {
323 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
324 // Output the constant in little endian byte order.
325 for (unsigned i = 0; i != Size; ++i) {
326 EmitByte(Val & 255, OS);
331 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
332 SmallVectorImpl<MCFixup> &Fixups) const;
335 } // end anonymous namespace
337 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
338 const MCSubtargetInfo &STI,
340 return new ARMMCCodeEmitter(MCII, STI, Ctx);
343 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
344 /// instructions, and rewrite them to their Thumb2 form if we are currently in
346 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
347 unsigned EncodedValue) const {
349 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
350 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
352 unsigned Bit24 = EncodedValue & 0x01000000;
353 unsigned Bit28 = Bit24 << 4;
354 EncodedValue &= 0xEFFFFFFF;
355 EncodedValue |= Bit28;
356 EncodedValue |= 0x0F000000;
362 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
363 /// instructions, and rewrite them to their Thumb2 form if we are currently in
365 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
366 unsigned EncodedValue) const {
368 EncodedValue &= 0xF0FFFFFF;
369 EncodedValue |= 0x09000000;
375 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
376 /// instructions, and rewrite them to their Thumb2 form if we are currently in
378 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
379 unsigned EncodedValue) const {
381 EncodedValue &= 0x00FFFFFF;
382 EncodedValue |= 0xEE000000;
388 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
389 /// them to their Thumb2 form if we are currently in Thumb2 mode.
390 unsigned ARMMCCodeEmitter::
391 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
393 EncodedValue &= 0x0FFFFFFF;
394 EncodedValue |= 0xE0000000;
399 /// getMachineOpValue - Return binary encoding of operand. If the machine
400 /// operand requires relocation, record the relocation and return zero.
401 unsigned ARMMCCodeEmitter::
402 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
403 SmallVectorImpl<MCFixup> &Fixups) const {
405 unsigned Reg = MO.getReg();
406 unsigned RegNo = getARMRegisterNumbering(Reg);
408 // Q registers are encoded as 2x their register number.
412 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
413 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
414 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
415 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
418 } else if (MO.isImm()) {
419 return static_cast<unsigned>(MO.getImm());
420 } else if (MO.isFPImm()) {
421 return static_cast<unsigned>(APFloat(MO.getFPImm())
422 .bitcastToAPInt().getHiBits(32).getLimitedValue());
425 llvm_unreachable("Unable to encode MCOperand!");
429 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
430 bool ARMMCCodeEmitter::
431 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
432 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
433 const MCOperand &MO = MI.getOperand(OpIdx);
434 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
436 Reg = getARMRegisterNumbering(MO.getReg());
438 int32_t SImm = MO1.getImm();
441 // Special value for #-0
442 if (SImm == INT32_MIN)
445 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
455 /// getBranchTargetOpValue - Helper function to get the branch target operand,
456 /// which is either an immediate or requires a fixup.
457 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
459 SmallVectorImpl<MCFixup> &Fixups) {
460 const MCOperand &MO = MI.getOperand(OpIdx);
462 // If the destination is an immediate, we have nothing to do.
463 if (MO.isImm()) return MO.getImm();
464 assert(MO.isExpr() && "Unexpected branch target type!");
465 const MCExpr *Expr = MO.getExpr();
466 MCFixupKind Kind = MCFixupKind(FixupKind);
467 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
469 // All of the information is in the fixup.
473 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
474 uint32_t ARMMCCodeEmitter::
475 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
476 SmallVectorImpl<MCFixup> &Fixups) const {
477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
480 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
481 /// BLX branch target.
482 uint32_t ARMMCCodeEmitter::
483 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
484 SmallVectorImpl<MCFixup> &Fixups) const {
485 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
488 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
489 uint32_t ARMMCCodeEmitter::
490 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
492 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
495 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
496 uint32_t ARMMCCodeEmitter::
497 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
498 SmallVectorImpl<MCFixup> &Fixups) const {
499 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
502 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
503 uint32_t ARMMCCodeEmitter::
504 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
505 SmallVectorImpl<MCFixup> &Fixups) const {
506 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
509 /// Return true if this branch has a non-always predication
510 static bool HasConditionalBranch(const MCInst &MI) {
511 int NumOp = MI.getNumOperands();
513 for (int i = 0; i < NumOp-1; ++i) {
514 const MCOperand &MCOp1 = MI.getOperand(i);
515 const MCOperand &MCOp2 = MI.getOperand(i + 1);
516 if (MCOp1.isImm() && MCOp2.isReg() &&
517 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
518 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
526 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
528 uint32_t ARMMCCodeEmitter::
529 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
530 SmallVectorImpl<MCFixup> &Fixups) const {
531 // FIXME: This really, really shouldn't use TargetMachine. We don't want
532 // coupling between MC and TM anywhere we can help it.
535 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
536 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
539 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
541 uint32_t ARMMCCodeEmitter::
542 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
543 SmallVectorImpl<MCFixup> &Fixups) const {
544 if (HasConditionalBranch(MI))
545 return ::getBranchTargetOpValue(MI, OpIdx,
546 ARM::fixup_arm_condbranch, Fixups);
547 return ::getBranchTargetOpValue(MI, OpIdx,
548 ARM::fixup_arm_uncondbranch, Fixups);
554 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
555 /// immediate branch target.
556 uint32_t ARMMCCodeEmitter::
557 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
560 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
561 bool I = (Val & 0x800000);
562 bool J1 = (Val & 0x400000);
563 bool J2 = (Val & 0x200000);
577 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
579 uint32_t ARMMCCodeEmitter::
580 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
581 SmallVectorImpl<MCFixup> &Fixups) const {
582 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
583 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
587 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
589 uint32_t ARMMCCodeEmitter::
590 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
591 SmallVectorImpl<MCFixup> &Fixups) const {
592 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
593 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
597 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
599 uint32_t ARMMCCodeEmitter::
600 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
603 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
607 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
609 uint32_t ARMMCCodeEmitter::
610 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
611 SmallVectorImpl<MCFixup> &) const {
615 const MCOperand &MO1 = MI.getOperand(OpIdx);
616 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
617 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
618 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
619 return (Rm << 3) | Rn;
622 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
623 uint32_t ARMMCCodeEmitter::
624 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
625 SmallVectorImpl<MCFixup> &Fixups) const {
627 // {12} = (U)nsigned (add == '1', sub == '0')
631 // If The first operand isn't a register, we have a label reference.
632 const MCOperand &MO = MI.getOperand(OpIdx);
634 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
636 isAdd = false ; // 'U' bit is set as part of the fixup.
638 assert(MO.isExpr() && "Unexpected machine operand type!");
639 const MCExpr *Expr = MO.getExpr();
643 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
645 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
646 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
648 ++MCNumCPRelocations;
650 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
652 uint32_t Binary = Imm12 & 0xfff;
653 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
656 Binary |= (Reg << 13);
660 /// getT2AddrModeImm8s4OpValue - Return encoding info for
661 /// 'reg +/- imm8<<2' operand.
662 uint32_t ARMMCCodeEmitter::
663 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
664 SmallVectorImpl<MCFixup> &Fixups) const {
666 // {8} = (U)nsigned (add == '1', sub == '0')
670 // If The first operand isn't a register, we have a label reference.
671 const MCOperand &MO = MI.getOperand(OpIdx);
673 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
675 isAdd = false ; // 'U' bit is set as part of the fixup.
677 assert(MO.isExpr() && "Unexpected machine operand type!");
678 const MCExpr *Expr = MO.getExpr();
679 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
680 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
682 ++MCNumCPRelocations;
684 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
686 uint32_t Binary = (Imm8 >> 2) & 0xff;
687 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
690 Binary |= (Reg << 9);
694 // FIXME: This routine assumes that a binary
695 // expression will always result in a PCRel expression
696 // In reality, its only true if one or more subexpressions
697 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
698 // but this is good enough for now.
699 static bool EvaluateAsPCRel(const MCExpr *Expr) {
700 switch (Expr->getKind()) {
701 default: assert(0 && "Unexpected expression type");
702 case MCExpr::SymbolRef: return false;
703 case MCExpr::Binary: return true;
708 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
709 SmallVectorImpl<MCFixup> &Fixups) const {
710 // {20-16} = imm{15-12}
711 // {11-0} = imm{11-0}
712 const MCOperand &MO = MI.getOperand(OpIdx);
714 // Hi / lo 16 bits already extracted during earlier passes.
715 return static_cast<unsigned>(MO.getImm());
717 // Handle :upper16: and :lower16: assembly prefixes.
718 const MCExpr *E = MO.getExpr();
719 if (E->getKind() == MCExpr::Target) {
720 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
721 E = ARM16Expr->getSubExpr();
724 switch (ARM16Expr->getKind()) {
725 default: assert(0 && "Unsupported ARMFixup");
726 case ARMMCExpr::VK_ARM_HI16:
727 if (!isTargetDarwin() && EvaluateAsPCRel(E))
728 Kind = MCFixupKind(isThumb2()
729 ? ARM::fixup_t2_movt_hi16_pcrel
730 : ARM::fixup_arm_movt_hi16_pcrel);
732 Kind = MCFixupKind(isThumb2()
733 ? ARM::fixup_t2_movt_hi16
734 : ARM::fixup_arm_movt_hi16);
736 case ARMMCExpr::VK_ARM_LO16:
737 if (!isTargetDarwin() && EvaluateAsPCRel(E))
738 Kind = MCFixupKind(isThumb2()
739 ? ARM::fixup_t2_movw_lo16_pcrel
740 : ARM::fixup_arm_movw_lo16_pcrel);
742 Kind = MCFixupKind(isThumb2()
743 ? ARM::fixup_t2_movw_lo16
744 : ARM::fixup_arm_movw_lo16);
747 Fixups.push_back(MCFixup::Create(0, E, Kind));
751 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
755 uint32_t ARMMCCodeEmitter::
756 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
757 SmallVectorImpl<MCFixup> &Fixups) const {
758 const MCOperand &MO = MI.getOperand(OpIdx);
759 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
760 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
761 unsigned Rn = getARMRegisterNumbering(MO.getReg());
762 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
763 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
764 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
765 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
766 unsigned SBits = getShiftOp(ShOp);
775 uint32_t Binary = Rm;
777 Binary |= SBits << 5;
778 Binary |= ShImm << 7;
784 uint32_t ARMMCCodeEmitter::
785 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
786 SmallVectorImpl<MCFixup> &Fixups) const {
788 // {13} 1 == imm12, 0 == Rm
791 const MCOperand &MO = MI.getOperand(OpIdx);
792 unsigned Rn = getARMRegisterNumbering(MO.getReg());
793 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
798 uint32_t ARMMCCodeEmitter::
799 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
800 SmallVectorImpl<MCFixup> &Fixups) const {
801 // {13} 1 == imm12, 0 == Rm
804 const MCOperand &MO = MI.getOperand(OpIdx);
805 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
806 unsigned Imm = MO1.getImm();
807 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
808 bool isReg = MO.getReg() != 0;
809 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
810 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
812 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
813 Binary <<= 7; // Shift amount is bits [11:7]
814 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
815 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
817 return Binary | (isAdd << 12) | (isReg << 13);
820 uint32_t ARMMCCodeEmitter::
821 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
822 SmallVectorImpl<MCFixup> &Fixups) const {
823 // {9} 1 == imm8, 0 == Rm
827 const MCOperand &MO = MI.getOperand(OpIdx);
828 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
829 unsigned Imm = MO1.getImm();
830 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
831 bool isImm = MO.getReg() == 0;
832 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
833 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
835 Imm8 = getARMRegisterNumbering(MO.getReg());
836 return Imm8 | (isAdd << 8) | (isImm << 9);
839 uint32_t ARMMCCodeEmitter::
840 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
841 SmallVectorImpl<MCFixup> &Fixups) const {
842 // {13} 1 == imm8, 0 == Rm
847 const MCOperand &MO = MI.getOperand(OpIdx);
848 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
849 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
850 unsigned Rn = getARMRegisterNumbering(MO.getReg());
851 unsigned Imm = MO2.getImm();
852 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
853 bool isImm = MO1.getReg() == 0;
854 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
855 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
857 Imm8 = getARMRegisterNumbering(MO1.getReg());
858 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
861 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
862 uint32_t ARMMCCodeEmitter::
863 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
864 SmallVectorImpl<MCFixup> &Fixups) const {
867 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
868 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
869 "Unexpected base register!");
871 // The immediate is already shifted for the implicit zeroes, so no change
873 return MO1.getImm() & 0xff;
876 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
877 uint32_t ARMMCCodeEmitter::
878 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
879 SmallVectorImpl<MCFixup> &Fixups) const {
883 const MCOperand &MO = MI.getOperand(OpIdx);
884 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
885 unsigned Rn = getARMRegisterNumbering(MO.getReg());
886 unsigned Imm5 = MO1.getImm();
887 return ((Imm5 & 0x1f) << 3) | Rn;
890 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
891 uint32_t ARMMCCodeEmitter::
892 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
893 SmallVectorImpl<MCFixup> &Fixups) const {
894 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
897 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
898 uint32_t ARMMCCodeEmitter::
899 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
900 SmallVectorImpl<MCFixup> &Fixups) const {
902 // {8} = (U)nsigned (add == '1', sub == '0')
906 // If The first operand isn't a register, we have a label reference.
907 const MCOperand &MO = MI.getOperand(OpIdx);
909 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
911 isAdd = false; // 'U' bit is handled as part of the fixup.
913 assert(MO.isExpr() && "Unexpected machine operand type!");
914 const MCExpr *Expr = MO.getExpr();
917 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
919 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
920 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
922 ++MCNumCPRelocations;
924 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
925 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
928 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
929 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
932 Binary |= (Reg << 9);
936 unsigned ARMMCCodeEmitter::
937 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
938 SmallVectorImpl<MCFixup> &Fixups) const {
939 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
940 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
941 // case the imm contains the amount to shift by.
944 // {4} = 1 if reg shift, 0 if imm shift
952 const MCOperand &MO = MI.getOperand(OpIdx);
953 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
954 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
955 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
958 unsigned Binary = getARMRegisterNumbering(MO.getReg());
960 // Encode the shift opcode.
962 unsigned Rs = MO1.getReg();
964 // Set shift operand (bit[7:4]).
969 // RRX - 0110 and bit[11:8] clear.
971 default: llvm_unreachable("Unknown shift opc!");
972 case ARM_AM::lsl: SBits = 0x1; break;
973 case ARM_AM::lsr: SBits = 0x3; break;
974 case ARM_AM::asr: SBits = 0x5; break;
975 case ARM_AM::ror: SBits = 0x7; break;
976 case ARM_AM::rrx: SBits = 0x6; break;
979 // Set shift operand (bit[6:4]).
985 default: llvm_unreachable("Unknown shift opc!");
986 case ARM_AM::lsl: SBits = 0x0; break;
987 case ARM_AM::lsr: SBits = 0x2; break;
988 case ARM_AM::asr: SBits = 0x4; break;
989 case ARM_AM::ror: SBits = 0x6; break;
993 Binary |= SBits << 4;
994 if (SOpc == ARM_AM::rrx)
997 // Encode the shift operation Rs or shift_imm (except rrx).
999 // Encode Rs bit[11:8].
1000 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1001 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1004 // Encode shift_imm bit[11:7].
1005 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
1008 unsigned ARMMCCodeEmitter::
1009 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1010 SmallVectorImpl<MCFixup> &Fixups) const {
1011 const MCOperand &MO1 = MI.getOperand(OpNum);
1012 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1013 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1015 // Encoded as [Rn, Rm, imm].
1016 // FIXME: Needs fixup support.
1017 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1019 Value |= getARMRegisterNumbering(MO2.getReg());
1021 Value |= MO3.getImm();
1026 unsigned ARMMCCodeEmitter::
1027 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1028 SmallVectorImpl<MCFixup> &Fixups) const {
1029 const MCOperand &MO1 = MI.getOperand(OpNum);
1030 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1032 // FIXME: Needs fixup support.
1033 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1035 // Even though the immediate is 8 bits long, we need 9 bits in order
1036 // to represent the (inverse of the) sign bit.
1038 int32_t tmp = (int32_t)MO2.getImm();
1042 Value |= 256; // Set the ADD bit
1047 unsigned ARMMCCodeEmitter::
1048 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1049 SmallVectorImpl<MCFixup> &Fixups) const {
1050 const MCOperand &MO1 = MI.getOperand(OpNum);
1052 // FIXME: Needs fixup support.
1054 int32_t tmp = (int32_t)MO1.getImm();
1058 Value |= 256; // Set the ADD bit
1063 unsigned ARMMCCodeEmitter::
1064 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1065 SmallVectorImpl<MCFixup> &Fixups) const {
1066 const MCOperand &MO1 = MI.getOperand(OpNum);
1068 // FIXME: Needs fixup support.
1070 int32_t tmp = (int32_t)MO1.getImm();
1074 Value |= 4096; // Set the ADD bit
1075 Value |= tmp & 4095;
1079 unsigned ARMMCCodeEmitter::
1080 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1081 SmallVectorImpl<MCFixup> &Fixups) const {
1082 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1083 // shifted. The second is the amount to shift by.
1090 const MCOperand &MO = MI.getOperand(OpIdx);
1091 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1092 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1095 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1097 // Encode the shift opcode.
1099 // Set shift operand (bit[6:4]).
1105 default: llvm_unreachable("Unknown shift opc!");
1106 case ARM_AM::lsl: SBits = 0x0; break;
1107 case ARM_AM::lsr: SBits = 0x2; break;
1108 case ARM_AM::asr: SBits = 0x4; break;
1109 case ARM_AM::ror: SBits = 0x6; break;
1112 Binary |= SBits << 4;
1113 if (SOpc == ARM_AM::rrx)
1116 // Encode shift_imm bit[11:7].
1117 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1120 unsigned ARMMCCodeEmitter::
1121 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1122 SmallVectorImpl<MCFixup> &Fixups) const {
1123 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1125 const MCOperand &MO = MI.getOperand(Op);
1126 uint32_t v = ~MO.getImm();
1127 uint32_t lsb = CountTrailingZeros_32(v);
1128 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1129 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1130 return lsb | (msb << 5);
1133 unsigned ARMMCCodeEmitter::
1134 getMsbOpValue(const MCInst &MI, unsigned Op,
1135 SmallVectorImpl<MCFixup> &Fixups) const {
1137 uint32_t lsb = MI.getOperand(Op-1).getImm();
1138 uint32_t width = MI.getOperand(Op).getImm();
1139 uint32_t msb = lsb+width-1;
1140 assert (width != 0 && msb < 32 && "Illegal bit width!");
1144 unsigned ARMMCCodeEmitter::
1145 getSsatBitPosValue(const MCInst &MI, unsigned Op,
1146 SmallVectorImpl<MCFixup> &Fixups) const {
1147 // For ssat instructions, the bit position should be encoded decremented by 1
1148 return MI.getOperand(Op).getImm()-1;
1151 unsigned ARMMCCodeEmitter::
1152 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1153 SmallVectorImpl<MCFixup> &Fixups) const {
1156 // {7-0} = Number of registers
1159 // {15-0} = Bitfield of GPRs.
1160 unsigned Reg = MI.getOperand(Op).getReg();
1161 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1162 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1164 unsigned Binary = 0;
1166 if (SPRRegs || DPRRegs) {
1168 unsigned RegNo = getARMRegisterNumbering(Reg);
1169 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1170 Binary |= (RegNo & 0x1f) << 8;
1174 Binary |= NumRegs * 2;
1176 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1177 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1178 Binary |= 1 << RegNo;
1185 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1186 /// with the alignment operand.
1187 unsigned ARMMCCodeEmitter::
1188 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1189 SmallVectorImpl<MCFixup> &Fixups) const {
1190 const MCOperand &Reg = MI.getOperand(Op);
1191 const MCOperand &Imm = MI.getOperand(Op + 1);
1193 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1196 switch (Imm.getImm()) {
1200 case 8: Align = 0x01; break;
1201 case 16: Align = 0x02; break;
1202 case 32: Align = 0x03; break;
1205 return RegNo | (Align << 4);
1208 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1209 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1210 unsigned ARMMCCodeEmitter::
1211 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1212 SmallVectorImpl<MCFixup> &Fixups) const {
1213 const MCOperand &Reg = MI.getOperand(Op);
1214 const MCOperand &Imm = MI.getOperand(Op + 1);
1216 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1219 switch (Imm.getImm()) {
1224 case 16: Align = 0x00; break;
1225 case 32: Align = 0x03; break;
1228 return RegNo | (Align << 4);
1232 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1233 /// alignment operand for use in VLD-dup instructions. This is the same as
1234 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1235 /// different for VLD4-dup.
1236 unsigned ARMMCCodeEmitter::
1237 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1238 SmallVectorImpl<MCFixup> &Fixups) const {
1239 const MCOperand &Reg = MI.getOperand(Op);
1240 const MCOperand &Imm = MI.getOperand(Op + 1);
1242 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1245 switch (Imm.getImm()) {
1249 case 8: Align = 0x01; break;
1250 case 16: Align = 0x03; break;
1253 return RegNo | (Align << 4);
1256 unsigned ARMMCCodeEmitter::
1257 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1258 SmallVectorImpl<MCFixup> &Fixups) const {
1259 const MCOperand &MO = MI.getOperand(Op);
1260 if (MO.getReg() == 0) return 0x0D;
1264 unsigned ARMMCCodeEmitter::
1265 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1266 SmallVectorImpl<MCFixup> &Fixups) const {
1267 return 8 - MI.getOperand(Op).getImm();
1270 unsigned ARMMCCodeEmitter::
1271 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1272 SmallVectorImpl<MCFixup> &Fixups) const {
1273 return 16 - MI.getOperand(Op).getImm();
1276 unsigned ARMMCCodeEmitter::
1277 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1278 SmallVectorImpl<MCFixup> &Fixups) const {
1279 return 32 - MI.getOperand(Op).getImm();
1282 unsigned ARMMCCodeEmitter::
1283 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1284 SmallVectorImpl<MCFixup> &Fixups) const {
1285 return 64 - MI.getOperand(Op).getImm();
1288 void ARMMCCodeEmitter::
1289 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1290 SmallVectorImpl<MCFixup> &Fixups) const {
1291 // Pseudo instructions don't get encoded.
1292 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1293 uint64_t TSFlags = Desc.TSFlags;
1294 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1298 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1299 Size = Desc.getSize();
1301 llvm_unreachable("Unexpected instruction size!");
1303 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1304 // Thumb 32-bit wide instructions need to emit the high order halfword
1306 if (isThumb() && Size == 4) {
1307 EmitConstant(Binary >> 16, 2, OS);
1308 EmitConstant(Binary & 0xffff, 2, OS);
1310 EmitConstant(Binary, Size, OS);
1311 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1314 #include "ARMGenMCCodeEmitter.inc"