Skip to content

Commit 63b9cbd

Browse files
committed
MCStreamer: Add helpers and eliminate direct MCFragment operations
To facilitate optimizing the MCFragment internals, we don't want users to access MCFragment directly.
1 parent 6ebc423 commit 63b9cbd

File tree

6 files changed

+43
-64
lines changed

6 files changed

+43
-64
lines changed

llvm/include/llvm/MC/MCObjectStreamer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,12 @@ class MCObjectStreamer : public MCStreamer {
8383
// Add a fragment with a variable-size tail and start a new empty fragment.
8484
void insert(MCFragment *F);
8585

86-
void addFixup(const MCExpr *Value, MCFixupKind Kind, uint32_t Offset = 0);
8786
// Add a new fragment to the current section without a variable-size tail.
8887
void newFragment();
8988

89+
void appendContents(size_t Num, char Elt);
90+
void addFixup(const MCExpr *Value, MCFixupKind Kind);
91+
9092
void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
9193
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F,
9294
uint64_t Offset);

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ class LLVM_ABI MCStreamer {
438438
assert(!CurFrag || CurFrag->getKind() == MCFragment::FT_Data);
439439
return CurFrag;
440440
}
441+
size_t getCurFragOffset() const { return getCurrentFragment()->Offset; }
441442
/// Save the current and previous section on the section stack.
442443
void pushSection() {
443444
SectionStack.push_back(

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ void MCObjectStreamer::insert(MCFragment *F) {
5858
newFragment();
5959
}
6060

61-
void MCObjectStreamer::addFixup(const MCExpr *Value, MCFixupKind Kind,
62-
uint32_t Offset) {
63-
CurFrag->addFixup(
64-
MCFixup::create(CurFrag->getFixedSize() + Offset, Value, Kind));
61+
void MCObjectStreamer::appendContents(size_t Num, char Elt) {
62+
CurFrag->appendContents(Num, Elt);
63+
}
64+
65+
void MCObjectStreamer::addFixup(const MCExpr *Value, MCFixupKind Kind) {
66+
CurFrag->addFixup(MCFixup::create(CurFrag->getFixedSize(), Value, Kind));
6567
}
6668

6769
// As a compile-time optimization, avoid allocating and evaluating an MCExpr

llvm/lib/MC/MCWin64EH.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,13 @@ static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
318318

319319
// Emit the epilog instructions.
320320
if (EnableUnwindV2) {
321-
MCFragment *DF = OS->getCurrentFragment();
322-
323321
bool IsLast = true;
324322
for (const auto &Epilog : llvm::reverse(info->EpilogMap)) {
325323
if (IsLast) {
326324
IsLast = false;
327325
uint8_t Flags = LastEpilogIsAtEnd ? 0x01 : 0;
328-
streamer.emitInt8(EpilogSize);
329-
streamer.emitInt8((Flags << 4) | Win64EH::UOP_Epilog);
326+
OS->emitInt8(EpilogSize);
327+
OS->emitInt8((Flags << 4) | Win64EH::UOP_Epilog);
330328

331329
if (LastEpilogIsAtEnd)
332330
continue;
@@ -337,9 +335,8 @@ static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
337335
// layout has been completed.
338336
auto *MCE = MCUnwindV2EpilogTargetExpr::create(*info, Epilog.second,
339337
EpilogSize, context);
340-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_2);
341-
DF->addFixup(Fixup);
342-
DF->appendContents(2, 0);
338+
OS->addFixup(MCE, FK_Data_2);
339+
OS->appendContents(2, 0);
343340
}
344341
}
345342
if (AddPaddingEpilogCode)

llvm/lib/MC/MCWinCOFFStreamer.cpp

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,76 +278,58 @@ void MCWinCOFFStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {
278278

279279
void MCWinCOFFStreamer::emitCOFFSectionIndex(const MCSymbol *Symbol) {
280280
visitUsedSymbol(*Symbol);
281-
MCFragment *DF = getCurrentFragment();
282281
const MCSymbolRefExpr *SRE = MCSymbolRefExpr::create(Symbol, getContext());
283-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), SRE, FK_SecRel_2);
284-
DF->addFixup(Fixup);
285-
DF->appendContents(2, 0);
282+
addFixup(SRE, FK_SecRel_2);
283+
appendContents(2, 0);
286284
}
287285

288286
void MCWinCOFFStreamer::emitCOFFSecRel32(const MCSymbol *Symbol,
289287
uint64_t Offset) {
290288
visitUsedSymbol(*Symbol);
291-
MCFragment *DF = getCurrentFragment();
292289
// Create Symbol A for the relocation relative reference.
293290
const MCExpr *MCE = MCSymbolRefExpr::create(Symbol, getContext());
294291
// Add the constant offset, if given.
295292
if (Offset)
296293
MCE = MCBinaryExpr::createAdd(
297294
MCE, MCConstantExpr::create(Offset, getContext()), getContext());
298-
// Build the secrel32 relocation.
299-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_SecRel_4);
300-
// Record the relocation.
301-
DF->addFixup(Fixup);
295+
addFixup(MCE, FK_SecRel_4);
302296
// Emit 4 bytes (zeros) to the object file.
303-
DF->appendContents(4, 0);
297+
appendContents(4, 0);
304298
}
305299

306300
void MCWinCOFFStreamer::emitCOFFImgRel32(const MCSymbol *Symbol,
307301
int64_t Offset) {
308302
visitUsedSymbol(*Symbol);
309-
MCFragment *DF = getCurrentFragment();
310303
// Create Symbol A for the relocation relative reference.
311304
const MCExpr *MCE = MCSymbolRefExpr::create(
312305
Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32, getContext());
313306
// Add the constant offset, if given.
314307
if (Offset)
315308
MCE = MCBinaryExpr::createAdd(
316309
MCE, MCConstantExpr::create(Offset, getContext()), getContext());
317-
// Build the imgrel relocation.
318-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4);
319-
// Record the relocation.
320-
DF->addFixup(Fixup);
310+
addFixup(MCE, FK_Data_4);
321311
// Emit 4 bytes (zeros) to the object file.
322-
DF->appendContents(4, 0);
312+
appendContents(4, 0);
323313
}
324314

325315
void MCWinCOFFStreamer::emitCOFFSecNumber(MCSymbol const *Symbol) {
326316
visitUsedSymbol(*Symbol);
327-
MCFragment *DF = getCurrentFragment();
328317
// Create Symbol for section number.
329318
const MCExpr *MCE = MCCOFFSectionNumberTargetExpr::create(
330319
*Symbol, this->getWriter(), getContext());
331-
// Build the relocation.
332-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4);
333-
// Record the relocation.
334-
DF->addFixup(Fixup);
320+
addFixup(MCE, FK_Data_4);
335321
// Emit 4 bytes (zeros) to the object file.
336-
DF->appendContents(4, 0);
322+
appendContents(4, 0);
337323
}
338324

339325
void MCWinCOFFStreamer::emitCOFFSecOffset(MCSymbol const *Symbol) {
340326
visitUsedSymbol(*Symbol);
341-
MCFragment *DF = getCurrentFragment();
342327
// Create Symbol for section offset.
343328
const MCExpr *MCE =
344329
MCCOFFSectionOffsetTargetExpr::create(*Symbol, getContext());
345-
// Build the relocation.
346-
MCFixup Fixup = MCFixup::create(DF->getContents().size(), MCE, FK_Data_4);
347-
// Record the relocation.
348-
DF->addFixup(Fixup);
330+
addFixup(MCE, FK_Data_4);
349331
// Emit 4 bytes (zeros) to the object file.
350-
DF->appendContents(4, 0);
332+
appendContents(4, 0);
351333
}
352334

353335
void MCWinCOFFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,

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

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,45 +1033,40 @@ MCELFStreamer &MipsTargetELFStreamer::getStreamer() {
10331033
}
10341034

10351035
void MipsTargetELFStreamer::emitGPRel32Value(const MCExpr *Value) {
1036-
MCFragment *DF = getStreamer().getCurrentFragment();
1037-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1038-
Mips::fixup_Mips_GPREL32));
1039-
DF->appendContents(4, 0);
1036+
auto &S = getStreamer();
1037+
S.addFixup(Value, Mips::fixup_Mips_GPREL32);
1038+
S.appendContents(4, 0);
10401039
}
10411040

10421041
void MipsTargetELFStreamer::emitGPRel64Value(const MCExpr *Value) {
1043-
MCFragment *DF = getStreamer().getCurrentFragment();
1044-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1045-
Mips::fixup_Mips_GPREL32));
1046-
DF->appendContents(8, 0);
1042+
auto &S = getStreamer();
1043+
// fixup_Mips_GPREL32 desginates R_MIPS_GPREL32+R_MIPS_64 on MIPS64.
1044+
S.addFixup(Value, Mips::fixup_Mips_GPREL32);
1045+
S.appendContents(8, 0);
10471046
}
10481047

10491048
void MipsTargetELFStreamer::emitDTPRel32Value(const MCExpr *Value) {
1050-
MCFragment *DF = getStreamer().getCurrentFragment();
1051-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1052-
Mips::fixup_Mips_DTPREL32));
1053-
DF->appendContents(4, 0);
1049+
auto &S = getStreamer();
1050+
S.addFixup(Value, Mips::fixup_Mips_DTPREL32);
1051+
S.appendContents(4, 0);
10541052
}
10551053

10561054
void MipsTargetELFStreamer::emitDTPRel64Value(const MCExpr *Value) {
1057-
MCFragment *DF = getStreamer().getCurrentFragment();
1058-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1059-
Mips::fixup_Mips_DTPREL64));
1060-
DF->appendContents(8, 0);
1055+
auto &S = getStreamer();
1056+
S.addFixup(Value, Mips::fixup_Mips_DTPREL64);
1057+
S.appendContents(8, 0);
10611058
}
10621059

10631060
void MipsTargetELFStreamer::emitTPRel32Value(const MCExpr *Value) {
1064-
MCFragment *DF = getStreamer().getCurrentFragment();
1065-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1066-
Mips::fixup_Mips_TPREL32));
1067-
DF->appendContents(4, 0);
1061+
auto &S = getStreamer();
1062+
S.addFixup(Value, Mips::fixup_Mips_TPREL32);
1063+
S.appendContents(4, 0);
10681064
}
10691065

10701066
void MipsTargetELFStreamer::emitTPRel64Value(const MCExpr *Value) {
1071-
MCFragment *DF = getStreamer().getCurrentFragment();
1072-
DF->addFixup(MCFixup::create(DF->getContents().size(), Value,
1073-
Mips::fixup_Mips_TPREL64));
1074-
DF->appendContents(8, 0);
1067+
auto &S = getStreamer();
1068+
S.addFixup(Value, Mips::fixup_Mips_TPREL64);
1069+
S.appendContents(8, 0);
10751070
}
10761071

10771072
void MipsTargetELFStreamer::emitDirectiveSetMicroMips() {

0 commit comments

Comments
 (0)