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