Rename the encoder method for t_cbtarget to match.
[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 "mccodeemitter"
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                       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 },
62     };
63
64     if (Kind < FirstTargetFixupKind)
65       return MCCodeEmitter::getFixupKindInfo(Kind);
66
67     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
68            "Invalid kind!");
69     return Infos[Kind - FirstTargetFixupKind];
70   }
71   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
72
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;
77
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;
82
83   /// getMovtImmOpValue - Return the encoding for the movw/movt pair
84   uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
85                              SmallVectorImpl<MCFixup> &Fixups) const;
86
87   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
88                               unsigned &Reg, unsigned &Imm,
89                               SmallVectorImpl<MCFixup> &Fixups) const;
90
91   /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
92   /// BL branch target.
93   uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
94                                    SmallVectorImpl<MCFixup> &Fixups) const;
95
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;
100
101   /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
102   uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
103                                    SmallVectorImpl<MCFixup> &Fixups) const;
104
105   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
106   /// branch target.
107   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
108                                   SmallVectorImpl<MCFixup> &Fixups) const;
109
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;
114
115   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
116   /// operand.
117   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
118                                    SmallVectorImpl<MCFixup> &Fixups) const;
119
120   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
121   /// operand.
122   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
123                                    SmallVectorImpl<MCFixup> &Fixups) const;
124
125
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;
130
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();
135     switch (Mode) {
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;
141     }
142   }
143   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
144   ///
145   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
146     switch (ShOpc) {
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;
152     case ARM_AM::ror:
153     case ARM_AM::rrx: return 3;
154     }
155     return 0;
156   }
157
158   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
159   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
160                                SmallVectorImpl<MCFixup> &Fixups) const;
161
162   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
163   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
164                                      SmallVectorImpl<MCFixup> &Fixups) const;
165
166   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
167   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
168                                      SmallVectorImpl<MCFixup> &Fixups) const;
169
170   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
171   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
172                                SmallVectorImpl<MCFixup> &Fixups) const;
173
174   /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
175   /// operand.
176   uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
177                                      SmallVectorImpl<MCFixup> &Fixups) const;
178
179   /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
180   uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
181                                 SmallVectorImpl<MCFixup> &Fixups) const;
182
183   /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
184   uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
185                                 SmallVectorImpl<MCFixup> &Fixups) const;
186
187   /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
188   uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
189                                 SmallVectorImpl<MCFixup> &Fixups) const;
190
191   /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
192   uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
193                                 SmallVectorImpl<MCFixup> &Fixups) const;
194
195   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
196   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
197                                SmallVectorImpl<MCFixup> &Fixups) const;
198
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
203     // '1' respectively.
204     return MI.getOperand(Op).getReg() == ARM::CPSR;
205   }
206
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!");
213
214     // Encode rotate_imm.
215     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
216       << ARMII::SoRotImmShift;
217
218     // Encode immed_8.
219     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
220     return Binary;
221   }
222   
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?");
229     return Encoded;
230   }
231
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;
240
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;
246
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!");
251     case 0:  return 0;
252     case 8:  return 1;
253     case 16: return 2;
254     case 24: return 3;
255     }
256   }
257
258   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
259                                  SmallVectorImpl<MCFixup> &Fixups) const {
260     return MI.getOperand(Op).getImm() - 1;
261   }
262
263   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
264                                    SmallVectorImpl<MCFixup> &Fixups) const {
265     return 64 - MI.getOperand(Op).getImm();
266   }
267
268   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
269                                       SmallVectorImpl<MCFixup> &Fixups) const;
270
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;
279
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;
286
287   unsigned VFPThumb2PostEncoder(const MCInst &MI,
288                                 unsigned EncodedValue) const;
289
290   void EmitByte(unsigned char C, raw_ostream &OS) const {
291     OS << (char)C;
292   }
293
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);
298       Val >>= 8;
299     }
300   }
301
302   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
303                          SmallVectorImpl<MCFixup> &Fixups) const;
304 };
305
306 } // end anonymous namespace
307
308 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
309                                             MCContext &Ctx) {
310   return new ARMMCCodeEmitter(TM, Ctx);
311 }
312
313 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing 
314 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
315 /// Thumb2 mode.
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
322     // set to 1111.
323     unsigned Bit24 = EncodedValue & 0x01000000;
324     unsigned Bit28 = Bit24 << 4;
325     EncodedValue &= 0xEFFFFFFF;
326     EncodedValue |= Bit28;
327     EncodedValue |= 0x0F000000;
328   }
329   
330   return EncodedValue;
331 }
332
333 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
334 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
335 /// Thumb2 mode.
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;
342   }
343   
344   return EncodedValue;
345 }
346
347 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
348 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
349 /// Thumb2 mode.
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;
356   }
357   
358   return EncodedValue;
359 }
360
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;
368   }
369   return EncodedValue;
370 }
371
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 {
377   if (MO.isReg()) {
378     unsigned Reg = MO.getReg();
379     unsigned RegNo = getARMRegisterNumbering(Reg);
380
381     // Q registers are encoded as 2x their register number.
382     switch (Reg) {
383     default:
384       return RegNo;
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:
389       return 2 * RegNo;
390     }
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());
396   }
397
398   llvm_unreachable("Unable to encode MCOperand!");
399   return 0;
400 }
401
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);
408
409   Reg = getARMRegisterNumbering(MO.getReg());
410
411   int32_t SImm = MO1.getImm();
412   bool isAdd = true;
413
414   // Special value for #-0
415   if (SImm == INT32_MIN)
416     SImm = 0;
417
418   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
419   if (SImm < 0) {
420     SImm = -SImm;
421     isAdd = false;
422   }
423
424   Imm = SImm;
425   return isAdd;
426 }
427
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,
431                                        unsigned FixupKind,
432                                        SmallVectorImpl<MCFixup> &Fixups) {
433   const MCOperand &MO = MI.getOperand(OpIdx);
434
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));
441
442   // All of the information is in the fixup.
443   return 0;
444 }
445
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);
451 }
452
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);
459 }
460
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);
466 }
467
468 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
469 /// target.
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);
477 }
478
479 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
480 /// target.
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,
486                                   Fixups);
487 }
488
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 {
493   // {17-13} = reg
494   // {12}    = (U)nsigned (add == '1', sub == '0')
495   // {11-0}  = imm12
496   unsigned Reg, Imm12;
497   bool isAdd = true;
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.
503     Imm12 = 0;
504     isAdd = false ; // 'U' bit is set as part of the fixup.
505
506     const MCExpr *Expr = 0;
507     if (!MO.isReg())
508       Expr = MO.getExpr();
509     else
510       Expr = MO2.getExpr();
511     
512     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
513     MCFixupKind Kind;
514     if (Subtarget.isThumb2())
515       Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
516     else
517       Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
518     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
519
520     ++MCNumCPRelocations;
521   } else
522     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
523
524   uint32_t Binary = Imm12 & 0xfff;
525   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
526   if (isAdd)
527     Binary |= (1 << 12);
528   Binary |= (Reg << 13);
529   return Binary;
530 }
531
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 {
537   // {17-13} = reg
538   // {12}    = (U)nsigned (add == '1', sub == '0')
539   // {11-0}  = imm8
540   unsigned Reg, Imm8;
541   bool isAdd = true;
542   // If The first operand isn't a register, we have a label reference.
543   const MCOperand &MO = MI.getOperand(OpIdx);
544   if (!MO.isReg()) {
545     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
546     Imm8 = 0;
547     isAdd = false ; // 'U' bit is set as part of the fixup.
548
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));
553
554     ++MCNumCPRelocations;
555   } else
556     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
557
558   uint32_t Binary = (Imm8 >> 2) & 0xff;
559   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
560   if (isAdd)
561     Binary |= (1 << 9);
562   Binary |= (Reg << 9);
563   return Binary;
564 }
565
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); 
572   if (MO.isImm()) {
573     return static_cast<unsigned>(MO.getImm());
574   } else if (const MCSymbolRefExpr *Expr = 
575              dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
576     MCFixupKind Kind;
577     switch (Expr->getKind()) {
578     default: assert(0 && "Unsupported ARMFixup");
579     case MCSymbolRefExpr::VK_ARM_HI16:
580       Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
581       break;
582     case MCSymbolRefExpr::VK_ARM_LO16:
583       Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
584       break;
585     }
586     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
587     return 0;
588   };
589   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
590   return 0;
591 }
592
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);
605
606   // {16-13} = Rn
607   // {12}    = isAdd
608   // {11-0}  = shifter
609   //  {3-0}  = Rm
610   //  {4}    = 0
611   //  {6-5}  = type
612   //  {11-7} = imm
613   uint32_t Binary = Rm;
614   Binary |= Rn << 13;
615   Binary |= SBits << 5;
616   Binary |= ShImm << 7;
617   if (isAdd)
618     Binary |= 1 << 12;
619   return Binary;
620 }
621
622 uint32_t ARMMCCodeEmitter::
623 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
624                     SmallVectorImpl<MCFixup> &Fixups) const {
625   // {17-14}  Rn
626   // {13}     1 == imm12, 0 == Rm
627   // {12}     isAdd
628   // {11-0}   imm12/Rm
629   const MCOperand &MO = MI.getOperand(OpIdx);
630   unsigned Rn = getARMRegisterNumbering(MO.getReg());
631   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
632   Binary |= Rn << 14;
633   return Binary;
634 }
635
636 uint32_t ARMMCCodeEmitter::
637 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
638                           SmallVectorImpl<MCFixup> &Fixups) const {
639   // {13}     1 == imm12, 0 == Rm
640   // {12}     isAdd
641   // {11-0}   imm12/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
649   if (isReg) {
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]
654   }
655   return Binary | (isAdd << 12) | (isReg << 13);
656 }
657
658 uint32_t ARMMCCodeEmitter::
659 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
660                           SmallVectorImpl<MCFixup> &Fixups) const {
661   // {9}      1 == imm8, 0 == Rm
662   // {8}      isAdd
663   // {7-4}    imm7_4/zero
664   // {3-0}    imm3_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
672   if (!isImm)
673     Imm8 = getARMRegisterNumbering(MO.getReg());
674   return Imm8 | (isAdd << 8) | (isImm << 9);
675 }
676
677 uint32_t ARMMCCodeEmitter::
678 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
679                     SmallVectorImpl<MCFixup> &Fixups) const {
680   // {13}     1 == imm8, 0 == Rm
681   // {12-9}   Rn
682   // {8}      isAdd
683   // {7-4}    imm7_4/zero
684   // {3-0}    imm3_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
694   if (!isImm)
695     Imm8 = getARMRegisterNumbering(MO1.getReg());
696   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
697 }
698
699 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
700 uint32_t ARMMCCodeEmitter::
701 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
702                           SmallVectorImpl<MCFixup> &Fixups) const {
703   // [SP, #imm]
704   //   {7-0} = imm8
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!");
709 #endif
710   // The immediate is already shifted for the implicit zeroes, so no change
711   // here.
712   return MO1.getImm() & 0xff;
713 }
714
715 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
716 static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
717                                     unsigned Scale) {
718   // [Rn, Rm]
719   //   {5-3} = Rm
720   //   {2-0} = Rn
721   //
722   // [Rn, #imm]
723   //   {7-3} = imm5
724   //   {2-0} = Rn
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;
730
731   if (MO2.getReg() != 0)
732     // Is an immediate.
733     Imm5 = getARMRegisterNumbering(MO2.getReg());
734
735   return (Imm5 << 3) | Rn;
736 }
737
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);
743 }
744
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);
750 }
751
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);
757 }
758
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);
764 }
765
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 {
770   // {12-9} = reg
771   // {8}    = (U)nsigned (add == '1', sub == '0')
772   // {7-0}  = imm8
773   unsigned Reg, Imm8;
774   bool isAdd;
775   // If The first operand isn't a register, we have a label reference.
776   const MCOperand &MO = MI.getOperand(OpIdx);
777   if (!MO.isReg()) {
778     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
779     Imm8 = 0;
780     isAdd = false; // 'U' bit is handled as part of the fixup.
781
782     assert(MO.isExpr() && "Unexpected machine operand type!");
783     const MCExpr *Expr = MO.getExpr();
784     MCFixupKind Kind;
785     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
786     if (Subtarget.isThumb2())
787       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
788     else
789       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
790     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
791
792     ++MCNumCPRelocations;
793   } else {
794     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
795     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
796   }
797
798   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
799   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
800   if (isAdd)
801     Binary |= (1 << 8);
802   Binary |= (Reg << 9);
803   return Binary;
804 }
805
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.
812   //
813   // {3-0} = Rm.
814   // {4}   = 1 if reg shift, 0 if imm shift
815   // {6-5} = type
816   //    If reg shift:
817   //      {11-8} = Rs
818   //      {7}    = 0
819   //    else (imm shift)
820   //      {11-7} = imm
821
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());
826
827   // Encode Rm.
828   unsigned Binary = getARMRegisterNumbering(MO.getReg());
829
830   // Encode the shift opcode.
831   unsigned SBits = 0;
832   unsigned Rs = MO1.getReg();
833   if (Rs) {
834     // Set shift operand (bit[7:4]).
835     // LSL - 0001
836     // LSR - 0011
837     // ASR - 0101
838     // ROR - 0111
839     // RRX - 0110 and bit[11:8] clear.
840     switch (SOpc) {
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;
847     }
848   } else {
849     // Set shift operand (bit[6:4]).
850     // LSL - 000
851     // LSR - 010
852     // ASR - 100
853     // ROR - 110
854     switch (SOpc) {
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;
860     }
861   }
862
863   Binary |= SBits << 4;
864   if (SOpc == ARM_AM::rrx)
865     return Binary;
866
867   // Encode the shift operation Rs or shift_imm (except rrx).
868   if (Rs) {
869     // Encode Rs bit[11:8].
870     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
871     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
872   }
873
874   // Encode shift_imm bit[11:7].
875   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
876 }
877
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);                 
884   
885   // Encoded as [Rn, Rm, imm].
886   // FIXME: Needs fixup support.
887   unsigned Value = getARMRegisterNumbering(MO1.getReg());
888   Value <<= 4;
889   Value |= getARMRegisterNumbering(MO2.getReg());
890   Value <<= 2;
891   Value |= MO3.getImm();
892   
893   return Value;
894 }
895
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);
901
902   // FIXME: Needs fixup support.
903   unsigned Value = getARMRegisterNumbering(MO1.getReg());
904   
905   // Even though the immediate is 8 bits long, we need 9 bits in order
906   // to represent the (inverse of the) sign bit.
907   Value <<= 9;
908   int32_t tmp = (int32_t)MO2.getImm();
909   if (tmp < 0)
910     tmp = abs(tmp);
911   else
912     Value |= 256; // Set the ADD bit
913   Value |= tmp & 255;
914   return Value;
915 }
916
917 unsigned ARMMCCodeEmitter::
918 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
919                          SmallVectorImpl<MCFixup> &Fixups) const {
920   const MCOperand &MO1 = MI.getOperand(OpNum);
921
922   // FIXME: Needs fixup support.
923   unsigned Value = 0;
924   int32_t tmp = (int32_t)MO1.getImm();
925   if (tmp < 0)
926     tmp = abs(tmp);
927   else
928     Value |= 256; // Set the ADD bit
929   Value |= tmp & 255;
930   return Value;
931 }
932
933 unsigned ARMMCCodeEmitter::
934 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
935                          SmallVectorImpl<MCFixup> &Fixups) const {
936   const MCOperand &MO1 = MI.getOperand(OpNum);
937
938   // FIXME: Needs fixup support.
939   unsigned Value = 0;
940   int32_t tmp = (int32_t)MO1.getImm();
941   if (tmp < 0)
942     tmp = abs(tmp);
943   else
944     Value |= 4096; // Set the ADD bit
945   Value |= tmp & 4095;
946   return Value;
947 }
948
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.
954   //
955   // {3-0} = Rm.
956   // {4}   = 0
957   // {6-5} = type
958   // {11-7} = imm
959
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());
963
964   // Encode Rm.
965   unsigned Binary = getARMRegisterNumbering(MO.getReg());
966
967   // Encode the shift opcode.
968   unsigned SBits = 0;
969   // Set shift operand (bit[6:4]).
970   // LSL - 000
971   // LSR - 010
972   // ASR - 100
973   // ROR - 110
974   switch (SOpc) {
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;
980   }
981
982   Binary |= SBits << 4;
983   if (SOpc == ARM_AM::rrx)
984     return Binary;
985
986   // Encode shift_imm bit[11:7].
987   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
988 }
989
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
994   // msb of the mask.
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);
1001 }
1002
1003 unsigned ARMMCCodeEmitter::
1004 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1005                        SmallVectorImpl<MCFixup> &Fixups) const {
1006   // VLDM/VSTM:
1007   //   {12-8} = Vd
1008   //   {7-0}  = Number of registers
1009   //
1010   // LDM/STM:
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);
1015
1016   unsigned Binary = 0;
1017
1018   if (SPRRegs || DPRRegs) {
1019     // VLDM/VSTM
1020     unsigned RegNo = getARMRegisterNumbering(Reg);
1021     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1022     Binary |= (RegNo & 0x1f) << 8;
1023     if (SPRRegs)
1024       Binary |= NumRegs;
1025     else
1026       Binary |= NumRegs * 2;
1027   } else {
1028     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1029       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1030       Binary |= 1 << RegNo;
1031     }
1032   }
1033
1034   return Binary;
1035 }
1036
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);
1044
1045   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1046   unsigned Align = 0;
1047
1048   switch (Imm.getImm()) {
1049   default: break;
1050   case 2:
1051   case 4:
1052   case 8:  Align = 0x01; break;
1053   case 16: Align = 0x02; break;
1054   case 32: Align = 0x03; break;
1055   }
1056
1057   return RegNo | (Align << 4);
1058 }
1059
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);
1069
1070   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1071   unsigned Align = 0;
1072
1073   switch (Imm.getImm()) {
1074   default: break;
1075   case 2:
1076   case 4:
1077   case 8:  Align = 0x01; break;
1078   case 16: Align = 0x03; break;
1079   }
1080
1081   return RegNo | (Align << 4);
1082 }
1083
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;
1089   return MO.getReg();
1090 }
1091
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)
1100     return;
1101   int Size;
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;
1107   }
1108   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1109   // Thumb 32-bit wide instructions need to be have the high order halfword
1110   // emitted first.
1111   if (Subtarget.isThumb() && Size == 4) {
1112     EmitConstant(Binary >> 16, 2, OS);
1113     EmitConstant(Binary & 0xffff, 2, OS);
1114   } else
1115     EmitConstant(Binary, Size, OS);
1116   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1117 }
1118
1119 #include "ARMGenMCCodeEmitter.inc"