[llvm] [GOFF] Introduce GOFFWriter class (PR #131216)

Kai Nacke via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 13 13:59:17 PDT 2025


https://github.com/redstar created https://github.com/llvm/llvm-project/pull/131216

The GOFFWriter has 2 purposes:
- Simplify resource management
- Enable writing of split DWARF files

It follows the design of the other writer classes. No added functionality at this point.

>From d3350544d0189509c3b505db1c68c2ced59d4dad Mon Sep 17 00:00:00 2001
From: Kai Nacke <kai.peter.nacke at ibm.com>
Date: Thu, 13 Mar 2025 09:30:22 -0400
Subject: [PATCH] [GOFF] Introduce GOFFWriter class

The GOFFWriter has 2 purposes:
- Simplify resource management
- Enable writing of split DWARF files

It follows the design of the other writer classes. No added functionality at this point.
---
 llvm/lib/MC/GOFFObjectWriter.cpp | 64 ++++++++++++++++++++------------
 1 file changed, 41 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/MC/GOFFObjectWriter.cpp b/llvm/lib/MC/GOFFObjectWriter.cpp
index 85deebd89d1f6..4ee8e1487751f 100644
--- a/llvm/lib/MC/GOFFObjectWriter.cpp
+++ b/llvm/lib/MC/GOFFObjectWriter.cpp
@@ -223,34 +223,23 @@ void GOFFOstream::finalizeRecord() {
 }
 
 namespace {
-
-class GOFFObjectWriter : public MCObjectWriter {
-  // The target specific GOFF writer instance.
-  std::unique_ptr<MCGOFFObjectTargetWriter> TargetObjectWriter;
-
-  // The stream used to write the GOFF records.
+class GOFFWriter {
   GOFFOstream OS;
+  [[maybe_unused]] MCAssembler &Asm;
 
-public:
-  GOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
-                   raw_pwrite_stream &OS)
-      : TargetObjectWriter(std::move(MOTW)), OS(OS) {}
-
-  ~GOFFObjectWriter() override {}
-
-  // Write GOFF records.
   void writeHeader();
   void writeEnd();
 
-  // Implementation of the MCObjectWriter interface.
-  void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
-                        const MCFixup &Fixup, MCValue Target,
-                        uint64_t &FixedValue) override {}
-  uint64_t writeObject(MCAssembler &Asm) override;
+public:
+  GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm);
+  uint64_t writeObject();
 };
-} // end anonymous namespace
+} // namespace
+
+GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm)
+    : OS(OS), Asm(Asm) {}
 
-void GOFFObjectWriter::writeHeader() {
+void GOFFWriter::writeHeader() {
   OS.newRecord(GOFF::RT_HDR);
   OS.write_zeros(1);       // Reserved
   OS.writebe<uint32_t>(0); // Target Hardware Environment
@@ -264,7 +253,7 @@ void GOFFObjectWriter::writeHeader() {
   OS.write_zeros(6);       // Reserved
 }
 
-void GOFFObjectWriter::writeEnd() {
+void GOFFWriter::writeEnd() {
   uint8_t F = GOFF::END_EPR_None;
   uint8_t AMODE = 0;
   uint32_t ESDID = 0;
@@ -282,7 +271,7 @@ void GOFFObjectWriter::writeEnd() {
   OS.writebe<uint32_t>(ESDID); // ESDID (of entry point)
 }
 
-uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
+uint64_t GOFFWriter::writeObject() {
   writeHeader();
   writeEnd();
 
@@ -295,6 +284,35 @@ uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
   return OS.getWrittenSize();
 }
 
+namespace {
+
+class GOFFObjectWriter : public MCObjectWriter {
+  // The target specific GOFF writer instance.
+  std::unique_ptr<MCGOFFObjectTargetWriter> TargetObjectWriter;
+
+  // The stream used to write the GOFF records.
+  raw_pwrite_stream &OS;
+
+public:
+  GOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
+                   raw_pwrite_stream &OS)
+      : TargetObjectWriter(std::move(MOTW)), OS(OS) {}
+
+  ~GOFFObjectWriter() override {}
+
+  // Implementation of the MCObjectWriter interface.
+  void recordRelocation(MCAssembler &Asm, const MCFragment *Fragment,
+                        const MCFixup &Fixup, MCValue Target,
+                        uint64_t &FixedValue) override {}
+  uint64_t writeObject(MCAssembler &Asm) override;
+};
+} // end anonymous namespace
+
+uint64_t GOFFObjectWriter::writeObject(MCAssembler &Asm) {
+  uint64_t Size = GOFFWriter(OS, Asm).writeObject();
+  return Size;
+}
+
 std::unique_ptr<MCObjectWriter>
 llvm::createGOFFObjectWriter(std::unique_ptr<MCGOFFObjectTargetWriter> MOTW,
                              raw_pwrite_stream &OS) {



More information about the llvm-commits mailing list