From 5fd5fe0f7bfac0f7973475fcf7a5f8061d983538 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 5 Jun 2013 02:32:26 +0000 Subject: [PATCH] Move BinaryRef to a new include/llvm/Object/YAML.h file. It will be used for ELF dumping too. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183287 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/COFFYAML.h | 35 ++------------------ include/llvm/Object/YAML.h | 59 ++++++++++++++++++++++++++++++++++ lib/Object/CMakeLists.txt | 1 + lib/Object/COFFYAML.cpp | 17 ---------- lib/Object/YAML.cpp | 34 ++++++++++++++++++++ tools/obj2yaml/coff2yaml.cpp | 4 +-- 6 files changed, 99 insertions(+), 51 deletions(-) create mode 100644 include/llvm/Object/YAML.h create mode 100644 lib/Object/YAML.cpp diff --git a/include/llvm/Object/COFFYAML.h b/include/llvm/Object/COFFYAML.h index 4c20260222f..7e8aefa0500 100644 --- a/include/llvm/Object/COFFYAML.h +++ b/include/llvm/Object/COFFYAML.h @@ -14,9 +14,8 @@ #ifndef LLVM_OBJECT_COFFYAML_H #define LLVM_OBJECT_COFFYAML_H - +#include "llvm/Object/YAML.h" #include "llvm/Support/COFF.h" -#include "llvm/Support/YAMLTraits.h" namespace llvm { @@ -36,32 +35,10 @@ inline SectionCharacteristics operator|(SectionCharacteristics a, // The structure of the yaml files is not an exact 1:1 match to COFF. In order // to use yaml::IO, we use these structures which are closer to the source. namespace COFFYAML { - /// In an object file this is just a binary blob. In an yaml file it is an hex - /// string. Using this avoid having to allocate temporary strings. - /// FIXME: not COFF specific. - class BinaryRef { - ArrayRef Data; - bool isBinary; - public: - BinaryRef(ArrayRef Data) : Data(Data), isBinary(true) {} - BinaryRef(StringRef Data) - : Data(reinterpret_cast(Data.data()), Data.size()), - isBinary(false) {} - BinaryRef() : isBinary(false) {} - StringRef getHex() const { - assert(!isBinary); - return StringRef(reinterpret_cast(Data.data()), Data.size()); - } - ArrayRef getBinary() const { - assert(isBinary); - return Data; - } - }; - struct Section { COFF::section Header; unsigned Alignment; - BinaryRef SectionData; + object::yaml::BinaryRef SectionData; std::vector Relocations; StringRef Name; Section(); @@ -71,7 +48,7 @@ namespace COFFYAML { COFF::symbol Header; COFF::SymbolBaseType SimpleType; COFF::SymbolComplexType ComplexType; - BinaryRef AuxiliaryData; + object::yaml::BinaryRef AuxiliaryData; StringRef Name; Symbol(); }; @@ -92,12 +69,6 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(COFF::relocation) namespace llvm { namespace yaml { -template<> -struct ScalarTraits { - static void output(const COFFYAML::BinaryRef &, void*, llvm::raw_ostream &); - static StringRef input(StringRef, void*, COFFYAML::BinaryRef &); -}; - template <> struct ScalarEnumerationTraits { static void enumeration(IO &IO, COFF::MachineTypes &Value); diff --git a/include/llvm/Object/YAML.h b/include/llvm/Object/YAML.h new file mode 100644 index 00000000000..44d387b84eb --- /dev/null +++ b/include/llvm/Object/YAML.h @@ -0,0 +1,59 @@ +//===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares utility classes for handling the YAML representation of +// object files. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_OBJECT_YAML_H +#define LLVM_OBJECT_YAML_H + +#include "llvm/Support/YAMLTraits.h" + +namespace llvm { +namespace object { +namespace yaml { + +/// In an object file this is just a binary blob. In an yaml file it is an hex +/// string. Using this avoid having to allocate temporary strings. +class BinaryRef { + ArrayRef Data; + bool isBinary; + +public: + BinaryRef(ArrayRef Data) : Data(Data), isBinary(true) {} + BinaryRef(StringRef Data) + : Data(reinterpret_cast(Data.data()), Data.size()), + isBinary(false) {} + BinaryRef() : isBinary(false) {} + StringRef getHex() const { + assert(!isBinary); + return StringRef(reinterpret_cast(Data.data()), Data.size()); + } + ArrayRef getBinary() const { + assert(isBinary); + return Data; + } +}; + +} +} + +namespace yaml { +template <> struct ScalarTraits { + static void output(const object::yaml::BinaryRef &, void *, + llvm::raw_ostream &); + static StringRef input(StringRef, void *, object::yaml::BinaryRef &); +}; +} + +} + +#endif diff --git a/lib/Object/CMakeLists.txt b/lib/Object/CMakeLists.txt index 0ca0c324014..e24f78a598d 100644 --- a/lib/Object/CMakeLists.txt +++ b/lib/Object/CMakeLists.txt @@ -8,4 +8,5 @@ add_llvm_library(LLVMObject MachOObjectFile.cpp Object.cpp ObjectFile.cpp + YAML.cpp ) diff --git a/lib/Object/COFFYAML.cpp b/lib/Object/COFFYAML.cpp index f3883afede4..6e8dad41f9a 100644 --- a/lib/Object/COFFYAML.cpp +++ b/lib/Object/COFFYAML.cpp @@ -229,23 +229,6 @@ struct NType { } -void ScalarTraits::output(const COFFYAML::BinaryRef &Val, - void *, llvm::raw_ostream &Out) { - ArrayRef Data = Val.getBinary(); - for (ArrayRef::iterator I = Data.begin(), E = Data.end(); I != E; - ++I) { - uint8_t Byte = *I; - Out << hexdigit(Byte >> 4); - Out << hexdigit(Byte & 0xf); - } -} - -StringRef ScalarTraits::input(StringRef Scalar, void *, - COFFYAML::BinaryRef &Val) { - Val = COFFYAML::BinaryRef(Scalar); - return StringRef(); -} - void MappingTraits::mapping(IO &IO, COFF::relocation &Rel) { MappingNormalization NT(IO, Rel.Type); diff --git a/lib/Object/YAML.cpp b/lib/Object/YAML.cpp new file mode 100644 index 00000000000..36b19974b0e --- /dev/null +++ b/lib/Object/YAML.cpp @@ -0,0 +1,34 @@ +//===- 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/Object/YAML.h" + +using namespace llvm; + +void yaml::ScalarTraits::output( + const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) { + ArrayRef Data = Val.getBinary(); + for (ArrayRef::iterator I = Data.begin(), E = Data.end(); I != E; + ++I) { + uint8_t Byte = *I; + Out << hexdigit(Byte >> 4); + Out << hexdigit(Byte & 0xf); + } +} + +StringRef yaml::ScalarTraits::input( + StringRef Scalar, void *, object::yaml::BinaryRef &Val) { + Val = object::yaml::BinaryRef(Scalar); + return StringRef(); +} diff --git a/tools/obj2yaml/coff2yaml.cpp b/tools/obj2yaml/coff2yaml.cpp index d1c4d01b68d..909b269385b 100644 --- a/tools/obj2yaml/coff2yaml.cpp +++ b/tools/obj2yaml/coff2yaml.cpp @@ -64,7 +64,7 @@ void COFFDumper::dumpSections(unsigned NumSections) { ArrayRef sectionData; Obj.getSectionContents(Sect, sectionData); - Sec.SectionData = COFFYAML::BinaryRef(sectionData); + Sec.SectionData = object::yaml::BinaryRef(sectionData); std::vector Relocations; for (object::relocation_iterator rIter = iter->begin_relocations(); @@ -96,7 +96,7 @@ void COFFDumper::dumpSymbols(unsigned NumSymbols) { Sym.Header.Value = Symbol->Value; Sym.Header.SectionNumber = Symbol->SectionNumber; Sym.Header.NumberOfAuxSymbols = Symbol->NumberOfAuxSymbols; - Sym.AuxiliaryData = COFFYAML::BinaryRef(Obj.getSymbolAuxData(Symbol)); + Sym.AuxiliaryData = object::yaml::BinaryRef(Obj.getSymbolAuxData(Symbol)); Symbols.push_back(Sym); } } -- 2.34.1