blob: 711fd1ac027e13b8389f5a00d0dfa2f232061653 [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/YamlWriter.cpp - Writes YAML ----------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lld/Core/YamlWriter.h"
11#include "lld/Core/Atom.h"
12#include "lld/Core/File.h"
13#include "lld/Core/Reference.h"
14
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/Support/DataTypes.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/system_error.h"
19
20#include <vector>
21
22namespace lld {
23namespace yaml {
24
25class Handler : public File::AtomHandler {
26public:
27 Handler(llvm::raw_ostream &out) : _out(out) { }
28
29 virtual void doFile(const class File &) { }
30 virtual void doAtom(const class Atom &atom) {
31 _out << " - name: " << atom.name() << "\n";
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000032 _out << " internal-name:" << atom.internalName() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000033 _out << " definition: " << definitionString(atom.definition()) <<"\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000034 _out << " scope: " << scopeString(atom.scope()) << "\n";
35 _out << " type: " << typeString(atom.contentType()) << "\n";
36 if (atom.referencesBegin() != atom.referencesEnd()) {
37 _out << " fixups:\n";
38 for (Reference::iterator it = atom.referencesBegin(),
39 end = atom.referencesEnd(); it != end; ++it) {
40 _out << " - kind: " << it->kind << "\n";
41 _out << " offset: " << it->offsetInAtom << "\n";
42 }
43 }
44
45 }
46
47private:
48 const char *scopeString(Atom::Scope scope) {
49 switch (scope) {
50 case Atom::scopeTranslationUnit:
51 return "static";
52 case Atom::scopeLinkageUnit:
53 return "hidden";
54 case Atom::scopeGlobal:
55 return "global";
56 }
57 return "???";
58 }
59
60 const char *typeString(Atom::ContentType type) {
61 switch (type) {
62 case Atom::typeCode:
63 return "code";
64 case Atom::typeCString:
65 return "c-string";
66 case Atom::typeZeroFill:
67 return "zero-fill";
68 case Atom::typeData:
69 return "data";
70 default:
71 return "???";
72 }
73 }
74
75 const char *definitionString(Atom::Definition def) {
76 switch (def) {
77 case Atom::definitionRegular:
78 return "regular";
79 case Atom::definitionTentative:
80 return "tentative";
81 case Atom::definitionAbsolute:
82 return "absolute";
83 default:
84 return "???";
85 }
86 }
87
88 llvm::raw_ostream &_out;
89};
90
91void writeObjectText(File *file, llvm::raw_ostream &out) {
92 Handler h(out);
93 out << "---\n";
94 out << "atoms:\n";
95 file->forEachAtom(h);
96 out << "...\n";
97}
98
99} // namespace yaml
100} // namespace lld