blob: 8fbeb0e6e0dbb95384621c6b9b044857845e8096 [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 },
50 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000051 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonfb20d892010-12-09 00:27:41 +000054 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach662a8162010-12-06 23:57:07 +000055 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000056 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingdff2f712010-12-08 23:01:43 +000057 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000058 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000059 { "fixup_arm_movt_hi16", 0, 16, 0 },
60 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000061 };
62
63 if (Kind < FirstTargetFixupKind)
64 return MCCodeEmitter::getFixupKindInfo(Kind);
65
66 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
67 "Invalid kind!");
68 return Infos[Kind - FirstTargetFixupKind];
69 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000070 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
71
Jim Grosbach9af82ba2010-10-07 21:57:55 +000072 // getBinaryCodeForInstr - TableGen'erated function for getting the
73 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000074 unsigned getBinaryCodeForInstr(const MCInst &MI,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000076
77 /// getMachineOpValue - Return binary encoding of operand. If the machine
78 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000079 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
80 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000081
Jason W Kim837caa92010-11-18 23:37:15 +000082 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
83 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
84 SmallVectorImpl<MCFixup> &Fixups) const;
85
Bill Wendling92b5a2e2010-11-03 01:49:29 +000086 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000087 unsigned &Reg, unsigned &Imm,
88 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000089
Jim Grosbach662a8162010-12-06 23:57:07 +000090 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000091 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000092 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
93 SmallVectorImpl<MCFixup> &Fixups) const;
94
Bill Wendling09aa3f02010-12-09 00:39:08 +000095 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
96 /// BLX branch target.
97 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const;
99
Bill Wendlingdff2f712010-12-08 23:01:43 +0000100 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
101 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
103
Jim Grosbachc466b932010-11-11 18:04:49 +0000104 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
105 /// branch target.
106 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000109 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
110 /// ADR label target.
111 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const;
113
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000114 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
115 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000116 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000118
Owen Anderson9d63d902010-12-01 19:18:46 +0000119 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
120 /// operand.
121 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
123
124
Jim Grosbach54fea632010-11-09 17:20:53 +0000125 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
126 /// operand as needed by load/store instructions.
127 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
129
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000130 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
131 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
132 SmallVectorImpl<MCFixup> &Fixups) const {
133 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
134 switch (Mode) {
135 default: assert(0 && "Unknown addressing sub-mode!");
136 case ARM_AM::da: return 0;
137 case ARM_AM::ia: return 1;
138 case ARM_AM::db: return 2;
139 case ARM_AM::ib: return 3;
140 }
141 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000142 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
143 ///
144 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
145 switch (ShOpc) {
146 default: llvm_unreachable("Unknown shift opc!");
147 case ARM_AM::no_shift:
148 case ARM_AM::lsl: return 0;
149 case ARM_AM::lsr: return 1;
150 case ARM_AM::asr: return 2;
151 case ARM_AM::ror:
152 case ARM_AM::rrx: return 3;
153 }
154 return 0;
155 }
156
157 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
158 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
160
161 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
162 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
164
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000165 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
166 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
168
Jim Grosbach570a9222010-11-11 01:09:40 +0000169 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
170 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000172
Jim Grosbachd967cd02010-12-07 21:50:47 +0000173 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
174 /// operand.
175 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
177
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000178 /// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
179 uint32_t getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181
Bill Wendling1fd374e2010-11-30 22:57:21 +0000182 /// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
183 uint32_t getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
186 /// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
187 uint32_t getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
189
Bill Wendlingb8958b02010-12-08 01:57:09 +0000190 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
191 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
192 SmallVectorImpl<MCFixup> &Fixups) const;
193
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000194 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000195 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000197
Jim Grosbach08bd5492010-10-12 23:00:24 +0000198 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000199 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
200 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000201 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
202 // '1' respectively.
203 return MI.getOperand(Op).getReg() == ARM::CPSR;
204 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000205
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000206 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000207 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
208 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000209 unsigned SoImm = MI.getOperand(Op).getImm();
210 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
211 assert(SoImmVal != -1 && "Not a valid so_imm value!");
212
213 // Encode rotate_imm.
214 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
215 << ARMII::SoRotImmShift;
216
217 // Encode immed_8.
218 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
219 return Binary;
220 }
Owen Anderson5de6d842010-11-12 21:12:40 +0000221
222 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
223 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
224 SmallVectorImpl<MCFixup> &Fixups) const {
225 unsigned SoImm = MI.getOperand(Op).getImm();
226 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
227 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
228 return Encoded;
229 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000230
Owen Anderson75579f72010-11-29 22:44:32 +0000231 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
232 SmallVectorImpl<MCFixup> &Fixups) const;
233 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
234 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000235 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
236 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000237 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
238 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000239
Jim Grosbachef324d72010-10-12 23:53:58 +0000240 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000241 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
242 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000243 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
244 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000245
Jim Grosbach806e80e2010-11-03 23:52:49 +0000246 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
247 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000248 switch (MI.getOperand(Op).getImm()) {
249 default: assert (0 && "Not a valid rot_imm value!");
250 case 0: return 0;
251 case 8: return 1;
252 case 16: return 2;
253 case 24: return 3;
254 }
255 }
256
Jim Grosbach806e80e2010-11-03 23:52:49 +0000257 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
258 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000259 return MI.getOperand(Op).getImm() - 1;
260 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000261
Jim Grosbach806e80e2010-11-03 23:52:49 +0000262 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000264 return 64 - MI.getOperand(Op).getImm();
265 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000266
Jim Grosbach806e80e2010-11-03 23:52:49 +0000267 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
268 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000269
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
272 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000274 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000276 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000278
Owen Andersonc7139a62010-11-11 19:07:48 +0000279 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
280 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000281 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000282 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000283 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000284 unsigned EncodedValue) const;
285
286 unsigned VFPThumb2PostEncoder(const MCInst &MI,
287 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000288
Jim Grosbach70933262010-11-04 01:12:30 +0000289 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000290 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000291 }
292
Jim Grosbach70933262010-11-04 01:12:30 +0000293 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000294 // Output the constant in little endian byte order.
295 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000296 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000297 Val >>= 8;
298 }
299 }
300
Jim Grosbach568eeed2010-09-17 18:46:17 +0000301 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
302 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303};
304
305} // end anonymous namespace
306
Bill Wendling0800ce72010-11-02 22:53:11 +0000307MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
308 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000309 return new ARMMCCodeEmitter(TM, Ctx);
310}
311
Owen Anderson57dac882010-11-11 21:36:43 +0000312/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
Owen Andersonc7139a62010-11-11 19:07:48 +0000313/// instructions, and rewrite them to their Thumb2 form if we are currently in
314/// Thumb2 mode.
315unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
316 unsigned EncodedValue) const {
317 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
318 if (Subtarget.isThumb2()) {
319 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
320 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
321 // set to 1111.
322 unsigned Bit24 = EncodedValue & 0x01000000;
323 unsigned Bit28 = Bit24 << 4;
324 EncodedValue &= 0xEFFFFFFF;
325 EncodedValue |= Bit28;
326 EncodedValue |= 0x0F000000;
327 }
328
329 return EncodedValue;
330}
331
Owen Anderson57dac882010-11-11 21:36:43 +0000332/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
333/// instructions, and rewrite them to their Thumb2 form if we are currently in
334/// Thumb2 mode.
335unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
336 unsigned EncodedValue) const {
337 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
338 if (Subtarget.isThumb2()) {
339 EncodedValue &= 0xF0FFFFFF;
340 EncodedValue |= 0x09000000;
341 }
342
343 return EncodedValue;
344}
345
Owen Anderson8f143912010-11-11 23:12:55 +0000346/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
347/// instructions, and rewrite them to their Thumb2 form if we are currently in
348/// Thumb2 mode.
349unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
350 unsigned EncodedValue) const {
351 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
352 if (Subtarget.isThumb2()) {
353 EncodedValue &= 0x00FFFFFF;
354 EncodedValue |= 0xEE000000;
355 }
356
357 return EncodedValue;
358}
359
Bill Wendlingcf590262010-12-01 21:54:50 +0000360/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
361/// them to their Thumb2 form if we are currently in Thumb2 mode.
362unsigned ARMMCCodeEmitter::
363VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
364 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
365 EncodedValue &= 0x0FFFFFFF;
366 EncodedValue |= 0xE0000000;
367 }
368 return EncodedValue;
369}
Owen Anderson57dac882010-11-11 21:36:43 +0000370
Jim Grosbach56ac9072010-10-08 21:45:55 +0000371/// getMachineOpValue - Return binary encoding of operand. If the machine
372/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000373unsigned ARMMCCodeEmitter::
374getMachineOpValue(const MCInst &MI, const MCOperand &MO,
375 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000376 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000377 unsigned Reg = MO.getReg();
378 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000379
Jim Grosbachb0708d22010-11-30 23:51:41 +0000380 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000381 switch (Reg) {
382 default:
383 return RegNo;
384 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
385 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
386 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
387 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
388 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000389 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000390 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000391 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000392 } else if (MO.isFPImm()) {
393 return static_cast<unsigned>(APFloat(MO.getFPImm())
394 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000395 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000396
Jim Grosbach817c1a62010-11-19 00:27:09 +0000397 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000398 return 0;
399}
400
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000401/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000402bool ARMMCCodeEmitter::
403EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
404 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000405 const MCOperand &MO = MI.getOperand(OpIdx);
406 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000407
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000408 Reg = getARMRegisterNumbering(MO.getReg());
409
410 int32_t SImm = MO1.getImm();
411 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000412
Jim Grosbachab682a22010-10-28 18:34:10 +0000413 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000414 if (SImm == INT32_MIN)
415 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000416
Jim Grosbachab682a22010-10-28 18:34:10 +0000417 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000418 if (SImm < 0) {
419 SImm = -SImm;
420 isAdd = false;
421 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000422
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000423 Imm = SImm;
424 return isAdd;
425}
426
Bill Wendlingdff2f712010-12-08 23:01:43 +0000427/// getBranchTargetOpValue - Helper function to get the branch target operand,
428/// which is either an immediate or requires a fixup.
429static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
430 unsigned FixupKind,
431 SmallVectorImpl<MCFixup> &Fixups) {
432 const MCOperand &MO = MI.getOperand(OpIdx);
433
434 // If the destination is an immediate, we have nothing to do.
435 if (MO.isImm()) return MO.getImm();
436 assert(MO.isExpr() && "Unexpected branch target type!");
437 const MCExpr *Expr = MO.getExpr();
438 MCFixupKind Kind = MCFixupKind(FixupKind);
439 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
440
441 // All of the information is in the fixup.
442 return 0;
443}
444
445/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000446uint32_t ARMMCCodeEmitter::
447getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
448 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000449 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000450}
451
Bill Wendling09aa3f02010-12-09 00:39:08 +0000452/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
453/// BLX branch target.
454uint32_t ARMMCCodeEmitter::
455getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
456 SmallVectorImpl<MCFixup> &Fixups) const {
457 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
458}
459
Bill Wendlingdff2f712010-12-08 23:01:43 +0000460/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
461uint32_t ARMMCCodeEmitter::
462getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
463 SmallVectorImpl<MCFixup> &Fixups) const {
464 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
465}
466
467/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
468/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000469uint32_t ARMMCCodeEmitter::
470getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000471 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000472 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
473 if (Subtarget.isThumb2())
474 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000475 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000476}
477
Bill Wendlingdff2f712010-12-08 23:01:43 +0000478/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
479/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000480uint32_t ARMMCCodeEmitter::
481getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
482 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000483 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
485 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000486}
487
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000488/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000489uint32_t ARMMCCodeEmitter::
490getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000492 // {17-13} = reg
493 // {12} = (U)nsigned (add == '1', sub == '0')
494 // {11-0} = imm12
495 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000496 bool isAdd = true;
497 // If The first operand isn't a register, we have a label reference.
498 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000499 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
500 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000501 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000502 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000503 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000504
Owen Andersoneb6779c2010-12-07 00:45:21 +0000505 const MCExpr *Expr = 0;
506 if (!MO.isReg())
507 Expr = MO.getExpr();
508 else
509 Expr = MO2.getExpr();
510
Jim Grosbachdff84b02010-12-02 00:28:45 +0000511 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000512 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
513
514 ++MCNumCPRelocations;
515 } else
516 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000517
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000518 uint32_t Binary = Imm12 & 0xfff;
519 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000520 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000521 Binary |= (1 << 12);
522 Binary |= (Reg << 13);
523 return Binary;
524}
525
Owen Anderson9d63d902010-12-01 19:18:46 +0000526/// getT2AddrModeImm8s4OpValue - Return encoding info for
527/// 'reg +/- imm8<<2' operand.
528uint32_t ARMMCCodeEmitter::
529getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
530 SmallVectorImpl<MCFixup> &Fixups) const {
531 // {17-13} = reg
532 // {12} = (U)nsigned (add == '1', sub == '0')
533 // {11-0} = imm8
534 unsigned Reg, Imm8;
535 bool isAdd = true;
536 // If The first operand isn't a register, we have a label reference.
537 const MCOperand &MO = MI.getOperand(OpIdx);
538 if (!MO.isReg()) {
539 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
540 Imm8 = 0;
541 isAdd = false ; // 'U' bit is set as part of the fixup.
542
543 assert(MO.isExpr() && "Unexpected machine operand type!");
544 const MCExpr *Expr = MO.getExpr();
545 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
546 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
547
548 ++MCNumCPRelocations;
549 } else
550 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
551
552 uint32_t Binary = (Imm8 >> 2) & 0xff;
553 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
554 if (isAdd)
555 Binary |= (1 << 9);
556 Binary |= (Reg << 9);
557 return Binary;
558}
559
Jim Grosbach54fea632010-11-09 17:20:53 +0000560uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000561getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
562 SmallVectorImpl<MCFixup> &Fixups) const {
563 // {20-16} = imm{15-12}
564 // {11-0} = imm{11-0}
565 const MCOperand &MO = MI.getOperand(OpIdx);
566 if (MO.isImm()) {
567 return static_cast<unsigned>(MO.getImm());
568 } else if (const MCSymbolRefExpr *Expr =
569 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
570 MCFixupKind Kind;
571 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000572 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000573 case MCSymbolRefExpr::VK_ARM_HI16:
574 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
575 break;
576 case MCSymbolRefExpr::VK_ARM_LO16:
577 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
578 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000579 }
580 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
581 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000582 };
583 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000584 return 0;
585}
586
587uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000588getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
589 SmallVectorImpl<MCFixup> &Fixups) const {
590 const MCOperand &MO = MI.getOperand(OpIdx);
591 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
592 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
593 unsigned Rn = getARMRegisterNumbering(MO.getReg());
594 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000595 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
596 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000597 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
598 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000599
600 // {16-13} = Rn
601 // {12} = isAdd
602 // {11-0} = shifter
603 // {3-0} = Rm
604 // {4} = 0
605 // {6-5} = type
606 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000607 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000608 Binary |= Rn << 13;
609 Binary |= SBits << 5;
610 Binary |= ShImm << 7;
611 if (isAdd)
612 Binary |= 1 << 12;
613 return Binary;
614}
615
Jim Grosbach570a9222010-11-11 01:09:40 +0000616uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000617getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
618 SmallVectorImpl<MCFixup> &Fixups) const {
619 // {17-14} Rn
620 // {13} 1 == imm12, 0 == Rm
621 // {12} isAdd
622 // {11-0} imm12/Rm
623 const MCOperand &MO = MI.getOperand(OpIdx);
624 unsigned Rn = getARMRegisterNumbering(MO.getReg());
625 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
626 Binary |= Rn << 14;
627 return Binary;
628}
629
630uint32_t ARMMCCodeEmitter::
631getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
632 SmallVectorImpl<MCFixup> &Fixups) const {
633 // {13} 1 == imm12, 0 == Rm
634 // {12} isAdd
635 // {11-0} imm12/Rm
636 const MCOperand &MO = MI.getOperand(OpIdx);
637 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
638 unsigned Imm = MO1.getImm();
639 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
640 bool isReg = MO.getReg() != 0;
641 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
642 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
643 if (isReg) {
644 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
645 Binary <<= 7; // Shift amount is bits [11:7]
646 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
647 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
648 }
649 return Binary | (isAdd << 12) | (isReg << 13);
650}
651
652uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000653getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
654 SmallVectorImpl<MCFixup> &Fixups) const {
655 // {9} 1 == imm8, 0 == Rm
656 // {8} isAdd
657 // {7-4} imm7_4/zero
658 // {3-0} imm3_0/Rm
659 const MCOperand &MO = MI.getOperand(OpIdx);
660 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
661 unsigned Imm = MO1.getImm();
662 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
663 bool isImm = MO.getReg() == 0;
664 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
665 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
666 if (!isImm)
667 Imm8 = getARMRegisterNumbering(MO.getReg());
668 return Imm8 | (isAdd << 8) | (isImm << 9);
669}
670
671uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000672getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
673 SmallVectorImpl<MCFixup> &Fixups) const {
674 // {13} 1 == imm8, 0 == Rm
675 // {12-9} Rn
676 // {8} isAdd
677 // {7-4} imm7_4/zero
678 // {3-0} imm3_0/Rm
679 const MCOperand &MO = MI.getOperand(OpIdx);
680 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
681 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
682 unsigned Rn = getARMRegisterNumbering(MO.getReg());
683 unsigned Imm = MO2.getImm();
684 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
685 bool isImm = MO1.getReg() == 0;
686 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
687 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
688 if (!isImm)
689 Imm8 = getARMRegisterNumbering(MO1.getReg());
690 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
691}
692
Bill Wendlingb8958b02010-12-08 01:57:09 +0000693/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000694uint32_t ARMMCCodeEmitter::
695getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
696 SmallVectorImpl<MCFixup> &Fixups) const {
697 // [SP, #imm]
698 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000699 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000700#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
701 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
702 "Unexpected base register!");
703#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000704 // The immediate is already shifted for the implicit zeroes, so no change
705 // here.
706 return MO1.getImm() & 0xff;
707}
708
Bill Wendling1fd374e2010-11-30 22:57:21 +0000709/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
710static unsigned getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
711 unsigned Scale) {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000712 // [Rn, Rm]
713 // {5-3} = Rm
714 // {2-0} = Rn
715 //
716 // [Rn, #imm]
717 // {7-3} = imm5
718 // {2-0} = Rn
719 const MCOperand &MO = MI.getOperand(OpIdx);
720 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
721 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
722 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling1fd374e2010-11-30 22:57:21 +0000723 unsigned Imm5 = (MO1.getImm() / Scale) & 0x1f;
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000724
725 if (MO2.getReg() != 0)
726 // Is an immediate.
727 Imm5 = getARMRegisterNumbering(MO2.getReg());
728
729 return (Imm5 << 3) | Rn;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000730}
731
Bill Wendling1fd374e2010-11-30 22:57:21 +0000732/// getAddrModeS4OpValue - Return encoding for t_addrmode_s4 operands.
733uint32_t ARMMCCodeEmitter::
734getAddrModeS4OpValue(const MCInst &MI, unsigned OpIdx,
735 SmallVectorImpl<MCFixup> &) const {
736 return getAddrModeSOpValue(MI, OpIdx, 4);
737}
738
739/// getAddrModeS2OpValue - Return encoding for t_addrmode_s2 operands.
740uint32_t ARMMCCodeEmitter::
741getAddrModeS2OpValue(const MCInst &MI, unsigned OpIdx,
742 SmallVectorImpl<MCFixup> &) const {
743 return getAddrModeSOpValue(MI, OpIdx, 2);
744}
745
746/// getAddrModeS1OpValue - Return encoding for t_addrmode_s1 operands.
747uint32_t ARMMCCodeEmitter::
748getAddrModeS1OpValue(const MCInst &MI, unsigned OpIdx,
749 SmallVectorImpl<MCFixup> &) const {
750 return getAddrModeSOpValue(MI, OpIdx, 1);
751}
752
Bill Wendlingb8958b02010-12-08 01:57:09 +0000753/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
754uint32_t ARMMCCodeEmitter::
755getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
756 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000757 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000758}
759
Jim Grosbach5177f792010-12-01 21:09:40 +0000760/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000761uint32_t ARMMCCodeEmitter::
762getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
763 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000764 // {12-9} = reg
765 // {8} = (U)nsigned (add == '1', sub == '0')
766 // {7-0} = imm8
767 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000768 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000769 // If The first operand isn't a register, we have a label reference.
770 const MCOperand &MO = MI.getOperand(OpIdx);
771 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000772 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000773 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000774 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000775
776 assert(MO.isExpr() && "Unexpected machine operand type!");
777 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000778 MCFixupKind Kind;
779 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
780 if (Subtarget.isThumb2())
781 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
782 else
783 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000784 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
785
786 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000787 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000788 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000789 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
790 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000791
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000792 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
793 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000794 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000795 Binary |= (1 << 8);
796 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000797 return Binary;
798}
799
Jim Grosbach806e80e2010-11-03 23:52:49 +0000800unsigned ARMMCCodeEmitter::
801getSORegOpValue(const MCInst &MI, unsigned OpIdx,
802 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000803 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
804 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
805 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000806 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000807 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000808 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000809 // {6-5} = type
810 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000811 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000812 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000813 // else (imm shift)
814 // {11-7} = imm
815
816 const MCOperand &MO = MI.getOperand(OpIdx);
817 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
818 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
819 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
820
821 // Encode Rm.
822 unsigned Binary = getARMRegisterNumbering(MO.getReg());
823
824 // Encode the shift opcode.
825 unsigned SBits = 0;
826 unsigned Rs = MO1.getReg();
827 if (Rs) {
828 // Set shift operand (bit[7:4]).
829 // LSL - 0001
830 // LSR - 0011
831 // ASR - 0101
832 // ROR - 0111
833 // RRX - 0110 and bit[11:8] clear.
834 switch (SOpc) {
835 default: llvm_unreachable("Unknown shift opc!");
836 case ARM_AM::lsl: SBits = 0x1; break;
837 case ARM_AM::lsr: SBits = 0x3; break;
838 case ARM_AM::asr: SBits = 0x5; break;
839 case ARM_AM::ror: SBits = 0x7; break;
840 case ARM_AM::rrx: SBits = 0x6; break;
841 }
842 } else {
843 // Set shift operand (bit[6:4]).
844 // LSL - 000
845 // LSR - 010
846 // ASR - 100
847 // ROR - 110
848 switch (SOpc) {
849 default: llvm_unreachable("Unknown shift opc!");
850 case ARM_AM::lsl: SBits = 0x0; break;
851 case ARM_AM::lsr: SBits = 0x2; break;
852 case ARM_AM::asr: SBits = 0x4; break;
853 case ARM_AM::ror: SBits = 0x6; break;
854 }
855 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000856
Jim Grosbachef324d72010-10-12 23:53:58 +0000857 Binary |= SBits << 4;
858 if (SOpc == ARM_AM::rrx)
859 return Binary;
860
861 // Encode the shift operation Rs or shift_imm (except rrx).
862 if (Rs) {
863 // Encode Rs bit[11:8].
864 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
865 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
866 }
867
868 // Encode shift_imm bit[11:7].
869 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
870}
871
Jim Grosbach806e80e2010-11-03 23:52:49 +0000872unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000873getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
874 SmallVectorImpl<MCFixup> &Fixups) const {
875 const MCOperand &MO1 = MI.getOperand(OpNum);
876 const MCOperand &MO2 = MI.getOperand(OpNum+1);
877 const MCOperand &MO3 = MI.getOperand(OpNum+2);
878
879 // Encoded as [Rn, Rm, imm].
880 // FIXME: Needs fixup support.
881 unsigned Value = getARMRegisterNumbering(MO1.getReg());
882 Value <<= 4;
883 Value |= getARMRegisterNumbering(MO2.getReg());
884 Value <<= 2;
885 Value |= MO3.getImm();
886
887 return Value;
888}
889
890unsigned ARMMCCodeEmitter::
891getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
892 SmallVectorImpl<MCFixup> &Fixups) const {
893 const MCOperand &MO1 = MI.getOperand(OpNum);
894 const MCOperand &MO2 = MI.getOperand(OpNum+1);
895
896 // FIXME: Needs fixup support.
897 unsigned Value = getARMRegisterNumbering(MO1.getReg());
898
899 // Even though the immediate is 8 bits long, we need 9 bits in order
900 // to represent the (inverse of the) sign bit.
901 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000902 int32_t tmp = (int32_t)MO2.getImm();
903 if (tmp < 0)
904 tmp = abs(tmp);
905 else
906 Value |= 256; // Set the ADD bit
907 Value |= tmp & 255;
908 return Value;
909}
910
911unsigned ARMMCCodeEmitter::
912getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
913 SmallVectorImpl<MCFixup> &Fixups) const {
914 const MCOperand &MO1 = MI.getOperand(OpNum);
915
916 // FIXME: Needs fixup support.
917 unsigned Value = 0;
918 int32_t tmp = (int32_t)MO1.getImm();
919 if (tmp < 0)
920 tmp = abs(tmp);
921 else
922 Value |= 256; // Set the ADD bit
923 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000924 return Value;
925}
926
927unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000928getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
929 SmallVectorImpl<MCFixup> &Fixups) const {
930 const MCOperand &MO1 = MI.getOperand(OpNum);
931
932 // FIXME: Needs fixup support.
933 unsigned Value = 0;
934 int32_t tmp = (int32_t)MO1.getImm();
935 if (tmp < 0)
936 tmp = abs(tmp);
937 else
938 Value |= 4096; // Set the ADD bit
939 Value |= tmp & 4095;
940 return Value;
941}
942
943unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000944getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
945 SmallVectorImpl<MCFixup> &Fixups) const {
946 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
947 // shifted. The second is the amount to shift by.
948 //
949 // {3-0} = Rm.
950 // {4} = 0
951 // {6-5} = type
952 // {11-7} = imm
953
954 const MCOperand &MO = MI.getOperand(OpIdx);
955 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
956 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
957
958 // Encode Rm.
959 unsigned Binary = getARMRegisterNumbering(MO.getReg());
960
961 // Encode the shift opcode.
962 unsigned SBits = 0;
963 // Set shift operand (bit[6:4]).
964 // LSL - 000
965 // LSR - 010
966 // ASR - 100
967 // ROR - 110
968 switch (SOpc) {
969 default: llvm_unreachable("Unknown shift opc!");
970 case ARM_AM::lsl: SBits = 0x0; break;
971 case ARM_AM::lsr: SBits = 0x2; break;
972 case ARM_AM::asr: SBits = 0x4; break;
973 case ARM_AM::ror: SBits = 0x6; break;
974 }
975
976 Binary |= SBits << 4;
977 if (SOpc == ARM_AM::rrx)
978 return Binary;
979
980 // Encode shift_imm bit[11:7].
981 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
982}
983
984unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +0000985getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
986 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +0000987 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
988 // msb of the mask.
989 const MCOperand &MO = MI.getOperand(Op);
990 uint32_t v = ~MO.getImm();
991 uint32_t lsb = CountTrailingZeros_32(v);
992 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
993 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
994 return lsb | (msb << 5);
995}
996
Jim Grosbach806e80e2010-11-03 23:52:49 +0000997unsigned ARMMCCodeEmitter::
998getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +0000999 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001000 // VLDM/VSTM:
1001 // {12-8} = Vd
1002 // {7-0} = Number of registers
1003 //
1004 // LDM/STM:
1005 // {15-0} = Bitfield of GPRs.
1006 unsigned Reg = MI.getOperand(Op).getReg();
1007 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1008 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1009
Bill Wendling5e559a22010-11-09 00:30:18 +00001010 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001011
1012 if (SPRRegs || DPRRegs) {
1013 // VLDM/VSTM
1014 unsigned RegNo = getARMRegisterNumbering(Reg);
1015 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1016 Binary |= (RegNo & 0x1f) << 8;
1017 if (SPRRegs)
1018 Binary |= NumRegs;
1019 else
1020 Binary |= NumRegs * 2;
1021 } else {
1022 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1023 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1024 Binary |= 1 << RegNo;
1025 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001026 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001027
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001028 return Binary;
1029}
1030
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001031/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1032/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001033unsigned ARMMCCodeEmitter::
1034getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1035 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001036 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001037 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001038
Owen Andersond9aa7d32010-11-02 00:05:05 +00001039 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001040 unsigned Align = 0;
1041
1042 switch (Imm.getImm()) {
1043 default: break;
1044 case 2:
1045 case 4:
1046 case 8: Align = 0x01; break;
1047 case 16: Align = 0x02; break;
1048 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001049 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001050
Owen Andersond9aa7d32010-11-02 00:05:05 +00001051 return RegNo | (Align << 4);
1052}
1053
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001054/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1055/// alignment operand for use in VLD-dup instructions. This is the same as
1056/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1057/// different for VLD4-dup.
1058unsigned ARMMCCodeEmitter::
1059getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1060 SmallVectorImpl<MCFixup> &Fixups) const {
1061 const MCOperand &Reg = MI.getOperand(Op);
1062 const MCOperand &Imm = MI.getOperand(Op + 1);
1063
1064 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1065 unsigned Align = 0;
1066
1067 switch (Imm.getImm()) {
1068 default: break;
1069 case 2:
1070 case 4:
1071 case 8: Align = 0x01; break;
1072 case 16: Align = 0x03; break;
1073 }
1074
1075 return RegNo | (Align << 4);
1076}
1077
Jim Grosbach806e80e2010-11-03 23:52:49 +00001078unsigned ARMMCCodeEmitter::
1079getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1080 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001081 const MCOperand &MO = MI.getOperand(Op);
1082 if (MO.getReg() == 0) return 0x0D;
1083 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001084}
1085
Jim Grosbach568eeed2010-09-17 18:46:17 +00001086void ARMMCCodeEmitter::
1087EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001088 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001089 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001090 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001091 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001092 uint64_t TSFlags = Desc.TSFlags;
1093 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001094 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001095 int Size;
1096 // Basic size info comes from the TSFlags field.
1097 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1098 default: llvm_unreachable("Unexpected instruction size!");
1099 case ARMII::Size2Bytes: Size = 2; break;
1100 case ARMII::Size4Bytes: Size = 4; break;
1101 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001102 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1103 // Thumb 32-bit wide instructions need to be have the high order halfword
1104 // emitted first.
1105 if (Subtarget.isThumb() && Size == 4) {
1106 EmitConstant(Binary >> 16, 2, OS);
1107 EmitConstant(Binary & 0xffff, 2, OS);
1108 } else
1109 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001110 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001111}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001112
Jim Grosbach806e80e2010-11-03 23:52:49 +00001113#include "ARMGenMCCodeEmitter.inc"