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 "ARMMCExpr.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/raw_ostream.h"
27 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
28 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
31 class ARMMCCodeEmitter : public MCCodeEmitter {
32 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
34 const TargetMachine &TM;
35 const TargetInstrInfo &TII;
39 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
40 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
43 ~ARMMCCodeEmitter() {}
45 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
47 // getBinaryCodeForInstr - TableGen'erated function for getting the
48 // binary encoding for an instruction.
49 unsigned getBinaryCodeForInstr(const MCInst &MI,
50 SmallVectorImpl<MCFixup> &Fixups) const;
52 /// getMachineOpValue - Return binary encoding of operand. If the machine
53 /// operand requires relocation, record the relocation and return zero.
54 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
55 SmallVectorImpl<MCFixup> &Fixups) const;
57 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
58 /// the specified operand. This is used for operands with :lower16: and
59 /// :upper16: prefixes.
60 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
61 SmallVectorImpl<MCFixup> &Fixups) const;
63 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
64 unsigned &Reg, unsigned &Imm,
65 SmallVectorImpl<MCFixup> &Fixups) const;
67 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
69 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
70 SmallVectorImpl<MCFixup> &Fixups) const;
72 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
73 /// BLX branch target.
74 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
75 SmallVectorImpl<MCFixup> &Fixups) const;
77 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
78 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
81 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
82 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups) const;
85 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
86 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
89 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
91 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
94 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
95 /// immediate Thumb2 direct branch target.
96 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
100 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
101 /// ADR label target.
102 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
104 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
106 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
110 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
112 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
115 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
116 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups)const;
119 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
121 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
125 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
126 /// operand as needed by load/store instructions.
127 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
130 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
131 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
132 SmallVectorImpl<MCFixup> &Fixups) const {
133 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
135 default: assert(0 && "Unknown addressing sub-mode!");
136 case ARM_AM::da: return 0;
137 case ARM_AM::ia: return 1;
138 case ARM_AM::db: return 2;
139 case ARM_AM::ib: return 3;
142 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
144 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
146 default: llvm_unreachable("Unknown shift opc!");
147 case ARM_AM::no_shift:
148 case ARM_AM::lsl: return 0;
149 case ARM_AM::lsr: return 1;
150 case ARM_AM::asr: return 2;
152 case ARM_AM::rrx: return 3;
157 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
158 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
161 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
162 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
165 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
166 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
169 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
170 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
173 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
175 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
178 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
179 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
182 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
183 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
186 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
187 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
190 /// getCCOutOpValue - Return encoding of the 's' bit.
191 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
192 SmallVectorImpl<MCFixup> &Fixups) const {
193 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
195 return MI.getOperand(Op).getReg() == ARM::CPSR;
198 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
199 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
200 SmallVectorImpl<MCFixup> &Fixups) const {
201 unsigned SoImm = MI.getOperand(Op).getImm();
202 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
203 assert(SoImmVal != -1 && "Not a valid so_imm value!");
205 // Encode rotate_imm.
206 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
207 << ARMII::SoRotImmShift;
210 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
214 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
215 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
216 SmallVectorImpl<MCFixup> &Fixups) const {
217 unsigned SoImm = MI.getOperand(Op).getImm();
218 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
219 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
223 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
224 SmallVectorImpl<MCFixup> &Fixups) const;
225 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
226 SmallVectorImpl<MCFixup> &Fixups) const;
227 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
228 SmallVectorImpl<MCFixup> &Fixups) const;
229 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
230 SmallVectorImpl<MCFixup> &Fixups) const;
232 /// getSORegOpValue - Return an encoded so_reg shifted register value.
233 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
234 SmallVectorImpl<MCFixup> &Fixups) const;
235 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
238 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const {
240 switch (MI.getOperand(Op).getImm()) {
241 default: assert (0 && "Not a valid rot_imm value!");
249 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
250 SmallVectorImpl<MCFixup> &Fixups) const {
251 return MI.getOperand(Op).getImm() - 1;
254 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const {
256 return 64 - MI.getOperand(Op).getImm();
259 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
262 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const;
264 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
265 SmallVectorImpl<MCFixup> &Fixups) const;
266 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const;
268 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
271 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
272 unsigned EncodedValue) const;
273 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
274 unsigned EncodedValue) const;
275 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
276 unsigned EncodedValue) const;
278 unsigned VFPThumb2PostEncoder(const MCInst &MI,
279 unsigned EncodedValue) const;
281 void EmitByte(unsigned char C, raw_ostream &OS) const {
285 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
286 // Output the constant in little endian byte order.
287 for (unsigned i = 0; i != Size; ++i) {
288 EmitByte(Val & 255, OS);
293 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
294 SmallVectorImpl<MCFixup> &Fixups) const;
297 } // end anonymous namespace
299 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
301 return new ARMMCCodeEmitter(TM, Ctx);
304 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
305 /// instructions, and rewrite them to their Thumb2 form if we are currently in
307 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
308 unsigned EncodedValue) const {
309 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
310 if (Subtarget.isThumb2()) {
311 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
312 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
314 unsigned Bit24 = EncodedValue & 0x01000000;
315 unsigned Bit28 = Bit24 << 4;
316 EncodedValue &= 0xEFFFFFFF;
317 EncodedValue |= Bit28;
318 EncodedValue |= 0x0F000000;
324 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
325 /// instructions, and rewrite them to their Thumb2 form if we are currently in
327 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
328 unsigned EncodedValue) const {
329 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
330 if (Subtarget.isThumb2()) {
331 EncodedValue &= 0xF0FFFFFF;
332 EncodedValue |= 0x09000000;
338 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
339 /// instructions, and rewrite them to their Thumb2 form if we are currently in
341 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
342 unsigned EncodedValue) const {
343 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
344 if (Subtarget.isThumb2()) {
345 EncodedValue &= 0x00FFFFFF;
346 EncodedValue |= 0xEE000000;
352 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
353 /// them to their Thumb2 form if we are currently in Thumb2 mode.
354 unsigned ARMMCCodeEmitter::
355 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
356 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
357 EncodedValue &= 0x0FFFFFFF;
358 EncodedValue |= 0xE0000000;
363 /// getMachineOpValue - Return binary encoding of operand. If the machine
364 /// operand requires relocation, record the relocation and return zero.
365 unsigned ARMMCCodeEmitter::
366 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
367 SmallVectorImpl<MCFixup> &Fixups) const {
369 unsigned Reg = MO.getReg();
370 unsigned RegNo = getARMRegisterNumbering(Reg);
372 // Q registers are encoded as 2x their register number.
376 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
377 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
378 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
379 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
382 } else if (MO.isImm()) {
383 return static_cast<unsigned>(MO.getImm());
384 } else if (MO.isFPImm()) {
385 return static_cast<unsigned>(APFloat(MO.getFPImm())
386 .bitcastToAPInt().getHiBits(32).getLimitedValue());
389 llvm_unreachable("Unable to encode MCOperand!");
393 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
394 bool ARMMCCodeEmitter::
395 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
396 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
397 const MCOperand &MO = MI.getOperand(OpIdx);
398 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
400 Reg = getARMRegisterNumbering(MO.getReg());
402 int32_t SImm = MO1.getImm();
405 // Special value for #-0
406 if (SImm == INT32_MIN)
409 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
419 /// getBranchTargetOpValue - Helper function to get the branch target operand,
420 /// which is either an immediate or requires a fixup.
421 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
423 SmallVectorImpl<MCFixup> &Fixups) {
424 const MCOperand &MO = MI.getOperand(OpIdx);
426 // If the destination is an immediate, we have nothing to do.
427 if (MO.isImm()) return MO.getImm();
428 assert(MO.isExpr() && "Unexpected branch target type!");
429 const MCExpr *Expr = MO.getExpr();
430 MCFixupKind Kind = MCFixupKind(FixupKind);
431 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
433 // All of the information is in the fixup.
437 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
438 uint32_t ARMMCCodeEmitter::
439 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
440 SmallVectorImpl<MCFixup> &Fixups) const {
441 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
444 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
445 /// BLX branch target.
446 uint32_t ARMMCCodeEmitter::
447 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
448 SmallVectorImpl<MCFixup> &Fixups) const {
449 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
452 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
453 uint32_t ARMMCCodeEmitter::
454 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
455 SmallVectorImpl<MCFixup> &Fixups) const {
456 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
459 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
460 uint32_t ARMMCCodeEmitter::
461 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
462 SmallVectorImpl<MCFixup> &Fixups) const {
463 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
466 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
467 uint32_t ARMMCCodeEmitter::
468 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
469 SmallVectorImpl<MCFixup> &Fixups) const {
470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
473 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
475 uint32_t ARMMCCodeEmitter::
476 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
477 SmallVectorImpl<MCFixup> &Fixups) const {
478 // FIXME: This really, really shouldn't use TargetMachine. We don't want
479 // coupling between MC and TM anywhere we can help it.
480 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
481 if (Subtarget.isThumb2())
483 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
487 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
488 /// immediate branch target.
489 uint32_t ARMMCCodeEmitter::
490 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
493 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
494 bool I = (Val & 0x800000);
495 bool J1 = (Val & 0x400000);
496 bool J2 = (Val & 0x200000);
510 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
512 uint32_t ARMMCCodeEmitter::
513 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
514 SmallVectorImpl<MCFixup> &Fixups) const {
515 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
516 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
520 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
522 uint32_t ARMMCCodeEmitter::
523 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
524 SmallVectorImpl<MCFixup> &Fixups) const {
525 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
526 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
530 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
532 uint32_t ARMMCCodeEmitter::
533 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
534 SmallVectorImpl<MCFixup> &Fixups) const {
535 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
536 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
540 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
542 uint32_t ARMMCCodeEmitter::
543 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
544 SmallVectorImpl<MCFixup> &) const {
548 const MCOperand &MO1 = MI.getOperand(OpIdx);
549 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
550 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
551 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
552 return (Rm << 3) | Rn;
555 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
556 uint32_t ARMMCCodeEmitter::
557 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
560 // {12} = (U)nsigned (add == '1', sub == '0')
564 // If The first operand isn't a register, we have a label reference.
565 const MCOperand &MO = MI.getOperand(OpIdx);
566 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
567 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
568 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
570 isAdd = false ; // 'U' bit is set as part of the fixup.
572 const MCExpr *Expr = 0;
576 Expr = MO2.getExpr();
578 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
580 if (Subtarget.isThumb2())
581 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
583 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
584 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
586 ++MCNumCPRelocations;
588 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
590 uint32_t Binary = Imm12 & 0xfff;
591 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
594 Binary |= (Reg << 13);
598 /// getT2AddrModeImm8s4OpValue - Return encoding info for
599 /// 'reg +/- imm8<<2' operand.
600 uint32_t ARMMCCodeEmitter::
601 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
602 SmallVectorImpl<MCFixup> &Fixups) const {
604 // {8} = (U)nsigned (add == '1', sub == '0')
608 // If The first operand isn't a register, we have a label reference.
609 const MCOperand &MO = MI.getOperand(OpIdx);
611 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
613 isAdd = false ; // 'U' bit is set as part of the fixup.
615 assert(MO.isExpr() && "Unexpected machine operand type!");
616 const MCExpr *Expr = MO.getExpr();
617 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
618 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
620 ++MCNumCPRelocations;
622 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
624 uint32_t Binary = (Imm8 >> 2) & 0xff;
625 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
628 Binary |= (Reg << 9);
632 // FIXME: This routine assumes that a binary
633 // expression will always result in a PCRel expression
634 // In reality, its only true if one or more subexpressions
635 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
636 // but this is good enough for now.
637 static bool EvaluateAsPCRel(const MCExpr *Expr) {
638 switch (Expr->getKind()) {
639 default: assert(0 && "Unexpected expression type");
640 case MCExpr::SymbolRef: return false;
641 case MCExpr::Binary: return true;
646 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
647 SmallVectorImpl<MCFixup> &Fixups) const {
648 // {20-16} = imm{15-12}
649 // {11-0} = imm{11-0}
650 const MCOperand &MO = MI.getOperand(OpIdx);
652 // Hi / lo 16 bits already extracted during earlier passes.
653 return static_cast<unsigned>(MO.getImm());
655 // Handle :upper16: and :lower16: assembly prefixes.
656 const MCExpr *E = MO.getExpr();
657 if (E->getKind() == MCExpr::Target) {
658 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
659 E = ARM16Expr->getSubExpr();
662 switch (ARM16Expr->getKind()) {
663 default: assert(0 && "Unsupported ARMFixup");
664 case ARMMCExpr::VK_ARM_HI16:
665 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
666 if (EvaluateAsPCRel(E))
667 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16_pcrel);
669 case ARMMCExpr::VK_ARM_LO16:
670 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
671 if (EvaluateAsPCRel(E))
672 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16_pcrel);
675 Fixups.push_back(MCFixup::Create(0, E, Kind));
679 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
683 uint32_t ARMMCCodeEmitter::
684 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
685 SmallVectorImpl<MCFixup> &Fixups) const {
686 const MCOperand &MO = MI.getOperand(OpIdx);
687 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
688 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
689 unsigned Rn = getARMRegisterNumbering(MO.getReg());
690 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
691 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
692 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
693 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
694 unsigned SBits = getShiftOp(ShOp);
703 uint32_t Binary = Rm;
705 Binary |= SBits << 5;
706 Binary |= ShImm << 7;
712 uint32_t ARMMCCodeEmitter::
713 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
714 SmallVectorImpl<MCFixup> &Fixups) const {
716 // {13} 1 == imm12, 0 == Rm
719 const MCOperand &MO = MI.getOperand(OpIdx);
720 unsigned Rn = getARMRegisterNumbering(MO.getReg());
721 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
726 uint32_t ARMMCCodeEmitter::
727 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
728 SmallVectorImpl<MCFixup> &Fixups) const {
729 // {13} 1 == imm12, 0 == Rm
732 const MCOperand &MO = MI.getOperand(OpIdx);
733 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
734 unsigned Imm = MO1.getImm();
735 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
736 bool isReg = MO.getReg() != 0;
737 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
738 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
740 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
741 Binary <<= 7; // Shift amount is bits [11:7]
742 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
743 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
745 return Binary | (isAdd << 12) | (isReg << 13);
748 uint32_t ARMMCCodeEmitter::
749 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
750 SmallVectorImpl<MCFixup> &Fixups) const {
751 // {9} 1 == imm8, 0 == Rm
755 const MCOperand &MO = MI.getOperand(OpIdx);
756 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
757 unsigned Imm = MO1.getImm();
758 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
759 bool isImm = MO.getReg() == 0;
760 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
761 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
763 Imm8 = getARMRegisterNumbering(MO.getReg());
764 return Imm8 | (isAdd << 8) | (isImm << 9);
767 uint32_t ARMMCCodeEmitter::
768 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
769 SmallVectorImpl<MCFixup> &Fixups) const {
770 // {13} 1 == imm8, 0 == Rm
775 const MCOperand &MO = MI.getOperand(OpIdx);
776 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
777 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
778 unsigned Rn = getARMRegisterNumbering(MO.getReg());
779 unsigned Imm = MO2.getImm();
780 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
781 bool isImm = MO1.getReg() == 0;
782 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
783 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
785 Imm8 = getARMRegisterNumbering(MO1.getReg());
786 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
789 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
790 uint32_t ARMMCCodeEmitter::
791 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
792 SmallVectorImpl<MCFixup> &Fixups) const {
795 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
796 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
797 "Unexpected base register!");
799 // The immediate is already shifted for the implicit zeroes, so no change
801 return MO1.getImm() & 0xff;
804 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
805 uint32_t ARMMCCodeEmitter::
806 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
807 SmallVectorImpl<MCFixup> &Fixups) const {
811 const MCOperand &MO = MI.getOperand(OpIdx);
812 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
813 unsigned Rn = getARMRegisterNumbering(MO.getReg());
814 unsigned Imm5 = MO1.getImm();
815 return ((Imm5 & 0x1f) << 3) | Rn;
818 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
819 uint32_t ARMMCCodeEmitter::
820 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
821 SmallVectorImpl<MCFixup> &Fixups) const {
822 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
825 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
826 uint32_t ARMMCCodeEmitter::
827 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
828 SmallVectorImpl<MCFixup> &Fixups) const {
830 // {8} = (U)nsigned (add == '1', sub == '0')
834 // If The first operand isn't a register, we have a label reference.
835 const MCOperand &MO = MI.getOperand(OpIdx);
837 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
839 isAdd = false; // 'U' bit is handled as part of the fixup.
841 assert(MO.isExpr() && "Unexpected machine operand type!");
842 const MCExpr *Expr = MO.getExpr();
844 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
845 if (Subtarget.isThumb2())
846 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
848 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
849 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
851 ++MCNumCPRelocations;
853 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
854 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
857 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
858 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
861 Binary |= (Reg << 9);
865 unsigned ARMMCCodeEmitter::
866 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
867 SmallVectorImpl<MCFixup> &Fixups) const {
868 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
869 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
870 // case the imm contains the amount to shift by.
873 // {4} = 1 if reg shift, 0 if imm shift
881 const MCOperand &MO = MI.getOperand(OpIdx);
882 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
883 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
884 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
887 unsigned Binary = getARMRegisterNumbering(MO.getReg());
889 // Encode the shift opcode.
891 unsigned Rs = MO1.getReg();
893 // Set shift operand (bit[7:4]).
898 // RRX - 0110 and bit[11:8] clear.
900 default: llvm_unreachable("Unknown shift opc!");
901 case ARM_AM::lsl: SBits = 0x1; break;
902 case ARM_AM::lsr: SBits = 0x3; break;
903 case ARM_AM::asr: SBits = 0x5; break;
904 case ARM_AM::ror: SBits = 0x7; break;
905 case ARM_AM::rrx: SBits = 0x6; break;
908 // Set shift operand (bit[6:4]).
914 default: llvm_unreachable("Unknown shift opc!");
915 case ARM_AM::lsl: SBits = 0x0; break;
916 case ARM_AM::lsr: SBits = 0x2; break;
917 case ARM_AM::asr: SBits = 0x4; break;
918 case ARM_AM::ror: SBits = 0x6; break;
922 Binary |= SBits << 4;
923 if (SOpc == ARM_AM::rrx)
926 // Encode the shift operation Rs or shift_imm (except rrx).
928 // Encode Rs bit[11:8].
929 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
930 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
933 // Encode shift_imm bit[11:7].
934 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
937 unsigned ARMMCCodeEmitter::
938 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
939 SmallVectorImpl<MCFixup> &Fixups) const {
940 const MCOperand &MO1 = MI.getOperand(OpNum);
941 const MCOperand &MO2 = MI.getOperand(OpNum+1);
942 const MCOperand &MO3 = MI.getOperand(OpNum+2);
944 // Encoded as [Rn, Rm, imm].
945 // FIXME: Needs fixup support.
946 unsigned Value = getARMRegisterNumbering(MO1.getReg());
948 Value |= getARMRegisterNumbering(MO2.getReg());
950 Value |= MO3.getImm();
955 unsigned ARMMCCodeEmitter::
956 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
957 SmallVectorImpl<MCFixup> &Fixups) const {
958 const MCOperand &MO1 = MI.getOperand(OpNum);
959 const MCOperand &MO2 = MI.getOperand(OpNum+1);
961 // FIXME: Needs fixup support.
962 unsigned Value = getARMRegisterNumbering(MO1.getReg());
964 // Even though the immediate is 8 bits long, we need 9 bits in order
965 // to represent the (inverse of the) sign bit.
967 int32_t tmp = (int32_t)MO2.getImm();
971 Value |= 256; // Set the ADD bit
976 unsigned ARMMCCodeEmitter::
977 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
978 SmallVectorImpl<MCFixup> &Fixups) const {
979 const MCOperand &MO1 = MI.getOperand(OpNum);
981 // FIXME: Needs fixup support.
983 int32_t tmp = (int32_t)MO1.getImm();
987 Value |= 256; // Set the ADD bit
992 unsigned ARMMCCodeEmitter::
993 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
994 SmallVectorImpl<MCFixup> &Fixups) const {
995 const MCOperand &MO1 = MI.getOperand(OpNum);
997 // FIXME: Needs fixup support.
999 int32_t tmp = (int32_t)MO1.getImm();
1003 Value |= 4096; // Set the ADD bit
1004 Value |= tmp & 4095;
1008 unsigned ARMMCCodeEmitter::
1009 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1010 SmallVectorImpl<MCFixup> &Fixups) const {
1011 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1012 // shifted. The second is the amount to shift by.
1019 const MCOperand &MO = MI.getOperand(OpIdx);
1020 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1021 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1024 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1026 // Encode the shift opcode.
1028 // Set shift operand (bit[6:4]).
1034 default: llvm_unreachable("Unknown shift opc!");
1035 case ARM_AM::lsl: SBits = 0x0; break;
1036 case ARM_AM::lsr: SBits = 0x2; break;
1037 case ARM_AM::asr: SBits = 0x4; break;
1038 case ARM_AM::ror: SBits = 0x6; break;
1041 Binary |= SBits << 4;
1042 if (SOpc == ARM_AM::rrx)
1045 // Encode shift_imm bit[11:7].
1046 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1049 unsigned ARMMCCodeEmitter::
1050 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1051 SmallVectorImpl<MCFixup> &Fixups) const {
1052 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1054 const MCOperand &MO = MI.getOperand(Op);
1055 uint32_t v = ~MO.getImm();
1056 uint32_t lsb = CountTrailingZeros_32(v);
1057 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1058 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1059 return lsb | (msb << 5);
1062 unsigned ARMMCCodeEmitter::
1063 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1064 SmallVectorImpl<MCFixup> &Fixups) const {
1067 // {7-0} = Number of registers
1070 // {15-0} = Bitfield of GPRs.
1071 unsigned Reg = MI.getOperand(Op).getReg();
1072 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1073 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1075 unsigned Binary = 0;
1077 if (SPRRegs || DPRRegs) {
1079 unsigned RegNo = getARMRegisterNumbering(Reg);
1080 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1081 Binary |= (RegNo & 0x1f) << 8;
1085 Binary |= NumRegs * 2;
1087 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1088 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1089 Binary |= 1 << RegNo;
1096 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1097 /// with the alignment operand.
1098 unsigned ARMMCCodeEmitter::
1099 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1100 SmallVectorImpl<MCFixup> &Fixups) const {
1101 const MCOperand &Reg = MI.getOperand(Op);
1102 const MCOperand &Imm = MI.getOperand(Op + 1);
1104 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1107 switch (Imm.getImm()) {
1111 case 8: Align = 0x01; break;
1112 case 16: Align = 0x02; break;
1113 case 32: Align = 0x03; break;
1116 return RegNo | (Align << 4);
1119 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1120 /// alignment operand for use in VLD-dup instructions. This is the same as
1121 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1122 /// different for VLD4-dup.
1123 unsigned ARMMCCodeEmitter::
1124 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1125 SmallVectorImpl<MCFixup> &Fixups) const {
1126 const MCOperand &Reg = MI.getOperand(Op);
1127 const MCOperand &Imm = MI.getOperand(Op + 1);
1129 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1132 switch (Imm.getImm()) {
1136 case 8: Align = 0x01; break;
1137 case 16: Align = 0x03; break;
1140 return RegNo | (Align << 4);
1143 unsigned ARMMCCodeEmitter::
1144 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1145 SmallVectorImpl<MCFixup> &Fixups) const {
1146 const MCOperand &MO = MI.getOperand(Op);
1147 if (MO.getReg() == 0) return 0x0D;
1151 void ARMMCCodeEmitter::
1152 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1153 SmallVectorImpl<MCFixup> &Fixups) const {
1154 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1155 // Pseudo instructions don't get encoded.
1156 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1157 uint64_t TSFlags = Desc.TSFlags;
1158 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1161 // Basic size info comes from the TSFlags field.
1162 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1163 default: llvm_unreachable("Unexpected instruction size!");
1164 case ARMII::Size2Bytes: Size = 2; break;
1165 case ARMII::Size4Bytes: Size = 4; break;
1167 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1168 // Thumb 32-bit wide instructions need to emit the high order halfword
1170 if (Subtarget.isThumb() && Size == 4) {
1171 EmitConstant(Binary >> 16, 2, OS);
1172 EmitConstant(Binary & 0xffff, 2, OS);
1174 EmitConstant(Binary, Size, OS);
1175 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1178 #include "ARMGenMCCodeEmitter.inc"