Refactor wasm/WriterUtil.{cpp,h}.
Summary:
- Makes code more in line with LLVM style (e.g. const char * -> StringRef)
- Do not use formatv since we can construct message just by `+`
- Replace some odd types such as `const StringRef` with more common type
- Do not use default arguments if they are not necessary
Reviewers: sbc100
Subscribers: jfb, aheejin, llvm-commits, sunfish
Differential Revision: https://reviews.llvm.org/D43403
llvm-svn: 325383
diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index b34a2c0..b952a45 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -15,6 +15,7 @@
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Threads.h"
#include "llvm/ADT/Twine.h"
+#include "llvm/Support/LEB128.h"
#define DEBUG_TYPE "lld"
@@ -72,7 +73,7 @@
void OutputSection::createHeader(size_t BodySize) {
raw_string_ostream OS(Header);
debugWrite(OS.tell(), "section type [" + Twine(getSectionName()) + "]");
- writeUleb128(OS, Type, nullptr);
+ encodeULEB128(Type, OS);
writeUleb128(OS, BodySize, "section size");
OS.flush();
log("createHeader: " + toString(*this) + " body=" + Twine(BodySize) +
diff --git a/lld/wasm/OutputSections.h b/lld/wasm/OutputSections.h
index 7a30361..335f1a2 100644
--- a/lld/wasm/OutputSections.h
+++ b/lld/wasm/OutputSections.h
@@ -60,7 +60,7 @@
SyntheticSection(uint32_t Type, std::string Name = "")
: OutputSection(Type, Name), BodyOutputStream(Body) {
if (!Name.empty())
- writeStr(BodyOutputStream, Name);
+ writeStr(BodyOutputStream, Name, "section name");
}
void writeTo(uint8_t *Buf) override {
@@ -96,8 +96,8 @@
std::string getSectionName() const;
void writeToStream(raw_ostream &OS) {
- writeBytes(OS, Header.data(), Header.size());
- writeBytes(OS, Body.data(), Body.size());
+ OS.write(Header.data(), Header.size());
+ OS.write(Body.data(), Body.size());
}
};
diff --git a/lld/wasm/WriterUtils.cpp b/lld/wasm/WriterUtils.cpp
index aba6f0f..561d970 100644
--- a/lld/wasm/WriterUtils.cpp
+++ b/lld/wasm/WriterUtils.cpp
@@ -13,7 +13,6 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/EndianStream.h"
-#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/LEB128.h"
#define DEBUG_TYPE "lld"
@@ -39,49 +38,43 @@
namespace lld {
-void wasm::debugWrite(uint64_t Offset, Twine Msg) {
- DEBUG(dbgs() << format(" | %08" PRIx64 ": ", Offset) << Msg << "\n");
+void wasm::debugWrite(uint64_t Offset, const Twine &Msg) {
+ DEBUG(dbgs() << format(" | %08lld: ", Offset) << Msg << "\n");
}
-void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, const char *Msg) {
- if (Msg)
- debugWrite(OS.tell(), Msg + formatv(" [{0:x}]", Number));
+void wasm::writeUleb128(raw_ostream &OS, uint32_t Number, StringRef Msg) {
+ debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]");
encodeULEB128(Number, OS);
}
-void wasm::writeSleb128(raw_ostream &OS, int32_t Number, const char *Msg) {
- if (Msg)
- debugWrite(OS.tell(), Msg + formatv(" [{0:x}]", Number));
+void wasm::writeSleb128(raw_ostream &OS, int32_t Number, StringRef Msg) {
+ debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]");
encodeSLEB128(Number, OS);
}
void wasm::writeBytes(raw_ostream &OS, const char *Bytes, size_t Count,
- const char *Msg) {
- if (Msg)
- debugWrite(OS.tell(), Msg + formatv(" [data[{0}]]", Count));
+ StringRef Msg) {
+ debugWrite(OS.tell(), Msg + " [data[" + Twine(Count) + "]]");
OS.write(Bytes, Count);
}
-void wasm::writeStr(raw_ostream &OS, const StringRef String, const char *Msg) {
- if (Msg)
- debugWrite(OS.tell(),
- Msg + formatv(" [str[{0}]: {1}]", String.size(), String));
- writeUleb128(OS, String.size(), nullptr);
- writeBytes(OS, String.data(), String.size());
+void wasm::writeStr(raw_ostream &OS, StringRef String, StringRef Msg) {
+ debugWrite(OS.tell(),
+ Msg + " [str[" + Twine(String.size()) + "]: " + String + "]");
+ encodeULEB128(String.size(), OS);
+ OS.write(String.data(), String.size());
}
-void wasm::writeU8(raw_ostream &OS, uint8_t byte, const char *Msg) {
- OS << byte;
-}
+void wasm::writeU8(raw_ostream &OS, uint8_t byte, StringRef Msg) { OS << byte; }
-void wasm::writeU32(raw_ostream &OS, uint32_t Number, const char *Msg) {
- debugWrite(OS.tell(), Msg + formatv("[{0:x}]", Number));
+void wasm::writeU32(raw_ostream &OS, uint32_t Number, StringRef Msg) {
+ debugWrite(OS.tell(), Msg + "[" + utohexstr(Number) + "]");
support::endian::Writer<support::little>(OS).write(Number);
}
-void wasm::writeValueType(raw_ostream &OS, int32_t Type, const char *Msg) {
- debugWrite(OS.tell(), Msg + formatv("[type: {0}]", valueTypeToString(Type)));
- writeSleb128(OS, Type, nullptr);
+void wasm::writeValueType(raw_ostream &OS, int32_t Type, StringRef Msg) {
+ debugWrite(OS.tell(), Msg + "[type: " + valueTypeToString(Type) + "]");
+ encodeSLEB128(Type, OS);
}
void wasm::writeSig(raw_ostream &OS, const WasmSignature &Sig) {
diff --git a/lld/wasm/WriterUtils.h b/lld/wasm/WriterUtils.h
index 1b338a0..a0557ec 100644
--- a/lld/wasm/WriterUtils.h
+++ b/lld/wasm/WriterUtils.h
@@ -10,6 +10,7 @@
#ifndef LLD_WASM_WRITERUTILS_H
#define LLD_WASM_WRITERUTILS_H
+#include "lld/Common/LLVM.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Object/Wasm.h"
#include "llvm/Support/raw_ostream.h"
@@ -36,23 +37,21 @@
uint32_t Value;
};
-void debugWrite(uint64_t Offset, llvm::Twine Msg);
+void debugWrite(uint64_t Offset, const Twine &Msg);
-void writeUleb128(raw_ostream &OS, uint32_t Number, const char *Msg);
+void writeUleb128(raw_ostream &OS, uint32_t Number, StringRef Msg);
-void writeSleb128(raw_ostream &OS, int32_t Number, const char *Msg);
+void writeSleb128(raw_ostream &OS, int32_t Number, StringRef Msg);
-void writeBytes(raw_ostream &OS, const char *Bytes, size_t count,
- const char *Msg = nullptr);
+void writeBytes(raw_ostream &OS, const char *Bytes, size_t count, StringRef Msg);
-void writeStr(raw_ostream &OS, const llvm::StringRef String,
- const char *Msg = nullptr);
+void writeStr(raw_ostream &OS, StringRef String, StringRef Msg);
-void writeU8(raw_ostream &OS, uint8_t byte, const char *Msg);
+void writeU8(raw_ostream &OS, uint8_t byte, StringRef Msg);
-void writeU32(raw_ostream &OS, uint32_t Number, const char *Msg);
+void writeU32(raw_ostream &OS, uint32_t Number, StringRef Msg);
-void writeValueType(raw_ostream &OS, int32_t Type, const char *Msg);
+void writeValueType(raw_ostream &OS, int32_t Type, StringRef Msg);
void writeSig(raw_ostream &OS, const llvm::wasm::WasmSignature &Sig);