blob: c81833b28f8881b550cc2d9232c89730bcfeb359 [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[] = {
Jim Grosbachdff84b02010-12-02 00:28:45 +000048 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond7b3f582010-12-09 01:51:07 +000050 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000051 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000052 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000053 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
54 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonfb20d892010-12-09 00:27:41 +000055 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000056 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000057 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachb492a7c2010-12-09 19:50:12 +000058 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000059 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000060 { "fixup_arm_movt_hi16", 0, 16, 0 },
61 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000062 };
63
64 if (Kind < FirstTargetFixupKind)
65 return MCCodeEmitter::getFixupKindInfo(Kind);
66
67 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
68 "Invalid kind!");
69 return Infos[Kind - FirstTargetFixupKind];
70 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000071 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
72
Jim Grosbach9af82ba2010-10-07 21:57:55 +000073 // getBinaryCodeForInstr - TableGen'erated function for getting the
74 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000075 unsigned getBinaryCodeForInstr(const MCInst &MI,
76 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000077
78 /// getMachineOpValue - Return binary encoding of operand. If the machine
79 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000080 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
81 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000082
Jason W Kim837caa92010-11-18 23:37:15 +000083 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
84 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
85 SmallVectorImpl<MCFixup> &Fixups) const;
86
Bill Wendling92b5a2e2010-11-03 01:49:29 +000087 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 unsigned &Reg, unsigned &Imm,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000090
Jim Grosbach662a8162010-12-06 23:57:07 +000091 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000092 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000093 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
Bill Wendling09aa3f02010-12-09 00:39:08 +000096 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
97 /// BLX branch target.
98 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
99 SmallVectorImpl<MCFixup> &Fixups) const;
100
Jim Grosbach027d6e82010-12-09 19:04:53 +0000101 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
102 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000103 SmallVectorImpl<MCFixup> &Fixups) const;
104
Jim Grosbachc466b932010-11-11 18:04:49 +0000105 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
106 /// branch target.
107 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
108 SmallVectorImpl<MCFixup> &Fixups) const;
109
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000110 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
111 /// ADR label target.
112 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
114
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000115 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
116 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000117 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000119
Owen Anderson9d63d902010-12-01 19:18:46 +0000120 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
121 /// operand.
122 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
123 SmallVectorImpl<MCFixup> &Fixups) const;
124
125
Jim Grosbach54fea632010-11-09 17:20:53 +0000126 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
127 /// operand as needed by load/store instructions.
128 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000131 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
132 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups) const {
134 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
135 switch (Mode) {
136 default: assert(0 && "Unknown addressing sub-mode!");
137 case ARM_AM::da: return 0;
138 case ARM_AM::ia: return 1;
139 case ARM_AM::db: return 2;
140 case ARM_AM::ib: return 3;
141 }
142 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000143 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
144 ///
145 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
146 switch (ShOpc) {
147 default: llvm_unreachable("Unknown shift opc!");
148 case ARM_AM::no_shift:
149 case ARM_AM::lsl: return 0;
150 case ARM_AM::lsr: return 1;
151 case ARM_AM::asr: return 2;
152 case ARM_AM::ror:
153 case ARM_AM::rrx: return 3;
154 }
155 return 0;
156 }
157
158 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
159 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
161
162 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
163 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000166 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
167 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const;
169
Jim Grosbach570a9222010-11-11 01:09:40 +0000170 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
171 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
172 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000173
Jim Grosbachd967cd02010-12-07 21:50:47 +0000174 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
175 /// operand.
176 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
177 SmallVectorImpl<MCFixup> &Fixups) const;
178
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000179 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
180 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182
Bill Wendling1fd374e2010-11-30 22:57:21 +0000183 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
184 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups) const;
186
187 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
188 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
189 SmallVectorImpl<MCFixup> &Fixups) const;
190
Bill Wendlingb8958b02010-12-08 01:57:09 +0000191 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
192 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
194
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000195 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000196 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000198
Jim Grosbach08bd5492010-10-12 23:00:24 +0000199 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000200 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
201 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000202 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
203 // '1' respectively.
204 return MI.getOperand(Op).getReg() == ARM::CPSR;
205 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000206
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000207 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000208 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000210 unsigned SoImm = MI.getOperand(Op).getImm();
211 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
212 assert(SoImmVal != -1 && "Not a valid so_imm value!");
213
214 // Encode rotate_imm.
215 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
216 << ARMII::SoRotImmShift;
217
218 // Encode immed_8.
219 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
220 return Binary;
221 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000222
223 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
224 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
225 SmallVectorImpl<MCFixup> &Fixups) const {
226 unsigned SoImm = MI.getOperand(Op).getImm();
227 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
228 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
229 return Encoded;
230 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000231
Owen Anderson75579f72010-11-29 22:44:32 +0000232 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
233 SmallVectorImpl<MCFixup> &Fixups) const;
234 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
235 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000236 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
237 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000238 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
239 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000240
Jim Grosbachef324d72010-10-12 23:53:58 +0000241 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000242 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
243 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000244 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
245 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000246
Jim Grosbach806e80e2010-11-03 23:52:49 +0000247 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
248 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000249 switch (MI.getOperand(Op).getImm()) {
250 default: assert (0 && "Not a valid rot_imm value!");
251 case 0: return 0;
252 case 8: return 1;
253 case 16: return 2;
254 case 24: return 3;
255 }
256 }
257
Jim Grosbach806e80e2010-11-03 23:52:49 +0000258 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
259 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000260 return MI.getOperand(Op).getImm() - 1;
261 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000262
Jim Grosbach806e80e2010-11-03 23:52:49 +0000263 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000265 return 64 - MI.getOperand(Op).getImm();
266 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000267
Jim Grosbach806e80e2010-11-03 23:52:49 +0000268 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000270
Jim Grosbach806e80e2010-11-03 23:52:49 +0000271 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const;
273 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000275 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000277 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000279
Owen Andersonc7139a62010-11-11 19:07:48 +0000280 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
281 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000282 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000283 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000284 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000285 unsigned EncodedValue) const;
286
287 unsigned VFPThumb2PostEncoder(const MCInst &MI,
288 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000289
Jim Grosbach70933262010-11-04 01:12:30 +0000290 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000291 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000292 }
293
Jim Grosbach70933262010-11-04 01:12:30 +0000294 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000295 // Output the constant in little endian byte order.
296 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000297 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000298 Val >>= 8;
299 }
300 }
301
Jim Grosbach568eeed2010-09-17 18:46:17 +0000302 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
303 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000304};
305
306} // end anonymous namespace
307
Bill Wendling0800ce72010-11-02 22:53:11 +0000308MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
309 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000310 return new ARMMCCodeEmitter(TM, Ctx);
311}
312
Owen Anderson57dac882010-11-11 21:36:43 +0000313/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000314/// instructions, and rewrite them to their Thumb2 form if we are currently in
315/// Thumb2 mode.
316unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
317 unsigned EncodedValue) const {
318 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
319 if (Subtarget.isThumb2()) {
320 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
321 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
322 // set to 1111.
323 unsigned Bit24 = EncodedValue & 0x01000000;
324 unsigned Bit28 = Bit24 << 4;
325 EncodedValue &= 0xEFFFFFFF;
326 EncodedValue |= Bit28;
327 EncodedValue |= 0x0F000000;
328 }
329
330 return EncodedValue;
331}
332
Owen Anderson57dac882010-11-11 21:36:43 +0000333/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
334/// instructions, and rewrite them to their Thumb2 form if we are currently in
335/// Thumb2 mode.
336unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
337 unsigned EncodedValue) const {
338 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
339 if (Subtarget.isThumb2()) {
340 EncodedValue &= 0xF0FFFFFF;
341 EncodedValue |= 0x09000000;
342 }
343
344 return EncodedValue;
345}
346
Owen Anderson8f143912010-11-11 23:12:55 +0000347/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
348/// instructions, and rewrite them to their Thumb2 form if we are currently in
349/// Thumb2 mode.
350unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
351 unsigned EncodedValue) const {
352 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
353 if (Subtarget.isThumb2()) {
354 EncodedValue &= 0x00FFFFFF;
355 EncodedValue |= 0xEE000000;
356 }
357
358 return EncodedValue;
359}
360
Bill Wendlingcf590262010-12-01 21:54:50 +0000361/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
362/// them to their Thumb2 form if we are currently in Thumb2 mode.
363unsigned ARMMCCodeEmitter::
364VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
365 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
366 EncodedValue &= 0x0FFFFFFF;
367 EncodedValue |= 0xE0000000;
368 }
369 return EncodedValue;
370}
Owen Anderson57dac882010-11-11 21:36:43 +0000371
Jim Grosbach56ac9072010-10-08 21:45:55 +0000372/// getMachineOpValue - Return binary encoding of operand. If the machine
373/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000374unsigned ARMMCCodeEmitter::
375getMachineOpValue(const MCInst &MI, const MCOperand &MO,
376 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000377 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000378 unsigned Reg = MO.getReg();
379 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000380
Jim Grosbachb0708d22010-11-30 23:51:41 +0000381 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000382 switch (Reg) {
383 default:
384 return RegNo;
385 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
386 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
387 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
388 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
389 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000390 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000391 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000392 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000393 } else if (MO.isFPImm()) {
394 return static_cast<unsigned>(APFloat(MO.getFPImm())
395 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000396 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000397
Jim Grosbach817c1a62010-11-19 00:27:09 +0000398 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000399 return 0;
400}
401
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000402/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000403bool ARMMCCodeEmitter::
404EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
405 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000406 const MCOperand &MO = MI.getOperand(OpIdx);
407 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000408
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000409 Reg = getARMRegisterNumbering(MO.getReg());
410
411 int32_t SImm = MO1.getImm();
412 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000413
Jim Grosbachab682a22010-10-28 18:34:10 +0000414 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000415 if (SImm == INT32_MIN)
416 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000417
Jim Grosbachab682a22010-10-28 18:34:10 +0000418 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000419 if (SImm < 0) {
420 SImm = -SImm;
421 isAdd = false;
422 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000423
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000424 Imm = SImm;
425 return isAdd;
426}
427
Bill Wendlingdff2f712010-12-08 23:01:43 +0000428/// getBranchTargetOpValue - Helper function to get the branch target operand,
429/// which is either an immediate or requires a fixup.
430static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
431 unsigned FixupKind,
432 SmallVectorImpl<MCFixup> &Fixups) {
433 const MCOperand &MO = MI.getOperand(OpIdx);
434
435 // If the destination is an immediate, we have nothing to do.
436 if (MO.isImm()) return MO.getImm();
437 assert(MO.isExpr() && "Unexpected branch target type!");
438 const MCExpr *Expr = MO.getExpr();
439 MCFixupKind Kind = MCFixupKind(FixupKind);
440 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
441
442 // All of the information is in the fixup.
443 return 0;
444}
445
446/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000447uint32_t ARMMCCodeEmitter::
448getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
449 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000450 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000451}
452
Bill Wendling09aa3f02010-12-09 00:39:08 +0000453/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
454/// BLX branch target.
455uint32_t ARMMCCodeEmitter::
456getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
457 SmallVectorImpl<MCFixup> &Fixups) const {
458 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
459}
460
Jim Grosbach027d6e82010-12-09 19:04:53 +0000461/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000462uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000463getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000464 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000465 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000466}
467
468/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
469/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000470uint32_t ARMMCCodeEmitter::
471getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000472 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000473 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
474 if (Subtarget.isThumb2())
475 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000476 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000477}
478
Bill Wendlingdff2f712010-12-08 23:01:43 +0000479/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
480/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000481uint32_t ARMMCCodeEmitter::
482getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
483 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000484 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
485 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
486 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000487}
488
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000489/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000490uint32_t ARMMCCodeEmitter::
491getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
492 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000493 // {17-13} = reg
494 // {12} = (U)nsigned (add == '1', sub == '0')
495 // {11-0} = imm12
496 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000497 bool isAdd = true;
498 // If The first operand isn't a register, we have a label reference.
499 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000500 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
501 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000502 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000503 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000504 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000505
Owen Andersoneb6779c2010-12-07 00:45:21 +0000506 const MCExpr *Expr = 0;
507 if (!MO.isReg())
508 Expr = MO.getExpr();
509 else
510 Expr = MO2.getExpr();
511
Owen Andersond7b3f582010-12-09 01:51:07 +0000512 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
513 MCFixupKind Kind;
514 if (Subtarget.isThumb2())
515 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
516 else
517 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000518 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
519
520 ++MCNumCPRelocations;
521 } else
522 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000523
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000524 uint32_t Binary = Imm12 & 0xfff;
525 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000526 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000527 Binary |= (1 << 12);
528 Binary |= (Reg << 13);
529 return Binary;
530}
531
Owen Anderson9d63d902010-12-01 19:18:46 +0000532/// getT2AddrModeImm8s4OpValue - Return encoding info for
533/// 'reg +/- imm8<<2' operand.
534uint32_t ARMMCCodeEmitter::
535getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
536 SmallVectorImpl<MCFixup> &Fixups) const {
537 // {17-13} = reg
538 // {12} = (U)nsigned (add == '1', sub == '0')
539 // {11-0} = imm8
540 unsigned Reg, Imm8;
541 bool isAdd = true;
542 // If The first operand isn't a register, we have a label reference.
543 const MCOperand &MO = MI.getOperand(OpIdx);
544 if (!MO.isReg()) {
545 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
546 Imm8 = 0;
547 isAdd = false ; // 'U' bit is set as part of the fixup.
548
549 assert(MO.isExpr() && "Unexpected machine operand type!");
550 const MCExpr *Expr = MO.getExpr();
551 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
552 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
553
554 ++MCNumCPRelocations;
555 } else
556 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
557
558 uint32_t Binary = (Imm8 >> 2) & 0xff;
559 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
560 if (isAdd)
561 Binary |= (1 << 9);
562 Binary |= (Reg << 9);
563 return Binary;
564}
565
Jim Grosbach54fea632010-11-09 17:20:53 +0000566uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000567getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
568 SmallVectorImpl<MCFixup> &Fixups) const {
569 // {20-16} = imm{15-12}
570 // {11-0} = imm{11-0}
571 const MCOperand &MO = MI.getOperand(OpIdx);
572 if (MO.isImm()) {
573 return static_cast<unsigned>(MO.getImm());
574 } else if (const MCSymbolRefExpr *Expr =
575 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
576 MCFixupKind Kind;
577 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000578 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000579 case MCSymbolRefExpr::VK_ARM_HI16:
580 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
581 break;
582 case MCSymbolRefExpr::VK_ARM_LO16:
583 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
584 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000585 }
586 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
587 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000588 };
589 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000590 return 0;
591}
592
593uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000594getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
596 const MCOperand &MO = MI.getOperand(OpIdx);
597 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
598 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
599 unsigned Rn = getARMRegisterNumbering(MO.getReg());
600 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000601 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
602 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000603 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
604 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000605
606 // {16-13} = Rn
607 // {12} = isAdd
608 // {11-0} = shifter
609 // {3-0} = Rm
610 // {4} = 0
611 // {6-5} = type
612 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000613 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000614 Binary |= Rn << 13;
615 Binary |= SBits << 5;
616 Binary |= ShImm << 7;
617 if (isAdd)
618 Binary |= 1 << 12;
619 return Binary;
620}
621
Jim Grosbach570a9222010-11-11 01:09:40 +0000622uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000623getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
624 SmallVectorImpl<MCFixup> &Fixups) const {
625 // {17-14} Rn
626 // {13} 1 == imm12, 0 == Rm
627 // {12} isAdd
628 // {11-0} imm12/Rm
629 const MCOperand &MO = MI.getOperand(OpIdx);
630 unsigned Rn = getARMRegisterNumbering(MO.getReg());
631 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
632 Binary |= Rn << 14;
633 return Binary;
634}
635
636uint32_t ARMMCCodeEmitter::
637getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
638 SmallVectorImpl<MCFixup> &Fixups) const {
639 // {13} 1 == imm12, 0 == Rm
640 // {12} isAdd
641 // {11-0} imm12/Rm
642 const MCOperand &MO = MI.getOperand(OpIdx);
643 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
644 unsigned Imm = MO1.getImm();
645 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
646 bool isReg = MO.getReg() != 0;
647 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
648 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
649 if (isReg) {
650 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
651 Binary <<= 7; // Shift amount is bits [11:7]
652 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
653 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
654 }
655 return Binary | (isAdd << 12) | (isReg << 13);
656}
657
658uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000659getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
660 SmallVectorImpl<MCFixup> &Fixups) const {
661 // {9} 1 == imm8, 0 == Rm
662 // {8} isAdd
663 // {7-4} imm7_4/zero
664 // {3-0} imm3_0/Rm
665 const MCOperand &MO = MI.getOperand(OpIdx);
666 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
667 unsigned Imm = MO1.getImm();
668 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
669 bool isImm = MO.getReg() == 0;
670 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
671 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
672 if (!isImm)
673 Imm8 = getARMRegisterNumbering(MO.getReg());
674 return Imm8 | (isAdd << 8) | (isImm << 9);
675}
676
677uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000678getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
679 SmallVectorImpl<MCFixup> &Fixups) const {
680 // {13} 1 == imm8, 0 == Rm
681 // {12-9} Rn
682 // {8} isAdd
683 // {7-4} imm7_4/zero
684 // {3-0} imm3_0/Rm
685 const MCOperand &MO = MI.getOperand(OpIdx);
686 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
687 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
688 unsigned Rn = getARMRegisterNumbering(MO.getReg());
689 unsigned Imm = MO2.getImm();
690 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
691 bool isImm = MO1.getReg() == 0;
692 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
693 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
694 if (!isImm)
695 Imm8 = getARMRegisterNumbering(MO1.getReg());
696 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
697}
698
Bill Wendlingb8958b02010-12-08 01:57:09 +0000699/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000700uint32_t ARMMCCodeEmitter::
701getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
702 SmallVectorImpl<MCFixup> &Fixups) const {
703 // [SP, #imm]
704 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000705 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000706#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
707 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
708 "Unexpected base register!");
709#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000710 // The immediate is already shifted for the implicit zeroes, so no change
711 // here.
712 return MO1.getImm() & 0xff;
713}
714
Bill Wendling1fd374e2010-11-30 22:57:21 +0000715/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
716static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
717 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000718 // [Rn, Rm]
719 // {5-3} = Rm
720 // {2-0} = Rn
721 //
722 // [Rn, #imm]
723 // {7-3} = imm5
724 // {2-0} = Rn
725 const MCOperand &MO = MI.getOperand(OpIdx);
726 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
727 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
728 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000729 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000730
731 if (MO2.getReg() != 0)
732 // Is an immediate.
733 Imm5 = getARMRegisterNumbering(MO2.getReg());
734
735 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000736}
737
Bill Wendling1fd374e2010-11-30 22:57:21 +0000738/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
739uint32_t ARMMCCodeEmitter::
740getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
741 SmallVectorImpl<MCFixup> &) const {
742 return getAddrModeSOpValue(MI, OpIdx, 4);
743}
744
745/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
746uint32_t ARMMCCodeEmitter::
747getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &) const {
749 return getAddrModeSOpValue(MI, OpIdx, 2);
750}
751
752/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
753uint32_t ARMMCCodeEmitter::
754getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
755 SmallVectorImpl<MCFixup> &) const {
756 return getAddrModeSOpValue(MI, OpIdx, 1);
757}
758
Bill Wendlingb8958b02010-12-08 01:57:09 +0000759/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
760uint32_t ARMMCCodeEmitter::
761getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
762 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000763 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000764}
765
Jim Grosbach5177f792010-12-01 21:09:40 +0000766/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000767uint32_t ARMMCCodeEmitter::
768getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
769 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000770 // {12-9} = reg
771 // {8} = (U)nsigned (add == '1', sub == '0')
772 // {7-0} = imm8
773 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000774 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000775 // If The first operand isn't a register, we have a label reference.
776 const MCOperand &MO = MI.getOperand(OpIdx);
777 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000778 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000779 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000780 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000781
782 assert(MO.isExpr() && "Unexpected machine operand type!");
783 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000784 MCFixupKind Kind;
785 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
786 if (Subtarget.isThumb2())
787 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
788 else
789 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000790 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
791
792 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000793 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000794 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000795 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
796 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000797
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000798 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
799 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000800 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000801 Binary |= (1 << 8);
802 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000803 return Binary;
804}
805
Jim Grosbach806e80e2010-11-03 23:52:49 +0000806unsigned ARMMCCodeEmitter::
807getSORegOpValue(const MCInst &MI, unsigned OpIdx,
808 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000809 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
810 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
811 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000812 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000813 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000814 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000815 // {6-5} = type
816 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000817 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000818 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000819 // else (imm shift)
820 // {11-7} = imm
821
822 const MCOperand &MO = MI.getOperand(OpIdx);
823 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
824 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
825 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
826
827 // Encode Rm.
828 unsigned Binary = getARMRegisterNumbering(MO.getReg());
829
830 // Encode the shift opcode.
831 unsigned SBits = 0;
832 unsigned Rs = MO1.getReg();
833 if (Rs) {
834 // Set shift operand (bit[7:4]).
835 // LSL - 0001
836 // LSR - 0011
837 // ASR - 0101
838 // ROR - 0111
839 // RRX - 0110 and bit[11:8] clear.
840 switch (SOpc) {
841 default: llvm_unreachable("Unknown shift opc!");
842 case ARM_AM::lsl: SBits = 0x1; break;
843 case ARM_AM::lsr: SBits = 0x3; break;
844 case ARM_AM::asr: SBits = 0x5; break;
845 case ARM_AM::ror: SBits = 0x7; break;
846 case ARM_AM::rrx: SBits = 0x6; break;
847 }
848 } else {
849 // Set shift operand (bit[6:4]).
850 // LSL - 000
851 // LSR - 010
852 // ASR - 100
853 // ROR - 110
854 switch (SOpc) {
855 default: llvm_unreachable("Unknown shift opc!");
856 case ARM_AM::lsl: SBits = 0x0; break;
857 case ARM_AM::lsr: SBits = 0x2; break;
858 case ARM_AM::asr: SBits = 0x4; break;
859 case ARM_AM::ror: SBits = 0x6; break;
860 }
861 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000862
Jim Grosbachef324d72010-10-12 23:53:58 +0000863 Binary |= SBits << 4;
864 if (SOpc == ARM_AM::rrx)
865 return Binary;
866
867 // Encode the shift operation Rs or shift_imm (except rrx).
868 if (Rs) {
869 // Encode Rs bit[11:8].
870 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
871 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
872 }
873
874 // Encode shift_imm bit[11:7].
875 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
876}
877
Jim Grosbach806e80e2010-11-03 23:52:49 +0000878unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000879getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
880 SmallVectorImpl<MCFixup> &Fixups) const {
881 const MCOperand &MO1 = MI.getOperand(OpNum);
882 const MCOperand &MO2 = MI.getOperand(OpNum+1);
883 const MCOperand &MO3 = MI.getOperand(OpNum+2);
884
885 // Encoded as [Rn, Rm, imm].
886 // FIXME: Needs fixup support.
887 unsigned Value = getARMRegisterNumbering(MO1.getReg());
888 Value <<= 4;
889 Value |= getARMRegisterNumbering(MO2.getReg());
890 Value <<= 2;
891 Value |= MO3.getImm();
892
893 return Value;
894}
895
896unsigned ARMMCCodeEmitter::
897getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
898 SmallVectorImpl<MCFixup> &Fixups) const {
899 const MCOperand &MO1 = MI.getOperand(OpNum);
900 const MCOperand &MO2 = MI.getOperand(OpNum+1);
901
902 // FIXME: Needs fixup support.
903 unsigned Value = getARMRegisterNumbering(MO1.getReg());
904
905 // Even though the immediate is 8 bits long, we need 9 bits in order
906 // to represent the (inverse of the) sign bit.
907 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000908 int32_t tmp = (int32_t)MO2.getImm();
909 if (tmp < 0)
910 tmp = abs(tmp);
911 else
912 Value |= 256; // Set the ADD bit
913 Value |= tmp & 255;
914 return Value;
915}
916
917unsigned ARMMCCodeEmitter::
918getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
919 SmallVectorImpl<MCFixup> &Fixups) const {
920 const MCOperand &MO1 = MI.getOperand(OpNum);
921
922 // FIXME: Needs fixup support.
923 unsigned Value = 0;
924 int32_t tmp = (int32_t)MO1.getImm();
925 if (tmp < 0)
926 tmp = abs(tmp);
927 else
928 Value |= 256; // Set the ADD bit
929 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000930 return Value;
931}
932
933unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000934getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
935 SmallVectorImpl<MCFixup> &Fixups) const {
936 const MCOperand &MO1 = MI.getOperand(OpNum);
937
938 // FIXME: Needs fixup support.
939 unsigned Value = 0;
940 int32_t tmp = (int32_t)MO1.getImm();
941 if (tmp < 0)
942 tmp = abs(tmp);
943 else
944 Value |= 4096; // Set the ADD bit
945 Value |= tmp & 4095;
946 return Value;
947}
948
949unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000950getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
951 SmallVectorImpl<MCFixup> &Fixups) const {
952 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
953 // shifted. The second is the amount to shift by.
954 //
955 // {3-0} = Rm.
956 // {4} = 0
957 // {6-5} = type
958 // {11-7} = imm
959
960 const MCOperand &MO = MI.getOperand(OpIdx);
961 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
962 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
963
964 // Encode Rm.
965 unsigned Binary = getARMRegisterNumbering(MO.getReg());
966
967 // Encode the shift opcode.
968 unsigned SBits = 0;
969 // Set shift operand (bit[6:4]).
970 // LSL - 000
971 // LSR - 010
972 // ASR - 100
973 // ROR - 110
974 switch (SOpc) {
975 default: llvm_unreachable("Unknown shift opc!");
976 case ARM_AM::lsl: SBits = 0x0; break;
977 case ARM_AM::lsr: SBits = 0x2; break;
978 case ARM_AM::asr: SBits = 0x4; break;
979 case ARM_AM::ror: SBits = 0x6; break;
980 }
981
982 Binary |= SBits << 4;
983 if (SOpc == ARM_AM::rrx)
984 return Binary;
985
986 // Encode shift_imm bit[11:7].
987 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
988}
989
990unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000991getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
992 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000993 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
994 // msb of the mask.
995 const MCOperand &MO = MI.getOperand(Op);
996 uint32_t v = ~MO.getImm();
997 uint32_t lsb = CountTrailingZeros_32(v);
998 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
999 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1000 return lsb | (msb << 5);
1001}
1002
Jim Grosbach806e80e2010-11-03 23:52:49 +00001003unsigned ARMMCCodeEmitter::
1004getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001005 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001006 // VLDM/VSTM:
1007 // {12-8} = Vd
1008 // {7-0} = Number of registers
1009 //
1010 // LDM/STM:
1011 // {15-0} = Bitfield of GPRs.
1012 unsigned Reg = MI.getOperand(Op).getReg();
1013 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1014 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1015
Bill Wendling5e559a22010-11-09 00:30:18 +00001016 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001017
1018 if (SPRRegs || DPRRegs) {
1019 // VLDM/VSTM
1020 unsigned RegNo = getARMRegisterNumbering(Reg);
1021 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1022 Binary |= (RegNo & 0x1f) << 8;
1023 if (SPRRegs)
1024 Binary |= NumRegs;
1025 else
1026 Binary |= NumRegs * 2;
1027 } else {
1028 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1029 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1030 Binary |= 1 << RegNo;
1031 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001032 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001033
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001034 return Binary;
1035}
1036
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001037/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1038/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001039unsigned ARMMCCodeEmitter::
1040getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1041 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001042 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001043 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001044
Owen Andersond9aa7d32010-11-02 00:05:05 +00001045 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001046 unsigned Align = 0;
1047
1048 switch (Imm.getImm()) {
1049 default: break;
1050 case 2:
1051 case 4:
1052 case 8: Align = 0x01; break;
1053 case 16: Align = 0x02; break;
1054 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001055 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001056
Owen Andersond9aa7d32010-11-02 00:05:05 +00001057 return RegNo | (Align << 4);
1058}
1059
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001060/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1061/// alignment operand for use in VLD-dup instructions. This is the same as
1062/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1063/// different for VLD4-dup.
1064unsigned ARMMCCodeEmitter::
1065getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1066 SmallVectorImpl<MCFixup> &Fixups) const {
1067 const MCOperand &Reg = MI.getOperand(Op);
1068 const MCOperand &Imm = MI.getOperand(Op + 1);
1069
1070 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1071 unsigned Align = 0;
1072
1073 switch (Imm.getImm()) {
1074 default: break;
1075 case 2:
1076 case 4:
1077 case 8: Align = 0x01; break;
1078 case 16: Align = 0x03; break;
1079 }
1080
1081 return RegNo | (Align << 4);
1082}
1083
Jim Grosbach806e80e2010-11-03 23:52:49 +00001084unsigned ARMMCCodeEmitter::
1085getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1086 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001087 const MCOperand &MO = MI.getOperand(Op);
1088 if (MO.getReg() == 0) return 0x0D;
1089 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001090}
1091
Jim Grosbach568eeed2010-09-17 18:46:17 +00001092void ARMMCCodeEmitter::
1093EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001094 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001095 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001096 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001097 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001098 uint64_t TSFlags = Desc.TSFlags;
1099 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001100 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001101 int Size;
1102 // Basic size info comes from the TSFlags field.
1103 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1104 default: llvm_unreachable("Unexpected instruction size!");
1105 case ARMII::Size2Bytes: Size = 2; break;
1106 case ARMII::Size4Bytes: Size = 4; break;
1107 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001108 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1109 // Thumb 32-bit wide instructions need to be have the high order halfword
1110 // emitted first.
1111 if (Subtarget.isThumb() && Size == 4) {
1112 EmitConstant(Binary >> 16, 2, OS);
1113 EmitConstant(Binary & 0xffff, 2, OS);
1114 } else
1115 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001116 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001117}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001118
Jim Grosbach806e80e2010-11-03 23:52:49 +00001119#include "ARMGenMCCodeEmitter.inc"