blob: 8dca2c35c65b7c3f31e55d798d3f6b2739a72274 [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
35 MCContext &Ctx;
36
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Bill Wendling1591b292010-12-10 22:37:19 +000048// This table *must* be in the order that the fixup_* kinds are defined in
49// ARMFixupKinds.h.
50//
51// Name Offset (bits) Size (bits) Flags
52{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson05018c22010-12-09 20:27:52 +000054 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000055{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersone2e0f582010-12-10 22:46:47 +000056{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson5fd873d2010-12-10 22:53:48 +000057 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000058{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersona838a252010-12-14 00:36:49 +000059{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
60 MCFixupKindInfo::FKF_IsAligned},
Bill Wendling1591b292010-12-10 22:37:19 +000061{ "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonc2666002010-12-13 19:31:11 +000062{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
63{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000064{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
66{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_movt_hi16", 0, 16, 0 },
71{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000072 };
73
74 if (Kind < FirstTargetFixupKind)
75 return MCCodeEmitter::getFixupKindInfo(Kind);
76
77 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
78 "Invalid kind!");
79 return Infos[Kind - FirstTargetFixupKind];
80 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000081 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
82
Jim Grosbach9af82ba2010-10-07 21:57:55 +000083 // getBinaryCodeForInstr - TableGen'erated function for getting the
84 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000085 unsigned getBinaryCodeForInstr(const MCInst &MI,
86 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000087
88 /// getMachineOpValue - Return binary encoding of operand. If the machine
89 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000090 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
91 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000092
Jason W Kim837caa92010-11-18 23:37:15 +000093 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
94 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
95 SmallVectorImpl<MCFixup> &Fixups) const;
96
Bill Wendling92b5a2e2010-11-03 01:49:29 +000097 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000098 unsigned &Reg, unsigned &Imm,
99 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000100
Jim Grosbach662a8162010-12-06 23:57:07 +0000101 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000102 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000103 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
105
Bill Wendling09aa3f02010-12-09 00:39:08 +0000106 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
107 /// BLX branch target.
108 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
109 SmallVectorImpl<MCFixup> &Fixups) const;
110
Jim Grosbache2467172010-12-10 18:21:33 +0000111 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
112 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
114
Jim Grosbach01086452010-12-10 17:13:40 +0000115 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
116 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups) const;
118
Jim Grosbach027d6e82010-12-09 19:04:53 +0000119 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
120 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000121 SmallVectorImpl<MCFixup> &Fixups) const;
122
Jim Grosbachc466b932010-11-11 18:04:49 +0000123 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
124 /// branch target.
125 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
126 SmallVectorImpl<MCFixup> &Fixups) const;
127
Owen Andersonc2666002010-12-13 19:31:11 +0000128 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
129 /// immediate Thumb2 direct branch target.
130 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132
133
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000134 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
135 /// ADR label target.
136 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
137 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000138 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
140
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000141
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000142 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
143 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000144 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000146
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000147 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
148 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const;
150
Owen Anderson9d63d902010-12-01 19:18:46 +0000151 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
152 /// operand.
153 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups) const;
155
156
Jim Grosbach54fea632010-11-09 17:20:53 +0000157 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
158 /// operand as needed by load/store instructions.
159 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
161
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000162 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
163 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const {
165 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
166 switch (Mode) {
167 default: assert(0 && "Unknown addressing sub-mode!");
168 case ARM_AM::da: return 0;
169 case ARM_AM::ia: return 1;
170 case ARM_AM::db: return 2;
171 case ARM_AM::ib: return 3;
172 }
173 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000174 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
175 ///
176 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
177 switch (ShOpc) {
178 default: llvm_unreachable("Unknown shift opc!");
179 case ARM_AM::no_shift:
180 case ARM_AM::lsl: return 0;
181 case ARM_AM::lsr: return 1;
182 case ARM_AM::asr: return 2;
183 case ARM_AM::ror:
184 case ARM_AM::rrx: return 3;
185 }
186 return 0;
187 }
188
189 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
190 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192
193 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
194 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
196
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000197 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
198 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
200
Jim Grosbach570a9222010-11-11 01:09:40 +0000201 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
202 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
203 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000204
Jim Grosbachd967cd02010-12-07 21:50:47 +0000205 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
206 /// operand.
207 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
209
Bill Wendling272df512010-12-09 21:49:07 +0000210 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
211 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
212 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000213
Bill Wendlingb8958b02010-12-08 01:57:09 +0000214 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
215 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
216 SmallVectorImpl<MCFixup> &Fixups) const;
217
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000218 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000219 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
220 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000221
Jim Grosbach08bd5492010-10-12 23:00:24 +0000222 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000223 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000225 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
226 // '1' respectively.
227 return MI.getOperand(Op).getReg() == ARM::CPSR;
228 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000229
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000230 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000231 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
232 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000233 unsigned SoImm = MI.getOperand(Op).getImm();
234 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
235 assert(SoImmVal != -1 && "Not a valid so_imm value!");
236
237 // Encode rotate_imm.
238 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
239 << ARMII::SoRotImmShift;
240
241 // Encode immed_8.
242 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
243 return Binary;
244 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000245
Owen Anderson5de6d842010-11-12 21:12:40 +0000246 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
247 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const {
249 unsigned SoImm = MI.getOperand(Op).getImm();
250 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
251 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
252 return Encoded;
253 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000254
Owen Anderson75579f72010-11-29 22:44:32 +0000255 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
256 SmallVectorImpl<MCFixup> &Fixups) const;
257 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
258 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000259 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
260 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000261 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
262 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000263
Jim Grosbachef324d72010-10-12 23:53:58 +0000264 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000265 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000267 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000269
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000272 switch (MI.getOperand(Op).getImm()) {
273 default: assert (0 && "Not a valid rot_imm value!");
274 case 0: return 0;
275 case 8: return 1;
276 case 16: return 2;
277 case 24: return 3;
278 }
279 }
280
Jim Grosbach806e80e2010-11-03 23:52:49 +0000281 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000283 return MI.getOperand(Op).getImm() - 1;
284 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000285
Jim Grosbach806e80e2010-11-03 23:52:49 +0000286 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000288 return 64 - MI.getOperand(Op).getImm();
289 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000290
Jim Grosbach806e80e2010-11-03 23:52:49 +0000291 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000293
Jim Grosbach806e80e2010-11-03 23:52:49 +0000294 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
295 SmallVectorImpl<MCFixup> &Fixups) const;
296 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000298 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
299 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000300 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
301 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000302
Owen Andersonc7139a62010-11-11 19:07:48 +0000303 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
304 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000305 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000306 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000307 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000308 unsigned EncodedValue) const;
309
310 unsigned VFPThumb2PostEncoder(const MCInst &MI,
311 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000312
Jim Grosbach70933262010-11-04 01:12:30 +0000313 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000314 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000315 }
316
Jim Grosbach70933262010-11-04 01:12:30 +0000317 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 // Output the constant in little endian byte order.
319 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000320 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000321 Val >>= 8;
322 }
323 }
324
Jim Grosbach568eeed2010-09-17 18:46:17 +0000325 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
326 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000327};
328
329} // end anonymous namespace
330
Bill Wendling0800ce72010-11-02 22:53:11 +0000331MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
332 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000333 return new ARMMCCodeEmitter(TM, Ctx);
334}
335
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000336/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
337/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000338/// Thumb2 mode.
339unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
340 unsigned EncodedValue) const {
341 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
342 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000343 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000344 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
345 // set to 1111.
346 unsigned Bit24 = EncodedValue & 0x01000000;
347 unsigned Bit28 = Bit24 << 4;
348 EncodedValue &= 0xEFFFFFFF;
349 EncodedValue |= Bit28;
350 EncodedValue |= 0x0F000000;
351 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000352
Owen Andersonc7139a62010-11-11 19:07:48 +0000353 return EncodedValue;
354}
355
Owen Anderson57dac882010-11-11 21:36:43 +0000356/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000357/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000358/// Thumb2 mode.
359unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
360 unsigned EncodedValue) const {
361 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
362 if (Subtarget.isThumb2()) {
363 EncodedValue &= 0xF0FFFFFF;
364 EncodedValue |= 0x09000000;
365 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000366
Owen Anderson57dac882010-11-11 21:36:43 +0000367 return EncodedValue;
368}
369
Owen Anderson8f143912010-11-11 23:12:55 +0000370/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000371/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000372/// Thumb2 mode.
373unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
374 unsigned EncodedValue) const {
375 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
376 if (Subtarget.isThumb2()) {
377 EncodedValue &= 0x00FFFFFF;
378 EncodedValue |= 0xEE000000;
379 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000380
Owen Anderson8f143912010-11-11 23:12:55 +0000381 return EncodedValue;
382}
383
Bill Wendlingcf590262010-12-01 21:54:50 +0000384/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
385/// them to their Thumb2 form if we are currently in Thumb2 mode.
386unsigned ARMMCCodeEmitter::
387VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
388 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
389 EncodedValue &= 0x0FFFFFFF;
390 EncodedValue |= 0xE0000000;
391 }
392 return EncodedValue;
393}
Owen Anderson57dac882010-11-11 21:36:43 +0000394
Jim Grosbach56ac9072010-10-08 21:45:55 +0000395/// getMachineOpValue - Return binary encoding of operand. If the machine
396/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000397unsigned ARMMCCodeEmitter::
398getMachineOpValue(const MCInst &MI, const MCOperand &MO,
399 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000400 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000401 unsigned Reg = MO.getReg();
402 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000403
Jim Grosbachb0708d22010-11-30 23:51:41 +0000404 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000405 switch (Reg) {
406 default:
407 return RegNo;
408 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
409 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
410 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
411 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
412 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000413 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000414 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000415 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000416 } else if (MO.isFPImm()) {
417 return static_cast<unsigned>(APFloat(MO.getFPImm())
418 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000419 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000420
Jim Grosbach817c1a62010-11-19 00:27:09 +0000421 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000422 return 0;
423}
424
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000425/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000426bool ARMMCCodeEmitter::
427EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
428 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000429 const MCOperand &MO = MI.getOperand(OpIdx);
430 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000431
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000432 Reg = getARMRegisterNumbering(MO.getReg());
433
434 int32_t SImm = MO1.getImm();
435 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000436
Jim Grosbachab682a22010-10-28 18:34:10 +0000437 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000438 if (SImm == INT32_MIN)
439 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000440
Jim Grosbachab682a22010-10-28 18:34:10 +0000441 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000442 if (SImm < 0) {
443 SImm = -SImm;
444 isAdd = false;
445 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000446
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000447 Imm = SImm;
448 return isAdd;
449}
450
Bill Wendlingdff2f712010-12-08 23:01:43 +0000451/// getBranchTargetOpValue - Helper function to get the branch target operand,
452/// which is either an immediate or requires a fixup.
453static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
454 unsigned FixupKind,
455 SmallVectorImpl<MCFixup> &Fixups) {
456 const MCOperand &MO = MI.getOperand(OpIdx);
457
458 // If the destination is an immediate, we have nothing to do.
459 if (MO.isImm()) return MO.getImm();
460 assert(MO.isExpr() && "Unexpected branch target type!");
461 const MCExpr *Expr = MO.getExpr();
462 MCFixupKind Kind = MCFixupKind(FixupKind);
463 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
464
465 // All of the information is in the fixup.
466 return 0;
467}
468
469/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000470uint32_t ARMMCCodeEmitter::
471getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000473 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000474}
475
Bill Wendling09aa3f02010-12-09 00:39:08 +0000476/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
477/// BLX branch target.
478uint32_t ARMMCCodeEmitter::
479getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
480 SmallVectorImpl<MCFixup> &Fixups) const {
481 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
482}
483
Jim Grosbache2467172010-12-10 18:21:33 +0000484/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
485uint32_t ARMMCCodeEmitter::
486getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
487 SmallVectorImpl<MCFixup> &Fixups) const {
488 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
489}
490
Jim Grosbach01086452010-12-10 17:13:40 +0000491/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
492uint32_t ARMMCCodeEmitter::
493getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000494 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000495 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
496}
497
Jim Grosbach027d6e82010-12-09 19:04:53 +0000498/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000499uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000500getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000501 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000502 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000503}
504
505/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
506/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000507uint32_t ARMMCCodeEmitter::
508getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000509 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000510 // FIXME: This really, really shouldn't use TargetMachine. We don't want
511 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000512 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
513 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000514 return
515 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000516 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000517}
518
Owen Andersonc2666002010-12-13 19:31:11 +0000519/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
520/// immediate branch target.
521uint32_t ARMMCCodeEmitter::
522getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
523 SmallVectorImpl<MCFixup> &Fixups) const {
524 unsigned Val =
525 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
526 bool I = (Val & 0x800000);
527 bool J1 = (Val & 0x400000);
528 bool J2 = (Val & 0x200000);
529 if (I ^ J1)
530 Val &= ~0x400000;
531 else
532 Val |= 0x400000;
533
534 if (I ^ J2)
535 Val &= ~0x200000;
536 else
537 Val |= 0x200000;
538
539 return Val;
540}
541
Bill Wendlingdff2f712010-12-08 23:01:43 +0000542/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
543/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000544uint32_t ARMMCCodeEmitter::
545getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
546 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000547 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
548 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
549 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000550}
551
Owen Andersona838a252010-12-14 00:36:49 +0000552/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
553/// target.
554uint32_t ARMMCCodeEmitter::
555getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
556 SmallVectorImpl<MCFixup> &Fixups) const {
557 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
558 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
559 Fixups);
560}
561
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000562/// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
563uint32_t ARMMCCodeEmitter::
564getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
565 SmallVectorImpl<MCFixup> &Fixups) const {
566 const MCOperand &MO1 = MI.getOperand(OpIdx);
567 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
568 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
569 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
570 return (Rm << 3) | Rn;
571}
572
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000573/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000574uint32_t ARMMCCodeEmitter::
575getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
576 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000577 // {17-13} = reg
578 // {12} = (U)nsigned (add == '1', sub == '0')
579 // {11-0} = imm12
580 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000581 bool isAdd = true;
582 // If The first operand isn't a register, we have a label reference.
583 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000584 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
585 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000586 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000587 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000588 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000589
Owen Andersoneb6779c2010-12-07 00:45:21 +0000590 const MCExpr *Expr = 0;
591 if (!MO.isReg())
592 Expr = MO.getExpr();
593 else
594 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000595
Owen Andersond7b3f582010-12-09 01:51:07 +0000596 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
597 MCFixupKind Kind;
598 if (Subtarget.isThumb2())
599 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
600 else
601 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000602 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
603
604 ++MCNumCPRelocations;
605 } else
606 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000607
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000608 uint32_t Binary = Imm12 & 0xfff;
609 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000610 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000611 Binary |= (1 << 12);
612 Binary |= (Reg << 13);
613 return Binary;
614}
615
Owen Anderson9d63d902010-12-01 19:18:46 +0000616/// getT2AddrModeImm8s4OpValue - Return encoding info for
617/// 'reg +/- imm8<<2' operand.
618uint32_t ARMMCCodeEmitter::
619getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
620 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000621 // {12-9} = reg
622 // {8} = (U)nsigned (add == '1', sub == '0')
623 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000624 unsigned Reg, Imm8;
625 bool isAdd = true;
626 // If The first operand isn't a register, we have a label reference.
627 const MCOperand &MO = MI.getOperand(OpIdx);
628 if (!MO.isReg()) {
629 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
630 Imm8 = 0;
631 isAdd = false ; // 'U' bit is set as part of the fixup.
632
633 assert(MO.isExpr() && "Unexpected machine operand type!");
634 const MCExpr *Expr = MO.getExpr();
635 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
636 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
637
638 ++MCNumCPRelocations;
639 } else
640 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
641
642 uint32_t Binary = (Imm8 >> 2) & 0xff;
643 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
644 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000645 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000646 Binary |= (Reg << 9);
647 return Binary;
648}
649
Jim Grosbach54fea632010-11-09 17:20:53 +0000650uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000651getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
652 SmallVectorImpl<MCFixup> &Fixups) const {
653 // {20-16} = imm{15-12}
654 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000655 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000656 if (MO.isImm()) {
657 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000658 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000659 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
660 MCFixupKind Kind;
661 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000662 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000663 case MCSymbolRefExpr::VK_ARM_HI16:
664 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
665 break;
666 case MCSymbolRefExpr::VK_ARM_LO16:
667 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
668 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000669 }
670 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
671 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000672 };
673 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000674 return 0;
675}
676
677uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000678getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups) const {
680 const MCOperand &MO = MI.getOperand(OpIdx);
681 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
682 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
683 unsigned Rn = getARMRegisterNumbering(MO.getReg());
684 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000685 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
686 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000687 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
688 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000689
690 // {16-13} = Rn
691 // {12} = isAdd
692 // {11-0} = shifter
693 // {3-0} = Rm
694 // {4} = 0
695 // {6-5} = type
696 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000697 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000698 Binary |= Rn << 13;
699 Binary |= SBits << 5;
700 Binary |= ShImm << 7;
701 if (isAdd)
702 Binary |= 1 << 12;
703 return Binary;
704}
705
Jim Grosbach570a9222010-11-11 01:09:40 +0000706uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000707getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
708 SmallVectorImpl<MCFixup> &Fixups) const {
709 // {17-14} Rn
710 // {13} 1 == imm12, 0 == Rm
711 // {12} isAdd
712 // {11-0} imm12/Rm
713 const MCOperand &MO = MI.getOperand(OpIdx);
714 unsigned Rn = getARMRegisterNumbering(MO.getReg());
715 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
716 Binary |= Rn << 14;
717 return Binary;
718}
719
720uint32_t ARMMCCodeEmitter::
721getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
722 SmallVectorImpl<MCFixup> &Fixups) const {
723 // {13} 1 == imm12, 0 == Rm
724 // {12} isAdd
725 // {11-0} imm12/Rm
726 const MCOperand &MO = MI.getOperand(OpIdx);
727 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
728 unsigned Imm = MO1.getImm();
729 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
730 bool isReg = MO.getReg() != 0;
731 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
732 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
733 if (isReg) {
734 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
735 Binary <<= 7; // Shift amount is bits [11:7]
736 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
737 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
738 }
739 return Binary | (isAdd << 12) | (isReg << 13);
740}
741
742uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000743getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
744 SmallVectorImpl<MCFixup> &Fixups) const {
745 // {9} 1 == imm8, 0 == Rm
746 // {8} isAdd
747 // {7-4} imm7_4/zero
748 // {3-0} imm3_0/Rm
749 const MCOperand &MO = MI.getOperand(OpIdx);
750 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
751 unsigned Imm = MO1.getImm();
752 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
753 bool isImm = MO.getReg() == 0;
754 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
755 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
756 if (!isImm)
757 Imm8 = getARMRegisterNumbering(MO.getReg());
758 return Imm8 | (isAdd << 8) | (isImm << 9);
759}
760
761uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000762getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
763 SmallVectorImpl<MCFixup> &Fixups) const {
764 // {13} 1 == imm8, 0 == Rm
765 // {12-9} Rn
766 // {8} isAdd
767 // {7-4} imm7_4/zero
768 // {3-0} imm3_0/Rm
769 const MCOperand &MO = MI.getOperand(OpIdx);
770 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
771 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
772 unsigned Rn = getARMRegisterNumbering(MO.getReg());
773 unsigned Imm = MO2.getImm();
774 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
775 bool isImm = MO1.getReg() == 0;
776 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
777 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
778 if (!isImm)
779 Imm8 = getARMRegisterNumbering(MO1.getReg());
780 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
781}
782
Bill Wendlingb8958b02010-12-08 01:57:09 +0000783/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000784uint32_t ARMMCCodeEmitter::
785getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
786 SmallVectorImpl<MCFixup> &Fixups) const {
787 // [SP, #imm]
788 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000789 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000790#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
791 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
792 "Unexpected base register!");
793#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000794 // The immediate is already shifted for the implicit zeroes, so no change
795 // here.
796 return MO1.getImm() & 0xff;
797}
798
Bill Wendling1fd374e2010-11-30 22:57:21 +0000799/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000800uint32_t ARMMCCodeEmitter::
801getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
802 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000803 // [Rn, Rm]
804 // {5-3} = Rm
805 // {2-0} = Rn
806 //
807 // [Rn, #imm]
808 // {7-3} = imm5
809 // {2-0} = Rn
810 const MCOperand &MO = MI.getOperand(OpIdx);
811 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
812 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
813 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000814 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000815
816 if (MO2.getReg() != 0)
817 // Is an immediate.
818 Imm5 = getARMRegisterNumbering(MO2.getReg());
819
Bill Wendling272df512010-12-09 21:49:07 +0000820 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000821}
822
Bill Wendlingb8958b02010-12-08 01:57:09 +0000823/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
824uint32_t ARMMCCodeEmitter::
825getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
826 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000827 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000828}
829
Jim Grosbach5177f792010-12-01 21:09:40 +0000830/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000831uint32_t ARMMCCodeEmitter::
832getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
833 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000834 // {12-9} = reg
835 // {8} = (U)nsigned (add == '1', sub == '0')
836 // {7-0} = imm8
837 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000838 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000839 // If The first operand isn't a register, we have a label reference.
840 const MCOperand &MO = MI.getOperand(OpIdx);
841 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000842 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000843 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000844 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000845
846 assert(MO.isExpr() && "Unexpected machine operand type!");
847 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000848 MCFixupKind Kind;
849 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
850 if (Subtarget.isThumb2())
851 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
852 else
853 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000854 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
855
856 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000857 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000858 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000859 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
860 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000861
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000862 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
863 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000864 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000865 Binary |= (1 << 8);
866 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000867 return Binary;
868}
869
Jim Grosbach806e80e2010-11-03 23:52:49 +0000870unsigned ARMMCCodeEmitter::
871getSORegOpValue(const MCInst &MI, unsigned OpIdx,
872 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000873 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
874 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
875 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000876 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000877 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000878 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000879 // {6-5} = type
880 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000881 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000882 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000883 // else (imm shift)
884 // {11-7} = imm
885
886 const MCOperand &MO = MI.getOperand(OpIdx);
887 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
888 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
889 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
890
891 // Encode Rm.
892 unsigned Binary = getARMRegisterNumbering(MO.getReg());
893
894 // Encode the shift opcode.
895 unsigned SBits = 0;
896 unsigned Rs = MO1.getReg();
897 if (Rs) {
898 // Set shift operand (bit[7:4]).
899 // LSL - 0001
900 // LSR - 0011
901 // ASR - 0101
902 // ROR - 0111
903 // RRX - 0110 and bit[11:8] clear.
904 switch (SOpc) {
905 default: llvm_unreachable("Unknown shift opc!");
906 case ARM_AM::lsl: SBits = 0x1; break;
907 case ARM_AM::lsr: SBits = 0x3; break;
908 case ARM_AM::asr: SBits = 0x5; break;
909 case ARM_AM::ror: SBits = 0x7; break;
910 case ARM_AM::rrx: SBits = 0x6; break;
911 }
912 } else {
913 // Set shift operand (bit[6:4]).
914 // LSL - 000
915 // LSR - 010
916 // ASR - 100
917 // ROR - 110
918 switch (SOpc) {
919 default: llvm_unreachable("Unknown shift opc!");
920 case ARM_AM::lsl: SBits = 0x0; break;
921 case ARM_AM::lsr: SBits = 0x2; break;
922 case ARM_AM::asr: SBits = 0x4; break;
923 case ARM_AM::ror: SBits = 0x6; break;
924 }
925 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000926
Jim Grosbachef324d72010-10-12 23:53:58 +0000927 Binary |= SBits << 4;
928 if (SOpc == ARM_AM::rrx)
929 return Binary;
930
931 // Encode the shift operation Rs or shift_imm (except rrx).
932 if (Rs) {
933 // Encode Rs bit[11:8].
934 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
935 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
936 }
937
938 // Encode shift_imm bit[11:7].
939 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
940}
941
Jim Grosbach806e80e2010-11-03 23:52:49 +0000942unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000943getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
944 SmallVectorImpl<MCFixup> &Fixups) const {
945 const MCOperand &MO1 = MI.getOperand(OpNum);
946 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000947 const MCOperand &MO3 = MI.getOperand(OpNum+2);
948
Owen Anderson75579f72010-11-29 22:44:32 +0000949 // Encoded as [Rn, Rm, imm].
950 // FIXME: Needs fixup support.
951 unsigned Value = getARMRegisterNumbering(MO1.getReg());
952 Value <<= 4;
953 Value |= getARMRegisterNumbering(MO2.getReg());
954 Value <<= 2;
955 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000956
Owen Anderson75579f72010-11-29 22:44:32 +0000957 return Value;
958}
959
960unsigned ARMMCCodeEmitter::
961getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
962 SmallVectorImpl<MCFixup> &Fixups) const {
963 const MCOperand &MO1 = MI.getOperand(OpNum);
964 const MCOperand &MO2 = MI.getOperand(OpNum+1);
965
966 // FIXME: Needs fixup support.
967 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000968
Owen Anderson75579f72010-11-29 22:44:32 +0000969 // Even though the immediate is 8 bits long, we need 9 bits in order
970 // to represent the (inverse of the) sign bit.
971 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000972 int32_t tmp = (int32_t)MO2.getImm();
973 if (tmp < 0)
974 tmp = abs(tmp);
975 else
976 Value |= 256; // Set the ADD bit
977 Value |= tmp & 255;
978 return Value;
979}
980
981unsigned ARMMCCodeEmitter::
982getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
983 SmallVectorImpl<MCFixup> &Fixups) const {
984 const MCOperand &MO1 = MI.getOperand(OpNum);
985
986 // FIXME: Needs fixup support.
987 unsigned Value = 0;
988 int32_t tmp = (int32_t)MO1.getImm();
989 if (tmp < 0)
990 tmp = abs(tmp);
991 else
992 Value |= 256; // Set the ADD bit
993 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000994 return Value;
995}
996
997unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000998getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
999 SmallVectorImpl<MCFixup> &Fixups) const {
1000 const MCOperand &MO1 = MI.getOperand(OpNum);
1001
1002 // FIXME: Needs fixup support.
1003 unsigned Value = 0;
1004 int32_t tmp = (int32_t)MO1.getImm();
1005 if (tmp < 0)
1006 tmp = abs(tmp);
1007 else
1008 Value |= 4096; // Set the ADD bit
1009 Value |= tmp & 4095;
1010 return Value;
1011}
1012
1013unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001014getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1015 SmallVectorImpl<MCFixup> &Fixups) const {
1016 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1017 // shifted. The second is the amount to shift by.
1018 //
1019 // {3-0} = Rm.
1020 // {4} = 0
1021 // {6-5} = type
1022 // {11-7} = imm
1023
1024 const MCOperand &MO = MI.getOperand(OpIdx);
1025 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1026 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1027
1028 // Encode Rm.
1029 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1030
1031 // Encode the shift opcode.
1032 unsigned SBits = 0;
1033 // Set shift operand (bit[6:4]).
1034 // LSL - 000
1035 // LSR - 010
1036 // ASR - 100
1037 // ROR - 110
1038 switch (SOpc) {
1039 default: llvm_unreachable("Unknown shift opc!");
1040 case ARM_AM::lsl: SBits = 0x0; break;
1041 case ARM_AM::lsr: SBits = 0x2; break;
1042 case ARM_AM::asr: SBits = 0x4; break;
1043 case ARM_AM::ror: SBits = 0x6; break;
1044 }
1045
1046 Binary |= SBits << 4;
1047 if (SOpc == ARM_AM::rrx)
1048 return Binary;
1049
1050 // Encode shift_imm bit[11:7].
1051 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1052}
1053
1054unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001055getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1056 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001057 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1058 // msb of the mask.
1059 const MCOperand &MO = MI.getOperand(Op);
1060 uint32_t v = ~MO.getImm();
1061 uint32_t lsb = CountTrailingZeros_32(v);
1062 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1063 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1064 return lsb | (msb << 5);
1065}
1066
Jim Grosbach806e80e2010-11-03 23:52:49 +00001067unsigned ARMMCCodeEmitter::
1068getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001069 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001070 // VLDM/VSTM:
1071 // {12-8} = Vd
1072 // {7-0} = Number of registers
1073 //
1074 // LDM/STM:
1075 // {15-0} = Bitfield of GPRs.
1076 unsigned Reg = MI.getOperand(Op).getReg();
1077 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1078 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1079
Bill Wendling5e559a22010-11-09 00:30:18 +00001080 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001081
1082 if (SPRRegs || DPRRegs) {
1083 // VLDM/VSTM
1084 unsigned RegNo = getARMRegisterNumbering(Reg);
1085 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1086 Binary |= (RegNo & 0x1f) << 8;
1087 if (SPRRegs)
1088 Binary |= NumRegs;
1089 else
1090 Binary |= NumRegs * 2;
1091 } else {
1092 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1093 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1094 Binary |= 1 << RegNo;
1095 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001096 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001097
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001098 return Binary;
1099}
1100
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001101/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1102/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001103unsigned ARMMCCodeEmitter::
1104getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1105 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001106 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001107 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001108
Owen Andersond9aa7d32010-11-02 00:05:05 +00001109 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001110 unsigned Align = 0;
1111
1112 switch (Imm.getImm()) {
1113 default: break;
1114 case 2:
1115 case 4:
1116 case 8: Align = 0x01; break;
1117 case 16: Align = 0x02; break;
1118 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001119 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001120
Owen Andersond9aa7d32010-11-02 00:05:05 +00001121 return RegNo | (Align << 4);
1122}
1123
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001124/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1125/// alignment operand for use in VLD-dup instructions. This is the same as
1126/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1127/// different for VLD4-dup.
1128unsigned ARMMCCodeEmitter::
1129getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1130 SmallVectorImpl<MCFixup> &Fixups) const {
1131 const MCOperand &Reg = MI.getOperand(Op);
1132 const MCOperand &Imm = MI.getOperand(Op + 1);
1133
1134 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1135 unsigned Align = 0;
1136
1137 switch (Imm.getImm()) {
1138 default: break;
1139 case 2:
1140 case 4:
1141 case 8: Align = 0x01; break;
1142 case 16: Align = 0x03; break;
1143 }
1144
1145 return RegNo | (Align << 4);
1146}
1147
Jim Grosbach806e80e2010-11-03 23:52:49 +00001148unsigned ARMMCCodeEmitter::
1149getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1150 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001151 const MCOperand &MO = MI.getOperand(Op);
1152 if (MO.getReg() == 0) return 0x0D;
1153 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001154}
1155
Jim Grosbach568eeed2010-09-17 18:46:17 +00001156void ARMMCCodeEmitter::
1157EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001158 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001159 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001160 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001161 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001162 uint64_t TSFlags = Desc.TSFlags;
1163 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001164 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001165 int Size;
1166 // Basic size info comes from the TSFlags field.
1167 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1168 default: llvm_unreachable("Unexpected instruction size!");
1169 case ARMII::Size2Bytes: Size = 2; break;
1170 case ARMII::Size4Bytes: Size = 4; break;
1171 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001172 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1173 // Thumb 32-bit wide instructions need to be have the high order halfword
1174 // emitted first.
1175 if (Subtarget.isThumb() && Size == 4) {
1176 EmitConstant(Binary >> 16, 2, OS);
1177 EmitConstant(Binary & 0xffff, 2, OS);
1178 } else
1179 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001180 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001181}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001182
Jim Grosbach806e80e2010-11-03 23:52:49 +00001183#include "ARMGenMCCodeEmitter.inc"