1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mccodeemitter"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/raw_ostream.h"
26 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
30 class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
42 ~ARMMCCodeEmitter() {}
44 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
48 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
51 MCFixupKindInfo::FKF_IsAligned},
52 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
54 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
56 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAligned},
58 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
59 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
61 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
62 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
63 { "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
64 { "fixup_arm_movt_hi16", 0, 16, 0 },
65 { "fixup_arm_movw_lo16", 0, 16, 0 },
68 if (Kind < FirstTargetFixupKind)
69 return MCCodeEmitter::getFixupKindInfo(Kind);
71 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
73 return Infos[Kind - FirstTargetFixupKind];
75 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
77 // getBinaryCodeForInstr - TableGen'erated function for getting the
78 // binary encoding for an instruction.
79 unsigned getBinaryCodeForInstr(const MCInst &MI,
80 SmallVectorImpl<MCFixup> &Fixups) const;
82 /// getMachineOpValue - Return binary encoding of operand. If the machine
83 /// operand requires relocation, record the relocation and return zero.
84 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
85 SmallVectorImpl<MCFixup> &Fixups) const;
87 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
88 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
91 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
92 unsigned &Reg, unsigned &Imm,
93 SmallVectorImpl<MCFixup> &Fixups) const;
95 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
97 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const;
100 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
101 /// BLX branch target.
102 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
105 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
106 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
109 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
110 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
113 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
117 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
119 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
122 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
123 /// ADR label target.
124 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
127 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
129 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups) const;
132 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
133 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
136 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
138 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
142 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
143 /// operand as needed by load/store instructions.
144 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
147 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
148 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const {
150 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
152 default: assert(0 && "Unknown addressing sub-mode!");
153 case ARM_AM::da: return 0;
154 case ARM_AM::ia: return 1;
155 case ARM_AM::db: return 2;
156 case ARM_AM::ib: return 3;
159 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
161 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
163 default: llvm_unreachable("Unknown shift opc!");
164 case ARM_AM::no_shift:
165 case ARM_AM::lsl: return 0;
166 case ARM_AM::lsr: return 1;
167 case ARM_AM::asr: return 2;
169 case ARM_AM::rrx: return 3;
174 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
175 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
178 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
179 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
182 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
183 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
186 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
187 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
190 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
192 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
195 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
196 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &) const;
199 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
200 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &Fixups) const;
203 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
204 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
207 /// getCCOutOpValue - Return encoding of the 's' bit.
208 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const {
210 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
212 return MI.getOperand(Op).getReg() == ARM::CPSR;
215 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
216 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
218 unsigned SoImm = MI.getOperand(Op).getImm();
219 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
220 assert(SoImmVal != -1 && "Not a valid so_imm value!");
222 // Encode rotate_imm.
223 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
224 << ARMII::SoRotImmShift;
227 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
231 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
232 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const {
234 unsigned SoImm = MI.getOperand(Op).getImm();
235 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
236 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
240 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
241 SmallVectorImpl<MCFixup> &Fixups) const;
242 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
243 SmallVectorImpl<MCFixup> &Fixups) const;
244 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
245 SmallVectorImpl<MCFixup> &Fixups) const;
246 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
247 SmallVectorImpl<MCFixup> &Fixups) const;
249 /// getSORegOpValue - Return an encoded so_reg shifted register value.
250 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const;
252 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
253 SmallVectorImpl<MCFixup> &Fixups) const;
255 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const {
257 switch (MI.getOperand(Op).getImm()) {
258 default: assert (0 && "Not a valid rot_imm value!");
266 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const {
268 return MI.getOperand(Op).getImm() - 1;
271 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const {
273 return 64 - MI.getOperand(Op).getImm();
276 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
279 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
281 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
283 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
285 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
288 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
289 unsigned EncodedValue) const;
290 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
291 unsigned EncodedValue) const;
292 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
293 unsigned EncodedValue) const;
295 unsigned VFPThumb2PostEncoder(const MCInst &MI,
296 unsigned EncodedValue) const;
298 void EmitByte(unsigned char C, raw_ostream &OS) const {
302 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
303 // Output the constant in little endian byte order.
304 for (unsigned i = 0; i != Size; ++i) {
305 EmitByte(Val & 255, OS);
310 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
311 SmallVectorImpl<MCFixup> &Fixups) const;
314 } // end anonymous namespace
316 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
318 return new ARMMCCodeEmitter(TM, Ctx);
321 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
322 /// instructions, and rewrite them to their Thumb2 form if we are currently in
324 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
325 unsigned EncodedValue) const {
326 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
327 if (Subtarget.isThumb2()) {
328 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
329 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
331 unsigned Bit24 = EncodedValue & 0x01000000;
332 unsigned Bit28 = Bit24 << 4;
333 EncodedValue &= 0xEFFFFFFF;
334 EncodedValue |= Bit28;
335 EncodedValue |= 0x0F000000;
341 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
342 /// instructions, and rewrite them to their Thumb2 form if we are currently in
344 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
345 unsigned EncodedValue) const {
346 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
347 if (Subtarget.isThumb2()) {
348 EncodedValue &= 0xF0FFFFFF;
349 EncodedValue |= 0x09000000;
355 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
356 /// instructions, and rewrite them to their Thumb2 form if we are currently in
358 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
359 unsigned EncodedValue) const {
360 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
361 if (Subtarget.isThumb2()) {
362 EncodedValue &= 0x00FFFFFF;
363 EncodedValue |= 0xEE000000;
369 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
370 /// them to their Thumb2 form if we are currently in Thumb2 mode.
371 unsigned ARMMCCodeEmitter::
372 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
373 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
374 EncodedValue &= 0x0FFFFFFF;
375 EncodedValue |= 0xE0000000;
380 /// getMachineOpValue - Return binary encoding of operand. If the machine
381 /// operand requires relocation, record the relocation and return zero.
382 unsigned ARMMCCodeEmitter::
383 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
384 SmallVectorImpl<MCFixup> &Fixups) const {
386 unsigned Reg = MO.getReg();
387 unsigned RegNo = getARMRegisterNumbering(Reg);
389 // Q registers are encoded as 2x their register number.
393 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
394 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
395 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
396 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
399 } else if (MO.isImm()) {
400 return static_cast<unsigned>(MO.getImm());
401 } else if (MO.isFPImm()) {
402 return static_cast<unsigned>(APFloat(MO.getFPImm())
403 .bitcastToAPInt().getHiBits(32).getLimitedValue());
406 llvm_unreachable("Unable to encode MCOperand!");
410 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
411 bool ARMMCCodeEmitter::
412 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
413 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
414 const MCOperand &MO = MI.getOperand(OpIdx);
415 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
417 Reg = getARMRegisterNumbering(MO.getReg());
419 int32_t SImm = MO1.getImm();
422 // Special value for #-0
423 if (SImm == INT32_MIN)
426 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
436 /// getBranchTargetOpValue - Helper function to get the branch target operand,
437 /// which is either an immediate or requires a fixup.
438 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
440 SmallVectorImpl<MCFixup> &Fixups) {
441 const MCOperand &MO = MI.getOperand(OpIdx);
443 // If the destination is an immediate, we have nothing to do.
444 if (MO.isImm()) return MO.getImm();
445 assert(MO.isExpr() && "Unexpected branch target type!");
446 const MCExpr *Expr = MO.getExpr();
447 MCFixupKind Kind = MCFixupKind(FixupKind);
448 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
450 // All of the information is in the fixup.
454 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
455 uint32_t ARMMCCodeEmitter::
456 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
457 SmallVectorImpl<MCFixup> &Fixups) const {
458 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
461 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
462 /// BLX branch target.
463 uint32_t ARMMCCodeEmitter::
464 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
465 SmallVectorImpl<MCFixup> &Fixups) const {
466 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
469 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
470 uint32_t ARMMCCodeEmitter::
471 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
473 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
476 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
477 uint32_t ARMMCCodeEmitter::
478 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
479 SmallVectorImpl<MCFixup> &Fixups) const {
480 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
483 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
484 uint32_t ARMMCCodeEmitter::
485 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
486 SmallVectorImpl<MCFixup> &Fixups) const {
487 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
490 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
492 uint32_t ARMMCCodeEmitter::
493 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
495 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
496 if (Subtarget.isThumb2())
497 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
498 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
501 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
503 uint32_t ARMMCCodeEmitter::
504 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
505 SmallVectorImpl<MCFixup> &Fixups) const {
506 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
507 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
511 /// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
512 uint32_t ARMMCCodeEmitter::
513 getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
514 SmallVectorImpl<MCFixup> &Fixups) const {
515 const MCOperand &MO1 = MI.getOperand(OpIdx);
516 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
517 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
518 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
519 return (Rm << 3) | Rn;
522 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
523 uint32_t ARMMCCodeEmitter::
524 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
525 SmallVectorImpl<MCFixup> &Fixups) const {
527 // {12} = (U)nsigned (add == '1', sub == '0')
531 // If The first operand isn't a register, we have a label reference.
532 const MCOperand &MO = MI.getOperand(OpIdx);
533 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
534 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
535 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
537 isAdd = false ; // 'U' bit is set as part of the fixup.
539 const MCExpr *Expr = 0;
543 Expr = MO2.getExpr();
545 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
547 if (Subtarget.isThumb2())
548 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
550 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
551 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
553 ++MCNumCPRelocations;
555 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
557 uint32_t Binary = Imm12 & 0xfff;
558 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
561 Binary |= (Reg << 13);
565 /// getT2AddrModeImm8s4OpValue - Return encoding info for
566 /// 'reg +/- imm8<<2' operand.
567 uint32_t ARMMCCodeEmitter::
568 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
571 // {8} = (U)nsigned (add == '1', sub == '0')
575 // If The first operand isn't a register, we have a label reference.
576 const MCOperand &MO = MI.getOperand(OpIdx);
578 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
580 isAdd = false ; // 'U' bit is set as part of the fixup.
582 assert(MO.isExpr() && "Unexpected machine operand type!");
583 const MCExpr *Expr = MO.getExpr();
584 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
585 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
587 ++MCNumCPRelocations;
589 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
591 uint32_t Binary = (Imm8 >> 2) & 0xff;
592 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
595 Binary |= (Reg << 9);
599 uint32_t ARMMCCodeEmitter::
600 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 // {20-16} = imm{15-12}
603 // {11-0} = imm{11-0}
604 const MCOperand &MO = MI.getOperand(OpIdx);
606 return static_cast<unsigned>(MO.getImm());
607 } else if (const MCSymbolRefExpr *Expr =
608 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
610 switch (Expr->getKind()) {
611 default: assert(0 && "Unsupported ARMFixup");
612 case MCSymbolRefExpr::VK_ARM_HI16:
613 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
615 case MCSymbolRefExpr::VK_ARM_LO16:
616 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
619 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
622 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
626 uint32_t ARMMCCodeEmitter::
627 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
628 SmallVectorImpl<MCFixup> &Fixups) const {
629 const MCOperand &MO = MI.getOperand(OpIdx);
630 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
631 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
632 unsigned Rn = getARMRegisterNumbering(MO.getReg());
633 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
634 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
635 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
636 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
637 unsigned SBits = getShiftOp(ShOp);
646 uint32_t Binary = Rm;
648 Binary |= SBits << 5;
649 Binary |= ShImm << 7;
655 uint32_t ARMMCCodeEmitter::
656 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
657 SmallVectorImpl<MCFixup> &Fixups) const {
659 // {13} 1 == imm12, 0 == Rm
662 const MCOperand &MO = MI.getOperand(OpIdx);
663 unsigned Rn = getARMRegisterNumbering(MO.getReg());
664 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
669 uint32_t ARMMCCodeEmitter::
670 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
671 SmallVectorImpl<MCFixup> &Fixups) const {
672 // {13} 1 == imm12, 0 == Rm
675 const MCOperand &MO = MI.getOperand(OpIdx);
676 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
677 unsigned Imm = MO1.getImm();
678 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
679 bool isReg = MO.getReg() != 0;
680 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
681 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
683 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
684 Binary <<= 7; // Shift amount is bits [11:7]
685 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
686 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
688 return Binary | (isAdd << 12) | (isReg << 13);
691 uint32_t ARMMCCodeEmitter::
692 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
693 SmallVectorImpl<MCFixup> &Fixups) const {
694 // {9} 1 == imm8, 0 == Rm
698 const MCOperand &MO = MI.getOperand(OpIdx);
699 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
700 unsigned Imm = MO1.getImm();
701 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
702 bool isImm = MO.getReg() == 0;
703 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
704 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
706 Imm8 = getARMRegisterNumbering(MO.getReg());
707 return Imm8 | (isAdd << 8) | (isImm << 9);
710 uint32_t ARMMCCodeEmitter::
711 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &Fixups) const {
713 // {13} 1 == imm8, 0 == Rm
718 const MCOperand &MO = MI.getOperand(OpIdx);
719 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
720 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
721 unsigned Rn = getARMRegisterNumbering(MO.getReg());
722 unsigned Imm = MO2.getImm();
723 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
724 bool isImm = MO1.getReg() == 0;
725 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
726 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
728 Imm8 = getARMRegisterNumbering(MO1.getReg());
729 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
732 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
733 uint32_t ARMMCCodeEmitter::
734 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
735 SmallVectorImpl<MCFixup> &Fixups) const {
738 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
739 #if 0 // FIXME: This crashes2003-05-14-initialize-string.c
740 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
741 "Unexpected base register!");
743 // The immediate is already shifted for the implicit zeroes, so no change
745 return MO1.getImm() & 0xff;
748 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
749 uint32_t ARMMCCodeEmitter::
750 getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
751 SmallVectorImpl<MCFixup> &) const {
759 const MCOperand &MO = MI.getOperand(OpIdx);
760 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
761 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
762 unsigned Rn = getARMRegisterNumbering(MO.getReg());
763 unsigned Imm5 = MO1.getImm();
765 if (MO2.getReg() != 0)
767 Imm5 = getARMRegisterNumbering(MO2.getReg());
769 return ((Imm5 & 0x1f) << 3) | Rn;
772 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
773 uint32_t ARMMCCodeEmitter::
774 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
775 SmallVectorImpl<MCFixup> &Fixups) const {
776 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
779 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
780 uint32_t ARMMCCodeEmitter::
781 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
782 SmallVectorImpl<MCFixup> &Fixups) const {
784 // {8} = (U)nsigned (add == '1', sub == '0')
788 // If The first operand isn't a register, we have a label reference.
789 const MCOperand &MO = MI.getOperand(OpIdx);
791 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
793 isAdd = false; // 'U' bit is handled as part of the fixup.
795 assert(MO.isExpr() && "Unexpected machine operand type!");
796 const MCExpr *Expr = MO.getExpr();
798 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
799 if (Subtarget.isThumb2())
800 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
802 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
803 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
805 ++MCNumCPRelocations;
807 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
808 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
811 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
812 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
815 Binary |= (Reg << 9);
819 unsigned ARMMCCodeEmitter::
820 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
821 SmallVectorImpl<MCFixup> &Fixups) const {
822 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
823 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
824 // case the imm contains the amount to shift by.
827 // {4} = 1 if reg shift, 0 if imm shift
835 const MCOperand &MO = MI.getOperand(OpIdx);
836 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
837 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
838 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
841 unsigned Binary = getARMRegisterNumbering(MO.getReg());
843 // Encode the shift opcode.
845 unsigned Rs = MO1.getReg();
847 // Set shift operand (bit[7:4]).
852 // RRX - 0110 and bit[11:8] clear.
854 default: llvm_unreachable("Unknown shift opc!");
855 case ARM_AM::lsl: SBits = 0x1; break;
856 case ARM_AM::lsr: SBits = 0x3; break;
857 case ARM_AM::asr: SBits = 0x5; break;
858 case ARM_AM::ror: SBits = 0x7; break;
859 case ARM_AM::rrx: SBits = 0x6; break;
862 // Set shift operand (bit[6:4]).
868 default: llvm_unreachable("Unknown shift opc!");
869 case ARM_AM::lsl: SBits = 0x0; break;
870 case ARM_AM::lsr: SBits = 0x2; break;
871 case ARM_AM::asr: SBits = 0x4; break;
872 case ARM_AM::ror: SBits = 0x6; break;
876 Binary |= SBits << 4;
877 if (SOpc == ARM_AM::rrx)
880 // Encode the shift operation Rs or shift_imm (except rrx).
882 // Encode Rs bit[11:8].
883 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
884 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
887 // Encode shift_imm bit[11:7].
888 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
891 unsigned ARMMCCodeEmitter::
892 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
893 SmallVectorImpl<MCFixup> &Fixups) const {
894 const MCOperand &MO1 = MI.getOperand(OpNum);
895 const MCOperand &MO2 = MI.getOperand(OpNum+1);
896 const MCOperand &MO3 = MI.getOperand(OpNum+2);
898 // Encoded as [Rn, Rm, imm].
899 // FIXME: Needs fixup support.
900 unsigned Value = getARMRegisterNumbering(MO1.getReg());
902 Value |= getARMRegisterNumbering(MO2.getReg());
904 Value |= MO3.getImm();
909 unsigned ARMMCCodeEmitter::
910 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
911 SmallVectorImpl<MCFixup> &Fixups) const {
912 const MCOperand &MO1 = MI.getOperand(OpNum);
913 const MCOperand &MO2 = MI.getOperand(OpNum+1);
915 // FIXME: Needs fixup support.
916 unsigned Value = getARMRegisterNumbering(MO1.getReg());
918 // Even though the immediate is 8 bits long, we need 9 bits in order
919 // to represent the (inverse of the) sign bit.
921 int32_t tmp = (int32_t)MO2.getImm();
925 Value |= 256; // Set the ADD bit
930 unsigned ARMMCCodeEmitter::
931 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
932 SmallVectorImpl<MCFixup> &Fixups) const {
933 const MCOperand &MO1 = MI.getOperand(OpNum);
935 // FIXME: Needs fixup support.
937 int32_t tmp = (int32_t)MO1.getImm();
941 Value |= 256; // Set the ADD bit
946 unsigned ARMMCCodeEmitter::
947 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
948 SmallVectorImpl<MCFixup> &Fixups) const {
949 const MCOperand &MO1 = MI.getOperand(OpNum);
951 // FIXME: Needs fixup support.
953 int32_t tmp = (int32_t)MO1.getImm();
957 Value |= 4096; // Set the ADD bit
962 unsigned ARMMCCodeEmitter::
963 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
964 SmallVectorImpl<MCFixup> &Fixups) const {
965 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
966 // shifted. The second is the amount to shift by.
973 const MCOperand &MO = MI.getOperand(OpIdx);
974 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
975 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
978 unsigned Binary = getARMRegisterNumbering(MO.getReg());
980 // Encode the shift opcode.
982 // Set shift operand (bit[6:4]).
988 default: llvm_unreachable("Unknown shift opc!");
989 case ARM_AM::lsl: SBits = 0x0; break;
990 case ARM_AM::lsr: SBits = 0x2; break;
991 case ARM_AM::asr: SBits = 0x4; break;
992 case ARM_AM::ror: SBits = 0x6; break;
995 Binary |= SBits << 4;
996 if (SOpc == ARM_AM::rrx)
999 // Encode shift_imm bit[11:7].
1000 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1003 unsigned ARMMCCodeEmitter::
1004 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1005 SmallVectorImpl<MCFixup> &Fixups) const {
1006 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1008 const MCOperand &MO = MI.getOperand(Op);
1009 uint32_t v = ~MO.getImm();
1010 uint32_t lsb = CountTrailingZeros_32(v);
1011 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1012 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1013 return lsb | (msb << 5);
1016 unsigned ARMMCCodeEmitter::
1017 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1018 SmallVectorImpl<MCFixup> &Fixups) const {
1021 // {7-0} = Number of registers
1024 // {15-0} = Bitfield of GPRs.
1025 unsigned Reg = MI.getOperand(Op).getReg();
1026 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1027 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1029 unsigned Binary = 0;
1031 if (SPRRegs || DPRRegs) {
1033 unsigned RegNo = getARMRegisterNumbering(Reg);
1034 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1035 Binary |= (RegNo & 0x1f) << 8;
1039 Binary |= NumRegs * 2;
1041 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1042 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1043 Binary |= 1 << RegNo;
1050 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1051 /// with the alignment operand.
1052 unsigned ARMMCCodeEmitter::
1053 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1054 SmallVectorImpl<MCFixup> &Fixups) const {
1055 const MCOperand &Reg = MI.getOperand(Op);
1056 const MCOperand &Imm = MI.getOperand(Op + 1);
1058 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1061 switch (Imm.getImm()) {
1065 case 8: Align = 0x01; break;
1066 case 16: Align = 0x02; break;
1067 case 32: Align = 0x03; break;
1070 return RegNo | (Align << 4);
1073 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1074 /// alignment operand for use in VLD-dup instructions. This is the same as
1075 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1076 /// different for VLD4-dup.
1077 unsigned ARMMCCodeEmitter::
1078 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1079 SmallVectorImpl<MCFixup> &Fixups) const {
1080 const MCOperand &Reg = MI.getOperand(Op);
1081 const MCOperand &Imm = MI.getOperand(Op + 1);
1083 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1086 switch (Imm.getImm()) {
1090 case 8: Align = 0x01; break;
1091 case 16: Align = 0x03; break;
1094 return RegNo | (Align << 4);
1097 unsigned ARMMCCodeEmitter::
1098 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1099 SmallVectorImpl<MCFixup> &Fixups) const {
1100 const MCOperand &MO = MI.getOperand(Op);
1101 if (MO.getReg() == 0) return 0x0D;
1105 void ARMMCCodeEmitter::
1106 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1107 SmallVectorImpl<MCFixup> &Fixups) const {
1108 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1109 // Pseudo instructions don't get encoded.
1110 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1111 uint64_t TSFlags = Desc.TSFlags;
1112 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1115 // Basic size info comes from the TSFlags field.
1116 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1117 default: llvm_unreachable("Unexpected instruction size!");
1118 case ARMII::Size2Bytes: Size = 2; break;
1119 case ARMII::Size4Bytes: Size = 4; break;
1121 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1122 // Thumb 32-bit wide instructions need to be have the high order halfword
1124 if (Subtarget.isThumb() && Size == 4) {
1125 EmitConstant(Binary >> 16, 2, OS);
1126 EmitConstant(Binary & 0xffff, 2, OS);
1128 EmitConstant(Binary, Size, OS);
1129 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1132 #include "ARMGenMCCodeEmitter.inc"