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