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