diff options
Diffstat (limited to 'llvm/lib/MC')
-rw-r--r-- | llvm/lib/MC/CMakeLists.txt | 2 | ||||
-rw-r--r-- | llvm/lib/MC/ELFObjectWriter.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/MC/LLVMBuild.txt | 2 | ||||
-rw-r--r-- | llvm/lib/MC/MCAnalysis/MCModuleYAML.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/MC/StringTableBuilder.cpp | 51 | ||||
-rw-r--r-- | llvm/lib/MC/YAML.cpp | 65 |
6 files changed, 122 insertions, 4 deletions
diff --git a/llvm/lib/MC/CMakeLists.txt b/llvm/lib/MC/CMakeLists.txt index f62894cab39..330519ece00 100644 --- a/llvm/lib/MC/CMakeLists.txt +++ b/llvm/lib/MC/CMakeLists.txt @@ -43,9 +43,11 @@ add_llvm_library(LLVMMC MCValue.cpp MCWin64EH.cpp MachObjectWriter.cpp + StringTableBuilder.cpp SubtargetFeature.cpp WinCOFFObjectWriter.cpp WinCOFFStreamer.cpp + YAML.cpp ) add_subdirectory(MCAnalysis) diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 87f6ec0f3d1..ead05351458 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -28,7 +28,7 @@ #include "llvm/MC/MCObjectWriter.h" #include "llvm/MC/MCSectionELF.h" #include "llvm/MC/MCValue.h" -#include "llvm/Object/StringTableBuilder.h" +#include "llvm/MC/StringTableBuilder.h" #include "llvm/Support/Compression.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h" diff --git a/llvm/lib/MC/LLVMBuild.txt b/llvm/lib/MC/LLVMBuild.txt index da9995d6c57..3fcb50b97c6 100644 --- a/llvm/lib/MC/LLVMBuild.txt +++ b/llvm/lib/MC/LLVMBuild.txt @@ -22,4 +22,4 @@ subdirectories = MCAnalysis MCDisassembler MCParser type = Library name = MC parent = Libraries -required_libraries = Object Support +required_libraries = Support diff --git a/llvm/lib/MC/MCAnalysis/MCModuleYAML.cpp b/llvm/lib/MC/MCAnalysis/MCModuleYAML.cpp index c51c62e928d..876b06de9c9 100644 --- a/llvm/lib/MC/MCAnalysis/MCModuleYAML.cpp +++ b/llvm/lib/MC/MCAnalysis/MCModuleYAML.cpp @@ -17,7 +17,7 @@ #include "llvm/MC/MCAnalysis/MCFunction.h" #include "llvm/MC/MCInstrInfo.h" #include "llvm/MC/MCRegisterInfo.h" -#include "llvm/Object/YAML.h" +#include "llvm/MC/YAML.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Casting.h" #include "llvm/Support/MathExtras.h" @@ -102,7 +102,7 @@ struct Atom { uint64_t Size; std::vector<Inst> Insts; - object::yaml::BinaryRef Data; + yaml::BinaryRef Data; }; struct BasicBlock { diff --git a/llvm/lib/MC/StringTableBuilder.cpp b/llvm/lib/MC/StringTableBuilder.cpp new file mode 100644 index 00000000000..db58ece5c86 --- /dev/null +++ b/llvm/lib/MC/StringTableBuilder.cpp @@ -0,0 +1,51 @@ +//===-- StringTableBuilder.cpp - String table building utility ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/StringTableBuilder.h" +#include "llvm/ADT/SmallVector.h" + +using namespace llvm; + +static bool compareBySuffix(StringRef a, StringRef b) { + size_t sizeA = a.size(); + size_t sizeB = b.size(); + size_t len = std::min(sizeA, sizeB); + for (size_t i = 0; i < len; ++i) { + char ca = a[sizeA - i - 1]; + char cb = b[sizeB - i - 1]; + if (ca != cb) + return ca > cb; + } + return sizeA > sizeB; +} + +void StringTableBuilder::finalize() { + SmallVector<StringRef, 8> Strings; + for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i) + Strings.push_back(i->getKey()); + + std::sort(Strings.begin(), Strings.end(), compareBySuffix); + + // FIXME: Starting with a null byte is ELF specific. Generalize this so we + // can use the class with other object formats. + StringTable += '\x00'; + + StringRef Previous; + for (StringRef s : Strings) { + if (Previous.endswith(s)) { + StringIndexMap[s] = StringTable.size() - 1 - s.size(); + continue; + } + + StringIndexMap[s] = StringTable.size(); + StringTable += s; + StringTable += '\x00'; + Previous = s; + } +} diff --git a/llvm/lib/MC/YAML.cpp b/llvm/lib/MC/YAML.cpp new file mode 100644 index 00000000000..067e91a26d3 --- /dev/null +++ b/llvm/lib/MC/YAML.cpp @@ -0,0 +1,65 @@ +//===- YAML.cpp - YAMLIO utilities for object files -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines utility classes for handling the YAML representation of +// object files. +// +//===----------------------------------------------------------------------===// + +#include "llvm/MC/YAML.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/raw_ostream.h" +#include <cctype> + +using namespace llvm; + +void yaml::ScalarTraits<yaml::BinaryRef>::output( + const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) { + Val.writeAsHex(Out); +} + +StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *, + yaml::BinaryRef &Val) { + if (Scalar.size() % 2 != 0) + return "BinaryRef hex string must contain an even number of nybbles."; + // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here? + // (e.g. a caret pointing to the offending character). + for (unsigned I = 0, N = Scalar.size(); I != N; ++I) + if (!isxdigit(Scalar[I])) + return "BinaryRef hex string must contain only hex digits."; + Val = yaml::BinaryRef(Scalar); + return StringRef(); +} + +void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const { + if (!DataIsHexString) { + OS.write((const char *)Data.data(), Data.size()); + return; + } + for (unsigned I = 0, N = Data.size(); I != N; I += 2) { + uint8_t Byte; + StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte); + OS.write(Byte); + } +} + +void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const { + if (binary_size() == 0) + return; + if (DataIsHexString) { + OS.write((const char *)Data.data(), Data.size()); + return; + } + for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E; + ++I) { + uint8_t Byte = *I; + OS << hexdigit(Byte >> 4); + OS << hexdigit(Byte & 0xf); + } +} |