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