blob: 2e9d804894ee287c650a0a5b73fbf52884ac8f40 [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
49 { "fixup_arm_pcrel_12", 2, 12, MCFixupKindInfo::FKF_IsPCRel },
50 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
51 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach70933262010-11-04 01:12:30 +000052 };
53
54 if (Kind < FirstTargetFixupKind)
55 return MCCodeEmitter::getFixupKindInfo(Kind);
56
57 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58 "Invalid kind!");
59 return Infos[Kind - FirstTargetFixupKind];
60 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000061 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
62
Jim Grosbach9af82ba2010-10-07 21:57:55 +000063 // getBinaryCodeForInstr - TableGen'erated function for getting the
64 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000065 unsigned getBinaryCodeForInstr(const MCInst &MI,
66 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000067
68 /// getMachineOpValue - Return binary encoding of operand. If the machine
69 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000070 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
71 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072
Jason W Kim837caa92010-11-18 23:37:15 +000073 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
74 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
75 SmallVectorImpl<MCFixup> &Fixups) const;
76
Bill Wendling92b5a2e2010-11-03 01:49:29 +000077 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000078 unsigned &Reg, unsigned &Imm,
79 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000080
Jim Grosbachc466b932010-11-11 18:04:49 +000081 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
82 /// branch target.
83 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
87 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000090
Jim Grosbach54fea632010-11-09 17:20:53 +000091 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
92 /// operand as needed by load/store instructions.
93 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +000096 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
97 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const {
99 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
100 switch (Mode) {
101 default: assert(0 && "Unknown addressing sub-mode!");
102 case ARM_AM::da: return 0;
103 case ARM_AM::ia: return 1;
104 case ARM_AM::db: return 2;
105 case ARM_AM::ib: return 3;
106 }
107 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000108 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
109 ///
110 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
111 switch (ShOpc) {
112 default: llvm_unreachable("Unknown shift opc!");
113 case ARM_AM::no_shift:
114 case ARM_AM::lsl: return 0;
115 case ARM_AM::lsr: return 1;
116 case ARM_AM::asr: return 2;
117 case ARM_AM::ror:
118 case ARM_AM::rrx: return 3;
119 }
120 return 0;
121 }
122
123 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
124 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
127 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
128 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000131 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
132 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134
Jim Grosbach570a9222010-11-11 01:09:40 +0000135 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
136 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000138
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000139 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000140 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000142
Jim Grosbach08bd5492010-10-12 23:00:24 +0000143 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000144 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
145 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000146 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
147 // '1' respectively.
148 return MI.getOperand(Op).getReg() == ARM::CPSR;
149 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000150
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000151 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000152 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
153 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000154 unsigned SoImm = MI.getOperand(Op).getImm();
155 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
156 assert(SoImmVal != -1 && "Not a valid so_imm value!");
157
158 // Encode rotate_imm.
159 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
160 << ARMII::SoRotImmShift;
161
162 // Encode immed_8.
163 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
164 return Binary;
165 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000166
167 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
168 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 unsigned SoImm = MI.getOperand(Op).getImm();
171 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
172 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
173 return Encoded;
174 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000175
Owen Anderson75579f72010-11-29 22:44:32 +0000176 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
177 SmallVectorImpl<MCFixup> &Fixups) const;
178 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
179 SmallVectorImpl<MCFixup> &Fixups) const;
180 unsigned getT2AddrModeImm12OpValue(const MCInst &MI, unsigned OpNum,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182
Jim Grosbachef324d72010-10-12 23:53:58 +0000183 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000184 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
185 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000186 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
187 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000188
Jim Grosbach806e80e2010-11-03 23:52:49 +0000189 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
190 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000191 switch (MI.getOperand(Op).getImm()) {
192 default: assert (0 && "Not a valid rot_imm value!");
193 case 0: return 0;
194 case 8: return 1;
195 case 16: return 2;
196 case 24: return 3;
197 }
198 }
199
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000202 return MI.getOperand(Op).getImm() - 1;
203 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000204
Jim Grosbach806e80e2010-11-03 23:52:49 +0000205 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
206 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000207 return 64 - MI.getOperand(Op).getImm();
208 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000209
Jim Grosbach806e80e2010-11-03 23:52:49 +0000210 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
211 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000212
Jim Grosbach806e80e2010-11-03 23:52:49 +0000213 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
214 SmallVectorImpl<MCFixup> &Fixups) const;
215 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
216 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000217 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
218 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000221
Owen Andersonc7139a62010-11-11 19:07:48 +0000222 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
223 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000224 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
225 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000226 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
227 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000228
Jim Grosbach70933262010-11-04 01:12:30 +0000229 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000230 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000231 }
232
Jim Grosbach70933262010-11-04 01:12:30 +0000233 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000234 // Output the constant in little endian byte order.
235 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000236 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000237 Val >>= 8;
238 }
239 }
240
Jim Grosbach568eeed2010-09-17 18:46:17 +0000241 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
242 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000243};
244
245} // end anonymous namespace
246
Bill Wendling0800ce72010-11-02 22:53:11 +0000247MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
248 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000249 return new ARMMCCodeEmitter(TM, Ctx);
250}
251
Owen Anderson57dac882010-11-11 21:36:43 +0000252/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000253/// instructions, and rewrite them to their Thumb2 form if we are currently in
254/// Thumb2 mode.
255unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
256 unsigned EncodedValue) const {
257 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
258 if (Subtarget.isThumb2()) {
259 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
260 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
261 // set to 1111.
262 unsigned Bit24 = EncodedValue & 0x01000000;
263 unsigned Bit28 = Bit24 << 4;
264 EncodedValue &= 0xEFFFFFFF;
265 EncodedValue |= Bit28;
266 EncodedValue |= 0x0F000000;
267 }
268
269 return EncodedValue;
270}
271
Owen Anderson57dac882010-11-11 21:36:43 +0000272/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
273/// instructions, and rewrite them to their Thumb2 form if we are currently in
274/// Thumb2 mode.
275unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
276 unsigned EncodedValue) const {
277 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
278 if (Subtarget.isThumb2()) {
279 EncodedValue &= 0xF0FFFFFF;
280 EncodedValue |= 0x09000000;
281 }
282
283 return EncodedValue;
284}
285
Owen Anderson8f143912010-11-11 23:12:55 +0000286/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
287/// instructions, and rewrite them to their Thumb2 form if we are currently in
288/// Thumb2 mode.
289unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
290 unsigned EncodedValue) const {
291 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
292 if (Subtarget.isThumb2()) {
293 EncodedValue &= 0x00FFFFFF;
294 EncodedValue |= 0xEE000000;
295 }
296
297 return EncodedValue;
298}
299
300
Owen Anderson57dac882010-11-11 21:36:43 +0000301
Jim Grosbach56ac9072010-10-08 21:45:55 +0000302/// getMachineOpValue - Return binary encoding of operand. If the machine
303/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000304unsigned ARMMCCodeEmitter::
305getMachineOpValue(const MCInst &MI, const MCOperand &MO,
306 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000307 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000308 unsigned Reg = MO.getReg();
309 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000310
Owen Anderson90d4cf92010-10-21 20:49:13 +0000311 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000312 switch (Reg) {
313 default:
314 return RegNo;
315 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
316 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
317 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
318 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
319 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000320 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000321 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000322 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000323 } else if (MO.isFPImm()) {
324 return static_cast<unsigned>(APFloat(MO.getFPImm())
325 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000326 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000327
Jim Grosbach817c1a62010-11-19 00:27:09 +0000328 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000329 return 0;
330}
331
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000332/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000333bool ARMMCCodeEmitter::
334EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
335 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000336 const MCOperand &MO = MI.getOperand(OpIdx);
337 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000338
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000339 Reg = getARMRegisterNumbering(MO.getReg());
340
341 int32_t SImm = MO1.getImm();
342 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000343
Jim Grosbachab682a22010-10-28 18:34:10 +0000344 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000345 if (SImm == INT32_MIN)
346 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000347
Jim Grosbachab682a22010-10-28 18:34:10 +0000348 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000349 if (SImm < 0) {
350 SImm = -SImm;
351 isAdd = false;
352 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000353
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000354 Imm = SImm;
355 return isAdd;
356}
357
Jim Grosbachc466b932010-11-11 18:04:49 +0000358/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
359/// branch target.
360uint32_t ARMMCCodeEmitter::
361getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
362 SmallVectorImpl<MCFixup> &Fixups) const {
363 const MCOperand &MO = MI.getOperand(OpIdx);
364
365 // If the destination is an immediate, we have nothing to do.
366 if (MO.isImm()) return MO.getImm();
367 assert (MO.isExpr() && "Unexpected branch target type!");
368 const MCExpr *Expr = MO.getExpr();
369 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
370 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
371
372 // All of the information is in the fixup.
373 return 0;
374}
375
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000376/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000377uint32_t ARMMCCodeEmitter::
378getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
379 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000380 // {17-13} = reg
381 // {12} = (U)nsigned (add == '1', sub == '0')
382 // {11-0} = imm12
383 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000384 bool isAdd = true;
385 // If The first operand isn't a register, we have a label reference.
386 const MCOperand &MO = MI.getOperand(OpIdx);
387 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000388 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000389 Imm12 = 0;
390
391 assert(MO.isExpr() && "Unexpected machine operand type!");
392 const MCExpr *Expr = MO.getExpr();
393 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
394 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
395
396 ++MCNumCPRelocations;
397 } else
398 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000399
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000400 uint32_t Binary = Imm12 & 0xfff;
401 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000402 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000403 Binary |= (1 << 12);
404 Binary |= (Reg << 13);
405 return Binary;
406}
407
Jim Grosbach54fea632010-11-09 17:20:53 +0000408uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000409getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
410 SmallVectorImpl<MCFixup> &Fixups) const {
411 // {20-16} = imm{15-12}
412 // {11-0} = imm{11-0}
413 const MCOperand &MO = MI.getOperand(OpIdx);
414 if (MO.isImm()) {
415 return static_cast<unsigned>(MO.getImm());
416 } else if (const MCSymbolRefExpr *Expr =
417 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
418 MCFixupKind Kind;
419 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000420 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000421 case MCSymbolRefExpr::VK_ARM_HI16:
422 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
423 break;
424 case MCSymbolRefExpr::VK_ARM_LO16:
425 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
426 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000427 }
428 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
429 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000430 };
431 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000432 return 0;
433}
434
435uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000436getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
437 SmallVectorImpl<MCFixup> &Fixups) const {
438 const MCOperand &MO = MI.getOperand(OpIdx);
439 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
440 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
441 unsigned Rn = getARMRegisterNumbering(MO.getReg());
442 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000443 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
444 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000445 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
446 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000447
448 // {16-13} = Rn
449 // {12} = isAdd
450 // {11-0} = shifter
451 // {3-0} = Rm
452 // {4} = 0
453 // {6-5} = type
454 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000455 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000456 Binary |= Rn << 13;
457 Binary |= SBits << 5;
458 Binary |= ShImm << 7;
459 if (isAdd)
460 Binary |= 1 << 12;
461 return Binary;
462}
463
Jim Grosbach570a9222010-11-11 01:09:40 +0000464uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000465getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
466 SmallVectorImpl<MCFixup> &Fixups) const {
467 // {17-14} Rn
468 // {13} 1 == imm12, 0 == Rm
469 // {12} isAdd
470 // {11-0} imm12/Rm
471 const MCOperand &MO = MI.getOperand(OpIdx);
472 unsigned Rn = getARMRegisterNumbering(MO.getReg());
473 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
474 Binary |= Rn << 14;
475 return Binary;
476}
477
478uint32_t ARMMCCodeEmitter::
479getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
480 SmallVectorImpl<MCFixup> &Fixups) const {
481 // {13} 1 == imm12, 0 == Rm
482 // {12} isAdd
483 // {11-0} imm12/Rm
484 const MCOperand &MO = MI.getOperand(OpIdx);
485 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
486 unsigned Imm = MO1.getImm();
487 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
488 bool isReg = MO.getReg() != 0;
489 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
490 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
491 if (isReg) {
492 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
493 Binary <<= 7; // Shift amount is bits [11:7]
494 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
495 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
496 }
497 return Binary | (isAdd << 12) | (isReg << 13);
498}
499
500uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000501getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
502 SmallVectorImpl<MCFixup> &Fixups) const {
503 // {9} 1 == imm8, 0 == Rm
504 // {8} isAdd
505 // {7-4} imm7_4/zero
506 // {3-0} imm3_0/Rm
507 const MCOperand &MO = MI.getOperand(OpIdx);
508 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
509 unsigned Imm = MO1.getImm();
510 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
511 bool isImm = MO.getReg() == 0;
512 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
513 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
514 if (!isImm)
515 Imm8 = getARMRegisterNumbering(MO.getReg());
516 return Imm8 | (isAdd << 8) | (isImm << 9);
517}
518
519uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000520getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
521 SmallVectorImpl<MCFixup> &Fixups) const {
522 // {13} 1 == imm8, 0 == Rm
523 // {12-9} Rn
524 // {8} isAdd
525 // {7-4} imm7_4/zero
526 // {3-0} imm3_0/Rm
527 const MCOperand &MO = MI.getOperand(OpIdx);
528 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
529 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
530 unsigned Rn = getARMRegisterNumbering(MO.getReg());
531 unsigned Imm = MO2.getImm();
532 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
533 bool isImm = MO1.getReg() == 0;
534 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
535 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
536 if (!isImm)
537 Imm8 = getARMRegisterNumbering(MO1.getReg());
538 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
539}
540
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000541/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000542uint32_t ARMMCCodeEmitter::
543getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
544 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000545 // {12-9} = reg
546 // {8} = (U)nsigned (add == '1', sub == '0')
547 // {7-0} = imm8
548 unsigned Reg, Imm8;
Jim Grosbach70933262010-11-04 01:12:30 +0000549 // If The first operand isn't a register, we have a label reference.
550 const MCOperand &MO = MI.getOperand(OpIdx);
551 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000552 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000553 Imm8 = 0;
554
555 assert(MO.isExpr() && "Unexpected machine operand type!");
556 const MCExpr *Expr = MO.getExpr();
557 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
558 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
559
560 ++MCNumCPRelocations;
561 } else
562 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000563
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000564 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
565 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
566 if (ARM_AM::getAM5Op(Imm8) == ARM_AM::add)
567 Binary |= (1 << 8);
568 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000569 return Binary;
570}
571
Jim Grosbach806e80e2010-11-03 23:52:49 +0000572unsigned ARMMCCodeEmitter::
573getSORegOpValue(const MCInst &MI, unsigned OpIdx,
574 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000575 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
576 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
577 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000578 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000579 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000580 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000581 // {6-5} = type
582 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000583 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000584 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000585 // else (imm shift)
586 // {11-7} = imm
587
588 const MCOperand &MO = MI.getOperand(OpIdx);
589 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
590 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
591 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
592
593 // Encode Rm.
594 unsigned Binary = getARMRegisterNumbering(MO.getReg());
595
596 // Encode the shift opcode.
597 unsigned SBits = 0;
598 unsigned Rs = MO1.getReg();
599 if (Rs) {
600 // Set shift operand (bit[7:4]).
601 // LSL - 0001
602 // LSR - 0011
603 // ASR - 0101
604 // ROR - 0111
605 // RRX - 0110 and bit[11:8] clear.
606 switch (SOpc) {
607 default: llvm_unreachable("Unknown shift opc!");
608 case ARM_AM::lsl: SBits = 0x1; break;
609 case ARM_AM::lsr: SBits = 0x3; break;
610 case ARM_AM::asr: SBits = 0x5; break;
611 case ARM_AM::ror: SBits = 0x7; break;
612 case ARM_AM::rrx: SBits = 0x6; break;
613 }
614 } else {
615 // Set shift operand (bit[6:4]).
616 // LSL - 000
617 // LSR - 010
618 // ASR - 100
619 // ROR - 110
620 switch (SOpc) {
621 default: llvm_unreachable("Unknown shift opc!");
622 case ARM_AM::lsl: SBits = 0x0; break;
623 case ARM_AM::lsr: SBits = 0x2; break;
624 case ARM_AM::asr: SBits = 0x4; break;
625 case ARM_AM::ror: SBits = 0x6; break;
626 }
627 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000628
Jim Grosbachef324d72010-10-12 23:53:58 +0000629 Binary |= SBits << 4;
630 if (SOpc == ARM_AM::rrx)
631 return Binary;
632
633 // Encode the shift operation Rs or shift_imm (except rrx).
634 if (Rs) {
635 // Encode Rs bit[11:8].
636 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
637 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
638 }
639
640 // Encode shift_imm bit[11:7].
641 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
642}
643
Jim Grosbach806e80e2010-11-03 23:52:49 +0000644unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000645getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
646 SmallVectorImpl<MCFixup> &Fixups) const {
647 const MCOperand &MO1 = MI.getOperand(OpNum);
648 const MCOperand &MO2 = MI.getOperand(OpNum+1);
649 const MCOperand &MO3 = MI.getOperand(OpNum+2);
650
651 // Encoded as [Rn, Rm, imm].
652 // FIXME: Needs fixup support.
653 unsigned Value = getARMRegisterNumbering(MO1.getReg());
654 Value <<= 4;
655 Value |= getARMRegisterNumbering(MO2.getReg());
656 Value <<= 2;
657 Value |= MO3.getImm();
658
659 return Value;
660}
661
662unsigned ARMMCCodeEmitter::
663getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
664 SmallVectorImpl<MCFixup> &Fixups) const {
665 const MCOperand &MO1 = MI.getOperand(OpNum);
666 const MCOperand &MO2 = MI.getOperand(OpNum+1);
667
668 // FIXME: Needs fixup support.
669 unsigned Value = getARMRegisterNumbering(MO1.getReg());
670
671 // Even though the immediate is 8 bits long, we need 9 bits in order
672 // to represent the (inverse of the) sign bit.
673 Value <<= 9;
674 Value |= ((int32_t)MO2.getImm()) & 511;
675 Value ^= 256; // Invert the sign bit.
676 return Value;
677}
678
679unsigned ARMMCCodeEmitter::
680getT2AddrModeImm12OpValue(const MCInst &MI, unsigned OpNum,
681 SmallVectorImpl<MCFixup> &Fixups) const {
682 const MCOperand &MO1 = MI.getOperand(OpNum);
683 const MCOperand &MO2 = MI.getOperand(OpNum+1);
684
685 // FIXME: Needs fixup support.
686 unsigned Value = getARMRegisterNumbering(MO1.getReg());
687 Value <<= 12;
688 Value |= MO2.getImm() & 4095;
689 return Value;
690}
691
692unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000693getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
694 SmallVectorImpl<MCFixup> &Fixups) const {
695 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
696 // shifted. The second is the amount to shift by.
697 //
698 // {3-0} = Rm.
699 // {4} = 0
700 // {6-5} = type
701 // {11-7} = imm
702
703 const MCOperand &MO = MI.getOperand(OpIdx);
704 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
705 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
706
707 // Encode Rm.
708 unsigned Binary = getARMRegisterNumbering(MO.getReg());
709
710 // Encode the shift opcode.
711 unsigned SBits = 0;
712 // Set shift operand (bit[6:4]).
713 // LSL - 000
714 // LSR - 010
715 // ASR - 100
716 // ROR - 110
717 switch (SOpc) {
718 default: llvm_unreachable("Unknown shift opc!");
719 case ARM_AM::lsl: SBits = 0x0; break;
720 case ARM_AM::lsr: SBits = 0x2; break;
721 case ARM_AM::asr: SBits = 0x4; break;
722 case ARM_AM::ror: SBits = 0x6; break;
723 }
724
725 Binary |= SBits << 4;
726 if (SOpc == ARM_AM::rrx)
727 return Binary;
728
729 // Encode shift_imm bit[11:7].
730 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
731}
732
733unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000734getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
735 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000736 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
737 // msb of the mask.
738 const MCOperand &MO = MI.getOperand(Op);
739 uint32_t v = ~MO.getImm();
740 uint32_t lsb = CountTrailingZeros_32(v);
741 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
742 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
743 return lsb | (msb << 5);
744}
745
Jim Grosbach806e80e2010-11-03 23:52:49 +0000746unsigned ARMMCCodeEmitter::
747getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000748 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000749 // VLDM/VSTM:
750 // {12-8} = Vd
751 // {7-0} = Number of registers
752 //
753 // LDM/STM:
754 // {15-0} = Bitfield of GPRs.
755 unsigned Reg = MI.getOperand(Op).getReg();
756 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
757 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
758
Bill Wendling5e559a22010-11-09 00:30:18 +0000759 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000760
761 if (SPRRegs || DPRRegs) {
762 // VLDM/VSTM
763 unsigned RegNo = getARMRegisterNumbering(Reg);
764 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
765 Binary |= (RegNo & 0x1f) << 8;
766 if (SPRRegs)
767 Binary |= NumRegs;
768 else
769 Binary |= NumRegs * 2;
770 } else {
771 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
772 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
773 Binary |= 1 << RegNo;
774 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000775 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000776
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000777 return Binary;
778}
779
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000780/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
781/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000782unsigned ARMMCCodeEmitter::
783getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
784 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000785 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000786 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000787
Owen Andersond9aa7d32010-11-02 00:05:05 +0000788 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000789 unsigned Align = 0;
790
791 switch (Imm.getImm()) {
792 default: break;
793 case 2:
794 case 4:
795 case 8: Align = 0x01; break;
796 case 16: Align = 0x02; break;
797 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000798 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000799
Owen Andersond9aa7d32010-11-02 00:05:05 +0000800 return RegNo | (Align << 4);
801}
802
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000803/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
804/// alignment operand for use in VLD-dup instructions. This is the same as
805/// getAddrMode6AddressOpValue except for the alignment encoding, which is
806/// different for VLD4-dup.
807unsigned ARMMCCodeEmitter::
808getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
809 SmallVectorImpl<MCFixup> &Fixups) const {
810 const MCOperand &Reg = MI.getOperand(Op);
811 const MCOperand &Imm = MI.getOperand(Op + 1);
812
813 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
814 unsigned Align = 0;
815
816 switch (Imm.getImm()) {
817 default: break;
818 case 2:
819 case 4:
820 case 8: Align = 0x01; break;
821 case 16: Align = 0x03; break;
822 }
823
824 return RegNo | (Align << 4);
825}
826
Jim Grosbach806e80e2010-11-03 23:52:49 +0000827unsigned ARMMCCodeEmitter::
828getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
829 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000830 const MCOperand &MO = MI.getOperand(Op);
831 if (MO.getReg() == 0) return 0x0D;
832 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000833}
834
Jim Grosbach568eeed2010-09-17 18:46:17 +0000835void ARMMCCodeEmitter::
836EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000837 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000838 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000839 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000840 uint64_t TSFlags = Desc.TSFlags;
841 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000842 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000843 int Size;
844 // Basic size info comes from the TSFlags field.
845 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
846 default: llvm_unreachable("Unexpected instruction size!");
847 case ARMII::Size2Bytes: Size = 2; break;
848 case ARMII::Size4Bytes: Size = 4; break;
849 }
850 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000851 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000852}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000853
Jim Grosbach806e80e2010-11-03 23:52:49 +0000854#include "ARMGenMCCodeEmitter.inc"