1 //===-- ARM/ARMMCCodeEmitter.cpp - Convert ARM code to machine code -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ARMMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mccodeemitter"
15 #include "MCTargetDesc/ARMMCTargetDesc.h"
16 #include "MCTargetDesc/ARMAddressingModes.h"
17 #include "MCTargetDesc/ARMBaseInfo.h"
18 #include "MCTargetDesc/ARMFixupKinds.h"
19 #include "MCTargetDesc/ARMMCExpr.h"
20 #include "llvm/ADT/APFloat.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstrInfo.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSubtargetInfo.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
34 STATISTIC(MCNumEmitted, "Number of MC instructions emitted.");
35 STATISTIC(MCNumCPRelocations, "Number of constant pool relocations created.");
38 class ARMMCCodeEmitter : public MCCodeEmitter {
39 ARMMCCodeEmitter(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
40 void operator=(const ARMMCCodeEmitter &) LLVM_DELETED_FUNCTION;
41 const MCInstrInfo &MCII;
42 const MCSubtargetInfo &STI;
46 ARMMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
48 : MCII(mcii), STI(sti), CTX(ctx) {
51 ~ARMMCCodeEmitter() {}
53 bool isThumb() const {
54 // FIXME: Can tablegen auto-generate this?
55 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
57 bool isThumb2() const {
58 return isThumb() && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
60 bool isTargetMachO() const {
61 Triple TT(STI.getTargetTriple());
62 return TT.isOSBinFormatMachO();
65 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
67 // getBinaryCodeForInstr - TableGen'erated function for getting the
68 // binary encoding for an instruction.
69 uint64_t getBinaryCodeForInstr(const MCInst &MI,
70 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const;
73 /// getMachineOpValue - Return binary encoding of operand. If the machine
74 /// operand requires relocation, record the relocation and return zero.
75 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
79 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
80 /// the specified operand. This is used for operands with :lower16: and
81 /// :upper16: prefixes.
82 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
83 SmallVectorImpl<MCFixup> &Fixups,
84 const MCSubtargetInfo &STI) const;
86 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
87 unsigned &Reg, unsigned &Imm,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
91 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
93 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
94 SmallVectorImpl<MCFixup> &Fixups,
95 const MCSubtargetInfo &STI) const;
97 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
98 /// BLX branch target.
99 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
100 SmallVectorImpl<MCFixup> &Fixups,
101 const MCSubtargetInfo &STI) const;
103 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
104 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
105 SmallVectorImpl<MCFixup> &Fixups,
106 const MCSubtargetInfo &STI) const;
108 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
109 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
110 SmallVectorImpl<MCFixup> &Fixups,
111 const MCSubtargetInfo &STI) const;
113 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
114 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
115 SmallVectorImpl<MCFixup> &Fixups,
116 const MCSubtargetInfo &STI) const;
118 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
120 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
121 SmallVectorImpl<MCFixup> &Fixups,
122 const MCSubtargetInfo &STI) const;
124 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
125 /// immediate Thumb2 direct branch target.
126 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
127 SmallVectorImpl<MCFixup> &Fixups,
128 const MCSubtargetInfo &STI) const;
130 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
132 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups,
134 const MCSubtargetInfo &STI) const;
135 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
136 SmallVectorImpl<MCFixup> &Fixups,
137 const MCSubtargetInfo &STI) const;
138 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
139 SmallVectorImpl<MCFixup> &Fixups,
140 const MCSubtargetInfo &STI) const;
142 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
143 /// ADR label target.
144 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups,
146 const MCSubtargetInfo &STI) const;
147 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const;
150 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
151 SmallVectorImpl<MCFixup> &Fixups,
152 const MCSubtargetInfo &STI) const;
155 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
157 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
158 SmallVectorImpl<MCFixup> &Fixups,
159 const MCSubtargetInfo &STI) const;
161 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
162 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
163 SmallVectorImpl<MCFixup> &Fixups,
164 const MCSubtargetInfo &STI) const;
166 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
168 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
169 SmallVectorImpl<MCFixup> &Fixups,
170 const MCSubtargetInfo &STI) const;
172 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
174 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
175 SmallVectorImpl<MCFixup> &Fixups,
176 const MCSubtargetInfo &STI) const;
178 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
180 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
181 SmallVectorImpl<MCFixup> &Fixups,
182 const MCSubtargetInfo &STI) const;
185 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
186 /// operand as needed by load/store instructions.
187 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
188 SmallVectorImpl<MCFixup> &Fixups,
189 const MCSubtargetInfo &STI) const;
191 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
192 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
193 SmallVectorImpl<MCFixup> &Fixups,
194 const MCSubtargetInfo &STI) const {
195 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
197 default: llvm_unreachable("Unknown addressing sub-mode!");
198 case ARM_AM::da: return 0;
199 case ARM_AM::ia: return 1;
200 case ARM_AM::db: return 2;
201 case ARM_AM::ib: return 3;
204 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
206 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
208 case ARM_AM::no_shift:
209 case ARM_AM::lsl: return 0;
210 case ARM_AM::lsr: return 1;
211 case ARM_AM::asr: return 2;
213 case ARM_AM::rrx: return 3;
215 llvm_unreachable("Invalid ShiftOpc!");
218 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
219 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
220 SmallVectorImpl<MCFixup> &Fixups,
221 const MCSubtargetInfo &STI) const;
223 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
224 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
225 SmallVectorImpl<MCFixup> &Fixups,
226 const MCSubtargetInfo &STI) const;
228 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
229 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
230 SmallVectorImpl<MCFixup> &Fixups,
231 const MCSubtargetInfo &STI) const;
233 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
234 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
235 SmallVectorImpl<MCFixup> &Fixups,
236 const MCSubtargetInfo &STI) const;
238 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
239 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
240 SmallVectorImpl<MCFixup> &Fixups,
241 const MCSubtargetInfo &STI) const;
243 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
245 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
246 SmallVectorImpl<MCFixup> &Fixups,
247 const MCSubtargetInfo &STI) const;
249 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
250 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
251 SmallVectorImpl<MCFixup> &Fixups,
252 const MCSubtargetInfo &STI) const;
254 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
255 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
256 SmallVectorImpl<MCFixup> &Fixups,
257 const MCSubtargetInfo &STI) const;
259 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
260 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
261 SmallVectorImpl<MCFixup> &Fixups,
262 const MCSubtargetInfo &STI) const;
264 /// getCCOutOpValue - Return encoding of the 's' bit.
265 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
266 SmallVectorImpl<MCFixup> &Fixups,
267 const MCSubtargetInfo &STI) const {
268 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
270 return MI.getOperand(Op).getReg() == ARM::CPSR;
273 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
274 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
275 SmallVectorImpl<MCFixup> &Fixups,
276 const MCSubtargetInfo &STI) const {
277 unsigned SoImm = MI.getOperand(Op).getImm();
278 int SoImmVal = ARM_AM::getSOImmVal(SoImm);
279 assert(SoImmVal != -1 && "Not a valid so_imm value!");
281 // Encode rotate_imm.
282 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
283 << ARMII::SoRotImmShift;
286 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
290 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
291 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
292 SmallVectorImpl<MCFixup> &Fixups,
293 const MCSubtargetInfo &STI) const {
294 unsigned SoImm = MI.getOperand(Op).getImm();
295 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
296 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
300 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
301 SmallVectorImpl<MCFixup> &Fixups,
302 const MCSubtargetInfo &STI) const;
303 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
304 SmallVectorImpl<MCFixup> &Fixups,
305 const MCSubtargetInfo &STI) const;
306 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
307 SmallVectorImpl<MCFixup> &Fixups,
308 const MCSubtargetInfo &STI) const;
309 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
310 SmallVectorImpl<MCFixup> &Fixups,
311 const MCSubtargetInfo &STI) const;
313 /// getSORegOpValue - Return an encoded so_reg shifted register value.
314 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
315 SmallVectorImpl<MCFixup> &Fixups,
316 const MCSubtargetInfo &STI) const;
317 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
318 SmallVectorImpl<MCFixup> &Fixups,
319 const MCSubtargetInfo &STI) const;
320 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
321 SmallVectorImpl<MCFixup> &Fixups,
322 const MCSubtargetInfo &STI) const;
324 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
325 SmallVectorImpl<MCFixup> &Fixups,
326 const MCSubtargetInfo &STI) const {
327 return 64 - MI.getOperand(Op).getImm();
330 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
331 SmallVectorImpl<MCFixup> &Fixups,
332 const MCSubtargetInfo &STI) const;
334 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
335 SmallVectorImpl<MCFixup> &Fixups,
336 const MCSubtargetInfo &STI) const;
337 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
338 SmallVectorImpl<MCFixup> &Fixups,
339 const MCSubtargetInfo &STI) const;
340 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
341 SmallVectorImpl<MCFixup> &Fixups,
342 const MCSubtargetInfo &STI) const;
343 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
344 SmallVectorImpl<MCFixup> &Fixups,
345 const MCSubtargetInfo &STI) const;
346 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
347 SmallVectorImpl<MCFixup> &Fixups,
348 const MCSubtargetInfo &STI) const;
350 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
351 SmallVectorImpl<MCFixup> &Fixups,
352 const MCSubtargetInfo &STI) const;
353 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
354 SmallVectorImpl<MCFixup> &Fixups,
355 const MCSubtargetInfo &STI) const;
356 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
357 SmallVectorImpl<MCFixup> &Fixups,
358 const MCSubtargetInfo &STI) const;
359 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
360 SmallVectorImpl<MCFixup> &Fixups,
361 const MCSubtargetInfo &STI) const;
363 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
364 SmallVectorImpl<MCFixup> &Fixups,
365 const MCSubtargetInfo &STI) const;
367 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
368 unsigned EncodedValue,
369 const MCSubtargetInfo &STI) const;
370 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
371 unsigned EncodedValue,
372 const MCSubtargetInfo &STI) const;
373 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
374 unsigned EncodedValue,
375 const MCSubtargetInfo &STI) const;
376 unsigned NEONThumb2V8PostEncoder(const MCInst &MI,
377 unsigned EncodedValue,
378 const MCSubtargetInfo &STI) const;
380 unsigned VFPThumb2PostEncoder(const MCInst &MI,
381 unsigned EncodedValue,
382 const MCSubtargetInfo &STI) const;
384 void EmitByte(unsigned char C, raw_ostream &OS) const {
388 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
389 // Output the constant in little endian byte order.
390 for (unsigned i = 0; i != Size; ++i) {
391 EmitByte(Val & 255, OS);
396 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
397 SmallVectorImpl<MCFixup> &Fixups,
398 const MCSubtargetInfo &STI) const;
401 } // end anonymous namespace
403 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
404 const MCRegisterInfo &MRI,
405 const MCSubtargetInfo &STI,
407 return new ARMMCCodeEmitter(MCII, STI, Ctx);
410 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
411 /// instructions, and rewrite them to their Thumb2 form if we are currently in
413 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
414 unsigned EncodedValue,
415 const MCSubtargetInfo &STI) const {
417 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
418 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
420 unsigned Bit24 = EncodedValue & 0x01000000;
421 unsigned Bit28 = Bit24 << 4;
422 EncodedValue &= 0xEFFFFFFF;
423 EncodedValue |= Bit28;
424 EncodedValue |= 0x0F000000;
430 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
431 /// instructions, and rewrite them to their Thumb2 form if we are currently in
433 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
434 unsigned EncodedValue,
435 const MCSubtargetInfo &STI) const {
437 EncodedValue &= 0xF0FFFFFF;
438 EncodedValue |= 0x09000000;
444 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
445 /// instructions, and rewrite them to their Thumb2 form if we are currently in
447 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
448 unsigned EncodedValue,
449 const MCSubtargetInfo &STI) const {
451 EncodedValue &= 0x00FFFFFF;
452 EncodedValue |= 0xEE000000;
458 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
459 /// if we are in Thumb2.
460 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI,
461 unsigned EncodedValue,
462 const MCSubtargetInfo &STI) const {
464 EncodedValue |= 0xC000000; // Set bits 27-26
470 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
471 /// them to their Thumb2 form if we are currently in Thumb2 mode.
472 unsigned ARMMCCodeEmitter::
473 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue,
474 const MCSubtargetInfo &STI) const {
476 EncodedValue &= 0x0FFFFFFF;
477 EncodedValue |= 0xE0000000;
482 /// getMachineOpValue - Return binary encoding of operand. If the machine
483 /// operand requires relocation, record the relocation and return zero.
484 unsigned ARMMCCodeEmitter::
485 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
486 SmallVectorImpl<MCFixup> &Fixups,
487 const MCSubtargetInfo &STI) const {
489 unsigned Reg = MO.getReg();
490 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
492 // Q registers are encoded as 2x their register number.
496 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
497 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
498 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
499 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
502 } else if (MO.isImm()) {
503 return static_cast<unsigned>(MO.getImm());
504 } else if (MO.isFPImm()) {
505 return static_cast<unsigned>(APFloat(MO.getFPImm())
506 .bitcastToAPInt().getHiBits(32).getLimitedValue());
509 llvm_unreachable("Unable to encode MCOperand!");
512 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
513 bool ARMMCCodeEmitter::
514 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
515 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups,
516 const MCSubtargetInfo &STI) const {
517 const MCOperand &MO = MI.getOperand(OpIdx);
518 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
520 Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
522 int32_t SImm = MO1.getImm();
525 // Special value for #-0
526 if (SImm == INT32_MIN) {
531 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
541 /// getBranchTargetOpValue - Helper function to get the branch target operand,
542 /// which is either an immediate or requires a fixup.
543 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
545 SmallVectorImpl<MCFixup> &Fixups,
546 const MCSubtargetInfo &STI) {
547 const MCOperand &MO = MI.getOperand(OpIdx);
549 // If the destination is an immediate, we have nothing to do.
550 if (MO.isImm()) return MO.getImm();
551 assert(MO.isExpr() && "Unexpected branch target type!");
552 const MCExpr *Expr = MO.getExpr();
553 MCFixupKind Kind = MCFixupKind(FixupKind);
554 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
556 // All of the information is in the fixup.
560 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
561 // determined by negating them and XOR'ing them with bit 23.
562 static int32_t encodeThumbBLOffset(int32_t offset) {
564 uint32_t S = (offset & 0x800000) >> 23;
565 uint32_t J1 = (offset & 0x400000) >> 22;
566 uint32_t J2 = (offset & 0x200000) >> 21;
579 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
580 uint32_t ARMMCCodeEmitter::
581 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
582 SmallVectorImpl<MCFixup> &Fixups,
583 const MCSubtargetInfo &STI) const {
584 const MCOperand MO = MI.getOperand(OpIdx);
586 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
588 return encodeThumbBLOffset(MO.getImm());
591 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
592 /// BLX branch target.
593 uint32_t ARMMCCodeEmitter::
594 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
595 SmallVectorImpl<MCFixup> &Fixups,
596 const MCSubtargetInfo &STI) const {
597 const MCOperand MO = MI.getOperand(OpIdx);
599 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
601 return encodeThumbBLOffset(MO.getImm());
604 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
605 uint32_t ARMMCCodeEmitter::
606 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
607 SmallVectorImpl<MCFixup> &Fixups,
608 const MCSubtargetInfo &STI) const {
609 const MCOperand MO = MI.getOperand(OpIdx);
611 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
613 return (MO.getImm() >> 1);
616 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
617 uint32_t ARMMCCodeEmitter::
618 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
619 SmallVectorImpl<MCFixup> &Fixups,
620 const MCSubtargetInfo &STI) const {
621 const MCOperand MO = MI.getOperand(OpIdx);
623 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
625 return (MO.getImm() >> 1);
628 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
629 uint32_t ARMMCCodeEmitter::
630 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
631 SmallVectorImpl<MCFixup> &Fixups,
632 const MCSubtargetInfo &STI) const {
633 const MCOperand MO = MI.getOperand(OpIdx);
635 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups, STI);
636 return (MO.getImm() >> 1);
639 /// Return true if this branch has a non-always predication
640 static bool HasConditionalBranch(const MCInst &MI) {
641 int NumOp = MI.getNumOperands();
643 for (int i = 0; i < NumOp-1; ++i) {
644 const MCOperand &MCOp1 = MI.getOperand(i);
645 const MCOperand &MCOp2 = MI.getOperand(i + 1);
646 if (MCOp1.isImm() && MCOp2.isReg() &&
647 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
648 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
656 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
658 uint32_t ARMMCCodeEmitter::
659 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
660 SmallVectorImpl<MCFixup> &Fixups,
661 const MCSubtargetInfo &STI) const {
662 // FIXME: This really, really shouldn't use TargetMachine. We don't want
663 // coupling between MC and TM anywhere we can help it.
666 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups, STI);
667 return getARMBranchTargetOpValue(MI, OpIdx, Fixups, STI);
670 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
672 uint32_t ARMMCCodeEmitter::
673 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
674 SmallVectorImpl<MCFixup> &Fixups,
675 const MCSubtargetInfo &STI) const {
676 const MCOperand MO = MI.getOperand(OpIdx);
678 if (HasConditionalBranch(MI))
679 return ::getBranchTargetOpValue(MI, OpIdx,
680 ARM::fixup_arm_condbranch, Fixups, STI);
681 return ::getBranchTargetOpValue(MI, OpIdx,
682 ARM::fixup_arm_uncondbranch, Fixups, STI);
685 return MO.getImm() >> 2;
688 uint32_t ARMMCCodeEmitter::
689 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
690 SmallVectorImpl<MCFixup> &Fixups,
691 const MCSubtargetInfo &STI) const {
692 const MCOperand MO = MI.getOperand(OpIdx);
694 if (HasConditionalBranch(MI))
695 return ::getBranchTargetOpValue(MI, OpIdx,
696 ARM::fixup_arm_condbl, Fixups, STI);
697 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups, STI);
700 return MO.getImm() >> 2;
703 uint32_t ARMMCCodeEmitter::
704 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
705 SmallVectorImpl<MCFixup> &Fixups,
706 const MCSubtargetInfo &STI) const {
707 const MCOperand MO = MI.getOperand(OpIdx);
709 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups, STI);
711 return MO.getImm() >> 1;
714 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
715 /// immediate branch target.
716 uint32_t ARMMCCodeEmitter::
717 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
718 SmallVectorImpl<MCFixup> &Fixups,
719 const MCSubtargetInfo &STI) const {
721 const MCOperand MO = MI.getOperand(OpIdx);
724 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups, STI);
726 Val = MO.getImm() >> 1;
728 bool I = (Val & 0x800000);
729 bool J1 = (Val & 0x400000);
730 bool J2 = (Val & 0x200000);
744 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
745 /// ADR label target.
746 uint32_t ARMMCCodeEmitter::
747 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
748 SmallVectorImpl<MCFixup> &Fixups,
749 const MCSubtargetInfo &STI) const {
750 const MCOperand MO = MI.getOperand(OpIdx);
752 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
754 int64_t offset = MO.getImm();
755 uint32_t Val = 0x2000;
758 if (offset == INT32_MIN) {
761 } else if (offset < 0) {
764 SoImmVal = ARM_AM::getSOImmVal(offset);
768 SoImmVal = ARM_AM::getSOImmVal(offset);
771 SoImmVal = ARM_AM::getSOImmVal(offset);
775 SoImmVal = ARM_AM::getSOImmVal(offset);
779 assert(SoImmVal != -1 && "Not a valid so_imm value!");
785 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
787 uint32_t ARMMCCodeEmitter::
788 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
789 SmallVectorImpl<MCFixup> &Fixups,
790 const MCSubtargetInfo &STI) const {
791 const MCOperand MO = MI.getOperand(OpIdx);
793 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
795 int32_t Val = MO.getImm();
796 if (Val == INT32_MIN)
805 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
807 uint32_t ARMMCCodeEmitter::
808 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
809 SmallVectorImpl<MCFixup> &Fixups,
810 const MCSubtargetInfo &STI) const {
811 const MCOperand MO = MI.getOperand(OpIdx);
813 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
818 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
820 uint32_t ARMMCCodeEmitter::
821 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
822 SmallVectorImpl<MCFixup> &,
823 const MCSubtargetInfo &STI) const {
827 const MCOperand &MO1 = MI.getOperand(OpIdx);
828 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
829 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
830 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
831 return (Rm << 3) | Rn;
834 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
835 uint32_t ARMMCCodeEmitter::
836 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
837 SmallVectorImpl<MCFixup> &Fixups,
838 const MCSubtargetInfo &STI) const {
840 // {12} = (U)nsigned (add == '1', sub == '0')
844 // If The first operand isn't a register, we have a label reference.
845 const MCOperand &MO = MI.getOperand(OpIdx);
847 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
851 const MCExpr *Expr = MO.getExpr();
852 isAdd = false ; // 'U' bit is set as part of the fixup.
856 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
858 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
859 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
861 ++MCNumCPRelocations;
864 int32_t Offset = MO.getImm();
865 if (Offset == INT32_MIN) {
868 } else if (Offset < 0) {
875 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups, STI);
877 uint32_t Binary = Imm12 & 0xfff;
878 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
881 Binary |= (Reg << 13);
885 /// getT2Imm8s4OpValue - Return encoding info for
886 /// '+/- imm8<<2' operand.
887 uint32_t ARMMCCodeEmitter::
888 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
889 SmallVectorImpl<MCFixup> &Fixups,
890 const MCSubtargetInfo &STI) const {
891 // FIXME: The immediate operand should have already been encoded like this
892 // before ever getting here. The encoder method should just need to combine
893 // the MI operands for the register and the offset into a single
894 // representation for the complex operand in the .td file. This isn't just
895 // style, unfortunately. As-is, we can't represent the distinct encoding
898 // {8} = (U)nsigned (add == '1', sub == '0')
900 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
901 bool isAdd = Imm8 >= 0;
903 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
905 Imm8 = -(uint32_t)Imm8;
910 uint32_t Binary = Imm8 & 0xff;
911 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
917 /// getT2AddrModeImm8s4OpValue - Return encoding info for
918 /// 'reg +/- imm8<<2' operand.
919 uint32_t ARMMCCodeEmitter::
920 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
921 SmallVectorImpl<MCFixup> &Fixups,
922 const MCSubtargetInfo &STI) const {
924 // {8} = (U)nsigned (add == '1', sub == '0')
928 // If The first operand isn't a register, we have a label reference.
929 const MCOperand &MO = MI.getOperand(OpIdx);
931 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
933 isAdd = false ; // 'U' bit is set as part of the fixup.
935 assert(MO.isExpr() && "Unexpected machine operand type!");
936 const MCExpr *Expr = MO.getExpr();
937 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
938 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
940 ++MCNumCPRelocations;
942 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
944 // FIXME: The immediate operand should have already been encoded like this
945 // before ever getting here. The encoder method should just need to combine
946 // the MI operands for the register and the offset into a single
947 // representation for the complex operand in the .td file. This isn't just
948 // style, unfortunately. As-is, we can't represent the distinct encoding
950 uint32_t Binary = (Imm8 >> 2) & 0xff;
951 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
954 Binary |= (Reg << 9);
958 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
959 /// 'reg + imm8<<2' operand.
960 uint32_t ARMMCCodeEmitter::
961 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
962 SmallVectorImpl<MCFixup> &Fixups,
963 const MCSubtargetInfo &STI) const {
966 const MCOperand &MO = MI.getOperand(OpIdx);
967 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
968 unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
969 unsigned Imm8 = MO1.getImm();
970 return (Reg << 8) | Imm8;
973 // FIXME: This routine assumes that a binary
974 // expression will always result in a PCRel expression
975 // In reality, its only true if one or more subexpressions
976 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
977 // but this is good enough for now.
978 static bool EvaluateAsPCRel(const MCExpr *Expr) {
979 switch (Expr->getKind()) {
980 default: llvm_unreachable("Unexpected expression type");
981 case MCExpr::SymbolRef: return false;
982 case MCExpr::Binary: return true;
987 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
988 SmallVectorImpl<MCFixup> &Fixups,
989 const MCSubtargetInfo &STI) const {
990 // {20-16} = imm{15-12}
991 // {11-0} = imm{11-0}
992 const MCOperand &MO = MI.getOperand(OpIdx);
994 // Hi / lo 16 bits already extracted during earlier passes.
995 return static_cast<unsigned>(MO.getImm());
997 // Handle :upper16: and :lower16: assembly prefixes.
998 const MCExpr *E = MO.getExpr();
1000 if (E->getKind() == MCExpr::Target) {
1001 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
1002 E = ARM16Expr->getSubExpr();
1004 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(E)) {
1005 const int64_t Value = MCE->getValue();
1006 if (Value > UINT32_MAX)
1007 report_fatal_error("constant value truncated (limited to 32-bit)");
1009 switch (ARM16Expr->getKind()) {
1010 case ARMMCExpr::VK_ARM_HI16:
1011 return (int32_t(Value) & 0xffff0000) >> 16;
1012 case ARMMCExpr::VK_ARM_LO16:
1013 return (int32_t(Value) & 0x0000ffff);
1014 default: llvm_unreachable("Unsupported ARMFixup");
1018 switch (ARM16Expr->getKind()) {
1019 default: llvm_unreachable("Unsupported ARMFixup");
1020 case ARMMCExpr::VK_ARM_HI16:
1021 if (!isTargetMachO() && EvaluateAsPCRel(E))
1022 Kind = MCFixupKind(isThumb2()
1023 ? ARM::fixup_t2_movt_hi16_pcrel
1024 : ARM::fixup_arm_movt_hi16_pcrel);
1026 Kind = MCFixupKind(isThumb2()
1027 ? ARM::fixup_t2_movt_hi16
1028 : ARM::fixup_arm_movt_hi16);
1030 case ARMMCExpr::VK_ARM_LO16:
1031 if (!isTargetMachO() && EvaluateAsPCRel(E))
1032 Kind = MCFixupKind(isThumb2()
1033 ? ARM::fixup_t2_movw_lo16_pcrel
1034 : ARM::fixup_arm_movw_lo16_pcrel);
1036 Kind = MCFixupKind(isThumb2()
1037 ? ARM::fixup_t2_movw_lo16
1038 : ARM::fixup_arm_movw_lo16);
1041 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
1044 // If the expression doesn't have :upper16: or :lower16: on it,
1045 // it's just a plain immediate expression, and those evaluate to
1046 // the lower 16 bits of the expression regardless of whether
1047 // we have a movt or a movw.
1048 if (!isTargetMachO() && EvaluateAsPCRel(E))
1049 Kind = MCFixupKind(isThumb2()
1050 ? ARM::fixup_t2_movw_lo16_pcrel
1051 : ARM::fixup_arm_movw_lo16_pcrel);
1053 Kind = MCFixupKind(isThumb2()
1054 ? ARM::fixup_t2_movw_lo16
1055 : ARM::fixup_arm_movw_lo16);
1056 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
1060 uint32_t ARMMCCodeEmitter::
1061 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
1062 SmallVectorImpl<MCFixup> &Fixups,
1063 const MCSubtargetInfo &STI) const {
1064 const MCOperand &MO = MI.getOperand(OpIdx);
1065 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1066 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1067 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1068 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1069 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
1070 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
1071 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
1072 unsigned SBits = getShiftOp(ShOp);
1074 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
1075 // amount. However, it would be an easy mistake to make so check here.
1076 assert((ShImm & ~0x1f) == 0 && "Out of range shift amount");
1085 uint32_t Binary = Rm;
1087 Binary |= SBits << 5;
1088 Binary |= ShImm << 7;
1094 uint32_t ARMMCCodeEmitter::
1095 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
1096 SmallVectorImpl<MCFixup> &Fixups,
1097 const MCSubtargetInfo &STI) const {
1099 // {13} 1 == imm12, 0 == Rm
1102 const MCOperand &MO = MI.getOperand(OpIdx);
1103 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1104 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups, STI);
1109 uint32_t ARMMCCodeEmitter::
1110 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1111 SmallVectorImpl<MCFixup> &Fixups,
1112 const MCSubtargetInfo &STI) const {
1113 // {13} 1 == imm12, 0 == Rm
1116 const MCOperand &MO = MI.getOperand(OpIdx);
1117 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1118 unsigned Imm = MO1.getImm();
1119 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
1120 bool isReg = MO.getReg() != 0;
1121 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
1122 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1124 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
1125 Binary <<= 7; // Shift amount is bits [11:7]
1126 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
1127 Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0]
1129 return Binary | (isAdd << 12) | (isReg << 13);
1132 uint32_t ARMMCCodeEmitter::
1133 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
1134 SmallVectorImpl<MCFixup> &Fixups,
1135 const MCSubtargetInfo &STI) const {
1138 const MCOperand &MO = MI.getOperand(OpIdx);
1139 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1140 bool isAdd = MO1.getImm() != 0;
1141 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4);
1144 uint32_t ARMMCCodeEmitter::
1145 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1146 SmallVectorImpl<MCFixup> &Fixups,
1147 const MCSubtargetInfo &STI) const {
1148 // {9} 1 == imm8, 0 == Rm
1150 // {7-4} imm7_4/zero
1152 const MCOperand &MO = MI.getOperand(OpIdx);
1153 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1154 unsigned Imm = MO1.getImm();
1155 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1156 bool isImm = MO.getReg() == 0;
1157 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1158 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1160 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1161 return Imm8 | (isAdd << 8) | (isImm << 9);
1164 uint32_t ARMMCCodeEmitter::
1165 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1166 SmallVectorImpl<MCFixup> &Fixups,
1167 const MCSubtargetInfo &STI) const {
1168 // {13} 1 == imm8, 0 == Rm
1171 // {7-4} imm7_4/zero
1173 const MCOperand &MO = MI.getOperand(OpIdx);
1174 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1175 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1177 // If The first operand isn't a register, we have a label reference.
1179 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1181 assert(MO.isExpr() && "Unexpected machine operand type!");
1182 const MCExpr *Expr = MO.getExpr();
1183 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1184 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1186 ++MCNumCPRelocations;
1187 return (Rn << 9) | (1 << 13);
1189 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1190 unsigned Imm = MO2.getImm();
1191 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1192 bool isImm = MO1.getReg() == 0;
1193 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1194 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1196 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1197 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1200 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1201 uint32_t ARMMCCodeEmitter::
1202 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1203 SmallVectorImpl<MCFixup> &Fixups,
1204 const MCSubtargetInfo &STI) const {
1207 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1208 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1209 "Unexpected base register!");
1211 // The immediate is already shifted for the implicit zeroes, so no change
1213 return MO1.getImm() & 0xff;
1216 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1217 uint32_t ARMMCCodeEmitter::
1218 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1219 SmallVectorImpl<MCFixup> &Fixups,
1220 const MCSubtargetInfo &STI) const {
1224 const MCOperand &MO = MI.getOperand(OpIdx);
1225 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1226 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1227 unsigned Imm5 = MO1.getImm();
1228 return ((Imm5 & 0x1f) << 3) | Rn;
1231 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1232 uint32_t ARMMCCodeEmitter::
1233 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1234 SmallVectorImpl<MCFixup> &Fixups,
1235 const MCSubtargetInfo &STI) const {
1236 const MCOperand MO = MI.getOperand(OpIdx);
1238 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups, STI);
1239 return (MO.getImm() >> 2);
1242 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1243 uint32_t ARMMCCodeEmitter::
1244 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1245 SmallVectorImpl<MCFixup> &Fixups,
1246 const MCSubtargetInfo &STI) const {
1248 // {8} = (U)nsigned (add == '1', sub == '0')
1252 // If The first operand isn't a register, we have a label reference.
1253 const MCOperand &MO = MI.getOperand(OpIdx);
1255 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1257 isAdd = false; // 'U' bit is handled as part of the fixup.
1259 assert(MO.isExpr() && "Unexpected machine operand type!");
1260 const MCExpr *Expr = MO.getExpr();
1263 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1265 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1266 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1268 ++MCNumCPRelocations;
1270 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1271 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1274 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1275 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1278 Binary |= (Reg << 9);
1282 unsigned ARMMCCodeEmitter::
1283 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1284 SmallVectorImpl<MCFixup> &Fixups,
1285 const MCSubtargetInfo &STI) const {
1286 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1287 // shifted. The second is Rs, the amount to shift by, and the third specifies
1288 // the type of the shift.
1296 const MCOperand &MO = MI.getOperand(OpIdx);
1297 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1298 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1299 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1302 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1304 // Encode the shift opcode.
1306 unsigned Rs = MO1.getReg();
1308 // Set shift operand (bit[7:4]).
1314 default: llvm_unreachable("Unknown shift opc!");
1315 case ARM_AM::lsl: SBits = 0x1; break;
1316 case ARM_AM::lsr: SBits = 0x3; break;
1317 case ARM_AM::asr: SBits = 0x5; break;
1318 case ARM_AM::ror: SBits = 0x7; break;
1322 Binary |= SBits << 4;
1324 // Encode the shift operation Rs.
1325 // Encode Rs bit[11:8].
1326 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1327 return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift);
1330 unsigned ARMMCCodeEmitter::
1331 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1332 SmallVectorImpl<MCFixup> &Fixups,
1333 const MCSubtargetInfo &STI) const {
1334 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1335 // shifted. The second is the amount to shift by.
1342 const MCOperand &MO = MI.getOperand(OpIdx);
1343 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1344 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1347 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1349 // Encode the shift opcode.
1352 // Set shift operand (bit[6:4]).
1357 // RRX - 110 and bit[11:8] clear.
1359 default: llvm_unreachable("Unknown shift opc!");
1360 case ARM_AM::lsl: SBits = 0x0; break;
1361 case ARM_AM::lsr: SBits = 0x2; break;
1362 case ARM_AM::asr: SBits = 0x4; break;
1363 case ARM_AM::ror: SBits = 0x6; break;
1369 // Encode shift_imm bit[11:7].
1370 Binary |= SBits << 4;
1371 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1372 assert(Offset < 32 && "Offset must be in range 0-31!");
1373 return Binary | (Offset << 7);
1377 unsigned ARMMCCodeEmitter::
1378 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1379 SmallVectorImpl<MCFixup> &Fixups,
1380 const MCSubtargetInfo &STI) const {
1381 const MCOperand &MO1 = MI.getOperand(OpNum);
1382 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1383 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1385 // Encoded as [Rn, Rm, imm].
1386 // FIXME: Needs fixup support.
1387 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1389 Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
1391 Value |= MO3.getImm();
1396 unsigned ARMMCCodeEmitter::
1397 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1398 SmallVectorImpl<MCFixup> &Fixups,
1399 const MCSubtargetInfo &STI) const {
1400 const MCOperand &MO1 = MI.getOperand(OpNum);
1401 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1403 // FIXME: Needs fixup support.
1404 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1406 // Even though the immediate is 8 bits long, we need 9 bits in order
1407 // to represent the (inverse of the) sign bit.
1409 int32_t tmp = (int32_t)MO2.getImm();
1413 Value |= 256; // Set the ADD bit
1418 unsigned ARMMCCodeEmitter::
1419 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1420 SmallVectorImpl<MCFixup> &Fixups,
1421 const MCSubtargetInfo &STI) const {
1422 const MCOperand &MO1 = MI.getOperand(OpNum);
1424 // FIXME: Needs fixup support.
1426 int32_t tmp = (int32_t)MO1.getImm();
1430 Value |= 256; // Set the ADD bit
1435 unsigned ARMMCCodeEmitter::
1436 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1437 SmallVectorImpl<MCFixup> &Fixups,
1438 const MCSubtargetInfo &STI) const {
1439 const MCOperand &MO1 = MI.getOperand(OpNum);
1441 // FIXME: Needs fixup support.
1443 int32_t tmp = (int32_t)MO1.getImm();
1447 Value |= 4096; // Set the ADD bit
1448 Value |= tmp & 4095;
1452 unsigned ARMMCCodeEmitter::
1453 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1454 SmallVectorImpl<MCFixup> &Fixups,
1455 const MCSubtargetInfo &STI) const {
1456 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1457 // shifted. The second is the amount to shift by.
1464 const MCOperand &MO = MI.getOperand(OpIdx);
1465 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1466 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1469 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1471 // Encode the shift opcode.
1473 // Set shift operand (bit[6:4]).
1479 default: llvm_unreachable("Unknown shift opc!");
1480 case ARM_AM::lsl: SBits = 0x0; break;
1481 case ARM_AM::lsr: SBits = 0x2; break;
1482 case ARM_AM::asr: SBits = 0x4; break;
1483 case ARM_AM::rrx: // FALLTHROUGH
1484 case ARM_AM::ror: SBits = 0x6; break;
1487 Binary |= SBits << 4;
1488 if (SOpc == ARM_AM::rrx)
1491 // Encode shift_imm bit[11:7].
1492 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1495 unsigned ARMMCCodeEmitter::
1496 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1497 SmallVectorImpl<MCFixup> &Fixups,
1498 const MCSubtargetInfo &STI) const {
1499 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1501 const MCOperand &MO = MI.getOperand(Op);
1502 uint32_t v = ~MO.getImm();
1503 uint32_t lsb = countTrailingZeros(v);
1504 uint32_t msb = (32 - countLeadingZeros (v)) - 1;
1505 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1506 return lsb | (msb << 5);
1509 unsigned ARMMCCodeEmitter::
1510 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1511 SmallVectorImpl<MCFixup> &Fixups,
1512 const MCSubtargetInfo &STI) const {
1515 // {7-0} = Number of registers
1518 // {15-0} = Bitfield of GPRs.
1519 unsigned Reg = MI.getOperand(Op).getReg();
1520 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1521 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1523 unsigned Binary = 0;
1525 if (SPRRegs || DPRRegs) {
1527 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
1528 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1529 Binary |= (RegNo & 0x1f) << 8;
1533 Binary |= NumRegs * 2;
1535 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1536 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(MI.getOperand(I).getReg());
1537 Binary |= 1 << RegNo;
1544 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1545 /// with the alignment operand.
1546 unsigned ARMMCCodeEmitter::
1547 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1548 SmallVectorImpl<MCFixup> &Fixups,
1549 const MCSubtargetInfo &STI) const {
1550 const MCOperand &Reg = MI.getOperand(Op);
1551 const MCOperand &Imm = MI.getOperand(Op + 1);
1553 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1556 switch (Imm.getImm()) {
1560 case 8: Align = 0x01; break;
1561 case 16: Align = 0x02; break;
1562 case 32: Align = 0x03; break;
1565 return RegNo | (Align << 4);
1568 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1569 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1570 unsigned ARMMCCodeEmitter::
1571 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1572 SmallVectorImpl<MCFixup> &Fixups,
1573 const MCSubtargetInfo &STI) const {
1574 const MCOperand &Reg = MI.getOperand(Op);
1575 const MCOperand &Imm = MI.getOperand(Op + 1);
1577 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1580 switch (Imm.getImm()) {
1584 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1585 case 2: Align = 0x00; break;
1586 case 4: Align = 0x03; break;
1589 return RegNo | (Align << 4);
1593 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1594 /// alignment operand for use in VLD-dup instructions. This is the same as
1595 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1596 /// different for VLD4-dup.
1597 unsigned ARMMCCodeEmitter::
1598 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1599 SmallVectorImpl<MCFixup> &Fixups,
1600 const MCSubtargetInfo &STI) const {
1601 const MCOperand &Reg = MI.getOperand(Op);
1602 const MCOperand &Imm = MI.getOperand(Op + 1);
1604 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1607 switch (Imm.getImm()) {
1611 case 8: Align = 0x01; break;
1612 case 16: Align = 0x03; break;
1615 return RegNo | (Align << 4);
1618 unsigned ARMMCCodeEmitter::
1619 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1620 SmallVectorImpl<MCFixup> &Fixups,
1621 const MCSubtargetInfo &STI) const {
1622 const MCOperand &MO = MI.getOperand(Op);
1623 if (MO.getReg() == 0) return 0x0D;
1624 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1627 unsigned ARMMCCodeEmitter::
1628 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1629 SmallVectorImpl<MCFixup> &Fixups,
1630 const MCSubtargetInfo &STI) const {
1631 return 8 - MI.getOperand(Op).getImm();
1634 unsigned ARMMCCodeEmitter::
1635 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1636 SmallVectorImpl<MCFixup> &Fixups,
1637 const MCSubtargetInfo &STI) const {
1638 return 16 - MI.getOperand(Op).getImm();
1641 unsigned ARMMCCodeEmitter::
1642 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1643 SmallVectorImpl<MCFixup> &Fixups,
1644 const MCSubtargetInfo &STI) const {
1645 return 32 - MI.getOperand(Op).getImm();
1648 unsigned ARMMCCodeEmitter::
1649 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1650 SmallVectorImpl<MCFixup> &Fixups,
1651 const MCSubtargetInfo &STI) const {
1652 return 64 - MI.getOperand(Op).getImm();
1655 void ARMMCCodeEmitter::
1656 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1657 SmallVectorImpl<MCFixup> &Fixups,
1658 const MCSubtargetInfo &STI) const {
1659 // Pseudo instructions don't get encoded.
1660 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1661 uint64_t TSFlags = Desc.TSFlags;
1662 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1666 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1667 Size = Desc.getSize();
1669 llvm_unreachable("Unexpected instruction size!");
1671 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
1672 // Thumb 32-bit wide instructions need to emit the high order halfword
1674 if (isThumb() && Size == 4) {
1675 EmitConstant(Binary >> 16, 2, OS);
1676 EmitConstant(Binary & 0xffff, 2, OS);
1678 EmitConstant(Binary, Size, OS);
1679 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1682 #include "ARMGenMCCodeEmitter.inc"