blob: e5711068c7c03a25dd308d4b587a448b54fa4633 [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) {
Nick Kledzik38eec3d2011-12-22 02:38:01 +000031 _out << " - name: " << atom.name() << "\n";
32
33 if ( atom.internalName() )
34 _out << " internal-name: true\n";
35
36 if ( atom.definition() != Atom::definitionRegular )
37 _out << " definition: " << definitionString(atom.definition()) <<"\n";
38
39 if ( atom.scope() != Atom::scopeTranslationUnit )
40 _out << " scope: " << scopeString(atom.scope()) << "\n";
41
42 _out << " type: " << typeString(atom.contentType()) << "\n";
43
44 if ( atom.mergeDuplicates() )
45 _out << " merge-duplicates: true\n";
46
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000047 if (atom.referencesBegin() != atom.referencesEnd()) {
48 _out << " fixups:\n";
49 for (Reference::iterator it = atom.referencesBegin(),
50 end = atom.referencesEnd(); it != end; ++it) {
51 _out << " - kind: " << it->kind << "\n";
52 _out << " offset: " << it->offsetInAtom << "\n";
53 }
54 }
55
56 }
57
58private:
59 const char *scopeString(Atom::Scope scope) {
60 switch (scope) {
61 case Atom::scopeTranslationUnit:
62 return "static";
63 case Atom::scopeLinkageUnit:
64 return "hidden";
65 case Atom::scopeGlobal:
66 return "global";
67 }
68 return "???";
69 }
70
71 const char *typeString(Atom::ContentType type) {
72 switch (type) {
73 case Atom::typeCode:
74 return "code";
75 case Atom::typeCString:
76 return "c-string";
77 case Atom::typeZeroFill:
78 return "zero-fill";
79 case Atom::typeData:
80 return "data";
81 default:
82 return "???";
83 }
84 }
85
86 const char *definitionString(Atom::Definition def) {
87 switch (def) {
88 case Atom::definitionRegular:
89 return "regular";
Nick Kledzik38eec3d2011-12-22 02:38:01 +000090 case Atom::definitionWeak:
91 return "weak";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000092 case Atom::definitionTentative:
93 return "tentative";
94 case Atom::definitionAbsolute:
95 return "absolute";
96 default:
97 return "???";
98 }
99 }
100
101 llvm::raw_ostream &_out;
102};
103
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000104void writeObjectText(File &file, llvm::raw_ostream &out) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000105 Handler h(out);
106 out << "---\n";
107 out << "atoms:\n";
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000108 file.forEachAtom(h);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000109 out << "...\n";
110}
111
112} // namespace yaml
113} // namespace lld