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