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_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
51 MCFixupKindInfo::FKF_IsAligned},
52 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
54 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
56 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAligned},
58 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
59 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
61 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
62 { "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
63 { "fixup_arm_movt_hi16", 0, 16, 0 },
64 { "fixup_arm_movw_lo16", 0, 16, 0 },
67 if (Kind < FirstTargetFixupKind)
68 return MCCodeEmitter::getFixupKindInfo(Kind);
70 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
72 return Infos[Kind - FirstTargetFixupKind];
74 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
76 // getBinaryCodeForInstr - TableGen'erated function for getting the
77 // binary encoding for an instruction.
78 unsigned getBinaryCodeForInstr(const MCInst &MI,
79 SmallVectorImpl<MCFixup> &Fixups) const;
81 /// getMachineOpValue - Return binary encoding of operand. If the machine
82 /// operand requires relocation, record the relocation and return zero.
83 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
84 SmallVectorImpl<MCFixup> &Fixups) const;
86 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
87 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
88 SmallVectorImpl<MCFixup> &Fixups) const;
90 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
91 unsigned &Reg, unsigned &Imm,
92 SmallVectorImpl<MCFixup> &Fixups) const;
94 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
96 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
99 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
100 /// BLX branch target.
101 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
104 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
105 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
108 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
109 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
112 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
114 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
117 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
118 /// ADR label target.
119 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
122 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
124 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
127 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
129 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups) const;
133 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
134 /// operand as needed by load/store instructions.
135 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
136 SmallVectorImpl<MCFixup> &Fixups) const;
138 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
139 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
140 SmallVectorImpl<MCFixup> &Fixups) const {
141 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
143 default: assert(0 && "Unknown addressing sub-mode!");
144 case ARM_AM::da: return 0;
145 case ARM_AM::ia: return 1;
146 case ARM_AM::db: return 2;
147 case ARM_AM::ib: return 3;
150 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
152 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
154 default: llvm_unreachable("Unknown shift opc!");
155 case ARM_AM::no_shift:
156 case ARM_AM::lsl: return 0;
157 case ARM_AM::lsr: return 1;
158 case ARM_AM::asr: return 2;
160 case ARM_AM::rrx: return 3;
165 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
166 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
169 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
170 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
173 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
174 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
175 SmallVectorImpl<MCFixup> &Fixups) const;
177 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
178 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
181 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
183 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
186 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
187 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &) const;
190 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
191 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
194 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
195 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
198 /// getCCOutOpValue - Return encoding of the 's' bit.
199 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
200 SmallVectorImpl<MCFixup> &Fixups) const {
201 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
203 return MI.getOperand(Op).getReg() == ARM::CPSR;
206 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
207 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
208 SmallVectorImpl<MCFixup> &Fixups) const {
209 unsigned SoImm = MI.getOperand(Op).getImm();
210 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
211 assert(SoImmVal != -1 && "Not a valid so_imm value!");
213 // Encode rotate_imm.
214 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
215 << ARMII::SoRotImmShift;
218 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
222 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
223 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
225 unsigned SoImm = MI.getOperand(Op).getImm();
226 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
227 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
231 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
232 SmallVectorImpl<MCFixup> &Fixups) const;
233 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
234 SmallVectorImpl<MCFixup> &Fixups) const;
235 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
236 SmallVectorImpl<MCFixup> &Fixups) const;
237 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
238 SmallVectorImpl<MCFixup> &Fixups) const;
240 /// getSORegOpValue - Return an encoded so_reg shifted register value.
241 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const;
243 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const;
246 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
247 SmallVectorImpl<MCFixup> &Fixups) const {
248 switch (MI.getOperand(Op).getImm()) {
249 default: assert (0 && "Not a valid rot_imm value!");
257 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
258 SmallVectorImpl<MCFixup> &Fixups) const {
259 return MI.getOperand(Op).getImm() - 1;
262 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const {
264 return 64 - MI.getOperand(Op).getImm();
267 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
270 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
272 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
274 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
276 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
279 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
280 unsigned EncodedValue) const;
281 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
282 unsigned EncodedValue) const;
283 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
284 unsigned EncodedValue) const;
286 unsigned VFPThumb2PostEncoder(const MCInst &MI,
287 unsigned EncodedValue) const;
289 void EmitByte(unsigned char C, raw_ostream &OS) const {
293 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
294 // Output the constant in little endian byte order.
295 for (unsigned i = 0; i != Size; ++i) {
296 EmitByte(Val & 255, OS);
301 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
302 SmallVectorImpl<MCFixup> &Fixups) const;
305 } // end anonymous namespace
307 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
309 return new ARMMCCodeEmitter(TM, Ctx);
312 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
313 /// instructions, and rewrite them to their Thumb2 form if we are currently in
315 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
316 unsigned EncodedValue) const {
317 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
318 if (Subtarget.isThumb2()) {
319 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
320 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
322 unsigned Bit24 = EncodedValue & 0x01000000;
323 unsigned Bit28 = Bit24 << 4;
324 EncodedValue &= 0xEFFFFFFF;
325 EncodedValue |= Bit28;
326 EncodedValue |= 0x0F000000;
332 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
333 /// instructions, and rewrite them to their Thumb2 form if we are currently in
335 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
336 unsigned EncodedValue) const {
337 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
338 if (Subtarget.isThumb2()) {
339 EncodedValue &= 0xF0FFFFFF;
340 EncodedValue |= 0x09000000;
346 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
347 /// instructions, and rewrite them to their Thumb2 form if we are currently in
349 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
350 unsigned EncodedValue) const {
351 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
352 if (Subtarget.isThumb2()) {
353 EncodedValue &= 0x00FFFFFF;
354 EncodedValue |= 0xEE000000;
360 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
361 /// them to their Thumb2 form if we are currently in Thumb2 mode.
362 unsigned ARMMCCodeEmitter::
363 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
364 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
365 EncodedValue &= 0x0FFFFFFF;
366 EncodedValue |= 0xE0000000;
371 /// getMachineOpValue - Return binary encoding of operand. If the machine
372 /// operand requires relocation, record the relocation and return zero.
373 unsigned ARMMCCodeEmitter::
374 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
375 SmallVectorImpl<MCFixup> &Fixups) const {
377 unsigned Reg = MO.getReg();
378 unsigned RegNo = getARMRegisterNumbering(Reg);
380 // Q registers are encoded as 2x their register number.
384 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
385 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
386 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
387 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
390 } else if (MO.isImm()) {
391 return static_cast<unsigned>(MO.getImm());
392 } else if (MO.isFPImm()) {
393 return static_cast<unsigned>(APFloat(MO.getFPImm())
394 .bitcastToAPInt().getHiBits(32).getLimitedValue());
397 llvm_unreachable("Unable to encode MCOperand!");
401 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
402 bool ARMMCCodeEmitter::
403 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
404 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
405 const MCOperand &MO = MI.getOperand(OpIdx);
406 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
408 Reg = getARMRegisterNumbering(MO.getReg());
410 int32_t SImm = MO1.getImm();
413 // Special value for #-0
414 if (SImm == INT32_MIN)
417 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
427 /// getBranchTargetOpValue - Helper function to get the branch target operand,
428 /// which is either an immediate or requires a fixup.
429 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
431 SmallVectorImpl<MCFixup> &Fixups) {
432 const MCOperand &MO = MI.getOperand(OpIdx);
434 // If the destination is an immediate, we have nothing to do.
435 if (MO.isImm()) return MO.getImm();
436 assert(MO.isExpr() && "Unexpected branch target type!");
437 const MCExpr *Expr = MO.getExpr();
438 MCFixupKind Kind = MCFixupKind(FixupKind);
439 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
441 // All of the information is in the fixup.
445 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
446 uint32_t ARMMCCodeEmitter::
447 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
448 SmallVectorImpl<MCFixup> &Fixups) const {
449 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
452 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
453 /// BLX branch target.
454 uint32_t ARMMCCodeEmitter::
455 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
456 SmallVectorImpl<MCFixup> &Fixups) const {
457 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
460 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
461 uint32_t ARMMCCodeEmitter::
462 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
463 SmallVectorImpl<MCFixup> &Fixups) const {
464 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
467 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
468 uint32_t ARMMCCodeEmitter::
469 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
470 SmallVectorImpl<MCFixup> &Fixups) const {
471 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
474 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
476 uint32_t ARMMCCodeEmitter::
477 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
478 SmallVectorImpl<MCFixup> &Fixups) const {
479 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
480 if (Subtarget.isThumb2())
481 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
482 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
485 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
487 uint32_t ARMMCCodeEmitter::
488 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
489 SmallVectorImpl<MCFixup> &Fixups) const {
490 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
491 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
495 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
496 uint32_t ARMMCCodeEmitter::
497 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
498 SmallVectorImpl<MCFixup> &Fixups) const {
500 // {12} = (U)nsigned (add == '1', sub == '0')
504 // If The first operand isn't a register, we have a label reference.
505 const MCOperand &MO = MI.getOperand(OpIdx);
506 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
507 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
508 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
510 isAdd = false ; // 'U' bit is set as part of the fixup.
512 const MCExpr *Expr = 0;
516 Expr = MO2.getExpr();
518 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
520 if (Subtarget.isThumb2())
521 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
523 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
524 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
526 ++MCNumCPRelocations;
528 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
530 uint32_t Binary = Imm12 & 0xfff;
531 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
534 Binary |= (Reg << 13);
538 /// getT2AddrModeImm8s4OpValue - Return encoding info for
539 /// 'reg +/- imm8<<2' operand.
540 uint32_t ARMMCCodeEmitter::
541 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
542 SmallVectorImpl<MCFixup> &Fixups) const {
544 // {12} = (U)nsigned (add == '1', sub == '0')
548 // If The first operand isn't a register, we have a label reference.
549 const MCOperand &MO = MI.getOperand(OpIdx);
551 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
553 isAdd = false ; // 'U' bit is set as part of the fixup.
555 assert(MO.isExpr() && "Unexpected machine operand type!");
556 const MCExpr *Expr = MO.getExpr();
557 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
558 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
560 ++MCNumCPRelocations;
562 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
564 uint32_t Binary = (Imm8 >> 2) & 0xff;
565 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
568 Binary |= (Reg << 9);
572 uint32_t ARMMCCodeEmitter::
573 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
574 SmallVectorImpl<MCFixup> &Fixups) const {
575 // {20-16} = imm{15-12}
576 // {11-0} = imm{11-0}
577 const MCOperand &MO = MI.getOperand(OpIdx);
579 return static_cast<unsigned>(MO.getImm());
580 } else if (const MCSymbolRefExpr *Expr =
581 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
583 switch (Expr->getKind()) {
584 default: assert(0 && "Unsupported ARMFixup");
585 case MCSymbolRefExpr::VK_ARM_HI16:
586 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
588 case MCSymbolRefExpr::VK_ARM_LO16:
589 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
592 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
595 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
599 uint32_t ARMMCCodeEmitter::
600 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 const MCOperand &MO = MI.getOperand(OpIdx);
603 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
604 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
605 unsigned Rn = getARMRegisterNumbering(MO.getReg());
606 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
607 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
608 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
609 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
610 unsigned SBits = getShiftOp(ShOp);
619 uint32_t Binary = Rm;
621 Binary |= SBits << 5;
622 Binary |= ShImm << 7;
628 uint32_t ARMMCCodeEmitter::
629 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
630 SmallVectorImpl<MCFixup> &Fixups) const {
632 // {13} 1 == imm12, 0 == Rm
635 const MCOperand &MO = MI.getOperand(OpIdx);
636 unsigned Rn = getARMRegisterNumbering(MO.getReg());
637 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
642 uint32_t ARMMCCodeEmitter::
643 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
644 SmallVectorImpl<MCFixup> &Fixups) const {
645 // {13} 1 == imm12, 0 == Rm
648 const MCOperand &MO = MI.getOperand(OpIdx);
649 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
650 unsigned Imm = MO1.getImm();
651 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
652 bool isReg = MO.getReg() != 0;
653 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
654 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
656 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
657 Binary <<= 7; // Shift amount is bits [11:7]
658 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
659 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
661 return Binary | (isAdd << 12) | (isReg << 13);
664 uint32_t ARMMCCodeEmitter::
665 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
666 SmallVectorImpl<MCFixup> &Fixups) const {
667 // {9} 1 == imm8, 0 == Rm
671 const MCOperand &MO = MI.getOperand(OpIdx);
672 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
673 unsigned Imm = MO1.getImm();
674 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
675 bool isImm = MO.getReg() == 0;
676 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
677 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
679 Imm8 = getARMRegisterNumbering(MO.getReg());
680 return Imm8 | (isAdd << 8) | (isImm << 9);
683 uint32_t ARMMCCodeEmitter::
684 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
685 SmallVectorImpl<MCFixup> &Fixups) const {
686 // {13} 1 == imm8, 0 == Rm
691 const MCOperand &MO = MI.getOperand(OpIdx);
692 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
693 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
694 unsigned Rn = getARMRegisterNumbering(MO.getReg());
695 unsigned Imm = MO2.getImm();
696 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
697 bool isImm = MO1.getReg() == 0;
698 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
699 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
701 Imm8 = getARMRegisterNumbering(MO1.getReg());
702 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
705 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
706 uint32_t ARMMCCodeEmitter::
707 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
708 SmallVectorImpl<MCFixup> &Fixups) const {
711 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
712 #if 0 // FIXME: This crashes2003-05-14-initialize-string.c
713 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
714 "Unexpected base register!");
716 // The immediate is already shifted for the implicit zeroes, so no change
718 return MO1.getImm() & 0xff;
721 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
722 uint32_t ARMMCCodeEmitter::
723 getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
724 SmallVectorImpl<MCFixup> &) const {
732 const MCOperand &MO = MI.getOperand(OpIdx);
733 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
734 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
735 unsigned Rn = getARMRegisterNumbering(MO.getReg());
736 unsigned Imm5 = MO1.getImm();
738 if (MO2.getReg() != 0)
740 Imm5 = getARMRegisterNumbering(MO2.getReg());
742 return ((Imm5 & 0x1f) << 3) | Rn;
745 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
746 uint32_t ARMMCCodeEmitter::
747 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &Fixups) const {
749 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
752 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
753 uint32_t ARMMCCodeEmitter::
754 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
755 SmallVectorImpl<MCFixup> &Fixups) const {
757 // {8} = (U)nsigned (add == '1', sub == '0')
761 // If The first operand isn't a register, we have a label reference.
762 const MCOperand &MO = MI.getOperand(OpIdx);
764 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
766 isAdd = false; // 'U' bit is handled as part of the fixup.
768 assert(MO.isExpr() && "Unexpected machine operand type!");
769 const MCExpr *Expr = MO.getExpr();
771 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
772 if (Subtarget.isThumb2())
773 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
775 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
776 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
778 ++MCNumCPRelocations;
780 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
781 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
784 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
785 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
788 Binary |= (Reg << 9);
792 unsigned ARMMCCodeEmitter::
793 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
794 SmallVectorImpl<MCFixup> &Fixups) const {
795 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
796 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
797 // case the imm contains the amount to shift by.
800 // {4} = 1 if reg shift, 0 if imm shift
808 const MCOperand &MO = MI.getOperand(OpIdx);
809 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
810 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
811 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
814 unsigned Binary = getARMRegisterNumbering(MO.getReg());
816 // Encode the shift opcode.
818 unsigned Rs = MO1.getReg();
820 // Set shift operand (bit[7:4]).
825 // RRX - 0110 and bit[11:8] clear.
827 default: llvm_unreachable("Unknown shift opc!");
828 case ARM_AM::lsl: SBits = 0x1; break;
829 case ARM_AM::lsr: SBits = 0x3; break;
830 case ARM_AM::asr: SBits = 0x5; break;
831 case ARM_AM::ror: SBits = 0x7; break;
832 case ARM_AM::rrx: SBits = 0x6; break;
835 // Set shift operand (bit[6:4]).
841 default: llvm_unreachable("Unknown shift opc!");
842 case ARM_AM::lsl: SBits = 0x0; break;
843 case ARM_AM::lsr: SBits = 0x2; break;
844 case ARM_AM::asr: SBits = 0x4; break;
845 case ARM_AM::ror: SBits = 0x6; break;
849 Binary |= SBits << 4;
850 if (SOpc == ARM_AM::rrx)
853 // Encode the shift operation Rs or shift_imm (except rrx).
855 // Encode Rs bit[11:8].
856 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
857 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
860 // Encode shift_imm bit[11:7].
861 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
864 unsigned ARMMCCodeEmitter::
865 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
866 SmallVectorImpl<MCFixup> &Fixups) const {
867 const MCOperand &MO1 = MI.getOperand(OpNum);
868 const MCOperand &MO2 = MI.getOperand(OpNum+1);
869 const MCOperand &MO3 = MI.getOperand(OpNum+2);
871 // Encoded as [Rn, Rm, imm].
872 // FIXME: Needs fixup support.
873 unsigned Value = getARMRegisterNumbering(MO1.getReg());
875 Value |= getARMRegisterNumbering(MO2.getReg());
877 Value |= MO3.getImm();
882 unsigned ARMMCCodeEmitter::
883 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
884 SmallVectorImpl<MCFixup> &Fixups) const {
885 const MCOperand &MO1 = MI.getOperand(OpNum);
886 const MCOperand &MO2 = MI.getOperand(OpNum+1);
888 // FIXME: Needs fixup support.
889 unsigned Value = getARMRegisterNumbering(MO1.getReg());
891 // Even though the immediate is 8 bits long, we need 9 bits in order
892 // to represent the (inverse of the) sign bit.
894 int32_t tmp = (int32_t)MO2.getImm();
898 Value |= 256; // Set the ADD bit
903 unsigned ARMMCCodeEmitter::
904 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
905 SmallVectorImpl<MCFixup> &Fixups) const {
906 const MCOperand &MO1 = MI.getOperand(OpNum);
908 // FIXME: Needs fixup support.
910 int32_t tmp = (int32_t)MO1.getImm();
914 Value |= 256; // Set the ADD bit
919 unsigned ARMMCCodeEmitter::
920 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
921 SmallVectorImpl<MCFixup> &Fixups) const {
922 const MCOperand &MO1 = MI.getOperand(OpNum);
924 // FIXME: Needs fixup support.
926 int32_t tmp = (int32_t)MO1.getImm();
930 Value |= 4096; // Set the ADD bit
935 unsigned ARMMCCodeEmitter::
936 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
937 SmallVectorImpl<MCFixup> &Fixups) const {
938 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
939 // shifted. The second is the amount to shift by.
946 const MCOperand &MO = MI.getOperand(OpIdx);
947 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
948 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
951 unsigned Binary = getARMRegisterNumbering(MO.getReg());
953 // Encode the shift opcode.
955 // Set shift operand (bit[6:4]).
961 default: llvm_unreachable("Unknown shift opc!");
962 case ARM_AM::lsl: SBits = 0x0; break;
963 case ARM_AM::lsr: SBits = 0x2; break;
964 case ARM_AM::asr: SBits = 0x4; break;
965 case ARM_AM::ror: SBits = 0x6; break;
968 Binary |= SBits << 4;
969 if (SOpc == ARM_AM::rrx)
972 // Encode shift_imm bit[11:7].
973 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
976 unsigned ARMMCCodeEmitter::
977 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
978 SmallVectorImpl<MCFixup> &Fixups) const {
979 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
981 const MCOperand &MO = MI.getOperand(Op);
982 uint32_t v = ~MO.getImm();
983 uint32_t lsb = CountTrailingZeros_32(v);
984 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
985 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
986 return lsb | (msb << 5);
989 unsigned ARMMCCodeEmitter::
990 getRegisterListOpValue(const MCInst &MI, unsigned Op,
991 SmallVectorImpl<MCFixup> &Fixups) const {
994 // {7-0} = Number of registers
997 // {15-0} = Bitfield of GPRs.
998 unsigned Reg = MI.getOperand(Op).getReg();
999 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1000 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1002 unsigned Binary = 0;
1004 if (SPRRegs || DPRRegs) {
1006 unsigned RegNo = getARMRegisterNumbering(Reg);
1007 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1008 Binary |= (RegNo & 0x1f) << 8;
1012 Binary |= NumRegs * 2;
1014 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1015 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1016 Binary |= 1 << RegNo;
1023 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1024 /// with the alignment operand.
1025 unsigned ARMMCCodeEmitter::
1026 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1027 SmallVectorImpl<MCFixup> &Fixups) const {
1028 const MCOperand &Reg = MI.getOperand(Op);
1029 const MCOperand &Imm = MI.getOperand(Op + 1);
1031 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1034 switch (Imm.getImm()) {
1038 case 8: Align = 0x01; break;
1039 case 16: Align = 0x02; break;
1040 case 32: Align = 0x03; break;
1043 return RegNo | (Align << 4);
1046 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1047 /// alignment operand for use in VLD-dup instructions. This is the same as
1048 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1049 /// different for VLD4-dup.
1050 unsigned ARMMCCodeEmitter::
1051 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1052 SmallVectorImpl<MCFixup> &Fixups) const {
1053 const MCOperand &Reg = MI.getOperand(Op);
1054 const MCOperand &Imm = MI.getOperand(Op + 1);
1056 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1059 switch (Imm.getImm()) {
1063 case 8: Align = 0x01; break;
1064 case 16: Align = 0x03; break;
1067 return RegNo | (Align << 4);
1070 unsigned ARMMCCodeEmitter::
1071 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1072 SmallVectorImpl<MCFixup> &Fixups) const {
1073 const MCOperand &MO = MI.getOperand(Op);
1074 if (MO.getReg() == 0) return 0x0D;
1078 void ARMMCCodeEmitter::
1079 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1080 SmallVectorImpl<MCFixup> &Fixups) const {
1081 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1082 // Pseudo instructions don't get encoded.
1083 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1084 uint64_t TSFlags = Desc.TSFlags;
1085 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1088 // Basic size info comes from the TSFlags field.
1089 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1090 default: llvm_unreachable("Unexpected instruction size!");
1091 case ARMII::Size2Bytes: Size = 2; break;
1092 case ARMII::Size4Bytes: Size = 4; break;
1094 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1095 // Thumb 32-bit wide instructions need to be have the high order halfword
1097 if (Subtarget.isThumb() && Size == 4) {
1098 EmitConstant(Binary >> 16, 2, OS);
1099 EmitConstant(Binary & 0xffff, 2, OS);
1101 EmitConstant(Binary, Size, OS);
1102 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1105 #include "ARMGenMCCodeEmitter.inc"