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