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