blob: f687968947132d8e5c984f55ced30ca5130ad72a [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 },
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 Wendlingef4a68b2010-11-30 07:44:32 +0000139 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
140 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
142
Bill Wendling1fd374e2010-11-30 22:57:21 +0000143 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
144 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
146
147 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
148 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000151 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000152 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000154
Jim Grosbach08bd5492010-10-12 23:00:24 +0000155 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000156 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
157 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000158 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
159 // '1' respectively.
160 return MI.getOperand(Op).getReg() == ARM::CPSR;
161 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000162
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000163 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000164 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
165 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000166 unsigned SoImm = MI.getOperand(Op).getImm();
167 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
168 assert(SoImmVal != -1 && "Not a valid so_imm value!");
169
170 // Encode rotate_imm.
171 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
172 << ARMII::SoRotImmShift;
173
174 // Encode immed_8.
175 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
176 return Binary;
177 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000178
179 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
180 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
181 SmallVectorImpl<MCFixup> &Fixups) const {
182 unsigned SoImm = MI.getOperand(Op).getImm();
183 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
184 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
185 return Encoded;
186 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000187
Owen Anderson75579f72010-11-29 22:44:32 +0000188 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
189 SmallVectorImpl<MCFixup> &Fixups) const;
190 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
191 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000192 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
193 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000194 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
195 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000196
Jim Grosbachef324d72010-10-12 23:53:58 +0000197 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000198 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
199 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000200 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000202
Jim Grosbach806e80e2010-11-03 23:52:49 +0000203 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
204 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000205 switch (MI.getOperand(Op).getImm()) {
206 default: assert (0 && "Not a valid rot_imm value!");
207 case 0: return 0;
208 case 8: return 1;
209 case 16: return 2;
210 case 24: return 3;
211 }
212 }
213
Jim Grosbach806e80e2010-11-03 23:52:49 +0000214 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
215 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000216 return MI.getOperand(Op).getImm() - 1;
217 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000218
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
220 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000221 return 64 - MI.getOperand(Op).getImm();
222 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000223
Jim Grosbach806e80e2010-11-03 23:52:49 +0000224 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000226
Jim Grosbach806e80e2010-11-03 23:52:49 +0000227 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const;
229 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
230 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000231 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
232 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000233 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
234 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000235
Owen Andersonc7139a62010-11-11 19:07:48 +0000236 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
237 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000238 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
239 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000240 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
241 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000242
Jim Grosbach70933262010-11-04 01:12:30 +0000243 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000244 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000245 }
246
Jim Grosbach70933262010-11-04 01:12:30 +0000247 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000248 // Output the constant in little endian byte order.
249 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000250 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000251 Val >>= 8;
252 }
253 }
254
Jim Grosbach568eeed2010-09-17 18:46:17 +0000255 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
256 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000257};
258
259} // end anonymous namespace
260
Bill Wendling0800ce72010-11-02 22:53:11 +0000261MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
262 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000263 return new ARMMCCodeEmitter(TM, Ctx);
264}
265
Owen Anderson57dac882010-11-11 21:36:43 +0000266/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000267/// instructions, and rewrite them to their Thumb2 form if we are currently in
268/// Thumb2 mode.
269unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
270 unsigned EncodedValue) const {
271 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
272 if (Subtarget.isThumb2()) {
273 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
274 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
275 // set to 1111.
276 unsigned Bit24 = EncodedValue & 0x01000000;
277 unsigned Bit28 = Bit24 << 4;
278 EncodedValue &= 0xEFFFFFFF;
279 EncodedValue |= Bit28;
280 EncodedValue |= 0x0F000000;
281 }
282
283 return EncodedValue;
284}
285
Owen Anderson57dac882010-11-11 21:36:43 +0000286/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
287/// instructions, and rewrite them to their Thumb2 form if we are currently in
288/// Thumb2 mode.
289unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
290 unsigned EncodedValue) const {
291 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
292 if (Subtarget.isThumb2()) {
293 EncodedValue &= 0xF0FFFFFF;
294 EncodedValue |= 0x09000000;
295 }
296
297 return EncodedValue;
298}
299
Owen Anderson8f143912010-11-11 23:12:55 +0000300/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
301/// instructions, and rewrite them to their Thumb2 form if we are currently in
302/// Thumb2 mode.
303unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
304 unsigned EncodedValue) const {
305 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
306 if (Subtarget.isThumb2()) {
307 EncodedValue &= 0x00FFFFFF;
308 EncodedValue |= 0xEE000000;
309 }
310
311 return EncodedValue;
312}
313
314
Owen Anderson57dac882010-11-11 21:36:43 +0000315
Jim Grosbach56ac9072010-10-08 21:45:55 +0000316/// getMachineOpValue - Return binary encoding of operand. If the machine
317/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000318unsigned ARMMCCodeEmitter::
319getMachineOpValue(const MCInst &MI, const MCOperand &MO,
320 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000321 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000322 unsigned Reg = MO.getReg();
323 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000324
Owen Anderson90d4cf92010-10-21 20:49:13 +0000325 // Q registers are encodes as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000326 switch (Reg) {
327 default:
328 return RegNo;
329 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
330 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
331 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
332 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
333 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000334 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000335 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000336 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000337 } else if (MO.isFPImm()) {
338 return static_cast<unsigned>(APFloat(MO.getFPImm())
339 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000340 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000341
Jim Grosbach817c1a62010-11-19 00:27:09 +0000342 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000343 return 0;
344}
345
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000346/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000347bool ARMMCCodeEmitter::
348EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
349 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000350 const MCOperand &MO = MI.getOperand(OpIdx);
351 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000352
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000353 Reg = getARMRegisterNumbering(MO.getReg());
354
355 int32_t SImm = MO1.getImm();
356 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000357
Jim Grosbachab682a22010-10-28 18:34:10 +0000358 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000359 if (SImm == INT32_MIN)
360 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000361
Jim Grosbachab682a22010-10-28 18:34:10 +0000362 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000363 if (SImm < 0) {
364 SImm = -SImm;
365 isAdd = false;
366 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000367
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000368 Imm = SImm;
369 return isAdd;
370}
371
Jim Grosbachc466b932010-11-11 18:04:49 +0000372/// getBranchTargetOpValue - Return encoding info for 24-bit immediate
373/// branch target.
374uint32_t ARMMCCodeEmitter::
375getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
376 SmallVectorImpl<MCFixup> &Fixups) const {
377 const MCOperand &MO = MI.getOperand(OpIdx);
378
379 // If the destination is an immediate, we have nothing to do.
380 if (MO.isImm()) return MO.getImm();
381 assert (MO.isExpr() && "Unexpected branch target type!");
382 const MCExpr *Expr = MO.getExpr();
383 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_branch);
384 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
385
386 // All of the information is in the fixup.
387 return 0;
388}
389
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000390/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000391uint32_t ARMMCCodeEmitter::
392getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
393 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000394 // {17-13} = reg
395 // {12} = (U)nsigned (add == '1', sub == '0')
396 // {11-0} = imm12
397 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000398 bool isAdd = true;
399 // If The first operand isn't a register, we have a label reference.
400 const MCOperand &MO = MI.getOperand(OpIdx);
401 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000402 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000403 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000404 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000405
406 assert(MO.isExpr() && "Unexpected machine operand type!");
407 const MCExpr *Expr = MO.getExpr();
408 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_12);
409 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
410
411 ++MCNumCPRelocations;
412 } else
413 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000414
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000415 uint32_t Binary = Imm12 & 0xfff;
416 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000417 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000418 Binary |= (1 << 12);
419 Binary |= (Reg << 13);
420 return Binary;
421}
422
Jim Grosbach54fea632010-11-09 17:20:53 +0000423uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000424getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
425 SmallVectorImpl<MCFixup> &Fixups) const {
426 // {20-16} = imm{15-12}
427 // {11-0} = imm{11-0}
428 const MCOperand &MO = MI.getOperand(OpIdx);
429 if (MO.isImm()) {
430 return static_cast<unsigned>(MO.getImm());
431 } else if (const MCSymbolRefExpr *Expr =
432 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
433 MCFixupKind Kind;
434 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000435 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000436 case MCSymbolRefExpr::VK_ARM_HI16:
437 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
438 break;
439 case MCSymbolRefExpr::VK_ARM_LO16:
440 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
441 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000442 }
443 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
444 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000445 };
446 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000447 return 0;
448}
449
450uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000451getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
452 SmallVectorImpl<MCFixup> &Fixups) const {
453 const MCOperand &MO = MI.getOperand(OpIdx);
454 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
455 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
456 unsigned Rn = getARMRegisterNumbering(MO.getReg());
457 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000458 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
459 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000460 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
461 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000462
463 // {16-13} = Rn
464 // {12} = isAdd
465 // {11-0} = shifter
466 // {3-0} = Rm
467 // {4} = 0
468 // {6-5} = type
469 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000470 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000471 Binary |= Rn << 13;
472 Binary |= SBits << 5;
473 Binary |= ShImm << 7;
474 if (isAdd)
475 Binary |= 1 << 12;
476 return Binary;
477}
478
Jim Grosbach570a9222010-11-11 01:09:40 +0000479uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000480getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
481 SmallVectorImpl<MCFixup> &Fixups) const {
482 // {17-14} Rn
483 // {13} 1 == imm12, 0 == Rm
484 // {12} isAdd
485 // {11-0} imm12/Rm
486 const MCOperand &MO = MI.getOperand(OpIdx);
487 unsigned Rn = getARMRegisterNumbering(MO.getReg());
488 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
489 Binary |= Rn << 14;
490 return Binary;
491}
492
493uint32_t ARMMCCodeEmitter::
494getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
495 SmallVectorImpl<MCFixup> &Fixups) const {
496 // {13} 1 == imm12, 0 == Rm
497 // {12} isAdd
498 // {11-0} imm12/Rm
499 const MCOperand &MO = MI.getOperand(OpIdx);
500 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
501 unsigned Imm = MO1.getImm();
502 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
503 bool isReg = MO.getReg() != 0;
504 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
505 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
506 if (isReg) {
507 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
508 Binary <<= 7; // Shift amount is bits [11:7]
509 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
510 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
511 }
512 return Binary | (isAdd << 12) | (isReg << 13);
513}
514
515uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000516getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
517 SmallVectorImpl<MCFixup> &Fixups) const {
518 // {9} 1 == imm8, 0 == Rm
519 // {8} isAdd
520 // {7-4} imm7_4/zero
521 // {3-0} imm3_0/Rm
522 const MCOperand &MO = MI.getOperand(OpIdx);
523 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
524 unsigned Imm = MO1.getImm();
525 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
526 bool isImm = MO.getReg() == 0;
527 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
528 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
529 if (!isImm)
530 Imm8 = getARMRegisterNumbering(MO.getReg());
531 return Imm8 | (isAdd << 8) | (isImm << 9);
532}
533
534uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000535getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
536 SmallVectorImpl<MCFixup> &Fixups) const {
537 // {13} 1 == imm8, 0 == Rm
538 // {12-9} Rn
539 // {8} isAdd
540 // {7-4} imm7_4/zero
541 // {3-0} imm3_0/Rm
542 const MCOperand &MO = MI.getOperand(OpIdx);
543 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
544 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
545 unsigned Rn = getARMRegisterNumbering(MO.getReg());
546 unsigned Imm = MO2.getImm();
547 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
548 bool isImm = MO1.getReg() == 0;
549 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
550 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
551 if (!isImm)
552 Imm8 = getARMRegisterNumbering(MO1.getReg());
553 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
554}
555
Bill Wendling1fd374e2010-11-30 22:57:21 +0000556/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
557static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
558 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000559 // [Rn, Rm]
560 // {5-3} = Rm
561 // {2-0} = Rn
562 //
563 // [Rn, #imm]
564 // {7-3} = imm5
565 // {2-0} = Rn
566 const MCOperand &MO = MI.getOperand(OpIdx);
567 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
568 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
569 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000570 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000571 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
572 return (Rm << 3) | (Imm5 << 3) | Rn;
573}
574
Bill Wendling1fd374e2010-11-30 22:57:21 +0000575/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
576uint32_t ARMMCCodeEmitter::
577getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
578 SmallVectorImpl<MCFixup> &) const {
579 return getAddrModeSOpValue(MI, OpIdx, 4);
580}
581
582/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
583uint32_t ARMMCCodeEmitter::
584getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
585 SmallVectorImpl<MCFixup> &) const {
586 return getAddrModeSOpValue(MI, OpIdx, 2);
587}
588
589/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
590uint32_t ARMMCCodeEmitter::
591getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
592 SmallVectorImpl<MCFixup> &) const {
593 return getAddrModeSOpValue(MI, OpIdx, 1);
594}
595
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000596/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000597uint32_t ARMMCCodeEmitter::
598getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
599 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000600 // {12-9} = reg
601 // {8} = (U)nsigned (add == '1', sub == '0')
602 // {7-0} = imm8
603 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000604 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000605 // If The first operand isn't a register, we have a label reference.
606 const MCOperand &MO = MI.getOperand(OpIdx);
607 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000608 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000609 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000610 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000611
612 assert(MO.isExpr() && "Unexpected machine operand type!");
613 const MCExpr *Expr = MO.getExpr();
614 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_vfp_pcrel_12);
615 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
616
617 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000618 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000619 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000620 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
621 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000622
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000623 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
624 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000625 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626 Binary |= (1 << 8);
627 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000628 return Binary;
629}
630
Jim Grosbach806e80e2010-11-03 23:52:49 +0000631unsigned ARMMCCodeEmitter::
632getSORegOpValue(const MCInst &MI, unsigned OpIdx,
633 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000634 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
635 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
636 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000637 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000638 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000639 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000640 // {6-5} = type
641 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000642 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000643 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000644 // else (imm shift)
645 // {11-7} = imm
646
647 const MCOperand &MO = MI.getOperand(OpIdx);
648 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
649 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
650 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
651
652 // Encode Rm.
653 unsigned Binary = getARMRegisterNumbering(MO.getReg());
654
655 // Encode the shift opcode.
656 unsigned SBits = 0;
657 unsigned Rs = MO1.getReg();
658 if (Rs) {
659 // Set shift operand (bit[7:4]).
660 // LSL - 0001
661 // LSR - 0011
662 // ASR - 0101
663 // ROR - 0111
664 // RRX - 0110 and bit[11:8] clear.
665 switch (SOpc) {
666 default: llvm_unreachable("Unknown shift opc!");
667 case ARM_AM::lsl: SBits = 0x1; break;
668 case ARM_AM::lsr: SBits = 0x3; break;
669 case ARM_AM::asr: SBits = 0x5; break;
670 case ARM_AM::ror: SBits = 0x7; break;
671 case ARM_AM::rrx: SBits = 0x6; break;
672 }
673 } else {
674 // Set shift operand (bit[6:4]).
675 // LSL - 000
676 // LSR - 010
677 // ASR - 100
678 // ROR - 110
679 switch (SOpc) {
680 default: llvm_unreachable("Unknown shift opc!");
681 case ARM_AM::lsl: SBits = 0x0; break;
682 case ARM_AM::lsr: SBits = 0x2; break;
683 case ARM_AM::asr: SBits = 0x4; break;
684 case ARM_AM::ror: SBits = 0x6; break;
685 }
686 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000687
Jim Grosbachef324d72010-10-12 23:53:58 +0000688 Binary |= SBits << 4;
689 if (SOpc == ARM_AM::rrx)
690 return Binary;
691
692 // Encode the shift operation Rs or shift_imm (except rrx).
693 if (Rs) {
694 // Encode Rs bit[11:8].
695 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
696 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
697 }
698
699 // Encode shift_imm bit[11:7].
700 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
701}
702
Jim Grosbach806e80e2010-11-03 23:52:49 +0000703unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000704getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
705 SmallVectorImpl<MCFixup> &Fixups) const {
706 const MCOperand &MO1 = MI.getOperand(OpNum);
707 const MCOperand &MO2 = MI.getOperand(OpNum+1);
708 const MCOperand &MO3 = MI.getOperand(OpNum+2);
709
710 // Encoded as [Rn, Rm, imm].
711 // FIXME: Needs fixup support.
712 unsigned Value = getARMRegisterNumbering(MO1.getReg());
713 Value <<= 4;
714 Value |= getARMRegisterNumbering(MO2.getReg());
715 Value <<= 2;
716 Value |= MO3.getImm();
717
718 return Value;
719}
720
721unsigned ARMMCCodeEmitter::
722getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
723 SmallVectorImpl<MCFixup> &Fixups) const {
724 const MCOperand &MO1 = MI.getOperand(OpNum);
725 const MCOperand &MO2 = MI.getOperand(OpNum+1);
726
727 // FIXME: Needs fixup support.
728 unsigned Value = getARMRegisterNumbering(MO1.getReg());
729
730 // Even though the immediate is 8 bits long, we need 9 bits in order
731 // to represent the (inverse of the) sign bit.
732 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000733 int32_t tmp = (int32_t)MO2.getImm();
734 if (tmp < 0)
735 tmp = abs(tmp);
736 else
737 Value |= 256; // Set the ADD bit
738 Value |= tmp & 255;
739 return Value;
740}
741
742unsigned ARMMCCodeEmitter::
743getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
744 SmallVectorImpl<MCFixup> &Fixups) const {
745 const MCOperand &MO1 = MI.getOperand(OpNum);
746
747 // FIXME: Needs fixup support.
748 unsigned Value = 0;
749 int32_t tmp = (int32_t)MO1.getImm();
750 if (tmp < 0)
751 tmp = abs(tmp);
752 else
753 Value |= 256; // Set the ADD bit
754 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000755 return Value;
756}
757
758unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000759getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
760 SmallVectorImpl<MCFixup> &Fixups) const {
761 const MCOperand &MO1 = MI.getOperand(OpNum);
762
763 // FIXME: Needs fixup support.
764 unsigned Value = 0;
765 int32_t tmp = (int32_t)MO1.getImm();
766 if (tmp < 0)
767 tmp = abs(tmp);
768 else
769 Value |= 4096; // Set the ADD bit
770 Value |= tmp & 4095;
771 return Value;
772}
773
774unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000775getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
776 SmallVectorImpl<MCFixup> &Fixups) const {
777 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
778 // shifted. The second is the amount to shift by.
779 //
780 // {3-0} = Rm.
781 // {4} = 0
782 // {6-5} = type
783 // {11-7} = imm
784
785 const MCOperand &MO = MI.getOperand(OpIdx);
786 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
787 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
788
789 // Encode Rm.
790 unsigned Binary = getARMRegisterNumbering(MO.getReg());
791
792 // Encode the shift opcode.
793 unsigned SBits = 0;
794 // Set shift operand (bit[6:4]).
795 // LSL - 000
796 // LSR - 010
797 // ASR - 100
798 // ROR - 110
799 switch (SOpc) {
800 default: llvm_unreachable("Unknown shift opc!");
801 case ARM_AM::lsl: SBits = 0x0; break;
802 case ARM_AM::lsr: SBits = 0x2; break;
803 case ARM_AM::asr: SBits = 0x4; break;
804 case ARM_AM::ror: SBits = 0x6; break;
805 }
806
807 Binary |= SBits << 4;
808 if (SOpc == ARM_AM::rrx)
809 return Binary;
810
811 // Encode shift_imm bit[11:7].
812 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
813}
814
815unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000816getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
817 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000818 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
819 // msb of the mask.
820 const MCOperand &MO = MI.getOperand(Op);
821 uint32_t v = ~MO.getImm();
822 uint32_t lsb = CountTrailingZeros_32(v);
823 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
824 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
825 return lsb | (msb << 5);
826}
827
Jim Grosbach806e80e2010-11-03 23:52:49 +0000828unsigned ARMMCCodeEmitter::
829getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000830 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +0000831 // VLDM/VSTM:
832 // {12-8} = Vd
833 // {7-0} = Number of registers
834 //
835 // LDM/STM:
836 // {15-0} = Bitfield of GPRs.
837 unsigned Reg = MI.getOperand(Op).getReg();
838 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
839 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
840
Bill Wendling5e559a22010-11-09 00:30:18 +0000841 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +0000842
843 if (SPRRegs || DPRRegs) {
844 // VLDM/VSTM
845 unsigned RegNo = getARMRegisterNumbering(Reg);
846 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
847 Binary |= (RegNo & 0x1f) << 8;
848 if (SPRRegs)
849 Binary |= NumRegs;
850 else
851 Binary |= NumRegs * 2;
852 } else {
853 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
854 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
855 Binary |= 1 << RegNo;
856 }
Bill Wendling5e559a22010-11-09 00:30:18 +0000857 }
Bill Wendling6bc105a2010-11-17 00:45:23 +0000858
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000859 return Binary;
860}
861
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000862/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
863/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000864unsigned ARMMCCodeEmitter::
865getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
866 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +0000867 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +0000868 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +0000869
Owen Andersond9aa7d32010-11-02 00:05:05 +0000870 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +0000871 unsigned Align = 0;
872
873 switch (Imm.getImm()) {
874 default: break;
875 case 2:
876 case 4:
877 case 8: Align = 0x01; break;
878 case 16: Align = 0x02; break;
879 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +0000880 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000881
Owen Andersond9aa7d32010-11-02 00:05:05 +0000882 return RegNo | (Align << 4);
883}
884
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000885/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
886/// alignment operand for use in VLD-dup instructions. This is the same as
887/// getAddrMode6AddressOpValue except for the alignment encoding, which is
888/// different for VLD4-dup.
889unsigned ARMMCCodeEmitter::
890getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
891 SmallVectorImpl<MCFixup> &Fixups) const {
892 const MCOperand &Reg = MI.getOperand(Op);
893 const MCOperand &Imm = MI.getOperand(Op + 1);
894
895 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
896 unsigned Align = 0;
897
898 switch (Imm.getImm()) {
899 default: break;
900 case 2:
901 case 4:
902 case 8: Align = 0x01; break;
903 case 16: Align = 0x03; break;
904 }
905
906 return RegNo | (Align << 4);
907}
908
Jim Grosbach806e80e2010-11-03 23:52:49 +0000909unsigned ARMMCCodeEmitter::
910getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
911 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000912 const MCOperand &MO = MI.getOperand(Op);
913 if (MO.getReg() == 0) return 0x0D;
914 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +0000915}
916
Jim Grosbach568eeed2010-09-17 18:46:17 +0000917void ARMMCCodeEmitter::
918EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000919 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000920 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +0000921 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000922 uint64_t TSFlags = Desc.TSFlags;
923 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +0000924 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +0000925 int Size;
926 // Basic size info comes from the TSFlags field.
927 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
928 default: llvm_unreachable("Unexpected instruction size!");
929 case ARMII::Size2Bytes: Size = 2; break;
930 case ARMII::Size4Bytes: Size = 4; break;
931 }
932 EmitConstant(getBinaryCodeForInstr(MI, Fixups), Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +0000933 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +0000934}
Jim Grosbach9af82ba2010-10-07 21:57:55 +0000935
Jim Grosbach806e80e2010-11-03 23:52:49 +0000936#include "ARMGenMCCodeEmitter.inc"