diff options
author | Karl Schimpf <kschimpf@google.com> | 2013-11-15 13:47:14 -0800 |
---|---|---|
committer | Karl Schimpf <kschimpf@google.com> | 2013-11-21 09:40:15 -0800 |
commit | 757414ba931360b0c65851c2583e511d67948d91 (patch) | |
tree | 55a0e6d96bc4afa01d82d5b06b6859759d722f05 /include | |
parent | 9d4cc0b069eb0f8b54682c3497ea8ccabf133b98 (diff) |
Define comparison for PNaCl bitcode abbreviations.
Define comparison of bitcode abbreviations so that we can add bitcode
abbreviations to collections.
BUG= https://code.google.com/p/nativeclient/issues/detail?id=3720
Diffstat (limited to 'include')
-rw-r--r-- | include/llvm/Bitcode/NaCl/NaClBitCodes.h | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/include/llvm/Bitcode/NaCl/NaClBitCodes.h b/include/llvm/Bitcode/NaCl/NaClBitCodes.h index bb52d0e20e..ce041abdde 100644 --- a/include/llvm/Bitcode/NaCl/NaClBitCodes.h +++ b/include/llvm/Bitcode/NaCl/NaClBitCodes.h @@ -169,12 +169,71 @@ public: llvm_unreachable("Not a value Char6 character!"); } + /// \brief Compares this to Op. Returns <0 if this is less than Op, + /// Returns 0 if they are equal, and >0 if this is greater than Op. + int Compare(const NaClBitCodeAbbrevOp &Op) const { + // Assume literals are smallest in comparisons. + if (IsLiteral) { + if (!Op.IsLiteral) + return -1; + return ValCompare(Op); + } else if (Op.IsLiteral) + return 1; + + // Neither is a literal, so now order on encoding. + int EncodingDiff = static_cast<int>(Enc) - static_cast<int>(Op.Enc); + if (EncodingDiff != 0) return EncodingDiff; + + // Encodings don't differ, so now base on data associated with the + // encoding. + return ValCompare(Op); + } + +private: + int ValCompare(const NaClBitCodeAbbrevOp &Op) const { + if (Val < Op.Val) + return -1; + else if (Val > Op.Val) + return 1; + else + return 0; + } }; template <> struct isPodLike<NaClBitCodeAbbrevOp> { static const bool value=true; }; +static inline bool operator<(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) < 0; +} + +static inline bool operator<=(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) <= 0; +} + +static inline bool operator==(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) == 0; +} + +static inline bool operator!=(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) != 0; +} + +static inline bool operator>=(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) >= 0; +} + +static inline bool operator>(const NaClBitCodeAbbrevOp &Op1, + const NaClBitCodeAbbrevOp &Op2) { + return Op1.Compare(Op2) > 0; +} + /// NaClBitCodeAbbrev - This class represents an abbreviation record. An /// abbreviation allows a complex record that has redundancy to be stored in a /// specialized format instead of the fully-general, fully-vbr, format. @@ -198,8 +257,55 @@ public: void Add(const NaClBitCodeAbbrevOp &OpInfo) { OperandList.push_back(OpInfo); } + + int Compare(const NaClBitCodeAbbrev &Abbrev) const { + // First order based on number of operands. + size_t OperandListSize = OperandList.size(); + size_t AbbrevOperandListSize = Abbrev.OperandList.size(); + if (OperandListSize < AbbrevOperandListSize) + return -1; + else if (OperandListSize > AbbrevOperandListSize) + return 1; + else + return 0; + + // Same number of operands, so compare element by element. + for (size_t I = 0; I < OperandListSize; ++I) { + if (int Diff = OperandList[I].Compare(Abbrev.OperandList[I])) + return Diff; + } + return 0; + } }; +static inline bool operator<(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) < 0; +} + +static inline bool operator<=(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) <= 0; +} +static inline bool operator==(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) == 0; +} + +static inline bool operator!=(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) != 0; +} +static inline bool operator>=(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) >= 0; +} + +static inline bool operator>(const NaClBitCodeAbbrev &A1, + const NaClBitCodeAbbrev &A2) { + return A1.Compare(A2) > 0; +} + /// \brief Returns number of bits needed to encode /// value for dense FIXED encoding. inline unsigned NaClBitsNeededForValue(unsigned Value) { |