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 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 17 | #include "lld/Platform/Platform.h" |
| 18 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/OwningPtr.h" |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
| 21 | #include "llvm/ADT/StringExtras.h" |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/ArrayRef.h" |
| 23 | #include "llvm/ADT/StringExtras.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/DataTypes.h" |
| 25 | #include "llvm/Support/MemoryBuffer.h" |
| 26 | #include "llvm/Support/system_error.h" |
| 27 | |
| 28 | #include <vector> |
| 29 | |
| 30 | namespace lld { |
| 31 | namespace yaml { |
| 32 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | /// |
| 35 | /// In most cases, atoms names are unambiguous, so references can just |
| 36 | /// use the atom name as the target (e.g. target: foo). But in a few |
| 37 | /// cases that does not work, so ref-names are added. These are labels |
| 38 | /// used only in yaml. The labels do not exist in the Atom model. |
| 39 | /// |
| 40 | /// One need for ref-names are when atoms have no user supplied name |
| 41 | /// (e.g. c-string literal). Another case is when two object files with |
| 42 | /// identically named static functions are merged (ld -r) into one object file. |
| 43 | /// In that case referencing the function by name is ambiguous, so a unique |
| 44 | /// ref-name is added. |
| 45 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 46 | class RefNameBuilder { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 47 | public: |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 48 | RefNameBuilder(const File& file) |
| 49 | : _collisionCount(0), _unnamedCounter(0) { |
| 50 | // visit all atoms |
| 51 | for(File::defined_iterator it=file.definedAtomsBegin(), |
| 52 | end=file.definedAtomsEnd(); |
| 53 | it != end; ++it) { |
| 54 | const DefinedAtom* atom = *it; |
| 55 | // Build map of atoms names to detect duplicates |
| 56 | if ( ! atom->name().empty() ) |
| 57 | buildDuplicateNameMap(*atom); |
| 58 | |
| 59 | // Find references to unnamed atoms and create ref-names for them. |
| 60 | for (auto rit=atom->referencesBegin(), rend=atom->referencesEnd(); |
| 61 | rit != rend; ++rit) { |
| 62 | const Reference* ref = *rit; |
| 63 | // create refname for any unnamed reference target |
| 64 | if ( ref->target()->name().empty() ) { |
| 65 | char* buffer; |
| 66 | asprintf(&buffer, "L%03d", _unnamedCounter++); |
| 67 | _refNames[ref->target()] = buffer; |
| 68 | } |
| 69 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 70 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 71 | for(File::undefined_iterator it=file.undefinedAtomsBegin(), |
| 72 | end=file.undefinedAtomsEnd(); |
| 73 | it != end; ++it) { |
| 74 | buildDuplicateNameMap(**it); |
| 75 | } |
| 76 | for(File::shared_library_iterator it=file.sharedLibraryAtomsBegin(), |
| 77 | end=file.sharedLibraryAtomsEnd(); |
| 78 | it != end; ++it) { |
| 79 | buildDuplicateNameMap(**it); |
| 80 | } |
| 81 | for(File::absolute_iterator it=file.absoluteAtomsBegin(), |
| 82 | end=file.absoluteAtomsEnd(); |
| 83 | it != end; ++it) { |
| 84 | buildDuplicateNameMap(**it); |
| 85 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 86 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 87 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 88 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 89 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 90 | void buildDuplicateNameMap(const Atom& atom) { |
| 91 | assert(!atom.name().empty()); |
| 92 | NameToAtom::iterator pos = _nameMap.find(atom.name()); |
| 93 | if ( pos != _nameMap.end() ) { |
| 94 | // Found name collision, give each a unique ref-name. |
| 95 | char* buffer; |
| 96 | asprintf(&buffer, "%s.%03d", atom.name().data(), ++_collisionCount); |
| 97 | _refNames[&atom] = buffer; |
| 98 | const Atom* prevAtom = pos->second; |
| 99 | AtomToRefName::iterator pos2 = _refNames.find(prevAtom); |
| 100 | if ( pos2 == _refNames.end() ) { |
| 101 | // only create ref-name for previous if none already created |
| 102 | asprintf(&buffer, "%s.%03d", prevAtom->name().data(), ++_collisionCount); |
| 103 | _refNames[prevAtom] = buffer; |
| 104 | } |
| 105 | } |
| 106 | else { |
| 107 | // First time we've seen this name, just add it to map. |
| 108 | _nameMap[atom.name()] = &atom; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bool hasRefName(const Atom* atom) { |
| 113 | return _refNames.count(atom); |
| 114 | } |
| 115 | |
| 116 | const char* refName(const Atom* atom) { |
| 117 | return _refNames.find(atom)->second; |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | struct MyMappingInfo { |
| 122 | static llvm::StringRef getEmptyKey() { return llvm::StringRef(); } |
| 123 | static llvm::StringRef getTombstoneKey() { return llvm::StringRef(" ", 0); } |
| 124 | static unsigned getHashValue(llvm::StringRef const val) { |
| 125 | return llvm::HashString(val); } |
| 126 | static bool isEqual(llvm::StringRef const lhs, |
| 127 | llvm::StringRef const rhs) { return lhs.equals(rhs); } |
| 128 | }; |
| 129 | typedef llvm::DenseMap<llvm::StringRef, const Atom*, MyMappingInfo> NameToAtom; |
| 130 | typedef llvm::DenseMap<const Atom*, const char*> AtomToRefName; |
| 131 | |
| 132 | unsigned int _collisionCount; |
| 133 | unsigned int _unnamedCounter; |
| 134 | NameToAtom _nameMap; |
| 135 | AtomToRefName _refNames; |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | /// |
| 140 | /// Helper class for writeObjectText() to write out atoms in yaml format. |
| 141 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 142 | class AtomWriter { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 143 | public: |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 144 | AtomWriter(const File& file, Platform& platform, RefNameBuilder& rnb) |
| 145 | : _file(file), _platform(platform), _rnb(rnb), _firstAtom(true) { } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 146 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 147 | |
| 148 | void write(llvm::raw_ostream& out) { |
| 149 | // write header |
| 150 | out << "---\n"; |
| 151 | |
| 152 | // visit all atoms |
| 153 | for(File::defined_iterator it=_file.definedAtomsBegin(), |
| 154 | end=_file.definedAtomsEnd(); |
| 155 | it != end; ++it) { |
| 156 | writeDefinedAtom(**it, out); |
| 157 | } |
| 158 | for(File::undefined_iterator it=_file.undefinedAtomsBegin(), |
| 159 | end=_file.undefinedAtomsEnd(); |
| 160 | it != end; ++it) { |
| 161 | writeUndefinedAtom(**it, out); |
| 162 | } |
| 163 | for(File::shared_library_iterator it=_file.sharedLibraryAtomsBegin(), |
| 164 | end=_file.sharedLibraryAtomsEnd(); |
| 165 | it != end; ++it) { |
| 166 | writeSharedLibraryAtom(**it, out); |
| 167 | } |
| 168 | for(File::absolute_iterator it=_file.absoluteAtomsBegin(), |
| 169 | end=_file.absoluteAtomsEnd(); |
| 170 | it != end; ++it) { |
| 171 | writeAbsoluteAtom(**it, out); |
| 172 | } |
| 173 | |
| 174 | out << "...\n"; |
| 175 | } |
| 176 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 177 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 178 | void writeDefinedAtom(const DefinedAtom &atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 179 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 180 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 181 | _firstAtom = false; |
| 182 | } |
| 183 | else { |
| 184 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 185 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 186 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 187 | |
| 188 | bool hasDash = false; |
| 189 | if ( !atom.name().empty() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 190 | out << " - " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 191 | << KeyValues::nameKeyword |
| 192 | << ":" |
| 193 | << spacePadding(KeyValues::nameKeyword) |
| 194 | << atom.name() |
| 195 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 196 | hasDash = true; |
| 197 | } |
| 198 | |
| 199 | if ( _rnb.hasRefName(&atom) ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 200 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 201 | << KeyValues::refNameKeyword |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 202 | << ":" |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 203 | << spacePadding(KeyValues::refNameKeyword) |
| 204 | << _rnb.refName(&atom) |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 205 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 206 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 207 | } |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 208 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 209 | if ( atom.definition() != KeyValues::definitionDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 210 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 211 | << KeyValues::definitionKeyword |
| 212 | << ":" |
| 213 | << spacePadding(KeyValues::definitionKeyword) |
| 214 | << KeyValues::definition(atom.definition()) |
| 215 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 216 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if ( atom.scope() != KeyValues::scopeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 220 | out << (hasDash ? " " : " - ") |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 221 | << KeyValues::scopeKeyword |
| 222 | << ":" |
| 223 | << spacePadding(KeyValues::scopeKeyword) |
| 224 | << KeyValues::scope(atom.scope()) |
| 225 | << "\n"; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 226 | hasDash = true; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 229 | if ( atom.interposable() != KeyValues::interposableDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 230 | out << " " |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 231 | << KeyValues::interposableKeyword |
| 232 | << ":" |
| 233 | << spacePadding(KeyValues::interposableKeyword) |
| 234 | << KeyValues::interposable(atom.interposable()) |
| 235 | << "\n"; |
| 236 | } |
| 237 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 238 | if ( atom.merge() != KeyValues::mergeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 239 | out << " " |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 240 | << KeyValues::mergeKeyword |
| 241 | << ":" |
| 242 | << spacePadding(KeyValues::mergeKeyword) |
| 243 | << KeyValues::merge(atom.merge()) |
| 244 | << "\n"; |
| 245 | } |
| 246 | |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 247 | if ( atom.contentType() != KeyValues::contentTypeDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 248 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 249 | << KeyValues::contentTypeKeyword |
| 250 | << ":" |
| 251 | << spacePadding(KeyValues::contentTypeKeyword) |
| 252 | << KeyValues::contentType(atom.contentType()) |
| 253 | << "\n"; |
| 254 | } |
| 255 | |
| 256 | if ( atom.deadStrip() != KeyValues::deadStripKindDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 257 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 258 | << KeyValues::deadStripKindKeyword |
| 259 | << ":" |
| 260 | << spacePadding(KeyValues::deadStripKindKeyword) |
| 261 | << KeyValues::deadStripKind(atom.deadStrip()) |
| 262 | << "\n"; |
| 263 | } |
| 264 | |
| 265 | if ( atom.sectionChoice() != KeyValues::sectionChoiceDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 266 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 267 | << KeyValues::sectionChoiceKeyword |
| 268 | << ":" |
| 269 | << spacePadding(KeyValues::sectionChoiceKeyword) |
| 270 | << KeyValues::sectionChoice(atom.sectionChoice()) |
| 271 | << "\n"; |
| 272 | assert( ! atom.customSectionName().empty() ); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 273 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 274 | << KeyValues::sectionNameKeyword |
| 275 | << ":" |
| 276 | << spacePadding(KeyValues::sectionNameKeyword) |
| 277 | << atom.customSectionName() |
| 278 | << "\n"; |
| 279 | } |
| 280 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 281 | if ( atom.isThumb() != KeyValues::isThumbDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 282 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 283 | << KeyValues::isThumbKeyword |
| 284 | << ":" |
| 285 | << spacePadding(KeyValues::isThumbKeyword) |
| 286 | << KeyValues::isThumb(atom.isThumb()) |
| 287 | << "\n"; |
| 288 | } |
| 289 | |
| 290 | if ( atom.isAlias() != KeyValues::isAliasDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 291 | out << " " |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 292 | << KeyValues::isAliasKeyword |
| 293 | << ":" |
| 294 | << spacePadding(KeyValues::isAliasKeyword) |
| 295 | << KeyValues::isAlias(atom.isAlias()) |
| 296 | << "\n"; |
| 297 | } |
| 298 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 299 | if ( (atom.contentType() != DefinedAtom::typeZeroFill) |
| 300 | && (atom.size() != 0) ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 301 | out << " " |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 302 | << KeyValues::contentKeyword |
| 303 | << ":" |
| 304 | << spacePadding(KeyValues::contentKeyword) |
| 305 | << "[ "; |
| 306 | llvm::ArrayRef<uint8_t> arr = atom.rawContent(); |
| 307 | bool needComma = false; |
| 308 | for (unsigned int i=0; i < arr.size(); ++i) { |
| 309 | if ( needComma ) |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 310 | out << ", "; |
| 311 | out << hexdigit(arr[i] >> 4); |
| 312 | out << hexdigit(arr[i] & 0x0F); |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 313 | needComma = true; |
| 314 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 315 | out << " ]\n"; |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 318 | bool wroteFirstFixup = false; |
| 319 | for (auto it=atom.referencesBegin(), end=atom.referencesEnd(); |
| 320 | it != end; ++it) { |
| 321 | const Reference* ref = *it; |
| 322 | if ( !wroteFirstFixup ) { |
| 323 | out << " fixups:\n"; |
| 324 | wroteFirstFixup = true; |
| 325 | } |
| 326 | out << " - " |
| 327 | << KeyValues::fixupsOffsetKeyword |
| 328 | << ":" |
| 329 | << spacePadding(KeyValues::fixupsOffsetKeyword) |
| 330 | << ref->offsetInAtom() |
| 331 | << "\n"; |
| 332 | out << " " |
| 333 | << KeyValues::fixupsKindKeyword |
| 334 | << ":" |
| 335 | << spacePadding(KeyValues::fixupsKindKeyword) |
| 336 | << _platform.kindToString(ref->kind()) |
| 337 | << "\n"; |
| 338 | const Atom* target = ref->target(); |
| 339 | if ( target != NULL ) { |
| 340 | llvm::StringRef refName = target->name(); |
| 341 | if ( _rnb.hasRefName(target) ) |
| 342 | refName = _rnb.refName(target); |
| 343 | assert(!refName.empty()); |
| 344 | out << " " |
| 345 | << KeyValues::fixupsTargetKeyword |
| 346 | << ":" |
| 347 | << spacePadding(KeyValues::fixupsTargetKeyword) |
| 348 | << refName |
| 349 | << "\n"; |
| 350 | } |
| 351 | if ( ref->addend() != 0 ) { |
| 352 | out << " " |
| 353 | << KeyValues::fixupsAddendKeyword |
| 354 | << ":" |
| 355 | << spacePadding(KeyValues::fixupsAddendKeyword) |
| 356 | << ref->addend() |
| 357 | << "\n"; |
| 358 | } |
| 359 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 360 | } |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 361 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 362 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 363 | void writeUndefinedAtom(const UndefinedAtom &atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 364 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 365 | out << "atoms:\n"; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 366 | _firstAtom = false; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 367 | } |
| 368 | else { |
| 369 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 370 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 371 | } |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 372 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 373 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 374 | << KeyValues::nameKeyword |
| 375 | << ":" |
| 376 | << spacePadding(KeyValues::nameKeyword) |
| 377 | << atom.name() |
| 378 | << "\n"; |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 379 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 380 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 381 | << KeyValues::definitionKeyword |
| 382 | << ":" |
| 383 | << spacePadding(KeyValues::definitionKeyword) |
| 384 | << KeyValues::definition(atom.definition()) |
| 385 | << "\n"; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 386 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 387 | if ( atom.canBeNull() != KeyValues::canBeNullDefault ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 388 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 389 | << KeyValues::canBeNullKeyword |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 390 | << ":" |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 391 | << spacePadding(KeyValues::canBeNullKeyword) |
| 392 | << KeyValues::canBeNull(atom.canBeNull()) |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 393 | << "\n"; |
| 394 | } |
| 395 | } |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 396 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 397 | void writeSharedLibraryAtom(const SharedLibraryAtom& atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 398 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 399 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 400 | _firstAtom = false; |
| 401 | } |
| 402 | else { |
| 403 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 404 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 407 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 408 | << KeyValues::nameKeyword |
| 409 | << ":" |
| 410 | << spacePadding(KeyValues::nameKeyword) |
| 411 | << atom.name() |
| 412 | << "\n"; |
| 413 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 414 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 415 | << KeyValues::definitionKeyword |
| 416 | << ":" |
| 417 | << spacePadding(KeyValues::definitionKeyword) |
| 418 | << KeyValues::definition(atom.definition()) |
| 419 | << "\n"; |
| 420 | |
| 421 | if ( !atom.loadName().empty() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 422 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 423 | << KeyValues::loadNameKeyword |
| 424 | << ":" |
| 425 | << spacePadding(KeyValues::loadNameKeyword) |
| 426 | << atom.loadName() |
| 427 | << "\n"; |
| 428 | } |
| 429 | |
| 430 | if ( atom.canBeNullAtRuntime() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 431 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 432 | << KeyValues::canBeNullKeyword |
| 433 | << ":" |
| 434 | << spacePadding(KeyValues::canBeNullKeyword) |
| 435 | << KeyValues::canBeNull(UndefinedAtom::canBeNullAtRuntime) |
| 436 | << "\n"; |
| 437 | } |
| 438 | } |
| 439 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 440 | void writeAbsoluteAtom(const AbsoluteAtom& atom, llvm::raw_ostream& out) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 441 | if ( _firstAtom ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 442 | out << "atoms:\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 443 | _firstAtom = false; |
| 444 | } |
| 445 | else { |
| 446 | // add blank line between atoms for readability |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 447 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 450 | out << " - " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 451 | << KeyValues::nameKeyword |
| 452 | << ":" |
| 453 | << spacePadding(KeyValues::nameKeyword) |
| 454 | << atom.name() |
| 455 | << "\n"; |
| 456 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 457 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 458 | << KeyValues::definitionKeyword |
| 459 | << ":" |
| 460 | << spacePadding(KeyValues::definitionKeyword) |
| 461 | << KeyValues::definition(atom.definition()) |
| 462 | << "\n"; |
| 463 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 464 | out << " " |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 465 | << KeyValues::valueKeyword |
| 466 | << ":" |
| 467 | << spacePadding(KeyValues::valueKeyword) |
| 468 | << "0x"; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 469 | out.write_hex(atom.value()); |
| 470 | out << "\n"; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 473 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 474 | private: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 475 | // return a string of the correct number of spaces to align value |
| 476 | const char* spacePadding(const char* key) { |
| 477 | const char* spaces = " "; |
| 478 | assert(strlen(spaces) > strlen(key)); |
| 479 | return &spaces[strlen(key)]; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 482 | char hexdigit(uint8_t nibble) { |
| 483 | if ( nibble < 0x0A ) |
| 484 | return '0' + nibble; |
| 485 | else |
| 486 | return 'A' + nibble - 0x0A; |
| 487 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 488 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 489 | const File& _file; |
| 490 | Platform& _platform; |
| 491 | RefNameBuilder& _rnb; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 492 | bool _firstAtom; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 493 | }; |
| 494 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 495 | } // anonymous namespace |
| 496 | |
| 497 | |
| 498 | |
| 499 | /// |
| 500 | /// writeObjectText - writes the lld::File object as in YAML |
| 501 | /// format to the specified stream. |
| 502 | /// |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 503 | void writeObjectText(const File& file, Platform& platform, |
| 504 | llvm::raw_ostream &out) { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 505 | // Figure what ref-name labels are needed |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 506 | RefNameBuilder rnb(file); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 507 | |
| 508 | // Write out all atoms |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame^] | 509 | AtomWriter writer(file, platform, rnb); |
| 510 | writer.write(out); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | } // namespace yaml |
| 514 | } // namespace lld |