blob: c633825adfee362f409a1348e7a44361d4efea20 [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 Grosbach9d04dc52010-12-14 23:47:35 +000058{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
59 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000060{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersona838a252010-12-14 00:36:49 +000061{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
62 MCFixupKindInfo::FKF_IsAligned},
Daniel Dunbarabfbac52010-12-14 17:37:16 +000063{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonc2666002010-12-13 19:31:11 +000064{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000066{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
71{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
72{ "fixup_arm_movt_hi16", 0, 16, 0 },
73{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000074 };
75
76 if (Kind < FirstTargetFixupKind)
77 return MCCodeEmitter::getFixupKindInfo(Kind);
78
79 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
80 "Invalid kind!");
81 return Infos[Kind - FirstTargetFixupKind];
82 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000083 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
84
Jim Grosbach9af82ba2010-10-07 21:57:55 +000085 // getBinaryCodeForInstr - TableGen'erated function for getting the
86 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000087 unsigned getBinaryCodeForInstr(const MCInst &MI,
88 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000089
90 /// getMachineOpValue - Return binary encoding of operand. If the machine
91 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000092 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
93 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000094
Jason W Kim837caa92010-11-18 23:37:15 +000095 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
96 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
98
Bill Wendling92b5a2e2010-11-03 01:49:29 +000099 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000100 unsigned &Reg, unsigned &Imm,
101 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000102
Jim Grosbach662a8162010-12-06 23:57:07 +0000103 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000104 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000105 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
106 SmallVectorImpl<MCFixup> &Fixups) const;
107
Bill Wendling09aa3f02010-12-09 00:39:08 +0000108 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
109 /// BLX branch target.
110 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
112
Jim Grosbache2467172010-12-10 18:21:33 +0000113 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Jim Grosbach01086452010-12-10 17:13:40 +0000117 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
118 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const;
120
Jim Grosbach027d6e82010-12-09 19:04:53 +0000121 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
122 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000123 SmallVectorImpl<MCFixup> &Fixups) const;
124
Jim Grosbachc466b932010-11-11 18:04:49 +0000125 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
126 /// branch target.
127 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
129
Owen Andersonc2666002010-12-13 19:31:11 +0000130 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
131 /// immediate Thumb2 direct branch target.
132 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const;
134
135
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000136 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
137 /// ADR label target.
138 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000140 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
141 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000142 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
143 SmallVectorImpl<MCFixup> &Fixups) const;
144
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000145
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000146 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
147 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000148 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000150
Bill Wendlingf4caf692010-12-14 03:36:38 +0000151 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
152 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
153 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000154
Owen Anderson9d63d902010-12-01 19:18:46 +0000155 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
156 /// operand.
157 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups) const;
159
160
Jim Grosbach54fea632010-11-09 17:20:53 +0000161 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
162 /// operand as needed by load/store instructions.
163 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000166 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
167 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const {
169 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
170 switch (Mode) {
171 default: assert(0 && "Unknown addressing sub-mode!");
172 case ARM_AM::da: return 0;
173 case ARM_AM::ia: return 1;
174 case ARM_AM::db: return 2;
175 case ARM_AM::ib: return 3;
176 }
177 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000178 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
179 ///
180 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
181 switch (ShOpc) {
182 default: llvm_unreachable("Unknown shift opc!");
183 case ARM_AM::no_shift:
184 case ARM_AM::lsl: return 0;
185 case ARM_AM::lsr: return 1;
186 case ARM_AM::asr: return 2;
187 case ARM_AM::ror:
188 case ARM_AM::rrx: return 3;
189 }
190 return 0;
191 }
192
193 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
194 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
196
197 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
198 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
200
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000201 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
202 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
203 SmallVectorImpl<MCFixup> &Fixups) const;
204
Jim Grosbach570a9222010-11-11 01:09:40 +0000205 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
206 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
207 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000208
Jim Grosbachd967cd02010-12-07 21:50:47 +0000209 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
210 /// operand.
211 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &Fixups) const;
213
Bill Wendlingf4caf692010-12-14 03:36:38 +0000214 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
215 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
216 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000217
Bill Wendlingb8958b02010-12-08 01:57:09 +0000218 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
219 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
220 SmallVectorImpl<MCFixup> &Fixups) const;
221
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000222 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000223 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
224 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000225
Jim Grosbach08bd5492010-10-12 23:00:24 +0000226 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000227 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
228 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000229 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
230 // '1' respectively.
231 return MI.getOperand(Op).getReg() == ARM::CPSR;
232 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000233
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000234 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000235 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000237 unsigned SoImm = MI.getOperand(Op).getImm();
238 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
239 assert(SoImmVal != -1 && "Not a valid so_imm value!");
240
241 // Encode rotate_imm.
242 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
243 << ARMII::SoRotImmShift;
244
245 // Encode immed_8.
246 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
247 return Binary;
248 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000249
Owen Anderson5de6d842010-11-12 21:12:40 +0000250 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
251 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
252 SmallVectorImpl<MCFixup> &Fixups) const {
253 unsigned SoImm = MI.getOperand(Op).getImm();
254 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
255 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
256 return Encoded;
257 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000258
Owen Anderson75579f72010-11-29 22:44:32 +0000259 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
260 SmallVectorImpl<MCFixup> &Fixups) const;
261 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
262 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000263 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
264 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000265 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000267
Jim Grosbachef324d72010-10-12 23:53:58 +0000268 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000269 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
270 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000271 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000273
Jim Grosbach806e80e2010-11-03 23:52:49 +0000274 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000276 switch (MI.getOperand(Op).getImm()) {
277 default: assert (0 && "Not a valid rot_imm value!");
278 case 0: return 0;
279 case 8: return 1;
280 case 16: return 2;
281 case 24: return 3;
282 }
283 }
284
Jim Grosbach806e80e2010-11-03 23:52:49 +0000285 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000287 return MI.getOperand(Op).getImm() - 1;
288 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000289
Jim Grosbach806e80e2010-11-03 23:52:49 +0000290 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
291 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000292 return 64 - MI.getOperand(Op).getImm();
293 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000294
Jim Grosbach806e80e2010-11-03 23:52:49 +0000295 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
296 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000297
Jim Grosbach806e80e2010-11-03 23:52:49 +0000298 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
300 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000302 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
303 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000304 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
305 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000306
Owen Andersonc7139a62010-11-11 19:07:48 +0000307 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
308 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000309 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000310 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000311 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000312 unsigned EncodedValue) const;
313
314 unsigned VFPThumb2PostEncoder(const MCInst &MI,
315 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000316
Jim Grosbach70933262010-11-04 01:12:30 +0000317 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000319 }
320
Jim Grosbach70933262010-11-04 01:12:30 +0000321 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000322 // Output the constant in little endian byte order.
323 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000324 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000325 Val >>= 8;
326 }
327 }
328
Jim Grosbach568eeed2010-09-17 18:46:17 +0000329 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
330 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000331};
332
333} // end anonymous namespace
334
Bill Wendling0800ce72010-11-02 22:53:11 +0000335MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
336 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000337 return new ARMMCCodeEmitter(TM, Ctx);
338}
339
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000340/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
341/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000342/// Thumb2 mode.
343unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
344 unsigned EncodedValue) const {
345 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
346 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000347 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000348 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
349 // set to 1111.
350 unsigned Bit24 = EncodedValue & 0x01000000;
351 unsigned Bit28 = Bit24 << 4;
352 EncodedValue &= 0xEFFFFFFF;
353 EncodedValue |= Bit28;
354 EncodedValue |= 0x0F000000;
355 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000356
Owen Andersonc7139a62010-11-11 19:07:48 +0000357 return EncodedValue;
358}
359
Owen Anderson57dac882010-11-11 21:36:43 +0000360/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000361/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000362/// Thumb2 mode.
363unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
364 unsigned EncodedValue) const {
365 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
366 if (Subtarget.isThumb2()) {
367 EncodedValue &= 0xF0FFFFFF;
368 EncodedValue |= 0x09000000;
369 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000370
Owen Anderson57dac882010-11-11 21:36:43 +0000371 return EncodedValue;
372}
373
Owen Anderson8f143912010-11-11 23:12:55 +0000374/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000375/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000376/// Thumb2 mode.
377unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
378 unsigned EncodedValue) const {
379 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
380 if (Subtarget.isThumb2()) {
381 EncodedValue &= 0x00FFFFFF;
382 EncodedValue |= 0xEE000000;
383 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000384
Owen Anderson8f143912010-11-11 23:12:55 +0000385 return EncodedValue;
386}
387
Bill Wendlingcf590262010-12-01 21:54:50 +0000388/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
389/// them to their Thumb2 form if we are currently in Thumb2 mode.
390unsigned ARMMCCodeEmitter::
391VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
392 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
393 EncodedValue &= 0x0FFFFFFF;
394 EncodedValue |= 0xE0000000;
395 }
396 return EncodedValue;
397}
Owen Anderson57dac882010-11-11 21:36:43 +0000398
Jim Grosbach56ac9072010-10-08 21:45:55 +0000399/// getMachineOpValue - Return binary encoding of operand. If the machine
400/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000401unsigned ARMMCCodeEmitter::
402getMachineOpValue(const MCInst &MI, const MCOperand &MO,
403 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000404 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000405 unsigned Reg = MO.getReg();
406 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000407
Jim Grosbachb0708d22010-11-30 23:51:41 +0000408 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000409 switch (Reg) {
410 default:
411 return RegNo;
412 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
413 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
414 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
415 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
416 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000417 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000418 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000419 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000420 } else if (MO.isFPImm()) {
421 return static_cast<unsigned>(APFloat(MO.getFPImm())
422 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000423 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000424
Jim Grosbach817c1a62010-11-19 00:27:09 +0000425 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000426 return 0;
427}
428
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000429/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000430bool ARMMCCodeEmitter::
431EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
432 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000433 const MCOperand &MO = MI.getOperand(OpIdx);
434 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000435
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000436 Reg = getARMRegisterNumbering(MO.getReg());
437
438 int32_t SImm = MO1.getImm();
439 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000440
Jim Grosbachab682a22010-10-28 18:34:10 +0000441 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000442 if (SImm == INT32_MIN)
443 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000444
Jim Grosbachab682a22010-10-28 18:34:10 +0000445 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446 if (SImm < 0) {
447 SImm = -SImm;
448 isAdd = false;
449 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000450
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000451 Imm = SImm;
452 return isAdd;
453}
454
Bill Wendlingdff2f712010-12-08 23:01:43 +0000455/// getBranchTargetOpValue - Helper function to get the branch target operand,
456/// which is either an immediate or requires a fixup.
457static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
458 unsigned FixupKind,
459 SmallVectorImpl<MCFixup> &Fixups) {
460 const MCOperand &MO = MI.getOperand(OpIdx);
461
462 // If the destination is an immediate, we have nothing to do.
463 if (MO.isImm()) return MO.getImm();
464 assert(MO.isExpr() && "Unexpected branch target type!");
465 const MCExpr *Expr = MO.getExpr();
466 MCFixupKind Kind = MCFixupKind(FixupKind);
467 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
468
469 // All of the information is in the fixup.
470 return 0;
471}
472
473/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000474uint32_t ARMMCCodeEmitter::
475getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
476 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000478}
479
Bill Wendling09aa3f02010-12-09 00:39:08 +0000480/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
481/// BLX branch target.
482uint32_t ARMMCCodeEmitter::
483getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
484 SmallVectorImpl<MCFixup> &Fixups) const {
485 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
486}
487
Jim Grosbache2467172010-12-10 18:21:33 +0000488/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
489uint32_t ARMMCCodeEmitter::
490getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
492 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
493}
494
Jim Grosbach01086452010-12-10 17:13:40 +0000495/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
496uint32_t ARMMCCodeEmitter::
497getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000498 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000499 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
500}
501
Jim Grosbach027d6e82010-12-09 19:04:53 +0000502/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000503uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000504getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000505 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000506 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000507}
508
509/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
510/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000511uint32_t ARMMCCodeEmitter::
512getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000513 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000514 // FIXME: This really, really shouldn't use TargetMachine. We don't want
515 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000516 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
517 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000518 return
519 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000520 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000521}
522
Owen Andersonc2666002010-12-13 19:31:11 +0000523/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
524/// immediate branch target.
525uint32_t ARMMCCodeEmitter::
526getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
527 SmallVectorImpl<MCFixup> &Fixups) const {
528 unsigned Val =
529 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
530 bool I = (Val & 0x800000);
531 bool J1 = (Val & 0x400000);
532 bool J2 = (Val & 0x200000);
533 if (I ^ J1)
534 Val &= ~0x400000;
535 else
536 Val |= 0x400000;
537
538 if (I ^ J2)
539 Val &= ~0x200000;
540 else
541 Val |= 0x200000;
542
543 return Val;
544}
545
Bill Wendlingdff2f712010-12-08 23:01:43 +0000546/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
547/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000548uint32_t ARMMCCodeEmitter::
549getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
550 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000551 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
552 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
553 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000554}
555
Owen Andersona838a252010-12-14 00:36:49 +0000556/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
557/// target.
558uint32_t ARMMCCodeEmitter::
559getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
560 SmallVectorImpl<MCFixup> &Fixups) const {
561 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
562 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
563 Fixups);
564}
565
Jim Grosbachd40963c2010-12-14 22:28:03 +0000566/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
567/// target.
568uint32_t ARMMCCodeEmitter::
569getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
570 SmallVectorImpl<MCFixup> &Fixups) const {
571 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
572 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
573 Fixups);
574}
575
Bill Wendlingf4caf692010-12-14 03:36:38 +0000576/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
577/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000578uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000579getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
580 SmallVectorImpl<MCFixup> &) const {
581 // [Rn, Rm]
582 // {5-3} = Rm
583 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000584 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000585 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000586 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
587 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
588 return (Rm << 3) | Rn;
589}
590
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000591/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000592uint32_t ARMMCCodeEmitter::
593getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
594 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000595 // {17-13} = reg
596 // {12} = (U)nsigned (add == '1', sub == '0')
597 // {11-0} = imm12
598 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000599 bool isAdd = true;
600 // If The first operand isn't a register, we have a label reference.
601 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000602 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
603 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000604 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000605 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000606 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000607
Owen Andersoneb6779c2010-12-07 00:45:21 +0000608 const MCExpr *Expr = 0;
609 if (!MO.isReg())
610 Expr = MO.getExpr();
611 else
612 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000613
Owen Andersond7b3f582010-12-09 01:51:07 +0000614 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
615 MCFixupKind Kind;
616 if (Subtarget.isThumb2())
617 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
618 else
619 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000620 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
621
622 ++MCNumCPRelocations;
623 } else
624 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000625
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626 uint32_t Binary = Imm12 & 0xfff;
627 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000628 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000629 Binary |= (1 << 12);
630 Binary |= (Reg << 13);
631 return Binary;
632}
633
Owen Anderson9d63d902010-12-01 19:18:46 +0000634/// getT2AddrModeImm8s4OpValue - Return encoding info for
635/// 'reg +/- imm8<<2' operand.
636uint32_t ARMMCCodeEmitter::
637getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
638 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000639 // {12-9} = reg
640 // {8} = (U)nsigned (add == '1', sub == '0')
641 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000642 unsigned Reg, Imm8;
643 bool isAdd = true;
644 // If The first operand isn't a register, we have a label reference.
645 const MCOperand &MO = MI.getOperand(OpIdx);
646 if (!MO.isReg()) {
647 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
648 Imm8 = 0;
649 isAdd = false ; // 'U' bit is set as part of the fixup.
650
651 assert(MO.isExpr() && "Unexpected machine operand type!");
652 const MCExpr *Expr = MO.getExpr();
653 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
654 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
655
656 ++MCNumCPRelocations;
657 } else
658 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
659
660 uint32_t Binary = (Imm8 >> 2) & 0xff;
661 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
662 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000663 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000664 Binary |= (Reg << 9);
665 return Binary;
666}
667
Jim Grosbach54fea632010-11-09 17:20:53 +0000668uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000669getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
670 SmallVectorImpl<MCFixup> &Fixups) const {
671 // {20-16} = imm{15-12}
672 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000673 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000674 if (MO.isImm()) {
675 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000676 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000677 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
678 MCFixupKind Kind;
679 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000680 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000681 case MCSymbolRefExpr::VK_ARM_HI16:
682 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
683 break;
684 case MCSymbolRefExpr::VK_ARM_LO16:
685 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
686 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000687 }
688 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
689 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000690 };
691 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000692 return 0;
693}
694
695uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000696getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
697 SmallVectorImpl<MCFixup> &Fixups) const {
698 const MCOperand &MO = MI.getOperand(OpIdx);
699 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
700 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
701 unsigned Rn = getARMRegisterNumbering(MO.getReg());
702 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000703 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
704 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000705 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
706 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000707
708 // {16-13} = Rn
709 // {12} = isAdd
710 // {11-0} = shifter
711 // {3-0} = Rm
712 // {4} = 0
713 // {6-5} = type
714 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000715 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000716 Binary |= Rn << 13;
717 Binary |= SBits << 5;
718 Binary |= ShImm << 7;
719 if (isAdd)
720 Binary |= 1 << 12;
721 return Binary;
722}
723
Jim Grosbach570a9222010-11-11 01:09:40 +0000724uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000725getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &Fixups) const {
727 // {17-14} Rn
728 // {13} 1 == imm12, 0 == Rm
729 // {12} isAdd
730 // {11-0} imm12/Rm
731 const MCOperand &MO = MI.getOperand(OpIdx);
732 unsigned Rn = getARMRegisterNumbering(MO.getReg());
733 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
734 Binary |= Rn << 14;
735 return Binary;
736}
737
738uint32_t ARMMCCodeEmitter::
739getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
740 SmallVectorImpl<MCFixup> &Fixups) const {
741 // {13} 1 == imm12, 0 == Rm
742 // {12} isAdd
743 // {11-0} imm12/Rm
744 const MCOperand &MO = MI.getOperand(OpIdx);
745 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
746 unsigned Imm = MO1.getImm();
747 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
748 bool isReg = MO.getReg() != 0;
749 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
750 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
751 if (isReg) {
752 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
753 Binary <<= 7; // Shift amount is bits [11:7]
754 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
755 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
756 }
757 return Binary | (isAdd << 12) | (isReg << 13);
758}
759
760uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000761getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
762 SmallVectorImpl<MCFixup> &Fixups) const {
763 // {9} 1 == imm8, 0 == Rm
764 // {8} isAdd
765 // {7-4} imm7_4/zero
766 // {3-0} imm3_0/Rm
767 const MCOperand &MO = MI.getOperand(OpIdx);
768 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
769 unsigned Imm = MO1.getImm();
770 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
771 bool isImm = MO.getReg() == 0;
772 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
773 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
774 if (!isImm)
775 Imm8 = getARMRegisterNumbering(MO.getReg());
776 return Imm8 | (isAdd << 8) | (isImm << 9);
777}
778
779uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000780getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
781 SmallVectorImpl<MCFixup> &Fixups) const {
782 // {13} 1 == imm8, 0 == Rm
783 // {12-9} Rn
784 // {8} isAdd
785 // {7-4} imm7_4/zero
786 // {3-0} imm3_0/Rm
787 const MCOperand &MO = MI.getOperand(OpIdx);
788 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
789 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
790 unsigned Rn = getARMRegisterNumbering(MO.getReg());
791 unsigned Imm = MO2.getImm();
792 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
793 bool isImm = MO1.getReg() == 0;
794 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
795 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
796 if (!isImm)
797 Imm8 = getARMRegisterNumbering(MO1.getReg());
798 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
799}
800
Bill Wendlingb8958b02010-12-08 01:57:09 +0000801/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000802uint32_t ARMMCCodeEmitter::
803getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
804 SmallVectorImpl<MCFixup> &Fixups) const {
805 // [SP, #imm]
806 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000807 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000808#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
809 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
810 "Unexpected base register!");
811#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000812 // The immediate is already shifted for the implicit zeroes, so no change
813 // here.
814 return MO1.getImm() & 0xff;
815}
816
Bill Wendlingf4caf692010-12-14 03:36:38 +0000817/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000818uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000819getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
820 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000821 // [Rn, #imm]
822 // {7-3} = imm5
823 // {2-0} = Rn
824 const MCOperand &MO = MI.getOperand(OpIdx);
825 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000826 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000827 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000828 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000829}
830
Bill Wendlingb8958b02010-12-08 01:57:09 +0000831/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
832uint32_t ARMMCCodeEmitter::
833getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
834 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000835 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000836}
837
Jim Grosbach5177f792010-12-01 21:09:40 +0000838/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000839uint32_t ARMMCCodeEmitter::
840getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
841 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000842 // {12-9} = reg
843 // {8} = (U)nsigned (add == '1', sub == '0')
844 // {7-0} = imm8
845 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000846 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000847 // If The first operand isn't a register, we have a label reference.
848 const MCOperand &MO = MI.getOperand(OpIdx);
849 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000850 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000851 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000852 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000853
854 assert(MO.isExpr() && "Unexpected machine operand type!");
855 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000856 MCFixupKind Kind;
857 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
858 if (Subtarget.isThumb2())
859 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
860 else
861 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000862 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
863
864 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000865 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000866 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000867 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
868 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000869
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000870 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
871 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000872 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000873 Binary |= (1 << 8);
874 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000875 return Binary;
876}
877
Jim Grosbach806e80e2010-11-03 23:52:49 +0000878unsigned ARMMCCodeEmitter::
879getSORegOpValue(const MCInst &MI, unsigned OpIdx,
880 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000881 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
882 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
883 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000884 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000885 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000886 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000887 // {6-5} = type
888 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000889 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000890 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000891 // else (imm shift)
892 // {11-7} = imm
893
894 const MCOperand &MO = MI.getOperand(OpIdx);
895 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
896 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
897 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
898
899 // Encode Rm.
900 unsigned Binary = getARMRegisterNumbering(MO.getReg());
901
902 // Encode the shift opcode.
903 unsigned SBits = 0;
904 unsigned Rs = MO1.getReg();
905 if (Rs) {
906 // Set shift operand (bit[7:4]).
907 // LSL - 0001
908 // LSR - 0011
909 // ASR - 0101
910 // ROR - 0111
911 // RRX - 0110 and bit[11:8] clear.
912 switch (SOpc) {
913 default: llvm_unreachable("Unknown shift opc!");
914 case ARM_AM::lsl: SBits = 0x1; break;
915 case ARM_AM::lsr: SBits = 0x3; break;
916 case ARM_AM::asr: SBits = 0x5; break;
917 case ARM_AM::ror: SBits = 0x7; break;
918 case ARM_AM::rrx: SBits = 0x6; break;
919 }
920 } else {
921 // Set shift operand (bit[6:4]).
922 // LSL - 000
923 // LSR - 010
924 // ASR - 100
925 // ROR - 110
926 switch (SOpc) {
927 default: llvm_unreachable("Unknown shift opc!");
928 case ARM_AM::lsl: SBits = 0x0; break;
929 case ARM_AM::lsr: SBits = 0x2; break;
930 case ARM_AM::asr: SBits = 0x4; break;
931 case ARM_AM::ror: SBits = 0x6; break;
932 }
933 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000934
Jim Grosbachef324d72010-10-12 23:53:58 +0000935 Binary |= SBits << 4;
936 if (SOpc == ARM_AM::rrx)
937 return Binary;
938
939 // Encode the shift operation Rs or shift_imm (except rrx).
940 if (Rs) {
941 // Encode Rs bit[11:8].
942 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
943 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
944 }
945
946 // Encode shift_imm bit[11:7].
947 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
948}
949
Jim Grosbach806e80e2010-11-03 23:52:49 +0000950unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000951getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
952 SmallVectorImpl<MCFixup> &Fixups) const {
953 const MCOperand &MO1 = MI.getOperand(OpNum);
954 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000955 const MCOperand &MO3 = MI.getOperand(OpNum+2);
956
Owen Anderson75579f72010-11-29 22:44:32 +0000957 // Encoded as [Rn, Rm, imm].
958 // FIXME: Needs fixup support.
959 unsigned Value = getARMRegisterNumbering(MO1.getReg());
960 Value <<= 4;
961 Value |= getARMRegisterNumbering(MO2.getReg());
962 Value <<= 2;
963 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000964
Owen Anderson75579f72010-11-29 22:44:32 +0000965 return Value;
966}
967
968unsigned ARMMCCodeEmitter::
969getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
970 SmallVectorImpl<MCFixup> &Fixups) const {
971 const MCOperand &MO1 = MI.getOperand(OpNum);
972 const MCOperand &MO2 = MI.getOperand(OpNum+1);
973
974 // FIXME: Needs fixup support.
975 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000976
Owen Anderson75579f72010-11-29 22:44:32 +0000977 // Even though the immediate is 8 bits long, we need 9 bits in order
978 // to represent the (inverse of the) sign bit.
979 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000980 int32_t tmp = (int32_t)MO2.getImm();
981 if (tmp < 0)
982 tmp = abs(tmp);
983 else
984 Value |= 256; // Set the ADD bit
985 Value |= tmp & 255;
986 return Value;
987}
988
989unsigned ARMMCCodeEmitter::
990getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
991 SmallVectorImpl<MCFixup> &Fixups) const {
992 const MCOperand &MO1 = MI.getOperand(OpNum);
993
994 // FIXME: Needs fixup support.
995 unsigned Value = 0;
996 int32_t tmp = (int32_t)MO1.getImm();
997 if (tmp < 0)
998 tmp = abs(tmp);
999 else
1000 Value |= 256; // Set the ADD bit
1001 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001002 return Value;
1003}
1004
1005unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001006getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1007 SmallVectorImpl<MCFixup> &Fixups) const {
1008 const MCOperand &MO1 = MI.getOperand(OpNum);
1009
1010 // FIXME: Needs fixup support.
1011 unsigned Value = 0;
1012 int32_t tmp = (int32_t)MO1.getImm();
1013 if (tmp < 0)
1014 tmp = abs(tmp);
1015 else
1016 Value |= 4096; // Set the ADD bit
1017 Value |= tmp & 4095;
1018 return Value;
1019}
1020
1021unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001022getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1023 SmallVectorImpl<MCFixup> &Fixups) const {
1024 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1025 // shifted. The second is the amount to shift by.
1026 //
1027 // {3-0} = Rm.
1028 // {4} = 0
1029 // {6-5} = type
1030 // {11-7} = imm
1031
1032 const MCOperand &MO = MI.getOperand(OpIdx);
1033 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1034 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1035
1036 // Encode Rm.
1037 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1038
1039 // Encode the shift opcode.
1040 unsigned SBits = 0;
1041 // Set shift operand (bit[6:4]).
1042 // LSL - 000
1043 // LSR - 010
1044 // ASR - 100
1045 // ROR - 110
1046 switch (SOpc) {
1047 default: llvm_unreachable("Unknown shift opc!");
1048 case ARM_AM::lsl: SBits = 0x0; break;
1049 case ARM_AM::lsr: SBits = 0x2; break;
1050 case ARM_AM::asr: SBits = 0x4; break;
1051 case ARM_AM::ror: SBits = 0x6; break;
1052 }
1053
1054 Binary |= SBits << 4;
1055 if (SOpc == ARM_AM::rrx)
1056 return Binary;
1057
1058 // Encode shift_imm bit[11:7].
1059 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1060}
1061
1062unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001063getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1064 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001065 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1066 // msb of the mask.
1067 const MCOperand &MO = MI.getOperand(Op);
1068 uint32_t v = ~MO.getImm();
1069 uint32_t lsb = CountTrailingZeros_32(v);
1070 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1071 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1072 return lsb | (msb << 5);
1073}
1074
Jim Grosbach806e80e2010-11-03 23:52:49 +00001075unsigned ARMMCCodeEmitter::
1076getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001077 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001078 // VLDM/VSTM:
1079 // {12-8} = Vd
1080 // {7-0} = Number of registers
1081 //
1082 // LDM/STM:
1083 // {15-0} = Bitfield of GPRs.
1084 unsigned Reg = MI.getOperand(Op).getReg();
1085 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1086 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1087
Bill Wendling5e559a22010-11-09 00:30:18 +00001088 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001089
1090 if (SPRRegs || DPRRegs) {
1091 // VLDM/VSTM
1092 unsigned RegNo = getARMRegisterNumbering(Reg);
1093 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1094 Binary |= (RegNo & 0x1f) << 8;
1095 if (SPRRegs)
1096 Binary |= NumRegs;
1097 else
1098 Binary |= NumRegs * 2;
1099 } else {
1100 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1101 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1102 Binary |= 1 << RegNo;
1103 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001104 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001105
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001106 return Binary;
1107}
1108
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001109/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1110/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001111unsigned ARMMCCodeEmitter::
1112getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1113 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001114 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001115 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001116
Owen Andersond9aa7d32010-11-02 00:05:05 +00001117 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001118 unsigned Align = 0;
1119
1120 switch (Imm.getImm()) {
1121 default: break;
1122 case 2:
1123 case 4:
1124 case 8: Align = 0x01; break;
1125 case 16: Align = 0x02; break;
1126 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001127 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001128
Owen Andersond9aa7d32010-11-02 00:05:05 +00001129 return RegNo | (Align << 4);
1130}
1131
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001132/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1133/// alignment operand for use in VLD-dup instructions. This is the same as
1134/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1135/// different for VLD4-dup.
1136unsigned ARMMCCodeEmitter::
1137getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1138 SmallVectorImpl<MCFixup> &Fixups) const {
1139 const MCOperand &Reg = MI.getOperand(Op);
1140 const MCOperand &Imm = MI.getOperand(Op + 1);
1141
1142 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1143 unsigned Align = 0;
1144
1145 switch (Imm.getImm()) {
1146 default: break;
1147 case 2:
1148 case 4:
1149 case 8: Align = 0x01; break;
1150 case 16: Align = 0x03; break;
1151 }
1152
1153 return RegNo | (Align << 4);
1154}
1155
Jim Grosbach806e80e2010-11-03 23:52:49 +00001156unsigned ARMMCCodeEmitter::
1157getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1158 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001159 const MCOperand &MO = MI.getOperand(Op);
1160 if (MO.getReg() == 0) return 0x0D;
1161 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001162}
1163
Jim Grosbach568eeed2010-09-17 18:46:17 +00001164void ARMMCCodeEmitter::
1165EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001166 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001167 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001168 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001169 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001170 uint64_t TSFlags = Desc.TSFlags;
1171 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001172 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001173 int Size;
1174 // Basic size info comes from the TSFlags field.
1175 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1176 default: llvm_unreachable("Unexpected instruction size!");
1177 case ARMII::Size2Bytes: Size = 2; break;
1178 case ARMII::Size4Bytes: Size = 4; break;
1179 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001180 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1181 // Thumb 32-bit wide instructions need to be have the high order halfword
1182 // emitted first.
1183 if (Subtarget.isThumb() && Size == 4) {
1184 EmitConstant(Binary >> 16, 2, OS);
1185 EmitConstant(Binary & 0xffff, 2, OS);
1186 } else
1187 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001188 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001189}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001190
Jim Grosbach806e80e2010-11-03 23:52:49 +00001191#include "ARMGenMCCodeEmitter.inc"