ebd0d9b559976dbf7d931d8ceef63a6ce81ea2b2
[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   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
178                                       unsigned EncodedValue) const;
179   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
180                                       unsigned EncodedValue) const;
181
182   void EmitByte(unsigned char C, raw_ostream &OS) const {
183     OS << (char)C;
184   }
185
186   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
187     // Output the constant in little endian byte order.
188     for (unsigned i = 0; i != Size; ++i) {
189       EmitByte(Val & 255, OS);
190       Val >>= 8;
191     }
192   }
193
194   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
195                          SmallVectorImpl<MCFixup> &Fixups) const;
196 };
197
198 } // end anonymous namespace
199
200 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
201                                             MCContext &Ctx) {
202   return new ARMMCCodeEmitter(TM, Ctx);
203 }
204
205 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing 
206 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
207 /// Thumb2 mode.
208 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
209                                                  unsigned EncodedValue) const {
210   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
211   if (Subtarget.isThumb2()) {
212     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved 
213     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
214     // set to 1111.
215     unsigned Bit24 = EncodedValue & 0x01000000;
216     unsigned Bit28 = Bit24 << 4;
217     EncodedValue &= 0xEFFFFFFF;
218     EncodedValue |= Bit28;
219     EncodedValue |= 0x0F000000;
220   }
221   
222   return EncodedValue;
223 }
224
225 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
226 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
227 /// Thumb2 mode.
228 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
229                                                  unsigned EncodedValue) const {
230   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
231   if (Subtarget.isThumb2()) {
232     EncodedValue &= 0xF0FFFFFF;
233     EncodedValue |= 0x09000000;
234   }
235   
236   return EncodedValue;
237 }
238
239 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
240 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
241 /// Thumb2 mode.
242 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
243                                                  unsigned EncodedValue) const {
244   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
245   if (Subtarget.isThumb2()) {
246     EncodedValue &= 0x00FFFFFF;
247     EncodedValue |= 0xEE000000;
248   }
249   
250   return EncodedValue;
251 }
252
253
254
255 /// getMachineOpValue - Return binary encoding of operand. If the machine
256 /// operand requires relocation, record the relocation and return zero.
257 unsigned ARMMCCodeEmitter::
258 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
259                   SmallVectorImpl<MCFixup> &Fixups) const {
260   if (MO.isReg()) {
261     unsigned Reg = MO.getReg();
262     unsigned RegNo = getARMRegisterNumbering(Reg);
263
264     // Q registers are encodes as 2x their register number.
265     switch (Reg) {
266     default:
267       return RegNo;
268     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
269     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
270     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
271     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
272       return 2 * RegNo;
273     }
274   } else if (MO.isImm()) {
275     return static_cast<unsigned>(MO.getImm());
276   } else if (MO.isFPImm()) {
277     return static_cast<unsigned>(APFloat(MO.getFPImm())
278                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
279   }
280
281 #ifndef NDEBUG
282   errs() << MO;
283 #endif
284   llvm_unreachable(0);
285   return 0;
286 }
287
288 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
289 bool ARMMCCodeEmitter::
290 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
291                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
292   const MCOperand &MO  = MI.getOperand(OpIdx);
293   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
294
295   Reg = getARMRegisterNumbering(MO.getReg());
296
297   int32_t SImm = MO1.getImm();
298   bool isAdd = true;
299
300   // Special value for #-0
301   if (SImm == INT32_MIN)
302     SImm = 0;
303
304   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
305   if (SImm < 0) {
306     SImm = -SImm;
307     isAdd = false;
308   }
309
310   Imm = SImm;
311   return isAdd;
312 }
313
314 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
315 /// branch target.
316 uint32_t ARMMCCodeEmitter::
317 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
318                         SmallVectorImpl<MCFixup> &Fixups) const {
319   const MCOperand &MO = MI.getOperand(OpIdx);
320
321   // If the destination is an immediate, we have nothing to do.
322   if (MO.isImm()) return MO.getImm();
323   assert (MO.isExpr() && "Unexpected branch target type!");
324   const MCExpr *Expr = MO.getExpr();
325   MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
326   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
327
328   // All of the information is in the fixup.
329   return 0;
330 }
331
332 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
333 uint32_t ARMMCCodeEmitter::
334 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
335                         SmallVectorImpl<MCFixup> &Fixups) const {
336   // {17-13} = reg
337   // {12}    = (U)nsigned (add == '1', sub == '0')
338   // {11-0}  = imm12
339   unsigned Reg, Imm12;
340   bool isAdd = true;
341   // If The first operand isn't a register, we have a label reference.
342   const MCOperand &MO = MI.getOperand(OpIdx);
343   if (!MO.isReg()) {
344     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
345     Imm12 = 0;
346
347     assert(MO.isExpr() && "Unexpected machine operand type!");
348     const MCExpr *Expr = MO.getExpr();
349     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
350     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
351
352     ++MCNumCPRelocations;
353   } else
354     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
355
356   uint32_t Binary = Imm12 & 0xfff;
357   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
358   if (isAdd)
359     Binary |= (1 << 12);
360   Binary |= (Reg << 13);
361   return Binary;
362 }
363
364 uint32_t ARMMCCodeEmitter::
365 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
366                     SmallVectorImpl<MCFixup> &Fixups) const {
367   const MCOperand &MO = MI.getOperand(OpIdx);
368   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
369   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
370   unsigned Rn = getARMRegisterNumbering(MO.getReg());
371   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
372   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
373   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
374   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
375   unsigned SBits;
376   // LSL - 00
377   // LSR - 01
378   // ASR - 10
379   // ROR - 11
380   switch (ShOp) {
381   default: llvm_unreachable("Unknown shift opc!");
382   case ARM_AM::no_shift:
383     assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
384     // fall through
385   case ARM_AM::lsl: SBits = 0x0; break;
386   case ARM_AM::lsr: SBits = 0x1; break;
387   case ARM_AM::asr: SBits = 0x2; break;
388   case ARM_AM::ror: SBits = 0x3; break;
389   }
390
391   // {16-13} = Rn
392   // {12}    = isAdd
393   // {11-0}  = shifter
394   //  {3-0}  = Rm
395   //  {4}    = 0
396   //  {6-5}  = type
397   //  {11-7} = imm
398   uint32_t Binary = Rm;
399   Binary |= Rn << 13;
400   Binary |= SBits << 5;
401   Binary |= ShImm << 7;
402   if (isAdd)
403     Binary |= 1 << 12;
404   return Binary;
405 }
406
407 uint32_t ARMMCCodeEmitter::
408 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
409                           SmallVectorImpl<MCFixup> &Fixups) const {
410   // {9}      1 == imm8, 0 == Rm
411   // {8}      isAdd
412   // {7-4}    imm7_4/zero
413   // {3-0}    imm3_0/Rm
414   const MCOperand &MO = MI.getOperand(OpIdx);
415   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
416   unsigned Imm = MO1.getImm();
417   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
418   bool isImm = MO.getReg() == 0;
419   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
420   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
421   if (!isImm)
422     Imm8 = getARMRegisterNumbering(MO.getReg());
423   return Imm8 | (isAdd << 8) | (isImm << 9);
424 }
425
426 uint32_t ARMMCCodeEmitter::
427 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
428                     SmallVectorImpl<MCFixup> &Fixups) const {
429   // {13}     1 == imm8, 0 == Rm
430   // {12-9}   Rn
431   // {8}      isAdd
432   // {7-4}    imm7_4/zero
433   // {3-0}    imm3_0/Rm
434   const MCOperand &MO = MI.getOperand(OpIdx);
435   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
436   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
437   unsigned Rn = getARMRegisterNumbering(MO.getReg());
438   unsigned Imm = MO2.getImm();
439   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
440   bool isImm = MO1.getReg() == 0;
441   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
442   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
443   if (!isImm)
444     Imm8 = getARMRegisterNumbering(MO1.getReg());
445   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
446 }
447
448 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
449 uint32_t ARMMCCodeEmitter::
450 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
451                     SmallVectorImpl<MCFixup> &Fixups) const {
452   // {12-9} = reg
453   // {8}    = (U)nsigned (add == '1', sub == '0')
454   // {7-0}  = imm8
455   unsigned Reg, Imm8;
456   // If The first operand isn't a register, we have a label reference.
457   const MCOperand &MO = MI.getOperand(OpIdx);
458   if (!MO.isReg()) {
459     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
460     Imm8 = 0;
461
462     assert(MO.isExpr() && "Unexpected machine operand type!");
463     const MCExpr *Expr = MO.getExpr();
464     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
465     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
466
467     ++MCNumCPRelocations;
468   } else
469     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
470
471   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
472   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
473   if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
474     Binary |= (1 << 8);
475   Binary |= (Reg << 9);
476   return Binary;
477 }
478
479 unsigned ARMMCCodeEmitter::
480 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
481                 SmallVectorImpl<MCFixup> &Fixups) const {
482   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
483   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
484   // case the imm contains the amount to shift by.
485   //
486   // {3-0} = Rm.
487   // {4}   = 1 if reg shift, 0 if imm shift
488   // {6-5} = type
489   //    If reg shift:
490   //      {11-8} = Rs
491   //      {7}    = 0
492   //    else (imm shift)
493   //      {11-7} = imm
494
495   const MCOperand &MO  = MI.getOperand(OpIdx);
496   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
497   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
498   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
499
500   // Encode Rm.
501   unsigned Binary = getARMRegisterNumbering(MO.getReg());
502
503   // Encode the shift opcode.
504   unsigned SBits = 0;
505   unsigned Rs = MO1.getReg();
506   if (Rs) {
507     // Set shift operand (bit[7:4]).
508     // LSL - 0001
509     // LSR - 0011
510     // ASR - 0101
511     // ROR - 0111
512     // RRX - 0110 and bit[11:8] clear.
513     switch (SOpc) {
514     default: llvm_unreachable("Unknown shift opc!");
515     case ARM_AM::lsl: SBits = 0x1; break;
516     case ARM_AM::lsr: SBits = 0x3; break;
517     case ARM_AM::asr: SBits = 0x5; break;
518     case ARM_AM::ror: SBits = 0x7; break;
519     case ARM_AM::rrx: SBits = 0x6; break;
520     }
521   } else {
522     // Set shift operand (bit[6:4]).
523     // LSL - 000
524     // LSR - 010
525     // ASR - 100
526     // ROR - 110
527     switch (SOpc) {
528     default: llvm_unreachable("Unknown shift opc!");
529     case ARM_AM::lsl: SBits = 0x0; break;
530     case ARM_AM::lsr: SBits = 0x2; break;
531     case ARM_AM::asr: SBits = 0x4; break;
532     case ARM_AM::ror: SBits = 0x6; break;
533     }
534   }
535
536   Binary |= SBits << 4;
537   if (SOpc == ARM_AM::rrx)
538     return Binary;
539
540   // Encode the shift operation Rs or shift_imm (except rrx).
541   if (Rs) {
542     // Encode Rs bit[11:8].
543     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
544     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
545   }
546
547   // Encode shift_imm bit[11:7].
548   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
549 }
550
551 unsigned ARMMCCodeEmitter::
552 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
553                                SmallVectorImpl<MCFixup> &Fixups) const {
554   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
555   // msb of the mask.
556   const MCOperand &MO = MI.getOperand(Op);
557   uint32_t v = ~MO.getImm();
558   uint32_t lsb = CountTrailingZeros_32(v);
559   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
560   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
561   return lsb | (msb << 5);
562 }
563
564 unsigned ARMMCCodeEmitter::
565 getRegisterListOpValue(const MCInst &MI, unsigned Op,
566                        SmallVectorImpl<MCFixup> &Fixups) const {
567   // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
568   // register in the list, set the corresponding bit.
569   unsigned Binary = 0;
570   for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
571     unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
572     Binary |= 1 << regno;
573   }
574   return Binary;
575 }
576
577 unsigned ARMMCCodeEmitter::
578 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
579                            SmallVectorImpl<MCFixup> &Fixups) const {
580   const MCOperand &Reg = MI.getOperand(Op);
581   const MCOperand &Imm = MI.getOperand(Op + 1);
582
583   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
584   unsigned Align = 0;
585
586   switch (Imm.getImm()) {
587   default: break;
588   case 2:
589   case 4:
590   case 8:  Align = 0x01; break;
591   case 16: Align = 0x02; break;
592   case 32: Align = 0x03; break;
593   }
594
595   return RegNo | (Align << 4);
596 }
597
598 unsigned ARMMCCodeEmitter::
599 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
600                           SmallVectorImpl<MCFixup> &Fixups) const {
601   const MCOperand &MO = MI.getOperand(Op);
602   if (MO.getReg() == 0) return 0x0D;
603   return MO.getReg();
604 }
605
606 void ARMMCCodeEmitter::
607 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
608                   SmallVectorImpl<MCFixup> &Fixups) const {
609   // Pseudo instructions don't get encoded.
610   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
611   if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
612     return;
613
614   EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
615   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
616 }
617
618 #include "ARMGenMCCodeEmitter.inc"