blob: 1685ce36cbe6e8115203514302526ed280f74874 [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
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class 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
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Jim Grosbachc466b932010-11-11 18:04:49 +000048 // name offset bits flags
Jim Grosbach97dd28f2010-11-30 22:40:36 +000049 { "fixup_arm_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_vfp_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachc466b932010-11-11 18:04:49 +000051 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jason W Kim61db6292010-11-30 23:27:18 +000052 { "fixup_arm_movt_hi16", 0, 16, 0 },
53 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000054 };
55
56 if (Kind < FirstTargetFixupKind)
57 return MCCodeEmitter::getFixupKindInfo(Kind);
58
59 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
60 "Invalid kind!");
61 return Infos[Kind - FirstTargetFixupKind];
62 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000063 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64
Jim Grosbach9af82ba2010-10-07 21:57:55 +000065 // getBinaryCodeForInstr - TableGen'erated function for getting the
66 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000067 unsigned getBinaryCodeForInstr(const MCInst &MI,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000069
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000072 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000074
Jason W Kim837caa92010-11-18 23:37:15 +000075 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
76 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
77 SmallVectorImpl<MCFixup> &Fixups) const;
78
Bill Wendling92b5a2e2010-11-03 01:49:29 +000079 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000080 unsigned &Reg, unsigned &Imm,
81 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000082
Jim Grosbachc466b932010-11-11 18:04:49 +000083 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
84 /// branch target.
85 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
86 SmallVectorImpl<MCFixup> &Fixups) const;
87
Bill Wendling92b5a2e2010-11-03 01:49:29 +000088 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
89 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000090 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000092
Jim Grosbach54fea632010-11-09 17:20:53 +000093 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
94 /// operand as needed by load/store instructions.
95 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000098 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
99 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const {
101 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
102 switch (Mode) {
103 default: assert(0 && "Unknown addressing sub-mode!");
104 case ARM_AM::da: return 0;
105 case ARM_AM::ia: return 1;
106 case ARM_AM::db: return 2;
107 case ARM_AM::ib: return 3;
108 }
109 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000110 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
111 ///
112 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
113 switch (ShOpc) {
114 default: llvm_unreachable("Unknown shift opc!");
115 case ARM_AM::no_shift:
116 case ARM_AM::lsl: return 0;
117 case ARM_AM::lsr: return 1;
118 case ARM_AM::asr: return 2;
119 case ARM_AM::ror:
120 case ARM_AM::rrx: return 3;
121 }
122 return 0;
123 }
124
125 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
126 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
128
129 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
130 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000133 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
134 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const;
136
Jim Grosbach570a9222010-11-11 01:09:40 +0000137 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
138 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000140
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000141 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
142 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
144
Bill Wendling1fd374e2010-11-30 22:57:21 +0000145 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
146 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
147 SmallVectorImpl<MCFixup> &Fixups) const;
148
149 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
150 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups) const;
152
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000153 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000154 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000156
Jim Grosbach08bd5492010-10-12 23:00:24 +0000157 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000158 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
159 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000160 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
161 // '1' respectively.
162 return MI.getOperand(Op).getReg() == ARM::CPSR;
163 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000164
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000165 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000166 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
167 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000168 unsigned SoImm = MI.getOperand(Op).getImm();
169 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
170 assert(SoImmVal != -1 && "Not a valid so_imm value!");
171
172 // Encode rotate_imm.
173 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
174 << ARMII::SoRotImmShift;
175
176 // Encode immed_8.
177 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
178 return Binary;
179 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000180
181 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
182 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
183 SmallVectorImpl<MCFixup> &Fixups) const {
184 unsigned SoImm = MI.getOperand(Op).getImm();
185 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
186 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
187 return Encoded;
188 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000189
Owen Anderson75579f72010-11-29 22:44:32 +0000190 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
193 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000194 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
195 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000196 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
197 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000198
Jim Grosbachef324d72010-10-12 23:53:58 +0000199 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000202 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
203 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000204
Jim Grosbach806e80e2010-11-03 23:52:49 +0000205 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
206 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000207 switch (MI.getOperand(Op).getImm()) {
208 default: assert (0 && "Not a valid rot_imm value!");
209 case 0: return 0;
210 case 8: return 1;
211 case 16: return 2;
212 case 24: return 3;
213 }
214 }
215
Jim Grosbach806e80e2010-11-03 23:52:49 +0000216 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000218 return MI.getOperand(Op).getImm() - 1;
219 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000220
Jim Grosbach806e80e2010-11-03 23:52:49 +0000221 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
222 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000223 return 64 - MI.getOperand(Op).getImm();
224 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000225
Jim Grosbach806e80e2010-11-03 23:52:49 +0000226 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000228
Jim Grosbach806e80e2010-11-03 23:52:49 +0000229 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const;
231 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
232 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000233 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
234 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000235 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000237
Owen Andersonc7139a62010-11-11 19:07:48 +0000238 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
239 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000240 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
241 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000242 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
243 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000244
Jim Grosbach70933262010-11-04 01:12:30 +0000245 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000246 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000247 }
248
Jim Grosbach70933262010-11-04 01:12:30 +0000249 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000250 // Output the constant in little endian byte order.
251 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000252 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000253 Val >>= 8;
254 }
255 }
256
Jim Grosbach568eeed2010-09-17 18:46:17 +0000257 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
258 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000259};
260
261} // end anonymous namespace
262
Bill Wendling0800ce72010-11-02 22:53:11 +0000263MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
264 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000265 return new ARMMCCodeEmitter(TM, Ctx);
266}
267
Owen Anderson57dac882010-11-11 21:36:43 +0000268/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000269/// instructions, and rewrite them to their Thumb2 form if we are currently in
270/// Thumb2 mode.
271unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
272 unsigned EncodedValue) const {
273 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
274 if (Subtarget.isThumb2()) {
275 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
276 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
277 // set to 1111.
278 unsigned Bit24 = EncodedValue & 0x01000000;
279 unsigned Bit28 = Bit24 << 4;
280 EncodedValue &= 0xEFFFFFFF;
281 EncodedValue |= Bit28;
282 EncodedValue |= 0x0F000000;
283 }
284
285 return EncodedValue;
286}
287
Owen Anderson57dac882010-11-11 21:36:43 +0000288/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
289/// instructions, and rewrite them to their Thumb2 form if we are currently in
290/// Thumb2 mode.
291unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
292 unsigned EncodedValue) const {
293 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
294 if (Subtarget.isThumb2()) {
295 EncodedValue &= 0xF0FFFFFF;
296 EncodedValue |= 0x09000000;
297 }
298
299 return EncodedValue;
300}
301
Owen Anderson8f143912010-11-11 23:12:55 +0000302/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
303/// instructions, and rewrite them to their Thumb2 form if we are currently in
304/// Thumb2 mode.
305unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
306 unsigned EncodedValue) const {
307 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
308 if (Subtarget.isThumb2()) {
309 EncodedValue &= 0x00FFFFFF;
310 EncodedValue |= 0xEE000000;
311 }
312
313 return EncodedValue;
314}
315
316
Owen Anderson57dac882010-11-11 21:36:43 +0000317
Jim Grosbach56ac9072010-10-08 21:45:55 +0000318/// getMachineOpValue - Return binary encoding of operand. If the machine
319/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000320unsigned ARMMCCodeEmitter::
321getMachineOpValue(const MCInst &MI, const MCOperand &MO,
322 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000323 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000324 unsigned Reg = MO.getReg();
325 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000326
Owen Anderson90d4cf92010-10-21 20:49:13 +0000327 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000328 switch (Reg) {
329 default:
330 return RegNo;
331 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
332 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
333 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
334 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
335 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000336 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000337 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000338 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000339 } else if (MO.isFPImm()) {
340 return static_cast<unsigned>(APFloat(MO.getFPImm())
341 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000342 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000343
Jim Grosbach817c1a62010-11-19 00:27:09 +0000344 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000345 return 0;
346}
347
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000348/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000349bool ARMMCCodeEmitter::
350EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
351 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000352 const MCOperand &MO = MI.getOperand(OpIdx);
353 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000354
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000355 Reg = getARMRegisterNumbering(MO.getReg());
356
357 int32_t SImm = MO1.getImm();
358 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000359
Jim Grosbachab682a22010-10-28 18:34:10 +0000360 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000361 if (SImm == INT32_MIN)
362 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000363
Jim Grosbachab682a22010-10-28 18:34:10 +0000364 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000365 if (SImm < 0) {
366 SImm = -SImm;
367 isAdd = false;
368 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000369
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000370 Imm = SImm;
371 return isAdd;
372}
373
Jim Grosbachc466b932010-11-11 18:04:49 +0000374/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
375/// branch target.
376uint32_t ARMMCCodeEmitter::
377getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
378 SmallVectorImpl<MCFixup> &Fixups) const {
379 const MCOperand &MO = MI.getOperand(OpIdx);
380
381 // If the destination is an immediate, we have nothing to do.
382 if (MO.isImm()) return MO.getImm();
383 assert (MO.isExpr() && "Unexpected branch target type!");
384 const MCExpr *Expr = MO.getExpr();
385 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
386 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
387
388 // All of the information is in the fixup.
389 return 0;
390}
391
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000392/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000393uint32_t ARMMCCodeEmitter::
394getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
395 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000396 // {17-13} = reg
397 // {12} = (U)nsigned (add == '1', sub == '0')
398 // {11-0} = imm12
399 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000400 bool isAdd = true;
401 // If The first operand isn't a register, we have a label reference.
402 const MCOperand &MO = MI.getOperand(OpIdx);
403 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000404 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000405 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000406 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000407
408 assert(MO.isExpr() && "Unexpected machine operand type!");
409 const MCExpr *Expr = MO.getExpr();
410 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
411 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
412
413 ++MCNumCPRelocations;
414 } else
415 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000416
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000417 uint32_t Binary = Imm12 & 0xfff;
418 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000419 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000420 Binary |= (1 << 12);
421 Binary |= (Reg << 13);
422 return Binary;
423}
424
Jim Grosbach54fea632010-11-09 17:20:53 +0000425uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000426getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
427 SmallVectorImpl<MCFixup> &Fixups) const {
428 // {20-16} = imm{15-12}
429 // {11-0} = imm{11-0}
430 const MCOperand &MO = MI.getOperand(OpIdx);
431 if (MO.isImm()) {
432 return static_cast<unsigned>(MO.getImm());
433 } else if (const MCSymbolRefExpr *Expr =
434 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
435 MCFixupKind Kind;
436 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000437 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000438 case MCSymbolRefExpr::VK_ARM_HI16:
439 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
440 break;
441 case MCSymbolRefExpr::VK_ARM_LO16:
442 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
443 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000444 }
445 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
446 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000447 };
448 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000449 return 0;
450}
451
452uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000453getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
454 SmallVectorImpl<MCFixup> &Fixups) const {
455 const MCOperand &MO = MI.getOperand(OpIdx);
456 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
457 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
458 unsigned Rn = getARMRegisterNumbering(MO.getReg());
459 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000460 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
461 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000462 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
463 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000464
465 // {16-13} = Rn
466 // {12} = isAdd
467 // {11-0} = shifter
468 // {3-0} = Rm
469 // {4} = 0
470 // {6-5} = type
471 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000472 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000473 Binary |= Rn << 13;
474 Binary |= SBits << 5;
475 Binary |= ShImm << 7;
476 if (isAdd)
477 Binary |= 1 << 12;
478 return Binary;
479}
480
Jim Grosbach570a9222010-11-11 01:09:40 +0000481uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000482getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
483 SmallVectorImpl<MCFixup> &Fixups) const {
484 // {17-14} Rn
485 // {13} 1 == imm12, 0 == Rm
486 // {12} isAdd
487 // {11-0} imm12/Rm
488 const MCOperand &MO = MI.getOperand(OpIdx);
489 unsigned Rn = getARMRegisterNumbering(MO.getReg());
490 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
491 Binary |= Rn << 14;
492 return Binary;
493}
494
495uint32_t ARMMCCodeEmitter::
496getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
497 SmallVectorImpl<MCFixup> &Fixups) const {
498 // {13} 1 == imm12, 0 == Rm
499 // {12} isAdd
500 // {11-0} imm12/Rm
501 const MCOperand &MO = MI.getOperand(OpIdx);
502 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
503 unsigned Imm = MO1.getImm();
504 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
505 bool isReg = MO.getReg() != 0;
506 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
507 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
508 if (isReg) {
509 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
510 Binary <<= 7; // Shift amount is bits [11:7]
511 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
512 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
513 }
514 return Binary | (isAdd << 12) | (isReg << 13);
515}
516
517uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000518getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
519 SmallVectorImpl<MCFixup> &Fixups) const {
520 // {9} 1 == imm8, 0 == Rm
521 // {8} isAdd
522 // {7-4} imm7_4/zero
523 // {3-0} imm3_0/Rm
524 const MCOperand &MO = MI.getOperand(OpIdx);
525 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
526 unsigned Imm = MO1.getImm();
527 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
528 bool isImm = MO.getReg() == 0;
529 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
530 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
531 if (!isImm)
532 Imm8 = getARMRegisterNumbering(MO.getReg());
533 return Imm8 | (isAdd << 8) | (isImm << 9);
534}
535
536uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000537getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
538 SmallVectorImpl<MCFixup> &Fixups) const {
539 // {13} 1 == imm8, 0 == Rm
540 // {12-9} Rn
541 // {8} isAdd
542 // {7-4} imm7_4/zero
543 // {3-0} imm3_0/Rm
544 const MCOperand &MO = MI.getOperand(OpIdx);
545 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
546 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
547 unsigned Rn = getARMRegisterNumbering(MO.getReg());
548 unsigned Imm = MO2.getImm();
549 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
550 bool isImm = MO1.getReg() == 0;
551 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
552 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
553 if (!isImm)
554 Imm8 = getARMRegisterNumbering(MO1.getReg());
555 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
556}
557
Bill Wendling1fd374e2010-11-30 22:57:21 +0000558/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
559static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
560 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000561 // [Rn, Rm]
562 // {5-3} = Rm
563 // {2-0} = Rn
564 //
565 // [Rn, #imm]
566 // {7-3} = imm5
567 // {2-0} = Rn
568 const MCOperand &MO = MI.getOperand(OpIdx);
569 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
570 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
571 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000572 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000573 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
574 return (Rm << 3) | (Imm5 << 3) | Rn;
575}
576
Bill Wendling1fd374e2010-11-30 22:57:21 +0000577/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
578uint32_t ARMMCCodeEmitter::
579getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
580 SmallVectorImpl<MCFixup> &) const {
581 return getAddrModeSOpValue(MI, OpIdx, 4);
582}
583
584/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
585uint32_t ARMMCCodeEmitter::
586getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
587 SmallVectorImpl<MCFixup> &) const {
588 return getAddrModeSOpValue(MI, OpIdx, 2);
589}
590
591/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
592uint32_t ARMMCCodeEmitter::
593getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
594 SmallVectorImpl<MCFixup> &) const {
595 return getAddrModeSOpValue(MI, OpIdx, 1);
596}
597
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000598/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000599uint32_t ARMMCCodeEmitter::
600getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000602 // {12-9} = reg
603 // {8} = (U)nsigned (add == '1', sub == '0')
604 // {7-0} = imm8
605 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000606 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000607 // If The first operand isn't a register, we have a label reference.
608 const MCOperand &MO = MI.getOperand(OpIdx);
609 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000610 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000611 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000612 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000613
614 assert(MO.isExpr() && "Unexpected machine operand type!");
615 const MCExpr *Expr = MO.getExpr();
616 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
617 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
618
619 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000620 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000621 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000622 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
623 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000624
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000625 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
626 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000627 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000628 Binary |= (1 << 8);
629 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000630 return Binary;
631}
632
Jim Grosbach806e80e2010-11-03 23:52:49 +0000633unsigned ARMMCCodeEmitter::
634getSORegOpValue(const MCInst &MI, unsigned OpIdx,
635 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000636 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
637 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
638 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000639 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000640 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000641 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000642 // {6-5} = type
643 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000644 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000645 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000646 // else (imm shift)
647 // {11-7} = imm
648
649 const MCOperand &MO = MI.getOperand(OpIdx);
650 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
651 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
652 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
653
654 // Encode Rm.
655 unsigned Binary = getARMRegisterNumbering(MO.getReg());
656
657 // Encode the shift opcode.
658 unsigned SBits = 0;
659 unsigned Rs = MO1.getReg();
660 if (Rs) {
661 // Set shift operand (bit[7:4]).
662 // LSL - 0001
663 // LSR - 0011
664 // ASR - 0101
665 // ROR - 0111
666 // RRX - 0110 and bit[11:8] clear.
667 switch (SOpc) {
668 default: llvm_unreachable("Unknown shift opc!");
669 case ARM_AM::lsl: SBits = 0x1; break;
670 case ARM_AM::lsr: SBits = 0x3; break;
671 case ARM_AM::asr: SBits = 0x5; break;
672 case ARM_AM::ror: SBits = 0x7; break;
673 case ARM_AM::rrx: SBits = 0x6; break;
674 }
675 } else {
676 // Set shift operand (bit[6:4]).
677 // LSL - 000
678 // LSR - 010
679 // ASR - 100
680 // ROR - 110
681 switch (SOpc) {
682 default: llvm_unreachable("Unknown shift opc!");
683 case ARM_AM::lsl: SBits = 0x0; break;
684 case ARM_AM::lsr: SBits = 0x2; break;
685 case ARM_AM::asr: SBits = 0x4; break;
686 case ARM_AM::ror: SBits = 0x6; break;
687 }
688 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000689
Jim Grosbachef324d72010-10-12 23:53:58 +0000690 Binary |= SBits << 4;
691 if (SOpc == ARM_AM::rrx)
692 return Binary;
693
694 // Encode the shift operation Rs or shift_imm (except rrx).
695 if (Rs) {
696 // Encode Rs bit[11:8].
697 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
698 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
699 }
700
701 // Encode shift_imm bit[11:7].
702 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
703}
704
Jim Grosbach806e80e2010-11-03 23:52:49 +0000705unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000706getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
707 SmallVectorImpl<MCFixup> &Fixups) const {
708 const MCOperand &MO1 = MI.getOperand(OpNum);
709 const MCOperand &MO2 = MI.getOperand(OpNum+1);
710 const MCOperand &MO3 = MI.getOperand(OpNum+2);
711
712 // Encoded as [Rn, Rm, imm].
713 // FIXME: Needs fixup support.
714 unsigned Value = getARMRegisterNumbering(MO1.getReg());
715 Value <<= 4;
716 Value |= getARMRegisterNumbering(MO2.getReg());
717 Value <<= 2;
718 Value |= MO3.getImm();
719
720 return Value;
721}
722
723unsigned ARMMCCodeEmitter::
724getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
725 SmallVectorImpl<MCFixup> &Fixups) const {
726 const MCOperand &MO1 = MI.getOperand(OpNum);
727 const MCOperand &MO2 = MI.getOperand(OpNum+1);
728
729 // FIXME: Needs fixup support.
730 unsigned Value = getARMRegisterNumbering(MO1.getReg());
731
732 // Even though the immediate is 8 bits long, we need 9 bits in order
733 // to represent the (inverse of the) sign bit.
734 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000735 int32_t tmp = (int32_t)MO2.getImm();
736 if (tmp < 0)
737 tmp = abs(tmp);
738 else
739 Value |= 256; // Set the ADD bit
740 Value |= tmp & 255;
741 return Value;
742}
743
744unsigned ARMMCCodeEmitter::
745getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
746 SmallVectorImpl<MCFixup> &Fixups) const {
747 const MCOperand &MO1 = MI.getOperand(OpNum);
748
749 // FIXME: Needs fixup support.
750 unsigned Value = 0;
751 int32_t tmp = (int32_t)MO1.getImm();
752 if (tmp < 0)
753 tmp = abs(tmp);
754 else
755 Value |= 256; // Set the ADD bit
756 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000757 return Value;
758}
759
760unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000761getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
762 SmallVectorImpl<MCFixup> &Fixups) const {
763 const MCOperand &MO1 = MI.getOperand(OpNum);
764
765 // FIXME: Needs fixup support.
766 unsigned Value = 0;
767 int32_t tmp = (int32_t)MO1.getImm();
768 if (tmp < 0)
769 tmp = abs(tmp);
770 else
771 Value |= 4096; // Set the ADD bit
772 Value |= tmp & 4095;
773 return Value;
774}
775
776unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000777getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
778 SmallVectorImpl<MCFixup> &Fixups) const {
779 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
780 // shifted. The second is the amount to shift by.
781 //
782 // {3-0} = Rm.
783 // {4} = 0
784 // {6-5} = type
785 // {11-7} = imm
786
787 const MCOperand &MO = MI.getOperand(OpIdx);
788 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
789 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
790
791 // Encode Rm.
792 unsigned Binary = getARMRegisterNumbering(MO.getReg());
793
794 // Encode the shift opcode.
795 unsigned SBits = 0;
796 // Set shift operand (bit[6:4]).
797 // LSL - 000
798 // LSR - 010
799 // ASR - 100
800 // ROR - 110
801 switch (SOpc) {
802 default: llvm_unreachable("Unknown shift opc!");
803 case ARM_AM::lsl: SBits = 0x0; break;
804 case ARM_AM::lsr: SBits = 0x2; break;
805 case ARM_AM::asr: SBits = 0x4; break;
806 case ARM_AM::ror: SBits = 0x6; break;
807 }
808
809 Binary |= SBits << 4;
810 if (SOpc == ARM_AM::rrx)
811 return Binary;
812
813 // Encode shift_imm bit[11:7].
814 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
815}
816
817unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000818getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
819 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000820 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
821 // msb of the mask.
822 const MCOperand &MO = MI.getOperand(Op);
823 uint32_t v = ~MO.getImm();
824 uint32_t lsb = CountTrailingZeros_32(v);
825 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
826 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
827 return lsb | (msb << 5);
828}
829
Jim Grosbach806e80e2010-11-03 23:52:49 +0000830unsigned ARMMCCodeEmitter::
831getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000832 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000833 // VLDM/VSTM:
834 // {12-8} = Vd
835 // {7-0} = Number of registers
836 //
837 // LDM/STM:
838 // {15-0} = Bitfield of GPRs.
839 unsigned Reg = MI.getOperand(Op).getReg();
840 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
841 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
842
Bill Wendling5e559a22010-11-09 00:30:18 +0000843 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000844
845 if (SPRRegs || DPRRegs) {
846 // VLDM/VSTM
847 unsigned RegNo = getARMRegisterNumbering(Reg);
848 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
849 Binary |= (RegNo & 0x1f) << 8;
850 if (SPRRegs)
851 Binary |= NumRegs;
852 else
853 Binary |= NumRegs * 2;
854 } else {
855 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
856 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
857 Binary |= 1 << RegNo;
858 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000859 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000860
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000861 return Binary;
862}
863
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000864/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
865/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000866unsigned ARMMCCodeEmitter::
867getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
868 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000869 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000870 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000871
Owen Andersond9aa7d32010-11-02 00:05:05 +0000872 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000873 unsigned Align = 0;
874
875 switch (Imm.getImm()) {
876 default: break;
877 case 2:
878 case 4:
879 case 8: Align = 0x01; break;
880 case 16: Align = 0x02; break;
881 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000882 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000883
Owen Andersond9aa7d32010-11-02 00:05:05 +0000884 return RegNo | (Align << 4);
885}
886
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000887/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
888/// alignment operand for use in VLD-dup instructions. This is the same as
889/// getAddrMode6AddressOpValue except for the alignment encoding, which is
890/// different for VLD4-dup.
891unsigned ARMMCCodeEmitter::
892getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
893 SmallVectorImpl<MCFixup> &Fixups) const {
894 const MCOperand &Reg = MI.getOperand(Op);
895 const MCOperand &Imm = MI.getOperand(Op + 1);
896
897 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
898 unsigned Align = 0;
899
900 switch (Imm.getImm()) {
901 default: break;
902 case 2:
903 case 4:
904 case 8: Align = 0x01; break;
905 case 16: Align = 0x03; break;
906 }
907
908 return RegNo | (Align << 4);
909}
910
Jim Grosbach806e80e2010-11-03 23:52:49 +0000911unsigned ARMMCCodeEmitter::
912getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
913 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000914 const MCOperand &MO = MI.getOperand(Op);
915 if (MO.getReg() == 0) return 0x0D;
916 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000917}
918
Jim Grosbach568eeed2010-09-17 18:46:17 +0000919void ARMMCCodeEmitter::
920EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000921 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000922 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000923 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000924 uint64_t TSFlags = Desc.TSFlags;
925 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000926 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000927 int Size;
928 // Basic size info comes from the TSFlags field.
929 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
930 default: llvm_unreachable("Unexpected instruction size!");
931 case ARMII::Size2Bytes: Size = 2; break;
932 case ARMII::Size4Bytes: Size = 4; break;
933 }
934 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000935 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000936}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000937
Jim Grosbach806e80e2010-11-03 23:52:49 +0000938#include "ARMGenMCCodeEmitter.inc"