Fix signed overflow in when computing encodings for ADR instructions
[oota-llvm.git] / lib / Target / ARM / MCTargetDesc / 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 "MCTargetDesc/ARMMCTargetDesc.h"
16 #include "MCTargetDesc/ARMAddressingModes.h"
17 #include "MCTargetDesc/ARMBaseInfo.h"
18 #include "MCTargetDesc/ARMFixupKinds.h"
19 #include "MCTargetDesc/ARMMCExpr.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/Support/raw_ostream.h"
30
31 using namespace llvm;
32
33 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
34 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
35
36 namespace {
37 class ARMMCCodeEmitter : public MCCodeEmitter {
38   ARMMCCodeEmitter(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
39   void operator=(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40   const MCInstrInfo &MCII;
41   const MCSubtargetInfo &STI;
42   const MCContext &CTX;
43
44 public:
45   ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
46                    MCContext &ctx)
47     : MCII(mcii), STI(sti), CTX(ctx) {
48   }
49
50   ~ARMMCCodeEmitter() {}
51
52   bool isThumb() const {
53     // FIXME: Can tablegen auto-generate this?
54     return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
55   }
56   bool isThumb2() const {
57     return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
58   }
59   bool isTargetDarwin() const {
60     Triple TT(STI.getTargetTriple());
61     Triple::OSType OS = TT.getOS();
62     return OS == Triple::Darwin || OS == Triple::MacOSX || OS == Triple::IOS;
63   }
64
65   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
66
67   // getBinaryCodeForInstr - TableGen'erated function for getting the
68   // binary encoding for an instruction.
69   uint64_t getBinaryCodeForInstr(const MCInst &MI,
70                                  SmallVectorImpl<MCFixup> &Fixups) const;
71
72   /// getMachineOpValue - Return binary encoding of operand. If the machine
73   /// operand requires relocation, record the relocation and return zero.
74   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
75                              SmallVectorImpl<MCFixup> &Fixups) const;
76
77   /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
78   /// the specified operand. This is used for operands with :lower16: and
79   /// :upper16: prefixes.
80   uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
81                                SmallVectorImpl<MCFixup> &Fixups) const;
82
83   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
84                               unsigned &Reg, unsigned &Imm,
85                               SmallVectorImpl<MCFixup> &Fixups) const;
86
87   /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
88   /// BL branch target.
89   uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
90                                    SmallVectorImpl<MCFixup> &Fixups) const;
91
92   /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
93   /// BLX branch target.
94   uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
95                                     SmallVectorImpl<MCFixup> &Fixups) const;
96
97   /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
98   uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
99                                    SmallVectorImpl<MCFixup> &Fixups) const;
100
101   /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
102   uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
103                                     SmallVectorImpl<MCFixup> &Fixups) const;
104
105   /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
106   uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
107                                    SmallVectorImpl<MCFixup> &Fixups) const;
108
109   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
110   /// branch target.
111   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
112                                   SmallVectorImpl<MCFixup> &Fixups) const;
113
114   /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
115   /// immediate Thumb2 direct branch target.
116   uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
117                                   SmallVectorImpl<MCFixup> &Fixups) const;
118
119   /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
120   /// branch target.
121   uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
122                                      SmallVectorImpl<MCFixup> &Fixups) const;
123   uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
124                                  SmallVectorImpl<MCFixup> &Fixups) const;
125   uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
126                                   SmallVectorImpl<MCFixup> &Fixups) const;
127
128   /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
129   /// ADR label target.
130   uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
131                               SmallVectorImpl<MCFixup> &Fixups) const;
132   uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
133                               SmallVectorImpl<MCFixup> &Fixups) const;
134   uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
135                               SmallVectorImpl<MCFixup> &Fixups) const;
136
137
138   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
139   /// operand.
140   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
141                                    SmallVectorImpl<MCFixup> &Fixups) const;
142
143   /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
144   uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
145                                          SmallVectorImpl<MCFixup> &Fixups)const;
146
147   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
148   /// operand.
149   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
150                                    SmallVectorImpl<MCFixup> &Fixups) const;
151
152   /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
153   /// operand.
154   uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
155                                    SmallVectorImpl<MCFixup> &Fixups) const;
156
157   /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
158   /// operand.
159   uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
160                               SmallVectorImpl<MCFixup> &Fixups) const;
161
162
163   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
164   /// operand as needed by load/store instructions.
165   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
166                                SmallVectorImpl<MCFixup> &Fixups) const;
167
168   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
169   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
170                                SmallVectorImpl<MCFixup> &Fixups) const {
171     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
172     switch (Mode) {
173     default: llvm_unreachable("Unknown addressing sub-mode!");
174     case ARM_AM::da: return 0;
175     case ARM_AM::ia: return 1;
176     case ARM_AM::db: return 2;
177     case ARM_AM::ib: return 3;
178     }
179   }
180   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
181   ///
182   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
183     switch (ShOpc) {
184     case ARM_AM::no_shift:
185     case ARM_AM::lsl: return 0;
186     case ARM_AM::lsr: return 1;
187     case ARM_AM::asr: return 2;
188     case ARM_AM::ror:
189     case ARM_AM::rrx: return 3;
190     }
191     llvm_unreachable("Invalid ShiftOpc!");
192   }
193
194   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
195   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
196                                SmallVectorImpl<MCFixup> &Fixups) const;
197
198   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
199   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
200                                      SmallVectorImpl<MCFixup> &Fixups) const;
201
202   /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
203   uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
204                                 SmallVectorImpl<MCFixup> &Fixups) const;
205
206   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
207   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
208                                      SmallVectorImpl<MCFixup> &Fixups) const;
209
210   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
211   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
212                                SmallVectorImpl<MCFixup> &Fixups) const;
213
214   /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
215   /// operand.
216   uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
217                                      SmallVectorImpl<MCFixup> &Fixups) const;
218
219   /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
220   uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
221                                 SmallVectorImpl<MCFixup> &Fixups) const;
222
223   /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
224   uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
225                                 SmallVectorImpl<MCFixup> &Fixups) const;
226
227   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
228   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
229                                SmallVectorImpl<MCFixup> &Fixups) const;
230
231   /// getCCOutOpValue - Return encoding of the 's' bit.
232   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
233                            SmallVectorImpl<MCFixup> &Fixups) const {
234     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
235     // '1' respectively.
236     return MI.getOperand(Op).getReg() == ARM::CPSR;
237   }
238
239   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
240   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
241                            SmallVectorImpl<MCFixup> &Fixups) const {
242     unsigned SoImm = MI.getOperand(Op).getImm();
243     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
244     assert(SoImmVal != -1 && "Not a valid so_imm value!");
245
246     // Encode rotate_imm.
247     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
248       << ARMII::SoRotImmShift;
249
250     // Encode immed_8.
251     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
252     return Binary;
253   }
254
255   /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
256   unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
257                            SmallVectorImpl<MCFixup> &Fixups) const {
258     unsigned SoImm = MI.getOperand(Op).getImm();
259     unsigned Encoded =  ARM_AM::getT2SOImmVal(SoImm);
260     assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
261     return Encoded;
262   }
263
264   unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
265     SmallVectorImpl<MCFixup> &Fixups) const;
266   unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
267     SmallVectorImpl<MCFixup> &Fixups) const;
268   unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
269     SmallVectorImpl<MCFixup> &Fixups) const;
270   unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
271     SmallVectorImpl<MCFixup> &Fixups) const;
272
273   /// getSORegOpValue - Return an encoded so_reg shifted register value.
274   unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
275                            SmallVectorImpl<MCFixup> &Fixups) const;
276   unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
277                            SmallVectorImpl<MCFixup> &Fixups) const;
278   unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
279                              SmallVectorImpl<MCFixup> &Fixups) const;
280
281   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
282                                    SmallVectorImpl<MCFixup> &Fixups) const {
283     return 64 - MI.getOperand(Op).getImm();
284   }
285
286   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
287                                       SmallVectorImpl<MCFixup> &Fixups) const;
288
289   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290                                   SmallVectorImpl<MCFixup> &Fixups) const;
291   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292                                       SmallVectorImpl<MCFixup> &Fixups) const;
293   unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
294                                         SmallVectorImpl<MCFixup> &Fixups) const;
295   unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
296                                         SmallVectorImpl<MCFixup> &Fixups) const;
297   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
298                                      SmallVectorImpl<MCFixup> &Fixups) const;
299
300   unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
301                              SmallVectorImpl<MCFixup> &Fixups) const;
302   unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
303                               SmallVectorImpl<MCFixup> &Fixups) const;
304   unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
305                               SmallVectorImpl<MCFixup> &Fixups) const;
306   unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
307                               SmallVectorImpl<MCFixup> &Fixups) const;
308
309   unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
310                                  SmallVectorImpl<MCFixup> &Fixups) const;
311
312   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
313                                       unsigned EncodedValue) const;
314   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
315                                           unsigned EncodedValue) const;
316   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
317                                     unsigned EncodedValue) const;
318   unsigned NEONThumb2V8PostEncoder(const MCInst &MI,
319                                    unsigned EncodedValue) const;
320
321   unsigned VFPThumb2PostEncoder(const MCInst &MI,
322                                 unsigned EncodedValue) const;
323
324   void EmitByte(unsigned char C, raw_ostream &OS) const {
325     OS << (char)C;
326   }
327
328   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
329     // Output the constant in little endian byte order.
330     for (unsigned i = 0; i != Size; ++i) {
331       EmitByte(Val & 255, OS);
332       Val >>= 8;
333     }
334   }
335
336   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
337                          SmallVectorImpl<MCFixup> &Fixups) const;
338 };
339
340 } // end anonymous namespace
341
342 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
343                                             const MCRegisterInfo &MRI,
344                                             const MCSubtargetInfo &STI,
345                                             MCContext &Ctx) {
346   return new ARMMCCodeEmitter(MCII, STI, Ctx);
347 }
348
349 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
350 /// instructions, and rewrite them to their Thumb2 form if we are currently in
351 /// Thumb2 mode.
352 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
353                                                  unsigned EncodedValue) const {
354   if (isThumb2()) {
355     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
356     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
357     // set to 1111.
358     unsigned Bit24 = EncodedValue & 0x01000000;
359     unsigned Bit28 = Bit24 << 4;
360     EncodedValue &= 0xEFFFFFFF;
361     EncodedValue |= Bit28;
362     EncodedValue |= 0x0F000000;
363   }
364
365   return EncodedValue;
366 }
367
368 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
369 /// instructions, and rewrite them to their Thumb2 form if we are currently in
370 /// Thumb2 mode.
371 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
372                                                  unsigned EncodedValue) const {
373   if (isThumb2()) {
374     EncodedValue &= 0xF0FFFFFF;
375     EncodedValue |= 0x09000000;
376   }
377
378   return EncodedValue;
379 }
380
381 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
382 /// instructions, and rewrite them to their Thumb2 form if we are currently in
383 /// Thumb2 mode.
384 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
385                                                  unsigned EncodedValue) const {
386   if (isThumb2()) {
387     EncodedValue &= 0x00FFFFFF;
388     EncodedValue |= 0xEE000000;
389   }
390
391   return EncodedValue;
392 }
393
394 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
395 /// if we are in Thumb2.
396 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI,
397                                                  unsigned EncodedValue) const {
398   if (isThumb2()) {
399     EncodedValue |= 0xC000000; // Set bits 27-26
400   }
401
402   return EncodedValue;
403 }
404
405 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
406 /// them to their Thumb2 form if we are currently in Thumb2 mode.
407 unsigned ARMMCCodeEmitter::
408 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
409   if (isThumb2()) {
410     EncodedValue &= 0x0FFFFFFF;
411     EncodedValue |= 0xE0000000;
412   }
413   return EncodedValue;
414 }
415
416 /// getMachineOpValue - Return binary encoding of operand. If the machine
417 /// operand requires relocation, record the relocation and return zero.
418 unsigned ARMMCCodeEmitter::
419 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
420                   SmallVectorImpl<MCFixup> &Fixups) const {
421   if (MO.isReg()) {
422     unsigned Reg = MO.getReg();
423     unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
424
425     // Q registers are encoded as 2x their register number.
426     switch (Reg) {
427     default:
428       return RegNo;
429     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
430     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
431     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
432     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
433       return 2 * RegNo;
434     }
435   } else if (MO.isImm()) {
436     return static_cast<unsigned>(MO.getImm());
437   } else if (MO.isFPImm()) {
438     return static_cast<unsigned>(APFloat(MO.getFPImm())
439                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
440   }
441
442   llvm_unreachable("Unable to encode MCOperand!");
443 }
444
445 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
446 bool ARMMCCodeEmitter::
447 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
448                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
449   const MCOperand &MO  = MI.getOperand(OpIdx);
450   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
451
452   Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
453
454   int32_t SImm = MO1.getImm();
455   bool isAdd = true;
456
457   // Special value for #-0
458   if (SImm == INT32_MIN) {
459     SImm = 0;
460     isAdd = false;
461   }
462
463   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
464   if (SImm < 0) {
465     SImm = -SImm;
466     isAdd = false;
467   }
468
469   Imm = SImm;
470   return isAdd;
471 }
472
473 /// getBranchTargetOpValue - Helper function to get the branch target operand,
474 /// which is either an immediate or requires a fixup.
475 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
476                                        unsigned FixupKind,
477                                        SmallVectorImpl<MCFixup> &Fixups) {
478   const MCOperand &MO = MI.getOperand(OpIdx);
479
480   // If the destination is an immediate, we have nothing to do.
481   if (MO.isImm()) return MO.getImm();
482   assert(MO.isExpr() && "Unexpected branch target type!");
483   const MCExpr *Expr = MO.getExpr();
484   MCFixupKind Kind = MCFixupKind(FixupKind);
485   Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
486
487   // All of the information is in the fixup.
488   return 0;
489 }
490
491 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
492 // determined by negating them and XOR'ing them with bit 23.
493 static int32_t encodeThumbBLOffset(int32_t offset) {
494   offset >>= 1;
495   uint32_t S  = (offset & 0x800000) >> 23;
496   uint32_t J1 = (offset & 0x400000) >> 22;
497   uint32_t J2 = (offset & 0x200000) >> 21;
498   J1 = (~J1 & 0x1);
499   J2 = (~J2 & 0x1);
500   J1 ^= S;
501   J2 ^= S;
502
503   offset &= ~0x600000;
504   offset |= J1 << 22;
505   offset |= J2 << 21;
506
507   return offset;
508 }
509
510 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
511 uint32_t ARMMCCodeEmitter::
512 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
513                         SmallVectorImpl<MCFixup> &Fixups) const {
514   const MCOperand MO = MI.getOperand(OpIdx);
515   if (MO.isExpr())
516     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
517                                     Fixups);
518   return encodeThumbBLOffset(MO.getImm());
519 }
520
521 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
522 /// BLX branch target.
523 uint32_t ARMMCCodeEmitter::
524 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
525                          SmallVectorImpl<MCFixup> &Fixups) const {
526   const MCOperand MO = MI.getOperand(OpIdx);
527   if (MO.isExpr())
528     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
529                                     Fixups);
530   return encodeThumbBLOffset(MO.getImm());
531 }
532
533 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
534 uint32_t ARMMCCodeEmitter::
535 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
536                         SmallVectorImpl<MCFixup> &Fixups) const {
537   const MCOperand MO = MI.getOperand(OpIdx);
538   if (MO.isExpr())
539     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
540                                     Fixups);
541   return (MO.getImm() >> 1);
542 }
543
544 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
545 uint32_t ARMMCCodeEmitter::
546 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
547                          SmallVectorImpl<MCFixup> &Fixups) const {
548   const MCOperand MO = MI.getOperand(OpIdx);
549   if (MO.isExpr())
550     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
551                                     Fixups);
552   return (MO.getImm() >> 1);
553 }
554
555 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
556 uint32_t ARMMCCodeEmitter::
557 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
558                         SmallVectorImpl<MCFixup> &Fixups) const {
559   const MCOperand MO = MI.getOperand(OpIdx);
560   if (MO.isExpr())
561     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
562   return (MO.getImm() >> 1);
563 }
564
565 /// Return true if this branch has a non-always predication
566 static bool HasConditionalBranch(const MCInst &MI) {
567   int NumOp = MI.getNumOperands();
568   if (NumOp >= 2) {
569     for (int i = 0; i < NumOp-1; ++i) {
570       const MCOperand &MCOp1 = MI.getOperand(i);
571       const MCOperand &MCOp2 = MI.getOperand(i + 1);
572       if (MCOp1.isImm() && MCOp2.isReg() &&
573           (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
574         if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
575           return true;
576       }
577     }
578   }
579   return false;
580 }
581
582 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
583 /// target.
584 uint32_t ARMMCCodeEmitter::
585 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
586                        SmallVectorImpl<MCFixup> &Fixups) const {
587   // FIXME: This really, really shouldn't use TargetMachine. We don't want
588   // coupling between MC and TM anywhere we can help it.
589   if (isThumb2())
590     return
591       ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
592   return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
593 }
594
595 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
596 /// target.
597 uint32_t ARMMCCodeEmitter::
598 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
599                           SmallVectorImpl<MCFixup> &Fixups) const {
600   const MCOperand MO = MI.getOperand(OpIdx);
601   if (MO.isExpr()) {
602     if (HasConditionalBranch(MI))
603       return ::getBranchTargetOpValue(MI, OpIdx,
604                                       ARM::fixup_arm_condbranch, Fixups);
605     return ::getBranchTargetOpValue(MI, OpIdx,
606                                     ARM::fixup_arm_uncondbranch, Fixups);
607   }
608
609   return MO.getImm() >> 2;
610 }
611
612 uint32_t ARMMCCodeEmitter::
613 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
614                           SmallVectorImpl<MCFixup> &Fixups) const {
615   const MCOperand MO = MI.getOperand(OpIdx);
616   if (MO.isExpr()) {
617     if (HasConditionalBranch(MI))
618       return ::getBranchTargetOpValue(MI, OpIdx, 
619                                       ARM::fixup_arm_condbl, Fixups);
620     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups);
621   }
622
623   return MO.getImm() >> 2;
624 }
625
626 uint32_t ARMMCCodeEmitter::
627 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
628                           SmallVectorImpl<MCFixup> &Fixups) const {
629   const MCOperand MO = MI.getOperand(OpIdx);
630   if (MO.isExpr())
631     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups);
632
633   return MO.getImm() >> 1;
634 }
635
636 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
637 /// immediate branch target.
638 uint32_t ARMMCCodeEmitter::
639 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
640                        SmallVectorImpl<MCFixup> &Fixups) const {
641   unsigned Val = 0;
642   const MCOperand MO = MI.getOperand(OpIdx);
643     
644   if(MO.isExpr())
645     Val = ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
646   else 
647     Val = MO.getImm() >> 1;
648
649   bool I  = (Val & 0x800000);
650   bool J1 = (Val & 0x400000);
651   bool J2 = (Val & 0x200000);
652   if (I ^ J1)
653     Val &= ~0x400000;
654   else
655     Val |= 0x400000;
656
657   if (I ^ J2)
658     Val &= ~0x200000;
659   else
660     Val |= 0x200000;
661
662   return Val;
663 }
664
665 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
666 /// ADR label target.
667 uint32_t ARMMCCodeEmitter::
668 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
669                    SmallVectorImpl<MCFixup> &Fixups) const {
670   const MCOperand MO = MI.getOperand(OpIdx);
671   if (MO.isExpr())
672     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
673                                     Fixups);
674   int64_t offset = MO.getImm();
675   uint32_t Val = 0x2000;
676
677   int SoImmVal;
678   if (offset == INT32_MIN) {
679     Val = 0x1000;
680     SoImmVal = 0;
681   } else if (offset < 0) {
682     Val = 0x1000;
683     offset *= -1;
684     SoImmVal = ARM_AM::getSOImmVal(offset);
685     if(SoImmVal == -1) {
686       Val = 0x2000;
687       offset *= -1;
688       SoImmVal = ARM_AM::getSOImmVal(offset);
689     }
690   } else {
691     SoImmVal = ARM_AM::getSOImmVal(offset);
692     if(SoImmVal == -1) {
693       Val = 0x1000;
694       offset *= -1;
695       SoImmVal = ARM_AM::getSOImmVal(offset);
696     }
697   }
698
699   assert(SoImmVal != -1 && "Not a valid so_imm value!");
700
701   Val |= SoImmVal;
702   return Val;
703 }
704
705 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
706 /// target.
707 uint32_t ARMMCCodeEmitter::
708 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
709                    SmallVectorImpl<MCFixup> &Fixups) const {
710   const MCOperand MO = MI.getOperand(OpIdx);
711   if (MO.isExpr())
712     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
713                                     Fixups);
714   int32_t Val = MO.getImm();
715   if (Val == INT32_MIN)
716     Val = 0x1000;
717   else if (Val < 0) {
718     Val *= -1;
719     Val |= 0x1000;
720   }
721   return Val;
722 }
723
724 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
725 /// target.
726 uint32_t ARMMCCodeEmitter::
727 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
728                    SmallVectorImpl<MCFixup> &Fixups) const {
729   const MCOperand MO = MI.getOperand(OpIdx);
730   if (MO.isExpr())
731     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
732                                     Fixups);
733   return MO.getImm();
734 }
735
736 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
737 /// operand.
738 uint32_t ARMMCCodeEmitter::
739 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
740                               SmallVectorImpl<MCFixup> &) const {
741   // [Rn, Rm]
742   //   {5-3} = Rm
743   //   {2-0} = Rn
744   const MCOperand &MO1 = MI.getOperand(OpIdx);
745   const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
746   unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
747   unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
748   return (Rm << 3) | Rn;
749 }
750
751 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
752 uint32_t ARMMCCodeEmitter::
753 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
754                         SmallVectorImpl<MCFixup> &Fixups) const {
755   // {17-13} = reg
756   // {12}    = (U)nsigned (add == '1', sub == '0')
757   // {11-0}  = imm12
758   unsigned Reg, Imm12;
759   bool isAdd = true;
760   // If The first operand isn't a register, we have a label reference.
761   const MCOperand &MO = MI.getOperand(OpIdx);
762   if (!MO.isReg()) {
763     Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC);   // Rn is PC.
764     Imm12 = 0;
765
766     if (MO.isExpr()) {
767       const MCExpr *Expr = MO.getExpr();
768       isAdd = false ; // 'U' bit is set as part of the fixup.
769
770       MCFixupKind Kind;
771       if (isThumb2())
772         Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
773       else
774         Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
775       Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
776
777       ++MCNumCPRelocations;
778     } else {
779       Reg = ARM::PC;
780       int32_t Offset = MO.getImm();
781       // FIXME: Handle #-0.
782       if (Offset < 0) {
783         Offset *= -1;
784         isAdd = false;
785       }
786       Imm12 = Offset;
787     }
788   } else
789     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
790
791   uint32_t Binary = Imm12 & 0xfff;
792   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
793   if (isAdd)
794     Binary |= (1 << 12);
795   Binary |= (Reg << 13);
796   return Binary;
797 }
798
799 /// getT2Imm8s4OpValue - Return encoding info for
800 /// '+/- imm8<<2' operand.
801 uint32_t ARMMCCodeEmitter::
802 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
803                    SmallVectorImpl<MCFixup> &Fixups) const {
804   // FIXME: The immediate operand should have already been encoded like this
805   // before ever getting here. The encoder method should just need to combine
806   // the MI operands for the register and the offset into a single
807   // representation for the complex operand in the .td file. This isn't just
808   // style, unfortunately. As-is, we can't represent the distinct encoding
809   // for #-0.
810
811   // {8}    = (U)nsigned (add == '1', sub == '0')
812   // {7-0}  = imm8
813   int32_t Imm8 = MI.getOperand(OpIdx).getImm();
814   bool isAdd = Imm8 >= 0;
815
816   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
817   if (Imm8 < 0)
818     Imm8 = -(uint32_t)Imm8;
819
820   // Scaled by 4.
821   Imm8 /= 4;
822
823   uint32_t Binary = Imm8 & 0xff;
824   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
825   if (isAdd)
826     Binary |= (1 << 8);
827   return Binary;
828 }
829
830 /// getT2AddrModeImm8s4OpValue - Return encoding info for
831 /// 'reg +/- imm8<<2' operand.
832 uint32_t ARMMCCodeEmitter::
833 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
834                         SmallVectorImpl<MCFixup> &Fixups) const {
835   // {12-9} = reg
836   // {8}    = (U)nsigned (add == '1', sub == '0')
837   // {7-0}  = imm8
838   unsigned Reg, Imm8;
839   bool isAdd = true;
840   // If The first operand isn't a register, we have a label reference.
841   const MCOperand &MO = MI.getOperand(OpIdx);
842   if (!MO.isReg()) {
843     Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC);   // Rn is PC.
844     Imm8 = 0;
845     isAdd = false ; // 'U' bit is set as part of the fixup.
846
847     assert(MO.isExpr() && "Unexpected machine operand type!");
848     const MCExpr *Expr = MO.getExpr();
849     MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
850     Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
851
852     ++MCNumCPRelocations;
853   } else
854     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
855
856   // FIXME: The immediate operand should have already been encoded like this
857   // before ever getting here. The encoder method should just need to combine
858   // the MI operands for the register and the offset into a single
859   // representation for the complex operand in the .td file. This isn't just
860   // style, unfortunately. As-is, we can't represent the distinct encoding
861   // for #-0.
862   uint32_t Binary = (Imm8 >> 2) & 0xff;
863   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
864   if (isAdd)
865     Binary |= (1 << 8);
866   Binary |= (Reg << 9);
867   return Binary;
868 }
869
870 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
871 /// 'reg + imm8<<2' operand.
872 uint32_t ARMMCCodeEmitter::
873 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
874                         SmallVectorImpl<MCFixup> &Fixups) const {
875   // {11-8} = reg
876   // {7-0}  = imm8
877   const MCOperand &MO = MI.getOperand(OpIdx);
878   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
879   unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
880   unsigned Imm8 = MO1.getImm();
881   return (Reg << 8) | Imm8;
882 }
883
884 // FIXME: This routine assumes that a binary
885 // expression will always result in a PCRel expression
886 // In reality, its only true if one or more subexpressions
887 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
888 // but this is good enough for now.
889 static bool EvaluateAsPCRel(const MCExpr *Expr) {
890   switch (Expr->getKind()) {
891   default: llvm_unreachable("Unexpected expression type");
892   case MCExpr::SymbolRef: return false;
893   case MCExpr::Binary: return true;
894   }
895 }
896
897 uint32_t
898 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
899                                       SmallVectorImpl<MCFixup> &Fixups) const {
900   // {20-16} = imm{15-12}
901   // {11-0}  = imm{11-0}
902   const MCOperand &MO = MI.getOperand(OpIdx);
903   if (MO.isImm())
904     // Hi / lo 16 bits already extracted during earlier passes.
905     return static_cast<unsigned>(MO.getImm());
906
907   // Handle :upper16: and :lower16: assembly prefixes.
908   const MCExpr *E = MO.getExpr();
909   MCFixupKind Kind;
910   if (E->getKind() == MCExpr::Target) {
911     const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
912     E = ARM16Expr->getSubExpr();
913
914     switch (ARM16Expr->getKind()) {
915     default: llvm_unreachable("Unsupported ARMFixup");
916     case ARMMCExpr::VK_ARM_HI16:
917       if (!isTargetDarwin() && EvaluateAsPCRel(E))
918         Kind = MCFixupKind(isThumb2()
919                            ? ARM::fixup_t2_movt_hi16_pcrel
920                            : ARM::fixup_arm_movt_hi16_pcrel);
921       else
922         Kind = MCFixupKind(isThumb2()
923                            ? ARM::fixup_t2_movt_hi16
924                            : ARM::fixup_arm_movt_hi16);
925       break;
926     case ARMMCExpr::VK_ARM_LO16:
927       if (!isTargetDarwin() && EvaluateAsPCRel(E))
928         Kind = MCFixupKind(isThumb2()
929                            ? ARM::fixup_t2_movw_lo16_pcrel
930                            : ARM::fixup_arm_movw_lo16_pcrel);
931       else
932         Kind = MCFixupKind(isThumb2()
933                            ? ARM::fixup_t2_movw_lo16
934                            : ARM::fixup_arm_movw_lo16);
935       break;
936     }
937     Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
938     return 0;
939   }
940   // If the expression doesn't have :upper16: or :lower16: on it,
941   // it's just a plain immediate expression, and those evaluate to
942   // the lower 16 bits of the expression regardless of whether
943   // we have a movt or a movw.
944   if (!isTargetDarwin() && EvaluateAsPCRel(E))
945     Kind = MCFixupKind(isThumb2()
946                        ? ARM::fixup_t2_movw_lo16_pcrel
947                        : ARM::fixup_arm_movw_lo16_pcrel);
948   else
949     Kind = MCFixupKind(isThumb2()
950                        ? ARM::fixup_t2_movw_lo16
951                        : ARM::fixup_arm_movw_lo16);
952   Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
953   return 0;
954 }
955
956 uint32_t ARMMCCodeEmitter::
957 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
958                     SmallVectorImpl<MCFixup> &Fixups) const {
959   const MCOperand &MO = MI.getOperand(OpIdx);
960   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
961   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
962   unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
963   unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
964   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
965   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
966   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
967   unsigned SBits = getShiftOp(ShOp);
968
969   // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
970   // amount. However, it would be an easy mistake to make so check here.
971   assert((ShImm & ~0x1f) == 0 && "Out of range shift amount");
972
973   // {16-13} = Rn
974   // {12}    = isAdd
975   // {11-0}  = shifter
976   //  {3-0}  = Rm
977   //  {4}    = 0
978   //  {6-5}  = type
979   //  {11-7} = imm
980   uint32_t Binary = Rm;
981   Binary |= Rn << 13;
982   Binary |= SBits << 5;
983   Binary |= ShImm << 7;
984   if (isAdd)
985     Binary |= 1 << 12;
986   return Binary;
987 }
988
989 uint32_t ARMMCCodeEmitter::
990 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
991                     SmallVectorImpl<MCFixup> &Fixups) const {
992   // {17-14}  Rn
993   // {13}     1 == imm12, 0 == Rm
994   // {12}     isAdd
995   // {11-0}   imm12/Rm
996   const MCOperand &MO = MI.getOperand(OpIdx);
997   unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
998   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
999   Binary |= Rn << 14;
1000   return Binary;
1001 }
1002
1003 uint32_t ARMMCCodeEmitter::
1004 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1005                           SmallVectorImpl<MCFixup> &Fixups) const {
1006   // {13}     1 == imm12, 0 == Rm
1007   // {12}     isAdd
1008   // {11-0}   imm12/Rm
1009   const MCOperand &MO = MI.getOperand(OpIdx);
1010   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1011   unsigned Imm = MO1.getImm();
1012   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
1013   bool isReg = MO.getReg() != 0;
1014   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
1015   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1016   if (isReg) {
1017     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
1018     Binary <<= 7;                    // Shift amount is bits [11:7]
1019     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
1020     Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0]
1021   }
1022   return Binary | (isAdd << 12) | (isReg << 13);
1023 }
1024
1025 uint32_t ARMMCCodeEmitter::
1026 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
1027                      SmallVectorImpl<MCFixup> &Fixups) const {
1028   // {4}      isAdd
1029   // {3-0}    Rm
1030   const MCOperand &MO = MI.getOperand(OpIdx);
1031   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1032   bool isAdd = MO1.getImm() != 0;
1033   return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4);
1034 }
1035
1036 uint32_t ARMMCCodeEmitter::
1037 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1038                           SmallVectorImpl<MCFixup> &Fixups) const {
1039   // {9}      1 == imm8, 0 == Rm
1040   // {8}      isAdd
1041   // {7-4}    imm7_4/zero
1042   // {3-0}    imm3_0/Rm
1043   const MCOperand &MO = MI.getOperand(OpIdx);
1044   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1045   unsigned Imm = MO1.getImm();
1046   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1047   bool isImm = MO.getReg() == 0;
1048   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1049   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1050   if (!isImm)
1051     Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1052   return Imm8 | (isAdd << 8) | (isImm << 9);
1053 }
1054
1055 uint32_t ARMMCCodeEmitter::
1056 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1057                     SmallVectorImpl<MCFixup> &Fixups) const {
1058   // {13}     1 == imm8, 0 == Rm
1059   // {12-9}   Rn
1060   // {8}      isAdd
1061   // {7-4}    imm7_4/zero
1062   // {3-0}    imm3_0/Rm
1063   const MCOperand &MO = MI.getOperand(OpIdx);
1064   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1065   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1066
1067   // If The first operand isn't a register, we have a label reference.
1068   if (!MO.isReg()) {
1069     unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC);   // Rn is PC.
1070
1071     assert(MO.isExpr() && "Unexpected machine operand type!");
1072     const MCExpr *Expr = MO.getExpr();
1073     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1074     Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1075
1076     ++MCNumCPRelocations;
1077     return (Rn << 9) | (1 << 13);
1078   }
1079   unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1080   unsigned Imm = MO2.getImm();
1081   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1082   bool isImm = MO1.getReg() == 0;
1083   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1084   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1085   if (!isImm)
1086     Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1087   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1088 }
1089
1090 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1091 uint32_t ARMMCCodeEmitter::
1092 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1093                           SmallVectorImpl<MCFixup> &Fixups) const {
1094   // [SP, #imm]
1095   //   {7-0} = imm8
1096   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1097   assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1098          "Unexpected base register!");
1099
1100   // The immediate is already shifted for the implicit zeroes, so no change
1101   // here.
1102   return MO1.getImm() & 0xff;
1103 }
1104
1105 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1106 uint32_t ARMMCCodeEmitter::
1107 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1108                      SmallVectorImpl<MCFixup> &Fixups) const {
1109   // [Rn, #imm]
1110   //   {7-3} = imm5
1111   //   {2-0} = Rn
1112   const MCOperand &MO = MI.getOperand(OpIdx);
1113   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1114   unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1115   unsigned Imm5 = MO1.getImm();
1116   return ((Imm5 & 0x1f) << 3) | Rn;
1117 }
1118
1119 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1120 uint32_t ARMMCCodeEmitter::
1121 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1122                      SmallVectorImpl<MCFixup> &Fixups) const {
1123   const MCOperand MO = MI.getOperand(OpIdx);
1124   if (MO.isExpr())
1125     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
1126   return (MO.getImm() >> 2);
1127 }
1128
1129 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1130 uint32_t ARMMCCodeEmitter::
1131 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1132                     SmallVectorImpl<MCFixup> &Fixups) const {
1133   // {12-9} = reg
1134   // {8}    = (U)nsigned (add == '1', sub == '0')
1135   // {7-0}  = imm8
1136   unsigned Reg, Imm8;
1137   bool isAdd;
1138   // If The first operand isn't a register, we have a label reference.
1139   const MCOperand &MO = MI.getOperand(OpIdx);
1140   if (!MO.isReg()) {
1141     Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC);   // Rn is PC.
1142     Imm8 = 0;
1143     isAdd = false; // 'U' bit is handled as part of the fixup.
1144
1145     assert(MO.isExpr() && "Unexpected machine operand type!");
1146     const MCExpr *Expr = MO.getExpr();
1147     MCFixupKind Kind;
1148     if (isThumb2())
1149       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1150     else
1151       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1152     Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1153
1154     ++MCNumCPRelocations;
1155   } else {
1156     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
1157     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1158   }
1159
1160   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1161   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1162   if (isAdd)
1163     Binary |= (1 << 8);
1164   Binary |= (Reg << 9);
1165   return Binary;
1166 }
1167
1168 unsigned ARMMCCodeEmitter::
1169 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1170                 SmallVectorImpl<MCFixup> &Fixups) const {
1171   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1172   // shifted. The second is Rs, the amount to shift by, and the third specifies
1173   // the type of the shift.
1174   //
1175   // {3-0} = Rm.
1176   // {4}   = 1
1177   // {6-5} = type
1178   // {11-8} = Rs
1179   // {7}    = 0
1180
1181   const MCOperand &MO  = MI.getOperand(OpIdx);
1182   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1183   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1184   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1185
1186   // Encode Rm.
1187   unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1188
1189   // Encode the shift opcode.
1190   unsigned SBits = 0;
1191   unsigned Rs = MO1.getReg();
1192   if (Rs) {
1193     // Set shift operand (bit[7:4]).
1194     // LSL - 0001
1195     // LSR - 0011
1196     // ASR - 0101
1197     // ROR - 0111
1198     switch (SOpc) {
1199     default: llvm_unreachable("Unknown shift opc!");
1200     case ARM_AM::lsl: SBits = 0x1; break;
1201     case ARM_AM::lsr: SBits = 0x3; break;
1202     case ARM_AM::asr: SBits = 0x5; break;
1203     case ARM_AM::ror: SBits = 0x7; break;
1204     }
1205   }
1206
1207   Binary |= SBits << 4;
1208
1209   // Encode the shift operation Rs.
1210   // Encode Rs bit[11:8].
1211   assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1212   return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift);
1213 }
1214
1215 unsigned ARMMCCodeEmitter::
1216 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1217                 SmallVectorImpl<MCFixup> &Fixups) const {
1218   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1219   // shifted. The second is the amount to shift by.
1220   //
1221   // {3-0} = Rm.
1222   // {4}   = 0
1223   // {6-5} = type
1224   // {11-7} = imm
1225
1226   const MCOperand &MO  = MI.getOperand(OpIdx);
1227   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1228   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1229
1230   // Encode Rm.
1231   unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1232
1233   // Encode the shift opcode.
1234   unsigned SBits = 0;
1235
1236   // Set shift operand (bit[6:4]).
1237   // LSL - 000
1238   // LSR - 010
1239   // ASR - 100
1240   // ROR - 110
1241   // RRX - 110 and bit[11:8] clear.
1242   switch (SOpc) {
1243   default: llvm_unreachable("Unknown shift opc!");
1244   case ARM_AM::lsl: SBits = 0x0; break;
1245   case ARM_AM::lsr: SBits = 0x2; break;
1246   case ARM_AM::asr: SBits = 0x4; break;
1247   case ARM_AM::ror: SBits = 0x6; break;
1248   case ARM_AM::rrx:
1249     Binary |= 0x60;
1250     return Binary;
1251   }
1252
1253   // Encode shift_imm bit[11:7].
1254   Binary |= SBits << 4;
1255   unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1256   assert(Offset < 32 && "Offset must be in range 0-31!");
1257   return Binary | (Offset << 7);
1258 }
1259
1260
1261 unsigned ARMMCCodeEmitter::
1262 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1263                 SmallVectorImpl<MCFixup> &Fixups) const {
1264   const MCOperand &MO1 = MI.getOperand(OpNum);
1265   const MCOperand &MO2 = MI.getOperand(OpNum+1);
1266   const MCOperand &MO3 = MI.getOperand(OpNum+2);
1267
1268   // Encoded as [Rn, Rm, imm].
1269   // FIXME: Needs fixup support.
1270   unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1271   Value <<= 4;
1272   Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
1273   Value <<= 2;
1274   Value |= MO3.getImm();
1275
1276   return Value;
1277 }
1278
1279 unsigned ARMMCCodeEmitter::
1280 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1281                          SmallVectorImpl<MCFixup> &Fixups) const {
1282   const MCOperand &MO1 = MI.getOperand(OpNum);
1283   const MCOperand &MO2 = MI.getOperand(OpNum+1);
1284
1285   // FIXME: Needs fixup support.
1286   unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1287
1288   // Even though the immediate is 8 bits long, we need 9 bits in order
1289   // to represent the (inverse of the) sign bit.
1290   Value <<= 9;
1291   int32_t tmp = (int32_t)MO2.getImm();
1292   if (tmp < 0)
1293     tmp = abs(tmp);
1294   else
1295     Value |= 256; // Set the ADD bit
1296   Value |= tmp & 255;
1297   return Value;
1298 }
1299
1300 unsigned ARMMCCodeEmitter::
1301 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1302                          SmallVectorImpl<MCFixup> &Fixups) const {
1303   const MCOperand &MO1 = MI.getOperand(OpNum);
1304
1305   // FIXME: Needs fixup support.
1306   unsigned Value = 0;
1307   int32_t tmp = (int32_t)MO1.getImm();
1308   if (tmp < 0)
1309     tmp = abs(tmp);
1310   else
1311     Value |= 256; // Set the ADD bit
1312   Value |= tmp & 255;
1313   return Value;
1314 }
1315
1316 unsigned ARMMCCodeEmitter::
1317 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1318                          SmallVectorImpl<MCFixup> &Fixups) const {
1319   const MCOperand &MO1 = MI.getOperand(OpNum);
1320
1321   // FIXME: Needs fixup support.
1322   unsigned Value = 0;
1323   int32_t tmp = (int32_t)MO1.getImm();
1324   if (tmp < 0)
1325     tmp = abs(tmp);
1326   else
1327     Value |= 4096; // Set the ADD bit
1328   Value |= tmp & 4095;
1329   return Value;
1330 }
1331
1332 unsigned ARMMCCodeEmitter::
1333 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1334                 SmallVectorImpl<MCFixup> &Fixups) const {
1335   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1336   // shifted. The second is the amount to shift by.
1337   //
1338   // {3-0} = Rm.
1339   // {4}   = 0
1340   // {6-5} = type
1341   // {11-7} = imm
1342
1343   const MCOperand &MO  = MI.getOperand(OpIdx);
1344   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1345   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1346
1347   // Encode Rm.
1348   unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1349
1350   // Encode the shift opcode.
1351   unsigned SBits = 0;
1352   // Set shift operand (bit[6:4]).
1353   // LSL - 000
1354   // LSR - 010
1355   // ASR - 100
1356   // ROR - 110
1357   switch (SOpc) {
1358   default: llvm_unreachable("Unknown shift opc!");
1359   case ARM_AM::lsl: SBits = 0x0; break;
1360   case ARM_AM::lsr: SBits = 0x2; break;
1361   case ARM_AM::asr: SBits = 0x4; break;
1362   case ARM_AM::rrx: // FALLTHROUGH
1363   case ARM_AM::ror: SBits = 0x6; break;
1364   }
1365
1366   Binary |= SBits << 4;
1367   if (SOpc == ARM_AM::rrx)
1368     return Binary;
1369
1370   // Encode shift_imm bit[11:7].
1371   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1372 }
1373
1374 unsigned ARMMCCodeEmitter::
1375 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1376                                SmallVectorImpl<MCFixup> &Fixups) const {
1377   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1378   // msb of the mask.
1379   const MCOperand &MO = MI.getOperand(Op);
1380   uint32_t v = ~MO.getImm();
1381   uint32_t lsb = countTrailingZeros(v);
1382   uint32_t msb = (32 - countLeadingZeros (v)) - 1;
1383   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1384   return lsb | (msb << 5);
1385 }
1386
1387 unsigned ARMMCCodeEmitter::
1388 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1389                        SmallVectorImpl<MCFixup> &Fixups) const {
1390   // VLDM/VSTM:
1391   //   {12-8} = Vd
1392   //   {7-0}  = Number of registers
1393   //
1394   // LDM/STM:
1395   //   {15-0}  = Bitfield of GPRs.
1396   unsigned Reg = MI.getOperand(Op).getReg();
1397   bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1398   bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1399
1400   unsigned Binary = 0;
1401
1402   if (SPRRegs || DPRRegs) {
1403     // VLDM/VSTM
1404     unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
1405     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1406     Binary |= (RegNo & 0x1f) << 8;
1407     if (SPRRegs)
1408       Binary |= NumRegs;
1409     else
1410       Binary |= NumRegs * 2;
1411   } else {
1412     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1413       unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(MI.getOperand(I).getReg());
1414       Binary |= 1 << RegNo;
1415     }
1416   }
1417
1418   return Binary;
1419 }
1420
1421 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1422 /// with the alignment operand.
1423 unsigned ARMMCCodeEmitter::
1424 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1425                            SmallVectorImpl<MCFixup> &Fixups) const {
1426   const MCOperand &Reg = MI.getOperand(Op);
1427   const MCOperand &Imm = MI.getOperand(Op + 1);
1428
1429   unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1430   unsigned Align = 0;
1431
1432   switch (Imm.getImm()) {
1433   default: break;
1434   case 2:
1435   case 4:
1436   case 8:  Align = 0x01; break;
1437   case 16: Align = 0x02; break;
1438   case 32: Align = 0x03; break;
1439   }
1440
1441   return RegNo | (Align << 4);
1442 }
1443
1444 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1445 /// along  with the alignment operand for use in VST1 and VLD1 with size 32.
1446 unsigned ARMMCCodeEmitter::
1447 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1448                                     SmallVectorImpl<MCFixup> &Fixups) const {
1449   const MCOperand &Reg = MI.getOperand(Op);
1450   const MCOperand &Imm = MI.getOperand(Op + 1);
1451
1452   unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1453   unsigned Align = 0;
1454
1455   switch (Imm.getImm()) {
1456   default: break;
1457   case 8:
1458   case 16:
1459   case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1460   case 2: Align = 0x00; break;
1461   case 4: Align = 0x03; break;
1462   }
1463
1464   return RegNo | (Align << 4);
1465 }
1466
1467
1468 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1469 /// alignment operand for use in VLD-dup instructions.  This is the same as
1470 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1471 /// different for VLD4-dup.
1472 unsigned ARMMCCodeEmitter::
1473 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1474                               SmallVectorImpl<MCFixup> &Fixups) const {
1475   const MCOperand &Reg = MI.getOperand(Op);
1476   const MCOperand &Imm = MI.getOperand(Op + 1);
1477
1478   unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1479   unsigned Align = 0;
1480
1481   switch (Imm.getImm()) {
1482   default: break;
1483   case 2:
1484   case 4:
1485   case 8:  Align = 0x01; break;
1486   case 16: Align = 0x03; break;
1487   }
1488
1489   return RegNo | (Align << 4);
1490 }
1491
1492 unsigned ARMMCCodeEmitter::
1493 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1494                           SmallVectorImpl<MCFixup> &Fixups) const {
1495   const MCOperand &MO = MI.getOperand(Op);
1496   if (MO.getReg() == 0) return 0x0D;
1497   return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1498 }
1499
1500 unsigned ARMMCCodeEmitter::
1501 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1502                   SmallVectorImpl<MCFixup> &Fixups) const {
1503   return 8 - MI.getOperand(Op).getImm();
1504 }
1505
1506 unsigned ARMMCCodeEmitter::
1507 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1508                    SmallVectorImpl<MCFixup> &Fixups) const {
1509   return 16 - MI.getOperand(Op).getImm();
1510 }
1511
1512 unsigned ARMMCCodeEmitter::
1513 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1514                    SmallVectorImpl<MCFixup> &Fixups) const {
1515   return 32 - MI.getOperand(Op).getImm();
1516 }
1517
1518 unsigned ARMMCCodeEmitter::
1519 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1520                    SmallVectorImpl<MCFixup> &Fixups) const {
1521   return 64 - MI.getOperand(Op).getImm();
1522 }
1523
1524 void ARMMCCodeEmitter::
1525 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1526                   SmallVectorImpl<MCFixup> &Fixups) const {
1527   // Pseudo instructions don't get encoded.
1528   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1529   uint64_t TSFlags = Desc.TSFlags;
1530   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1531     return;
1532
1533   int Size;
1534   if (Desc.getSize() == 2 || Desc.getSize() == 4)
1535     Size = Desc.getSize();
1536   else
1537     llvm_unreachable("Unexpected instruction size!");
1538
1539   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1540   // Thumb 32-bit wide instructions need to emit the high order halfword
1541   // first.
1542   if (isThumb() && Size == 4) {
1543     EmitConstant(Binary >> 16, 2, OS);
1544     EmitConstant(Binary & 0xffff, 2, OS);
1545   } else
1546     EmitConstant(Binary, Size, OS);
1547   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1548 }
1549
1550 #include "ARMGenMCCodeEmitter.inc"