blob: 0924591a60c74aaef77e95946eb3b6842dd201f8 [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},
Daniel Dunbarabfbac52010-12-14 17:37:16 +000061{ "fixup_arm_branch", 0, 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
Bill Wendlingf4caf692010-12-14 03:36:38 +0000147 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
148 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000150
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 Wendlingf4caf692010-12-14 03:36:38 +0000210 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
211 uint32_t getAddrModeISOpValue(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
Bill Wendlingf4caf692010-12-14 03:36:38 +0000562/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
563/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000564uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000565getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
566 SmallVectorImpl<MCFixup> &) const {
567 // [Rn, Rm]
568 // {5-3} = Rm
569 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000570 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000571 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000572 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
573 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
574 return (Rm << 3) | Rn;
575}
576
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000577/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000578uint32_t ARMMCCodeEmitter::
579getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
580 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000581 // {17-13} = reg
582 // {12} = (U)nsigned (add == '1', sub == '0')
583 // {11-0} = imm12
584 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000585 bool isAdd = true;
586 // If The first operand isn't a register, we have a label reference.
587 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000588 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
589 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000590 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000591 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000592 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000593
Owen Andersoneb6779c2010-12-07 00:45:21 +0000594 const MCExpr *Expr = 0;
595 if (!MO.isReg())
596 Expr = MO.getExpr();
597 else
598 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000599
Owen Andersond7b3f582010-12-09 01:51:07 +0000600 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
601 MCFixupKind Kind;
602 if (Subtarget.isThumb2())
603 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
604 else
605 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000606 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
607
608 ++MCNumCPRelocations;
609 } else
610 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000611
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000612 uint32_t Binary = Imm12 & 0xfff;
613 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000614 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000615 Binary |= (1 << 12);
616 Binary |= (Reg << 13);
617 return Binary;
618}
619
Owen Anderson9d63d902010-12-01 19:18:46 +0000620/// getT2AddrModeImm8s4OpValue - Return encoding info for
621/// 'reg +/- imm8<<2' operand.
622uint32_t ARMMCCodeEmitter::
623getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
624 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000625 // {12-9} = reg
626 // {8} = (U)nsigned (add == '1', sub == '0')
627 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000628 unsigned Reg, Imm8;
629 bool isAdd = true;
630 // If The first operand isn't a register, we have a label reference.
631 const MCOperand &MO = MI.getOperand(OpIdx);
632 if (!MO.isReg()) {
633 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
634 Imm8 = 0;
635 isAdd = false ; // 'U' bit is set as part of the fixup.
636
637 assert(MO.isExpr() && "Unexpected machine operand type!");
638 const MCExpr *Expr = MO.getExpr();
639 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
640 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
641
642 ++MCNumCPRelocations;
643 } else
644 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
645
646 uint32_t Binary = (Imm8 >> 2) & 0xff;
647 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
648 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000649 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000650 Binary |= (Reg << 9);
651 return Binary;
652}
653
Jim Grosbach54fea632010-11-09 17:20:53 +0000654uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000655getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
656 SmallVectorImpl<MCFixup> &Fixups) const {
657 // {20-16} = imm{15-12}
658 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000659 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000660 if (MO.isImm()) {
661 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000662 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000663 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
664 MCFixupKind Kind;
665 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000666 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000667 case MCSymbolRefExpr::VK_ARM_HI16:
668 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
669 break;
670 case MCSymbolRefExpr::VK_ARM_LO16:
671 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
672 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000673 }
674 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
675 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000676 };
677 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000678 return 0;
679}
680
681uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000682getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
683 SmallVectorImpl<MCFixup> &Fixups) const {
684 const MCOperand &MO = MI.getOperand(OpIdx);
685 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
686 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
687 unsigned Rn = getARMRegisterNumbering(MO.getReg());
688 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000689 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
690 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000691 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
692 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000693
694 // {16-13} = Rn
695 // {12} = isAdd
696 // {11-0} = shifter
697 // {3-0} = Rm
698 // {4} = 0
699 // {6-5} = type
700 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000701 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000702 Binary |= Rn << 13;
703 Binary |= SBits << 5;
704 Binary |= ShImm << 7;
705 if (isAdd)
706 Binary |= 1 << 12;
707 return Binary;
708}
709
Jim Grosbach570a9222010-11-11 01:09:40 +0000710uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000711getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &Fixups) const {
713 // {17-14} Rn
714 // {13} 1 == imm12, 0 == Rm
715 // {12} isAdd
716 // {11-0} imm12/Rm
717 const MCOperand &MO = MI.getOperand(OpIdx);
718 unsigned Rn = getARMRegisterNumbering(MO.getReg());
719 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
720 Binary |= Rn << 14;
721 return Binary;
722}
723
724uint32_t ARMMCCodeEmitter::
725getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &Fixups) const {
727 // {13} 1 == imm12, 0 == Rm
728 // {12} isAdd
729 // {11-0} imm12/Rm
730 const MCOperand &MO = MI.getOperand(OpIdx);
731 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
732 unsigned Imm = MO1.getImm();
733 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
734 bool isReg = MO.getReg() != 0;
735 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
736 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
737 if (isReg) {
738 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
739 Binary <<= 7; // Shift amount is bits [11:7]
740 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
741 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
742 }
743 return Binary | (isAdd << 12) | (isReg << 13);
744}
745
746uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000747getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &Fixups) const {
749 // {9} 1 == imm8, 0 == Rm
750 // {8} isAdd
751 // {7-4} imm7_4/zero
752 // {3-0} imm3_0/Rm
753 const MCOperand &MO = MI.getOperand(OpIdx);
754 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
755 unsigned Imm = MO1.getImm();
756 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
757 bool isImm = MO.getReg() == 0;
758 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
759 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
760 if (!isImm)
761 Imm8 = getARMRegisterNumbering(MO.getReg());
762 return Imm8 | (isAdd << 8) | (isImm << 9);
763}
764
765uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000766getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
767 SmallVectorImpl<MCFixup> &Fixups) const {
768 // {13} 1 == imm8, 0 == Rm
769 // {12-9} Rn
770 // {8} isAdd
771 // {7-4} imm7_4/zero
772 // {3-0} imm3_0/Rm
773 const MCOperand &MO = MI.getOperand(OpIdx);
774 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
775 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
776 unsigned Rn = getARMRegisterNumbering(MO.getReg());
777 unsigned Imm = MO2.getImm();
778 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
779 bool isImm = MO1.getReg() == 0;
780 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
781 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
782 if (!isImm)
783 Imm8 = getARMRegisterNumbering(MO1.getReg());
784 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
785}
786
Bill Wendlingb8958b02010-12-08 01:57:09 +0000787/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000788uint32_t ARMMCCodeEmitter::
789getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
790 SmallVectorImpl<MCFixup> &Fixups) const {
791 // [SP, #imm]
792 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000793 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000794#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
795 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
796 "Unexpected base register!");
797#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000798 // The immediate is already shifted for the implicit zeroes, so no change
799 // here.
800 return MO1.getImm() & 0xff;
801}
802
Bill Wendlingf4caf692010-12-14 03:36:38 +0000803/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000804uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000805getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
806 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000807 // [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);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000812 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000813 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000814 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000815}
816
Bill Wendlingb8958b02010-12-08 01:57:09 +0000817/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
818uint32_t ARMMCCodeEmitter::
819getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
820 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000821 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000822}
823
Jim Grosbach5177f792010-12-01 21:09:40 +0000824/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000825uint32_t ARMMCCodeEmitter::
826getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
827 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000828 // {12-9} = reg
829 // {8} = (U)nsigned (add == '1', sub == '0')
830 // {7-0} = imm8
831 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000832 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000833 // If The first operand isn't a register, we have a label reference.
834 const MCOperand &MO = MI.getOperand(OpIdx);
835 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000836 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000837 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000838 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000839
840 assert(MO.isExpr() && "Unexpected machine operand type!");
841 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000842 MCFixupKind Kind;
843 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
844 if (Subtarget.isThumb2())
845 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
846 else
847 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000848 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
849
850 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000851 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000852 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000853 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
854 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000855
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000856 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
857 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000858 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000859 Binary |= (1 << 8);
860 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000861 return Binary;
862}
863
Jim Grosbach806e80e2010-11-03 23:52:49 +0000864unsigned ARMMCCodeEmitter::
865getSORegOpValue(const MCInst &MI, unsigned OpIdx,
866 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000867 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
868 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
869 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000870 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000871 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000872 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000873 // {6-5} = type
874 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000875 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000876 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000877 // else (imm shift)
878 // {11-7} = imm
879
880 const MCOperand &MO = MI.getOperand(OpIdx);
881 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
882 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
883 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
884
885 // Encode Rm.
886 unsigned Binary = getARMRegisterNumbering(MO.getReg());
887
888 // Encode the shift opcode.
889 unsigned SBits = 0;
890 unsigned Rs = MO1.getReg();
891 if (Rs) {
892 // Set shift operand (bit[7:4]).
893 // LSL - 0001
894 // LSR - 0011
895 // ASR - 0101
896 // ROR - 0111
897 // RRX - 0110 and bit[11:8] clear.
898 switch (SOpc) {
899 default: llvm_unreachable("Unknown shift opc!");
900 case ARM_AM::lsl: SBits = 0x1; break;
901 case ARM_AM::lsr: SBits = 0x3; break;
902 case ARM_AM::asr: SBits = 0x5; break;
903 case ARM_AM::ror: SBits = 0x7; break;
904 case ARM_AM::rrx: SBits = 0x6; break;
905 }
906 } else {
907 // Set shift operand (bit[6:4]).
908 // LSL - 000
909 // LSR - 010
910 // ASR - 100
911 // ROR - 110
912 switch (SOpc) {
913 default: llvm_unreachable("Unknown shift opc!");
914 case ARM_AM::lsl: SBits = 0x0; break;
915 case ARM_AM::lsr: SBits = 0x2; break;
916 case ARM_AM::asr: SBits = 0x4; break;
917 case ARM_AM::ror: SBits = 0x6; break;
918 }
919 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000920
Jim Grosbachef324d72010-10-12 23:53:58 +0000921 Binary |= SBits << 4;
922 if (SOpc == ARM_AM::rrx)
923 return Binary;
924
925 // Encode the shift operation Rs or shift_imm (except rrx).
926 if (Rs) {
927 // Encode Rs bit[11:8].
928 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
929 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
930 }
931
932 // Encode shift_imm bit[11:7].
933 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
934}
935
Jim Grosbach806e80e2010-11-03 23:52:49 +0000936unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000937getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
938 SmallVectorImpl<MCFixup> &Fixups) const {
939 const MCOperand &MO1 = MI.getOperand(OpNum);
940 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000941 const MCOperand &MO3 = MI.getOperand(OpNum+2);
942
Owen Anderson75579f72010-11-29 22:44:32 +0000943 // Encoded as [Rn, Rm, imm].
944 // FIXME: Needs fixup support.
945 unsigned Value = getARMRegisterNumbering(MO1.getReg());
946 Value <<= 4;
947 Value |= getARMRegisterNumbering(MO2.getReg());
948 Value <<= 2;
949 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000950
Owen Anderson75579f72010-11-29 22:44:32 +0000951 return Value;
952}
953
954unsigned ARMMCCodeEmitter::
955getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
956 SmallVectorImpl<MCFixup> &Fixups) const {
957 const MCOperand &MO1 = MI.getOperand(OpNum);
958 const MCOperand &MO2 = MI.getOperand(OpNum+1);
959
960 // FIXME: Needs fixup support.
961 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000962
Owen Anderson75579f72010-11-29 22:44:32 +0000963 // Even though the immediate is 8 bits long, we need 9 bits in order
964 // to represent the (inverse of the) sign bit.
965 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000966 int32_t tmp = (int32_t)MO2.getImm();
967 if (tmp < 0)
968 tmp = abs(tmp);
969 else
970 Value |= 256; // Set the ADD bit
971 Value |= tmp & 255;
972 return Value;
973}
974
975unsigned ARMMCCodeEmitter::
976getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
977 SmallVectorImpl<MCFixup> &Fixups) const {
978 const MCOperand &MO1 = MI.getOperand(OpNum);
979
980 // FIXME: Needs fixup support.
981 unsigned Value = 0;
982 int32_t tmp = (int32_t)MO1.getImm();
983 if (tmp < 0)
984 tmp = abs(tmp);
985 else
986 Value |= 256; // Set the ADD bit
987 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000988 return Value;
989}
990
991unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000992getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
993 SmallVectorImpl<MCFixup> &Fixups) const {
994 const MCOperand &MO1 = MI.getOperand(OpNum);
995
996 // FIXME: Needs fixup support.
997 unsigned Value = 0;
998 int32_t tmp = (int32_t)MO1.getImm();
999 if (tmp < 0)
1000 tmp = abs(tmp);
1001 else
1002 Value |= 4096; // Set the ADD bit
1003 Value |= tmp & 4095;
1004 return Value;
1005}
1006
1007unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001008getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1009 SmallVectorImpl<MCFixup> &Fixups) const {
1010 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1011 // shifted. The second is the amount to shift by.
1012 //
1013 // {3-0} = Rm.
1014 // {4} = 0
1015 // {6-5} = type
1016 // {11-7} = imm
1017
1018 const MCOperand &MO = MI.getOperand(OpIdx);
1019 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1020 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1021
1022 // Encode Rm.
1023 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1024
1025 // Encode the shift opcode.
1026 unsigned SBits = 0;
1027 // Set shift operand (bit[6:4]).
1028 // LSL - 000
1029 // LSR - 010
1030 // ASR - 100
1031 // ROR - 110
1032 switch (SOpc) {
1033 default: llvm_unreachable("Unknown shift opc!");
1034 case ARM_AM::lsl: SBits = 0x0; break;
1035 case ARM_AM::lsr: SBits = 0x2; break;
1036 case ARM_AM::asr: SBits = 0x4; break;
1037 case ARM_AM::ror: SBits = 0x6; break;
1038 }
1039
1040 Binary |= SBits << 4;
1041 if (SOpc == ARM_AM::rrx)
1042 return Binary;
1043
1044 // Encode shift_imm bit[11:7].
1045 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1046}
1047
1048unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001049getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1050 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001051 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1052 // msb of the mask.
1053 const MCOperand &MO = MI.getOperand(Op);
1054 uint32_t v = ~MO.getImm();
1055 uint32_t lsb = CountTrailingZeros_32(v);
1056 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1057 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1058 return lsb | (msb << 5);
1059}
1060
Jim Grosbach806e80e2010-11-03 23:52:49 +00001061unsigned ARMMCCodeEmitter::
1062getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001063 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001064 // VLDM/VSTM:
1065 // {12-8} = Vd
1066 // {7-0} = Number of registers
1067 //
1068 // LDM/STM:
1069 // {15-0} = Bitfield of GPRs.
1070 unsigned Reg = MI.getOperand(Op).getReg();
1071 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1072 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1073
Bill Wendling5e559a22010-11-09 00:30:18 +00001074 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001075
1076 if (SPRRegs || DPRRegs) {
1077 // VLDM/VSTM
1078 unsigned RegNo = getARMRegisterNumbering(Reg);
1079 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1080 Binary |= (RegNo & 0x1f) << 8;
1081 if (SPRRegs)
1082 Binary |= NumRegs;
1083 else
1084 Binary |= NumRegs * 2;
1085 } else {
1086 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1087 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1088 Binary |= 1 << RegNo;
1089 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001090 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001091
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001092 return Binary;
1093}
1094
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001095/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1096/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001097unsigned ARMMCCodeEmitter::
1098getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1099 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001100 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001101 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001102
Owen Andersond9aa7d32010-11-02 00:05:05 +00001103 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001104 unsigned Align = 0;
1105
1106 switch (Imm.getImm()) {
1107 default: break;
1108 case 2:
1109 case 4:
1110 case 8: Align = 0x01; break;
1111 case 16: Align = 0x02; break;
1112 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001113 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001114
Owen Andersond9aa7d32010-11-02 00:05:05 +00001115 return RegNo | (Align << 4);
1116}
1117
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001118/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1119/// alignment operand for use in VLD-dup instructions. This is the same as
1120/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1121/// different for VLD4-dup.
1122unsigned ARMMCCodeEmitter::
1123getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1124 SmallVectorImpl<MCFixup> &Fixups) const {
1125 const MCOperand &Reg = MI.getOperand(Op);
1126 const MCOperand &Imm = MI.getOperand(Op + 1);
1127
1128 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1129 unsigned Align = 0;
1130
1131 switch (Imm.getImm()) {
1132 default: break;
1133 case 2:
1134 case 4:
1135 case 8: Align = 0x01; break;
1136 case 16: Align = 0x03; break;
1137 }
1138
1139 return RegNo | (Align << 4);
1140}
1141
Jim Grosbach806e80e2010-11-03 23:52:49 +00001142unsigned ARMMCCodeEmitter::
1143getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1144 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001145 const MCOperand &MO = MI.getOperand(Op);
1146 if (MO.getReg() == 0) return 0x0D;
1147 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001148}
1149
Jim Grosbach568eeed2010-09-17 18:46:17 +00001150void ARMMCCodeEmitter::
1151EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001152 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001153 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001154 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001155 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001156 uint64_t TSFlags = Desc.TSFlags;
1157 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001158 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001159 int Size;
1160 // Basic size info comes from the TSFlags field.
1161 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1162 default: llvm_unreachable("Unexpected instruction size!");
1163 case ARMII::Size2Bytes: Size = 2; break;
1164 case ARMII::Size4Bytes: Size = 4; break;
1165 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001166 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1167 // Thumb 32-bit wide instructions need to be have the high order halfword
1168 // emitted first.
1169 if (Subtarget.isThumb() && Size == 4) {
1170 EmitConstant(Binary >> 16, 2, OS);
1171 EmitConstant(Binary & 0xffff, 2, OS);
1172 } else
1173 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001174 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001175}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001176
Jim Grosbach806e80e2010-11-03 23:52:49 +00001177#include "ARMGenMCCodeEmitter.inc"