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 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
52 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
54 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
56 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
57 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
58 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
59 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_arm_movt_hi16", 0, 16, 0 },
61 { "fixup_arm_movw_lo16", 0, 16, 0 },
64 if (Kind < FirstTargetFixupKind)
65 return MCCodeEmitter::getFixupKindInfo(Kind);
67 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
69 return Infos[Kind - FirstTargetFixupKind];
71 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
73 // getBinaryCodeForInstr - TableGen'erated function for getting the
74 // binary encoding for an instruction.
75 unsigned getBinaryCodeForInstr(const MCInst &MI,
76 SmallVectorImpl<MCFixup> &Fixups) const;
78 /// getMachineOpValue - Return binary encoding of operand. If the machine
79 /// operand requires relocation, record the relocation and return zero.
80 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
81 SmallVectorImpl<MCFixup> &Fixups) const;
83 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
84 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
85 SmallVectorImpl<MCFixup> &Fixups) const;
87 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
88 unsigned &Reg, unsigned &Imm,
89 SmallVectorImpl<MCFixup> &Fixups) const;
91 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
93 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
96 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
97 /// BLX branch target.
98 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
101 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
102 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
105 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
107 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
108 SmallVectorImpl<MCFixup> &Fixups) const;
110 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
111 /// ADR label target.
112 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
115 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
117 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups) const;
120 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
122 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
123 SmallVectorImpl<MCFixup> &Fixups) const;
126 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
127 /// operand as needed by load/store instructions.
128 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
131 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
132 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const {
134 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
136 default: assert(0 && "Unknown addressing sub-mode!");
137 case ARM_AM::da: return 0;
138 case ARM_AM::ia: return 1;
139 case ARM_AM::db: return 2;
140 case ARM_AM::ib: return 3;
143 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
145 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
147 default: llvm_unreachable("Unknown shift opc!");
148 case ARM_AM::no_shift:
149 case ARM_AM::lsl: return 0;
150 case ARM_AM::lsr: return 1;
151 case ARM_AM::asr: return 2;
153 case ARM_AM::rrx: return 3;
158 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
159 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
162 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
163 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
166 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
167 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const;
170 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
171 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
172 SmallVectorImpl<MCFixup> &Fixups) const;
174 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
176 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
177 SmallVectorImpl<MCFixup> &Fixups) const;
179 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
180 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups) const;
183 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
184 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups) const;
187 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
188 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
189 SmallVectorImpl<MCFixup> &Fixups) const;
191 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
192 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
195 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
196 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
199 /// getCCOutOpValue - Return encoding of the 's' bit.
200 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const {
202 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
204 return MI.getOperand(Op).getReg() == ARM::CPSR;
207 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
208 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const {
210 unsigned SoImm = MI.getOperand(Op).getImm();
211 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
212 assert(SoImmVal != -1 && "Not a valid so_imm value!");
214 // Encode rotate_imm.
215 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
216 << ARMII::SoRotImmShift;
219 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
223 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
224 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const {
226 unsigned SoImm = MI.getOperand(Op).getImm();
227 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
228 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
232 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
233 SmallVectorImpl<MCFixup> &Fixups) const;
234 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
235 SmallVectorImpl<MCFixup> &Fixups) const;
236 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
237 SmallVectorImpl<MCFixup> &Fixups) const;
238 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
239 SmallVectorImpl<MCFixup> &Fixups) const;
241 /// getSORegOpValue - Return an encoded so_reg shifted register value.
242 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
243 SmallVectorImpl<MCFixup> &Fixups) const;
244 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
245 SmallVectorImpl<MCFixup> &Fixups) const;
247 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const {
249 switch (MI.getOperand(Op).getImm()) {
250 default: assert (0 && "Not a valid rot_imm value!");
258 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const {
260 return MI.getOperand(Op).getImm() - 1;
263 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const {
265 return 64 - MI.getOperand(Op).getImm();
268 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
271 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
273 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
275 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const;
277 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
280 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
281 unsigned EncodedValue) const;
282 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
283 unsigned EncodedValue) const;
284 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
285 unsigned EncodedValue) const;
287 unsigned VFPThumb2PostEncoder(const MCInst &MI,
288 unsigned EncodedValue) const;
290 void EmitByte(unsigned char C, raw_ostream &OS) const {
294 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
295 // Output the constant in little endian byte order.
296 for (unsigned i = 0; i != Size; ++i) {
297 EmitByte(Val & 255, OS);
302 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
303 SmallVectorImpl<MCFixup> &Fixups) const;
306 } // end anonymous namespace
308 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
310 return new ARMMCCodeEmitter(TM, Ctx);
313 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
314 /// instructions, and rewrite them to their Thumb2 form if we are currently in
316 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const {
318 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
319 if (Subtarget.isThumb2()) {
320 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
321 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
323 unsigned Bit24 = EncodedValue & 0x01000000;
324 unsigned Bit28 = Bit24 << 4;
325 EncodedValue &= 0xEFFFFFFF;
326 EncodedValue |= Bit28;
327 EncodedValue |= 0x0F000000;
333 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
334 /// instructions, and rewrite them to their Thumb2 form if we are currently in
336 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
337 unsigned EncodedValue) const {
338 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
339 if (Subtarget.isThumb2()) {
340 EncodedValue &= 0xF0FFFFFF;
341 EncodedValue |= 0x09000000;
347 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
348 /// instructions, and rewrite them to their Thumb2 form if we are currently in
350 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
351 unsigned EncodedValue) const {
352 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
353 if (Subtarget.isThumb2()) {
354 EncodedValue &= 0x00FFFFFF;
355 EncodedValue |= 0xEE000000;
361 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
362 /// them to their Thumb2 form if we are currently in Thumb2 mode.
363 unsigned ARMMCCodeEmitter::
364 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
365 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
366 EncodedValue &= 0x0FFFFFFF;
367 EncodedValue |= 0xE0000000;
372 /// getMachineOpValue - Return binary encoding of operand. If the machine
373 /// operand requires relocation, record the relocation and return zero.
374 unsigned ARMMCCodeEmitter::
375 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
376 SmallVectorImpl<MCFixup> &Fixups) const {
378 unsigned Reg = MO.getReg();
379 unsigned RegNo = getARMRegisterNumbering(Reg);
381 // Q registers are encoded as 2x their register number.
385 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
386 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
387 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
388 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
391 } else if (MO.isImm()) {
392 return static_cast<unsigned>(MO.getImm());
393 } else if (MO.isFPImm()) {
394 return static_cast<unsigned>(APFloat(MO.getFPImm())
395 .bitcastToAPInt().getHiBits(32).getLimitedValue());
398 llvm_unreachable("Unable to encode MCOperand!");
402 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
403 bool ARMMCCodeEmitter::
404 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
405 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
406 const MCOperand &MO = MI.getOperand(OpIdx);
407 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
409 Reg = getARMRegisterNumbering(MO.getReg());
411 int32_t SImm = MO1.getImm();
414 // Special value for #-0
415 if (SImm == INT32_MIN)
418 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
428 /// getBranchTargetOpValue - Helper function to get the branch target operand,
429 /// which is either an immediate or requires a fixup.
430 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
432 SmallVectorImpl<MCFixup> &Fixups) {
433 const MCOperand &MO = MI.getOperand(OpIdx);
435 // If the destination is an immediate, we have nothing to do.
436 if (MO.isImm()) return MO.getImm();
437 assert(MO.isExpr() && "Unexpected branch target type!");
438 const MCExpr *Expr = MO.getExpr();
439 MCFixupKind Kind = MCFixupKind(FixupKind);
440 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
442 // All of the information is in the fixup.
446 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
447 uint32_t ARMMCCodeEmitter::
448 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
449 SmallVectorImpl<MCFixup> &Fixups) const {
450 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
453 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
454 /// BLX branch target.
455 uint32_t ARMMCCodeEmitter::
456 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
457 SmallVectorImpl<MCFixup> &Fixups) const {
458 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
461 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
462 uint32_t ARMMCCodeEmitter::
463 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
464 SmallVectorImpl<MCFixup> &Fixups) const {
465 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
468 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
470 uint32_t ARMMCCodeEmitter::
471 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
473 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
474 if (Subtarget.isThumb2())
475 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
479 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
481 uint32_t ARMMCCodeEmitter::
482 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
483 SmallVectorImpl<MCFixup> &Fixups) const {
484 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
485 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
489 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
490 uint32_t ARMMCCodeEmitter::
491 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
492 SmallVectorImpl<MCFixup> &Fixups) const {
494 // {12} = (U)nsigned (add == '1', sub == '0')
498 // If The first operand isn't a register, we have a label reference.
499 const MCOperand &MO = MI.getOperand(OpIdx);
500 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
501 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
502 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
504 isAdd = false ; // 'U' bit is set as part of the fixup.
506 const MCExpr *Expr = 0;
510 Expr = MO2.getExpr();
512 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
514 if (Subtarget.isThumb2())
515 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
517 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
518 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
520 ++MCNumCPRelocations;
522 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
524 uint32_t Binary = Imm12 & 0xfff;
525 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
528 Binary |= (Reg << 13);
532 /// getT2AddrModeImm8s4OpValue - Return encoding info for
533 /// 'reg +/- imm8<<2' operand.
534 uint32_t ARMMCCodeEmitter::
535 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
536 SmallVectorImpl<MCFixup> &Fixups) const {
538 // {12} = (U)nsigned (add == '1', sub == '0')
542 // If The first operand isn't a register, we have a label reference.
543 const MCOperand &MO = MI.getOperand(OpIdx);
545 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
547 isAdd = false ; // 'U' bit is set as part of the fixup.
549 assert(MO.isExpr() && "Unexpected machine operand type!");
550 const MCExpr *Expr = MO.getExpr();
551 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
552 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
554 ++MCNumCPRelocations;
556 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
558 uint32_t Binary = (Imm8 >> 2) & 0xff;
559 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
562 Binary |= (Reg << 9);
566 uint32_t ARMMCCodeEmitter::
567 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
568 SmallVectorImpl<MCFixup> &Fixups) const {
569 // {20-16} = imm{15-12}
570 // {11-0} = imm{11-0}
571 const MCOperand &MO = MI.getOperand(OpIdx);
573 return static_cast<unsigned>(MO.getImm());
574 } else if (const MCSymbolRefExpr *Expr =
575 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
577 switch (Expr->getKind()) {
578 default: assert(0 && "Unsupported ARMFixup");
579 case MCSymbolRefExpr::VK_ARM_HI16:
580 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
582 case MCSymbolRefExpr::VK_ARM_LO16:
583 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
586 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
589 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
593 uint32_t ARMMCCodeEmitter::
594 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
596 const MCOperand &MO = MI.getOperand(OpIdx);
597 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
598 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
599 unsigned Rn = getARMRegisterNumbering(MO.getReg());
600 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
601 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
602 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
603 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
604 unsigned SBits = getShiftOp(ShOp);
613 uint32_t Binary = Rm;
615 Binary |= SBits << 5;
616 Binary |= ShImm << 7;
622 uint32_t ARMMCCodeEmitter::
623 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
624 SmallVectorImpl<MCFixup> &Fixups) const {
626 // {13} 1 == imm12, 0 == Rm
629 const MCOperand &MO = MI.getOperand(OpIdx);
630 unsigned Rn = getARMRegisterNumbering(MO.getReg());
631 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
636 uint32_t ARMMCCodeEmitter::
637 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
638 SmallVectorImpl<MCFixup> &Fixups) const {
639 // {13} 1 == imm12, 0 == Rm
642 const MCOperand &MO = MI.getOperand(OpIdx);
643 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
644 unsigned Imm = MO1.getImm();
645 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
646 bool isReg = MO.getReg() != 0;
647 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
648 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
650 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
651 Binary <<= 7; // Shift amount is bits [11:7]
652 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
653 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
655 return Binary | (isAdd << 12) | (isReg << 13);
658 uint32_t ARMMCCodeEmitter::
659 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
660 SmallVectorImpl<MCFixup> &Fixups) const {
661 // {9} 1 == imm8, 0 == Rm
665 const MCOperand &MO = MI.getOperand(OpIdx);
666 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
667 unsigned Imm = MO1.getImm();
668 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
669 bool isImm = MO.getReg() == 0;
670 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
671 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
673 Imm8 = getARMRegisterNumbering(MO.getReg());
674 return Imm8 | (isAdd << 8) | (isImm << 9);
677 uint32_t ARMMCCodeEmitter::
678 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups) const {
680 // {13} 1 == imm8, 0 == Rm
685 const MCOperand &MO = MI.getOperand(OpIdx);
686 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
687 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
688 unsigned Rn = getARMRegisterNumbering(MO.getReg());
689 unsigned Imm = MO2.getImm();
690 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
691 bool isImm = MO1.getReg() == 0;
692 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
693 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
695 Imm8 = getARMRegisterNumbering(MO1.getReg());
696 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
699 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
700 uint32_t ARMMCCodeEmitter::
701 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
702 SmallVectorImpl<MCFixup> &Fixups) const {
705 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
706 #if 0 // FIXME: This crashes2003-05-14-initialize-string.c
707 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
708 "Unexpected base register!");
710 // The immediate is already shifted for the implicit zeroes, so no change
712 return MO1.getImm() & 0xff;
715 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
716 static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
725 const MCOperand &MO = MI.getOperand(OpIdx);
726 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
727 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
728 unsigned Rn = getARMRegisterNumbering(MO.getReg());
729 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
731 if (MO2.getReg() != 0)
733 Imm5 = getARMRegisterNumbering(MO2.getReg());
735 return (Imm5 << 3) | Rn;
738 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
739 uint32_t ARMMCCodeEmitter::
740 getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
741 SmallVectorImpl<MCFixup> &) const {
742 return getAddrModeSOpValue(MI, OpIdx, 4);
745 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
746 uint32_t ARMMCCodeEmitter::
747 getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &) const {
749 return getAddrModeSOpValue(MI, OpIdx, 2);
752 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
753 uint32_t ARMMCCodeEmitter::
754 getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
755 SmallVectorImpl<MCFixup> &) const {
756 return getAddrModeSOpValue(MI, OpIdx, 1);
759 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
760 uint32_t ARMMCCodeEmitter::
761 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
762 SmallVectorImpl<MCFixup> &Fixups) const {
763 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
766 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
767 uint32_t ARMMCCodeEmitter::
768 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
769 SmallVectorImpl<MCFixup> &Fixups) const {
771 // {8} = (U)nsigned (add == '1', sub == '0')
775 // If The first operand isn't a register, we have a label reference.
776 const MCOperand &MO = MI.getOperand(OpIdx);
778 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
780 isAdd = false; // 'U' bit is handled as part of the fixup.
782 assert(MO.isExpr() && "Unexpected machine operand type!");
783 const MCExpr *Expr = MO.getExpr();
785 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
786 if (Subtarget.isThumb2())
787 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
789 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
790 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
792 ++MCNumCPRelocations;
794 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
795 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
798 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
799 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
802 Binary |= (Reg << 9);
806 unsigned ARMMCCodeEmitter::
807 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
808 SmallVectorImpl<MCFixup> &Fixups) const {
809 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
810 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
811 // case the imm contains the amount to shift by.
814 // {4} = 1 if reg shift, 0 if imm shift
822 const MCOperand &MO = MI.getOperand(OpIdx);
823 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
824 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
825 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
828 unsigned Binary = getARMRegisterNumbering(MO.getReg());
830 // Encode the shift opcode.
832 unsigned Rs = MO1.getReg();
834 // Set shift operand (bit[7:4]).
839 // RRX - 0110 and bit[11:8] clear.
841 default: llvm_unreachable("Unknown shift opc!");
842 case ARM_AM::lsl: SBits = 0x1; break;
843 case ARM_AM::lsr: SBits = 0x3; break;
844 case ARM_AM::asr: SBits = 0x5; break;
845 case ARM_AM::ror: SBits = 0x7; break;
846 case ARM_AM::rrx: SBits = 0x6; break;
849 // Set shift operand (bit[6:4]).
855 default: llvm_unreachable("Unknown shift opc!");
856 case ARM_AM::lsl: SBits = 0x0; break;
857 case ARM_AM::lsr: SBits = 0x2; break;
858 case ARM_AM::asr: SBits = 0x4; break;
859 case ARM_AM::ror: SBits = 0x6; break;
863 Binary |= SBits << 4;
864 if (SOpc == ARM_AM::rrx)
867 // Encode the shift operation Rs or shift_imm (except rrx).
869 // Encode Rs bit[11:8].
870 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
871 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
874 // Encode shift_imm bit[11:7].
875 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
878 unsigned ARMMCCodeEmitter::
879 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
880 SmallVectorImpl<MCFixup> &Fixups) const {
881 const MCOperand &MO1 = MI.getOperand(OpNum);
882 const MCOperand &MO2 = MI.getOperand(OpNum+1);
883 const MCOperand &MO3 = MI.getOperand(OpNum+2);
885 // Encoded as [Rn, Rm, imm].
886 // FIXME: Needs fixup support.
887 unsigned Value = getARMRegisterNumbering(MO1.getReg());
889 Value |= getARMRegisterNumbering(MO2.getReg());
891 Value |= MO3.getImm();
896 unsigned ARMMCCodeEmitter::
897 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
898 SmallVectorImpl<MCFixup> &Fixups) const {
899 const MCOperand &MO1 = MI.getOperand(OpNum);
900 const MCOperand &MO2 = MI.getOperand(OpNum+1);
902 // FIXME: Needs fixup support.
903 unsigned Value = getARMRegisterNumbering(MO1.getReg());
905 // Even though the immediate is 8 bits long, we need 9 bits in order
906 // to represent the (inverse of the) sign bit.
908 int32_t tmp = (int32_t)MO2.getImm();
912 Value |= 256; // Set the ADD bit
917 unsigned ARMMCCodeEmitter::
918 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
919 SmallVectorImpl<MCFixup> &Fixups) const {
920 const MCOperand &MO1 = MI.getOperand(OpNum);
922 // FIXME: Needs fixup support.
924 int32_t tmp = (int32_t)MO1.getImm();
928 Value |= 256; // Set the ADD bit
933 unsigned ARMMCCodeEmitter::
934 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
935 SmallVectorImpl<MCFixup> &Fixups) const {
936 const MCOperand &MO1 = MI.getOperand(OpNum);
938 // FIXME: Needs fixup support.
940 int32_t tmp = (int32_t)MO1.getImm();
944 Value |= 4096; // Set the ADD bit
949 unsigned ARMMCCodeEmitter::
950 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
951 SmallVectorImpl<MCFixup> &Fixups) const {
952 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
953 // shifted. The second is the amount to shift by.
960 const MCOperand &MO = MI.getOperand(OpIdx);
961 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
962 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
965 unsigned Binary = getARMRegisterNumbering(MO.getReg());
967 // Encode the shift opcode.
969 // Set shift operand (bit[6:4]).
975 default: llvm_unreachable("Unknown shift opc!");
976 case ARM_AM::lsl: SBits = 0x0; break;
977 case ARM_AM::lsr: SBits = 0x2; break;
978 case ARM_AM::asr: SBits = 0x4; break;
979 case ARM_AM::ror: SBits = 0x6; break;
982 Binary |= SBits << 4;
983 if (SOpc == ARM_AM::rrx)
986 // Encode shift_imm bit[11:7].
987 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
990 unsigned ARMMCCodeEmitter::
991 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
992 SmallVectorImpl<MCFixup> &Fixups) const {
993 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
995 const MCOperand &MO = MI.getOperand(Op);
996 uint32_t v = ~MO.getImm();
997 uint32_t lsb = CountTrailingZeros_32(v);
998 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
999 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1000 return lsb | (msb << 5);
1003 unsigned ARMMCCodeEmitter::
1004 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1005 SmallVectorImpl<MCFixup> &Fixups) const {
1008 // {7-0} = Number of registers
1011 // {15-0} = Bitfield of GPRs.
1012 unsigned Reg = MI.getOperand(Op).getReg();
1013 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1014 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1016 unsigned Binary = 0;
1018 if (SPRRegs || DPRRegs) {
1020 unsigned RegNo = getARMRegisterNumbering(Reg);
1021 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1022 Binary |= (RegNo & 0x1f) << 8;
1026 Binary |= NumRegs * 2;
1028 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1029 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1030 Binary |= 1 << RegNo;
1037 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1038 /// with the alignment operand.
1039 unsigned ARMMCCodeEmitter::
1040 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1041 SmallVectorImpl<MCFixup> &Fixups) const {
1042 const MCOperand &Reg = MI.getOperand(Op);
1043 const MCOperand &Imm = MI.getOperand(Op + 1);
1045 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1048 switch (Imm.getImm()) {
1052 case 8: Align = 0x01; break;
1053 case 16: Align = 0x02; break;
1054 case 32: Align = 0x03; break;
1057 return RegNo | (Align << 4);
1060 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1061 /// alignment operand for use in VLD-dup instructions. This is the same as
1062 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1063 /// different for VLD4-dup.
1064 unsigned ARMMCCodeEmitter::
1065 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1066 SmallVectorImpl<MCFixup> &Fixups) const {
1067 const MCOperand &Reg = MI.getOperand(Op);
1068 const MCOperand &Imm = MI.getOperand(Op + 1);
1070 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1073 switch (Imm.getImm()) {
1077 case 8: Align = 0x01; break;
1078 case 16: Align = 0x03; break;
1081 return RegNo | (Align << 4);
1084 unsigned ARMMCCodeEmitter::
1085 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1086 SmallVectorImpl<MCFixup> &Fixups) const {
1087 const MCOperand &MO = MI.getOperand(Op);
1088 if (MO.getReg() == 0) return 0x0D;
1092 void ARMMCCodeEmitter::
1093 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1094 SmallVectorImpl<MCFixup> &Fixups) const {
1095 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1096 // Pseudo instructions don't get encoded.
1097 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1098 uint64_t TSFlags = Desc.TSFlags;
1099 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1102 // Basic size info comes from the TSFlags field.
1103 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1104 default: llvm_unreachable("Unexpected instruction size!");
1105 case ARMII::Size2Bytes: Size = 2; break;
1106 case ARMII::Size4Bytes: Size = 4; break;
1108 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1109 // Thumb 32-bit wide instructions need to be have the high order halfword
1111 if (Subtarget.isThumb() && Size == 4) {
1112 EmitConstant(Binary >> 16, 2, OS);
1113 EmitConstant(Binary & 0xffff, 2, OS);
1115 EmitConstant(Binary, Size, OS);
1116 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1119 #include "ARMGenMCCodeEmitter.inc"