Skip to content

Commit 18b67a7

Browse files
committed
MC: Add MCAsmInfo::printExpr to replace MCExpr::print
* Make relocation specifier code closer (MCAsmInfo defines specifiers). * MCExpr::print has an optional MCAsmInfo argument, which is error-prone when omitted. * Enable MCSpecifierExpr
1 parent dc9e300 commit 18b67a7

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class MCSection;
3232
class MCStreamer;
3333
class MCSubtargetInfo;
3434
class MCSymbol;
35+
class raw_ostream;
3536

3637
namespace WinEH {
3738

@@ -709,6 +710,8 @@ class LLVM_ABI MCAsmInfo {
709710

710711
StringRef getSpecifierName(uint32_t S) const;
711712
std::optional<uint32_t> getSpecifierForName(StringRef Name) const;
713+
714+
void printExpr(raw_ostream &, const MCExpr &) const;
712715
};
713716

714717
} // end namespace llvm

llvm/lib/MC/MCAsmInfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/MC/MCContext.h"
1818
#include "llvm/MC/MCExpr.h"
1919
#include "llvm/MC/MCStreamer.h"
20+
#include "llvm/Support/Casting.h"
2021
#include "llvm/Support/CommandLine.h"
2122

2223
using namespace llvm;
@@ -148,3 +149,7 @@ std::optional<uint32_t> MCAsmInfo::getSpecifierForName(StringRef Name) const {
148149
return It->second;
149150
return {};
150151
}
152+
153+
void MCAsmInfo::printExpr(raw_ostream &OS, const MCExpr &Expr) const {
154+
Expr.print(OS, this);
155+
}

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ void MCAsmStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
700700
OS << ".set ";
701701
Symbol->print(OS, MAI);
702702
OS << (UseSet ? ", " : " = ");
703-
Value->print(OS, MAI);
703+
MAI->printExpr(OS, *Value);
704704

705705
EmitEOL();
706706
}
@@ -713,7 +713,7 @@ void MCAsmStreamer::emitConditionalAssignment(MCSymbol *Symbol,
713713
OS << ".lto_set_conditional ";
714714
Symbol->print(OS, MAI);
715715
OS << ", ";
716-
Value->print(OS, MAI);
716+
MAI->printExpr(OS, *Value);
717717
EmitEOL();
718718
}
719719

@@ -1065,7 +1065,7 @@ void MCAsmStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
10651065
OS << "\t.size\t";
10661066
Symbol->print(OS, MAI);
10671067
OS << ", ";
1068-
Value->print(OS, MAI);
1068+
MAI->printExpr(OS, *Value);
10691069
EmitEOL();
10701070
}
10711071

@@ -1399,7 +1399,7 @@ void MCAsmStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
13991399
if (MCTargetStreamer *TS = getTargetStreamer()) {
14001400
TS->emitValue(Value);
14011401
} else {
1402-
Value->print(OS, MAI);
1402+
MAI->printExpr(OS, *Value);
14031403
EmitEOL();
14041404
}
14051405
}
@@ -1411,7 +1411,7 @@ void MCAsmStreamer::emitULEB128Value(const MCExpr *Value) {
14111411
return;
14121412
}
14131413
OS << "\t.uleb128 ";
1414-
Value->print(OS, MAI);
1414+
MAI->printExpr(OS, *Value);
14151415
EmitEOL();
14161416
}
14171417

@@ -1422,7 +1422,7 @@ void MCAsmStreamer::emitSLEB128Value(const MCExpr *Value) {
14221422
return;
14231423
}
14241424
OS << "\t.sleb128 ";
1425-
Value->print(OS, MAI);
1425+
MAI->printExpr(OS, *Value);
14261426
EmitEOL();
14271427
}
14281428

@@ -1437,7 +1437,7 @@ void MCAsmStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
14371437
if (!MAI->isAIX() || FillValue == 0) {
14381438
// FIXME: Emit location directives
14391439
OS << ZeroDirective;
1440-
NumBytes.print(OS, MAI);
1440+
MAI->printExpr(OS, NumBytes);
14411441
if (FillValue != 0)
14421442
OS << ',' << (int)FillValue;
14431443
EmitEOL();
@@ -1460,7 +1460,7 @@ void MCAsmStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
14601460
int64_t Expr, SMLoc Loc) {
14611461
// FIXME: Emit location directives
14621462
OS << "\t.fill\t";
1463-
NumValues.print(OS, MAI);
1463+
MAI->printExpr(OS, NumValues);
14641464
OS << ", " << Size << ", 0x";
14651465
OS.write_hex(truncateToSize(Expr, 4));
14661466
EmitEOL();
@@ -1558,7 +1558,7 @@ void MCAsmStreamer::emitValueToOffset(const MCExpr *Offset,
15581558
SMLoc Loc) {
15591559
// FIXME: Verify that Offset is associated with the current section.
15601560
OS << ".org ";
1561-
Offset->print(OS, MAI);
1561+
MAI->printExpr(OS, *Offset);
15621562
OS << ", " << (unsigned)Value;
15631563
EmitEOL();
15641564
}
@@ -2417,7 +2417,7 @@ void MCAsmStreamer::AddEncodingComment(const MCInst &Inst,
24172417
MCFixup &F = Fixups[i];
24182418
OS << " fixup " << char('A' + i) << " - "
24192419
<< "offset: " << F.getOffset() << ", value: ";
2420-
F.getValue()->print(OS, MAI);
2420+
MAI->printExpr(OS, *F.getValue());
24212421
auto Kind = F.getKind();
24222422
if (mc::isRelocation(Kind))
24232423
OS << ", relocation type: " << Kind;
@@ -2496,11 +2496,11 @@ MCAsmStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
24962496
const MCExpr *Expr, SMLoc,
24972497
const MCSubtargetInfo &STI) {
24982498
OS << "\t.reloc ";
2499-
Offset.print(OS, MAI);
2499+
MAI->printExpr(OS, Offset);
25002500
OS << ", " << Name;
25012501
if (Expr) {
25022502
OS << ", ";
2503-
Expr->print(OS, MAI);
2503+
MAI->printExpr(OS, *Expr);
25042504
}
25052505
EmitEOL();
25062506
return std::nullopt;

llvm/lib/MC/MCStreamer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void MCTargetStreamer::emitValue(const MCExpr *Value) {
7272
SmallString<128> Str;
7373
raw_svector_ostream OS(Str);
7474

75-
Value->print(OS, Streamer.getContext().getAsmInfo());
75+
Streamer.getContext().getAsmInfo()->printExpr(OS, *Value);
7676
Streamer.emitRawText(OS.str());
7777
}
7878

0 commit comments

Comments
 (0)