blob: 8a779cebb952e607f2676c574353c116f4476e7a [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
35 MCContext &Ctx;
36
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Bill Wendling1591b292010-12-10 22:37:19 +000048// This table *must* be in the order that the fixup_* kinds are defined in
49// ARMFixupKinds.h.
50//
51// Name Offset (bits) Size (bits) Flags
52{ "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
53{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson47dbd422010-12-15 18:48:27 +000054 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Bill Wendling1591b292010-12-10 22:37:19 +000055{ "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersone2e0f582010-12-10 22:46:47 +000056{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson47dbd422010-12-15 18:48:27 +000057 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jim Grosbach9d04dc52010-12-14 23:47:35 +000058{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson47dbd422010-12-15 18:48:27 +000059 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Bill Wendling1591b292010-12-10 22:37:19 +000060{ "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersona838a252010-12-14 00:36:49 +000061{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
Owen Anderson47dbd422010-12-15 18:48:27 +000062 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Daniel Dunbarabfbac52010-12-14 17:37:16 +000063{ "fixup_arm_branch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonc2666002010-12-13 19:31:11 +000064{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
65{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000066{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
67{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
68{ "fixup_arm_thumb_blx", 7, 21, MCFixupKindInfo::FKF_IsPCRel },
69{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
70{ "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling22447ae2010-12-15 08:51:02 +000071{ "fixup_arm_thumb_ldst", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling1591b292010-12-10 22:37:19 +000072{ "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
73{ "fixup_arm_movt_hi16", 0, 16, 0 },
74{ "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000075 };
76
77 if (Kind < FirstTargetFixupKind)
78 return MCCodeEmitter::getFixupKindInfo(Kind);
79
80 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
81 "Invalid kind!");
82 return Infos[Kind - FirstTargetFixupKind];
83 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000084 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
85
Jim Grosbach9af82ba2010-10-07 21:57:55 +000086 // getBinaryCodeForInstr - TableGen'erated function for getting the
87 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000088 unsigned getBinaryCodeForInstr(const MCInst &MI,
89 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000090
91 /// getMachineOpValue - Return binary encoding of operand. If the machine
92 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000093 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
94 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000095
Jason W Kim837caa92010-11-18 23:37:15 +000096 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
97 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const;
99
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000100 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +0000101 unsigned &Reg, unsigned &Imm,
102 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000103
Jim Grosbach662a8162010-12-06 23:57:07 +0000104 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +0000105 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000106 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Bill Wendling09aa3f02010-12-09 00:39:08 +0000109 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
110 /// BLX branch target.
111 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups) const;
113
Jim Grosbache2467172010-12-10 18:21:33 +0000114 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
115 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
116 SmallVectorImpl<MCFixup> &Fixups) const;
117
Jim Grosbach01086452010-12-10 17:13:40 +0000118 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
119 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
121
Jim Grosbach027d6e82010-12-09 19:04:53 +0000122 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
123 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000124 SmallVectorImpl<MCFixup> &Fixups) const;
125
Jim Grosbachc466b932010-11-11 18:04:49 +0000126 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
127 /// branch target.
128 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const;
130
Owen Andersonc2666002010-12-13 19:31:11 +0000131 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
132 /// immediate Thumb2 direct branch target.
133 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
135
136
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000137 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
138 /// ADR label target.
139 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
140 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000141 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000143 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
144 SmallVectorImpl<MCFixup> &Fixups) const;
145
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000146
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000147 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
148 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000149 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
150 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000151
Bill Wendlingf4caf692010-12-14 03:36:38 +0000152 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
153 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
154 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000155
Owen Anderson9d63d902010-12-01 19:18:46 +0000156 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
157 /// operand.
158 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
160
161
Jim Grosbach54fea632010-11-09 17:20:53 +0000162 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
163 /// operand as needed by load/store instructions.
164 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
165 SmallVectorImpl<MCFixup> &Fixups) const;
166
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000167 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
168 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
171 switch (Mode) {
172 default: assert(0 && "Unknown addressing sub-mode!");
173 case ARM_AM::da: return 0;
174 case ARM_AM::ia: return 1;
175 case ARM_AM::db: return 2;
176 case ARM_AM::ib: return 3;
177 }
178 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000179 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
180 ///
181 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
182 switch (ShOpc) {
183 default: llvm_unreachable("Unknown shift opc!");
184 case ARM_AM::no_shift:
185 case ARM_AM::lsl: return 0;
186 case ARM_AM::lsr: return 1;
187 case ARM_AM::asr: return 2;
188 case ARM_AM::ror:
189 case ARM_AM::rrx: return 3;
190 }
191 return 0;
192 }
193
194 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
195 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
196 SmallVectorImpl<MCFixup> &Fixups) const;
197
198 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
199 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
200 SmallVectorImpl<MCFixup> &Fixups) const;
201
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000202 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
203 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
204 SmallVectorImpl<MCFixup> &Fixups) const;
205
Jim Grosbach570a9222010-11-11 01:09:40 +0000206 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
207 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
208 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000209
Jim Grosbachd967cd02010-12-07 21:50:47 +0000210 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
211 /// operand.
212 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
213 SmallVectorImpl<MCFixup> &Fixups) const;
214
Bill Wendlingf4caf692010-12-14 03:36:38 +0000215 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
216 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000217 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000218
Bill Wendlingb8958b02010-12-08 01:57:09 +0000219 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
220 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
221 SmallVectorImpl<MCFixup> &Fixups) const;
222
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000223 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000224 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
225 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000226
Jim Grosbach08bd5492010-10-12 23:00:24 +0000227 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000228 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
229 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000230 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
231 // '1' respectively.
232 return MI.getOperand(Op).getReg() == ARM::CPSR;
233 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000234
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000235 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000236 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
237 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000238 unsigned SoImm = MI.getOperand(Op).getImm();
239 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
240 assert(SoImmVal != -1 && "Not a valid so_imm value!");
241
242 // Encode rotate_imm.
243 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
244 << ARMII::SoRotImmShift;
245
246 // Encode immed_8.
247 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
248 return Binary;
249 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000250
Owen Anderson5de6d842010-11-12 21:12:40 +0000251 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
252 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
253 SmallVectorImpl<MCFixup> &Fixups) const {
254 unsigned SoImm = MI.getOperand(Op).getImm();
255 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
256 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
257 return Encoded;
258 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000259
Owen Anderson75579f72010-11-29 22:44:32 +0000260 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
261 SmallVectorImpl<MCFixup> &Fixups) const;
262 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
263 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000264 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000266 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000268
Jim Grosbachef324d72010-10-12 23:53:58 +0000269 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000270 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
271 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000272 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
273 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000274
Jim Grosbach806e80e2010-11-03 23:52:49 +0000275 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
276 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000277 switch (MI.getOperand(Op).getImm()) {
278 default: assert (0 && "Not a valid rot_imm value!");
279 case 0: return 0;
280 case 8: return 1;
281 case 16: return 2;
282 case 24: return 3;
283 }
284 }
285
Jim Grosbach806e80e2010-11-03 23:52:49 +0000286 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
287 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000288 return MI.getOperand(Op).getImm() - 1;
289 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000290
Jim Grosbach806e80e2010-11-03 23:52:49 +0000291 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000293 return 64 - MI.getOperand(Op).getImm();
294 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000295
Jim Grosbach806e80e2010-11-03 23:52:49 +0000296 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
297 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000298
Jim Grosbach806e80e2010-11-03 23:52:49 +0000299 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups) const;
301 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
302 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000303 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
304 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000305 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
306 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000307
Owen Andersonc7139a62010-11-11 19:07:48 +0000308 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
309 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000310 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000311 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000312 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000313 unsigned EncodedValue) const;
314
315 unsigned VFPThumb2PostEncoder(const MCInst &MI,
316 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000317
Jim Grosbach70933262010-11-04 01:12:30 +0000318 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000319 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000320 }
321
Jim Grosbach70933262010-11-04 01:12:30 +0000322 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000323 // Output the constant in little endian byte order.
324 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000325 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000326 Val >>= 8;
327 }
328 }
329
Jim Grosbach568eeed2010-09-17 18:46:17 +0000330 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
331 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000332};
333
334} // end anonymous namespace
335
Bill Wendling0800ce72010-11-02 22:53:11 +0000336MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
337 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000338 return new ARMMCCodeEmitter(TM, Ctx);
339}
340
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000341/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
342/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000343/// Thumb2 mode.
344unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
345 unsigned EncodedValue) const {
346 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
347 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000348 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000349 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
350 // set to 1111.
351 unsigned Bit24 = EncodedValue & 0x01000000;
352 unsigned Bit28 = Bit24 << 4;
353 EncodedValue &= 0xEFFFFFFF;
354 EncodedValue |= Bit28;
355 EncodedValue |= 0x0F000000;
356 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000357
Owen Andersonc7139a62010-11-11 19:07:48 +0000358 return EncodedValue;
359}
360
Owen Anderson57dac882010-11-11 21:36:43 +0000361/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000362/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000363/// Thumb2 mode.
364unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
365 unsigned EncodedValue) const {
366 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
367 if (Subtarget.isThumb2()) {
368 EncodedValue &= 0xF0FFFFFF;
369 EncodedValue |= 0x09000000;
370 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000371
Owen Anderson57dac882010-11-11 21:36:43 +0000372 return EncodedValue;
373}
374
Owen Anderson8f143912010-11-11 23:12:55 +0000375/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000376/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000377/// Thumb2 mode.
378unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
379 unsigned EncodedValue) const {
380 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
381 if (Subtarget.isThumb2()) {
382 EncodedValue &= 0x00FFFFFF;
383 EncodedValue |= 0xEE000000;
384 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000385
Owen Anderson8f143912010-11-11 23:12:55 +0000386 return EncodedValue;
387}
388
Bill Wendlingcf590262010-12-01 21:54:50 +0000389/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
390/// them to their Thumb2 form if we are currently in Thumb2 mode.
391unsigned ARMMCCodeEmitter::
392VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
393 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
394 EncodedValue &= 0x0FFFFFFF;
395 EncodedValue |= 0xE0000000;
396 }
397 return EncodedValue;
398}
Owen Anderson57dac882010-11-11 21:36:43 +0000399
Jim Grosbach56ac9072010-10-08 21:45:55 +0000400/// getMachineOpValue - Return binary encoding of operand. If the machine
401/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000402unsigned ARMMCCodeEmitter::
403getMachineOpValue(const MCInst &MI, const MCOperand &MO,
404 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000405 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000406 unsigned Reg = MO.getReg();
407 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000408
Jim Grosbachb0708d22010-11-30 23:51:41 +0000409 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000410 switch (Reg) {
411 default:
412 return RegNo;
413 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
414 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
415 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
416 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
417 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000418 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000419 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000420 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000421 } else if (MO.isFPImm()) {
422 return static_cast<unsigned>(APFloat(MO.getFPImm())
423 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000424 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000425
Jim Grosbach817c1a62010-11-19 00:27:09 +0000426 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000427 return 0;
428}
429
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000430/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000431bool ARMMCCodeEmitter::
432EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
433 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000434 const MCOperand &MO = MI.getOperand(OpIdx);
435 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000436
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000437 Reg = getARMRegisterNumbering(MO.getReg());
438
439 int32_t SImm = MO1.getImm();
440 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000441
Jim Grosbachab682a22010-10-28 18:34:10 +0000442 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000443 if (SImm == INT32_MIN)
444 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000445
Jim Grosbachab682a22010-10-28 18:34:10 +0000446 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000447 if (SImm < 0) {
448 SImm = -SImm;
449 isAdd = false;
450 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000451
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000452 Imm = SImm;
453 return isAdd;
454}
455
Bill Wendlingdff2f712010-12-08 23:01:43 +0000456/// getBranchTargetOpValue - Helper function to get the branch target operand,
457/// which is either an immediate or requires a fixup.
458static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
459 unsigned FixupKind,
460 SmallVectorImpl<MCFixup> &Fixups) {
461 const MCOperand &MO = MI.getOperand(OpIdx);
462
463 // If the destination is an immediate, we have nothing to do.
464 if (MO.isImm()) return MO.getImm();
465 assert(MO.isExpr() && "Unexpected branch target type!");
466 const MCExpr *Expr = MO.getExpr();
467 MCFixupKind Kind = MCFixupKind(FixupKind);
468 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
469
470 // All of the information is in the fixup.
471 return 0;
472}
473
474/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000475uint32_t ARMMCCodeEmitter::
476getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
477 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000478 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000479}
480
Bill Wendling09aa3f02010-12-09 00:39:08 +0000481/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
482/// BLX branch target.
483uint32_t ARMMCCodeEmitter::
484getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
485 SmallVectorImpl<MCFixup> &Fixups) const {
486 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
487}
488
Jim Grosbache2467172010-12-10 18:21:33 +0000489/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
490uint32_t ARMMCCodeEmitter::
491getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
492 SmallVectorImpl<MCFixup> &Fixups) const {
493 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
494}
495
Jim Grosbach01086452010-12-10 17:13:40 +0000496/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
497uint32_t ARMMCCodeEmitter::
498getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000499 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000500 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
501}
502
Jim Grosbach027d6e82010-12-09 19:04:53 +0000503/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000504uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000505getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000506 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000507 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000508}
509
510/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
511/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000512uint32_t ARMMCCodeEmitter::
513getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000514 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000515 // FIXME: This really, really shouldn't use TargetMachine. We don't want
516 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000517 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
518 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000519 return
520 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000521 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000522}
523
Owen Andersonc2666002010-12-13 19:31:11 +0000524/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
525/// immediate branch target.
526uint32_t ARMMCCodeEmitter::
527getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
528 SmallVectorImpl<MCFixup> &Fixups) const {
529 unsigned Val =
530 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
531 bool I = (Val & 0x800000);
532 bool J1 = (Val & 0x400000);
533 bool J2 = (Val & 0x200000);
534 if (I ^ J1)
535 Val &= ~0x400000;
536 else
537 Val |= 0x400000;
538
539 if (I ^ J2)
540 Val &= ~0x200000;
541 else
542 Val |= 0x200000;
543
544 return Val;
545}
546
Bill Wendlingdff2f712010-12-08 23:01:43 +0000547/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
548/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000549uint32_t ARMMCCodeEmitter::
550getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
551 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000552 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
553 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
554 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000555}
556
Owen Andersona838a252010-12-14 00:36:49 +0000557/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
558/// target.
559uint32_t ARMMCCodeEmitter::
560getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
561 SmallVectorImpl<MCFixup> &Fixups) const {
562 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
563 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
564 Fixups);
565}
566
Jim Grosbachd40963c2010-12-14 22:28:03 +0000567/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
568/// target.
569uint32_t ARMMCCodeEmitter::
570getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
571 SmallVectorImpl<MCFixup> &Fixups) const {
572 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
573 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
574 Fixups);
575}
576
Bill Wendlingf4caf692010-12-14 03:36:38 +0000577/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
578/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000579uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000580getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
581 SmallVectorImpl<MCFixup> &) const {
582 // [Rn, Rm]
583 // {5-3} = Rm
584 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000585 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000586 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000587 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
588 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
589 return (Rm << 3) | Rn;
590}
591
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000592/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000593uint32_t ARMMCCodeEmitter::
594getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000596 // {17-13} = reg
597 // {12} = (U)nsigned (add == '1', sub == '0')
598 // {11-0} = imm12
599 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000600 bool isAdd = true;
601 // If The first operand isn't a register, we have a label reference.
602 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000603 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
604 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000605 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000606 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000607 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000608
Owen Andersoneb6779c2010-12-07 00:45:21 +0000609 const MCExpr *Expr = 0;
610 if (!MO.isReg())
611 Expr = MO.getExpr();
612 else
613 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000614
Owen Andersond7b3f582010-12-09 01:51:07 +0000615 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
616 MCFixupKind Kind;
617 if (Subtarget.isThumb2())
618 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
619 else
620 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000621 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
622
623 ++MCNumCPRelocations;
624 } else
625 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000626
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000627 uint32_t Binary = Imm12 & 0xfff;
628 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000629 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000630 Binary |= (1 << 12);
631 Binary |= (Reg << 13);
632 return Binary;
633}
634
Owen Anderson9d63d902010-12-01 19:18:46 +0000635/// getT2AddrModeImm8s4OpValue - Return encoding info for
636/// 'reg +/- imm8<<2' operand.
637uint32_t ARMMCCodeEmitter::
638getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
639 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000640 // {12-9} = reg
641 // {8} = (U)nsigned (add == '1', sub == '0')
642 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000643 unsigned Reg, Imm8;
644 bool isAdd = true;
645 // If The first operand isn't a register, we have a label reference.
646 const MCOperand &MO = MI.getOperand(OpIdx);
647 if (!MO.isReg()) {
648 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
649 Imm8 = 0;
650 isAdd = false ; // 'U' bit is set as part of the fixup.
651
652 assert(MO.isExpr() && "Unexpected machine operand type!");
653 const MCExpr *Expr = MO.getExpr();
654 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
655 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
656
657 ++MCNumCPRelocations;
658 } else
659 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
660
661 uint32_t Binary = (Imm8 >> 2) & 0xff;
662 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
663 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000664 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000665 Binary |= (Reg << 9);
666 return Binary;
667}
668
Jim Grosbach54fea632010-11-09 17:20:53 +0000669uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000670getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
671 SmallVectorImpl<MCFixup> &Fixups) const {
672 // {20-16} = imm{15-12}
673 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000674 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000675 if (MO.isImm()) {
676 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000677 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000678 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
679 MCFixupKind Kind;
680 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000681 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000682 case MCSymbolRefExpr::VK_ARM_HI16:
683 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
684 break;
685 case MCSymbolRefExpr::VK_ARM_LO16:
686 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
687 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000688 }
689 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
690 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000691 };
692 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000693 return 0;
694}
695
696uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000697getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
698 SmallVectorImpl<MCFixup> &Fixups) const {
699 const MCOperand &MO = MI.getOperand(OpIdx);
700 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
701 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
702 unsigned Rn = getARMRegisterNumbering(MO.getReg());
703 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000704 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
705 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000706 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
707 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000708
709 // {16-13} = Rn
710 // {12} = isAdd
711 // {11-0} = shifter
712 // {3-0} = Rm
713 // {4} = 0
714 // {6-5} = type
715 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000716 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000717 Binary |= Rn << 13;
718 Binary |= SBits << 5;
719 Binary |= ShImm << 7;
720 if (isAdd)
721 Binary |= 1 << 12;
722 return Binary;
723}
724
Jim Grosbach570a9222010-11-11 01:09:40 +0000725uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000726getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
727 SmallVectorImpl<MCFixup> &Fixups) const {
728 // {17-14} Rn
729 // {13} 1 == imm12, 0 == Rm
730 // {12} isAdd
731 // {11-0} imm12/Rm
732 const MCOperand &MO = MI.getOperand(OpIdx);
733 unsigned Rn = getARMRegisterNumbering(MO.getReg());
734 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
735 Binary |= Rn << 14;
736 return Binary;
737}
738
739uint32_t ARMMCCodeEmitter::
740getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
741 SmallVectorImpl<MCFixup> &Fixups) const {
742 // {13} 1 == imm12, 0 == Rm
743 // {12} isAdd
744 // {11-0} imm12/Rm
745 const MCOperand &MO = MI.getOperand(OpIdx);
746 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
747 unsigned Imm = MO1.getImm();
748 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
749 bool isReg = MO.getReg() != 0;
750 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
751 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
752 if (isReg) {
753 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
754 Binary <<= 7; // Shift amount is bits [11:7]
755 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
756 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
757 }
758 return Binary | (isAdd << 12) | (isReg << 13);
759}
760
761uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000762getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
763 SmallVectorImpl<MCFixup> &Fixups) const {
764 // {9} 1 == imm8, 0 == Rm
765 // {8} isAdd
766 // {7-4} imm7_4/zero
767 // {3-0} imm3_0/Rm
768 const MCOperand &MO = MI.getOperand(OpIdx);
769 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
770 unsigned Imm = MO1.getImm();
771 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
772 bool isImm = MO.getReg() == 0;
773 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
774 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
775 if (!isImm)
776 Imm8 = getARMRegisterNumbering(MO.getReg());
777 return Imm8 | (isAdd << 8) | (isImm << 9);
778}
779
780uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000781getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
782 SmallVectorImpl<MCFixup> &Fixups) const {
783 // {13} 1 == imm8, 0 == Rm
784 // {12-9} Rn
785 // {8} isAdd
786 // {7-4} imm7_4/zero
787 // {3-0} imm3_0/Rm
788 const MCOperand &MO = MI.getOperand(OpIdx);
789 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
790 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
791 unsigned Rn = getARMRegisterNumbering(MO.getReg());
792 unsigned Imm = MO2.getImm();
793 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
794 bool isImm = MO1.getReg() == 0;
795 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
796 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
797 if (!isImm)
798 Imm8 = getARMRegisterNumbering(MO1.getReg());
799 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
800}
801
Bill Wendlingb8958b02010-12-08 01:57:09 +0000802/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000803uint32_t ARMMCCodeEmitter::
804getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
805 SmallVectorImpl<MCFixup> &Fixups) const {
806 // [SP, #imm]
807 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000808 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000809 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
810 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000811
Jim Grosbachd967cd02010-12-07 21:50:47 +0000812 // The immediate is already shifted for the implicit zeroes, so no change
813 // here.
814 return MO1.getImm() & 0xff;
815}
816
Bill Wendlingf4caf692010-12-14 03:36:38 +0000817/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000818uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000819getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000820 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000821 // [Rn, #imm]
822 // {7-3} = imm5
823 // {2-0} = Rn
824 const MCOperand &MO = MI.getOperand(OpIdx);
825 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000826 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling22447ae2010-12-15 08:51:02 +0000827 unsigned Imm5 = 0;
828
829 if (MO1.isExpr()) {
830 const MCExpr *Expr = MO.getExpr();
831 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_ldst);
832 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
833 } else {
834 Imm5 = MO1.getImm();
835 }
836
Bill Wendling272df512010-12-09 21:49:07 +0000837 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000838}
839
Bill Wendlingb8958b02010-12-08 01:57:09 +0000840/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
841uint32_t ARMMCCodeEmitter::
842getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
843 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000844 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000845}
846
Jim Grosbach5177f792010-12-01 21:09:40 +0000847/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000848uint32_t ARMMCCodeEmitter::
849getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
850 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000851 // {12-9} = reg
852 // {8} = (U)nsigned (add == '1', sub == '0')
853 // {7-0} = imm8
854 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000855 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000856 // If The first operand isn't a register, we have a label reference.
857 const MCOperand &MO = MI.getOperand(OpIdx);
858 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000859 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000860 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000861 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000862
863 assert(MO.isExpr() && "Unexpected machine operand type!");
864 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000865 MCFixupKind Kind;
866 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
867 if (Subtarget.isThumb2())
868 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
869 else
870 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000871 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
872
873 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000874 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000875 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000876 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
877 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000878
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000879 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
880 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000881 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000882 Binary |= (1 << 8);
883 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000884 return Binary;
885}
886
Jim Grosbach806e80e2010-11-03 23:52:49 +0000887unsigned ARMMCCodeEmitter::
888getSORegOpValue(const MCInst &MI, unsigned OpIdx,
889 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000890 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
891 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
892 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000893 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000894 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000895 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000896 // {6-5} = type
897 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000898 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000899 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000900 // else (imm shift)
901 // {11-7} = imm
902
903 const MCOperand &MO = MI.getOperand(OpIdx);
904 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
905 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
906 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
907
908 // Encode Rm.
909 unsigned Binary = getARMRegisterNumbering(MO.getReg());
910
911 // Encode the shift opcode.
912 unsigned SBits = 0;
913 unsigned Rs = MO1.getReg();
914 if (Rs) {
915 // Set shift operand (bit[7:4]).
916 // LSL - 0001
917 // LSR - 0011
918 // ASR - 0101
919 // ROR - 0111
920 // RRX - 0110 and bit[11:8] clear.
921 switch (SOpc) {
922 default: llvm_unreachable("Unknown shift opc!");
923 case ARM_AM::lsl: SBits = 0x1; break;
924 case ARM_AM::lsr: SBits = 0x3; break;
925 case ARM_AM::asr: SBits = 0x5; break;
926 case ARM_AM::ror: SBits = 0x7; break;
927 case ARM_AM::rrx: SBits = 0x6; break;
928 }
929 } else {
930 // Set shift operand (bit[6:4]).
931 // LSL - 000
932 // LSR - 010
933 // ASR - 100
934 // ROR - 110
935 switch (SOpc) {
936 default: llvm_unreachable("Unknown shift opc!");
937 case ARM_AM::lsl: SBits = 0x0; break;
938 case ARM_AM::lsr: SBits = 0x2; break;
939 case ARM_AM::asr: SBits = 0x4; break;
940 case ARM_AM::ror: SBits = 0x6; break;
941 }
942 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000943
Jim Grosbachef324d72010-10-12 23:53:58 +0000944 Binary |= SBits << 4;
945 if (SOpc == ARM_AM::rrx)
946 return Binary;
947
948 // Encode the shift operation Rs or shift_imm (except rrx).
949 if (Rs) {
950 // Encode Rs bit[11:8].
951 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
952 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
953 }
954
955 // Encode shift_imm bit[11:7].
956 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
957}
958
Jim Grosbach806e80e2010-11-03 23:52:49 +0000959unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000960getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
961 SmallVectorImpl<MCFixup> &Fixups) const {
962 const MCOperand &MO1 = MI.getOperand(OpNum);
963 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000964 const MCOperand &MO3 = MI.getOperand(OpNum+2);
965
Owen Anderson75579f72010-11-29 22:44:32 +0000966 // Encoded as [Rn, Rm, imm].
967 // FIXME: Needs fixup support.
968 unsigned Value = getARMRegisterNumbering(MO1.getReg());
969 Value <<= 4;
970 Value |= getARMRegisterNumbering(MO2.getReg());
971 Value <<= 2;
972 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000973
Owen Anderson75579f72010-11-29 22:44:32 +0000974 return Value;
975}
976
977unsigned ARMMCCodeEmitter::
978getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
979 SmallVectorImpl<MCFixup> &Fixups) const {
980 const MCOperand &MO1 = MI.getOperand(OpNum);
981 const MCOperand &MO2 = MI.getOperand(OpNum+1);
982
983 // FIXME: Needs fixup support.
984 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000985
Owen Anderson75579f72010-11-29 22:44:32 +0000986 // Even though the immediate is 8 bits long, we need 9 bits in order
987 // to represent the (inverse of the) sign bit.
988 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000989 int32_t tmp = (int32_t)MO2.getImm();
990 if (tmp < 0)
991 tmp = abs(tmp);
992 else
993 Value |= 256; // Set the ADD bit
994 Value |= tmp & 255;
995 return Value;
996}
997
998unsigned ARMMCCodeEmitter::
999getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1000 SmallVectorImpl<MCFixup> &Fixups) const {
1001 const MCOperand &MO1 = MI.getOperand(OpNum);
1002
1003 // FIXME: Needs fixup support.
1004 unsigned Value = 0;
1005 int32_t tmp = (int32_t)MO1.getImm();
1006 if (tmp < 0)
1007 tmp = abs(tmp);
1008 else
1009 Value |= 256; // Set the ADD bit
1010 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001011 return Value;
1012}
1013
1014unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001015getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1016 SmallVectorImpl<MCFixup> &Fixups) const {
1017 const MCOperand &MO1 = MI.getOperand(OpNum);
1018
1019 // FIXME: Needs fixup support.
1020 unsigned Value = 0;
1021 int32_t tmp = (int32_t)MO1.getImm();
1022 if (tmp < 0)
1023 tmp = abs(tmp);
1024 else
1025 Value |= 4096; // Set the ADD bit
1026 Value |= tmp & 4095;
1027 return Value;
1028}
1029
1030unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001031getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1032 SmallVectorImpl<MCFixup> &Fixups) const {
1033 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1034 // shifted. The second is the amount to shift by.
1035 //
1036 // {3-0} = Rm.
1037 // {4} = 0
1038 // {6-5} = type
1039 // {11-7} = imm
1040
1041 const MCOperand &MO = MI.getOperand(OpIdx);
1042 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1043 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1044
1045 // Encode Rm.
1046 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1047
1048 // Encode the shift opcode.
1049 unsigned SBits = 0;
1050 // Set shift operand (bit[6:4]).
1051 // LSL - 000
1052 // LSR - 010
1053 // ASR - 100
1054 // ROR - 110
1055 switch (SOpc) {
1056 default: llvm_unreachable("Unknown shift opc!");
1057 case ARM_AM::lsl: SBits = 0x0; break;
1058 case ARM_AM::lsr: SBits = 0x2; break;
1059 case ARM_AM::asr: SBits = 0x4; break;
1060 case ARM_AM::ror: SBits = 0x6; break;
1061 }
1062
1063 Binary |= SBits << 4;
1064 if (SOpc == ARM_AM::rrx)
1065 return Binary;
1066
1067 // Encode shift_imm bit[11:7].
1068 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1069}
1070
1071unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001072getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1073 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001074 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1075 // msb of the mask.
1076 const MCOperand &MO = MI.getOperand(Op);
1077 uint32_t v = ~MO.getImm();
1078 uint32_t lsb = CountTrailingZeros_32(v);
1079 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1080 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1081 return lsb | (msb << 5);
1082}
1083
Jim Grosbach806e80e2010-11-03 23:52:49 +00001084unsigned ARMMCCodeEmitter::
1085getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001086 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001087 // VLDM/VSTM:
1088 // {12-8} = Vd
1089 // {7-0} = Number of registers
1090 //
1091 // LDM/STM:
1092 // {15-0} = Bitfield of GPRs.
1093 unsigned Reg = MI.getOperand(Op).getReg();
1094 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1095 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1096
Bill Wendling5e559a22010-11-09 00:30:18 +00001097 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001098
1099 if (SPRRegs || DPRRegs) {
1100 // VLDM/VSTM
1101 unsigned RegNo = getARMRegisterNumbering(Reg);
1102 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1103 Binary |= (RegNo & 0x1f) << 8;
1104 if (SPRRegs)
1105 Binary |= NumRegs;
1106 else
1107 Binary |= NumRegs * 2;
1108 } else {
1109 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1110 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1111 Binary |= 1 << RegNo;
1112 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001113 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001114
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001115 return Binary;
1116}
1117
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001118/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1119/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001120unsigned ARMMCCodeEmitter::
1121getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1122 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001123 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001124 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001125
Owen Andersond9aa7d32010-11-02 00:05:05 +00001126 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001127 unsigned Align = 0;
1128
1129 switch (Imm.getImm()) {
1130 default: break;
1131 case 2:
1132 case 4:
1133 case 8: Align = 0x01; break;
1134 case 16: Align = 0x02; break;
1135 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001136 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001137
Owen Andersond9aa7d32010-11-02 00:05:05 +00001138 return RegNo | (Align << 4);
1139}
1140
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001141/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1142/// alignment operand for use in VLD-dup instructions. This is the same as
1143/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1144/// different for VLD4-dup.
1145unsigned ARMMCCodeEmitter::
1146getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1147 SmallVectorImpl<MCFixup> &Fixups) const {
1148 const MCOperand &Reg = MI.getOperand(Op);
1149 const MCOperand &Imm = MI.getOperand(Op + 1);
1150
1151 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1152 unsigned Align = 0;
1153
1154 switch (Imm.getImm()) {
1155 default: break;
1156 case 2:
1157 case 4:
1158 case 8: Align = 0x01; break;
1159 case 16: Align = 0x03; break;
1160 }
1161
1162 return RegNo | (Align << 4);
1163}
1164
Jim Grosbach806e80e2010-11-03 23:52:49 +00001165unsigned ARMMCCodeEmitter::
1166getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1167 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001168 const MCOperand &MO = MI.getOperand(Op);
1169 if (MO.getReg() == 0) return 0x0D;
1170 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001171}
1172
Jim Grosbach568eeed2010-09-17 18:46:17 +00001173void ARMMCCodeEmitter::
1174EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001175 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001176 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001177 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001178 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001179 uint64_t TSFlags = Desc.TSFlags;
1180 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001181 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001182 int Size;
1183 // Basic size info comes from the TSFlags field.
1184 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1185 default: llvm_unreachable("Unexpected instruction size!");
1186 case ARMII::Size2Bytes: Size = 2; break;
1187 case ARMII::Size4Bytes: Size = 4; break;
1188 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001189 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1190 // Thumb 32-bit wide instructions need to be have the high order halfword
1191 // emitted first.
1192 if (Subtarget.isThumb() && Size == 4) {
1193 EmitConstant(Binary >> 16, 2, OS);
1194 EmitConstant(Binary & 0xffff, 2, OS);
1195 } else
1196 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001197 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001198}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001199
Jim Grosbach806e80e2010-11-03 23:52:49 +00001200#include "ARMGenMCCodeEmitter.inc"