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