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 offset bits flags
49 { "fixup_arm_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52 { "fixup_arm_movt_hi16", 0, 16, 0 },
53 { "fixup_arm_movw_lo16", 0, 16, 0 },
56 if (Kind < FirstTargetFixupKind)
57 return MCCodeEmitter::getFixupKindInfo(Kind);
59 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
61 return Infos[Kind - FirstTargetFixupKind];
63 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
65 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
67 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
72 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
75 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
76 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
77 SmallVectorImpl<MCFixup> &Fixups) const;
79 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
80 unsigned &Reg, unsigned &Imm,
81 SmallVectorImpl<MCFixup> &Fixups) const;
83 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
85 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
86 SmallVectorImpl<MCFixup> &Fixups) const;
88 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
90 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups) const;
93 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
95 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
99 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
100 /// operand as needed by load/store instructions.
101 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
104 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
105 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const {
107 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
109 default: assert(0 && "Unknown addressing sub-mode!");
110 case ARM_AM::da: return 0;
111 case ARM_AM::ia: return 1;
112 case ARM_AM::db: return 2;
113 case ARM_AM::ib: return 3;
116 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
118 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
120 default: llvm_unreachable("Unknown shift opc!");
121 case ARM_AM::no_shift:
122 case ARM_AM::lsl: return 0;
123 case ARM_AM::lsr: return 1;
124 case ARM_AM::asr: return 2;
126 case ARM_AM::rrx: return 3;
131 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
132 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
135 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
136 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
139 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
140 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
143 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
144 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
147 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
148 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
151 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
152 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
155 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
156 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const;
159 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
160 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
161 SmallVectorImpl<MCFixup> &Fixups) const;
163 /// getCCOutOpValue - Return encoding of the 's' bit.
164 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
165 SmallVectorImpl<MCFixup> &Fixups) const {
166 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
168 return MI.getOperand(Op).getReg() == ARM::CPSR;
171 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
172 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
173 SmallVectorImpl<MCFixup> &Fixups) const {
174 unsigned SoImm = MI.getOperand(Op).getImm();
175 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
176 assert(SoImmVal != -1 && "Not a valid so_imm value!");
178 // Encode rotate_imm.
179 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
180 << ARMII::SoRotImmShift;
183 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
187 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
188 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
189 SmallVectorImpl<MCFixup> &Fixups) const {
190 unsigned SoImm = MI.getOperand(Op).getImm();
191 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
192 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
196 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
197 SmallVectorImpl<MCFixup> &Fixups) const;
198 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
199 SmallVectorImpl<MCFixup> &Fixups) const;
200 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
201 SmallVectorImpl<MCFixup> &Fixups) const;
202 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
203 SmallVectorImpl<MCFixup> &Fixups) const;
205 /// getSORegOpValue - Return an encoded so_reg shifted register value.
206 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const;
208 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const;
211 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const {
213 switch (MI.getOperand(Op).getImm()) {
214 default: assert (0 && "Not a valid rot_imm value!");
222 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
223 SmallVectorImpl<MCFixup> &Fixups) const {
224 return MI.getOperand(Op).getImm() - 1;
227 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
229 return 64 - MI.getOperand(Op).getImm();
232 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const;
235 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
237 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const;
239 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
240 SmallVectorImpl<MCFixup> &Fixups) const;
241 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const;
244 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
245 unsigned EncodedValue) const;
246 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
247 unsigned EncodedValue) const;
248 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
249 unsigned EncodedValue) const;
251 void EmitByte(unsigned char C, raw_ostream &OS) const {
255 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
256 // Output the constant in little endian byte order.
257 for (unsigned i = 0; i != Size; ++i) {
258 EmitByte(Val & 255, OS);
263 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
264 SmallVectorImpl<MCFixup> &Fixups) const;
267 } // end anonymous namespace
269 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
271 return new ARMMCCodeEmitter(TM, Ctx);
274 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
275 /// instructions, and rewrite them to their Thumb2 form if we are currently in
277 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
278 unsigned EncodedValue) const {
279 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
280 if (Subtarget.isThumb2()) {
281 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
282 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
284 unsigned Bit24 = EncodedValue & 0x01000000;
285 unsigned Bit28 = Bit24 << 4;
286 EncodedValue &= 0xEFFFFFFF;
287 EncodedValue |= Bit28;
288 EncodedValue |= 0x0F000000;
294 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
295 /// instructions, and rewrite them to their Thumb2 form if we are currently in
297 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
298 unsigned EncodedValue) const {
299 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
300 if (Subtarget.isThumb2()) {
301 EncodedValue &= 0xF0FFFFFF;
302 EncodedValue |= 0x09000000;
308 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
309 /// instructions, and rewrite them to their Thumb2 form if we are currently in
311 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
312 unsigned EncodedValue) const {
313 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
314 if (Subtarget.isThumb2()) {
315 EncodedValue &= 0x00FFFFFF;
316 EncodedValue |= 0xEE000000;
324 /// getMachineOpValue - Return binary encoding of operand. If the machine
325 /// operand requires relocation, record the relocation and return zero.
326 unsigned ARMMCCodeEmitter::
327 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
328 SmallVectorImpl<MCFixup> &Fixups) const {
330 unsigned Reg = MO.getReg();
331 unsigned RegNo = getARMRegisterNumbering(Reg);
333 // Q registers are encoded as 2x their register number.
337 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
338 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
339 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
340 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
343 } else if (MO.isImm()) {
344 return static_cast<unsigned>(MO.getImm());
345 } else if (MO.isFPImm()) {
346 return static_cast<unsigned>(APFloat(MO.getFPImm())
347 .bitcastToAPInt().getHiBits(32).getLimitedValue());
350 llvm_unreachable("Unable to encode MCOperand!");
354 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
355 bool ARMMCCodeEmitter::
356 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
357 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
358 const MCOperand &MO = MI.getOperand(OpIdx);
359 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
361 Reg = getARMRegisterNumbering(MO.getReg());
363 int32_t SImm = MO1.getImm();
366 // Special value for #-0
367 if (SImm == INT32_MIN)
370 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
380 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
382 uint32_t ARMMCCodeEmitter::
383 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
384 SmallVectorImpl<MCFixup> &Fixups) const {
385 const MCOperand &MO = MI.getOperand(OpIdx);
387 // If the destination is an immediate, we have nothing to do.
388 if (MO.isImm()) return MO.getImm();
389 assert (MO.isExpr() && "Unexpected branch target type!");
390 const MCExpr *Expr = MO.getExpr();
391 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
392 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
394 // All of the information is in the fixup.
398 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
399 uint32_t ARMMCCodeEmitter::
400 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
401 SmallVectorImpl<MCFixup> &Fixups) const {
403 // {12} = (U)nsigned (add == '1', sub == '0')
407 // If The first operand isn't a register, we have a label reference.
408 const MCOperand &MO = MI.getOperand(OpIdx);
410 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
412 isAdd = false ; // 'U' bit is set as part of the fixup.
414 assert(MO.isExpr() && "Unexpected machine operand type!");
415 const MCExpr *Expr = MO.getExpr();
416 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
417 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
419 ++MCNumCPRelocations;
421 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
423 uint32_t Binary = Imm12 & 0xfff;
424 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
427 Binary |= (Reg << 13);
431 /// getT2AddrModeImm8s4OpValue - Return encoding info for
432 /// 'reg +/- imm8<<2' operand.
433 uint32_t ARMMCCodeEmitter::
434 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
435 SmallVectorImpl<MCFixup> &Fixups) const {
437 // {12} = (U)nsigned (add == '1', sub == '0')
441 // If The first operand isn't a register, we have a label reference.
442 const MCOperand &MO = MI.getOperand(OpIdx);
444 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
446 isAdd = false ; // 'U' bit is set as part of the fixup.
448 assert(MO.isExpr() && "Unexpected machine operand type!");
449 const MCExpr *Expr = MO.getExpr();
450 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
451 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
453 ++MCNumCPRelocations;
455 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
457 uint32_t Binary = (Imm8 >> 2) & 0xff;
458 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
461 Binary |= (Reg << 9);
465 uint32_t ARMMCCodeEmitter::
466 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
467 SmallVectorImpl<MCFixup> &Fixups) const {
468 // {20-16} = imm{15-12}
469 // {11-0} = imm{11-0}
470 const MCOperand &MO = MI.getOperand(OpIdx);
472 return static_cast<unsigned>(MO.getImm());
473 } else if (const MCSymbolRefExpr *Expr =
474 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
476 switch (Expr->getKind()) {
477 default: assert(0 && "Unsupported ARMFixup");
478 case MCSymbolRefExpr::VK_ARM_HI16:
479 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
481 case MCSymbolRefExpr::VK_ARM_LO16:
482 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
485 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
488 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
492 uint32_t ARMMCCodeEmitter::
493 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
495 const MCOperand &MO = MI.getOperand(OpIdx);
496 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
497 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
498 unsigned Rn = getARMRegisterNumbering(MO.getReg());
499 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
500 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
501 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
502 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
503 unsigned SBits = getShiftOp(ShOp);
512 uint32_t Binary = Rm;
514 Binary |= SBits << 5;
515 Binary |= ShImm << 7;
521 uint32_t ARMMCCodeEmitter::
522 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
523 SmallVectorImpl<MCFixup> &Fixups) const {
525 // {13} 1 == imm12, 0 == Rm
528 const MCOperand &MO = MI.getOperand(OpIdx);
529 unsigned Rn = getARMRegisterNumbering(MO.getReg());
530 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
535 uint32_t ARMMCCodeEmitter::
536 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
537 SmallVectorImpl<MCFixup> &Fixups) const {
538 // {13} 1 == imm12, 0 == Rm
541 const MCOperand &MO = MI.getOperand(OpIdx);
542 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
543 unsigned Imm = MO1.getImm();
544 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
545 bool isReg = MO.getReg() != 0;
546 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
547 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
549 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
550 Binary <<= 7; // Shift amount is bits [11:7]
551 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
552 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
554 return Binary | (isAdd << 12) | (isReg << 13);
557 uint32_t ARMMCCodeEmitter::
558 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
559 SmallVectorImpl<MCFixup> &Fixups) const {
560 // {9} 1 == imm8, 0 == Rm
564 const MCOperand &MO = MI.getOperand(OpIdx);
565 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
566 unsigned Imm = MO1.getImm();
567 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
568 bool isImm = MO.getReg() == 0;
569 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
570 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
572 Imm8 = getARMRegisterNumbering(MO.getReg());
573 return Imm8 | (isAdd << 8) | (isImm << 9);
576 uint32_t ARMMCCodeEmitter::
577 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
578 SmallVectorImpl<MCFixup> &Fixups) const {
579 // {13} 1 == imm8, 0 == Rm
584 const MCOperand &MO = MI.getOperand(OpIdx);
585 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
586 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
587 unsigned Rn = getARMRegisterNumbering(MO.getReg());
588 unsigned Imm = MO2.getImm();
589 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
590 bool isImm = MO1.getReg() == 0;
591 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
592 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
594 Imm8 = getARMRegisterNumbering(MO1.getReg());
595 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
598 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
599 static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
608 const MCOperand &MO = MI.getOperand(OpIdx);
609 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
610 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
611 unsigned Rn = getARMRegisterNumbering(MO.getReg());
612 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
613 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
614 return (Rm << 3) | (Imm5 << 3) | Rn;
617 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
618 uint32_t ARMMCCodeEmitter::
619 getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &) const {
621 return getAddrModeSOpValue(MI, OpIdx, 4);
624 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
625 uint32_t ARMMCCodeEmitter::
626 getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &) const {
628 return getAddrModeSOpValue(MI, OpIdx, 2);
631 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
632 uint32_t ARMMCCodeEmitter::
633 getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
634 SmallVectorImpl<MCFixup> &) const {
635 return getAddrModeSOpValue(MI, OpIdx, 1);
638 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
639 uint32_t ARMMCCodeEmitter::
640 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
641 SmallVectorImpl<MCFixup> &Fixups) const {
643 // {8} = (U)nsigned (add == '1', sub == '0')
647 // If The first operand isn't a register, we have a label reference.
648 const MCOperand &MO = MI.getOperand(OpIdx);
650 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
652 isAdd = false; // 'U' bit is handled as part of the fixup.
654 assert(MO.isExpr() && "Unexpected machine operand type!");
655 const MCExpr *Expr = MO.getExpr();
656 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
657 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
659 ++MCNumCPRelocations;
661 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
662 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
665 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
666 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
669 Binary |= (Reg << 9);
673 unsigned ARMMCCodeEmitter::
674 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
675 SmallVectorImpl<MCFixup> &Fixups) const {
676 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
677 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
678 // case the imm contains the amount to shift by.
681 // {4} = 1 if reg shift, 0 if imm shift
689 const MCOperand &MO = MI.getOperand(OpIdx);
690 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
691 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
692 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
695 unsigned Binary = getARMRegisterNumbering(MO.getReg());
697 // Encode the shift opcode.
699 unsigned Rs = MO1.getReg();
701 // Set shift operand (bit[7:4]).
706 // RRX - 0110 and bit[11:8] clear.
708 default: llvm_unreachable("Unknown shift opc!");
709 case ARM_AM::lsl: SBits = 0x1; break;
710 case ARM_AM::lsr: SBits = 0x3; break;
711 case ARM_AM::asr: SBits = 0x5; break;
712 case ARM_AM::ror: SBits = 0x7; break;
713 case ARM_AM::rrx: SBits = 0x6; break;
716 // Set shift operand (bit[6:4]).
722 default: llvm_unreachable("Unknown shift opc!");
723 case ARM_AM::lsl: SBits = 0x0; break;
724 case ARM_AM::lsr: SBits = 0x2; break;
725 case ARM_AM::asr: SBits = 0x4; break;
726 case ARM_AM::ror: SBits = 0x6; break;
730 Binary |= SBits << 4;
731 if (SOpc == ARM_AM::rrx)
734 // Encode the shift operation Rs or shift_imm (except rrx).
736 // Encode Rs bit[11:8].
737 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
738 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
741 // Encode shift_imm bit[11:7].
742 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
745 unsigned ARMMCCodeEmitter::
746 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
747 SmallVectorImpl<MCFixup> &Fixups) const {
748 const MCOperand &MO1 = MI.getOperand(OpNum);
749 const MCOperand &MO2 = MI.getOperand(OpNum+1);
750 const MCOperand &MO3 = MI.getOperand(OpNum+2);
752 // Encoded as [Rn, Rm, imm].
753 // FIXME: Needs fixup support.
754 unsigned Value = getARMRegisterNumbering(MO1.getReg());
756 Value |= getARMRegisterNumbering(MO2.getReg());
758 Value |= MO3.getImm();
763 unsigned ARMMCCodeEmitter::
764 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
765 SmallVectorImpl<MCFixup> &Fixups) const {
766 const MCOperand &MO1 = MI.getOperand(OpNum);
767 const MCOperand &MO2 = MI.getOperand(OpNum+1);
769 // FIXME: Needs fixup support.
770 unsigned Value = getARMRegisterNumbering(MO1.getReg());
772 // Even though the immediate is 8 bits long, we need 9 bits in order
773 // to represent the (inverse of the) sign bit.
775 int32_t tmp = (int32_t)MO2.getImm();
779 Value |= 256; // Set the ADD bit
784 unsigned ARMMCCodeEmitter::
785 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
786 SmallVectorImpl<MCFixup> &Fixups) const {
787 const MCOperand &MO1 = MI.getOperand(OpNum);
789 // FIXME: Needs fixup support.
791 int32_t tmp = (int32_t)MO1.getImm();
795 Value |= 256; // Set the ADD bit
800 unsigned ARMMCCodeEmitter::
801 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
802 SmallVectorImpl<MCFixup> &Fixups) const {
803 const MCOperand &MO1 = MI.getOperand(OpNum);
805 // FIXME: Needs fixup support.
807 int32_t tmp = (int32_t)MO1.getImm();
811 Value |= 4096; // Set the ADD bit
816 unsigned ARMMCCodeEmitter::
817 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
818 SmallVectorImpl<MCFixup> &Fixups) const {
819 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
820 // shifted. The second is the amount to shift by.
827 const MCOperand &MO = MI.getOperand(OpIdx);
828 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
829 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
832 unsigned Binary = getARMRegisterNumbering(MO.getReg());
834 // Encode the shift opcode.
836 // Set shift operand (bit[6:4]).
842 default: llvm_unreachable("Unknown shift opc!");
843 case ARM_AM::lsl: SBits = 0x0; break;
844 case ARM_AM::lsr: SBits = 0x2; break;
845 case ARM_AM::asr: SBits = 0x4; break;
846 case ARM_AM::ror: SBits = 0x6; break;
849 Binary |= SBits << 4;
850 if (SOpc == ARM_AM::rrx)
853 // Encode shift_imm bit[11:7].
854 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
857 unsigned ARMMCCodeEmitter::
858 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
859 SmallVectorImpl<MCFixup> &Fixups) const {
860 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
862 const MCOperand &MO = MI.getOperand(Op);
863 uint32_t v = ~MO.getImm();
864 uint32_t lsb = CountTrailingZeros_32(v);
865 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
866 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
867 return lsb | (msb << 5);
870 unsigned ARMMCCodeEmitter::
871 getRegisterListOpValue(const MCInst &MI, unsigned Op,
872 SmallVectorImpl<MCFixup> &Fixups) const {
875 // {7-0} = Number of registers
878 // {15-0} = Bitfield of GPRs.
879 unsigned Reg = MI.getOperand(Op).getReg();
880 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
881 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
885 if (SPRRegs || DPRRegs) {
887 unsigned RegNo = getARMRegisterNumbering(Reg);
888 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
889 Binary |= (RegNo & 0x1f) << 8;
893 Binary |= NumRegs * 2;
895 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
896 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
897 Binary |= 1 << RegNo;
904 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
905 /// with the alignment operand.
906 unsigned ARMMCCodeEmitter::
907 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
908 SmallVectorImpl<MCFixup> &Fixups) const {
909 const MCOperand &Reg = MI.getOperand(Op);
910 const MCOperand &Imm = MI.getOperand(Op + 1);
912 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
915 switch (Imm.getImm()) {
919 case 8: Align = 0x01; break;
920 case 16: Align = 0x02; break;
921 case 32: Align = 0x03; break;
924 return RegNo | (Align << 4);
927 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
928 /// alignment operand for use in VLD-dup instructions. This is the same as
929 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
930 /// different for VLD4-dup.
931 unsigned ARMMCCodeEmitter::
932 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
933 SmallVectorImpl<MCFixup> &Fixups) const {
934 const MCOperand &Reg = MI.getOperand(Op);
935 const MCOperand &Imm = MI.getOperand(Op + 1);
937 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
940 switch (Imm.getImm()) {
944 case 8: Align = 0x01; break;
945 case 16: Align = 0x03; break;
948 return RegNo | (Align << 4);
951 unsigned ARMMCCodeEmitter::
952 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
953 SmallVectorImpl<MCFixup> &Fixups) const {
954 const MCOperand &MO = MI.getOperand(Op);
955 if (MO.getReg() == 0) return 0x0D;
959 void ARMMCCodeEmitter::
960 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
961 SmallVectorImpl<MCFixup> &Fixups) const {
962 // Pseudo instructions don't get encoded.
963 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
964 uint64_t TSFlags = Desc.TSFlags;
965 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
968 // Basic size info comes from the TSFlags field.
969 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
970 default: llvm_unreachable("Unexpected instruction size!");
971 case ARMII::Size2Bytes: Size = 2; break;
972 case ARMII::Size4Bytes: Size = 4; break;
974 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
975 ++MCNumEmitted; // Keep track of the # of mi's emitted.
978 #include "ARMGenMCCodeEmitter.inc"