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