blob: 542d24d055816fc741ed6e01d5088c2be938b938 [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"
Evan Cheng75972122011-01-13 07:58:56 +000019#include "ARMMCExpr.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000020#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
Jim Grosbachd6d4b422010-10-07 22:12:50 +000023#include "llvm/ADT/Statistic.h"
Jim Grosbach568eeed2010-09-17 18:46:17 +000024#include "llvm/Support/raw_ostream.h"
25using namespace llvm;
26
Jim Grosbach70933262010-11-04 01:12:30 +000027STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
28STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
Jim Grosbachd6d4b422010-10-07 22:12:50 +000029
Jim Grosbach568eeed2010-09-17 18:46:17 +000030namespace {
31class ARMMCCodeEmitter : public MCCodeEmitter {
32 ARMMCCodeEmitter(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
33 void operator=(const ARMMCCodeEmitter &); // DO NOT IMPLEMENT
34 const TargetMachine &TM;
35 const TargetInstrInfo &TII;
36 MCContext &Ctx;
37
38public:
39 ARMMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
40 : TM(tm), TII(*TM.getInstrInfo()), Ctx(ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +000041 }
42
43 ~ARMMCCodeEmitter() {}
44
Jim Grosbach0de6ab32010-10-12 17:11:26 +000045 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
46
Jim Grosbach9af82ba2010-10-07 21:57:55 +000047 // getBinaryCodeForInstr - TableGen'erated function for getting the
48 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000049 unsigned getBinaryCodeForInstr(const MCInst &MI,
50 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000051
52 /// getMachineOpValue - Return binary encoding of operand. If the machine
53 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000054 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
55 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000056
Evan Cheng75972122011-01-13 07:58:56 +000057 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
58 /// the specified operand. This is used for operands with :lower16: and
59 /// :upper16: prefixes.
60 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
61 SmallVectorImpl<MCFixup> &Fixups) const;
Jason W Kim837caa92010-11-18 23:37:15 +000062
Bill Wendling92b5a2e2010-11-03 01:49:29 +000063 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000064 unsigned &Reg, unsigned &Imm,
65 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000066
Jim Grosbach662a8162010-12-06 23:57:07 +000067 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000068 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000069 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
70 SmallVectorImpl<MCFixup> &Fixups) const;
71
Bill Wendling09aa3f02010-12-09 00:39:08 +000072 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
73 /// BLX branch target.
74 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
75 SmallVectorImpl<MCFixup> &Fixups) const;
76
Jim Grosbache2467172010-12-10 18:21:33 +000077 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
78 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
79 SmallVectorImpl<MCFixup> &Fixups) const;
80
Jim Grosbach01086452010-12-10 17:13:40 +000081 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
82 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups) const;
84
Jim Grosbach027d6e82010-12-09 19:04:53 +000085 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
86 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +000087 SmallVectorImpl<MCFixup> &Fixups) const;
88
Jim Grosbachc466b932010-11-11 18:04:49 +000089 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
90 /// branch target.
91 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
92 SmallVectorImpl<MCFixup> &Fixups) const;
93
Owen Andersonc2666002010-12-13 19:31:11 +000094 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
95 /// immediate Thumb2 direct branch target.
96 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups) const;
98
99
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000100 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
101 /// ADR label target.
102 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
103 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000104 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000106 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups) const;
108
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000109
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000110 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
111 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000112 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
113 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000114
Bill Wendlingf4caf692010-12-14 03:36:38 +0000115 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
116 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
117 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000118
Owen Anderson9d63d902010-12-01 19:18:46 +0000119 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
120 /// operand.
121 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
122 SmallVectorImpl<MCFixup> &Fixups) const;
123
124
Jim Grosbach54fea632010-11-09 17:20:53 +0000125 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
126 /// operand as needed by load/store instructions.
127 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
128 SmallVectorImpl<MCFixup> &Fixups) const;
129
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000130 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
131 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
132 SmallVectorImpl<MCFixup> &Fixups) const {
133 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
134 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000135 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000136 case ARM_AM::da: return 0;
137 case ARM_AM::ia: return 1;
138 case ARM_AM::db: return 2;
139 case ARM_AM::ib: return 3;
140 }
141 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000142 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
143 ///
144 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
145 switch (ShOpc) {
146 default: llvm_unreachable("Unknown shift opc!");
147 case ARM_AM::no_shift:
148 case ARM_AM::lsl: return 0;
149 case ARM_AM::lsr: return 1;
150 case ARM_AM::asr: return 2;
151 case ARM_AM::ror:
152 case ARM_AM::rrx: return 3;
153 }
154 return 0;
155 }
156
157 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
158 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
159 SmallVectorImpl<MCFixup> &Fixups) const;
160
161 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
162 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups) const;
164
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000165 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
166 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
167 SmallVectorImpl<MCFixup> &Fixups) const;
168
Jim Grosbach570a9222010-11-11 01:09:40 +0000169 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
170 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
171 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000172
Jim Grosbachd967cd02010-12-07 21:50:47 +0000173 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
174 /// operand.
175 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
176 SmallVectorImpl<MCFixup> &Fixups) const;
177
Bill Wendlingf4caf692010-12-14 03:36:38 +0000178 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
179 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000180 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000181
Bill Wendlingb8958b02010-12-08 01:57:09 +0000182 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
183 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
184 SmallVectorImpl<MCFixup> &Fixups) const;
185
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000186 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000187 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000189
Jim Grosbach08bd5492010-10-12 23:00:24 +0000190 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000191 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
192 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000193 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
194 // '1' respectively.
195 return MI.getOperand(Op).getReg() == ARM::CPSR;
196 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000197
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000198 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000199 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
200 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000201 unsigned SoImm = MI.getOperand(Op).getImm();
202 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
203 assert(SoImmVal != -1 && "Not a valid so_imm value!");
204
205 // Encode rotate_imm.
206 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
207 << ARMII::SoRotImmShift;
208
209 // Encode immed_8.
210 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
211 return Binary;
212 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000213
Owen Anderson5de6d842010-11-12 21:12:40 +0000214 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
215 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
216 SmallVectorImpl<MCFixup> &Fixups) const {
217 unsigned SoImm = MI.getOperand(Op).getImm();
218 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
219 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
220 return Encoded;
221 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000222
Owen Anderson75579f72010-11-29 22:44:32 +0000223 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
224 SmallVectorImpl<MCFixup> &Fixups) const;
225 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
226 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000227 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
228 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000229 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
230 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000231
Jim Grosbachef324d72010-10-12 23:53:58 +0000232 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000233 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
234 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000235 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000237
Jim Grosbach806e80e2010-11-03 23:52:49 +0000238 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
239 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000240 switch (MI.getOperand(Op).getImm()) {
241 default: assert (0 && "Not a valid rot_imm value!");
242 case 0: return 0;
243 case 8: return 1;
244 case 16: return 2;
245 case 24: return 3;
246 }
247 }
248
Jim Grosbach806e80e2010-11-03 23:52:49 +0000249 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
250 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000251 return MI.getOperand(Op).getImm() - 1;
252 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000253
Jim Grosbach806e80e2010-11-03 23:52:49 +0000254 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
255 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000256 return 64 - MI.getOperand(Op).getImm();
257 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000258
Jim Grosbach806e80e2010-11-03 23:52:49 +0000259 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000261
Jim Grosbach806e80e2010-11-03 23:52:49 +0000262 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups) const;
264 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
265 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000266 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
267 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000268 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
269 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000270
Owen Andersonc7139a62010-11-11 19:07:48 +0000271 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
272 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000273 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000274 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000275 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000276 unsigned EncodedValue) const;
277
278 unsigned VFPThumb2PostEncoder(const MCInst &MI,
279 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000280
Jim Grosbach70933262010-11-04 01:12:30 +0000281 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000282 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000283 }
284
Jim Grosbach70933262010-11-04 01:12:30 +0000285 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000286 // Output the constant in little endian byte order.
287 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000288 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000289 Val >>= 8;
290 }
291 }
292
Jim Grosbach568eeed2010-09-17 18:46:17 +0000293 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
294 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000295};
296
297} // end anonymous namespace
298
Bill Wendling0800ce72010-11-02 22:53:11 +0000299MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
300 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000301 return new ARMMCCodeEmitter(TM, Ctx);
302}
303
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000304/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
305/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000306/// Thumb2 mode.
307unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
308 unsigned EncodedValue) const {
309 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
310 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000311 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000312 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
313 // set to 1111.
314 unsigned Bit24 = EncodedValue & 0x01000000;
315 unsigned Bit28 = Bit24 << 4;
316 EncodedValue &= 0xEFFFFFFF;
317 EncodedValue |= Bit28;
318 EncodedValue |= 0x0F000000;
319 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000320
Owen Andersonc7139a62010-11-11 19:07:48 +0000321 return EncodedValue;
322}
323
Owen Anderson57dac882010-11-11 21:36:43 +0000324/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000325/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000326/// Thumb2 mode.
327unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
328 unsigned EncodedValue) const {
329 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
330 if (Subtarget.isThumb2()) {
331 EncodedValue &= 0xF0FFFFFF;
332 EncodedValue |= 0x09000000;
333 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000334
Owen Anderson57dac882010-11-11 21:36:43 +0000335 return EncodedValue;
336}
337
Owen Anderson8f143912010-11-11 23:12:55 +0000338/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000339/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000340/// Thumb2 mode.
341unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
342 unsigned EncodedValue) const {
343 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
344 if (Subtarget.isThumb2()) {
345 EncodedValue &= 0x00FFFFFF;
346 EncodedValue |= 0xEE000000;
347 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000348
Owen Anderson8f143912010-11-11 23:12:55 +0000349 return EncodedValue;
350}
351
Bill Wendlingcf590262010-12-01 21:54:50 +0000352/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
353/// them to their Thumb2 form if we are currently in Thumb2 mode.
354unsigned ARMMCCodeEmitter::
355VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
356 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
357 EncodedValue &= 0x0FFFFFFF;
358 EncodedValue |= 0xE0000000;
359 }
360 return EncodedValue;
361}
Owen Anderson57dac882010-11-11 21:36:43 +0000362
Jim Grosbach56ac9072010-10-08 21:45:55 +0000363/// getMachineOpValue - Return binary encoding of operand. If the machine
364/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000365unsigned ARMMCCodeEmitter::
366getMachineOpValue(const MCInst &MI, const MCOperand &MO,
367 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000368 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000369 unsigned Reg = MO.getReg();
370 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000371
Jim Grosbachb0708d22010-11-30 23:51:41 +0000372 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000373 switch (Reg) {
374 default:
375 return RegNo;
376 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
377 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
378 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
379 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
380 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000381 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000382 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000383 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000384 } else if (MO.isFPImm()) {
385 return static_cast<unsigned>(APFloat(MO.getFPImm())
386 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000387 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000388
Jim Grosbach817c1a62010-11-19 00:27:09 +0000389 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000390 return 0;
391}
392
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000393/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000394bool ARMMCCodeEmitter::
395EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
396 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000397 const MCOperand &MO = MI.getOperand(OpIdx);
398 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000399
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000400 Reg = getARMRegisterNumbering(MO.getReg());
401
402 int32_t SImm = MO1.getImm();
403 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000404
Jim Grosbachab682a22010-10-28 18:34:10 +0000405 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000406 if (SImm == INT32_MIN)
407 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000408
Jim Grosbachab682a22010-10-28 18:34:10 +0000409 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000410 if (SImm < 0) {
411 SImm = -SImm;
412 isAdd = false;
413 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000414
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000415 Imm = SImm;
416 return isAdd;
417}
418
Bill Wendlingdff2f712010-12-08 23:01:43 +0000419/// getBranchTargetOpValue - Helper function to get the branch target operand,
420/// which is either an immediate or requires a fixup.
421static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
422 unsigned FixupKind,
423 SmallVectorImpl<MCFixup> &Fixups) {
424 const MCOperand &MO = MI.getOperand(OpIdx);
425
426 // If the destination is an immediate, we have nothing to do.
427 if (MO.isImm()) return MO.getImm();
428 assert(MO.isExpr() && "Unexpected branch target type!");
429 const MCExpr *Expr = MO.getExpr();
430 MCFixupKind Kind = MCFixupKind(FixupKind);
431 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
432
433 // All of the information is in the fixup.
434 return 0;
435}
436
437/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000438uint32_t ARMMCCodeEmitter::
439getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
440 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000441 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000442}
443
Bill Wendling09aa3f02010-12-09 00:39:08 +0000444/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
445/// BLX branch target.
446uint32_t ARMMCCodeEmitter::
447getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
448 SmallVectorImpl<MCFixup> &Fixups) const {
449 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
450}
451
Jim Grosbache2467172010-12-10 18:21:33 +0000452/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
453uint32_t ARMMCCodeEmitter::
454getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
455 SmallVectorImpl<MCFixup> &Fixups) const {
456 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
457}
458
Jim Grosbach01086452010-12-10 17:13:40 +0000459/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
460uint32_t ARMMCCodeEmitter::
461getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000462 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000463 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
464}
465
Jim Grosbach027d6e82010-12-09 19:04:53 +0000466/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000467uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000468getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000469 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000470 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000471}
472
473/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
474/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000475uint32_t ARMMCCodeEmitter::
476getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000477 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000478 // FIXME: This really, really shouldn't use TargetMachine. We don't want
479 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000480 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
481 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000482 return
483 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000484 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000485}
486
Owen Andersonc2666002010-12-13 19:31:11 +0000487/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
488/// immediate branch target.
489uint32_t ARMMCCodeEmitter::
490getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
491 SmallVectorImpl<MCFixup> &Fixups) const {
492 unsigned Val =
493 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
494 bool I = (Val & 0x800000);
495 bool J1 = (Val & 0x400000);
496 bool J2 = (Val & 0x200000);
497 if (I ^ J1)
498 Val &= ~0x400000;
499 else
500 Val |= 0x400000;
501
502 if (I ^ J2)
503 Val &= ~0x200000;
504 else
505 Val |= 0x200000;
506
507 return Val;
508}
509
Bill Wendlingdff2f712010-12-08 23:01:43 +0000510/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
511/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000512uint32_t ARMMCCodeEmitter::
513getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
514 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000515 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
516 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
517 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000518}
519
Owen Andersona838a252010-12-14 00:36:49 +0000520/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
521/// target.
522uint32_t ARMMCCodeEmitter::
523getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
524 SmallVectorImpl<MCFixup> &Fixups) const {
525 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
526 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
527 Fixups);
528}
529
Jim Grosbachd40963c2010-12-14 22:28:03 +0000530/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
531/// target.
532uint32_t ARMMCCodeEmitter::
533getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
534 SmallVectorImpl<MCFixup> &Fixups) const {
535 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
536 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
537 Fixups);
538}
539
Bill Wendlingf4caf692010-12-14 03:36:38 +0000540/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
541/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000542uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000543getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
544 SmallVectorImpl<MCFixup> &) const {
545 // [Rn, Rm]
546 // {5-3} = Rm
547 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000548 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000549 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000550 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
551 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
552 return (Rm << 3) | Rn;
553}
554
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000555/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000556uint32_t ARMMCCodeEmitter::
557getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
558 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000559 // {17-13} = reg
560 // {12} = (U)nsigned (add == '1', sub == '0')
561 // {11-0} = imm12
562 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000563 bool isAdd = true;
564 // If The first operand isn't a register, we have a label reference.
565 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000566 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
567 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000568 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000569 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000570 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000571
Owen Andersoneb6779c2010-12-07 00:45:21 +0000572 const MCExpr *Expr = 0;
573 if (!MO.isReg())
574 Expr = MO.getExpr();
575 else
576 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000577
Owen Andersond7b3f582010-12-09 01:51:07 +0000578 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
579 MCFixupKind Kind;
580 if (Subtarget.isThumb2())
581 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
582 else
583 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000584 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
585
586 ++MCNumCPRelocations;
587 } else
588 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000589
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000590 uint32_t Binary = Imm12 & 0xfff;
591 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000592 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000593 Binary |= (1 << 12);
594 Binary |= (Reg << 13);
595 return Binary;
596}
597
Owen Anderson9d63d902010-12-01 19:18:46 +0000598/// getT2AddrModeImm8s4OpValue - Return encoding info for
599/// 'reg +/- imm8<<2' operand.
600uint32_t ARMMCCodeEmitter::
601getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
602 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000603 // {12-9} = reg
604 // {8} = (U)nsigned (add == '1', sub == '0')
605 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000606 unsigned Reg, Imm8;
607 bool isAdd = true;
608 // If The first operand isn't a register, we have a label reference.
609 const MCOperand &MO = MI.getOperand(OpIdx);
610 if (!MO.isReg()) {
611 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
612 Imm8 = 0;
613 isAdd = false ; // 'U' bit is set as part of the fixup.
614
615 assert(MO.isExpr() && "Unexpected machine operand type!");
616 const MCExpr *Expr = MO.getExpr();
617 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
618 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
619
620 ++MCNumCPRelocations;
621 } else
622 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
623
624 uint32_t Binary = (Imm8 >> 2) & 0xff;
625 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
626 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000627 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000628 Binary |= (Reg << 9);
629 return Binary;
630}
631
Jason W Kim86a97f22011-01-12 00:19:25 +0000632// FIXME: This routine assumes that a binary
633// expression will always result in a PCRel expression
634// In reality, its only true if one or more subexpressions
635// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
636// but this is good enough for now.
637static bool EvaluateAsPCRel(const MCExpr *Expr) {
638 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000639 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000640 case MCExpr::SymbolRef: return false;
641 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000642 }
643}
644
Evan Cheng75972122011-01-13 07:58:56 +0000645uint32_t
646ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
647 SmallVectorImpl<MCFixup> &Fixups) const {
Jason W Kim837caa92010-11-18 23:37:15 +0000648 // {20-16} = imm{15-12}
649 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000650 const MCOperand &MO = MI.getOperand(OpIdx);
Evan Cheng75972122011-01-13 07:58:56 +0000651 if (MO.isImm())
652 // Hi / lo 16 bits already extracted during earlier passes.
Jason W Kim837caa92010-11-18 23:37:15 +0000653 return static_cast<unsigned>(MO.getImm());
Evan Cheng75972122011-01-13 07:58:56 +0000654
655 // Handle :upper16: and :lower16: assembly prefixes.
656 const MCExpr *E = MO.getExpr();
657 if (E->getKind() == MCExpr::Target) {
658 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
659 E = ARM16Expr->getSubExpr();
660
Jason W Kim837caa92010-11-18 23:37:15 +0000661 MCFixupKind Kind;
Evan Cheng75972122011-01-13 07:58:56 +0000662 switch (ARM16Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000663 default: assert(0 && "Unsupported ARMFixup");
Evan Cheng75972122011-01-13 07:58:56 +0000664 case ARMMCExpr::VK_ARM_HI16:
Jason W Kim837caa92010-11-18 23:37:15 +0000665 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
Evan Cheng75972122011-01-13 07:58:56 +0000666 if (EvaluateAsPCRel(E))
Jason W Kim86a97f22011-01-12 00:19:25 +0000667 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16_pcrel);
Jason W Kim837caa92010-11-18 23:37:15 +0000668 break;
Evan Cheng75972122011-01-13 07:58:56 +0000669 case ARMMCExpr::VK_ARM_LO16:
Jason W Kim837caa92010-11-18 23:37:15 +0000670 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
Evan Cheng75972122011-01-13 07:58:56 +0000671 if (EvaluateAsPCRel(E))
Jason W Kim86a97f22011-01-12 00:19:25 +0000672 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16_pcrel);
Jason W Kim837caa92010-11-18 23:37:15 +0000673 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000674 }
Evan Cheng75972122011-01-13 07:58:56 +0000675 Fixups.push_back(MCFixup::Create(0, E, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000676 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000677 };
Evan Cheng75972122011-01-13 07:58:56 +0000678
Jim Grosbach817c1a62010-11-19 00:27:09 +0000679 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000680 return 0;
681}
682
683uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000684getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
685 SmallVectorImpl<MCFixup> &Fixups) const {
686 const MCOperand &MO = MI.getOperand(OpIdx);
687 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
688 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
689 unsigned Rn = getARMRegisterNumbering(MO.getReg());
690 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000691 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
692 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000693 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
694 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000695
696 // {16-13} = Rn
697 // {12} = isAdd
698 // {11-0} = shifter
699 // {3-0} = Rm
700 // {4} = 0
701 // {6-5} = type
702 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000703 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000704 Binary |= Rn << 13;
705 Binary |= SBits << 5;
706 Binary |= ShImm << 7;
707 if (isAdd)
708 Binary |= 1 << 12;
709 return Binary;
710}
711
Jim Grosbach570a9222010-11-11 01:09:40 +0000712uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000713getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
714 SmallVectorImpl<MCFixup> &Fixups) const {
715 // {17-14} Rn
716 // {13} 1 == imm12, 0 == Rm
717 // {12} isAdd
718 // {11-0} imm12/Rm
719 const MCOperand &MO = MI.getOperand(OpIdx);
720 unsigned Rn = getARMRegisterNumbering(MO.getReg());
721 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
722 Binary |= Rn << 14;
723 return Binary;
724}
725
726uint32_t ARMMCCodeEmitter::
727getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
728 SmallVectorImpl<MCFixup> &Fixups) const {
729 // {13} 1 == imm12, 0 == Rm
730 // {12} isAdd
731 // {11-0} imm12/Rm
732 const MCOperand &MO = MI.getOperand(OpIdx);
733 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
734 unsigned Imm = MO1.getImm();
735 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
736 bool isReg = MO.getReg() != 0;
737 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
738 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
739 if (isReg) {
740 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
741 Binary <<= 7; // Shift amount is bits [11:7]
742 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
743 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
744 }
745 return Binary | (isAdd << 12) | (isReg << 13);
746}
747
748uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000749getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
750 SmallVectorImpl<MCFixup> &Fixups) const {
751 // {9} 1 == imm8, 0 == Rm
752 // {8} isAdd
753 // {7-4} imm7_4/zero
754 // {3-0} imm3_0/Rm
755 const MCOperand &MO = MI.getOperand(OpIdx);
756 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
757 unsigned Imm = MO1.getImm();
758 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
759 bool isImm = MO.getReg() == 0;
760 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
761 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
762 if (!isImm)
763 Imm8 = getARMRegisterNumbering(MO.getReg());
764 return Imm8 | (isAdd << 8) | (isImm << 9);
765}
766
767uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000768getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
769 SmallVectorImpl<MCFixup> &Fixups) const {
770 // {13} 1 == imm8, 0 == Rm
771 // {12-9} Rn
772 // {8} isAdd
773 // {7-4} imm7_4/zero
774 // {3-0} imm3_0/Rm
775 const MCOperand &MO = MI.getOperand(OpIdx);
776 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
777 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
778 unsigned Rn = getARMRegisterNumbering(MO.getReg());
779 unsigned Imm = MO2.getImm();
780 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
781 bool isImm = MO1.getReg() == 0;
782 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
783 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
784 if (!isImm)
785 Imm8 = getARMRegisterNumbering(MO1.getReg());
786 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
787}
788
Bill Wendlingb8958b02010-12-08 01:57:09 +0000789/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000790uint32_t ARMMCCodeEmitter::
791getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
792 SmallVectorImpl<MCFixup> &Fixups) const {
793 // [SP, #imm]
794 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000795 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000796 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
797 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000798
Jim Grosbachd967cd02010-12-07 21:50:47 +0000799 // The immediate is already shifted for the implicit zeroes, so no change
800 // here.
801 return MO1.getImm() & 0xff;
802}
803
Bill Wendlingf4caf692010-12-14 03:36:38 +0000804/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000805uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000806getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000807 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000808 // [Rn, #imm]
809 // {7-3} = imm5
810 // {2-0} = Rn
811 const MCOperand &MO = MI.getOperand(OpIdx);
812 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000813 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000814 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000815 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000816}
817
Bill Wendlingb8958b02010-12-08 01:57:09 +0000818/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
819uint32_t ARMMCCodeEmitter::
820getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
821 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000822 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000823}
824
Jim Grosbach5177f792010-12-01 21:09:40 +0000825/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000826uint32_t ARMMCCodeEmitter::
827getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
828 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000829 // {12-9} = reg
830 // {8} = (U)nsigned (add == '1', sub == '0')
831 // {7-0} = imm8
832 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000833 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000834 // If The first operand isn't a register, we have a label reference.
835 const MCOperand &MO = MI.getOperand(OpIdx);
836 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000837 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000838 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000839 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000840
841 assert(MO.isExpr() && "Unexpected machine operand type!");
842 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000843 MCFixupKind Kind;
844 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
845 if (Subtarget.isThumb2())
846 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
847 else
848 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000849 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
850
851 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000852 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000853 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000854 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
855 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000856
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000857 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
858 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000859 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000860 Binary |= (1 << 8);
861 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000862 return Binary;
863}
864
Jim Grosbach806e80e2010-11-03 23:52:49 +0000865unsigned ARMMCCodeEmitter::
866getSORegOpValue(const MCInst &MI, unsigned OpIdx,
867 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000868 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
869 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
870 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000871 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000872 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000873 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000874 // {6-5} = type
875 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000876 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000877 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000878 // else (imm shift)
879 // {11-7} = imm
880
881 const MCOperand &MO = MI.getOperand(OpIdx);
882 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
883 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
884 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
885
886 // Encode Rm.
887 unsigned Binary = getARMRegisterNumbering(MO.getReg());
888
889 // Encode the shift opcode.
890 unsigned SBits = 0;
891 unsigned Rs = MO1.getReg();
892 if (Rs) {
893 // Set shift operand (bit[7:4]).
894 // LSL - 0001
895 // LSR - 0011
896 // ASR - 0101
897 // ROR - 0111
898 // RRX - 0110 and bit[11:8] clear.
899 switch (SOpc) {
900 default: llvm_unreachable("Unknown shift opc!");
901 case ARM_AM::lsl: SBits = 0x1; break;
902 case ARM_AM::lsr: SBits = 0x3; break;
903 case ARM_AM::asr: SBits = 0x5; break;
904 case ARM_AM::ror: SBits = 0x7; break;
905 case ARM_AM::rrx: SBits = 0x6; break;
906 }
907 } else {
908 // Set shift operand (bit[6:4]).
909 // LSL - 000
910 // LSR - 010
911 // ASR - 100
912 // ROR - 110
913 switch (SOpc) {
914 default: llvm_unreachable("Unknown shift opc!");
915 case ARM_AM::lsl: SBits = 0x0; break;
916 case ARM_AM::lsr: SBits = 0x2; break;
917 case ARM_AM::asr: SBits = 0x4; break;
918 case ARM_AM::ror: SBits = 0x6; break;
919 }
920 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000921
Jim Grosbachef324d72010-10-12 23:53:58 +0000922 Binary |= SBits << 4;
923 if (SOpc == ARM_AM::rrx)
924 return Binary;
925
926 // Encode the shift operation Rs or shift_imm (except rrx).
927 if (Rs) {
928 // Encode Rs bit[11:8].
929 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
930 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
931 }
932
933 // Encode shift_imm bit[11:7].
934 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
935}
936
Jim Grosbach806e80e2010-11-03 23:52:49 +0000937unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000938getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
939 SmallVectorImpl<MCFixup> &Fixups) const {
940 const MCOperand &MO1 = MI.getOperand(OpNum);
941 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000942 const MCOperand &MO3 = MI.getOperand(OpNum+2);
943
Owen Anderson75579f72010-11-29 22:44:32 +0000944 // Encoded as [Rn, Rm, imm].
945 // FIXME: Needs fixup support.
946 unsigned Value = getARMRegisterNumbering(MO1.getReg());
947 Value <<= 4;
948 Value |= getARMRegisterNumbering(MO2.getReg());
949 Value <<= 2;
950 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000951
Owen Anderson75579f72010-11-29 22:44:32 +0000952 return Value;
953}
954
955unsigned ARMMCCodeEmitter::
956getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
957 SmallVectorImpl<MCFixup> &Fixups) const {
958 const MCOperand &MO1 = MI.getOperand(OpNum);
959 const MCOperand &MO2 = MI.getOperand(OpNum+1);
960
961 // FIXME: Needs fixup support.
962 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000963
Owen Anderson75579f72010-11-29 22:44:32 +0000964 // Even though the immediate is 8 bits long, we need 9 bits in order
965 // to represent the (inverse of the) sign bit.
966 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000967 int32_t tmp = (int32_t)MO2.getImm();
968 if (tmp < 0)
969 tmp = abs(tmp);
970 else
971 Value |= 256; // Set the ADD bit
972 Value |= tmp & 255;
973 return Value;
974}
975
976unsigned ARMMCCodeEmitter::
977getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
978 SmallVectorImpl<MCFixup> &Fixups) const {
979 const MCOperand &MO1 = MI.getOperand(OpNum);
980
981 // FIXME: Needs fixup support.
982 unsigned Value = 0;
983 int32_t tmp = (int32_t)MO1.getImm();
984 if (tmp < 0)
985 tmp = abs(tmp);
986 else
987 Value |= 256; // Set the ADD bit
988 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000989 return Value;
990}
991
992unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000993getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
994 SmallVectorImpl<MCFixup> &Fixups) const {
995 const MCOperand &MO1 = MI.getOperand(OpNum);
996
997 // FIXME: Needs fixup support.
998 unsigned Value = 0;
999 int32_t tmp = (int32_t)MO1.getImm();
1000 if (tmp < 0)
1001 tmp = abs(tmp);
1002 else
1003 Value |= 4096; // Set the ADD bit
1004 Value |= tmp & 4095;
1005 return Value;
1006}
1007
1008unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001009getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1010 SmallVectorImpl<MCFixup> &Fixups) const {
1011 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1012 // shifted. The second is the amount to shift by.
1013 //
1014 // {3-0} = Rm.
1015 // {4} = 0
1016 // {6-5} = type
1017 // {11-7} = imm
1018
1019 const MCOperand &MO = MI.getOperand(OpIdx);
1020 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1021 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1022
1023 // Encode Rm.
1024 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1025
1026 // Encode the shift opcode.
1027 unsigned SBits = 0;
1028 // Set shift operand (bit[6:4]).
1029 // LSL - 000
1030 // LSR - 010
1031 // ASR - 100
1032 // ROR - 110
1033 switch (SOpc) {
1034 default: llvm_unreachable("Unknown shift opc!");
1035 case ARM_AM::lsl: SBits = 0x0; break;
1036 case ARM_AM::lsr: SBits = 0x2; break;
1037 case ARM_AM::asr: SBits = 0x4; break;
1038 case ARM_AM::ror: SBits = 0x6; break;
1039 }
1040
1041 Binary |= SBits << 4;
1042 if (SOpc == ARM_AM::rrx)
1043 return Binary;
1044
1045 // Encode shift_imm bit[11:7].
1046 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1047}
1048
1049unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001050getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1051 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001052 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1053 // msb of the mask.
1054 const MCOperand &MO = MI.getOperand(Op);
1055 uint32_t v = ~MO.getImm();
1056 uint32_t lsb = CountTrailingZeros_32(v);
1057 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1058 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1059 return lsb | (msb << 5);
1060}
1061
Jim Grosbach806e80e2010-11-03 23:52:49 +00001062unsigned ARMMCCodeEmitter::
1063getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001064 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001065 // VLDM/VSTM:
1066 // {12-8} = Vd
1067 // {7-0} = Number of registers
1068 //
1069 // LDM/STM:
1070 // {15-0} = Bitfield of GPRs.
1071 unsigned Reg = MI.getOperand(Op).getReg();
1072 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1073 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1074
Bill Wendling5e559a22010-11-09 00:30:18 +00001075 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001076
1077 if (SPRRegs || DPRRegs) {
1078 // VLDM/VSTM
1079 unsigned RegNo = getARMRegisterNumbering(Reg);
1080 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1081 Binary |= (RegNo & 0x1f) << 8;
1082 if (SPRRegs)
1083 Binary |= NumRegs;
1084 else
1085 Binary |= NumRegs * 2;
1086 } else {
1087 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1088 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1089 Binary |= 1 << RegNo;
1090 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001091 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001092
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001093 return Binary;
1094}
1095
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001096/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1097/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001098unsigned ARMMCCodeEmitter::
1099getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1100 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001101 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001102 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001103
Owen Andersond9aa7d32010-11-02 00:05:05 +00001104 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001105 unsigned Align = 0;
1106
1107 switch (Imm.getImm()) {
1108 default: break;
1109 case 2:
1110 case 4:
1111 case 8: Align = 0x01; break;
1112 case 16: Align = 0x02; break;
1113 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001114 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001115
Owen Andersond9aa7d32010-11-02 00:05:05 +00001116 return RegNo | (Align << 4);
1117}
1118
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001119/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1120/// alignment operand for use in VLD-dup instructions. This is the same as
1121/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1122/// different for VLD4-dup.
1123unsigned ARMMCCodeEmitter::
1124getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1125 SmallVectorImpl<MCFixup> &Fixups) const {
1126 const MCOperand &Reg = MI.getOperand(Op);
1127 const MCOperand &Imm = MI.getOperand(Op + 1);
1128
1129 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1130 unsigned Align = 0;
1131
1132 switch (Imm.getImm()) {
1133 default: break;
1134 case 2:
1135 case 4:
1136 case 8: Align = 0x01; break;
1137 case 16: Align = 0x03; break;
1138 }
1139
1140 return RegNo | (Align << 4);
1141}
1142
Jim Grosbach806e80e2010-11-03 23:52:49 +00001143unsigned ARMMCCodeEmitter::
1144getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1145 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001146 const MCOperand &MO = MI.getOperand(Op);
1147 if (MO.getReg() == 0) return 0x0D;
1148 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001149}
1150
Jim Grosbach568eeed2010-09-17 18:46:17 +00001151void ARMMCCodeEmitter::
1152EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001153 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001154 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001155 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001156 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001157 uint64_t TSFlags = Desc.TSFlags;
1158 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001159 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001160 int Size;
1161 // Basic size info comes from the TSFlags field.
1162 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1163 default: llvm_unreachable("Unexpected instruction size!");
1164 case ARMII::Size2Bytes: Size = 2; break;
1165 case ARMII::Size4Bytes: Size = 4; break;
1166 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001167 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
Evan Cheng75972122011-01-13 07:58:56 +00001168 // Thumb 32-bit wide instructions need to emit the high order halfword
1169 // first.
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001170 if (Subtarget.isThumb() && Size == 4) {
1171 EmitConstant(Binary >> 16, 2, OS);
1172 EmitConstant(Binary & 0xffff, 2, OS);
1173 } else
1174 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001175 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001176}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001177
Jim Grosbach806e80e2010-11-03 23:52:49 +00001178#include "ARMGenMCCodeEmitter.inc"