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/ARMMCTargetDesc.h"
16 #include "MCTargetDesc/ARMAddressingModes.h"
17 #include "MCTargetDesc/ARMBaseInfo.h"
18 #include "MCTargetDesc/ARMFixupKinds.h"
19 #include "MCTargetDesc/ARMMCExpr.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/Support/raw_ostream.h"
33 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
34 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
37 class ARMMCCodeEmitter : public MCCodeEmitter {
38 ARMMCCodeEmitter(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39 void operator=(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40 const MCInstrInfo &MCII;
41 const MCSubtargetInfo &STI;
45 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
47 : MCII(mcii), STI(sti), CTX(ctx) {
50 ~ARMMCCodeEmitter() {}
52 bool isThumb() const {
53 // FIXME: Can tablegen auto-generate this?
54 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
56 bool isThumb2() const {
57 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
59 bool isTargetDarwin() const {
60 Triple TT(STI.getTargetTriple());
61 Triple::OSType OS = TT.getOS();
62 return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
65 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
67 // getBinaryCodeForInstr - TableGen'erated function for getting the
68 // binary encoding for an instruction.
69 uint64_t getBinaryCodeForInstr(const MCInst &MI,
70 SmallVectorImpl<MCFixup> &Fixups) const;
72 /// getMachineOpValue - Return binary encoding of operand. If the machine
73 /// operand requires relocation, record the relocation and return zero.
74 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
75 SmallVectorImpl<MCFixup> &Fixups) const;
77 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
78 /// the specified operand. This is used for operands with :lower16: and
79 /// :upper16: prefixes.
80 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
81 SmallVectorImpl<MCFixup> &Fixups) const;
83 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
84 unsigned &Reg, unsigned &Imm,
85 SmallVectorImpl<MCFixup> &Fixups) const;
87 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
89 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
90 SmallVectorImpl<MCFixup> &Fixups) const;
92 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
93 /// BLX branch target.
94 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
97 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
98 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
101 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
102 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
105 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
106 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
109 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
111 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const;
114 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
115 /// immediate Thumb2 direct branch target.
116 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups) const;
119 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
121 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
123 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
125 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
126 SmallVectorImpl<MCFixup> &Fixups) const;
128 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
129 /// ADR label target.
130 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const;
138 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
140 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
143 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
144 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups)const;
147 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
149 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
152 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
154 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups) const;
157 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
159 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
163 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
164 /// operand as needed by load/store instructions.
165 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const;
168 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
169 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups) const {
171 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
173 default: llvm_unreachable("Unknown addressing sub-mode!");
174 case ARM_AM::da: return 0;
175 case ARM_AM::ia: return 1;
176 case ARM_AM::db: return 2;
177 case ARM_AM::ib: return 3;
180 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
182 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
184 case ARM_AM::no_shift:
185 case ARM_AM::lsl: return 0;
186 case ARM_AM::lsr: return 1;
187 case ARM_AM::asr: return 2;
189 case ARM_AM::rrx: return 3;
191 llvm_unreachable("Invalid ShiftOpc!");
194 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
195 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
198 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
199 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &Fixups) const;
202 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
203 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
206 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
207 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
210 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
211 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
214 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
216 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
217 SmallVectorImpl<MCFixup> &Fixups) const;
219 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
220 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
221 SmallVectorImpl<MCFixup> &Fixups) const;
223 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
224 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
225 SmallVectorImpl<MCFixup> &Fixups) const;
227 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
228 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
229 SmallVectorImpl<MCFixup> &Fixups) const;
231 /// getCCOutOpValue - Return encoding of the 's' bit.
232 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const {
234 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
236 return MI.getOperand(Op).getReg() == ARM::CPSR;
239 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
240 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const {
242 unsigned SoImm = MI.getOperand(Op).getImm();
243 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
244 assert(SoImmVal != -1 && "Not a valid so_imm value!");
246 // Encode rotate_imm.
247 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
248 << ARMII::SoRotImmShift;
251 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
255 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
256 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const {
258 unsigned SoImm = MI.getOperand(Op).getImm();
259 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
260 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
264 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
266 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
267 SmallVectorImpl<MCFixup> &Fixups) const;
268 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
269 SmallVectorImpl<MCFixup> &Fixups) const;
270 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
271 SmallVectorImpl<MCFixup> &Fixups) const;
273 /// getSORegOpValue - Return an encoded so_reg shifted register value.
274 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
276 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
278 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
279 SmallVectorImpl<MCFixup> &Fixups) const;
281 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const {
283 return 64 - MI.getOperand(Op).getImm();
286 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
289 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
293 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
295 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
297 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
302 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
304 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
306 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
307 SmallVectorImpl<MCFixup> &Fixups) const;
309 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
310 SmallVectorImpl<MCFixup> &Fixups) const;
312 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
313 unsigned EncodedValue) const;
314 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
315 unsigned EncodedValue) const;
316 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const;
318 unsigned NEONThumb2V8PostEncoder(const MCInst &MI,
319 unsigned EncodedValue) const;
321 unsigned VFPThumb2PostEncoder(const MCInst &MI,
322 unsigned EncodedValue) const;
324 void EmitByte(unsigned char C, raw_ostream &OS) const {
328 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
329 // Output the constant in little endian byte order.
330 for (unsigned i = 0; i != Size; ++i) {
331 EmitByte(Val & 255, OS);
336 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
337 SmallVectorImpl<MCFixup> &Fixups) const;
340 } // end anonymous namespace
342 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
343 const MCRegisterInfo &MRI,
344 const MCSubtargetInfo &STI,
346 return new ARMMCCodeEmitter(MCII, STI, Ctx);
349 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
350 /// instructions, and rewrite them to their Thumb2 form if we are currently in
352 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
353 unsigned EncodedValue) const {
355 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
356 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
358 unsigned Bit24 = EncodedValue & 0x01000000;
359 unsigned Bit28 = Bit24 << 4;
360 EncodedValue &= 0xEFFFFFFF;
361 EncodedValue |= Bit28;
362 EncodedValue |= 0x0F000000;
368 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
369 /// instructions, and rewrite them to their Thumb2 form if we are currently in
371 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
372 unsigned EncodedValue) const {
374 EncodedValue &= 0xF0FFFFFF;
375 EncodedValue |= 0x09000000;
381 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
382 /// instructions, and rewrite them to their Thumb2 form if we are currently in
384 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
385 unsigned EncodedValue) const {
387 EncodedValue &= 0x00FFFFFF;
388 EncodedValue |= 0xEE000000;
394 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
395 /// if we are in Thumb2.
396 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI,
397 unsigned EncodedValue) const {
399 EncodedValue |= 0xC000000; // Set bits 27-26
405 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
406 /// them to their Thumb2 form if we are currently in Thumb2 mode.
407 unsigned ARMMCCodeEmitter::
408 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
410 EncodedValue &= 0x0FFFFFFF;
411 EncodedValue |= 0xE0000000;
416 /// getMachineOpValue - Return binary encoding of operand. If the machine
417 /// operand requires relocation, record the relocation and return zero.
418 unsigned ARMMCCodeEmitter::
419 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
420 SmallVectorImpl<MCFixup> &Fixups) const {
422 unsigned Reg = MO.getReg();
423 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
425 // Q registers are encoded as 2x their register number.
429 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
430 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
431 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
432 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
435 } else if (MO.isImm()) {
436 return static_cast<unsigned>(MO.getImm());
437 } else if (MO.isFPImm()) {
438 return static_cast<unsigned>(APFloat(MO.getFPImm())
439 .bitcastToAPInt().getHiBits(32).getLimitedValue());
442 llvm_unreachable("Unable to encode MCOperand!");
445 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
446 bool ARMMCCodeEmitter::
447 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
448 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
449 const MCOperand &MO = MI.getOperand(OpIdx);
450 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
452 Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
454 int32_t SImm = MO1.getImm();
457 // Special value for #-0
458 if (SImm == INT32_MIN) {
463 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
473 /// getBranchTargetOpValue - Helper function to get the branch target operand,
474 /// which is either an immediate or requires a fixup.
475 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
477 SmallVectorImpl<MCFixup> &Fixups) {
478 const MCOperand &MO = MI.getOperand(OpIdx);
480 // If the destination is an immediate, we have nothing to do.
481 if (MO.isImm()) return MO.getImm();
482 assert(MO.isExpr() && "Unexpected branch target type!");
483 const MCExpr *Expr = MO.getExpr();
484 MCFixupKind Kind = MCFixupKind(FixupKind);
485 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
487 // All of the information is in the fixup.
491 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
492 // determined by negating them and XOR'ing them with bit 23.
493 static int32_t encodeThumbBLOffset(int32_t offset) {
495 uint32_t S = (offset & 0x800000) >> 23;
496 uint32_t J1 = (offset & 0x400000) >> 22;
497 uint32_t J2 = (offset & 0x200000) >> 21;
510 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
511 uint32_t ARMMCCodeEmitter::
512 getThumbBLTargetOpValue(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_bl,
518 return encodeThumbBLOffset(MO.getImm());
521 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
522 /// BLX branch target.
523 uint32_t ARMMCCodeEmitter::
524 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
525 SmallVectorImpl<MCFixup> &Fixups) const {
526 const MCOperand MO = MI.getOperand(OpIdx);
528 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
530 return encodeThumbBLOffset(MO.getImm());
533 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
534 uint32_t ARMMCCodeEmitter::
535 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
536 SmallVectorImpl<MCFixup> &Fixups) const {
537 const MCOperand MO = MI.getOperand(OpIdx);
539 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
541 return (MO.getImm() >> 1);
544 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
545 uint32_t ARMMCCodeEmitter::
546 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
547 SmallVectorImpl<MCFixup> &Fixups) const {
548 const MCOperand MO = MI.getOperand(OpIdx);
550 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
552 return (MO.getImm() >> 1);
555 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
556 uint32_t ARMMCCodeEmitter::
557 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
559 const MCOperand MO = MI.getOperand(OpIdx);
561 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
562 return (MO.getImm() >> 1);
565 /// Return true if this branch has a non-always predication
566 static bool HasConditionalBranch(const MCInst &MI) {
567 int NumOp = MI.getNumOperands();
569 for (int i = 0; i < NumOp-1; ++i) {
570 const MCOperand &MCOp1 = MI.getOperand(i);
571 const MCOperand &MCOp2 = MI.getOperand(i + 1);
572 if (MCOp1.isImm() && MCOp2.isReg() &&
573 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
574 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
582 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
584 uint32_t ARMMCCodeEmitter::
585 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
586 SmallVectorImpl<MCFixup> &Fixups) const {
587 // FIXME: This really, really shouldn't use TargetMachine. We don't want
588 // coupling between MC and TM anywhere we can help it.
591 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
592 return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
595 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
597 uint32_t ARMMCCodeEmitter::
598 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
599 SmallVectorImpl<MCFixup> &Fixups) const {
600 const MCOperand MO = MI.getOperand(OpIdx);
602 if (HasConditionalBranch(MI))
603 return ::getBranchTargetOpValue(MI, OpIdx,
604 ARM::fixup_arm_condbranch, Fixups);
605 return ::getBranchTargetOpValue(MI, OpIdx,
606 ARM::fixup_arm_uncondbranch, Fixups);
609 return MO.getImm() >> 2;
612 uint32_t ARMMCCodeEmitter::
613 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
614 SmallVectorImpl<MCFixup> &Fixups) const {
615 const MCOperand MO = MI.getOperand(OpIdx);
617 if (HasConditionalBranch(MI))
618 return ::getBranchTargetOpValue(MI, OpIdx,
619 ARM::fixup_arm_condbl, Fixups);
620 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups);
623 return MO.getImm() >> 2;
626 uint32_t ARMMCCodeEmitter::
627 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
628 SmallVectorImpl<MCFixup> &Fixups) const {
629 const MCOperand MO = MI.getOperand(OpIdx);
631 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups);
633 return MO.getImm() >> 1;
636 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
637 /// immediate branch target.
638 uint32_t ARMMCCodeEmitter::
639 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
640 SmallVectorImpl<MCFixup> &Fixups) const {
642 const MCOperand MO = MI.getOperand(OpIdx);
645 Val = ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
647 Val = MO.getImm() >> 1;
649 bool I = (Val & 0x800000);
650 bool J1 = (Val & 0x400000);
651 bool J2 = (Val & 0x200000);
665 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
666 /// ADR label target.
667 uint32_t ARMMCCodeEmitter::
668 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
669 SmallVectorImpl<MCFixup> &Fixups) const {
670 const MCOperand MO = MI.getOperand(OpIdx);
672 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
674 int64_t offset = MO.getImm();
675 uint32_t Val = 0x2000;
678 if (offset == INT32_MIN) {
681 } else if (offset < 0) {
684 SoImmVal = ARM_AM::getSOImmVal(offset);
688 SoImmVal = ARM_AM::getSOImmVal(offset);
691 SoImmVal = ARM_AM::getSOImmVal(offset);
695 SoImmVal = ARM_AM::getSOImmVal(offset);
699 assert(SoImmVal != -1 && "Not a valid so_imm value!");
705 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
707 uint32_t ARMMCCodeEmitter::
708 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
709 SmallVectorImpl<MCFixup> &Fixups) const {
710 const MCOperand MO = MI.getOperand(OpIdx);
712 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
714 int32_t Val = MO.getImm();
715 if (Val == INT32_MIN)
724 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
726 uint32_t ARMMCCodeEmitter::
727 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
728 SmallVectorImpl<MCFixup> &Fixups) const {
729 const MCOperand MO = MI.getOperand(OpIdx);
731 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
736 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
738 uint32_t ARMMCCodeEmitter::
739 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
740 SmallVectorImpl<MCFixup> &) const {
744 const MCOperand &MO1 = MI.getOperand(OpIdx);
745 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
746 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
747 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
748 return (Rm << 3) | Rn;
751 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
752 uint32_t ARMMCCodeEmitter::
753 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
754 SmallVectorImpl<MCFixup> &Fixups) const {
756 // {12} = (U)nsigned (add == '1', sub == '0')
760 // If The first operand isn't a register, we have a label reference.
761 const MCOperand &MO = MI.getOperand(OpIdx);
763 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
767 const MCExpr *Expr = MO.getExpr();
768 isAdd = false ; // 'U' bit is set as part of the fixup.
772 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
774 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
775 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
777 ++MCNumCPRelocations;
780 int32_t Offset = MO.getImm();
781 if (Offset == INT32_MIN) {
784 } else if (Offset < 0) {
791 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
793 uint32_t Binary = Imm12 & 0xfff;
794 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
797 Binary |= (Reg << 13);
801 /// getT2Imm8s4OpValue - Return encoding info for
802 /// '+/- imm8<<2' operand.
803 uint32_t ARMMCCodeEmitter::
804 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
805 SmallVectorImpl<MCFixup> &Fixups) const {
806 // FIXME: The immediate operand should have already been encoded like this
807 // before ever getting here. The encoder method should just need to combine
808 // the MI operands for the register and the offset into a single
809 // representation for the complex operand in the .td file. This isn't just
810 // style, unfortunately. As-is, we can't represent the distinct encoding
813 // {8} = (U)nsigned (add == '1', sub == '0')
815 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
816 bool isAdd = Imm8 >= 0;
818 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
820 Imm8 = -(uint32_t)Imm8;
825 uint32_t Binary = Imm8 & 0xff;
826 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
832 /// getT2AddrModeImm8s4OpValue - Return encoding info for
833 /// 'reg +/- imm8<<2' operand.
834 uint32_t ARMMCCodeEmitter::
835 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
836 SmallVectorImpl<MCFixup> &Fixups) const {
838 // {8} = (U)nsigned (add == '1', sub == '0')
842 // If The first operand isn't a register, we have a label reference.
843 const MCOperand &MO = MI.getOperand(OpIdx);
845 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
847 isAdd = false ; // 'U' bit is set as part of the fixup.
849 assert(MO.isExpr() && "Unexpected machine operand type!");
850 const MCExpr *Expr = MO.getExpr();
851 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
852 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
854 ++MCNumCPRelocations;
856 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
858 // FIXME: The immediate operand should have already been encoded like this
859 // before ever getting here. The encoder method should just need to combine
860 // the MI operands for the register and the offset into a single
861 // representation for the complex operand in the .td file. This isn't just
862 // style, unfortunately. As-is, we can't represent the distinct encoding
864 uint32_t Binary = (Imm8 >> 2) & 0xff;
865 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
868 Binary |= (Reg << 9);
872 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
873 /// 'reg + imm8<<2' operand.
874 uint32_t ARMMCCodeEmitter::
875 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
876 SmallVectorImpl<MCFixup> &Fixups) const {
879 const MCOperand &MO = MI.getOperand(OpIdx);
880 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
881 unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
882 unsigned Imm8 = MO1.getImm();
883 return (Reg << 8) | Imm8;
886 // FIXME: This routine assumes that a binary
887 // expression will always result in a PCRel expression
888 // In reality, its only true if one or more subexpressions
889 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
890 // but this is good enough for now.
891 static bool EvaluateAsPCRel(const MCExpr *Expr) {
892 switch (Expr->getKind()) {
893 default: llvm_unreachable("Unexpected expression type");
894 case MCExpr::SymbolRef: return false;
895 case MCExpr::Binary: return true;
900 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
901 SmallVectorImpl<MCFixup> &Fixups) const {
902 // {20-16} = imm{15-12}
903 // {11-0} = imm{11-0}
904 const MCOperand &MO = MI.getOperand(OpIdx);
906 // Hi / lo 16 bits already extracted during earlier passes.
907 return static_cast<unsigned>(MO.getImm());
909 // Handle :upper16: and :lower16: assembly prefixes.
910 const MCExpr *E = MO.getExpr();
912 if (E->getKind() == MCExpr::Target) {
913 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
914 E = ARM16Expr->getSubExpr();
916 switch (ARM16Expr->getKind()) {
917 default: llvm_unreachable("Unsupported ARMFixup");
918 case ARMMCExpr::VK_ARM_HI16:
919 if (!isTargetDarwin() && EvaluateAsPCRel(E))
920 Kind = MCFixupKind(isThumb2()
921 ? ARM::fixup_t2_movt_hi16_pcrel
922 : ARM::fixup_arm_movt_hi16_pcrel);
924 Kind = MCFixupKind(isThumb2()
925 ? ARM::fixup_t2_movt_hi16
926 : ARM::fixup_arm_movt_hi16);
928 case ARMMCExpr::VK_ARM_LO16:
929 if (!isTargetDarwin() && EvaluateAsPCRel(E))
930 Kind = MCFixupKind(isThumb2()
931 ? ARM::fixup_t2_movw_lo16_pcrel
932 : ARM::fixup_arm_movw_lo16_pcrel);
934 Kind = MCFixupKind(isThumb2()
935 ? ARM::fixup_t2_movw_lo16
936 : ARM::fixup_arm_movw_lo16);
939 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
942 // If the expression doesn't have :upper16: or :lower16: on it,
943 // it's just a plain immediate expression, and those evaluate to
944 // the lower 16 bits of the expression regardless of whether
945 // we have a movt or a movw.
946 if (!isTargetDarwin() && EvaluateAsPCRel(E))
947 Kind = MCFixupKind(isThumb2()
948 ? ARM::fixup_t2_movw_lo16_pcrel
949 : ARM::fixup_arm_movw_lo16_pcrel);
951 Kind = MCFixupKind(isThumb2()
952 ? ARM::fixup_t2_movw_lo16
953 : ARM::fixup_arm_movw_lo16);
954 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
958 uint32_t ARMMCCodeEmitter::
959 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
960 SmallVectorImpl<MCFixup> &Fixups) const {
961 const MCOperand &MO = MI.getOperand(OpIdx);
962 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
963 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
964 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
965 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
966 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
967 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
968 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
969 unsigned SBits = getShiftOp(ShOp);
971 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
972 // amount. However, it would be an easy mistake to make so check here.
973 assert((ShImm & ~0x1f) == 0 && "Out of range shift amount");
982 uint32_t Binary = Rm;
984 Binary |= SBits << 5;
985 Binary |= ShImm << 7;
991 uint32_t ARMMCCodeEmitter::
992 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
993 SmallVectorImpl<MCFixup> &Fixups) const {
995 // {13} 1 == imm12, 0 == Rm
998 const MCOperand &MO = MI.getOperand(OpIdx);
999 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1000 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
1005 uint32_t ARMMCCodeEmitter::
1006 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1007 SmallVectorImpl<MCFixup> &Fixups) const {
1008 // {13} 1 == imm12, 0 == Rm
1011 const MCOperand &MO = MI.getOperand(OpIdx);
1012 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1013 unsigned Imm = MO1.getImm();
1014 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
1015 bool isReg = MO.getReg() != 0;
1016 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
1017 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1019 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
1020 Binary <<= 7; // Shift amount is bits [11:7]
1021 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
1022 Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0]
1024 return Binary | (isAdd << 12) | (isReg << 13);
1027 uint32_t ARMMCCodeEmitter::
1028 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
1029 SmallVectorImpl<MCFixup> &Fixups) const {
1032 const MCOperand &MO = MI.getOperand(OpIdx);
1033 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1034 bool isAdd = MO1.getImm() != 0;
1035 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4);
1038 uint32_t ARMMCCodeEmitter::
1039 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1040 SmallVectorImpl<MCFixup> &Fixups) const {
1041 // {9} 1 == imm8, 0 == Rm
1043 // {7-4} imm7_4/zero
1045 const MCOperand &MO = MI.getOperand(OpIdx);
1046 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1047 unsigned Imm = MO1.getImm();
1048 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1049 bool isImm = MO.getReg() == 0;
1050 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1051 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1053 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1054 return Imm8 | (isAdd << 8) | (isImm << 9);
1057 uint32_t ARMMCCodeEmitter::
1058 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1059 SmallVectorImpl<MCFixup> &Fixups) const {
1060 // {13} 1 == imm8, 0 == Rm
1063 // {7-4} imm7_4/zero
1065 const MCOperand &MO = MI.getOperand(OpIdx);
1066 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1067 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1069 // If The first operand isn't a register, we have a label reference.
1071 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1073 assert(MO.isExpr() && "Unexpected machine operand type!");
1074 const MCExpr *Expr = MO.getExpr();
1075 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1076 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1078 ++MCNumCPRelocations;
1079 return (Rn << 9) | (1 << 13);
1081 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1082 unsigned Imm = MO2.getImm();
1083 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1084 bool isImm = MO1.getReg() == 0;
1085 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1086 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1088 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1089 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1092 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1093 uint32_t ARMMCCodeEmitter::
1094 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1095 SmallVectorImpl<MCFixup> &Fixups) const {
1098 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1099 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1100 "Unexpected base register!");
1102 // The immediate is already shifted for the implicit zeroes, so no change
1104 return MO1.getImm() & 0xff;
1107 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1108 uint32_t ARMMCCodeEmitter::
1109 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1110 SmallVectorImpl<MCFixup> &Fixups) const {
1114 const MCOperand &MO = MI.getOperand(OpIdx);
1115 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1116 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1117 unsigned Imm5 = MO1.getImm();
1118 return ((Imm5 & 0x1f) << 3) | Rn;
1121 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1122 uint32_t ARMMCCodeEmitter::
1123 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1124 SmallVectorImpl<MCFixup> &Fixups) const {
1125 const MCOperand MO = MI.getOperand(OpIdx);
1127 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1128 return (MO.getImm() >> 2);
1131 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1132 uint32_t ARMMCCodeEmitter::
1133 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1134 SmallVectorImpl<MCFixup> &Fixups) const {
1136 // {8} = (U)nsigned (add == '1', sub == '0')
1140 // If The first operand isn't a register, we have a label reference.
1141 const MCOperand &MO = MI.getOperand(OpIdx);
1143 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1145 isAdd = false; // 'U' bit is handled as part of the fixup.
1147 assert(MO.isExpr() && "Unexpected machine operand type!");
1148 const MCExpr *Expr = MO.getExpr();
1151 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1153 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1154 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1156 ++MCNumCPRelocations;
1158 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
1159 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1162 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1163 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1166 Binary |= (Reg << 9);
1170 unsigned ARMMCCodeEmitter::
1171 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1172 SmallVectorImpl<MCFixup> &Fixups) const {
1173 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1174 // shifted. The second is Rs, the amount to shift by, and the third specifies
1175 // the type of the shift.
1183 const MCOperand &MO = MI.getOperand(OpIdx);
1184 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1185 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1186 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1189 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1191 // Encode the shift opcode.
1193 unsigned Rs = MO1.getReg();
1195 // Set shift operand (bit[7:4]).
1201 default: llvm_unreachable("Unknown shift opc!");
1202 case ARM_AM::lsl: SBits = 0x1; break;
1203 case ARM_AM::lsr: SBits = 0x3; break;
1204 case ARM_AM::asr: SBits = 0x5; break;
1205 case ARM_AM::ror: SBits = 0x7; break;
1209 Binary |= SBits << 4;
1211 // Encode the shift operation Rs.
1212 // Encode Rs bit[11:8].
1213 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1214 return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift);
1217 unsigned ARMMCCodeEmitter::
1218 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1219 SmallVectorImpl<MCFixup> &Fixups) const {
1220 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1221 // shifted. The second is the amount to shift by.
1228 const MCOperand &MO = MI.getOperand(OpIdx);
1229 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1230 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1233 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1235 // Encode the shift opcode.
1238 // Set shift operand (bit[6:4]).
1243 // RRX - 110 and bit[11:8] clear.
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;
1255 // Encode shift_imm bit[11:7].
1256 Binary |= SBits << 4;
1257 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1258 assert(Offset < 32 && "Offset must be in range 0-31!");
1259 return Binary | (Offset << 7);
1263 unsigned ARMMCCodeEmitter::
1264 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1265 SmallVectorImpl<MCFixup> &Fixups) const {
1266 const MCOperand &MO1 = MI.getOperand(OpNum);
1267 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1268 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1270 // Encoded as [Rn, Rm, imm].
1271 // FIXME: Needs fixup support.
1272 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1274 Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
1276 Value |= MO3.getImm();
1281 unsigned ARMMCCodeEmitter::
1282 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1283 SmallVectorImpl<MCFixup> &Fixups) const {
1284 const MCOperand &MO1 = MI.getOperand(OpNum);
1285 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1287 // FIXME: Needs fixup support.
1288 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1290 // Even though the immediate is 8 bits long, we need 9 bits in order
1291 // to represent the (inverse of the) sign bit.
1293 int32_t tmp = (int32_t)MO2.getImm();
1297 Value |= 256; // Set the ADD bit
1302 unsigned ARMMCCodeEmitter::
1303 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1304 SmallVectorImpl<MCFixup> &Fixups) const {
1305 const MCOperand &MO1 = MI.getOperand(OpNum);
1307 // FIXME: Needs fixup support.
1309 int32_t tmp = (int32_t)MO1.getImm();
1313 Value |= 256; // Set the ADD bit
1318 unsigned ARMMCCodeEmitter::
1319 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1320 SmallVectorImpl<MCFixup> &Fixups) const {
1321 const MCOperand &MO1 = MI.getOperand(OpNum);
1323 // FIXME: Needs fixup support.
1325 int32_t tmp = (int32_t)MO1.getImm();
1329 Value |= 4096; // Set the ADD bit
1330 Value |= tmp & 4095;
1334 unsigned ARMMCCodeEmitter::
1335 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1336 SmallVectorImpl<MCFixup> &Fixups) const {
1337 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1338 // shifted. The second is the amount to shift by.
1345 const MCOperand &MO = MI.getOperand(OpIdx);
1346 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1347 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1350 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1352 // Encode the shift opcode.
1354 // Set shift operand (bit[6:4]).
1360 default: llvm_unreachable("Unknown shift opc!");
1361 case ARM_AM::lsl: SBits = 0x0; break;
1362 case ARM_AM::lsr: SBits = 0x2; break;
1363 case ARM_AM::asr: SBits = 0x4; break;
1364 case ARM_AM::rrx: // FALLTHROUGH
1365 case ARM_AM::ror: SBits = 0x6; break;
1368 Binary |= SBits << 4;
1369 if (SOpc == ARM_AM::rrx)
1372 // Encode shift_imm bit[11:7].
1373 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1376 unsigned ARMMCCodeEmitter::
1377 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1378 SmallVectorImpl<MCFixup> &Fixups) const {
1379 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1381 const MCOperand &MO = MI.getOperand(Op);
1382 uint32_t v = ~MO.getImm();
1383 uint32_t lsb = countTrailingZeros(v);
1384 uint32_t msb = (32 - countLeadingZeros (v)) - 1;
1385 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1386 return lsb | (msb << 5);
1389 unsigned ARMMCCodeEmitter::
1390 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1391 SmallVectorImpl<MCFixup> &Fixups) const {
1394 // {7-0} = Number of registers
1397 // {15-0} = Bitfield of GPRs.
1398 unsigned Reg = MI.getOperand(Op).getReg();
1399 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1400 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1402 unsigned Binary = 0;
1404 if (SPRRegs || DPRRegs) {
1406 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
1407 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1408 Binary |= (RegNo & 0x1f) << 8;
1412 Binary |= NumRegs * 2;
1414 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1415 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(MI.getOperand(I).getReg());
1416 Binary |= 1 << RegNo;
1423 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1424 /// with the alignment operand.
1425 unsigned ARMMCCodeEmitter::
1426 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1427 SmallVectorImpl<MCFixup> &Fixups) const {
1428 const MCOperand &Reg = MI.getOperand(Op);
1429 const MCOperand &Imm = MI.getOperand(Op + 1);
1431 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1434 switch (Imm.getImm()) {
1438 case 8: Align = 0x01; break;
1439 case 16: Align = 0x02; break;
1440 case 32: Align = 0x03; break;
1443 return RegNo | (Align << 4);
1446 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1447 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1448 unsigned ARMMCCodeEmitter::
1449 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1450 SmallVectorImpl<MCFixup> &Fixups) const {
1451 const MCOperand &Reg = MI.getOperand(Op);
1452 const MCOperand &Imm = MI.getOperand(Op + 1);
1454 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1457 switch (Imm.getImm()) {
1461 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1462 case 2: Align = 0x00; break;
1463 case 4: Align = 0x03; break;
1466 return RegNo | (Align << 4);
1470 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1471 /// alignment operand for use in VLD-dup instructions. This is the same as
1472 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1473 /// different for VLD4-dup.
1474 unsigned ARMMCCodeEmitter::
1475 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1476 SmallVectorImpl<MCFixup> &Fixups) const {
1477 const MCOperand &Reg = MI.getOperand(Op);
1478 const MCOperand &Imm = MI.getOperand(Op + 1);
1480 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1483 switch (Imm.getImm()) {
1487 case 8: Align = 0x01; break;
1488 case 16: Align = 0x03; break;
1491 return RegNo | (Align << 4);
1494 unsigned ARMMCCodeEmitter::
1495 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1496 SmallVectorImpl<MCFixup> &Fixups) const {
1497 const MCOperand &MO = MI.getOperand(Op);
1498 if (MO.getReg() == 0) return 0x0D;
1499 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1502 unsigned ARMMCCodeEmitter::
1503 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1504 SmallVectorImpl<MCFixup> &Fixups) const {
1505 return 8 - MI.getOperand(Op).getImm();
1508 unsigned ARMMCCodeEmitter::
1509 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1510 SmallVectorImpl<MCFixup> &Fixups) const {
1511 return 16 - MI.getOperand(Op).getImm();
1514 unsigned ARMMCCodeEmitter::
1515 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1516 SmallVectorImpl<MCFixup> &Fixups) const {
1517 return 32 - MI.getOperand(Op).getImm();
1520 unsigned ARMMCCodeEmitter::
1521 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1522 SmallVectorImpl<MCFixup> &Fixups) const {
1523 return 64 - MI.getOperand(Op).getImm();
1526 void ARMMCCodeEmitter::
1527 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1528 SmallVectorImpl<MCFixup> &Fixups) const {
1529 // Pseudo instructions don't get encoded.
1530 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1531 uint64_t TSFlags = Desc.TSFlags;
1532 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1536 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1537 Size = Desc.getSize();
1539 llvm_unreachable("Unexpected instruction size!");
1541 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1542 // Thumb 32-bit wide instructions need to emit the high order halfword
1544 if (isThumb() && Size == 4) {
1545 EmitConstant(Binary >> 16, 2, OS);
1546 EmitConstant(Binary & 0xffff, 2, OS);
1548 EmitConstant(Binary, Size, OS);
1549 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1552 #include "ARMGenMCCodeEmitter.inc"