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