[Mips] Allow expressions in some immediate operands · llvm/llvm-project@b65e094 (original) (raw)
`@@ -11,6 +11,7 @@
`
11
11
`//===----------------------------------------------------------------------===//
`
12
12
``
13
13
`#include "MipsMCCodeEmitter.h"
`
``
14
`+
#include "MCTargetDesc/MipsBaseInfo.h"
`
14
15
`#include "MCTargetDesc/MipsFixupKinds.h"
`
15
16
`#include "MCTargetDesc/MipsMCExpr.h"
`
16
17
`#include "MCTargetDesc/MipsMCTargetDesc.h"
`
`@@ -578,23 +579,7 @@ getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo,
`
578
579
`unsigned MipsMCCodeEmitter::
`
579
580
`getExprOpValue(const MCExpr *Expr, SmallVectorImpl &Fixups,
`
580
581
`const MCSubtargetInfo &STI) const {
`
581
``
`-
int64_t Res;
`
582
``
-
583
``
`-
if (Expr->evaluateAsAbsolute(Res))
`
584
``
`-
return Res;
`
585
``
-
586
582
` MCExpr::ExprKind Kind = Expr->getKind();
`
587
``
`-
if (Kind == MCExpr::Constant) {
`
588
``
`-
return cast(Expr)->getValue();
`
589
``
`-
}
`
590
``
-
591
``
`-
if (Kind == MCExpr::Binary) {
`
592
``
`-
unsigned Res =
`
593
``
`-
getExprOpValue(cast(Expr)->getLHS(), Fixups, STI);
`
594
``
`-
Res += getExprOpValue(cast(Expr)->getRHS(), Fixups, STI);
`
595
``
`-
return Res;
`
596
``
`-
}
`
597
``
-
598
583
`if (Kind == MCExpr::Target) {
`
599
584
`const MipsMCExpr *MipsExpr = cast(Expr);
`
600
585
``
`@@ -712,8 +697,7 @@ getExprOpValue(const MCExpr *Expr, SmallVectorImpl &Fixups,
`
712
697
`return 0;
`
713
698
` }
`
714
699
``
715
``
`-
if (Kind == MCExpr::SymbolRef)
`
716
``
`-
Ctx.reportError(Expr->getLoc(), "expected an immediate");
`
``
700
`+
Ctx.reportError(Expr->getLoc(), "expected an immediate");
`
717
701
`return 0;
`
718
702
`}
`
719
703
``
`@@ -732,9 +716,29 @@ getMachineOpValue(const MCInst &MI, const MCOperand &MO,
`
732
716
` } else if (MO.isDFPImm()) {
`
733
717
`return static_cast(bit_cast(MO.getDFPImm()));
`
734
718
` }
`
735
``
`-
// MO must be an Expr.
`
``
719
`+
// TODO: Set EncoderMethod to "getImmOpValue" for imm Operand so that
`
``
720
`+
// getMachineOpValue will not be called for isExpr code paths.
`
736
721
`assert(MO.isExpr());
`
737
``
`-
return getExprOpValue(MO.getExpr(),Fixups, STI);
`
``
722
`+
return getImmOpValue(MI, MO, Fixups, STI);
`
``
723
`+
}
`
``
724
+
``
725
`+
unsigned MipsMCCodeEmitter::getImmOpValue(const MCInst &MI, const MCOperand &MO,
`
``
726
`+
SmallVectorImpl &Fixups,
`
``
727
`+
const MCSubtargetInfo &STI) const {
`
``
728
`+
if (MO.isImm())
`
``
729
`+
return MO.getImm();
`
``
730
`+
assert(MO.isExpr() && "getImmOpValue expects only expressions or immediates");
`
``
731
`+
const MCExpr *Expr = MO.getExpr();
`
``
732
`+
int64_t Res;
`
``
733
`+
if (Expr->evaluateAsAbsolute(Res))
`
``
734
`+
return Res;
`
``
735
`+
unsigned MIFrm = MipsII::getFormat(MCII.get(MI.getOpcode()).TSFlags);
`
``
736
`+
if (!isa(Expr) && MIFrm == MipsII::FrmI) {
`
``
737
`+
Fixups.push_back(MCFixup::create(
`
``
738
`+
0, Expr, MCFixupKind(Mips::fixup_Mips_AnyImm16), Expr->getLoc()));
`
``
739
`+
return 0;
`
``
740
`+
}
`
``
741
`+
return getExprOpValue(Expr, Fixups, STI);
`
738
742
`}
`
739
743
``
740
744
`/// Return binary encoding of memory related operand.
`