bc16fe04c389876b0931257430f13b7ab19184ea
[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   /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
53   uint32_t getAddrModeImmOpValue(const MCInst &MI, unsigned Op) const;
54
55   /// getCCOutOpValue - Return encoding of the 's' bit.
56   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
57     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
58     // '1' respectively.
59     return MI.getOperand(Op).getReg() == ARM::CPSR;
60   }
61
62   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
63   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
64     unsigned SoImm = MI.getOperand(Op).getImm();
65     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
66     assert(SoImmVal != -1 && "Not a valid so_imm value!");
67
68     // Encode rotate_imm.
69     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
70       << ARMII::SoRotImmShift;
71
72     // Encode immed_8.
73     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
74     return Binary;
75   }
76
77   /// getSORegOpValue - Return an encoded so_reg shifted register value.
78   unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
79
80   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
81     switch (MI.getOperand(Op).getImm()) {
82     default: assert (0 && "Not a valid rot_imm value!");
83     case 0:  return 0;
84     case 8:  return 1;
85     case 16: return 2;
86     case 24: return 3;
87     }
88   }
89
90   unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
91     return MI.getOperand(Op).getImm() - 1;
92   }
93
94   unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op) const {
95     return 64 - MI.getOperand(Op).getImm();
96   }
97
98   unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op) const;
99
100   unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op) const;
101   unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op) const;
102   unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op) const;
103
104   unsigned getNumFixupKinds() const {
105     assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
106     return 0;
107   }
108
109   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
110     static MCFixupKindInfo rtn;
111     assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
112     return rtn;
113   }
114
115   void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
116     OS << (char)C;
117     ++CurByte;
118   }
119
120   void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
121                     raw_ostream &OS) const {
122     // Output the constant in little endian byte order.
123     for (unsigned i = 0; i != Size; ++i) {
124       EmitByte(Val & 255, CurByte, OS);
125       Val >>= 8;
126     }
127   }
128
129   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
130                          SmallVectorImpl<MCFixup> &Fixups) const;
131 };
132
133 } // end anonymous namespace
134
135 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
136                                              TargetMachine &TM,
137                                              MCContext &Ctx) {
138   return new ARMMCCodeEmitter(TM, Ctx);
139 }
140
141 /// getMachineOpValue - Return binary encoding of operand. If the machine
142 /// operand requires relocation, record the relocation and return zero.
143 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
144                                              const MCOperand &MO) const {
145   if (MO.isReg()) {
146     unsigned regno = getARMRegisterNumbering(MO.getReg());
147
148     // Q registers are encodes as 2x their register number.
149     switch (MO.getReg()) {
150       case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
151       case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
152       case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
153       case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
154         return 2 * regno;
155       default:
156         return regno;
157     }
158   } else if (MO.isImm()) {
159     return static_cast<unsigned>(MO.getImm());
160   } else if (MO.isFPImm()) {
161     return static_cast<unsigned>(APFloat(MO.getFPImm())
162                      .bitcastToAPInt().getHiBits(32).getLimitedValue());
163   } else {
164 #ifndef NDEBUG
165     errs() << MO;
166 #endif
167     llvm_unreachable(0);
168   }
169   return 0;
170 }
171
172 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
173 uint32_t ARMMCCodeEmitter::getAddrModeImmOpValue(const MCInst &MI,
174                                                  unsigned OpIdx) const {
175   // {20-17} = reg
176   // {16}    = (U)nsigned (add == '1', sub == '0')
177   // {15-0}  = imm
178   const MCOperand &MO  = MI.getOperand(OpIdx);
179   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
180   uint32_t Binary = 0;
181
182   // If The first operand isn't a register, we have a label reference.
183   if (!MO.isReg()) {
184     Binary |= ARM::PC << 17;     // Rn is PC.
185     // FIXME: Add a fixup referencing the label.
186     return Binary;
187   }
188
189   unsigned Reg = getARMRegisterNumbering(MO.getReg());
190   int32_t Imm = MO1.getImm();
191   bool isAdd = Imm >= 0;
192
193   // Special value for #-0
194   if (Imm == INT32_MIN)
195     Imm = 0;
196
197   // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
198   if (Imm < 0) Imm = -Imm;
199
200   Binary = Imm & 0xffff;
201   if (isAdd)
202     Binary |= (1 << 16);
203   Binary |= (Reg << 17);
204   return Binary;
205 }
206
207 unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
208                                            unsigned OpIdx) const {
209   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
210   // to be shifted. The second is either Rs, the amount to shift by, or
211   // reg0 in which case the imm contains the amount to shift by.
212   // {3-0} = Rm.
213   // {4} = 1 if reg shift, 0 if imm shift
214   // {6-5} = type
215   //    If reg shift:
216   //      {7} = 0
217   //      {11-8} = Rs
218   //    else (imm shift)
219   //      {11-7} = imm
220
221   const MCOperand &MO  = MI.getOperand(OpIdx);
222   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
223   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
224   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
225
226   // Encode Rm.
227   unsigned Binary = getARMRegisterNumbering(MO.getReg());
228
229   // Encode the shift opcode.
230   unsigned SBits = 0;
231   unsigned Rs = MO1.getReg();
232   if (Rs) {
233     // Set shift operand (bit[7:4]).
234     // LSL - 0001
235     // LSR - 0011
236     // ASR - 0101
237     // ROR - 0111
238     // RRX - 0110 and bit[11:8] clear.
239     switch (SOpc) {
240     default: llvm_unreachable("Unknown shift opc!");
241     case ARM_AM::lsl: SBits = 0x1; break;
242     case ARM_AM::lsr: SBits = 0x3; break;
243     case ARM_AM::asr: SBits = 0x5; break;
244     case ARM_AM::ror: SBits = 0x7; break;
245     case ARM_AM::rrx: SBits = 0x6; break;
246     }
247   } else {
248     // Set shift operand (bit[6:4]).
249     // LSL - 000
250     // LSR - 010
251     // ASR - 100
252     // ROR - 110
253     switch (SOpc) {
254     default: llvm_unreachable("Unknown shift opc!");
255     case ARM_AM::lsl: SBits = 0x0; break;
256     case ARM_AM::lsr: SBits = 0x2; break;
257     case ARM_AM::asr: SBits = 0x4; break;
258     case ARM_AM::ror: SBits = 0x6; break;
259     }
260   }
261   Binary |= SBits << 4;
262   if (SOpc == ARM_AM::rrx)
263     return Binary;
264
265   // Encode the shift operation Rs or shift_imm (except rrx).
266   if (Rs) {
267     // Encode Rs bit[11:8].
268     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
269     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
270   }
271
272   // Encode shift_imm bit[11:7].
273   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
274 }
275
276 unsigned ARMMCCodeEmitter::getBitfieldInvertedMaskOpValue(const MCInst &MI,
277                                                           unsigned Op) const {
278   // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
279   // msb of the mask.
280   const MCOperand &MO = MI.getOperand(Op);
281   uint32_t v = ~MO.getImm();
282   uint32_t lsb = CountTrailingZeros_32(v);
283   uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
284   assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
285   return lsb | (msb << 5);
286 }
287
288 unsigned ARMMCCodeEmitter::getRegisterListOpValue(const MCInst &MI,
289                                                   unsigned Op) const {
290   // Convert a list of GPRs into a bitfield (R0 -> bit 0). For each
291   // register in the list, set the corresponding bit.
292   unsigned Binary = 0;
293   for (unsigned i = Op, e = MI.getNumOperands(); i < e; ++i) {
294     unsigned regno = getARMRegisterNumbering(MI.getOperand(i).getReg());
295     Binary |= 1 << regno;
296   }
297   return Binary;
298 }
299
300 unsigned ARMMCCodeEmitter::getAddrMode6AddressOpValue(const MCInst &MI,
301                                                       unsigned Op) const {
302   const MCOperand &Reg = MI.getOperand(Op);
303   const MCOperand &Imm = MI.getOperand(Op+1);
304   
305   unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
306   unsigned Align = Imm.getImm();
307   switch(Align) {
308     case 2: case 4: case 8:  Align = 0x01; break;
309     case 16: Align = 0x02; break;
310     case 32: Align = 0x03; break;
311     default: Align = 0x00; break;
312   }
313   return RegNo | (Align << 4);
314 }
315
316 unsigned ARMMCCodeEmitter::getAddrMode6OffsetOpValue(const MCInst &MI,
317                                                      unsigned Op) const {
318   const MCOperand &regno = MI.getOperand(Op);
319   if (regno.getReg() == 0) return 0x0D;
320   return regno.getReg();
321 }
322
323 void ARMMCCodeEmitter::
324 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
325                   SmallVectorImpl<MCFixup> &Fixups) const {
326   unsigned Opcode = MI.getOpcode();
327   const TargetInstrDesc &Desc = TII.get(Opcode);
328   uint64_t TSFlags = Desc.TSFlags;
329   // Keep track of the current byte being emitted.
330   unsigned CurByte = 0;
331
332   // Pseudo instructions don't get encoded.
333   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
334     return;
335
336   ++MCNumEmitted;  // Keep track of the # of mi's emitted
337   unsigned Value = getBinaryCodeForInstr(MI);
338   switch (Opcode) {
339   default: break;
340   }
341   EmitConstant(Value, 4, CurByte, OS);
342 }
343
344 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
345 // be able to generate code emitter helpers for either variant, like it
346 // does for the AsmWriter.
347 #define ARMCodeEmitter ARMMCCodeEmitter
348 #define MachineInstr MCInst
349 #include "ARMGenCodeEmitter.inc"
350 #undef ARMCodeEmitter
351 #undef MachineInstr