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