Model :upper16: and :lower16: as ARM specific MCTargetExpr. This is a step
[oota-llvm.git] / lib / Target / ARM / 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 "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMCExpr.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/raw_ostream.h"
25 using namespace llvm;
26
27 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
28 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
29
30 namespace {
31 class ARMMCCodeEmitter : public MCCodeEmitter {
32   ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33   void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
34   const TargetMachine &TM;
35   const TargetInstrInfo &TII;
36   MCContext &Ctx;
37
38 public:
39   ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
40     : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
41   }
42
43   ~ARMMCCodeEmitter() {}
44
45   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
46
47   // getBinaryCodeForInstr - TableGen'erated function for getting the
48   // binary encoding for an instruction.
49   unsigned getBinaryCodeForInstr(const MCInst &MI,
50                                  SmallVectorImpl<MCFixup> &Fixups) const;
51
52   /// getMachineOpValue - Return binary encoding of operand. If the machine
53   /// operand requires relocation, record the relocation and return zero.
54   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
55                              SmallVectorImpl<MCFixup> &Fixups) const;
56
57   /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
58   /// the specified operand. This is used for operands with :lower16: and 
59   /// :upper16: prefixes.
60   uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
61                                SmallVectorImpl<MCFixup> &Fixups) const;
62
63   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
64                               unsigned &Reg, unsigned &Imm,
65                               SmallVectorImpl<MCFixup> &Fixups) const;
66
67   /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
68   /// BL branch target.
69   uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
70                                    SmallVectorImpl<MCFixup> &Fixups) const;
71
72   /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
73   /// BLX branch target.
74   uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
75                                     SmallVectorImpl<MCFixup> &Fixups) const;
76
77   /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
78   uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
79                                    SmallVectorImpl<MCFixup> &Fixups) const;
80
81   /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
82   uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
83                                     SmallVectorImpl<MCFixup> &Fixups) const;
84
85   /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
86   uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
87                                    SmallVectorImpl<MCFixup> &Fixups) const;
88
89   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
90   /// branch target.
91   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
92                                   SmallVectorImpl<MCFixup> &Fixups) const;
93
94   /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
95   /// immediate Thumb2 direct branch target.
96   uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
97                                   SmallVectorImpl<MCFixup> &Fixups) const;
98   
99
100   /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
101   /// ADR label target.
102   uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
103                               SmallVectorImpl<MCFixup> &Fixups) const;
104   uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
105                               SmallVectorImpl<MCFixup> &Fixups) const;
106   uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
107                               SmallVectorImpl<MCFixup> &Fixups) const;
108   
109
110   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
111   /// operand.
112   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
113                                    SmallVectorImpl<MCFixup> &Fixups) const;
114
115   /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
116   uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
117                                          SmallVectorImpl<MCFixup> &Fixups)const;
118
119   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
120   /// operand.
121   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
122                                    SmallVectorImpl<MCFixup> &Fixups) const;
123
124
125   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
126   /// operand as needed by load/store instructions.
127   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
128                                SmallVectorImpl<MCFixup> &Fixups) const;
129
130   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
131   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
132                                SmallVectorImpl<MCFixup> &Fixups) const {
133     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
134     switch (Mode) {
135     default: assert(0 && "Unknown addressing sub-mode!");
136     case ARM_AM::da: return 0;
137     case ARM_AM::ia: return 1;
138     case ARM_AM::db: return 2;
139     case ARM_AM::ib: return 3;
140     }
141   }
142   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
143   ///
144   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
145     switch (ShOpc) {
146     default: llvm_unreachable("Unknown shift opc!");
147     case ARM_AM::no_shift:
148     case ARM_AM::lsl: return 0;
149     case ARM_AM::lsr: return 1;
150     case ARM_AM::asr: return 2;
151     case ARM_AM::ror:
152     case ARM_AM::rrx: return 3;
153     }
154     return 0;
155   }
156
157   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
158   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
159                                SmallVectorImpl<MCFixup> &Fixups) const;
160
161   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
162   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
163                                      SmallVectorImpl<MCFixup> &Fixups) const;
164
165   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
166   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
167                                      SmallVectorImpl<MCFixup> &Fixups) const;
168
169   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
170   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
171                                SmallVectorImpl<MCFixup> &Fixups) const;
172
173   /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
174   /// operand.
175   uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
176                                      SmallVectorImpl<MCFixup> &Fixups) const;
177
178   /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
179   uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
180                                 SmallVectorImpl<MCFixup> &Fixups) const;
181
182   /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
183   uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
184                                 SmallVectorImpl<MCFixup> &Fixups) const;
185
186   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
187   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
188                                SmallVectorImpl<MCFixup> &Fixups) const;
189
190   /// getCCOutOpValue - Return encoding of the 's' bit.
191   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
192                            SmallVectorImpl<MCFixup> &Fixups) const {
193     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
194     // '1' respectively.
195     return MI.getOperand(Op).getReg() == ARM::CPSR;
196   }
197
198   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
199   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
200                            SmallVectorImpl<MCFixup> &Fixups) const {
201     unsigned SoImm = MI.getOperand(Op).getImm();
202     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
203     assert(SoImmVal != -1 && "Not a valid so_imm value!");
204
205     // Encode rotate_imm.
206     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
207       << ARMII::SoRotImmShift;
208
209     // Encode immed_8.
210     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
211     return Binary;
212   }
213
214   /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
215   unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
216                            SmallVectorImpl<MCFixup> &Fixups) const {
217     unsigned SoImm = MI.getOperand(Op).getImm();
218     unsigned Encoded =  ARM_AM::getT2SOImmVal(SoImm);
219     assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
220     return Encoded;
221   }
222
223   unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
224     SmallVectorImpl<MCFixup> &Fixups) const;
225   unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
226     SmallVectorImpl<MCFixup> &Fixups) const;
227   unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
228     SmallVectorImpl<MCFixup> &Fixups) const;
229   unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
230     SmallVectorImpl<MCFixup> &Fixups) const;
231
232   /// getSORegOpValue - Return an encoded so_reg shifted register value.
233   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
234                            SmallVectorImpl<MCFixup> &Fixups) const;
235   unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
236                              SmallVectorImpl<MCFixup> &Fixups) const;
237
238   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
239                             SmallVectorImpl<MCFixup> &Fixups) const {
240     switch (MI.getOperand(Op).getImm()) {
241     default: assert (0 && "Not a valid rot_imm value!");
242     case 0:  return 0;
243     case 8:  return 1;
244     case 16: return 2;
245     case 24: return 3;
246     }
247   }
248
249   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
250                                  SmallVectorImpl<MCFixup> &Fixups) const {
251     return MI.getOperand(Op).getImm() - 1;
252   }
253
254   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
255                                    SmallVectorImpl<MCFixup> &Fixups) const {
256     return 64 - MI.getOperand(Op).getImm();
257   }
258
259   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
260                                       SmallVectorImpl<MCFixup> &Fixups) const;
261
262   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
263                                   SmallVectorImpl<MCFixup> &Fixups) const;
264   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
265                                       SmallVectorImpl<MCFixup> &Fixups) const;
266   unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
267                                         SmallVectorImpl<MCFixup> &Fixups) const;
268   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
269                                      SmallVectorImpl<MCFixup> &Fixups) const;
270
271   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
272                                       unsigned EncodedValue) const;
273   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
274                                           unsigned EncodedValue) const;
275   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
276                                     unsigned EncodedValue) const;
277
278   unsigned VFPThumb2PostEncoder(const MCInst &MI,
279                                 unsigned EncodedValue) const;
280
281   void EmitByte(unsigned char C, raw_ostream &OS) const {
282     OS << (char)C;
283   }
284
285   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
286     // Output the constant in little endian byte order.
287     for (unsigned i = 0; i != Size; ++i) {
288       EmitByte(Val & 255, OS);
289       Val >>= 8;
290     }
291   }
292
293   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
294                          SmallVectorImpl<MCFixup> &Fixups) const;
295 };
296
297 } // end anonymous namespace
298
299 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
300                                             MCContext &Ctx) {
301   return new ARMMCCodeEmitter(TM, Ctx);
302 }
303
304 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
305 /// instructions, and rewrite them to their Thumb2 form if we are currently in
306 /// Thumb2 mode.
307 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
308                                                  unsigned EncodedValue) const {
309   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
310   if (Subtarget.isThumb2()) {
311     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
312     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
313     // set to 1111.
314     unsigned Bit24 = EncodedValue & 0x01000000;
315     unsigned Bit28 = Bit24 << 4;
316     EncodedValue &= 0xEFFFFFFF;
317     EncodedValue |= Bit28;
318     EncodedValue |= 0x0F000000;
319   }
320
321   return EncodedValue;
322 }
323
324 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
325 /// instructions, and rewrite them to their Thumb2 form if we are currently in
326 /// Thumb2 mode.
327 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
328                                                  unsigned EncodedValue) const {
329   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
330   if (Subtarget.isThumb2()) {
331     EncodedValue &= 0xF0FFFFFF;
332     EncodedValue |= 0x09000000;
333   }
334
335   return EncodedValue;
336 }
337
338 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
339 /// instructions, and rewrite them to their Thumb2 form if we are currently in
340 /// Thumb2 mode.
341 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
342                                                  unsigned EncodedValue) const {
343   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
344   if (Subtarget.isThumb2()) {
345     EncodedValue &= 0x00FFFFFF;
346     EncodedValue |= 0xEE000000;
347   }
348
349   return EncodedValue;
350 }
351
352 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
353 /// them to their Thumb2 form if we are currently in Thumb2 mode.
354 unsigned ARMMCCodeEmitter::
355 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
356   if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
357     EncodedValue &= 0x0FFFFFFF;
358     EncodedValue |= 0xE0000000;
359   }
360   return EncodedValue;
361 }
362
363 /// getMachineOpValue - Return binary encoding of operand. If the machine
364 /// operand requires relocation, record the relocation and return zero.
365 unsigned ARMMCCodeEmitter::
366 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
367                   SmallVectorImpl<MCFixup> &Fixups) const {
368   if (MO.isReg()) {
369     unsigned Reg = MO.getReg();
370     unsigned RegNo = getARMRegisterNumbering(Reg);
371
372     // Q registers are encoded as 2x their register number.
373     switch (Reg) {
374     default:
375       return RegNo;
376     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
377     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
378     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
379     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
380       return 2 * RegNo;
381     }
382   } else if (MO.isImm()) {
383     return static_cast<unsigned>(MO.getImm());
384   } else if (MO.isFPImm()) {
385     return static_cast<unsigned>(APFloat(MO.getFPImm())
386                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
387   }
388
389   llvm_unreachable("Unable to encode MCOperand!");
390   return 0;
391 }
392
393 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
394 bool ARMMCCodeEmitter::
395 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
396                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
397   const MCOperand &MO  = MI.getOperand(OpIdx);
398   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
399
400   Reg = getARMRegisterNumbering(MO.getReg());
401
402   int32_t SImm = MO1.getImm();
403   bool isAdd = true;
404
405   // Special value for #-0
406   if (SImm == INT32_MIN)
407     SImm = 0;
408
409   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
410   if (SImm < 0) {
411     SImm = -SImm;
412     isAdd = false;
413   }
414
415   Imm = SImm;
416   return isAdd;
417 }
418
419 /// getBranchTargetOpValue - Helper function to get the branch target operand,
420 /// which is either an immediate or requires a fixup.
421 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
422                                        unsigned FixupKind,
423                                        SmallVectorImpl<MCFixup> &Fixups) {
424   const MCOperand &MO = MI.getOperand(OpIdx);
425
426   // If the destination is an immediate, we have nothing to do.
427   if (MO.isImm()) return MO.getImm();
428   assert(MO.isExpr() && "Unexpected branch target type!");
429   const MCExpr *Expr = MO.getExpr();
430   MCFixupKind Kind = MCFixupKind(FixupKind);
431   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
432
433   // All of the information is in the fixup.
434   return 0;
435 }
436
437 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
438 uint32_t ARMMCCodeEmitter::
439 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
440                         SmallVectorImpl<MCFixup> &Fixups) const {
441   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
442 }
443
444 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
445 /// BLX branch target.
446 uint32_t ARMMCCodeEmitter::
447 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
448                          SmallVectorImpl<MCFixup> &Fixups) const {
449   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
450 }
451
452 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
453 uint32_t ARMMCCodeEmitter::
454 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
455                         SmallVectorImpl<MCFixup> &Fixups) const {
456   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
457 }
458
459 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
460 uint32_t ARMMCCodeEmitter::
461 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
462                          SmallVectorImpl<MCFixup> &Fixups) const {
463   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
464 }
465
466 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
467 uint32_t ARMMCCodeEmitter::
468 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
469                         SmallVectorImpl<MCFixup> &Fixups) const {
470   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
471 }
472
473 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
474 /// target.
475 uint32_t ARMMCCodeEmitter::
476 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
477                        SmallVectorImpl<MCFixup> &Fixups) const {
478   // FIXME: This really, really shouldn't use TargetMachine. We don't want
479   // coupling between MC and TM anywhere we can help it.
480   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
481   if (Subtarget.isThumb2())
482     return
483       ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
484   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
485 }
486
487 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
488 /// immediate branch target.
489 uint32_t ARMMCCodeEmitter::
490 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
491                        SmallVectorImpl<MCFixup> &Fixups) const {
492   unsigned Val =
493     ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
494   bool I  = (Val & 0x800000);
495   bool J1 = (Val & 0x400000);
496   bool J2 = (Val & 0x200000);
497   if (I ^ J1)
498     Val &= ~0x400000;
499   else
500     Val |= 0x400000;
501     
502   if (I ^ J2)
503     Val &= ~0x200000;
504   else
505     Val |= 0x200000;
506   
507   return Val;
508 }
509
510 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
511 /// target.
512 uint32_t ARMMCCodeEmitter::
513 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
514                    SmallVectorImpl<MCFixup> &Fixups) const {
515   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
516   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
517                                   Fixups);
518 }
519
520 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
521 /// target.
522 uint32_t ARMMCCodeEmitter::
523 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
524                    SmallVectorImpl<MCFixup> &Fixups) const {
525   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
526   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
527                                   Fixups);
528 }
529
530 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
531 /// target.
532 uint32_t ARMMCCodeEmitter::
533 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
534                    SmallVectorImpl<MCFixup> &Fixups) const {
535   assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
536   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
537                                   Fixups);
538 }
539
540 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
541 /// operand.
542 uint32_t ARMMCCodeEmitter::
543 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
544                               SmallVectorImpl<MCFixup> &) const {
545   // [Rn, Rm]
546   //   {5-3} = Rm
547   //   {2-0} = Rn
548   const MCOperand &MO1 = MI.getOperand(OpIdx);
549   const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
550   unsigned Rn = getARMRegisterNumbering(MO1.getReg());
551   unsigned Rm = getARMRegisterNumbering(MO2.getReg());
552   return (Rm << 3) | Rn;
553 }
554
555 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
556 uint32_t ARMMCCodeEmitter::
557 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
558                         SmallVectorImpl<MCFixup> &Fixups) const {
559   // {17-13} = reg
560   // {12}    = (U)nsigned (add == '1', sub == '0')
561   // {11-0}  = imm12
562   unsigned Reg, Imm12;
563   bool isAdd = true;
564   // If The first operand isn't a register, we have a label reference.
565   const MCOperand &MO = MI.getOperand(OpIdx);
566   const MCOperand &MO2 = MI.getOperand(OpIdx+1);
567   if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
568     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
569     Imm12 = 0;
570     isAdd = false ; // 'U' bit is set as part of the fixup.
571
572     const MCExpr *Expr = 0;
573     if (!MO.isReg())
574       Expr = MO.getExpr();
575     else
576       Expr = MO2.getExpr();
577
578     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
579     MCFixupKind Kind;
580     if (Subtarget.isThumb2())
581       Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
582     else
583       Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
584     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
585
586     ++MCNumCPRelocations;
587   } else
588     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
589
590   uint32_t Binary = Imm12 & 0xfff;
591   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
592   if (isAdd)
593     Binary |= (1 << 12);
594   Binary |= (Reg << 13);
595   return Binary;
596 }
597
598 /// getT2AddrModeImm8s4OpValue - Return encoding info for
599 /// 'reg +/- imm8<<2' operand.
600 uint32_t ARMMCCodeEmitter::
601 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
602                         SmallVectorImpl<MCFixup> &Fixups) const {
603   // {12-9} = reg
604   // {8}    = (U)nsigned (add == '1', sub == '0')
605   // {7-0}  = imm8
606   unsigned Reg, Imm8;
607   bool isAdd = true;
608   // If The first operand isn't a register, we have a label reference.
609   const MCOperand &MO = MI.getOperand(OpIdx);
610   if (!MO.isReg()) {
611     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
612     Imm8 = 0;
613     isAdd = false ; // 'U' bit is set as part of the fixup.
614
615     assert(MO.isExpr() && "Unexpected machine operand type!");
616     const MCExpr *Expr = MO.getExpr();
617     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
618     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
619
620     ++MCNumCPRelocations;
621   } else
622     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
623
624   uint32_t Binary = (Imm8 >> 2) & 0xff;
625   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
626   if (isAdd)
627     Binary |= (1 << 8);
628   Binary |= (Reg << 9);
629   return Binary;
630 }
631
632 // FIXME: This routine assumes that a binary
633 // expression will always result in a PCRel expression
634 // In reality, its only true if one or more subexpressions
635 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
636 // but this is good enough for now.
637 static bool EvaluateAsPCRel(const MCExpr *Expr) {
638   switch (Expr->getKind()) {
639   default: assert(0 && "Unexpected expression type");
640   case MCExpr::SymbolRef: return false;
641   case MCExpr::Binary: return true;
642   }
643 }
644
645 uint32_t
646 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
647                                       SmallVectorImpl<MCFixup> &Fixups) const {
648   // {20-16} = imm{15-12}
649   // {11-0}  = imm{11-0}
650   const MCOperand &MO = MI.getOperand(OpIdx);
651   if (MO.isImm())
652     // Hi / lo 16 bits already extracted during earlier passes.
653     return static_cast<unsigned>(MO.getImm());
654
655   // Handle :upper16: and :lower16: assembly prefixes.
656   const MCExpr *E = MO.getExpr();
657   if (E->getKind() == MCExpr::Target) {
658     const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
659     E = ARM16Expr->getSubExpr();
660
661     MCFixupKind Kind;
662     switch (ARM16Expr->getKind()) {
663     default: assert(0 && "Unsupported ARMFixup");
664     case ARMMCExpr::VK_ARM_HI16:
665       Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
666       if (EvaluateAsPCRel(E)) 
667         Kind = MCFixupKind(ARM::fixup_arm_movt_hi16_pcrel);
668       break;
669     case ARMMCExpr::VK_ARM_LO16:
670       Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
671       if (EvaluateAsPCRel(E)) 
672         Kind = MCFixupKind(ARM::fixup_arm_movw_lo16_pcrel);
673       break;
674     }
675     Fixups.push_back(MCFixup::Create(0, E, Kind));
676     return 0;
677   };
678
679   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
680   return 0;
681 }
682
683 uint32_t ARMMCCodeEmitter::
684 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
685                     SmallVectorImpl<MCFixup> &Fixups) const {
686   const MCOperand &MO = MI.getOperand(OpIdx);
687   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
688   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
689   unsigned Rn = getARMRegisterNumbering(MO.getReg());
690   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
691   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
692   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
693   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
694   unsigned SBits = getShiftOp(ShOp);
695
696   // {16-13} = Rn
697   // {12}    = isAdd
698   // {11-0}  = shifter
699   //  {3-0}  = Rm
700   //  {4}    = 0
701   //  {6-5}  = type
702   //  {11-7} = imm
703   uint32_t Binary = Rm;
704   Binary |= Rn << 13;
705   Binary |= SBits << 5;
706   Binary |= ShImm << 7;
707   if (isAdd)
708     Binary |= 1 << 12;
709   return Binary;
710 }
711
712 uint32_t ARMMCCodeEmitter::
713 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
714                     SmallVectorImpl<MCFixup> &Fixups) const {
715   // {17-14}  Rn
716   // {13}     1 == imm12, 0 == Rm
717   // {12}     isAdd
718   // {11-0}   imm12/Rm
719   const MCOperand &MO = MI.getOperand(OpIdx);
720   unsigned Rn = getARMRegisterNumbering(MO.getReg());
721   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
722   Binary |= Rn << 14;
723   return Binary;
724 }
725
726 uint32_t ARMMCCodeEmitter::
727 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
728                           SmallVectorImpl<MCFixup> &Fixups) const {
729   // {13}     1 == imm12, 0 == Rm
730   // {12}     isAdd
731   // {11-0}   imm12/Rm
732   const MCOperand &MO = MI.getOperand(OpIdx);
733   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
734   unsigned Imm = MO1.getImm();
735   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
736   bool isReg = MO.getReg() != 0;
737   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
738   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
739   if (isReg) {
740     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
741     Binary <<= 7;                    // Shift amount is bits [11:7]
742     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
743     Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
744   }
745   return Binary | (isAdd << 12) | (isReg << 13);
746 }
747
748 uint32_t ARMMCCodeEmitter::
749 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
750                           SmallVectorImpl<MCFixup> &Fixups) const {
751   // {9}      1 == imm8, 0 == Rm
752   // {8}      isAdd
753   // {7-4}    imm7_4/zero
754   // {3-0}    imm3_0/Rm
755   const MCOperand &MO = MI.getOperand(OpIdx);
756   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
757   unsigned Imm = MO1.getImm();
758   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
759   bool isImm = MO.getReg() == 0;
760   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
761   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
762   if (!isImm)
763     Imm8 = getARMRegisterNumbering(MO.getReg());
764   return Imm8 | (isAdd << 8) | (isImm << 9);
765 }
766
767 uint32_t ARMMCCodeEmitter::
768 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
769                     SmallVectorImpl<MCFixup> &Fixups) const {
770   // {13}     1 == imm8, 0 == Rm
771   // {12-9}   Rn
772   // {8}      isAdd
773   // {7-4}    imm7_4/zero
774   // {3-0}    imm3_0/Rm
775   const MCOperand &MO = MI.getOperand(OpIdx);
776   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
777   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
778   unsigned Rn = getARMRegisterNumbering(MO.getReg());
779   unsigned Imm = MO2.getImm();
780   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
781   bool isImm = MO1.getReg() == 0;
782   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
783   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
784   if (!isImm)
785     Imm8 = getARMRegisterNumbering(MO1.getReg());
786   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
787 }
788
789 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
790 uint32_t ARMMCCodeEmitter::
791 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
792                           SmallVectorImpl<MCFixup> &Fixups) const {
793   // [SP, #imm]
794   //   {7-0} = imm8
795   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
796   assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
797          "Unexpected base register!");
798
799   // The immediate is already shifted for the implicit zeroes, so no change
800   // here.
801   return MO1.getImm() & 0xff;
802 }
803
804 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
805 uint32_t ARMMCCodeEmitter::
806 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
807                      SmallVectorImpl<MCFixup> &Fixups) const {
808   // [Rn, #imm]
809   //   {7-3} = imm5
810   //   {2-0} = Rn
811   const MCOperand &MO = MI.getOperand(OpIdx);
812   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
813   unsigned Rn = getARMRegisterNumbering(MO.getReg());
814   unsigned Imm5 = MO1.getImm();
815   return ((Imm5 & 0x1f) << 3) | Rn;
816 }
817
818 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
819 uint32_t ARMMCCodeEmitter::
820 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
821                      SmallVectorImpl<MCFixup> &Fixups) const {
822   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
823 }
824
825 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
826 uint32_t ARMMCCodeEmitter::
827 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
828                     SmallVectorImpl<MCFixup> &Fixups) const {
829   // {12-9} = reg
830   // {8}    = (U)nsigned (add == '1', sub == '0')
831   // {7-0}  = imm8
832   unsigned Reg, Imm8;
833   bool isAdd;
834   // If The first operand isn't a register, we have a label reference.
835   const MCOperand &MO = MI.getOperand(OpIdx);
836   if (!MO.isReg()) {
837     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
838     Imm8 = 0;
839     isAdd = false; // 'U' bit is handled as part of the fixup.
840
841     assert(MO.isExpr() && "Unexpected machine operand type!");
842     const MCExpr *Expr = MO.getExpr();
843     MCFixupKind Kind;
844     const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
845     if (Subtarget.isThumb2())
846       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
847     else
848       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
849     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
850
851     ++MCNumCPRelocations;
852   } else {
853     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
854     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
855   }
856
857   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
858   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
859   if (isAdd)
860     Binary |= (1 << 8);
861   Binary |= (Reg << 9);
862   return Binary;
863 }
864
865 unsigned ARMMCCodeEmitter::
866 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
867                 SmallVectorImpl<MCFixup> &Fixups) const {
868   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
869   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
870   // case the imm contains the amount to shift by.
871   //
872   // {3-0} = Rm.
873   // {4}   = 1 if reg shift, 0 if imm shift
874   // {6-5} = type
875   //    If reg shift:
876   //      {11-8} = Rs
877   //      {7}    = 0
878   //    else (imm shift)
879   //      {11-7} = imm
880
881   const MCOperand &MO  = MI.getOperand(OpIdx);
882   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
883   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
884   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
885
886   // Encode Rm.
887   unsigned Binary = getARMRegisterNumbering(MO.getReg());
888
889   // Encode the shift opcode.
890   unsigned SBits = 0;
891   unsigned Rs = MO1.getReg();
892   if (Rs) {
893     // Set shift operand (bit[7:4]).
894     // LSL - 0001
895     // LSR - 0011
896     // ASR - 0101
897     // ROR - 0111
898     // RRX - 0110 and bit[11:8] clear.
899     switch (SOpc) {
900     default: llvm_unreachable("Unknown shift opc!");
901     case ARM_AM::lsl: SBits = 0x1; break;
902     case ARM_AM::lsr: SBits = 0x3; break;
903     case ARM_AM::asr: SBits = 0x5; break;
904     case ARM_AM::ror: SBits = 0x7; break;
905     case ARM_AM::rrx: SBits = 0x6; break;
906     }
907   } else {
908     // Set shift operand (bit[6:4]).
909     // LSL - 000
910     // LSR - 010
911     // ASR - 100
912     // ROR - 110
913     switch (SOpc) {
914     default: llvm_unreachable("Unknown shift opc!");
915     case ARM_AM::lsl: SBits = 0x0; break;
916     case ARM_AM::lsr: SBits = 0x2; break;
917     case ARM_AM::asr: SBits = 0x4; break;
918     case ARM_AM::ror: SBits = 0x6; break;
919     }
920   }
921
922   Binary |= SBits << 4;
923   if (SOpc == ARM_AM::rrx)
924     return Binary;
925
926   // Encode the shift operation Rs or shift_imm (except rrx).
927   if (Rs) {
928     // Encode Rs bit[11:8].
929     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
930     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
931   }
932
933   // Encode shift_imm bit[11:7].
934   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
935 }
936
937 unsigned ARMMCCodeEmitter::
938 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
939                 SmallVectorImpl<MCFixup> &Fixups) const {
940   const MCOperand &MO1 = MI.getOperand(OpNum);
941   const MCOperand &MO2 = MI.getOperand(OpNum+1);
942   const MCOperand &MO3 = MI.getOperand(OpNum+2);
943
944   // Encoded as [Rn, Rm, imm].
945   // FIXME: Needs fixup support.
946   unsigned Value = getARMRegisterNumbering(MO1.getReg());
947   Value <<= 4;
948   Value |= getARMRegisterNumbering(MO2.getReg());
949   Value <<= 2;
950   Value |= MO3.getImm();
951
952   return Value;
953 }
954
955 unsigned ARMMCCodeEmitter::
956 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
957                          SmallVectorImpl<MCFixup> &Fixups) const {
958   const MCOperand &MO1 = MI.getOperand(OpNum);
959   const MCOperand &MO2 = MI.getOperand(OpNum+1);
960
961   // FIXME: Needs fixup support.
962   unsigned Value = getARMRegisterNumbering(MO1.getReg());
963
964   // Even though the immediate is 8 bits long, we need 9 bits in order
965   // to represent the (inverse of the) sign bit.
966   Value <<= 9;
967   int32_t tmp = (int32_t)MO2.getImm();
968   if (tmp < 0)
969     tmp = abs(tmp);
970   else
971     Value |= 256; // Set the ADD bit
972   Value |= tmp & 255;
973   return Value;
974 }
975
976 unsigned ARMMCCodeEmitter::
977 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
978                          SmallVectorImpl<MCFixup> &Fixups) const {
979   const MCOperand &MO1 = MI.getOperand(OpNum);
980
981   // FIXME: Needs fixup support.
982   unsigned Value = 0;
983   int32_t tmp = (int32_t)MO1.getImm();
984   if (tmp < 0)
985     tmp = abs(tmp);
986   else
987     Value |= 256; // Set the ADD bit
988   Value |= tmp & 255;
989   return Value;
990 }
991
992 unsigned ARMMCCodeEmitter::
993 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
994                          SmallVectorImpl<MCFixup> &Fixups) const {
995   const MCOperand &MO1 = MI.getOperand(OpNum);
996
997   // FIXME: Needs fixup support.
998   unsigned Value = 0;
999   int32_t tmp = (int32_t)MO1.getImm();
1000   if (tmp < 0)
1001     tmp = abs(tmp);
1002   else
1003     Value |= 4096; // Set the ADD bit
1004   Value |= tmp & 4095;
1005   return Value;
1006 }
1007
1008 unsigned ARMMCCodeEmitter::
1009 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1010                 SmallVectorImpl<MCFixup> &Fixups) const {
1011   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1012   // shifted. The second is the amount to shift by.
1013   //
1014   // {3-0} = Rm.
1015   // {4}   = 0
1016   // {6-5} = type
1017   // {11-7} = imm
1018
1019   const MCOperand &MO  = MI.getOperand(OpIdx);
1020   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1021   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1022
1023   // Encode Rm.
1024   unsigned Binary = getARMRegisterNumbering(MO.getReg());
1025
1026   // Encode the shift opcode.
1027   unsigned SBits = 0;
1028   // Set shift operand (bit[6:4]).
1029   // LSL - 000
1030   // LSR - 010
1031   // ASR - 100
1032   // ROR - 110
1033   switch (SOpc) {
1034   default: llvm_unreachable("Unknown shift opc!");
1035   case ARM_AM::lsl: SBits = 0x0; break;
1036   case ARM_AM::lsr: SBits = 0x2; break;
1037   case ARM_AM::asr: SBits = 0x4; break;
1038   case ARM_AM::ror: SBits = 0x6; break;
1039   }
1040
1041   Binary |= SBits << 4;
1042   if (SOpc == ARM_AM::rrx)
1043     return Binary;
1044
1045   // Encode shift_imm bit[11:7].
1046   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1047 }
1048
1049 unsigned ARMMCCodeEmitter::
1050 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1051                                SmallVectorImpl<MCFixup> &Fixups) const {
1052   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1053   // msb of the mask.
1054   const MCOperand &MO = MI.getOperand(Op);
1055   uint32_t v = ~MO.getImm();
1056   uint32_t lsb = CountTrailingZeros_32(v);
1057   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1058   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1059   return lsb | (msb << 5);
1060 }
1061
1062 unsigned ARMMCCodeEmitter::
1063 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1064                        SmallVectorImpl<MCFixup> &Fixups) const {
1065   // VLDM/VSTM:
1066   //   {12-8} = Vd
1067   //   {7-0}  = Number of registers
1068   //
1069   // LDM/STM:
1070   //   {15-0}  = Bitfield of GPRs.
1071   unsigned Reg = MI.getOperand(Op).getReg();
1072   bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1073   bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1074
1075   unsigned Binary = 0;
1076
1077   if (SPRRegs || DPRRegs) {
1078     // VLDM/VSTM
1079     unsigned RegNo = getARMRegisterNumbering(Reg);
1080     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1081     Binary |= (RegNo & 0x1f) << 8;
1082     if (SPRRegs)
1083       Binary |= NumRegs;
1084     else
1085       Binary |= NumRegs * 2;
1086   } else {
1087     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1088       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1089       Binary |= 1 << RegNo;
1090     }
1091   }
1092
1093   return Binary;
1094 }
1095
1096 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1097 /// with the alignment operand.
1098 unsigned ARMMCCodeEmitter::
1099 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1100                            SmallVectorImpl<MCFixup> &Fixups) const {
1101   const MCOperand &Reg = MI.getOperand(Op);
1102   const MCOperand &Imm = MI.getOperand(Op + 1);
1103
1104   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1105   unsigned Align = 0;
1106
1107   switch (Imm.getImm()) {
1108   default: break;
1109   case 2:
1110   case 4:
1111   case 8:  Align = 0x01; break;
1112   case 16: Align = 0x02; break;
1113   case 32: Align = 0x03; break;
1114   }
1115
1116   return RegNo | (Align << 4);
1117 }
1118
1119 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1120 /// alignment operand for use in VLD-dup instructions.  This is the same as
1121 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1122 /// different for VLD4-dup.
1123 unsigned ARMMCCodeEmitter::
1124 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1125                               SmallVectorImpl<MCFixup> &Fixups) const {
1126   const MCOperand &Reg = MI.getOperand(Op);
1127   const MCOperand &Imm = MI.getOperand(Op + 1);
1128
1129   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1130   unsigned Align = 0;
1131
1132   switch (Imm.getImm()) {
1133   default: break;
1134   case 2:
1135   case 4:
1136   case 8:  Align = 0x01; break;
1137   case 16: Align = 0x03; break;
1138   }
1139
1140   return RegNo | (Align << 4);
1141 }
1142
1143 unsigned ARMMCCodeEmitter::
1144 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1145                           SmallVectorImpl<MCFixup> &Fixups) const {
1146   const MCOperand &MO = MI.getOperand(Op);
1147   if (MO.getReg() == 0) return 0x0D;
1148   return MO.getReg();
1149 }
1150
1151 void ARMMCCodeEmitter::
1152 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1153                   SmallVectorImpl<MCFixup> &Fixups) const {
1154   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
1155   // Pseudo instructions don't get encoded.
1156   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
1157   uint64_t TSFlags = Desc.TSFlags;
1158   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1159     return;
1160   int Size;
1161   // Basic size info comes from the TSFlags field.
1162   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1163   default: llvm_unreachable("Unexpected instruction size!");
1164   case ARMII::Size2Bytes: Size = 2; break;
1165   case ARMII::Size4Bytes: Size = 4; break;
1166   }
1167   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1168   // Thumb 32-bit wide instructions need to emit the high order halfword
1169   // first.
1170   if (Subtarget.isThumb() && Size == 4) {
1171     EmitConstant(Binary >> 16, 2, OS);
1172     EmitConstant(Binary & 0xffff, 2, OS);
1173   } else
1174     EmitConstant(Binary, Size, OS);
1175   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1176 }
1177
1178 #include "ARMGenMCCodeEmitter.inc"