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