blob: e4f3288c82d70bb581101f1d70f0797b9f87d5a4 [file] [log] [blame]
Jim Grosbach568eeed2010-09-17 18:46:17 +00001//===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ARMMCCodeEmitter class.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2ac19022010-11-15 05:19:05 +000014#define DEBUG_TYPE "mccodeemitter"
Jim Grosbach568eeed2010-09-17 18:46:17 +000015#include "ARM.h"
Jim Grosbach42fac8e2010-10-11 23:16:21 +000016#include "ARMAddressingModes.h"
Jim Grosbach70933262010-11-04 01:12:30 +000017#include "ARMFixupKinds.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000018#include "ARMInstrInfo.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000022#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
Jim Grosbach70933262010-11-04 01:12:30 +000026STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
27STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000028
Jim Grosbach568eeed2010-09-17 18:46:17 +000029namespace {
30class ARMMCCodeEmitter : public MCCodeEmitter {
31 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
32 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 const TargetMachine &TM;
34 const TargetInstrInfo &TII;
35 MCContext &Ctx;
36
37public:
38 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000040 }
41
42 ~ARMMCCodeEmitter() {}
43
Jim Grosbachc466b932010-11-11 18:04:49 +000044 unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; }
Jim Grosbach70933262010-11-04 01:12:30 +000045
46 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo Infos[] = {
Jim Grosbachdff84b02010-12-02 00:28:45 +000048 // name off bits flags
49 { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Anderson05018c22010-12-09 20:27:52 +000050 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
51 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbachdff84b02010-12-02 00:28:45 +000052 { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersond8e351b2010-12-08 00:18:36 +000053 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000054 { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
55 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
Owen Andersonfe7fac72010-12-09 21:34:47 +000056 { "fixup_t2_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
57 MCFixupKindInfo::FKF_IsAligned},
Jim Grosbach662a8162010-12-06 23:57:07 +000058 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendling09aa3f02010-12-09 00:39:08 +000059 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachb492a7c2010-12-09 19:50:12 +000060 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Bill Wendlingb8958b02010-12-08 01:57:09 +000061 { "fixup_arm_thumb_cp", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbache2467172010-12-10 18:21:33 +000062 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach01086452010-12-10 17:13:40 +000063 { "fixup_arm_thumb_bcc", 1, 8, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachdff84b02010-12-02 00:28:45 +000064 { "fixup_arm_movt_hi16", 0, 16, 0 },
65 { "fixup_arm_movw_lo16", 0, 16, 0 },
Jim Grosbach70933262010-11-04 01:12:30 +000066 };
67
68 if (Kind < FirstTargetFixupKind)
69 return MCCodeEmitter::getFixupKindInfo(Kind);
70
71 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
72 "Invalid kind!");
73 return Infos[Kind - FirstTargetFixupKind];
74 }
Jim Grosbach0de6ab32010-10-12 17:11:26 +000075 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
76
Jim Grosbach9af82ba2010-10-07 21:57:55 +000077 // getBinaryCodeForInstr - TableGen'erated function for getting the
78 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000079 unsigned getBinaryCodeForInstr(const MCInst &MI,
80 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000081
82 /// getMachineOpValue - Return binary encoding of operand. If the machine
83 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000084 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
85 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000086
Jason W Kim837caa92010-11-18 23:37:15 +000087 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
88 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
90
Bill Wendling92b5a2e2010-11-03 01:49:29 +000091 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000092 unsigned &Reg, unsigned &Imm,
93 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000094
Jim Grosbach662a8162010-12-06 23:57:07 +000095 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000096 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000097 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
98 SmallVectorImpl<MCFixup> &Fixups) const;
99
Bill Wendling09aa3f02010-12-09 00:39:08 +0000100 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
101 /// BLX branch target.
102 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
104
Jim Grosbache2467172010-12-10 18:21:33 +0000105 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
106 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbach01086452010-12-10 17:13:40 +0000109 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
110 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
111 SmallVectorImpl<MCFixup> &Fixups) const;
112
Jim Grosbach027d6e82010-12-09 19:04:53 +0000113 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000115 SmallVectorImpl<MCFixup> &Fixups) const;
116
Jim Grosbachc466b932010-11-11 18:04:49 +0000117 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
118 /// branch target.
119 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
120 SmallVectorImpl<MCFixup> &Fixups) const;
121
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000122 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
123 /// ADR label target.
124 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000127 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
128 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000129 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000131
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000132 /// getTAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
133 uint32_t getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
134 SmallVectorImpl<MCFixup> &Fixups) const;
135
Owen Anderson9d63d902010-12-01 19:18:46 +0000136 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
137 /// operand.
138 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups) const;
140
141
Jim Grosbach54fea632010-11-09 17:20:53 +0000142 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
143 /// operand as needed by load/store instructions.
144 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups) const;
146
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000147 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
148 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
149 SmallVectorImpl<MCFixup> &Fixups) const {
150 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
151 switch (Mode) {
152 default: assert(0 && "Unknown addressing sub-mode!");
153 case ARM_AM::da: return 0;
154 case ARM_AM::ia: return 1;
155 case ARM_AM::db: return 2;
156 case ARM_AM::ib: return 3;
157 }
158 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000159 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
160 ///
161 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
162 switch (ShOpc) {
163 default: llvm_unreachable("Unknown shift opc!");
164 case ARM_AM::no_shift:
165 case ARM_AM::lsl: return 0;
166 case ARM_AM::lsr: return 1;
167 case ARM_AM::asr: return 2;
168 case ARM_AM::ror:
169 case ARM_AM::rrx: return 3;
170 }
171 return 0;
172 }
173
174 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
175 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
177
178 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
179 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
180 SmallVectorImpl<MCFixup> &Fixups) const;
181
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000182 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
183 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
Jim Grosbach570a9222010-11-11 01:09:40 +0000186 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
187 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000189
Jim Grosbachd967cd02010-12-07 21:50:47 +0000190 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
191 /// operand.
192 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups) const;
194
Bill Wendling272df512010-12-09 21:49:07 +0000195 /// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
196 uint32_t getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
197 SmallVectorImpl<MCFixup> &) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000198
Bill Wendlingb8958b02010-12-08 01:57:09 +0000199 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
200 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
201 SmallVectorImpl<MCFixup> &Fixups) const;
202
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000203 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000204 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
205 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000206
Jim Grosbach08bd5492010-10-12 23:00:24 +0000207 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000208 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
209 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000210 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
211 // '1' respectively.
212 return MI.getOperand(Op).getReg() == ARM::CPSR;
213 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000214
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000215 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000216 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
217 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000218 unsigned SoImm = MI.getOperand(Op).getImm();
219 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
220 assert(SoImmVal != -1 && "Not a valid so_imm value!");
221
222 // Encode rotate_imm.
223 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
224 << ARMII::SoRotImmShift;
225
226 // Encode immed_8.
227 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
228 return Binary;
229 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000230
Owen Anderson5de6d842010-11-12 21:12:40 +0000231 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
232 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const {
234 unsigned SoImm = MI.getOperand(Op).getImm();
235 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
236 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
237 return Encoded;
238 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000239
Owen Anderson75579f72010-11-29 22:44:32 +0000240 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
241 SmallVectorImpl<MCFixup> &Fixups) const;
242 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
243 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000244 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
245 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000246 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
247 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000248
Jim Grosbachef324d72010-10-12 23:53:58 +0000249 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000250 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
251 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000252 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
253 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000254
Jim Grosbach806e80e2010-11-03 23:52:49 +0000255 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
256 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000257 switch (MI.getOperand(Op).getImm()) {
258 default: assert (0 && "Not a valid rot_imm value!");
259 case 0: return 0;
260 case 8: return 1;
261 case 16: return 2;
262 case 24: return 3;
263 }
264 }
265
Jim Grosbach806e80e2010-11-03 23:52:49 +0000266 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000268 return MI.getOperand(Op).getImm() - 1;
269 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000270
Jim Grosbach806e80e2010-11-03 23:52:49 +0000271 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000273 return 64 - MI.getOperand(Op).getImm();
274 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000275
Jim Grosbach806e80e2010-11-03 23:52:49 +0000276 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
277 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000278
Jim Grosbach806e80e2010-11-03 23:52:49 +0000279 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
280 SmallVectorImpl<MCFixup> &Fixups) const;
281 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
282 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000283 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
284 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000285 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
286 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000287
Owen Andersonc7139a62010-11-11 19:07:48 +0000288 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
289 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000290 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000291 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000292 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000293 unsigned EncodedValue) const;
294
295 unsigned VFPThumb2PostEncoder(const MCInst &MI,
296 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000297
Jim Grosbach70933262010-11-04 01:12:30 +0000298 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000299 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000300 }
301
Jim Grosbach70933262010-11-04 01:12:30 +0000302 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000303 // Output the constant in little endian byte order.
304 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000305 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000306 Val >>= 8;
307 }
308 }
309
Jim Grosbach568eeed2010-09-17 18:46:17 +0000310 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
311 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000312};
313
314} // end anonymous namespace
315
Bill Wendling0800ce72010-11-02 22:53:11 +0000316MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
317 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000318 return new ARMMCCodeEmitter(TM, Ctx);
319}
320
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000321/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
322/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000323/// Thumb2 mode.
324unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
325 unsigned EncodedValue) const {
326 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
327 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000328 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000329 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
330 // set to 1111.
331 unsigned Bit24 = EncodedValue & 0x01000000;
332 unsigned Bit28 = Bit24 << 4;
333 EncodedValue &= 0xEFFFFFFF;
334 EncodedValue |= Bit28;
335 EncodedValue |= 0x0F000000;
336 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000337
Owen Andersonc7139a62010-11-11 19:07:48 +0000338 return EncodedValue;
339}
340
Owen Anderson57dac882010-11-11 21:36:43 +0000341/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000342/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000343/// Thumb2 mode.
344unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
345 unsigned EncodedValue) const {
346 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
347 if (Subtarget.isThumb2()) {
348 EncodedValue &= 0xF0FFFFFF;
349 EncodedValue |= 0x09000000;
350 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000351
Owen Anderson57dac882010-11-11 21:36:43 +0000352 return EncodedValue;
353}
354
Owen Anderson8f143912010-11-11 23:12:55 +0000355/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000356/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000357/// Thumb2 mode.
358unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
359 unsigned EncodedValue) const {
360 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
361 if (Subtarget.isThumb2()) {
362 EncodedValue &= 0x00FFFFFF;
363 EncodedValue |= 0xEE000000;
364 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000365
Owen Anderson8f143912010-11-11 23:12:55 +0000366 return EncodedValue;
367}
368
Bill Wendlingcf590262010-12-01 21:54:50 +0000369/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
370/// them to their Thumb2 form if we are currently in Thumb2 mode.
371unsigned ARMMCCodeEmitter::
372VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
373 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
374 EncodedValue &= 0x0FFFFFFF;
375 EncodedValue |= 0xE0000000;
376 }
377 return EncodedValue;
378}
Owen Anderson57dac882010-11-11 21:36:43 +0000379
Jim Grosbach56ac9072010-10-08 21:45:55 +0000380/// getMachineOpValue - Return binary encoding of operand. If the machine
381/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000382unsigned ARMMCCodeEmitter::
383getMachineOpValue(const MCInst &MI, const MCOperand &MO,
384 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000385 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000386 unsigned Reg = MO.getReg();
387 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000388
Jim Grosbachb0708d22010-11-30 23:51:41 +0000389 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000390 switch (Reg) {
391 default:
392 return RegNo;
393 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
394 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
395 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
396 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
397 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000398 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000399 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000400 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000401 } else if (MO.isFPImm()) {
402 return static_cast<unsigned>(APFloat(MO.getFPImm())
403 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000404 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000405
Jim Grosbach817c1a62010-11-19 00:27:09 +0000406 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000407 return 0;
408}
409
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000410/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000411bool ARMMCCodeEmitter::
412EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
413 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000414 const MCOperand &MO = MI.getOperand(OpIdx);
415 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000416
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000417 Reg = getARMRegisterNumbering(MO.getReg());
418
419 int32_t SImm = MO1.getImm();
420 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000421
Jim Grosbachab682a22010-10-28 18:34:10 +0000422 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000423 if (SImm == INT32_MIN)
424 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000425
Jim Grosbachab682a22010-10-28 18:34:10 +0000426 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000427 if (SImm < 0) {
428 SImm = -SImm;
429 isAdd = false;
430 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000431
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000432 Imm = SImm;
433 return isAdd;
434}
435
Bill Wendlingdff2f712010-12-08 23:01:43 +0000436/// getBranchTargetOpValue - Helper function to get the branch target operand,
437/// which is either an immediate or requires a fixup.
438static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
439 unsigned FixupKind,
440 SmallVectorImpl<MCFixup> &Fixups) {
441 const MCOperand &MO = MI.getOperand(OpIdx);
442
443 // If the destination is an immediate, we have nothing to do.
444 if (MO.isImm()) return MO.getImm();
445 assert(MO.isExpr() && "Unexpected branch target type!");
446 const MCExpr *Expr = MO.getExpr();
447 MCFixupKind Kind = MCFixupKind(FixupKind);
448 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
449
450 // All of the information is in the fixup.
451 return 0;
452}
453
454/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000455uint32_t ARMMCCodeEmitter::
456getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
457 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000458 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000459}
460
Bill Wendling09aa3f02010-12-09 00:39:08 +0000461/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
462/// BLX branch target.
463uint32_t ARMMCCodeEmitter::
464getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
465 SmallVectorImpl<MCFixup> &Fixups) const {
466 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
467}
468
Jim Grosbache2467172010-12-10 18:21:33 +0000469/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
470uint32_t ARMMCCodeEmitter::
471getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
472 SmallVectorImpl<MCFixup> &Fixups) const {
473 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
474}
475
Jim Grosbach01086452010-12-10 17:13:40 +0000476/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
477uint32_t ARMMCCodeEmitter::
478getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000479 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000480 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
481}
482
Jim Grosbach027d6e82010-12-09 19:04:53 +0000483/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000484uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000485getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000486 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000487 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000488}
489
490/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
491/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000492uint32_t ARMMCCodeEmitter::
493getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000494 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersonfb20d892010-12-09 00:27:41 +0000495 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
496 if (Subtarget.isThumb2())
497 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_branch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000498 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000499}
500
Bill Wendlingdff2f712010-12-08 23:01:43 +0000501/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
502/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000503uint32_t ARMMCCodeEmitter::
504getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
505 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000506 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
507 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
508 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000509}
510
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000511/// getTAddrModeRegRegOpValue - Return encoding info for 'reg + reg' operand.
512uint32_t ARMMCCodeEmitter::
513getTAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
514 SmallVectorImpl<MCFixup> &Fixups) const {
515 const MCOperand &MO1 = MI.getOperand(OpIdx);
516 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
517 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
518 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
519 return (Rm << 3) | Rn;
520}
521
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000522/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000523uint32_t ARMMCCodeEmitter::
524getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
525 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000526 // {17-13} = reg
527 // {12} = (U)nsigned (add == '1', sub == '0')
528 // {11-0} = imm12
529 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000530 bool isAdd = true;
531 // If The first operand isn't a register, we have a label reference.
532 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000533 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
534 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000535 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000536 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000537 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000538
Owen Andersoneb6779c2010-12-07 00:45:21 +0000539 const MCExpr *Expr = 0;
540 if (!MO.isReg())
541 Expr = MO.getExpr();
542 else
543 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000544
Owen Andersond7b3f582010-12-09 01:51:07 +0000545 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
546 MCFixupKind Kind;
547 if (Subtarget.isThumb2())
548 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
549 else
550 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000551 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
552
553 ++MCNumCPRelocations;
554 } else
555 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000556
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000557 uint32_t Binary = Imm12 & 0xfff;
558 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000559 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000560 Binary |= (1 << 12);
561 Binary |= (Reg << 13);
562 return Binary;
563}
564
Owen Anderson9d63d902010-12-01 19:18:46 +0000565/// getT2AddrModeImm8s4OpValue - Return encoding info for
566/// 'reg +/- imm8<<2' operand.
567uint32_t ARMMCCodeEmitter::
568getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
569 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000570 // {12-9} = reg
571 // {8} = (U)nsigned (add == '1', sub == '0')
572 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000573 unsigned Reg, Imm8;
574 bool isAdd = true;
575 // If The first operand isn't a register, we have a label reference.
576 const MCOperand &MO = MI.getOperand(OpIdx);
577 if (!MO.isReg()) {
578 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
579 Imm8 = 0;
580 isAdd = false ; // 'U' bit is set as part of the fixup.
581
582 assert(MO.isExpr() && "Unexpected machine operand type!");
583 const MCExpr *Expr = MO.getExpr();
584 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
585 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
586
587 ++MCNumCPRelocations;
588 } else
589 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
590
591 uint32_t Binary = (Imm8 >> 2) & 0xff;
592 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
593 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000594 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000595 Binary |= (Reg << 9);
596 return Binary;
597}
598
Jim Grosbach54fea632010-11-09 17:20:53 +0000599uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000600getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
601 SmallVectorImpl<MCFixup> &Fixups) const {
602 // {20-16} = imm{15-12}
603 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000604 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000605 if (MO.isImm()) {
606 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000607 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim837caa92010-11-18 23:37:15 +0000608 dyn_cast<MCSymbolRefExpr>(MO.getExpr())) {
609 MCFixupKind Kind;
610 switch (Expr->getKind()) {
Duncan Sands3d938932010-11-22 09:38:00 +0000611 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000612 case MCSymbolRefExpr::VK_ARM_HI16:
613 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
614 break;
615 case MCSymbolRefExpr::VK_ARM_LO16:
616 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
617 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000618 }
619 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
620 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000621 };
622 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000623 return 0;
624}
625
626uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000627getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
628 SmallVectorImpl<MCFixup> &Fixups) const {
629 const MCOperand &MO = MI.getOperand(OpIdx);
630 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
631 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
632 unsigned Rn = getARMRegisterNumbering(MO.getReg());
633 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000634 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
635 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000636 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
637 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000638
639 // {16-13} = Rn
640 // {12} = isAdd
641 // {11-0} = shifter
642 // {3-0} = Rm
643 // {4} = 0
644 // {6-5} = type
645 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000646 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000647 Binary |= Rn << 13;
648 Binary |= SBits << 5;
649 Binary |= ShImm << 7;
650 if (isAdd)
651 Binary |= 1 << 12;
652 return Binary;
653}
654
Jim Grosbach570a9222010-11-11 01:09:40 +0000655uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000656getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
657 SmallVectorImpl<MCFixup> &Fixups) const {
658 // {17-14} Rn
659 // {13} 1 == imm12, 0 == Rm
660 // {12} isAdd
661 // {11-0} imm12/Rm
662 const MCOperand &MO = MI.getOperand(OpIdx);
663 unsigned Rn = getARMRegisterNumbering(MO.getReg());
664 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
665 Binary |= Rn << 14;
666 return Binary;
667}
668
669uint32_t ARMMCCodeEmitter::
670getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
671 SmallVectorImpl<MCFixup> &Fixups) const {
672 // {13} 1 == imm12, 0 == Rm
673 // {12} isAdd
674 // {11-0} imm12/Rm
675 const MCOperand &MO = MI.getOperand(OpIdx);
676 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
677 unsigned Imm = MO1.getImm();
678 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
679 bool isReg = MO.getReg() != 0;
680 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
681 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
682 if (isReg) {
683 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
684 Binary <<= 7; // Shift amount is bits [11:7]
685 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
686 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
687 }
688 return Binary | (isAdd << 12) | (isReg << 13);
689}
690
691uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000692getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
693 SmallVectorImpl<MCFixup> &Fixups) const {
694 // {9} 1 == imm8, 0 == Rm
695 // {8} isAdd
696 // {7-4} imm7_4/zero
697 // {3-0} imm3_0/Rm
698 const MCOperand &MO = MI.getOperand(OpIdx);
699 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
700 unsigned Imm = MO1.getImm();
701 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
702 bool isImm = MO.getReg() == 0;
703 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
704 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
705 if (!isImm)
706 Imm8 = getARMRegisterNumbering(MO.getReg());
707 return Imm8 | (isAdd << 8) | (isImm << 9);
708}
709
710uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000711getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
712 SmallVectorImpl<MCFixup> &Fixups) const {
713 // {13} 1 == imm8, 0 == Rm
714 // {12-9} Rn
715 // {8} isAdd
716 // {7-4} imm7_4/zero
717 // {3-0} imm3_0/Rm
718 const MCOperand &MO = MI.getOperand(OpIdx);
719 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
720 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
721 unsigned Rn = getARMRegisterNumbering(MO.getReg());
722 unsigned Imm = MO2.getImm();
723 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
724 bool isImm = MO1.getReg() == 0;
725 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
726 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
727 if (!isImm)
728 Imm8 = getARMRegisterNumbering(MO1.getReg());
729 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
730}
731
Bill Wendlingb8958b02010-12-08 01:57:09 +0000732/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000733uint32_t ARMMCCodeEmitter::
734getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
735 SmallVectorImpl<MCFixup> &Fixups) const {
736 // [SP, #imm]
737 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000738 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000739#if 0 // FIXME: This crashes2003-05-14-initialize-string.c
740 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
741 "Unexpected base register!");
742#endif
Jim Grosbachd967cd02010-12-07 21:50:47 +0000743 // The immediate is already shifted for the implicit zeroes, so no change
744 // here.
745 return MO1.getImm() & 0xff;
746}
747
Bill Wendling1fd374e2010-11-30 22:57:21 +0000748/// getAddrModeSOpValue - Encode the t_addrmode_s# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000749uint32_t ARMMCCodeEmitter::
750getAddrModeSOpValue(const MCInst &MI, unsigned OpIdx,
751 SmallVectorImpl<MCFixup> &) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000752 // [Rn, Rm]
753 // {5-3} = Rm
754 // {2-0} = Rn
755 //
756 // [Rn, #imm]
757 // {7-3} = imm5
758 // {2-0} = Rn
759 const MCOperand &MO = MI.getOperand(OpIdx);
760 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
761 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
762 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Bill Wendling272df512010-12-09 21:49:07 +0000763 unsigned Imm5 = MO1.getImm();
Bill Wendling0bdf0c02010-12-03 00:53:22 +0000764
765 if (MO2.getReg() != 0)
766 // Is an immediate.
767 Imm5 = getARMRegisterNumbering(MO2.getReg());
768
Bill Wendling272df512010-12-09 21:49:07 +0000769 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000770}
771
Bill Wendlingb8958b02010-12-08 01:57:09 +0000772/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
773uint32_t ARMMCCodeEmitter::
774getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
775 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000776 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000777}
778
Jim Grosbach5177f792010-12-01 21:09:40 +0000779/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000780uint32_t ARMMCCodeEmitter::
781getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
782 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000783 // {12-9} = reg
784 // {8} = (U)nsigned (add == '1', sub == '0')
785 // {7-0} = imm8
786 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000787 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000788 // If The first operand isn't a register, we have a label reference.
789 const MCOperand &MO = MI.getOperand(OpIdx);
790 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000791 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000792 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000793 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000794
795 assert(MO.isExpr() && "Unexpected machine operand type!");
796 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000797 MCFixupKind Kind;
798 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
799 if (Subtarget.isThumb2())
800 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
801 else
802 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000803 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
804
805 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000806 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000807 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000808 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
809 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000810
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000811 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
812 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000813 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000814 Binary |= (1 << 8);
815 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000816 return Binary;
817}
818
Jim Grosbach806e80e2010-11-03 23:52:49 +0000819unsigned ARMMCCodeEmitter::
820getSORegOpValue(const MCInst &MI, unsigned OpIdx,
821 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000822 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
823 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
824 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000825 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000826 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000827 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000828 // {6-5} = type
829 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000830 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000831 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000832 // else (imm shift)
833 // {11-7} = imm
834
835 const MCOperand &MO = MI.getOperand(OpIdx);
836 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
837 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
838 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
839
840 // Encode Rm.
841 unsigned Binary = getARMRegisterNumbering(MO.getReg());
842
843 // Encode the shift opcode.
844 unsigned SBits = 0;
845 unsigned Rs = MO1.getReg();
846 if (Rs) {
847 // Set shift operand (bit[7:4]).
848 // LSL - 0001
849 // LSR - 0011
850 // ASR - 0101
851 // ROR - 0111
852 // RRX - 0110 and bit[11:8] clear.
853 switch (SOpc) {
854 default: llvm_unreachable("Unknown shift opc!");
855 case ARM_AM::lsl: SBits = 0x1; break;
856 case ARM_AM::lsr: SBits = 0x3; break;
857 case ARM_AM::asr: SBits = 0x5; break;
858 case ARM_AM::ror: SBits = 0x7; break;
859 case ARM_AM::rrx: SBits = 0x6; break;
860 }
861 } else {
862 // Set shift operand (bit[6:4]).
863 // LSL - 000
864 // LSR - 010
865 // ASR - 100
866 // ROR - 110
867 switch (SOpc) {
868 default: llvm_unreachable("Unknown shift opc!");
869 case ARM_AM::lsl: SBits = 0x0; break;
870 case ARM_AM::lsr: SBits = 0x2; break;
871 case ARM_AM::asr: SBits = 0x4; break;
872 case ARM_AM::ror: SBits = 0x6; break;
873 }
874 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000875
Jim Grosbachef324d72010-10-12 23:53:58 +0000876 Binary |= SBits << 4;
877 if (SOpc == ARM_AM::rrx)
878 return Binary;
879
880 // Encode the shift operation Rs or shift_imm (except rrx).
881 if (Rs) {
882 // Encode Rs bit[11:8].
883 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
884 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
885 }
886
887 // Encode shift_imm bit[11:7].
888 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
889}
890
Jim Grosbach806e80e2010-11-03 23:52:49 +0000891unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000892getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
893 SmallVectorImpl<MCFixup> &Fixups) const {
894 const MCOperand &MO1 = MI.getOperand(OpNum);
895 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000896 const MCOperand &MO3 = MI.getOperand(OpNum+2);
897
Owen Anderson75579f72010-11-29 22:44:32 +0000898 // Encoded as [Rn, Rm, imm].
899 // FIXME: Needs fixup support.
900 unsigned Value = getARMRegisterNumbering(MO1.getReg());
901 Value <<= 4;
902 Value |= getARMRegisterNumbering(MO2.getReg());
903 Value <<= 2;
904 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000905
Owen Anderson75579f72010-11-29 22:44:32 +0000906 return Value;
907}
908
909unsigned ARMMCCodeEmitter::
910getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
911 SmallVectorImpl<MCFixup> &Fixups) const {
912 const MCOperand &MO1 = MI.getOperand(OpNum);
913 const MCOperand &MO2 = MI.getOperand(OpNum+1);
914
915 // FIXME: Needs fixup support.
916 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000917
Owen Anderson75579f72010-11-29 22:44:32 +0000918 // Even though the immediate is 8 bits long, we need 9 bits in order
919 // to represent the (inverse of the) sign bit.
920 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000921 int32_t tmp = (int32_t)MO2.getImm();
922 if (tmp < 0)
923 tmp = abs(tmp);
924 else
925 Value |= 256; // Set the ADD bit
926 Value |= tmp & 255;
927 return Value;
928}
929
930unsigned ARMMCCodeEmitter::
931getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
932 SmallVectorImpl<MCFixup> &Fixups) const {
933 const MCOperand &MO1 = MI.getOperand(OpNum);
934
935 // FIXME: Needs fixup support.
936 unsigned Value = 0;
937 int32_t tmp = (int32_t)MO1.getImm();
938 if (tmp < 0)
939 tmp = abs(tmp);
940 else
941 Value |= 256; // Set the ADD bit
942 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000943 return Value;
944}
945
946unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000947getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
948 SmallVectorImpl<MCFixup> &Fixups) const {
949 const MCOperand &MO1 = MI.getOperand(OpNum);
950
951 // FIXME: Needs fixup support.
952 unsigned Value = 0;
953 int32_t tmp = (int32_t)MO1.getImm();
954 if (tmp < 0)
955 tmp = abs(tmp);
956 else
957 Value |= 4096; // Set the ADD bit
958 Value |= tmp & 4095;
959 return Value;
960}
961
962unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +0000963getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
964 SmallVectorImpl<MCFixup> &Fixups) const {
965 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
966 // shifted. The second is the amount to shift by.
967 //
968 // {3-0} = Rm.
969 // {4} = 0
970 // {6-5} = type
971 // {11-7} = imm
972
973 const MCOperand &MO = MI.getOperand(OpIdx);
974 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
975 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
976
977 // Encode Rm.
978 unsigned Binary = getARMRegisterNumbering(MO.getReg());
979
980 // Encode the shift opcode.
981 unsigned SBits = 0;
982 // Set shift operand (bit[6:4]).
983 // LSL - 000
984 // LSR - 010
985 // ASR - 100
986 // ROR - 110
987 switch (SOpc) {
988 default: llvm_unreachable("Unknown shift opc!");
989 case ARM_AM::lsl: SBits = 0x0; break;
990 case ARM_AM::lsr: SBits = 0x2; break;
991 case ARM_AM::asr: SBits = 0x4; break;
992 case ARM_AM::ror: SBits = 0x6; break;
993 }
994
995 Binary |= SBits << 4;
996 if (SOpc == ARM_AM::rrx)
997 return Binary;
998
999 // Encode shift_imm bit[11:7].
1000 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1001}
1002
1003unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001004getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1005 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001006 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1007 // msb of the mask.
1008 const MCOperand &MO = MI.getOperand(Op);
1009 uint32_t v = ~MO.getImm();
1010 uint32_t lsb = CountTrailingZeros_32(v);
1011 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1012 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1013 return lsb | (msb << 5);
1014}
1015
Jim Grosbach806e80e2010-11-03 23:52:49 +00001016unsigned ARMMCCodeEmitter::
1017getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001018 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001019 // VLDM/VSTM:
1020 // {12-8} = Vd
1021 // {7-0} = Number of registers
1022 //
1023 // LDM/STM:
1024 // {15-0} = Bitfield of GPRs.
1025 unsigned Reg = MI.getOperand(Op).getReg();
1026 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1027 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1028
Bill Wendling5e559a22010-11-09 00:30:18 +00001029 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001030
1031 if (SPRRegs || DPRRegs) {
1032 // VLDM/VSTM
1033 unsigned RegNo = getARMRegisterNumbering(Reg);
1034 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1035 Binary |= (RegNo & 0x1f) << 8;
1036 if (SPRRegs)
1037 Binary |= NumRegs;
1038 else
1039 Binary |= NumRegs * 2;
1040 } else {
1041 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1042 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1043 Binary |= 1 << RegNo;
1044 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001045 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001046
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001047 return Binary;
1048}
1049
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001050/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1051/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001052unsigned ARMMCCodeEmitter::
1053getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1054 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001055 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001056 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001057
Owen Andersond9aa7d32010-11-02 00:05:05 +00001058 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001059 unsigned Align = 0;
1060
1061 switch (Imm.getImm()) {
1062 default: break;
1063 case 2:
1064 case 4:
1065 case 8: Align = 0x01; break;
1066 case 16: Align = 0x02; break;
1067 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001068 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001069
Owen Andersond9aa7d32010-11-02 00:05:05 +00001070 return RegNo | (Align << 4);
1071}
1072
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001073/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1074/// alignment operand for use in VLD-dup instructions. This is the same as
1075/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1076/// different for VLD4-dup.
1077unsigned ARMMCCodeEmitter::
1078getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1079 SmallVectorImpl<MCFixup> &Fixups) const {
1080 const MCOperand &Reg = MI.getOperand(Op);
1081 const MCOperand &Imm = MI.getOperand(Op + 1);
1082
1083 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1084 unsigned Align = 0;
1085
1086 switch (Imm.getImm()) {
1087 default: break;
1088 case 2:
1089 case 4:
1090 case 8: Align = 0x01; break;
1091 case 16: Align = 0x03; break;
1092 }
1093
1094 return RegNo | (Align << 4);
1095}
1096
Jim Grosbach806e80e2010-11-03 23:52:49 +00001097unsigned ARMMCCodeEmitter::
1098getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1099 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001100 const MCOperand &MO = MI.getOperand(Op);
1101 if (MO.getReg() == 0) return 0x0D;
1102 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001103}
1104
Jim Grosbach568eeed2010-09-17 18:46:17 +00001105void ARMMCCodeEmitter::
1106EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001107 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001108 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001109 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001110 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001111 uint64_t TSFlags = Desc.TSFlags;
1112 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001113 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001114 int Size;
1115 // Basic size info comes from the TSFlags field.
1116 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1117 default: llvm_unreachable("Unexpected instruction size!");
1118 case ARMII::Size2Bytes: Size = 2; break;
1119 case ARMII::Size4Bytes: Size = 4; break;
1120 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001121 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1122 // Thumb 32-bit wide instructions need to be have the high order halfword
1123 // emitted first.
1124 if (Subtarget.isThumb() && Size == 4) {
1125 EmitConstant(Binary >> 16, 2, OS);
1126 EmitConstant(Binary & 0xffff, 2, OS);
1127 } else
1128 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001129 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001130}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001131
Jim Grosbach806e80e2010-11-03 23:52:49 +00001132#include "ARMGenMCCodeEmitter.inc"