29#define DEBUG_TYPE "csky-mccode-emitter"
31STATISTIC(MCNumEmitted,
"Number of MC instructions emitted");
37 case CSKY::Fixups::fixup_csky_pcrel_imm16_scale2:
38 case CSKY::Fixups::fixup_csky_pcrel_uimm16_scale4:
39 case CSKY::Fixups::fixup_csky_pcrel_imm26_scale2:
40 case CSKY::Fixups::fixup_csky_pcrel_imm18_scale2:
41 case CSKY::Fixups::fixup_csky_gotpc:
42 case CSKY::Fixups::fixup_csky_pcrel_imm10_scale2:
43 case CSKY::Fixups::fixup_csky_pcrel_uimm7_scale4:
44 case CSKY::Fixups::fixup_csky_pcrel_uimm8_scale4:
57 : Ctx(Ctx), MII(MII) {}
59 ~CSKYMCCodeEmitter() {}
75 template <
int shift = 0>
76 unsigned getImmOpValue(
const MCInst &
MI,
unsigned Idx,
81 return (MO.
getImm() >> shift);
90 unsigned getRegSeqImmOpValue(
const MCInst &
MI,
unsigned Idx,
94 unsigned getRegisterSeqOpValue(
const MCInst &
MI,
unsigned Op,
98 unsigned getOImmOpValue(
const MCInst &
MI,
unsigned Idx,
102 unsigned getImmOpValueIDLY(
const MCInst &
MI,
unsigned Idx,
106 unsigned getImmJMPIX(
const MCInst &
MI,
unsigned Idx,
110 unsigned getImmOpValueMSBSize(
const MCInst &
MI,
unsigned Idx,
114 unsigned getImmShiftOpValue(
const MCInst &
MI,
unsigned Idx,
124 template <llvm::CSKY::Fixups FIXUP>
125 unsigned getBranchSymbolOpValue(
const MCInst &
MI,
unsigned Idx,
143 template <llvm::CSKY::Fixups FIXUP>
144 unsigned getConstpoolSymbolOpValue(
const MCInst &
MI,
unsigned Idx,
158 template <llvm::CSKY::Fixups FIXUP>
159 unsigned getDataSymbolOpValue(
const MCInst &
MI,
unsigned Idx,
173 unsigned getCallSymbolOpValue(
const MCInst &
MI,
unsigned Idx,
187 unsigned getBareSymbolOpValue(
const MCInst &
MI,
unsigned Idx,
213unsigned CSKYMCCodeEmitter::getOImmOpValue(
const MCInst &
MI,
unsigned Idx,
222CSKYMCCodeEmitter::getImmOpValueIDLY(
const MCInst &
MI,
unsigned Idx,
233CSKYMCCodeEmitter::getImmOpValueMSBSize(
const MCInst &
MI,
unsigned Idx,
263 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
272 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
283 unsigned Size =
MI.getOpcode() == CSKY::NEG32 ? 4 : 2;
288 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
295 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
305 unsigned Size =
MI.getOpcode() == CSKY::RSUBI32 ? 4 : 2;
310 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
316 .
addImm(
MI.getOperand(2).getImm() + 1);
317 Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI);
321void CSKYMCCodeEmitter::encodeInstruction(
const MCInst &
MI,
330 switch (
MI.getOpcode()) {
336 expandJBTF(
MI, CB, Fixups, STI);
341 expandNEG(
MI, CB, Fixups, STI);
346 expandRSUBI(
MI, CB, Fixups, STI);
378 case CSKY::LRW32_Gen:
383 case CSKY::LRW16_Gen:
392 .
addImm(
MI.getOperand(2).getImm() + 1);
398 .
addImm(
MI.getOperand(2).getImm() + 1);
404 .
addImm(32 -
MI.getOperand(2).getImm());
407 auto V = 1 <<
MI.getOperand(1).getImm();
414 writeData(getBinaryCodeForInstr(TmpInst, Fixups, STI),
Size, CB);
425 return static_cast<unsigned>(MO.
getImm());
432CSKYMCCodeEmitter::getRegSeqImmOpValue(
const MCInst &
MI,
unsigned Idx,
435 assert(
MI.getOperand(
Idx).isReg() &&
"Unexpected MO type.");
436 assert(
MI.getOperand(
Idx + 1).isImm() &&
"Unexpected MO type.");
438 unsigned Ry =
MI.getOperand(
Idx).getReg();
439 unsigned Rz =
MI.getOperand(
Idx + 1).getImm();
448CSKYMCCodeEmitter::getRegisterSeqOpValue(
const MCInst &
MI,
unsigned Op,
456 unsigned Binary = ((Reg1 & 0x1f) << 5) | (Reg2 - Reg1);
461unsigned CSKYMCCodeEmitter::getImmJMPIX(
const MCInst &
MI,
unsigned Idx,
464 switch (
MI.getOperand(
Idx).getImm()) {
479 const auto *CSKYExpr = cast<MCSpecifierExpr>(Expr);
480 switch (CSKYExpr->getSpecifier()) {
506 return new CSKYMCCodeEmitter(Ctx, MCII);
509#include "CSKYGenMCCodeEmitter.inc"
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void writeData(uint32_t Bin, unsigned Size, SmallVectorImpl< char > &CB)
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This class represents an Operation in the Expression.
MCCodeEmitter - Generic instruction encoding interface.
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
Context object for machine code objects.
const MCRegisterInfo * getRegisterInfo() const
Base class for the full range of assembler expressions which are needed for parsing.
@ Specifier
Expression with a relocation specifier.
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
MCInstBuilder & addOperand(const MCOperand &Op)
Add an operand.
MCInstBuilder & addImm(int64_t Val)
Add a new integer immediate operand.
Instances of this class represent a single low-level machine instruction.
Describe properties that are true of each instruction in the target description file.
Interface to description of machine instruction set.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Instances of this class represent operands of the MCInst class.
MCRegister getReg() const
Returns the register number.
const MCExpr * getExpr() const
uint16_t getEncodingValue(MCRegister Reg) const
Returns the encoding for Reg.
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
LLVM Value Representation.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ fixup_csky_pcrel_imm18_scale2
@ fixup_csky_plt_imm18_scale4
@ fixup_csky_pcrel_imm26_scale2
@ fixup_csky_got_imm18_scale4
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
This is an optimization pass for GlobalISel generic memory operations.
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
static void addFixup(SmallVectorImpl< MCFixup > &Fixups, uint32_t Offset, const MCExpr *Value, uint16_t Kind)
MCCodeEmitter * createCSKYMCCodeEmitter(const MCInstrInfo &MCII, MCContext &Ctx)
Description of the encoding of one expression Op.