Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- NaClBitcodeMungeUtils.h - Munge bitcode records --------*- C++ -*-===// | 1 //===- NaClBitcodeMungeUtils.h - Munge bitcode records --------*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file defines utility class NaClMungedBitcode to edit a base | 10 // This file defines utility class NaClMungedBitcode to edit a base |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 #ifndef LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H | 74 #ifndef LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H |
| 75 #define LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H | 75 #define LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H |
| 76 | 76 |
| 77 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 77 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 78 | 78 |
| 79 #include <list> | 79 #include <list> |
| 80 #include <map> | 80 #include <map> |
| 81 | 81 |
| 82 namespace llvm { | 82 namespace llvm { |
| 83 | 83 |
| 84 class MemoryBuffer; // Buffer to read bitcode from | |
| 84 class NaClBitcodeAbbrevRecord; // bitcode record. | 85 class NaClBitcodeAbbrevRecord; // bitcode record. |
| 85 class NaClMungedBitcodeIter; // iterator over edited bitcode records. | 86 class NaClMungedBitcodeIter; // iterator over edited bitcode records. |
| 86 | 87 |
| 87 /// \brief Defines a list of bitcode records. | 88 /// \brief Defines a list of bitcode records. |
| 88 typedef std::vector<std::unique_ptr<NaClBitcodeAbbrevRecord>> | 89 typedef std::vector<std::unique_ptr<NaClBitcodeAbbrevRecord>> |
| 89 NaClBitcodeRecordList; | 90 NaClBitcodeRecordList; |
| 90 | 91 |
| 91 /// \brief Extracts out the records in Records, and puts them into RecordList. | 92 /// \brief Extracts out the records in Records, and puts them into RecordList. |
| 92 /// | 93 /// |
| 93 /// \brief RecordList[in/out] Record list to read into. | 94 /// \brief RecordList[in/out] Record list to read into. |
| 94 /// \brief Records Array containing data defining records. | 95 /// \brief Records Array containing data defining records. |
| 95 /// \brief RecordsSize The size of array Records. | 96 /// \brief RecordsSize The size of array Records. |
| 96 /// \brief RecordTerminator The value used to terminate records. | 97 /// \brief RecordTerminator The value used to terminate records. |
| 97 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, | 98 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, |
| 98 const uint64_t Records[], | 99 const uint64_t Records[], |
| 99 size_t RecordsSize, | 100 size_t RecordsSize, |
| 100 uint64_t RecordTerminator); | 101 uint64_t RecordTerminator); |
| 101 | 102 |
| 103 /// \brief Read in the list of records from bitcode in a memory buffer. | |
| 104 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, | |
| 105 std::unique_ptr<MemoryBuffer> InputBuffer); | |
| 106 | |
| 102 /// \brief An edited (i.e. munged) list of bitcode records. Edits are | 107 /// \brief An edited (i.e. munged) list of bitcode records. Edits are |
| 103 /// always relative to the initial list of records. | 108 /// always relative to the initial list of records. |
| 104 class NaClMungedBitcode { | 109 class NaClMungedBitcode { |
| 105 friend class NaClMungedBitcodeIter; | 110 friend class NaClMungedBitcodeIter; |
| 106 NaClMungedBitcode() = delete; | 111 NaClMungedBitcode() = delete; |
| 107 NaClMungedBitcode(const NaClMungedBitcode &) = delete; | 112 NaClMungedBitcode(const NaClMungedBitcode &) = delete; |
| 108 NaClMungedBitcode &operator=(const NaClMungedBitcode &) = delete; | 113 NaClMungedBitcode &operator=(const NaClMungedBitcode &) = delete; |
| 109 | 114 |
| 110 public: | 115 public: |
| 111 /// \brief Iterator over edited records. | 116 /// \brief Iterator over edited records. |
| 112 typedef NaClMungedBitcodeIter iterator; | 117 typedef NaClMungedBitcodeIter iterator; |
| 113 | 118 |
| 119 /// \brief Read in initial list of records from bitcode in a memory buffer. | |
| 120 explicit NaClMungedBitcode(std::unique_ptr<MemoryBuffer> InputBuffer); | |
| 121 | |
| 114 /// \brief Initialize the list of records to be edited. | 122 /// \brief Initialize the list of records to be edited. |
| 115 explicit NaClMungedBitcode(std::unique_ptr<NaClBitcodeRecordList> BaseRecords) | 123 explicit NaClMungedBitcode(std::unique_ptr<NaClBitcodeRecordList> BaseRecords) |
| 116 : BaseRecords(std::move(BaseRecords)) {} | 124 : BaseRecords(std::move(BaseRecords)) {} |
| 117 | 125 |
| 118 /// \brief Initialize the list of records to be edited using | 126 /// \brief Initialize the list of records to be edited using |
| 119 /// array specification. | 127 /// array specification. |
| 120 /// | 128 /// |
| 121 /// \brief Records Array containing data defining records. | 129 /// \brief Records Array containing data defining records. |
| 122 /// \brief RecordsSize The size of array Records. | 130 /// \brief RecordsSize The size of array Records. |
| 123 /// \brief RecordTerminator The value used to terminate records. | 131 /// \brief RecordTerminator The value used to terminate records. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 153 | 161 |
| 154 /// \brief Replace the record at RecordIndex with Record. Note that | 162 /// \brief Replace the record at RecordIndex with Record. Note that |
| 155 /// because the record index is based on the base records, and those | 163 /// because the record index is based on the base records, and those |
| 156 /// records are not modified, this editing action effectively undoes | 164 /// records are not modified, this editing action effectively undoes |
| 157 /// all previous remove/replace editing actions for this index. | 165 /// all previous remove/replace editing actions for this index. |
| 158 void replace(size_t RecordIndex, NaClBitcodeAbbrevRecord &Record); | 166 void replace(size_t RecordIndex, NaClBitcodeAbbrevRecord &Record); |
| 159 | 167 |
| 160 /// \brief Print out the resulting edited list of records. | 168 /// \brief Print out the resulting edited list of records. |
| 161 void print(raw_ostream &Out) const; | 169 void print(raw_ostream &Out) const; |
| 162 | 170 |
| 171 /// Defines set of possible write flags. | |
| 172 struct WriteFlags { | |
| 173 /// True if error recovery should be applied. | |
| 174 bool TryToRecover = false; | |
| 175 /// True if bad abbreviation indices should be saved, rather | |
| 176 /// than trying to error recover. | |
|
jvoung (off chromium)
2015/05/06 22:08:09
What does the Save case do after saving? Abort?
S
Karl
2015/05/07 20:09:18
Updated comment to clarify.
| |
| 177 bool SaveBadAbbrevIndices = false; | |
| 178 }; | |
| 179 | |
| 180 /// Defines how writing performed. | |
|
jvoung (off chromium)
2015/05/06 22:08:09
"Defines how writing *is* performed"?
Karl
2015/05/07 20:09:18
Changed to: Defines the results associated with wr
| |
| 181 struct WriteResults { | |
| 182 /// True if errors occurred during writing. | |
| 183 bool HasErrors = false; | |
| 184 /// True if repairs (via error recovery) were applied. | |
| 185 bool HasRepairs = false; | |
| 186 /// True if saved bad abbreviation indices. | |
| 187 bool SavedBadAbbrevIndices = false; | |
| 188 }; | |
| 189 | |
| 190 /// \brief Write out the edited list of bitcode records using | |
| 191 /// the given buffer. | |
| 192 /// | |
| 193 /// \param Buffer The buffer to write into. | |
| 194 /// \param AddHeader Add header block when true. | |
| 195 /// \param Flags Write flags to use. | |
| 196 /// | |
| 197 /// \return Returns the results of the write. | |
| 198 WriteResults writeMaybeRepair( | |
| 199 SmallVectorImpl<char> &Buffer, bool AddHeader, | |
| 200 const WriteFlags &Flags) const; | |
| 201 | |
| 202 bool write(SmallVectorImpl<char> &Buffer, bool AddHeader, | |
| 203 const WriteFlags &Flags) const { | |
| 204 return !writeMaybeRepair(Buffer, AddHeader, Flags).HasErrors; | |
| 205 } | |
| 206 | |
| 207 bool write(SmallVectorImpl<char> &Buffer, bool AddHeader) const { | |
| 208 WriteFlags Flags; | |
| 209 return write(Buffer, AddHeader, Flags); | |
| 210 } | |
| 211 | |
| 163 /// \brief The types of editing actions that can be applied. | 212 /// \brief The types of editing actions that can be applied. |
| 164 enum EditAction { | 213 enum EditAction { |
| 165 AddBefore, // Insert new record before base record at index. | 214 AddBefore, // Insert new record before base record at index. |
| 166 AddAfter, // Insert new record after base record at index. | 215 AddAfter, // Insert new record after base record at index. |
| 167 Remove, // Remove record at index. | 216 Remove, // Remove record at index. |
| 168 Replace // Replace base record at index with new record. | 217 Replace // Replace base record at index with new record. |
| 169 }; | 218 }; |
| 170 | 219 |
| 171 /// \brief Apply a set of edits defined in the given array. | 220 /// \brief Apply a set of edits defined in the given array. |
| 172 /// | 221 /// |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 183 /// | 232 /// |
| 184 /// \param Munges The array containing the edits to apply. | 233 /// \param Munges The array containing the edits to apply. |
| 185 /// \param MungesSize The size of Munges. | 234 /// \param MungesSize The size of Munges. |
| 186 /// \param Terminator The value used to terminate records in editing actions. | 235 /// \param Terminator The value used to terminate records in editing actions. |
| 187 void munge(const uint64_t Munges[], size_t MungesSize, uint64_t Terminator); | 236 void munge(const uint64_t Munges[], size_t MungesSize, uint64_t Terminator); |
| 188 | 237 |
| 189 /// \brief Removes all editing actions and resets back to the original | 238 /// \brief Removes all editing actions and resets back to the original |
| 190 /// set of base records. | 239 /// set of base records. |
| 191 void removeEdits(); | 240 void removeEdits(); |
| 192 | 241 |
| 242 /// Returns the unedited list of bitcode records. | |
| 243 const NaClBitcodeRecordList &getBaseRecords() const { | |
| 244 return *BaseRecords; | |
| 245 } | |
| 246 | |
| 193 private: | 247 private: |
| 194 typedef std::list<NaClBitcodeAbbrevRecord *> RecordListType; | 248 typedef std::list<NaClBitcodeAbbrevRecord *> RecordListType; |
| 195 typedef std::map<size_t, RecordListType *> InsertionsMapType; | 249 typedef std::map<size_t, RecordListType *> InsertionsMapType; |
| 196 typedef std::map<size_t, NaClBitcodeAbbrevRecord *> ReplaceMapType; | 250 typedef std::map<size_t, NaClBitcodeAbbrevRecord *> ReplaceMapType; |
| 197 | 251 |
| 198 /// \brief The list of base records that will be edited. | 252 /// \brief The list of base records that will be edited. |
| 199 std::unique_ptr<NaClBitcodeRecordList> BaseRecords; | 253 std::unique_ptr<NaClBitcodeRecordList> BaseRecords; |
| 200 // Holds map from record index to list of records added before | 254 // Holds map from record index to list of records added before |
| 201 // the corresponding record in the list of base records. | 255 // the corresponding record in the list of base records. |
| 202 InsertionsMapType BeforeInsertionsMap; | 256 InsertionsMapType BeforeInsertionsMap; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 234 public: | 288 public: |
| 235 /// \brief The abbreviation associated with the bitcode record. | 289 /// \brief The abbreviation associated with the bitcode record. |
| 236 unsigned Abbrev; | 290 unsigned Abbrev; |
| 237 | 291 |
| 238 /// \brief Creates a bitcode record. | 292 /// \brief Creates a bitcode record. |
| 239 /// | 293 /// |
| 240 /// \param Abbrev Abbreviation index associated with record. | 294 /// \param Abbrev Abbreviation index associated with record. |
| 241 /// \param Code The selector code of the record. | 295 /// \param Code The selector code of the record. |
| 242 /// \param Values The values associated with the selector code. | 296 /// \param Values The values associated with the selector code. |
| 243 NaClBitcodeAbbrevRecord(unsigned Abbrev, unsigned Code, | 297 NaClBitcodeAbbrevRecord(unsigned Abbrev, unsigned Code, |
| 244 NaClRecordVector &Values) | 298 const NaClRecordVector &Values) |
| 245 : NaClBitcodeRecordData(Code, Values), Abbrev(Abbrev) {} | 299 : NaClBitcodeRecordData(Code, Values), Abbrev(Abbrev) {} |
| 246 | 300 |
| 247 /// \brief Creates a copy of the given abbreviated bitcode record. | 301 /// \brief Creates a copy of the given abbreviated bitcode record. |
| 248 explicit NaClBitcodeAbbrevRecord(const NaClBitcodeAbbrevRecord &Rcd) | 302 explicit NaClBitcodeAbbrevRecord(const NaClBitcodeAbbrevRecord &Rcd) |
| 249 : NaClBitcodeRecordData(Rcd), Abbrev(Rcd.Abbrev) {} | 303 : NaClBitcodeRecordData(Rcd), Abbrev(Rcd.Abbrev) {} |
| 250 | 304 |
| 251 /// \brief Creates a default abbreviated bitcode record. | 305 /// \brief Creates a default abbreviated bitcode record. |
| 252 NaClBitcodeAbbrevRecord() : Abbrev(naclbitc::UNABBREV_RECORD) {} | 306 NaClBitcodeAbbrevRecord() : Abbrev(naclbitc::UNABBREV_RECORD) {} |
| 253 | 307 |
| 254 ~NaClBitcodeAbbrevRecord() {} | 308 ~NaClBitcodeAbbrevRecord() {} |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 361 } | 415 } |
| 362 | 416 |
| 363 // \brief Moves the iterator to the position of the next edited | 417 // \brief Moves the iterator to the position of the next edited |
| 364 // record. | 418 // record. |
| 365 void updatePosition(); | 419 void updatePosition(); |
| 366 }; | 420 }; |
| 367 | 421 |
| 368 } // end namespace llvm. | 422 } // end namespace llvm. |
| 369 | 423 |
| 370 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H | 424 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H |
| OLD | NEW |