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