Add support for Thumb2 encodings of NEON data processing instructions, using the...
[oota-llvm.git] / lib / Target / ARM / ARMMCCodeEmitter.cpp
1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ARMMCCodeEmitter class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "arm-emitter"
15 #include "ARM.h"
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"
24 using namespace llvm;
25
26 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
28
29 namespace {
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;
35   MCContext &Ctx;
36
37 public:
38   ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39     : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
40   }
41
42   ~ARMMCCodeEmitter() {}
43
44   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
45
46   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47     const static MCFixupKindInfo Infos[] = {
48       // name                     offset  bits  flags
49       { "fixup_arm_pcrel_12",     2,      12,   MCFixupKindInfo::FKF_IsPCRel },
50       { "fixup_arm_vfp_pcrel_12", 3,      8,    MCFixupKindInfo::FKF_IsPCRel },
51       { "fixup_arm_branch",       1,      24,   MCFixupKindInfo::FKF_IsPCRel },
52     };
53
54     if (Kind < FirstTargetFixupKind)
55       return MCCodeEmitter::getFixupKindInfo(Kind);
56
57     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58            "Invalid kind!");
59     return Infos[Kind - FirstTargetFixupKind];
60   }
61   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
62
63   // getBinaryCodeForInstr - TableGen'erated function for getting the
64   // binary encoding for an instruction.
65   unsigned getBinaryCodeForInstr(const MCInst &MI,
66                                  SmallVectorImpl<MCFixup> &Fixups) const;
67
68   /// getMachineOpValue - Return binary encoding of operand. If the machine
69   /// operand requires relocation, record the relocation and return zero.
70   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
71                              SmallVectorImpl<MCFixup> &Fixups) const;
72
73   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
74                               unsigned &Reg, unsigned &Imm,
75                               SmallVectorImpl<MCFixup> &Fixups) const;
76
77   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
78   /// branch target.
79   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
80                                   SmallVectorImpl<MCFixup> &Fixups) const;
81
82   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
83   /// operand.
84   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
85                                    SmallVectorImpl<MCFixup> &Fixups) const;
86
87   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
88   /// operand as needed by load/store instructions.
89   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
90                                SmallVectorImpl<MCFixup> &Fixups) const;
91
92   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
93   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
94                                SmallVectorImpl<MCFixup> &Fixups) const {
95     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
96     switch (Mode) {
97     default: assert(0 && "Unknown addressing sub-mode!");
98     case ARM_AM::da: return 0;
99     case ARM_AM::ia: return 1;
100     case ARM_AM::db: return 2;
101     case ARM_AM::ib: return 3;
102     }
103   }
104   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
105   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
106                                      SmallVectorImpl<MCFixup> &Fixups) const;
107
108   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
109   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
110                                SmallVectorImpl<MCFixup> &Fixups) const;
111
112   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
113   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
114                                SmallVectorImpl<MCFixup> &Fixups) const;
115
116   /// getCCOutOpValue - Return encoding of the 's' bit.
117   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
118                            SmallVectorImpl<MCFixup> &Fixups) const {
119     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
120     // '1' respectively.
121     return MI.getOperand(Op).getReg() == ARM::CPSR;
122   }
123
124   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
125   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
126                            SmallVectorImpl<MCFixup> &Fixups) const {
127     unsigned SoImm = MI.getOperand(Op).getImm();
128     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
129     assert(SoImmVal != -1 && "Not a valid so_imm value!");
130
131     // Encode rotate_imm.
132     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
133       << ARMII::SoRotImmShift;
134
135     // Encode immed_8.
136     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
137     return Binary;
138   }
139
140   /// getSORegOpValue - Return an encoded so_reg shifted register value.
141   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
142                            SmallVectorImpl<MCFixup> &Fixups) const;
143
144   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
145                             SmallVectorImpl<MCFixup> &Fixups) const {
146     switch (MI.getOperand(Op).getImm()) {
147     default: assert (0 && "Not a valid rot_imm value!");
148     case 0:  return 0;
149     case 8:  return 1;
150     case 16: return 2;
151     case 24: return 3;
152     }
153   }
154
155   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
156                                  SmallVectorImpl<MCFixup> &Fixups) const {
157     return MI.getOperand(Op).getImm() - 1;
158   }
159
160   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
161                                    SmallVectorImpl<MCFixup> &Fixups) const {
162     return 64 - MI.getOperand(Op).getImm();
163   }
164
165   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
166                                       SmallVectorImpl<MCFixup> &Fixups) const;
167
168   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
169                                   SmallVectorImpl<MCFixup> &Fixups) const;
170   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
171                                       SmallVectorImpl<MCFixup> &Fixups) const;
172   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
173                                      SmallVectorImpl<MCFixup> &Fixups) const;
174
175   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
176                                       unsigned EncodedValue) const;
177
178   void EmitByte(unsigned char C, raw_ostream &OS) const {
179     OS << (char)C;
180   }
181
182   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
183     // Output the constant in little endian byte order.
184     for (unsigned i = 0; i != Size; ++i) {
185       EmitByte(Val & 255, OS);
186       Val >>= 8;
187     }
188   }
189
190   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
191                          SmallVectorImpl<MCFixup> &Fixups) const;
192 };
193
194 } // end anonymous namespace
195
196 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
197                                             MCContext &Ctx) {
198   return new ARMMCCodeEmitter(TM, Ctx);
199 }
200
201 /// NEONThumb2PostEncoder - Post-process encoded NEON data-processing 
202 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
203 /// Thumb2 mode.
204 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
205                                                  unsigned EncodedValue) const {
206   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
207   if (Subtarget.isThumb2()) {
208     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved 
209     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
210     // set to 1111.
211     unsigned Bit24 = EncodedValue & 0x01000000;
212     unsigned Bit28 = Bit24 << 4;
213     EncodedValue &= 0xEFFFFFFF;
214     EncodedValue |= Bit28;
215     EncodedValue |= 0x0F000000;
216   }
217   
218   return EncodedValue;
219 }
220
221 /// getMachineOpValue - Return binary encoding of operand. If the machine
222 /// operand requires relocation, record the relocation and return zero.
223 unsigned ARMMCCodeEmitter::
224 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
225                   SmallVectorImpl<MCFixup> &Fixups) const {
226   if (MO.isReg()) {
227     unsigned Reg = MO.getReg();
228     unsigned RegNo = getARMRegisterNumbering(Reg);
229
230     // Q registers are encodes as 2x their register number.
231     switch (Reg) {
232     default:
233       return RegNo;
234     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
235     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
236     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
237     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
238       return 2 * RegNo;
239     }
240   } else if (MO.isImm()) {
241     return static_cast<unsigned>(MO.getImm());
242   } else if (MO.isFPImm()) {
243     return static_cast<unsigned>(APFloat(MO.getFPImm())
244                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
245   }
246
247 #ifndef NDEBUG
248   errs() << MO;
249 #endif
250   llvm_unreachable(0);
251   return 0;
252 }
253
254 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
255 bool ARMMCCodeEmitter::
256 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
257                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
258   const MCOperand &MO  = MI.getOperand(OpIdx);
259   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
260
261   Reg = getARMRegisterNumbering(MO.getReg());
262
263   int32_t SImm = MO1.getImm();
264   bool isAdd = true;
265
266   // Special value for #-0
267   if (SImm == INT32_MIN)
268     SImm = 0;
269
270   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
271   if (SImm < 0) {
272     SImm = -SImm;
273     isAdd = false;
274   }
275
276   Imm = SImm;
277   return isAdd;
278 }
279
280 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
281 /// branch target.
282 uint32_t ARMMCCodeEmitter::
283 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
284                         SmallVectorImpl<MCFixup> &Fixups) const {
285   const MCOperand &MO = MI.getOperand(OpIdx);
286
287   // If the destination is an immediate, we have nothing to do.
288   if (MO.isImm()) return MO.getImm();
289   assert (MO.isExpr() && "Unexpected branch target type!");
290   const MCExpr *Expr = MO.getExpr();
291   MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
292   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
293
294   // All of the information is in the fixup.
295   return 0;
296 }
297
298 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
299 uint32_t ARMMCCodeEmitter::
300 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
301                         SmallVectorImpl<MCFixup> &Fixups) const {
302   // {17-13} = reg
303   // {12}    = (U)nsigned (add == '1', sub == '0')
304   // {11-0}  = imm12
305   unsigned Reg, Imm12;
306   bool isAdd = true;
307   // If The first operand isn't a register, we have a label reference.
308   const MCOperand &MO = MI.getOperand(OpIdx);
309   if (!MO.isReg()) {
310     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
311     Imm12 = 0;
312
313     assert(MO.isExpr() && "Unexpected machine operand type!");
314     const MCExpr *Expr = MO.getExpr();
315     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
316     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
317
318     ++MCNumCPRelocations;
319   } else
320     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
321
322   uint32_t Binary = Imm12 & 0xfff;
323   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
324   if (isAdd)
325     Binary |= (1 << 12);
326   Binary |= (Reg << 13);
327   return Binary;
328 }
329
330 uint32_t ARMMCCodeEmitter::
331 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
332                     SmallVectorImpl<MCFixup> &Fixups) const {
333   const MCOperand &MO = MI.getOperand(OpIdx);
334   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
335   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
336   unsigned Rn = getARMRegisterNumbering(MO.getReg());
337   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
338   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
339   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
340   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
341   unsigned SBits;
342   // LSL - 00
343   // LSR - 01
344   // ASR - 10
345   // ROR - 11
346   switch (ShOp) {
347   default: llvm_unreachable("Unknown shift opc!");
348   case ARM_AM::no_shift:
349     assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
350     // fall through
351   case ARM_AM::lsl: SBits = 0x0; break;
352   case ARM_AM::lsr: SBits = 0x1; break;
353   case ARM_AM::asr: SBits = 0x2; break;
354   case ARM_AM::ror: SBits = 0x3; break;
355   }
356
357   // {16-13} = Rn
358   // {12}    = isAdd
359   // {11-0}  = shifter
360   //  {3-0}  = Rm
361   //  {4}    = 0
362   //  {6-5}  = type
363   //  {11-7} = imm
364   uint32_t Binary = Rm;
365   Binary |= Rn << 13;
366   Binary |= SBits << 5;
367   Binary |= ShImm << 7;
368   if (isAdd)
369     Binary |= 1 << 12;
370   return Binary;
371 }
372
373 uint32_t ARMMCCodeEmitter::
374 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
375                           SmallVectorImpl<MCFixup> &Fixups) const {
376   // {9}      1 == imm8, 0 == Rm
377   // {8}      isAdd
378   // {7-4}    imm7_4/zero
379   // {3-0}    imm3_0/Rm
380   const MCOperand &MO = MI.getOperand(OpIdx);
381   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
382   unsigned Imm = MO1.getImm();
383   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
384   bool isImm = MO.getReg() == 0;
385   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
386   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
387   if (!isImm)
388     Imm8 = getARMRegisterNumbering(MO.getReg());
389   return Imm8 | (isAdd << 8) | (isImm << 9);
390 }
391
392 uint32_t ARMMCCodeEmitter::
393 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
394                     SmallVectorImpl<MCFixup> &Fixups) const {
395   // {13}     1 == imm8, 0 == Rm
396   // {12-9}   Rn
397   // {8}      isAdd
398   // {7-4}    imm7_4/zero
399   // {3-0}    imm3_0/Rm
400   const MCOperand &MO = MI.getOperand(OpIdx);
401   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
402   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
403   unsigned Rn = getARMRegisterNumbering(MO.getReg());
404   unsigned Imm = MO2.getImm();
405   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
406   bool isImm = MO1.getReg() == 0;
407   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
408   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
409   if (!isImm)
410     Imm8 = getARMRegisterNumbering(MO1.getReg());
411   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
412 }
413
414 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
415 uint32_t ARMMCCodeEmitter::
416 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
417                     SmallVectorImpl<MCFixup> &Fixups) const {
418   // {12-9} = reg
419   // {8}    = (U)nsigned (add == '1', sub == '0')
420   // {7-0}  = imm8
421   unsigned Reg, Imm8;
422   // If The first operand isn't a register, we have a label reference.
423   const MCOperand &MO = MI.getOperand(OpIdx);
424   if (!MO.isReg()) {
425     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
426     Imm8 = 0;
427
428     assert(MO.isExpr() && "Unexpected machine operand type!");
429     const MCExpr *Expr = MO.getExpr();
430     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
431     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
432
433     ++MCNumCPRelocations;
434   } else
435     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
436
437   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
438   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
439   if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
440     Binary |= (1 << 8);
441   Binary |= (Reg << 9);
442   return Binary;
443 }
444
445 unsigned ARMMCCodeEmitter::
446 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
447                 SmallVectorImpl<MCFixup> &Fixups) const {
448   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
449   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
450   // case the imm contains the amount to shift by.
451   //
452   // {3-0} = Rm.
453   // {4}   = 1 if reg shift, 0 if imm shift
454   // {6-5} = type
455   //    If reg shift:
456   //      {11-8} = Rs
457   //      {7}    = 0
458   //    else (imm shift)
459   //      {11-7} = imm
460
461   const MCOperand &MO  = MI.getOperand(OpIdx);
462   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
463   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
464   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
465
466   // Encode Rm.
467   unsigned Binary = getARMRegisterNumbering(MO.getReg());
468
469   // Encode the shift opcode.
470   unsigned SBits = 0;
471   unsigned Rs = MO1.getReg();
472   if (Rs) {
473     // Set shift operand (bit[7:4]).
474     // LSL - 0001
475     // LSR - 0011
476     // ASR - 0101
477     // ROR - 0111
478     // RRX - 0110 and bit[11:8] clear.
479     switch (SOpc) {
480     default: llvm_unreachable("Unknown shift opc!");
481     case ARM_AM::lsl: SBits = 0x1; break;
482     case ARM_AM::lsr: SBits = 0x3; break;
483     case ARM_AM::asr: SBits = 0x5; break;
484     case ARM_AM::ror: SBits = 0x7; break;
485     case ARM_AM::rrx: SBits = 0x6; break;
486     }
487   } else {
488     // Set shift operand (bit[6:4]).
489     // LSL - 000
490     // LSR - 010
491     // ASR - 100
492     // ROR - 110
493     switch (SOpc) {
494     default: llvm_unreachable("Unknown shift opc!");
495     case ARM_AM::lsl: SBits = 0x0; break;
496     case ARM_AM::lsr: SBits = 0x2; break;
497     case ARM_AM::asr: SBits = 0x4; break;
498     case ARM_AM::ror: SBits = 0x6; break;
499     }
500   }
501
502   Binary |= SBits << 4;
503   if (SOpc == ARM_AM::rrx)
504     return Binary;
505
506   // Encode the shift operation Rs or shift_imm (except rrx).
507   if (Rs) {
508     // Encode Rs bit[11:8].
509     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
510     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
511   }
512
513   // Encode shift_imm bit[11:7].
514   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
515 }
516
517 unsigned ARMMCCodeEmitter::
518 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
519                                SmallVectorImpl<MCFixup> &Fixups) const {
520   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
521   // msb of the mask.
522   const MCOperand &MO = MI.getOperand(Op);
523   uint32_t v = ~MO.getImm();
524   uint32_t lsb = CountTrailingZeros_32(v);
525   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
526   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
527   return lsb | (msb << 5);
528 }
529
530 unsigned ARMMCCodeEmitter::
531 getRegisterListOpValue(const MCInst &MI, unsigned Op,
532                        SmallVectorImpl<MCFixup> &Fixups) const {
533   // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
534   // register in the list, set the corresponding bit.
535   unsigned Binary = 0;
536   for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
537     unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
538     Binary |= 1 << regno;
539   }
540   return Binary;
541 }
542
543 unsigned ARMMCCodeEmitter::
544 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
545                            SmallVectorImpl<MCFixup> &Fixups) const {
546   const MCOperand &Reg = MI.getOperand(Op);
547   const MCOperand &Imm = MI.getOperand(Op + 1);
548
549   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
550   unsigned Align = 0;
551
552   switch (Imm.getImm()) {
553   default: break;
554   case 2:
555   case 4:
556   case 8:  Align = 0x01; break;
557   case 16: Align = 0x02; break;
558   case 32: Align = 0x03; break;
559   }
560
561   return RegNo | (Align << 4);
562 }
563
564 unsigned ARMMCCodeEmitter::
565 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
566                           SmallVectorImpl<MCFixup> &Fixups) const {
567   const MCOperand &MO = MI.getOperand(Op);
568   if (MO.getReg() == 0) return 0x0D;
569   return MO.getReg();
570 }
571
572 void ARMMCCodeEmitter::
573 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
574                   SmallVectorImpl<MCFixup> &Fixups) const {
575   // Pseudo instructions don't get encoded.
576   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
577   if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
578     return;
579
580   EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
581   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
582 }
583
584 #include "ARMGenMCCodeEmitter.inc"