mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
[RISCV][MC] Modify evaluateConstantImm interface to allow reuse from addExpr
This is a trivial refactoring that I'm committing now as it makes a patch I'm about to post for review easier to follow. There is some overlap between evaluateConstantImm and addExpr in RISCVAsmParser. This patch allows evaluateConstantImm to be reused from addExpr to remove this overlap. The benefit will be greater when a future patch adds extra code to allows immediates to be evaluated from constant symbols (e.g. `.equ CONST, 0x1234`). No functional change intended. llvm-svn: 342641
This commit is contained in:
parent
0df44de1ff
commit
e355bee0d9
@ -189,18 +189,20 @@ public:
|
|||||||
bool isImm() const override { return Kind == Immediate; }
|
bool isImm() const override { return Kind == Immediate; }
|
||||||
bool isMem() const override { return false; }
|
bool isMem() const override { return false; }
|
||||||
|
|
||||||
bool evaluateConstantImm(int64_t &Imm, RISCVMCExpr::VariantKind &VK) const {
|
static bool evaluateConstantImm(const MCExpr *Expr, int64_t &Imm,
|
||||||
const MCExpr *Val = getImm();
|
RISCVMCExpr::VariantKind &VK) {
|
||||||
bool Ret = false;
|
if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
|
||||||
if (auto *RE = dyn_cast<RISCVMCExpr>(Val)) {
|
|
||||||
Ret = RE->evaluateAsConstant(Imm);
|
|
||||||
VK = RE->getKind();
|
VK = RE->getKind();
|
||||||
} else if (auto CE = dyn_cast<MCConstantExpr>(Val)) {
|
return RE->evaluateAsConstant(Imm);
|
||||||
Ret = true;
|
}
|
||||||
|
|
||||||
|
if (auto CE = dyn_cast<MCConstantExpr>(Expr)) {
|
||||||
VK = RISCVMCExpr::VK_RISCV_None;
|
VK = RISCVMCExpr::VK_RISCV_None;
|
||||||
Imm = CE->getValue();
|
Imm = CE->getValue();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return Ret;
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// True if operand is a symbol with no modifiers, or a constant with no
|
// True if operand is a symbol with no modifiers, or a constant with no
|
||||||
@ -210,7 +212,7 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
bool IsValid;
|
bool IsValid;
|
||||||
if (!IsConstantImm)
|
if (!IsConstantImm)
|
||||||
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
||||||
@ -225,7 +227,7 @@ public:
|
|||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
// Must be of 'immediate' type but not a constant.
|
// Must be of 'immediate' type but not a constant.
|
||||||
if (!isImm() || evaluateConstantImm(Imm, VK))
|
if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
|
||||||
return false;
|
return false;
|
||||||
return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
|
return RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
@ -275,7 +277,7 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
// Given only Imm, ensuring that the actually specified constant is either
|
// Given only Imm, ensuring that the actually specified constant is either
|
||||||
// a signed or unsigned 64-bit number is unfortunately impossible.
|
// a signed or unsigned 64-bit number is unfortunately impossible.
|
||||||
bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
|
bool IsInRange = isRV64() ? true : isInt<32>(Imm) || isUInt<32>(Imm);
|
||||||
@ -287,7 +289,8 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
|
if (!evaluateConstantImm(getImm(), Imm, VK) ||
|
||||||
|
VK != RISCVMCExpr::VK_RISCV_None)
|
||||||
return false;
|
return false;
|
||||||
return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
|
return (isRV64() && isUInt<6>(Imm)) || isUInt<5>(Imm);
|
||||||
}
|
}
|
||||||
@ -297,7 +300,8 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
if (!evaluateConstantImm(Imm, VK) || VK != RISCVMCExpr::VK_RISCV_None)
|
if (!evaluateConstantImm(getImm(), Imm, VK) ||
|
||||||
|
VK != RISCVMCExpr::VK_RISCV_None)
|
||||||
return false;
|
return false;
|
||||||
if (Imm == 0)
|
if (Imm == 0)
|
||||||
return false;
|
return false;
|
||||||
@ -309,7 +313,7 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
|
return IsConstantImm && isUInt<5>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,7 +322,7 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
|
return IsConstantImm && isUInt<5>(Imm) && (Imm != 0) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -328,7 +332,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isInt<6>(Imm) &&
|
return IsConstantImm && isInt<6>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -338,7 +342,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
|
return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -348,7 +352,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && (Imm != 0) &&
|
return IsConstantImm && (Imm != 0) &&
|
||||||
(isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
|
(isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
@ -359,7 +363,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
|
return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -369,7 +373,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
|
return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -379,7 +383,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
|
return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -391,7 +395,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
|
return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -401,7 +405,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
|
return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -412,7 +416,7 @@ public:
|
|||||||
bool IsValid;
|
bool IsValid;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
if (!IsConstantImm)
|
if (!IsConstantImm)
|
||||||
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
||||||
else
|
else
|
||||||
@ -429,7 +433,7 @@ public:
|
|||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
|
return IsConstantImm && isUInt<12>(Imm) && VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -440,7 +444,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
int64_t Imm;
|
int64_t Imm;
|
||||||
RISCVMCExpr::VariantKind VK;
|
RISCVMCExpr::VariantKind VK;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
|
return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
|
||||||
VK == RISCVMCExpr::VK_RISCV_None;
|
VK == RISCVMCExpr::VK_RISCV_None;
|
||||||
}
|
}
|
||||||
@ -451,7 +455,7 @@ public:
|
|||||||
bool IsValid;
|
bool IsValid;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
if (!IsConstantImm) {
|
if (!IsConstantImm) {
|
||||||
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
||||||
return IsValid && VK == RISCVMCExpr::VK_RISCV_HI;
|
return IsValid && VK == RISCVMCExpr::VK_RISCV_HI;
|
||||||
@ -467,7 +471,7 @@ public:
|
|||||||
bool IsValid;
|
bool IsValid;
|
||||||
if (!isImm())
|
if (!isImm())
|
||||||
return false;
|
return false;
|
||||||
bool IsConstantImm = evaluateConstantImm(Imm, VK);
|
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
|
||||||
if (!IsConstantImm) {
|
if (!IsConstantImm) {
|
||||||
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
IsValid = RISCVAsmParser::classifySymbolRef(getImm(), VK, Imm);
|
||||||
return IsValid && VK == RISCVMCExpr::VK_RISCV_PCREL_HI;
|
return IsValid && VK == RISCVMCExpr::VK_RISCV_PCREL_HI;
|
||||||
@ -549,13 +553,8 @@ public:
|
|||||||
void addExpr(MCInst &Inst, const MCExpr *Expr) const {
|
void addExpr(MCInst &Inst, const MCExpr *Expr) const {
|
||||||
assert(Expr && "Expr shouldn't be null!");
|
assert(Expr && "Expr shouldn't be null!");
|
||||||
int64_t Imm = 0;
|
int64_t Imm = 0;
|
||||||
bool IsConstant = false;
|
RISCVMCExpr::VariantKind VK;
|
||||||
if (auto *RE = dyn_cast<RISCVMCExpr>(Expr)) {
|
bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
|
||||||
IsConstant = RE->evaluateAsConstant(Imm);
|
|
||||||
} else if (auto *CE = dyn_cast<MCConstantExpr>(Expr)) {
|
|
||||||
IsConstant = true;
|
|
||||||
Imm = CE->getValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (IsConstant)
|
if (IsConstant)
|
||||||
Inst.addOperand(MCOperand::createImm(Imm));
|
Inst.addOperand(MCOperand::createImm(Imm));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user