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;
147 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
149 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
153 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
154 /// operand as needed by load/store instructions.
155 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const;
158 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
159 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const {
161 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
163 default: assert(0 && "Unknown addressing sub-mode!");
164 case ARM_AM::da: return 0;
165 case ARM_AM::ia: return 1;
166 case ARM_AM::db: return 2;
167 case ARM_AM::ib: return 3;
170 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
172 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
174 default: llvm_unreachable("Unknown shift opc!");
175 case ARM_AM::no_shift:
176 case ARM_AM::lsl: return 0;
177 case ARM_AM::lsr: return 1;
178 case ARM_AM::asr: return 2;
180 case ARM_AM::rrx: return 3;
185 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
186 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
189 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
190 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
193 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
194 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
197 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
198 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
201 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
202 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
203 SmallVectorImpl<MCFixup> &Fixups) const;
205 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
207 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
210 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
211 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
214 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
215 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
216 SmallVectorImpl<MCFixup> &Fixups) const;
218 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
219 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
220 SmallVectorImpl<MCFixup> &Fixups) const;
222 /// getCCOutOpValue - Return encoding of the 's' bit.
223 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
225 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
227 return MI.getOperand(Op).getReg() == ARM::CPSR;
230 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
231 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
232 SmallVectorImpl<MCFixup> &Fixups) const {
233 unsigned SoImm = MI.getOperand(Op).getImm();
234 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
235 assert(SoImmVal != -1 && "Not a valid so_imm value!");
237 // Encode rotate_imm.
238 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
239 << ARMII::SoRotImmShift;
242 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
246 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
247 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const {
249 unsigned SoImm = MI.getOperand(Op).getImm();
250 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
251 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
255 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
256 SmallVectorImpl<MCFixup> &Fixups) const;
257 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
258 SmallVectorImpl<MCFixup> &Fixups) const;
259 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
260 SmallVectorImpl<MCFixup> &Fixups) const;
261 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
262 SmallVectorImpl<MCFixup> &Fixups) const;
264 /// getSORegOpValue - Return an encoded so_reg shifted register value.
265 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
267 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
269 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
272 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const {
274 return 64 - MI.getOperand(Op).getImm();
277 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
280 unsigned getMsbOpValue(const MCInst &MI, unsigned Op,
281 SmallVectorImpl<MCFixup> &Fixups) const;
283 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
285 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
287 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
289 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
294 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
296 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
298 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
303 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
304 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
307 unsigned EncodedValue) const;
308 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
309 unsigned EncodedValue) const;
310 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
311 unsigned EncodedValue) const;
313 unsigned VFPThumb2PostEncoder(const MCInst &MI,
314 unsigned EncodedValue) const;
316 void EmitByte(unsigned char C, raw_ostream &OS) const {
320 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
321 // Output the constant in little endian byte order.
322 for (unsigned i = 0; i != Size; ++i) {
323 EmitByte(Val & 255, OS);
328 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
329 SmallVectorImpl<MCFixup> &Fixups) const;
332 } // end anonymous namespace
334 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
335 const MCSubtargetInfo &STI,
337 return new ARMMCCodeEmitter(MCII, STI, Ctx);
340 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
341 /// instructions, and rewrite them to their Thumb2 form if we are currently in
343 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
344 unsigned EncodedValue) const {
346 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
347 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
349 unsigned Bit24 = EncodedValue & 0x01000000;
350 unsigned Bit28 = Bit24 << 4;
351 EncodedValue &= 0xEFFFFFFF;
352 EncodedValue |= Bit28;
353 EncodedValue |= 0x0F000000;
359 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
360 /// instructions, and rewrite them to their Thumb2 form if we are currently in
362 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
363 unsigned EncodedValue) const {
365 EncodedValue &= 0xF0FFFFFF;
366 EncodedValue |= 0x09000000;
372 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
373 /// instructions, and rewrite them to their Thumb2 form if we are currently in
375 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
376 unsigned EncodedValue) const {
378 EncodedValue &= 0x00FFFFFF;
379 EncodedValue |= 0xEE000000;
385 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
386 /// them to their Thumb2 form if we are currently in Thumb2 mode.
387 unsigned ARMMCCodeEmitter::
388 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
390 EncodedValue &= 0x0FFFFFFF;
391 EncodedValue |= 0xE0000000;
396 /// getMachineOpValue - Return binary encoding of operand. If the machine
397 /// operand requires relocation, record the relocation and return zero.
398 unsigned ARMMCCodeEmitter::
399 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
400 SmallVectorImpl<MCFixup> &Fixups) const {
402 unsigned Reg = MO.getReg();
403 unsigned RegNo = getARMRegisterNumbering(Reg);
405 // Q registers are encoded as 2x their register number.
409 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
410 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
411 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
412 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
415 } else if (MO.isImm()) {
416 return static_cast<unsigned>(MO.getImm());
417 } else if (MO.isFPImm()) {
418 return static_cast<unsigned>(APFloat(MO.getFPImm())
419 .bitcastToAPInt().getHiBits(32).getLimitedValue());
422 llvm_unreachable("Unable to encode MCOperand!");
426 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
427 bool ARMMCCodeEmitter::
428 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
429 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
430 const MCOperand &MO = MI.getOperand(OpIdx);
431 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
433 Reg = getARMRegisterNumbering(MO.getReg());
435 int32_t SImm = MO1.getImm();
438 // Special value for #-0
439 if (SImm == INT32_MIN) {
444 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
454 /// getBranchTargetOpValue - Helper function to get the branch target operand,
455 /// which is either an immediate or requires a fixup.
456 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
458 SmallVectorImpl<MCFixup> &Fixups) {
459 const MCOperand &MO = MI.getOperand(OpIdx);
461 // If the destination is an immediate, we have nothing to do.
462 if (MO.isImm()) return MO.getImm();
463 assert(MO.isExpr() && "Unexpected branch target type!");
464 const MCExpr *Expr = MO.getExpr();
465 MCFixupKind Kind = MCFixupKind(FixupKind);
466 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
468 // All of the information is in the fixup.
472 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
473 // determined by negating them and XOR'ing them with bit 23.
474 static int32_t encodeThumbBLOffset(int32_t offset) {
476 uint32_t S = (offset & 0x800000) >> 23;
477 uint32_t J1 = (offset & 0x400000) >> 22;
478 uint32_t J2 = (offset & 0x200000) >> 21;
491 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
492 uint32_t ARMMCCodeEmitter::
493 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
495 const MCOperand MO = MI.getOperand(OpIdx);
497 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
499 return encodeThumbBLOffset(MO.getImm());
502 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
503 /// BLX branch target.
504 uint32_t ARMMCCodeEmitter::
505 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
506 SmallVectorImpl<MCFixup> &Fixups) const {
507 const MCOperand MO = MI.getOperand(OpIdx);
509 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
511 return encodeThumbBLOffset(MO.getImm());
514 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
515 uint32_t ARMMCCodeEmitter::
516 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
517 SmallVectorImpl<MCFixup> &Fixups) const {
518 const MCOperand MO = MI.getOperand(OpIdx);
520 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
522 return (MO.getImm() >> 1);
525 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
526 uint32_t ARMMCCodeEmitter::
527 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
528 SmallVectorImpl<MCFixup> &Fixups) const {
529 const MCOperand MO = MI.getOperand(OpIdx);
531 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
533 return (MO.getImm() >> 1);
536 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
537 uint32_t ARMMCCodeEmitter::
538 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
539 SmallVectorImpl<MCFixup> &Fixups) const {
540 const MCOperand MO = MI.getOperand(OpIdx);
542 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
543 return (MO.getImm() >> 1);
546 /// Return true if this branch has a non-always predication
547 static bool HasConditionalBranch(const MCInst &MI) {
548 int NumOp = MI.getNumOperands();
550 for (int i = 0; i < NumOp-1; ++i) {
551 const MCOperand &MCOp1 = MI.getOperand(i);
552 const MCOperand &MCOp2 = MI.getOperand(i + 1);
553 if (MCOp1.isImm() && MCOp2.isReg() &&
554 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
555 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
563 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
565 uint32_t ARMMCCodeEmitter::
566 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
567 SmallVectorImpl<MCFixup> &Fixups) const {
568 // FIXME: This really, really shouldn't use TargetMachine. We don't want
569 // coupling between MC and TM anywhere we can help it.
572 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
573 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
576 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
578 uint32_t ARMMCCodeEmitter::
579 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
580 SmallVectorImpl<MCFixup> &Fixups) const {
581 const MCOperand MO = MI.getOperand(OpIdx);
583 if (HasConditionalBranch(MI))
584 return ::getBranchTargetOpValue(MI, OpIdx,
585 ARM::fixup_arm_condbranch, Fixups);
586 return ::getBranchTargetOpValue(MI, OpIdx,
587 ARM::fixup_arm_uncondbranch, Fixups);
590 return MO.getImm() >> 2;
593 uint32_t ARMMCCodeEmitter::
594 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
596 const MCOperand MO = MI.getOperand(OpIdx);
598 if (HasConditionalBranch(MI))
599 return ::getBranchTargetOpValue(MI, OpIdx,
600 ARM::fixup_arm_condbranch, Fixups);
601 return ::getBranchTargetOpValue(MI, OpIdx,
602 ARM::fixup_arm_uncondbranch, Fixups);
605 return MO.getImm() >> 1;
608 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
609 /// immediate branch target.
610 uint32_t ARMMCCodeEmitter::
611 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
612 SmallVectorImpl<MCFixup> &Fixups) const {
614 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
615 bool I = (Val & 0x800000);
616 bool J1 = (Val & 0x400000);
617 bool J2 = (Val & 0x200000);
631 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
633 uint32_t ARMMCCodeEmitter::
634 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
635 SmallVectorImpl<MCFixup> &Fixups) const {
636 const MCOperand MO = MI.getOperand(OpIdx);
638 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
640 int32_t offset = MO.getImm();
641 uint32_t Val = 0x2000;
650 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
652 uint32_t ARMMCCodeEmitter::
653 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
654 SmallVectorImpl<MCFixup> &Fixups) const {
655 const MCOperand MO = MI.getOperand(OpIdx);
657 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
662 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
664 uint32_t ARMMCCodeEmitter::
665 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
666 SmallVectorImpl<MCFixup> &Fixups) const {
667 const MCOperand MO = MI.getOperand(OpIdx);
669 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
674 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
676 uint32_t ARMMCCodeEmitter::
677 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
678 SmallVectorImpl<MCFixup> &) const {
682 const MCOperand &MO1 = MI.getOperand(OpIdx);
683 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
684 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
685 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
686 return (Rm << 3) | Rn;
689 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
690 uint32_t ARMMCCodeEmitter::
691 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
692 SmallVectorImpl<MCFixup> &Fixups) const {
694 // {12} = (U)nsigned (add == '1', sub == '0')
698 // If The first operand isn't a register, we have a label reference.
699 const MCOperand &MO = MI.getOperand(OpIdx);
701 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
703 isAdd = false ; // 'U' bit is set as part of the fixup.
705 assert(MO.isExpr() && "Unexpected machine operand type!");
706 const MCExpr *Expr = MO.getExpr();
710 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
712 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
713 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
715 ++MCNumCPRelocations;
717 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
719 uint32_t Binary = Imm12 & 0xfff;
720 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
723 Binary |= (Reg << 13);
727 /// getT2Imm8s4OpValue - Return encoding info for
728 /// '+/- imm8<<2' operand.
729 uint32_t ARMMCCodeEmitter::
730 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
731 SmallVectorImpl<MCFixup> &Fixups) const {
732 // FIXME: The immediate operand should have already been encoded like this
733 // before ever getting here. The encoder method should just need to combine
734 // the MI operands for the register and the offset into a single
735 // representation for the complex operand in the .td file. This isn't just
736 // style, unfortunately. As-is, we can't represent the distinct encoding
739 // {8} = (U)nsigned (add == '1', sub == '0')
741 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
742 bool isAdd = Imm8 >= 0;
744 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
751 uint32_t Binary = Imm8 & 0xff;
752 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
758 /// getT2AddrModeImm8s4OpValue - Return encoding info for
759 /// 'reg +/- imm8<<2' operand.
760 uint32_t ARMMCCodeEmitter::
761 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
762 SmallVectorImpl<MCFixup> &Fixups) const {
764 // {8} = (U)nsigned (add == '1', sub == '0')
768 // If The first operand isn't a register, we have a label reference.
769 const MCOperand &MO = MI.getOperand(OpIdx);
771 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
773 isAdd = false ; // 'U' bit is set as part of the fixup.
775 assert(MO.isExpr() && "Unexpected machine operand type!");
776 const MCExpr *Expr = MO.getExpr();
777 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
778 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
780 ++MCNumCPRelocations;
782 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
784 // FIXME: The immediate operand should have already been encoded like this
785 // before ever getting here. The encoder method should just need to combine
786 // the MI operands for the register and the offset into a single
787 // representation for the complex operand in the .td file. This isn't just
788 // style, unfortunately. As-is, we can't represent the distinct encoding
790 uint32_t Binary = (Imm8 >> 2) & 0xff;
791 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
794 Binary |= (Reg << 9);
798 // FIXME: This routine assumes that a binary
799 // expression will always result in a PCRel expression
800 // In reality, its only true if one or more subexpressions
801 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
802 // but this is good enough for now.
803 static bool EvaluateAsPCRel(const MCExpr *Expr) {
804 switch (Expr->getKind()) {
805 default: assert(0 && "Unexpected expression type");
806 case MCExpr::SymbolRef: return false;
807 case MCExpr::Binary: return true;
812 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
813 SmallVectorImpl<MCFixup> &Fixups) const {
814 // {20-16} = imm{15-12}
815 // {11-0} = imm{11-0}
816 const MCOperand &MO = MI.getOperand(OpIdx);
818 // Hi / lo 16 bits already extracted during earlier passes.
819 return static_cast<unsigned>(MO.getImm());
821 // Handle :upper16: and :lower16: assembly prefixes.
822 const MCExpr *E = MO.getExpr();
823 if (E->getKind() == MCExpr::Target) {
824 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
825 E = ARM16Expr->getSubExpr();
828 switch (ARM16Expr->getKind()) {
829 default: assert(0 && "Unsupported ARMFixup");
830 case ARMMCExpr::VK_ARM_HI16:
831 if (!isTargetDarwin() && EvaluateAsPCRel(E))
832 Kind = MCFixupKind(isThumb2()
833 ? ARM::fixup_t2_movt_hi16_pcrel
834 : ARM::fixup_arm_movt_hi16_pcrel);
836 Kind = MCFixupKind(isThumb2()
837 ? ARM::fixup_t2_movt_hi16
838 : ARM::fixup_arm_movt_hi16);
840 case ARMMCExpr::VK_ARM_LO16:
841 if (!isTargetDarwin() && EvaluateAsPCRel(E))
842 Kind = MCFixupKind(isThumb2()
843 ? ARM::fixup_t2_movw_lo16_pcrel
844 : ARM::fixup_arm_movw_lo16_pcrel);
846 Kind = MCFixupKind(isThumb2()
847 ? ARM::fixup_t2_movw_lo16
848 : ARM::fixup_arm_movw_lo16);
851 Fixups.push_back(MCFixup::Create(0, E, Kind));
855 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
859 uint32_t ARMMCCodeEmitter::
860 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
861 SmallVectorImpl<MCFixup> &Fixups) const {
862 const MCOperand &MO = MI.getOperand(OpIdx);
863 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
864 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
865 unsigned Rn = getARMRegisterNumbering(MO.getReg());
866 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
867 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
868 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
869 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
870 unsigned SBits = getShiftOp(ShOp);
879 uint32_t Binary = Rm;
881 Binary |= SBits << 5;
882 Binary |= ShImm << 7;
888 uint32_t ARMMCCodeEmitter::
889 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
890 SmallVectorImpl<MCFixup> &Fixups) const {
892 // {13} 1 == imm12, 0 == Rm
895 const MCOperand &MO = MI.getOperand(OpIdx);
896 unsigned Rn = getARMRegisterNumbering(MO.getReg());
897 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
902 uint32_t ARMMCCodeEmitter::
903 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
904 SmallVectorImpl<MCFixup> &Fixups) const {
905 // {13} 1 == imm12, 0 == Rm
908 const MCOperand &MO = MI.getOperand(OpIdx);
909 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
910 unsigned Imm = MO1.getImm();
911 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
912 bool isReg = MO.getReg() != 0;
913 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
914 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
916 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
917 Binary <<= 7; // Shift amount is bits [11:7]
918 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
919 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
921 return Binary | (isAdd << 12) | (isReg << 13);
924 uint32_t ARMMCCodeEmitter::
925 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
926 SmallVectorImpl<MCFixup> &Fixups) const {
929 const MCOperand &MO = MI.getOperand(OpIdx);
930 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
931 bool isAdd = MO1.getImm() != 0;
932 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
935 uint32_t ARMMCCodeEmitter::
936 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
937 SmallVectorImpl<MCFixup> &Fixups) const {
938 // {9} 1 == imm8, 0 == Rm
942 const MCOperand &MO = MI.getOperand(OpIdx);
943 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
944 unsigned Imm = MO1.getImm();
945 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
946 bool isImm = MO.getReg() == 0;
947 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
948 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
950 Imm8 = getARMRegisterNumbering(MO.getReg());
951 return Imm8 | (isAdd << 8) | (isImm << 9);
954 uint32_t ARMMCCodeEmitter::
955 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
956 SmallVectorImpl<MCFixup> &Fixups) const {
957 // {13} 1 == imm8, 0 == Rm
962 const MCOperand &MO = MI.getOperand(OpIdx);
963 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
964 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
965 unsigned Rn = getARMRegisterNumbering(MO.getReg());
966 unsigned Imm = MO2.getImm();
967 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
968 bool isImm = MO1.getReg() == 0;
969 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
970 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
972 Imm8 = getARMRegisterNumbering(MO1.getReg());
973 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
976 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
977 uint32_t ARMMCCodeEmitter::
978 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
979 SmallVectorImpl<MCFixup> &Fixups) const {
982 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
983 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
984 "Unexpected base register!");
986 // The immediate is already shifted for the implicit zeroes, so no change
988 return MO1.getImm() & 0xff;
991 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
992 uint32_t ARMMCCodeEmitter::
993 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
994 SmallVectorImpl<MCFixup> &Fixups) const {
998 const MCOperand &MO = MI.getOperand(OpIdx);
999 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1000 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1001 unsigned Imm5 = MO1.getImm();
1002 return ((Imm5 & 0x1f) << 3) | Rn;
1005 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1006 uint32_t ARMMCCodeEmitter::
1007 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1008 SmallVectorImpl<MCFixup> &Fixups) const {
1009 const MCOperand MO = MI.getOperand(OpIdx);
1011 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1012 return (MO.getImm() >> 2);
1015 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1016 uint32_t ARMMCCodeEmitter::
1017 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1018 SmallVectorImpl<MCFixup> &Fixups) const {
1020 // {8} = (U)nsigned (add == '1', sub == '0')
1024 // If The first operand isn't a register, we have a label reference.
1025 const MCOperand &MO = MI.getOperand(OpIdx);
1027 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
1029 isAdd = false; // 'U' bit is handled as part of the fixup.
1031 assert(MO.isExpr() && "Unexpected machine operand type!");
1032 const MCExpr *Expr = MO.getExpr();
1035 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1037 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1038 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
1040 ++MCNumCPRelocations;
1042 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
1043 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1046 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1047 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1050 Binary |= (Reg << 9);
1054 unsigned ARMMCCodeEmitter::
1055 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1056 SmallVectorImpl<MCFixup> &Fixups) const {
1057 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1058 // shifted. The second is Rs, the amount to shift by, and the third specifies
1059 // the type of the shift.
1067 const MCOperand &MO = MI.getOperand(OpIdx);
1068 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1069 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1070 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1073 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1075 // Encode the shift opcode.
1077 unsigned Rs = MO1.getReg();
1079 // Set shift operand (bit[7:4]).
1085 default: llvm_unreachable("Unknown shift opc!");
1086 case ARM_AM::lsl: SBits = 0x1; break;
1087 case ARM_AM::lsr: SBits = 0x3; break;
1088 case ARM_AM::asr: SBits = 0x5; break;
1089 case ARM_AM::ror: SBits = 0x7; break;
1093 Binary |= SBits << 4;
1095 // Encode the shift operation Rs.
1096 // Encode Rs bit[11:8].
1097 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1098 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1101 unsigned ARMMCCodeEmitter::
1102 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1103 SmallVectorImpl<MCFixup> &Fixups) const {
1104 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1105 // shifted. The second is the amount to shift by.
1112 const MCOperand &MO = MI.getOperand(OpIdx);
1113 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1114 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1117 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1119 // Encode the shift opcode.
1122 // Set shift operand (bit[6:4]).
1127 // RRX - 110 and bit[11:8] clear.
1129 default: llvm_unreachable("Unknown shift opc!");
1130 case ARM_AM::lsl: SBits = 0x0; break;
1131 case ARM_AM::lsr: SBits = 0x2; break;
1132 case ARM_AM::asr: SBits = 0x4; break;
1133 case ARM_AM::ror: SBits = 0x6; break;
1139 // Encode shift_imm bit[11:7].
1140 Binary |= SBits << 4;
1141 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1142 assert(Offset && "Offset must be in range 1-32!");
1143 if (Offset == 32) Offset = 0;
1144 return Binary | (Offset << 7);
1148 unsigned ARMMCCodeEmitter::
1149 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1150 SmallVectorImpl<MCFixup> &Fixups) const {
1151 const MCOperand &MO1 = MI.getOperand(OpNum);
1152 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1153 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1155 // Encoded as [Rn, Rm, imm].
1156 // FIXME: Needs fixup support.
1157 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1159 Value |= getARMRegisterNumbering(MO2.getReg());
1161 Value |= MO3.getImm();
1166 unsigned ARMMCCodeEmitter::
1167 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1168 SmallVectorImpl<MCFixup> &Fixups) const {
1169 const MCOperand &MO1 = MI.getOperand(OpNum);
1170 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1172 // FIXME: Needs fixup support.
1173 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1175 // Even though the immediate is 8 bits long, we need 9 bits in order
1176 // to represent the (inverse of the) sign bit.
1178 int32_t tmp = (int32_t)MO2.getImm();
1182 Value |= 256; // Set the ADD bit
1187 unsigned ARMMCCodeEmitter::
1188 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1189 SmallVectorImpl<MCFixup> &Fixups) const {
1190 const MCOperand &MO1 = MI.getOperand(OpNum);
1192 // FIXME: Needs fixup support.
1194 int32_t tmp = (int32_t)MO1.getImm();
1198 Value |= 256; // Set the ADD bit
1203 unsigned ARMMCCodeEmitter::
1204 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1205 SmallVectorImpl<MCFixup> &Fixups) const {
1206 const MCOperand &MO1 = MI.getOperand(OpNum);
1208 // FIXME: Needs fixup support.
1210 int32_t tmp = (int32_t)MO1.getImm();
1214 Value |= 4096; // Set the ADD bit
1215 Value |= tmp & 4095;
1219 unsigned ARMMCCodeEmitter::
1220 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1221 SmallVectorImpl<MCFixup> &Fixups) const {
1222 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1223 // shifted. The second is the amount to shift by.
1230 const MCOperand &MO = MI.getOperand(OpIdx);
1231 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1232 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1235 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1237 // Encode the shift opcode.
1239 // Set shift operand (bit[6:4]).
1245 default: llvm_unreachable("Unknown shift opc!");
1246 case ARM_AM::lsl: SBits = 0x0; break;
1247 case ARM_AM::lsr: SBits = 0x2; break;
1248 case ARM_AM::asr: SBits = 0x4; break;
1249 case ARM_AM::ror: SBits = 0x6; break;
1252 Binary |= SBits << 4;
1253 if (SOpc == ARM_AM::rrx)
1256 // Encode shift_imm bit[11:7].
1257 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1260 unsigned ARMMCCodeEmitter::
1261 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1262 SmallVectorImpl<MCFixup> &Fixups) const {
1263 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1265 const MCOperand &MO = MI.getOperand(Op);
1266 uint32_t v = ~MO.getImm();
1267 uint32_t lsb = CountTrailingZeros_32(v);
1268 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1269 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1270 return lsb | (msb << 5);
1273 unsigned ARMMCCodeEmitter::
1274 getMsbOpValue(const MCInst &MI, unsigned Op,
1275 SmallVectorImpl<MCFixup> &Fixups) const {
1277 uint32_t lsb = MI.getOperand(Op-1).getImm();
1278 uint32_t width = MI.getOperand(Op).getImm();
1279 uint32_t msb = lsb+width-1;
1280 assert (width != 0 && msb < 32 && "Illegal bit width!");
1284 unsigned ARMMCCodeEmitter::
1285 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1286 SmallVectorImpl<MCFixup> &Fixups) const {
1289 // {7-0} = Number of registers
1292 // {15-0} = Bitfield of GPRs.
1293 unsigned Reg = MI.getOperand(Op).getReg();
1294 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1295 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1297 unsigned Binary = 0;
1299 if (SPRRegs || DPRRegs) {
1301 unsigned RegNo = getARMRegisterNumbering(Reg);
1302 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1303 Binary |= (RegNo & 0x1f) << 8;
1307 Binary |= NumRegs * 2;
1309 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1310 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1311 Binary |= 1 << RegNo;
1318 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1319 /// with the alignment operand.
1320 unsigned ARMMCCodeEmitter::
1321 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1322 SmallVectorImpl<MCFixup> &Fixups) const {
1323 const MCOperand &Reg = MI.getOperand(Op);
1324 const MCOperand &Imm = MI.getOperand(Op + 1);
1326 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1329 switch (Imm.getImm()) {
1333 case 8: Align = 0x01; break;
1334 case 16: Align = 0x02; break;
1335 case 32: Align = 0x03; break;
1338 return RegNo | (Align << 4);
1341 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1342 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1343 unsigned ARMMCCodeEmitter::
1344 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1345 SmallVectorImpl<MCFixup> &Fixups) const {
1346 const MCOperand &Reg = MI.getOperand(Op);
1347 const MCOperand &Imm = MI.getOperand(Op + 1);
1349 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1352 switch (Imm.getImm()) {
1357 case 16: Align = 0x00; break;
1358 case 32: Align = 0x03; break;
1361 return RegNo | (Align << 4);
1365 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1366 /// alignment operand for use in VLD-dup instructions. This is the same as
1367 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1368 /// different for VLD4-dup.
1369 unsigned ARMMCCodeEmitter::
1370 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1371 SmallVectorImpl<MCFixup> &Fixups) const {
1372 const MCOperand &Reg = MI.getOperand(Op);
1373 const MCOperand &Imm = MI.getOperand(Op + 1);
1375 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1378 switch (Imm.getImm()) {
1382 case 8: Align = 0x01; break;
1383 case 16: Align = 0x03; break;
1386 return RegNo | (Align << 4);
1389 unsigned ARMMCCodeEmitter::
1390 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1391 SmallVectorImpl<MCFixup> &Fixups) const {
1392 const MCOperand &MO = MI.getOperand(Op);
1393 if (MO.getReg() == 0) return 0x0D;
1397 unsigned ARMMCCodeEmitter::
1398 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1399 SmallVectorImpl<MCFixup> &Fixups) const {
1400 return 8 - MI.getOperand(Op).getImm();
1403 unsigned ARMMCCodeEmitter::
1404 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1405 SmallVectorImpl<MCFixup> &Fixups) const {
1406 return 16 - MI.getOperand(Op).getImm();
1409 unsigned ARMMCCodeEmitter::
1410 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1411 SmallVectorImpl<MCFixup> &Fixups) const {
1412 return 32 - MI.getOperand(Op).getImm();
1415 unsigned ARMMCCodeEmitter::
1416 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1417 SmallVectorImpl<MCFixup> &Fixups) const {
1418 return 64 - MI.getOperand(Op).getImm();
1421 void ARMMCCodeEmitter::
1422 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1423 SmallVectorImpl<MCFixup> &Fixups) const {
1424 // Pseudo instructions don't get encoded.
1425 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1426 uint64_t TSFlags = Desc.TSFlags;
1427 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1431 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1432 Size = Desc.getSize();
1434 llvm_unreachable("Unexpected instruction size!");
1436 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1437 // Thumb 32-bit wide instructions need to emit the high order halfword
1439 if (isThumb() && Size == 4) {
1440 EmitConstant(Binary >> 16, 2, OS);
1441 EmitConstant(Binary & 0xffff, 2, OS);
1443 EmitConstant(Binary, Size, OS);
1444 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1447 #include "ARMGenMCCodeEmitter.inc"