Skip to content

Commit 003fa77

Browse files
committed
Sparc: Remove VK_LO/VK_HI and replace enum Specifier with uint16_t
1 parent 1fc8b1b commit 003fa77

File tree

5 files changed

+91
-117
lines changed

5 files changed

+91
-117
lines changed

llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ class SparcAsmParser : public MCTargetAsmParser {
109109
ParseStatus parseExpression(int64_t &Val);
110110

111111
// Helper function for dealing with %lo / %hi in PIC mode.
112-
const SparcMCExpr *adjustPICRelocation(SparcMCExpr::Specifier VK,
113-
const MCExpr *subExpr);
112+
const SparcMCExpr *adjustPICRelocation(uint16_t VK, const MCExpr *subExpr);
114113

115114
// Helper function to see if current token can start an expression.
116115
bool isPossibleExpression(const AsmToken &Token);
@@ -701,7 +700,7 @@ bool SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc,
701700
// In either case, start with the 'sethi'.
702701
if (!IsEffectivelyImm13) {
703702
MCInst TmpInst;
704-
const MCExpr *Expr = adjustPICRelocation(SparcMCExpr::VK_HI, ValExpr);
703+
const MCExpr *Expr = adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr);
705704
TmpInst.setLoc(IDLoc);
706705
TmpInst.setOpcode(SP::SETHIi);
707706
TmpInst.addOperand(MCRegOp);
@@ -726,7 +725,7 @@ bool SparcAsmParser::expandSET(MCInst &Inst, SMLoc IDLoc,
726725
if (IsEffectivelyImm13)
727726
Expr = ValExpr;
728727
else
729-
Expr = adjustPICRelocation(SparcMCExpr::VK_LO, ValExpr);
728+
Expr = adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr);
730729
TmpInst.setLoc(IDLoc);
731730
TmpInst.setOpcode(SP::ORri);
732731
TmpInst.addOperand(MCRegOp);
@@ -766,15 +765,15 @@ bool SparcAsmParser::expandSETSW(MCInst &Inst, SMLoc IDLoc,
766765
Instructions.push_back(
767766
MCInstBuilder(SP::SETHIi)
768767
.addReg(MCRegOp.getReg())
769-
.addExpr(adjustPICRelocation(SparcMCExpr::VK_HI, ValExpr)));
768+
.addExpr(adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr)));
770769

771770
PrevReg = MCRegOp;
772771
}
773772

774773
// If the immediate has the lower bits set or is small, we need to emit an or.
775774
if (!NoLowBitsImm || IsSmallImm) {
776775
const MCExpr *Expr =
777-
IsSmallImm ? ValExpr : adjustPICRelocation(SparcMCExpr::VK_LO, ValExpr);
776+
IsSmallImm ? ValExpr : adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr);
778777

779778
// or rd, %lo(val), rd
780779
Instructions.push_back(MCInstBuilder(SP::ORri)
@@ -830,13 +829,13 @@ bool SparcAsmParser::expandSETX(MCInst &Inst, SMLoc IDLoc,
830829
Instructions.push_back(
831830
MCInstBuilder(SP::SETHIi)
832831
.addReg(MCRegOp.getReg())
833-
.addExpr(adjustPICRelocation(SparcMCExpr::VK_HI, ValExpr)));
832+
.addExpr(adjustPICRelocation(ELF::R_SPARC_HI22, ValExpr)));
834833
// or rd, %lo(val), rd
835834
Instructions.push_back(
836835
MCInstBuilder(SP::ORri)
837836
.addReg(MCRegOp.getReg())
838837
.addReg(MCRegOp.getReg())
839-
.addExpr(adjustPICRelocation(SparcMCExpr::VK_LO, ValExpr)));
838+
.addExpr(adjustPICRelocation(ELF::R_SPARC_LO10, ValExpr)));
840839

841840
// Small positive immediates can be expressed as a single `sethi`+`or`
842841
// combination, so we can just return here.
@@ -1166,11 +1165,11 @@ ParseStatus SparcAsmParser::parseTailRelocSym(OperandVector &Operands) {
11661165
return Error(getLoc(), "expected valid identifier for operand modifier");
11671166

11681167
StringRef Name = getParser().getTok().getIdentifier();
1169-
SparcMCExpr::Specifier VK = SparcMCExpr::parseSpecifier(Name);
1170-
if (VK == SparcMCExpr::VK_None)
1168+
uint16_t RelType = SparcMCExpr::parseSpecifier(Name);
1169+
if (RelType == 0)
11711170
return Error(getLoc(), "invalid relocation specifier");
11721171

1173-
if (!MatchesKind(VK)) {
1172+
if (!MatchesKind(RelType)) {
11741173
// Did not match the specified set of relocation types, put '%' back.
11751174
getLexer().UnLex(Tok);
11761175
return ParseStatus::NoMatch;
@@ -1185,7 +1184,7 @@ ParseStatus SparcAsmParser::parseTailRelocSym(OperandVector &Operands) {
11851184
if (getParser().parseParenExpression(SubExpr, E))
11861185
return ParseStatus::Failure;
11871186

1188-
const MCExpr *Val = adjustPICRelocation(VK, SubExpr);
1187+
const MCExpr *Val = adjustPICRelocation(RelType, SubExpr);
11891188
Operands.push_back(SparcOperand::CreateTailRelocSym(Val, S, E));
11901189
return ParseStatus::Success;
11911190
}
@@ -1666,29 +1665,28 @@ static bool hasGOTReference(const MCExpr *Expr) {
16661665
return false;
16671666
}
16681667

1669-
const SparcMCExpr *
1670-
SparcAsmParser::adjustPICRelocation(SparcMCExpr::Specifier VK,
1671-
const MCExpr *subExpr) {
1668+
const SparcMCExpr *SparcAsmParser::adjustPICRelocation(uint16_t RelType,
1669+
const MCExpr *subExpr) {
16721670
// When in PIC mode, "%lo(...)" and "%hi(...)" behave differently.
16731671
// If the expression refers contains _GLOBAL_OFFSET_TABLE, it is
16741672
// actually a %pc10 or %pc22 relocation. Otherwise, they are interpreted
16751673
// as %got10 or %got22 relocation.
16761674

16771675
if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1678-
switch(VK) {
1676+
switch (RelType) {
16791677
default: break;
1680-
case SparcMCExpr::VK_LO:
1681-
VK = SparcMCExpr::Specifier(
1682-
hasGOTReference(subExpr) ? ELF::R_SPARC_PC10 : ELF::R_SPARC_GOT10);
1678+
case ELF::R_SPARC_LO10:
1679+
RelType =
1680+
hasGOTReference(subExpr) ? ELF::R_SPARC_PC10 : ELF::R_SPARC_GOT10;
16831681
break;
1684-
case SparcMCExpr::VK_HI:
1685-
VK = SparcMCExpr::Specifier(
1686-
hasGOTReference(subExpr) ? ELF::R_SPARC_PC22 : ELF::R_SPARC_GOT22);
1682+
case ELF::R_SPARC_HI22:
1683+
RelType =
1684+
hasGOTReference(subExpr) ? ELF::R_SPARC_PC22 : ELF::R_SPARC_GOT22;
16871685
break;
16881686
}
16891687
}
16901688

1691-
return SparcMCExpr::create(VK, subExpr, getContext());
1689+
return SparcMCExpr::create(RelType, subExpr, getContext());
16921690
}
16931691

16941692
bool SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
@@ -1699,9 +1697,9 @@ bool SparcAsmParser::matchSparcAsmModifiers(const MCExpr *&EVal,
16991697

17001698
StringRef name = Tok.getString();
17011699

1702-
SparcMCExpr::Specifier VK = SparcMCExpr::parseSpecifier(name);
1703-
switch (uint16_t(VK)) {
1704-
case SparcMCExpr::VK_None:
1700+
auto VK = SparcMCExpr::parseSpecifier(name);
1701+
switch (VK) {
1702+
case 0:
17051703
Error(getLoc(), "invalid relocation specifier");
17061704
return false;
17071705

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

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ using namespace llvm;
2626

2727
const SparcMCExpr *SparcMCExpr::create(uint16_t S, const MCExpr *Expr,
2828
MCContext &Ctx) {
29-
return new (Ctx) SparcMCExpr(Specifier(S), Expr);
29+
return new (Ctx) SparcMCExpr(S, Expr);
3030
}
3131

3232
void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
@@ -38,12 +38,12 @@ void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
3838
OS << ')';
3939
}
4040

41-
StringRef SparcMCExpr::getSpecifierName(SparcMCExpr::Specifier S) {
41+
StringRef SparcMCExpr::getSpecifierName(uint16_t S) {
4242
// clang-format off
4343
switch (uint16_t(S)) {
44-
case VK_None: return {};
45-
case VK_LO: return "lo";
46-
case VK_HI: return "hi";
44+
case 0: return {};
45+
case ELF::R_SPARC_LO10: return "lo";
46+
case ELF::R_SPARC_HI22: return "hi";
4747
case ELF::R_SPARC_H44: return "h44";
4848
case ELF::R_SPARC_M44: return "m44";
4949
case ELF::R_SPARC_L44: return "l44";
@@ -85,62 +85,55 @@ StringRef SparcMCExpr::getSpecifierName(SparcMCExpr::Specifier S) {
8585
llvm_unreachable("Unhandled SparcMCExpr::Specifier");
8686
}
8787

88-
SparcMCExpr::Specifier SparcMCExpr::parseSpecifier(StringRef name) {
89-
return StringSwitch<SparcMCExpr::Specifier>(name)
90-
.Case("lo", VK_LO)
91-
.Case("hi", VK_HI)
92-
.Case("h44", (SparcMCExpr::Specifier)ELF::R_SPARC_H44)
93-
.Case("m44", (SparcMCExpr::Specifier)ELF::R_SPARC_M44)
94-
.Case("l44", (SparcMCExpr::Specifier)ELF::R_SPARC_L44)
95-
.Case("hh", (SparcMCExpr::Specifier)ELF::R_SPARC_HH22)
88+
uint16_t SparcMCExpr::parseSpecifier(StringRef name) {
89+
return StringSwitch<uint16_t>(name)
90+
.Case("lo", ELF::R_SPARC_LO10)
91+
.Case("hi", ELF::R_SPARC_HI22)
92+
.Case("h44", ELF::R_SPARC_H44)
93+
.Case("m44", ELF::R_SPARC_M44)
94+
.Case("l44", ELF::R_SPARC_L44)
95+
.Case("hh", ELF::R_SPARC_HH22)
9696
// Nonstandard GNU extension
97-
.Case("uhi", (SparcMCExpr::Specifier)ELF::R_SPARC_HH22)
98-
.Case("hm", (SparcMCExpr::Specifier)ELF::R_SPARC_HM10)
97+
.Case("uhi", ELF::R_SPARC_HH22)
98+
.Case("hm", ELF::R_SPARC_HM10)
9999
// Nonstandard GNU extension
100-
.Case("ulo", (SparcMCExpr::Specifier)ELF::R_SPARC_HM10)
101-
.Case("lm", (SparcMCExpr::Specifier)ELF::R_SPARC_LM22)
102-
.Case("pc22", (SparcMCExpr::Specifier)ELF::R_SPARC_PC22)
103-
.Case("pc10", (SparcMCExpr::Specifier)ELF::R_SPARC_PC10)
104-
.Case("got22", (SparcMCExpr::Specifier)ELF::R_SPARC_GOT22)
105-
.Case("got10", (SparcMCExpr::Specifier)ELF::R_SPARC_GOT10)
106-
.Case("got13", (SparcMCExpr::Specifier)ELF::R_SPARC_GOT13)
107-
.Case("r_disp32", (SparcMCExpr::Specifier)ELF::R_SPARC_DISP32)
108-
.Case("tgd_hi22", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_GD_HI22)
109-
.Case("tgd_lo10", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_GD_LO10)
110-
.Case("tgd_add", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_GD_ADD)
111-
.Case("tgd_call", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_GD_CALL)
112-
.Case("tldm_hi22", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDM_HI22)
113-
.Case("tldm_lo10", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDM_LO10)
114-
.Case("tldm_add", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDM_ADD)
115-
.Case("tldm_call", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDM_CALL)
116-
.Case("tldo_hix22", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDO_HIX22)
117-
.Case("tldo_lox10", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDO_LOX10)
118-
.Case("tldo_add", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LDO_ADD)
119-
.Case("tie_hi22", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_IE_HI22)
120-
.Case("tie_lo10", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_IE_LO10)
121-
.Case("tie_ld", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_IE_LD)
122-
.Case("tie_ldx", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_IE_LDX)
123-
.Case("tie_add", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_IE_ADD)
124-
.Case("tle_hix22", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LE_HIX22)
125-
.Case("tle_lox10", (SparcMCExpr::Specifier)ELF::R_SPARC_TLS_LE_LOX10)
126-
.Case("hix", (SparcMCExpr::Specifier)ELF::R_SPARC_HIX22)
127-
.Case("lox", (SparcMCExpr::Specifier)ELF::R_SPARC_LOX10)
128-
.Case("gdop_hix22", (SparcMCExpr::Specifier)ELF::R_SPARC_GOTDATA_OP_HIX22)
129-
.Case("gdop_lox10", (SparcMCExpr::Specifier)ELF::R_SPARC_GOTDATA_OP_LOX10)
130-
.Case("gdop", (SparcMCExpr::Specifier)ELF::R_SPARC_GOTDATA_OP)
131-
.Default(VK_None);
100+
.Case("ulo", ELF::R_SPARC_HM10)
101+
.Case("lm", ELF::R_SPARC_LM22)
102+
.Case("pc22", ELF::R_SPARC_PC22)
103+
.Case("pc10", ELF::R_SPARC_PC10)
104+
.Case("got22", ELF::R_SPARC_GOT22)
105+
.Case("got10", ELF::R_SPARC_GOT10)
106+
.Case("got13", ELF::R_SPARC_GOT13)
107+
.Case("r_disp32", ELF::R_SPARC_DISP32)
108+
.Case("tgd_hi22", ELF::R_SPARC_TLS_GD_HI22)
109+
.Case("tgd_lo10", ELF::R_SPARC_TLS_GD_LO10)
110+
.Case("tgd_add", ELF::R_SPARC_TLS_GD_ADD)
111+
.Case("tgd_call", ELF::R_SPARC_TLS_GD_CALL)
112+
.Case("tldm_hi22", ELF::R_SPARC_TLS_LDM_HI22)
113+
.Case("tldm_lo10", ELF::R_SPARC_TLS_LDM_LO10)
114+
.Case("tldm_add", ELF::R_SPARC_TLS_LDM_ADD)
115+
.Case("tldm_call", ELF::R_SPARC_TLS_LDM_CALL)
116+
.Case("tldo_hix22", ELF::R_SPARC_TLS_LDO_HIX22)
117+
.Case("tldo_lox10", ELF::R_SPARC_TLS_LDO_LOX10)
118+
.Case("tldo_add", ELF::R_SPARC_TLS_LDO_ADD)
119+
.Case("tie_hi22", ELF::R_SPARC_TLS_IE_HI22)
120+
.Case("tie_lo10", ELF::R_SPARC_TLS_IE_LO10)
121+
.Case("tie_ld", ELF::R_SPARC_TLS_IE_LD)
122+
.Case("tie_ldx", ELF::R_SPARC_TLS_IE_LDX)
123+
.Case("tie_add", ELF::R_SPARC_TLS_IE_ADD)
124+
.Case("tle_hix22", ELF::R_SPARC_TLS_LE_HIX22)
125+
.Case("tle_lox10", ELF::R_SPARC_TLS_LE_LOX10)
126+
.Case("hix", ELF::R_SPARC_HIX22)
127+
.Case("lox", ELF::R_SPARC_LOX10)
128+
.Case("gdop_hix22", ELF::R_SPARC_GOTDATA_OP_HIX22)
129+
.Case("gdop_lox10", ELF::R_SPARC_GOTDATA_OP_LOX10)
130+
.Case("gdop", ELF::R_SPARC_GOTDATA_OP)
131+
.Default(0);
132132
}
133133

134134
uint16_t SparcMCExpr::getFixupKind() const {
135-
// clang-format off
136-
switch (specifier) {
137-
default:
138-
assert(uint16_t(specifier) < FirstTargetFixupKind);
139-
return specifier;
140-
case VK_LO: return ELF::R_SPARC_LO10;
141-
case VK_HI: return ELF::R_SPARC_HI22;
142-
}
143-
// clang-format on
135+
assert(uint16_t(specifier) < FirstTargetFixupKind);
136+
return specifier;
144137
}
145138

146139
bool SparcMCExpr::evaluateAsRelocatableImpl(MCValue &Res,

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,20 @@ namespace llvm {
2121

2222
class StringRef;
2323
class SparcMCExpr : public MCTargetExpr {
24-
public:
25-
enum Specifier {
26-
VK_None,
27-
VK_LO = 200, // larger than any relocation type
28-
VK_HI,
29-
};
30-
3124
private:
32-
const Specifier specifier;
25+
const uint16_t specifier;
3326
const MCExpr *Expr;
3427

35-
explicit SparcMCExpr(Specifier S, const MCExpr *Expr)
28+
explicit SparcMCExpr(uint16_t S, const MCExpr *Expr)
3629
: specifier(S), Expr(Expr) {}
3730

3831
public:
39-
/// @name Construction
40-
/// @{
41-
4232
static const SparcMCExpr *create(uint16_t S, const MCExpr *Expr,
4333
MCContext &Ctx);
44-
/// @}
45-
/// @name Accessors
46-
/// @{
47-
48-
Specifier getSpecifier() const { return specifier; }
34+
uint16_t getSpecifier() const { return specifier; }
4935
const MCExpr *getSubExpr() const { return Expr; }
5036
uint16_t getFixupKind() const;
5137

52-
/// @}
5338
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
5439
bool evaluateAsRelocatableImpl(MCValue &Res,
5540
const MCAssembler *Asm) const override;
@@ -62,8 +47,8 @@ class SparcMCExpr : public MCTargetExpr {
6247
return E->getKind() == MCExpr::Target;
6348
}
6449

65-
static Specifier parseSpecifier(StringRef name);
66-
static StringRef getSpecifierName(Specifier S);
50+
static uint16_t parseSpecifier(StringRef name);
51+
static StringRef getSpecifierName(uint16_t S);
6752
};
6853

6954
} // end namespace llvm.

0 commit comments

Comments
 (0)