5b206769614fd370f99ee15ea23d2fc7d5d2ddca
[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   /// getCCOutOpValue - Return encoding of the 's' bit.
53   unsigned getCCOutOpValue(const MCInst &MI, unsigned Op) const {
54     // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
55     // '1' respectively.
56     return MI.getOperand(Op).getReg() == ARM::CPSR;
57   }
58
59   /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
60   unsigned getSOImmOpValue(const MCInst &MI, unsigned Op) const {
61     unsigned SoImm = MI.getOperand(Op).getImm();
62     int SoImmVal = ARM_AM::getSOImmVal(SoImm);
63     assert(SoImmVal != -1 && "Not a valid so_imm value!");
64
65     // Encode rotate_imm.
66     unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
67       << ARMII::SoRotImmShift;
68
69     // Encode immed_8.
70     Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
71     return Binary;
72   }
73
74   /// getSORegOpValue - Return an encoded so_reg shifted register value.
75   unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
76
77   unsigned getRotImmOpValue(const MCInst &MI, unsigned Op) const {
78     switch (MI.getOperand(Op).getImm()) {
79     default: assert (0 && "Not a valid rot_imm value!");
80     case 0:  return 0;
81     case 8:  return 1;
82     case 16: return 2;
83     case 24: return 3;
84     }
85   }
86
87   unsigned getNumFixupKinds() const {
88     assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
89     return 0;
90   }
91
92   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
93     static MCFixupKindInfo rtn;
94     assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
95     return rtn;
96   }
97
98   void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
99     OS << (char)C;
100     ++CurByte;
101   }
102
103   void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
104                     raw_ostream &OS) const {
105     // Output the constant in little endian byte order.
106     for (unsigned i = 0; i != Size; ++i) {
107       EmitByte(Val & 255, CurByte, OS);
108       Val >>= 8;
109     }
110   }
111
112   void EmitImmediate(const MCOperand &Disp,
113                      unsigned ImmSize, MCFixupKind FixupKind,
114                      unsigned &CurByte, raw_ostream &OS,
115                      SmallVectorImpl<MCFixup> &Fixups,
116                      int ImmOffset = 0) const;
117
118   void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
119                          SmallVectorImpl<MCFixup> &Fixups) const;
120 };
121
122 } // end anonymous namespace
123
124 MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
125                                              TargetMachine &TM,
126                                              MCContext &Ctx) {
127   return new ARMMCCodeEmitter(TM, Ctx);
128 }
129
130 void ARMMCCodeEmitter::
131 EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
132               unsigned &CurByte, raw_ostream &OS,
133               SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
134   assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
135 }
136
137 /// getMachineOpValue - Return binary encoding of operand. If the machine
138 /// operand requires relocation, record the relocation and return zero.
139 unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
140                                              const MCOperand &MO) const {
141   if (MO.isReg())
142     return getARMRegisterNumbering(MO.getReg());
143   else if (MO.isImm()) {
144     return static_cast<unsigned>(MO.getImm());
145   } else {
146 #ifndef NDEBUG
147     errs() << MO;
148 #endif
149     llvm_unreachable(0);
150   }
151   return 0;
152 }
153
154
155 unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
156                                            unsigned OpIdx) const {
157   // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
158   // to be shifted. The second is either Rs, the amount to shift by, or
159   // reg0 in which case the imm contains the amount to shift by.
160   // {3-0} = Rm.
161   // {4} = 1 if reg shift, 0 if imm shift
162   // {6-5} = type
163   //    If reg shift:
164   //      {7} = 0
165   //      {11-8} = Rs
166   //    else (imm shift)
167   //      {11-7} = imm
168
169   const MCOperand &MO  = MI.getOperand(OpIdx);
170   const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
171   const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
172   ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
173
174   // Encode Rm.
175   unsigned Binary = getARMRegisterNumbering(MO.getReg());
176
177   // Encode the shift opcode.
178   unsigned SBits = 0;
179   unsigned Rs = MO1.getReg();
180   if (Rs) {
181     // Set shift operand (bit[7:4]).
182     // LSL - 0001
183     // LSR - 0011
184     // ASR - 0101
185     // ROR - 0111
186     // RRX - 0110 and bit[11:8] clear.
187     switch (SOpc) {
188     default: llvm_unreachable("Unknown shift opc!");
189     case ARM_AM::lsl: SBits = 0x1; break;
190     case ARM_AM::lsr: SBits = 0x3; break;
191     case ARM_AM::asr: SBits = 0x5; break;
192     case ARM_AM::ror: SBits = 0x7; break;
193     case ARM_AM::rrx: SBits = 0x6; break;
194     }
195   } else {
196     // Set shift operand (bit[6:4]).
197     // LSL - 000
198     // LSR - 010
199     // ASR - 100
200     // ROR - 110
201     switch (SOpc) {
202     default: llvm_unreachable("Unknown shift opc!");
203     case ARM_AM::lsl: SBits = 0x0; break;
204     case ARM_AM::lsr: SBits = 0x2; break;
205     case ARM_AM::asr: SBits = 0x4; break;
206     case ARM_AM::ror: SBits = 0x6; break;
207     }
208   }
209   Binary |= SBits << 4;
210   if (SOpc == ARM_AM::rrx)
211     return Binary;
212
213   // Encode the shift operation Rs or shift_imm (except rrx).
214   if (Rs) {
215     // Encode Rs bit[11:8].
216     assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
217     return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
218   }
219
220   // Encode shift_imm bit[11:7].
221   return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
222 }
223
224 void ARMMCCodeEmitter::
225 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
226                   SmallVectorImpl<MCFixup> &Fixups) const {
227   unsigned Opcode = MI.getOpcode();
228   const TargetInstrDesc &Desc = TII.get(Opcode);
229   uint64_t TSFlags = Desc.TSFlags;
230   // Keep track of the current byte being emitted.
231   unsigned CurByte = 0;
232
233   // Pseudo instructions don't get encoded.
234   if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
235     return;
236
237   ++MCNumEmitted;  // Keep track of the # of mi's emitted
238   unsigned Value = getBinaryCodeForInstr(MI);
239   switch (Opcode) {
240   default: break;
241   }
242   EmitConstant(Value, 4, CurByte, OS);
243 }
244
245 // FIXME: These #defines shouldn't be necessary. Instead, tblgen should
246 // be able to generate code emitter helpers for either variant, like it
247 // does for the AsmWriter.
248 #define ARMCodeEmitter ARMMCCodeEmitter
249 #define MachineInstr MCInst
250 #include "ARMGenCodeEmitter.inc"
251 #undef ARMCodeEmitter
252 #undef MachineInstr