blob: 207332fba729081a2423a14df90e5a33bff42815 [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 },
Owen Anderson9d63d902010-12-01 19:18:46 +000050 { "fixup_arm_pcrel_10", 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
Owen Anderson9d63d902010-12-01 19:18:46 +000093 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
94 /// operand.
95 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
98
Jim Grosbach54fea632010-11-09 17:20:53 +000099 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
100 /// operand as needed by load/store instructions.
101 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
103
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000104 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
105 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const {
107 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
108 switch (Mode) {
109 default: assert(0 && "Unknown addressing sub-mode!");
110 case ARM_AM::da: return 0;
111 case ARM_AM::ia: return 1;
112 case ARM_AM::db: return 2;
113 case ARM_AM::ib: return 3;
114 }
115 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000116 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
117 ///
118 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
119 switch (ShOpc) {
120 default: llvm_unreachable("Unknown shift opc!");
121 case ARM_AM::no_shift:
122 case ARM_AM::lsl: return 0;
123 case ARM_AM::lsr: return 1;
124 case ARM_AM::asr: return 2;
125 case ARM_AM::ror:
126 case ARM_AM::rrx: return 3;
127 }
128 return 0;
129 }
130
131 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
132 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134
135 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
136 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
138
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000139 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
140 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
142
Jim Grosbach570a9222010-11-11 01:09:40 +0000143 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
144 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000146
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000147 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
148 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Bill Wendling1fd374e2010-11-30 22:57:21 +0000151 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
152 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
154
155 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
156 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const;
158
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000159 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000160 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
161 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000162
Jim Grosbach08bd5492010-10-12 23:00:24 +0000163 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000164 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
165 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000166 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
167 // '1' respectively.
168 return MI.getOperand(Op).getReg() == ARM::CPSR;
169 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000170
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000171 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000172 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
173 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000174 unsigned SoImm = MI.getOperand(Op).getImm();
175 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
176 assert(SoImmVal != -1 && "Not a valid so_imm value!");
177
178 // Encode rotate_imm.
179 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
180 << ARMII::SoRotImmShift;
181
182 // Encode immed_8.
183 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
184 return Binary;
185 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000186
187 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
188 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
189 SmallVectorImpl<MCFixup> &Fixups) const {
190 unsigned SoImm = MI.getOperand(Op).getImm();
191 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
192 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
193 return Encoded;
194 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000195
Owen Anderson75579f72010-11-29 22:44:32 +0000196 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
197 SmallVectorImpl<MCFixup> &Fixups) const;
198 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
199 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000200 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
201 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000202 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
203 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000204
Jim Grosbachef324d72010-10-12 23:53:58 +0000205 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000206 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
207 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000208 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000210
Jim Grosbach806e80e2010-11-03 23:52:49 +0000211 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
212 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000213 switch (MI.getOperand(Op).getImm()) {
214 default: assert (0 && "Not a valid rot_imm value!");
215 case 0: return 0;
216 case 8: return 1;
217 case 16: return 2;
218 case 24: return 3;
219 }
220 }
221
Jim Grosbach806e80e2010-11-03 23:52:49 +0000222 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
223 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000224 return MI.getOperand(Op).getImm() - 1;
225 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000226
Jim Grosbach806e80e2010-11-03 23:52:49 +0000227 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000229 return 64 - MI.getOperand(Op).getImm();
230 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000231
Jim Grosbach806e80e2010-11-03 23:52:49 +0000232 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000234
Jim Grosbach806e80e2010-11-03 23:52:49 +0000235 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
237 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
238 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000239 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
240 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000241 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000243
Owen Andersonc7139a62010-11-11 19:07:48 +0000244 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
245 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000246 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
247 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000248 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
249 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000250
Jim Grosbach70933262010-11-04 01:12:30 +0000251 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000252 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000253 }
254
Jim Grosbach70933262010-11-04 01:12:30 +0000255 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000256 // Output the constant in little endian byte order.
257 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000258 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000259 Val >>= 8;
260 }
261 }
262
Jim Grosbach568eeed2010-09-17 18:46:17 +0000263 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
264 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000265};
266
267} // end anonymous namespace
268
Bill Wendling0800ce72010-11-02 22:53:11 +0000269MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
270 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000271 return new ARMMCCodeEmitter(TM, Ctx);
272}
273
Owen Anderson57dac882010-11-11 21:36:43 +0000274/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000275/// instructions, and rewrite them to their Thumb2 form if we are currently in
276/// Thumb2 mode.
277unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
278 unsigned EncodedValue) const {
279 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
280 if (Subtarget.isThumb2()) {
281 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
282 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
283 // set to 1111.
284 unsigned Bit24 = EncodedValue & 0x01000000;
285 unsigned Bit28 = Bit24 << 4;
286 EncodedValue &= 0xEFFFFFFF;
287 EncodedValue |= Bit28;
288 EncodedValue |= 0x0F000000;
289 }
290
291 return EncodedValue;
292}
293
Owen Anderson57dac882010-11-11 21:36:43 +0000294/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
295/// instructions, and rewrite them to their Thumb2 form if we are currently in
296/// Thumb2 mode.
297unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
298 unsigned EncodedValue) const {
299 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
300 if (Subtarget.isThumb2()) {
301 EncodedValue &= 0xF0FFFFFF;
302 EncodedValue |= 0x09000000;
303 }
304
305 return EncodedValue;
306}
307
Owen Anderson8f143912010-11-11 23:12:55 +0000308/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
309/// instructions, and rewrite them to their Thumb2 form if we are currently in
310/// Thumb2 mode.
311unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
312 unsigned EncodedValue) const {
313 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
314 if (Subtarget.isThumb2()) {
315 EncodedValue &= 0x00FFFFFF;
316 EncodedValue |= 0xEE000000;
317 }
318
319 return EncodedValue;
320}
321
322
Owen Anderson57dac882010-11-11 21:36:43 +0000323
Jim Grosbach56ac9072010-10-08 21:45:55 +0000324/// getMachineOpValue - Return binary encoding of operand. If the machine
325/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000326unsigned ARMMCCodeEmitter::
327getMachineOpValue(const MCInst &MI, const MCOperand &MO,
328 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000329 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000330 unsigned Reg = MO.getReg();
331 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000332
Jim Grosbachb0708d22010-11-30 23:51:41 +0000333 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000334 switch (Reg) {
335 default:
336 return RegNo;
337 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
338 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
339 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
340 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
341 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000342 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000343 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000344 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000345 } else if (MO.isFPImm()) {
346 return static_cast<unsigned>(APFloat(MO.getFPImm())
347 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000348 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000349
Jim Grosbach817c1a62010-11-19 00:27:09 +0000350 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000351 return 0;
352}
353
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000354/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000355bool ARMMCCodeEmitter::
356EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
357 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000358 const MCOperand &MO = MI.getOperand(OpIdx);
359 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000360
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000361 Reg = getARMRegisterNumbering(MO.getReg());
362
363 int32_t SImm = MO1.getImm();
364 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000365
Jim Grosbachab682a22010-10-28 18:34:10 +0000366 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000367 if (SImm == INT32_MIN)
368 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000369
Jim Grosbachab682a22010-10-28 18:34:10 +0000370 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000371 if (SImm < 0) {
372 SImm = -SImm;
373 isAdd = false;
374 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000375
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000376 Imm = SImm;
377 return isAdd;
378}
379
Jim Grosbachc466b932010-11-11 18:04:49 +0000380/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
381/// branch target.
382uint32_t ARMMCCodeEmitter::
383getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
384 SmallVectorImpl<MCFixup> &Fixups) const {
385 const MCOperand &MO = MI.getOperand(OpIdx);
386
387 // If the destination is an immediate, we have nothing to do.
388 if (MO.isImm()) return MO.getImm();
389 assert (MO.isExpr() && "Unexpected branch target type!");
390 const MCExpr *Expr = MO.getExpr();
391 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
392 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
393
394 // All of the information is in the fixup.
395 return 0;
396}
397
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000398/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000399uint32_t ARMMCCodeEmitter::
400getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
401 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000402 // {17-13} = reg
403 // {12} = (U)nsigned (add == '1', sub == '0')
404 // {11-0} = imm12
405 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000406 bool isAdd = true;
407 // If The first operand isn't a register, we have a label reference.
408 const MCOperand &MO = MI.getOperand(OpIdx);
409 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000410 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000411 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000412 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000413
414 assert(MO.isExpr() && "Unexpected machine operand type!");
415 const MCExpr *Expr = MO.getExpr();
416 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
417 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
418
419 ++MCNumCPRelocations;
420 } else
421 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000422
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000423 uint32_t Binary = Imm12 & 0xfff;
424 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000425 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000426 Binary |= (1 << 12);
427 Binary |= (Reg << 13);
428 return Binary;
429}
430
Owen Anderson9d63d902010-12-01 19:18:46 +0000431/// getT2AddrModeImm8s4OpValue - Return encoding info for
432/// 'reg +/- imm8<<2' operand.
433uint32_t ARMMCCodeEmitter::
434getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
435 SmallVectorImpl<MCFixup> &Fixups) const {
436 // {17-13} = reg
437 // {12} = (U)nsigned (add == '1', sub == '0')
438 // {11-0} = imm8
439 unsigned Reg, Imm8;
440 bool isAdd = true;
441 // If The first operand isn't a register, we have a label reference.
442 const MCOperand &MO = MI.getOperand(OpIdx);
443 if (!MO.isReg()) {
444 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
445 Imm8 = 0;
446 isAdd = false ; // 'U' bit is set as part of the fixup.
447
448 assert(MO.isExpr() && "Unexpected machine operand type!");
449 const MCExpr *Expr = MO.getExpr();
450 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
451 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
452
453 ++MCNumCPRelocations;
454 } else
455 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
456
457 uint32_t Binary = (Imm8 >> 2) & 0xff;
458 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
459 if (isAdd)
460 Binary |= (1 << 9);
461 Binary |= (Reg << 9);
462 return Binary;
463}
464
Jim Grosbach54fea632010-11-09 17:20:53 +0000465uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000466getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
467 SmallVectorImpl<MCFixup> &Fixups) const {
468 // {20-16} = imm{15-12}
469 // {11-0} = imm{11-0}
470 const MCOperand &MO = MI.getOperand(OpIdx);
471 if (MO.isImm()) {
472 return static_cast<unsigned>(MO.getImm());
473 } else if (const MCSymbolRefExpr *Expr =
474 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
475 MCFixupKind Kind;
476 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000477 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000478 case MCSymbolRefExpr::VK_ARM_HI16:
479 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
480 break;
481 case MCSymbolRefExpr::VK_ARM_LO16:
482 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
483 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000484 }
485 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
486 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000487 };
488 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000489 return 0;
490}
491
492uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000493getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
495 const MCOperand &MO = MI.getOperand(OpIdx);
496 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
497 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
498 unsigned Rn = getARMRegisterNumbering(MO.getReg());
499 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000500 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
501 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000502 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
503 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000504
505 // {16-13} = Rn
506 // {12} = isAdd
507 // {11-0} = shifter
508 // {3-0} = Rm
509 // {4} = 0
510 // {6-5} = type
511 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000512 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000513 Binary |= Rn << 13;
514 Binary |= SBits << 5;
515 Binary |= ShImm << 7;
516 if (isAdd)
517 Binary |= 1 << 12;
518 return Binary;
519}
520
Jim Grosbach570a9222010-11-11 01:09:40 +0000521uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000522getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
523 SmallVectorImpl<MCFixup> &Fixups) const {
524 // {17-14} Rn
525 // {13} 1 == imm12, 0 == Rm
526 // {12} isAdd
527 // {11-0} imm12/Rm
528 const MCOperand &MO = MI.getOperand(OpIdx);
529 unsigned Rn = getARMRegisterNumbering(MO.getReg());
530 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
531 Binary |= Rn << 14;
532 return Binary;
533}
534
535uint32_t ARMMCCodeEmitter::
536getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
537 SmallVectorImpl<MCFixup> &Fixups) const {
538 // {13} 1 == imm12, 0 == Rm
539 // {12} isAdd
540 // {11-0} imm12/Rm
541 const MCOperand &MO = MI.getOperand(OpIdx);
542 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
543 unsigned Imm = MO1.getImm();
544 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
545 bool isReg = MO.getReg() != 0;
546 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
547 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
548 if (isReg) {
549 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
550 Binary <<= 7; // Shift amount is bits [11:7]
551 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
552 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
553 }
554 return Binary | (isAdd << 12) | (isReg << 13);
555}
556
557uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000558getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
559 SmallVectorImpl<MCFixup> &Fixups) const {
560 // {9} 1 == imm8, 0 == Rm
561 // {8} isAdd
562 // {7-4} imm7_4/zero
563 // {3-0} imm3_0/Rm
564 const MCOperand &MO = MI.getOperand(OpIdx);
565 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
566 unsigned Imm = MO1.getImm();
567 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
568 bool isImm = MO.getReg() == 0;
569 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
570 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
571 if (!isImm)
572 Imm8 = getARMRegisterNumbering(MO.getReg());
573 return Imm8 | (isAdd << 8) | (isImm << 9);
574}
575
576uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000577getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
578 SmallVectorImpl<MCFixup> &Fixups) const {
579 // {13} 1 == imm8, 0 == Rm
580 // {12-9} Rn
581 // {8} isAdd
582 // {7-4} imm7_4/zero
583 // {3-0} imm3_0/Rm
584 const MCOperand &MO = MI.getOperand(OpIdx);
585 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
586 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
587 unsigned Rn = getARMRegisterNumbering(MO.getReg());
588 unsigned Imm = MO2.getImm();
589 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
590 bool isImm = MO1.getReg() == 0;
591 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
592 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
593 if (!isImm)
594 Imm8 = getARMRegisterNumbering(MO1.getReg());
595 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
596}
597
Bill Wendling1fd374e2010-11-30 22:57:21 +0000598/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
599static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
600 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000601 // [Rn, Rm]
602 // {5-3} = Rm
603 // {2-0} = Rn
604 //
605 // [Rn, #imm]
606 // {7-3} = imm5
607 // {2-0} = Rn
608 const MCOperand &MO = MI.getOperand(OpIdx);
609 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
610 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
611 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000612 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000613 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
614 return (Rm << 3) | (Imm5 << 3) | Rn;
615}
616
Bill Wendling1fd374e2010-11-30 22:57:21 +0000617/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
618uint32_t ARMMCCodeEmitter::
619getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &) const {
621 return getAddrModeSOpValue(MI, OpIdx, 4);
622}
623
624/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
625uint32_t ARMMCCodeEmitter::
626getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &) const {
628 return getAddrModeSOpValue(MI, OpIdx, 2);
629}
630
631/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
632uint32_t ARMMCCodeEmitter::
633getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
634 SmallVectorImpl<MCFixup> &) const {
635 return getAddrModeSOpValue(MI, OpIdx, 1);
636}
637
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000638/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000639uint32_t ARMMCCodeEmitter::
640getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
641 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000642 // {12-9} = reg
643 // {8} = (U)nsigned (add == '1', sub == '0')
644 // {7-0} = imm8
645 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000646 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000647 // If The first operand isn't a register, we have a label reference.
648 const MCOperand &MO = MI.getOperand(OpIdx);
649 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000650 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000651 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000652 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000653
654 assert(MO.isExpr() && "Unexpected machine operand type!");
655 const MCExpr *Expr = MO.getExpr();
Owen Anderson9d63d902010-12-01 19:18:46 +0000656 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000657 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
658
659 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000660 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000661 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000662 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
663 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000664
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000665 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
666 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000667 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000668 Binary |= (1 << 8);
669 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000670 return Binary;
671}
672
Jim Grosbach806e80e2010-11-03 23:52:49 +0000673unsigned ARMMCCodeEmitter::
674getSORegOpValue(const MCInst &MI, unsigned OpIdx,
675 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000676 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
677 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
678 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000679 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000680 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000681 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000682 // {6-5} = type
683 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000684 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000685 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000686 // else (imm shift)
687 // {11-7} = imm
688
689 const MCOperand &MO = MI.getOperand(OpIdx);
690 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
691 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
692 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
693
694 // Encode Rm.
695 unsigned Binary = getARMRegisterNumbering(MO.getReg());
696
697 // Encode the shift opcode.
698 unsigned SBits = 0;
699 unsigned Rs = MO1.getReg();
700 if (Rs) {
701 // Set shift operand (bit[7:4]).
702 // LSL - 0001
703 // LSR - 0011
704 // ASR - 0101
705 // ROR - 0111
706 // RRX - 0110 and bit[11:8] clear.
707 switch (SOpc) {
708 default: llvm_unreachable("Unknown shift opc!");
709 case ARM_AM::lsl: SBits = 0x1; break;
710 case ARM_AM::lsr: SBits = 0x3; break;
711 case ARM_AM::asr: SBits = 0x5; break;
712 case ARM_AM::ror: SBits = 0x7; break;
713 case ARM_AM::rrx: SBits = 0x6; break;
714 }
715 } else {
716 // Set shift operand (bit[6:4]).
717 // LSL - 000
718 // LSR - 010
719 // ASR - 100
720 // ROR - 110
721 switch (SOpc) {
722 default: llvm_unreachable("Unknown shift opc!");
723 case ARM_AM::lsl: SBits = 0x0; break;
724 case ARM_AM::lsr: SBits = 0x2; break;
725 case ARM_AM::asr: SBits = 0x4; break;
726 case ARM_AM::ror: SBits = 0x6; break;
727 }
728 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000729
Jim Grosbachef324d72010-10-12 23:53:58 +0000730 Binary |= SBits << 4;
731 if (SOpc == ARM_AM::rrx)
732 return Binary;
733
734 // Encode the shift operation Rs or shift_imm (except rrx).
735 if (Rs) {
736 // Encode Rs bit[11:8].
737 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
738 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
739 }
740
741 // Encode shift_imm bit[11:7].
742 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
743}
744
Jim Grosbach806e80e2010-11-03 23:52:49 +0000745unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000746getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
747 SmallVectorImpl<MCFixup> &Fixups) const {
748 const MCOperand &MO1 = MI.getOperand(OpNum);
749 const MCOperand &MO2 = MI.getOperand(OpNum+1);
750 const MCOperand &MO3 = MI.getOperand(OpNum+2);
751
752 // Encoded as [Rn, Rm, imm].
753 // FIXME: Needs fixup support.
754 unsigned Value = getARMRegisterNumbering(MO1.getReg());
755 Value <<= 4;
756 Value |= getARMRegisterNumbering(MO2.getReg());
757 Value <<= 2;
758 Value |= MO3.getImm();
759
760 return Value;
761}
762
763unsigned ARMMCCodeEmitter::
764getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
765 SmallVectorImpl<MCFixup> &Fixups) const {
766 const MCOperand &MO1 = MI.getOperand(OpNum);
767 const MCOperand &MO2 = MI.getOperand(OpNum+1);
768
769 // FIXME: Needs fixup support.
770 unsigned Value = getARMRegisterNumbering(MO1.getReg());
771
772 // Even though the immediate is 8 bits long, we need 9 bits in order
773 // to represent the (inverse of the) sign bit.
774 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000775 int32_t tmp = (int32_t)MO2.getImm();
776 if (tmp < 0)
777 tmp = abs(tmp);
778 else
779 Value |= 256; // Set the ADD bit
780 Value |= tmp & 255;
781 return Value;
782}
783
784unsigned ARMMCCodeEmitter::
785getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
786 SmallVectorImpl<MCFixup> &Fixups) const {
787 const MCOperand &MO1 = MI.getOperand(OpNum);
788
789 // FIXME: Needs fixup support.
790 unsigned Value = 0;
791 int32_t tmp = (int32_t)MO1.getImm();
792 if (tmp < 0)
793 tmp = abs(tmp);
794 else
795 Value |= 256; // Set the ADD bit
796 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000797 return Value;
798}
799
800unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000801getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
802 SmallVectorImpl<MCFixup> &Fixups) const {
803 const MCOperand &MO1 = MI.getOperand(OpNum);
804
805 // FIXME: Needs fixup support.
806 unsigned Value = 0;
807 int32_t tmp = (int32_t)MO1.getImm();
808 if (tmp < 0)
809 tmp = abs(tmp);
810 else
811 Value |= 4096; // Set the ADD bit
812 Value |= tmp & 4095;
813 return Value;
814}
815
816unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000817getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
818 SmallVectorImpl<MCFixup> &Fixups) const {
819 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
820 // shifted. The second is the amount to shift by.
821 //
822 // {3-0} = Rm.
823 // {4} = 0
824 // {6-5} = type
825 // {11-7} = imm
826
827 const MCOperand &MO = MI.getOperand(OpIdx);
828 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
829 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
830
831 // Encode Rm.
832 unsigned Binary = getARMRegisterNumbering(MO.getReg());
833
834 // Encode the shift opcode.
835 unsigned SBits = 0;
836 // Set shift operand (bit[6:4]).
837 // LSL - 000
838 // LSR - 010
839 // ASR - 100
840 // ROR - 110
841 switch (SOpc) {
842 default: llvm_unreachable("Unknown shift opc!");
843 case ARM_AM::lsl: SBits = 0x0; break;
844 case ARM_AM::lsr: SBits = 0x2; break;
845 case ARM_AM::asr: SBits = 0x4; break;
846 case ARM_AM::ror: SBits = 0x6; break;
847 }
848
849 Binary |= SBits << 4;
850 if (SOpc == ARM_AM::rrx)
851 return Binary;
852
853 // Encode shift_imm bit[11:7].
854 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
855}
856
857unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000858getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
859 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000860 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
861 // msb of the mask.
862 const MCOperand &MO = MI.getOperand(Op);
863 uint32_t v = ~MO.getImm();
864 uint32_t lsb = CountTrailingZeros_32(v);
865 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
866 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
867 return lsb | (msb << 5);
868}
869
Jim Grosbach806e80e2010-11-03 23:52:49 +0000870unsigned ARMMCCodeEmitter::
871getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000872 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000873 // VLDM/VSTM:
874 // {12-8} = Vd
875 // {7-0} = Number of registers
876 //
877 // LDM/STM:
878 // {15-0} = Bitfield of GPRs.
879 unsigned Reg = MI.getOperand(Op).getReg();
880 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
881 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
882
Bill Wendling5e559a22010-11-09 00:30:18 +0000883 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000884
885 if (SPRRegs || DPRRegs) {
886 // VLDM/VSTM
887 unsigned RegNo = getARMRegisterNumbering(Reg);
888 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
889 Binary |= (RegNo & 0x1f) << 8;
890 if (SPRRegs)
891 Binary |= NumRegs;
892 else
893 Binary |= NumRegs * 2;
894 } else {
895 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
896 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
897 Binary |= 1 << RegNo;
898 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000899 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000900
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000901 return Binary;
902}
903
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000904/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
905/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000906unsigned ARMMCCodeEmitter::
907getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
908 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000909 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000910 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000911
Owen Andersond9aa7d32010-11-02 00:05:05 +0000912 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000913 unsigned Align = 0;
914
915 switch (Imm.getImm()) {
916 default: break;
917 case 2:
918 case 4:
919 case 8: Align = 0x01; break;
920 case 16: Align = 0x02; break;
921 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000922 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000923
Owen Andersond9aa7d32010-11-02 00:05:05 +0000924 return RegNo | (Align << 4);
925}
926
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000927/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
928/// alignment operand for use in VLD-dup instructions. This is the same as
929/// getAddrMode6AddressOpValue except for the alignment encoding, which is
930/// different for VLD4-dup.
931unsigned ARMMCCodeEmitter::
932getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
933 SmallVectorImpl<MCFixup> &Fixups) const {
934 const MCOperand &Reg = MI.getOperand(Op);
935 const MCOperand &Imm = MI.getOperand(Op + 1);
936
937 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
938 unsigned Align = 0;
939
940 switch (Imm.getImm()) {
941 default: break;
942 case 2:
943 case 4:
944 case 8: Align = 0x01; break;
945 case 16: Align = 0x03; break;
946 }
947
948 return RegNo | (Align << 4);
949}
950
Jim Grosbach806e80e2010-11-03 23:52:49 +0000951unsigned ARMMCCodeEmitter::
952getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
953 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000954 const MCOperand &MO = MI.getOperand(Op);
955 if (MO.getReg() == 0) return 0x0D;
956 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000957}
958
Jim Grosbach568eeed2010-09-17 18:46:17 +0000959void ARMMCCodeEmitter::
960EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000961 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000962 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000963 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000964 uint64_t TSFlags = Desc.TSFlags;
965 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000966 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000967 int Size;
968 // Basic size info comes from the TSFlags field.
969 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
970 default: llvm_unreachable("Unexpected instruction size!");
971 case ARMII::Size2Bytes: Size = 2; break;
972 case ARMII::Size4Bytes: Size = 4; break;
973 }
974 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000975 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000976}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000977
Jim Grosbach806e80e2010-11-03 23:52:49 +0000978#include "ARMGenMCCodeEmitter.inc"