blob: c901831d85814f5b761aa078af3fa2e52454ccb7 [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 Anderson05018c22010-12-09 20:27:52 +000050 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
51 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000053 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000054 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonfe7fac72010-12-09 21:34:47 +000056 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbach662a8162010-12-06 23:57:07 +000058 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000059 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachb492a7c2010-12-09 19:50:12 +000060 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000061 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000062 { "fixup_arm_movt_hi16", 0, 16, 0 },
63 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000064 };
65
66 if (Kind < FirstTargetFixupKind)
67 return MCCodeEmitter::getFixupKindInfo(Kind);
68
69 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
70 "Invalid kind!");
71 return Infos[Kind - FirstTargetFixupKind];
72 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000073 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
74
Jim Grosbach9af82ba2010-10-07 21:57:55 +000075 // getBinaryCodeForInstr - TableGen'erated function for getting the
76 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000077 unsigned getBinaryCodeForInstr(const MCInst &MI,
78 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000079
80 /// getMachineOpValue - Return binary encoding of operand. If the machine
81 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000082 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
83 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000084
Jason W Kim837caa92010-11-18 23:37:15 +000085 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
86 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
87 SmallVectorImpl<MCFixup> &Fixups) const;
88
Bill Wendling92b5a2e2010-11-03 01:49:29 +000089 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000090 unsigned &Reg, unsigned &Imm,
91 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000092
Jim Grosbach662a8162010-12-06 23:57:07 +000093 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000094 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000095 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
96 SmallVectorImpl<MCFixup> &Fixups) const;
97
Bill Wendling09aa3f02010-12-09 00:39:08 +000098 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
99 /// BLX branch target.
100 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
101 SmallVectorImpl<MCFixup> &Fixups) const;
102
Jim Grosbach027d6e82010-12-09 19:04:53 +0000103 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000105 SmallVectorImpl<MCFixup> &Fixups) const;
106
Jim Grosbachc466b932010-11-11 18:04:49 +0000107 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
108 /// branch target.
109 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
111
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000112 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
113 /// ADR label target.
114 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000117 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
118 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000119 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000121
Owen Anderson9d63d902010-12-01 19:18:46 +0000122 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
123 /// operand.
124 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
127
Jim Grosbach54fea632010-11-09 17:20:53 +0000128 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
129 /// operand as needed by load/store instructions.
130 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
131 SmallVectorImpl<MCFixup> &Fixups) const;
132
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000133 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
134 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
135 SmallVectorImpl<MCFixup> &Fixups) const {
136 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
137 switch (Mode) {
138 default: assert(0 && "Unknown addressing sub-mode!");
139 case ARM_AM::da: return 0;
140 case ARM_AM::ia: return 1;
141 case ARM_AM::db: return 2;
142 case ARM_AM::ib: return 3;
143 }
144 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000145 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
146 ///
147 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
148 switch (ShOpc) {
149 default: llvm_unreachable("Unknown shift opc!");
150 case ARM_AM::no_shift:
151 case ARM_AM::lsl: return 0;
152 case ARM_AM::lsr: return 1;
153 case ARM_AM::asr: return 2;
154 case ARM_AM::ror:
155 case ARM_AM::rrx: return 3;
156 }
157 return 0;
158 }
159
160 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
161 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
162 SmallVectorImpl<MCFixup> &Fixups) const;
163
164 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
165 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups) const;
167
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000168 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
169 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
170 SmallVectorImpl<MCFixup> &Fixups) const;
171
Jim Grosbach570a9222010-11-11 01:09:40 +0000172 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
173 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
174 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000175
Jim Grosbachd967cd02010-12-07 21:50:47 +0000176 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
177 /// operand.
178 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
179 SmallVectorImpl<MCFixup> &Fixups) const;
180
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000181 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
182 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
183 SmallVectorImpl<MCFixup> &Fixups) const;
184
Bill Wendling1fd374e2010-11-30 22:57:21 +0000185 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
186 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
187 SmallVectorImpl<MCFixup> &Fixups) const;
188
189 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
190 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
191 SmallVectorImpl<MCFixup> &Fixups) const;
192
Bill Wendlingb8958b02010-12-08 01:57:09 +0000193 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
194 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
195 SmallVectorImpl<MCFixup> &Fixups) const;
196
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000197 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000198 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
199 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000200
Jim Grosbach08bd5492010-10-12 23:00:24 +0000201 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000202 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
203 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000204 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
205 // '1' respectively.
206 return MI.getOperand(Op).getReg() == ARM::CPSR;
207 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000208
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000209 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000210 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
211 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000212 unsigned SoImm = MI.getOperand(Op).getImm();
213 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
214 assert(SoImmVal != -1 && "Not a valid so_imm value!");
215
216 // Encode rotate_imm.
217 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
218 << ARMII::SoRotImmShift;
219
220 // Encode immed_8.
221 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
222 return Binary;
223 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000224
225 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
226 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
227 SmallVectorImpl<MCFixup> &Fixups) const {
228 unsigned SoImm = MI.getOperand(Op).getImm();
229 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
230 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
231 return Encoded;
232 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000233
Owen Anderson75579f72010-11-29 22:44:32 +0000234 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
235 SmallVectorImpl<MCFixup> &Fixups) const;
236 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
237 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000238 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
239 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000240 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
241 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000242
Jim Grosbachef324d72010-10-12 23:53:58 +0000243 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000244 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
245 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000246 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
247 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000248
Jim Grosbach806e80e2010-11-03 23:52:49 +0000249 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
250 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000251 switch (MI.getOperand(Op).getImm()) {
252 default: assert (0 && "Not a valid rot_imm value!");
253 case 0: return 0;
254 case 8: return 1;
255 case 16: return 2;
256 case 24: return 3;
257 }
258 }
259
Jim Grosbach806e80e2010-11-03 23:52:49 +0000260 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
261 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000262 return MI.getOperand(Op).getImm() - 1;
263 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000264
Jim Grosbach806e80e2010-11-03 23:52:49 +0000265 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000267 return 64 - MI.getOperand(Op).getImm();
268 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000269
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000272
Jim Grosbach806e80e2010-11-03 23:52:49 +0000273 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
274 SmallVectorImpl<MCFixup> &Fixups) const;
275 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000277 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
278 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000279 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000281
Owen Andersonc7139a62010-11-11 19:07:48 +0000282 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
283 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000284 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000285 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000286 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000287 unsigned EncodedValue) const;
288
289 unsigned VFPThumb2PostEncoder(const MCInst &MI,
290 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000291
Jim Grosbach70933262010-11-04 01:12:30 +0000292 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000293 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000294 }
295
Jim Grosbach70933262010-11-04 01:12:30 +0000296 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000297 // Output the constant in little endian byte order.
298 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000299 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000300 Val >>= 8;
301 }
302 }
303
Jim Grosbach568eeed2010-09-17 18:46:17 +0000304 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
305 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000306};
307
308} // end anonymous namespace
309
Bill Wendling0800ce72010-11-02 22:53:11 +0000310MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
311 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000312 return new ARMMCCodeEmitter(TM, Ctx);
313}
314
Owen Anderson57dac882010-11-11 21:36:43 +0000315/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000316/// instructions, and rewrite them to their Thumb2 form if we are currently in
317/// Thumb2 mode.
318unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
319 unsigned EncodedValue) const {
320 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
321 if (Subtarget.isThumb2()) {
322 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
323 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
324 // set to 1111.
325 unsigned Bit24 = EncodedValue & 0x01000000;
326 unsigned Bit28 = Bit24 << 4;
327 EncodedValue &= 0xEFFFFFFF;
328 EncodedValue |= Bit28;
329 EncodedValue |= 0x0F000000;
330 }
331
332 return EncodedValue;
333}
334
Owen Anderson57dac882010-11-11 21:36:43 +0000335/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
336/// instructions, and rewrite them to their Thumb2 form if we are currently in
337/// Thumb2 mode.
338unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
339 unsigned EncodedValue) const {
340 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
341 if (Subtarget.isThumb2()) {
342 EncodedValue &= 0xF0FFFFFF;
343 EncodedValue |= 0x09000000;
344 }
345
346 return EncodedValue;
347}
348
Owen Anderson8f143912010-11-11 23:12:55 +0000349/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
350/// instructions, and rewrite them to their Thumb2 form if we are currently in
351/// Thumb2 mode.
352unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
353 unsigned EncodedValue) const {
354 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
355 if (Subtarget.isThumb2()) {
356 EncodedValue &= 0x00FFFFFF;
357 EncodedValue |= 0xEE000000;
358 }
359
360 return EncodedValue;
361}
362
Bill Wendlingcf590262010-12-01 21:54:50 +0000363/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
364/// them to their Thumb2 form if we are currently in Thumb2 mode.
365unsigned ARMMCCodeEmitter::
366VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
367 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
368 EncodedValue &= 0x0FFFFFFF;
369 EncodedValue |= 0xE0000000;
370 }
371 return EncodedValue;
372}
Owen Anderson57dac882010-11-11 21:36:43 +0000373
Jim Grosbach56ac9072010-10-08 21:45:55 +0000374/// getMachineOpValue - Return binary encoding of operand. If the machine
375/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000376unsigned ARMMCCodeEmitter::
377getMachineOpValue(const MCInst &MI, const MCOperand &MO,
378 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000379 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000380 unsigned Reg = MO.getReg();
381 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000382
Jim Grosbachb0708d22010-11-30 23:51:41 +0000383 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000384 switch (Reg) {
385 default:
386 return RegNo;
387 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
388 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
389 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
390 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
391 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000392 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000393 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000394 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000395 } else if (MO.isFPImm()) {
396 return static_cast<unsigned>(APFloat(MO.getFPImm())
397 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000398 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000399
Jim Grosbach817c1a62010-11-19 00:27:09 +0000400 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000401 return 0;
402}
403
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000404/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000405bool ARMMCCodeEmitter::
406EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
407 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000408 const MCOperand &MO = MI.getOperand(OpIdx);
409 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000410
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000411 Reg = getARMRegisterNumbering(MO.getReg());
412
413 int32_t SImm = MO1.getImm();
414 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000415
Jim Grosbachab682a22010-10-28 18:34:10 +0000416 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000417 if (SImm == INT32_MIN)
418 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000419
Jim Grosbachab682a22010-10-28 18:34:10 +0000420 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000421 if (SImm < 0) {
422 SImm = -SImm;
423 isAdd = false;
424 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000425
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000426 Imm = SImm;
427 return isAdd;
428}
429
Bill Wendlingdff2f712010-12-08 23:01:43 +0000430/// getBranchTargetOpValue - Helper function to get the branch target operand,
431/// which is either an immediate or requires a fixup.
432static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
433 unsigned FixupKind,
434 SmallVectorImpl<MCFixup> &Fixups) {
435 const MCOperand &MO = MI.getOperand(OpIdx);
436
437 // If the destination is an immediate, we have nothing to do.
438 if (MO.isImm()) return MO.getImm();
439 assert(MO.isExpr() && "Unexpected branch target type!");
440 const MCExpr *Expr = MO.getExpr();
441 MCFixupKind Kind = MCFixupKind(FixupKind);
442 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
443
444 // All of the information is in the fixup.
445 return 0;
446}
447
448/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000449uint32_t ARMMCCodeEmitter::
450getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
451 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000452 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000453}
454
Bill Wendling09aa3f02010-12-09 00:39:08 +0000455/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
456/// BLX branch target.
457uint32_t ARMMCCodeEmitter::
458getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
459 SmallVectorImpl<MCFixup> &Fixups) const {
460 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
461}
462
Jim Grosbach027d6e82010-12-09 19:04:53 +0000463/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000464uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000465getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000466 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000467 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000468}
469
470/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
471/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000472uint32_t ARMMCCodeEmitter::
473getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000474 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000475 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
476 if (Subtarget.isThumb2())
477 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000478 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000479}
480
Bill Wendlingdff2f712010-12-08 23:01:43 +0000481/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
482/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000483uint32_t ARMMCCodeEmitter::
484getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
485 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000486 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
487 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
488 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000489}
490
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000491/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000492uint32_t ARMMCCodeEmitter::
493getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
494 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000495 // {17-13} = reg
496 // {12} = (U)nsigned (add == '1', sub == '0')
497 // {11-0} = imm12
498 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000499 bool isAdd = true;
500 // If The first operand isn't a register, we have a label reference.
501 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000502 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
503 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000504 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000505 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000506 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000507
Owen Andersoneb6779c2010-12-07 00:45:21 +0000508 const MCExpr *Expr = 0;
509 if (!MO.isReg())
510 Expr = MO.getExpr();
511 else
512 Expr = MO2.getExpr();
513
Owen Andersond7b3f582010-12-09 01:51:07 +0000514 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
515 MCFixupKind Kind;
516 if (Subtarget.isThumb2())
517 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
518 else
519 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000520 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
521
522 ++MCNumCPRelocations;
523 } else
524 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000525
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000526 uint32_t Binary = Imm12 & 0xfff;
527 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000528 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000529 Binary |= (1 << 12);
530 Binary |= (Reg << 13);
531 return Binary;
532}
533
Owen Anderson9d63d902010-12-01 19:18:46 +0000534/// getT2AddrModeImm8s4OpValue - Return encoding info for
535/// 'reg +/- imm8<<2' operand.
536uint32_t ARMMCCodeEmitter::
537getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
538 SmallVectorImpl<MCFixup> &Fixups) const {
539 // {17-13} = reg
540 // {12} = (U)nsigned (add == '1', sub == '0')
541 // {11-0} = imm8
542 unsigned Reg, Imm8;
543 bool isAdd = true;
544 // If The first operand isn't a register, we have a label reference.
545 const MCOperand &MO = MI.getOperand(OpIdx);
546 if (!MO.isReg()) {
547 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
548 Imm8 = 0;
549 isAdd = false ; // 'U' bit is set as part of the fixup.
550
551 assert(MO.isExpr() && "Unexpected machine operand type!");
552 const MCExpr *Expr = MO.getExpr();
553 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
554 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
555
556 ++MCNumCPRelocations;
557 } else
558 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
559
560 uint32_t Binary = (Imm8 >> 2) & 0xff;
561 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
562 if (isAdd)
563 Binary |= (1 << 9);
564 Binary |= (Reg << 9);
565 return Binary;
566}
567
Jim Grosbach54fea632010-11-09 17:20:53 +0000568uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000569getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
570 SmallVectorImpl<MCFixup> &Fixups) const {
571 // {20-16} = imm{15-12}
572 // {11-0} = imm{11-0}
573 const MCOperand &MO = MI.getOperand(OpIdx);
574 if (MO.isImm()) {
575 return static_cast<unsigned>(MO.getImm());
576 } else if (const MCSymbolRefExpr *Expr =
577 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
578 MCFixupKind Kind;
579 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000580 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000581 case MCSymbolRefExpr::VK_ARM_HI16:
582 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
583 break;
584 case MCSymbolRefExpr::VK_ARM_LO16:
585 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
586 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000587 }
588 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
589 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000590 };
591 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000592 return 0;
593}
594
595uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000596getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
597 SmallVectorImpl<MCFixup> &Fixups) const {
598 const MCOperand &MO = MI.getOperand(OpIdx);
599 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
600 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
601 unsigned Rn = getARMRegisterNumbering(MO.getReg());
602 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000603 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
604 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000605 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
606 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000607
608 // {16-13} = Rn
609 // {12} = isAdd
610 // {11-0} = shifter
611 // {3-0} = Rm
612 // {4} = 0
613 // {6-5} = type
614 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000615 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000616 Binary |= Rn << 13;
617 Binary |= SBits << 5;
618 Binary |= ShImm << 7;
619 if (isAdd)
620 Binary |= 1 << 12;
621 return Binary;
622}
623
Jim Grosbach570a9222010-11-11 01:09:40 +0000624uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000625getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
626 SmallVectorImpl<MCFixup> &Fixups) const {
627 // {17-14} Rn
628 // {13} 1 == imm12, 0 == Rm
629 // {12} isAdd
630 // {11-0} imm12/Rm
631 const MCOperand &MO = MI.getOperand(OpIdx);
632 unsigned Rn = getARMRegisterNumbering(MO.getReg());
633 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
634 Binary |= Rn << 14;
635 return Binary;
636}
637
638uint32_t ARMMCCodeEmitter::
639getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
640 SmallVectorImpl<MCFixup> &Fixups) const {
641 // {13} 1 == imm12, 0 == Rm
642 // {12} isAdd
643 // {11-0} imm12/Rm
644 const MCOperand &MO = MI.getOperand(OpIdx);
645 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
646 unsigned Imm = MO1.getImm();
647 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
648 bool isReg = MO.getReg() != 0;
649 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
650 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
651 if (isReg) {
652 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
653 Binary <<= 7; // Shift amount is bits [11:7]
654 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
655 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
656 }
657 return Binary | (isAdd << 12) | (isReg << 13);
658}
659
660uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000661getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
662 SmallVectorImpl<MCFixup> &Fixups) const {
663 // {9} 1 == imm8, 0 == Rm
664 // {8} isAdd
665 // {7-4} imm7_4/zero
666 // {3-0} imm3_0/Rm
667 const MCOperand &MO = MI.getOperand(OpIdx);
668 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
669 unsigned Imm = MO1.getImm();
670 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
671 bool isImm = MO.getReg() == 0;
672 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
673 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
674 if (!isImm)
675 Imm8 = getARMRegisterNumbering(MO.getReg());
676 return Imm8 | (isAdd << 8) | (isImm << 9);
677}
678
679uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000680getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
681 SmallVectorImpl<MCFixup> &Fixups) const {
682 // {13} 1 == imm8, 0 == Rm
683 // {12-9} Rn
684 // {8} isAdd
685 // {7-4} imm7_4/zero
686 // {3-0} imm3_0/Rm
687 const MCOperand &MO = MI.getOperand(OpIdx);
688 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
689 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
690 unsigned Rn = getARMRegisterNumbering(MO.getReg());
691 unsigned Imm = MO2.getImm();
692 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
693 bool isImm = MO1.getReg() == 0;
694 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
695 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
696 if (!isImm)
697 Imm8 = getARMRegisterNumbering(MO1.getReg());
698 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
699}
700
Bill Wendlingb8958b02010-12-08 01:57:09 +0000701/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000702uint32_t ARMMCCodeEmitter::
703getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
704 SmallVectorImpl<MCFixup> &Fixups) const {
705 // [SP, #imm]
706 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000707 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000708#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
709 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
710 "Unexpected base register!");
711#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000712 // The immediate is already shifted for the implicit zeroes, so no change
713 // here.
714 return MO1.getImm() & 0xff;
715}
716
Bill Wendling1fd374e2010-11-30 22:57:21 +0000717/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
718static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
719 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000720 // [Rn, Rm]
721 // {5-3} = Rm
722 // {2-0} = Rn
723 //
724 // [Rn, #imm]
725 // {7-3} = imm5
726 // {2-0} = Rn
727 const MCOperand &MO = MI.getOperand(OpIdx);
728 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
729 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
730 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000731 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000732
733 if (MO2.getReg() != 0)
734 // Is an immediate.
735 Imm5 = getARMRegisterNumbering(MO2.getReg());
736
737 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000738}
739
Bill Wendling1fd374e2010-11-30 22:57:21 +0000740/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
741uint32_t ARMMCCodeEmitter::
742getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
743 SmallVectorImpl<MCFixup> &) const {
744 return getAddrModeSOpValue(MI, OpIdx, 4);
745}
746
747/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
748uint32_t ARMMCCodeEmitter::
749getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
750 SmallVectorImpl<MCFixup> &) const {
751 return getAddrModeSOpValue(MI, OpIdx, 2);
752}
753
754/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
755uint32_t ARMMCCodeEmitter::
756getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
757 SmallVectorImpl<MCFixup> &) const {
758 return getAddrModeSOpValue(MI, OpIdx, 1);
759}
760
Bill Wendlingb8958b02010-12-08 01:57:09 +0000761/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
762uint32_t ARMMCCodeEmitter::
763getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
764 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000765 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000766}
767
Jim Grosbach5177f792010-12-01 21:09:40 +0000768/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000769uint32_t ARMMCCodeEmitter::
770getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
771 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000772 // {12-9} = reg
773 // {8} = (U)nsigned (add == '1', sub == '0')
774 // {7-0} = imm8
775 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000776 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000777 // If The first operand isn't a register, we have a label reference.
778 const MCOperand &MO = MI.getOperand(OpIdx);
779 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000780 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000781 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000782 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000783
784 assert(MO.isExpr() && "Unexpected machine operand type!");
785 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000786 MCFixupKind Kind;
787 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
788 if (Subtarget.isThumb2())
789 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
790 else
791 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000792 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
793
794 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000795 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000796 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000797 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
798 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000799
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000800 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
801 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000802 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000803 Binary |= (1 << 8);
804 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000805 return Binary;
806}
807
Jim Grosbach806e80e2010-11-03 23:52:49 +0000808unsigned ARMMCCodeEmitter::
809getSORegOpValue(const MCInst &MI, unsigned OpIdx,
810 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000811 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
812 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
813 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000814 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000815 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000816 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000817 // {6-5} = type
818 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000819 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000820 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000821 // else (imm shift)
822 // {11-7} = imm
823
824 const MCOperand &MO = MI.getOperand(OpIdx);
825 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
826 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
827 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
828
829 // Encode Rm.
830 unsigned Binary = getARMRegisterNumbering(MO.getReg());
831
832 // Encode the shift opcode.
833 unsigned SBits = 0;
834 unsigned Rs = MO1.getReg();
835 if (Rs) {
836 // Set shift operand (bit[7:4]).
837 // LSL - 0001
838 // LSR - 0011
839 // ASR - 0101
840 // ROR - 0111
841 // RRX - 0110 and bit[11:8] clear.
842 switch (SOpc) {
843 default: llvm_unreachable("Unknown shift opc!");
844 case ARM_AM::lsl: SBits = 0x1; break;
845 case ARM_AM::lsr: SBits = 0x3; break;
846 case ARM_AM::asr: SBits = 0x5; break;
847 case ARM_AM::ror: SBits = 0x7; break;
848 case ARM_AM::rrx: SBits = 0x6; break;
849 }
850 } else {
851 // Set shift operand (bit[6:4]).
852 // LSL - 000
853 // LSR - 010
854 // ASR - 100
855 // ROR - 110
856 switch (SOpc) {
857 default: llvm_unreachable("Unknown shift opc!");
858 case ARM_AM::lsl: SBits = 0x0; break;
859 case ARM_AM::lsr: SBits = 0x2; break;
860 case ARM_AM::asr: SBits = 0x4; break;
861 case ARM_AM::ror: SBits = 0x6; break;
862 }
863 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000864
Jim Grosbachef324d72010-10-12 23:53:58 +0000865 Binary |= SBits << 4;
866 if (SOpc == ARM_AM::rrx)
867 return Binary;
868
869 // Encode the shift operation Rs or shift_imm (except rrx).
870 if (Rs) {
871 // Encode Rs bit[11:8].
872 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
873 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
874 }
875
876 // Encode shift_imm bit[11:7].
877 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
878}
879
Jim Grosbach806e80e2010-11-03 23:52:49 +0000880unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000881getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
882 SmallVectorImpl<MCFixup> &Fixups) const {
883 const MCOperand &MO1 = MI.getOperand(OpNum);
884 const MCOperand &MO2 = MI.getOperand(OpNum+1);
885 const MCOperand &MO3 = MI.getOperand(OpNum+2);
886
887 // Encoded as [Rn, Rm, imm].
888 // FIXME: Needs fixup support.
889 unsigned Value = getARMRegisterNumbering(MO1.getReg());
890 Value <<= 4;
891 Value |= getARMRegisterNumbering(MO2.getReg());
892 Value <<= 2;
893 Value |= MO3.getImm();
894
895 return Value;
896}
897
898unsigned ARMMCCodeEmitter::
899getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
900 SmallVectorImpl<MCFixup> &Fixups) const {
901 const MCOperand &MO1 = MI.getOperand(OpNum);
902 const MCOperand &MO2 = MI.getOperand(OpNum+1);
903
904 // FIXME: Needs fixup support.
905 unsigned Value = getARMRegisterNumbering(MO1.getReg());
906
907 // Even though the immediate is 8 bits long, we need 9 bits in order
908 // to represent the (inverse of the) sign bit.
909 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000910 int32_t tmp = (int32_t)MO2.getImm();
911 if (tmp < 0)
912 tmp = abs(tmp);
913 else
914 Value |= 256; // Set the ADD bit
915 Value |= tmp & 255;
916 return Value;
917}
918
919unsigned ARMMCCodeEmitter::
920getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
921 SmallVectorImpl<MCFixup> &Fixups) const {
922 const MCOperand &MO1 = MI.getOperand(OpNum);
923
924 // FIXME: Needs fixup support.
925 unsigned Value = 0;
926 int32_t tmp = (int32_t)MO1.getImm();
927 if (tmp < 0)
928 tmp = abs(tmp);
929 else
930 Value |= 256; // Set the ADD bit
931 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000932 return Value;
933}
934
935unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000936getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
937 SmallVectorImpl<MCFixup> &Fixups) const {
938 const MCOperand &MO1 = MI.getOperand(OpNum);
939
940 // FIXME: Needs fixup support.
941 unsigned Value = 0;
942 int32_t tmp = (int32_t)MO1.getImm();
943 if (tmp < 0)
944 tmp = abs(tmp);
945 else
946 Value |= 4096; // Set the ADD bit
947 Value |= tmp & 4095;
948 return Value;
949}
950
951unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000952getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
953 SmallVectorImpl<MCFixup> &Fixups) const {
954 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
955 // shifted. The second is the amount to shift by.
956 //
957 // {3-0} = Rm.
958 // {4} = 0
959 // {6-5} = type
960 // {11-7} = imm
961
962 const MCOperand &MO = MI.getOperand(OpIdx);
963 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
964 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
965
966 // Encode Rm.
967 unsigned Binary = getARMRegisterNumbering(MO.getReg());
968
969 // Encode the shift opcode.
970 unsigned SBits = 0;
971 // Set shift operand (bit[6:4]).
972 // LSL - 000
973 // LSR - 010
974 // ASR - 100
975 // ROR - 110
976 switch (SOpc) {
977 default: llvm_unreachable("Unknown shift opc!");
978 case ARM_AM::lsl: SBits = 0x0; break;
979 case ARM_AM::lsr: SBits = 0x2; break;
980 case ARM_AM::asr: SBits = 0x4; break;
981 case ARM_AM::ror: SBits = 0x6; break;
982 }
983
984 Binary |= SBits << 4;
985 if (SOpc == ARM_AM::rrx)
986 return Binary;
987
988 // Encode shift_imm bit[11:7].
989 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
990}
991
992unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000993getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
994 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000995 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
996 // msb of the mask.
997 const MCOperand &MO = MI.getOperand(Op);
998 uint32_t v = ~MO.getImm();
999 uint32_t lsb = CountTrailingZeros_32(v);
1000 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1001 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1002 return lsb | (msb << 5);
1003}
1004
Jim Grosbach806e80e2010-11-03 23:52:49 +00001005unsigned ARMMCCodeEmitter::
1006getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001007 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001008 // VLDM/VSTM:
1009 // {12-8} = Vd
1010 // {7-0} = Number of registers
1011 //
1012 // LDM/STM:
1013 // {15-0} = Bitfield of GPRs.
1014 unsigned Reg = MI.getOperand(Op).getReg();
1015 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1016 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1017
Bill Wendling5e559a22010-11-09 00:30:18 +00001018 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001019
1020 if (SPRRegs || DPRRegs) {
1021 // VLDM/VSTM
1022 unsigned RegNo = getARMRegisterNumbering(Reg);
1023 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1024 Binary |= (RegNo & 0x1f) << 8;
1025 if (SPRRegs)
1026 Binary |= NumRegs;
1027 else
1028 Binary |= NumRegs * 2;
1029 } else {
1030 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1031 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1032 Binary |= 1 << RegNo;
1033 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001034 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001035
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001036 return Binary;
1037}
1038
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001039/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1040/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001041unsigned ARMMCCodeEmitter::
1042getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1043 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001044 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001045 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001046
Owen Andersond9aa7d32010-11-02 00:05:05 +00001047 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001048 unsigned Align = 0;
1049
1050 switch (Imm.getImm()) {
1051 default: break;
1052 case 2:
1053 case 4:
1054 case 8: Align = 0x01; break;
1055 case 16: Align = 0x02; break;
1056 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001057 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001058
Owen Andersond9aa7d32010-11-02 00:05:05 +00001059 return RegNo | (Align << 4);
1060}
1061
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001062/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1063/// alignment operand for use in VLD-dup instructions. This is the same as
1064/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1065/// different for VLD4-dup.
1066unsigned ARMMCCodeEmitter::
1067getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1068 SmallVectorImpl<MCFixup> &Fixups) const {
1069 const MCOperand &Reg = MI.getOperand(Op);
1070 const MCOperand &Imm = MI.getOperand(Op + 1);
1071
1072 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1073 unsigned Align = 0;
1074
1075 switch (Imm.getImm()) {
1076 default: break;
1077 case 2:
1078 case 4:
1079 case 8: Align = 0x01; break;
1080 case 16: Align = 0x03; break;
1081 }
1082
1083 return RegNo | (Align << 4);
1084}
1085
Jim Grosbach806e80e2010-11-03 23:52:49 +00001086unsigned ARMMCCodeEmitter::
1087getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1088 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001089 const MCOperand &MO = MI.getOperand(Op);
1090 if (MO.getReg() == 0) return 0x0D;
1091 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001092}
1093
Jim Grosbach568eeed2010-09-17 18:46:17 +00001094void ARMMCCodeEmitter::
1095EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001096 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001097 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001098 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001099 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001100 uint64_t TSFlags = Desc.TSFlags;
1101 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001102 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001103 int Size;
1104 // Basic size info comes from the TSFlags field.
1105 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1106 default: llvm_unreachable("Unexpected instruction size!");
1107 case ARMII::Size2Bytes: Size = 2; break;
1108 case ARMII::Size4Bytes: Size = 4; break;
1109 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001110 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1111 // Thumb 32-bit wide instructions need to be have the high order halfword
1112 // emitted first.
1113 if (Subtarget.isThumb() && Size == 4) {
1114 EmitConstant(Binary >> 16, 2, OS);
1115 EmitConstant(Binary & 0xffff, 2, OS);
1116 } else
1117 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001118 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001119}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001120
Jim Grosbach806e80e2010-11-03 23:52:49 +00001121#include "ARMGenMCCodeEmitter.inc"