Skip to content

Commit 05a9ad9

Browse files
committed
Lanai: Migrate to the new relocation specifier representation
Use MCSpecifierExpr directly and remove the LanaiMCExpr subclass. Define MCSpecifierExpr::printImpl to print the relocation specifier in decimal for llvm-mc -show-inst. The output is not guaranteed to be stable. Depends on f8e0518 ("MC: Adjust -show-inst output for MCExpr")
1 parent f8e0518 commit 05a9ad9

File tree

8 files changed

+95
-147
lines changed

8 files changed

+95
-147
lines changed

llvm/lib/Target/Lanai/AsmParser/LanaiAsmParser.cpp

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "LanaiAluCode.h"
1010
#include "LanaiCondCode.h"
1111
#include "LanaiInstrInfo.h"
12-
#include "MCTargetDesc/LanaiMCExpr.h"
12+
#include "MCTargetDesc/LanaiMCAsmInfo.h"
1313
#include "TargetInfo/LanaiTargetInfo.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/ADT/StringSwitch.h"
@@ -231,14 +231,14 @@ struct LanaiOperand : public MCParsedAsmOperand {
231231
}
232232

233233
// Symbolic reference expression
234-
if (const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(Imm.Value))
235-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_HI;
234+
if (const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(Imm.Value))
235+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_HI;
236236

237237
// Binary expression
238238
if (const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(Imm.Value))
239-
if (const LanaiMCExpr *SymbolRefExpr =
240-
dyn_cast<LanaiMCExpr>(BinaryExpr->getLHS()))
241-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_HI;
239+
if (const auto *SymbolRefExpr =
240+
dyn_cast<MCSpecifierExpr>(BinaryExpr->getLHS()))
241+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_HI;
242242

243243
return false;
244244
}
@@ -268,14 +268,14 @@ struct LanaiOperand : public MCParsedAsmOperand {
268268
}
269269

270270
// Symbolic reference expression
271-
if (const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(Imm.Value))
272-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_LO;
271+
if (const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(Imm.Value))
272+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_LO;
273273

274274
// Binary expression
275275
if (const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(Imm.Value))
276-
if (const LanaiMCExpr *SymbolRefExpr =
277-
dyn_cast<LanaiMCExpr>(BinaryExpr->getLHS()))
278-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_LO;
276+
if (const auto *SymbolRefExpr =
277+
dyn_cast<MCSpecifierExpr>(BinaryExpr->getLHS()))
278+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_LO;
279279

280280
return false;
281281
}
@@ -292,14 +292,14 @@ struct LanaiOperand : public MCParsedAsmOperand {
292292
}
293293

294294
// Symbolic reference expression
295-
if (const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(Imm.Value))
296-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_LO;
295+
if (const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(Imm.Value))
296+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_LO;
297297

298298
// Binary expression
299299
if (const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(Imm.Value))
300-
if (const LanaiMCExpr *SymbolRefExpr =
301-
dyn_cast<LanaiMCExpr>(BinaryExpr->getLHS()))
302-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_LO;
300+
if (const auto *SymbolRefExpr =
301+
dyn_cast<MCSpecifierExpr>(BinaryExpr->getLHS()))
302+
return SymbolRefExpr->getSpecifier() == Lanai::S_ABS_LO;
303303

304304
return false;
305305
}
@@ -339,18 +339,18 @@ struct LanaiOperand : public MCParsedAsmOperand {
339339
}
340340

341341
// Symbolic reference expression
342-
if (const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(Imm.Value))
343-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_None;
342+
if (const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(Imm.Value))
343+
return SymbolRefExpr->getSpecifier() == Lanai::S_None;
344344
if (const MCSymbolRefExpr *SymbolRefExpr =
345345
dyn_cast<MCSymbolRefExpr>(Imm.Value)) {
346346
return SymbolRefExpr->getKind() == MCSymbolRefExpr::VK_None;
347347
}
348348

349349
// Binary expression
350350
if (const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(Imm.Value)) {
351-
if (const LanaiMCExpr *SymbolRefExpr =
352-
dyn_cast<LanaiMCExpr>(BinaryExpr->getLHS()))
353-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_None;
351+
if (const auto *SymbolRefExpr =
352+
dyn_cast<MCSpecifierExpr>(BinaryExpr->getLHS()))
353+
return SymbolRefExpr->getSpecifier() == Lanai::S_None;
354354
if (const MCSymbolRefExpr *SymbolRefExpr =
355355
dyn_cast<MCSymbolRefExpr>(BinaryExpr->getLHS()))
356356
return SymbolRefExpr->getKind() == MCSymbolRefExpr::VK_None;
@@ -464,19 +464,18 @@ struct LanaiOperand : public MCParsedAsmOperand {
464464
if (const MCConstantExpr *ConstExpr = dyn_cast<MCConstantExpr>(getImm()))
465465
Inst.addOperand(
466466
MCOperand::createImm(static_cast<int32_t>(ConstExpr->getValue())));
467-
else if (isa<LanaiMCExpr>(getImm())) {
467+
else if (isa<MCSpecifierExpr>(getImm())) {
468468
#ifndef NDEBUG
469-
const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(getImm());
470-
assert(SymbolRefExpr &&
471-
SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_LO);
469+
const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(getImm());
470+
assert(SymbolRefExpr && SymbolRefExpr->getSpecifier() == Lanai::S_ABS_LO);
472471
#endif
473472
Inst.addOperand(MCOperand::createExpr(getImm()));
474473
} else if (isa<MCBinaryExpr>(getImm())) {
475474
#ifndef NDEBUG
476475
const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(getImm());
477-
assert(BinaryExpr && isa<LanaiMCExpr>(BinaryExpr->getLHS()) &&
478-
cast<LanaiMCExpr>(BinaryExpr->getLHS())->getSpecifier() ==
479-
LanaiMCExpr::VK_Lanai_ABS_LO);
476+
assert(BinaryExpr && isa<MCSpecifierExpr>(BinaryExpr->getLHS()) &&
477+
cast<MCSpecifierExpr>(BinaryExpr->getLHS())->getSpecifier() ==
478+
Lanai::S_ABS_LO);
480479
#endif
481480
Inst.addOperand(MCOperand::createExpr(getImm()));
482481
} else
@@ -495,19 +494,18 @@ struct LanaiOperand : public MCParsedAsmOperand {
495494
assert(N == 1 && "Invalid number of operands!");
496495
if (const MCConstantExpr *ConstExpr = dyn_cast<MCConstantExpr>(getImm()))
497496
Inst.addOperand(MCOperand::createImm(ConstExpr->getValue() >> 16));
498-
else if (isa<LanaiMCExpr>(getImm())) {
497+
else if (isa<MCSpecifierExpr>(getImm())) {
499498
#ifndef NDEBUG
500-
const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(getImm());
501-
assert(SymbolRefExpr &&
502-
SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_ABS_HI);
499+
const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(getImm());
500+
assert(SymbolRefExpr && SymbolRefExpr->getSpecifier() == Lanai::S_ABS_HI);
503501
#endif
504502
Inst.addOperand(MCOperand::createExpr(getImm()));
505503
} else if (isa<MCBinaryExpr>(getImm())) {
506504
#ifndef NDEBUG
507505
const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(getImm());
508-
assert(BinaryExpr && isa<LanaiMCExpr>(BinaryExpr->getLHS()) &&
509-
cast<LanaiMCExpr>(BinaryExpr->getLHS())->getSpecifier() ==
510-
LanaiMCExpr::VK_Lanai_ABS_HI);
506+
assert(BinaryExpr && isa<MCSpecifierExpr>(BinaryExpr->getLHS()) &&
507+
cast<MCSpecifierExpr>(BinaryExpr->getLHS())->getSpecifier() ==
508+
Lanai::S_ABS_HI);
511509
#endif
512510
Inst.addOperand(MCOperand::createExpr(getImm()));
513511
} else
@@ -526,11 +524,10 @@ struct LanaiOperand : public MCParsedAsmOperand {
526524
assert(N == 1 && "Invalid number of operands!");
527525
if (const MCConstantExpr *ConstExpr = dyn_cast<MCConstantExpr>(getImm()))
528526
Inst.addOperand(MCOperand::createImm(ConstExpr->getValue() & 0x1fffff));
529-
else if (isa<LanaiMCExpr>(getImm())) {
527+
else if (isa<MCSpecifierExpr>(getImm())) {
530528
#ifndef NDEBUG
531-
const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(getImm());
532-
assert(SymbolRefExpr &&
533-
SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_None);
529+
const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(getImm());
530+
assert(SymbolRefExpr && SymbolRefExpr->getSpecifier() == Lanai::S_None);
534531
#endif
535532
Inst.addOperand(MCOperand::createExpr(getImm()));
536533
} else if (isa<MCSymbolRefExpr>(getImm())) {
@@ -544,9 +541,9 @@ struct LanaiOperand : public MCParsedAsmOperand {
544541
} else if (isa<MCBinaryExpr>(getImm())) {
545542
#ifndef NDEBUG
546543
const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(getImm());
547-
assert(BinaryExpr && isa<LanaiMCExpr>(BinaryExpr->getLHS()) &&
548-
cast<LanaiMCExpr>(BinaryExpr->getLHS())->getSpecifier() ==
549-
LanaiMCExpr::VK_Lanai_None);
544+
assert(BinaryExpr && isa<MCSpecifierExpr>(BinaryExpr->getLHS()) &&
545+
cast<MCSpecifierExpr>(BinaryExpr->getLHS())->getSpecifier() ==
546+
Lanai::S_None);
550547
#endif
551548
Inst.addOperand(MCOperand::createExpr(getImm()));
552549
} else
@@ -737,7 +734,7 @@ std::unique_ptr<LanaiOperand> LanaiAsmParser::parseIdentifier() {
737734
SMLoc Start = Parser.getTok().getLoc();
738735
SMLoc End = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
739736
const MCExpr *Res, *RHS = nullptr;
740-
LanaiMCExpr::Spec Kind = LanaiMCExpr::VK_Lanai_None;
737+
auto Kind = Lanai::S_None;
741738

742739
if (Lexer.getKind() != AsmToken::Identifier)
743740
return nullptr;
@@ -748,13 +745,13 @@ std::unique_ptr<LanaiOperand> LanaiAsmParser::parseIdentifier() {
748745

749746
// Check if identifier has a modifier
750747
if (Identifier.equals_insensitive("hi"))
751-
Kind = LanaiMCExpr::VK_Lanai_ABS_HI;
748+
Kind = Lanai::S_ABS_HI;
752749
else if (Identifier.equals_insensitive("lo"))
753-
Kind = LanaiMCExpr::VK_Lanai_ABS_LO;
750+
Kind = Lanai::S_ABS_LO;
754751

755752
// If the identifier corresponds to a variant then extract the real
756753
// identifier.
757-
if (Kind != LanaiMCExpr::VK_Lanai_None) {
754+
if (Kind != Lanai::S_None) {
758755
if (Lexer.getKind() != AsmToken::LParen) {
759756
Error(Lexer.getLoc(), "Expected '('");
760757
return nullptr;
@@ -771,7 +768,7 @@ std::unique_ptr<LanaiOperand> LanaiAsmParser::parseIdentifier() {
771768
return nullptr;
772769

773770
// For variants parse the final ')'
774-
if (Kind != LanaiMCExpr::VK_Lanai_None) {
771+
if (Kind != Lanai::S_None) {
775772
if (Lexer.getKind() != AsmToken::RParen) {
776773
Error(Lexer.getLoc(), "Expected ')'");
777774
return nullptr;
@@ -781,8 +778,7 @@ std::unique_ptr<LanaiOperand> LanaiAsmParser::parseIdentifier() {
781778

782779
End = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
783780
MCSymbol *Sym = getContext().getOrCreateSymbol(Identifier);
784-
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
785-
Res = LanaiMCExpr::create(Kind, Expr, getContext());
781+
Res = MCSpecifierExpr::create(Sym, Kind, getContext());
786782

787783
// Nest if this was an addition
788784
if (RHS)
@@ -865,16 +861,16 @@ bool shouldBeSls(const LanaiOperand &Op) {
865861
}
866862
// The instruction should be encoded as an SLS if the operand is a symbolic
867863
// reference with no variant.
868-
if (const LanaiMCExpr *SymbolRefExpr = dyn_cast<LanaiMCExpr>(Op.getImm()))
869-
return SymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_None;
864+
if (const auto *SymbolRefExpr = dyn_cast<MCSpecifierExpr>(Op.getImm()))
865+
return SymbolRefExpr->getSpecifier() == Lanai::S_None;
870866
// The instruction should be encoded as an SLS if the operand is a binary
871867
// expression with the left-hand side being a symbolic reference with no
872868
// variant.
873869
if (const MCBinaryExpr *BinaryExpr = dyn_cast<MCBinaryExpr>(Op.getImm())) {
874-
const LanaiMCExpr *LHSSymbolRefExpr =
875-
dyn_cast<LanaiMCExpr>(BinaryExpr->getLHS());
870+
const auto *LHSSymbolRefExpr =
871+
dyn_cast<MCSpecifierExpr>(BinaryExpr->getLHS());
876872
return (LHSSymbolRefExpr &&
877-
LHSSymbolRefExpr->getSpecifier() == LanaiMCExpr::VK_Lanai_None);
873+
LHSSymbolRefExpr->getSpecifier() == Lanai::S_None);
878874
}
879875
return false;
880876
}

llvm/lib/Target/Lanai/LanaiMCInstLower.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "LanaiMCInstLower.h"
1515

1616
#include "MCTargetDesc/LanaiBaseInfo.h"
17-
#include "MCTargetDesc/LanaiMCExpr.h"
17+
#include "MCTargetDesc/LanaiMCAsmInfo.h"
1818
#include "llvm/ADT/SmallString.h"
1919
#include "llvm/CodeGen/AsmPrinter.h"
2020
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -64,17 +64,16 @@ LanaiMCInstLower::GetConstantPoolIndexSymbol(const MachineOperand &MO) const {
6464

6565
MCOperand LanaiMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
6666
MCSymbol *Sym) const {
67-
LanaiMCExpr::Spec Kind;
68-
67+
Lanai::Specifier Kind;
6968
switch (MO.getTargetFlags()) {
7069
case LanaiII::MO_NO_FLAG:
71-
Kind = LanaiMCExpr::VK_Lanai_None;
70+
Kind = Lanai::S_None;
7271
break;
7372
case LanaiII::MO_ABS_HI:
74-
Kind = LanaiMCExpr::VK_Lanai_ABS_HI;
73+
Kind = Lanai::S_ABS_HI;
7574
break;
7675
case LanaiII::MO_ABS_LO:
77-
Kind = LanaiMCExpr::VK_Lanai_ABS_LO;
76+
Kind = Lanai::S_ABS_LO;
7877
break;
7978
default:
8079
llvm_unreachable("Unknown target flag on GV operand");
@@ -84,7 +83,7 @@ MCOperand LanaiMCInstLower::LowerSymbolOperand(const MachineOperand &MO,
8483
if (!MO.isJTI() && MO.getOffset())
8584
Expr = MCBinaryExpr::createAdd(
8685
Expr, MCConstantExpr::create(MO.getOffset(), Ctx), Ctx);
87-
Expr = LanaiMCExpr::create(Kind, Expr, Ctx);
86+
Expr = MCSpecifierExpr::create(Expr, Kind, Ctx);
8887
return MCOperand::createExpr(Expr);
8988
}
9089

llvm/lib/Target/Lanai/MCTargetDesc/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ add_llvm_component_library(LLVMLanaiDesc
44
LanaiInstPrinter.cpp
55
LanaiMCAsmInfo.cpp
66
LanaiMCCodeEmitter.cpp
7-
LanaiMCExpr.cpp
87
LanaiMCTargetDesc.cpp
98

109
LINK_COMPONENTS

llvm/lib/Target/Lanai/MCTargetDesc/LanaiMCAsmInfo.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "LanaiMCAsmInfo.h"
14-
14+
#include "llvm/MC/MCExpr.h"
15+
#include "llvm/Support/raw_ostream.h"
1516
#include "llvm/TargetParser/Triple.h"
1617

1718
using namespace llvm;
@@ -38,3 +39,26 @@ LanaiMCAsmInfo::LanaiMCAsmInfo(const Triple & /*TheTriple*/,
3839
// in dwarf generation.
3940
MinInstAlignment = 4;
4041
}
42+
43+
void LanaiMCAsmInfo::printSpecifierExpr(raw_ostream &OS,
44+
const MCSpecifierExpr &Expr) const {
45+
if (Expr.getSpecifier() == 0) {
46+
printExpr(OS, *Expr.getSubExpr());
47+
return;
48+
}
49+
50+
switch (Expr.getSpecifier()) {
51+
default:
52+
llvm_unreachable("Invalid kind!");
53+
case Lanai::S_ABS_HI:
54+
OS << "hi";
55+
break;
56+
case Lanai::S_ABS_LO:
57+
OS << "lo";
58+
break;
59+
}
60+
61+
OS << '(';
62+
printExpr(OS, *Expr.getSubExpr());
63+
OS << ')';
64+
}

llvm/lib/Target/Lanai/MCTargetDesc/LanaiMCAsmInfo.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ class LanaiMCAsmInfo : public MCAsmInfoELF {
2424
public:
2525
explicit LanaiMCAsmInfo(const Triple &TheTriple,
2626
const MCTargetOptions &Options);
27+
void printSpecifierExpr(raw_ostream &OS,
28+
const MCSpecifierExpr &Expr) const override;
2729
};
2830

31+
namespace Lanai {
32+
using Specifier = uint8_t;
33+
enum { S_None, S_ABS_HI, S_ABS_LO };
34+
} // namespace Lanai
35+
2936
} // namespace llvm
3037

3138
#endif // LLVM_LIB_TARGET_LANAI_MCTARGETDESC_LANAIMCASMINFO_H

llvm/lib/Target/Lanai/MCTargetDesc/LanaiMCCodeEmitter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "LanaiAluCode.h"
1414
#include "MCTargetDesc/LanaiBaseInfo.h"
1515
#include "MCTargetDesc/LanaiFixupKinds.h"
16-
#include "MCTargetDesc/LanaiMCExpr.h"
16+
#include "MCTargetDesc/LanaiMCAsmInfo.h"
1717
#include "llvm/ADT/SmallVector.h"
1818
#include "llvm/ADT/Statistic.h"
1919
#include "llvm/MC/MCCodeEmitter.h"
@@ -89,14 +89,14 @@ class LanaiMCCodeEmitter : public MCCodeEmitter {
8989
static Lanai::Fixups FixupKind(const MCExpr *Expr) {
9090
if (isa<MCSymbolRefExpr>(Expr))
9191
return Lanai::FIXUP_LANAI_21;
92-
if (const LanaiMCExpr *McExpr = dyn_cast<LanaiMCExpr>(Expr)) {
93-
LanaiMCExpr::Spec ExprKind = McExpr->getSpecifier();
92+
if (const MCSpecifierExpr *McExpr = dyn_cast<MCSpecifierExpr>(Expr)) {
93+
Lanai::Specifier ExprKind = McExpr->getSpecifier();
9494
switch (ExprKind) {
95-
case LanaiMCExpr::VK_Lanai_None:
95+
case Lanai::S_None:
9696
return Lanai::FIXUP_LANAI_21;
97-
case LanaiMCExpr::VK_Lanai_ABS_HI:
97+
case Lanai::S_ABS_HI:
9898
return Lanai::FIXUP_LANAI_HI16;
99-
case LanaiMCExpr::VK_Lanai_ABS_LO:
99+
case Lanai::S_ABS_LO:
100100
return Lanai::FIXUP_LANAI_LO16;
101101
}
102102
}
@@ -123,7 +123,7 @@ unsigned LanaiMCCodeEmitter::getMachineOpValue(
123123
Expr = BinaryExpr->getLHS();
124124
}
125125

126-
assert(isa<LanaiMCExpr>(Expr) || Expr->getKind() == MCExpr::SymbolRef);
126+
assert(isa<MCSpecifierExpr>(Expr) || Expr->getKind() == MCExpr::SymbolRef);
127127
// Push fixup (all info is contained within)
128128
Fixups.push_back(
129129
MCFixup::create(0, MCOp.getExpr(), MCFixupKind(FixupKind(Expr))));

0 commit comments

Comments
 (0)