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