833bd22c7482dbf253c71dd0f87c5a38133187e6
[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 // This table *must* be in the order that the fixup_* kinds are defined in
49 // ARMFixupKinds.h.
50 //
51 // Name                      Offset (bits) Size (bits)     Flags
52 { "fixup_arm_ldst_pcrel_12", 1,            24,  MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_t2_ldst_pcrel_12",  0,            32,  MCFixupKindInfo::FKF_IsPCRel |
54                                                 MCFixupKindInfo::FKF_IsAligned},
55 { "fixup_arm_pcrel_10",      1,            24,  MCFixupKindInfo::FKF_IsPCRel },
56 { "fixup_t2_pcrel_10",       0,            32,  MCFixupKindInfo::FKF_IsPCRel |
57                                                 MCFixupKindInfo::FKF_IsAligned},
58 { "fixup_arm_adr_pcrel_12",  1,            24,  MCFixupKindInfo::FKF_IsPCRel },
59 { "fixup_arm_branch",        1,            24,  MCFixupKindInfo::FKF_IsPCRel },
60 { "fixup_t2_branch",         0,            32,  MCFixupKindInfo::FKF_IsPCRel },
61 { "fixup_arm_thumb_br",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
62 { "fixup_arm_thumb_bl",      0,            32,  MCFixupKindInfo::FKF_IsPCRel },
63 { "fixup_arm_thumb_blx",     7,            21,  MCFixupKindInfo::FKF_IsPCRel },
64 { "fixup_arm_thumb_cb",      0,            16,  MCFixupKindInfo::FKF_IsPCRel },
65 { "fixup_arm_thumb_cp",      1,             8,  MCFixupKindInfo::FKF_IsPCRel },
66 { "fixup_arm_thumb_bcc",     1,             8,  MCFixupKindInfo::FKF_IsPCRel },
67 { "fixup_arm_movt_hi16",     0,            16,  0 },
68 { "fixup_arm_movw_lo16",     0,            16,  0 },
69     };
70
71     if (Kind < FirstTargetFixupKind)
72       return MCCodeEmitter::getFixupKindInfo(Kind);
73
74     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
75            "Invalid kind!");
76     return Infos[Kind - FirstTargetFixupKind];
77   }
78   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
79
80   // getBinaryCodeForInstr - TableGen'erated function for getting the
81   // binary encoding for an instruction.
82   unsigned getBinaryCodeForInstr(const MCInst &MI,
83                                  SmallVectorImpl<MCFixup> &Fixups) const;
84
85   /// getMachineOpValue - Return binary encoding of operand. If the machine
86   /// operand requires relocation, record the relocation and return zero.
87   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
88                              SmallVectorImpl<MCFixup> &Fixups) const;
89
90   /// getMovtImmOpValue - Return the encoding for the movw/movt pair
91   uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
92                              SmallVectorImpl<MCFixup> &Fixups) const;
93
94   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
95                               unsigned &Reg, unsigned &Imm,
96                               SmallVectorImpl<MCFixup> &Fixups) const;
97
98   /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
99   /// BL branch target.
100   uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
101                                    SmallVectorImpl<MCFixup> &Fixups) const;
102
103   /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
104   /// BLX branch target.
105   uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
106                                     SmallVectorImpl<MCFixup> &Fixups) const;
107
108   /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
109   uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
110                                    SmallVectorImpl<MCFixup> &Fixups) const;
111
112   /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
113   uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
114                                     SmallVectorImpl<MCFixup> &Fixups) const;
115
116   /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
117   uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
118                                    SmallVectorImpl<MCFixup> &Fixups) const;
119
120   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
121   /// branch target.
122   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
123                                   SmallVectorImpl<MCFixup> &Fixups) const;
124
125   /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
126   /// ADR label target.
127   uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
128                               SmallVectorImpl<MCFixup> &Fixups) const;
129
130   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
131   /// operand.
132   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
133                                    SmallVectorImpl<MCFixup> &Fixups) const;
134
135   /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
136   uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
137                                      SmallVectorImpl<MCFixup> &Fixups) const;
138
139   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
140   /// operand.
141   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
142                                    SmallVectorImpl<MCFixup> &Fixups) const;
143
144
145   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
146   /// operand as needed by load/store instructions.
147   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
148                                SmallVectorImpl<MCFixup> &Fixups) const;
149
150   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
151   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
152                                SmallVectorImpl<MCFixup> &Fixups) const {
153     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
154     switch (Mode) {
155     default: assert(0 && "Unknown addressing sub-mode!");
156     case ARM_AM::da: return 0;
157     case ARM_AM::ia: return 1;
158     case ARM_AM::db: return 2;
159     case ARM_AM::ib: return 3;
160     }
161   }
162   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
163   ///
164   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
165     switch (ShOpc) {
166     default: llvm_unreachable("Unknown shift opc!");
167     case ARM_AM::no_shift:
168     case ARM_AM::lsl: return 0;
169     case ARM_AM::lsr: return 1;
170     case ARM_AM::asr: return 2;
171     case ARM_AM::ror:
172     case ARM_AM::rrx: return 3;
173     }
174     return 0;
175   }
176
177   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
178   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
179                                SmallVectorImpl<MCFixup> &Fixups) const;
180
181   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
182   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
183                                      SmallVectorImpl<MCFixup> &Fixups) const;
184
185   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
186   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
187                                      SmallVectorImpl<MCFixup> &Fixups) const;
188
189   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
190   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
191                                SmallVectorImpl<MCFixup> &Fixups) const;
192
193   /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
194   /// operand.
195   uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
196                                      SmallVectorImpl<MCFixup> &Fixups) const;
197
198   /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
199   uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
200                                SmallVectorImpl<MCFixup> &) const;
201
202   /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
203   uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
204                                 SmallVectorImpl<MCFixup> &Fixups) const;
205
206   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
207   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
208                                SmallVectorImpl<MCFixup> &Fixups) const;
209
210   /// getCCOutOpValue - Return encoding of the 's' bit.
211   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
212                            SmallVectorImpl<MCFixup> &Fixups) const {
213     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
214     // '1' respectively.
215     return MI.getOperand(Op).getReg() == ARM::CPSR;
216   }
217
218   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
219   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
220                            SmallVectorImpl<MCFixup> &Fixups) const {
221     unsigned SoImm = MI.getOperand(Op).getImm();
222     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
223     assert(SoImmVal != -1 && "Not a valid so_imm value!");
224
225     // Encode rotate_imm.
226     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
227       << ARMII::SoRotImmShift;
228
229     // Encode immed_8.
230     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
231     return Binary;
232   }
233
234   /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
235   unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
236                            SmallVectorImpl<MCFixup> &Fixups) const {
237     unsigned SoImm = MI.getOperand(Op).getImm();
238     unsigned Encoded =  ARM_AM::getT2SOImmVal(SoImm);
239     assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
240     return Encoded;
241   }
242
243   unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
244     SmallVectorImpl<MCFixup> &Fixups) const;
245   unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
246     SmallVectorImpl<MCFixup> &Fixups) const;
247   unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
248     SmallVectorImpl<MCFixup> &Fixups) const;
249   unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
250     SmallVectorImpl<MCFixup> &Fixups) const;
251
252   /// getSORegOpValue - Return an encoded so_reg shifted register value.
253   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
254                            SmallVectorImpl<MCFixup> &Fixups) const;
255   unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
256                              SmallVectorImpl<MCFixup> &Fixups) const;
257
258   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
259                             SmallVectorImpl<MCFixup> &Fixups) const {
260     switch (MI.getOperand(Op).getImm()) {
261     default: assert (0 && "Not a valid rot_imm value!");
262     case 0:  return 0;
263     case 8:  return 1;
264     case 16: return 2;
265     case 24: return 3;
266     }
267   }
268
269   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
270                                  SmallVectorImpl<MCFixup> &Fixups) const {
271     return MI.getOperand(Op).getImm() - 1;
272   }
273
274   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
275                                    SmallVectorImpl<MCFixup> &Fixups) const {
276     return 64 - MI.getOperand(Op).getImm();
277   }
278
279   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
280                                       SmallVectorImpl<MCFixup> &Fixups) const;
281
282   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
283                                   SmallVectorImpl<MCFixup> &Fixups) const;
284   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
285                                       SmallVectorImpl<MCFixup> &Fixups) const;
286   unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
287                                         SmallVectorImpl<MCFixup> &Fixups) const;
288   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
289                                      SmallVectorImpl<MCFixup> &Fixups) const;
290
291   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
292                                       unsigned EncodedValue) const;
293   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
294                                           unsigned EncodedValue) const;
295   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
296                                     unsigned EncodedValue) const;
297
298   unsigned VFPThumb2PostEncoder(const MCInst &MI,
299                                 unsigned EncodedValue) const;
300
301   void EmitByte(unsigned char C, raw_ostream &OS) const {
302     OS << (char)C;
303   }
304
305   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
306     // Output the constant in little endian byte order.
307     for (unsigned i = 0; i != Size; ++i) {
308       EmitByte(Val & 255, OS);
309       Val >>= 8;
310     }
311   }
312
313   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
314                          SmallVectorImpl<MCFixup> &Fixups) const;
315 };
316
317 } // end anonymous namespace
318
319 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
320                                             MCContext &Ctx) {
321   return new ARMMCCodeEmitter(TM, Ctx);
322 }
323
324 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
325 /// instructions, and rewrite them to their Thumb2 form if we are currently in
326 /// Thumb2 mode.
327 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
328                                                  unsigned EncodedValue) const {
329   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
330   if (Subtarget.isThumb2()) {
331     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
332     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
333     // set to 1111.
334     unsigned Bit24 = EncodedValue & 0x01000000;
335     unsigned Bit28 = Bit24 << 4;
336     EncodedValue &= 0xEFFFFFFF;
337     EncodedValue |= Bit28;
338     EncodedValue |= 0x0F000000;
339   }
340
341   return EncodedValue;
342 }
343
344 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
345 /// instructions, and rewrite them to their Thumb2 form if we are currently in
346 /// Thumb2 mode.
347 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
348                                                  unsigned EncodedValue) const {
349   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
350   if (Subtarget.isThumb2()) {
351     EncodedValue &= 0xF0FFFFFF;
352     EncodedValue |= 0x09000000;
353   }
354
355   return EncodedValue;
356 }
357
358 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
359 /// instructions, and rewrite them to their Thumb2 form if we are currently in
360 /// Thumb2 mode.
361 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
362                                                  unsigned EncodedValue) const {
363   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
364   if (Subtarget.isThumb2()) {
365     EncodedValue &= 0x00FFFFFF;
366     EncodedValue |= 0xEE000000;
367   }
368
369   return EncodedValue;
370 }
371
372 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
373 /// them to their Thumb2 form if we are currently in Thumb2 mode.
374 unsigned ARMMCCodeEmitter::
375 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
376   if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
377     EncodedValue &= 0x0FFFFFFF;
378     EncodedValue |= 0xE0000000;
379   }
380   return EncodedValue;
381 }
382
383 /// getMachineOpValue - Return binary encoding of operand. If the machine
384 /// operand requires relocation, record the relocation and return zero.
385 unsigned ARMMCCodeEmitter::
386 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
387                   SmallVectorImpl<MCFixup> &Fixups) const {
388   if (MO.isReg()) {
389     unsigned Reg = MO.getReg();
390     unsigned RegNo = getARMRegisterNumbering(Reg);
391
392     // Q registers are encoded as 2x their register number.
393     switch (Reg) {
394     default:
395       return RegNo;
396     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
397     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
398     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
399     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
400       return 2 * RegNo;
401     }
402   } else if (MO.isImm()) {
403     return static_cast<unsigned>(MO.getImm());
404   } else if (MO.isFPImm()) {
405     return static_cast<unsigned>(APFloat(MO.getFPImm())
406                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
407   }
408
409   llvm_unreachable("Unable to encode MCOperand!");
410   return 0;
411 }
412
413 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
414 bool ARMMCCodeEmitter::
415 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
416                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
417   const MCOperand &MO  = MI.getOperand(OpIdx);
418   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
419
420   Reg = getARMRegisterNumbering(MO.getReg());
421
422   int32_t SImm = MO1.getImm();
423   bool isAdd = true;
424
425   // Special value for #-0
426   if (SImm == INT32_MIN)
427     SImm = 0;
428
429   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
430   if (SImm < 0) {
431     SImm = -SImm;
432     isAdd = false;
433   }
434
435   Imm = SImm;
436   return isAdd;
437 }
438
439 /// getBranchTargetOpValue - Helper function to get the branch target operand,
440 /// which is either an immediate or requires a fixup.
441 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
442                                        unsigned FixupKind,
443                                        SmallVectorImpl<MCFixup> &Fixups) {
444   const MCOperand &MO = MI.getOperand(OpIdx);
445
446   // If the destination is an immediate, we have nothing to do.
447   if (MO.isImm()) return MO.getImm();
448   assert(MO.isExpr() && "Unexpected branch target type!");
449   const MCExpr *Expr = MO.getExpr();
450   MCFixupKind Kind = MCFixupKind(FixupKind);
451   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
452
453   // All of the information is in the fixup.
454   return 0;
455 }
456
457 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
458 uint32_t ARMMCCodeEmitter::
459 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
460                         SmallVectorImpl<MCFixup> &Fixups) const {
461   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
462 }
463
464 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
465 /// BLX branch target.
466 uint32_t ARMMCCodeEmitter::
467 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
468                          SmallVectorImpl<MCFixup> &Fixups) const {
469   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
470 }
471
472 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
473 uint32_t ARMMCCodeEmitter::
474 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
475                         SmallVectorImpl<MCFixup> &Fixups) const {
476   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
477 }
478
479 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
480 uint32_t ARMMCCodeEmitter::
481 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
482                          SmallVectorImpl<MCFixup> &Fixups) const {
483   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
484 }
485
486 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
487 uint32_t ARMMCCodeEmitter::
488 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
489                         SmallVectorImpl<MCFixup> &Fixups) const {
490   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
491 }
492
493 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
494 /// target.
495 uint32_t ARMMCCodeEmitter::
496 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
497                        SmallVectorImpl<MCFixup> &Fixups) const {
498   // FIXME: This really, really shouldn't use TargetMachine. We don't want
499   // coupling between MC and TM anywhere we can help it.
500   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
501   if (Subtarget.isThumb2())
502     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
503   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
504 }
505
506 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
507 /// target.
508 uint32_t ARMMCCodeEmitter::
509 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
510                    SmallVectorImpl<MCFixup> &Fixups) const {
511   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
512   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
513                                   Fixups);
514 }
515
516 /// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
517 uint32_t ARMMCCodeEmitter::
518 getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
519                         SmallVectorImpl<MCFixup> &Fixups) const {
520   const MCOperand &MO1 = MI.getOperand(OpIdx);
521   const MCOperand &MO2 = MI.getOperand(OpIdx+1);
522   unsigned Rn = getARMRegisterNumbering(MO1.getReg());
523   unsigned Rm = getARMRegisterNumbering(MO2.getReg());
524   return (Rm << 3) | Rn;
525 }
526
527 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
528 uint32_t ARMMCCodeEmitter::
529 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
530                         SmallVectorImpl<MCFixup> &Fixups) const {
531   // {17-13} = reg
532   // {12}    = (U)nsigned (add == '1', sub == '0')
533   // {11-0}  = imm12
534   unsigned Reg, Imm12;
535   bool isAdd = true;
536   // If The first operand isn't a register, we have a label reference.
537   const MCOperand &MO = MI.getOperand(OpIdx);
538   const MCOperand &MO2 = MI.getOperand(OpIdx+1);
539   if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
540     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
541     Imm12 = 0;
542     isAdd = false ; // 'U' bit is set as part of the fixup.
543
544     const MCExpr *Expr = 0;
545     if (!MO.isReg())
546       Expr = MO.getExpr();
547     else
548       Expr = MO2.getExpr();
549
550     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
551     MCFixupKind Kind;
552     if (Subtarget.isThumb2())
553       Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
554     else
555       Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
556     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
557
558     ++MCNumCPRelocations;
559   } else
560     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
561
562   uint32_t Binary = Imm12 & 0xfff;
563   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
564   if (isAdd)
565     Binary |= (1 << 12);
566   Binary |= (Reg << 13);
567   return Binary;
568 }
569
570 /// getT2AddrModeImm8s4OpValue - Return encoding info for
571 /// 'reg +/- imm8<<2' operand.
572 uint32_t ARMMCCodeEmitter::
573 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
574                         SmallVectorImpl<MCFixup> &Fixups) const {
575   // {12-9} = reg
576   // {8}    = (U)nsigned (add == '1', sub == '0')
577   // {7-0}  = imm8
578   unsigned Reg, Imm8;
579   bool isAdd = true;
580   // If The first operand isn't a register, we have a label reference.
581   const MCOperand &MO = MI.getOperand(OpIdx);
582   if (!MO.isReg()) {
583     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
584     Imm8 = 0;
585     isAdd = false ; // 'U' bit is set as part of the fixup.
586
587     assert(MO.isExpr() && "Unexpected machine operand type!");
588     const MCExpr *Expr = MO.getExpr();
589     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
590     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
591
592     ++MCNumCPRelocations;
593   } else
594     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
595
596   uint32_t Binary = (Imm8 >> 2) & 0xff;
597   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
598   if (isAdd)
599     Binary |= (1 << 8);
600   Binary |= (Reg << 9);
601   return Binary;
602 }
603
604 uint32_t ARMMCCodeEmitter::
605 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
606                   SmallVectorImpl<MCFixup> &Fixups) const {
607   // {20-16} = imm{15-12}
608   // {11-0}  = imm{11-0}
609   const MCOperand &MO = MI.getOperand(OpIdx);
610   if (MO.isImm()) {
611     return static_cast<unsigned>(MO.getImm());
612   } else if (const MCSymbolRefExpr *Expr =
613              dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
614     MCFixupKind Kind;
615     switch (Expr->getKind()) {
616     default: assert(0 && "Unsupported ARMFixup");
617     case MCSymbolRefExpr::VK_ARM_HI16:
618       Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
619       break;
620     case MCSymbolRefExpr::VK_ARM_LO16:
621       Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
622       break;
623     }
624     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
625     return 0;
626   };
627   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
628   return 0;
629 }
630
631 uint32_t ARMMCCodeEmitter::
632 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
633                     SmallVectorImpl<MCFixup> &Fixups) const {
634   const MCOperand &MO = MI.getOperand(OpIdx);
635   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
636   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
637   unsigned Rn = getARMRegisterNumbering(MO.getReg());
638   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
639   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
640   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
641   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
642   unsigned SBits = getShiftOp(ShOp);
643
644   // {16-13} = Rn
645   // {12}    = isAdd
646   // {11-0}  = shifter
647   //  {3-0}  = Rm
648   //  {4}    = 0
649   //  {6-5}  = type
650   //  {11-7} = imm
651   uint32_t Binary = Rm;
652   Binary |= Rn << 13;
653   Binary |= SBits << 5;
654   Binary |= ShImm << 7;
655   if (isAdd)
656     Binary |= 1 << 12;
657   return Binary;
658 }
659
660 uint32_t ARMMCCodeEmitter::
661 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
662                     SmallVectorImpl<MCFixup> &Fixups) const {
663   // {17-14}  Rn
664   // {13}     1 == imm12, 0 == Rm
665   // {12}     isAdd
666   // {11-0}   imm12/Rm
667   const MCOperand &MO = MI.getOperand(OpIdx);
668   unsigned Rn = getARMRegisterNumbering(MO.getReg());
669   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
670   Binary |= Rn << 14;
671   return Binary;
672 }
673
674 uint32_t ARMMCCodeEmitter::
675 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
676                           SmallVectorImpl<MCFixup> &Fixups) const {
677   // {13}     1 == imm12, 0 == Rm
678   // {12}     isAdd
679   // {11-0}   imm12/Rm
680   const MCOperand &MO = MI.getOperand(OpIdx);
681   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
682   unsigned Imm = MO1.getImm();
683   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
684   bool isReg = MO.getReg() != 0;
685   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
686   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
687   if (isReg) {
688     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
689     Binary <<= 7;                    // Shift amount is bits [11:7]
690     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
691     Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
692   }
693   return Binary | (isAdd << 12) | (isReg << 13);
694 }
695
696 uint32_t ARMMCCodeEmitter::
697 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
698                           SmallVectorImpl<MCFixup> &Fixups) const {
699   // {9}      1 == imm8, 0 == Rm
700   // {8}      isAdd
701   // {7-4}    imm7_4/zero
702   // {3-0}    imm3_0/Rm
703   const MCOperand &MO = MI.getOperand(OpIdx);
704   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
705   unsigned Imm = MO1.getImm();
706   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
707   bool isImm = MO.getReg() == 0;
708   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
709   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
710   if (!isImm)
711     Imm8 = getARMRegisterNumbering(MO.getReg());
712   return Imm8 | (isAdd << 8) | (isImm << 9);
713 }
714
715 uint32_t ARMMCCodeEmitter::
716 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
717                     SmallVectorImpl<MCFixup> &Fixups) const {
718   // {13}     1 == imm8, 0 == Rm
719   // {12-9}   Rn
720   // {8}      isAdd
721   // {7-4}    imm7_4/zero
722   // {3-0}    imm3_0/Rm
723   const MCOperand &MO = MI.getOperand(OpIdx);
724   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
725   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
726   unsigned Rn = getARMRegisterNumbering(MO.getReg());
727   unsigned Imm = MO2.getImm();
728   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
729   bool isImm = MO1.getReg() == 0;
730   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
731   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
732   if (!isImm)
733     Imm8 = getARMRegisterNumbering(MO1.getReg());
734   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
735 }
736
737 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
738 uint32_t ARMMCCodeEmitter::
739 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
740                           SmallVectorImpl<MCFixup> &Fixups) const {
741   // [SP, #imm]
742   //   {7-0} = imm8
743   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
744 #if 0  // FIXME: This crashes2003-05-14-initialize-string.c
745   assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
746          "Unexpected base register!");
747 #endif
748   // The immediate is already shifted for the implicit zeroes, so no change
749   // here.
750   return MO1.getImm() & 0xff;
751 }
752
753 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
754 uint32_t ARMMCCodeEmitter::
755 getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
756                     SmallVectorImpl<MCFixup> &) const {
757   // [Rn, Rm]
758   //   {5-3} = Rm
759   //   {2-0} = Rn
760   //
761   // [Rn, #imm]
762   //   {7-3} = imm5
763   //   {2-0} = Rn
764   const MCOperand &MO = MI.getOperand(OpIdx);
765   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
766   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
767   unsigned Rn = getARMRegisterNumbering(MO.getReg());
768   unsigned Imm5 = MO1.getImm();
769
770   if (MO2.getReg() != 0)
771     // Is an immediate.
772     Imm5 = getARMRegisterNumbering(MO2.getReg());
773
774   return ((Imm5 & 0x1f) << 3) | Rn;
775 }
776
777 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
778 uint32_t ARMMCCodeEmitter::
779 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
780                      SmallVectorImpl<MCFixup> &Fixups) const {
781   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
782 }
783
784 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
785 uint32_t ARMMCCodeEmitter::
786 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
787                     SmallVectorImpl<MCFixup> &Fixups) const {
788   // {12-9} = reg
789   // {8}    = (U)nsigned (add == '1', sub == '0')
790   // {7-0}  = imm8
791   unsigned Reg, Imm8;
792   bool isAdd;
793   // If The first operand isn't a register, we have a label reference.
794   const MCOperand &MO = MI.getOperand(OpIdx);
795   if (!MO.isReg()) {
796     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
797     Imm8 = 0;
798     isAdd = false; // 'U' bit is handled as part of the fixup.
799
800     assert(MO.isExpr() && "Unexpected machine operand type!");
801     const MCExpr *Expr = MO.getExpr();
802     MCFixupKind Kind;
803     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
804     if (Subtarget.isThumb2())
805       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
806     else
807       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
808     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
809
810     ++MCNumCPRelocations;
811   } else {
812     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
813     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
814   }
815
816   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
817   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
818   if (isAdd)
819     Binary |= (1 << 8);
820   Binary |= (Reg << 9);
821   return Binary;
822 }
823
824 unsigned ARMMCCodeEmitter::
825 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
826                 SmallVectorImpl<MCFixup> &Fixups) const {
827   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
828   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
829   // case the imm contains the amount to shift by.
830   //
831   // {3-0} = Rm.
832   // {4}   = 1 if reg shift, 0 if imm shift
833   // {6-5} = type
834   //    If reg shift:
835   //      {11-8} = Rs
836   //      {7}    = 0
837   //    else (imm shift)
838   //      {11-7} = imm
839
840   const MCOperand &MO  = MI.getOperand(OpIdx);
841   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
842   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
843   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
844
845   // Encode Rm.
846   unsigned Binary = getARMRegisterNumbering(MO.getReg());
847
848   // Encode the shift opcode.
849   unsigned SBits = 0;
850   unsigned Rs = MO1.getReg();
851   if (Rs) {
852     // Set shift operand (bit[7:4]).
853     // LSL - 0001
854     // LSR - 0011
855     // ASR - 0101
856     // ROR - 0111
857     // RRX - 0110 and bit[11:8] clear.
858     switch (SOpc) {
859     default: llvm_unreachable("Unknown shift opc!");
860     case ARM_AM::lsl: SBits = 0x1; break;
861     case ARM_AM::lsr: SBits = 0x3; break;
862     case ARM_AM::asr: SBits = 0x5; break;
863     case ARM_AM::ror: SBits = 0x7; break;
864     case ARM_AM::rrx: SBits = 0x6; break;
865     }
866   } else {
867     // Set shift operand (bit[6:4]).
868     // LSL - 000
869     // LSR - 010
870     // ASR - 100
871     // ROR - 110
872     switch (SOpc) {
873     default: llvm_unreachable("Unknown shift opc!");
874     case ARM_AM::lsl: SBits = 0x0; break;
875     case ARM_AM::lsr: SBits = 0x2; break;
876     case ARM_AM::asr: SBits = 0x4; break;
877     case ARM_AM::ror: SBits = 0x6; break;
878     }
879   }
880
881   Binary |= SBits << 4;
882   if (SOpc == ARM_AM::rrx)
883     return Binary;
884
885   // Encode the shift operation Rs or shift_imm (except rrx).
886   if (Rs) {
887     // Encode Rs bit[11:8].
888     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
889     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
890   }
891
892   // Encode shift_imm bit[11:7].
893   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
894 }
895
896 unsigned ARMMCCodeEmitter::
897 getT2AddrModeSORegOpValue(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   const MCOperand &MO3 = MI.getOperand(OpNum+2);
902
903   // Encoded as [Rn, Rm, imm].
904   // FIXME: Needs fixup support.
905   unsigned Value = getARMRegisterNumbering(MO1.getReg());
906   Value <<= 4;
907   Value |= getARMRegisterNumbering(MO2.getReg());
908   Value <<= 2;
909   Value |= MO3.getImm();
910
911   return Value;
912 }
913
914 unsigned ARMMCCodeEmitter::
915 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
916                          SmallVectorImpl<MCFixup> &Fixups) const {
917   const MCOperand &MO1 = MI.getOperand(OpNum);
918   const MCOperand &MO2 = MI.getOperand(OpNum+1);
919
920   // FIXME: Needs fixup support.
921   unsigned Value = getARMRegisterNumbering(MO1.getReg());
922
923   // Even though the immediate is 8 bits long, we need 9 bits in order
924   // to represent the (inverse of the) sign bit.
925   Value <<= 9;
926   int32_t tmp = (int32_t)MO2.getImm();
927   if (tmp < 0)
928     tmp = abs(tmp);
929   else
930     Value |= 256; // Set the ADD bit
931   Value |= tmp & 255;
932   return Value;
933 }
934
935 unsigned ARMMCCodeEmitter::
936 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
937                          SmallVectorImpl<MCFixup> &Fixups) const {
938   const MCOperand &MO1 = MI.getOperand(OpNum);
939
940   // FIXME: Needs fixup support.
941   unsigned Value = 0;
942   int32_t tmp = (int32_t)MO1.getImm();
943   if (tmp < 0)
944     tmp = abs(tmp);
945   else
946     Value |= 256; // Set the ADD bit
947   Value |= tmp & 255;
948   return Value;
949 }
950
951 unsigned ARMMCCodeEmitter::
952 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
953                          SmallVectorImpl<MCFixup> &Fixups) const {
954   const MCOperand &MO1 = MI.getOperand(OpNum);
955
956   // FIXME: Needs fixup support.
957   unsigned Value = 0;
958   int32_t tmp = (int32_t)MO1.getImm();
959   if (tmp < 0)
960     tmp = abs(tmp);
961   else
962     Value |= 4096; // Set the ADD bit
963   Value |= tmp & 4095;
964   return Value;
965 }
966
967 unsigned ARMMCCodeEmitter::
968 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
969                 SmallVectorImpl<MCFixup> &Fixups) const {
970   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
971   // shifted. The second is the amount to shift by.
972   //
973   // {3-0} = Rm.
974   // {4}   = 0
975   // {6-5} = type
976   // {11-7} = imm
977
978   const MCOperand &MO  = MI.getOperand(OpIdx);
979   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
980   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
981
982   // Encode Rm.
983   unsigned Binary = getARMRegisterNumbering(MO.getReg());
984
985   // Encode the shift opcode.
986   unsigned SBits = 0;
987   // Set shift operand (bit[6:4]).
988   // LSL - 000
989   // LSR - 010
990   // ASR - 100
991   // ROR - 110
992   switch (SOpc) {
993   default: llvm_unreachable("Unknown shift opc!");
994   case ARM_AM::lsl: SBits = 0x0; break;
995   case ARM_AM::lsr: SBits = 0x2; break;
996   case ARM_AM::asr: SBits = 0x4; break;
997   case ARM_AM::ror: SBits = 0x6; break;
998   }
999
1000   Binary |= SBits << 4;
1001   if (SOpc == ARM_AM::rrx)
1002     return Binary;
1003
1004   // Encode shift_imm bit[11:7].
1005   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1006 }
1007
1008 unsigned ARMMCCodeEmitter::
1009 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1010                                SmallVectorImpl<MCFixup> &Fixups) const {
1011   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1012   // msb of the mask.
1013   const MCOperand &MO = MI.getOperand(Op);
1014   uint32_t v = ~MO.getImm();
1015   uint32_t lsb = CountTrailingZeros_32(v);
1016   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1017   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1018   return lsb | (msb << 5);
1019 }
1020
1021 unsigned ARMMCCodeEmitter::
1022 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1023                        SmallVectorImpl<MCFixup> &Fixups) const {
1024   // VLDM/VSTM:
1025   //   {12-8} = Vd
1026   //   {7-0}  = Number of registers
1027   //
1028   // LDM/STM:
1029   //   {15-0}  = Bitfield of GPRs.
1030   unsigned Reg = MI.getOperand(Op).getReg();
1031   bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1032   bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1033
1034   unsigned Binary = 0;
1035
1036   if (SPRRegs || DPRRegs) {
1037     // VLDM/VSTM
1038     unsigned RegNo = getARMRegisterNumbering(Reg);
1039     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1040     Binary |= (RegNo & 0x1f) << 8;
1041     if (SPRRegs)
1042       Binary |= NumRegs;
1043     else
1044       Binary |= NumRegs * 2;
1045   } else {
1046     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1047       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1048       Binary |= 1 << RegNo;
1049     }
1050   }
1051
1052   return Binary;
1053 }
1054
1055 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1056 /// with the alignment operand.
1057 unsigned ARMMCCodeEmitter::
1058 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1059                            SmallVectorImpl<MCFixup> &Fixups) const {
1060   const MCOperand &Reg = MI.getOperand(Op);
1061   const MCOperand &Imm = MI.getOperand(Op + 1);
1062
1063   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1064   unsigned Align = 0;
1065
1066   switch (Imm.getImm()) {
1067   default: break;
1068   case 2:
1069   case 4:
1070   case 8:  Align = 0x01; break;
1071   case 16: Align = 0x02; break;
1072   case 32: Align = 0x03; break;
1073   }
1074
1075   return RegNo | (Align << 4);
1076 }
1077
1078 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1079 /// alignment operand for use in VLD-dup instructions.  This is the same as
1080 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1081 /// different for VLD4-dup.
1082 unsigned ARMMCCodeEmitter::
1083 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1084                               SmallVectorImpl<MCFixup> &Fixups) const {
1085   const MCOperand &Reg = MI.getOperand(Op);
1086   const MCOperand &Imm = MI.getOperand(Op + 1);
1087
1088   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1089   unsigned Align = 0;
1090
1091   switch (Imm.getImm()) {
1092   default: break;
1093   case 2:
1094   case 4:
1095   case 8:  Align = 0x01; break;
1096   case 16: Align = 0x03; break;
1097   }
1098
1099   return RegNo | (Align << 4);
1100 }
1101
1102 unsigned ARMMCCodeEmitter::
1103 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1104                           SmallVectorImpl<MCFixup> &Fixups) const {
1105   const MCOperand &MO = MI.getOperand(Op);
1106   if (MO.getReg() == 0) return 0x0D;
1107   return MO.getReg();
1108 }
1109
1110 void ARMMCCodeEmitter::
1111 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1112                   SmallVectorImpl<MCFixup> &Fixups) const {
1113   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1114   // Pseudo instructions don't get encoded.
1115   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1116   uint64_t TSFlags = Desc.TSFlags;
1117   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1118     return;
1119   int Size;
1120   // Basic size info comes from the TSFlags field.
1121   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1122   default: llvm_unreachable("Unexpected instruction size!");
1123   case ARMII::Size2Bytes: Size = 2; break;
1124   case ARMII::Size4Bytes: Size = 4; break;
1125   }
1126   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1127   // Thumb 32-bit wide instructions need to be have the high order halfword
1128   // emitted first.
1129   if (Subtarget.isThumb() && Size == 4) {
1130     EmitConstant(Binary >> 16, 2, OS);
1131     EmitConstant(Binary & 0xffff, 2, OS);
1132   } else
1133     EmitConstant(Binary, Size, OS);
1134   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1135 }
1136
1137 #include "ARMGenMCCodeEmitter.inc"