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