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"
15 #include "MCTargetDesc/ARMAddressingModes.h"
16 #include "MCTargetDesc/ARMBaseInfo.h"
17 #include "MCTargetDesc/ARMFixupKinds.h"
18 #include "MCTargetDesc/ARMMCExpr.h"
19 #include "MCTargetDesc/ARMMCTargetDesc.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCRegisterInfo.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/ADT/APFloat.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Support/raw_ostream.h"
32 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
33 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
36 class ARMMCCodeEmitter : public MCCodeEmitter {
37 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
38 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
39 const MCInstrInfo &MCII;
40 const MCSubtargetInfo &STI;
43 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
45 : MCII(mcii), STI(sti) {
48 ~ARMMCCodeEmitter() {}
50 bool isThumb() const {
51 // FIXME: Can tablegen auto-generate this?
52 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
54 bool isThumb2() const {
55 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
57 bool isTargetDarwin() const {
58 Triple TT(STI.getTargetTriple());
59 Triple::OSType OS = TT.getOS();
60 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
63 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
65 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
67 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
72 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
75 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
76 /// the specified operand. This is used for operands with :lower16: and
77 /// :upper16: prefixes.
78 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
81 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
82 unsigned &Reg, unsigned &Imm,
83 SmallVectorImpl<MCFixup> &Fixups) const;
85 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
87 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
88 SmallVectorImpl<MCFixup> &Fixups) const;
90 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
91 /// BLX branch target.
92 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
95 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
96 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
99 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
100 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
103 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
107 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
109 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
112 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
113 /// immediate Thumb2 direct branch target.
114 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
117 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
119 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
121 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
124 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
125 /// ADR label target.
126 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
128 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
134 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
136 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
139 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
140 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups)const;
143 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
145 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
149 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
150 /// operand as needed by load/store instructions.
151 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups) const;
154 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
155 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const {
157 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
159 default: assert(0 && "Unknown addressing sub-mode!");
160 case ARM_AM::da: return 0;
161 case ARM_AM::ia: return 1;
162 case ARM_AM::db: return 2;
163 case ARM_AM::ib: return 3;
166 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
168 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
170 default: llvm_unreachable("Unknown shift opc!");
171 case ARM_AM::no_shift:
172 case ARM_AM::lsl: return 0;
173 case ARM_AM::lsr: return 1;
174 case ARM_AM::asr: return 2;
176 case ARM_AM::rrx: return 3;
181 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
182 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
185 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
186 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
189 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
190 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
193 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
194 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
197 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
198 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
201 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
203 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
206 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
207 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
210 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
211 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
214 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
215 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
216 SmallVectorImpl<MCFixup> &Fixups) const;
218 /// getCCOutOpValue - Return encoding of the 's' bit.
219 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const {
221 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
223 return MI.getOperand(Op).getReg() == ARM::CPSR;
226 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
227 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
229 unsigned SoImm = MI.getOperand(Op).getImm();
230 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
231 assert(SoImmVal != -1 && "Not a valid so_imm value!");
233 // Encode rotate_imm.
234 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
235 << ARMII::SoRotImmShift;
238 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
242 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
243 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const {
245 unsigned SoImm = MI.getOperand(Op).getImm();
246 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
247 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
251 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
252 SmallVectorImpl<MCFixup> &Fixups) const;
253 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
254 SmallVectorImpl<MCFixup> &Fixups) const;
255 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
256 SmallVectorImpl<MCFixup> &Fixups) const;
257 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
258 SmallVectorImpl<MCFixup> &Fixups) const;
260 /// getSORegOpValue - Return an encoded so_reg shifted register value.
261 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const;
263 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const;
265 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
268 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const {
270 return 64 - MI.getOperand(Op).getImm();
273 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
276 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
279 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
281 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
283 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
285 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
287 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
290 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const;
292 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
293 SmallVectorImpl<MCFixup> &Fixups) const;
294 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
296 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
299 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
303 unsigned EncodedValue) const;
304 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
305 unsigned EncodedValue) const;
306 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
307 unsigned EncodedValue) const;
309 unsigned VFPThumb2PostEncoder(const MCInst &MI,
310 unsigned EncodedValue) const;
312 void EmitByte(unsigned char C, raw_ostream &OS) const {
316 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
317 // Output the constant in little endian byte order.
318 for (unsigned i = 0; i != Size; ++i) {
319 EmitByte(Val & 255, OS);
324 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
325 SmallVectorImpl<MCFixup> &Fixups) const;
328 } // end anonymous namespace
330 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
331 const MCSubtargetInfo &STI,
333 return new ARMMCCodeEmitter(MCII, STI, Ctx);
336 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
337 /// instructions, and rewrite them to their Thumb2 form if we are currently in
339 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
340 unsigned EncodedValue) const {
342 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
343 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
345 unsigned Bit24 = EncodedValue & 0x01000000;
346 unsigned Bit28 = Bit24 << 4;
347 EncodedValue &= 0xEFFFFFFF;
348 EncodedValue |= Bit28;
349 EncodedValue |= 0x0F000000;
355 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
356 /// instructions, and rewrite them to their Thumb2 form if we are currently in
358 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
359 unsigned EncodedValue) const {
361 EncodedValue &= 0xF0FFFFFF;
362 EncodedValue |= 0x09000000;
368 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
369 /// instructions, and rewrite them to their Thumb2 form if we are currently in
371 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
372 unsigned EncodedValue) const {
374 EncodedValue &= 0x00FFFFFF;
375 EncodedValue |= 0xEE000000;
381 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
382 /// them to their Thumb2 form if we are currently in Thumb2 mode.
383 unsigned ARMMCCodeEmitter::
384 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
386 EncodedValue &= 0x0FFFFFFF;
387 EncodedValue |= 0xE0000000;
392 /// getMachineOpValue - Return binary encoding of operand. If the machine
393 /// operand requires relocation, record the relocation and return zero.
394 unsigned ARMMCCodeEmitter::
395 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
396 SmallVectorImpl<MCFixup> &Fixups) const {
398 unsigned Reg = MO.getReg();
399 unsigned RegNo = getARMRegisterNumbering(Reg);
401 // Q registers are encoded as 2x their register number.
405 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
406 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
407 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
408 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
411 } else if (MO.isImm()) {
412 return static_cast<unsigned>(MO.getImm());
413 } else if (MO.isFPImm()) {
414 return static_cast<unsigned>(APFloat(MO.getFPImm())
415 .bitcastToAPInt().getHiBits(32).getLimitedValue());
418 llvm_unreachable("Unable to encode MCOperand!");
422 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
423 bool ARMMCCodeEmitter::
424 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
425 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
426 const MCOperand &MO = MI.getOperand(OpIdx);
427 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
429 Reg = getARMRegisterNumbering(MO.getReg());
431 int32_t SImm = MO1.getImm();
434 // Special value for #-0
435 if (SImm == INT32_MIN) {
440 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
450 /// getBranchTargetOpValue - Helper function to get the branch target operand,
451 /// which is either an immediate or requires a fixup.
452 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
454 SmallVectorImpl<MCFixup> &Fixups) {
455 const MCOperand &MO = MI.getOperand(OpIdx);
457 // If the destination is an immediate, we have nothing to do.
458 if (MO.isImm()) return MO.getImm();
459 assert(MO.isExpr() && "Unexpected branch target type!");
460 const MCExpr *Expr = MO.getExpr();
461 MCFixupKind Kind = MCFixupKind(FixupKind);
462 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
464 // All of the information is in the fixup.
468 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
469 // determined by negating them and XOR'ing them with bit 23.
470 static int32_t encodeThumbBLOffset(int32_t offset) {
472 uint32_t S = (offset & 0x800000) >> 23;
473 uint32_t J1 = (offset & 0x400000) >> 22;
474 uint32_t J2 = (offset & 0x200000) >> 21;
487 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
488 uint32_t ARMMCCodeEmitter::
489 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
490 SmallVectorImpl<MCFixup> &Fixups) const {
491 const MCOperand MO = MI.getOperand(OpIdx);
493 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
495 return encodeThumbBLOffset(MO.getImm());
498 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
499 /// BLX branch target.
500 uint32_t ARMMCCodeEmitter::
501 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
503 const MCOperand MO = MI.getOperand(OpIdx);
505 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
507 return encodeThumbBLOffset(MO.getImm());
510 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
511 uint32_t ARMMCCodeEmitter::
512 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
513 SmallVectorImpl<MCFixup> &Fixups) const {
514 const MCOperand MO = MI.getOperand(OpIdx);
516 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
518 return (MO.getImm() >> 1);
521 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
522 uint32_t ARMMCCodeEmitter::
523 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
524 SmallVectorImpl<MCFixup> &Fixups) const {
525 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
528 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
529 uint32_t ARMMCCodeEmitter::
530 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
531 SmallVectorImpl<MCFixup> &Fixups) const {
532 const MCOperand MO = MI.getOperand(OpIdx);
534 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
535 return (MO.getImm() >> 1);
538 /// Return true if this branch has a non-always predication
539 static bool HasConditionalBranch(const MCInst &MI) {
540 int NumOp = MI.getNumOperands();
542 for (int i = 0; i < NumOp-1; ++i) {
543 const MCOperand &MCOp1 = MI.getOperand(i);
544 const MCOperand &MCOp2 = MI.getOperand(i + 1);
545 if (MCOp1.isImm() && MCOp2.isReg() &&
546 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
547 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
555 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
557 uint32_t ARMMCCodeEmitter::
558 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
559 SmallVectorImpl<MCFixup> &Fixups) const {
560 // FIXME: This really, really shouldn't use TargetMachine. We don't want
561 // coupling between MC and TM anywhere we can help it.
564 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
565 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
568 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
570 uint32_t ARMMCCodeEmitter::
571 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
572 SmallVectorImpl<MCFixup> &Fixups) const {
573 const MCOperand MO = MI.getOperand(OpIdx);
575 if (HasConditionalBranch(MI))
576 return ::getBranchTargetOpValue(MI, OpIdx,
577 ARM::fixup_arm_condbranch, Fixups);
578 return ::getBranchTargetOpValue(MI, OpIdx,
579 ARM::fixup_arm_uncondbranch, Fixups);
582 return MO.getImm() >> 2;
585 uint32_t ARMMCCodeEmitter::
586 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
587 SmallVectorImpl<MCFixup> &Fixups) const {
588 const MCOperand MO = MI.getOperand(OpIdx);
590 if (HasConditionalBranch(MI))
591 return ::getBranchTargetOpValue(MI, OpIdx,
592 ARM::fixup_arm_condbranch, Fixups);
593 return ::getBranchTargetOpValue(MI, OpIdx,
594 ARM::fixup_arm_uncondbranch, Fixups);
597 return MO.getImm() >> 1;
600 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
601 /// immediate branch target.
602 uint32_t ARMMCCodeEmitter::
603 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
606 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
607 bool I = (Val & 0x800000);
608 bool J1 = (Val & 0x400000);
609 bool J2 = (Val & 0x200000);
623 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
625 uint32_t ARMMCCodeEmitter::
626 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &Fixups) const {
628 const MCOperand MO = MI.getOperand(OpIdx);
630 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
632 int32_t offset = MO.getImm();
633 uint32_t Val = 0x2000;
642 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
644 uint32_t ARMMCCodeEmitter::
645 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
646 SmallVectorImpl<MCFixup> &Fixups) const {
647 const MCOperand MO = MI.getOperand(OpIdx);
649 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
654 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
656 uint32_t ARMMCCodeEmitter::
657 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
658 SmallVectorImpl<MCFixup> &Fixups) const {
659 const MCOperand MO = MI.getOperand(OpIdx);
661 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
666 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
668 uint32_t ARMMCCodeEmitter::
669 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
670 SmallVectorImpl<MCFixup> &) const {
674 const MCOperand &MO1 = MI.getOperand(OpIdx);
675 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
676 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
677 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
678 return (Rm << 3) | Rn;
681 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
682 uint32_t ARMMCCodeEmitter::
683 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
684 SmallVectorImpl<MCFixup> &Fixups) const {
686 // {12} = (U)nsigned (add == '1', sub == '0')
690 // If The first operand isn't a register, we have a label reference.
691 const MCOperand &MO = MI.getOperand(OpIdx);
693 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
695 isAdd = false ; // 'U' bit is set as part of the fixup.
697 assert(MO.isExpr() && "Unexpected machine operand type!");
698 const MCExpr *Expr = MO.getExpr();
702 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
704 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
705 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
707 ++MCNumCPRelocations;
709 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
711 uint32_t Binary = Imm12 & 0xfff;
712 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
715 Binary |= (Reg << 13);
719 /// getT2AddrModeImm8s4OpValue - Return encoding info for
720 /// 'reg +/- imm8<<2' operand.
721 uint32_t ARMMCCodeEmitter::
722 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
723 SmallVectorImpl<MCFixup> &Fixups) const {
725 // {8} = (U)nsigned (add == '1', sub == '0')
729 // If The first operand isn't a register, we have a label reference.
730 const MCOperand &MO = MI.getOperand(OpIdx);
732 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
734 isAdd = false ; // 'U' bit is set as part of the fixup.
736 assert(MO.isExpr() && "Unexpected machine operand type!");
737 const MCExpr *Expr = MO.getExpr();
738 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
739 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
741 ++MCNumCPRelocations;
743 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
745 uint32_t Binary = (Imm8 >> 2) & 0xff;
746 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
749 Binary |= (Reg << 9);
753 // FIXME: This routine assumes that a binary
754 // expression will always result in a PCRel expression
755 // In reality, its only true if one or more subexpressions
756 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
757 // but this is good enough for now.
758 static bool EvaluateAsPCRel(const MCExpr *Expr) {
759 switch (Expr->getKind()) {
760 default: assert(0 && "Unexpected expression type");
761 case MCExpr::SymbolRef: return false;
762 case MCExpr::Binary: return true;
767 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
768 SmallVectorImpl<MCFixup> &Fixups) const {
769 // {20-16} = imm{15-12}
770 // {11-0} = imm{11-0}
771 const MCOperand &MO = MI.getOperand(OpIdx);
773 // Hi / lo 16 bits already extracted during earlier passes.
774 return static_cast<unsigned>(MO.getImm());
776 // Handle :upper16: and :lower16: assembly prefixes.
777 const MCExpr *E = MO.getExpr();
778 if (E->getKind() == MCExpr::Target) {
779 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
780 E = ARM16Expr->getSubExpr();
783 switch (ARM16Expr->getKind()) {
784 default: assert(0 && "Unsupported ARMFixup");
785 case ARMMCExpr::VK_ARM_HI16:
786 if (!isTargetDarwin() && EvaluateAsPCRel(E))
787 Kind = MCFixupKind(isThumb2()
788 ? ARM::fixup_t2_movt_hi16_pcrel
789 : ARM::fixup_arm_movt_hi16_pcrel);
791 Kind = MCFixupKind(isThumb2()
792 ? ARM::fixup_t2_movt_hi16
793 : ARM::fixup_arm_movt_hi16);
795 case ARMMCExpr::VK_ARM_LO16:
796 if (!isTargetDarwin() && EvaluateAsPCRel(E))
797 Kind = MCFixupKind(isThumb2()
798 ? ARM::fixup_t2_movw_lo16_pcrel
799 : ARM::fixup_arm_movw_lo16_pcrel);
801 Kind = MCFixupKind(isThumb2()
802 ? ARM::fixup_t2_movw_lo16
803 : ARM::fixup_arm_movw_lo16);
806 Fixups.push_back(MCFixup::Create(0, E, Kind));
810 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
814 uint32_t ARMMCCodeEmitter::
815 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
816 SmallVectorImpl<MCFixup> &Fixups) const {
817 const MCOperand &MO = MI.getOperand(OpIdx);
818 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
819 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
820 unsigned Rn = getARMRegisterNumbering(MO.getReg());
821 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
822 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
823 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
824 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
825 unsigned SBits = getShiftOp(ShOp);
834 uint32_t Binary = Rm;
836 Binary |= SBits << 5;
837 Binary |= ShImm << 7;
843 uint32_t ARMMCCodeEmitter::
844 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
845 SmallVectorImpl<MCFixup> &Fixups) const {
847 // {13} 1 == imm12, 0 == Rm
850 const MCOperand &MO = MI.getOperand(OpIdx);
851 unsigned Rn = getARMRegisterNumbering(MO.getReg());
852 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
857 uint32_t ARMMCCodeEmitter::
858 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
859 SmallVectorImpl<MCFixup> &Fixups) const {
860 // {13} 1 == imm12, 0 == Rm
863 const MCOperand &MO = MI.getOperand(OpIdx);
864 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
865 unsigned Imm = MO1.getImm();
866 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
867 bool isReg = MO.getReg() != 0;
868 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
869 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
871 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
872 Binary <<= 7; // Shift amount is bits [11:7]
873 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
874 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
876 return Binary | (isAdd << 12) | (isReg << 13);
879 uint32_t ARMMCCodeEmitter::
880 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
881 SmallVectorImpl<MCFixup> &Fixups) const {
884 const MCOperand &MO = MI.getOperand(OpIdx);
885 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
886 bool isAdd = MO1.getImm() != 0;
887 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
890 uint32_t ARMMCCodeEmitter::
891 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
892 SmallVectorImpl<MCFixup> &Fixups) const {
893 // {9} 1 == imm8, 0 == Rm
897 const MCOperand &MO = MI.getOperand(OpIdx);
898 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
899 unsigned Imm = MO1.getImm();
900 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
901 bool isImm = MO.getReg() == 0;
902 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
903 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
905 Imm8 = getARMRegisterNumbering(MO.getReg());
906 return Imm8 | (isAdd << 8) | (isImm << 9);
909 uint32_t ARMMCCodeEmitter::
910 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
911 SmallVectorImpl<MCFixup> &Fixups) const {
912 // {13} 1 == imm8, 0 == Rm
917 const MCOperand &MO = MI.getOperand(OpIdx);
918 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
919 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
920 unsigned Rn = getARMRegisterNumbering(MO.getReg());
921 unsigned Imm = MO2.getImm();
922 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
923 bool isImm = MO1.getReg() == 0;
924 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
925 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
927 Imm8 = getARMRegisterNumbering(MO1.getReg());
928 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
931 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
932 uint32_t ARMMCCodeEmitter::
933 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
934 SmallVectorImpl<MCFixup> &Fixups) const {
937 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
938 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
939 "Unexpected base register!");
941 // The immediate is already shifted for the implicit zeroes, so no change
943 return MO1.getImm() & 0xff;
946 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
947 uint32_t ARMMCCodeEmitter::
948 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
949 SmallVectorImpl<MCFixup> &Fixups) const {
953 const MCOperand &MO = MI.getOperand(OpIdx);
954 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
955 unsigned Rn = getARMRegisterNumbering(MO.getReg());
956 unsigned Imm5 = MO1.getImm();
957 return ((Imm5 & 0x1f) << 3) | Rn;
960 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
961 uint32_t ARMMCCodeEmitter::
962 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
963 SmallVectorImpl<MCFixup> &Fixups) const {
964 const MCOperand MO = MI.getOperand(OpIdx);
966 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
967 return (MO.getImm() >> 2);
970 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
971 uint32_t ARMMCCodeEmitter::
972 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
973 SmallVectorImpl<MCFixup> &Fixups) const {
975 // {8} = (U)nsigned (add == '1', sub == '0')
979 // If The first operand isn't a register, we have a label reference.
980 const MCOperand &MO = MI.getOperand(OpIdx);
982 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
984 isAdd = false; // 'U' bit is handled as part of the fixup.
986 assert(MO.isExpr() && "Unexpected machine operand type!");
987 const MCExpr *Expr = MO.getExpr();
990 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
992 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
993 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
995 ++MCNumCPRelocations;
997 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
998 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1001 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1002 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1005 Binary |= (Reg << 9);
1009 unsigned ARMMCCodeEmitter::
1010 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1011 SmallVectorImpl<MCFixup> &Fixups) const {
1012 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1013 // shifted. The second is Rs, the amount to shift by, and the third specifies
1014 // the type of the shift.
1022 const MCOperand &MO = MI.getOperand(OpIdx);
1023 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1024 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1025 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1028 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1030 // Encode the shift opcode.
1032 unsigned Rs = MO1.getReg();
1034 // Set shift operand (bit[7:4]).
1040 default: llvm_unreachable("Unknown shift opc!");
1041 case ARM_AM::lsl: SBits = 0x1; break;
1042 case ARM_AM::lsr: SBits = 0x3; break;
1043 case ARM_AM::asr: SBits = 0x5; break;
1044 case ARM_AM::ror: SBits = 0x7; break;
1048 Binary |= SBits << 4;
1050 // Encode the shift operation Rs.
1051 // Encode Rs bit[11:8].
1052 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1053 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1056 unsigned ARMMCCodeEmitter::
1057 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1058 SmallVectorImpl<MCFixup> &Fixups) const {
1059 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1060 // shifted. The second is the amount to shift by.
1067 const MCOperand &MO = MI.getOperand(OpIdx);
1068 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1069 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1072 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1074 // Encode the shift opcode.
1077 // Set shift operand (bit[6:4]).
1082 // RRX - 110 and bit[11:8] clear.
1084 default: llvm_unreachable("Unknown shift opc!");
1085 case ARM_AM::lsl: SBits = 0x0; break;
1086 case ARM_AM::lsr: SBits = 0x2; break;
1087 case ARM_AM::asr: SBits = 0x4; break;
1088 case ARM_AM::ror: SBits = 0x6; break;
1094 // Encode shift_imm bit[11:7].
1095 Binary |= SBits << 4;
1096 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1097 assert(Offset && "Offset must be in range 1-32!");
1098 if (Offset == 32) Offset = 0;
1099 return Binary | (Offset << 7);
1103 unsigned ARMMCCodeEmitter::
1104 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1105 SmallVectorImpl<MCFixup> &Fixups) const {
1106 const MCOperand &MO1 = MI.getOperand(OpNum);
1107 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1108 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1110 // Encoded as [Rn, Rm, imm].
1111 // FIXME: Needs fixup support.
1112 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1114 Value |= getARMRegisterNumbering(MO2.getReg());
1116 Value |= MO3.getImm();
1121 unsigned ARMMCCodeEmitter::
1122 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1123 SmallVectorImpl<MCFixup> &Fixups) const {
1124 const MCOperand &MO1 = MI.getOperand(OpNum);
1125 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1127 // FIXME: Needs fixup support.
1128 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1130 // Even though the immediate is 8 bits long, we need 9 bits in order
1131 // to represent the (inverse of the) sign bit.
1133 int32_t tmp = (int32_t)MO2.getImm();
1137 Value |= 256; // Set the ADD bit
1142 unsigned ARMMCCodeEmitter::
1143 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1144 SmallVectorImpl<MCFixup> &Fixups) const {
1145 const MCOperand &MO1 = MI.getOperand(OpNum);
1147 // FIXME: Needs fixup support.
1149 int32_t tmp = (int32_t)MO1.getImm();
1153 Value |= 256; // Set the ADD bit
1158 unsigned ARMMCCodeEmitter::
1159 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1160 SmallVectorImpl<MCFixup> &Fixups) const {
1161 const MCOperand &MO1 = MI.getOperand(OpNum);
1163 // FIXME: Needs fixup support.
1165 int32_t tmp = (int32_t)MO1.getImm();
1169 Value |= 4096; // Set the ADD bit
1170 Value |= tmp & 4095;
1174 unsigned ARMMCCodeEmitter::
1175 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1176 SmallVectorImpl<MCFixup> &Fixups) const {
1177 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1178 // shifted. The second is the amount to shift by.
1185 const MCOperand &MO = MI.getOperand(OpIdx);
1186 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1187 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1190 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1192 // Encode the shift opcode.
1194 // Set shift operand (bit[6:4]).
1200 default: llvm_unreachable("Unknown shift opc!");
1201 case ARM_AM::lsl: SBits = 0x0; break;
1202 case ARM_AM::lsr: SBits = 0x2; break;
1203 case ARM_AM::asr: SBits = 0x4; break;
1204 case ARM_AM::ror: SBits = 0x6; break;
1207 Binary |= SBits << 4;
1208 if (SOpc == ARM_AM::rrx)
1211 // Encode shift_imm bit[11:7].
1212 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1215 unsigned ARMMCCodeEmitter::
1216 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1217 SmallVectorImpl<MCFixup> &Fixups) const {
1218 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1220 const MCOperand &MO = MI.getOperand(Op);
1221 uint32_t v = ~MO.getImm();
1222 uint32_t lsb = CountTrailingZeros_32(v);
1223 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1224 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1225 return lsb | (msb << 5);
1228 unsigned ARMMCCodeEmitter::
1229 getMsbOpValue(const MCInst &MI, unsigned Op,
1230 SmallVectorImpl<MCFixup> &Fixups) const {
1232 uint32_t lsb = MI.getOperand(Op-1).getImm();
1233 uint32_t width = MI.getOperand(Op).getImm();
1234 uint32_t msb = lsb+width-1;
1235 assert (width != 0 && msb < 32 && "Illegal bit width!");
1239 unsigned ARMMCCodeEmitter::
1240 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1241 SmallVectorImpl<MCFixup> &Fixups) const {
1244 // {7-0} = Number of registers
1247 // {15-0} = Bitfield of GPRs.
1248 unsigned Reg = MI.getOperand(Op).getReg();
1249 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1250 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1252 unsigned Binary = 0;
1254 if (SPRRegs || DPRRegs) {
1256 unsigned RegNo = getARMRegisterNumbering(Reg);
1257 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1258 Binary |= (RegNo & 0x1f) << 8;
1262 Binary |= NumRegs * 2;
1264 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1265 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1266 Binary |= 1 << RegNo;
1273 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1274 /// with the alignment operand.
1275 unsigned ARMMCCodeEmitter::
1276 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1277 SmallVectorImpl<MCFixup> &Fixups) const {
1278 const MCOperand &Reg = MI.getOperand(Op);
1279 const MCOperand &Imm = MI.getOperand(Op + 1);
1281 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1284 switch (Imm.getImm()) {
1288 case 8: Align = 0x01; break;
1289 case 16: Align = 0x02; break;
1290 case 32: Align = 0x03; break;
1293 return RegNo | (Align << 4);
1296 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1297 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1298 unsigned ARMMCCodeEmitter::
1299 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1300 SmallVectorImpl<MCFixup> &Fixups) const {
1301 const MCOperand &Reg = MI.getOperand(Op);
1302 const MCOperand &Imm = MI.getOperand(Op + 1);
1304 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1307 switch (Imm.getImm()) {
1312 case 16: Align = 0x00; break;
1313 case 32: Align = 0x03; break;
1316 return RegNo | (Align << 4);
1320 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1321 /// alignment operand for use in VLD-dup instructions. This is the same as
1322 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1323 /// different for VLD4-dup.
1324 unsigned ARMMCCodeEmitter::
1325 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1326 SmallVectorImpl<MCFixup> &Fixups) const {
1327 const MCOperand &Reg = MI.getOperand(Op);
1328 const MCOperand &Imm = MI.getOperand(Op + 1);
1330 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1333 switch (Imm.getImm()) {
1337 case 8: Align = 0x01; break;
1338 case 16: Align = 0x03; break;
1341 return RegNo | (Align << 4);
1344 unsigned ARMMCCodeEmitter::
1345 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1346 SmallVectorImpl<MCFixup> &Fixups) const {
1347 const MCOperand &MO = MI.getOperand(Op);
1348 if (MO.getReg() == 0) return 0x0D;
1352 unsigned ARMMCCodeEmitter::
1353 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1354 SmallVectorImpl<MCFixup> &Fixups) const {
1355 return 8 - MI.getOperand(Op).getImm();
1358 unsigned ARMMCCodeEmitter::
1359 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1360 SmallVectorImpl<MCFixup> &Fixups) const {
1361 return 16 - MI.getOperand(Op).getImm();
1364 unsigned ARMMCCodeEmitter::
1365 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1366 SmallVectorImpl<MCFixup> &Fixups) const {
1367 return 32 - MI.getOperand(Op).getImm();
1370 unsigned ARMMCCodeEmitter::
1371 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1372 SmallVectorImpl<MCFixup> &Fixups) const {
1373 return 64 - MI.getOperand(Op).getImm();
1376 void ARMMCCodeEmitter::
1377 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1378 SmallVectorImpl<MCFixup> &Fixups) const {
1379 // Pseudo instructions don't get encoded.
1380 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1381 uint64_t TSFlags = Desc.TSFlags;
1382 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1386 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1387 Size = Desc.getSize();
1389 llvm_unreachable("Unexpected instruction size!");
1391 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1392 // Thumb 32-bit wide instructions need to emit the high order halfword
1394 if (isThumb() && Size == 4) {
1395 EmitConstant(Binary >> 16, 2, OS);
1396 EmitConstant(Binary & 0xffff, 2, OS);
1398 EmitConstant(Binary, Size, OS);
1399 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1402 #include "ARMGenMCCodeEmitter.inc"