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 uint64_t 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 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
123 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
126 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
127 /// ADR label target.
128 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
136 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
138 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
141 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
142 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups)const;
145 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
147 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups) const;
150 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
152 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
155 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
157 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
161 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
162 /// operand as needed by load/store instructions.
163 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
166 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
167 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const {
169 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
171 default: llvm_unreachable("Unknown addressing sub-mode!");
172 case ARM_AM::da: return 0;
173 case ARM_AM::ia: return 1;
174 case ARM_AM::db: return 2;
175 case ARM_AM::ib: return 3;
178 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
180 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
182 case ARM_AM::no_shift:
183 case ARM_AM::lsl: return 0;
184 case ARM_AM::lsr: return 1;
185 case ARM_AM::asr: return 2;
187 case ARM_AM::rrx: return 3;
189 llvm_unreachable("Invalid ShiftOpc!");
192 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
193 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
194 SmallVectorImpl<MCFixup> &Fixups) const;
196 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
197 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
200 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
201 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
202 SmallVectorImpl<MCFixup> &Fixups) const;
204 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
205 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
206 SmallVectorImpl<MCFixup> &Fixups) const;
208 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
209 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
210 SmallVectorImpl<MCFixup> &Fixups) const;
212 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
214 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
215 SmallVectorImpl<MCFixup> &Fixups) const;
217 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
218 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
219 SmallVectorImpl<MCFixup> &Fixups) const;
221 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
222 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
223 SmallVectorImpl<MCFixup> &Fixups) const;
225 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
226 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
227 SmallVectorImpl<MCFixup> &Fixups) const;
229 /// getCCOutOpValue - Return encoding of the 's' bit.
230 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
231 SmallVectorImpl<MCFixup> &Fixups) const {
232 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
234 return MI.getOperand(Op).getReg() == ARM::CPSR;
237 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
238 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const {
240 unsigned SoImm = MI.getOperand(Op).getImm();
241 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
242 assert(SoImmVal != -1 && "Not a valid so_imm value!");
244 // Encode rotate_imm.
245 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
246 << ARMII::SoRotImmShift;
249 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
253 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
254 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const {
256 unsigned SoImm = MI.getOperand(Op).getImm();
257 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
258 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
262 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
263 SmallVectorImpl<MCFixup> &Fixups) const;
264 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
266 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
267 SmallVectorImpl<MCFixup> &Fixups) const;
268 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
269 SmallVectorImpl<MCFixup> &Fixups) const;
271 /// getSORegOpValue - Return an encoded so_reg shifted register value.
272 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
274 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
276 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
279 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const {
281 return 64 - MI.getOperand(Op).getImm();
284 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const;
287 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
288 SmallVectorImpl<MCFixup> &Fixups) const;
289 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
293 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
295 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
298 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
307 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
308 SmallVectorImpl<MCFixup> &Fixups) const;
310 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
311 unsigned EncodedValue) const;
312 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
313 unsigned EncodedValue) const;
314 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
315 unsigned EncodedValue) const;
317 unsigned VFPThumb2PostEncoder(const MCInst &MI,
318 unsigned EncodedValue) const;
320 void EmitByte(unsigned char C, raw_ostream &OS) const {
324 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
325 // Output the constant in little endian byte order.
326 for (unsigned i = 0; i != Size; ++i) {
327 EmitByte(Val & 255, OS);
332 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
333 SmallVectorImpl<MCFixup> &Fixups) const;
336 } // end anonymous namespace
338 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
339 const MCSubtargetInfo &STI,
341 return new ARMMCCodeEmitter(MCII, STI, Ctx);
344 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
345 /// instructions, and rewrite them to their Thumb2 form if we are currently in
347 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
348 unsigned EncodedValue) const {
350 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
351 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
353 unsigned Bit24 = EncodedValue & 0x01000000;
354 unsigned Bit28 = Bit24 << 4;
355 EncodedValue &= 0xEFFFFFFF;
356 EncodedValue |= Bit28;
357 EncodedValue |= 0x0F000000;
363 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
364 /// instructions, and rewrite them to their Thumb2 form if we are currently in
366 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
367 unsigned EncodedValue) const {
369 EncodedValue &= 0xF0FFFFFF;
370 EncodedValue |= 0x09000000;
376 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
377 /// instructions, and rewrite them to their Thumb2 form if we are currently in
379 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
380 unsigned EncodedValue) const {
382 EncodedValue &= 0x00FFFFFF;
383 EncodedValue |= 0xEE000000;
389 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
390 /// them to their Thumb2 form if we are currently in Thumb2 mode.
391 unsigned ARMMCCodeEmitter::
392 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
394 EncodedValue &= 0x0FFFFFFF;
395 EncodedValue |= 0xE0000000;
400 /// getMachineOpValue - Return binary encoding of operand. If the machine
401 /// operand requires relocation, record the relocation and return zero.
402 unsigned ARMMCCodeEmitter::
403 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
404 SmallVectorImpl<MCFixup> &Fixups) const {
406 unsigned Reg = MO.getReg();
407 unsigned RegNo = getARMRegisterNumbering(Reg);
409 // Q registers are encoded as 2x their register number.
413 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
414 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
415 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
416 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
419 } else if (MO.isImm()) {
420 return static_cast<unsigned>(MO.getImm());
421 } else if (MO.isFPImm()) {
422 return static_cast<unsigned>(APFloat(MO.getFPImm())
423 .bitcastToAPInt().getHiBits(32).getLimitedValue());
426 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) {
447 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
457 /// getBranchTargetOpValue - Helper function to get the branch target operand,
458 /// which is either an immediate or requires a fixup.
459 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
461 SmallVectorImpl<MCFixup> &Fixups) {
462 const MCOperand &MO = MI.getOperand(OpIdx);
464 // If the destination is an immediate, we have nothing to do.
465 if (MO.isImm()) return MO.getImm();
466 assert(MO.isExpr() && "Unexpected branch target type!");
467 const MCExpr *Expr = MO.getExpr();
468 MCFixupKind Kind = MCFixupKind(FixupKind);
469 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
471 // All of the information is in the fixup.
475 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
476 // determined by negating them and XOR'ing them with bit 23.
477 static int32_t encodeThumbBLOffset(int32_t offset) {
479 uint32_t S = (offset & 0x800000) >> 23;
480 uint32_t J1 = (offset & 0x400000) >> 22;
481 uint32_t J2 = (offset & 0x200000) >> 21;
494 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
495 uint32_t ARMMCCodeEmitter::
496 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
497 SmallVectorImpl<MCFixup> &Fixups) const {
498 const MCOperand MO = MI.getOperand(OpIdx);
500 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
502 return encodeThumbBLOffset(MO.getImm());
505 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
506 /// BLX branch target.
507 uint32_t ARMMCCodeEmitter::
508 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
509 SmallVectorImpl<MCFixup> &Fixups) const {
510 const MCOperand MO = MI.getOperand(OpIdx);
512 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
514 return encodeThumbBLOffset(MO.getImm());
517 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
518 uint32_t ARMMCCodeEmitter::
519 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
520 SmallVectorImpl<MCFixup> &Fixups) const {
521 const MCOperand MO = MI.getOperand(OpIdx);
523 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
525 return (MO.getImm() >> 1);
528 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
529 uint32_t ARMMCCodeEmitter::
530 getThumbBCCTargetOpValue(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_bcc,
536 return (MO.getImm() >> 1);
539 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
540 uint32_t ARMMCCodeEmitter::
541 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
542 SmallVectorImpl<MCFixup> &Fixups) const {
543 const MCOperand MO = MI.getOperand(OpIdx);
545 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
546 return (MO.getImm() >> 1);
549 /// Return true if this branch has a non-always predication
550 static bool HasConditionalBranch(const MCInst &MI) {
551 int NumOp = MI.getNumOperands();
553 for (int i = 0; i < NumOp-1; ++i) {
554 const MCOperand &MCOp1 = MI.getOperand(i);
555 const MCOperand &MCOp2 = MI.getOperand(i + 1);
556 if (MCOp1.isImm() && MCOp2.isReg() &&
557 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
558 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
566 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
568 uint32_t ARMMCCodeEmitter::
569 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
570 SmallVectorImpl<MCFixup> &Fixups) const {
571 // FIXME: This really, really shouldn't use TargetMachine. We don't want
572 // coupling between MC and TM anywhere we can help it.
575 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
576 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
579 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
581 uint32_t ARMMCCodeEmitter::
582 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
583 SmallVectorImpl<MCFixup> &Fixups) const {
584 const MCOperand MO = MI.getOperand(OpIdx);
586 if (HasConditionalBranch(MI))
587 return ::getBranchTargetOpValue(MI, OpIdx,
588 ARM::fixup_arm_condbranch, Fixups);
589 return ::getBranchTargetOpValue(MI, OpIdx,
590 ARM::fixup_arm_uncondbranch, Fixups);
593 return MO.getImm() >> 2;
596 uint32_t ARMMCCodeEmitter::
597 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
598 SmallVectorImpl<MCFixup> &Fixups) const {
599 const MCOperand MO = MI.getOperand(OpIdx);
601 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_bl, Fixups);
603 return MO.getImm() >> 2;
606 uint32_t ARMMCCodeEmitter::
607 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
608 SmallVectorImpl<MCFixup> &Fixups) const {
609 const MCOperand MO = MI.getOperand(OpIdx);
611 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups);
613 return MO.getImm() >> 1;
616 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
617 /// immediate branch target.
618 uint32_t ARMMCCodeEmitter::
619 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &Fixups) const {
622 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
623 bool I = (Val & 0x800000);
624 bool J1 = (Val & 0x400000);
625 bool J2 = (Val & 0x200000);
639 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
641 uint32_t ARMMCCodeEmitter::
642 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
643 SmallVectorImpl<MCFixup> &Fixups) const {
644 const MCOperand MO = MI.getOperand(OpIdx);
646 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
648 int32_t offset = MO.getImm();
649 uint32_t Val = 0x2000;
658 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
660 uint32_t ARMMCCodeEmitter::
661 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
662 SmallVectorImpl<MCFixup> &Fixups) const {
663 const MCOperand MO = MI.getOperand(OpIdx);
665 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
667 int32_t Val = MO.getImm();
675 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
677 uint32_t ARMMCCodeEmitter::
678 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups) const {
680 const MCOperand MO = MI.getOperand(OpIdx);
682 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
687 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
689 uint32_t ARMMCCodeEmitter::
690 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
691 SmallVectorImpl<MCFixup> &) const {
695 const MCOperand &MO1 = MI.getOperand(OpIdx);
696 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
697 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
698 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
699 return (Rm << 3) | Rn;
702 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
703 uint32_t ARMMCCodeEmitter::
704 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
705 SmallVectorImpl<MCFixup> &Fixups) const {
707 // {12} = (U)nsigned (add == '1', sub == '0')
711 // If The first operand isn't a register, we have a label reference.
712 const MCOperand &MO = MI.getOperand(OpIdx);
714 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
716 isAdd = false ; // 'U' bit is set as part of the fixup.
719 const MCExpr *Expr = MO.getExpr();
723 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
725 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
726 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
728 ++MCNumCPRelocations;
731 int32_t Offset = MO.getImm();
732 // FIXME: Handle #-0.
740 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
742 uint32_t Binary = Imm12 & 0xfff;
743 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
746 Binary |= (Reg << 13);
750 /// getT2Imm8s4OpValue - Return encoding info for
751 /// '+/- imm8<<2' operand.
752 uint32_t ARMMCCodeEmitter::
753 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
754 SmallVectorImpl<MCFixup> &Fixups) const {
755 // FIXME: The immediate operand should have already been encoded like this
756 // before ever getting here. The encoder method should just need to combine
757 // the MI operands for the register and the offset into a single
758 // representation for the complex operand in the .td file. This isn't just
759 // style, unfortunately. As-is, we can't represent the distinct encoding
762 // {8} = (U)nsigned (add == '1', sub == '0')
764 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
765 bool isAdd = Imm8 >= 0;
767 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
774 uint32_t Binary = Imm8 & 0xff;
775 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
781 /// getT2AddrModeImm8s4OpValue - Return encoding info for
782 /// 'reg +/- imm8<<2' operand.
783 uint32_t ARMMCCodeEmitter::
784 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
785 SmallVectorImpl<MCFixup> &Fixups) const {
787 // {8} = (U)nsigned (add == '1', sub == '0')
791 // If The first operand isn't a register, we have a label reference.
792 const MCOperand &MO = MI.getOperand(OpIdx);
794 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
796 isAdd = false ; // 'U' bit is set as part of the fixup.
798 assert(MO.isExpr() && "Unexpected machine operand type!");
799 const MCExpr *Expr = MO.getExpr();
800 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
801 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
803 ++MCNumCPRelocations;
805 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
807 // FIXME: The immediate operand should have already been encoded like this
808 // before ever getting here. The encoder method should just need to combine
809 // the MI operands for the register and the offset into a single
810 // representation for the complex operand in the .td file. This isn't just
811 // style, unfortunately. As-is, we can't represent the distinct encoding
813 uint32_t Binary = (Imm8 >> 2) & 0xff;
814 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
817 Binary |= (Reg << 9);
821 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
822 /// 'reg + imm8<<2' operand.
823 uint32_t ARMMCCodeEmitter::
824 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
825 SmallVectorImpl<MCFixup> &Fixups) const {
828 const MCOperand &MO = MI.getOperand(OpIdx);
829 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
830 unsigned Reg = getARMRegisterNumbering(MO.getReg());
831 unsigned Imm8 = MO1.getImm();
832 return (Reg << 8) | Imm8;
835 // FIXME: This routine assumes that a binary
836 // expression will always result in a PCRel expression
837 // In reality, its only true if one or more subexpressions
838 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
839 // but this is good enough for now.
840 static bool EvaluateAsPCRel(const MCExpr *Expr) {
841 switch (Expr->getKind()) {
842 default: llvm_unreachable("Unexpected expression type");
843 case MCExpr::SymbolRef: return false;
844 case MCExpr::Binary: return true;
849 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
850 SmallVectorImpl<MCFixup> &Fixups) const {
851 // {20-16} = imm{15-12}
852 // {11-0} = imm{11-0}
853 const MCOperand &MO = MI.getOperand(OpIdx);
855 // Hi / lo 16 bits already extracted during earlier passes.
856 return static_cast<unsigned>(MO.getImm());
858 // Handle :upper16: and :lower16: assembly prefixes.
859 const MCExpr *E = MO.getExpr();
860 if (E->getKind() == MCExpr::Target) {
861 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
862 E = ARM16Expr->getSubExpr();
865 switch (ARM16Expr->getKind()) {
866 default: llvm_unreachable("Unsupported ARMFixup");
867 case ARMMCExpr::VK_ARM_HI16:
868 if (!isTargetDarwin() && EvaluateAsPCRel(E))
869 Kind = MCFixupKind(isThumb2()
870 ? ARM::fixup_t2_movt_hi16_pcrel
871 : ARM::fixup_arm_movt_hi16_pcrel);
873 Kind = MCFixupKind(isThumb2()
874 ? ARM::fixup_t2_movt_hi16
875 : ARM::fixup_arm_movt_hi16);
877 case ARMMCExpr::VK_ARM_LO16:
878 if (!isTargetDarwin() && EvaluateAsPCRel(E))
879 Kind = MCFixupKind(isThumb2()
880 ? ARM::fixup_t2_movw_lo16_pcrel
881 : ARM::fixup_arm_movw_lo16_pcrel);
883 Kind = MCFixupKind(isThumb2()
884 ? ARM::fixup_t2_movw_lo16
885 : ARM::fixup_arm_movw_lo16);
888 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
892 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
895 uint32_t ARMMCCodeEmitter::
896 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
897 SmallVectorImpl<MCFixup> &Fixups) const {
898 const MCOperand &MO = MI.getOperand(OpIdx);
899 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
900 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
901 unsigned Rn = getARMRegisterNumbering(MO.getReg());
902 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
903 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
904 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
905 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
906 unsigned SBits = getShiftOp(ShOp);
915 uint32_t Binary = Rm;
917 Binary |= SBits << 5;
918 Binary |= ShImm << 7;
924 uint32_t ARMMCCodeEmitter::
925 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
926 SmallVectorImpl<MCFixup> &Fixups) const {
928 // {13} 1 == imm12, 0 == Rm
931 const MCOperand &MO = MI.getOperand(OpIdx);
932 unsigned Rn = getARMRegisterNumbering(MO.getReg());
933 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
938 uint32_t ARMMCCodeEmitter::
939 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
940 SmallVectorImpl<MCFixup> &Fixups) const {
941 // {13} 1 == imm12, 0 == Rm
944 const MCOperand &MO = MI.getOperand(OpIdx);
945 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
946 unsigned Imm = MO1.getImm();
947 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
948 bool isReg = MO.getReg() != 0;
949 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
950 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
952 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
953 Binary <<= 7; // Shift amount is bits [11:7]
954 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
955 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
957 return Binary | (isAdd << 12) | (isReg << 13);
960 uint32_t ARMMCCodeEmitter::
961 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
962 SmallVectorImpl<MCFixup> &Fixups) const {
965 const MCOperand &MO = MI.getOperand(OpIdx);
966 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
967 bool isAdd = MO1.getImm() != 0;
968 return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
971 uint32_t ARMMCCodeEmitter::
972 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
973 SmallVectorImpl<MCFixup> &Fixups) const {
974 // {9} 1 == imm8, 0 == Rm
978 const MCOperand &MO = MI.getOperand(OpIdx);
979 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
980 unsigned Imm = MO1.getImm();
981 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
982 bool isImm = MO.getReg() == 0;
983 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
984 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
986 Imm8 = getARMRegisterNumbering(MO.getReg());
987 return Imm8 | (isAdd << 8) | (isImm << 9);
990 uint32_t ARMMCCodeEmitter::
991 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
992 SmallVectorImpl<MCFixup> &Fixups) const {
993 // {13} 1 == imm8, 0 == Rm
998 const MCOperand &MO = MI.getOperand(OpIdx);
999 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1000 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1002 // If The first operand isn't a register, we have a label reference.
1004 unsigned Rn = getARMRegisterNumbering(ARM::PC); // Rn is PC.
1006 assert(MO.isExpr() && "Unexpected machine operand type!");
1007 const MCExpr *Expr = MO.getExpr();
1008 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1009 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1011 ++MCNumCPRelocations;
1012 return (Rn << 9) | (1 << 13);
1014 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1015 unsigned Imm = MO2.getImm();
1016 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1017 bool isImm = MO1.getReg() == 0;
1018 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1019 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1021 Imm8 = getARMRegisterNumbering(MO1.getReg());
1022 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1025 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1026 uint32_t ARMMCCodeEmitter::
1027 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1028 SmallVectorImpl<MCFixup> &Fixups) const {
1031 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1032 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1033 "Unexpected base register!");
1035 // The immediate is already shifted for the implicit zeroes, so no change
1037 return MO1.getImm() & 0xff;
1040 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1041 uint32_t ARMMCCodeEmitter::
1042 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1043 SmallVectorImpl<MCFixup> &Fixups) const {
1047 const MCOperand &MO = MI.getOperand(OpIdx);
1048 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1049 unsigned Rn = getARMRegisterNumbering(MO.getReg());
1050 unsigned Imm5 = MO1.getImm();
1051 return ((Imm5 & 0x1f) << 3) | Rn;
1054 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1055 uint32_t ARMMCCodeEmitter::
1056 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1057 SmallVectorImpl<MCFixup> &Fixups) const {
1058 const MCOperand MO = MI.getOperand(OpIdx);
1060 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1061 return (MO.getImm() >> 2);
1064 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1065 uint32_t ARMMCCodeEmitter::
1066 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1067 SmallVectorImpl<MCFixup> &Fixups) const {
1069 // {8} = (U)nsigned (add == '1', sub == '0')
1073 // If The first operand isn't a register, we have a label reference.
1074 const MCOperand &MO = MI.getOperand(OpIdx);
1076 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
1078 isAdd = false; // 'U' bit is handled as part of the fixup.
1080 assert(MO.isExpr() && "Unexpected machine operand type!");
1081 const MCExpr *Expr = MO.getExpr();
1084 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1086 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1087 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1089 ++MCNumCPRelocations;
1091 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
1092 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1095 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1096 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1099 Binary |= (Reg << 9);
1103 unsigned ARMMCCodeEmitter::
1104 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1105 SmallVectorImpl<MCFixup> &Fixups) const {
1106 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1107 // shifted. The second is Rs, the amount to shift by, and the third specifies
1108 // the type of the shift.
1116 const MCOperand &MO = MI.getOperand(OpIdx);
1117 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1118 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1119 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1122 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1124 // Encode the shift opcode.
1126 unsigned Rs = MO1.getReg();
1128 // Set shift operand (bit[7:4]).
1134 default: llvm_unreachable("Unknown shift opc!");
1135 case ARM_AM::lsl: SBits = 0x1; break;
1136 case ARM_AM::lsr: SBits = 0x3; break;
1137 case ARM_AM::asr: SBits = 0x5; break;
1138 case ARM_AM::ror: SBits = 0x7; break;
1142 Binary |= SBits << 4;
1144 // Encode the shift operation Rs.
1145 // Encode Rs bit[11:8].
1146 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1147 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1150 unsigned ARMMCCodeEmitter::
1151 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1152 SmallVectorImpl<MCFixup> &Fixups) const {
1153 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1154 // shifted. The second is the amount to shift by.
1161 const MCOperand &MO = MI.getOperand(OpIdx);
1162 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1163 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1166 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1168 // Encode the shift opcode.
1171 // Set shift operand (bit[6:4]).
1176 // RRX - 110 and bit[11:8] clear.
1178 default: llvm_unreachable("Unknown shift opc!");
1179 case ARM_AM::lsl: SBits = 0x0; break;
1180 case ARM_AM::lsr: SBits = 0x2; break;
1181 case ARM_AM::asr: SBits = 0x4; break;
1182 case ARM_AM::ror: SBits = 0x6; break;
1188 // Encode shift_imm bit[11:7].
1189 Binary |= SBits << 4;
1190 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1191 assert(Offset && "Offset must be in range 1-32!");
1192 if (Offset == 32) Offset = 0;
1193 return Binary | (Offset << 7);
1197 unsigned ARMMCCodeEmitter::
1198 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1199 SmallVectorImpl<MCFixup> &Fixups) const {
1200 const MCOperand &MO1 = MI.getOperand(OpNum);
1201 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1202 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1204 // Encoded as [Rn, Rm, imm].
1205 // FIXME: Needs fixup support.
1206 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1208 Value |= getARMRegisterNumbering(MO2.getReg());
1210 Value |= MO3.getImm();
1215 unsigned ARMMCCodeEmitter::
1216 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1217 SmallVectorImpl<MCFixup> &Fixups) const {
1218 const MCOperand &MO1 = MI.getOperand(OpNum);
1219 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1221 // FIXME: Needs fixup support.
1222 unsigned Value = getARMRegisterNumbering(MO1.getReg());
1224 // Even though the immediate is 8 bits long, we need 9 bits in order
1225 // to represent the (inverse of the) sign bit.
1227 int32_t tmp = (int32_t)MO2.getImm();
1231 Value |= 256; // Set the ADD bit
1236 unsigned ARMMCCodeEmitter::
1237 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1238 SmallVectorImpl<MCFixup> &Fixups) const {
1239 const MCOperand &MO1 = MI.getOperand(OpNum);
1241 // FIXME: Needs fixup support.
1243 int32_t tmp = (int32_t)MO1.getImm();
1247 Value |= 256; // Set the ADD bit
1252 unsigned ARMMCCodeEmitter::
1253 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1254 SmallVectorImpl<MCFixup> &Fixups) const {
1255 const MCOperand &MO1 = MI.getOperand(OpNum);
1257 // FIXME: Needs fixup support.
1259 int32_t tmp = (int32_t)MO1.getImm();
1263 Value |= 4096; // Set the ADD bit
1264 Value |= tmp & 4095;
1268 unsigned ARMMCCodeEmitter::
1269 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1270 SmallVectorImpl<MCFixup> &Fixups) const {
1271 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1272 // shifted. The second is the amount to shift by.
1279 const MCOperand &MO = MI.getOperand(OpIdx);
1280 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1281 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1284 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1286 // Encode the shift opcode.
1288 // Set shift operand (bit[6:4]).
1294 default: llvm_unreachable("Unknown shift opc!");
1295 case ARM_AM::lsl: SBits = 0x0; break;
1296 case ARM_AM::lsr: SBits = 0x2; break;
1297 case ARM_AM::asr: SBits = 0x4; break;
1298 case ARM_AM::rrx: // FALLTHROUGH
1299 case ARM_AM::ror: SBits = 0x6; break;
1302 Binary |= SBits << 4;
1303 if (SOpc == ARM_AM::rrx)
1306 // Encode shift_imm bit[11:7].
1307 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1310 unsigned ARMMCCodeEmitter::
1311 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1312 SmallVectorImpl<MCFixup> &Fixups) const {
1313 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1315 const MCOperand &MO = MI.getOperand(Op);
1316 uint32_t v = ~MO.getImm();
1317 uint32_t lsb = CountTrailingZeros_32(v);
1318 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1319 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1320 return lsb | (msb << 5);
1323 unsigned ARMMCCodeEmitter::
1324 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1325 SmallVectorImpl<MCFixup> &Fixups) const {
1328 // {7-0} = Number of registers
1331 // {15-0} = Bitfield of GPRs.
1332 unsigned Reg = MI.getOperand(Op).getReg();
1333 bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1334 bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1336 unsigned Binary = 0;
1338 if (SPRRegs || DPRRegs) {
1340 unsigned RegNo = getARMRegisterNumbering(Reg);
1341 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1342 Binary |= (RegNo & 0x1f) << 8;
1346 Binary |= NumRegs * 2;
1348 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1349 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1350 Binary |= 1 << RegNo;
1357 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1358 /// with the alignment operand.
1359 unsigned ARMMCCodeEmitter::
1360 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1361 SmallVectorImpl<MCFixup> &Fixups) const {
1362 const MCOperand &Reg = MI.getOperand(Op);
1363 const MCOperand &Imm = MI.getOperand(Op + 1);
1365 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1368 switch (Imm.getImm()) {
1372 case 8: Align = 0x01; break;
1373 case 16: Align = 0x02; break;
1374 case 32: Align = 0x03; break;
1377 return RegNo | (Align << 4);
1380 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1381 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1382 unsigned ARMMCCodeEmitter::
1383 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1384 SmallVectorImpl<MCFixup> &Fixups) const {
1385 const MCOperand &Reg = MI.getOperand(Op);
1386 const MCOperand &Imm = MI.getOperand(Op + 1);
1388 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1391 switch (Imm.getImm()) {
1395 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1396 case 2: Align = 0x00; break;
1397 case 4: Align = 0x03; break;
1400 return RegNo | (Align << 4);
1404 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1405 /// alignment operand for use in VLD-dup instructions. This is the same as
1406 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1407 /// different for VLD4-dup.
1408 unsigned ARMMCCodeEmitter::
1409 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1410 SmallVectorImpl<MCFixup> &Fixups) const {
1411 const MCOperand &Reg = MI.getOperand(Op);
1412 const MCOperand &Imm = MI.getOperand(Op + 1);
1414 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1417 switch (Imm.getImm()) {
1421 case 8: Align = 0x01; break;
1422 case 16: Align = 0x03; break;
1425 return RegNo | (Align << 4);
1428 unsigned ARMMCCodeEmitter::
1429 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1430 SmallVectorImpl<MCFixup> &Fixups) const {
1431 const MCOperand &MO = MI.getOperand(Op);
1432 if (MO.getReg() == 0) return 0x0D;
1433 return getARMRegisterNumbering(MO.getReg());
1436 unsigned ARMMCCodeEmitter::
1437 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1438 SmallVectorImpl<MCFixup> &Fixups) const {
1439 return 8 - MI.getOperand(Op).getImm();
1442 unsigned ARMMCCodeEmitter::
1443 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1444 SmallVectorImpl<MCFixup> &Fixups) const {
1445 return 16 - MI.getOperand(Op).getImm();
1448 unsigned ARMMCCodeEmitter::
1449 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1450 SmallVectorImpl<MCFixup> &Fixups) const {
1451 return 32 - MI.getOperand(Op).getImm();
1454 unsigned ARMMCCodeEmitter::
1455 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1456 SmallVectorImpl<MCFixup> &Fixups) const {
1457 return 64 - MI.getOperand(Op).getImm();
1460 void ARMMCCodeEmitter::
1461 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1462 SmallVectorImpl<MCFixup> &Fixups) const {
1463 // Pseudo instructions don't get encoded.
1464 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1465 uint64_t TSFlags = Desc.TSFlags;
1466 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1470 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1471 Size = Desc.getSize();
1473 llvm_unreachable("Unexpected instruction size!");
1475 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1476 // Thumb 32-bit wide instructions need to emit the high order halfword
1478 if (isThumb() && Size == 4) {
1479 EmitConstant(Binary >> 16, 2, OS);
1480 EmitConstant(Binary & 0xffff, 2, OS);
1482 EmitConstant(Binary, Size, OS);
1483 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1486 #include "ARMGenMCCodeEmitter.inc"