blob: 7f34ee96e379ed4eeebb3d0ef26d230bff9ead53 [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[] = {
Bill Wendling1591b292010-12-10 22:37:19 +000048// This table *must* be in the order that the fixup_* kinds are defined in
49// ARMFixupKinds.h.
50//
51// Name Offset (bits) Size (bits) Flags
52{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson05018c22010-12-09 20:27:52 +000054 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000055{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersone2e0f582010-12-10 22:46:47 +000056{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson5fd873d2010-12-10 22:53:48 +000057 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000058{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
59{ "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonc2666002010-12-13 19:31:11 +000060{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
61{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000062{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_movt_hi16", 0, 16, 0 },
69{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000070 };
71
72 if (Kind < FirstTargetFixupKind)
73 return MCCodeEmitter::getFixupKindInfo(Kind);
74
75 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
76 "Invalid kind!");
77 return Infos[Kind - FirstTargetFixupKind];
78 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000079 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
80
Jim Grosbach9af82ba2010-10-07 21:57:55 +000081 // getBinaryCodeForInstr - TableGen'erated function for getting the
82 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000083 unsigned getBinaryCodeForInstr(const MCInst &MI,
84 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000085
86 /// getMachineOpValue - Return binary encoding of operand. If the machine
87 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000090
Jason W Kim837caa92010-11-18 23:37:15 +000091 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
92 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
94
Bill Wendling92b5a2e2010-11-03 01:49:29 +000095 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000096 unsigned &Reg, unsigned &Imm,
97 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000098
Jim Grosbach662a8162010-12-06 23:57:07 +000099 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000100 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000101 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
103
Bill Wendling09aa3f02010-12-09 00:39:08 +0000104 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
105 /// BLX branch target.
106 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbache2467172010-12-10 18:21:33 +0000109 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
110 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
112
Jim Grosbach01086452010-12-10 17:13:40 +0000113 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Jim Grosbach027d6e82010-12-09 19:04:53 +0000117 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
118 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000119 SmallVectorImpl<MCFixup> &Fixups) const;
120
Jim Grosbachc466b932010-11-11 18:04:49 +0000121 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
122 /// branch target.
123 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups) const;
125
Owen Andersonc2666002010-12-13 19:31:11 +0000126 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
127 /// immediate Thumb2 direct branch target.
128 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
131
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000132 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
133 /// ADR label target.
134 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const;
136
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000137 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
138 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000139 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
140 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000141
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000142 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
143 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
144 SmallVectorImpl<MCFixup> &Fixups) const;
145
Owen Anderson9d63d902010-12-01 19:18:46 +0000146 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
147 /// operand.
148 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
151
Jim Grosbach54fea632010-11-09 17:20:53 +0000152 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
153 /// operand as needed by load/store instructions.
154 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups) const;
156
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000157 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
158 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const {
160 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
161 switch (Mode) {
162 default: assert(0 && "Unknown addressing sub-mode!");
163 case ARM_AM::da: return 0;
164 case ARM_AM::ia: return 1;
165 case ARM_AM::db: return 2;
166 case ARM_AM::ib: return 3;
167 }
168 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000169 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
170 ///
171 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
172 switch (ShOpc) {
173 default: llvm_unreachable("Unknown shift opc!");
174 case ARM_AM::no_shift:
175 case ARM_AM::lsl: return 0;
176 case ARM_AM::lsr: return 1;
177 case ARM_AM::asr: return 2;
178 case ARM_AM::ror:
179 case ARM_AM::rrx: return 3;
180 }
181 return 0;
182 }
183
184 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
185 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
186 SmallVectorImpl<MCFixup> &Fixups) const;
187
188 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
189 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
190 SmallVectorImpl<MCFixup> &Fixups) const;
191
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000192 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
193 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
194 SmallVectorImpl<MCFixup> &Fixups) const;
195
Jim Grosbach570a9222010-11-11 01:09:40 +0000196 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
197 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000199
Jim Grosbachd967cd02010-12-07 21:50:47 +0000200 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
201 /// operand.
202 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
203 SmallVectorImpl<MCFixup> &Fixups) const;
204
Bill Wendling272df512010-12-09 21:49:07 +0000205 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
206 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
207 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000208
Bill Wendlingb8958b02010-12-08 01:57:09 +0000209 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
210 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
211 SmallVectorImpl<MCFixup> &Fixups) const;
212
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000213 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000214 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
215 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000216
Jim Grosbach08bd5492010-10-12 23:00:24 +0000217 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000218 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
219 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000220 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
221 // '1' respectively.
222 return MI.getOperand(Op).getReg() == ARM::CPSR;
223 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000224
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000225 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000226 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000228 unsigned SoImm = MI.getOperand(Op).getImm();
229 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
230 assert(SoImmVal != -1 && "Not a valid so_imm value!");
231
232 // Encode rotate_imm.
233 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
234 << ARMII::SoRotImmShift;
235
236 // Encode immed_8.
237 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
238 return Binary;
239 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000240
Owen Anderson5de6d842010-11-12 21:12:40 +0000241 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
242 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
243 SmallVectorImpl<MCFixup> &Fixups) const {
244 unsigned SoImm = MI.getOperand(Op).getImm();
245 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
246 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
247 return Encoded;
248 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000249
Owen Anderson75579f72010-11-29 22:44:32 +0000250 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
251 SmallVectorImpl<MCFixup> &Fixups) const;
252 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
253 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000254 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
255 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000256 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
257 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000258
Jim Grosbachef324d72010-10-12 23:53:58 +0000259 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000260 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
261 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000262 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000264
Jim Grosbach806e80e2010-11-03 23:52:49 +0000265 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000267 switch (MI.getOperand(Op).getImm()) {
268 default: assert (0 && "Not a valid rot_imm value!");
269 case 0: return 0;
270 case 8: return 1;
271 case 16: return 2;
272 case 24: return 3;
273 }
274 }
275
Jim Grosbach806e80e2010-11-03 23:52:49 +0000276 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000278 return MI.getOperand(Op).getImm() - 1;
279 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000280
Jim Grosbach806e80e2010-11-03 23:52:49 +0000281 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000283 return 64 - MI.getOperand(Op).getImm();
284 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000285
Jim Grosbach806e80e2010-11-03 23:52:49 +0000286 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000288
Jim Grosbach806e80e2010-11-03 23:52:49 +0000289 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const;
291 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000293 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000295 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000297
Owen Andersonc7139a62010-11-11 19:07:48 +0000298 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
299 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000300 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000301 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000302 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000303 unsigned EncodedValue) const;
304
305 unsigned VFPThumb2PostEncoder(const MCInst &MI,
306 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000307
Jim Grosbach70933262010-11-04 01:12:30 +0000308 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000309 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000310 }
311
Jim Grosbach70933262010-11-04 01:12:30 +0000312 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000313 // Output the constant in little endian byte order.
314 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000315 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000316 Val >>= 8;
317 }
318 }
319
Jim Grosbach568eeed2010-09-17 18:46:17 +0000320 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
321 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000322};
323
324} // end anonymous namespace
325
Bill Wendling0800ce72010-11-02 22:53:11 +0000326MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
327 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000328 return new ARMMCCodeEmitter(TM, Ctx);
329}
330
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000331/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
332/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000333/// Thumb2 mode.
334unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
335 unsigned EncodedValue) const {
336 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
337 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000338 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000339 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
340 // set to 1111.
341 unsigned Bit24 = EncodedValue & 0x01000000;
342 unsigned Bit28 = Bit24 << 4;
343 EncodedValue &= 0xEFFFFFFF;
344 EncodedValue |= Bit28;
345 EncodedValue |= 0x0F000000;
346 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000347
Owen Andersonc7139a62010-11-11 19:07:48 +0000348 return EncodedValue;
349}
350
Owen Anderson57dac882010-11-11 21:36:43 +0000351/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000352/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000353/// Thumb2 mode.
354unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
355 unsigned EncodedValue) const {
356 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
357 if (Subtarget.isThumb2()) {
358 EncodedValue &= 0xF0FFFFFF;
359 EncodedValue |= 0x09000000;
360 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000361
Owen Anderson57dac882010-11-11 21:36:43 +0000362 return EncodedValue;
363}
364
Owen Anderson8f143912010-11-11 23:12:55 +0000365/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000366/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000367/// Thumb2 mode.
368unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
369 unsigned EncodedValue) const {
370 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
371 if (Subtarget.isThumb2()) {
372 EncodedValue &= 0x00FFFFFF;
373 EncodedValue |= 0xEE000000;
374 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000375
Owen Anderson8f143912010-11-11 23:12:55 +0000376 return EncodedValue;
377}
378
Bill Wendlingcf590262010-12-01 21:54:50 +0000379/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
380/// them to their Thumb2 form if we are currently in Thumb2 mode.
381unsigned ARMMCCodeEmitter::
382VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
383 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
384 EncodedValue &= 0x0FFFFFFF;
385 EncodedValue |= 0xE0000000;
386 }
387 return EncodedValue;
388}
Owen Anderson57dac882010-11-11 21:36:43 +0000389
Jim Grosbach56ac9072010-10-08 21:45:55 +0000390/// getMachineOpValue - Return binary encoding of operand. If the machine
391/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000392unsigned ARMMCCodeEmitter::
393getMachineOpValue(const MCInst &MI, const MCOperand &MO,
394 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000395 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000396 unsigned Reg = MO.getReg();
397 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000398
Jim Grosbachb0708d22010-11-30 23:51:41 +0000399 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000400 switch (Reg) {
401 default:
402 return RegNo;
403 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
404 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
405 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
406 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
407 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000408 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000409 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000410 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000411 } else if (MO.isFPImm()) {
412 return static_cast<unsigned>(APFloat(MO.getFPImm())
413 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000414 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000415
Jim Grosbach817c1a62010-11-19 00:27:09 +0000416 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000417 return 0;
418}
419
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000420/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000421bool ARMMCCodeEmitter::
422EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
423 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000424 const MCOperand &MO = MI.getOperand(OpIdx);
425 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000426
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000427 Reg = getARMRegisterNumbering(MO.getReg());
428
429 int32_t SImm = MO1.getImm();
430 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000431
Jim Grosbachab682a22010-10-28 18:34:10 +0000432 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000433 if (SImm == INT32_MIN)
434 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000435
Jim Grosbachab682a22010-10-28 18:34:10 +0000436 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000437 if (SImm < 0) {
438 SImm = -SImm;
439 isAdd = false;
440 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000441
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000442 Imm = SImm;
443 return isAdd;
444}
445
Bill Wendlingdff2f712010-12-08 23:01:43 +0000446/// getBranchTargetOpValue - Helper function to get the branch target operand,
447/// which is either an immediate or requires a fixup.
448static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
449 unsigned FixupKind,
450 SmallVectorImpl<MCFixup> &Fixups) {
451 const MCOperand &MO = MI.getOperand(OpIdx);
452
453 // If the destination is an immediate, we have nothing to do.
454 if (MO.isImm()) return MO.getImm();
455 assert(MO.isExpr() && "Unexpected branch target type!");
456 const MCExpr *Expr = MO.getExpr();
457 MCFixupKind Kind = MCFixupKind(FixupKind);
458 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
459
460 // All of the information is in the fixup.
461 return 0;
462}
463
464/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000465uint32_t ARMMCCodeEmitter::
466getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
467 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000468 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000469}
470
Bill Wendling09aa3f02010-12-09 00:39:08 +0000471/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
472/// BLX branch target.
473uint32_t ARMMCCodeEmitter::
474getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
475 SmallVectorImpl<MCFixup> &Fixups) const {
476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
477}
478
Jim Grosbache2467172010-12-10 18:21:33 +0000479/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
480uint32_t ARMMCCodeEmitter::
481getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
482 SmallVectorImpl<MCFixup> &Fixups) const {
483 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
484}
485
Jim Grosbach01086452010-12-10 17:13:40 +0000486/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
487uint32_t ARMMCCodeEmitter::
488getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000489 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000490 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
491}
492
Jim Grosbach027d6e82010-12-09 19:04:53 +0000493/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000494uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000495getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000496 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000497 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000498}
499
500/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
501/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000502uint32_t ARMMCCodeEmitter::
503getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000504 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000505 // FIXME: This really, really shouldn't use TargetMachine. We don't want
506 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000507 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
508 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000509 return
510 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000511 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000512}
513
Owen Andersonc2666002010-12-13 19:31:11 +0000514/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
515/// immediate branch target.
516uint32_t ARMMCCodeEmitter::
517getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
518 SmallVectorImpl<MCFixup> &Fixups) const {
519 unsigned Val =
520 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
521 bool I = (Val & 0x800000);
522 bool J1 = (Val & 0x400000);
523 bool J2 = (Val & 0x200000);
524 if (I ^ J1)
525 Val &= ~0x400000;
526 else
527 Val |= 0x400000;
528
529 if (I ^ J2)
530 Val &= ~0x200000;
531 else
532 Val |= 0x200000;
533
534 return Val;
535}
536
Bill Wendlingdff2f712010-12-08 23:01:43 +0000537/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
538/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000539uint32_t ARMMCCodeEmitter::
540getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
541 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000542 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
543 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
544 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000545}
546
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000547/// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
548uint32_t ARMMCCodeEmitter::
549getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
550 SmallVectorImpl<MCFixup> &Fixups) const {
551 const MCOperand &MO1 = MI.getOperand(OpIdx);
552 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
553 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
554 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
555 return (Rm << 3) | Rn;
556}
557
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000558/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000559uint32_t ARMMCCodeEmitter::
560getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
561 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000562 // {17-13} = reg
563 // {12} = (U)nsigned (add == '1', sub == '0')
564 // {11-0} = imm12
565 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000566 bool isAdd = true;
567 // If The first operand isn't a register, we have a label reference.
568 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000569 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
570 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000571 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000572 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000573 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000574
Owen Andersoneb6779c2010-12-07 00:45:21 +0000575 const MCExpr *Expr = 0;
576 if (!MO.isReg())
577 Expr = MO.getExpr();
578 else
579 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000580
Owen Andersond7b3f582010-12-09 01:51:07 +0000581 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
582 MCFixupKind Kind;
583 if (Subtarget.isThumb2())
584 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
585 else
586 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000587 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
588
589 ++MCNumCPRelocations;
590 } else
591 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000592
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000593 uint32_t Binary = Imm12 & 0xfff;
594 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000595 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000596 Binary |= (1 << 12);
597 Binary |= (Reg << 13);
598 return Binary;
599}
600
Owen Anderson9d63d902010-12-01 19:18:46 +0000601/// getT2AddrModeImm8s4OpValue - Return encoding info for
602/// 'reg +/- imm8<<2' operand.
603uint32_t ARMMCCodeEmitter::
604getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
605 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000606 // {12-9} = reg
607 // {8} = (U)nsigned (add == '1', sub == '0')
608 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000609 unsigned Reg, Imm8;
610 bool isAdd = true;
611 // If The first operand isn't a register, we have a label reference.
612 const MCOperand &MO = MI.getOperand(OpIdx);
613 if (!MO.isReg()) {
614 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
615 Imm8 = 0;
616 isAdd = false ; // 'U' bit is set as part of the fixup.
617
618 assert(MO.isExpr() && "Unexpected machine operand type!");
619 const MCExpr *Expr = MO.getExpr();
620 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
621 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
622
623 ++MCNumCPRelocations;
624 } else
625 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
626
627 uint32_t Binary = (Imm8 >> 2) & 0xff;
628 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
629 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000630 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000631 Binary |= (Reg << 9);
632 return Binary;
633}
634
Jim Grosbach54fea632010-11-09 17:20:53 +0000635uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000636getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
637 SmallVectorImpl<MCFixup> &Fixups) const {
638 // {20-16} = imm{15-12}
639 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000640 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000641 if (MO.isImm()) {
642 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000643 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000644 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
645 MCFixupKind Kind;
646 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000647 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000648 case MCSymbolRefExpr::VK_ARM_HI16:
649 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
650 break;
651 case MCSymbolRefExpr::VK_ARM_LO16:
652 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
653 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000654 }
655 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
656 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000657 };
658 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000659 return 0;
660}
661
662uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000663getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
664 SmallVectorImpl<MCFixup> &Fixups) const {
665 const MCOperand &MO = MI.getOperand(OpIdx);
666 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
667 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
668 unsigned Rn = getARMRegisterNumbering(MO.getReg());
669 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000670 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
671 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000672 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
673 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000674
675 // {16-13} = Rn
676 // {12} = isAdd
677 // {11-0} = shifter
678 // {3-0} = Rm
679 // {4} = 0
680 // {6-5} = type
681 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000682 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000683 Binary |= Rn << 13;
684 Binary |= SBits << 5;
685 Binary |= ShImm << 7;
686 if (isAdd)
687 Binary |= 1 << 12;
688 return Binary;
689}
690
Jim Grosbach570a9222010-11-11 01:09:40 +0000691uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000692getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
693 SmallVectorImpl<MCFixup> &Fixups) const {
694 // {17-14} Rn
695 // {13} 1 == imm12, 0 == Rm
696 // {12} isAdd
697 // {11-0} imm12/Rm
698 const MCOperand &MO = MI.getOperand(OpIdx);
699 unsigned Rn = getARMRegisterNumbering(MO.getReg());
700 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
701 Binary |= Rn << 14;
702 return Binary;
703}
704
705uint32_t ARMMCCodeEmitter::
706getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
707 SmallVectorImpl<MCFixup> &Fixups) const {
708 // {13} 1 == imm12, 0 == Rm
709 // {12} isAdd
710 // {11-0} imm12/Rm
711 const MCOperand &MO = MI.getOperand(OpIdx);
712 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
713 unsigned Imm = MO1.getImm();
714 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
715 bool isReg = MO.getReg() != 0;
716 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
717 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
718 if (isReg) {
719 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
720 Binary <<= 7; // Shift amount is bits [11:7]
721 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
722 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
723 }
724 return Binary | (isAdd << 12) | (isReg << 13);
725}
726
727uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000728getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
729 SmallVectorImpl<MCFixup> &Fixups) const {
730 // {9} 1 == imm8, 0 == Rm
731 // {8} isAdd
732 // {7-4} imm7_4/zero
733 // {3-0} imm3_0/Rm
734 const MCOperand &MO = MI.getOperand(OpIdx);
735 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
736 unsigned Imm = MO1.getImm();
737 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
738 bool isImm = MO.getReg() == 0;
739 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
740 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
741 if (!isImm)
742 Imm8 = getARMRegisterNumbering(MO.getReg());
743 return Imm8 | (isAdd << 8) | (isImm << 9);
744}
745
746uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000747getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &Fixups) const {
749 // {13} 1 == imm8, 0 == Rm
750 // {12-9} Rn
751 // {8} isAdd
752 // {7-4} imm7_4/zero
753 // {3-0} imm3_0/Rm
754 const MCOperand &MO = MI.getOperand(OpIdx);
755 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
756 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
757 unsigned Rn = getARMRegisterNumbering(MO.getReg());
758 unsigned Imm = MO2.getImm();
759 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
760 bool isImm = MO1.getReg() == 0;
761 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
762 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
763 if (!isImm)
764 Imm8 = getARMRegisterNumbering(MO1.getReg());
765 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
766}
767
Bill Wendlingb8958b02010-12-08 01:57:09 +0000768/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000769uint32_t ARMMCCodeEmitter::
770getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
771 SmallVectorImpl<MCFixup> &Fixups) const {
772 // [SP, #imm]
773 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000774 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000775#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
776 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
777 "Unexpected base register!");
778#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000779 // The immediate is already shifted for the implicit zeroes, so no change
780 // here.
781 return MO1.getImm() & 0xff;
782}
783
Bill Wendling1fd374e2010-11-30 22:57:21 +0000784/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000785uint32_t ARMMCCodeEmitter::
786getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
787 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000788 // [Rn, Rm]
789 // {5-3} = Rm
790 // {2-0} = Rn
791 //
792 // [Rn, #imm]
793 // {7-3} = imm5
794 // {2-0} = Rn
795 const MCOperand &MO = MI.getOperand(OpIdx);
796 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
797 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
798 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000799 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000800
801 if (MO2.getReg() != 0)
802 // Is an immediate.
803 Imm5 = getARMRegisterNumbering(MO2.getReg());
804
Bill Wendling272df512010-12-09 21:49:07 +0000805 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000806}
807
Bill Wendlingb8958b02010-12-08 01:57:09 +0000808/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
809uint32_t ARMMCCodeEmitter::
810getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
811 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000812 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000813}
814
Jim Grosbach5177f792010-12-01 21:09:40 +0000815/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000816uint32_t ARMMCCodeEmitter::
817getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
818 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000819 // {12-9} = reg
820 // {8} = (U)nsigned (add == '1', sub == '0')
821 // {7-0} = imm8
822 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000823 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000824 // If The first operand isn't a register, we have a label reference.
825 const MCOperand &MO = MI.getOperand(OpIdx);
826 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000827 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000828 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000829 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000830
831 assert(MO.isExpr() && "Unexpected machine operand type!");
832 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000833 MCFixupKind Kind;
834 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
835 if (Subtarget.isThumb2())
836 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
837 else
838 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000839 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
840
841 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000842 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000843 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000844 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
845 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000846
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000847 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
848 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000849 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000850 Binary |= (1 << 8);
851 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000852 return Binary;
853}
854
Jim Grosbach806e80e2010-11-03 23:52:49 +0000855unsigned ARMMCCodeEmitter::
856getSORegOpValue(const MCInst &MI, unsigned OpIdx,
857 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000858 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
859 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
860 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000861 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000862 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000863 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000864 // {6-5} = type
865 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000866 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000867 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000868 // else (imm shift)
869 // {11-7} = imm
870
871 const MCOperand &MO = MI.getOperand(OpIdx);
872 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
873 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
874 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
875
876 // Encode Rm.
877 unsigned Binary = getARMRegisterNumbering(MO.getReg());
878
879 // Encode the shift opcode.
880 unsigned SBits = 0;
881 unsigned Rs = MO1.getReg();
882 if (Rs) {
883 // Set shift operand (bit[7:4]).
884 // LSL - 0001
885 // LSR - 0011
886 // ASR - 0101
887 // ROR - 0111
888 // RRX - 0110 and bit[11:8] clear.
889 switch (SOpc) {
890 default: llvm_unreachable("Unknown shift opc!");
891 case ARM_AM::lsl: SBits = 0x1; break;
892 case ARM_AM::lsr: SBits = 0x3; break;
893 case ARM_AM::asr: SBits = 0x5; break;
894 case ARM_AM::ror: SBits = 0x7; break;
895 case ARM_AM::rrx: SBits = 0x6; break;
896 }
897 } else {
898 // Set shift operand (bit[6:4]).
899 // LSL - 000
900 // LSR - 010
901 // ASR - 100
902 // ROR - 110
903 switch (SOpc) {
904 default: llvm_unreachable("Unknown shift opc!");
905 case ARM_AM::lsl: SBits = 0x0; break;
906 case ARM_AM::lsr: SBits = 0x2; break;
907 case ARM_AM::asr: SBits = 0x4; break;
908 case ARM_AM::ror: SBits = 0x6; break;
909 }
910 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000911
Jim Grosbachef324d72010-10-12 23:53:58 +0000912 Binary |= SBits << 4;
913 if (SOpc == ARM_AM::rrx)
914 return Binary;
915
916 // Encode the shift operation Rs or shift_imm (except rrx).
917 if (Rs) {
918 // Encode Rs bit[11:8].
919 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
920 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
921 }
922
923 // Encode shift_imm bit[11:7].
924 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
925}
926
Jim Grosbach806e80e2010-11-03 23:52:49 +0000927unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000928getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
929 SmallVectorImpl<MCFixup> &Fixups) const {
930 const MCOperand &MO1 = MI.getOperand(OpNum);
931 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000932 const MCOperand &MO3 = MI.getOperand(OpNum+2);
933
Owen Anderson75579f72010-11-29 22:44:32 +0000934 // Encoded as [Rn, Rm, imm].
935 // FIXME: Needs fixup support.
936 unsigned Value = getARMRegisterNumbering(MO1.getReg());
937 Value <<= 4;
938 Value |= getARMRegisterNumbering(MO2.getReg());
939 Value <<= 2;
940 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000941
Owen Anderson75579f72010-11-29 22:44:32 +0000942 return Value;
943}
944
945unsigned ARMMCCodeEmitter::
946getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
947 SmallVectorImpl<MCFixup> &Fixups) const {
948 const MCOperand &MO1 = MI.getOperand(OpNum);
949 const MCOperand &MO2 = MI.getOperand(OpNum+1);
950
951 // FIXME: Needs fixup support.
952 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000953
Owen Anderson75579f72010-11-29 22:44:32 +0000954 // Even though the immediate is 8 bits long, we need 9 bits in order
955 // to represent the (inverse of the) sign bit.
956 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000957 int32_t tmp = (int32_t)MO2.getImm();
958 if (tmp < 0)
959 tmp = abs(tmp);
960 else
961 Value |= 256; // Set the ADD bit
962 Value |= tmp & 255;
963 return Value;
964}
965
966unsigned ARMMCCodeEmitter::
967getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
968 SmallVectorImpl<MCFixup> &Fixups) const {
969 const MCOperand &MO1 = MI.getOperand(OpNum);
970
971 // FIXME: Needs fixup support.
972 unsigned Value = 0;
973 int32_t tmp = (int32_t)MO1.getImm();
974 if (tmp < 0)
975 tmp = abs(tmp);
976 else
977 Value |= 256; // Set the ADD bit
978 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000979 return Value;
980}
981
982unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000983getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
984 SmallVectorImpl<MCFixup> &Fixups) const {
985 const MCOperand &MO1 = MI.getOperand(OpNum);
986
987 // FIXME: Needs fixup support.
988 unsigned Value = 0;
989 int32_t tmp = (int32_t)MO1.getImm();
990 if (tmp < 0)
991 tmp = abs(tmp);
992 else
993 Value |= 4096; // Set the ADD bit
994 Value |= tmp & 4095;
995 return Value;
996}
997
998unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000999getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1000 SmallVectorImpl<MCFixup> &Fixups) const {
1001 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1002 // shifted. The second is the amount to shift by.
1003 //
1004 // {3-0} = Rm.
1005 // {4} = 0
1006 // {6-5} = type
1007 // {11-7} = imm
1008
1009 const MCOperand &MO = MI.getOperand(OpIdx);
1010 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1011 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1012
1013 // Encode Rm.
1014 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1015
1016 // Encode the shift opcode.
1017 unsigned SBits = 0;
1018 // Set shift operand (bit[6:4]).
1019 // LSL - 000
1020 // LSR - 010
1021 // ASR - 100
1022 // ROR - 110
1023 switch (SOpc) {
1024 default: llvm_unreachable("Unknown shift opc!");
1025 case ARM_AM::lsl: SBits = 0x0; break;
1026 case ARM_AM::lsr: SBits = 0x2; break;
1027 case ARM_AM::asr: SBits = 0x4; break;
1028 case ARM_AM::ror: SBits = 0x6; break;
1029 }
1030
1031 Binary |= SBits << 4;
1032 if (SOpc == ARM_AM::rrx)
1033 return Binary;
1034
1035 // Encode shift_imm bit[11:7].
1036 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1037}
1038
1039unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001040getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1041 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001042 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1043 // msb of the mask.
1044 const MCOperand &MO = MI.getOperand(Op);
1045 uint32_t v = ~MO.getImm();
1046 uint32_t lsb = CountTrailingZeros_32(v);
1047 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1048 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1049 return lsb | (msb << 5);
1050}
1051
Jim Grosbach806e80e2010-11-03 23:52:49 +00001052unsigned ARMMCCodeEmitter::
1053getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001054 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001055 // VLDM/VSTM:
1056 // {12-8} = Vd
1057 // {7-0} = Number of registers
1058 //
1059 // LDM/STM:
1060 // {15-0} = Bitfield of GPRs.
1061 unsigned Reg = MI.getOperand(Op).getReg();
1062 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1063 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1064
Bill Wendling5e559a22010-11-09 00:30:18 +00001065 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001066
1067 if (SPRRegs || DPRRegs) {
1068 // VLDM/VSTM
1069 unsigned RegNo = getARMRegisterNumbering(Reg);
1070 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1071 Binary |= (RegNo & 0x1f) << 8;
1072 if (SPRRegs)
1073 Binary |= NumRegs;
1074 else
1075 Binary |= NumRegs * 2;
1076 } else {
1077 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1078 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1079 Binary |= 1 << RegNo;
1080 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001081 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001082
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001083 return Binary;
1084}
1085
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001086/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1087/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001088unsigned ARMMCCodeEmitter::
1089getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1090 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001091 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001092 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001093
Owen Andersond9aa7d32010-11-02 00:05:05 +00001094 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001095 unsigned Align = 0;
1096
1097 switch (Imm.getImm()) {
1098 default: break;
1099 case 2:
1100 case 4:
1101 case 8: Align = 0x01; break;
1102 case 16: Align = 0x02; break;
1103 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001104 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001105
Owen Andersond9aa7d32010-11-02 00:05:05 +00001106 return RegNo | (Align << 4);
1107}
1108
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001109/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1110/// alignment operand for use in VLD-dup instructions. This is the same as
1111/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1112/// different for VLD4-dup.
1113unsigned ARMMCCodeEmitter::
1114getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1115 SmallVectorImpl<MCFixup> &Fixups) const {
1116 const MCOperand &Reg = MI.getOperand(Op);
1117 const MCOperand &Imm = MI.getOperand(Op + 1);
1118
1119 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1120 unsigned Align = 0;
1121
1122 switch (Imm.getImm()) {
1123 default: break;
1124 case 2:
1125 case 4:
1126 case 8: Align = 0x01; break;
1127 case 16: Align = 0x03; break;
1128 }
1129
1130 return RegNo | (Align << 4);
1131}
1132
Jim Grosbach806e80e2010-11-03 23:52:49 +00001133unsigned ARMMCCodeEmitter::
1134getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1135 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001136 const MCOperand &MO = MI.getOperand(Op);
1137 if (MO.getReg() == 0) return 0x0D;
1138 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001139}
1140
Jim Grosbach568eeed2010-09-17 18:46:17 +00001141void ARMMCCodeEmitter::
1142EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001143 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001144 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001145 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001146 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001147 uint64_t TSFlags = Desc.TSFlags;
1148 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001149 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001150 int Size;
1151 // Basic size info comes from the TSFlags field.
1152 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1153 default: llvm_unreachable("Unexpected instruction size!");
1154 case ARMII::Size2Bytes: Size = 2; break;
1155 case ARMII::Size4Bytes: Size = 4; break;
1156 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001157 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1158 // Thumb 32-bit wide instructions need to be have the high order halfword
1159 // emitted first.
1160 if (Subtarget.isThumb() && Size == 4) {
1161 EmitConstant(Binary >> 16, 2, OS);
1162 EmitConstant(Binary & 0xffff, 2, OS);
1163 } else
1164 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001165 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001166}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001167
Jim Grosbach806e80e2010-11-03 23:52:49 +00001168#include "ARMGenMCCodeEmitter.inc"