44 #define DEBUG_TYPE "mccodeemitter" 46 STATISTIC(MCNumEmitted,
"Number of MC instructions emitted.");
47 STATISTIC(MCNumCPRelocations,
"Number of constant pool relocations created.");
58 : MCII(mcii), CTX(ctx), IsLittleEndian(IsLittle) {
60 ARMMCCodeEmitter(
const ARMMCCodeEmitter &) =
delete;
61 ARMMCCodeEmitter &operator=(
const ARMMCCodeEmitter &) =
delete;
62 ~ARMMCCodeEmitter()
override =
default;
77 unsigned getMachineSoImmOpValue(
unsigned SoImm)
const;
81 uint64_t getBinaryCodeForInstr(
const MCInst &
MI,
98 bool EncodeAddrModeOpValues(
const MCInst &
MI,
unsigned OpIdx,
99 unsigned &
Reg,
unsigned &Imm,
232 uint32_t getAddrMode2OffsetOpValue(
const MCInst &MI,
unsigned OpIdx,
242 uint32_t getAddrMode3OffsetOpValue(
const MCInst &MI,
unsigned OpIdx,
253 uint32_t getAddrModeThumbSPOpValue(
const MCInst &MI,
unsigned OpIdx,
273 uint32_t getAddrMode5FP16OpValue(
const MCInst &MI,
unsigned OpIdx,
278 unsigned getCCOutOpValue(
const MCInst &MI,
unsigned Op,
287 unsigned getSOImmOpValue(
const MCInst &MI,
unsigned Op,
307 unsigned SoImm = MO.
getImm();
309 assert(SoImmVal != -1 &&
"Not a valid so_imm value!");
320 unsigned getModImmOpValue(
const MCInst &MI,
unsigned Op,
339 unsigned getT2SOImmOpValue(
const MCInst &MI,
unsigned Op,
352 unsigned SoImm = MO.
getImm();
354 assert(Encoded != ~0U &&
"Not a Thumb2 so_imm value?");
358 unsigned getT2AddrModeSORegOpValue(
const MCInst &MI,
unsigned OpNum,
361 unsigned getT2AddrModeImm8OpValue(
const MCInst &MI,
unsigned OpNum,
364 unsigned getT2AddrModeImm8OffsetOpValue(
const MCInst &MI,
unsigned OpNum,
369 unsigned getSORegRegOpValue(
const MCInst &MI,
unsigned Op,
372 unsigned getSORegImmOpValue(
const MCInst &MI,
unsigned Op,
375 unsigned getT2SORegOpValue(
const MCInst &MI,
unsigned Op,
379 unsigned getNEONVcvtImm32OpValue(
const MCInst &MI,
unsigned Op,
385 unsigned getBitfieldInvertedMaskOpValue(
const MCInst &MI,
unsigned Op,
389 unsigned getRegisterListOpValue(
const MCInst &MI,
unsigned Op,
392 unsigned getAddrMode6AddressOpValue(
const MCInst &MI,
unsigned Op,
395 unsigned getAddrMode6OneLane32AddressOpValue(
const MCInst &MI,
unsigned Op,
398 unsigned getAddrMode6DupAddressOpValue(
const MCInst &MI,
unsigned Op,
401 unsigned getAddrMode6OffsetOpValue(
const MCInst &MI,
unsigned Op,
405 unsigned getShiftRight8Imm(
const MCInst &MI,
unsigned Op,
408 unsigned getShiftRight16Imm(
const MCInst &MI,
unsigned Op,
411 unsigned getShiftRight32Imm(
const MCInst &MI,
unsigned Op,
414 unsigned getShiftRight64Imm(
const MCInst &MI,
unsigned Op,
418 unsigned getThumbSRImmOpValue(
const MCInst &MI,
unsigned Op,
422 unsigned NEONThumb2DataIPostEncoder(
const MCInst &MI,
423 unsigned EncodedValue,
425 unsigned NEONThumb2LoadStorePostEncoder(
const MCInst &MI,
426 unsigned EncodedValue,
428 unsigned NEONThumb2DupPostEncoder(
const MCInst &MI,
429 unsigned EncodedValue,
431 unsigned NEONThumb2V8PostEncoder(
const MCInst &MI,
432 unsigned EncodedValue,
435 unsigned VFPThumb2PostEncoder(
const MCInst &MI,
436 unsigned EncodedValue,
443 void EmitConstant(uint64_t Val,
unsigned Size,
raw_ostream &OS)
const {
445 for (
unsigned i = 0; i !=
Size; ++i) {
446 unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8;
447 EmitByte((Val >> Shift) & 0xff, OS);
461 unsigned ARMMCCodeEmitter::NEONThumb2DataIPostEncoder(
const MCInst &MI,
462 unsigned EncodedValue,
468 unsigned Bit24 = EncodedValue & 0x01000000;
469 unsigned Bit28 = Bit24 << 4;
470 EncodedValue &= 0xEFFFFFFF;
471 EncodedValue |= Bit28;
472 EncodedValue |= 0x0F000000;
481 unsigned ARMMCCodeEmitter::NEONThumb2LoadStorePostEncoder(
const MCInst &MI,
482 unsigned EncodedValue,
485 EncodedValue &= 0xF0FFFFFF;
486 EncodedValue |= 0x09000000;
495 unsigned ARMMCCodeEmitter::NEONThumb2DupPostEncoder(
const MCInst &MI,
496 unsigned EncodedValue,
499 EncodedValue &= 0x00FFFFFF;
500 EncodedValue |= 0xEE000000;
508 unsigned ARMMCCodeEmitter::NEONThumb2V8PostEncoder(
const MCInst &MI,
509 unsigned EncodedValue,
512 EncodedValue |= 0xC000000;
520 unsigned ARMMCCodeEmitter::
521 VFPThumb2PostEncoder(
const MCInst &MI,
unsigned EncodedValue,
524 EncodedValue &= 0x0FFFFFFF;
525 EncodedValue |= 0xE0000000;
532 unsigned ARMMCCodeEmitter::
544 case ARM::Q0:
case ARM::Q1:
case ARM::Q2:
case ARM::Q3:
545 case ARM::Q4:
case ARM::Q5:
case ARM::Q6:
case ARM::Q7:
546 case ARM::Q8:
case ARM::Q9:
case ARM::Q10:
case ARM::Q11:
547 case ARM::Q12:
case ARM::Q13:
case ARM::Q14:
case ARM::Q15:
550 }
else if (MO.
isImm()) {
551 return static_cast<unsigned>(MO.
getImm());
554 .bitcastToAPInt().getHiBits(32).getLimitedValue());
561 bool ARMMCCodeEmitter::
562 EncodeAddrModeOpValues(
const MCInst &MI,
unsigned OpIdx,
unsigned &Reg,
570 int32_t SImm = MO1.
getImm();
574 if (SImm == INT32_MIN) {
599 assert(MO.
isExpr() &&
"Unexpected branch target type!");
612 uint32_t S = (offset & 0x800000) >> 23;
613 uint32_t J1 = (offset & 0x400000) >> 22;
614 uint32_t J2 = (offset & 0x200000) >> 21;
629 getThumbBLTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
642 getThumbBLXTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
654 getThumbBRTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
661 return (MO.
getImm() >> 1);
666 getThumbBCCTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
673 return (MO.
getImm() >> 1);
678 getThumbCBTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
684 return (MO.
getImm() >> 1);
691 for (
int i = 0; i < NumOp-1; ++i) {
715 return getARMBranchTargetOpValue(MI, OpIdx, Fixups, STI);
721 getARMBranchTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
737 getARMBLTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
752 getARMBLXTargetOpValue(
const MCInst &MI,
unsigned OpIdx,
764 uint32_t ARMMCCodeEmitter::getThumbBranchTargetOpValue(
775 bool I = (Val & 0x800000);
776 bool J1 = (Val & 0x400000);
777 bool J2 = (Val & 0x200000);
794 getAdrLabelOpValue(
const MCInst &MI,
unsigned OpIdx,
801 int64_t offset = MO.
getImm();
805 if (offset == INT32_MIN) {
808 }
else if (offset < 0) {
826 assert(SoImmVal != -1 &&
"Not a valid so_imm value!");
835 getT2AdrLabelOpValue(
const MCInst &MI,
unsigned OpIdx,
842 int32_t Val = MO.
getImm();
843 if (Val == INT32_MIN)
855 getThumbAdrLabelOpValue(
const MCInst &MI,
unsigned OpIdx,
868 getThumbAddrModeRegRegOpValue(
const MCInst &MI,
unsigned OpIdx,
878 return (Rm << 3) | Rn;
883 getAddrModeImm12OpValue(
const MCInst &MI,
unsigned OpIdx,
908 ++MCNumCPRelocations;
912 if (Offset == INT32_MIN) {
915 }
else if (Offset < 0) {
922 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm12, Fixups, STI);
928 Binary |= (Reg << 13);
935 getT2Imm8s4OpValue(
const MCInst &MI,
unsigned OpIdx,
948 bool isAdd = Imm8 >= 0;
967 getT2AddrModeImm8s4OpValue(
const MCInst &MI,
unsigned OpIdx,
982 assert(MO.
isExpr() &&
"Unexpected machine operand type!");
987 ++MCNumCPRelocations;
989 isAdd = EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
997 uint32_t Binary = (Imm8 >> 2) & 0xff;
1001 Binary |= (Reg << 9);
1008 getT2AddrModeImm0_1020s4OpValue(
const MCInst &MI,
unsigned OpIdx,
1017 return (Reg << 8) |
Imm8;
1021 ARMMCCodeEmitter::getHiLo16ImmOpValue(
const MCInst &MI,
unsigned OpIdx,
1029 return static_cast<unsigned>(MO.
getImm());
1035 const ARMMCExpr *ARM16Expr = cast<ARMMCExpr>(
E);
1039 const int64_t
Value = MCE->getValue();
1040 if (Value > UINT32_MAX)
1043 switch (ARM16Expr->
getKind()) {
1045 return (int32_t(Value) & 0xffff0000) >> 16;
1047 return (int32_t(Value) & 0x0000ffff);
1052 switch (ARM16Expr->
getKind()) {
1077 getLdStSORegOpValue(
const MCInst &MI,
unsigned OpIdx,
1088 unsigned SBits = getShiftOp(ShOp);
1092 assert((ShImm & ~0x1f) == 0 &&
"Out of range shift amount");
1103 Binary |= SBits << 5;
1104 Binary |= ShImm << 7;
1111 getAddrMode2OffsetOpValue(
const MCInst &MI,
unsigned OpIdx,
1119 unsigned Imm = MO1.
getImm();
1127 Binary |= getShiftOp(ShOp) << 5;
1130 return Binary | (isAdd << 12) | (isReg << 13);
1134 getPostIdxRegOpValue(
const MCInst &MI,
unsigned OpIdx,
1141 bool isAdd = MO1.
getImm() != 0;
1146 getAddrMode3OffsetOpValue(
const MCInst &MI,
unsigned OpIdx,
1155 unsigned Imm = MO1.
getImm();
1157 bool isImm = MO.
getReg() == 0;
1162 return Imm8 | (isAdd << 8) | (isImm << 9);
1166 getAddrMode3OpValue(
const MCInst &MI,
unsigned OpIdx,
1182 assert(MO.
isExpr() &&
"Unexpected machine operand type!");
1187 ++MCNumCPRelocations;
1188 return (Rn << 9) | (1 << 13);
1191 unsigned Imm = MO2.
getImm();
1193 bool isImm = MO1.
getReg() == 0;
1198 return (Rn << 9) | Imm8 | (isAdd << 8) | (isImm << 13);
1203 getAddrModeThumbSPOpValue(
const MCInst &MI,
unsigned OpIdx,
1210 "Unexpected base register!");
1214 return MO1.
getImm() & 0xff;
1219 getAddrModeISOpValue(
const MCInst &MI,
unsigned OpIdx,
1228 unsigned Imm5 = MO1.
getImm();
1229 return ((Imm5 & 0x1f) << 3) | Rn;
1234 getAddrModePCOpValue(
const MCInst &MI,
unsigned OpIdx,
1240 return (MO.
getImm() >> 2);
1245 getAddrMode5OpValue(
const MCInst &MI,
unsigned OpIdx,
1260 assert(MO.
isExpr() &&
"Unexpected machine operand type!");
1269 ++MCNumCPRelocations;
1271 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1279 Binary |= (Reg << 9);
1285 getAddrMode5FP16OpValue(
const MCInst &MI,
unsigned OpIdx,
1300 assert(MO.
isExpr() &&
"Unexpected machine operand type!");
1309 ++MCNumCPRelocations;
1311 EncodeAddrModeOpValues(MI, OpIdx, Reg, Imm8, Fixups, STI);
1319 Binary |= (Reg << 9);
1323 unsigned ARMMCCodeEmitter::
1324 getSORegRegOpValue(
const MCInst &MI,
unsigned OpIdx,
1347 unsigned Rs = MO1.
getReg();
1363 Binary |= SBits << 4;
1371 unsigned ARMMCCodeEmitter::
1372 getSORegImmOpValue(
const MCInst &MI,
unsigned OpIdx,
1411 Binary |= SBits << 4;
1413 assert(Offset < 32 &&
"Offset must be in range 0-31!");
1414 return Binary | (Offset << 7);
1418 unsigned ARMMCCodeEmitter::
1419 getT2AddrModeSORegOpValue(
const MCInst &MI,
unsigned OpNum,
1437 unsigned ARMMCCodeEmitter::
1438 getT2AddrModeImm8OpValue(
const MCInst &MI,
unsigned OpNum,
1450 int32_t tmp = (int32_t)MO2.
getImm();
1459 unsigned ARMMCCodeEmitter::
1460 getT2AddrModeImm8OffsetOpValue(
const MCInst &MI,
unsigned OpNum,
1467 int32_t tmp = (int32_t)MO1.
getImm();
1476 unsigned ARMMCCodeEmitter::
1477 getT2SORegOpValue(
const MCInst &MI,
unsigned OpIdx,
1511 Binary |= SBits << 4;
1519 unsigned ARMMCCodeEmitter::
1520 getBitfieldInvertedMaskOpValue(
const MCInst &MI,
unsigned Op,
1529 assert(v != 0 && lsb < 32 && msb < 32 &&
"Illegal bitfield mask!");
1530 return lsb | (msb << 5);
1533 unsigned ARMMCCodeEmitter::
1534 getRegisterListOpValue(
const MCInst &MI,
unsigned Op,
1544 bool SPRRegs = ARMMCRegisterClasses[ARM::SPRRegClassID].contains(Reg);
1545 bool DPRRegs = ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg);
1547 unsigned Binary = 0;
1549 if (SPRRegs || DPRRegs) {
1553 Binary |= (RegNo & 0x1f) << 8;
1557 Binary |= NumRegs * 2;
1568 Binary |= 1 << RegNo;
1577 unsigned ARMMCCodeEmitter::
1578 getAddrMode6AddressOpValue(
const MCInst &MI,
unsigned Op,
1591 case 8: Align = 0x01;
break;
1592 case 16: Align = 0x02;
break;
1593 case 32: Align = 0x03;
break;
1596 return RegNo | (Align << 4);
1601 unsigned ARMMCCodeEmitter::
1602 getAddrMode6OneLane32AddressOpValue(
const MCInst &MI,
unsigned Op,
1616 case 2: Align = 0x00;
break;
1617 case 4: Align = 0x03;
break;
1620 return RegNo | (Align << 4);
1628 unsigned ARMMCCodeEmitter::
1629 getAddrMode6DupAddressOpValue(
const MCInst &MI,
unsigned Op,
1642 case 8: Align = 0x01;
break;
1643 case 16: Align = 0x03;
break;
1646 return RegNo | (Align << 4);
1649 unsigned ARMMCCodeEmitter::
1650 getAddrMode6OffsetOpValue(
const MCInst &MI,
unsigned Op,
1654 if (MO.
getReg() == 0)
return 0x0D;
1658 unsigned ARMMCCodeEmitter::
1659 getShiftRight8Imm(
const MCInst &MI,
unsigned Op,
1665 unsigned ARMMCCodeEmitter::
1666 getShiftRight16Imm(
const MCInst &MI,
unsigned Op,
1672 unsigned ARMMCCodeEmitter::
1673 getShiftRight32Imm(
const MCInst &MI,
unsigned Op,
1679 unsigned ARMMCCodeEmitter::
1680 getShiftRight64Imm(
const MCInst &MI,
unsigned Op,
1686 void ARMMCCodeEmitter::
1692 uint64_t TSFlags = Desc.
TSFlags;
1702 uint32_t Binary = getBinaryCodeForInstr(MI, Fixups, STI);
1705 if (
isThumb(STI) && Size == 4) {
1706 EmitConstant(Binary >> 16, 2, OS);
1707 EmitConstant(Binary & 0xffff, 2, OS);
1709 EmitConstant(Binary, Size, OS);
1713 #include "ARMGenMCCodeEmitter.inc" 1718 return new ARMMCCodeEmitter(MCII, Ctx,
true);
1724 return new ARMMCCodeEmitter(MCII, Ctx,
false);
static bool isReg(const MCInst &MI, unsigned OpNo)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned getSOImmValImm(unsigned Imm)
getSOImmValImm - Given an encoded imm field for the reg/imm form, return the 8-bit imm value...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
VariantKind getKind() const
getOpcode - Get the kind of this expression.
void push_back(const T &Elt)
Describe properties that are true of each instruction in the target description file.
STATISTIC(NumFunctions, "Total number of functions")
std::size_t countLeadingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the most significant bit to the least stopping at the first 1...
static Lanai::Fixups FixupKind(const MCExpr *Expr)
static bool isThumb(const MCSubtargetInfo &STI)
const Triple & getTargetTriple() const
ShiftOpc getAM2ShiftOpc(unsigned AM2Opc)
const FeatureBitset & getFeatureBits() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Base class for the full range of assembler expressions which are needed for parsing.
This file implements a class to represent arbitrary precision integral constant values and operations...
unsigned getReg() const
Returns the register number.
Context object for machine code objects.
unsigned char getAM3Offset(unsigned AM3Opc)
const MCExpr * getExpr() const
Instances of this class represent a single low-level machine instruction.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
AddrOpc getAM2Op(unsigned AM2Opc)
unsigned char getAM5Offset(unsigned AM5Opc)
AddrOpc getAM3Op(unsigned AM3Opc)
unsigned const MachineRegisterInfo * MRI
std::size_t countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
unsigned getSORegOffset(unsigned Op)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
MCCodeEmitter - Generic instruction encoding interface.
Interface to description of machine instruction set.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
This file declares a class to represent arbitrary precision floating point values and provide a varie...
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
unsigned getSOImmValRot(unsigned Imm)
getSOImmValRot - Given an encoded imm field for the reg/imm form, return the rotate amount...
unsigned getNumOperands() const
bool isOSBinFormatMachO() const
Tests whether the environment is MachO.
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
static int32_t encodeThumbBLOffset(int32_t offset)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
static bool HasConditionalBranch(const MCInst &MI)
Return true if this branch has a non-always predication.
int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
const MCOperand & getOperand(unsigned i) const
unsigned getAM2Offset(unsigned AM2Opc)
ShiftOpc getSORegShOp(unsigned Op)
MCCodeEmitter * createARMLEMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
uint16_t getEncodingValue(unsigned RegNo) const
Returns the encoding for RegNo.
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Generic base class for all target subtargets.
static uint32_t getBranchTargetOpValue(const MCInst &MI, unsigned OpIdx, unsigned FixupKind, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI)
getBranchTargetOpValue - Helper function to get the branch target operand, which is either an immedia...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCRegisterInfo * getRegisterInfo() const
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
Target specific expression.
AddrOpc getAM5Op(unsigned AM5Opc)
unsigned getOpcode() const
Instances of this class represent operands of the MCInst class.
MCCodeEmitter * createARMBEMCCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo &MRI, MCContext &Ctx)
Function Alias Analysis false
unsigned getSize() const
Return the number of bytes in the encoding of this instruction, or zero if the encoding size cannot b...