blob: 1efcae03a4f7aea9f8a6daa9d104f1068fc7d8d2 [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#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
810 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
811 "Unexpected base register!");
812#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000813 // The immediate is already shifted for the implicit zeroes, so no change
814 // here.
815 return MO1.getImm() & 0xff;
816}
817
Bill Wendlingf4caf692010-12-14 03:36:38 +0000818/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000819uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000820getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000821 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000822 // [Rn, #imm]
823 // {7-3} = imm5
824 // {2-0} = Rn
825 const MCOperand &MO = MI.getOperand(OpIdx);
826 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000827 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling22447ae2010-12-15 08:51:02 +0000828 unsigned Imm5 = 0;
829
830 if (MO1.isExpr()) {
831 const MCExpr *Expr = MO.getExpr();
832 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_thumb_ldst);
833 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
834 } else {
835 Imm5 = MO1.getImm();
836 }
837
Bill Wendling272df512010-12-09 21:49:07 +0000838 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000839}
840
Bill Wendlingb8958b02010-12-08 01:57:09 +0000841/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
842uint32_t ARMMCCodeEmitter::
843getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
844 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000845 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000846}
847
Jim Grosbach5177f792010-12-01 21:09:40 +0000848/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000849uint32_t ARMMCCodeEmitter::
850getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
851 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000852 // {12-9} = reg
853 // {8} = (U)nsigned (add == '1', sub == '0')
854 // {7-0} = imm8
855 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000856 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000857 // If The first operand isn't a register, we have a label reference.
858 const MCOperand &MO = MI.getOperand(OpIdx);
859 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000860 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000861 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000862 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000863
864 assert(MO.isExpr() && "Unexpected machine operand type!");
865 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000866 MCFixupKind Kind;
867 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
868 if (Subtarget.isThumb2())
869 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
870 else
871 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000872 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
873
874 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000875 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000876 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000877 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
878 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000879
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000880 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
881 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000882 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000883 Binary |= (1 << 8);
884 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000885 return Binary;
886}
887
Jim Grosbach806e80e2010-11-03 23:52:49 +0000888unsigned ARMMCCodeEmitter::
889getSORegOpValue(const MCInst &MI, unsigned OpIdx,
890 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000891 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
892 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
893 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000894 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000895 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000896 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000897 // {6-5} = type
898 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000899 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000900 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000901 // else (imm shift)
902 // {11-7} = imm
903
904 const MCOperand &MO = MI.getOperand(OpIdx);
905 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
906 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
907 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
908
909 // Encode Rm.
910 unsigned Binary = getARMRegisterNumbering(MO.getReg());
911
912 // Encode the shift opcode.
913 unsigned SBits = 0;
914 unsigned Rs = MO1.getReg();
915 if (Rs) {
916 // Set shift operand (bit[7:4]).
917 // LSL - 0001
918 // LSR - 0011
919 // ASR - 0101
920 // ROR - 0111
921 // RRX - 0110 and bit[11:8] clear.
922 switch (SOpc) {
923 default: llvm_unreachable("Unknown shift opc!");
924 case ARM_AM::lsl: SBits = 0x1; break;
925 case ARM_AM::lsr: SBits = 0x3; break;
926 case ARM_AM::asr: SBits = 0x5; break;
927 case ARM_AM::ror: SBits = 0x7; break;
928 case ARM_AM::rrx: SBits = 0x6; break;
929 }
930 } else {
931 // Set shift operand (bit[6:4]).
932 // LSL - 000
933 // LSR - 010
934 // ASR - 100
935 // ROR - 110
936 switch (SOpc) {
937 default: llvm_unreachable("Unknown shift opc!");
938 case ARM_AM::lsl: SBits = 0x0; break;
939 case ARM_AM::lsr: SBits = 0x2; break;
940 case ARM_AM::asr: SBits = 0x4; break;
941 case ARM_AM::ror: SBits = 0x6; break;
942 }
943 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000944
Jim Grosbachef324d72010-10-12 23:53:58 +0000945 Binary |= SBits << 4;
946 if (SOpc == ARM_AM::rrx)
947 return Binary;
948
949 // Encode the shift operation Rs or shift_imm (except rrx).
950 if (Rs) {
951 // Encode Rs bit[11:8].
952 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
953 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
954 }
955
956 // Encode shift_imm bit[11:7].
957 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
958}
959
Jim Grosbach806e80e2010-11-03 23:52:49 +0000960unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000961getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
962 SmallVectorImpl<MCFixup> &Fixups) const {
963 const MCOperand &MO1 = MI.getOperand(OpNum);
964 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000965 const MCOperand &MO3 = MI.getOperand(OpNum+2);
966
Owen Anderson75579f72010-11-29 22:44:32 +0000967 // Encoded as [Rn, Rm, imm].
968 // FIXME: Needs fixup support.
969 unsigned Value = getARMRegisterNumbering(MO1.getReg());
970 Value <<= 4;
971 Value |= getARMRegisterNumbering(MO2.getReg());
972 Value <<= 2;
973 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000974
Owen Anderson75579f72010-11-29 22:44:32 +0000975 return Value;
976}
977
978unsigned ARMMCCodeEmitter::
979getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
980 SmallVectorImpl<MCFixup> &Fixups) const {
981 const MCOperand &MO1 = MI.getOperand(OpNum);
982 const MCOperand &MO2 = MI.getOperand(OpNum+1);
983
984 // FIXME: Needs fixup support.
985 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000986
Owen Anderson75579f72010-11-29 22:44:32 +0000987 // Even though the immediate is 8 bits long, we need 9 bits in order
988 // to represent the (inverse of the) sign bit.
989 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000990 int32_t tmp = (int32_t)MO2.getImm();
991 if (tmp < 0)
992 tmp = abs(tmp);
993 else
994 Value |= 256; // Set the ADD bit
995 Value |= tmp & 255;
996 return Value;
997}
998
999unsigned ARMMCCodeEmitter::
1000getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1001 SmallVectorImpl<MCFixup> &Fixups) const {
1002 const MCOperand &MO1 = MI.getOperand(OpNum);
1003
1004 // FIXME: Needs fixup support.
1005 unsigned Value = 0;
1006 int32_t tmp = (int32_t)MO1.getImm();
1007 if (tmp < 0)
1008 tmp = abs(tmp);
1009 else
1010 Value |= 256; // Set the ADD bit
1011 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +00001012 return Value;
1013}
1014
1015unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001016getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1017 SmallVectorImpl<MCFixup> &Fixups) const {
1018 const MCOperand &MO1 = MI.getOperand(OpNum);
1019
1020 // FIXME: Needs fixup support.
1021 unsigned Value = 0;
1022 int32_t tmp = (int32_t)MO1.getImm();
1023 if (tmp < 0)
1024 tmp = abs(tmp);
1025 else
1026 Value |= 4096; // Set the ADD bit
1027 Value |= tmp & 4095;
1028 return Value;
1029}
1030
1031unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001032getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1033 SmallVectorImpl<MCFixup> &Fixups) const {
1034 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1035 // shifted. The second is the amount to shift by.
1036 //
1037 // {3-0} = Rm.
1038 // {4} = 0
1039 // {6-5} = type
1040 // {11-7} = imm
1041
1042 const MCOperand &MO = MI.getOperand(OpIdx);
1043 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1044 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1045
1046 // Encode Rm.
1047 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1048
1049 // Encode the shift opcode.
1050 unsigned SBits = 0;
1051 // Set shift operand (bit[6:4]).
1052 // LSL - 000
1053 // LSR - 010
1054 // ASR - 100
1055 // ROR - 110
1056 switch (SOpc) {
1057 default: llvm_unreachable("Unknown shift opc!");
1058 case ARM_AM::lsl: SBits = 0x0; break;
1059 case ARM_AM::lsr: SBits = 0x2; break;
1060 case ARM_AM::asr: SBits = 0x4; break;
1061 case ARM_AM::ror: SBits = 0x6; break;
1062 }
1063
1064 Binary |= SBits << 4;
1065 if (SOpc == ARM_AM::rrx)
1066 return Binary;
1067
1068 // Encode shift_imm bit[11:7].
1069 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1070}
1071
1072unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001073getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1074 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001075 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1076 // msb of the mask.
1077 const MCOperand &MO = MI.getOperand(Op);
1078 uint32_t v = ~MO.getImm();
1079 uint32_t lsb = CountTrailingZeros_32(v);
1080 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1081 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1082 return lsb | (msb << 5);
1083}
1084
Jim Grosbach806e80e2010-11-03 23:52:49 +00001085unsigned ARMMCCodeEmitter::
1086getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001087 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001088 // VLDM/VSTM:
1089 // {12-8} = Vd
1090 // {7-0} = Number of registers
1091 //
1092 // LDM/STM:
1093 // {15-0} = Bitfield of GPRs.
1094 unsigned Reg = MI.getOperand(Op).getReg();
1095 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1096 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1097
Bill Wendling5e559a22010-11-09 00:30:18 +00001098 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001099
1100 if (SPRRegs || DPRRegs) {
1101 // VLDM/VSTM
1102 unsigned RegNo = getARMRegisterNumbering(Reg);
1103 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1104 Binary |= (RegNo & 0x1f) << 8;
1105 if (SPRRegs)
1106 Binary |= NumRegs;
1107 else
1108 Binary |= NumRegs * 2;
1109 } else {
1110 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1111 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1112 Binary |= 1 << RegNo;
1113 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001114 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001115
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001116 return Binary;
1117}
1118
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001119/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1120/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001121unsigned ARMMCCodeEmitter::
1122getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1123 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001124 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001125 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001126
Owen Andersond9aa7d32010-11-02 00:05:05 +00001127 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001128 unsigned Align = 0;
1129
1130 switch (Imm.getImm()) {
1131 default: break;
1132 case 2:
1133 case 4:
1134 case 8: Align = 0x01; break;
1135 case 16: Align = 0x02; break;
1136 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001137 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001138
Owen Andersond9aa7d32010-11-02 00:05:05 +00001139 return RegNo | (Align << 4);
1140}
1141
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001142/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1143/// alignment operand for use in VLD-dup instructions. This is the same as
1144/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1145/// different for VLD4-dup.
1146unsigned ARMMCCodeEmitter::
1147getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1148 SmallVectorImpl<MCFixup> &Fixups) const {
1149 const MCOperand &Reg = MI.getOperand(Op);
1150 const MCOperand &Imm = MI.getOperand(Op + 1);
1151
1152 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1153 unsigned Align = 0;
1154
1155 switch (Imm.getImm()) {
1156 default: break;
1157 case 2:
1158 case 4:
1159 case 8: Align = 0x01; break;
1160 case 16: Align = 0x03; break;
1161 }
1162
1163 return RegNo | (Align << 4);
1164}
1165
Jim Grosbach806e80e2010-11-03 23:52:49 +00001166unsigned ARMMCCodeEmitter::
1167getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1168 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001169 const MCOperand &MO = MI.getOperand(Op);
1170 if (MO.getReg() == 0) return 0x0D;
1171 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001172}
1173
Jim Grosbach568eeed2010-09-17 18:46:17 +00001174void ARMMCCodeEmitter::
1175EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001176 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001177 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001178 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001179 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001180 uint64_t TSFlags = Desc.TSFlags;
1181 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001182 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001183 int Size;
1184 // Basic size info comes from the TSFlags field.
1185 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1186 default: llvm_unreachable("Unexpected instruction size!");
1187 case ARMII::Size2Bytes: Size = 2; break;
1188 case ARMII::Size4Bytes: Size = 4; break;
1189 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001190 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1191 // Thumb 32-bit wide instructions need to be have the high order halfword
1192 // emitted first.
1193 if (Subtarget.isThumb() && Size == 4) {
1194 EmitConstant(Binary >> 16, 2, OS);
1195 EmitConstant(Binary & 0xffff, 2, OS);
1196 } else
1197 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001198 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001199}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001200
Jim Grosbach806e80e2010-11-03 23:52:49 +00001201#include "ARMGenMCCodeEmitter.inc"