blob: 2770d6e5a50e0e5e4992ed8d86617d8428e1e6de [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},
Jim Grosbachd40963c2010-12-14 22:28:03 +000058{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000059{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersona838a252010-12-14 00:36:49 +000060{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
61 MCFixupKindInfo::FKF_IsAligned},
Daniel Dunbarabfbac52010-12-14 17:37:16 +000062{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonc2666002010-12-13 19:31:11 +000063{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
64{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000065{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_arm_movt_hi16", 0, 16, 0 },
72{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000073 };
74
75 if (Kind < FirstTargetFixupKind)
76 return MCCodeEmitter::getFixupKindInfo(Kind);
77
78 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
79 "Invalid kind!");
80 return Infos[Kind - FirstTargetFixupKind];
81 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000082 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
83
Jim Grosbach9af82ba2010-10-07 21:57:55 +000084 // getBinaryCodeForInstr - TableGen'erated function for getting the
85 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000086 unsigned getBinaryCodeForInstr(const MCInst &MI,
87 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000088
89 /// getMachineOpValue - Return binary encoding of operand. If the machine
90 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000091 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
92 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000093
Jason W Kim837caa92010-11-18 23:37:15 +000094 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
95 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Bill Wendling92b5a2e2010-11-03 01:49:29 +000098 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000099 unsigned &Reg, unsigned &Imm,
100 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000101
Jim Grosbach662a8162010-12-06 23:57:07 +0000102 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000103 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000104 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Bill Wendling09aa3f02010-12-09 00:39:08 +0000107 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
108 /// BLX branch target.
109 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
Jim Grosbache2467172010-12-10 18:21:33 +0000112 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
113 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups) const;
115
Jim Grosbach01086452010-12-10 17:13:40 +0000116 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
117 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups) const;
119
Jim Grosbach027d6e82010-12-09 19:04:53 +0000120 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
121 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000122 SmallVectorImpl<MCFixup> &Fixups) const;
123
Jim Grosbachc466b932010-11-11 18:04:49 +0000124 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
125 /// branch target.
126 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups) const;
128
Owen Andersonc2666002010-12-13 19:31:11 +0000129 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
130 /// immediate Thumb2 direct branch target.
131 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
132 SmallVectorImpl<MCFixup> &Fixups) const;
133
134
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000135 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
136 /// ADR label target.
137 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
138 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000139 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
140 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000141 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups) const;
143
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000144
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000145 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
146 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000147 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000149
Bill Wendlingf4caf692010-12-14 03:36:38 +0000150 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
151 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
152 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000153
Owen Anderson9d63d902010-12-01 19:18:46 +0000154 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
155 /// operand.
156 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
157 SmallVectorImpl<MCFixup> &Fixups) const;
158
159
Jim Grosbach54fea632010-11-09 17:20:53 +0000160 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
161 /// operand as needed by load/store instructions.
162 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
164
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000165 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
166 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const {
168 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
169 switch (Mode) {
170 default: assert(0 && "Unknown addressing sub-mode!");
171 case ARM_AM::da: return 0;
172 case ARM_AM::ia: return 1;
173 case ARM_AM::db: return 2;
174 case ARM_AM::ib: return 3;
175 }
176 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000177 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
178 ///
179 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
180 switch (ShOpc) {
181 default: llvm_unreachable("Unknown shift opc!");
182 case ARM_AM::no_shift:
183 case ARM_AM::lsl: return 0;
184 case ARM_AM::lsr: return 1;
185 case ARM_AM::asr: return 2;
186 case ARM_AM::ror:
187 case ARM_AM::rrx: return 3;
188 }
189 return 0;
190 }
191
192 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
193 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
194 SmallVectorImpl<MCFixup> &Fixups) const;
195
196 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
197 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
198 SmallVectorImpl<MCFixup> &Fixups) const;
199
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000200 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
201 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
202 SmallVectorImpl<MCFixup> &Fixups) const;
203
Jim Grosbach570a9222010-11-11 01:09:40 +0000204 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
205 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
206 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000207
Jim Grosbachd967cd02010-12-07 21:50:47 +0000208 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
209 /// operand.
210 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
211 SmallVectorImpl<MCFixup> &Fixups) const;
212
Bill Wendlingf4caf692010-12-14 03:36:38 +0000213 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
214 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
215 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000216
Bill Wendlingb8958b02010-12-08 01:57:09 +0000217 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
218 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
219 SmallVectorImpl<MCFixup> &Fixups) const;
220
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000221 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000222 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
223 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000224
Jim Grosbach08bd5492010-10-12 23:00:24 +0000225 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000226 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000228 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
229 // '1' respectively.
230 return MI.getOperand(Op).getReg() == ARM::CPSR;
231 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000232
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000233 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000234 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
235 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000236 unsigned SoImm = MI.getOperand(Op).getImm();
237 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
238 assert(SoImmVal != -1 && "Not a valid so_imm value!");
239
240 // Encode rotate_imm.
241 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
242 << ARMII::SoRotImmShift;
243
244 // Encode immed_8.
245 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
246 return Binary;
247 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000248
Owen Anderson5de6d842010-11-12 21:12:40 +0000249 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
250 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const {
252 unsigned SoImm = MI.getOperand(Op).getImm();
253 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
254 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
255 return Encoded;
256 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000257
Owen Anderson75579f72010-11-29 22:44:32 +0000258 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
259 SmallVectorImpl<MCFixup> &Fixups) const;
260 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
261 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000262 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000264 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000266
Jim Grosbachef324d72010-10-12 23:53:58 +0000267 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000268 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000270 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000272
Jim Grosbach806e80e2010-11-03 23:52:49 +0000273 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000275 switch (MI.getOperand(Op).getImm()) {
276 default: assert (0 && "Not a valid rot_imm value!");
277 case 0: return 0;
278 case 8: return 1;
279 case 16: return 2;
280 case 24: return 3;
281 }
282 }
283
Jim Grosbach806e80e2010-11-03 23:52:49 +0000284 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
285 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000286 return MI.getOperand(Op).getImm() - 1;
287 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000288
Jim Grosbach806e80e2010-11-03 23:52:49 +0000289 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
290 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000291 return 64 - MI.getOperand(Op).getImm();
292 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000293
Jim Grosbach806e80e2010-11-03 23:52:49 +0000294 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000296
Jim Grosbach806e80e2010-11-03 23:52:49 +0000297 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
298 SmallVectorImpl<MCFixup> &Fixups) const;
299 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000301 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
302 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000303 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
304 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000305
Owen Andersonc7139a62010-11-11 19:07:48 +0000306 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
307 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000308 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000309 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000310 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000311 unsigned EncodedValue) const;
312
313 unsigned VFPThumb2PostEncoder(const MCInst &MI,
314 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000315
Jim Grosbach70933262010-11-04 01:12:30 +0000316 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000317 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 }
319
Jim Grosbach70933262010-11-04 01:12:30 +0000320 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000321 // Output the constant in little endian byte order.
322 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000323 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000324 Val >>= 8;
325 }
326 }
327
Jim Grosbach568eeed2010-09-17 18:46:17 +0000328 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
329 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000330};
331
332} // end anonymous namespace
333
Bill Wendling0800ce72010-11-02 22:53:11 +0000334MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
335 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000336 return new ARMMCCodeEmitter(TM, Ctx);
337}
338
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000339/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
340/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000341/// Thumb2 mode.
342unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
343 unsigned EncodedValue) const {
344 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
345 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000346 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000347 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
348 // set to 1111.
349 unsigned Bit24 = EncodedValue & 0x01000000;
350 unsigned Bit28 = Bit24 << 4;
351 EncodedValue &= 0xEFFFFFFF;
352 EncodedValue |= Bit28;
353 EncodedValue |= 0x0F000000;
354 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000355
Owen Andersonc7139a62010-11-11 19:07:48 +0000356 return EncodedValue;
357}
358
Owen Anderson57dac882010-11-11 21:36:43 +0000359/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000360/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000361/// Thumb2 mode.
362unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
363 unsigned EncodedValue) const {
364 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
365 if (Subtarget.isThumb2()) {
366 EncodedValue &= 0xF0FFFFFF;
367 EncodedValue |= 0x09000000;
368 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000369
Owen Anderson57dac882010-11-11 21:36:43 +0000370 return EncodedValue;
371}
372
Owen Anderson8f143912010-11-11 23:12:55 +0000373/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000374/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000375/// Thumb2 mode.
376unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
377 unsigned EncodedValue) const {
378 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
379 if (Subtarget.isThumb2()) {
380 EncodedValue &= 0x00FFFFFF;
381 EncodedValue |= 0xEE000000;
382 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000383
Owen Anderson8f143912010-11-11 23:12:55 +0000384 return EncodedValue;
385}
386
Bill Wendlingcf590262010-12-01 21:54:50 +0000387/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
388/// them to their Thumb2 form if we are currently in Thumb2 mode.
389unsigned ARMMCCodeEmitter::
390VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
391 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
392 EncodedValue &= 0x0FFFFFFF;
393 EncodedValue |= 0xE0000000;
394 }
395 return EncodedValue;
396}
Owen Anderson57dac882010-11-11 21:36:43 +0000397
Jim Grosbach56ac9072010-10-08 21:45:55 +0000398/// getMachineOpValue - Return binary encoding of operand. If the machine
399/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000400unsigned ARMMCCodeEmitter::
401getMachineOpValue(const MCInst &MI, const MCOperand &MO,
402 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000403 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000404 unsigned Reg = MO.getReg();
405 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000406
Jim Grosbachb0708d22010-11-30 23:51:41 +0000407 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000408 switch (Reg) {
409 default:
410 return RegNo;
411 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
412 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
413 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
414 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
415 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000416 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000417 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000418 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000419 } else if (MO.isFPImm()) {
420 return static_cast<unsigned>(APFloat(MO.getFPImm())
421 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000422 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000423
Jim Grosbach817c1a62010-11-19 00:27:09 +0000424 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000425 return 0;
426}
427
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000428/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000429bool ARMMCCodeEmitter::
430EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
431 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000432 const MCOperand &MO = MI.getOperand(OpIdx);
433 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000434
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000435 Reg = getARMRegisterNumbering(MO.getReg());
436
437 int32_t SImm = MO1.getImm();
438 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000439
Jim Grosbachab682a22010-10-28 18:34:10 +0000440 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000441 if (SImm == INT32_MIN)
442 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000443
Jim Grosbachab682a22010-10-28 18:34:10 +0000444 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000445 if (SImm < 0) {
446 SImm = -SImm;
447 isAdd = false;
448 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000449
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000450 Imm = SImm;
451 return isAdd;
452}
453
Bill Wendlingdff2f712010-12-08 23:01:43 +0000454/// getBranchTargetOpValue - Helper function to get the branch target operand,
455/// which is either an immediate or requires a fixup.
456static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
457 unsigned FixupKind,
458 SmallVectorImpl<MCFixup> &Fixups) {
459 const MCOperand &MO = MI.getOperand(OpIdx);
460
461 // If the destination is an immediate, we have nothing to do.
462 if (MO.isImm()) return MO.getImm();
463 assert(MO.isExpr() && "Unexpected branch target type!");
464 const MCExpr *Expr = MO.getExpr();
465 MCFixupKind Kind = MCFixupKind(FixupKind);
466 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
467
468 // All of the information is in the fixup.
469 return 0;
470}
471
472/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000473uint32_t ARMMCCodeEmitter::
474getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
475 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000477}
478
Bill Wendling09aa3f02010-12-09 00:39:08 +0000479/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
480/// BLX branch target.
481uint32_t ARMMCCodeEmitter::
482getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
483 SmallVectorImpl<MCFixup> &Fixups) const {
484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
485}
486
Jim Grosbache2467172010-12-10 18:21:33 +0000487/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
488uint32_t ARMMCCodeEmitter::
489getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
490 SmallVectorImpl<MCFixup> &Fixups) const {
491 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
492}
493
Jim Grosbach01086452010-12-10 17:13:40 +0000494/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
495uint32_t ARMMCCodeEmitter::
496getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000497 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000498 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
499}
500
Jim Grosbach027d6e82010-12-09 19:04:53 +0000501/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000502uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000503getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000504 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000505 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000506}
507
508/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
509/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000510uint32_t ARMMCCodeEmitter::
511getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000512 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000513 // FIXME: This really, really shouldn't use TargetMachine. We don't want
514 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000515 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
516 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000517 return
518 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000519 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000520}
521
Owen Andersonc2666002010-12-13 19:31:11 +0000522/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
523/// immediate branch target.
524uint32_t ARMMCCodeEmitter::
525getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
526 SmallVectorImpl<MCFixup> &Fixups) const {
527 unsigned Val =
528 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
529 bool I = (Val & 0x800000);
530 bool J1 = (Val & 0x400000);
531 bool J2 = (Val & 0x200000);
532 if (I ^ J1)
533 Val &= ~0x400000;
534 else
535 Val |= 0x400000;
536
537 if (I ^ J2)
538 Val &= ~0x200000;
539 else
540 Val |= 0x200000;
541
542 return Val;
543}
544
Bill Wendlingdff2f712010-12-08 23:01:43 +0000545/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
546/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000547uint32_t ARMMCCodeEmitter::
548getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
549 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000550 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
551 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
552 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000553}
554
Owen Andersona838a252010-12-14 00:36:49 +0000555/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
556/// target.
557uint32_t ARMMCCodeEmitter::
558getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
559 SmallVectorImpl<MCFixup> &Fixups) const {
560 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
561 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
562 Fixups);
563}
564
Jim Grosbachd40963c2010-12-14 22:28:03 +0000565/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
566/// target.
567uint32_t ARMMCCodeEmitter::
568getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
570 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
571 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
572 Fixups);
573}
574
Bill Wendlingf4caf692010-12-14 03:36:38 +0000575/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
576/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000577uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000578getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
579 SmallVectorImpl<MCFixup> &) const {
580 // [Rn, Rm]
581 // {5-3} = Rm
582 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000583 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000584 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000585 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
586 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
587 return (Rm << 3) | Rn;
588}
589
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000590/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000591uint32_t ARMMCCodeEmitter::
592getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
593 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000594 // {17-13} = reg
595 // {12} = (U)nsigned (add == '1', sub == '0')
596 // {11-0} = imm12
597 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000598 bool isAdd = true;
599 // If The first operand isn't a register, we have a label reference.
600 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000601 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
602 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000603 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000604 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000605 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000606
Owen Andersoneb6779c2010-12-07 00:45:21 +0000607 const MCExpr *Expr = 0;
608 if (!MO.isReg())
609 Expr = MO.getExpr();
610 else
611 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000612
Owen Andersond7b3f582010-12-09 01:51:07 +0000613 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
614 MCFixupKind Kind;
615 if (Subtarget.isThumb2())
616 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
617 else
618 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000619 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
620
621 ++MCNumCPRelocations;
622 } else
623 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000624
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000625 uint32_t Binary = Imm12 & 0xfff;
626 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000627 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000628 Binary |= (1 << 12);
629 Binary |= (Reg << 13);
630 return Binary;
631}
632
Owen Anderson9d63d902010-12-01 19:18:46 +0000633/// getT2AddrModeImm8s4OpValue - Return encoding info for
634/// 'reg +/- imm8<<2' operand.
635uint32_t ARMMCCodeEmitter::
636getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
637 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000638 // {12-9} = reg
639 // {8} = (U)nsigned (add == '1', sub == '0')
640 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000641 unsigned Reg, Imm8;
642 bool isAdd = true;
643 // If The first operand isn't a register, we have a label reference.
644 const MCOperand &MO = MI.getOperand(OpIdx);
645 if (!MO.isReg()) {
646 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
647 Imm8 = 0;
648 isAdd = false ; // 'U' bit is set as part of the fixup.
649
650 assert(MO.isExpr() && "Unexpected machine operand type!");
651 const MCExpr *Expr = MO.getExpr();
652 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
653 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
654
655 ++MCNumCPRelocations;
656 } else
657 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
658
659 uint32_t Binary = (Imm8 >> 2) & 0xff;
660 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
661 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000662 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000663 Binary |= (Reg << 9);
664 return Binary;
665}
666
Jim Grosbach54fea632010-11-09 17:20:53 +0000667uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000668getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
669 SmallVectorImpl<MCFixup> &Fixups) const {
670 // {20-16} = imm{15-12}
671 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000672 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000673 if (MO.isImm()) {
674 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000675 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000676 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
677 MCFixupKind Kind;
678 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000679 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000680 case MCSymbolRefExpr::VK_ARM_HI16:
681 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
682 break;
683 case MCSymbolRefExpr::VK_ARM_LO16:
684 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
685 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000686 }
687 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
688 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000689 };
690 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000691 return 0;
692}
693
694uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000695getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
696 SmallVectorImpl<MCFixup> &Fixups) const {
697 const MCOperand &MO = MI.getOperand(OpIdx);
698 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
699 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
700 unsigned Rn = getARMRegisterNumbering(MO.getReg());
701 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000702 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
703 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000704 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
705 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000706
707 // {16-13} = Rn
708 // {12} = isAdd
709 // {11-0} = shifter
710 // {3-0} = Rm
711 // {4} = 0
712 // {6-5} = type
713 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000714 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000715 Binary |= Rn << 13;
716 Binary |= SBits << 5;
717 Binary |= ShImm << 7;
718 if (isAdd)
719 Binary |= 1 << 12;
720 return Binary;
721}
722
Jim Grosbach570a9222010-11-11 01:09:40 +0000723uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000724getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
725 SmallVectorImpl<MCFixup> &Fixups) const {
726 // {17-14} Rn
727 // {13} 1 == imm12, 0 == Rm
728 // {12} isAdd
729 // {11-0} imm12/Rm
730 const MCOperand &MO = MI.getOperand(OpIdx);
731 unsigned Rn = getARMRegisterNumbering(MO.getReg());
732 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
733 Binary |= Rn << 14;
734 return Binary;
735}
736
737uint32_t ARMMCCodeEmitter::
738getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
739 SmallVectorImpl<MCFixup> &Fixups) const {
740 // {13} 1 == imm12, 0 == Rm
741 // {12} isAdd
742 // {11-0} imm12/Rm
743 const MCOperand &MO = MI.getOperand(OpIdx);
744 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
745 unsigned Imm = MO1.getImm();
746 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
747 bool isReg = MO.getReg() != 0;
748 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
749 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
750 if (isReg) {
751 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
752 Binary <<= 7; // Shift amount is bits [11:7]
753 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
754 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
755 }
756 return Binary | (isAdd << 12) | (isReg << 13);
757}
758
759uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000760getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
761 SmallVectorImpl<MCFixup> &Fixups) const {
762 // {9} 1 == imm8, 0 == Rm
763 // {8} isAdd
764 // {7-4} imm7_4/zero
765 // {3-0} imm3_0/Rm
766 const MCOperand &MO = MI.getOperand(OpIdx);
767 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
768 unsigned Imm = MO1.getImm();
769 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
770 bool isImm = MO.getReg() == 0;
771 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
772 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
773 if (!isImm)
774 Imm8 = getARMRegisterNumbering(MO.getReg());
775 return Imm8 | (isAdd << 8) | (isImm << 9);
776}
777
778uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000779getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
780 SmallVectorImpl<MCFixup> &Fixups) const {
781 // {13} 1 == imm8, 0 == Rm
782 // {12-9} Rn
783 // {8} isAdd
784 // {7-4} imm7_4/zero
785 // {3-0} imm3_0/Rm
786 const MCOperand &MO = MI.getOperand(OpIdx);
787 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
788 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
789 unsigned Rn = getARMRegisterNumbering(MO.getReg());
790 unsigned Imm = MO2.getImm();
791 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
792 bool isImm = MO1.getReg() == 0;
793 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
794 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
795 if (!isImm)
796 Imm8 = getARMRegisterNumbering(MO1.getReg());
797 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
798}
799
Bill Wendlingb8958b02010-12-08 01:57:09 +0000800/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000801uint32_t ARMMCCodeEmitter::
802getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
803 SmallVectorImpl<MCFixup> &Fixups) const {
804 // [SP, #imm]
805 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000806 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000807#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
808 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
809 "Unexpected base register!");
810#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000811 // The immediate is already shifted for the implicit zeroes, so no change
812 // here.
813 return MO1.getImm() & 0xff;
814}
815
Bill Wendlingf4caf692010-12-14 03:36:38 +0000816/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000817uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000818getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
819 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000820 // [Rn, #imm]
821 // {7-3} = imm5
822 // {2-0} = Rn
823 const MCOperand &MO = MI.getOperand(OpIdx);
824 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000825 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000826 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000827 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000828}
829
Bill Wendlingb8958b02010-12-08 01:57:09 +0000830/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
831uint32_t ARMMCCodeEmitter::
832getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
833 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000834 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000835}
836
Jim Grosbach5177f792010-12-01 21:09:40 +0000837/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000838uint32_t ARMMCCodeEmitter::
839getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
840 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000841 // {12-9} = reg
842 // {8} = (U)nsigned (add == '1', sub == '0')
843 // {7-0} = imm8
844 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000845 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000846 // If The first operand isn't a register, we have a label reference.
847 const MCOperand &MO = MI.getOperand(OpIdx);
848 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000849 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000850 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000851 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000852
853 assert(MO.isExpr() && "Unexpected machine operand type!");
854 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000855 MCFixupKind Kind;
856 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
857 if (Subtarget.isThumb2())
858 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
859 else
860 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000861 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
862
863 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000864 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000865 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000866 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
867 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000868
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000869 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
870 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000871 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000872 Binary |= (1 << 8);
873 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000874 return Binary;
875}
876
Jim Grosbach806e80e2010-11-03 23:52:49 +0000877unsigned ARMMCCodeEmitter::
878getSORegOpValue(const MCInst &MI, unsigned OpIdx,
879 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000880 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
881 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
882 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000883 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000884 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000885 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000886 // {6-5} = type
887 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000888 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000889 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000890 // else (imm shift)
891 // {11-7} = imm
892
893 const MCOperand &MO = MI.getOperand(OpIdx);
894 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
895 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
896 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
897
898 // Encode Rm.
899 unsigned Binary = getARMRegisterNumbering(MO.getReg());
900
901 // Encode the shift opcode.
902 unsigned SBits = 0;
903 unsigned Rs = MO1.getReg();
904 if (Rs) {
905 // Set shift operand (bit[7:4]).
906 // LSL - 0001
907 // LSR - 0011
908 // ASR - 0101
909 // ROR - 0111
910 // RRX - 0110 and bit[11:8] clear.
911 switch (SOpc) {
912 default: llvm_unreachable("Unknown shift opc!");
913 case ARM_AM::lsl: SBits = 0x1; break;
914 case ARM_AM::lsr: SBits = 0x3; break;
915 case ARM_AM::asr: SBits = 0x5; break;
916 case ARM_AM::ror: SBits = 0x7; break;
917 case ARM_AM::rrx: SBits = 0x6; break;
918 }
919 } else {
920 // Set shift operand (bit[6:4]).
921 // LSL - 000
922 // LSR - 010
923 // ASR - 100
924 // ROR - 110
925 switch (SOpc) {
926 default: llvm_unreachable("Unknown shift opc!");
927 case ARM_AM::lsl: SBits = 0x0; break;
928 case ARM_AM::lsr: SBits = 0x2; break;
929 case ARM_AM::asr: SBits = 0x4; break;
930 case ARM_AM::ror: SBits = 0x6; break;
931 }
932 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000933
Jim Grosbachef324d72010-10-12 23:53:58 +0000934 Binary |= SBits << 4;
935 if (SOpc == ARM_AM::rrx)
936 return Binary;
937
938 // Encode the shift operation Rs or shift_imm (except rrx).
939 if (Rs) {
940 // Encode Rs bit[11:8].
941 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
942 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
943 }
944
945 // Encode shift_imm bit[11:7].
946 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
947}
948
Jim Grosbach806e80e2010-11-03 23:52:49 +0000949unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000950getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
951 SmallVectorImpl<MCFixup> &Fixups) const {
952 const MCOperand &MO1 = MI.getOperand(OpNum);
953 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000954 const MCOperand &MO3 = MI.getOperand(OpNum+2);
955
Owen Anderson75579f72010-11-29 22:44:32 +0000956 // Encoded as [Rn, Rm, imm].
957 // FIXME: Needs fixup support.
958 unsigned Value = getARMRegisterNumbering(MO1.getReg());
959 Value <<= 4;
960 Value |= getARMRegisterNumbering(MO2.getReg());
961 Value <<= 2;
962 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000963
Owen Anderson75579f72010-11-29 22:44:32 +0000964 return Value;
965}
966
967unsigned ARMMCCodeEmitter::
968getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
969 SmallVectorImpl<MCFixup> &Fixups) const {
970 const MCOperand &MO1 = MI.getOperand(OpNum);
971 const MCOperand &MO2 = MI.getOperand(OpNum+1);
972
973 // FIXME: Needs fixup support.
974 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000975
Owen Anderson75579f72010-11-29 22:44:32 +0000976 // Even though the immediate is 8 bits long, we need 9 bits in order
977 // to represent the (inverse of the) sign bit.
978 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000979 int32_t tmp = (int32_t)MO2.getImm();
980 if (tmp < 0)
981 tmp = abs(tmp);
982 else
983 Value |= 256; // Set the ADD bit
984 Value |= tmp & 255;
985 return Value;
986}
987
988unsigned ARMMCCodeEmitter::
989getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
990 SmallVectorImpl<MCFixup> &Fixups) const {
991 const MCOperand &MO1 = MI.getOperand(OpNum);
992
993 // FIXME: Needs fixup support.
994 unsigned Value = 0;
995 int32_t tmp = (int32_t)MO1.getImm();
996 if (tmp < 0)
997 tmp = abs(tmp);
998 else
999 Value |= 256; // Set the ADD bit
1000 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001001 return Value;
1002}
1003
1004unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001005getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1006 SmallVectorImpl<MCFixup> &Fixups) const {
1007 const MCOperand &MO1 = MI.getOperand(OpNum);
1008
1009 // FIXME: Needs fixup support.
1010 unsigned Value = 0;
1011 int32_t tmp = (int32_t)MO1.getImm();
1012 if (tmp < 0)
1013 tmp = abs(tmp);
1014 else
1015 Value |= 4096; // Set the ADD bit
1016 Value |= tmp & 4095;
1017 return Value;
1018}
1019
1020unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001021getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1022 SmallVectorImpl<MCFixup> &Fixups) const {
1023 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1024 // shifted. The second is the amount to shift by.
1025 //
1026 // {3-0} = Rm.
1027 // {4} = 0
1028 // {6-5} = type
1029 // {11-7} = imm
1030
1031 const MCOperand &MO = MI.getOperand(OpIdx);
1032 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1033 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1034
1035 // Encode Rm.
1036 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1037
1038 // Encode the shift opcode.
1039 unsigned SBits = 0;
1040 // Set shift operand (bit[6:4]).
1041 // LSL - 000
1042 // LSR - 010
1043 // ASR - 100
1044 // ROR - 110
1045 switch (SOpc) {
1046 default: llvm_unreachable("Unknown shift opc!");
1047 case ARM_AM::lsl: SBits = 0x0; break;
1048 case ARM_AM::lsr: SBits = 0x2; break;
1049 case ARM_AM::asr: SBits = 0x4; break;
1050 case ARM_AM::ror: SBits = 0x6; break;
1051 }
1052
1053 Binary |= SBits << 4;
1054 if (SOpc == ARM_AM::rrx)
1055 return Binary;
1056
1057 // Encode shift_imm bit[11:7].
1058 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1059}
1060
1061unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001062getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1063 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001064 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1065 // msb of the mask.
1066 const MCOperand &MO = MI.getOperand(Op);
1067 uint32_t v = ~MO.getImm();
1068 uint32_t lsb = CountTrailingZeros_32(v);
1069 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1070 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1071 return lsb | (msb << 5);
1072}
1073
Jim Grosbach806e80e2010-11-03 23:52:49 +00001074unsigned ARMMCCodeEmitter::
1075getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001076 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001077 // VLDM/VSTM:
1078 // {12-8} = Vd
1079 // {7-0} = Number of registers
1080 //
1081 // LDM/STM:
1082 // {15-0} = Bitfield of GPRs.
1083 unsigned Reg = MI.getOperand(Op).getReg();
1084 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1085 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1086
Bill Wendling5e559a22010-11-09 00:30:18 +00001087 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001088
1089 if (SPRRegs || DPRRegs) {
1090 // VLDM/VSTM
1091 unsigned RegNo = getARMRegisterNumbering(Reg);
1092 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1093 Binary |= (RegNo & 0x1f) << 8;
1094 if (SPRRegs)
1095 Binary |= NumRegs;
1096 else
1097 Binary |= NumRegs * 2;
1098 } else {
1099 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1100 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1101 Binary |= 1 << RegNo;
1102 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001103 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001104
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001105 return Binary;
1106}
1107
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001108/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1109/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001110unsigned ARMMCCodeEmitter::
1111getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1112 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001113 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001114 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001115
Owen Andersond9aa7d32010-11-02 00:05:05 +00001116 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001117 unsigned Align = 0;
1118
1119 switch (Imm.getImm()) {
1120 default: break;
1121 case 2:
1122 case 4:
1123 case 8: Align = 0x01; break;
1124 case 16: Align = 0x02; break;
1125 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001126 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001127
Owen Andersond9aa7d32010-11-02 00:05:05 +00001128 return RegNo | (Align << 4);
1129}
1130
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001131/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1132/// alignment operand for use in VLD-dup instructions. This is the same as
1133/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1134/// different for VLD4-dup.
1135unsigned ARMMCCodeEmitter::
1136getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1137 SmallVectorImpl<MCFixup> &Fixups) const {
1138 const MCOperand &Reg = MI.getOperand(Op);
1139 const MCOperand &Imm = MI.getOperand(Op + 1);
1140
1141 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1142 unsigned Align = 0;
1143
1144 switch (Imm.getImm()) {
1145 default: break;
1146 case 2:
1147 case 4:
1148 case 8: Align = 0x01; break;
1149 case 16: Align = 0x03; break;
1150 }
1151
1152 return RegNo | (Align << 4);
1153}
1154
Jim Grosbach806e80e2010-11-03 23:52:49 +00001155unsigned ARMMCCodeEmitter::
1156getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1157 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001158 const MCOperand &MO = MI.getOperand(Op);
1159 if (MO.getReg() == 0) return 0x0D;
1160 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001161}
1162
Jim Grosbach568eeed2010-09-17 18:46:17 +00001163void ARMMCCodeEmitter::
1164EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001165 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001166 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001167 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001168 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001169 uint64_t TSFlags = Desc.TSFlags;
1170 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001171 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001172 int Size;
1173 // Basic size info comes from the TSFlags field.
1174 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1175 default: llvm_unreachable("Unexpected instruction size!");
1176 case ARMII::Size2Bytes: Size = 2; break;
1177 case ARMII::Size4Bytes: Size = 4; break;
1178 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001179 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1180 // Thumb 32-bit wide instructions need to be have the high order halfword
1181 // emitted first.
1182 if (Subtarget.isThumb() && Size == 4) {
1183 EmitConstant(Binary >> 16, 2, OS);
1184 EmitConstant(Binary & 0xffff, 2, OS);
1185 } else
1186 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001187 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001188}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001189
Jim Grosbach806e80e2010-11-03 23:52:49 +00001190#include "ARMGenMCCodeEmitter.inc"