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;
45 ARMMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
46 : MCII(mcii), CTX(ctx) {
49 ~ARMMCCodeEmitter() {}
51 bool isThumb(const MCSubtargetInfo &STI) const {
52 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
54 bool isThumb2(const MCSubtargetInfo &STI) const {
55 return isThumb(STI) && (STI.getFeatureBits() & ARM::FeatureThumb2) != 0;
57 bool isTargetMachO(const MCSubtargetInfo &STI) const {
58 Triple TT(STI.getTargetTriple());
59 return TT.isOSBinFormatMachO();
62 unsigned getMachineSoImmOpValue(unsigned SoImm) const;
64 // getBinaryCodeForInstr - TableGen'erated function for getting the
65 // binary encoding for an instruction.
66 uint64_t getBinaryCodeForInstr(const MCInst &MI,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
70 /// getMachineOpValue - Return binary encoding of operand. If the machine
71 /// operand requires relocation, record the relocation and return zero.
72 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
76 /// getHiLo16ImmOpValue - Return the encoding for the hi / low 16-bit of
77 /// the specified operand. This is used for operands with :lower16: and
78 /// :upper16: prefixes.
79 uint32_t getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
80 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
83 bool EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx,
84 unsigned &Reg, unsigned &Imm,
85 SmallVectorImpl<MCFixup> &Fixups,
86 const MCSubtargetInfo &STI) const;
88 /// getThumbBLTargetOpValue - Return encoding info for Thumb immediate
90 uint32_t getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
91 SmallVectorImpl<MCFixup> &Fixups,
92 const MCSubtargetInfo &STI) const;
94 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
95 /// BLX branch target.
96 uint32_t getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
97 SmallVectorImpl<MCFixup> &Fixups,
98 const MCSubtargetInfo &STI) const;
100 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
101 uint32_t getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
102 SmallVectorImpl<MCFixup> &Fixups,
103 const MCSubtargetInfo &STI) const;
105 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
106 uint32_t getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
107 SmallVectorImpl<MCFixup> &Fixups,
108 const MCSubtargetInfo &STI) const;
110 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
111 uint32_t getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
112 SmallVectorImpl<MCFixup> &Fixups,
113 const MCSubtargetInfo &STI) const;
115 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate
117 uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
118 SmallVectorImpl<MCFixup> &Fixups,
119 const MCSubtargetInfo &STI) const;
121 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
122 /// immediate Thumb2 direct branch target.
123 uint32_t getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
124 SmallVectorImpl<MCFixup> &Fixups,
125 const MCSubtargetInfo &STI) const;
127 /// getARMBranchTargetOpValue - Return encoding info for 24-bit immediate
129 uint32_t getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
130 SmallVectorImpl<MCFixup> &Fixups,
131 const MCSubtargetInfo &STI) const;
132 uint32_t getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
133 SmallVectorImpl<MCFixup> &Fixups,
134 const MCSubtargetInfo &STI) const;
135 uint32_t getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
136 SmallVectorImpl<MCFixup> &Fixups,
137 const MCSubtargetInfo &STI) const;
139 /// getAdrLabelOpValue - Return encoding info for 12-bit immediate
140 /// ADR label target.
141 uint32_t getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
142 SmallVectorImpl<MCFixup> &Fixups,
143 const MCSubtargetInfo &STI) const;
144 uint32_t getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
145 SmallVectorImpl<MCFixup> &Fixups,
146 const MCSubtargetInfo &STI) const;
147 uint32_t getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
148 SmallVectorImpl<MCFixup> &Fixups,
149 const MCSubtargetInfo &STI) const;
152 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12'
154 uint32_t getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
155 SmallVectorImpl<MCFixup> &Fixups,
156 const MCSubtargetInfo &STI) const;
158 /// getThumbAddrModeRegRegOpValue - Return encoding for 'reg + reg' operand.
159 uint32_t getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
160 SmallVectorImpl<MCFixup> &Fixups,
161 const MCSubtargetInfo &STI) const;
163 /// getT2AddrModeImm8s4OpValue - Return encoding info for 'reg +/- imm8<<2'
165 uint32_t getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
166 SmallVectorImpl<MCFixup> &Fixups,
167 const MCSubtargetInfo &STI) const;
169 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for 'reg + imm8<<2'
171 uint32_t getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
172 SmallVectorImpl<MCFixup> &Fixups,
173 const MCSubtargetInfo &STI) const;
175 /// getT2Imm8s4OpValue - Return encoding info for '+/- imm8<<2'
177 uint32_t getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
178 SmallVectorImpl<MCFixup> &Fixups,
179 const MCSubtargetInfo &STI) const;
182 /// getLdStSORegOpValue - Return encoding info for 'reg +/- reg shop imm'
183 /// operand as needed by load/store instructions.
184 uint32_t getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
185 SmallVectorImpl<MCFixup> &Fixups,
186 const MCSubtargetInfo &STI) const;
188 /// getLdStmModeOpValue - Return encoding for load/store multiple mode.
189 uint32_t getLdStmModeOpValue(const MCInst &MI, unsigned OpIdx,
190 SmallVectorImpl<MCFixup> &Fixups,
191 const MCSubtargetInfo &STI) const {
192 ARM_AM::AMSubMode Mode = (ARM_AM::AMSubMode)MI.getOperand(OpIdx).getImm();
194 default: llvm_unreachable("Unknown addressing sub-mode!");
195 case ARM_AM::da: return 0;
196 case ARM_AM::ia: return 1;
197 case ARM_AM::db: return 2;
198 case ARM_AM::ib: return 3;
201 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value.
203 unsigned getShiftOp(ARM_AM::ShiftOpc ShOpc) const {
205 case ARM_AM::no_shift:
206 case ARM_AM::lsl: return 0;
207 case ARM_AM::lsr: return 1;
208 case ARM_AM::asr: return 2;
210 case ARM_AM::rrx: return 3;
212 llvm_unreachable("Invalid ShiftOpc!");
215 /// getAddrMode2OpValue - Return encoding for addrmode2 operands.
216 uint32_t getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
217 SmallVectorImpl<MCFixup> &Fixups,
218 const MCSubtargetInfo &STI) const;
220 /// getAddrMode2OffsetOpValue - Return encoding for am2offset operands.
221 uint32_t getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
222 SmallVectorImpl<MCFixup> &Fixups,
223 const MCSubtargetInfo &STI) const;
225 /// getPostIdxRegOpValue - Return encoding for postidx_reg operands.
226 uint32_t getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
227 SmallVectorImpl<MCFixup> &Fixups,
228 const MCSubtargetInfo &STI) const;
230 /// getAddrMode3OffsetOpValue - Return encoding for am3offset operands.
231 uint32_t getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
232 SmallVectorImpl<MCFixup> &Fixups,
233 const MCSubtargetInfo &STI) const;
235 /// getAddrMode3OpValue - Return encoding for addrmode3 operands.
236 uint32_t getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
237 SmallVectorImpl<MCFixup> &Fixups,
238 const MCSubtargetInfo &STI) const;
240 /// getAddrModeThumbSPOpValue - Return encoding info for 'reg +/- imm12'
242 uint32_t getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
243 SmallVectorImpl<MCFixup> &Fixups,
244 const MCSubtargetInfo &STI) const;
246 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
247 uint32_t getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
248 SmallVectorImpl<MCFixup> &Fixups,
249 const MCSubtargetInfo &STI) const;
251 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
252 uint32_t getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
253 SmallVectorImpl<MCFixup> &Fixups,
254 const MCSubtargetInfo &STI) const;
256 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm8' operand.
257 uint32_t getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
258 SmallVectorImpl<MCFixup> &Fixups,
259 const MCSubtargetInfo &STI) const;
261 /// getCCOutOpValue - Return encoding of the 's' bit.
262 unsigned getCCOutOpValue(const MCInst &MI, unsigned Op,
263 SmallVectorImpl<MCFixup> &Fixups,
264 const MCSubtargetInfo &STI) const {
265 // The operand is either reg0 or CPSR. The 's' bit is encoded as '0' or
267 return MI.getOperand(Op).getReg() == ARM::CPSR;
270 /// getSOImmOpValue - Return an encoded 12-bit shifted-immediate value.
271 unsigned getSOImmOpValue(const MCInst &MI, unsigned Op,
272 SmallVectorImpl<MCFixup> &Fixups,
273 const MCSubtargetInfo &STI) const {
276 const MCOperand &MO = MI.getOperand(Op);
278 SoImmVal = ARM_AM::getSOImmVal(MO.getImm());
279 } else if (MO.isExpr()) {
281 bool Invalid = MO.getExpr()->EvaluateAsAbsolute(Value);
282 assert(!Invalid && "non-constant expression is not a valid SOImm operand");
283 assert((Value >= INT32_MIN && Value <= INT32_MAX) &&
284 "expression must be representable in 32 bits");
287 assert(SoImmVal != -1 && "Not a valid so_imm value!");
289 // Encode rotate_imm.
290 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1)
291 << ARMII::SoRotImmShift;
294 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal);
298 /// getT2SOImmOpValue - Return an encoded 12-bit shifted-immediate value.
299 unsigned getT2SOImmOpValue(const MCInst &MI, unsigned Op,
300 SmallVectorImpl<MCFixup> &Fixups,
301 const MCSubtargetInfo &STI) const {
302 unsigned SoImm = MI.getOperand(Op).getImm();
303 unsigned Encoded = ARM_AM::getT2SOImmVal(SoImm);
304 assert(Encoded != ~0U && "Not a Thumb2 so_imm value?");
308 unsigned getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
309 SmallVectorImpl<MCFixup> &Fixups,
310 const MCSubtargetInfo &STI) const;
311 unsigned getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
312 SmallVectorImpl<MCFixup> &Fixups,
313 const MCSubtargetInfo &STI) const;
314 unsigned getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
315 SmallVectorImpl<MCFixup> &Fixups,
316 const MCSubtargetInfo &STI) const;
317 unsigned getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
318 SmallVectorImpl<MCFixup> &Fixups,
319 const MCSubtargetInfo &STI) const;
321 /// getSORegOpValue - Return an encoded so_reg shifted register value.
322 unsigned getSORegRegOpValue(const MCInst &MI, unsigned Op,
323 SmallVectorImpl<MCFixup> &Fixups,
324 const MCSubtargetInfo &STI) const;
325 unsigned getSORegImmOpValue(const MCInst &MI, unsigned Op,
326 SmallVectorImpl<MCFixup> &Fixups,
327 const MCSubtargetInfo &STI) const;
328 unsigned getT2SORegOpValue(const MCInst &MI, unsigned Op,
329 SmallVectorImpl<MCFixup> &Fixups,
330 const MCSubtargetInfo &STI) const;
332 unsigned getNEONVcvtImm32OpValue(const MCInst &MI, unsigned Op,
333 SmallVectorImpl<MCFixup> &Fixups,
334 const MCSubtargetInfo &STI) const {
335 return 64 - MI.getOperand(Op).getImm();
338 unsigned getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
339 SmallVectorImpl<MCFixup> &Fixups,
340 const MCSubtargetInfo &STI) const;
342 unsigned getRegisterListOpValue(const MCInst &MI, unsigned Op,
343 SmallVectorImpl<MCFixup> &Fixups,
344 const MCSubtargetInfo &STI) const;
345 unsigned getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
346 SmallVectorImpl<MCFixup> &Fixups,
347 const MCSubtargetInfo &STI) const;
348 unsigned getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
349 SmallVectorImpl<MCFixup> &Fixups,
350 const MCSubtargetInfo &STI) const;
351 unsigned getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
352 SmallVectorImpl<MCFixup> &Fixups,
353 const MCSubtargetInfo &STI) const;
354 unsigned getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
355 SmallVectorImpl<MCFixup> &Fixups,
356 const MCSubtargetInfo &STI) const;
358 unsigned getShiftRight8Imm(const MCInst &MI, unsigned Op,
359 SmallVectorImpl<MCFixup> &Fixups,
360 const MCSubtargetInfo &STI) const;
361 unsigned getShiftRight16Imm(const MCInst &MI, unsigned Op,
362 SmallVectorImpl<MCFixup> &Fixups,
363 const MCSubtargetInfo &STI) const;
364 unsigned getShiftRight32Imm(const MCInst &MI, unsigned Op,
365 SmallVectorImpl<MCFixup> &Fixups,
366 const MCSubtargetInfo &STI) const;
367 unsigned getShiftRight64Imm(const MCInst &MI, unsigned Op,
368 SmallVectorImpl<MCFixup> &Fixups,
369 const MCSubtargetInfo &STI) const;
371 unsigned getThumbSRImmOpValue(const MCInst &MI, unsigned Op,
372 SmallVectorImpl<MCFixup> &Fixups,
373 const MCSubtargetInfo &STI) const;
375 unsigned NEONThumb2DataIPostEncoder(const MCInst &MI,
376 unsigned EncodedValue,
377 const MCSubtargetInfo &STI) const;
378 unsigned NEONThumb2LoadStorePostEncoder(const MCInst &MI,
379 unsigned EncodedValue,
380 const MCSubtargetInfo &STI) const;
381 unsigned NEONThumb2DupPostEncoder(const MCInst &MI,
382 unsigned EncodedValue,
383 const MCSubtargetInfo &STI) const;
384 unsigned NEONThumb2V8PostEncoder(const MCInst &MI,
385 unsigned EncodedValue,
386 const MCSubtargetInfo &STI) const;
388 unsigned VFPThumb2PostEncoder(const MCInst &MI,
389 unsigned EncodedValue,
390 const MCSubtargetInfo &STI) const;
392 void EmitByte(unsigned char C, raw_ostream &OS) const {
396 void EmitConstant(uint64_t Val, unsigned Size, raw_ostream &OS) const {
397 // Output the constant in little endian byte order.
398 for (unsigned i = 0; i != Size; ++i) {
399 EmitByte(Val & 255, OS);
404 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
405 SmallVectorImpl<MCFixup> &Fixups,
406 const MCSubtargetInfo &STI) const override;
409 } // end anonymous namespace
411 MCCodeEmitter *llvm::createARMMCCodeEmitter(const MCInstrInfo &MCII,
412 const MCRegisterInfo &MRI,
413 const MCSubtargetInfo &STI,
415 return new ARMMCCodeEmitter(MCII, Ctx);
418 /// NEONThumb2DataIPostEncoder - Post-process encoded NEON data-processing
419 /// instructions, and rewrite them to their Thumb2 form if we are currently in
421 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(const MCInst &MI,
422 unsigned EncodedValue,
423 const MCSubtargetInfo &STI) const {
425 // NEON Thumb2 data-processsing encodings are very simple: bit 24 is moved
426 // to bit 12 of the high half-word (i.e. bit 28), and bits 27-24 are
428 unsigned Bit24 = EncodedValue & 0x01000000;
429 unsigned Bit28 = Bit24 << 4;
430 EncodedValue &= 0xEFFFFFFF;
431 EncodedValue |= Bit28;
432 EncodedValue |= 0x0F000000;
438 /// NEONThumb2LoadStorePostEncoder - Post-process encoded NEON load/store
439 /// instructions, and rewrite them to their Thumb2 form if we are currently in
441 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(const MCInst &MI,
442 unsigned EncodedValue,
443 const MCSubtargetInfo &STI) const {
445 EncodedValue &= 0xF0FFFFFF;
446 EncodedValue |= 0x09000000;
452 /// NEONThumb2DupPostEncoder - Post-process encoded NEON vdup
453 /// instructions, and rewrite them to their Thumb2 form if we are currently in
455 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(const MCInst &MI,
456 unsigned EncodedValue,
457 const MCSubtargetInfo &STI) const {
459 EncodedValue &= 0x00FFFFFF;
460 EncodedValue |= 0xEE000000;
466 /// Post-process encoded NEON v8 instructions, and rewrite them to Thumb2 form
467 /// if we are in Thumb2.
468 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(const MCInst &MI,
469 unsigned EncodedValue,
470 const MCSubtargetInfo &STI) const {
472 EncodedValue |= 0xC000000; // Set bits 27-26
478 /// VFPThumb2PostEncoder - Post-process encoded VFP instructions and rewrite
479 /// them to their Thumb2 form if we are currently in Thumb2 mode.
480 unsigned ARMMCCodeEmitter::
481 VFPThumb2PostEncoder(const MCInst &MI, unsigned EncodedValue,
482 const MCSubtargetInfo &STI) const {
484 EncodedValue &= 0x0FFFFFFF;
485 EncodedValue |= 0xE0000000;
490 /// getMachineOpValue - Return binary encoding of operand. If the machine
491 /// operand requires relocation, record the relocation and return zero.
492 unsigned ARMMCCodeEmitter::
493 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
494 SmallVectorImpl<MCFixup> &Fixups,
495 const MCSubtargetInfo &STI) const {
497 unsigned Reg = MO.getReg();
498 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
500 // Q registers are encoded as 2x their register number.
504 case ARM::Q0: case ARM::Q1: case ARM::Q2: case ARM::Q3:
505 case ARM::Q4: case ARM::Q5: case ARM::Q6: case ARM::Q7:
506 case ARM::Q8: case ARM::Q9: case ARM::Q10: case ARM::Q11:
507 case ARM::Q12: case ARM::Q13: case ARM::Q14: case ARM::Q15:
510 } else if (MO.isImm()) {
511 return static_cast<unsigned>(MO.getImm());
512 } else if (MO.isFPImm()) {
513 return static_cast<unsigned>(APFloat(MO.getFPImm())
514 .bitcastToAPInt().getHiBits(32).getLimitedValue());
517 llvm_unreachable("Unable to encode MCOperand!");
520 /// getAddrModeImmOpValue - Return encoding info for 'reg +/- imm' operand.
521 bool ARMMCCodeEmitter::
522 EncodeAddrModeOpValues(const MCInst &MI, unsigned OpIdx, unsigned &Reg,
523 unsigned &Imm, SmallVectorImpl<MCFixup> &Fixups,
524 const MCSubtargetInfo &STI) const {
525 const MCOperand &MO = MI.getOperand(OpIdx);
526 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
528 Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
530 int32_t SImm = MO1.getImm();
533 // Special value for #-0
534 if (SImm == INT32_MIN) {
539 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
549 /// getBranchTargetOpValue - Helper function to get the branch target operand,
550 /// which is either an immediate or requires a fixup.
551 static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
553 SmallVectorImpl<MCFixup> &Fixups,
554 const MCSubtargetInfo &STI) {
555 const MCOperand &MO = MI.getOperand(OpIdx);
557 // If the destination is an immediate, we have nothing to do.
558 if (MO.isImm()) return MO.getImm();
559 assert(MO.isExpr() && "Unexpected branch target type!");
560 const MCExpr *Expr = MO.getExpr();
561 MCFixupKind Kind = MCFixupKind(FixupKind);
562 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
564 // All of the information is in the fixup.
568 // Thumb BL and BLX use a strange offset encoding where bits 22 and 21 are
569 // determined by negating them and XOR'ing them with bit 23.
570 static int32_t encodeThumbBLOffset(int32_t offset) {
572 uint32_t S = (offset & 0x800000) >> 23;
573 uint32_t J1 = (offset & 0x400000) >> 22;
574 uint32_t J2 = (offset & 0x200000) >> 21;
587 /// getThumbBLTargetOpValue - Return encoding info for immediate branch target.
588 uint32_t ARMMCCodeEmitter::
589 getThumbBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
590 SmallVectorImpl<MCFixup> &Fixups,
591 const MCSubtargetInfo &STI) const {
592 const MCOperand MO = MI.getOperand(OpIdx);
594 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bl,
596 return encodeThumbBLOffset(MO.getImm());
599 /// getThumbBLXTargetOpValue - Return encoding info for Thumb immediate
600 /// BLX branch target.
601 uint32_t ARMMCCodeEmitter::
602 getThumbBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
603 SmallVectorImpl<MCFixup> &Fixups,
604 const MCSubtargetInfo &STI) const {
605 const MCOperand MO = MI.getOperand(OpIdx);
607 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_blx,
609 return encodeThumbBLOffset(MO.getImm());
612 /// getThumbBRTargetOpValue - Return encoding info for Thumb branch target.
613 uint32_t ARMMCCodeEmitter::
614 getThumbBRTargetOpValue(const MCInst &MI, unsigned OpIdx,
615 SmallVectorImpl<MCFixup> &Fixups,
616 const MCSubtargetInfo &STI) const {
617 const MCOperand MO = MI.getOperand(OpIdx);
619 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_br,
621 return (MO.getImm() >> 1);
624 /// getThumbBCCTargetOpValue - Return encoding info for Thumb branch target.
625 uint32_t ARMMCCodeEmitter::
626 getThumbBCCTargetOpValue(const MCInst &MI, unsigned OpIdx,
627 SmallVectorImpl<MCFixup> &Fixups,
628 const MCSubtargetInfo &STI) const {
629 const MCOperand MO = MI.getOperand(OpIdx);
631 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_bcc,
633 return (MO.getImm() >> 1);
636 /// getThumbCBTargetOpValue - Return encoding info for Thumb branch target.
637 uint32_t ARMMCCodeEmitter::
638 getThumbCBTargetOpValue(const MCInst &MI, unsigned OpIdx,
639 SmallVectorImpl<MCFixup> &Fixups,
640 const MCSubtargetInfo &STI) const {
641 const MCOperand MO = MI.getOperand(OpIdx);
643 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cb, Fixups, STI);
644 return (MO.getImm() >> 1);
647 /// Return true if this branch has a non-always predication
648 static bool HasConditionalBranch(const MCInst &MI) {
649 int NumOp = MI.getNumOperands();
651 for (int i = 0; i < NumOp-1; ++i) {
652 const MCOperand &MCOp1 = MI.getOperand(i);
653 const MCOperand &MCOp2 = MI.getOperand(i + 1);
654 if (MCOp1.isImm() && MCOp2.isReg() &&
655 (MCOp2.getReg() == 0 || MCOp2.getReg() == ARM::CPSR)) {
656 if (ARMCC::CondCodes(MCOp1.getImm()) != ARMCC::AL)
664 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
666 uint32_t ARMMCCodeEmitter::
667 getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
668 SmallVectorImpl<MCFixup> &Fixups,
669 const MCSubtargetInfo &STI) const {
670 // FIXME: This really, really shouldn't use TargetMachine. We don't want
671 // coupling between MC and TM anywhere we can help it.
674 ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_condbranch, Fixups, STI);
675 return getARMBranchTargetOpValue(MI, OpIdx, Fixups, STI);
678 /// getBranchTargetOpValue - Return encoding info for 24-bit immediate branch
680 uint32_t ARMMCCodeEmitter::
681 getARMBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
682 SmallVectorImpl<MCFixup> &Fixups,
683 const MCSubtargetInfo &STI) const {
684 const MCOperand MO = MI.getOperand(OpIdx);
686 if (HasConditionalBranch(MI))
687 return ::getBranchTargetOpValue(MI, OpIdx,
688 ARM::fixup_arm_condbranch, Fixups, STI);
689 return ::getBranchTargetOpValue(MI, OpIdx,
690 ARM::fixup_arm_uncondbranch, Fixups, STI);
693 return MO.getImm() >> 2;
696 uint32_t ARMMCCodeEmitter::
697 getARMBLTargetOpValue(const MCInst &MI, unsigned OpIdx,
698 SmallVectorImpl<MCFixup> &Fixups,
699 const MCSubtargetInfo &STI) const {
700 const MCOperand MO = MI.getOperand(OpIdx);
702 if (HasConditionalBranch(MI))
703 return ::getBranchTargetOpValue(MI, OpIdx,
704 ARM::fixup_arm_condbl, Fixups, STI);
705 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_uncondbl, Fixups, STI);
708 return MO.getImm() >> 2;
711 uint32_t ARMMCCodeEmitter::
712 getARMBLXTargetOpValue(const MCInst &MI, unsigned OpIdx,
713 SmallVectorImpl<MCFixup> &Fixups,
714 const MCSubtargetInfo &STI) const {
715 const MCOperand MO = MI.getOperand(OpIdx);
717 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_blx, Fixups, STI);
719 return MO.getImm() >> 1;
722 /// getUnconditionalBranchTargetOpValue - Return encoding info for 24-bit
723 /// immediate branch target.
724 uint32_t ARMMCCodeEmitter::
725 getUnconditionalBranchTargetOpValue(const MCInst &MI, unsigned OpIdx,
726 SmallVectorImpl<MCFixup> &Fixups,
727 const MCSubtargetInfo &STI) const {
729 const MCOperand MO = MI.getOperand(OpIdx);
732 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_uncondbranch, Fixups, STI);
734 Val = MO.getImm() >> 1;
736 bool I = (Val & 0x800000);
737 bool J1 = (Val & 0x400000);
738 bool J2 = (Val & 0x200000);
752 /// getAdrLabelOpValue - Return encoding info for 12-bit shifted-immediate
753 /// ADR label target.
754 uint32_t ARMMCCodeEmitter::
755 getAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
756 SmallVectorImpl<MCFixup> &Fixups,
757 const MCSubtargetInfo &STI) const {
758 const MCOperand MO = MI.getOperand(OpIdx);
760 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_adr_pcrel_12,
762 int64_t offset = MO.getImm();
763 uint32_t Val = 0x2000;
766 if (offset == INT32_MIN) {
769 } else if (offset < 0) {
772 SoImmVal = ARM_AM::getSOImmVal(offset);
776 SoImmVal = ARM_AM::getSOImmVal(offset);
779 SoImmVal = ARM_AM::getSOImmVal(offset);
783 SoImmVal = ARM_AM::getSOImmVal(offset);
787 assert(SoImmVal != -1 && "Not a valid so_imm value!");
793 /// getT2AdrLabelOpValue - Return encoding info for 12-bit immediate ADR label
795 uint32_t ARMMCCodeEmitter::
796 getT2AdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
797 SmallVectorImpl<MCFixup> &Fixups,
798 const MCSubtargetInfo &STI) const {
799 const MCOperand MO = MI.getOperand(OpIdx);
801 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_t2_adr_pcrel_12,
803 int32_t Val = MO.getImm();
804 if (Val == INT32_MIN)
813 /// getThumbAdrLabelOpValue - Return encoding info for 8-bit immediate ADR label
815 uint32_t ARMMCCodeEmitter::
816 getThumbAdrLabelOpValue(const MCInst &MI, unsigned OpIdx,
817 SmallVectorImpl<MCFixup> &Fixups,
818 const MCSubtargetInfo &STI) const {
819 const MCOperand MO = MI.getOperand(OpIdx);
821 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_thumb_adr_pcrel_10,
826 /// getThumbAddrModeRegRegOpValue - Return encoding info for 'reg + reg'
828 uint32_t ARMMCCodeEmitter::
829 getThumbAddrModeRegRegOpValue(const MCInst &MI, unsigned OpIdx,
830 SmallVectorImpl<MCFixup> &,
831 const MCSubtargetInfo &STI) const {
835 const MCOperand &MO1 = MI.getOperand(OpIdx);
836 const MCOperand &MO2 = MI.getOperand(OpIdx + 1);
837 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
838 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
839 return (Rm << 3) | Rn;
842 /// getAddrModeImm12OpValue - Return encoding info for 'reg +/- imm12' operand.
843 uint32_t ARMMCCodeEmitter::
844 getAddrModeImm12OpValue(const MCInst &MI, unsigned OpIdx,
845 SmallVectorImpl<MCFixup> &Fixups,
846 const MCSubtargetInfo &STI) const {
848 // {12} = (U)nsigned (add == '1', sub == '0')
852 // If The first operand isn't a register, we have a label reference.
853 const MCOperand &MO = MI.getOperand(OpIdx);
855 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
859 const MCExpr *Expr = MO.getExpr();
860 isAdd = false ; // 'U' bit is set as part of the fixup.
864 Kind = MCFixupKind(ARM::fixup_t2_ldst_pcrel_12);
866 Kind = MCFixupKind(ARM::fixup_arm_ldst_pcrel_12);
867 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
869 ++MCNumCPRelocations;
872 int32_t Offset = MO.getImm();
873 if (Offset == INT32_MIN) {
876 } else if (Offset < 0) {
883 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups, STI);
885 uint32_t Binary = Imm12 & 0xfff;
886 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
889 Binary |= (Reg << 13);
893 /// getT2Imm8s4OpValue - Return encoding info for
894 /// '+/- imm8<<2' operand.
895 uint32_t ARMMCCodeEmitter::
896 getT2Imm8s4OpValue(const MCInst &MI, unsigned OpIdx,
897 SmallVectorImpl<MCFixup> &Fixups,
898 const MCSubtargetInfo &STI) const {
899 // FIXME: The immediate operand should have already been encoded like this
900 // before ever getting here. The encoder method should just need to combine
901 // the MI operands for the register and the offset into a single
902 // representation for the complex operand in the .td file. This isn't just
903 // style, unfortunately. As-is, we can't represent the distinct encoding
906 // {8} = (U)nsigned (add == '1', sub == '0')
908 int32_t Imm8 = MI.getOperand(OpIdx).getImm();
909 bool isAdd = Imm8 >= 0;
911 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
913 Imm8 = -(uint32_t)Imm8;
918 uint32_t Binary = Imm8 & 0xff;
919 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
925 /// getT2AddrModeImm8s4OpValue - Return encoding info for
926 /// 'reg +/- imm8<<2' operand.
927 uint32_t ARMMCCodeEmitter::
928 getT2AddrModeImm8s4OpValue(const MCInst &MI, unsigned OpIdx,
929 SmallVectorImpl<MCFixup> &Fixups,
930 const MCSubtargetInfo &STI) const {
932 // {8} = (U)nsigned (add == '1', sub == '0')
936 // If The first operand isn't a register, we have a label reference.
937 const MCOperand &MO = MI.getOperand(OpIdx);
939 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
941 isAdd = false ; // 'U' bit is set as part of the fixup.
943 assert(MO.isExpr() && "Unexpected machine operand type!");
944 const MCExpr *Expr = MO.getExpr();
945 MCFixupKind Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
946 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
948 ++MCNumCPRelocations;
950 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
952 // FIXME: The immediate operand should have already been encoded like this
953 // before ever getting here. The encoder method should just need to combine
954 // the MI operands for the register and the offset into a single
955 // representation for the complex operand in the .td file. This isn't just
956 // style, unfortunately. As-is, we can't represent the distinct encoding
958 uint32_t Binary = (Imm8 >> 2) & 0xff;
959 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
962 Binary |= (Reg << 9);
966 /// getT2AddrModeImm0_1020s4OpValue - Return encoding info for
967 /// 'reg + imm8<<2' operand.
968 uint32_t ARMMCCodeEmitter::
969 getT2AddrModeImm0_1020s4OpValue(const MCInst &MI, unsigned OpIdx,
970 SmallVectorImpl<MCFixup> &Fixups,
971 const MCSubtargetInfo &STI) const {
974 const MCOperand &MO = MI.getOperand(OpIdx);
975 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
976 unsigned Reg = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
977 unsigned Imm8 = MO1.getImm();
978 return (Reg << 8) | Imm8;
981 // FIXME: This routine assumes that a binary
982 // expression will always result in a PCRel expression
983 // In reality, its only true if one or more subexpressions
984 // is itself a PCRel (i.e. "." in asm or some other pcrel construct)
985 // but this is good enough for now.
986 static bool EvaluateAsPCRel(const MCExpr *Expr) {
987 switch (Expr->getKind()) {
988 default: llvm_unreachable("Unexpected expression type");
989 case MCExpr::SymbolRef: return false;
990 case MCExpr::Binary: return true;
995 ARMMCCodeEmitter::getHiLo16ImmOpValue(const MCInst &MI, unsigned OpIdx,
996 SmallVectorImpl<MCFixup> &Fixups,
997 const MCSubtargetInfo &STI) const {
998 // {20-16} = imm{15-12}
999 // {11-0} = imm{11-0}
1000 const MCOperand &MO = MI.getOperand(OpIdx);
1002 // Hi / lo 16 bits already extracted during earlier passes.
1003 return static_cast<unsigned>(MO.getImm());
1005 // Handle :upper16: and :lower16: assembly prefixes.
1006 const MCExpr *E = MO.getExpr();
1008 if (E->getKind() == MCExpr::Target) {
1009 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(E);
1010 E = ARM16Expr->getSubExpr();
1012 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(E)) {
1013 const int64_t Value = MCE->getValue();
1014 if (Value > UINT32_MAX)
1015 report_fatal_error("constant value truncated (limited to 32-bit)");
1017 switch (ARM16Expr->getKind()) {
1018 case ARMMCExpr::VK_ARM_HI16:
1019 return (int32_t(Value) & 0xffff0000) >> 16;
1020 case ARMMCExpr::VK_ARM_LO16:
1021 return (int32_t(Value) & 0x0000ffff);
1022 default: llvm_unreachable("Unsupported ARMFixup");
1026 switch (ARM16Expr->getKind()) {
1027 default: llvm_unreachable("Unsupported ARMFixup");
1028 case ARMMCExpr::VK_ARM_HI16:
1029 if (!isTargetMachO(STI) && EvaluateAsPCRel(E))
1030 Kind = MCFixupKind(isThumb2(STI)
1031 ? ARM::fixup_t2_movt_hi16_pcrel
1032 : ARM::fixup_arm_movt_hi16_pcrel);
1034 Kind = MCFixupKind(isThumb2(STI)
1035 ? ARM::fixup_t2_movt_hi16
1036 : ARM::fixup_arm_movt_hi16);
1038 case ARMMCExpr::VK_ARM_LO16:
1039 if (!isTargetMachO(STI) && EvaluateAsPCRel(E))
1040 Kind = MCFixupKind(isThumb2(STI)
1041 ? ARM::fixup_t2_movw_lo16_pcrel
1042 : ARM::fixup_arm_movw_lo16_pcrel);
1044 Kind = MCFixupKind(isThumb2(STI)
1045 ? ARM::fixup_t2_movw_lo16
1046 : ARM::fixup_arm_movw_lo16);
1049 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
1052 // If the expression doesn't have :upper16: or :lower16: on it,
1053 // it's just a plain immediate expression, and those evaluate to
1054 // the lower 16 bits of the expression regardless of whether
1055 // we have a movt or a movw.
1056 if (!isTargetMachO(STI) && EvaluateAsPCRel(E))
1057 Kind = MCFixupKind(isThumb2(STI)
1058 ? ARM::fixup_t2_movw_lo16_pcrel
1059 : ARM::fixup_arm_movw_lo16_pcrel);
1061 Kind = MCFixupKind(isThumb2(STI)
1062 ? ARM::fixup_t2_movw_lo16
1063 : ARM::fixup_arm_movw_lo16);
1064 Fixups.push_back(MCFixup::Create(0, E, Kind, MI.getLoc()));
1068 uint32_t ARMMCCodeEmitter::
1069 getLdStSORegOpValue(const MCInst &MI, unsigned OpIdx,
1070 SmallVectorImpl<MCFixup> &Fixups,
1071 const MCSubtargetInfo &STI) const {
1072 const MCOperand &MO = MI.getOperand(OpIdx);
1073 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1074 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1075 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1076 unsigned Rm = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1077 unsigned ShImm = ARM_AM::getAM2Offset(MO2.getImm());
1078 bool isAdd = ARM_AM::getAM2Op(MO2.getImm()) == ARM_AM::add;
1079 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(MO2.getImm());
1080 unsigned SBits = getShiftOp(ShOp);
1082 // While "lsr #32" and "asr #32" exist, they are encoded with a 0 in the shift
1083 // amount. However, it would be an easy mistake to make so check here.
1084 assert((ShImm & ~0x1f) == 0 && "Out of range shift amount");
1093 uint32_t Binary = Rm;
1095 Binary |= SBits << 5;
1096 Binary |= ShImm << 7;
1102 uint32_t ARMMCCodeEmitter::
1103 getAddrMode2OpValue(const MCInst &MI, unsigned OpIdx,
1104 SmallVectorImpl<MCFixup> &Fixups,
1105 const MCSubtargetInfo &STI) const {
1107 // {13} 1 == imm12, 0 == Rm
1110 const MCOperand &MO = MI.getOperand(OpIdx);
1111 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1112 uint32_t Binary = getAddrMode2OffsetOpValue(MI, OpIdx + 1, Fixups, STI);
1117 uint32_t ARMMCCodeEmitter::
1118 getAddrMode2OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1119 SmallVectorImpl<MCFixup> &Fixups,
1120 const MCSubtargetInfo &STI) const {
1121 // {13} 1 == imm12, 0 == Rm
1124 const MCOperand &MO = MI.getOperand(OpIdx);
1125 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1126 unsigned Imm = MO1.getImm();
1127 bool isAdd = ARM_AM::getAM2Op(Imm) == ARM_AM::add;
1128 bool isReg = MO.getReg() != 0;
1129 uint32_t Binary = ARM_AM::getAM2Offset(Imm);
1130 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm12
1132 ARM_AM::ShiftOpc ShOp = ARM_AM::getAM2ShiftOpc(Imm);
1133 Binary <<= 7; // Shift amount is bits [11:7]
1134 Binary |= getShiftOp(ShOp) << 5; // Shift type is bits [6:5]
1135 Binary |= CTX.getRegisterInfo()->getEncodingValue(MO.getReg()); // Rm is bits [3:0]
1137 return Binary | (isAdd << 12) | (isReg << 13);
1140 uint32_t ARMMCCodeEmitter::
1141 getPostIdxRegOpValue(const MCInst &MI, unsigned OpIdx,
1142 SmallVectorImpl<MCFixup> &Fixups,
1143 const MCSubtargetInfo &STI) const {
1146 const MCOperand &MO = MI.getOperand(OpIdx);
1147 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1148 bool isAdd = MO1.getImm() != 0;
1149 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg()) | (isAdd << 4);
1152 uint32_t ARMMCCodeEmitter::
1153 getAddrMode3OffsetOpValue(const MCInst &MI, unsigned OpIdx,
1154 SmallVectorImpl<MCFixup> &Fixups,
1155 const MCSubtargetInfo &STI) const {
1156 // {9} 1 == imm8, 0 == Rm
1158 // {7-4} imm7_4/zero
1160 const MCOperand &MO = MI.getOperand(OpIdx);
1161 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1162 unsigned Imm = MO1.getImm();
1163 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1164 bool isImm = MO.getReg() == 0;
1165 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1166 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1168 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1169 return Imm8 | (isAdd << 8) | (isImm << 9);
1172 uint32_t ARMMCCodeEmitter::
1173 getAddrMode3OpValue(const MCInst &MI, unsigned OpIdx,
1174 SmallVectorImpl<MCFixup> &Fixups,
1175 const MCSubtargetInfo &STI) const {
1176 // {13} 1 == imm8, 0 == Rm
1179 // {7-4} imm7_4/zero
1181 const MCOperand &MO = MI.getOperand(OpIdx);
1182 const MCOperand &MO1 = MI.getOperand(OpIdx+1);
1183 const MCOperand &MO2 = MI.getOperand(OpIdx+2);
1185 // If The first operand isn't a register, we have a label reference.
1187 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1189 assert(MO.isExpr() && "Unexpected machine operand type!");
1190 const MCExpr *Expr = MO.getExpr();
1191 MCFixupKind Kind = MCFixupKind(ARM::fixup_arm_pcrel_10_unscaled);
1192 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1194 ++MCNumCPRelocations;
1195 return (Rn << 9) | (1 << 13);
1197 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1198 unsigned Imm = MO2.getImm();
1199 bool isAdd = ARM_AM::getAM3Op(Imm) == ARM_AM::add;
1200 bool isImm = MO1.getReg() == 0;
1201 uint32_t Imm8 = ARM_AM::getAM3Offset(Imm);
1202 // if reg +/- reg, Rm will be non-zero. Otherwise, we have reg +/- imm8
1204 Imm8 = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1205 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1208 /// getAddrModeThumbSPOpValue - Encode the t_addrmode_sp operands.
1209 uint32_t ARMMCCodeEmitter::
1210 getAddrModeThumbSPOpValue(const MCInst &MI, unsigned OpIdx,
1211 SmallVectorImpl<MCFixup> &Fixups,
1212 const MCSubtargetInfo &STI) const {
1215 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1216 assert(MI.getOperand(OpIdx).getReg() == ARM::SP &&
1217 "Unexpected base register!");
1219 // The immediate is already shifted for the implicit zeroes, so no change
1221 return MO1.getImm() & 0xff;
1224 /// getAddrModeISOpValue - Encode the t_addrmode_is# operands.
1225 uint32_t ARMMCCodeEmitter::
1226 getAddrModeISOpValue(const MCInst &MI, unsigned OpIdx,
1227 SmallVectorImpl<MCFixup> &Fixups,
1228 const MCSubtargetInfo &STI) const {
1232 const MCOperand &MO = MI.getOperand(OpIdx);
1233 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1234 unsigned Rn = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1235 unsigned Imm5 = MO1.getImm();
1236 return ((Imm5 & 0x1f) << 3) | Rn;
1239 /// getAddrModePCOpValue - Return encoding for t_addrmode_pc operands.
1240 uint32_t ARMMCCodeEmitter::
1241 getAddrModePCOpValue(const MCInst &MI, unsigned OpIdx,
1242 SmallVectorImpl<MCFixup> &Fixups,
1243 const MCSubtargetInfo &STI) const {
1244 const MCOperand MO = MI.getOperand(OpIdx);
1246 return ::getBranchTargetOpValue(MI, OpIdx, ARM::fixup_arm_thumb_cp, Fixups, STI);
1247 return (MO.getImm() >> 2);
1250 /// getAddrMode5OpValue - Return encoding info for 'reg +/- imm10' operand.
1251 uint32_t ARMMCCodeEmitter::
1252 getAddrMode5OpValue(const MCInst &MI, unsigned OpIdx,
1253 SmallVectorImpl<MCFixup> &Fixups,
1254 const MCSubtargetInfo &STI) const {
1256 // {8} = (U)nsigned (add == '1', sub == '0')
1260 // If The first operand isn't a register, we have a label reference.
1261 const MCOperand &MO = MI.getOperand(OpIdx);
1263 Reg = CTX.getRegisterInfo()->getEncodingValue(ARM::PC); // Rn is PC.
1265 isAdd = false; // 'U' bit is handled as part of the fixup.
1267 assert(MO.isExpr() && "Unexpected machine operand type!");
1268 const MCExpr *Expr = MO.getExpr();
1271 Kind = MCFixupKind(ARM::fixup_t2_pcrel_10);
1273 Kind = MCFixupKind(ARM::fixup_arm_pcrel_10);
1274 Fixups.push_back(MCFixup::Create(0, Expr, Kind, MI.getLoc()));
1276 ++MCNumCPRelocations;
1278 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1279 isAdd = ARM_AM::getAM5Op(Imm8) == ARM_AM::add;
1282 uint32_t Binary = ARM_AM::getAM5Offset(Imm8);
1283 // Immediate is always encoded as positive. The 'U' bit controls add vs sub.
1286 Binary |= (Reg << 9);
1290 unsigned ARMMCCodeEmitter::
1291 getSORegRegOpValue(const MCInst &MI, unsigned OpIdx,
1292 SmallVectorImpl<MCFixup> &Fixups,
1293 const MCSubtargetInfo &STI) const {
1294 // Sub-operands are [reg, reg, imm]. The first register is Rm, the reg to be
1295 // shifted. The second is Rs, the amount to shift by, and the third specifies
1296 // the type of the shift.
1304 const MCOperand &MO = MI.getOperand(OpIdx);
1305 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1306 const MCOperand &MO2 = MI.getOperand(OpIdx + 2);
1307 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm());
1310 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1312 // Encode the shift opcode.
1314 unsigned Rs = MO1.getReg();
1316 // Set shift operand (bit[7:4]).
1322 default: llvm_unreachable("Unknown shift opc!");
1323 case ARM_AM::lsl: SBits = 0x1; break;
1324 case ARM_AM::lsr: SBits = 0x3; break;
1325 case ARM_AM::asr: SBits = 0x5; break;
1326 case ARM_AM::ror: SBits = 0x7; break;
1330 Binary |= SBits << 4;
1332 // Encode the shift operation Rs.
1333 // Encode Rs bit[11:8].
1334 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0);
1335 return Binary | (CTX.getRegisterInfo()->getEncodingValue(Rs) << ARMII::RegRsShift);
1338 unsigned ARMMCCodeEmitter::
1339 getSORegImmOpValue(const MCInst &MI, unsigned OpIdx,
1340 SmallVectorImpl<MCFixup> &Fixups,
1341 const MCSubtargetInfo &STI) const {
1342 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1343 // shifted. The second is the amount to shift by.
1350 const MCOperand &MO = MI.getOperand(OpIdx);
1351 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1352 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1355 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1357 // Encode the shift opcode.
1360 // Set shift operand (bit[6:4]).
1365 // RRX - 110 and bit[11:8] clear.
1367 default: llvm_unreachable("Unknown shift opc!");
1368 case ARM_AM::lsl: SBits = 0x0; break;
1369 case ARM_AM::lsr: SBits = 0x2; break;
1370 case ARM_AM::asr: SBits = 0x4; break;
1371 case ARM_AM::ror: SBits = 0x6; break;
1377 // Encode shift_imm bit[11:7].
1378 Binary |= SBits << 4;
1379 unsigned Offset = ARM_AM::getSORegOffset(MO1.getImm());
1380 assert(Offset < 32 && "Offset must be in range 0-31!");
1381 return Binary | (Offset << 7);
1385 unsigned ARMMCCodeEmitter::
1386 getT2AddrModeSORegOpValue(const MCInst &MI, unsigned OpNum,
1387 SmallVectorImpl<MCFixup> &Fixups,
1388 const MCSubtargetInfo &STI) const {
1389 const MCOperand &MO1 = MI.getOperand(OpNum);
1390 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1391 const MCOperand &MO3 = MI.getOperand(OpNum+2);
1393 // Encoded as [Rn, Rm, imm].
1394 // FIXME: Needs fixup support.
1395 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1397 Value |= CTX.getRegisterInfo()->getEncodingValue(MO2.getReg());
1399 Value |= MO3.getImm();
1404 unsigned ARMMCCodeEmitter::
1405 getT2AddrModeImm8OpValue(const MCInst &MI, unsigned OpNum,
1406 SmallVectorImpl<MCFixup> &Fixups,
1407 const MCSubtargetInfo &STI) const {
1408 const MCOperand &MO1 = MI.getOperand(OpNum);
1409 const MCOperand &MO2 = MI.getOperand(OpNum+1);
1411 // FIXME: Needs fixup support.
1412 unsigned Value = CTX.getRegisterInfo()->getEncodingValue(MO1.getReg());
1414 // Even though the immediate is 8 bits long, we need 9 bits in order
1415 // to represent the (inverse of the) sign bit.
1417 int32_t tmp = (int32_t)MO2.getImm();
1421 Value |= 256; // Set the ADD bit
1426 unsigned ARMMCCodeEmitter::
1427 getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
1428 SmallVectorImpl<MCFixup> &Fixups,
1429 const MCSubtargetInfo &STI) const {
1430 const MCOperand &MO1 = MI.getOperand(OpNum);
1432 // FIXME: Needs fixup support.
1434 int32_t tmp = (int32_t)MO1.getImm();
1438 Value |= 256; // Set the ADD bit
1443 unsigned ARMMCCodeEmitter::
1444 getT2AddrModeImm12OffsetOpValue(const MCInst &MI, unsigned OpNum,
1445 SmallVectorImpl<MCFixup> &Fixups,
1446 const MCSubtargetInfo &STI) const {
1447 const MCOperand &MO1 = MI.getOperand(OpNum);
1449 // FIXME: Needs fixup support.
1451 int32_t tmp = (int32_t)MO1.getImm();
1455 Value |= 4096; // Set the ADD bit
1456 Value |= tmp & 4095;
1460 unsigned ARMMCCodeEmitter::
1461 getT2SORegOpValue(const MCInst &MI, unsigned OpIdx,
1462 SmallVectorImpl<MCFixup> &Fixups,
1463 const MCSubtargetInfo &STI) const {
1464 // Sub-operands are [reg, imm]. The first register is Rm, the reg to be
1465 // shifted. The second is the amount to shift by.
1472 const MCOperand &MO = MI.getOperand(OpIdx);
1473 const MCOperand &MO1 = MI.getOperand(OpIdx + 1);
1474 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO1.getImm());
1477 unsigned Binary = CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1479 // Encode the shift opcode.
1481 // Set shift operand (bit[6:4]).
1487 default: llvm_unreachable("Unknown shift opc!");
1488 case ARM_AM::lsl: SBits = 0x0; break;
1489 case ARM_AM::lsr: SBits = 0x2; break;
1490 case ARM_AM::asr: SBits = 0x4; break;
1491 case ARM_AM::rrx: // FALLTHROUGH
1492 case ARM_AM::ror: SBits = 0x6; break;
1495 Binary |= SBits << 4;
1496 if (SOpc == ARM_AM::rrx)
1499 // Encode shift_imm bit[11:7].
1500 return Binary | ARM_AM::getSORegOffset(MO1.getImm()) << 7;
1503 unsigned ARMMCCodeEmitter::
1504 getBitfieldInvertedMaskOpValue(const MCInst &MI, unsigned Op,
1505 SmallVectorImpl<MCFixup> &Fixups,
1506 const MCSubtargetInfo &STI) const {
1507 // 10 bits. lower 5 bits are are the lsb of the mask, high five bits are the
1509 const MCOperand &MO = MI.getOperand(Op);
1510 uint32_t v = ~MO.getImm();
1511 uint32_t lsb = countTrailingZeros(v);
1512 uint32_t msb = (32 - countLeadingZeros (v)) - 1;
1513 assert (v != 0 && lsb < 32 && msb < 32 && "Illegal bitfield mask!");
1514 return lsb | (msb << 5);
1517 unsigned ARMMCCodeEmitter::
1518 getRegisterListOpValue(const MCInst &MI, unsigned Op,
1519 SmallVectorImpl<MCFixup> &Fixups,
1520 const MCSubtargetInfo &STI) const {
1523 // {7-0} = Number of registers
1526 // {15-0} = Bitfield of GPRs.
1527 unsigned Reg = MI.getOperand(Op).getReg();
1528 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1529 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1531 unsigned Binary = 0;
1533 if (SPRRegs || DPRRegs) {
1535 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg);
1536 unsigned NumRegs = (MI.getNumOperands() - Op) & 0xff;
1537 Binary |= (RegNo & 0x1f) << 8;
1541 Binary |= NumRegs * 2;
1543 for (unsigned I = Op, E = MI.getNumOperands(); I < E; ++I) {
1544 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(MI.getOperand(I).getReg());
1545 Binary |= 1 << RegNo;
1552 /// getAddrMode6AddressOpValue - Encode an addrmode6 register number along
1553 /// with the alignment operand.
1554 unsigned ARMMCCodeEmitter::
1555 getAddrMode6AddressOpValue(const MCInst &MI, unsigned Op,
1556 SmallVectorImpl<MCFixup> &Fixups,
1557 const MCSubtargetInfo &STI) const {
1558 const MCOperand &Reg = MI.getOperand(Op);
1559 const MCOperand &Imm = MI.getOperand(Op + 1);
1561 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1564 switch (Imm.getImm()) {
1568 case 8: Align = 0x01; break;
1569 case 16: Align = 0x02; break;
1570 case 32: Align = 0x03; break;
1573 return RegNo | (Align << 4);
1576 /// getAddrMode6OneLane32AddressOpValue - Encode an addrmode6 register number
1577 /// along with the alignment operand for use in VST1 and VLD1 with size 32.
1578 unsigned ARMMCCodeEmitter::
1579 getAddrMode6OneLane32AddressOpValue(const MCInst &MI, unsigned Op,
1580 SmallVectorImpl<MCFixup> &Fixups,
1581 const MCSubtargetInfo &STI) const {
1582 const MCOperand &Reg = MI.getOperand(Op);
1583 const MCOperand &Imm = MI.getOperand(Op + 1);
1585 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1588 switch (Imm.getImm()) {
1592 case 32: // Default '0' value for invalid alignments of 8, 16, 32 bytes.
1593 case 2: Align = 0x00; break;
1594 case 4: Align = 0x03; break;
1597 return RegNo | (Align << 4);
1601 /// getAddrMode6DupAddressOpValue - Encode an addrmode6 register number and
1602 /// alignment operand for use in VLD-dup instructions. This is the same as
1603 /// getAddrMode6AddressOpValue except for the alignment encoding, which is
1604 /// different for VLD4-dup.
1605 unsigned ARMMCCodeEmitter::
1606 getAddrMode6DupAddressOpValue(const MCInst &MI, unsigned Op,
1607 SmallVectorImpl<MCFixup> &Fixups,
1608 const MCSubtargetInfo &STI) const {
1609 const MCOperand &Reg = MI.getOperand(Op);
1610 const MCOperand &Imm = MI.getOperand(Op + 1);
1612 unsigned RegNo = CTX.getRegisterInfo()->getEncodingValue(Reg.getReg());
1615 switch (Imm.getImm()) {
1619 case 8: Align = 0x01; break;
1620 case 16: Align = 0x03; break;
1623 return RegNo | (Align << 4);
1626 unsigned ARMMCCodeEmitter::
1627 getAddrMode6OffsetOpValue(const MCInst &MI, unsigned Op,
1628 SmallVectorImpl<MCFixup> &Fixups,
1629 const MCSubtargetInfo &STI) const {
1630 const MCOperand &MO = MI.getOperand(Op);
1631 if (MO.getReg() == 0) return 0x0D;
1632 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
1635 unsigned ARMMCCodeEmitter::
1636 getShiftRight8Imm(const MCInst &MI, unsigned Op,
1637 SmallVectorImpl<MCFixup> &Fixups,
1638 const MCSubtargetInfo &STI) const {
1639 return 8 - MI.getOperand(Op).getImm();
1642 unsigned ARMMCCodeEmitter::
1643 getShiftRight16Imm(const MCInst &MI, unsigned Op,
1644 SmallVectorImpl<MCFixup> &Fixups,
1645 const MCSubtargetInfo &STI) const {
1646 return 16 - MI.getOperand(Op).getImm();
1649 unsigned ARMMCCodeEmitter::
1650 getShiftRight32Imm(const MCInst &MI, unsigned Op,
1651 SmallVectorImpl<MCFixup> &Fixups,
1652 const MCSubtargetInfo &STI) const {
1653 return 32 - MI.getOperand(Op).getImm();
1656 unsigned ARMMCCodeEmitter::
1657 getShiftRight64Imm(const MCInst &MI, unsigned Op,
1658 SmallVectorImpl<MCFixup> &Fixups,
1659 const MCSubtargetInfo &STI) const {
1660 return 64 - MI.getOperand(Op).getImm();
1663 void ARMMCCodeEmitter::
1664 EncodeInstruction(const MCInst &MI, raw_ostream &OS,
1665 SmallVectorImpl<MCFixup> &Fixups,
1666 const MCSubtargetInfo &STI) const {
1667 // Pseudo instructions don't get encoded.
1668 const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
1669 uint64_t TSFlags = Desc.TSFlags;
1670 if ((TSFlags & ARMII::FormMask) == ARMII::Pseudo)
1674 if (Desc.getSize() == 2 || Desc.getSize() == 4)
1675 Size = Desc.getSize();
1677 llvm_unreachable("Unexpected instruction size!");
1679 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
1680 // Thumb 32-bit wide instructions need to emit the high order halfword
1682 if (isThumb(STI) && Size == 4) {
1683 EmitConstant(Binary >> 16, 2, OS);
1684 EmitConstant(Binary & 0xffff, 2, OS);
1686 EmitConstant(Binary, Size, OS);
1687 ++MCNumEmitted; // Keep track of the # of mi's emitted.
1690 #include "ARMGenMCCodeEmitter.inc"