Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 1 | //===- NaClBitcodeHeader.cpp ----------------------------------------------===// |
| 2 | // PNaCl bitcode header reader. |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // Implementation of Bitcode abbrevations. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Bitcode/NaCl/NaClBitCodes.h" |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 16 | #include "llvm/Support/ErrorHandling.h" |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | const bool NaClBitCodeAbbrevOp::HasValueArray[] = { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 22 | true, // Literal |
| 23 | true, // Fixed |
| 24 | true, // VBR |
| 25 | false, // Array |
| 26 | false // Char6 |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | const char *NaClBitCodeAbbrevOp::EncodingNameArray[] = { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 30 | "Literal", "Fixed", "VBR", "Array", "Char6"}; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 31 | |
| 32 | NaClBitCodeAbbrevOp::NaClBitCodeAbbrevOp(Encoding E, uint64_t Data) |
| 33 | : Enc(E), Val(Data) { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 34 | if (isValid(E, Data)) |
| 35 | return; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 36 | std::string Buffer; |
| 37 | raw_string_ostream StrBuf(Buffer); |
| 38 | StrBuf << "Invalid NaClBitCodeAbbrevOp(" << E << ", " << Data << ")"; |
| 39 | report_fatal_error(StrBuf.str()); |
| 40 | } |
| 41 | |
| 42 | bool NaClBitCodeAbbrevOp::isValid(Encoding E, uint64_t Val) { |
| 43 | switch (NaClBitCodeAbbrevOp::Encoding(E)) { |
| 44 | case Literal: |
| 45 | return true; |
| 46 | case Fixed: |
| 47 | case VBR: |
| 48 | return Val <= naclbitc::MaxAbbrevWidth; |
| 49 | case Char6: |
| 50 | case Array: |
| 51 | return Val == 0; |
| 52 | } |
| 53 | llvm_unreachable("unhandled abbreviation"); |
| 54 | } |
| 55 | |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 56 | void NaClBitCodeAbbrevOp::Print(raw_ostream &Stream) const { |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 57 | if (Enc == Literal) { |
| 58 | Stream << getValue(); |
| 59 | return; |
| 60 | } |
| 61 | Stream << getEncodingName(Enc); |
| 62 | if (!hasValue()) |
| 63 | return; |
| 64 | Stream << "(" << Val << ")"; |
| 65 | } |
| 66 | |
| 67 | static void PrintExpression(raw_ostream &Stream, |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 68 | const NaClBitCodeAbbrev *Abbrev, unsigned &Index) { |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 69 | // Bail out early, in case we are incrementally building the |
| 70 | // expression and the argument is not available yet. |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 71 | if (Index >= Abbrev->getNumOperandInfos()) |
| 72 | return; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 73 | |
| 74 | const NaClBitCodeAbbrevOp &Op = Abbrev->getOperandInfo(Index); |
| 75 | Op.Print(Stream); |
| 76 | if (unsigned NumArgs = Op.NumArguments()) { |
| 77 | Stream << "("; |
| 78 | for (unsigned i = 0; i < NumArgs; ++i) { |
| 79 | ++Index; |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 80 | if (i > 0) |
| 81 | Stream << ","; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 82 | PrintExpression(Stream, Abbrev, Index); |
| 83 | } |
| 84 | Stream << ")"; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void NaClBitCodeAbbrev::Print(raw_ostream &Stream, bool AddNewLine) const { |
| 89 | Stream << "["; |
| 90 | for (unsigned i = 0; i < getNumOperandInfos(); ++i) { |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 91 | if (i > 0) |
| 92 | Stream << ", "; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 93 | PrintExpression(Stream, this, i); |
| 94 | } |
| 95 | Stream << "]"; |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 96 | if (AddNewLine) |
| 97 | Stream << "\n"; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | NaClBitCodeAbbrev *NaClBitCodeAbbrev::Simplify() const { |
| 101 | NaClBitCodeAbbrev *Abbrev = new NaClBitCodeAbbrev(); |
| 102 | for (unsigned i = 0; i < OperandList.size(); ++i) { |
| 103 | const NaClBitCodeAbbrevOp &Op = OperandList[i]; |
| 104 | // Simplify if possible. Currently, the only simplification known |
| 105 | // is to remove unnecessary operands appearing immediately before an |
| 106 | // array operator. That is, apply the simplification: |
| 107 | // Op Array(Op) -> Array(Op) |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 108 | assert(!Op.isArrayOp() || i == OperandList.size() - 2); |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 109 | while (Op.isArrayOp() && !Abbrev->OperandList.empty() && |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 110 | Abbrev->OperandList.back() == OperandList[i + 1]) { |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 111 | Abbrev->OperandList.pop_back(); |
| 112 | } |
| 113 | Abbrev->OperandList.push_back(Op); |
| 114 | } |
| 115 | return Abbrev; |
| 116 | } |
| 117 | |
| 118 | bool NaClBitCodeAbbrev::isValid() const { |
| 119 | // Verify that an array op appears can only appear if it is the |
| 120 | // second to last element. |
| 121 | unsigned NumOperands = getNumOperandInfos(); |
Antonio Maiorano | ca8a16e | 2020-11-10 16:56:20 -0500 | [diff] [blame] | 122 | if (NumOperands == 0) |
| 123 | return false; |
Jim Stichnoth | a5b16ab | 2016-05-10 11:20:41 -0700 | [diff] [blame] | 124 | for (unsigned i = 0; i < NumOperands; ++i) { |
| 125 | const NaClBitCodeAbbrevOp &Op = getOperandInfo(i); |
| 126 | if (Op.isArrayOp() && i + 2 != NumOperands) |
| 127 | // Note: Unlike LLVM bitcode, we allow literals in arrays! |
| 128 | return false; |
| 129 | } |
| 130 | return true; |
| 131 | } |