[lld] r325383 - Refactor wasm/WriterUtil.{cpp,h}.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 16 12:38:00 PST 2018
Author: ruiu
Date: Fri Feb 16 12:38:00 2018
New Revision: 325383
URL: http://llvm.org/viewvc/llvm-project?rev=325383&view=rev
Log:
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
Modified:
lld/trunk/wasm/OutputSections.cpp
lld/trunk/wasm/OutputSections.h
lld/trunk/wasm/WriterUtils.cpp
lld/trunk/wasm/WriterUtils.h
Modified: lld/trunk/wasm/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/OutputSections.cpp?rev=325383&r1=325382&r2=325383&view=diff
==============================================================================
--- lld/trunk/wasm/OutputSections.cpp (original)
+++ lld/trunk/wasm/OutputSections.cpp Fri Feb 16 12:38:00 2018
@@ -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 @@ std::string SubSection::getSectionName()
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) +
Modified: lld/trunk/wasm/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/OutputSections.h?rev=325383&r1=325382&r2=325383&view=diff
==============================================================================
--- lld/trunk/wasm/OutputSections.h (original)
+++ lld/trunk/wasm/OutputSections.h Fri Feb 16 12:38:00 2018
@@ -60,7 +60,7 @@ public:
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 @@ public:
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());
}
};
Modified: lld/trunk/wasm/WriterUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/WriterUtils.cpp?rev=325383&r1=325382&r2=325383&view=diff
==============================================================================
--- lld/trunk/wasm/WriterUtils.cpp (original)
+++ lld/trunk/wasm/WriterUtils.cpp Fri Feb 16 12:38:00 2018
@@ -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 @@ static const char *valueTypeToString(int
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) {
Modified: lld/trunk/wasm/WriterUtils.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/WriterUtils.h?rev=325383&r1=325382&r2=325383&view=diff
==============================================================================
--- lld/trunk/wasm/WriterUtils.h (original)
+++ lld/trunk/wasm/WriterUtils.h Fri Feb 16 12:38:00 2018
@@ -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 @@ struct OutputRelocation {
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);
More information about the llvm-commits
mailing list