llvm-project/llvm/lib/Target/Foot/MCTargetDesc/FootMCCodeEmitter.cpp
2025-10-25 22:30:05 -04:00

76 lines
2.5 KiB
C++

#include "MCTargetDesc/FootMCTargetDesc.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/EndianStream.h"
#include <cstdint>
using namespace llvm;
#define DEBUG_TYPE "mccodeemitter"
namespace {
class FootMCCodeEmitter : public MCCodeEmitter {
MCContext &MCCtxt;
public:
FootMCCodeEmitter(MCContext &MCCtxt) : MCCodeEmitter(), MCCtxt(MCCtxt) {}
~FootMCCodeEmitter() override = default;
uint64_t getBinaryCodeForInstr(const MCInst &MI,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;
unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;
void encodeInstruction(const MCInst &MI, SmallVectorImpl<char> &CB,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const override;
};
} // namespace
MCCodeEmitter *llvm::createFootMCCodeEmitter(const MCInstrInfo &MCII,
MCContext &MCCtxt) {
return new FootMCCodeEmitter(MCCtxt);
}
unsigned FootMCCodeEmitter::getMachineOpValue(const MCInst &MI, const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
if (MO.isReg()) {
return MCCtxt.getRegisterInfo()->getEncodingValue(MO.getReg());
}
if (MO.isImm()) {
return static_cast<unsigned>(MO.getImm());
}
if (MO.isExpr()) {
MCFixupKind FK = FK_Data_2;
Fixups.push_back(MCFixup::create(0, MO.getExpr(), FK));
return 0;
}
llvm_unreachable("Unsupported operation type");
return 0;
}
void FootMCCodeEmitter::encodeInstruction(const MCInst &MI,
SmallVectorImpl<char> &CB,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
uint64_t Encoding = getBinaryCodeForInstr(MI, Fixups, STI);
assert(((Encoding & 0xffffffff00000000) == 0) && "Only the first 32 bits should be set");
support::endian::write<uint32_t>(CB, Encoding, llvm::endianness::little);
}
#include "FootGenAsmEmitter.inc"