Skip to content

Commit 752b91b

Browse files
committed
If available, pass down the Fixup object to EvaluateAsRelocatable.
At least on PowerPC, the interpretation of certain modifiers depends on the context they appear in. llvm-svn: 215310
1 parent 13c8cf2 commit 752b91b

File tree

16 files changed

+70
-49
lines changed

16 files changed

+70
-49
lines changed

llvm/include/llvm/MC/MCExpr.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MCAsmInfo;
1919
class MCAsmLayout;
2020
class MCAssembler;
2121
class MCContext;
22+
class MCFixup;
2223
class MCSection;
2324
class MCSectionData;
2425
class MCStreamer;
@@ -54,6 +55,7 @@ class MCExpr {
5455

5556
bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
5657
const MCAsmLayout *Layout,
58+
const MCFixup *Fixup,
5759
const SectionAddrMap *Addrs, bool InSet,
5860
bool ForceVarExpansion) const;
5961

@@ -92,16 +94,19 @@ class MCExpr {
9294
///
9395
/// @param Res - The relocatable value, if evaluation succeeds.
9496
/// @param Layout - The assembler layout object to use for evaluating values.
97+
/// @param Fixup - The Fixup object if available.
9598
/// @result - True on success.
96-
bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout) const;
99+
bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
100+
const MCFixup *Fixup) const;
97101

98102
/// \brief Try to evaluate the expression to the form (a - b + constant) where
99103
/// neither a nor b are variables.
100104
///
101105
/// This is a more aggressive variant of EvaluateAsRelocatable. The intended
102106
/// use is for when relocations are not available, like the symbol value in
103107
/// the symbol table.
104-
bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout) const;
108+
bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
109+
const MCFixup *Fixup) const;
105110

106111
/// FindAssociatedSection - Find the "associated section" for this expression,
107112
/// which is currently defined as the absolute section for constants, or
@@ -524,7 +529,8 @@ class MCTargetExpr : public MCExpr {
524529

525530
virtual void PrintImpl(raw_ostream &OS) const = 0;
526531
virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
527-
const MCAsmLayout *Layout) const = 0;
532+
const MCAsmLayout *Layout,
533+
const MCFixup *Fixup) const = 0;
528534
virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
529535
virtual const MCSection *FindAssociatedSection() const = 0;
530536

llvm/lib/MC/MCAssembler.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static bool getSymbolOffsetImpl(const MCAsmLayout &Layout,
141141

142142
// If SD is a variable, evaluate it.
143143
MCValue Target;
144-
if (!S.getVariableValue()->EvaluateAsValue(Target, &Layout))
144+
if (!S.getVariableValue()->EvaluateAsValue(Target, &Layout, nullptr))
145145
report_fatal_error("unable to evaluate offset for variable '" +
146146
S.getName() + "'");
147147

@@ -187,7 +187,7 @@ const MCSymbol *MCAsmLayout::getBaseSymbol(const MCSymbol &Symbol) const {
187187

188188
const MCExpr *Expr = Symbol.getVariableValue();
189189
MCValue Value;
190-
if (!Expr->EvaluateAsValue(Value, this))
190+
if (!Expr->EvaluateAsValue(Value, this, nullptr))
191191
llvm_unreachable("Invalid Expression");
192192

193193
const MCSymbolRefExpr *RefB = Value.getSymB();
@@ -438,11 +438,12 @@ const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
438438
// a relocatable expr.
439439
// FIXME: Should this be the behavior of EvaluateAsRelocatable itself?
440440
static bool evaluate(const MCExpr &Expr, const MCAsmLayout &Layout,
441-
MCValue &Target) {
442-
if (Expr.EvaluateAsValue(Target, &Layout))
441+
const MCFixup &Fixup, MCValue &Target) {
442+
if (Expr.EvaluateAsValue(Target, &Layout, &Fixup)) {
443443
if (Target.isAbsolute())
444444
return true;
445-
return Expr.EvaluateAsRelocatable(Target, &Layout);
445+
}
446+
return Expr.EvaluateAsRelocatable(Target, &Layout, &Fixup);
446447
}
447448

448449
bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
@@ -454,7 +455,7 @@ bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
454455
// probably merge the two into a single callback that tries to evaluate a
455456
// fixup and records a relocation if one is needed.
456457
const MCExpr *Expr = Fixup.getValue();
457-
if (!evaluate(*Expr, Layout, Target))
458+
if (!evaluate(*Expr, Layout, Fixup, Target))
458459
getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
459460

460461
bool IsPCRel = Backend.getFixupKindInfo(

llvm/lib/MC/MCExpr.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
482482
// absolutize differences across sections and that is what the MachO writer
483483
// uses Addrs for.
484484
bool IsRelocatable =
485-
EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs,
486-
/*ForceVarExpansion*/ false);
485+
EvaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs,
486+
/*InSet*/ Addrs, /*ForceVarExpansion*/ false);
487487

488488
// Record the current value.
489489
Res = Value.getConstant();
@@ -632,27 +632,31 @@ static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
632632
}
633633

634634
bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
635-
const MCAsmLayout *Layout) const {
635+
const MCAsmLayout *Layout,
636+
const MCFixup *Fixup) const {
636637
MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
637-
return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
638-
/*ForceVarExpansion*/ false);
638+
return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
639+
false, /*ForceVarExpansion*/ false);
639640
}
640641

641-
bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout) const {
642+
bool MCExpr::EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout,
643+
const MCFixup *Fixup) const {
642644
MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
643-
return EvaluateAsRelocatableImpl(Res, Assembler, Layout, nullptr, false,
644-
/*ForceVarExpansion*/ true);
645+
return EvaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
646+
false, /*ForceVarExpansion*/ true);
645647
}
646648

647649
bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
648650
const MCAsmLayout *Layout,
651+
const MCFixup *Fixup,
649652
const SectionAddrMap *Addrs, bool InSet,
650653
bool ForceVarExpansion) const {
651654
++stats::MCExprEvaluate;
652655

653656
switch (getKind()) {
654657
case Target:
655-
return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
658+
return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout,
659+
Fixup);
656660

657661
case Constant:
658662
Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
@@ -666,7 +670,7 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
666670
// Evaluate recursively if this is a variable.
667671
if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
668672
if (Sym.getVariableValue()->EvaluateAsRelocatableImpl(
669-
Res, Asm, Layout, Addrs, true, ForceVarExpansion)) {
673+
Res, Asm, Layout, Fixup, Addrs, true, ForceVarExpansion)) {
670674
const MCSymbolRefExpr *A = Res.getSymA();
671675
const MCSymbolRefExpr *B = Res.getSymB();
672676

@@ -697,8 +701,9 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
697701
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
698702
MCValue Value;
699703

700-
if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs,
701-
InSet, ForceVarExpansion))
704+
if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
705+
Fixup, Addrs, InSet,
706+
ForceVarExpansion))
702707
return false;
703708

704709
switch (AUE->getOpcode()) {
@@ -731,10 +736,12 @@ bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
731736
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
732737
MCValue LHSValue, RHSValue;
733738

734-
if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout, Addrs,
735-
InSet, ForceVarExpansion) ||
736-
!ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout, Addrs,
737-
InSet, ForceVarExpansion))
739+
if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
740+
Fixup, Addrs, InSet,
741+
ForceVarExpansion) ||
742+
!ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
743+
Fixup, Addrs, InSet,
744+
ForceVarExpansion))
738745
return false;
739746

740747
// We only support a few operations on non-constant expressions, handle

llvm/lib/MC/MachObjectWriter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ uint64_t MachObjectWriter::getSymbolAddress(const MCSymbolData* SD,
8484

8585

8686
MCValue Target;
87-
if (!S.getVariableValue()->EvaluateAsRelocatable(Target, &Layout))
87+
if (!S.getVariableValue()->EvaluateAsRelocatable(Target, &Layout, nullptr))
8888
report_fatal_error("unable to evaluate offset for variable '" +
8989
S.getName() + "'");
9090

@@ -664,7 +664,7 @@ void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm,
664664
// and neither symbol is external, mark the variable as absolute.
665665
const MCExpr *Expr = SD.getSymbol().getVariableValue();
666666
MCValue Value;
667-
if (Expr->EvaluateAsRelocatable(Value, &Layout)) {
667+
if (Expr->EvaluateAsRelocatable(Value, &Layout, nullptr)) {
668668
if (Value.getSymA() && Value.getSymB())
669669
const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute();
670670
}

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ const MCSection *AArch64MCExpr::FindAssociatedSection() const {
9090
}
9191

9292
bool AArch64MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
93-
const MCAsmLayout *Layout) const {
94-
if (!getSubExpr()->EvaluateAsRelocatable(Res, Layout))
93+
const MCAsmLayout *Layout,
94+
const MCFixup *Fixup) const {
95+
if (!getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup))
9596
return false;
9697

9798
Res =

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class AArch64MCExpr : public MCTargetExpr {
152152
const MCSection *FindAssociatedSection() const override;
153153

154154
bool EvaluateAsRelocatableImpl(MCValue &Res,
155-
const MCAsmLayout *Layout) const override;
155+
const MCAsmLayout *Layout,
156+
const MCFixup *Fixup) const override;
156157

157158
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override;
158159

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ void AArch64MachObjectWriter::RecordRelocation(
288288
// FIXME: Will the Target we already have ever have any data in it
289289
// we need to preserve and merge with the new Target? How about
290290
// the FixedValue?
291-
if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout))
291+
if (!Symbol->getVariableValue()->EvaluateAsRelocatable(Target, &Layout,
292+
&Fixup))
292293
Asm.getContext().FatalError(Fixup.getLoc(),
293294
"unable to resolve variable '" +
294295
Symbol->getName() + "'");

llvm/lib/Target/ARM/MCTargetDesc/ARMMCExpr.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ void ARMMCExpr::PrintImpl(raw_ostream &OS) const {
3535
OS << ')';
3636
}
3737

38-
bool
39-
ARMMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
40-
const MCAsmLayout *Layout) const {
41-
return false;
42-
}
43-
4438
void ARMMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
4539
Streamer.visitUsedExpr(*getSubExpr());
4640
}

llvm/lib/Target/ARM/MCTargetDesc/ARMMCExpr.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ class ARMMCExpr : public MCTargetExpr {
5858

5959
void PrintImpl(raw_ostream &OS) const override;
6060
bool EvaluateAsRelocatableImpl(MCValue &Res,
61-
const MCAsmLayout *Layout) const override;
62-
void visitUsedExpr(MCStreamer &Streamer) const override;
61+
const MCAsmLayout *Layout,
62+
const MCFixup *Fixup) const override {
63+
return false;
64+
}
65+
void visitUsedExpr(MCStreamer &Streamer) const override;
6366
const MCSection *FindAssociatedSection() const override {
6467
return getSubExpr()->FindAssociatedSection();
6568
}

llvm/lib/Target/Mips/MCTargetDesc/MipsMCExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ void MipsMCExpr::PrintImpl(raw_ostream &OS) const {
8080

8181
bool
8282
MipsMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
83-
const MCAsmLayout *Layout) const {
84-
return getSubExpr()->EvaluateAsRelocatable(Res, Layout);
83+
const MCAsmLayout *Layout,
84+
const MCFixup *Fixup) const {
85+
return getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup);
8586
}
8687

8788
void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {

llvm/lib/Target/Mips/MCTargetDesc/MipsMCExpr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class MipsMCExpr : public MCTargetExpr {
4848

4949
void PrintImpl(raw_ostream &OS) const override;
5050
bool EvaluateAsRelocatableImpl(MCValue &Res,
51-
const MCAsmLayout *Layout) const override;
51+
const MCAsmLayout *Layout,
52+
const MCFixup *Fixup) const override;
5253
void visitUsedExpr(MCStreamer &Streamer) const override;
5354
const MCSection *FindAssociatedSection() const override {
5455
return getSubExpr()->FindAssociatedSection();

llvm/lib/Target/NVPTX/NVPTXMCExpr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class NVPTXFloatMCExpr : public MCTargetExpr {
6363

6464
void PrintImpl(raw_ostream &OS) const override;
6565
bool EvaluateAsRelocatableImpl(MCValue &Res,
66-
const MCAsmLayout *Layout) const override {
66+
const MCAsmLayout *Layout,
67+
const MCFixup *Fixup) const override {
6768
return false;
6869
}
6970
void visitUsedExpr(MCStreamer &Streamer) const override {};

llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ void PPCMCExpr::PrintImpl(raw_ostream &OS) const {
5353

5454
bool
5555
PPCMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
56-
const MCAsmLayout *Layout) const {
56+
const MCAsmLayout *Layout,
57+
const MCFixup *Fixup) const {
5758
MCValue Value;
5859

59-
if (!getSubExpr()->EvaluateAsRelocatable(Value, Layout))
60+
if (!getSubExpr()->EvaluateAsRelocatable(Value, Layout, Fixup))
6061
return false;
6162

6263
if (Value.isAbsolute()) {

llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCExpr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class PPCMCExpr : public MCTargetExpr {
7878

7979
void PrintImpl(raw_ostream &OS) const override;
8080
bool EvaluateAsRelocatableImpl(MCValue &Res,
81-
const MCAsmLayout *Layout) const override;
81+
const MCAsmLayout *Layout,
82+
const MCFixup *Fixup) const override;
8283
void visitUsedExpr(MCStreamer &Streamer) const override;
8384
const MCSection *FindAssociatedSection() const override {
8485
return getSubExpr()->FindAssociatedSection();

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
161161

162162
bool
163163
SparcMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
164-
const MCAsmLayout *Layout) const {
165-
return getSubExpr()->EvaluateAsRelocatable(Res, Layout);
164+
const MCAsmLayout *Layout,
165+
const MCFixup *Fixup) const {
166+
return getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup);
166167
}
167168

168169
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class SparcMCExpr : public MCTargetExpr {
8787
/// @}
8888
void PrintImpl(raw_ostream &OS) const override;
8989
bool EvaluateAsRelocatableImpl(MCValue &Res,
90-
const MCAsmLayout *Layout) const override;
90+
const MCAsmLayout *Layout,
91+
const MCFixup *Fixup) const override;
9192
void visitUsedExpr(MCStreamer &Streamer) const override;
9293
const MCSection *FindAssociatedSection() const override {
9394
return getSubExpr()->FindAssociatedSection();

0 commit comments

Comments
 (0)