mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-10 02:36:06 +00:00
b594c4c873
The GNU assembler treats things like: brasl %r14, 100 in the same way as: brasl %r14, .+100 rather than as a branch to absolute address 100. We implemented this in LLVM by creating an immediate operand rather than the usual expr operand, and by handling immediate operands specially in the code emitter. This was undesirable for (at least) three reasons: - the specialness of immediate operands was exposed to the backend MC code, rather than being limited to the assembler parser. - in disassembly, an immediate operand really is an absolute address. (Note that this means reassembling printed disassembly can't recreate the original code.) - it would interfere with any assembly manipulation that we might try in future. E.g. operations like branch shortening can change the relative position of instructions, but any code that updates sym+offset addresses wouldn't update an immediate "100" operand in the same way as an explicit ".+100" operand. This patch changes the implementation so that the assembler creates a "." label for immediate PC-relative operands, so that the operand to the MCInst is always the absolute address. The patch also adds some error checking of the offset. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181773 91177308-0d34-0410-b5e6-96231b3b80d8
184 lines
7.4 KiB
C++
184 lines
7.4 KiB
C++
//===-- SystemZMCCodeEmitter.cpp - Convert SystemZ code to machine code ---===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the SystemZMCCodeEmitter class.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "mccodeemitter"
|
|
#include "MCTargetDesc/SystemZMCTargetDesc.h"
|
|
#include "MCTargetDesc/SystemZMCFixups.h"
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
#include "llvm/MC/MCContext.h"
|
|
#include "llvm/MC/MCExpr.h"
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
class SystemZMCCodeEmitter : public MCCodeEmitter {
|
|
const MCInstrInfo &MCII;
|
|
MCContext &Ctx;
|
|
|
|
public:
|
|
SystemZMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
|
|
: MCII(mcii), Ctx(ctx) {
|
|
}
|
|
|
|
~SystemZMCCodeEmitter() {}
|
|
|
|
// OVerride MCCodeEmitter.
|
|
virtual void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
SmallVectorImpl<MCFixup> &Fixups) const
|
|
LLVM_OVERRIDE;
|
|
|
|
private:
|
|
// Automatically generated by TableGen.
|
|
uint64_t getBinaryCodeForInstr(const MCInst &MI,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
// Called by the TableGen code to get the binary encoding of operand
|
|
// MO in MI. Fixups is the list of fixups against MI.
|
|
uint64_t getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
// Called by the TableGen code to get the binary encoding of an address.
|
|
// The index, if any, is encoded first, followed by the base,
|
|
// followed by the displacement. In a 20-bit displacement,
|
|
// the low 12 bits are encoded before the high 8 bits.
|
|
uint64_t getBDAddr12Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
uint64_t getBDAddr20Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
uint64_t getBDXAddr12Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
uint64_t getBDXAddr20Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const;
|
|
|
|
// Operand OpNum of MI needs a PC-relative fixup of kind Kind at
|
|
// Offset bytes from the start of MI. Add the fixup to Fixups
|
|
// and return the in-place addend, which since we're a RELA target
|
|
// is always 0.
|
|
uint64_t getPCRelEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
unsigned Kind, int64_t Offset) const;
|
|
|
|
uint64_t getPC16DBLEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
return getPCRelEncoding(MI, OpNum, Fixups, SystemZ::FK_390_PC16DBL, 2);
|
|
}
|
|
uint64_t getPC32DBLEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
return getPCRelEncoding(MI, OpNum, Fixups, SystemZ::FK_390_PC32DBL, 2);
|
|
}
|
|
uint64_t getPLT16DBLEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
return getPCRelEncoding(MI, OpNum, Fixups, SystemZ::FK_390_PLT16DBL, 2);
|
|
}
|
|
uint64_t getPLT32DBLEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
return getPCRelEncoding(MI, OpNum, Fixups, SystemZ::FK_390_PLT32DBL, 2);
|
|
}
|
|
};
|
|
}
|
|
|
|
MCCodeEmitter *llvm::createSystemZMCCodeEmitter(const MCInstrInfo &MCII,
|
|
const MCRegisterInfo &MRI,
|
|
const MCSubtargetInfo &MCSTI,
|
|
MCContext &Ctx) {
|
|
return new SystemZMCCodeEmitter(MCII, Ctx);
|
|
}
|
|
|
|
void SystemZMCCodeEmitter::
|
|
EncodeInstruction(const MCInst &MI, raw_ostream &OS,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
|
|
unsigned Size = MCII.get(MI.getOpcode()).getSize();
|
|
// Big-endian insertion of Size bytes.
|
|
unsigned ShiftValue = (Size * 8) - 8;
|
|
for (unsigned I = 0; I != Size; ++I) {
|
|
OS << uint8_t(Bits >> ShiftValue);
|
|
ShiftValue -= 8;
|
|
}
|
|
}
|
|
|
|
uint64_t SystemZMCCodeEmitter::
|
|
getMachineOpValue(const MCInst &MI, const MCOperand &MO,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
if (MO.isReg())
|
|
return Ctx.getRegisterInfo().getEncodingValue(MO.getReg());
|
|
if (MO.isImm())
|
|
return static_cast<uint64_t>(MO.getImm());
|
|
llvm_unreachable("Unexpected operand type!");
|
|
}
|
|
|
|
uint64_t SystemZMCCodeEmitter::
|
|
getBDAddr12Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups);
|
|
uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups);
|
|
assert(isUInt<4>(Base) && isUInt<12>(Disp));
|
|
return (Base << 12) | Disp;
|
|
}
|
|
|
|
uint64_t SystemZMCCodeEmitter::
|
|
getBDAddr20Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups);
|
|
uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups);
|
|
assert(isUInt<4>(Base) && isInt<20>(Disp));
|
|
return (Base << 20) | ((Disp & 0xfff) << 8) | ((Disp & 0xff000) >> 12);
|
|
}
|
|
|
|
uint64_t SystemZMCCodeEmitter::
|
|
getBDXAddr12Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups);
|
|
uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups);
|
|
uint64_t Index = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups);
|
|
assert(isUInt<4>(Base) && isUInt<12>(Disp) && isUInt<4>(Index));
|
|
return (Index << 16) | (Base << 12) | Disp;
|
|
}
|
|
|
|
uint64_t SystemZMCCodeEmitter::
|
|
getBDXAddr20Encoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups) const {
|
|
uint64_t Base = getMachineOpValue(MI, MI.getOperand(OpNum), Fixups);
|
|
uint64_t Disp = getMachineOpValue(MI, MI.getOperand(OpNum + 1), Fixups);
|
|
uint64_t Index = getMachineOpValue(MI, MI.getOperand(OpNum + 2), Fixups);
|
|
assert(isUInt<4>(Base) && isInt<20>(Disp) && isUInt<4>(Index));
|
|
return (Index << 24) | (Base << 20) | ((Disp & 0xfff) << 8)
|
|
| ((Disp & 0xff000) >> 12);
|
|
}
|
|
|
|
uint64_t
|
|
SystemZMCCodeEmitter::getPCRelEncoding(const MCInst &MI, unsigned OpNum,
|
|
SmallVectorImpl<MCFixup> &Fixups,
|
|
unsigned Kind, int64_t Offset) const {
|
|
const MCOperand &MO = MI.getOperand(OpNum);
|
|
const MCExpr *Expr;
|
|
if (MO.isImm())
|
|
Expr = MCConstantExpr::Create(MO.getImm() + Offset, Ctx);
|
|
else {
|
|
Expr = MO.getExpr();
|
|
if (Offset) {
|
|
// The operand value is relative to the start of MI, but the fixup
|
|
// is relative to the operand field itself, which is Offset bytes
|
|
// into MI. Add Offset to the relocation value to cancel out
|
|
// this difference.
|
|
const MCExpr *OffsetExpr = MCConstantExpr::Create(Offset, Ctx);
|
|
Expr = MCBinaryExpr::CreateAdd(Expr, OffsetExpr, Ctx);
|
|
}
|
|
}
|
|
Fixups.push_back(MCFixup::Create(Offset, Expr, (MCFixupKind)Kind));
|
|
return 0;
|
|
}
|
|
|
|
#include "SystemZGenMCCodeEmitter.inc"
|