Skip to content

Commit 72de33a

Browse files
committed
MC: Add MCAsmInfo::evaluateAsRelocatableImpl and replace VEMCExpr with MCSpecifierExpr
Expressions with specifier can only be folded during relocation generatin. At parse time the `MCAssembler *` argument might be null, and targets should not rely on the evaluateAsRelocatable result. Therefore, we can move evaluateAsRelocatableImpl from MCSpecifierExpr to MCAsmInfo, so that targets do not need to inherit from MCSpecifierExpr.
1 parent 254a92d commit 72de33a

File tree

12 files changed

+47
-50
lines changed

12 files changed

+47
-50
lines changed

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace llvm {
2727

28+
class MCAssembler;
2829
class MCContext;
2930
class MCCFIInstruction;
3031
class MCExpr;
@@ -33,6 +34,7 @@ class MCSection;
3334
class MCStreamer;
3435
class MCSubtargetInfo;
3536
class MCSymbol;
37+
class MCValue;
3638
class raw_ostream;
3739

3840
namespace WinEH {
@@ -714,6 +716,8 @@ class LLVM_ABI MCAsmInfo {
714716

715717
void printExpr(raw_ostream &, const MCExpr &) const;
716718
virtual void printSpecifierExpr(raw_ostream &, const MCSpecifierExpr &) const;
719+
virtual bool evaluateAsRelocatableImpl(const MCSpecifierExpr &, MCValue &Res,
720+
const MCAssembler *Asm) const;
717721
};
718722

719723
} // end namespace llvm

llvm/lib/MC/MCAsmInfo.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,11 @@ void MCAsmInfo::printSpecifierExpr(raw_ostream &OS,
163163
// migrate to MCAsmInfo::printSpecifierExpr.
164164
Expr.printImpl(OS, this);
165165
}
166+
167+
bool MCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &Expr,
168+
MCValue &Res,
169+
const MCAssembler *Asm) const {
170+
// TODO: Remove after all targets that use MCSpecifierExpr migrate to
171+
// MCAsmInfo::evaluateAsRelocatableImpl.
172+
return Expr.evaluateAsRelocatableImpl(Res, Asm);
173+
}

llvm/lib/MC/MCExpr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,10 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
680680
return true;
681681
}
682682
case Specifier:
683-
return cast<MCSpecifierExpr>(this)->evaluateAsRelocatableImpl(Res, Asm);
683+
// Fold the expression during relocation generation. As parse time Asm might
684+
// be null, and targets should not rely on the folding.
685+
return Asm && Asm->getContext().getAsmInfo()->evaluateAsRelocatableImpl(
686+
cast<MCSpecifierExpr>(*this), Res, Asm);
684687
}
685688

686689
llvm_unreachable("Invalid assembly expression kind!");

llvm/lib/Target/VE/AsmParser/VEAsmParser.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class VEAsmParser : public MCTargetAsmParser {
7373
ParseStatus parseVEAsmOperand(std::unique_ptr<VEOperand> &Operand);
7474

7575
// Helper function to parse expression with a symbol.
76-
const MCExpr *extractSpecifier(const MCExpr *E, VEMCExpr::Specifier &Variant);
76+
const MCExpr *extractSpecifier(const MCExpr *E, VE::Specifier &Variant);
7777
bool parseExpression(const MCExpr *&EVal);
7878

7979
// Split the mnemonic stripping conditional code and quantifiers
@@ -1036,11 +1036,11 @@ bool VEAsmParser::parseLiteralValues(unsigned Size, SMLoc L) {
10361036
/// Extract \code @lo32/@hi32/etc \endcode specifier from expression.
10371037
/// Recursively scan the expression and check for VK_HI32/LO32/etc
10381038
/// symbol variants. If all symbols with modifier use the same
1039-
/// variant, return the corresponding VEMCExpr::Specifier,
1039+
/// variant, return the corresponding VE::Specifier,
10401040
/// and a modified expression using the default symbol variant.
10411041
/// Otherwise, return NULL.
10421042
const MCExpr *VEAsmParser::extractSpecifier(const MCExpr *E,
1043-
VEMCExpr::Specifier &Variant) {
1043+
VE::Specifier &Variant) {
10441044
MCContext &Context = getParser().getContext();
10451045
Variant = VE::S_None;
10461046

@@ -1118,7 +1118,7 @@ const MCExpr *VEAsmParser::extractSpecifier(const MCExpr *E,
11181118

11191119
case MCExpr::Binary: {
11201120
const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1121-
VEMCExpr::Specifier LHSVariant, RHSVariant;
1121+
VE::Specifier LHSVariant, RHSVariant;
11221122
const MCExpr *LHS = extractSpecifier(BE->getLHS(), LHSVariant);
11231123
const MCExpr *RHS = extractSpecifier(BE->getRHS(), RHSVariant);
11241124

@@ -1153,11 +1153,11 @@ bool VEAsmParser::parseExpression(const MCExpr *&EVal) {
11531153
if (getParser().parseExpression(EVal))
11541154
return true;
11551155

1156-
// Convert MCSymbolRefExpr with VK_* to MCExpr with VK_*.
1157-
VEMCExpr::Specifier Specifier;
1156+
// Convert MCSymbolRefExpr with specifier to MCSpecifierExpr.
1157+
VE::Specifier Specifier;
11581158
const MCExpr *E = extractSpecifier(EVal, Specifier);
11591159
if (E)
1160-
EVal = VEMCExpr::create(Specifier, E, getParser().getContext());
1160+
EVal = MCSpecifierExpr::create(E, Specifier, getParser().getContext());
11611161

11621162
return false;
11631163
}

llvm/lib/Target/VE/MCTargetDesc/VEELFObjectWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ unsigned VEELFObjectWriter::getRelocType(const MCFixup &Fixup,
5050
default:
5151
break;
5252
}
53-
if (const VEMCExpr *SExpr = dyn_cast<VEMCExpr>(Fixup.getValue())) {
53+
if (auto *SExpr = dyn_cast<MCSpecifierExpr>(Fixup.getValue())) {
5454
if (SExpr->getSpecifier() == VE::S_PC_LO32)
5555
return ELF::R_VE_PC_LO32;
5656
}

llvm/lib/Target/VE/MCTargetDesc/VEMCAsmInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "VEMCExpr.h"
1515
#include "llvm/MC/MCExpr.h"
1616
#include "llvm/MC/MCStreamer.h"
17+
#include "llvm/MC/MCValue.h"
1718
#include "llvm/TargetParser/Triple.h"
1819

1920
using namespace llvm;
@@ -64,3 +65,12 @@ void VEELFMCAsmInfo::printSpecifierExpr(raw_ostream &OS,
6465
if (specifier && specifier != VE::S_REFLONG)
6566
OS << '@' << getSpecifierName(specifier);
6667
}
68+
69+
bool VEELFMCAsmInfo::evaluateAsRelocatableImpl(const MCSpecifierExpr &Expr,
70+
MCValue &Res,
71+
const MCAssembler *Asm) const {
72+
if (!Expr.getSubExpr()->evaluateAsRelocatable(Res, Asm))
73+
return false;
74+
Res.setSpecifier(Expr.getSpecifier());
75+
return true;
76+
}

llvm/lib/Target/VE/MCTargetDesc/VEMCAsmInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class VEELFMCAsmInfo : public MCAsmInfoELF {
2626
explicit VEELFMCAsmInfo(const Triple &TheTriple);
2727
void printSpecifierExpr(raw_ostream &OS,
2828
const MCSpecifierExpr &Expr) const override;
29+
bool evaluateAsRelocatableImpl(const MCSpecifierExpr &Expr, MCValue &Res,
30+
const MCAssembler *Asm) const override;
2931
};
3032

3133
} // namespace llvm

llvm/lib/Target/VE/MCTargetDesc/VEMCCodeEmitter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ unsigned VEMCCodeEmitter::getMachineOpValue(const MCInst &MI,
9898
assert(MO.isExpr());
9999

100100
const MCExpr *Expr = MO.getExpr();
101-
if (const VEMCExpr *SExpr = dyn_cast<VEMCExpr>(Expr)) {
101+
if (const auto *SExpr = dyn_cast<MCSpecifierExpr>(Expr)) {
102102
auto Kind = VE::getFixupKind(SExpr->getSpecifier());
103103
Fixups.push_back(MCFixup::create(0, Expr, Kind));
104104
return 0;

llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ using namespace llvm;
2222

2323
#define DEBUG_TYPE "vemcexpr"
2424

25-
const VEMCExpr *VEMCExpr::create(Specifier S, const MCExpr *Expr,
26-
MCContext &Ctx) {
27-
return new (Ctx) VEMCExpr(Expr, S);
28-
}
29-
3025
VE::Fixups VE::getFixupKind(uint8_t S) {
3126
switch (S) {
3227
default:
@@ -63,11 +58,3 @@ VE::Fixups VE::getFixupKind(uint8_t S) {
6358
return VE::fixup_ve_tpoff_lo32;
6459
}
6560
}
66-
67-
bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
68-
const MCAssembler *Asm) const {
69-
if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
70-
return false;
71-
Res.setSpecifier(specifier);
72-
return true;
73-
}

llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,6 @@
2020
namespace llvm {
2121

2222
class StringRef;
23-
class VEMCExpr : public MCSpecifierExpr {
24-
public:
25-
using Specifier = uint8_t;
26-
27-
private:
28-
explicit VEMCExpr(const MCExpr *Expr, Specifier S)
29-
: MCSpecifierExpr(Expr, S) {}
30-
31-
public:
32-
static const VEMCExpr *create(Specifier Kind, const MCExpr *Expr,
33-
MCContext &Ctx);
34-
35-
bool evaluateAsRelocatableImpl(MCValue &Res,
36-
const MCAssembler *Asm) const override;
37-
};
3823

3924
namespace VE {
4025
enum Specifier {

llvm/lib/Target/VE/VEAsmPrinter.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,17 @@ class VEAsmPrinter : public AsmPrinter {
6767
};
6868
} // end of anonymous namespace
6969

70-
static MCOperand createVEMCOperand(VEMCExpr::Specifier Kind, MCSymbol *Sym,
70+
static MCOperand createVEMCOperand(VE::Specifier Kind, MCSymbol *Sym,
7171
MCContext &OutContext) {
7272
const MCSymbolRefExpr *MCSym = MCSymbolRefExpr::create(Sym, OutContext);
73-
const VEMCExpr *expr = VEMCExpr::create(Kind, MCSym, OutContext);
74-
return MCOperand::createExpr(expr);
73+
return MCOperand::createExpr(
74+
MCSpecifierExpr::create(MCSym, Kind, OutContext));
7575
}
7676

77-
static MCOperand createGOTRelExprOp(VEMCExpr::Specifier Kind,
78-
MCSymbol *GOTLabel, MCContext &OutContext) {
77+
static MCOperand createGOTRelExprOp(VE::Specifier Kind, MCSymbol *GOTLabel,
78+
MCContext &OutContext) {
7979
const MCSymbolRefExpr *GOT = MCSymbolRefExpr::create(GOTLabel, OutContext);
80-
const VEMCExpr *expr = VEMCExpr::create(Kind, GOT, OutContext);
81-
return MCOperand::createExpr(expr);
80+
return MCOperand::createExpr(MCSpecifierExpr::create(GOT, Kind, OutContext));
8281
}
8382

8483
static void emitSIC(MCStreamer &OutStreamer, MCOperand &RD,
@@ -166,9 +165,8 @@ static void emitANDrm(MCStreamer &OutStreamer, MCOperand &RS1, MCOperand &Imm,
166165
}
167166

168167
static void emitHiLo(MCStreamer &OutStreamer, MCSymbol *GOTSym,
169-
VEMCExpr::Specifier HiKind, VEMCExpr::Specifier LoKind,
170-
MCOperand &RD, MCContext &OutContext,
171-
const MCSubtargetInfo &STI) {
168+
VE::Specifier HiKind, VE::Specifier LoKind, MCOperand &RD,
169+
MCContext &OutContext, const MCSubtargetInfo &STI) {
172170

173171
MCOperand hi = createVEMCOperand(HiKind, GOTSym, OutContext);
174172
MCOperand lo = createVEMCOperand(LoKind, GOTSym, OutContext);

llvm/lib/Target/VE/VEMCInstLower.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ using namespace llvm;
2828
static MCOperand LowerSymbolOperand(const MachineInstr *MI,
2929
const MachineOperand &MO,
3030
const MCSymbol *Symbol, AsmPrinter &AP) {
31-
VEMCExpr::Specifier Kind = (VEMCExpr::Specifier)MO.getTargetFlags();
31+
auto Kind = (VE::Specifier)MO.getTargetFlags();
3232

3333
const MCExpr *Expr = MCSymbolRefExpr::create(Symbol, AP.OutContext);
3434
// Add offset iff MO is not jump table info or machine basic block.
3535
if (!MO.isJTI() && !MO.isMBB() && MO.getOffset())
3636
Expr = MCBinaryExpr::createAdd(
3737
Expr, MCConstantExpr::create(MO.getOffset(), AP.OutContext),
3838
AP.OutContext);
39-
Expr = VEMCExpr::create(Kind, Expr, AP.OutContext);
39+
Expr = MCSpecifierExpr::create(Expr, Kind, AP.OutContext);
4040
return MCOperand::createExpr(Expr);
4141
}
4242

0 commit comments

Comments
 (0)