blob: ea7789c073c218905899aa35c2685a633eb3078d [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- 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"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000017#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000018#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000021#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000022#include "llvm/Support/raw_ostream.h"
23using namespace llvm;
24
Jim Grosbachd6d4b422010-10-07 22:12:50 +000025STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
Jim Grosbach568eeed2010-09-17 18:46:17 +000027namespace {
28class 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
35public:
36 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
37 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000038 }
39
40 ~ARMMCCodeEmitter() {}
41
Jim Grosbach0de6ab32010-10-12 17:11:26 +000042 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
43
Jim Grosbach9af82ba2010-10-07 21:57:55 +000044 // getBinaryCodeForInstr - TableGen'erated function for getting the
45 // binary encoding for an instruction.
Jim Grosbachbade37b2010-10-08 00:21:28 +000046 unsigned getBinaryCodeForInstr(const MCInst &MI) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000047
48 /// getMachineOpValue - Return binary encoding of operand. If the machine
49 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach3e094132010-10-08 17:45:54 +000050 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000051
Jim Grosbach08bd5492010-10-12 23:00:24 +000052 /// 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 }
Jim Grosbachef324d72010-10-12 23:53:58 +000058
Jim Grosbach2a6a93d2010-10-12 23:18:08 +000059 /// 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 }
Jim Grosbach08bd5492010-10-12 23:00:24 +000073
Jim Grosbachef324d72010-10-12 23:53:58 +000074 /// getSORegOpValue - Return an encoded so_reg shifted register value.
75 unsigned getSORegOpValue(const MCInst &MI, unsigned Op) const;
76
Jim Grosbachb35ad412010-10-13 19:56:10 +000077 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
Jim Grosbach8abe32a2010-10-15 17:15:16 +000087 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op) const {
88 return MI.getOperand(Op).getImm() - 1;
89 }
90
Jim Grosbach568eeed2010-09-17 18:46:17 +000091 unsigned getNumFixupKinds() const {
92 assert(0 && "ARMMCCodeEmitter::getNumFixupKinds() not yet implemented.");
Michael J. Spencer895dda62010-09-18 17:54:37 +000093 return 0;
Jim Grosbach568eeed2010-09-17 18:46:17 +000094 }
95
96 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
97 static MCFixupKindInfo rtn;
98 assert(0 && "ARMMCCodeEmitter::getFixupKindInfo() not yet implemented.");
99 return rtn;
100 }
101
Jim Grosbach568eeed2010-09-17 18:46:17 +0000102 void EmitByte(unsigned char C, unsigned &CurByte, raw_ostream &OS) const {
103 OS << (char)C;
104 ++CurByte;
105 }
106
107 void EmitConstant(uint64_t Val, unsigned Size, unsigned &CurByte,
108 raw_ostream &OS) const {
109 // Output the constant in little endian byte order.
110 for (unsigned i = 0; i != Size; ++i) {
111 EmitByte(Val & 255, CurByte, OS);
112 Val >>= 8;
113 }
114 }
115
116 void EmitImmediate(const MCOperand &Disp,
117 unsigned ImmSize, MCFixupKind FixupKind,
118 unsigned &CurByte, raw_ostream &OS,
119 SmallVectorImpl<MCFixup> &Fixups,
120 int ImmOffset = 0) const;
121
122 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
123 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000124};
125
126} // end anonymous namespace
127
Jim Grosbach568eeed2010-09-17 18:46:17 +0000128MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &,
129 TargetMachine &TM,
130 MCContext &Ctx) {
131 return new ARMMCCodeEmitter(TM, Ctx);
132}
133
134void ARMMCCodeEmitter::
135EmitImmediate(const MCOperand &DispOp, unsigned Size, MCFixupKind FixupKind,
136 unsigned &CurByte, raw_ostream &OS,
137 SmallVectorImpl<MCFixup> &Fixups, int ImmOffset) const {
138 assert(0 && "ARMMCCodeEmitter::EmitImmediate() not yet implemented.");
139}
140
Jim Grosbach56ac9072010-10-08 21:45:55 +0000141/// getMachineOpValue - Return binary encoding of operand. If the machine
142/// operand requires relocation, record the relocation and return zero.
143unsigned ARMMCCodeEmitter::getMachineOpValue(const MCInst &MI,
144 const MCOperand &MO) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000145 if (MO.isReg()) {
Owen Anderson90d4cf92010-10-21 20:49:13 +0000146 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 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000158 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000159 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000160 } else if (MO.isFPImm()) {
161 return static_cast<unsigned>(APFloat(MO.getFPImm())
162 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000163 } else {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000164#ifndef NDEBUG
165 errs() << MO;
166#endif
167 llvm_unreachable(0);
168 }
169 return 0;
170}
171
Jim Grosbachef324d72010-10-12 23:53:58 +0000172unsigned ARMMCCodeEmitter::getSORegOpValue(const MCInst &MI,
173 unsigned OpIdx) const {
174 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg
175 // to be shifted. The second is either Rs, the amount to shift by, or
176 // reg0 in which case the imm contains the amount to shift by.
177 // {3-0} = Rm.
178 // {4} = 1 if reg shift, 0 if imm shift
179 // {6-5} = type
180 // If reg shift:
181 // {7} = 0
182 // {11-8} = Rs
183 // else (imm shift)
184 // {11-7} = imm
185
186 const MCOperand &MO = MI.getOperand(OpIdx);
187 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
188 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
189 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
190
191 // Encode Rm.
192 unsigned Binary = getARMRegisterNumbering(MO.getReg());
193
194 // Encode the shift opcode.
195 unsigned SBits = 0;
196 unsigned Rs = MO1.getReg();
197 if (Rs) {
198 // Set shift operand (bit[7:4]).
199 // LSL - 0001
200 // LSR - 0011
201 // ASR - 0101
202 // ROR - 0111
203 // RRX - 0110 and bit[11:8] clear.
204 switch (SOpc) {
205 default: llvm_unreachable("Unknown shift opc!");
206 case ARM_AM::lsl: SBits = 0x1; break;
207 case ARM_AM::lsr: SBits = 0x3; break;
208 case ARM_AM::asr: SBits = 0x5; break;
209 case ARM_AM::ror: SBits = 0x7; break;
210 case ARM_AM::rrx: SBits = 0x6; break;
211 }
212 } else {
213 // Set shift operand (bit[6:4]).
214 // LSL - 000
215 // LSR - 010
216 // ASR - 100
217 // ROR - 110
218 switch (SOpc) {
219 default: llvm_unreachable("Unknown shift opc!");
220 case ARM_AM::lsl: SBits = 0x0; break;
221 case ARM_AM::lsr: SBits = 0x2; break;
222 case ARM_AM::asr: SBits = 0x4; break;
223 case ARM_AM::ror: SBits = 0x6; break;
224 }
225 }
226 Binary |= SBits << 4;
227 if (SOpc == ARM_AM::rrx)
228 return Binary;
229
230 // Encode the shift operation Rs or shift_imm (except rrx).
231 if (Rs) {
232 // Encode Rs bit[11:8].
233 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
234 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
235 }
236
237 // Encode shift_imm bit[11:7].
238 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
239}
240
Jim Grosbach568eeed2010-09-17 18:46:17 +0000241void ARMMCCodeEmitter::
242EncodeInstruction(const MCInst &MI, raw_ostream &OS,
243 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000244 unsigned Opcode = MI.getOpcode();
245 const TargetInstrDesc &Desc = TII.get(Opcode);
246 uint64_t TSFlags = Desc.TSFlags;
Jim Grosbach58f38bf2010-10-08 00:39:21 +0000247 // Keep track of the current byte being emitted.
248 unsigned CurByte = 0;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000249
250 // Pseudo instructions don't get encoded.
251 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
252 return;
253
254 ++MCNumEmitted; // Keep track of the # of mi's emitted
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000255 unsigned Value = getBinaryCodeForInstr(MI);
Jim Grosbach3e094132010-10-08 17:45:54 +0000256 switch (Opcode) {
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000257 default: break;
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000258 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +0000259 EmitConstant(Value, 4, CurByte, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000260}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000261
262// FIXME: These #defines shouldn't be necessary. Instead, tblgen should
263// be able to generate code emitter helpers for either variant, like it
264// does for the AsmWriter.
265#define ARMCodeEmitter ARMMCCodeEmitter
266#define MachineInstr MCInst
267#include "ARMGenCodeEmitter.inc"
268#undef ARMCodeEmitter
269#undef MachineInstr