Skip to content

[Mips] Allow expressions in some immediate operands #127581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,11 +1311,23 @@ class MipsOperand : public MCParsedAsmOperand {
}

template <unsigned Bits> bool isSImm() const {
return isConstantImm() ? isInt<Bits>(getConstantImm()) : isImm();
if (!isImm())
return false;
int64_t Res;
if (getImm()->evaluateAsAbsolute(Res))
return isInt<Bits>(Res);
// Allow conservatively if not a parse-time constant.
return true;
}

template <unsigned Bits> bool isUImm() const {
return isConstantImm() ? isUInt<Bits>(getConstantImm()) : isImm();
if (!isImm())
return false;
int64_t Res;
if (getImm()->evaluateAsAbsolute(Res))
return isUInt<Bits>(Res);
// Allow conservatively if not a parse-time constant.
return true;
}

template <unsigned Bits> bool isAnyImm() const {
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
case Mips::fixup_MIPS_PCLO16:
Value &= 0xffff;
break;
case Mips::fixup_Mips_AnyImm16:
if (!isInt<16>(Value) && !isUInt<16>(Value))
Ctx.reportError(Fixup.getLoc(),
"fixup value out of range [-32768, 65535]");
break;
case FK_DTPRel_4:
case FK_DTPRel_8:
case FK_TPRel_4:
Expand Down Expand Up @@ -362,6 +367,7 @@ getFixupKindInfo(MCFixupKind Kind) const {
{ "fixup_Mips_26", 0, 26, 0 },
{ "fixup_Mips_HI16", 0, 16, 0 },
{ "fixup_Mips_LO16", 0, 16, 0 },
{ "fixup_Mips_AnyImm16", 0, 16, 0 },
{ "fixup_Mips_GPREL16", 0, 16, 0 },
{ "fixup_Mips_LITERAL", 0, 16, 0 },
{ "fixup_Mips_GOT", 0, 16, 0 },
Expand Down Expand Up @@ -443,6 +449,7 @@ getFixupKindInfo(MCFixupKind Kind) const {
{ "fixup_Mips_26", 6, 26, 0 },
{ "fixup_Mips_HI16", 16, 16, 0 },
{ "fixup_Mips_LO16", 16, 16, 0 },
{ "fixup_Mips_AnyImm16", 16, 16, 0 },
{ "fixup_Mips_GPREL16", 16, 16, 0 },
{ "fixup_Mips_LITERAL", 16, 16, 0 },
{ "fixup_Mips_GOT", 16, 16, 0 },
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsBaseInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ enum OperandType : unsigned {
OPERAND_MEM_SIMM9 = OPERAND_FIRST_MIPS_MEM_IMM,
OPERAND_LAST_MIPS_MEM_IMM = OPERAND_MEM_SIMM9
};

static inline unsigned getFormat(uint64_t TSFlags) {
return TSFlags & FormMask;
}
} // namespace MipsII

inline static MCRegister getMSARegFromFReg(MCRegister Reg) {
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
return ELF::R_MICROMIPS_JALR;
}

llvm_unreachable("invalid fixup kind!");
Ctx.reportError(Fixup.getLoc(), "unsupported relocation type");
return ELF::R_MIPS_NONE;
}

/// Sort relocation table entries by offset except where another order is
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsFixupKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ enum Fixups {
// Pure lower 16 bit fixup resulting in - R_MIPS_LO16.
fixup_Mips_LO16,

// 16-bit fixup that must be resolved.
fixup_Mips_AnyImm16,

// 16 bit fixup for GP offest resulting in - R_MIPS_GPREL16.
fixup_Mips_GPREL16,

Expand Down
44 changes: 24 additions & 20 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "MipsMCCodeEmitter.h"
#include "MCTargetDesc/MipsBaseInfo.h"
#include "MCTargetDesc/MipsFixupKinds.h"
#include "MCTargetDesc/MipsMCExpr.h"
#include "MCTargetDesc/MipsMCTargetDesc.h"
Expand Down Expand Up @@ -578,23 +579,7 @@ getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
unsigned MipsMCCodeEmitter::
getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
int64_t Res;

if (Expr->evaluateAsAbsolute(Res))
return Res;

MCExpr::ExprKind Kind = Expr->getKind();
if (Kind == MCExpr::Constant) {
return cast<MCConstantExpr>(Expr)->getValue();
}

if (Kind == MCExpr::Binary) {
unsigned Res =
getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI);
Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI);
return Res;
}

if (Kind == MCExpr::Target) {
const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr);

Expand Down Expand Up @@ -712,8 +697,7 @@ getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups,
return 0;
}

if (Kind == MCExpr::SymbolRef)
Ctx.reportError(Expr->getLoc(), "expected an immediate");
Ctx.reportError(Expr->getLoc(), "expected an immediate");
return 0;
}

Expand All @@ -732,9 +716,29 @@ getMachineOpValue(const MCInst &MI, const MCOperand &MO,
} else if (MO.isDFPImm()) {
return static_cast<unsigned>(bit_cast<double>(MO.getDFPImm()));
}
// MO must be an Expr.
// TODO: Set EncoderMethod to "getImmOpValue" for imm Operand so that
// getMachineOpValue will not be called for isExpr code paths.
assert(MO.isExpr());
return getExprOpValue(MO.getExpr(),Fixups, STI);
return getImmOpValue(MI, MO, Fixups, STI);
}

unsigned MipsMCCodeEmitter::getImmOpValue(const MCInst &MI, const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const {
if (MO.isImm())
return MO.getImm();
assert(MO.isExpr() && "getImmOpValue expects only expressions or immediates");
const MCExpr *Expr = MO.getExpr();
int64_t Res;
if (Expr->evaluateAsAbsolute(Res))
return Res;
unsigned MIFrm = MipsII::getFormat(MCII.get(MI.getOpcode()).TSFlags);
if (!isa<MCTargetExpr>(Expr) && MIFrm == MipsII::FrmI) {
Fixups.push_back(MCFixup::create(
0, Expr, MCFixupKind(Mips::fixup_Mips_AnyImm16), Expr->getLoc()));
return 0;
}
return getExprOpValue(Expr, Fixups, STI);
}

/// Return binary encoding of memory related operand.
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/Target/Mips/MCTargetDesc/MipsMCCodeEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class MipsMCCodeEmitter : public MCCodeEmitter {
unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;
unsigned getImmOpValue(const MCInst &MI, const MCOperand &MO,
SmallVectorImpl<MCFixup> &Fixups,
const MCSubtargetInfo &STI) const;

unsigned getMSAMemEncoding(const MCInst &MI, unsigned OpNo,
SmallVectorImpl<MCFixup> &Fixups,
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/MC/Mips/fixup-expr.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# RUN: llvm-mc -filetype=obj -triple=mips64 %s -o %t.be
# RUN: llvm-objdump -d %t.be | FileCheck %s
# RUN: llvm-mc -filetype=obj -triple=mips64el %s -o %t.le
# RUN: llvm-objdump -d %t.le | FileCheck %s

# RUN: not llvm-mc -filetype=obj -triple=mips64el --defsym ERR=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=ERR

# CHECK: addiu $4, $5, -0x8000
# CHECK-NEXT: addiu $4, $5, -0x1
# CHECK-NEXT: addiu $4, $5, -0x8000
# CHECK-NEXT: addiu $4, $5, 0x7fff
# CHECK-NEXT: addiu $4, $5, -0x1
addiu $4, $5, v_32769+1
addiu $4, $5, v65535
addiu $4, $5, .L0-.L1
addiu $4, $5, .L2-.L1
addiu $4, $5, .L2-.L0+0

# CHECK: andi $4, $5, 0xffff
# CHECK: slti $4, $5, -0x1
andi $4, $5, v65535 # uimm16
slti $4, $5, v65535 # simm16

.ifdef ERR
# ERR: :[[#@LINE+1]]:15: error: fixup value out of range [-32768, 65535]
addiu $4, $5, v_32769
# ERR: :[[#@LINE+1]]:21: error: fixup value out of range [-32768, 65535]
addiu $4, $5, v65535+1

# ERR: [[#@LINE+1]]:18: error: fixup value out of range [-32768, 65535]
addiu $4, $5, .L2-.L0+1
.endif

v_32769 = -32769
v65535 = 65535

.section .rodata,"a"
.L0:
.space 32768
.L1:
.space 32767
.L2:
10 changes: 4 additions & 6 deletions llvm/test/MC/Mips/imm-operand-err.s
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
## Print an error if a non-immediate operand is used while an immediate is expected
# RUN: not llvm-mc -filetype=obj -triple=mips -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llvm-mc -filetype=obj -triple=mips64 -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llvm-mc -filetype=obj -triple=mips -o /dev/null %s 2>&1 | FileCheck %s --implicit-check-not=error:
# RUN: not llvm-mc -filetype=obj -triple=mips64 -o /dev/null %s 2>&1 | FileCheck %s --implicit-check-not=error:

# CHECK: [[#@LINE+1]]:16: error: expected an immediate
# CHECK: [[#@LINE+1]]:16: error: unsupported relocation type
ori $4, $4, start
# CHECK: [[#@LINE+1]]:17: error: expected an immediate
ori $4, $4, (start - .)

# CHECK: [[#@LINE+1]]:18: error: expected an immediate
# CHECK: [[#@LINE+1]]:18: error: unsupported relocation type
addiu $4, $4, start
# CHECK: [[#@LINE+1]]:19: error: expected an immediate
addiu $4, $4, (start - .)

start:
Loading