Skip to content

Commit

Permalink
[MC] Thread unique_ptr<MCObjectWriter> through the create.*ObjectWriter
Browse files Browse the repository at this point in the history
functions.

This makes the ownership of the resulting MCObjectWriter clear, and allows us
to remove one instance of MCObjectStreamer's bizarre "holding ownership via
someone else's reference" trick.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315327 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
lhames committed Oct 10, 2017
1 parent 8e46b87 commit e471346
Show file tree
Hide file tree
Showing 55 changed files with 204 additions and 137 deletions.
3 changes: 2 additions & 1 deletion include/llvm/MC/MCAsmBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class MCAsmBackend {

/// Create a new MCObjectWriter instance for use by the assembler backend to
/// emit the final object file.
virtual MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const = 0;
virtual std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const = 0;

/// \name Target Fixup Interfaces
/// @{
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/MC/MCELFObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class MCELFObjectTargetWriter {
/// \param MOTW - The target specific ELF writer subclass.
/// \param OS - The stream to write to.
/// \returns The constructed object writer.
MCObjectWriter *
std::unique_ptr<MCObjectWriter>
createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
raw_pwrite_stream &OS, bool IsLittleEndian);

Expand Down
2 changes: 1 addition & 1 deletion include/llvm/MC/MCMachObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class MachObjectWriter : public MCObjectWriter {
/// \param MOTW - The target specific Mach-O writer subclass.
/// \param OS - The stream to write to.
/// \returns The constructed object writer.
MCObjectWriter *
std::unique_ptr<MCObjectWriter>
createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
raw_pwrite_stream &OS, bool IsLittleEndian);

Expand Down
1 change: 1 addition & 0 deletions include/llvm/MC/MCObjectStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class raw_pwrite_stream;
/// to that file format or custom semantics expected by the object writer
/// implementation.
class MCObjectStreamer : public MCStreamer {
std::unique_ptr<MCObjectWriter> ObjectWriter;
std::unique_ptr<MCAssembler> Assembler;
MCSection::iterator CurInsertionPoint;
bool EmitEHFrame;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/MC/MCWasmObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MCWasmObjectTargetWriter {
/// \param MOTW - The target specific Wasm writer subclass.
/// \param OS - The stream to write to.
/// \returns The constructed object writer.
MCObjectWriter *
std::unique_ptr<MCObjectWriter>
createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS);

Expand Down
2 changes: 1 addition & 1 deletion include/llvm/MC/MCWinCOFFObjectWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class raw_pwrite_stream;
/// \param MOTW - The target specific WinCOFF writer subclass.
/// \param OS - The stream to write to.
/// \returns The constructed object writer.
MCObjectWriter *
std::unique_ptr<MCObjectWriter>
createWinCOFFObjectWriter(std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW,
raw_pwrite_stream &OS);
} // end namespace llvm
Expand Down
5 changes: 3 additions & 2 deletions lib/MC/ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,9 @@ bool ELFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
InSet, IsPCRel);
}

MCObjectWriter *
std::unique_ptr<MCObjectWriter>
llvm::createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
raw_pwrite_stream &OS, bool IsLittleEndian) {
return new ELFObjectWriter(std::move(MOTW), OS, IsLittleEndian);
return llvm::make_unique<ELFObjectWriter>(std::move(MOTW), OS,
IsLittleEndian);
}
9 changes: 5 additions & 4 deletions lib/MC/MCELFStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ void MCELFStreamer::mergeFragment(MCDataFragment *DF,
if (RequiredBundlePadding > 0) {
SmallString<256> Code;
raw_svector_ostream VecOS(Code);
MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
{
auto OW = Assembler.getBackend().createObjectWriter(VecOS);

EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));

Assembler.writeFragmentPadding(*EF, FSize, OW);
delete OW;
Assembler.writeFragmentPadding(*EF, FSize, OW.get());
}

DF->getContents().append(Code.begin(), Code.end());
}
Expand Down
5 changes: 2 additions & 3 deletions lib/MC/MCObjectStreamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ using namespace llvm;
MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB,
raw_pwrite_stream &OS,
MCCodeEmitter *Emitter_)
: MCStreamer(Context),
: MCStreamer(Context), ObjectWriter(TAB.createObjectWriter(OS)),
Assembler(llvm::make_unique<MCAssembler>(Context, TAB, *Emitter_,
*TAB.createObjectWriter(OS))),
*ObjectWriter)),
EmitEHFrame(true), EmitDebugFrame(false) {}

MCObjectStreamer::~MCObjectStreamer() {
delete &Assembler->getBackend();
delete &Assembler->getEmitter();
delete &Assembler->getWriter();
}

void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
Expand Down
5 changes: 3 additions & 2 deletions lib/MC/MachObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,9 @@ void MachObjectWriter::writeObject(MCAssembler &Asm,
}
}

MCObjectWriter *
std::unique_ptr<MCObjectWriter>
llvm::createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
raw_pwrite_stream &OS, bool IsLittleEndian) {
return new MachObjectWriter(std::move(MOTW), OS, IsLittleEndian);
return llvm::make_unique<MachObjectWriter>(std::move(MOTW), OS,
IsLittleEndian);
}
7 changes: 5 additions & 2 deletions lib/MC/WasmObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1317,8 +1317,11 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
// TODO: Translate debug sections to the output.
}

MCObjectWriter *
std::unique_ptr<MCObjectWriter>
llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
raw_pwrite_stream &OS) {
return new WasmObjectWriter(std::move(MOTW), OS);
// FIXME: Can't use make_unique<WasmObjectWriter>(...) as WasmObjectWriter's
// destructor is private. Is that necessary?
return std::unique_ptr<MCObjectWriter>(
new WasmObjectWriter(std::move(MOTW), OS));
}
4 changes: 2 additions & 2 deletions lib/MC/WinCOFFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ void MCWinCOFFObjectTargetWriter::anchor() {}
//------------------------------------------------------------------------------
// WinCOFFObjectWriter factory function

MCObjectWriter *llvm::createWinCOFFObjectWriter(
std::unique_ptr<MCObjectWriter> llvm::createWinCOFFObjectWriter(
std::unique_ptr<MCWinCOFFObjectTargetWriter> MOTW, raw_pwrite_stream &OS) {
return new WinCOFFObjectWriter(std::move(MOTW), OS);
return llvm::make_unique<WinCOFFObjectWriter>(std::move(MOTW), OS);
}
9 changes: 6 additions & 3 deletions lib/Target/AArch64/MCTargetDesc/AArch64AsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ class DarwinAArch64AsmBackend : public AArch64AsmBackend {
const MCRegisterInfo &MRI)
: AArch64AsmBackend(T, TT, /*IsLittleEndian*/ true), MRI(MRI) {}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createAArch64MachObjectWriter(OS, MachO::CPU_TYPE_ARM64,
MachO::CPU_SUBTYPE_ARM64_ALL);
}
Expand Down Expand Up @@ -582,7 +583,8 @@ class ELFAArch64AsmBackend : public AArch64AsmBackend {
: AArch64AsmBackend(T, TT, IsLittleEndian), OSABI(OSABI),
IsILP32(IsILP32) {}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createAArch64ELFObjectWriter(OS, OSABI, IsLittleEndian, IsILP32);
}
};
Expand All @@ -595,7 +597,8 @@ class COFFAArch64AsmBackend : public AArch64AsmBackend {
COFFAArch64AsmBackend(const Target &T, const Triple &TheTriple)
: AArch64AsmBackend(T, TheTriple, /*IsLittleEndian*/ true) {}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createAArch64WinCOFFObjectWriter(OS);
}
};
Expand Down
8 changes: 4 additions & 4 deletions lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
Expand Down Expand Up @@ -428,10 +429,9 @@ unsigned AArch64ELFObjectWriter::getRelocType(MCContext &Ctx,
llvm_unreachable("Unimplemented fixup -> relocation");
}

MCObjectWriter *llvm::createAArch64ELFObjectWriter(raw_pwrite_stream &OS,
uint8_t OSABI,
bool IsLittleEndian,
bool IsILP32) {
std::unique_ptr<MCObjectWriter>
llvm::createAArch64ELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI,
bool IsLittleEndian, bool IsILP32) {
auto MOTW =
llvm::make_unique<AArch64ELFObjectWriter>(OSABI, IsLittleEndian, IsILP32);
return createELFObjectWriter(std::move(MOTW), OS, IsLittleEndian);
Expand Down
18 changes: 10 additions & 8 deletions lib/Target/AArch64/MCTargetDesc/AArch64MCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "llvm/Support/DataTypes.h"

#include <memory>

namespace llvm {
class formatted_raw_ostream;
class MCAsmBackend;
Expand Down Expand Up @@ -51,16 +53,16 @@ MCAsmBackend *createAArch64beAsmBackend(const Target &T,
const Triple &TT, StringRef CPU,
const MCTargetOptions &Options);

MCObjectWriter *createAArch64ELFObjectWriter(raw_pwrite_stream &OS,
uint8_t OSABI,
bool IsLittleEndian,
bool IsILP32);
std::unique_ptr<MCObjectWriter>
createAArch64ELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI,
bool IsLittleEndian, bool IsILP32);

MCObjectWriter *createAArch64MachObjectWriter(raw_pwrite_stream &OS,
uint32_t CPUType,
uint32_t CPUSubtype);
std::unique_ptr<MCObjectWriter>
createAArch64MachObjectWriter(raw_pwrite_stream &OS, uint32_t CPUType,
uint32_t CPUSubtype);

MCObjectWriter *createAArch64WinCOFFObjectWriter(raw_pwrite_stream &OS);
std::unique_ptr<MCObjectWriter>
createAArch64WinCOFFObjectWriter(raw_pwrite_stream &OS);

MCTargetStreamer *createAArch64AsmTargetStreamer(MCStreamer &S,
formatted_raw_ostream &OS,
Expand Down
6 changes: 3 additions & 3 deletions lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ void AArch64MachObjectWriter::recordRelocation(
Writer->addRelocation(RelSymbol, Fragment->getParent(), MRE);
}

MCObjectWriter *llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS,
uint32_t CPUType,
uint32_t CPUSubtype) {
std::unique_ptr<MCObjectWriter>
llvm::createAArch64MachObjectWriter(raw_pwrite_stream &OS, uint32_t CPUType,
uint32_t CPUSubtype) {
return createMachObjectWriter(
llvm::make_unique<AArch64MachObjectWriter>(CPUType, CPUSubtype), OS,
/*IsLittleEndian=*/true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCValue.h"
#include "llvm/MC/MCWinCOFFObjectWriter.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -96,7 +97,8 @@ bool AArch64WinCOFFObjectWriter::recordRelocation(const MCFixup &Fixup) const {

namespace llvm {

MCObjectWriter *createAArch64WinCOFFObjectWriter(raw_pwrite_stream &OS) {
std::unique_ptr<MCObjectWriter>
createAArch64WinCOFFObjectWriter(raw_pwrite_stream &OS) {
auto MOTW = llvm::make_unique<AArch64WinCOFFObjectWriter>();
return createWinCOFFObjectWriter(std::move(MOTW), OS);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Target/AMDGPU/MCTargetDesc/AMDGPUAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
}
}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createAMDGPUELFObjectWriter(Is64Bit, OSABI, HasRelocationAddend, OS);
}
};
Expand Down
9 changes: 5 additions & 4 deletions lib/Target/AMDGPU/MCTargetDesc/AMDGPUELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -81,10 +82,10 @@ unsigned AMDGPUELFObjectWriter::getRelocType(MCContext &Ctx,
llvm_unreachable("unhandled relocation type");
}

MCObjectWriter *llvm::createAMDGPUELFObjectWriter(bool Is64Bit,
uint8_t OSABI,
bool HasRelocationAddend,
raw_pwrite_stream &OS) {
std::unique_ptr<MCObjectWriter>
llvm::createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
bool HasRelocationAddend,
raw_pwrite_stream &OS) {
auto MOTW = llvm::make_unique<AMDGPUELFObjectWriter>(Is64Bit, OSABI,
HasRelocationAddend);
return createELFObjectWriter(std::move(MOTW), OS, true);
Expand Down
9 changes: 5 additions & 4 deletions lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "llvm/Support/DataTypes.h"

#include <memory>

namespace llvm {
class MCAsmBackend;
class MCCodeEmitter;
Expand Down Expand Up @@ -47,10 +49,9 @@ MCAsmBackend *createAMDGPUAsmBackend(const Target &T, const MCRegisterInfo &MRI,
const Triple &TT, StringRef CPU,
const MCTargetOptions &Options);

MCObjectWriter *createAMDGPUELFObjectWriter(bool Is64Bit,
uint8_t OSABI,
bool HasRelocationAddend,
raw_pwrite_stream &OS);
std::unique_ptr<MCObjectWriter>
createAMDGPUELFObjectWriter(bool Is64Bit, uint8_t OSABI,
bool HasRelocationAddend, raw_pwrite_stream &OS);
} // End llvm namespace

#define GET_REGINFO_ENUM
Expand Down
3 changes: 2 additions & 1 deletion lib/Target/ARM/MCTargetDesc/ARMAsmBackendDarwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ARMAsmBackendDarwin : public ARMAsmBackend {
: ARMAsmBackend(T, TT, /* IsLittleEndian */ true), MRI(MRI), Subtype(st) {
}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createARMMachObjectWriter(OS, /*Is64Bit=*/false, MachO::CPU_TYPE_ARM,
Subtype);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Target/ARM/MCTargetDesc/ARMAsmBackendELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ARMAsmBackendELF : public ARMAsmBackend {
bool IsLittle)
: ARMAsmBackend(T, TT, IsLittle), OSABI(OSABI) {}

MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createARMELFObjectWriter(OS, OSABI, isLittle());
}
};
Expand Down
3 changes: 2 additions & 1 deletion lib/Target/ARM/MCTargetDesc/ARMAsmBackendWinCOFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ARMAsmBackendWinCOFF : public ARMAsmBackend {
public:
ARMAsmBackendWinCOFF(const Target &T, const Triple &TheTriple)
: ARMAsmBackend(T, TheTriple, true) {}
MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
std::unique_ptr<MCObjectWriter>
createObjectWriter(raw_pwrite_stream &OS) const override {
return createARMWinCOFFObjectWriter(OS, /*Is64Bit=*/false);
}
};
Expand Down
7 changes: 4 additions & 3 deletions lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -235,9 +236,9 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
}
}

MCObjectWriter *llvm::createARMELFObjectWriter(raw_pwrite_stream &OS,
uint8_t OSABI,
bool IsLittleEndian) {
std::unique_ptr<MCObjectWriter>
llvm::createARMELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI,
bool IsLittleEndian) {
return createELFObjectWriter(llvm::make_unique<ARMELFObjectWriter>(OSABI), OS,
IsLittleEndian);
}
16 changes: 9 additions & 7 deletions lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,19 @@ MCStreamer *createARMWinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB,
bool IncrementalLinkerCompatible);

/// Construct an ELF Mach-O object writer.
MCObjectWriter *createARMELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI,
bool IsLittleEndian);
std::unique_ptr<MCObjectWriter> createARMELFObjectWriter(raw_pwrite_stream &OS,
uint8_t OSABI,
bool IsLittleEndian);

/// Construct an ARM Mach-O object writer.
MCObjectWriter *createARMMachObjectWriter(raw_pwrite_stream &OS, bool Is64Bit,
uint32_t CPUType,
uint32_t CPUSubtype);
std::unique_ptr<MCObjectWriter> createARMMachObjectWriter(raw_pwrite_stream &OS,
bool Is64Bit,
uint32_t CPUType,
uint32_t CPUSubtype);

/// Construct an ARM PE/COFF object writer.
MCObjectWriter *createARMWinCOFFObjectWriter(raw_pwrite_stream &OS,
bool Is64Bit);
std::unique_ptr<MCObjectWriter>
createARMWinCOFFObjectWriter(raw_pwrite_stream &OS, bool Is64Bit);

/// Construct ARM Mach-O relocation info.
MCRelocationInfo *createARMMachORelocationInfo(MCContext &Ctx);
Expand Down
Loading

0 comments on commit e471346

Please sign in to comment.