Provide correct encodings for NEON vcvt, which has its own special immediate encoding
[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 "ARMInstrInfo.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
27 namespace {
28 class ARMMCCodeEmitter : public MCCodeEmitter {
29   ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
30   void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
31   const TargetMachine &TM;
32   const TargetInstrInfo &TII;
33   MCContext &Ctx;
34
35 public:
36   ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37     : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
38   }
39
40   ~ARMMCCodeEmitter() {}
41
42   unsigned getMachineSoImmOpValue(unsigned SoImm) const;
43
44   // getBinaryCodeForInstr - TableGen'erated function for getting the
45   // binary encoding for an instruction.
46   unsigned getBinaryCodeForInstr(const MCInst &MI) const;
47
48   /// getMachineOpValue - Return binary encoding of operand. If the machine
49   /// operand requires relocation, record the relocation and return zero.
50   unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
51
52   /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
53   /// operand.
54   unsigned getAddrModeImm12OpValue(const MCInst &MI, unsigned Op) const;
55
56   /// getCCOutOpValue - Return encoding of the 's' bit.
57   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
58     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
59     // '1' respectively.
60     return MI.getOperand(Op).getReg() == ARM::CPSR;
61   }
62
63   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
64   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
65     unsigned SoImm = MI.getOperand(Op).getImm();
66     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
67     assert(SoImmVal != -1 && "Not a valid so_imm value!");
68
69     // Encode rotate_imm.
70     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
71       << ARMII::SoRotImmShift;
72
73     // Encode immed_8.
74     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
75     return Binary;
76   }
77
78   /// getSORegOpValue - Return an encoded so_reg shifted register value.
79   unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
80
81   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
82     switch (MI.getOperand(Op).getImm()) {
83     default: assert (0 && "Not a valid rot_imm value!");
84     case 0:  return 0;
85     case 8:  return 1;
86     case 16: return 2;
87     case 24: return 3;
88     }
89   }
90
91   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
92     return MI.getOperand(Op).getImm() - 1;
93   }
94   
95   unsigned getNEONVcvtImm32(const MCInst &MI, unsigned Op) const {
96     return 64 - MI.getOperand(Op).getImm();
97   }
98
99   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op) const;
100
101   unsigned getNumFixupKinds() const {
102     assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
103     return 0;
104   }
105
106   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
107     static MCFixupKindInfo rtn;
108     assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
109     return rtn;
110   }
111
112   void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
113     OS << (char)C;
114     ++CurByte;
115   }
116
117   void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
118                     raw_ostream &OS) const {
119     // Output the constant in little endian byte order.
120     for (unsigned i = 0; i != Size; ++i) {
121       EmitByte(Val & 255, CurByte, OS);
122       Val >>= 8;
123     }
124   }
125
126   void EmitImmediate(const MCOperand &Disp,
127                      unsigned ImmSize, MCFixupKind FixupKind,
128                      unsigned &CurByte, raw_ostream &OS,
129                      SmallVectorImpl<MCFixup> &Fixups,
130                      int ImmOffset = 0) const;
131
132   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
133                          SmallVectorImpl<MCFixup> &Fixups) const;
134 };
135
136 } // end anonymous namespace
137
138 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
139                                              TargetMachine &TM,
140                                              MCContext &Ctx) {
141   return new ARMMCCodeEmitter(TM, Ctx);
142 }
143
144 void ARMMCCodeEmitter::
145 EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
146               unsigned &CurByte, raw_ostream &OS,
147               SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
148   assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
149 }
150
151 /// getMachineOpValue - Return binary encoding of operand. If the machine
152 /// operand requires relocation, record the relocation and return zero.
153 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
154                                              const MCOperand &MO) const {
155   if (MO.isReg()) {
156     unsigned regno = getARMRegisterNumbering(MO.getReg());
157     
158     // Q registers are encodes as 2x their register number.
159     switch (MO.getReg()) {
160       case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
161       case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
162       case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
163       case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
164         return 2 * regno;
165       default:
166         return regno;
167     }
168   } else if (MO.isImm()) {
169     return static_cast<unsigned>(MO.getImm());
170   } else if (MO.isFPImm()) {
171     return static_cast<unsigned>(APFloat(MO.getFPImm())
172                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
173   } else {
174 #ifndef NDEBUG
175     errs() << MO;
176 #endif
177     llvm_unreachable(0);
178   }
179   return 0;
180 }
181
182 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
183 /// operand.
184 unsigned ARMMCCodeEmitter::getAddrModeImm12OpValue(const MCInst &MI,
185                                                    unsigned OpIdx) const {
186   // {17-13} = reg
187   // {12}    = (U)nsigned (add == '1', sub == '0')
188   // {11-0}  = imm12
189   const MCOperand &MO  = MI.getOperand(OpIdx);
190   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
191   unsigned Reg = getARMRegisterNumbering(MO.getReg());
192   int32_t Imm12 = MO1.getImm();
193   uint32_t Binary;
194   Binary = Imm12 & 0xfff;
195   if (Imm12 >= 0)
196     Binary |= (1 << 12);
197   Binary |= (Reg << 13);
198   return Binary;
199 }
200
201 unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
202                                            unsigned OpIdx) const {
203   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
204   // to be shifted. The second is either Rs, the amount to shift by, or
205   // reg0 in which case the imm contains the amount to shift by.
206   // {3-0} = Rm.
207   // {4} = 1 if reg shift, 0 if imm shift
208   // {6-5} = type
209   //    If reg shift:
210   //      {7} = 0
211   //      {11-8} = Rs
212   //    else (imm shift)
213   //      {11-7} = imm
214
215   const MCOperand &MO  = MI.getOperand(OpIdx);
216   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
217   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
218   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
219
220   // Encode Rm.
221   unsigned Binary = getARMRegisterNumbering(MO.getReg());
222
223   // Encode the shift opcode.
224   unsigned SBits = 0;
225   unsigned Rs = MO1.getReg();
226   if (Rs) {
227     // Set shift operand (bit[7:4]).
228     // LSL - 0001
229     // LSR - 0011
230     // ASR - 0101
231     // ROR - 0111
232     // RRX - 0110 and bit[11:8] clear.
233     switch (SOpc) {
234     default: llvm_unreachable("Unknown shift opc!");
235     case ARM_AM::lsl: SBits = 0x1; break;
236     case ARM_AM::lsr: SBits = 0x3; break;
237     case ARM_AM::asr: SBits = 0x5; break;
238     case ARM_AM::ror: SBits = 0x7; break;
239     case ARM_AM::rrx: SBits = 0x6; break;
240     }
241   } else {
242     // Set shift operand (bit[6:4]).
243     // LSL - 000
244     // LSR - 010
245     // ASR - 100
246     // ROR - 110
247     switch (SOpc) {
248     default: llvm_unreachable("Unknown shift opc!");
249     case ARM_AM::lsl: SBits = 0x0; break;
250     case ARM_AM::lsr: SBits = 0x2; break;
251     case ARM_AM::asr: SBits = 0x4; break;
252     case ARM_AM::ror: SBits = 0x6; break;
253     }
254   }
255   Binary |= SBits << 4;
256   if (SOpc == ARM_AM::rrx)
257     return Binary;
258
259   // Encode the shift operation Rs or shift_imm (except rrx).
260   if (Rs) {
261     // Encode Rs bit[11:8].
262     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
263     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
264   }
265
266   // Encode shift_imm bit[11:7].
267   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
268 }
269
270 unsigned ARMMCCodeEmitter::getBitfieldInvertedMaskOpValue(const MCInst &MI,
271                                                           unsigned Op) const {
272   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
273   // msb of the mask.
274   const MCOperand &MO = MI.getOperand(Op);
275   uint32_t v = ~MO.getImm();
276   uint32_t lsb = CountTrailingZeros_32(v);
277   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
278   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
279   return lsb | (msb << 5);
280 }
281
282 void ARMMCCodeEmitter::
283 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
284                   SmallVectorImpl<MCFixup> &Fixups) const {
285   unsigned Opcode = MI.getOpcode();
286   const TargetInstrDesc &Desc = TII.get(Opcode);
287   uint64_t TSFlags = Desc.TSFlags;
288   // Keep track of the current byte being emitted.
289   unsigned CurByte = 0;
290
291   // Pseudo instructions don't get encoded.
292   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
293     return;
294
295   ++MCNumEmitted;  // Keep track of the # of mi's emitted
296   unsigned Value = getBinaryCodeForInstr(MI);
297   switch (Opcode) {
298   default: break;
299   }
300   EmitConstant(Value, 4, CurByte, OS);
301 }
302
303 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
304 // be able to generate code emitter helpers for either variant, like it
305 // does for the AsmWriter.
306 #define ARMCodeEmitter ARMMCCodeEmitter
307 #define MachineInstr MCInst
308 #include "ARMGenCodeEmitter.inc"
309 #undef ARMCodeEmitter
310 #undef MachineInstr