ARM LDM encoding for the mode (ia, ib, da, db) operand.
[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 "arm-emitter"
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 2; }
45
46   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47     const static MCFixupKindInfo Infos[] = {
48       { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
49       { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
50     };
51
52     if (Kind < FirstTargetFixupKind)
53       return MCCodeEmitter::getFixupKindInfo(Kind);
54
55     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
56            "Invalid kind!");
57     return Infos[Kind - FirstTargetFixupKind];
58   }
59   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
60
61   // getBinaryCodeForInstr - TableGen'erated function for getting the
62   // binary encoding for an instruction.
63   unsigned getBinaryCodeForInstr(const MCInst &MI,
64                                  SmallVectorImpl<MCFixup> &Fixups) const;
65
66   /// getMachineOpValue - Return binary encoding of operand. If the machine
67   /// operand requires relocation, record the relocation and return zero.
68   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
69                              SmallVectorImpl<MCFixup> &Fixups) const;
70
71   bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
72                               unsigned &Reg, unsigned &Imm,
73                               SmallVectorImpl<MCFixup> &Fixups) const;
74
75   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
76   /// operand.
77   uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
78                                    SmallVectorImpl<MCFixup> &Fixups) const;
79
80   /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
81   /// operand as needed by load/store instructions.
82   uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
83                                SmallVectorImpl<MCFixup> &Fixups) const;
84
85   /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
86   uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
87                                SmallVectorImpl<MCFixup> &Fixups) const {
88     ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
89     switch (Mode) {
90     default: assert(0 && "Unknown addressing sub-mode!");
91     case ARM_AM::da: return 0;
92     case ARM_AM::ia: return 1;
93     case ARM_AM::db: return 2;
94     case ARM_AM::ib: return 3;
95     }
96   }
97
98   /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
99   uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
100                                SmallVectorImpl<MCFixup> &Fixups) const;
101
102   /// getCCOutOpValue - Return encoding of the 's' bit.
103   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
104                            SmallVectorImpl<MCFixup> &Fixups) const {
105     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
106     // '1' respectively.
107     return MI.getOperand(Op).getReg() == ARM::CPSR;
108   }
109
110   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
111   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
112                            SmallVectorImpl<MCFixup> &Fixups) const {
113     unsigned SoImm = MI.getOperand(Op).getImm();
114     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
115     assert(SoImmVal != -1 && "Not a valid so_imm value!");
116
117     // Encode rotate_imm.
118     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
119       << ARMII::SoRotImmShift;
120
121     // Encode immed_8.
122     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
123     return Binary;
124   }
125
126   /// getSORegOpValue - Return an encoded so_reg shifted register value.
127   unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
128                            SmallVectorImpl<MCFixup> &Fixups) const;
129
130   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
131                             SmallVectorImpl<MCFixup> &Fixups) const {
132     switch (MI.getOperand(Op).getImm()) {
133     default: assert (0 && "Not a valid rot_imm value!");
134     case 0:  return 0;
135     case 8:  return 1;
136     case 16: return 2;
137     case 24: return 3;
138     }
139   }
140
141   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
142                                  SmallVectorImpl<MCFixup> &Fixups) const {
143     return MI.getOperand(Op).getImm() - 1;
144   }
145
146   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
147                                    SmallVectorImpl<MCFixup> &Fixups) const {
148     return 64 - MI.getOperand(Op).getImm();
149   }
150
151   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
152                                       SmallVectorImpl<MCFixup> &Fixups) const;
153
154   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
155                                   SmallVectorImpl<MCFixup> &Fixups) const;
156   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
157                                       SmallVectorImpl<MCFixup> &Fixups) const;
158   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
159                                      SmallVectorImpl<MCFixup> &Fixups) const;
160
161   void EmitByte(unsigned char C, raw_ostream &OS) const {
162     OS << (char)C;
163   }
164
165   void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
166     // Output the constant in little endian byte order.
167     for (unsigned i = 0; i != Size; ++i) {
168       EmitByte(Val & 255, OS);
169       Val >>= 8;
170     }
171   }
172
173   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
174                          SmallVectorImpl<MCFixup> &Fixups) const;
175 };
176
177 } // end anonymous namespace
178
179 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
180                                             MCContext &Ctx) {
181   return new ARMMCCodeEmitter(TM, Ctx);
182 }
183
184 /// getMachineOpValue - Return binary encoding of operand. If the machine
185 /// operand requires relocation, record the relocation and return zero.
186 unsigned ARMMCCodeEmitter::
187 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
188                   SmallVectorImpl<MCFixup> &Fixups) const {
189   if (MO.isReg()) {
190     unsigned Reg = MO.getReg();
191     unsigned RegNo = getARMRegisterNumbering(Reg);
192
193     // Q registers are encodes as 2x their register number.
194     switch (Reg) {
195     default:
196       return RegNo;
197     case ARM::Q0:  case ARM::Q1:  case ARM::Q2:  case ARM::Q3:
198     case ARM::Q4:  case ARM::Q5:  case ARM::Q6:  case ARM::Q7:
199     case ARM::Q8:  case ARM::Q9:  case ARM::Q10: case ARM::Q11:
200     case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
201       return 2 * RegNo;
202     }
203   } else if (MO.isImm()) {
204     return static_cast<unsigned>(MO.getImm());
205   } else if (MO.isFPImm()) {
206     return static_cast<unsigned>(APFloat(MO.getFPImm())
207                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
208   }
209
210 #ifndef NDEBUG
211   errs() << MO;
212 #endif
213   llvm_unreachable(0);
214   return 0;
215 }
216
217 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
218 bool ARMMCCodeEmitter::
219 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
220                        unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
221   const MCOperand &MO  = MI.getOperand(OpIdx);
222   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
223
224   Reg = getARMRegisterNumbering(MO.getReg());
225
226   int32_t SImm = MO1.getImm();
227   bool isAdd = true;
228
229   // Special value for #-0
230   if (SImm == INT32_MIN)
231     SImm = 0;
232
233   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
234   if (SImm < 0) {
235     SImm = -SImm;
236     isAdd = false;
237   }
238
239   Imm = SImm;
240   return isAdd;
241 }
242
243 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
244 uint32_t ARMMCCodeEmitter::
245 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
246                         SmallVectorImpl<MCFixup> &Fixups) const {
247   // {17-13} = reg
248   // {12}    = (U)nsigned (add == '1', sub == '0')
249   // {11-0}  = imm12
250   unsigned Reg, Imm12;
251   bool isAdd = true;
252   // If The first operand isn't a register, we have a label reference.
253   const MCOperand &MO = MI.getOperand(OpIdx);
254   if (!MO.isReg()) {
255     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
256     Imm12 = 0;
257
258     assert(MO.isExpr() && "Unexpected machine operand type!");
259     const MCExpr *Expr = MO.getExpr();
260     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
261     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
262
263     ++MCNumCPRelocations;
264   } else
265     isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
266
267   uint32_t Binary = Imm12 & 0xfff;
268   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
269   if (isAdd)
270     Binary |= (1 << 12);
271   Binary |= (Reg << 13);
272   return Binary;
273 }
274
275 uint32_t ARMMCCodeEmitter::
276 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
277                     SmallVectorImpl<MCFixup> &Fixups) const {
278   const MCOperand &MO = MI.getOperand(OpIdx);
279   const MCOperand &MO1 = MI.getOperand(OpIdx+1);
280   const MCOperand &MO2 = MI.getOperand(OpIdx+2);
281   unsigned Rn = getARMRegisterNumbering(MO.getReg());
282   unsigned Rm = getARMRegisterNumbering(MO1.getReg());
283   ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
284   unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
285   bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
286   unsigned SBits;
287   // LSL - 00
288   // LSR - 01
289   // ASR - 10
290   // ROR - 11
291   switch (ShOp) {
292   default: llvm_unreachable("Unknown shift opc!");
293   case ARM_AM::no_shift:
294     assert(ShImm == 0 && "Non-zero shift amount with no shift type!");
295     // fall through
296   case ARM_AM::lsl: SBits = 0x0; break;
297   case ARM_AM::lsr: SBits = 0x1; break;
298   case ARM_AM::asr: SBits = 0x2; break;
299   case ARM_AM::ror: SBits = 0x3; break;
300   }
301
302   // {16-13} = Rn
303   // {12}    = isAdd
304   // {11-0}  = shifter
305   //  {3-0}  = Rm
306   //  {4}    = 0
307   //  {6-5}  = type
308   //  {11-7} = imm
309   int64_t Binary = Rm;
310   Binary |= Rn << 13;
311   Binary |= SBits << 5;
312   Binary |= ShImm << 7;
313   if (isAdd)
314     Binary |= 1 << 12;
315   return Binary;
316 }
317
318 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
319 uint32_t ARMMCCodeEmitter::
320 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
321                     SmallVectorImpl<MCFixup> &Fixups) const {
322   // {12-9} = reg
323   // {8}    = (U)nsigned (add == '1', sub == '0')
324   // {7-0}  = imm8
325   unsigned Reg, Imm8;
326   // If The first operand isn't a register, we have a label reference.
327   const MCOperand &MO = MI.getOperand(OpIdx);
328   if (!MO.isReg()) {
329     Reg = getARMRegisterNumbering(ARM::PC);   // Rn is PC.
330     Imm8 = 0;
331
332     assert(MO.isExpr() && "Unexpected machine operand type!");
333     const MCExpr *Expr = MO.getExpr();
334     MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
335     Fixups.push_back(MCFixup::Create(0, Expr, Kind));
336
337     ++MCNumCPRelocations;
338   } else
339     EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
340
341   uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
342   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
343   if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
344     Binary |= (1 << 8);
345   Binary |= (Reg << 9);
346   return Binary;
347 }
348
349 unsigned ARMMCCodeEmitter::
350 getSORegOpValue(const MCInst &MI, unsigned OpIdx,
351                 SmallVectorImpl<MCFixup> &Fixups) const {
352   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
353   // shifted. The second is either Rs, the amount to shift by, or reg0 in which
354   // case the imm contains the amount to shift by.
355   //
356   // {3-0} = Rm.
357   // {4}   = 1 if reg shift, 0 if imm shift
358   // {6-5} = type
359   //    If reg shift:
360   //      {11-8} = Rs
361   //      {7}    = 0
362   //    else (imm shift)
363   //      {11-7} = imm
364
365   const MCOperand &MO  = MI.getOperand(OpIdx);
366   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
367   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
368   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
369
370   // Encode Rm.
371   unsigned Binary = getARMRegisterNumbering(MO.getReg());
372
373   // Encode the shift opcode.
374   unsigned SBits = 0;
375   unsigned Rs = MO1.getReg();
376   if (Rs) {
377     // Set shift operand (bit[7:4]).
378     // LSL - 0001
379     // LSR - 0011
380     // ASR - 0101
381     // ROR - 0111
382     // RRX - 0110 and bit[11:8] clear.
383     switch (SOpc) {
384     default: llvm_unreachable("Unknown shift opc!");
385     case ARM_AM::lsl: SBits = 0x1; break;
386     case ARM_AM::lsr: SBits = 0x3; break;
387     case ARM_AM::asr: SBits = 0x5; break;
388     case ARM_AM::ror: SBits = 0x7; break;
389     case ARM_AM::rrx: SBits = 0x6; break;
390     }
391   } else {
392     // Set shift operand (bit[6:4]).
393     // LSL - 000
394     // LSR - 010
395     // ASR - 100
396     // ROR - 110
397     switch (SOpc) {
398     default: llvm_unreachable("Unknown shift opc!");
399     case ARM_AM::lsl: SBits = 0x0; break;
400     case ARM_AM::lsr: SBits = 0x2; break;
401     case ARM_AM::asr: SBits = 0x4; break;
402     case ARM_AM::ror: SBits = 0x6; break;
403     }
404   }
405
406   Binary |= SBits << 4;
407   if (SOpc == ARM_AM::rrx)
408     return Binary;
409
410   // Encode the shift operation Rs or shift_imm (except rrx).
411   if (Rs) {
412     // Encode Rs bit[11:8].
413     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
414     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
415   }
416
417   // Encode shift_imm bit[11:7].
418   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
419 }
420
421 unsigned ARMMCCodeEmitter::
422 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
423                                SmallVectorImpl<MCFixup> &Fixups) const {
424   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
425   // msb of the mask.
426   const MCOperand &MO = MI.getOperand(Op);
427   uint32_t v = ~MO.getImm();
428   uint32_t lsb = CountTrailingZeros_32(v);
429   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
430   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
431   return lsb | (msb << 5);
432 }
433
434 unsigned ARMMCCodeEmitter::
435 getRegisterListOpValue(const MCInst &MI, unsigned Op,
436                        SmallVectorImpl<MCFixup> &Fixups) const {
437   // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
438   // register in the list, set the corresponding bit.
439   unsigned Binary = 0;
440   for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
441     unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
442     Binary |= 1 << regno;
443   }
444   return Binary;
445 }
446
447 unsigned ARMMCCodeEmitter::
448 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
449                            SmallVectorImpl<MCFixup> &Fixups) const {
450   const MCOperand &Reg = MI.getOperand(Op);
451   const MCOperand &Imm = MI.getOperand(Op + 1);
452
453   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
454   unsigned Align = 0;
455
456   switch (Imm.getImm()) {
457   default: break;
458   case 2:
459   case 4:
460   case 8:  Align = 0x01; break;
461   case 16: Align = 0x02; break;
462   case 32: Align = 0x03; break;
463   }
464
465   return RegNo | (Align << 4);
466 }
467
468 unsigned ARMMCCodeEmitter::
469 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
470                           SmallVectorImpl<MCFixup> &Fixups) const {
471   const MCOperand &MO = MI.getOperand(Op);
472   if (MO.getReg() == 0) return 0x0D;
473   return MO.getReg();
474 }
475
476 void ARMMCCodeEmitter::
477 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
478                   SmallVectorImpl<MCFixup> &Fixups) const {
479   // Pseudo instructions don't get encoded.
480   const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
481   if ((Desc.TSFlags & ARMII::FormMask) == ARMII::Pseudo)
482     return;
483
484   EmitConstant(getBinaryCodeForInstr(MI, Fixups), 4, OS);
485   ++MCNumEmitted;  // Keep track of the # of mi's emitted.
486 }
487
488 #include "ARMGenMCCodeEmitter.inc"