207332fba729081a2423a14df90e5a33bff42815
[oota-llvm.git] / lib / Target / ARM / 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 "ARM.h"
16 #include "ARMAddressingModes.h"
17 #include "ARMFixupKinds.h"
18 #include "ARMInstrInfo.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25
26 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
28
29 namespace {
30 class ARMMCCodeEmitter : public MCCodeEmitter {
31   ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32   void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33   const TargetMachine &TM;
34   const TargetInstrInfo &TII;
35   MCContext &Ctx;
36
37 public:
38   ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39     : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
40   }
41
42   ~ARMMCCodeEmitter() {}
43
44   unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
45
46   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47     const static MCFixupKindInfo Infos[] = {
48       // name                     offset  bits  flags
49       { "fixup_arm_pcrel_12",     1,      24,   MCFixupKindInfo::FKF_IsPCRel },
50       { "fixup_arm_pcrel_10",     1,      24,   MCFixupKindInfo::FKF_IsPCRel },
51       { "fixup_arm_branch",       1,      24,   MCFixupKindInfo::FKF_IsPCRel },
52       { "fixup_arm_movt_hi16",    0,      16,   0 },
53       { "fixup_arm_movw_lo16",    0,      16,   0 },
54     };
55
56     if (Kind < FirstTargetFixupKind)
57       return MCCodeEmitter::getFixupKindInfo(Kind);
58
59     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
60            "Invalid kind!");
61     return Infos[Kind - FirstTargetFixupKind];
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   /// getMovtImmOpValue - Return the encoding for the movw/movt pair
76   uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
77                              SmallVectorImpl<MCFixup> &Fixups) const;
78
79   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
80                               unsigned &Reg, unsigned &Imm,
81                               SmallVectorImpl<MCFixup> &Fixups) const;
82
83   /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
84   /// branch target.
85   uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
86                                   SmallVectorImpl<MCFixup> &Fixups) const;
87
88   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
89   /// operand.
90   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
91                                    SmallVectorImpl<MCFixup> &Fixups) const;
92
93   /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
94   /// operand.
95   uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
96                                    SmallVectorImpl<MCFixup> &Fixups) const;
97
98
99   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
100   /// operand as needed by load/store instructions.
101   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
102                                SmallVectorImpl<MCFixup> &Fixups) const;
103
104   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
105   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
106                                SmallVectorImpl<MCFixup> &Fixups) const {
107     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
108     switch (Mode) {
109     default: assert(0 && "Unknown addressing sub-mode!");
110     case ARM_AM::da: return 0;
111     case ARM_AM::ia: return 1;
112     case ARM_AM::db: return 2;
113     case ARM_AM::ib: return 3;
114     }
115   }
116   /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
117   ///
118   unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
119     switch (ShOpc) {
120     default: llvm_unreachable("Unknown shift opc!");
121     case ARM_AM::no_shift:
122     case ARM_AM::lsl: return 0;
123     case ARM_AM::lsr: return 1;
124     case ARM_AM::asr: return 2;
125     case ARM_AM::ror:
126     case ARM_AM::rrx: return 3;
127     }
128     return 0;
129   }
130
131   /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
132   uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
133                                SmallVectorImpl<MCFixup> &Fixups) const;
134
135   /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
136   uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
137                                      SmallVectorImpl<MCFixup> &Fixups) const;
138
139   /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
140   uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
141                                      SmallVectorImpl<MCFixup> &Fixups) const;
142
143   /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
144   uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
145                                SmallVectorImpl<MCFixup> &Fixups) const;
146
147   /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
148   uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
149                                 SmallVectorImpl<MCFixup> &Fixups) const;
150
151   /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
152   uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
153                                 SmallVectorImpl<MCFixup> &Fixups) const;
154
155   /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
156   uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
157                                 SmallVectorImpl<MCFixup> &Fixups) const;
158
159   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
160   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
161                                SmallVectorImpl<MCFixup> &Fixups) const;
162
163   /// getCCOutOpValue - Return encoding of the 's' bit.
164   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
165                            SmallVectorImpl<MCFixup> &Fixups) const {
166     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
167     // '1' respectively.
168     return MI.getOperand(Op).getReg() == ARM::CPSR;
169   }
170
171   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
172   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
173                            SmallVectorImpl<MCFixup> &Fixups) const {
174     unsigned SoImm = MI.getOperand(Op).getImm();
175     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
176     assert(SoImmVal != -1 && "Not a valid so_imm value!");
177
178     // Encode rotate_imm.
179     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
180       << ARMII::SoRotImmShift;
181
182     // Encode immed_8.
183     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
184     return Binary;
185   }
186   
187   /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
188   unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
189                            SmallVectorImpl<MCFixup> &Fixups) const {
190     unsigned SoImm = MI.getOperand(Op).getImm();
191     unsigned Encoded =  ARM_AM::getT2SOImmVal(SoImm);
192     assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
193     return Encoded;
194   }
195
196   unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
197     SmallVectorImpl<MCFixup> &Fixups) const;
198   unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
199     SmallVectorImpl<MCFixup> &Fixups) const;
200   unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
201     SmallVectorImpl<MCFixup> &Fixups) const;
202   unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
203     SmallVectorImpl<MCFixup> &Fixups) const;
204
205   /// getSORegOpValue - Return an encoded so_reg shifted register value.
206   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
207                            SmallVectorImpl<MCFixup> &Fixups) const;
208   unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
209                              SmallVectorImpl<MCFixup> &Fixups) const;
210
211   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
212                             SmallVectorImpl<MCFixup> &Fixups) const {
213     switch (MI.getOperand(Op).getImm()) {
214     default: assert (0 && "Not a valid rot_imm value!");
215     case 0:  return 0;
216     case 8:  return 1;
217     case 16: return 2;
218     case 24: return 3;
219     }
220   }
221
222   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
223                                  SmallVectorImpl<MCFixup> &Fixups) const {
224     return MI.getOperand(Op).getImm() - 1;
225   }
226
227   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
228                                    SmallVectorImpl<MCFixup> &Fixups) const {
229     return 64 - MI.getOperand(Op).getImm();
230   }
231
232   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
233                                       SmallVectorImpl<MCFixup> &Fixups) const;
234
235   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
236                                   SmallVectorImpl<MCFixup> &Fixups) const;
237   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
238                                       SmallVectorImpl<MCFixup> &Fixups) const;
239   unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
240                                         SmallVectorImpl<MCFixup> &Fixups) const;
241   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
242                                      SmallVectorImpl<MCFixup> &Fixups) const;
243
244   unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
245                                       unsigned EncodedValue) const;
246   unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
247                                       unsigned EncodedValue) const;
248   unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
249                                       unsigned EncodedValue) const;
250
251   void EmitByte(unsigned char C, raw_ostream &OS) const {
252     OS << (char)C;
253   }
254
255   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
256     // Output the constant in little endian byte order.
257     for (unsigned i = 0; i != Size; ++i) {
258       EmitByte(Val & 255, OS);
259       Val >>= 8;
260     }
261   }
262
263   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
264                          SmallVectorImpl<MCFixup> &Fixups) const;
265 };
266
267 } // end anonymous namespace
268
269 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
270                                             MCContext &Ctx) {
271   return new ARMMCCodeEmitter(TM, Ctx);
272 }
273
274 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing 
275 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
276 /// Thumb2 mode.
277 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
278                                                  unsigned EncodedValue) const {
279   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
280   if (Subtarget.isThumb2()) {
281     // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved 
282     // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
283     // set to 1111.
284     unsigned Bit24 = EncodedValue & 0x01000000;
285     unsigned Bit28 = Bit24 << 4;
286     EncodedValue &= 0xEFFFFFFF;
287     EncodedValue |= Bit28;
288     EncodedValue |= 0x0F000000;
289   }
290   
291   return EncodedValue;
292 }
293
294 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
295 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
296 /// Thumb2 mode.
297 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
298                                                  unsigned EncodedValue) const {
299   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
300   if (Subtarget.isThumb2()) {
301     EncodedValue &= 0xF0FFFFFF;
302     EncodedValue |= 0x09000000;
303   }
304   
305   return EncodedValue;
306 }
307
308 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
309 /// instructions, and rewrite them to their Thumb2 form if we are currently in 
310 /// Thumb2 mode.
311 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
312                                                  unsigned EncodedValue) const {
313   const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
314   if (Subtarget.isThumb2()) {
315     EncodedValue &= 0x00FFFFFF;
316     EncodedValue |= 0xEE000000;
317   }
318   
319   return EncodedValue;
320 }
321
322
323
324 /// getMachineOpValue - Return binary encoding of operand. If the machine
325 /// operand requires relocation, record the relocation and return zero.
326 unsigned ARMMCCodeEmitter::
327 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
328                   SmallVectorImpl<MCFixup> &Fixups) const {
329   if (MO.isReg()) {
330     unsigned Reg = MO.getReg();
331     unsigned RegNo = getARMRegisterNumbering(Reg);
332
333     // Q registers are encoded as 2x their register number.
334     switch (Reg) {
335     default:
336       return RegNo;
337     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
338     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
339     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
340     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
341       return 2 * RegNo;
342     }
343   } else if (MO.isImm()) {
344     return static_cast<unsigned>(MO.getImm());
345   } else if (MO.isFPImm()) {
346     return static_cast<unsigned>(APFloat(MO.getFPImm())
347                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
348   }
349
350   llvm_unreachable("Unable to encode MCOperand!");
351   return 0;
352 }
353
354 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
355 bool ARMMCCodeEmitter::
356 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
357                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
358   const MCOperand &MO  = MI.getOperand(OpIdx);
359   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
360
361   Reg = getARMRegisterNumbering(MO.getReg());
362
363   int32_t SImm = MO1.getImm();
364   bool isAdd = true;
365
366   // Special value for #-0
367   if (SImm == INT32_MIN)
368     SImm = 0;
369
370   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
371   if (SImm < 0) {
372     SImm = -SImm;
373     isAdd = false;
374   }
375
376   Imm = SImm;
377   return isAdd;
378 }
379
380 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
381 /// branch target.
382 uint32_t ARMMCCodeEmitter::
383 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
384                         SmallVectorImpl<MCFixup> &Fixups) const {
385   const MCOperand &MO = MI.getOperand(OpIdx);
386
387   // If the destination is an immediate, we have nothing to do.
388   if (MO.isImm()) return MO.getImm();
389   assert (MO.isExpr() && "Unexpected branch target type!");
390   const MCExpr *Expr = MO.getExpr();
391   MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
392   Fixups.push_back(MCFixup::Create(0, Expr, Kind));
393
394   // All of the information is in the fixup.
395   return 0;
396 }
397
398 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
399 uint32_t ARMMCCodeEmitter::
400 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
401                         SmallVectorImpl<MCFixup> &Fixups) const {
402   // {17-13} = reg
403   // {12}    = (U)nsigned (add == '1', sub == '0')
404   // {11-0}  = imm12
405   unsigned Reg, Imm12;
406   bool isAdd = true;
407   // If The first operand isn't a register, we have a label reference.
408   const MCOperand &MO = MI.getOperand(OpIdx);
409   if (!MO.isReg()) {
410     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
411     Imm12 = 0;
412     isAdd = false ; // 'U' bit is set as part of the fixup.
413
414     assert(MO.isExpr() && "Unexpected machine operand type!");
415     const MCExpr *Expr = MO.getExpr();
416     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
417     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
418
419     ++MCNumCPRelocations;
420   } else
421     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
422
423   uint32_t Binary = Imm12 & 0xfff;
424   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
425   if (isAdd)
426     Binary |= (1 << 12);
427   Binary |= (Reg << 13);
428   return Binary;
429 }
430
431 /// getT2AddrModeImm8s4OpValue - Return encoding info for
432 /// 'reg +/- imm8<<2' operand.
433 uint32_t ARMMCCodeEmitter::
434 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
435                         SmallVectorImpl<MCFixup> &Fixups) const {
436   // {17-13} = reg
437   // {12}    = (U)nsigned (add == '1', sub == '0')
438   // {11-0}  = imm8
439   unsigned Reg, Imm8;
440   bool isAdd = true;
441   // If The first operand isn't a register, we have a label reference.
442   const MCOperand &MO = MI.getOperand(OpIdx);
443   if (!MO.isReg()) {
444     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
445     Imm8 = 0;
446     isAdd = false ; // 'U' bit is set as part of the fixup.
447
448     assert(MO.isExpr() && "Unexpected machine operand type!");
449     const MCExpr *Expr = MO.getExpr();
450     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
451     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
452
453     ++MCNumCPRelocations;
454   } else
455     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
456
457   uint32_t Binary = (Imm8 >> 2) & 0xff;
458   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
459   if (isAdd)
460     Binary |= (1 << 9);
461   Binary |= (Reg << 9);
462   return Binary;
463 }
464
465 uint32_t ARMMCCodeEmitter::
466 getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
467                   SmallVectorImpl<MCFixup> &Fixups) const {
468   // {20-16} = imm{15-12}
469   // {11-0}  = imm{11-0}
470   const MCOperand &MO = MI.getOperand(OpIdx); 
471   if (MO.isImm()) {
472     return static_cast<unsigned>(MO.getImm());
473   } else if (const MCSymbolRefExpr *Expr = 
474              dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
475     MCFixupKind Kind;
476     switch (Expr->getKind()) {
477     default: assert(0 && "Unsupported ARMFixup");
478     case MCSymbolRefExpr::VK_ARM_HI16:
479       Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
480       break;
481     case MCSymbolRefExpr::VK_ARM_LO16:
482       Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
483       break;
484     }
485     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
486     return 0;
487   };
488   llvm_unreachable("Unsupported MCExpr type in MCOperand!");
489   return 0;
490 }
491
492 uint32_t ARMMCCodeEmitter::
493 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
494                     SmallVectorImpl<MCFixup> &Fixups) const {
495   const MCOperand &MO = MI.getOperand(OpIdx);
496   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
497   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
498   unsigned Rn = getARMRegisterNumbering(MO.getReg());
499   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
500   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
501   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
502   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
503   unsigned SBits = getShiftOp(ShOp);
504
505   // {16-13} = Rn
506   // {12}    = isAdd
507   // {11-0}  = shifter
508   //  {3-0}  = Rm
509   //  {4}    = 0
510   //  {6-5}  = type
511   //  {11-7} = imm
512   uint32_t Binary = Rm;
513   Binary |= Rn << 13;
514   Binary |= SBits << 5;
515   Binary |= ShImm << 7;
516   if (isAdd)
517     Binary |= 1 << 12;
518   return Binary;
519 }
520
521 uint32_t ARMMCCodeEmitter::
522 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
523                     SmallVectorImpl<MCFixup> &Fixups) const {
524   // {17-14}  Rn
525   // {13}     1 == imm12, 0 == Rm
526   // {12}     isAdd
527   // {11-0}   imm12/Rm
528   const MCOperand &MO = MI.getOperand(OpIdx);
529   unsigned Rn = getARMRegisterNumbering(MO.getReg());
530   uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
531   Binary |= Rn << 14;
532   return Binary;
533 }
534
535 uint32_t ARMMCCodeEmitter::
536 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
537                           SmallVectorImpl<MCFixup> &Fixups) const {
538   // {13}     1 == imm12, 0 == Rm
539   // {12}     isAdd
540   // {11-0}   imm12/Rm
541   const MCOperand &MO = MI.getOperand(OpIdx);
542   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
543   unsigned Imm = MO1.getImm();
544   bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
545   bool isReg = MO.getReg() != 0;
546   uint32_t Binary = ARM_AM::getAM2Offset(Imm);
547   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
548   if (isReg) {
549     ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
550     Binary <<= 7;                    // Shift amount is bits [11:7]
551     Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
552     Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
553   }
554   return Binary | (isAdd << 12) | (isReg << 13);
555 }
556
557 uint32_t ARMMCCodeEmitter::
558 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
559                           SmallVectorImpl<MCFixup> &Fixups) const {
560   // {9}      1 == imm8, 0 == Rm
561   // {8}      isAdd
562   // {7-4}    imm7_4/zero
563   // {3-0}    imm3_0/Rm
564   const MCOperand &MO = MI.getOperand(OpIdx);
565   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
566   unsigned Imm = MO1.getImm();
567   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
568   bool isImm = MO.getReg() == 0;
569   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
570   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
571   if (!isImm)
572     Imm8 = getARMRegisterNumbering(MO.getReg());
573   return Imm8 | (isAdd << 8) | (isImm << 9);
574 }
575
576 uint32_t ARMMCCodeEmitter::
577 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
578                     SmallVectorImpl<MCFixup> &Fixups) const {
579   // {13}     1 == imm8, 0 == Rm
580   // {12-9}   Rn
581   // {8}      isAdd
582   // {7-4}    imm7_4/zero
583   // {3-0}    imm3_0/Rm
584   const MCOperand &MO = MI.getOperand(OpIdx);
585   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
586   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
587   unsigned Rn = getARMRegisterNumbering(MO.getReg());
588   unsigned Imm = MO2.getImm();
589   bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
590   bool isImm = MO1.getReg() == 0;
591   uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
592   // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
593   if (!isImm)
594     Imm8 = getARMRegisterNumbering(MO1.getReg());
595   return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
596 }
597
598 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
599 static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
600                                     unsigned Scale) {
601   // [Rn, Rm]
602   //   {5-3} = Rm
603   //   {2-0} = Rn
604   //
605   // [Rn, #imm]
606   //   {7-3} = imm5
607   //   {2-0} = Rn
608   const MCOperand &MO = MI.getOperand(OpIdx);
609   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
610   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
611   unsigned Rn = getARMRegisterNumbering(MO.getReg());
612   unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
613   unsigned Rm = getARMRegisterNumbering(MO2.getReg());
614   return (Rm << 3) | (Imm5 << 3) | Rn;
615 }
616
617 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
618 uint32_t ARMMCCodeEmitter::
619 getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
620                      SmallVectorImpl<MCFixup> &) const {
621   return getAddrModeSOpValue(MI, OpIdx, 4);
622 }
623
624 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
625 uint32_t ARMMCCodeEmitter::
626 getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
627                      SmallVectorImpl<MCFixup> &) const {
628   return getAddrModeSOpValue(MI, OpIdx, 2);
629 }
630
631 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
632 uint32_t ARMMCCodeEmitter::
633 getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
634                      SmallVectorImpl<MCFixup> &) const {
635   return getAddrModeSOpValue(MI, OpIdx, 1);
636 }
637
638 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
639 uint32_t ARMMCCodeEmitter::
640 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
641                     SmallVectorImpl<MCFixup> &Fixups) const {
642   // {12-9} = reg
643   // {8}    = (U)nsigned (add == '1', sub == '0')
644   // {7-0}  = imm8
645   unsigned Reg, Imm8;
646   bool isAdd;
647   // If The first operand isn't a register, we have a label reference.
648   const MCOperand &MO = MI.getOperand(OpIdx);
649   if (!MO.isReg()) {
650     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
651     Imm8 = 0;
652     isAdd = false; // 'U' bit is handled as part of the fixup.
653
654     assert(MO.isExpr() && "Unexpected machine operand type!");
655     const MCExpr *Expr = MO.getExpr();
656     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
657     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
658
659     ++MCNumCPRelocations;
660   } else {
661     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
662     isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
663   }
664
665   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
666   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
667   if (isAdd)
668     Binary |= (1 << 8);
669   Binary |= (Reg << 9);
670   return Binary;
671 }
672
673 unsigned ARMMCCodeEmitter::
674 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
675                 SmallVectorImpl<MCFixup> &Fixups) const {
676   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
677   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
678   // case the imm contains the amount to shift by.
679   //
680   // {3-0} = Rm.
681   // {4}   = 1 if reg shift, 0 if imm shift
682   // {6-5} = type
683   //    If reg shift:
684   //      {11-8} = Rs
685   //      {7}    = 0
686   //    else (imm shift)
687   //      {11-7} = imm
688
689   const MCOperand &MO  = MI.getOperand(OpIdx);
690   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
691   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
692   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
693
694   // Encode Rm.
695   unsigned Binary = getARMRegisterNumbering(MO.getReg());
696
697   // Encode the shift opcode.
698   unsigned SBits = 0;
699   unsigned Rs = MO1.getReg();
700   if (Rs) {
701     // Set shift operand (bit[7:4]).
702     // LSL - 0001
703     // LSR - 0011
704     // ASR - 0101
705     // ROR - 0111
706     // RRX - 0110 and bit[11:8] clear.
707     switch (SOpc) {
708     default: llvm_unreachable("Unknown shift opc!");
709     case ARM_AM::lsl: SBits = 0x1; break;
710     case ARM_AM::lsr: SBits = 0x3; break;
711     case ARM_AM::asr: SBits = 0x5; break;
712     case ARM_AM::ror: SBits = 0x7; break;
713     case ARM_AM::rrx: SBits = 0x6; break;
714     }
715   } else {
716     // Set shift operand (bit[6:4]).
717     // LSL - 000
718     // LSR - 010
719     // ASR - 100
720     // ROR - 110
721     switch (SOpc) {
722     default: llvm_unreachable("Unknown shift opc!");
723     case ARM_AM::lsl: SBits = 0x0; break;
724     case ARM_AM::lsr: SBits = 0x2; break;
725     case ARM_AM::asr: SBits = 0x4; break;
726     case ARM_AM::ror: SBits = 0x6; break;
727     }
728   }
729
730   Binary |= SBits << 4;
731   if (SOpc == ARM_AM::rrx)
732     return Binary;
733
734   // Encode the shift operation Rs or shift_imm (except rrx).
735   if (Rs) {
736     // Encode Rs bit[11:8].
737     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
738     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
739   }
740
741   // Encode shift_imm bit[11:7].
742   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
743 }
744
745 unsigned ARMMCCodeEmitter::
746 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
747                 SmallVectorImpl<MCFixup> &Fixups) const {
748   const MCOperand &MO1 = MI.getOperand(OpNum);
749   const MCOperand &MO2 = MI.getOperand(OpNum+1);
750   const MCOperand &MO3 = MI.getOperand(OpNum+2);                 
751   
752   // Encoded as [Rn, Rm, imm].
753   // FIXME: Needs fixup support.
754   unsigned Value = getARMRegisterNumbering(MO1.getReg());
755   Value <<= 4;
756   Value |= getARMRegisterNumbering(MO2.getReg());
757   Value <<= 2;
758   Value |= MO3.getImm();
759   
760   return Value;
761 }
762
763 unsigned ARMMCCodeEmitter::
764 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
765                          SmallVectorImpl<MCFixup> &Fixups) const {
766   const MCOperand &MO1 = MI.getOperand(OpNum);
767   const MCOperand &MO2 = MI.getOperand(OpNum+1);
768
769   // FIXME: Needs fixup support.
770   unsigned Value = getARMRegisterNumbering(MO1.getReg());
771   
772   // Even though the immediate is 8 bits long, we need 9 bits in order
773   // to represent the (inverse of the) sign bit.
774   Value <<= 9;
775   int32_t tmp = (int32_t)MO2.getImm();
776   if (tmp < 0)
777     tmp = abs(tmp);
778   else
779     Value |= 256; // Set the ADD bit
780   Value |= tmp & 255;
781   return Value;
782 }
783
784 unsigned ARMMCCodeEmitter::
785 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
786                          SmallVectorImpl<MCFixup> &Fixups) const {
787   const MCOperand &MO1 = MI.getOperand(OpNum);
788
789   // FIXME: Needs fixup support.
790   unsigned Value = 0;
791   int32_t tmp = (int32_t)MO1.getImm();
792   if (tmp < 0)
793     tmp = abs(tmp);
794   else
795     Value |= 256; // Set the ADD bit
796   Value |= tmp & 255;
797   return Value;
798 }
799
800 unsigned ARMMCCodeEmitter::
801 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
802                          SmallVectorImpl<MCFixup> &Fixups) const {
803   const MCOperand &MO1 = MI.getOperand(OpNum);
804
805   // FIXME: Needs fixup support.
806   unsigned Value = 0;
807   int32_t tmp = (int32_t)MO1.getImm();
808   if (tmp < 0)
809     tmp = abs(tmp);
810   else
811     Value |= 4096; // Set the ADD bit
812   Value |= tmp & 4095;
813   return Value;
814 }
815
816 unsigned ARMMCCodeEmitter::
817 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
818                 SmallVectorImpl<MCFixup> &Fixups) const {
819   // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
820   // shifted. The second is the amount to shift by.
821   //
822   // {3-0} = Rm.
823   // {4}   = 0
824   // {6-5} = type
825   // {11-7} = imm
826
827   const MCOperand &MO  = MI.getOperand(OpIdx);
828   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
829   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
830
831   // Encode Rm.
832   unsigned Binary = getARMRegisterNumbering(MO.getReg());
833
834   // Encode the shift opcode.
835   unsigned SBits = 0;
836   // Set shift operand (bit[6:4]).
837   // LSL - 000
838   // LSR - 010
839   // ASR - 100
840   // ROR - 110
841   switch (SOpc) {
842   default: llvm_unreachable("Unknown shift opc!");
843   case ARM_AM::lsl: SBits = 0x0; break;
844   case ARM_AM::lsr: SBits = 0x2; break;
845   case ARM_AM::asr: SBits = 0x4; break;
846   case ARM_AM::ror: SBits = 0x6; break;
847   }
848
849   Binary |= SBits << 4;
850   if (SOpc == ARM_AM::rrx)
851     return Binary;
852
853   // Encode shift_imm bit[11:7].
854   return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
855 }
856
857 unsigned ARMMCCodeEmitter::
858 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
859                                SmallVectorImpl<MCFixup> &Fixups) const {
860   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
861   // msb of the mask.
862   const MCOperand &MO = MI.getOperand(Op);
863   uint32_t v = ~MO.getImm();
864   uint32_t lsb = CountTrailingZeros_32(v);
865   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
866   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
867   return lsb | (msb << 5);
868 }
869
870 unsigned ARMMCCodeEmitter::
871 getRegisterListOpValue(const MCInst &MI, unsigned Op,
872                        SmallVectorImpl<MCFixup> &Fixups) const {
873   // VLDM/VSTM:
874   //   {12-8} = Vd
875   //   {7-0}  = Number of registers
876   //
877   // LDM/STM:
878   //   {15-0}  = Bitfield of GPRs.
879   unsigned Reg = MI.getOperand(Op).getReg();
880   bool SPRRegs = ARM::SPRRegClass.contains(Reg);
881   bool DPRRegs = ARM::DPRRegClass.contains(Reg);
882
883   unsigned Binary = 0;
884
885   if (SPRRegs || DPRRegs) {
886     // VLDM/VSTM
887     unsigned RegNo = getARMRegisterNumbering(Reg);
888     unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
889     Binary |= (RegNo & 0x1f) << 8;
890     if (SPRRegs)
891       Binary |= NumRegs;
892     else
893       Binary |= NumRegs * 2;
894   } else {
895     for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
896       unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
897       Binary |= 1 << RegNo;
898     }
899   }
900
901   return Binary;
902 }
903
904 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
905 /// with the alignment operand.
906 unsigned ARMMCCodeEmitter::
907 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
908                            SmallVectorImpl<MCFixup> &Fixups) const {
909   const MCOperand &Reg = MI.getOperand(Op);
910   const MCOperand &Imm = MI.getOperand(Op + 1);
911
912   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
913   unsigned Align = 0;
914
915   switch (Imm.getImm()) {
916   default: break;
917   case 2:
918   case 4:
919   case 8:  Align = 0x01; break;
920   case 16: Align = 0x02; break;
921   case 32: Align = 0x03; break;
922   }
923
924   return RegNo | (Align << 4);
925 }
926
927 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
928 /// alignment operand for use in VLD-dup instructions.  This is the same as
929 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
930 /// different for VLD4-dup.
931 unsigned ARMMCCodeEmitter::
932 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
933                               SmallVectorImpl<MCFixup> &Fixups) const {
934   const MCOperand &Reg = MI.getOperand(Op);
935   const MCOperand &Imm = MI.getOperand(Op + 1);
936
937   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
938   unsigned Align = 0;
939
940   switch (Imm.getImm()) {
941   default: break;
942   case 2:
943   case 4:
944   case 8:  Align = 0x01; break;
945   case 16: Align = 0x03; break;
946   }
947
948   return RegNo | (Align << 4);
949 }
950
951 unsigned ARMMCCodeEmitter::
952 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
953                           SmallVectorImpl<MCFixup> &Fixups) const {
954   const MCOperand &MO = MI.getOperand(Op);
955   if (MO.getReg() == 0) return 0x0D;
956   return MO.getReg();
957 }
958
959 void ARMMCCodeEmitter::
960 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
961                   SmallVectorImpl<MCFixup> &Fixups) const {
962   // Pseudo instructions don't get encoded.
963   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
964   uint64_t TSFlags = Desc.TSFlags;
965   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
966     return;
967   int Size;
968   // Basic size info comes from the TSFlags field.
969   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
970   default: llvm_unreachable("Unexpected instruction size!");
971   case ARMII::Size2Bytes: Size = 2; break;
972   case ARMII::Size4Bytes: Size = 4; break;
973   }
974   EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
975   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
976 }
977
978 #include "ARMGenMCCodeEmitter.inc"