blob: 1c6cdad2b9ac05fd7eaeef9dfd5439f71cab5e31 [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 Grosbach0de6ab32010-10-12 17:11:26 +000044 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
45
Jim Grosbach9af82ba2010-10-07 21:57:55 +000046 // getBinaryCodeForInstr - TableGen'erated function for getting the
47 // binary encoding for an instruction.
Jim Grosbach806e80e2010-11-03 23:52:49 +000048 unsigned getBinaryCodeForInstr(const MCInst &MI,
49 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000050
51 /// getMachineOpValue - Return binary encoding of operand. If the machine
52 /// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +000053 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
54 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach9af82ba2010-10-07 21:57:55 +000055
Jason W Kim837caa92010-11-18 23:37:15 +000056 /// getMovtImmOpValue - Return the encoding for the movw/movt pair
57 uint32_t getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
58 SmallVectorImpl<MCFixup> &Fixups) const;
59
Bill Wendling92b5a2e2010-11-03 01:49:29 +000060 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
Jim Grosbach806e80e2010-11-03 23:52:49 +000061 unsigned &Reg, unsigned &Imm,
62 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +000063
Jim Grosbach662a8162010-12-06 23:57:07 +000064 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
Bill Wendling09aa3f02010-12-09 00:39:08 +000065 /// BL branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +000066 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
67 SmallVectorImpl<MCFixup> &Fixups) const;
68
Bill Wendling09aa3f02010-12-09 00:39:08 +000069 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
70 /// BLX branch target.
71 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
72 SmallVectorImpl<MCFixup> &Fixups) const;
73
Jim Grosbache2467172010-12-10 18:21:33 +000074 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
75 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
76 SmallVectorImpl<MCFixup> &Fixups) const;
77
Jim Grosbach01086452010-12-10 17:13:40 +000078 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
79 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups) const;
81
Jim Grosbach027d6e82010-12-09 19:04:53 +000082 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
83 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +000084 SmallVectorImpl<MCFixup> &Fixups) const;
85
Jim Grosbachc466b932010-11-11 18:04:49 +000086 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
87 /// branch target.
88 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
89 SmallVectorImpl<MCFixup> &Fixups) const;
90
Owen Andersonc2666002010-12-13 19:31:11 +000091 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
92 /// immediate Thumb2 direct branch target.
93 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups) const;
95
96
Jim Grosbach5d14f9b2010-12-01 19:47:31 +000097 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
98 /// ADR label target.
99 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachd40963c2010-12-14 22:28:03 +0000101 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Andersona838a252010-12-14 00:36:49 +0000103 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
104 SmallVectorImpl<MCFixup> &Fixups) const;
105
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000106
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000107 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
108 /// operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000109 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000111
Bill Wendlingf4caf692010-12-14 03:36:38 +0000112 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
113 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
114 SmallVectorImpl<MCFixup> &Fixups)const;
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000115
Owen Anderson9d63d902010-12-01 19:18:46 +0000116 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
117 /// operand.
118 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
119 SmallVectorImpl<MCFixup> &Fixups) const;
120
121
Jim Grosbach54fea632010-11-09 17:20:53 +0000122 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
123 /// operand as needed by load/store instructions.
124 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
125 SmallVectorImpl<MCFixup> &Fixups) const;
126
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000127 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
128 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
129 SmallVectorImpl<MCFixup> &Fixups) const {
130 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
131 switch (Mode) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000132 default: assert(0 && "Unknown addressing sub-mode!");
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000133 case ARM_AM::da: return 0;
134 case ARM_AM::ia: return 1;
135 case ARM_AM::db: return 2;
136 case ARM_AM::ib: return 3;
137 }
138 }
Jim Grosbach99f53d12010-11-15 20:47:07 +0000139 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
140 ///
141 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
142 switch (ShOpc) {
143 default: llvm_unreachable("Unknown shift opc!");
144 case ARM_AM::no_shift:
145 case ARM_AM::lsl: return 0;
146 case ARM_AM::lsr: return 1;
147 case ARM_AM::asr: return 2;
148 case ARM_AM::ror:
149 case ARM_AM::rrx: return 3;
150 }
151 return 0;
152 }
153
154 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
155 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
156 SmallVectorImpl<MCFixup> &Fixups) const;
157
158 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
159 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups) const;
161
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000162 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
163 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
164 SmallVectorImpl<MCFixup> &Fixups) const;
165
Jim Grosbach570a9222010-11-11 01:09:40 +0000166 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
167 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
168 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach5d5eb9e2010-11-10 23:38:36 +0000169
Jim Grosbachd967cd02010-12-07 21:50:47 +0000170 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
171 /// operand.
172 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
173 SmallVectorImpl<MCFixup> &Fixups) const;
174
Bill Wendlingf4caf692010-12-14 03:36:38 +0000175 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
176 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000177 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000178
Bill Wendlingb8958b02010-12-08 01:57:09 +0000179 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
180 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups) const;
182
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000183 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000184 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3e556122010-10-26 22:37:02 +0000186
Jim Grosbach08bd5492010-10-12 23:00:24 +0000187 /// getCCOutOpValue - Return encoding of the 's' bit.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000188 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
189 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach08bd5492010-10-12 23:00:24 +0000190 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
191 // '1' respectively.
192 return MI.getOperand(Op).getReg() == ARM::CPSR;
193 }
Jim Grosbachef324d72010-10-12 23:53:58 +0000194
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000195 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000196 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
197 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach2a6a93d2010-10-12 23:18:08 +0000198 unsigned SoImm = MI.getOperand(Op).getImm();
199 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
200 assert(SoImmVal != -1 && "Not a valid so_imm value!");
201
202 // Encode rotate_imm.
203 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
204 << ARMII::SoRotImmShift;
205
206 // Encode immed_8.
207 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
208 return Binary;
209 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000210
Owen Anderson5de6d842010-11-12 21:12:40 +0000211 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
212 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
213 SmallVectorImpl<MCFixup> &Fixups) const {
214 unsigned SoImm = MI.getOperand(Op).getImm();
215 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
216 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
217 return Encoded;
218 }
Jim Grosbach08bd5492010-10-12 23:00:24 +0000219
Owen Anderson75579f72010-11-29 22:44:32 +0000220 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
221 SmallVectorImpl<MCFixup> &Fixups) const;
222 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
223 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson6af50f72010-11-30 00:14:31 +0000224 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
225 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson0e1bcdf2010-11-30 19:19:31 +0000226 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
227 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson75579f72010-11-29 22:44:32 +0000228
Jim Grosbachef324d72010-10-12 23:53:58 +0000229 /// getSORegOpValue - Return an encoded so_reg shifted register value.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000230 unsigned getSORegOpValue(const MCInst &MI, unsigned Op,
231 SmallVectorImpl<MCFixup> &Fixups) const;
Owen Anderson5de6d842010-11-12 21:12:40 +0000232 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
233 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbachef324d72010-10-12 23:53:58 +0000234
Jim Grosbach806e80e2010-11-03 23:52:49 +0000235 unsigned getRotImmOpValue(const MCInst &MI, unsigned Op,
236 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb35ad412010-10-13 19:56:10 +0000237 switch (MI.getOperand(Op).getImm()) {
238 default: assert (0 && "Not a valid rot_imm value!");
239 case 0: return 0;
240 case 8: return 1;
241 case 16: return 2;
242 case 24: return 3;
243 }
244 }
245
Jim Grosbach806e80e2010-11-03 23:52:49 +0000246 unsigned getImmMinusOneOpValue(const MCInst &MI, unsigned Op,
247 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000248 return MI.getOperand(Op).getImm() - 1;
249 }
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000250
Jim Grosbach806e80e2010-11-03 23:52:49 +0000251 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
252 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Anderson498ec202010-10-27 22:49:00 +0000253 return 64 - MI.getOperand(Op).getImm();
254 }
Jim Grosbach8abe32a2010-10-15 17:15:16 +0000255
Jim Grosbach806e80e2010-11-03 23:52:49 +0000256 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
257 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach3fea191052010-10-21 22:03:21 +0000258
Jim Grosbach806e80e2010-11-03 23:52:49 +0000259 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
260 SmallVectorImpl<MCFixup> &Fixups) const;
261 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
262 SmallVectorImpl<MCFixup> &Fixups) const;
Bob Wilson8e0c7b52010-11-30 00:00:42 +0000263 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
264 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach806e80e2010-11-03 23:52:49 +0000265 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach6b5252d2010-10-30 00:37:59 +0000267
Owen Andersonc7139a62010-11-11 19:07:48 +0000268 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
269 unsigned EncodedValue) const;
Owen Anderson57dac882010-11-11 21:36:43 +0000270 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000271 unsigned EncodedValue) const;
Owen Anderson8f143912010-11-11 23:12:55 +0000272 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
Bill Wendlingcf590262010-12-01 21:54:50 +0000273 unsigned EncodedValue) const;
274
275 unsigned VFPThumb2PostEncoder(const MCInst &MI,
276 unsigned EncodedValue) const;
Owen Andersonc7139a62010-11-11 19:07:48 +0000277
Jim Grosbach70933262010-11-04 01:12:30 +0000278 void EmitByte(unsigned char C, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000279 OS << (char)C;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000280 }
281
Jim Grosbach70933262010-11-04 01:12:30 +0000282 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000283 // Output the constant in little endian byte order.
284 for (unsigned i = 0; i != Size; ++i) {
Jim Grosbach70933262010-11-04 01:12:30 +0000285 EmitByte(Val & 255, OS);
Jim Grosbach568eeed2010-09-17 18:46:17 +0000286 Val >>= 8;
287 }
288 }
289
Jim Grosbach568eeed2010-09-17 18:46:17 +0000290 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
291 SmallVectorImpl<MCFixup> &Fixups) const;
Jim Grosbach568eeed2010-09-17 18:46:17 +0000292};
293
294} // end anonymous namespace
295
Bill Wendling0800ce72010-11-02 22:53:11 +0000296MCCodeEmitter *llvm::createARMMCCodeEmitter(const Target &, TargetMachine &TM,
297 MCContext &Ctx) {
Jim Grosbach568eeed2010-09-17 18:46:17 +0000298 return new ARMMCCodeEmitter(TM, Ctx);
299}
300
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000301/// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
302/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Andersonc7139a62010-11-11 19:07:48 +0000303/// Thumb2 mode.
304unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
305 unsigned EncodedValue) const {
306 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
307 if (Subtarget.isThumb2()) {
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000308 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
Owen Andersonc7139a62010-11-11 19:07:48 +0000309 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
310 // set to 1111.
311 unsigned Bit24 = EncodedValue & 0x01000000;
312 unsigned Bit28 = Bit24 << 4;
313 EncodedValue &= 0xEFFFFFFF;
314 EncodedValue |= Bit28;
315 EncodedValue |= 0x0F000000;
316 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000317
Owen Andersonc7139a62010-11-11 19:07:48 +0000318 return EncodedValue;
319}
320
Owen Anderson57dac882010-11-11 21:36:43 +0000321/// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000322/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson57dac882010-11-11 21:36:43 +0000323/// Thumb2 mode.
324unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
325 unsigned EncodedValue) const {
326 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
327 if (Subtarget.isThumb2()) {
328 EncodedValue &= 0xF0FFFFFF;
329 EncodedValue |= 0x09000000;
330 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000331
Owen Anderson57dac882010-11-11 21:36:43 +0000332 return EncodedValue;
333}
334
Owen Anderson8f143912010-11-11 23:12:55 +0000335/// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000336/// instructions, and rewrite them to their Thumb2 form if we are currently in
Owen Anderson8f143912010-11-11 23:12:55 +0000337/// Thumb2 mode.
338unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
339 unsigned EncodedValue) const {
340 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
341 if (Subtarget.isThumb2()) {
342 EncodedValue &= 0x00FFFFFF;
343 EncodedValue |= 0xEE000000;
344 }
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000345
Owen Anderson8f143912010-11-11 23:12:55 +0000346 return EncodedValue;
347}
348
Bill Wendlingcf590262010-12-01 21:54:50 +0000349/// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
350/// them to their Thumb2 form if we are currently in Thumb2 mode.
351unsigned ARMMCCodeEmitter::
352VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue) const {
353 if (TM.getSubtarget<ARMSubtarget>().isThumb2()) {
354 EncodedValue &= 0x0FFFFFFF;
355 EncodedValue |= 0xE0000000;
356 }
357 return EncodedValue;
358}
Owen Anderson57dac882010-11-11 21:36:43 +0000359
Jim Grosbach56ac9072010-10-08 21:45:55 +0000360/// getMachineOpValue - Return binary encoding of operand. If the machine
361/// operand requires relocation, record the relocation and return zero.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000362unsigned ARMMCCodeEmitter::
363getMachineOpValue(const MCInst &MI, const MCOperand &MO,
364 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000365 if (MO.isReg()) {
Bill Wendling0800ce72010-11-02 22:53:11 +0000366 unsigned Reg = MO.getReg();
367 unsigned RegNo = getARMRegisterNumbering(Reg);
Jim Grosbachd8a11c22010-10-29 23:21:03 +0000368
Jim Grosbachb0708d22010-11-30 23:51:41 +0000369 // Q registers are encoded as 2x their register number.
Bill Wendling0800ce72010-11-02 22:53:11 +0000370 switch (Reg) {
371 default:
372 return RegNo;
373 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
374 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
375 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
376 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
377 return 2 * RegNo;
Owen Anderson90d4cf92010-10-21 20:49:13 +0000378 }
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000379 } else if (MO.isImm()) {
Jim Grosbach56ac9072010-10-08 21:45:55 +0000380 return static_cast<unsigned>(MO.getImm());
Bill Wendlingbbbdcd42010-10-14 02:33:26 +0000381 } else if (MO.isFPImm()) {
382 return static_cast<unsigned>(APFloat(MO.getFPImm())
383 .bitcastToAPInt().getHiBits(32).getLimitedValue());
Jim Grosbach56ac9072010-10-08 21:45:55 +0000384 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000385
Jim Grosbach817c1a62010-11-19 00:27:09 +0000386 llvm_unreachable("Unable to encode MCOperand!");
Jim Grosbach56ac9072010-10-08 21:45:55 +0000387 return 0;
388}
389
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000390/// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000391bool ARMMCCodeEmitter::
392EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
393 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3e556122010-10-26 22:37:02 +0000394 const MCOperand &MO = MI.getOperand(OpIdx);
395 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Jim Grosbach9af3d1c2010-11-01 23:45:50 +0000396
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000397 Reg = getARMRegisterNumbering(MO.getReg());
398
399 int32_t SImm = MO1.getImm();
400 bool isAdd = true;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000401
Jim Grosbachab682a22010-10-28 18:34:10 +0000402 // Special value for #-0
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000403 if (SImm == INT32_MIN)
404 SImm = 0;
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000405
Jim Grosbachab682a22010-10-28 18:34:10 +0000406 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000407 if (SImm < 0) {
408 SImm = -SImm;
409 isAdd = false;
410 }
Bill Wendling5df0e0a2010-11-02 22:31:46 +0000411
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000412 Imm = SImm;
413 return isAdd;
414}
415
Bill Wendlingdff2f712010-12-08 23:01:43 +0000416/// getBranchTargetOpValue - Helper function to get the branch target operand,
417/// which is either an immediate or requires a fixup.
418static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
419 unsigned FixupKind,
420 SmallVectorImpl<MCFixup> &Fixups) {
421 const MCOperand &MO = MI.getOperand(OpIdx);
422
423 // If the destination is an immediate, we have nothing to do.
424 if (MO.isImm()) return MO.getImm();
425 assert(MO.isExpr() && "Unexpected branch target type!");
426 const MCExpr *Expr = MO.getExpr();
427 MCFixupKind Kind = MCFixupKind(FixupKind);
428 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
429
430 // All of the information is in the fixup.
431 return 0;
432}
433
434/// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
Jim Grosbach662a8162010-12-06 23:57:07 +0000435uint32_t ARMMCCodeEmitter::
436getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
437 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000438 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl, Fixups);
Jim Grosbach662a8162010-12-06 23:57:07 +0000439}
440
Bill Wendling09aa3f02010-12-09 00:39:08 +0000441/// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
442/// BLX branch target.
443uint32_t ARMMCCodeEmitter::
444getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
445 SmallVectorImpl<MCFixup> &Fixups) const {
446 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx, Fixups);
447}
448
Jim Grosbache2467172010-12-10 18:21:33 +0000449/// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
450uint32_t ARMMCCodeEmitter::
451getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
452 SmallVectorImpl<MCFixup> &Fixups) const {
453 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br, Fixups);
454}
455
Jim Grosbach01086452010-12-10 17:13:40 +0000456/// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
457uint32_t ARMMCCodeEmitter::
458getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
Jim Grosbache2467172010-12-10 18:21:33 +0000459 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach01086452010-12-10 17:13:40 +0000460 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc, Fixups);
461}
462
Jim Grosbach027d6e82010-12-09 19:04:53 +0000463/// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
Bill Wendlingdff2f712010-12-08 23:01:43 +0000464uint32_t ARMMCCodeEmitter::
Jim Grosbach027d6e82010-12-09 19:04:53 +0000465getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000466 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachb492a7c2010-12-09 19:50:12 +0000467 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000468}
469
470/// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
471/// target.
Jim Grosbachc466b932010-11-11 18:04:49 +0000472uint32_t ARMMCCodeEmitter::
473getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendlingdff2f712010-12-08 23:01:43 +0000474 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach092e2cd2010-12-10 23:41:10 +0000475 // FIXME: This really, really shouldn't use TargetMachine. We don't want
476 // coupling between MC and TM anywhere we can help it.
Owen Andersonfb20d892010-12-09 00:27:41 +0000477 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
478 if (Subtarget.isThumb2())
Owen Andersonc2666002010-12-13 19:31:11 +0000479 return
480 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups);
Bill Wendlingdff2f712010-12-08 23:01:43 +0000481 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_branch, Fixups);
Jim Grosbachc466b932010-11-11 18:04:49 +0000482}
483
Owen Andersonc2666002010-12-13 19:31:11 +0000484/// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
485/// immediate branch target.
486uint32_t ARMMCCodeEmitter::
487getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
488 SmallVectorImpl<MCFixup> &Fixups) const {
489 unsigned Val =
490 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups);
491 bool I = (Val & 0x800000);
492 bool J1 = (Val & 0x400000);
493 bool J2 = (Val & 0x200000);
494 if (I ^ J1)
495 Val &= ~0x400000;
496 else
497 Val |= 0x400000;
498
499 if (I ^ J2)
500 Val &= ~0x200000;
501 else
502 Val |= 0x200000;
503
504 return Val;
505}
506
Bill Wendlingdff2f712010-12-08 23:01:43 +0000507/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
508/// target.
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000509uint32_t ARMMCCodeEmitter::
510getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
511 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingdff2f712010-12-08 23:01:43 +0000512 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
513 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
514 Fixups);
Jim Grosbach5d14f9b2010-12-01 19:47:31 +0000515}
516
Owen Andersona838a252010-12-14 00:36:49 +0000517/// getAdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
518/// target.
519uint32_t ARMMCCodeEmitter::
520getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
521 SmallVectorImpl<MCFixup> &Fixups) const {
522 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
523 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
524 Fixups);
525}
526
Jim Grosbachd40963c2010-12-14 22:28:03 +0000527/// getAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
528/// target.
529uint32_t ARMMCCodeEmitter::
530getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
531 SmallVectorImpl<MCFixup> &Fixups) const {
532 assert(MI.getOperand(OpIdx).isExpr() && "Unexpected adr target type!");
533 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
534 Fixups);
535}
536
Bill Wendlingf4caf692010-12-14 03:36:38 +0000537/// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
538/// operand.
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000539uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000540getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
541 SmallVectorImpl<MCFixup> &) const {
542 // [Rn, Rm]
543 // {5-3} = Rm
544 // {2-0} = Rn
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000545 const MCOperand &MO1 = MI.getOperand(OpIdx);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000546 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
Owen Anderson0f4b60d2010-12-10 22:11:13 +0000547 unsigned Rn = getARMRegisterNumbering(MO1.getReg());
548 unsigned Rm = getARMRegisterNumbering(MO2.getReg());
549 return (Rm << 3) | Rn;
550}
551
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000552/// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000553uint32_t ARMMCCodeEmitter::
554getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
555 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000556 // {17-13} = reg
557 // {12} = (U)nsigned (add == '1', sub == '0')
558 // {11-0} = imm12
559 unsigned Reg, Imm12;
Jim Grosbach70933262010-11-04 01:12:30 +0000560 bool isAdd = true;
561 // If The first operand isn't a register, we have a label reference.
562 const MCOperand &MO = MI.getOperand(OpIdx);
Owen Andersoneb6779c2010-12-07 00:45:21 +0000563 const MCOperand &MO2 = MI.getOperand(OpIdx+1);
564 if (!MO.isReg() || (MO.getReg() == ARM::PC && MO2.isExpr())) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000565 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000566 Imm12 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000567 isAdd = false ; // 'U' bit is set as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000568
Owen Andersoneb6779c2010-12-07 00:45:21 +0000569 const MCExpr *Expr = 0;
570 if (!MO.isReg())
571 Expr = MO.getExpr();
572 else
573 Expr = MO2.getExpr();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000574
Owen Andersond7b3f582010-12-09 01:51:07 +0000575 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
576 MCFixupKind Kind;
577 if (Subtarget.isThumb2())
578 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
579 else
580 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
Jim Grosbach70933262010-11-04 01:12:30 +0000581 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
582
583 ++MCNumCPRelocations;
584 } else
585 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000586
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000587 uint32_t Binary = Imm12 & 0xfff;
588 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbachab682a22010-10-28 18:34:10 +0000589 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000590 Binary |= (1 << 12);
591 Binary |= (Reg << 13);
592 return Binary;
593}
594
Owen Anderson9d63d902010-12-01 19:18:46 +0000595/// getT2AddrModeImm8s4OpValue - Return encoding info for
596/// 'reg +/- imm8<<2' operand.
597uint32_t ARMMCCodeEmitter::
598getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
599 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach90cc5332010-12-10 21:05:07 +0000600 // {12-9} = reg
601 // {8} = (U)nsigned (add == '1', sub == '0')
602 // {7-0} = imm8
Owen Anderson9d63d902010-12-01 19:18:46 +0000603 unsigned Reg, Imm8;
604 bool isAdd = true;
605 // If The first operand isn't a register, we have a label reference.
606 const MCOperand &MO = MI.getOperand(OpIdx);
607 if (!MO.isReg()) {
608 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
609 Imm8 = 0;
610 isAdd = false ; // 'U' bit is set as part of the fixup.
611
612 assert(MO.isExpr() && "Unexpected machine operand type!");
613 const MCExpr *Expr = MO.getExpr();
614 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
615 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
616
617 ++MCNumCPRelocations;
618 } else
619 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
620
621 uint32_t Binary = (Imm8 >> 2) & 0xff;
622 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
623 if (isAdd)
Jim Grosbach90cc5332010-12-10 21:05:07 +0000624 Binary |= (1 << 8);
Owen Anderson9d63d902010-12-01 19:18:46 +0000625 Binary |= (Reg << 9);
626 return Binary;
627}
628
Jason W Kim86a97f22011-01-12 00:19:25 +0000629// FIXME: This routine needs to handle more MCExpr types
630static const MCSymbolRefExpr *FindLHSymExpr(const MCExpr *E) {
631 // recurse left child until finding a MCSymbolRefExpr
632 switch (E->getKind()) {
633 case MCExpr::SymbolRef:
634 return cast<MCSymbolRefExpr>(E);
635 case MCExpr::Binary:
636 return FindLHSymExpr(cast<MCBinaryExpr>(E)->getLHS());
637 default:
638 return NULL;
639 }
640}
641
642// FIXME: This routine assumes that a binary
643// expression will always result in a PCRel expression
644// In reality, its only true if one or more subexpressions
645// is itself a PCRel (i.e. "." in asm or some other pcrel construct)
646// but this is good enough for now.
647static bool EvaluateAsPCRel(const MCExpr *Expr) {
648 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000649 default: assert(0 && "Unexpected expression type");
Jason W Kim86a97f22011-01-12 00:19:25 +0000650 case MCExpr::SymbolRef: return false;
651 case MCExpr::Binary: return true;
Jason W Kim86a97f22011-01-12 00:19:25 +0000652 }
653}
654
Jim Grosbach54fea632010-11-09 17:20:53 +0000655uint32_t ARMMCCodeEmitter::
Jason W Kim837caa92010-11-18 23:37:15 +0000656getMovtImmOpValue(const MCInst &MI, unsigned OpIdx,
657 SmallVectorImpl<MCFixup> &Fixups) const {
658 // {20-16} = imm{15-12}
659 // {11-0} = imm{11-0}
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000660 const MCOperand &MO = MI.getOperand(OpIdx);
Jason W Kim837caa92010-11-18 23:37:15 +0000661 if (MO.isImm()) {
662 return static_cast<unsigned>(MO.getImm());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000663 } else if (const MCSymbolRefExpr *Expr =
Jason W Kim86a97f22011-01-12 00:19:25 +0000664 FindLHSymExpr(MO.getExpr())) {
665 // FIXME: :lower16: and :upper16: should be applicable to
666 // to whole expression, not just symbolrefs
667 // Until that change takes place, this hack is required to
668 // generate working code.
669 const MCExpr *OrigExpr = MO.getExpr();
Jason W Kim837caa92010-11-18 23:37:15 +0000670 MCFixupKind Kind;
671 switch (Expr->getKind()) {
Matt Beaumont-Gay5f8a9172011-01-12 18:02:55 +0000672 default: assert(0 && "Unsupported ARMFixup");
Jason W Kim837caa92010-11-18 23:37:15 +0000673 case MCSymbolRefExpr::VK_ARM_HI16:
674 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16);
Jason W Kim86a97f22011-01-12 00:19:25 +0000675 if (EvaluateAsPCRel(OrigExpr))
676 Kind = MCFixupKind(ARM::fixup_arm_movt_hi16_pcrel);
Jason W Kim837caa92010-11-18 23:37:15 +0000677 break;
678 case MCSymbolRefExpr::VK_ARM_LO16:
679 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16);
Jason W Kim86a97f22011-01-12 00:19:25 +0000680 if (EvaluateAsPCRel(OrigExpr))
681 Kind = MCFixupKind(ARM::fixup_arm_movw_lo16_pcrel);
Jason W Kim837caa92010-11-18 23:37:15 +0000682 break;
Jason W Kim837caa92010-11-18 23:37:15 +0000683 }
Jason W Kim86a97f22011-01-12 00:19:25 +0000684 Fixups.push_back(MCFixup::Create(0, OrigExpr, Kind));
Jason W Kim837caa92010-11-18 23:37:15 +0000685 return 0;
Jim Grosbach817c1a62010-11-19 00:27:09 +0000686 };
687 llvm_unreachable("Unsupported MCExpr type in MCOperand!");
Jason W Kim837caa92010-11-18 23:37:15 +0000688 return 0;
689}
690
691uint32_t ARMMCCodeEmitter::
Jim Grosbach54fea632010-11-09 17:20:53 +0000692getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
693 SmallVectorImpl<MCFixup> &Fixups) const {
694 const MCOperand &MO = MI.getOperand(OpIdx);
695 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
696 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
697 unsigned Rn = getARMRegisterNumbering(MO.getReg());
698 unsigned Rm = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach54fea632010-11-09 17:20:53 +0000699 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
700 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
Jim Grosbach99f53d12010-11-15 20:47:07 +0000701 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
702 unsigned SBits = getShiftOp(ShOp);
Jim Grosbach54fea632010-11-09 17:20:53 +0000703
704 // {16-13} = Rn
705 // {12} = isAdd
706 // {11-0} = shifter
707 // {3-0} = Rm
708 // {4} = 0
709 // {6-5} = type
710 // {11-7} = imm
Jim Grosbach570a9222010-11-11 01:09:40 +0000711 uint32_t Binary = Rm;
Jim Grosbach54fea632010-11-09 17:20:53 +0000712 Binary |= Rn << 13;
713 Binary |= SBits << 5;
714 Binary |= ShImm << 7;
715 if (isAdd)
716 Binary |= 1 << 12;
717 return Binary;
718}
719
Jim Grosbach570a9222010-11-11 01:09:40 +0000720uint32_t ARMMCCodeEmitter::
Jim Grosbach99f53d12010-11-15 20:47:07 +0000721getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
722 SmallVectorImpl<MCFixup> &Fixups) const {
723 // {17-14} Rn
724 // {13} 1 == imm12, 0 == Rm
725 // {12} isAdd
726 // {11-0} imm12/Rm
727 const MCOperand &MO = MI.getOperand(OpIdx);
728 unsigned Rn = getARMRegisterNumbering(MO.getReg());
729 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups);
730 Binary |= Rn << 14;
731 return Binary;
732}
733
734uint32_t ARMMCCodeEmitter::
735getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
736 SmallVectorImpl<MCFixup> &Fixups) const {
737 // {13} 1 == imm12, 0 == Rm
738 // {12} isAdd
739 // {11-0} imm12/Rm
740 const MCOperand &MO = MI.getOperand(OpIdx);
741 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
742 unsigned Imm = MO1.getImm();
743 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
744 bool isReg = MO.getReg() != 0;
745 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
746 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
747 if (isReg) {
748 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
749 Binary <<= 7; // Shift amount is bits [11:7]
750 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
751 Binary |= getARMRegisterNumbering(MO.getReg()); // Rm is bits [3:0]
752 }
753 return Binary | (isAdd << 12) | (isReg << 13);
754}
755
756uint32_t ARMMCCodeEmitter::
Jim Grosbach7eab97f2010-11-11 16:55:29 +0000757getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
758 SmallVectorImpl<MCFixup> &Fixups) const {
759 // {9} 1 == imm8, 0 == Rm
760 // {8} isAdd
761 // {7-4} imm7_4/zero
762 // {3-0} imm3_0/Rm
763 const MCOperand &MO = MI.getOperand(OpIdx);
764 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
765 unsigned Imm = MO1.getImm();
766 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
767 bool isImm = MO.getReg() == 0;
768 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
769 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
770 if (!isImm)
771 Imm8 = getARMRegisterNumbering(MO.getReg());
772 return Imm8 | (isAdd << 8) | (isImm << 9);
773}
774
775uint32_t ARMMCCodeEmitter::
Jim Grosbach570a9222010-11-11 01:09:40 +0000776getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
777 SmallVectorImpl<MCFixup> &Fixups) const {
778 // {13} 1 == imm8, 0 == Rm
779 // {12-9} Rn
780 // {8} isAdd
781 // {7-4} imm7_4/zero
782 // {3-0} imm3_0/Rm
783 const MCOperand &MO = MI.getOperand(OpIdx);
784 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
785 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
786 unsigned Rn = getARMRegisterNumbering(MO.getReg());
787 unsigned Imm = MO2.getImm();
788 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
789 bool isImm = MO1.getReg() == 0;
790 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
791 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
792 if (!isImm)
793 Imm8 = getARMRegisterNumbering(MO1.getReg());
794 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
795}
796
Bill Wendlingb8958b02010-12-08 01:57:09 +0000797/// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
Jim Grosbachd967cd02010-12-07 21:50:47 +0000798uint32_t ARMMCCodeEmitter::
799getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
800 SmallVectorImpl<MCFixup> &Fixups) const {
801 // [SP, #imm]
802 // {7-0} = imm8
Jim Grosbachd967cd02010-12-07 21:50:47 +0000803 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000804 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
805 "Unexpected base register!");
Bill Wendling7a905a82010-12-15 23:32:27 +0000806
Jim Grosbachd967cd02010-12-07 21:50:47 +0000807 // The immediate is already shifted for the implicit zeroes, so no change
808 // here.
809 return MO1.getImm() & 0xff;
810}
811
Bill Wendlingf4caf692010-12-14 03:36:38 +0000812/// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
Bill Wendling272df512010-12-09 21:49:07 +0000813uint32_t ARMMCCodeEmitter::
Bill Wendlingf4caf692010-12-14 03:36:38 +0000814getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
Bill Wendling22447ae2010-12-15 08:51:02 +0000815 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000816 // [Rn, #imm]
817 // {7-3} = imm5
818 // {2-0} = Rn
819 const MCOperand &MO = MI.getOperand(OpIdx);
820 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000821 unsigned Rn = getARMRegisterNumbering(MO.getReg());
Matt Beaumont-Gay656b3d22010-12-16 01:34:26 +0000822 unsigned Imm5 = MO1.getImm();
Bill Wendling272df512010-12-09 21:49:07 +0000823 return ((Imm5 & 0x1f) << 3) | Rn;
Bill Wendling1fd374e2010-11-30 22:57:21 +0000824}
825
Bill Wendlingb8958b02010-12-08 01:57:09 +0000826/// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
827uint32_t ARMMCCodeEmitter::
828getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
829 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling09aa3f02010-12-09 00:39:08 +0000830 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups);
Bill Wendlingb8958b02010-12-08 01:57:09 +0000831}
832
Jim Grosbach5177f792010-12-01 21:09:40 +0000833/// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +0000834uint32_t ARMMCCodeEmitter::
835getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
836 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000837 // {12-9} = reg
838 // {8} = (U)nsigned (add == '1', sub == '0')
839 // {7-0} = imm8
840 unsigned Reg, Imm8;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000841 bool isAdd;
Jim Grosbach70933262010-11-04 01:12:30 +0000842 // If The first operand isn't a register, we have a label reference.
843 const MCOperand &MO = MI.getOperand(OpIdx);
844 if (!MO.isReg()) {
Jim Grosbach679cbd32010-11-09 01:37:15 +0000845 Reg = getARMRegisterNumbering(ARM::PC); // Rn is PC.
Jim Grosbach70933262010-11-04 01:12:30 +0000846 Imm8 = 0;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000847 isAdd = false; // 'U' bit is handled as part of the fixup.
Jim Grosbach70933262010-11-04 01:12:30 +0000848
849 assert(MO.isExpr() && "Unexpected machine operand type!");
850 const MCExpr *Expr = MO.getExpr();
Owen Andersond8e351b2010-12-08 00:18:36 +0000851 MCFixupKind Kind;
852 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
853 if (Subtarget.isThumb2())
854 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
855 else
856 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
Jim Grosbach70933262010-11-04 01:12:30 +0000857 Fixups.push_back(MCFixup::Create(0, Expr, Kind));
858
859 ++MCNumCPRelocations;
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000860 } else {
Jim Grosbach70933262010-11-04 01:12:30 +0000861 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups);
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000862 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
863 }
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000864
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000865 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
866 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
Jim Grosbach97dd28f2010-11-30 22:40:36 +0000867 if (isAdd)
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000868 Binary |= (1 << 8);
869 Binary |= (Reg << 9);
Jim Grosbach3e556122010-10-26 22:37:02 +0000870 return Binary;
871}
872
Jim Grosbach806e80e2010-11-03 23:52:49 +0000873unsigned ARMMCCodeEmitter::
874getSORegOpValue(const MCInst &MI, unsigned OpIdx,
875 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +0000876 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
877 // shifted. The second is either Rs, the amount to shift by, or reg0 in which
878 // case the imm contains the amount to shift by.
Jim Grosbach35b2de02010-11-03 22:03:20 +0000879 //
Jim Grosbachef324d72010-10-12 23:53:58 +0000880 // {3-0} = Rm.
Bill Wendling0800ce72010-11-02 22:53:11 +0000881 // {4} = 1 if reg shift, 0 if imm shift
Jim Grosbachef324d72010-10-12 23:53:58 +0000882 // {6-5} = type
883 // If reg shift:
Jim Grosbachef324d72010-10-12 23:53:58 +0000884 // {11-8} = Rs
Bill Wendling0800ce72010-11-02 22:53:11 +0000885 // {7} = 0
Jim Grosbachef324d72010-10-12 23:53:58 +0000886 // else (imm shift)
887 // {11-7} = imm
888
889 const MCOperand &MO = MI.getOperand(OpIdx);
890 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
891 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
892 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
893
894 // Encode Rm.
895 unsigned Binary = getARMRegisterNumbering(MO.getReg());
896
897 // Encode the shift opcode.
898 unsigned SBits = 0;
899 unsigned Rs = MO1.getReg();
900 if (Rs) {
901 // Set shift operand (bit[7:4]).
902 // LSL - 0001
903 // LSR - 0011
904 // ASR - 0101
905 // ROR - 0111
906 // RRX - 0110 and bit[11:8] clear.
907 switch (SOpc) {
908 default: llvm_unreachable("Unknown shift opc!");
909 case ARM_AM::lsl: SBits = 0x1; break;
910 case ARM_AM::lsr: SBits = 0x3; break;
911 case ARM_AM::asr: SBits = 0x5; break;
912 case ARM_AM::ror: SBits = 0x7; break;
913 case ARM_AM::rrx: SBits = 0x6; break;
914 }
915 } else {
916 // Set shift operand (bit[6:4]).
917 // LSL - 000
918 // LSR - 010
919 // ASR - 100
920 // ROR - 110
921 switch (SOpc) {
922 default: llvm_unreachable("Unknown shift opc!");
923 case ARM_AM::lsl: SBits = 0x0; break;
924 case ARM_AM::lsr: SBits = 0x2; break;
925 case ARM_AM::asr: SBits = 0x4; break;
926 case ARM_AM::ror: SBits = 0x6; break;
927 }
928 }
Bill Wendling0800ce72010-11-02 22:53:11 +0000929
Jim Grosbachef324d72010-10-12 23:53:58 +0000930 Binary |= SBits << 4;
931 if (SOpc == ARM_AM::rrx)
932 return Binary;
933
934 // Encode the shift operation Rs or shift_imm (except rrx).
935 if (Rs) {
936 // Encode Rs bit[11:8].
937 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
938 return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift);
939 }
940
941 // Encode shift_imm bit[11:7].
942 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7;
943}
944
Jim Grosbach806e80e2010-11-03 23:52:49 +0000945unsigned ARMMCCodeEmitter::
Owen Anderson75579f72010-11-29 22:44:32 +0000946getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
947 SmallVectorImpl<MCFixup> &Fixups) const {
948 const MCOperand &MO1 = MI.getOperand(OpNum);
949 const MCOperand &MO2 = MI.getOperand(OpNum+1);
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000950 const MCOperand &MO3 = MI.getOperand(OpNum+2);
951
Owen Anderson75579f72010-11-29 22:44:32 +0000952 // Encoded as [Rn, Rm, imm].
953 // FIXME: Needs fixup support.
954 unsigned Value = getARMRegisterNumbering(MO1.getReg());
955 Value <<= 4;
956 Value |= getARMRegisterNumbering(MO2.getReg());
957 Value <<= 2;
958 Value |= MO3.getImm();
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000959
Owen Anderson75579f72010-11-29 22:44:32 +0000960 return Value;
961}
962
963unsigned ARMMCCodeEmitter::
964getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
965 SmallVectorImpl<MCFixup> &Fixups) const {
966 const MCOperand &MO1 = MI.getOperand(OpNum);
967 const MCOperand &MO2 = MI.getOperand(OpNum+1);
968
969 // FIXME: Needs fixup support.
970 unsigned Value = getARMRegisterNumbering(MO1.getReg());
Jim Grosbach7bf4c022010-12-10 21:57:34 +0000971
Owen Anderson75579f72010-11-29 22:44:32 +0000972 // Even though the immediate is 8 bits long, we need 9 bits in order
973 // to represent the (inverse of the) sign bit.
974 Value <<= 9;
Owen Anderson6af50f72010-11-30 00:14:31 +0000975 int32_t tmp = (int32_t)MO2.getImm();
976 if (tmp < 0)
977 tmp = abs(tmp);
978 else
979 Value |= 256; // Set the ADD bit
980 Value |= tmp & 255;
981 return Value;
982}
983
984unsigned ARMMCCodeEmitter::
985getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
986 SmallVectorImpl<MCFixup> &Fixups) const {
987 const MCOperand &MO1 = MI.getOperand(OpNum);
988
989 // FIXME: Needs fixup support.
990 unsigned Value = 0;
991 int32_t tmp = (int32_t)MO1.getImm();
992 if (tmp < 0)
993 tmp = abs(tmp);
994 else
995 Value |= 256; // Set the ADD bit
996 Value |= tmp & 255;
Owen Anderson75579f72010-11-29 22:44:32 +0000997 return Value;
998}
999
1000unsigned ARMMCCodeEmitter::
Owen Anderson0e1bcdf2010-11-30 19:19:31 +00001001getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1002 SmallVectorImpl<MCFixup> &Fixups) const {
1003 const MCOperand &MO1 = MI.getOperand(OpNum);
1004
1005 // FIXME: Needs fixup support.
1006 unsigned Value = 0;
1007 int32_t tmp = (int32_t)MO1.getImm();
1008 if (tmp < 0)
1009 tmp = abs(tmp);
1010 else
1011 Value |= 4096; // Set the ADD bit
1012 Value |= tmp & 4095;
1013 return Value;
1014}
1015
1016unsigned ARMMCCodeEmitter::
Owen Anderson5de6d842010-11-12 21:12:40 +00001017getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1018 SmallVectorImpl<MCFixup> &Fixups) const {
1019 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1020 // shifted. The second is the amount to shift by.
1021 //
1022 // {3-0} = Rm.
1023 // {4} = 0
1024 // {6-5} = type
1025 // {11-7} = imm
1026
1027 const MCOperand &MO = MI.getOperand(OpIdx);
1028 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1029 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1030
1031 // Encode Rm.
1032 unsigned Binary = getARMRegisterNumbering(MO.getReg());
1033
1034 // Encode the shift opcode.
1035 unsigned SBits = 0;
1036 // Set shift operand (bit[6:4]).
1037 // LSL - 000
1038 // LSR - 010
1039 // ASR - 100
1040 // ROR - 110
1041 switch (SOpc) {
1042 default: llvm_unreachable("Unknown shift opc!");
1043 case ARM_AM::lsl: SBits = 0x0; break;
1044 case ARM_AM::lsr: SBits = 0x2; break;
1045 case ARM_AM::asr: SBits = 0x4; break;
1046 case ARM_AM::ror: SBits = 0x6; break;
1047 }
1048
1049 Binary |= SBits << 4;
1050 if (SOpc == ARM_AM::rrx)
1051 return Binary;
1052
1053 // Encode shift_imm bit[11:7].
1054 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1055}
1056
1057unsigned ARMMCCodeEmitter::
Jim Grosbach806e80e2010-11-03 23:52:49 +00001058getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1059 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbach3fea191052010-10-21 22:03:21 +00001060 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1061 // msb of the mask.
1062 const MCOperand &MO = MI.getOperand(Op);
1063 uint32_t v = ~MO.getImm();
1064 uint32_t lsb = CountTrailingZeros_32(v);
1065 uint32_t msb = (32 - CountLeadingZeros_32 (v)) - 1;
1066 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1067 return lsb | (msb << 5);
1068}
1069
Jim Grosbach806e80e2010-11-03 23:52:49 +00001070unsigned ARMMCCodeEmitter::
1071getRegisterListOpValue(const MCInst &MI, unsigned Op,
Bill Wendling5e559a22010-11-09 00:30:18 +00001072 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling6bc105a2010-11-17 00:45:23 +00001073 // VLDM/VSTM:
1074 // {12-8} = Vd
1075 // {7-0} = Number of registers
1076 //
1077 // LDM/STM:
1078 // {15-0} = Bitfield of GPRs.
1079 unsigned Reg = MI.getOperand(Op).getReg();
1080 bool SPRRegs = ARM::SPRRegClass.contains(Reg);
1081 bool DPRRegs = ARM::DPRRegClass.contains(Reg);
1082
Bill Wendling5e559a22010-11-09 00:30:18 +00001083 unsigned Binary = 0;
Bill Wendling6bc105a2010-11-17 00:45:23 +00001084
1085 if (SPRRegs || DPRRegs) {
1086 // VLDM/VSTM
1087 unsigned RegNo = getARMRegisterNumbering(Reg);
1088 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1089 Binary |= (RegNo & 0x1f) << 8;
1090 if (SPRRegs)
1091 Binary |= NumRegs;
1092 else
1093 Binary |= NumRegs * 2;
1094 } else {
1095 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1096 unsigned RegNo = getARMRegisterNumbering(MI.getOperand(I).getReg());
1097 Binary |= 1 << RegNo;
1098 }
Bill Wendling5e559a22010-11-09 00:30:18 +00001099 }
Bill Wendling6bc105a2010-11-17 00:45:23 +00001100
Jim Grosbach6b5252d2010-10-30 00:37:59 +00001101 return Binary;
1102}
1103
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001104/// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1105/// with the alignment operand.
Jim Grosbach806e80e2010-11-03 23:52:49 +00001106unsigned ARMMCCodeEmitter::
1107getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1108 SmallVectorImpl<MCFixup> &Fixups) const {
Owen Andersond9aa7d32010-11-02 00:05:05 +00001109 const MCOperand &Reg = MI.getOperand(Op);
Bill Wendling0800ce72010-11-02 22:53:11 +00001110 const MCOperand &Imm = MI.getOperand(Op + 1);
Jim Grosbach35b2de02010-11-03 22:03:20 +00001111
Owen Andersond9aa7d32010-11-02 00:05:05 +00001112 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
Bill Wendling0800ce72010-11-02 22:53:11 +00001113 unsigned Align = 0;
1114
1115 switch (Imm.getImm()) {
1116 default: break;
1117 case 2:
1118 case 4:
1119 case 8: Align = 0x01; break;
1120 case 16: Align = 0x02; break;
1121 case 32: Align = 0x03; break;
Owen Andersond9aa7d32010-11-02 00:05:05 +00001122 }
Bill Wendling0800ce72010-11-02 22:53:11 +00001123
Owen Andersond9aa7d32010-11-02 00:05:05 +00001124 return RegNo | (Align << 4);
1125}
1126
Bob Wilson8e0c7b52010-11-30 00:00:42 +00001127/// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1128/// alignment operand for use in VLD-dup instructions. This is the same as
1129/// getAddrMode6AddressOpValue except for the alignment encoding, which is
1130/// different for VLD4-dup.
1131unsigned ARMMCCodeEmitter::
1132getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1133 SmallVectorImpl<MCFixup> &Fixups) const {
1134 const MCOperand &Reg = MI.getOperand(Op);
1135 const MCOperand &Imm = MI.getOperand(Op + 1);
1136
1137 unsigned RegNo = getARMRegisterNumbering(Reg.getReg());
1138 unsigned Align = 0;
1139
1140 switch (Imm.getImm()) {
1141 default: break;
1142 case 2:
1143 case 4:
1144 case 8: Align = 0x01; break;
1145 case 16: Align = 0x03; break;
1146 }
1147
1148 return RegNo | (Align << 4);
1149}
1150
Jim Grosbach806e80e2010-11-03 23:52:49 +00001151unsigned ARMMCCodeEmitter::
1152getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1153 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Wendling0800ce72010-11-02 22:53:11 +00001154 const MCOperand &MO = MI.getOperand(Op);
1155 if (MO.getReg() == 0) return 0x0D;
1156 return MO.getReg();
Owen Andersoncf667be2010-11-02 01:24:55 +00001157}
1158
Jim Grosbach568eeed2010-09-17 18:46:17 +00001159void ARMMCCodeEmitter::
1160EncodeInstruction(const MCInst &MI, raw_ostream &OS,
Jim Grosbach806e80e2010-11-03 23:52:49 +00001161 SmallVectorImpl<MCFixup> &Fixups) const {
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001162 const ARMSubtarget &Subtarget = TM.getSubtarget<ARMSubtarget>();
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001163 // Pseudo instructions don't get encoded.
Bill Wendling7292e0a2010-11-02 22:44:12 +00001164 const TargetInstrDesc &Desc = TII.get(MI.getOpcode());
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001165 uint64_t TSFlags = Desc.TSFlags;
1166 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
Jim Grosbachd6d4b422010-10-07 22:12:50 +00001167 return;
Jim Grosbache50e6bc2010-11-11 23:41:09 +00001168 int Size;
1169 // Basic size info comes from the TSFlags field.
1170 switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
1171 default: llvm_unreachable("Unexpected instruction size!");
1172 case ARMII::Size2Bytes: Size = 2; break;
1173 case ARMII::Size4Bytes: Size = 4; break;
1174 }
Jim Grosbachd91f4e42010-12-03 22:31:40 +00001175 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups);
1176 // Thumb 32-bit wide instructions need to be have the high order halfword
1177 // emitted first.
1178 if (Subtarget.isThumb() && Size == 4) {
1179 EmitConstant(Binary >> 16, 2, OS);
1180 EmitConstant(Binary & 0xffff, 2, OS);
1181 } else
1182 EmitConstant(Binary, Size, OS);
Bill Wendling7292e0a2010-11-02 22:44:12 +00001183 ++MCNumEmitted; // Keep track of the # of mi's emitted.
Jim Grosbach568eeed2010-09-17 18:46:17 +00001184}
Jim Grosbach9af82ba2010-10-07 21:57:55 +00001185
Jim Grosbach806e80e2010-11-03 23:52:49 +00001186#include "ARMGenMCCodeEmitter.inc"