1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mccodeemitter"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/raw_ostream.h"
26 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
30 class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
42 ~ARMMCCodeEmitter() {}
44 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
48 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
52 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
54 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_movt_hi16", 0, 16, 0 },
56 { "fixup_arm_movw_lo16", 0, 16, 0 },
59 if (Kind < FirstTargetFixupKind)
60 return MCCodeEmitter::getFixupKindInfo(Kind);
62 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
64 return Infos[Kind - FirstTargetFixupKind];
66 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
68 // getBinaryCodeForInstr - TableGen'erated function for getting the
69 // binary encoding for an instruction.
70 unsigned getBinaryCodeForInstr(const MCInst &MI,
71 SmallVectorImpl<MCFixup> &Fixups) const;
73 /// getMachineOpValue - Return binary encoding of operand. If the machine
74 /// operand requires relocation, record the relocation and return zero.
75 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76 SmallVectorImpl<MCFixup> &Fixups) const;
78 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
79 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups) const;
82 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
83 unsigned &Reg, unsigned &Imm,
84 SmallVectorImpl<MCFixup> &Fixups) const;
86 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
88 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
91 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
93 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
96 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
98 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
101 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
103 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
106 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
108 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
112 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
113 /// operand as needed by load/store instructions.
114 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
117 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
118 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const {
120 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
122 default: assert(0 && "Unknown addressing sub-mode!");
123 case ARM_AM::da: return 0;
124 case ARM_AM::ia: return 1;
125 case ARM_AM::db: return 2;
126 case ARM_AM::ib: return 3;
129 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
131 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
133 default: llvm_unreachable("Unknown shift opc!");
134 case ARM_AM::no_shift:
135 case ARM_AM::lsl: return 0;
136 case ARM_AM::lsr: return 1;
137 case ARM_AM::asr: return 2;
139 case ARM_AM::rrx: return 3;
144 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
145 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
146 SmallVectorImpl<MCFixup> &Fixups) const;
148 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
149 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
152 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
153 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups) const;
156 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
157 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
160 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
162 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
165 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
166 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
169 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
170 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
173 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
174 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
175 SmallVectorImpl<MCFixup> &Fixups) const;
177 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
178 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
181 /// getCCOutOpValue - Return encoding of the 's' bit.
182 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
183 SmallVectorImpl<MCFixup> &Fixups) const {
184 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
186 return MI.getOperand(Op).getReg() == ARM::CPSR;
189 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
190 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
191 SmallVectorImpl<MCFixup> &Fixups) const {
192 unsigned SoImm = MI.getOperand(Op).getImm();
193 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
194 assert(SoImmVal != -1 && "Not a valid so_imm value!");
196 // Encode rotate_imm.
197 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
198 << ARMII::SoRotImmShift;
201 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
205 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
206 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const {
208 unsigned SoImm = MI.getOperand(Op).getImm();
209 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
210 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
214 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
215 SmallVectorImpl<MCFixup> &Fixups) const;
216 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
217 SmallVectorImpl<MCFixup> &Fixups) const;
218 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
219 SmallVectorImpl<MCFixup> &Fixups) const;
220 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
221 SmallVectorImpl<MCFixup> &Fixups) const;
223 /// getSORegOpValue - Return an encoded so_reg shifted register value.
224 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const;
226 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const;
229 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const {
231 switch (MI.getOperand(Op).getImm()) {
232 default: assert (0 && "Not a valid rot_imm value!");
240 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
241 SmallVectorImpl<MCFixup> &Fixups) const {
242 return MI.getOperand(Op).getImm() - 1;
245 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
246 SmallVectorImpl<MCFixup> &Fixups) const {
247 return 64 - MI.getOperand(Op).getImm();
250 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const;
253 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
254 SmallVectorImpl<MCFixup> &Fixups) const;
255 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const;
257 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
258 SmallVectorImpl<MCFixup> &Fixups) const;
259 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
262 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
263 unsigned EncodedValue) const;
264 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
265 unsigned EncodedValue) const;
266 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
267 unsigned EncodedValue) const;
269 unsigned VFPThumb2PostEncoder(const MCInst &MI,
270 unsigned EncodedValue) const;
272 void EmitByte(unsigned char C, raw_ostream &OS) const {
276 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
277 // Output the constant in little endian byte order.
278 for (unsigned i = 0; i != Size; ++i) {
279 EmitByte(Val & 255, OS);
284 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
285 SmallVectorImpl<MCFixup> &Fixups) const;
288 } // end anonymous namespace
290 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
292 return new ARMMCCodeEmitter(TM, Ctx);
295 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
296 /// instructions, and rewrite them to their Thumb2 form if we are currently in
298 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
299 unsigned EncodedValue) const {
300 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
301 if (Subtarget.isThumb2()) {
302 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
303 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
305 unsigned Bit24 = EncodedValue & 0x01000000;
306 unsigned Bit28 = Bit24 << 4;
307 EncodedValue &= 0xEFFFFFFF;
308 EncodedValue |= Bit28;
309 EncodedValue |= 0x0F000000;
315 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
316 /// instructions, and rewrite them to their Thumb2 form if we are currently in
318 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
319 unsigned EncodedValue) const {
320 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
321 if (Subtarget.isThumb2()) {
322 EncodedValue &= 0xF0FFFFFF;
323 EncodedValue |= 0x09000000;
329 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
330 /// instructions, and rewrite them to their Thumb2 form if we are currently in
332 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
333 unsigned EncodedValue) const {
334 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
335 if (Subtarget.isThumb2()) {
336 EncodedValue &= 0x00FFFFFF;
337 EncodedValue |= 0xEE000000;
343 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
344 /// them to their Thumb2 form if we are currently in Thumb2 mode.
345 unsigned ARMMCCodeEmitter::
346 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
347 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
348 EncodedValue &= 0x0FFFFFFF;
349 EncodedValue |= 0xE0000000;
354 /// getMachineOpValue - Return binary encoding of operand. If the machine
355 /// operand requires relocation, record the relocation and return zero.
356 unsigned ARMMCCodeEmitter::
357 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
358 SmallVectorImpl<MCFixup> &Fixups) const {
360 unsigned Reg = MO.getReg();
361 unsigned RegNo = getARMRegisterNumbering(Reg);
363 // Q registers are encoded as 2x their register number.
367 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
368 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
369 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
370 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
373 } else if (MO.isImm()) {
374 return static_cast<unsigned>(MO.getImm());
375 } else if (MO.isFPImm()) {
376 return static_cast<unsigned>(APFloat(MO.getFPImm())
377 .bitcastToAPInt().getHiBits(32).getLimitedValue());
380 llvm_unreachable("Unable to encode MCOperand!");
384 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
385 bool ARMMCCodeEmitter::
386 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
387 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
388 const MCOperand &MO = MI.getOperand(OpIdx);
389 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
391 Reg = getARMRegisterNumbering(MO.getReg());
393 int32_t SImm = MO1.getImm();
396 // Special value for #-0
397 if (SImm == INT32_MIN)
400 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
410 /// getThumbBLTargetOpValue - Return encoding info for immediate
412 uint32_t ARMMCCodeEmitter::
413 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
414 SmallVectorImpl<MCFixup> &Fixups) const {
415 const MCOperand &MO = MI.getOperand(OpIdx);
417 // If the destination is an immediate, we have nothing to do.
418 if (MO.isImm()) return MO.getImm();
419 assert (MO.isExpr() && "Unexpected branch target type!");
420 const MCExpr *Expr = MO.getExpr();
421 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_bl);
422 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
424 // All of the information is in the fixup.
428 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
430 uint32_t ARMMCCodeEmitter::
431 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
432 SmallVectorImpl<MCFixup> &Fixups) const {
433 const MCOperand &MO = MI.getOperand(OpIdx);
435 // If the destination is an immediate, we have nothing to do.
436 if (MO.isImm()) return MO.getImm();
437 assert (MO.isExpr() && "Unexpected branch target type!");
438 const MCExpr *Expr = MO.getExpr();
439 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
440 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
442 // All of the information is in the fixup.
446 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
447 /// ADR label target.
448 uint32_t ARMMCCodeEmitter::
449 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
450 SmallVectorImpl<MCFixup> &Fixups) const {
451 const MCOperand &MO = MI.getOperand(OpIdx);
452 assert (MO.isExpr() && "Unexpected adr target type!");
453 const MCExpr *Expr = MO.getExpr();
454 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_adr_pcrel_12);
455 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
456 // All of the information is in the fixup.
460 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
461 uint32_t ARMMCCodeEmitter::
462 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
463 SmallVectorImpl<MCFixup> &Fixups) const {
465 // {12} = (U)nsigned (add == '1', sub == '0')
469 // If The first operand isn't a register, we have a label reference.
470 const MCOperand &MO = MI.getOperand(OpIdx);
471 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
472 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
473 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
475 isAdd = false ; // 'U' bit is set as part of the fixup.
477 const MCExpr *Expr = 0;
481 Expr = MO2.getExpr();
483 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
484 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
486 ++MCNumCPRelocations;
488 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
490 uint32_t Binary = Imm12 & 0xfff;
491 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
494 Binary |= (Reg << 13);
498 /// getT2AddrModeImm8s4OpValue - Return encoding info for
499 /// 'reg +/- imm8<<2' operand.
500 uint32_t ARMMCCodeEmitter::
501 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
504 // {12} = (U)nsigned (add == '1', sub == '0')
508 // If The first operand isn't a register, we have a label reference.
509 const MCOperand &MO = MI.getOperand(OpIdx);
511 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
513 isAdd = false ; // 'U' bit is set as part of the fixup.
515 assert(MO.isExpr() && "Unexpected machine operand type!");
516 const MCExpr *Expr = MO.getExpr();
517 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
518 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
520 ++MCNumCPRelocations;
522 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
524 uint32_t Binary = (Imm8 >> 2) & 0xff;
525 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
528 Binary |= (Reg << 9);
532 uint32_t ARMMCCodeEmitter::
533 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
534 SmallVectorImpl<MCFixup> &Fixups) const {
535 // {20-16} = imm{15-12}
536 // {11-0} = imm{11-0}
537 const MCOperand &MO = MI.getOperand(OpIdx);
539 return static_cast<unsigned>(MO.getImm());
540 } else if (const MCSymbolRefExpr *Expr =
541 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
543 switch (Expr->getKind()) {
544 default: assert(0 && "Unsupported ARMFixup");
545 case MCSymbolRefExpr::VK_ARM_HI16:
546 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
548 case MCSymbolRefExpr::VK_ARM_LO16:
549 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
552 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
555 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
559 uint32_t ARMMCCodeEmitter::
560 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
561 SmallVectorImpl<MCFixup> &Fixups) const {
562 const MCOperand &MO = MI.getOperand(OpIdx);
563 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
564 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
565 unsigned Rn = getARMRegisterNumbering(MO.getReg());
566 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
567 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
568 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
569 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
570 unsigned SBits = getShiftOp(ShOp);
579 uint32_t Binary = Rm;
581 Binary |= SBits << 5;
582 Binary |= ShImm << 7;
588 uint32_t ARMMCCodeEmitter::
589 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
590 SmallVectorImpl<MCFixup> &Fixups) const {
592 // {13} 1 == imm12, 0 == Rm
595 const MCOperand &MO = MI.getOperand(OpIdx);
596 unsigned Rn = getARMRegisterNumbering(MO.getReg());
597 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
602 uint32_t ARMMCCodeEmitter::
603 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
604 SmallVectorImpl<MCFixup> &Fixups) const {
605 // {13} 1 == imm12, 0 == Rm
608 const MCOperand &MO = MI.getOperand(OpIdx);
609 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
610 unsigned Imm = MO1.getImm();
611 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
612 bool isReg = MO.getReg() != 0;
613 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
614 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
616 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
617 Binary <<= 7; // Shift amount is bits [11:7]
618 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
619 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
621 return Binary | (isAdd << 12) | (isReg << 13);
624 uint32_t ARMMCCodeEmitter::
625 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
626 SmallVectorImpl<MCFixup> &Fixups) const {
627 // {9} 1 == imm8, 0 == Rm
631 const MCOperand &MO = MI.getOperand(OpIdx);
632 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
633 unsigned Imm = MO1.getImm();
634 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
635 bool isImm = MO.getReg() == 0;
636 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
637 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
639 Imm8 = getARMRegisterNumbering(MO.getReg());
640 return Imm8 | (isAdd << 8) | (isImm << 9);
643 uint32_t ARMMCCodeEmitter::
644 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
645 SmallVectorImpl<MCFixup> &Fixups) const {
646 // {13} 1 == imm8, 0 == Rm
651 const MCOperand &MO = MI.getOperand(OpIdx);
652 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
653 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
654 unsigned Rn = getARMRegisterNumbering(MO.getReg());
655 unsigned Imm = MO2.getImm();
656 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
657 bool isImm = MO1.getReg() == 0;
658 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
659 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
661 Imm8 = getARMRegisterNumbering(MO1.getReg());
662 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
665 /// getAddrModeThumbSPOpValue- Encode the t_addrmode_sp operands.
666 uint32_t ARMMCCodeEmitter::
667 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
668 SmallVectorImpl<MCFixup> &Fixups) const {
671 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
672 assert (MI.getOperand(OpIdx).getReg() == ARM::SP &&
673 "Unexpected base register!");
674 // The immediate is already shifted for the implicit zeroes, so no change
676 return MO1.getImm() & 0xff;
679 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
680 static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
689 const MCOperand &MO = MI.getOperand(OpIdx);
690 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
691 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
692 unsigned Rn = getARMRegisterNumbering(MO.getReg());
693 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
695 if (MO2.getReg() != 0)
697 Imm5 = getARMRegisterNumbering(MO2.getReg());
699 return (Imm5 << 3) | Rn;
702 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
703 uint32_t ARMMCCodeEmitter::
704 getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
705 SmallVectorImpl<MCFixup> &) const {
706 return getAddrModeSOpValue(MI, OpIdx, 4);
709 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
710 uint32_t ARMMCCodeEmitter::
711 getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &) const {
713 return getAddrModeSOpValue(MI, OpIdx, 2);
716 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
717 uint32_t ARMMCCodeEmitter::
718 getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
719 SmallVectorImpl<MCFixup> &) const {
720 return getAddrModeSOpValue(MI, OpIdx, 1);
723 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
724 uint32_t ARMMCCodeEmitter::
725 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &Fixups) const {
728 // {8} = (U)nsigned (add == '1', sub == '0')
732 // If The first operand isn't a register, we have a label reference.
733 const MCOperand &MO = MI.getOperand(OpIdx);
735 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
737 isAdd = false; // 'U' bit is handled as part of the fixup.
739 assert(MO.isExpr() && "Unexpected machine operand type!");
740 const MCExpr *Expr = MO.getExpr();
742 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
743 if (Subtarget.isThumb2())
744 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
746 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
747 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
749 ++MCNumCPRelocations;
751 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
752 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
755 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
756 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
759 Binary |= (Reg << 9);
763 unsigned ARMMCCodeEmitter::
764 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
765 SmallVectorImpl<MCFixup> &Fixups) const {
766 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
767 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
768 // case the imm contains the amount to shift by.
771 // {4} = 1 if reg shift, 0 if imm shift
779 const MCOperand &MO = MI.getOperand(OpIdx);
780 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
781 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
782 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
785 unsigned Binary = getARMRegisterNumbering(MO.getReg());
787 // Encode the shift opcode.
789 unsigned Rs = MO1.getReg();
791 // Set shift operand (bit[7:4]).
796 // RRX - 0110 and bit[11:8] clear.
798 default: llvm_unreachable("Unknown shift opc!");
799 case ARM_AM::lsl: SBits = 0x1; break;
800 case ARM_AM::lsr: SBits = 0x3; break;
801 case ARM_AM::asr: SBits = 0x5; break;
802 case ARM_AM::ror: SBits = 0x7; break;
803 case ARM_AM::rrx: SBits = 0x6; break;
806 // Set shift operand (bit[6:4]).
812 default: llvm_unreachable("Unknown shift opc!");
813 case ARM_AM::lsl: SBits = 0x0; break;
814 case ARM_AM::lsr: SBits = 0x2; break;
815 case ARM_AM::asr: SBits = 0x4; break;
816 case ARM_AM::ror: SBits = 0x6; break;
820 Binary |= SBits << 4;
821 if (SOpc == ARM_AM::rrx)
824 // Encode the shift operation Rs or shift_imm (except rrx).
826 // Encode Rs bit[11:8].
827 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
828 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
831 // Encode shift_imm bit[11:7].
832 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
835 unsigned ARMMCCodeEmitter::
836 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
837 SmallVectorImpl<MCFixup> &Fixups) const {
838 const MCOperand &MO1 = MI.getOperand(OpNum);
839 const MCOperand &MO2 = MI.getOperand(OpNum+1);
840 const MCOperand &MO3 = MI.getOperand(OpNum+2);
842 // Encoded as [Rn, Rm, imm].
843 // FIXME: Needs fixup support.
844 unsigned Value = getARMRegisterNumbering(MO1.getReg());
846 Value |= getARMRegisterNumbering(MO2.getReg());
848 Value |= MO3.getImm();
853 unsigned ARMMCCodeEmitter::
854 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
855 SmallVectorImpl<MCFixup> &Fixups) const {
856 const MCOperand &MO1 = MI.getOperand(OpNum);
857 const MCOperand &MO2 = MI.getOperand(OpNum+1);
859 // FIXME: Needs fixup support.
860 unsigned Value = getARMRegisterNumbering(MO1.getReg());
862 // Even though the immediate is 8 bits long, we need 9 bits in order
863 // to represent the (inverse of the) sign bit.
865 int32_t tmp = (int32_t)MO2.getImm();
869 Value |= 256; // Set the ADD bit
874 unsigned ARMMCCodeEmitter::
875 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
876 SmallVectorImpl<MCFixup> &Fixups) const {
877 const MCOperand &MO1 = MI.getOperand(OpNum);
879 // FIXME: Needs fixup support.
881 int32_t tmp = (int32_t)MO1.getImm();
885 Value |= 256; // Set the ADD bit
890 unsigned ARMMCCodeEmitter::
891 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
892 SmallVectorImpl<MCFixup> &Fixups) const {
893 const MCOperand &MO1 = MI.getOperand(OpNum);
895 // FIXME: Needs fixup support.
897 int32_t tmp = (int32_t)MO1.getImm();
901 Value |= 4096; // Set the ADD bit
906 unsigned ARMMCCodeEmitter::
907 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
908 SmallVectorImpl<MCFixup> &Fixups) const {
909 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
910 // shifted. The second is the amount to shift by.
917 const MCOperand &MO = MI.getOperand(OpIdx);
918 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
919 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
922 unsigned Binary = getARMRegisterNumbering(MO.getReg());
924 // Encode the shift opcode.
926 // Set shift operand (bit[6:4]).
932 default: llvm_unreachable("Unknown shift opc!");
933 case ARM_AM::lsl: SBits = 0x0; break;
934 case ARM_AM::lsr: SBits = 0x2; break;
935 case ARM_AM::asr: SBits = 0x4; break;
936 case ARM_AM::ror: SBits = 0x6; break;
939 Binary |= SBits << 4;
940 if (SOpc == ARM_AM::rrx)
943 // Encode shift_imm bit[11:7].
944 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
947 unsigned ARMMCCodeEmitter::
948 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
949 SmallVectorImpl<MCFixup> &Fixups) const {
950 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
952 const MCOperand &MO = MI.getOperand(Op);
953 uint32_t v = ~MO.getImm();
954 uint32_t lsb = CountTrailingZeros_32(v);
955 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
956 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
957 return lsb | (msb << 5);
960 unsigned ARMMCCodeEmitter::
961 getRegisterListOpValue(const MCInst &MI, unsigned Op,
962 SmallVectorImpl<MCFixup> &Fixups) const {
965 // {7-0} = Number of registers
968 // {15-0} = Bitfield of GPRs.
969 unsigned Reg = MI.getOperand(Op).getReg();
970 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
971 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
975 if (SPRRegs || DPRRegs) {
977 unsigned RegNo = getARMRegisterNumbering(Reg);
978 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
979 Binary |= (RegNo & 0x1f) << 8;
983 Binary |= NumRegs * 2;
985 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
986 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
987 Binary |= 1 << RegNo;
994 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
995 /// with the alignment operand.
996 unsigned ARMMCCodeEmitter::
997 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
998 SmallVectorImpl<MCFixup> &Fixups) const {
999 const MCOperand &Reg = MI.getOperand(Op);
1000 const MCOperand &Imm = MI.getOperand(Op + 1);
1002 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1005 switch (Imm.getImm()) {
1009 case 8: Align = 0x01; break;
1010 case 16: Align = 0x02; break;
1011 case 32: Align = 0x03; break;
1014 return RegNo | (Align << 4);
1017 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1018 /// alignment operand for use in VLD-dup instructions. This is the same as
1019 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1020 /// different for VLD4-dup.
1021 unsigned ARMMCCodeEmitter::
1022 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1023 SmallVectorImpl<MCFixup> &Fixups) const {
1024 const MCOperand &Reg = MI.getOperand(Op);
1025 const MCOperand &Imm = MI.getOperand(Op + 1);
1027 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1030 switch (Imm.getImm()) {
1034 case 8: Align = 0x01; break;
1035 case 16: Align = 0x03; break;
1038 return RegNo | (Align << 4);
1041 unsigned ARMMCCodeEmitter::
1042 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1043 SmallVectorImpl<MCFixup> &Fixups) const {
1044 const MCOperand &MO = MI.getOperand(Op);
1045 if (MO.getReg() == 0) return 0x0D;
1049 void ARMMCCodeEmitter::
1050 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1051 SmallVectorImpl<MCFixup> &Fixups) const {
1052 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1053 // Pseudo instructions don't get encoded.
1054 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1055 uint64_t TSFlags = Desc.TSFlags;
1056 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1059 // Basic size info comes from the TSFlags field.
1060 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1061 default: llvm_unreachable("Unexpected instruction size!");
1062 case ARMII::Size2Bytes: Size = 2; break;
1063 case ARMII::Size4Bytes: Size = 4; break;
1065 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1066 // Thumb 32-bit wide instructions need to be have the high order halfword
1068 if (Subtarget.isThumb() && Size == 4) {
1069 EmitConstant(Binary >> 16, 2, OS);
1070 EmitConstant(Binary & 0xffff, 2, OS);
1072 EmitConstant(Binary, Size, OS);
1073 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1076 #include "ARMGenMCCodeEmitter.inc"