OLD | NEW |
(Empty) | |
| 1 //===- NaClBitCodes.h - Enum values for the bitcode format ------*- C++ -*-===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This header Bitcode enum values. |
| 11 // |
| 12 // The enum values defined in this file should be considered permanent. If |
| 13 // new features are added, they should have values added at the end of the |
| 14 // respective lists. |
| 15 // |
| 16 //===----------------------------------------------------------------------===// |
| 17 |
| 18 #ifndef LLVM_BITCODE_NACL_NACLBITCODES_H |
| 19 #define LLVM_BITCODE_NACL_NACLBITCODES_H |
| 20 |
| 21 #include "llvm/ADT/SmallVector.h" |
| 22 #include "llvm/Support/DataTypes.h" |
| 23 #include "llvm/Support/ErrorHandling.h" |
| 24 #include "llvm/Support/MathExtras.h" |
| 25 #include <cassert> |
| 26 |
| 27 namespace llvm { |
| 28 class raw_ostream; |
| 29 |
| 30 namespace naclbitc { |
| 31 enum StandardWidths { |
| 32 BlockIDWidth = 8, // We use VBR-8 for block IDs. |
| 33 CodeLenWidth = 4, // Codelen are VBR-4. |
| 34 BlockSizeWidth = 32, // BlockSize up to 2^32 32-bit words = 16GB per block. |
| 35 MaxAbbrevWidth = 32 // Maximum value allowed for Fixed and VBR. |
| 36 }; |
| 37 |
| 38 // The standard abbrev namespace always has a way to exit a block, enter a |
| 39 // nested block, define abbrevs, and define an unabbreviated record. |
| 40 enum FixedAbbrevIDs { |
| 41 END_BLOCK = 0, // Must be zero to guarantee termination for broken bitcode. |
| 42 ENTER_SUBBLOCK = 1, |
| 43 |
| 44 /// DEFINE_ABBREV - Defines an abbrev for the current block. It consists |
| 45 /// of a vbr5 for # operand infos. Each operand info is emitted with a |
| 46 /// single bit to indicate if it is a literal encoding. If so, the value is |
| 47 /// emitted with a vbr8. If not, the encoding is emitted as 3 bits followed |
| 48 /// by the info value as a vbr5 if needed. |
| 49 DEFINE_ABBREV = 2, |
| 50 |
| 51 // UNABBREV_RECORDs are emitted with a vbr6 for the record code, followed by |
| 52 // a vbr6 for the # operands, followed by vbr6's for each operand. |
| 53 UNABBREV_RECORD = 3, |
| 54 |
| 55 // This is not a code, this is a marker for the first abbrev assignment. |
| 56 // In addition, we assume up to two additional enumerated constants are |
| 57 // added for each extension. These constants are: |
| 58 // |
| 59 // PREFIX_MAX_FIXED_ABBREV |
| 60 // PREFIX_MAX_ABBREV |
| 61 // |
| 62 // PREFIX_MAX_ABBREV defines the maximal enumeration value used for |
| 63 // the code selector of a block. If Both PREFIX_MAX_FIXED_ABBREV |
| 64 // and PREFIX_MAX_ABBREV is defined, then PREFIX_MAX_FIXED_ABBREV |
| 65 // defines the last code selector of the block that must be read using |
| 66 // a single read (i.e. a FIXED read, or the first chunk of a VBR read. |
| 67 FIRST_APPLICATION_ABBREV = 4, |
| 68 // Defines default values for code length, if no additional selectors |
| 69 // are added. |
| 70 DEFAULT_MAX_ABBREV = FIRST_APPLICATION_ABBREV-1 |
| 71 }; |
| 72 |
| 73 /// StandardBlockIDs - All bitcode files can optionally include a BLOCKINFO |
| 74 /// block, which contains metadata about other blocks in the file. |
| 75 enum StandardBlockIDs { |
| 76 /// BLOCKINFO_BLOCK is used to define metadata about blocks, for example, |
| 77 /// standard abbrevs that should be available to all blocks of a specified |
| 78 /// ID. |
| 79 BLOCKINFO_BLOCK_ID = 0, |
| 80 |
| 81 // Block IDs 1-7 are reserved for future expansion. |
| 82 FIRST_APPLICATION_BLOCKID = 8 |
| 83 }; |
| 84 |
| 85 /// BlockInfoCodes - The blockinfo block contains metadata about user-defined |
| 86 /// blocks. |
| 87 enum BlockInfoCodes { |
| 88 // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd |
| 89 // block, instead of the BlockInfo block. |
| 90 |
| 91 BLOCKINFO_CODE_SETBID = 1, // SETBID: [blockid#] |
| 92 // The following two codes were removed |
| 93 // because the PNaCl reader could read |
| 94 // them, but couldn't be generated by |
| 95 // the writer. |
| 96 BLOCKINFO_CODE_BLOCKNAME = 2, // Not used in PNaCl. |
| 97 BLOCKINFO_CODE_SETRECORDNAME = 3 // Not used in PNaCl. |
| 98 }; |
| 99 |
| 100 } // End naclbitc namespace |
| 101 |
| 102 /// NaClBitCodeAbbrevOp - This describes one or more operands in an abbreviation
. |
| 103 /// This is actually a union of two different things: |
| 104 /// 1. It could be a literal integer value ("the operand is always 17"). |
| 105 /// 2. It could be an encoding specification ("this operand encoded like so"). |
| 106 /// |
| 107 class NaClBitCodeAbbrevOp { |
| 108 public: |
| 109 enum Encoding { |
| 110 Literal = 0, // Value is literal value. |
| 111 Fixed = 1, // A fixed width field, Val specifies number of bits. |
| 112 VBR = 2, // A VBR field where Val specifies the width of each chunk. |
| 113 Array = 3, // A sequence of fields, next field species elt encoding. |
| 114 Char6 = 4, // A 6-bit fixed field which maps to [a-zA-Z0-9._]. |
| 115 Encoding_MAX = Char6 |
| 116 }; |
| 117 |
| 118 explicit NaClBitCodeAbbrevOp(uint64_t V) : Enc(Literal), Val(V) {} |
| 119 explicit NaClBitCodeAbbrevOp(Encoding E, uint64_t Data = 0); |
| 120 |
| 121 Encoding getEncoding() const { return Enc; } |
| 122 |
| 123 static bool isValidEncoding(uint64_t Enc) { return Enc <= Encoding_MAX; } |
| 124 |
| 125 uint64_t getValue() const { return Val; } |
| 126 |
| 127 bool hasValue() const { |
| 128 return hasValue(Enc); |
| 129 } |
| 130 static bool hasValue(Encoding E) { |
| 131 return E <= Encoding_MAX && HasValueArray[E]; |
| 132 } |
| 133 |
| 134 bool isValid() const { return isValid(Enc, Val); } |
| 135 static bool isValid(Encoding E, uint64_t Val); |
| 136 static bool isValid(Encoding E) { return isValid(E, 0); } |
| 137 |
| 138 bool isLiteral() const { return Enc == Literal; } |
| 139 |
| 140 bool isArrayOp() const { return Enc == Array; } |
| 141 |
| 142 /// Returns the number of arguments expected by this abbrevation operator. |
| 143 unsigned NumArguments() const { |
| 144 if (isArrayOp()) |
| 145 return 1; |
| 146 else |
| 147 return 0; |
| 148 } |
| 149 |
| 150 // Returns the name of the encoding |
| 151 static const char *getEncodingName(Encoding E) { |
| 152 if (E > Encoding_MAX) |
| 153 return "???"; |
| 154 return EncodingNameArray[E]; |
| 155 } |
| 156 |
| 157 /// Prints out the abbreviation operator to the given stream. |
| 158 void Print(raw_ostream &Stream) const; |
| 159 |
| 160 /// isChar6 - Return true if this character is legal in the Char6 encoding. |
| 161 static bool isChar6(char C) { |
| 162 if (C >= 'a' && C <= 'z') return true; |
| 163 if (C >= 'A' && C <= 'Z') return true; |
| 164 if (C >= '0' && C <= '9') return true; |
| 165 if (C == '.' || C == '_') return true; |
| 166 return false; |
| 167 } |
| 168 static unsigned EncodeChar6(char C) { |
| 169 if (C >= 'a' && C <= 'z') return C-'a'; |
| 170 if (C >= 'A' && C <= 'Z') return C-'A'+26; |
| 171 if (C >= '0' && C <= '9') return C-'0'+26+26; |
| 172 if (C == '.') return 62; |
| 173 if (C == '_') return 63; |
| 174 llvm_unreachable("Not a value Char6 character!"); |
| 175 } |
| 176 |
| 177 static char DecodeChar6(unsigned V) { |
| 178 assert((V & ~63) == 0 && "Not a Char6 encoded character!"); |
| 179 if (V < 26) return V+'a'; |
| 180 if (V < 26+26) return V-26+'A'; |
| 181 if (V < 26+26+10) return V-26-26+'0'; |
| 182 if (V == 62) return '.'; |
| 183 if (V == 63) return '_'; |
| 184 llvm_unreachable("Not a value Char6 character!"); |
| 185 } |
| 186 |
| 187 /// \brief Compares this to Op. Returns <0 if this is less than Op, |
| 188 /// Returns 0 if they are equal, and >0 if this is greater than Op. |
| 189 int Compare(const NaClBitCodeAbbrevOp &Op) const { |
| 190 // Compare encoding values. |
| 191 int EncodingDiff = static_cast<int>(Enc) - static_cast<int>(Op.Enc); |
| 192 if (EncodingDiff != 0) return EncodingDiff; |
| 193 |
| 194 // Encodings don't differ, so now base on data associated with the |
| 195 // encoding. |
| 196 return ValCompare(Op); |
| 197 } |
| 198 |
| 199 private: |
| 200 Encoding Enc; // The encoding to use. |
| 201 uint64_t Val; // Data associated with encoding (if any). |
| 202 |
| 203 int ValCompare(const NaClBitCodeAbbrevOp &Op) const { |
| 204 if (Val < Op.Val) |
| 205 return -1; |
| 206 else if (Val > Op.Val) |
| 207 return 1; |
| 208 else |
| 209 return 0; |
| 210 } |
| 211 static const bool HasValueArray[]; |
| 212 static const char *EncodingNameArray[]; |
| 213 }; |
| 214 |
| 215 template <> struct isPodLike<NaClBitCodeAbbrevOp> { |
| 216 static const bool value=true; |
| 217 }; |
| 218 |
| 219 static inline bool operator<(const NaClBitCodeAbbrevOp &Op1, |
| 220 const NaClBitCodeAbbrevOp &Op2) { |
| 221 return Op1.Compare(Op2) < 0; |
| 222 } |
| 223 |
| 224 static inline bool operator<=(const NaClBitCodeAbbrevOp &Op1, |
| 225 const NaClBitCodeAbbrevOp &Op2) { |
| 226 return Op1.Compare(Op2) <= 0; |
| 227 } |
| 228 |
| 229 static inline bool operator==(const NaClBitCodeAbbrevOp &Op1, |
| 230 const NaClBitCodeAbbrevOp &Op2) { |
| 231 return Op1.Compare(Op2) == 0; |
| 232 } |
| 233 |
| 234 static inline bool operator!=(const NaClBitCodeAbbrevOp &Op1, |
| 235 const NaClBitCodeAbbrevOp &Op2) { |
| 236 return Op1.Compare(Op2) != 0; |
| 237 } |
| 238 |
| 239 static inline bool operator>=(const NaClBitCodeAbbrevOp &Op1, |
| 240 const NaClBitCodeAbbrevOp &Op2) { |
| 241 return Op1.Compare(Op2) >= 0; |
| 242 } |
| 243 |
| 244 static inline bool operator>(const NaClBitCodeAbbrevOp &Op1, |
| 245 const NaClBitCodeAbbrevOp &Op2) { |
| 246 return Op1.Compare(Op2) > 0; |
| 247 } |
| 248 |
| 249 /// NaClBitCodeAbbrev - This class represents an abbreviation record. An |
| 250 /// abbreviation allows a complex record that has redundancy to be stored in a |
| 251 /// specialized format instead of the fully-general, fully-vbr, format. |
| 252 class NaClBitCodeAbbrev { |
| 253 SmallVector<NaClBitCodeAbbrevOp, 8> OperandList; |
| 254 unsigned char RefCount; // Number of things using this. |
| 255 ~NaClBitCodeAbbrev() {} |
| 256 public: |
| 257 NaClBitCodeAbbrev() : RefCount(1) {} |
| 258 |
| 259 void addRef() { ++RefCount; } |
| 260 void dropRef() { if (--RefCount == 0) delete this; } |
| 261 |
| 262 unsigned getNumOperandInfos() const { |
| 263 return static_cast<unsigned>(OperandList.size()); |
| 264 } |
| 265 const NaClBitCodeAbbrevOp &getOperandInfo(unsigned N) const { |
| 266 return OperandList[N]; |
| 267 } |
| 268 |
| 269 void Add(const NaClBitCodeAbbrevOp &OpInfo) { |
| 270 OperandList.push_back(OpInfo); |
| 271 } |
| 272 |
| 273 // Returns a simplified version of the abbreviation. Used |
| 274 // to recognize equivalent abbrevations. |
| 275 NaClBitCodeAbbrev *Simplify() const; |
| 276 |
| 277 // Returns true if the abbreviation is valid wrt to the bitcode reader. |
| 278 bool isValid() const; |
| 279 |
| 280 int Compare(const NaClBitCodeAbbrev &Abbrev) const { |
| 281 // First order based on number of operands. |
| 282 size_t OperandListSize = OperandList.size(); |
| 283 size_t AbbrevOperandListSize = Abbrev.OperandList.size(); |
| 284 if (OperandListSize < AbbrevOperandListSize) |
| 285 return -1; |
| 286 else if (OperandListSize > AbbrevOperandListSize) |
| 287 return 1; |
| 288 |
| 289 // Same number of operands, so compare element by element. |
| 290 for (size_t I = 0; I < OperandListSize; ++I) { |
| 291 if (int Diff = OperandList[I].Compare(Abbrev.OperandList[I])) |
| 292 return Diff; |
| 293 } |
| 294 return 0; |
| 295 } |
| 296 |
| 297 // Returns true if all records matching the abbreviation must be |
| 298 // of fixed length. |
| 299 bool IsFixedSize() const { |
| 300 unsigned Size = getNumOperandInfos(); |
| 301 if (Size < 2) return true; |
| 302 return !OperandList[Size-2].isArrayOp(); |
| 303 } |
| 304 |
| 305 // Returns the smallest record size that will match this |
| 306 // abbreviation. |
| 307 size_t GetMinRecordSize() const { |
| 308 size_t Min = getNumOperandInfos(); |
| 309 if (!IsFixedSize()) Min -= 2; |
| 310 return Min; |
| 311 } |
| 312 |
| 313 void Print(raw_ostream &Stream, bool AddNewline=true) const; |
| 314 |
| 315 NaClBitCodeAbbrev *Copy() const { |
| 316 NaClBitCodeAbbrev *AbbrevCopy = new NaClBitCodeAbbrev(); |
| 317 for (unsigned I = 0, IEnd = getNumOperandInfos(); |
| 318 I != IEnd; ++I) { |
| 319 AbbrevCopy->Add(NaClBitCodeAbbrevOp(getOperandInfo(I))); |
| 320 } |
| 321 return AbbrevCopy; |
| 322 } |
| 323 }; |
| 324 |
| 325 static inline bool operator<(const NaClBitCodeAbbrev &A1, |
| 326 const NaClBitCodeAbbrev &A2) { |
| 327 return A1.Compare(A2) < 0; |
| 328 } |
| 329 |
| 330 static inline bool operator<=(const NaClBitCodeAbbrev &A1, |
| 331 const NaClBitCodeAbbrev &A2) { |
| 332 return A1.Compare(A2) <= 0; |
| 333 } |
| 334 static inline bool operator==(const NaClBitCodeAbbrev &A1, |
| 335 const NaClBitCodeAbbrev &A2) { |
| 336 return A1.Compare(A2) == 0; |
| 337 } |
| 338 |
| 339 static inline bool operator!=(const NaClBitCodeAbbrev &A1, |
| 340 const NaClBitCodeAbbrev &A2) { |
| 341 return A1.Compare(A2) != 0; |
| 342 } |
| 343 static inline bool operator>=(const NaClBitCodeAbbrev &A1, |
| 344 const NaClBitCodeAbbrev &A2) { |
| 345 return A1.Compare(A2) >= 0; |
| 346 } |
| 347 |
| 348 static inline bool operator>(const NaClBitCodeAbbrev &A1, |
| 349 const NaClBitCodeAbbrev &A2) { |
| 350 return A1.Compare(A2) > 0; |
| 351 } |
| 352 |
| 353 /// \brief Returns number of bits needed to encode |
| 354 /// value for dense FIXED encoding. |
| 355 inline unsigned NaClBitsNeededForValue(unsigned Value) { |
| 356 // Note: Need to handle case where Value=0xFFFFFFFF as special case, |
| 357 // since we can't add 1 to it. |
| 358 if (Value >= 0x80000000) return 32; |
| 359 return Log2_32_Ceil(Value+1); |
| 360 } |
| 361 |
| 362 /// \brief Encode a signed value by moving the sign to the LSB for dense |
| 363 /// VBR encoding. |
| 364 inline uint64_t NaClEncodeSignRotatedValue(int64_t V) { |
| 365 return (V >= 0) ? (V << 1) : ((-V << 1) | 1); |
| 366 } |
| 367 |
| 368 /// \brief Decode a signed value stored with the sign bit in |
| 369 /// the LSB for dense VBR encoding. |
| 370 inline uint64_t NaClDecodeSignRotatedValue(uint64_t V) { |
| 371 if ((V & 1) == 0) |
| 372 return V >> 1; |
| 373 if (V != 1) |
| 374 return -(V >> 1); |
| 375 // There is no such thing as -0 with integers. "-0" really means MININT. |
| 376 return 1ULL << 63; |
| 377 } |
| 378 |
| 379 /// \brief This class determines whether a FIXED or VBR |
| 380 /// abbreviation should be used for the selector, and the number of bits |
| 381 /// needed to capture such selectors. |
| 382 class NaClBitcodeSelectorAbbrev { |
| 383 |
| 384 public: |
| 385 // If true, use a FIXED abbreviation. Otherwise, use a VBR abbreviation. |
| 386 bool IsFixed; |
| 387 // Number of bits needed for selector. |
| 388 unsigned NumBits; |
| 389 |
| 390 // Creates a selector range for the given values. |
| 391 NaClBitcodeSelectorAbbrev(bool IF, unsigned NB) |
| 392 : IsFixed(IF), NumBits(NB) {} |
| 393 |
| 394 // Creates a selector range when no abbreviations are defined. |
| 395 NaClBitcodeSelectorAbbrev() |
| 396 : IsFixed(true), |
| 397 NumBits(NaClBitsNeededForValue(naclbitc::DEFAULT_MAX_ABBREV)) {} |
| 398 |
| 399 // Creates a selector range to handle fixed abbrevations up to |
| 400 // the specified value. |
| 401 explicit NaClBitcodeSelectorAbbrev(unsigned MaxAbbrev) |
| 402 : IsFixed(true), |
| 403 NumBits(NaClBitsNeededForValue(MaxAbbrev)) {} |
| 404 }; |
| 405 } // End llvm namespace |
| 406 |
| 407 #endif |
OLD | NEW |