Correct encoding of BL with immediate offset.
[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 /// getBranchTargetOpValue - Helper function to get the branch target operand,
447 /// which is either an immediate or requires a fixup.
448 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
449                                        unsigned FixupKind,
450                                        SmallVectorImpl<MCFixup> &Fixups) {
451   const MCOperand &MO = MI.getOperand(OpIdx);
452
453   // If the destination is an immediate, we have nothing to do.
454   if (MO.isImm()) return MO.getImm();
455   assert(MO.isExpr() && "Unexpected branch target type!");
456   const MCExpr *Expr = MO.getExpr();
457   MCFixupKind Kind = MCFixupKind(FixupKind);
458   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
459
460   // All of the information is in the fixup.
461   return 0;
462 }
463
464 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
465 uint32_t ARMMCCodeEmitter::
466 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
467                         SmallVectorImpl<MCFixup> &Fixups) const {
468   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
469 }
470
471 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
472 /// BLX branch target.
473 uint32_t ARMMCCodeEmitter::
474 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
475                          SmallVectorImpl<MCFixup> &Fixups) const {
476   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
477 }
478
479 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
480 uint32_t ARMMCCodeEmitter::
481 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
482                         SmallVectorImpl<MCFixup> &Fixups) const {
483   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
484 }
485
486 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
487 uint32_t ARMMCCodeEmitter::
488 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
489                          SmallVectorImpl<MCFixup> &Fixups) const {
490   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
491 }
492
493 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
494 uint32_t ARMMCCodeEmitter::
495 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
496                         SmallVectorImpl<MCFixup> &Fixups) const {
497   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
498 }
499
500 /// Return true if this branch has a non-always predication
501 static bool HasConditionalBranch(const MCInst &MI) {
502   int NumOp = MI.getNumOperands();
503   if (NumOp >= 2) {
504     for (int i = 0; i < NumOp-1; ++i) {
505       const MCOperand &MCOp1 = MI.getOperand(i);
506       const MCOperand &MCOp2 = MI.getOperand(i + 1);
507       if (MCOp1.isImm() && MCOp2.isReg() && 
508           (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
509         if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL) 
510           return true;
511       }
512     }
513   }
514   return false;
515 }
516
517 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
518 /// target.
519 uint32_t ARMMCCodeEmitter::
520 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
521                        SmallVectorImpl<MCFixup> &Fixups) const {
522   // FIXME: This really, really shouldn't use TargetMachine. We don't want
523   // coupling between MC and TM anywhere we can help it.
524   if (isThumb2())
525     return
526       ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
527   return getARMBranchTargetOpValue(MI, OpIdx, Fixups);
528 }
529
530 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
531 /// target.
532 uint32_t ARMMCCodeEmitter::
533 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
534                           SmallVectorImpl<MCFixup> &Fixups) const {
535   const MCOperand MO = MI.getOperand(OpIdx);
536   if (MO.isExpr()) {
537     if (HasConditionalBranch(MI)) 
538       return ::getBranchTargetOpValue(MI, OpIdx,
539                                       ARM::fixup_arm_condbranch, Fixups);
540     return ::getBranchTargetOpValue(MI, OpIdx, 
541                                     ARM::fixup_arm_uncondbranch, Fixups);
542   }
543
544   return MO.getImm() >> 2;
545 }
546
547
548
549
550 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
551 /// immediate branch target.
552 uint32_t ARMMCCodeEmitter::
553 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
554                        SmallVectorImpl<MCFixup> &Fixups) const {
555   unsigned Val =
556     ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
557   bool I  = (Val & 0x800000);
558   bool J1 = (Val & 0x400000);
559   bool J2 = (Val & 0x200000);
560   if (I ^ J1)
561     Val &= ~0x400000;
562   else
563     Val |= 0x400000;
564
565   if (I ^ J2)
566     Val &= ~0x200000;
567   else
568     Val |= 0x200000;
569
570   return Val;
571 }
572
573 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
574 /// target.
575 uint32_t ARMMCCodeEmitter::
576 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
577                    SmallVectorImpl<MCFixup> &Fixups) const {
578   const MCOperand MO = MI.getOperand(OpIdx);
579   if (MO.isExpr())
580     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
581                                     Fixups);
582   int32_t offset = MO.getImm();
583   uint32_t Val = 0x2000;
584   if (offset < 0) {
585     Val = 0x1000;
586     offset *= -1;
587   }
588   Val |= offset;
589   return Val;
590 }
591
592 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
593 /// target.
594 uint32_t ARMMCCodeEmitter::
595 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
596                    SmallVectorImpl<MCFixup> &Fixups) const {
597   const MCOperand MO = MI.getOperand(OpIdx);
598   if (MO.isExpr())
599     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
600                                     Fixups);
601   return MO.getImm();
602 }
603
604 /// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
605 /// target.
606 uint32_t ARMMCCodeEmitter::
607 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
608                    SmallVectorImpl<MCFixup> &Fixups) const {
609   const MCOperand MO = MI.getOperand(OpIdx);
610   if (MO.isExpr())
611     return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
612                                     Fixups);
613   return MO.getImm();
614 }
615
616 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
617 /// operand.
618 uint32_t ARMMCCodeEmitter::
619 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
620                               SmallVectorImpl<MCFixup> &) const {
621   // [Rn, Rm]
622   //   {5-3} = Rm
623   //   {2-0} = Rn
624   const MCOperand &MO1 = MI.getOperand(OpIdx);
625   const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
626   unsigned Rn = getARMRegisterNumbering(MO1.getReg());
627   unsigned Rm = getARMRegisterNumbering(MO2.getReg());
628   return (Rm << 3) | Rn;
629 }
630
631 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
632 uint32_t ARMMCCodeEmitter::
633 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
634                         SmallVectorImpl<MCFixup> &Fixups) const {
635   // {17-13} = reg
636   // {12}    = (U)nsigned (add == '1', sub == '0')
637   // {11-0}  = imm12
638   unsigned Reg, Imm12;
639   bool isAdd = true;
640   // If The first operand isn't a register, we have a label reference.
641   const MCOperand &MO = MI.getOperand(OpIdx);
642   if (!MO.isReg()) {
643     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
644     Imm12 = 0;
645     isAdd = false ; // 'U' bit is set as part of the fixup.
646
647     assert(MO.isExpr() && "Unexpected machine operand type!");
648     const MCExpr *Expr = MO.getExpr();
649
650     MCFixupKind Kind;
651     if (isThumb2())
652       Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
653     else
654       Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
655     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
656
657     ++MCNumCPRelocations;
658   } else
659     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
660
661   uint32_t Binary = Imm12 & 0xfff;
662   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
663   if (isAdd)
664     Binary |= (1 << 12);
665   Binary |= (Reg << 13);
666   return Binary;
667 }
668
669 /// getT2AddrModeImm8s4OpValue - Return encoding info for
670 /// 'reg +/- imm8<<2' operand.
671 uint32_t ARMMCCodeEmitter::
672 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
673                         SmallVectorImpl<MCFixup> &Fixups) const {
674   // {12-9} = reg
675   // {8}    = (U)nsigned (add == '1', sub == '0')
676   // {7-0}  = imm8
677   unsigned Reg, Imm8;
678   bool isAdd = true;
679   // If The first operand isn't a register, we have a label reference.
680   const MCOperand &MO = MI.getOperand(OpIdx);
681   if (!MO.isReg()) {
682     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
683     Imm8 = 0;
684     isAdd = false ; // 'U' bit is set as part of the fixup.
685
686     assert(MO.isExpr() && "Unexpected machine operand type!");
687     const MCExpr *Expr = MO.getExpr();
688     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
689     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
690
691     ++MCNumCPRelocations;
692   } else
693     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
694
695   uint32_t Binary = (Imm8 >> 2) & 0xff;
696   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
697   if (isAdd)
698     Binary |= (1 << 8);
699   Binary |= (Reg << 9);
700   return Binary;
701 }
702
703 // FIXME: This routine assumes that a binary
704 // expression will always result in a PCRel expression
705 // In reality, its only true if one or more subexpressions
706 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
707 // but this is good enough for now.
708 static bool EvaluateAsPCRel(const MCExpr *Expr) {
709   switch (Expr->getKind()) {
710   default: assert(0 && "Unexpected expression type");
711   case MCExpr::SymbolRef: return false;
712   case MCExpr::Binary: return true;
713   }
714 }
715
716 uint32_t
717 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
718                                       SmallVectorImpl<MCFixup> &Fixups) const {
719   // {20-16} = imm{15-12}
720   // {11-0}  = imm{11-0}
721   const MCOperand &MO = MI.getOperand(OpIdx);
722   if (MO.isImm())
723     // Hi / lo 16 bits already extracted during earlier passes.
724     return static_cast<unsigned>(MO.getImm());
725
726   // Handle :upper16: and :lower16: assembly prefixes.
727   const MCExpr *E = MO.getExpr();
728   if (E->getKind() == MCExpr::Target) {
729     const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
730     E = ARM16Expr->getSubExpr();
731
732     MCFixupKind Kind;
733     switch (ARM16Expr->getKind()) {
734     default: assert(0 && "Unsupported ARMFixup");
735     case ARMMCExpr::VK_ARM_HI16:
736       if (!isTargetDarwin() && EvaluateAsPCRel(E))
737         Kind = MCFixupKind(isThumb2()
738                            ? ARM::fixup_t2_movt_hi16_pcrel
739                            : ARM::fixup_arm_movt_hi16_pcrel);
740       else
741         Kind = MCFixupKind(isThumb2()
742                            ? ARM::fixup_t2_movt_hi16
743                            : ARM::fixup_arm_movt_hi16);
744       break;
745     case ARMMCExpr::VK_ARM_LO16:
746       if (!isTargetDarwin() && EvaluateAsPCRel(E))
747         Kind = MCFixupKind(isThumb2()
748                            ? ARM::fixup_t2_movw_lo16_pcrel
749                            : ARM::fixup_arm_movw_lo16_pcrel);
750       else
751         Kind = MCFixupKind(isThumb2()
752                            ? ARM::fixup_t2_movw_lo16
753                            : ARM::fixup_arm_movw_lo16);
754       break;
755     }
756     Fixups.push_back(MCFixup::Create(0, E, Kind));
757     return 0;
758   };
759
760   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
761   return 0;
762 }
763
764 uint32_t ARMMCCodeEmitter::
765 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
766                     SmallVectorImpl<MCFixup> &Fixups) const {
767   const MCOperand &MO = MI.getOperand(OpIdx);
768   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
769   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
770   unsigned Rn = getARMRegisterNumbering(MO.getReg());
771   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
772   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
773   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
774   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
775   unsigned SBits = getShiftOp(ShOp);
776
777   // {16-13} = Rn
778   // {12}    = isAdd
779   // {11-0}  = shifter
780   //  {3-0}  = Rm
781   //  {4}    = 0
782   //  {6-5}  = type
783   //  {11-7} = imm
784   uint32_t Binary = Rm;
785   Binary |= Rn << 13;
786   Binary |= SBits << 5;
787   Binary |= ShImm << 7;
788   if (isAdd)
789     Binary |= 1 << 12;
790   return Binary;
791 }
792
793 uint32_t ARMMCCodeEmitter::
794 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
795                     SmallVectorImpl<MCFixup> &Fixups) const {
796   // {17-14}  Rn
797   // {13}     1 == imm12, 0 == Rm
798   // {12}     isAdd
799   // {11-0}   imm12/Rm
800   const MCOperand &MO = MI.getOperand(OpIdx);
801   unsigned Rn = getARMRegisterNumbering(MO.getReg());
802   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
803   Binary |= Rn << 14;
804   return Binary;
805 }
806
807 uint32_t ARMMCCodeEmitter::
808 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
809                           SmallVectorImpl<MCFixup> &Fixups) const {
810   // {13}     1 == imm12, 0 == Rm
811   // {12}     isAdd
812   // {11-0}   imm12/Rm
813   const MCOperand &MO = MI.getOperand(OpIdx);
814   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
815   unsigned Imm = MO1.getImm();
816   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
817   bool isReg = MO.getReg() != 0;
818   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
819   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
820   if (isReg) {
821     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
822     Binary <<= 7;                    // Shift amount is bits [11:7]
823     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
824     Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
825   }
826   return Binary | (isAdd << 12) | (isReg << 13);
827 }
828
829 uint32_t ARMMCCodeEmitter::
830 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
831                      SmallVectorImpl<MCFixup> &Fixups) const {
832   // {4}      isAdd
833   // {3-0}    Rm
834   const MCOperand &MO = MI.getOperand(OpIdx);
835   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
836   bool isAdd = MO1.getImm() != 0;
837   return getARMRegisterNumbering(MO.getReg()) | (isAdd << 4);
838 }
839
840 uint32_t ARMMCCodeEmitter::
841 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
842                           SmallVectorImpl<MCFixup> &Fixups) const {
843   // {9}      1 == imm8, 0 == Rm
844   // {8}      isAdd
845   // {7-4}    imm7_4/zero
846   // {3-0}    imm3_0/Rm
847   const MCOperand &MO = MI.getOperand(OpIdx);
848   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
849   unsigned Imm = MO1.getImm();
850   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
851   bool isImm = MO.getReg() == 0;
852   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
853   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
854   if (!isImm)
855     Imm8 = getARMRegisterNumbering(MO.getReg());
856   return Imm8 | (isAdd << 8) | (isImm << 9);
857 }
858
859 uint32_t ARMMCCodeEmitter::
860 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
861                     SmallVectorImpl<MCFixup> &Fixups) const {
862   // {13}     1 == imm8, 0 == Rm
863   // {12-9}   Rn
864   // {8}      isAdd
865   // {7-4}    imm7_4/zero
866   // {3-0}    imm3_0/Rm
867   const MCOperand &MO = MI.getOperand(OpIdx);
868   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
869   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
870   unsigned Rn = getARMRegisterNumbering(MO.getReg());
871   unsigned Imm = MO2.getImm();
872   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
873   bool isImm = MO1.getReg() == 0;
874   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
875   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
876   if (!isImm)
877     Imm8 = getARMRegisterNumbering(MO1.getReg());
878   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
879 }
880
881 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
882 uint32_t ARMMCCodeEmitter::
883 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
884                           SmallVectorImpl<MCFixup> &Fixups) const {
885   // [SP, #imm]
886   //   {7-0} = imm8
887   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
888   assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
889          "Unexpected base register!");
890
891   // The immediate is already shifted for the implicit zeroes, so no change
892   // here.
893   return MO1.getImm() & 0xff;
894 }
895
896 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
897 uint32_t ARMMCCodeEmitter::
898 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
899                      SmallVectorImpl<MCFixup> &Fixups) const {
900   // [Rn, #imm]
901   //   {7-3} = imm5
902   //   {2-0} = Rn
903   const MCOperand &MO = MI.getOperand(OpIdx);
904   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
905   unsigned Rn = getARMRegisterNumbering(MO.getReg());
906   unsigned Imm5 = MO1.getImm();
907   return ((Imm5 & 0x1f) << 3) | Rn;
908 }
909
910 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
911 uint32_t ARMMCCodeEmitter::
912 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
913                      SmallVectorImpl<MCFixup> &Fixups) const {
914   return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
915 }
916
917 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
918 uint32_t ARMMCCodeEmitter::
919 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
920                     SmallVectorImpl<MCFixup> &Fixups) const {
921   // {12-9} = reg
922   // {8}    = (U)nsigned (add == '1', sub == '0')
923   // {7-0}  = imm8
924   unsigned Reg, Imm8;
925   bool isAdd;
926   // If The first operand isn't a register, we have a label reference.
927   const MCOperand &MO = MI.getOperand(OpIdx);
928   if (!MO.isReg()) {
929     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
930     Imm8 = 0;
931     isAdd = false; // 'U' bit is handled as part of the fixup.
932
933     assert(MO.isExpr() && "Unexpected machine operand type!");
934     const MCExpr *Expr = MO.getExpr();
935     MCFixupKind Kind;
936     if (isThumb2())
937       Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
938     else
939       Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
940     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
941
942     ++MCNumCPRelocations;
943   } else {
944     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
945     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
946   }
947
948   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
949   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
950   if (isAdd)
951     Binary |= (1 << 8);
952   Binary |= (Reg << 9);
953   return Binary;
954 }
955
956 unsigned ARMMCCodeEmitter::
957 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
958                 SmallVectorImpl<MCFixup> &Fixups) const {
959   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
960   // shifted. The second is Rs, the amount to shift by, and the third specifies
961   // the type of the shift.
962   //
963   // {3-0} = Rm.
964   // {4}   = 1
965   // {6-5} = type
966   // {11-8} = Rs
967   // {7}    = 0
968
969   const MCOperand &MO  = MI.getOperand(OpIdx);
970   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
971   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
972   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
973
974   // Encode Rm.
975   unsigned Binary = getARMRegisterNumbering(MO.getReg());
976
977   // Encode the shift opcode.
978   unsigned SBits = 0;
979   unsigned Rs = MO1.getReg();
980   if (Rs) {
981     // Set shift operand (bit[7:4]).
982     // LSL - 0001
983     // LSR - 0011
984     // ASR - 0101
985     // ROR - 0111
986     switch (SOpc) {
987     default: llvm_unreachable("Unknown shift opc!");
988     case ARM_AM::lsl: SBits = 0x1; break;
989     case ARM_AM::lsr: SBits = 0x3; break;
990     case ARM_AM::asr: SBits = 0x5; break;
991     case ARM_AM::ror: SBits = 0x7; break;
992     }
993   }
994
995   Binary |= SBits << 4;
996
997   // Encode the shift operation Rs.
998   // Encode Rs bit[11:8].
999   assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1000   return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
1001 }
1002
1003 unsigned ARMMCCodeEmitter::
1004 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1005                 SmallVectorImpl<MCFixup> &Fixups) const {
1006   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1007   // shifted. The second is the amount to shift by.
1008   //
1009   // {3-0} = Rm.
1010   // {4}   = 0
1011   // {6-5} = type
1012   // {11-7} = imm
1013
1014   const MCOperand &MO  = MI.getOperand(OpIdx);
1015   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1016   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1017
1018   // Encode Rm.
1019   unsigned Binary = getARMRegisterNumbering(MO.getReg());
1020
1021   // Encode the shift opcode.
1022   unsigned SBits = 0;
1023
1024   // Set shift operand (bit[6:4]).
1025   // LSL - 000
1026   // LSR - 010
1027   // ASR - 100
1028   // ROR - 110
1029   // RRX - 110 and bit[11:8] clear.
1030   switch (SOpc) {
1031   default: llvm_unreachable("Unknown shift opc!");
1032   case ARM_AM::lsl: SBits = 0x0; break;
1033   case ARM_AM::lsr: SBits = 0x2; break;
1034   case ARM_AM::asr: SBits = 0x4; break;
1035   case ARM_AM::ror: SBits = 0x6; break;
1036   case ARM_AM::rrx:
1037     Binary |= 0x60;
1038     return Binary;
1039   }
1040
1041   // Encode shift_imm bit[11:7].
1042   Binary |= SBits << 4;
1043   unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1044   assert(Offset && "Offset must be in range 1-32!");
1045   if (Offset == 32) Offset = 0;
1046   return Binary | (Offset << 7);
1047 }
1048
1049
1050 unsigned ARMMCCodeEmitter::
1051 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1052                 SmallVectorImpl<MCFixup> &Fixups) const {
1053   const MCOperand &MO1 = MI.getOperand(OpNum);
1054   const MCOperand &MO2 = MI.getOperand(OpNum+1);
1055   const MCOperand &MO3 = MI.getOperand(OpNum+2);
1056
1057   // Encoded as [Rn, Rm, imm].
1058   // FIXME: Needs fixup support.
1059   unsigned Value = getARMRegisterNumbering(MO1.getReg());
1060   Value <<= 4;
1061   Value |= getARMRegisterNumbering(MO2.getReg());
1062   Value <<= 2;
1063   Value |= MO3.getImm();
1064
1065   return Value;
1066 }
1067
1068 unsigned ARMMCCodeEmitter::
1069 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1070                          SmallVectorImpl<MCFixup> &Fixups) const {
1071   const MCOperand &MO1 = MI.getOperand(OpNum);
1072   const MCOperand &MO2 = MI.getOperand(OpNum+1);
1073
1074   // FIXME: Needs fixup support.
1075   unsigned Value = getARMRegisterNumbering(MO1.getReg());
1076
1077   // Even though the immediate is 8 bits long, we need 9 bits in order
1078   // to represent the (inverse of the) sign bit.
1079   Value <<= 9;
1080   int32_t tmp = (int32_t)MO2.getImm();
1081   if (tmp < 0)
1082     tmp = abs(tmp);
1083   else
1084     Value |= 256; // Set the ADD bit
1085   Value |= tmp & 255;
1086   return Value;
1087 }
1088
1089 unsigned ARMMCCodeEmitter::
1090 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1091                          SmallVectorImpl<MCFixup> &Fixups) const {
1092   const MCOperand &MO1 = MI.getOperand(OpNum);
1093
1094   // FIXME: Needs fixup support.
1095   unsigned Value = 0;
1096   int32_t tmp = (int32_t)MO1.getImm();
1097   if (tmp < 0)
1098     tmp = abs(tmp);
1099   else
1100     Value |= 256; // Set the ADD bit
1101   Value |= tmp & 255;
1102   return Value;
1103 }
1104
1105 unsigned ARMMCCodeEmitter::
1106 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1107                          SmallVectorImpl<MCFixup> &Fixups) const {
1108   const MCOperand &MO1 = MI.getOperand(OpNum);
1109
1110   // FIXME: Needs fixup support.
1111   unsigned Value = 0;
1112   int32_t tmp = (int32_t)MO1.getImm();
1113   if (tmp < 0)
1114     tmp = abs(tmp);
1115   else
1116     Value |= 4096; // Set the ADD bit
1117   Value |= tmp & 4095;
1118   return Value;
1119 }
1120
1121 unsigned ARMMCCodeEmitter::
1122 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1123                 SmallVectorImpl<MCFixup> &Fixups) const {
1124   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1125   // shifted. The second is the amount to shift by.
1126   //
1127   // {3-0} = Rm.
1128   // {4}   = 0
1129   // {6-5} = type
1130   // {11-7} = imm
1131
1132   const MCOperand &MO  = MI.getOperand(OpIdx);
1133   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1134   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1135
1136   // Encode Rm.
1137   unsigned Binary = getARMRegisterNumbering(MO.getReg());
1138
1139   // Encode the shift opcode.
1140   unsigned SBits = 0;
1141   // Set shift operand (bit[6:4]).
1142   // LSL - 000
1143   // LSR - 010
1144   // ASR - 100
1145   // ROR - 110
1146   switch (SOpc) {
1147   default: llvm_unreachable("Unknown shift opc!");
1148   case ARM_AM::lsl: SBits = 0x0; break;
1149   case ARM_AM::lsr: SBits = 0x2; break;
1150   case ARM_AM::asr: SBits = 0x4; break;
1151   case ARM_AM::ror: SBits = 0x6; break;
1152   }
1153
1154   Binary |= SBits << 4;
1155   if (SOpc == ARM_AM::rrx)
1156     return Binary;
1157
1158   // Encode shift_imm bit[11:7].
1159   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1160 }
1161
1162 unsigned ARMMCCodeEmitter::
1163 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1164                                SmallVectorImpl<MCFixup> &Fixups) const {
1165   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1166   // msb of the mask.
1167   const MCOperand &MO = MI.getOperand(Op);
1168   uint32_t v = ~MO.getImm();
1169   uint32_t lsb = CountTrailingZeros_32(v);
1170   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1171   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1172   return lsb | (msb << 5);
1173 }
1174
1175 unsigned ARMMCCodeEmitter::
1176 getMsbOpValue(const MCInst &MI, unsigned Op,
1177               SmallVectorImpl<MCFixup> &Fixups) const {
1178   // MSB - 5 bits.
1179   uint32_t lsb = MI.getOperand(Op-1).getImm();
1180   uint32_t width = MI.getOperand(Op).getImm();
1181   uint32_t msb = lsb+width-1;
1182   assert (width != 0 && msb < 32 && "Illegal bit width!");
1183   return msb;
1184 }
1185
1186 unsigned ARMMCCodeEmitter::
1187 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1188                        SmallVectorImpl<MCFixup> &Fixups) const {
1189   // VLDM/VSTM:
1190   //   {12-8} = Vd
1191   //   {7-0}  = Number of registers
1192   //
1193   // LDM/STM:
1194   //   {15-0}  = Bitfield of GPRs.
1195   unsigned Reg = MI.getOperand(Op).getReg();
1196   bool SPRRegs = llvm::ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1197   bool DPRRegs = llvm::ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1198
1199   unsigned Binary = 0;
1200
1201   if (SPRRegs || DPRRegs) {
1202     // VLDM/VSTM
1203     unsigned RegNo = getARMRegisterNumbering(Reg);
1204     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1205     Binary |= (RegNo & 0x1f) << 8;
1206     if (SPRRegs)
1207       Binary |= NumRegs;
1208     else
1209       Binary |= NumRegs * 2;
1210   } else {
1211     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1212       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1213       Binary |= 1 << RegNo;
1214     }
1215   }
1216
1217   return Binary;
1218 }
1219
1220 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1221 /// with the alignment operand.
1222 unsigned ARMMCCodeEmitter::
1223 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1224                            SmallVectorImpl<MCFixup> &Fixups) const {
1225   const MCOperand &Reg = MI.getOperand(Op);
1226   const MCOperand &Imm = MI.getOperand(Op + 1);
1227
1228   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1229   unsigned Align = 0;
1230
1231   switch (Imm.getImm()) {
1232   default: break;
1233   case 2:
1234   case 4:
1235   case 8:  Align = 0x01; break;
1236   case 16: Align = 0x02; break;
1237   case 32: Align = 0x03; break;
1238   }
1239
1240   return RegNo | (Align << 4);
1241 }
1242
1243 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1244 /// along  with the alignment operand for use in VST1 and VLD1 with size 32.
1245 unsigned ARMMCCodeEmitter::
1246 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1247                                     SmallVectorImpl<MCFixup> &Fixups) const {
1248   const MCOperand &Reg = MI.getOperand(Op);
1249   const MCOperand &Imm = MI.getOperand(Op + 1);
1250
1251   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1252   unsigned Align = 0;
1253
1254   switch (Imm.getImm()) {
1255   default: break;
1256   case 2:
1257   case 4:
1258   case 8:
1259   case 16: Align = 0x00; break;
1260   case 32: Align = 0x03; break;
1261   }
1262
1263   return RegNo | (Align << 4);
1264 }
1265
1266
1267 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1268 /// alignment operand for use in VLD-dup instructions.  This is the same as
1269 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1270 /// different for VLD4-dup.
1271 unsigned ARMMCCodeEmitter::
1272 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1273                               SmallVectorImpl<MCFixup> &Fixups) const {
1274   const MCOperand &Reg = MI.getOperand(Op);
1275   const MCOperand &Imm = MI.getOperand(Op + 1);
1276
1277   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1278   unsigned Align = 0;
1279
1280   switch (Imm.getImm()) {
1281   default: break;
1282   case 2:
1283   case 4:
1284   case 8:  Align = 0x01; break;
1285   case 16: Align = 0x03; break;
1286   }
1287
1288   return RegNo | (Align << 4);
1289 }
1290
1291 unsigned ARMMCCodeEmitter::
1292 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1293                           SmallVectorImpl<MCFixup> &Fixups) const {
1294   const MCOperand &MO = MI.getOperand(Op);
1295   if (MO.getReg() == 0) return 0x0D;
1296   return MO.getReg();
1297 }
1298
1299 unsigned ARMMCCodeEmitter::
1300 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1301                   SmallVectorImpl<MCFixup> &Fixups) const {
1302   return 8 - MI.getOperand(Op).getImm();
1303 }
1304
1305 unsigned ARMMCCodeEmitter::
1306 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1307                    SmallVectorImpl<MCFixup> &Fixups) const {
1308   return 16 - MI.getOperand(Op).getImm();
1309 }
1310
1311 unsigned ARMMCCodeEmitter::
1312 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1313                    SmallVectorImpl<MCFixup> &Fixups) const {
1314   return 32 - MI.getOperand(Op).getImm();
1315 }
1316
1317 unsigned ARMMCCodeEmitter::
1318 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1319                    SmallVectorImpl<MCFixup> &Fixups) const {
1320   return 64 - MI.getOperand(Op).getImm();
1321 }
1322
1323 void ARMMCCodeEmitter::
1324 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1325                   SmallVectorImpl<MCFixup> &Fixups) const {
1326   // Pseudo instructions don't get encoded.
1327   const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1328   uint64_t TSFlags = Desc.TSFlags;
1329   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1330     return;
1331
1332   int Size;
1333   if (Desc.getSize() == 2 || Desc.getSize() == 4)
1334     Size = Desc.getSize();
1335   else
1336     llvm_unreachable("Unexpected instruction size!");
1337   
1338   uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1339   // Thumb 32-bit wide instructions need to emit the high order halfword
1340   // first.
1341   if (isThumb() && Size == 4) {
1342     EmitConstant(Binary >> 16, 2, OS);
1343     EmitConstant(Binary & 0xffff, 2, OS);
1344   } else
1345     EmitConstant(Binary, Size, OS);
1346   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
1347 }
1348
1349 #include "ARMGenMCCodeEmitter.inc"