Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 1 | //===- 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 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 10 | #include "YamlKeyValues.h" |
| 11 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 12 | #include "lld/Core/YamlWriter.h" |
| 13 | #include "lld/Core/Atom.h" |
| 14 | #include "lld/Core/File.h" |
| 15 | #include "lld/Core/Reference.h" |
| 16 | |
| 17 | #include "llvm/ADT/OwningPtr.h" |
| 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | #include "llvm/Support/system_error.h" |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace lld { |
| 25 | namespace yaml { |
| 26 | |
| 27 | class Handler : public File::AtomHandler { |
| 28 | public: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 29 | Handler(llvm::raw_ostream &out) : _out(out), _firstAtom(true) { } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 30 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 31 | virtual void doFile(const class File &) { _firstAtom = true; } |
| 32 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 33 | virtual void doAtom(const class Atom &atom) { |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 34 | // add blank line between atoms for readability |
| 35 | if ( !_firstAtom ) |
| 36 | _out << "\n"; |
| 37 | _firstAtom = false; |
| 38 | |
| 39 | _out << " - " |
| 40 | << KeyValues::nameKeyword |
| 41 | << ":" |
| 42 | << spacePadding(KeyValues::nameKeyword) |
| 43 | << atom.name() |
| 44 | << "\n"; |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 45 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 46 | if ( atom.internalName() != KeyValues::internalNameDefault ) { |
| 47 | _out << " " |
| 48 | << KeyValues::internalNameKeyword |
| 49 | << ":" |
| 50 | << spacePadding(KeyValues::internalNameKeyword) |
| 51 | << KeyValues::internalName(atom.internalName()) |
| 52 | << "\n"; |
| 53 | } |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 54 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 55 | if ( atom.definition() != KeyValues::definitionDefault ) { |
| 56 | _out << " " |
| 57 | << KeyValues::definitionKeyword |
| 58 | << ":" |
| 59 | << spacePadding(KeyValues::definitionKeyword) |
| 60 | << KeyValues::definition(atom.definition()) |
| 61 | << "\n"; |
| 62 | } |
| 63 | |
| 64 | if ( atom.scope() != KeyValues::scopeDefault ) { |
| 65 | _out << " " |
| 66 | << KeyValues::scopeKeyword |
| 67 | << ":" |
| 68 | << spacePadding(KeyValues::scopeKeyword) |
| 69 | << KeyValues::scope(atom.scope()) |
| 70 | << "\n"; |
| 71 | } |
| 72 | |
| 73 | if ( atom.contentType() != KeyValues::contentTypeDefault ) { |
| 74 | _out << " " |
| 75 | << KeyValues::contentTypeKeyword |
| 76 | << ":" |
| 77 | << spacePadding(KeyValues::contentTypeKeyword) |
| 78 | << KeyValues::contentType(atom.contentType()) |
| 79 | << "\n"; |
| 80 | } |
| 81 | |
| 82 | if ( atom.deadStrip() != KeyValues::deadStripKindDefault ) { |
| 83 | _out << " " |
| 84 | << KeyValues::deadStripKindKeyword |
| 85 | << ":" |
| 86 | << spacePadding(KeyValues::deadStripKindKeyword) |
| 87 | << KeyValues::deadStripKind(atom.deadStrip()) |
| 88 | << "\n"; |
| 89 | } |
| 90 | |
| 91 | if ( atom.sectionChoice() != KeyValues::sectionChoiceDefault ) { |
| 92 | _out << " " |
| 93 | << KeyValues::sectionChoiceKeyword |
| 94 | << ":" |
| 95 | << spacePadding(KeyValues::sectionChoiceKeyword) |
| 96 | << KeyValues::sectionChoice(atom.sectionChoice()) |
| 97 | << "\n"; |
| 98 | assert( ! atom.customSectionName().empty() ); |
| 99 | _out << " " |
| 100 | << KeyValues::sectionNameKeyword |
| 101 | << ":" |
| 102 | << spacePadding(KeyValues::sectionNameKeyword) |
| 103 | << atom.customSectionName() |
| 104 | << "\n"; |
| 105 | } |
| 106 | |
| 107 | if ( atom.mergeDuplicates() != KeyValues::mergeDuplicatesDefault ) { |
| 108 | _out << " " |
| 109 | << KeyValues::mergeDuplicatesKeyword |
| 110 | << ":" |
| 111 | << spacePadding(KeyValues::mergeDuplicatesKeyword) |
| 112 | << KeyValues::mergeDuplicates(atom.mergeDuplicates()) |
| 113 | << "\n"; |
| 114 | } |
| 115 | |
| 116 | if ( atom.autoHide() != KeyValues::autoHideDefault ) { |
| 117 | _out << " " |
| 118 | << KeyValues::autoHideKeyword |
| 119 | << ":" |
| 120 | << spacePadding(KeyValues::autoHideKeyword) |
| 121 | << KeyValues::autoHide(atom.autoHide()) |
| 122 | << "\n"; |
| 123 | } |
| 124 | |
| 125 | if ( atom.isThumb() != KeyValues::isThumbDefault ) { |
| 126 | _out << " " |
| 127 | << KeyValues::isThumbKeyword |
| 128 | << ":" |
| 129 | << spacePadding(KeyValues::isThumbKeyword) |
| 130 | << KeyValues::isThumb(atom.isThumb()) |
| 131 | << "\n"; |
| 132 | } |
| 133 | |
| 134 | if ( atom.isAlias() != KeyValues::isAliasDefault ) { |
| 135 | _out << " " |
| 136 | << KeyValues::isAliasKeyword |
| 137 | << ":" |
| 138 | << spacePadding(KeyValues::isAliasKeyword) |
| 139 | << KeyValues::isAlias(atom.isAlias()) |
| 140 | << "\n"; |
| 141 | } |
| 142 | |
| 143 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 144 | if (atom.referencesBegin() != atom.referencesEnd()) { |
| 145 | _out << " fixups:\n"; |
| 146 | for (Reference::iterator it = atom.referencesBegin(), |
| 147 | end = atom.referencesEnd(); it != end; ++it) { |
| 148 | _out << " - kind: " << it->kind << "\n"; |
| 149 | _out << " offset: " << it->offsetInAtom << "\n"; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | private: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 156 | // return a string of the correct number of spaces to align value |
| 157 | const char* spacePadding(const char* key) { |
| 158 | const char* spaces = " "; |
| 159 | assert(strlen(spaces) > strlen(key)); |
| 160 | return &spaces[strlen(key)]; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 163 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 164 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 165 | llvm::raw_ostream& _out; |
| 166 | bool _firstAtom; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 169 | void writeObjectText(File &file, llvm::raw_ostream &out) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 170 | Handler h(out); |
| 171 | out << "---\n"; |
| 172 | out << "atoms:\n"; |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 173 | file.forEachAtom(h); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 174 | out << "...\n"; |
| 175 | } |
| 176 | |
| 177 | } // namespace yaml |
| 178 | } // namespace lld |