#include "AVRMCCodeEmitter.h"
#include "MCTargetDesc/AVRMCExpr.h"
#include "MCTargetDesc/AVRMCTargetDesc.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/EndianStream.h"
#include "llvm/Support/raw_ostream.h"
#define DEBUG_TYPE "mccodeemitter"
#define GET_INSTRMAP_INFO
#include "AVRGenInstrInfo.inc"
#undef GET_INSTRMAP_INFO
namespace llvm {
unsigned
AVRMCCodeEmitter::loadStorePostEncoder(const MCInst &MI, unsigned EncodedValue,
const MCSubtargetInfo &STI) const {
assert(MI.getOperand(0).isReg() && MI.getOperand(1).isReg() &&
"the load/store operands must be registers");
unsigned Opcode = MI.getOpcode();
bool IsRegX = MI.getOperand(0).getReg() == AVR::R27R26 ||
MI.getOperand(1).getReg() == AVR::R27R26;
bool IsPredec = Opcode == AVR::LDRdPtrPd || Opcode == AVR::STPtrPdRr;
bool IsPostinc = Opcode == AVR::LDRdPtrPi || Opcode == AVR::STPtrPiRr;
if (IsRegX || IsPredec || IsPostinc) {
EncodedValue |= (1 << 12);
}
return EncodedValue;
}
template <AVR::Fixups Fixup>
unsigned
AVRMCCodeEmitter::encodeRelCondBrTarget(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
const MCOperand &MO = MI.getOperand(OpNo);
if (MO.isExpr()) {
Fixups.push_back(
MCFixup::create(0, MO.getExpr(), MCFixupKind(Fixup), MI.getLoc()));
return 0;
}
assert(MO.isImm());
auto target = MO.getImm();
AVR::fixups::adjustBranchTarget(target);
return target;
}
unsigned AVRMCCodeEmitter::encodeLDSTPtrReg(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
auto MO = MI.getOperand(OpNo);
assert(MO.isReg());
switch (MO.getReg()) {
case AVR::R27R26:
return 0x03; case AVR::R29R28:
return 0x02; case AVR::R31R30:
return 0x00; default:
llvm_unreachable("invalid pointer register");
}
}
unsigned AVRMCCodeEmitter::encodeMemri(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
auto RegOp = MI.getOperand(OpNo);
auto OffsetOp = MI.getOperand(OpNo + 1);
assert(RegOp.isReg() && "Expected register operand");
uint8_t RegBit = 0;
switch (RegOp.getReg()) {
default:
llvm_unreachable("Expected either Y or Z register");
case AVR::R31R30:
RegBit = 0;
break; case AVR::R29R28:
RegBit = 1;
break; }
int8_t OffsetBits;
if (OffsetOp.isImm()) {
OffsetBits = OffsetOp.getImm();
} else if (OffsetOp.isExpr()) {
OffsetBits = 0;
Fixups.push_back(MCFixup::create(0, OffsetOp.getExpr(),
MCFixupKind(AVR::fixup_6), MI.getLoc()));
} else {
llvm_unreachable("invalid value for offset");
}
return (RegBit << 6) | OffsetBits;
}
unsigned AVRMCCodeEmitter::encodeComplement(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
assert(MI.getOperand(OpNo).isImm());
auto Imm = MI.getOperand(OpNo).getImm();
return (~0) - Imm;
}
template <AVR::Fixups Fixup, unsigned Offset>
unsigned AVRMCCodeEmitter::encodeImm(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
auto MO = MI.getOperand(OpNo);
if (MO.isExpr()) {
if (isa<AVRMCExpr>(MO.getExpr())) {
return getExprOpValue(MO.getExpr(), Fixups, STI);
}
MCFixupKind FixupKind = static_cast<MCFixupKind>(Fixup);
Fixups.push_back(
MCFixup::create(Offset, MO.getExpr(), FixupKind, MI.getLoc()));
return 0;
}
assert(MO.isImm());
return MO.getImm();
}
unsigned AVRMCCodeEmitter::encodeCallTarget(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
auto MO = MI.getOperand(OpNo);
if (MO.isExpr()) {
MCFixupKind FixupKind = static_cast<MCFixupKind>(AVR::fixup_call);
Fixups.push_back(MCFixup::create(0, MO.getExpr(), FixupKind, MI.getLoc()));
return 0;
}
assert(MO.isImm());
auto Target = MO.getImm();
AVR::fixups::adjustBranchTarget(Target);
return Target;
}
unsigned AVRMCCodeEmitter::getExprOpValue(const MCExpr *Expr,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
MCExpr::ExprKind Kind = Expr->getKind();
if (Kind == MCExpr::Binary) {
Expr = static_cast<const MCBinaryExpr *>(Expr)->getLHS();
Kind = Expr->getKind();
}
if (Kind == MCExpr::Target) {
AVRMCExpr const *AVRExpr = cast<AVRMCExpr>(Expr);
int64_t Result;
if (AVRExpr->evaluateAsConstant(Result)) {
return Result;
}
MCFixupKind FixupKind = static_cast<MCFixupKind>(AVRExpr->getFixupKind());
Fixups.push_back(MCFixup::create(0, AVRExpr, FixupKind));
return 0;
}
assert(Kind == MCExpr::SymbolRef);
return 0;
}
unsigned AVRMCCodeEmitter::getMachineOpValue(const MCInst &MI,
const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
if (MO.isReg())
return Ctx.getRegisterInfo()->getEncodingValue(MO.getReg());
if (MO.isImm())
return static_cast<unsigned>(MO.getImm());
if (MO.isDFPImm())
return static_cast<unsigned>(bit_cast<double>(MO.getDFPImm()));
assert(MO.isExpr());
return getExprOpValue(MO.getExpr(), Fixups, STI);
}
void AVRMCCodeEmitter::emitInstruction(uint64_t Val, unsigned Size,
const MCSubtargetInfo &STI,
raw_ostream &OS) const {
size_t WordCount = Size / 2;
for (int64_t i = WordCount - 1; i >= 0; --i) {
uint16_t Word = (Val >> (i * 16)) & 0xFFFF;
support::endian::write(OS, Word, support::endianness::little);
}
}
void AVRMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
unsigned Size = Desc.getSize();
assert(Size > 0 && "Instruction size cannot be zero");
uint64_t BinaryOpCode = getBinaryCodeForInstr(MI, Fixups, STI);
emitInstruction(BinaryOpCode, Size, STI, OS);
}
MCCodeEmitter *createAVRMCCodeEmitter(const MCInstrInfo &MCII,
MCContext &Ctx) {
return new AVRMCCodeEmitter(MCII, Ctx);
}
#include "AVRGenMCCodeEmitter.inc"
}