Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1 | //===- subzero/src/PNaClTranslator.cpp - ICE from bitcode -----------------===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Implements the interface for translation from PNaCl bitcode files to |
| 12 | /// ICE to machine code. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 16 | #include "PNaClTranslator.h" |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 17 | |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 18 | #include "IceCfg.h" |
| 19 | #include "IceCfgNode.h" |
| 20 | #include "IceClFlags.h" |
| 21 | #include "IceDefs.h" |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 22 | #include "IceGlobalInits.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 23 | #include "IceInst.h" |
| 24 | #include "IceOperand.h" |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 25 | #include "IceRangeSpec.h" |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 26 | |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 27 | #ifdef __clang__ |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 28 | #pragma clang diagnostic push |
| 29 | #pragma clang diagnostic ignored "-Wunused-parameter" |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 30 | #endif // __clang__ |
| 31 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 32 | #include "llvm/ADT/Hashing.h" |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 33 | #include "llvm/ADT/SmallString.h" |
| 34 | #include "llvm/Bitcode/NaCl/NaClBitcodeDecoders.h" |
| 35 | #include "llvm/Bitcode/NaCl/NaClBitcodeDefs.h" |
| 36 | #include "llvm/Bitcode/NaCl/NaClBitcodeHeader.h" |
| 37 | #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
| 38 | #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 39 | #include "llvm/Support/Format.h" |
| 40 | #include "llvm/Support/MemoryBuffer.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 42 | |
| 43 | #ifdef __clang__ |
Jim Stichnoth | 98da966 | 2015-06-27 06:38:08 -0700 | [diff] [blame] | 44 | #pragma clang diagnostic pop |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 45 | #endif // __clang__ |
| 46 | |
| 47 | #include <unordered_set> |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 48 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 49 | // Define a hash function for SmallString's, so that it can be used in hash |
| 50 | // tables. |
| 51 | namespace std { |
| 52 | template <unsigned InternalLen> struct hash<llvm::SmallString<InternalLen>> { |
| 53 | size_t operator()(const llvm::SmallString<InternalLen> &Key) const { |
| 54 | return llvm::hash_combine_range(Key.begin(), Key.end()); |
| 55 | } |
| 56 | }; |
| 57 | } // end of namespace std |
| 58 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 59 | namespace { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 60 | using namespace llvm; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 61 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 62 | // Models elements in the list of types defined in the types block. These |
| 63 | // elements can be undefined, a (simple) type, or a function type signature. |
| 64 | // Note that an extended type is undefined on construction. Use methods |
| 65 | // setAsSimpleType and setAsFuncSigType to define the extended type. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 66 | class ExtendedType { |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 67 | ExtendedType &operator=(const ExtendedType &Ty) = delete; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 68 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 69 | public: |
| 70 | /// Discriminator for LLVM-style RTTI. |
| 71 | enum TypeKind { Undefined, Simple, FuncSig }; |
| 72 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 73 | ExtendedType() = default; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 74 | ExtendedType(const ExtendedType &Ty) = default; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 75 | |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 76 | virtual ~ExtendedType() = default; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 77 | |
| 78 | ExtendedType::TypeKind getKind() const { return Kind; } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 79 | void dump(Ice::Ostream &Stream) const; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 80 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 81 | /// Changes the extended type to a simple type with the given / value. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 82 | void setAsSimpleType(Ice::Type Ty) { |
| 83 | assert(Kind == Undefined); |
| 84 | Kind = Simple; |
| 85 | Signature.setReturnType(Ty); |
| 86 | } |
| 87 | |
| 88 | /// Changes the extended type to an (empty) function signature type. |
| 89 | void setAsFunctionType() { |
| 90 | assert(Kind == Undefined); |
| 91 | Kind = FuncSig; |
| 92 | } |
| 93 | |
| 94 | protected: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 95 | // Note: For simple types, the return type of the signature will be used to |
| 96 | // hold the simple type. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 97 | Ice::FuncSigType Signature; |
| 98 | |
| 99 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 100 | ExtendedType::TypeKind Kind = Undefined; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | Ice::Ostream &operator<<(Ice::Ostream &Stream, const ExtendedType &Ty) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 104 | if (!Ice::BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 105 | return Stream; |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 106 | Ty.dump(Stream); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 107 | return Stream; |
| 108 | } |
| 109 | |
| 110 | Ice::Ostream &operator<<(Ice::Ostream &Stream, ExtendedType::TypeKind Kind) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 111 | if (!Ice::BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 112 | return Stream; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 113 | Stream << "ExtendedType::"; |
| 114 | switch (Kind) { |
| 115 | case ExtendedType::Undefined: |
| 116 | Stream << "Undefined"; |
| 117 | break; |
| 118 | case ExtendedType::Simple: |
| 119 | Stream << "Simple"; |
| 120 | break; |
| 121 | case ExtendedType::FuncSig: |
| 122 | Stream << "FuncSig"; |
| 123 | break; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 124 | } |
| 125 | return Stream; |
| 126 | } |
| 127 | |
| 128 | // Models an ICE type as an extended type. |
| 129 | class SimpleExtendedType : public ExtendedType { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 130 | SimpleExtendedType() = delete; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 131 | SimpleExtendedType(const SimpleExtendedType &) = delete; |
| 132 | SimpleExtendedType &operator=(const SimpleExtendedType &) = delete; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 133 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 134 | public: |
| 135 | Ice::Type getType() const { return Signature.getReturnType(); } |
| 136 | |
| 137 | static bool classof(const ExtendedType *Ty) { |
| 138 | return Ty->getKind() == Simple; |
| 139 | } |
| 140 | }; |
| 141 | |
| 142 | // Models a function signature as an extended type. |
| 143 | class FuncSigExtendedType : public ExtendedType { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 144 | FuncSigExtendedType() = delete; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 145 | FuncSigExtendedType(const FuncSigExtendedType &) = delete; |
| 146 | FuncSigExtendedType &operator=(const FuncSigExtendedType &) = delete; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 147 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 148 | public: |
| 149 | const Ice::FuncSigType &getSignature() const { return Signature; } |
| 150 | void setReturnType(Ice::Type ReturnType) { |
| 151 | Signature.setReturnType(ReturnType); |
| 152 | } |
| 153 | void appendArgType(Ice::Type ArgType) { Signature.appendArgType(ArgType); } |
| 154 | static bool classof(const ExtendedType *Ty) { |
| 155 | return Ty->getKind() == FuncSig; |
| 156 | } |
| 157 | }; |
| 158 | |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 159 | void ExtendedType::dump(Ice::Ostream &Stream) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 160 | if (!Ice::BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 161 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 162 | Stream << Kind; |
| 163 | switch (Kind) { |
| 164 | case Simple: { |
| 165 | Stream << " " << Signature.getReturnType(); |
| 166 | break; |
| 167 | } |
| 168 | case FuncSig: { |
| 169 | Stream << " " << Signature; |
| 170 | } |
| 171 | default: |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 176 | // Models integer literals as a sequence of bits. Used to read integer values |
| 177 | // from bitcode files. Based on llvm::APInt. |
| 178 | class BitcodeInt { |
| 179 | BitcodeInt() = delete; |
| 180 | BitcodeInt(const BitcodeInt &) = delete; |
| 181 | BitcodeInt &operator=(const BitcodeInt &) = delete; |
| 182 | |
| 183 | public: |
| 184 | BitcodeInt(Ice::SizeT Bits, uint64_t Val) : BitWidth(Bits), Val(Val) { |
| 185 | assert(Bits && "bitwidth too small"); |
| 186 | assert(Bits <= BITS_PER_WORD && "bitwidth too big"); |
| 187 | clearUnusedBits(); |
| 188 | } |
| 189 | |
| 190 | int64_t getSExtValue() const { |
| 191 | return static_cast<int64_t>(Val << (BITS_PER_WORD - BitWidth)) >> |
| 192 | (BITS_PER_WORD - BitWidth); |
| 193 | } |
| 194 | |
| 195 | template <typename IntType, typename FpType> |
| 196 | inline FpType convertToFp() const { |
| 197 | static_assert(sizeof(IntType) == sizeof(FpType), |
| 198 | "IntType and FpType should be the same width"); |
| 199 | assert(BitWidth == sizeof(IntType) * CHAR_BIT); |
| 200 | auto V = static_cast<IntType>(Val); |
Jim Stichnoth | b0051df | 2016-01-13 11:39:15 -0800 | [diff] [blame] | 201 | return Ice::Utils::bitCopy<FpType>(V); |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private: |
| 205 | /// Bits in the (internal) value. |
| 206 | static const Ice::SizeT BITS_PER_WORD = sizeof(uint64_t) * CHAR_BIT; |
| 207 | |
| 208 | uint32_t BitWidth; /// The number of bits in the floating point number. |
| 209 | uint64_t Val; /// The (64-bit) equivalent integer value. |
| 210 | |
| 211 | /// Clear unused high order bits. |
| 212 | void clearUnusedBits() { |
| 213 | // If all bits are used, we want to leave the value alone. |
| 214 | if (BitWidth == BITS_PER_WORD) |
| 215 | return; |
| 216 | |
| 217 | // Mask out the high bits. |
| 218 | Val &= ~static_cast<uint64_t>(0) >> (BITS_PER_WORD - BitWidth); |
| 219 | } |
| 220 | }; |
| 221 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 222 | class BlockParserBaseClass; |
| 223 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 224 | // Top-level class to read PNaCl bitcode files, and translate to ICE. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 225 | class TopLevelParser final : public NaClBitcodeParser { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 226 | TopLevelParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 227 | TopLevelParser(const TopLevelParser &) = delete; |
| 228 | TopLevelParser &operator=(const TopLevelParser &) = delete; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 229 | |
| 230 | public: |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 231 | TopLevelParser(Ice::Translator &Translator, NaClBitstreamCursor &Cursor, |
| 232 | Ice::ErrorCode &ErrorStatus) |
| 233 | : NaClBitcodeParser(Cursor), Translator(Translator), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 234 | ErrorStatus(ErrorStatus), |
| 235 | VariableDeclarations(new Ice::VariableDeclarationList()) {} |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 236 | |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 237 | ~TopLevelParser() override = default; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 238 | |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 239 | Ice::Translator &getTranslator() const { return Translator; } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 240 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 241 | /// Generates error with given Message, occurring at BitPosition within the |
| 242 | /// bitcode file. Always returns true. |
Karl Schimpf | 17b1a13 | 2015-03-12 09:32:06 -0700 | [diff] [blame] | 243 | bool ErrorAt(naclbitc::ErrorLevel Level, uint64_t BitPosition, |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 244 | const std::string &Message) override; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 245 | |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 246 | /// Generates error message with respect to the current block parser. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 247 | bool blockError(const std::string &Message); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 248 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 249 | /// Changes the size of the type list to the given size. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 250 | void resizeTypeIDValues(size_t NewSize) { TypeIDValues.resize(NewSize); } |
| 251 | |
| 252 | size_t getNumTypeIDValues() const { return TypeIDValues.size(); } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 253 | |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 254 | /// Returns a pointer to the pool where globals are allocated. |
| 255 | Ice::VariableDeclarationList *getGlobalVariablesPool() { |
| 256 | return VariableDeclarations.get(); |
| 257 | } |
| 258 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 259 | /// Returns the undefined type associated with type ID. Note: Returns extended |
| 260 | /// type ready to be defined. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 261 | ExtendedType *getTypeByIDForDefining(NaClBcIndexSize_t ID) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 262 | // Get corresponding element, verifying the value is still undefined (and |
| 263 | // hence allowed to be defined). |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 264 | ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::Undefined); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 265 | if (Ty) |
| 266 | return Ty; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 267 | if (ID >= TypeIDValues.size()) { |
| 268 | if (ID >= NaClBcIndexSize_t_Max) { |
| 269 | std::string Buffer; |
| 270 | raw_string_ostream StrBuf(Buffer); |
| 271 | StrBuf << "Can't define more than " << NaClBcIndexSize_t_Max |
| 272 | << " types\n"; |
| 273 | blockError(StrBuf.str()); |
| 274 | // Recover by using existing type slot. |
| 275 | return &TypeIDValues[0]; |
| 276 | } |
Jim Stichnoth | 2f00bf6 | 2016-03-11 14:59:43 -0800 | [diff] [blame] | 277 | Ice::Utils::reserveAndResize(TypeIDValues, ID + 1); |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 278 | } |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 279 | return &TypeIDValues[ID]; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 282 | /// Returns the type associated with the given index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 283 | Ice::Type getSimpleTypeByID(NaClBcIndexSize_t ID) { |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 284 | const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::Simple); |
| 285 | if (Ty == nullptr) |
| 286 | // Return error recovery value. |
| 287 | return Ice::IceType_void; |
| 288 | return cast<SimpleExtendedType>(Ty)->getType(); |
| 289 | } |
| 290 | |
| 291 | /// Returns the type signature associated with the given index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 292 | const Ice::FuncSigType &getFuncSigTypeByID(NaClBcIndexSize_t ID) { |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 293 | const ExtendedType *Ty = getTypeByIDAsKind(ID, ExtendedType::FuncSig); |
| 294 | if (Ty == nullptr) |
| 295 | // Return error recovery value. |
| 296 | return UndefinedFuncSigType; |
| 297 | return cast<FuncSigExtendedType>(Ty)->getSignature(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /// Sets the next function ID to the given LLVM function. |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 301 | void setNextFunctionID(Ice::FunctionDeclaration *Fcn) { |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 302 | FunctionDeclarations.push_back(Fcn); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 305 | /// Returns the value id that should be associated with the the current |
| 306 | /// function block. Increments internal counters during call so that it will |
| 307 | /// be in correct position for next function block. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 308 | NaClBcIndexSize_t getNextFunctionBlockValueID() { |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 309 | size_t NumDeclaredFunctions = FunctionDeclarations.size(); |
Karl Schimpf | 0c729c8 | 2015-01-28 10:58:25 -0800 | [diff] [blame] | 310 | while (NextDefiningFunctionID < NumDeclaredFunctions && |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 311 | FunctionDeclarations[NextDefiningFunctionID]->isProto()) |
Karl Schimpf | 0c729c8 | 2015-01-28 10:58:25 -0800 | [diff] [blame] | 312 | ++NextDefiningFunctionID; |
| 313 | if (NextDefiningFunctionID >= NumDeclaredFunctions) |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 314 | Fatal("More function blocks than defined function addresses"); |
Karl Schimpf | 0c729c8 | 2015-01-28 10:58:25 -0800 | [diff] [blame] | 315 | return NextDefiningFunctionID++; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 318 | /// Returns the function associated with ID. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 319 | Ice::FunctionDeclaration *getFunctionByID(NaClBcIndexSize_t ID) { |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 320 | if (ID < FunctionDeclarations.size()) |
| 321 | return FunctionDeclarations[ID]; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 322 | return reportGetFunctionByIDError(ID); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 325 | /// Returns the constant associated with the given global value ID. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 326 | Ice::Constant *getGlobalConstantByID(NaClBcIndexSize_t ID) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 327 | assert(ID < ValueIDConstants.size()); |
| 328 | return ValueIDConstants[ID]; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 331 | /// Install names for all global values without names. Called after the global |
| 332 | /// value symbol table is processed, but before any function blocks are |
| 333 | /// processed. |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 334 | void installGlobalNames() { |
| 335 | assert(VariableDeclarations); |
| 336 | installGlobalVarNames(); |
| 337 | installFunctionNames(); |
| 338 | } |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 339 | |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 340 | void verifyFunctionTypeSignatures(); |
| 341 | |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 342 | void createValueIDs() { |
| 343 | assert(VariableDeclarations); |
| 344 | ValueIDConstants.reserve(VariableDeclarations->size() + |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 345 | FunctionDeclarations.size()); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 346 | createValueIDsForFunctions(); |
| 347 | createValueIDsForGlobalVars(); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 350 | /// Returns the number of function declarations in the bitcode file. |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 351 | size_t getNumFunctionIDs() const { return FunctionDeclarations.size(); } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 352 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 353 | /// Returns the number of global declarations (i.e. IDs) defined in the |
| 354 | /// bitcode file. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 355 | size_t getNumGlobalIDs() const { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 356 | if (VariableDeclarations) { |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 357 | return FunctionDeclarations.size() + VariableDeclarations->size(); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 358 | } else { |
| 359 | return ValueIDConstants.size(); |
| 360 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 363 | /// Adds the given global declaration to the end of the list of global |
| 364 | /// declarations. |
| 365 | void addGlobalDeclaration(Ice::VariableDeclaration *Decl) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 366 | assert(VariableDeclarations); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 367 | VariableDeclarations->push_back(Decl); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 370 | /// Returns the global variable declaration with the given index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 371 | Ice::VariableDeclaration *getGlobalVariableByID(NaClBcIndexSize_t Index) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 372 | assert(VariableDeclarations); |
| 373 | if (Index < VariableDeclarations->size()) |
| 374 | return VariableDeclarations->at(Index); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 375 | return reportGetGlobalVariableByIDError(Index); |
| 376 | } |
| 377 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 378 | /// Returns the global declaration (variable or function) with the given |
| 379 | /// Index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 380 | Ice::GlobalDeclaration *getGlobalDeclarationByID(NaClBcIndexSize_t Index) { |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 381 | size_t NumFunctionIds = FunctionDeclarations.size(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 382 | if (Index < NumFunctionIds) |
| 383 | return getFunctionByID(Index); |
| 384 | else |
| 385 | return getGlobalVariableByID(Index - NumFunctionIds); |
| 386 | } |
| 387 | |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 388 | /// Returns true if a module block has been parsed. |
| 389 | bool parsedModuleBlock() const { return ParsedModuleBlock; } |
| 390 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 391 | /// Returns the list of parsed global variable declarations. Releases |
| 392 | /// ownership of the current list of global variables. Note: only returns |
| 393 | /// non-null pointer on first call. All successive calls return a null |
| 394 | /// pointer. |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 395 | std::unique_ptr<Ice::VariableDeclarationList> getGlobalVariables() { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 396 | // Before returning, check that ValidIDConstants has already been built. |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 397 | assert(!VariableDeclarations || |
| 398 | VariableDeclarations->size() <= ValueIDConstants.size()); |
| 399 | return std::move(VariableDeclarations); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Karl Schimpf | 07af2ac | 2015-09-30 15:33:41 -0700 | [diff] [blame] | 402 | // Upper limit of alignment power allowed by LLVM |
| 403 | static constexpr uint32_t AlignPowerLimit = 29; |
| 404 | |
| 405 | // Extracts the corresponding Alignment to use, given the AlignPower (i.e. |
| 406 | // 2**(AlignPower-1), or 0 if AlignPower == 0). Parser defines the block |
| 407 | // context the alignment check appears in, and Prefix defines the context the |
| 408 | // alignment appears in. |
| 409 | uint32_t extractAlignment(NaClBitcodeParser *Parser, const char *Prefix, |
| 410 | uint32_t AlignPower) { |
| 411 | if (AlignPower <= AlignPowerLimit + 1) |
| 412 | return (1 << AlignPower) >> 1; |
| 413 | std::string Buffer; |
| 414 | raw_string_ostream StrBuf(Buffer); |
| 415 | StrBuf << Prefix << " alignment greater than 2**" << AlignPowerLimit |
| 416 | << ". Found: 2**" << (AlignPower - 1); |
| 417 | Parser->Error(StrBuf.str()); |
| 418 | // Error recover with value that is always acceptable. |
| 419 | return 1; |
| 420 | } |
| 421 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 422 | private: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 423 | // The translator associated with the parser. |
| 424 | Ice::Translator &Translator; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 425 | |
| 426 | // ErrorStatus should only be updated while this lock is locked. |
| 427 | Ice::GlobalLockType ErrorReportingLock; |
Karl Schimpf | b164d20 | 2014-07-11 10:26:34 -0700 | [diff] [blame] | 428 | // The exit status that should be set to true if an error occurs. |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 429 | Ice::ErrorCode &ErrorStatus; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 430 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 431 | // The types associated with each type ID. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 432 | std::vector<ExtendedType> TypeIDValues; |
Karl Schimpf | 0c729c8 | 2015-01-28 10:58:25 -0800 | [diff] [blame] | 433 | // The set of functions (prototype and defined). |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 434 | Ice::FunctionDeclarationList FunctionDeclarations; |
| 435 | // The ID of the next possible defined function ID in FunctionDeclarations. |
| 436 | // FunctionDeclarations is filled first. It's the set of functions (either |
| 437 | // defined or isproto). Then function definitions are encountered/parsed and |
| 438 | // NextDefiningFunctionID is incremented to track the next actually-defined |
| 439 | // function. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 440 | size_t NextDefiningFunctionID = 0; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 441 | // The set of global variables. |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 442 | std::unique_ptr<Ice::VariableDeclarationList> VariableDeclarations; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 443 | // Relocatable constants associated with global declarations. |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 444 | Ice::ConstantList ValueIDConstants; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 445 | // Error recovery value to use when getFuncSigTypeByID fails. |
| 446 | Ice::FuncSigType UndefinedFuncSigType; |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 447 | // Defines if a module block has already been parsed. |
| 448 | bool ParsedModuleBlock = false; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 449 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 450 | bool ParseBlock(unsigned BlockID) override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 451 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 452 | // Gets extended type associated with the given index, assuming the extended |
| 453 | // type is of the WantedKind. Generates error message if corresponding |
| 454 | // extended type of WantedKind can't be found, and returns nullptr. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 455 | ExtendedType *getTypeByIDAsKind(NaClBcIndexSize_t ID, |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 456 | ExtendedType::TypeKind WantedKind) { |
| 457 | ExtendedType *Ty = nullptr; |
| 458 | if (ID < TypeIDValues.size()) { |
| 459 | Ty = &TypeIDValues[ID]; |
| 460 | if (Ty->getKind() == WantedKind) |
| 461 | return Ty; |
| 462 | } |
| 463 | // Generate an error message and set ErrorStatus. |
| 464 | this->reportBadTypeIDAs(ID, Ty, WantedKind); |
| 465 | return nullptr; |
| 466 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 467 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 468 | // Gives Decl a name if it doesn't already have one. Prefix and NameIndex are |
| 469 | // used to generate the name. NameIndex is automatically incremented if a new |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 470 | // name is created. DeclType is literal text describing the type of name being |
| 471 | // created. Also generates a warning if created names may conflict with named |
| 472 | // declarations. |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 473 | void installDeclarationName(Ice::GlobalDeclaration *Decl, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 474 | const std::string &Prefix, const char *DeclType, |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 475 | NaClBcIndexSize_t &NameIndex) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 476 | if (Decl->hasName()) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 477 | Translator.checkIfUnnamedNameSafe(Decl->getName().toString(), DeclType, |
| 478 | Prefix); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 479 | } else { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 480 | Ice::GlobalContext *Ctx = Translator.getContext(); |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 481 | // Synthesize a dummy name if any of the following is true: |
| 482 | // - DUMP is enabled |
| 483 | // - The symbol is external |
| 484 | // - The -timing-funcs flag is enabled |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 485 | // - Some RangeSpec is initialized with actual names |
Jim Stichnoth | 318c01b | 2016-04-03 21:58:03 -0700 | [diff] [blame] | 486 | if (Ice::BuildDefs::dump() || !Decl->isInternal() || |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 487 | Ice::RangeSpec::hasNames() || Ice::getFlags().getTimeEachFunction()) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 488 | Decl->setName(Ctx, Translator.createUnnamedName(Prefix, NameIndex)); |
| 489 | } else { |
| 490 | Decl->setName(Ctx); |
| 491 | } |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 492 | ++NameIndex; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Installs names for global variables without names. |
| 497 | void installGlobalVarNames() { |
| 498 | assert(VariableDeclarations); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 499 | const std::string &GlobalPrefix = Ice::getFlags().getDefaultGlobalPrefix(); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 500 | if (!GlobalPrefix.empty()) { |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 501 | NaClBcIndexSize_t NameIndex = 0; |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 502 | for (Ice::VariableDeclaration *Var : *VariableDeclarations) { |
| 503 | installDeclarationName(Var, GlobalPrefix, "global", NameIndex); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Installs names for functions without names. |
| 509 | void installFunctionNames() { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 510 | const std::string &FunctionPrefix = |
| 511 | Ice::getFlags().getDefaultFunctionPrefix(); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 512 | if (!FunctionPrefix.empty()) { |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 513 | NaClBcIndexSize_t NameIndex = 0; |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 514 | for (Ice::FunctionDeclaration *Func : FunctionDeclarations) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 515 | installDeclarationName(Func, FunctionPrefix, "function", NameIndex); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 520 | // Builds a constant symbol named Name. IsExternal is true iff the symbol is |
| 521 | // external. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 522 | Ice::Constant *getConstantSym(Ice::GlobalString Name, bool IsExternal) const { |
| 523 | Ice::GlobalContext *Ctx = getTranslator().getContext(); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 524 | if (IsExternal) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 525 | return Ctx->getConstantExternSym(Name); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 526 | } else { |
| 527 | const Ice::RelocOffsetT Offset = 0; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 528 | return Ctx->getConstantSym(Offset, Name); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Karl Schimpf | a313a12 | 2015-10-08 10:40:57 -0700 | [diff] [blame] | 532 | void reportLinkageError(const char *Kind, |
| 533 | const Ice::GlobalDeclaration &Decl) { |
| 534 | std::string Buffer; |
| 535 | raw_string_ostream StrBuf(Buffer); |
| 536 | StrBuf << Kind << " " << Decl.getName() |
| 537 | << " has incorrect linkage: " << Decl.getLinkageName(); |
| 538 | if (Decl.isExternal()) |
| 539 | StrBuf << "\n Use flag -allow-externally-defined-symbols to override"; |
| 540 | Error(StrBuf.str()); |
| 541 | } |
| 542 | |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 543 | // Converts function declarations into constant value IDs. |
| 544 | void createValueIDsForFunctions() { |
Karl Schimpf | 57d31ac | 2015-10-07 09:53:12 -0700 | [diff] [blame] | 545 | Ice::GlobalContext *Ctx = getTranslator().getContext(); |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 546 | for (const Ice::FunctionDeclaration *Func : FunctionDeclarations) { |
Karl Schimpf | a313a12 | 2015-10-08 10:40:57 -0700 | [diff] [blame] | 547 | if (!Func->verifyLinkageCorrect(Ctx)) |
| 548 | reportLinkageError("Function", *Func); |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 549 | Ice::Constant *C = getConstantSym(Func->getName(), Func->isProto()); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 550 | ValueIDConstants.push_back(C); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Converts global variable declarations into constant value IDs. |
| 555 | void createValueIDsForGlobalVars() { |
| 556 | for (const Ice::VariableDeclaration *Decl : *VariableDeclarations) { |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 557 | if (!Decl->verifyLinkageCorrect()) |
Karl Schimpf | a313a12 | 2015-10-08 10:40:57 -0700 | [diff] [blame] | 558 | reportLinkageError("Global", *Decl); |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 559 | Ice::Constant *C = |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 560 | getConstantSym(Decl->getName(), !Decl->hasInitializer()); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 561 | ValueIDConstants.push_back(C); |
| 562 | } |
| 563 | } |
| 564 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 565 | // Reports that type ID is undefined, or not of the WantedType. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 566 | void reportBadTypeIDAs(NaClBcIndexSize_t ID, const ExtendedType *Ty, |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 567 | ExtendedType::TypeKind WantedType); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 568 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 569 | // Reports that there is no function declaration for ID. Returns an error |
| 570 | // recovery value to use. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 571 | Ice::FunctionDeclaration *reportGetFunctionByIDError(NaClBcIndexSize_t ID); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 572 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 573 | // Reports that there is not global variable declaration for ID. Returns an |
| 574 | // error recovery value to use. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 575 | Ice::VariableDeclaration * |
| 576 | reportGetGlobalVariableByIDError(NaClBcIndexSize_t Index); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 577 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 578 | // Reports that there is no corresponding ICE type for LLVMTy, and returns |
| 579 | // Ice::IceType_void. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 580 | Ice::Type convertToIceTypeError(Type *LLVMTy); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Karl Schimpf | 17b1a13 | 2015-03-12 09:32:06 -0700 | [diff] [blame] | 583 | bool TopLevelParser::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, |
| 584 | const std::string &Message) { |
Karl Schimpf | 819d7b5 | 2015-01-22 10:00:07 -0800 | [diff] [blame] | 585 | Ice::GlobalContext *Context = Translator.getContext(); |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 586 | { |
| 587 | std::unique_lock<Ice::GlobalLockType> _(ErrorReportingLock); |
| 588 | ErrorStatus.assign(Ice::EC_Bitcode); |
| 589 | } |
Karl Schimpf | d8b3289 | 2015-04-16 15:47:25 -0700 | [diff] [blame] | 590 | { // Lock while printing out error message. |
| 591 | Ice::OstreamLocker L(Context); |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 592 | raw_ostream &OldErrStream = setErrStream(Context->getStrError()); |
Karl Schimpf | d8b3289 | 2015-04-16 15:47:25 -0700 | [diff] [blame] | 593 | NaClBitcodeParser::ErrorAt(Level, Bit, Message); |
| 594 | setErrStream(OldErrStream); |
| 595 | } |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 596 | if (Level >= naclbitc::Error && !Ice::getFlags().getAllowErrorRecovery()) |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 597 | Fatal(); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 598 | return true; |
| 599 | } |
| 600 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 601 | void TopLevelParser::reportBadTypeIDAs(NaClBcIndexSize_t ID, |
| 602 | const ExtendedType *Ty, |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 603 | ExtendedType::TypeKind WantedType) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 604 | std::string Buffer; |
| 605 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 606 | if (Ty == nullptr) { |
| 607 | StrBuf << "Can't find extended type for type id: " << ID; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 608 | } else { |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 609 | StrBuf << "Type id " << ID << " not " << WantedType << ". Found: " << *Ty; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 610 | } |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 611 | blockError(StrBuf.str()); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 614 | Ice::FunctionDeclaration * |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 615 | TopLevelParser::reportGetFunctionByIDError(NaClBcIndexSize_t ID) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 616 | std::string Buffer; |
| 617 | raw_string_ostream StrBuf(Buffer); |
| 618 | StrBuf << "Function index " << ID |
| 619 | << " not allowed. Out of range. Must be less than " |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 620 | << FunctionDeclarations.size(); |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 621 | blockError(StrBuf.str()); |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 622 | if (!FunctionDeclarations.empty()) |
| 623 | return FunctionDeclarations[0]; |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 624 | Fatal(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | Ice::VariableDeclaration * |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 628 | TopLevelParser::reportGetGlobalVariableByIDError(NaClBcIndexSize_t Index) { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 629 | std::string Buffer; |
| 630 | raw_string_ostream StrBuf(Buffer); |
| 631 | StrBuf << "Global index " << Index |
| 632 | << " not allowed. Out of range. Must be less than " |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 633 | << VariableDeclarations->size(); |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 634 | blockError(StrBuf.str()); |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 635 | if (!VariableDeclarations->empty()) |
| 636 | return VariableDeclarations->at(0); |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 637 | Fatal(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 640 | Ice::Type TopLevelParser::convertToIceTypeError(Type *LLVMTy) { |
| 641 | std::string Buffer; |
| 642 | raw_string_ostream StrBuf(Buffer); |
| 643 | StrBuf << "Invalid LLVM type: " << *LLVMTy; |
| 644 | Error(StrBuf.str()); |
| 645 | return Ice::IceType_void; |
| 646 | } |
| 647 | |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 648 | void TopLevelParser::verifyFunctionTypeSignatures() { |
| 649 | const Ice::GlobalContext *Ctx = getTranslator().getContext(); |
| 650 | for (Ice::FunctionDeclaration *FuncDecl : FunctionDeclarations) { |
| 651 | if (!FuncDecl->validateTypeSignature(Ctx)) |
| 652 | Error(FuncDecl->getTypeSignatureError(Ctx)); |
| 653 | } |
| 654 | } |
| 655 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 656 | // Base class for parsing blocks within the bitcode file. Note: Because this is |
| 657 | // the base class of block parsers, we generate error messages if ParseBlock or |
| 658 | // ParseRecord is not overridden in derived classes. |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 659 | class BlockParserBaseClass : public NaClBitcodeParser { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 660 | BlockParserBaseClass() = delete; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 661 | BlockParserBaseClass(const BlockParserBaseClass &) = delete; |
| 662 | BlockParserBaseClass &operator=(const BlockParserBaseClass &) = delete; |
| 663 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 664 | public: |
| 665 | // Constructor for the top-level module block parser. |
| 666 | BlockParserBaseClass(unsigned BlockID, TopLevelParser *Context) |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 667 | : NaClBitcodeParser(BlockID, Context), Context(Context) {} |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 668 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 669 | BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser, |
| 670 | NaClBitstreamCursor &Cursor) |
| 671 | : NaClBitcodeParser(BlockID, EnclosingParser, Cursor), |
| 672 | Context(EnclosingParser->Context) {} |
| 673 | |
| 674 | ~BlockParserBaseClass() override {} |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 675 | |
| 676 | // Returns the printable name of the type of block being parsed. |
| 677 | virtual const char *getBlockName() const { |
| 678 | // If this class is used, it is parsing an unknown block. |
| 679 | return "unknown"; |
| 680 | } |
| 681 | |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 682 | // Generates an error Message with the Bit address prefixed to it. |
Karl Schimpf | 17b1a13 | 2015-03-12 09:32:06 -0700 | [diff] [blame] | 683 | bool ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 684 | const std::string &Message) override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 685 | |
| 686 | protected: |
| 687 | // The context parser that contains the decoded state. |
| 688 | TopLevelParser *Context; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 689 | // True if ErrorAt has been called in this block. |
| 690 | bool BlockHasError = false; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 691 | |
| 692 | // Constructor for nested block parsers. |
| 693 | BlockParserBaseClass(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 694 | : NaClBitcodeParser(BlockID, EnclosingParser), |
| 695 | Context(EnclosingParser->Context) {} |
| 696 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 697 | // Gets the translator associated with the bitcode parser. |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 698 | Ice::Translator &getTranslator() const { return Context->getTranslator(); } |
| 699 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 700 | // Default implementation. Reports that block is unknown and skips its |
| 701 | // contents. |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 702 | bool ParseBlock(unsigned BlockID) override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 703 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 704 | // Default implementation. Reports that the record is not understood. |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 705 | void ProcessRecord() override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 706 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 707 | // Checks if the size of the record is Size. Return true if valid. Otherwise |
| 708 | // generates an error and returns false. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 709 | bool isValidRecordSize(size_t Size, const char *RecordName) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 710 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 711 | if (Values.size() == Size) |
| 712 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 713 | reportRecordSizeError(Size, RecordName, nullptr); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 714 | return false; |
| 715 | } |
| 716 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 717 | // Checks if the size of the record is at least as large as the LowerLimit. |
| 718 | // Returns true if valid. Otherwise generates an error and returns false. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 719 | bool isValidRecordSizeAtLeast(size_t LowerLimit, const char *RecordName) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 720 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 721 | if (Values.size() >= LowerLimit) |
| 722 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 723 | reportRecordSizeError(LowerLimit, RecordName, "at least"); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 727 | // Checks if the size of the record is no larger than the |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 728 | // UpperLimit. Returns true if valid. Otherwise generates an error and |
| 729 | // returns false. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 730 | bool isValidRecordSizeAtMost(size_t UpperLimit, const char *RecordName) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 731 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 732 | if (Values.size() <= UpperLimit) |
| 733 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 734 | reportRecordSizeError(UpperLimit, RecordName, "no more than"); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 735 | return false; |
| 736 | } |
| 737 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 738 | // Checks if the size of the record is at least as large as the LowerLimit, |
| 739 | // and no larger than the UpperLimit. Returns true if valid. Otherwise |
| 740 | // generates an error and returns false. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 741 | bool isValidRecordSizeInRange(size_t LowerLimit, size_t UpperLimit, |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 742 | const char *RecordName) { |
| 743 | return isValidRecordSizeAtLeast(LowerLimit, RecordName) || |
| 744 | isValidRecordSizeAtMost(UpperLimit, RecordName); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | private: |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 748 | /// Generates a record size error. ExpectedSize is the number of elements |
| 749 | /// expected. RecordName is the name of the kind of record that has incorrect |
| 750 | /// size. ContextMessage (if not nullptr) is appended to "record expects" to |
| 751 | /// describe how ExpectedSize should be interpreted. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 752 | void reportRecordSizeError(size_t ExpectedSize, const char *RecordName, |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 753 | const char *ContextMessage); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 754 | }; |
| 755 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 756 | bool TopLevelParser::blockError(const std::string &Message) { |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 757 | // TODO(kschimpf): Remove this method. This method used to redirect |
| 758 | // block-level errors to the block we are in, rather than the top-level |
| 759 | // block. This gave better bit location for error messages. However, with |
| 760 | // parallel parsing, we can't keep a field to redirect (there could be many |
| 761 | // and we don't know which block parser applies). Hence, This redirect can't |
| 762 | // be applied anymore. |
| 763 | return Error(Message); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Generates an error Message with the bit address prefixed to it. |
Jan Voung | f644a4b | 2015-03-19 11:57:52 -0700 | [diff] [blame] | 767 | bool BlockParserBaseClass::ErrorAt(naclbitc::ErrorLevel Level, uint64_t Bit, |
| 768 | const std::string &Message) { |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 769 | BlockHasError = true; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 770 | std::string Buffer; |
| 771 | raw_string_ostream StrBuf(Buffer); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 772 | // Note: If dump routines have been turned off, the error messages will not |
| 773 | // be readable. Hence, replace with simple error. We also use the simple form |
| 774 | // for unit tests. |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 775 | if (Ice::getFlags().getGenerateUnitTestMessages()) { |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 776 | StrBuf << "Invalid " << getBlockName() << " record: <" << Record.GetCode(); |
| 777 | for (const uint64_t Val : Record.GetValues()) { |
| 778 | StrBuf << " " << Val; |
| 779 | } |
| 780 | StrBuf << ">"; |
Karl Schimpf | df80eb8 | 2015-02-09 14:20:22 -0800 | [diff] [blame] | 781 | } else { |
| 782 | StrBuf << Message; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 783 | } |
Karl Schimpf | 2a9d186 | 2016-04-02 10:06:56 -0700 | [diff] [blame] | 784 | return Context->ErrorAt(Level, Record.GetCursor().getErrorBitNo(Bit), |
| 785 | StrBuf.str()); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 788 | void BlockParserBaseClass::reportRecordSizeError(size_t ExpectedSize, |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 789 | const char *RecordName, |
| 790 | const char *ContextMessage) { |
| 791 | std::string Buffer; |
| 792 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 793 | const char *BlockName = getBlockName(); |
| 794 | const char FirstChar = toupper(*BlockName); |
| 795 | StrBuf << FirstChar << (BlockName + 1) << " " << RecordName |
| 796 | << " record expects"; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 797 | if (ContextMessage) |
| 798 | StrBuf << " " << ContextMessage; |
| 799 | StrBuf << " " << ExpectedSize << " argument"; |
| 800 | if (ExpectedSize > 1) |
| 801 | StrBuf << "s"; |
| 802 | StrBuf << ". Found: " << Record.GetValues().size(); |
| 803 | Error(StrBuf.str()); |
| 804 | } |
| 805 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 806 | bool BlockParserBaseClass::ParseBlock(unsigned BlockID) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 807 | // If called, derived class doesn't know how to handle block. Report error |
| 808 | // and skip. |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 809 | std::string Buffer; |
| 810 | raw_string_ostream StrBuf(Buffer); |
| 811 | StrBuf << "Don't know how to parse block id: " << BlockID; |
| 812 | Error(StrBuf.str()); |
| 813 | SkipBlock(); |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | void BlockParserBaseClass::ProcessRecord() { |
| 818 | // If called, derived class doesn't know how to handle. |
| 819 | std::string Buffer; |
| 820 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 821 | StrBuf << "Don't know how to process " << getBlockName() |
| 822 | << " record:" << Record; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 823 | Error(StrBuf.str()); |
| 824 | } |
| 825 | |
| 826 | // Class to parse a types block. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 827 | class TypesParser final : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 828 | TypesParser() = delete; |
| 829 | TypesParser(const TypesParser &) = delete; |
| 830 | TypesParser &operator=(const TypesParser &) = delete; |
| 831 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 832 | public: |
| 833 | TypesParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 834 | : BlockParserBaseClass(BlockID, EnclosingParser), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 835 | Timer(Ice::TimerStack::TT_parseTypes, getTranslator().getContext()) {} |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 836 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 837 | ~TypesParser() override { |
| 838 | if (ExpectedNumTypes != Context->getNumTypeIDValues()) { |
| 839 | std::string Buffer; |
| 840 | raw_string_ostream StrBuf(Buffer); |
| 841 | StrBuf << "Types block expected " << ExpectedNumTypes |
| 842 | << " types but found: " << NextTypeId; |
| 843 | Error(StrBuf.str()); |
| 844 | } |
| 845 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 846 | |
| 847 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 848 | Ice::TimerMarker Timer; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 849 | // The type ID that will be associated with the next type defining record in |
| 850 | // the types block. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 851 | NaClBcIndexSize_t NextTypeId = 0; |
| 852 | |
| 853 | // The expected number of types, based on record TYPE_CODE_NUMENTRY. |
| 854 | NaClBcIndexSize_t ExpectedNumTypes = 0; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 855 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 856 | void ProcessRecord() override; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 857 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 858 | const char *getBlockName() const override { return "type"; } |
| 859 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 860 | void setNextTypeIDAsSimpleType(Ice::Type Ty) { |
| 861 | Context->getTypeByIDForDefining(NextTypeId++)->setAsSimpleType(Ty); |
| 862 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 863 | }; |
| 864 | |
| 865 | void TypesParser::ProcessRecord() { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 866 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 867 | switch (Record.GetCode()) { |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 868 | case naclbitc::TYPE_CODE_NUMENTRY: { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 869 | // NUMENTRY: [numentries] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 870 | if (!isValidRecordSize(1, "count")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 871 | return; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 872 | uint64_t Size = Values[0]; |
| 873 | if (Size > NaClBcIndexSize_t_Max) { |
| 874 | std::string Buffer; |
| 875 | raw_string_ostream StrBuf(Buffer); |
| 876 | StrBuf << "Size to big for count record: " << Size; |
| 877 | Error(StrBuf.str()); |
| 878 | ExpectedNumTypes = NaClBcIndexSize_t_Max; |
| 879 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 880 | // The code double checks that Expected size and the actual size at the end |
| 881 | // of the block. To reduce allocations we preallocate the space. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 882 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 883 | // However, if the number is large, we suspect that the number is |
| 884 | // (possibly) incorrect. In that case, we preallocate a smaller space. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 885 | constexpr uint64_t DefaultLargeResizeValue = 1000000; |
| 886 | Context->resizeTypeIDValues(std::min(Size, DefaultLargeResizeValue)); |
| 887 | ExpectedNumTypes = Size; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 888 | return; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 889 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 890 | case naclbitc::TYPE_CODE_VOID: |
| 891 | // VOID |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 892 | if (!isValidRecordSize(0, "void")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 893 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 894 | setNextTypeIDAsSimpleType(Ice::IceType_void); |
| 895 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 896 | case naclbitc::TYPE_CODE_FLOAT: |
| 897 | // FLOAT |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 898 | if (!isValidRecordSize(0, "float")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 899 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 900 | setNextTypeIDAsSimpleType(Ice::IceType_f32); |
| 901 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 902 | case naclbitc::TYPE_CODE_DOUBLE: |
| 903 | // DOUBLE |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 904 | if (!isValidRecordSize(0, "double")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 905 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 906 | setNextTypeIDAsSimpleType(Ice::IceType_f64); |
| 907 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 908 | case naclbitc::TYPE_CODE_INTEGER: |
| 909 | // INTEGER: [width] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 910 | if (!isValidRecordSize(1, "integer")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 911 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 912 | switch (Values[0]) { |
| 913 | case 1: |
| 914 | setNextTypeIDAsSimpleType(Ice::IceType_i1); |
| 915 | return; |
| 916 | case 8: |
| 917 | setNextTypeIDAsSimpleType(Ice::IceType_i8); |
| 918 | return; |
| 919 | case 16: |
| 920 | setNextTypeIDAsSimpleType(Ice::IceType_i16); |
| 921 | return; |
| 922 | case 32: |
| 923 | setNextTypeIDAsSimpleType(Ice::IceType_i32); |
| 924 | return; |
| 925 | case 64: |
| 926 | setNextTypeIDAsSimpleType(Ice::IceType_i64); |
| 927 | return; |
| 928 | default: |
| 929 | break; |
| 930 | } |
| 931 | { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 932 | std::string Buffer; |
| 933 | raw_string_ostream StrBuf(Buffer); |
| 934 | StrBuf << "Type integer record with invalid bitsize: " << Values[0]; |
| 935 | Error(StrBuf.str()); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 936 | } |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 937 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 938 | case naclbitc::TYPE_CODE_VECTOR: { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 939 | // VECTOR: [numelts, eltty] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 940 | if (!isValidRecordSize(2, "vector")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 941 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 942 | Ice::Type BaseTy = Context->getSimpleTypeByID(Values[1]); |
| 943 | Ice::SizeT Size = Values[0]; |
| 944 | switch (BaseTy) { |
| 945 | case Ice::IceType_i1: |
| 946 | switch (Size) { |
| 947 | case 4: |
| 948 | setNextTypeIDAsSimpleType(Ice::IceType_v4i1); |
| 949 | return; |
| 950 | case 8: |
| 951 | setNextTypeIDAsSimpleType(Ice::IceType_v8i1); |
| 952 | return; |
| 953 | case 16: |
| 954 | setNextTypeIDAsSimpleType(Ice::IceType_v16i1); |
| 955 | return; |
| 956 | default: |
| 957 | break; |
| 958 | } |
| 959 | break; |
| 960 | case Ice::IceType_i8: |
| 961 | if (Size == 16) { |
| 962 | setNextTypeIDAsSimpleType(Ice::IceType_v16i8); |
| 963 | return; |
| 964 | } |
| 965 | break; |
| 966 | case Ice::IceType_i16: |
| 967 | if (Size == 8) { |
| 968 | setNextTypeIDAsSimpleType(Ice::IceType_v8i16); |
| 969 | return; |
| 970 | } |
| 971 | break; |
| 972 | case Ice::IceType_i32: |
| 973 | if (Size == 4) { |
| 974 | setNextTypeIDAsSimpleType(Ice::IceType_v4i32); |
| 975 | return; |
| 976 | } |
| 977 | break; |
| 978 | case Ice::IceType_f32: |
| 979 | if (Size == 4) { |
| 980 | setNextTypeIDAsSimpleType(Ice::IceType_v4f32); |
| 981 | return; |
| 982 | } |
| 983 | break; |
| 984 | default: |
| 985 | break; |
| 986 | } |
| 987 | { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 988 | std::string Buffer; |
| 989 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 990 | StrBuf << "Invalid type vector record: <" << Values[0] << " x " << BaseTy |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 991 | << ">"; |
| 992 | Error(StrBuf.str()); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 993 | } |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 994 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 995 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 996 | case naclbitc::TYPE_CODE_FUNCTION: { |
| 997 | // FUNCTION: [vararg, retty, paramty x N] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 998 | if (!isValidRecordSizeAtLeast(2, "signature")) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 999 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1000 | if (Values[0]) |
| 1001 | Error("Function type can't define varargs"); |
| 1002 | ExtendedType *Ty = Context->getTypeByIDForDefining(NextTypeId++); |
| 1003 | Ty->setAsFunctionType(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 1004 | auto *FuncTy = cast<FuncSigExtendedType>(Ty); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1005 | FuncTy->setReturnType(Context->getSimpleTypeByID(Values[1])); |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1006 | for (size_t i = 2, e = Values.size(); i != e; ++i) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1007 | // Check that type void not used as argument type. Note: PNaCl |
| 1008 | // restrictions can't be checked until we know the name, because we have |
| 1009 | // to check for intrinsic signatures. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1010 | Ice::Type ArgTy = Context->getSimpleTypeByID(Values[i]); |
| 1011 | if (ArgTy == Ice::IceType_void) { |
| 1012 | std::string Buffer; |
| 1013 | raw_string_ostream StrBuf(Buffer); |
| 1014 | StrBuf << "Type for parameter " << (i - 1) |
| 1015 | << " not valid. Found: " << ArgTy; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1016 | ArgTy = Ice::IceType_i32; |
| 1017 | } |
| 1018 | FuncTy->appendArgType(ArgTy); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1019 | } |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1020 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1021 | } |
| 1022 | default: |
| 1023 | BlockParserBaseClass::ProcessRecord(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1024 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1025 | } |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 1026 | llvm_unreachable("Unknown type block record not processed!"); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1029 | /// Parses the globals block (i.e. global variable declarations and |
| 1030 | /// corresponding initializers). |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1031 | class GlobalsParser final : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 1032 | GlobalsParser() = delete; |
| 1033 | GlobalsParser(const GlobalsParser &) = delete; |
| 1034 | GlobalsParser &operator=(const GlobalsParser &) = delete; |
| 1035 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1036 | public: |
| 1037 | GlobalsParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 1038 | : BlockParserBaseClass(BlockID, EnclosingParser), |
| 1039 | Timer(Ice::TimerStack::TT_parseGlobals, getTranslator().getContext()), |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1040 | NumFunctionIDs(Context->getNumFunctionIDs()), |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1041 | DummyGlobalVar(Ice::VariableDeclaration::create( |
| 1042 | Context->getGlobalVariablesPool())), |
| 1043 | CurGlobalVar(DummyGlobalVar) { |
| 1044 | Context->getGlobalVariablesPool()->willNotBeEmitted(DummyGlobalVar); |
| 1045 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1046 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1047 | ~GlobalsParser() override = default; |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 1048 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1049 | const char *getBlockName() const override { return "globals"; } |
| 1050 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1051 | private: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 1052 | using GlobalVarsMapType = |
| 1053 | std::unordered_map<NaClBcIndexSize_t, Ice::VariableDeclaration *>; |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1054 | |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 1055 | Ice::TimerMarker Timer; |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1056 | |
| 1057 | // Holds global variables generated/referenced in the global variables block. |
| 1058 | GlobalVarsMapType GlobalVarsMap; |
| 1059 | |
| 1060 | // Holds the number of defined function IDs. |
| 1061 | NaClBcIndexSize_t NumFunctionIDs; |
| 1062 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1063 | // Holds the specified number of global variables by the count record in the |
| 1064 | // global variables block. |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1065 | NaClBcIndexSize_t SpecifiedNumberVars = 0; |
| 1066 | |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1067 | // Keeps track of how many initializers are expected for the global variable |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1068 | // declaration being built. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1069 | NaClBcIndexSize_t InitializersNeeded = 0; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1070 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1071 | // The index of the next global variable declaration. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1072 | NaClBcIndexSize_t NextGlobalID = 0; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1073 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1074 | // Dummy global variable declaration to guarantee CurGlobalVar is always |
| 1075 | // defined (allowing code to not need to check if CurGlobalVar is nullptr). |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1076 | Ice::VariableDeclaration *DummyGlobalVar; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1077 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1078 | // Holds the current global variable declaration being built. |
| 1079 | Ice::VariableDeclaration *CurGlobalVar; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1080 | |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1081 | // Returns the global variable associated with the given Index. |
| 1082 | Ice::VariableDeclaration *getGlobalVarByID(NaClBcIndexSize_t Index) { |
| 1083 | Ice::VariableDeclaration *&Decl = GlobalVarsMap[Index]; |
| 1084 | if (Decl == nullptr) |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1085 | Decl = |
| 1086 | Ice::VariableDeclaration::create(Context->getGlobalVariablesPool()); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1087 | return Decl; |
| 1088 | } |
| 1089 | |
| 1090 | // Returns the global declaration associated with the given index. |
| 1091 | Ice::GlobalDeclaration *getGlobalDeclByID(NaClBcIndexSize_t Index) { |
| 1092 | if (Index < NumFunctionIDs) |
| 1093 | return Context->getFunctionByID(Index); |
| 1094 | return getGlobalVarByID(Index - NumFunctionIDs); |
| 1095 | } |
| 1096 | |
| 1097 | // If global variables parsed correctly, install them into the top-level |
| 1098 | // context. |
| 1099 | void installGlobalVariables() { |
| 1100 | // Verify specified number of globals matches number found. |
| 1101 | size_t NumGlobals = GlobalVarsMap.size(); |
| 1102 | if (SpecifiedNumberVars != NumGlobals || |
| 1103 | SpecifiedNumberVars != NextGlobalID) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1104 | std::string Buffer; |
| 1105 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1106 | StrBuf << getBlockName() << " block expects " << SpecifiedNumberVars |
| 1107 | << " global variables. Found: " << GlobalVarsMap.size(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1108 | Error(StrBuf.str()); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1109 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1110 | } |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1111 | // Install global variables into top-level context. |
| 1112 | for (size_t I = 0; I < NumGlobals; ++I) |
| 1113 | Context->addGlobalDeclaration(GlobalVarsMap[I]); |
| 1114 | } |
| 1115 | |
| 1116 | void ExitBlock() override { |
| 1117 | verifyNoMissingInitializers(); |
| 1118 | installGlobalVariables(); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1119 | BlockParserBaseClass::ExitBlock(); |
| 1120 | } |
| 1121 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 1122 | void ProcessRecord() override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1123 | |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1124 | // Checks if the number of initializers for the CurGlobalVar is the same as |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1125 | // the number found in the bitcode file. If different, and error message is |
| 1126 | // generated, and the internal state of the parser is fixed so this condition |
| 1127 | // is no longer violated. |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1128 | void verifyNoMissingInitializers() { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1129 | size_t NumInits = CurGlobalVar->getInitializers().size(); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1130 | if (InitializersNeeded != NumInits) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1131 | std::string Buffer; |
| 1132 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1133 | StrBuf << "Global variable @g" << NextGlobalID << " expected " |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1134 | << InitializersNeeded << " initializer"; |
| 1135 | if (InitializersNeeded > 1) |
| 1136 | StrBuf << "s"; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1137 | StrBuf << ". Found: " << NumInits; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1138 | Error(StrBuf.str()); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1139 | InitializersNeeded = NumInits; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1140 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1141 | } |
| 1142 | }; |
| 1143 | |
| 1144 | void GlobalsParser::ProcessRecord() { |
| 1145 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 1146 | switch (Record.GetCode()) { |
| 1147 | case naclbitc::GLOBALVAR_COUNT: |
| 1148 | // COUNT: [n] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1149 | if (!isValidRecordSize(1, "count")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1150 | return; |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1151 | if (SpecifiedNumberVars || NextGlobalID) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1152 | Error("Globals count record not first in block."); |
| 1153 | return; |
| 1154 | } |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1155 | SpecifiedNumberVars = Values[0]; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1156 | return; |
| 1157 | case naclbitc::GLOBALVAR_VAR: { |
| 1158 | // VAR: [align, isconst] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1159 | if (!isValidRecordSize(2, "variable")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1160 | return; |
| 1161 | verifyNoMissingInitializers(); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1162 | // Always build the global variable, even if IR generation is turned off. |
| 1163 | // This is needed because we need a placeholder in the top-level context |
| 1164 | // when no IR is generated. |
Karl Schimpf | 07af2ac | 2015-09-30 15:33:41 -0700 | [diff] [blame] | 1165 | uint32_t Alignment = |
| 1166 | Context->extractAlignment(this, "Global variable", Values[0]); |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1167 | CurGlobalVar = getGlobalVarByID(NextGlobalID); |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 1168 | InitializersNeeded = 1; |
| 1169 | CurGlobalVar->setAlignment(Alignment); |
| 1170 | CurGlobalVar->setIsConstant(Values[1] != 0); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1171 | ++NextGlobalID; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | case naclbitc::GLOBALVAR_COMPOUND: |
| 1175 | // COMPOUND: [size] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1176 | if (!isValidRecordSize(1, "compound")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1177 | return; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1178 | if (!CurGlobalVar->getInitializers().empty()) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1179 | Error("Globals compound record not first initializer"); |
| 1180 | return; |
| 1181 | } |
| 1182 | if (Values[0] < 2) { |
| 1183 | std::string Buffer; |
| 1184 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1185 | StrBuf << getBlockName() |
| 1186 | << " compound record size invalid. Found: " << Values[0]; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1187 | Error(StrBuf.str()); |
| 1188 | return; |
| 1189 | } |
| 1190 | InitializersNeeded = Values[0]; |
| 1191 | return; |
| 1192 | case naclbitc::GLOBALVAR_ZEROFILL: { |
| 1193 | // ZEROFILL: [size] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1194 | if (!isValidRecordSize(1, "zerofill")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1195 | return; |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1196 | auto *Pool = Context->getGlobalVariablesPool(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1197 | CurGlobalVar->addInitializer( |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1198 | Ice::VariableDeclaration::ZeroInitializer::create(Pool, Values[0])); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1199 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1200 | } |
| 1201 | case naclbitc::GLOBALVAR_DATA: { |
| 1202 | // DATA: [b0, b1, ...] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1203 | if (!isValidRecordSizeAtLeast(1, "data")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1204 | return; |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1205 | auto *Pool = Context->getGlobalVariablesPool(); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1206 | CurGlobalVar->addInitializer( |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1207 | Ice::VariableDeclaration::DataInitializer::create(Pool, Values)); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1208 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1209 | } |
| 1210 | case naclbitc::GLOBALVAR_RELOC: { |
| 1211 | // RELOC: [val, [addend]] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1212 | if (!isValidRecordSizeInRange(1, 2, "reloc")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1213 | return; |
Karl Schimpf | aa0ce79 | 2015-08-07 09:28:57 -0700 | [diff] [blame] | 1214 | NaClBcIndexSize_t Index = Values[0]; |
| 1215 | NaClBcIndexSize_t IndexLimit = SpecifiedNumberVars + NumFunctionIDs; |
| 1216 | if (Index >= IndexLimit) { |
| 1217 | std::string Buffer; |
| 1218 | raw_string_ostream StrBuf(Buffer); |
| 1219 | StrBuf << "Relocation index " << Index << " to big. Expect index < " |
| 1220 | << IndexLimit; |
| 1221 | Error(StrBuf.str()); |
| 1222 | } |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1223 | uint64_t Offset = 0; |
| 1224 | if (Values.size() == 2) { |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1225 | Offset = Values[1]; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1226 | if (Offset > std::numeric_limits<uint32_t>::max()) { |
| 1227 | std::string Buffer; |
| 1228 | raw_string_ostream StrBuf(Buffer); |
| 1229 | StrBuf << "Addend of global reloc record too big: " << Offset; |
| 1230 | Error(StrBuf.str()); |
| 1231 | } |
| 1232 | } |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1233 | auto *Pool = Context->getGlobalVariablesPool(); |
John Porto | 844211e | 2016-02-04 08:42:48 -0800 | [diff] [blame] | 1234 | Ice::GlobalContext *Ctx = getTranslator().getContext(); |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 1235 | CurGlobalVar->addInitializer( |
| 1236 | Ice::VariableDeclaration::RelocInitializer::create( |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 1237 | Pool, getGlobalDeclByID(Index), |
| 1238 | {Ice::RelocOffset::create(Ctx, Offset)})); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1239 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1240 | } |
| 1241 | default: |
| 1242 | BlockParserBaseClass::ProcessRecord(); |
| 1243 | return; |
| 1244 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1247 | /// Base class for parsing a valuesymtab block in the bitcode file. |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1248 | class ValuesymtabParser : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 1249 | ValuesymtabParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 1250 | ValuesymtabParser(const ValuesymtabParser &) = delete; |
| 1251 | void operator=(const ValuesymtabParser &) = delete; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1252 | |
| 1253 | public: |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1254 | ValuesymtabParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser) |
| 1255 | : BlockParserBaseClass(BlockID, EnclosingParser) {} |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1256 | |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 1257 | ~ValuesymtabParser() override = default; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1258 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1259 | const char *getBlockName() const override { return "valuesymtab"; } |
| 1260 | |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1261 | protected: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 1262 | using StringType = SmallString<128>; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1263 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1264 | // Returns the name to identify the kind of symbol table this is |
| 1265 | // in error messages. |
| 1266 | virtual const char *getTableKind() const = 0; |
| 1267 | |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1268 | // Associates Name with the value defined by the given Index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1269 | virtual void setValueName(NaClBcIndexSize_t Index, StringType &Name) = 0; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1270 | |
| 1271 | // Associates Name with the value defined by the given Index; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1272 | virtual void setBbName(NaClBcIndexSize_t Index, StringType &Name) = 0; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1273 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1274 | // Reports that the assignment of Name to the value associated with |
| 1275 | // index is not possible, for the given Context. |
| 1276 | void reportUnableToAssign(const char *Context, NaClBcIndexSize_t Index, |
| 1277 | StringType &Name); |
| 1278 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1279 | private: |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1280 | using NamesSetType = std::unordered_set<StringType>; |
| 1281 | NamesSetType ValueNames; |
| 1282 | NamesSetType BlockNames; |
| 1283 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 1284 | void ProcessRecord() override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1285 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1286 | // Extracts out ConvertedName. Returns true if unique wrt to Names. |
| 1287 | bool convertToString(NamesSetType &Names, StringType &ConvertedName) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1288 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 1289 | for (size_t i = 1, e = Values.size(); i != e; ++i) { |
| 1290 | ConvertedName += static_cast<char>(Values[i]); |
| 1291 | } |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1292 | auto Pair = Names.insert(ConvertedName); |
| 1293 | return Pair.second; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1294 | } |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1295 | |
| 1296 | void ReportDuplicateName(const char *NameCat, StringType &Name); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1297 | }; |
| 1298 | |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1299 | void ValuesymtabParser::reportUnableToAssign(const char *Context, |
| 1300 | NaClBcIndexSize_t Index, |
| 1301 | StringType &Name) { |
| 1302 | std::string Buffer; |
| 1303 | raw_string_ostream StrBuf(Buffer); |
| 1304 | StrBuf << getTableKind() << " " << getBlockName() << ": " << Context |
| 1305 | << " name '" << Name << "' can't be associated with index " << Index; |
| 1306 | Error(StrBuf.str()); |
| 1307 | } |
| 1308 | |
| 1309 | void ValuesymtabParser::ReportDuplicateName(const char *NameCat, |
| 1310 | StringType &Name) { |
| 1311 | std::string Buffer; |
| 1312 | raw_string_ostream StrBuf(Buffer); |
| 1313 | StrBuf << getTableKind() << " " << getBlockName() << " defines duplicate " |
| 1314 | << NameCat << " name: '" << Name << "'"; |
| 1315 | Error(StrBuf.str()); |
| 1316 | } |
| 1317 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1318 | void ValuesymtabParser::ProcessRecord() { |
| 1319 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 1320 | StringType ConvertedName; |
| 1321 | switch (Record.GetCode()) { |
| 1322 | case naclbitc::VST_CODE_ENTRY: { |
| 1323 | // VST_ENTRY: [ValueId, namechar x N] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1324 | if (!isValidRecordSizeAtLeast(2, "value entry")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1325 | return; |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1326 | if (convertToString(ValueNames, ConvertedName)) |
| 1327 | setValueName(Values[0], ConvertedName); |
| 1328 | else |
| 1329 | ReportDuplicateName("value", ConvertedName); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1330 | return; |
| 1331 | } |
| 1332 | case naclbitc::VST_CODE_BBENTRY: { |
| 1333 | // VST_BBENTRY: [BbId, namechar x N] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1334 | if (!isValidRecordSizeAtLeast(2, "basic block entry")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1335 | return; |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 1336 | if (convertToString(BlockNames, ConvertedName)) |
| 1337 | setBbName(Values[0], ConvertedName); |
| 1338 | else |
| 1339 | ReportDuplicateName("block", ConvertedName); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 1340 | return; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 1341 | } |
| 1342 | default: |
| 1343 | break; |
| 1344 | } |
| 1345 | // If reached, don't know how to handle record. |
| 1346 | BlockParserBaseClass::ProcessRecord(); |
| 1347 | return; |
| 1348 | } |
| 1349 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1350 | /// Parses function blocks in the bitcode file. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1351 | class FunctionParser final : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 1352 | FunctionParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 1353 | FunctionParser(const FunctionParser &) = delete; |
| 1354 | FunctionParser &operator=(const FunctionParser &) = delete; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1355 | |
| 1356 | public: |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1357 | FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser, |
| 1358 | NaClBcIndexSize_t FcnId) |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1359 | : BlockParserBaseClass(BlockID, EnclosingParser), |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 1360 | Timer(Ice::TimerStack::TT_parseFunctions, getTranslator().getContext()), |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1361 | Func(nullptr), FuncDecl(Context->getFunctionByID(FcnId)), |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1362 | CachedNumGlobalValueIDs(Context->getNumGlobalIDs()), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1363 | NextLocalInstIndex(Context->getNumGlobalIDs()) {} |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 1364 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1365 | FunctionParser(unsigned BlockID, BlockParserBaseClass *EnclosingParser, |
| 1366 | NaClBcIndexSize_t FcnId, NaClBitstreamCursor &Cursor) |
| 1367 | : BlockParserBaseClass(BlockID, EnclosingParser, Cursor), |
| 1368 | Timer(Ice::TimerStack::TT_parseFunctions, getTranslator().getContext()), |
| 1369 | Func(nullptr), FuncDecl(Context->getFunctionByID(FcnId)), |
| 1370 | CachedNumGlobalValueIDs(Context->getNumGlobalIDs()), |
| 1371 | NextLocalInstIndex(Context->getNumGlobalIDs()) {} |
| 1372 | |
| 1373 | std::unique_ptr<Ice::Cfg> parseFunction(uint32_t SeqNumber) { |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1374 | bool ParserResult; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1375 | Ice::GlobalContext *Ctx = getTranslator().getContext(); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1376 | { |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1377 | Ice::TimerMarker T(Ctx, FuncDecl->getName().toStringOrEmpty()); |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 1378 | // Note: The Cfg is created, even when IR generation is disabled. This is |
| 1379 | // done to install a CfgLocalAllocator for various internal containers. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 1380 | Ice::GlobalContext *Ctx = getTranslator().getContext(); |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1381 | Func = Ice::Cfg::create(Ctx, SeqNumber); |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 1382 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1383 | Ice::CfgLocalAllocatorScope _(Func.get()); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 1384 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1385 | // TODO(kschimpf) Clean up API to add a function signature to a CFG. |
| 1386 | const Ice::FuncSigType &Signature = FuncDecl->getSignature(); |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 1387 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 1388 | Func->setFunctionName(FuncDecl->getName()); |
| 1389 | Func->setReturnType(Signature.getReturnType()); |
| 1390 | Func->setInternal(FuncDecl->getLinkage() == GlobalValue::InternalLinkage); |
| 1391 | CurrentNode = installNextBasicBlock(); |
| 1392 | Func->setEntryNode(CurrentNode); |
| 1393 | for (Ice::Type ArgType : Signature.getArgList()) { |
| 1394 | Func->addArg(getNextInstVar(ArgType)); |
| 1395 | } |
| 1396 | |
| 1397 | ParserResult = ParseThisBlock(); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1400 | if (ParserResult || BlockHasError) |
| 1401 | Func->setError("Unable to parse function"); |
| 1402 | |
| 1403 | return std::move(Func); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1406 | ~FunctionParser() override = default; |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 1407 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1408 | const char *getBlockName() const override { return "function"; } |
| 1409 | |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 1410 | Ice::Cfg *getFunc() const { return Func.get(); } |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1411 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1412 | size_t getNumGlobalIDs() const { return CachedNumGlobalValueIDs; } |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1413 | |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 1414 | void setNextLocalInstIndex(Ice::Operand *Op) { |
| 1415 | setOperand(NextLocalInstIndex++, Op); |
| 1416 | } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1417 | |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 1418 | // Set the next constant ID to the given constant C. |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 1419 | void setNextConstantID(Ice::Constant *C) { setNextLocalInstIndex(C); } |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 1420 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1421 | // Returns the value referenced by the given value Index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1422 | Ice::Operand *getOperand(NaClBcIndexSize_t Index) { |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1423 | if (Index < CachedNumGlobalValueIDs) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 1424 | return Context->getGlobalConstantByID(Index); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1425 | } |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1426 | NaClBcIndexSize_t LocalIndex = Index - CachedNumGlobalValueIDs; |
Karl Schimpf | 9d25e62 | 2015-09-16 13:47:01 -0700 | [diff] [blame] | 1427 | if (LocalIndex >= LocalOperands.size()) |
| 1428 | reportGetOperandUndefined(Index); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1429 | Ice::Operand *Op = LocalOperands[LocalIndex]; |
Karl Schimpf | 9d25e62 | 2015-09-16 13:47:01 -0700 | [diff] [blame] | 1430 | if (Op == nullptr) |
| 1431 | reportGetOperandUndefined(Index); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 1432 | return Op; |
| 1433 | } |
| 1434 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1435 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 1436 | Ice::TimerMarker Timer; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1437 | // The number of words in the bitstream defining the function block. |
| 1438 | uint64_t NumBytesDefiningFunction = 0; |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1439 | // Maximum number of records that can appear in the function block, based on |
| 1440 | // the number of bytes defining the function block. |
| 1441 | uint64_t MaxRecordsInBlock = 0; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1442 | // The corresponding ICE function defined by the function block. |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 1443 | std::unique_ptr<Ice::Cfg> Func; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1444 | // The index to the current basic block being built. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1445 | NaClBcIndexSize_t CurrentBbIndex = 0; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1446 | // The number of basic blocks declared for the function block. |
| 1447 | NaClBcIndexSize_t DeclaredNumberBbs = 0; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1448 | // The basic block being built. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1449 | Ice::CfgNode *CurrentNode = nullptr; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 1450 | // The corresponding function declaration. |
| 1451 | Ice::FunctionDeclaration *FuncDecl; |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1452 | // Holds the dividing point between local and global absolute value indices. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1453 | size_t CachedNumGlobalValueIDs; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1454 | // Holds operands local to the function block, based on indices defined in |
| 1455 | // the bitcode file. |
Karl Schimpf | 209318a | 2015-08-20 13:24:02 -0700 | [diff] [blame] | 1456 | Ice::OperandList LocalOperands; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1457 | // Holds the index within LocalOperands corresponding to the next instruction |
| 1458 | // that generates a value. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1459 | NaClBcIndexSize_t NextLocalInstIndex; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1460 | // True if the last processed instruction was a terminating instruction. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1461 | bool InstIsTerminating = false; |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1462 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 1463 | bool ParseBlock(unsigned BlockID) override; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 1464 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 1465 | void ProcessRecord() override; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1466 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 1467 | void EnterBlock(unsigned NumWords) override { |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1468 | // Note: Bitstream defines words as 32-bit values. |
| 1469 | NumBytesDefiningFunction = NumWords * sizeof(uint32_t); |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1470 | // We know that all records are minimally defined by a two-bit abreviation. |
| 1471 | MaxRecordsInBlock = NumBytesDefiningFunction * (CHAR_BIT >> 1); |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 1474 | void ExitBlock() override; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1475 | |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1476 | // Creates and appends a new basic block to the list of basic blocks. |
| 1477 | Ice::CfgNode *installNextBasicBlock() { |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1478 | Ice::CfgNode *Node = Func->makeNode(); |
| 1479 | return Node; |
| 1480 | } |
| 1481 | |
| 1482 | // Returns the Index-th basic block in the list of basic blocks. |
| 1483 | Ice::CfgNode *getBasicBlock(NaClBcIndexSize_t Index) { |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 1484 | if (Index >= Func->getNumNodes()) { |
| 1485 | std::string Buffer; |
| 1486 | raw_string_ostream StrBuf(Buffer); |
| 1487 | StrBuf << "Reference to basic block " << Index |
| 1488 | << " not found. Must be less than " << Func->getNumNodes(); |
| 1489 | Error(StrBuf.str()); |
| 1490 | Index = 0; |
| 1491 | } |
| 1492 | return Func->getNodes()[Index]; |
| 1493 | } |
| 1494 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1495 | // Returns the Index-th basic block in the list of basic blocks. Assumes |
| 1496 | // Index corresponds to a branch instruction. Hence, if the branch references |
| 1497 | // the entry block, it also generates a corresponding error. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1498 | Ice::CfgNode *getBranchBasicBlock(NaClBcIndexSize_t Index) { |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 1499 | if (Index == 0) { |
| 1500 | Error("Branch to entry block not allowed"); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 1501 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1502 | return getBasicBlock(Index); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1505 | // Generate an instruction variable with type Ty. |
| 1506 | Ice::Variable *createInstVar(Ice::Type Ty) { |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1507 | if (Ty == Ice::IceType_void) { |
| 1508 | Error("Can't define instruction value using type void"); |
| 1509 | // Recover since we can't throw an exception. |
| 1510 | Ty = Ice::IceType_i32; |
| 1511 | } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1512 | return Func->makeVariable(Ty); |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | // Generates the next available local variable using the given type. |
| 1516 | Ice::Variable *getNextInstVar(Ice::Type Ty) { |
| 1517 | assert(NextLocalInstIndex >= CachedNumGlobalValueIDs); |
| 1518 | // Before creating one, see if a forwardtyperef has already defined it. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1519 | NaClBcIndexSize_t LocalIndex = NextLocalInstIndex - CachedNumGlobalValueIDs; |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1520 | if (LocalIndex < LocalOperands.size()) { |
| 1521 | Ice::Operand *Op = LocalOperands[LocalIndex]; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 1522 | if (Op != nullptr) { |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 1523 | if (auto *Var = dyn_cast<Ice::Variable>(Op)) { |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1524 | if (Var->getType() == Ty) { |
| 1525 | ++NextLocalInstIndex; |
| 1526 | return Var; |
| 1527 | } |
| 1528 | } |
| 1529 | std::string Buffer; |
| 1530 | raw_string_ostream StrBuf(Buffer); |
| 1531 | StrBuf << "Illegal forward referenced instruction (" |
| 1532 | << NextLocalInstIndex << "): " << *Op; |
| 1533 | Error(StrBuf.str()); |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1534 | ++NextLocalInstIndex; |
| 1535 | return createInstVar(Ty); |
| 1536 | } |
| 1537 | } |
| 1538 | Ice::Variable *Var = createInstVar(Ty); |
| 1539 | setOperand(NextLocalInstIndex++, Var); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1540 | return Var; |
| 1541 | } |
| 1542 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1543 | // Converts a relative index (wrt to BaseIndex) to an absolute value index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1544 | NaClBcIndexSize_t convertRelativeToAbsIndex(NaClRelBcIndexSize_t Id, |
| 1545 | NaClRelBcIndexSize_t BaseIndex) { |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1546 | if (BaseIndex < Id) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1547 | std::string Buffer; |
| 1548 | raw_string_ostream StrBuf(Buffer); |
| 1549 | StrBuf << "Invalid relative value id: " << Id |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1550 | << " (must be <= " << BaseIndex << ")"; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1551 | Error(StrBuf.str()); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1552 | return 0; |
| 1553 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1554 | return BaseIndex - Id; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1557 | // Sets element Index (in the local operands list) to Op. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1558 | void setOperand(NaClBcIndexSize_t Index, Ice::Operand *Op) { |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 1559 | assert(Op); |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1560 | // Check if simple push works. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1561 | NaClBcIndexSize_t LocalIndex = Index - CachedNumGlobalValueIDs; |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1562 | if (LocalIndex == LocalOperands.size()) { |
| 1563 | LocalOperands.push_back(Op); |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1567 | // Must be forward reference, expand vector to accommodate. |
| 1568 | if (LocalIndex >= LocalOperands.size()) { |
| 1569 | if (LocalIndex > MaxRecordsInBlock) { |
| 1570 | std::string Buffer; |
| 1571 | raw_string_ostream StrBuf(Buffer); |
| 1572 | StrBuf << "Forward reference @" << Index << " too big. Have " |
| 1573 | << CachedNumGlobalValueIDs << " globals and function contains " |
| 1574 | << NumBytesDefiningFunction << " bytes"; |
| 1575 | Fatal(StrBuf.str()); |
| 1576 | // Recover by using index one beyond the maximal allowed. |
| 1577 | LocalIndex = MaxRecordsInBlock; |
| 1578 | } |
Jim Stichnoth | 2f00bf6 | 2016-03-11 14:59:43 -0800 | [diff] [blame] | 1579 | Ice::Utils::reserveAndResize(LocalOperands, LocalIndex + 1); |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | // If element not defined, set it. |
| 1583 | Ice::Operand *OldOp = LocalOperands[LocalIndex]; |
| 1584 | if (OldOp == nullptr) { |
| 1585 | LocalOperands[LocalIndex] = Op; |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | // See if forward reference matches. |
| 1590 | if (OldOp == Op) |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1591 | return; |
| 1592 | |
| 1593 | // Error has occurred. |
| 1594 | std::string Buffer; |
| 1595 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 1596 | StrBuf << "Multiple definitions for index " << Index << ": " << *Op |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1597 | << " and " << *OldOp; |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 1598 | Error(StrBuf.str()); |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 1599 | LocalOperands[LocalIndex] = Op; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1602 | // Returns the relative operand (wrt to BaseIndex) referenced by the given |
| 1603 | // value Index. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1604 | Ice::Operand *getRelativeOperand(NaClBcIndexSize_t Index, |
| 1605 | NaClBcIndexSize_t BaseIndex) { |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 1606 | return getOperand(convertRelativeToAbsIndex(Index, BaseIndex)); |
| 1607 | } |
| 1608 | |
| 1609 | // Returns the absolute index of the next value generating instruction. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1610 | NaClBcIndexSize_t getNextInstIndex() const { return NextLocalInstIndex; } |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 1611 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1612 | // Generates type error message for binary operator Op operating on Type |
| 1613 | // OpTy. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1614 | void reportInvalidBinaryOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1615 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1616 | // Validates if integer logical Op, for type OpTy, is valid. Returns true if |
| 1617 | // valid. Otherwise generates error message and returns false. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1618 | bool isValidIntegerLogicalOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 1619 | if (Ice::isIntegerType(OpTy)) |
| 1620 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1621 | reportInvalidBinaryOp(Op, OpTy); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1622 | return false; |
| 1623 | } |
| 1624 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1625 | // Validates if integer (or vector of integers) arithmetic Op, for type OpTy, |
| 1626 | // is valid. Returns true if valid. Otherwise generates error message and |
| 1627 | // returns false. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1628 | bool isValidIntegerArithOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 1629 | if (Ice::isIntegerArithmeticType(OpTy)) |
| 1630 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1631 | reportInvalidBinaryOp(Op, OpTy); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1632 | return false; |
| 1633 | } |
| 1634 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1635 | // Checks if floating arithmetic Op, for type OpTy, is valid. Returns true if |
| 1636 | // valid. Otherwise generates an error message and returns false; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1637 | bool isValidFloatingArithOp(Ice::InstArithmetic::OpKind Op, Ice::Type OpTy) { |
| 1638 | if (Ice::isFloatingType(OpTy)) |
| 1639 | return true; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 1640 | reportInvalidBinaryOp(Op, OpTy); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1641 | return false; |
| 1642 | } |
| 1643 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1644 | // Checks if the type of operand Op is the valid pointer type, for the given |
| 1645 | // InstructionName. Returns true if valid. Otherwise generates an error |
| 1646 | // message and returns false. |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1647 | bool isValidPointerType(Ice::Operand *Op, const char *InstructionName) { |
Karl Schimpf | 4019f08 | 2014-12-15 13:45:00 -0800 | [diff] [blame] | 1648 | Ice::Type PtrType = Ice::getPointerType(); |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1649 | if (Op->getType() == PtrType) |
| 1650 | return true; |
| 1651 | std::string Buffer; |
| 1652 | raw_string_ostream StrBuf(Buffer); |
| 1653 | StrBuf << InstructionName << " address not " << PtrType |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 1654 | << ". Found: " << Op->getType(); |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1655 | Error(StrBuf.str()); |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1659 | // Checks if loading/storing a value of type Ty is allowed. Returns true if |
| 1660 | // Valid. Otherwise generates an error message and returns false. |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1661 | bool isValidLoadStoreType(Ice::Type Ty, const char *InstructionName) { |
| 1662 | if (isLoadStoreType(Ty)) |
| 1663 | return true; |
| 1664 | std::string Buffer; |
| 1665 | raw_string_ostream StrBuf(Buffer); |
| 1666 | StrBuf << InstructionName << " type not allowed: " << Ty << "*"; |
| 1667 | Error(StrBuf.str()); |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1671 | // Checks if loading/storing a value of type Ty is allowed for the given |
| 1672 | // Alignment. Otherwise generates an error message and returns false. |
Karl Schimpf | f875d45 | 2014-12-09 13:50:07 -0800 | [diff] [blame] | 1673 | bool isValidLoadStoreAlignment(size_t Alignment, Ice::Type Ty, |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1674 | const char *InstructionName) { |
| 1675 | if (!isValidLoadStoreType(Ty, InstructionName)) |
| 1676 | return false; |
Karl Schimpf | f875d45 | 2014-12-09 13:50:07 -0800 | [diff] [blame] | 1677 | if (isAllowedAlignment(Alignment, Ty)) |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 1678 | return true; |
| 1679 | std::string Buffer; |
| 1680 | raw_string_ostream StrBuf(Buffer); |
| 1681 | StrBuf << InstructionName << " " << Ty << "*: not allowed for alignment " |
| 1682 | << Alignment; |
| 1683 | Error(StrBuf.str()); |
| 1684 | return false; |
| 1685 | } |
| 1686 | |
Karl Schimpf | f875d45 | 2014-12-09 13:50:07 -0800 | [diff] [blame] | 1687 | // Defines if the given alignment is valid for the given type. Simplified |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1688 | // version of PNaClABIProps::isAllowedAlignment, based on API's offered for |
| 1689 | // Ice::Type. |
Karl Schimpf | f875d45 | 2014-12-09 13:50:07 -0800 | [diff] [blame] | 1690 | bool isAllowedAlignment(size_t Alignment, Ice::Type Ty) const { |
| 1691 | return Alignment == typeAlignInBytes(Ty) || |
| 1692 | (Alignment == 1 && !isVectorType(Ty)); |
| 1693 | } |
| 1694 | |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 1695 | // Types of errors that can occur for insertelement and extractelement |
| 1696 | // instructions. |
| 1697 | enum VectorIndexCheckValue { |
| 1698 | VectorIndexNotVector, |
| 1699 | VectorIndexNotConstant, |
| 1700 | VectorIndexNotInRange, |
| 1701 | VectorIndexNotI32, |
| 1702 | VectorIndexValid |
| 1703 | }; |
| 1704 | |
| 1705 | void dumpVectorIndexCheckValue(raw_ostream &Stream, |
| 1706 | VectorIndexCheckValue Value) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 1707 | if (!Ice::BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 1708 | return; |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 1709 | switch (Value) { |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 1710 | case VectorIndexNotVector: |
| 1711 | Stream << "Vector index on non vector"; |
| 1712 | break; |
| 1713 | case VectorIndexNotConstant: |
| 1714 | Stream << "Vector index not integer constant"; |
| 1715 | break; |
| 1716 | case VectorIndexNotInRange: |
| 1717 | Stream << "Vector index not in range of vector"; |
| 1718 | break; |
| 1719 | case VectorIndexNotI32: |
| 1720 | Stream << "Vector index not of type " << Ice::IceType_i32; |
| 1721 | break; |
| 1722 | case VectorIndexValid: |
| 1723 | Stream << "Valid vector index"; |
| 1724 | break; |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | // Returns whether the given vector index (for insertelement and |
| 1729 | // extractelement instructions) is valid. |
| 1730 | VectorIndexCheckValue validateVectorIndex(const Ice::Operand *Vec, |
| 1731 | const Ice::Operand *Index) const { |
| 1732 | Ice::Type VecType = Vec->getType(); |
| 1733 | if (!Ice::isVectorType(VecType)) |
| 1734 | return VectorIndexNotVector; |
| 1735 | const auto *C = dyn_cast<Ice::ConstantInteger32>(Index); |
| 1736 | if (C == nullptr) |
| 1737 | return VectorIndexNotConstant; |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 1738 | if (static_cast<size_t>(C->getValue()) >= typeNumElements(VecType)) |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 1739 | return VectorIndexNotInRange; |
| 1740 | if (Index->getType() != Ice::IceType_i32) |
| 1741 | return VectorIndexNotI32; |
| 1742 | return VectorIndexValid; |
| 1743 | } |
| 1744 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1745 | // Takes the PNaCl bitcode binary operator Opcode, and the opcode type Ty, |
| 1746 | // and sets Op to the corresponding ICE binary opcode. Returns true if able |
| 1747 | // to convert, false otherwise. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1748 | bool convertBinopOpcode(unsigned Opcode, Ice::Type Ty, |
| 1749 | Ice::InstArithmetic::OpKind &Op) { |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1750 | switch (Opcode) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1751 | default: { |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1752 | std::string Buffer; |
| 1753 | raw_string_ostream StrBuf(Buffer); |
| 1754 | StrBuf << "Binary opcode " << Opcode << "not understood for type " << Ty; |
| 1755 | Error(StrBuf.str()); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1756 | Op = Ice::InstArithmetic::Add; |
| 1757 | return false; |
| 1758 | } |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1759 | case naclbitc::BINOP_ADD: |
| 1760 | if (Ice::isIntegerType(Ty)) { |
| 1761 | Op = Ice::InstArithmetic::Add; |
| 1762 | return isValidIntegerArithOp(Op, Ty); |
| 1763 | } else { |
| 1764 | Op = Ice::InstArithmetic::Fadd; |
| 1765 | return isValidFloatingArithOp(Op, Ty); |
| 1766 | } |
| 1767 | case naclbitc::BINOP_SUB: |
| 1768 | if (Ice::isIntegerType(Ty)) { |
| 1769 | Op = Ice::InstArithmetic::Sub; |
| 1770 | return isValidIntegerArithOp(Op, Ty); |
| 1771 | } else { |
| 1772 | Op = Ice::InstArithmetic::Fsub; |
| 1773 | return isValidFloatingArithOp(Op, Ty); |
| 1774 | } |
| 1775 | case naclbitc::BINOP_MUL: |
| 1776 | if (Ice::isIntegerType(Ty)) { |
| 1777 | Op = Ice::InstArithmetic::Mul; |
| 1778 | return isValidIntegerArithOp(Op, Ty); |
| 1779 | } else { |
| 1780 | Op = Ice::InstArithmetic::Fmul; |
| 1781 | return isValidFloatingArithOp(Op, Ty); |
| 1782 | } |
| 1783 | case naclbitc::BINOP_UDIV: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1784 | Op = Ice::InstArithmetic::Udiv; |
| 1785 | return isValidIntegerArithOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1786 | case naclbitc::BINOP_SDIV: |
| 1787 | if (Ice::isIntegerType(Ty)) { |
| 1788 | Op = Ice::InstArithmetic::Sdiv; |
| 1789 | return isValidIntegerArithOp(Op, Ty); |
| 1790 | } else { |
| 1791 | Op = Ice::InstArithmetic::Fdiv; |
| 1792 | return isValidFloatingArithOp(Op, Ty); |
| 1793 | } |
| 1794 | case naclbitc::BINOP_UREM: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1795 | Op = Ice::InstArithmetic::Urem; |
| 1796 | return isValidIntegerArithOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1797 | case naclbitc::BINOP_SREM: |
| 1798 | if (Ice::isIntegerType(Ty)) { |
| 1799 | Op = Ice::InstArithmetic::Srem; |
| 1800 | return isValidIntegerArithOp(Op, Ty); |
| 1801 | } else { |
| 1802 | Op = Ice::InstArithmetic::Frem; |
| 1803 | return isValidFloatingArithOp(Op, Ty); |
| 1804 | } |
| 1805 | case naclbitc::BINOP_SHL: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1806 | Op = Ice::InstArithmetic::Shl; |
| 1807 | return isValidIntegerArithOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1808 | case naclbitc::BINOP_LSHR: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1809 | Op = Ice::InstArithmetic::Lshr; |
| 1810 | return isValidIntegerArithOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1811 | case naclbitc::BINOP_ASHR: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1812 | Op = Ice::InstArithmetic::Ashr; |
| 1813 | return isValidIntegerArithOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1814 | case naclbitc::BINOP_AND: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1815 | Op = Ice::InstArithmetic::And; |
| 1816 | return isValidIntegerLogicalOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1817 | case naclbitc::BINOP_OR: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1818 | Op = Ice::InstArithmetic::Or; |
| 1819 | return isValidIntegerLogicalOp(Op, Ty); |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 1820 | case naclbitc::BINOP_XOR: |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 1821 | Op = Ice::InstArithmetic::Xor; |
| 1822 | return isValidIntegerLogicalOp(Op, Ty); |
| 1823 | } |
| 1824 | } |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 1825 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1826 | /// Simplifies out vector types from Type1 and Type2, if both are vectors of |
| 1827 | /// the same size. Returns true iff both are vectors of the same size, or are |
| 1828 | /// both scalar types. |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1829 | static bool simplifyOutCommonVectorType(Ice::Type &Type1, Ice::Type &Type2) { |
| 1830 | bool IsType1Vector = isVectorType(Type1); |
| 1831 | bool IsType2Vector = isVectorType(Type2); |
| 1832 | if (IsType1Vector != IsType2Vector) |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 1833 | return false; |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1834 | if (!IsType1Vector) |
| 1835 | return true; |
| 1836 | if (typeNumElements(Type1) != typeNumElements(Type2)) |
| 1837 | return false; |
| 1838 | Type1 = typeElementType(Type1); |
| 1839 | Type2 = typeElementType(Type2); |
| 1840 | return true; |
| 1841 | } |
| 1842 | |
| 1843 | /// Returns true iff an integer truncation from SourceType to TargetType is |
| 1844 | /// valid. |
| 1845 | static bool isIntTruncCastValid(Ice::Type SourceType, Ice::Type TargetType) { |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 1846 | return Ice::isIntegerType(SourceType) && Ice::isIntegerType(TargetType) && |
| 1847 | simplifyOutCommonVectorType(SourceType, TargetType) && |
| 1848 | getScalarIntBitWidth(SourceType) > getScalarIntBitWidth(TargetType); |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | /// Returns true iff a floating type truncation from SourceType to TargetType |
| 1852 | /// is valid. |
| 1853 | static bool isFloatTruncCastValid(Ice::Type SourceType, |
| 1854 | Ice::Type TargetType) { |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 1855 | return simplifyOutCommonVectorType(SourceType, TargetType) && |
| 1856 | SourceType == Ice::IceType_f64 && TargetType == Ice::IceType_f32; |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /// Returns true iff an integer extension from SourceType to TargetType is |
| 1860 | /// valid. |
| 1861 | static bool isIntExtCastValid(Ice::Type SourceType, Ice::Type TargetType) { |
| 1862 | return isIntTruncCastValid(TargetType, SourceType); |
| 1863 | } |
| 1864 | |
| 1865 | /// Returns true iff a floating type extension from SourceType to TargetType |
| 1866 | /// is valid. |
| 1867 | static bool isFloatExtCastValid(Ice::Type SourceType, Ice::Type TargetType) { |
| 1868 | return isFloatTruncCastValid(TargetType, SourceType); |
| 1869 | } |
| 1870 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1871 | /// Returns true iff a cast from floating type SourceType to integer type |
| 1872 | /// TargetType is valid. |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1873 | static bool isFloatToIntCastValid(Ice::Type SourceType, |
| 1874 | Ice::Type TargetType) { |
| 1875 | if (!(Ice::isFloatingType(SourceType) && Ice::isIntegerType(TargetType))) |
| 1876 | return false; |
| 1877 | bool IsSourceVector = isVectorType(SourceType); |
| 1878 | bool IsTargetVector = isVectorType(TargetType); |
| 1879 | if (IsSourceVector != IsTargetVector) |
| 1880 | return false; |
| 1881 | if (IsSourceVector) { |
| 1882 | return typeNumElements(SourceType) == typeNumElements(TargetType); |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 1883 | } |
| 1884 | return true; |
| 1885 | } |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 1886 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1887 | /// Returns true iff a cast from integer type SourceType to floating type |
| 1888 | /// TargetType is valid. |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1889 | static bool isIntToFloatCastValid(Ice::Type SourceType, |
| 1890 | Ice::Type TargetType) { |
| 1891 | return isFloatToIntCastValid(TargetType, SourceType); |
| 1892 | } |
| 1893 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1894 | /// Returns the number of bits used to model type Ty when defining the bitcast |
| 1895 | /// instruction. |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1896 | static Ice::SizeT bitcastSizeInBits(Ice::Type Ty) { |
| 1897 | if (Ice::isVectorType(Ty)) |
| 1898 | return Ice::typeNumElements(Ty) * |
| 1899 | bitcastSizeInBits(Ice::typeElementType(Ty)); |
| 1900 | if (Ty == Ice::IceType_i1) |
| 1901 | return 1; |
| 1902 | return Ice::typeWidthInBytes(Ty) * CHAR_BIT; |
| 1903 | } |
| 1904 | |
| 1905 | /// Returns true iff a bitcast from SourceType to TargetType is allowed. |
| 1906 | static bool isBitcastValid(Ice::Type SourceType, Ice::Type TargetType) { |
| 1907 | return bitcastSizeInBits(SourceType) == bitcastSizeInBits(TargetType); |
| 1908 | } |
| 1909 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1910 | /// Returns true iff the NaCl bitcode Opcode is a valid cast opcode for |
| 1911 | /// converting SourceType to TargetType. Updates CastKind to the corresponding |
| 1912 | /// instruction cast opcode. Also generates an error message when this |
| 1913 | /// function returns false. |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1914 | bool convertCastOpToIceOp(uint64_t Opcode, Ice::Type SourceType, |
| 1915 | Ice::Type TargetType, |
| 1916 | Ice::InstCast::OpKind &CastKind) { |
| 1917 | bool Result; |
| 1918 | switch (Opcode) { |
| 1919 | default: { |
| 1920 | std::string Buffer; |
| 1921 | raw_string_ostream StrBuf(Buffer); |
| 1922 | StrBuf << "Cast opcode " << Opcode << " not understood.\n"; |
| 1923 | Error(StrBuf.str()); |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 1924 | CastKind = Ice::InstCast::Bitcast; |
| 1925 | return false; |
| 1926 | } |
| 1927 | case naclbitc::CAST_TRUNC: |
| 1928 | CastKind = Ice::InstCast::Trunc; |
| 1929 | Result = isIntTruncCastValid(SourceType, TargetType); |
| 1930 | break; |
| 1931 | case naclbitc::CAST_ZEXT: |
| 1932 | CastKind = Ice::InstCast::Zext; |
| 1933 | Result = isIntExtCastValid(SourceType, TargetType); |
| 1934 | break; |
| 1935 | case naclbitc::CAST_SEXT: |
| 1936 | CastKind = Ice::InstCast::Sext; |
| 1937 | Result = isIntExtCastValid(SourceType, TargetType); |
| 1938 | break; |
| 1939 | case naclbitc::CAST_FPTOUI: |
| 1940 | CastKind = Ice::InstCast::Fptoui; |
| 1941 | Result = isFloatToIntCastValid(SourceType, TargetType); |
| 1942 | break; |
| 1943 | case naclbitc::CAST_FPTOSI: |
| 1944 | CastKind = Ice::InstCast::Fptosi; |
| 1945 | Result = isFloatToIntCastValid(SourceType, TargetType); |
| 1946 | break; |
| 1947 | case naclbitc::CAST_UITOFP: |
| 1948 | CastKind = Ice::InstCast::Uitofp; |
| 1949 | Result = isIntToFloatCastValid(SourceType, TargetType); |
| 1950 | break; |
| 1951 | case naclbitc::CAST_SITOFP: |
| 1952 | CastKind = Ice::InstCast::Sitofp; |
| 1953 | Result = isIntToFloatCastValid(SourceType, TargetType); |
| 1954 | break; |
| 1955 | case naclbitc::CAST_FPTRUNC: |
| 1956 | CastKind = Ice::InstCast::Fptrunc; |
| 1957 | Result = isFloatTruncCastValid(SourceType, TargetType); |
| 1958 | break; |
| 1959 | case naclbitc::CAST_FPEXT: |
| 1960 | CastKind = Ice::InstCast::Fpext; |
| 1961 | Result = isFloatExtCastValid(SourceType, TargetType); |
| 1962 | break; |
| 1963 | case naclbitc::CAST_BITCAST: |
| 1964 | CastKind = Ice::InstCast::Bitcast; |
| 1965 | Result = isBitcastValid(SourceType, TargetType); |
| 1966 | break; |
| 1967 | } |
| 1968 | if (!Result) { |
| 1969 | std::string Buffer; |
| 1970 | raw_string_ostream StrBuf(Buffer); |
| 1971 | StrBuf << "Illegal cast: " << Ice::InstCast::getCastName(CastKind) << " " |
| 1972 | << SourceType << " to " << TargetType; |
| 1973 | Error(StrBuf.str()); |
| 1974 | } |
| 1975 | return Result; |
| 1976 | } |
| 1977 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1978 | // Converts PNaCl bitcode Icmp operator to corresponding ICE op. Returns true |
| 1979 | // if able to convert, false otherwise. |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 1980 | bool convertNaClBitcICmpOpToIce(uint64_t Op, |
| 1981 | Ice::InstIcmp::ICond &Cond) const { |
| 1982 | switch (Op) { |
| 1983 | case naclbitc::ICMP_EQ: |
| 1984 | Cond = Ice::InstIcmp::Eq; |
| 1985 | return true; |
| 1986 | case naclbitc::ICMP_NE: |
| 1987 | Cond = Ice::InstIcmp::Ne; |
| 1988 | return true; |
| 1989 | case naclbitc::ICMP_UGT: |
| 1990 | Cond = Ice::InstIcmp::Ugt; |
| 1991 | return true; |
| 1992 | case naclbitc::ICMP_UGE: |
| 1993 | Cond = Ice::InstIcmp::Uge; |
| 1994 | return true; |
| 1995 | case naclbitc::ICMP_ULT: |
| 1996 | Cond = Ice::InstIcmp::Ult; |
| 1997 | return true; |
| 1998 | case naclbitc::ICMP_ULE: |
| 1999 | Cond = Ice::InstIcmp::Ule; |
| 2000 | return true; |
| 2001 | case naclbitc::ICMP_SGT: |
| 2002 | Cond = Ice::InstIcmp::Sgt; |
| 2003 | return true; |
| 2004 | case naclbitc::ICMP_SGE: |
| 2005 | Cond = Ice::InstIcmp::Sge; |
| 2006 | return true; |
| 2007 | case naclbitc::ICMP_SLT: |
| 2008 | Cond = Ice::InstIcmp::Slt; |
| 2009 | return true; |
| 2010 | case naclbitc::ICMP_SLE: |
| 2011 | Cond = Ice::InstIcmp::Sle; |
| 2012 | return true; |
| 2013 | default: |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 2014 | // Make sure Cond is always initialized. |
| 2015 | Cond = static_cast<Ice::InstIcmp::ICond>(0); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2016 | return false; |
| 2017 | } |
| 2018 | } |
| 2019 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2020 | // Converts PNaCl bitcode Fcmp operator to corresponding ICE op. Returns true |
| 2021 | // if able to convert, false otherwise. |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2022 | bool convertNaClBitcFCompOpToIce(uint64_t Op, |
| 2023 | Ice::InstFcmp::FCond &Cond) const { |
| 2024 | switch (Op) { |
| 2025 | case naclbitc::FCMP_FALSE: |
| 2026 | Cond = Ice::InstFcmp::False; |
| 2027 | return true; |
| 2028 | case naclbitc::FCMP_OEQ: |
| 2029 | Cond = Ice::InstFcmp::Oeq; |
| 2030 | return true; |
| 2031 | case naclbitc::FCMP_OGT: |
| 2032 | Cond = Ice::InstFcmp::Ogt; |
| 2033 | return true; |
| 2034 | case naclbitc::FCMP_OGE: |
| 2035 | Cond = Ice::InstFcmp::Oge; |
| 2036 | return true; |
| 2037 | case naclbitc::FCMP_OLT: |
| 2038 | Cond = Ice::InstFcmp::Olt; |
| 2039 | return true; |
| 2040 | case naclbitc::FCMP_OLE: |
| 2041 | Cond = Ice::InstFcmp::Ole; |
| 2042 | return true; |
| 2043 | case naclbitc::FCMP_ONE: |
| 2044 | Cond = Ice::InstFcmp::One; |
| 2045 | return true; |
| 2046 | case naclbitc::FCMP_ORD: |
| 2047 | Cond = Ice::InstFcmp::Ord; |
| 2048 | return true; |
| 2049 | case naclbitc::FCMP_UNO: |
| 2050 | Cond = Ice::InstFcmp::Uno; |
| 2051 | return true; |
| 2052 | case naclbitc::FCMP_UEQ: |
| 2053 | Cond = Ice::InstFcmp::Ueq; |
| 2054 | return true; |
| 2055 | case naclbitc::FCMP_UGT: |
| 2056 | Cond = Ice::InstFcmp::Ugt; |
| 2057 | return true; |
| 2058 | case naclbitc::FCMP_UGE: |
| 2059 | Cond = Ice::InstFcmp::Uge; |
| 2060 | return true; |
| 2061 | case naclbitc::FCMP_ULT: |
| 2062 | Cond = Ice::InstFcmp::Ult; |
| 2063 | return true; |
| 2064 | case naclbitc::FCMP_ULE: |
| 2065 | Cond = Ice::InstFcmp::Ule; |
| 2066 | return true; |
| 2067 | case naclbitc::FCMP_UNE: |
| 2068 | Cond = Ice::InstFcmp::Une; |
| 2069 | return true; |
| 2070 | case naclbitc::FCMP_TRUE: |
| 2071 | Cond = Ice::InstFcmp::True; |
| 2072 | return true; |
| 2073 | default: |
Jim Stichnoth | dddaf9c | 2014-12-04 14:09:21 -0800 | [diff] [blame] | 2074 | // Make sure Cond is always initialized. |
| 2075 | Cond = static_cast<Ice::InstFcmp::FCond>(0); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2076 | return false; |
| 2077 | } |
| 2078 | } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2079 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2080 | // Creates an error instruction, generating a value of type Ty, and adds a |
| 2081 | // placeholder so that instruction indices line up. Some instructions, such |
| 2082 | // as a call, will not generate a value if the return type is void. In such |
| 2083 | // cases, a placeholder value for the badly formed instruction is not needed. |
| 2084 | // Hence, if Ty is void, an error instruction is not appended. |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2085 | void appendErrorInstruction(Ice::Type Ty) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2086 | // Note: we don't worry about downstream translation errors because the |
| 2087 | // function will not be translated if any errors occur. |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2088 | if (Ty == Ice::IceType_void) |
| 2089 | return; |
| 2090 | Ice::Variable *Var = getNextInstVar(Ty); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2091 | CurrentNode->appendInst(Ice::InstAssign::create(Func.get(), Var, Var)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2092 | } |
Karl Schimpf | 9d25e62 | 2015-09-16 13:47:01 -0700 | [diff] [blame] | 2093 | |
| 2094 | Ice::Operand *reportGetOperandUndefined(NaClBcIndexSize_t Index) { |
| 2095 | std::string Buffer; |
| 2096 | raw_string_ostream StrBuf(Buffer); |
| 2097 | StrBuf << "Value index " << Index << " not defined!"; |
Karl Schimpf | 43632b9 | 2015-09-21 15:32:30 -0700 | [diff] [blame] | 2098 | Error(StrBuf.str()); |
| 2099 | // Recover and return some value. |
| 2100 | if (!LocalOperands.empty()) |
| 2101 | return LocalOperands.front(); |
| 2102 | return Context->getGlobalConstantByID(0); |
Karl Schimpf | 9d25e62 | 2015-09-16 13:47:01 -0700 | [diff] [blame] | 2103 | } |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2104 | |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2105 | void verifyCallArgTypeMatches(Ice::FunctionDeclaration *Fcn, Ice::SizeT Index, |
| 2106 | Ice::Type ArgType, Ice::Type ParamType) { |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2107 | if (ArgType != ParamType) { |
| 2108 | std::string Buffer; |
| 2109 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2110 | StrBuf << "Argument " << (Index + 1) << " of " << printName(Fcn) |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2111 | << " expects " << ParamType << ". Found: " << ArgType; |
| 2112 | Error(StrBuf.str()); |
| 2113 | } |
| 2114 | } |
| 2115 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 2116 | const std::string printName(Ice::FunctionDeclaration *Fcn) { |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2117 | if (Fcn) |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 2118 | return Fcn->getName().toString(); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2119 | return "function"; |
| 2120 | } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2121 | }; |
| 2122 | |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2123 | void FunctionParser::ExitBlock() { |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2124 | // Check if the last instruction in the function was terminating. |
| 2125 | if (!InstIsTerminating) { |
| 2126 | Error("Last instruction in function not terminator"); |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2127 | // Recover by inserting an unreachable instruction. |
| 2128 | CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2129 | } |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2130 | ++CurrentBbIndex; |
| 2131 | if (CurrentBbIndex != DeclaredNumberBbs) { |
| 2132 | std::string Buffer; |
| 2133 | raw_string_ostream StrBuf(Buffer); |
| 2134 | StrBuf << "Function declared " << DeclaredNumberBbs |
| 2135 | << " basic blocks, but defined " << CurrentBbIndex << "."; |
| 2136 | Error(StrBuf.str()); |
| 2137 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2138 | // Before translating, check for blocks without instructions, and insert |
| 2139 | // unreachable. This shouldn't happen, but be safe. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2140 | size_t Index = 0; |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 2141 | for (Ice::CfgNode *Node : Func->getNodes()) { |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 2142 | if (Node->getInsts().empty()) { |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2143 | std::string Buffer; |
| 2144 | raw_string_ostream StrBuf(Buffer); |
| 2145 | StrBuf << "Basic block " << Index << " contains no instructions"; |
| 2146 | Error(StrBuf.str()); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2147 | Node->appendInst(Ice::InstUnreachable::create(Func.get())); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2148 | } |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 2149 | ++Index; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2150 | } |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 2151 | Func->computeInOutEdges(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2154 | void FunctionParser::reportInvalidBinaryOp(Ice::InstArithmetic::OpKind Op, |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2155 | Ice::Type OpTy) { |
| 2156 | std::string Buffer; |
| 2157 | raw_string_ostream StrBuf(Buffer); |
| 2158 | StrBuf << "Invalid operator type for " << Ice::InstArithmetic::getOpName(Op) |
| 2159 | << ". Found " << OpTy; |
| 2160 | Error(StrBuf.str()); |
| 2161 | } |
| 2162 | |
| 2163 | void FunctionParser::ProcessRecord() { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2164 | // Note: To better separate parse/IR generation times, when IR generation is |
| 2165 | // disabled we do the following: |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2166 | // 1) Delay exiting until after we extract operands. |
| 2167 | // 2) return before we access operands, since all operands will be a nullptr. |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2168 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 2169 | if (InstIsTerminating) { |
| 2170 | InstIsTerminating = false; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2171 | ++CurrentBbIndex; |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2172 | CurrentNode = getBasicBlock(CurrentBbIndex); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2173 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2174 | // The base index for relative indexing. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2175 | NaClBcIndexSize_t BaseIndex = getNextInstIndex(); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2176 | switch (Record.GetCode()) { |
| 2177 | case naclbitc::FUNC_CODE_DECLAREBLOCKS: { |
| 2178 | // DECLAREBLOCKS: [n] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2179 | if (!isValidRecordSize(1, "count")) |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2180 | return; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2181 | if (DeclaredNumberBbs > 0) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2182 | Error("Duplicate function block count record"); |
| 2183 | return; |
| 2184 | } |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2185 | |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2186 | // Check for bad large sizes, since they can make ridiculous memory |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 2187 | // requests and hang the user for large amounts of time. |
| 2188 | uint64_t NumBbs = Values[0]; |
| 2189 | if (NumBbs > MaxRecordsInBlock) { |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2190 | std::string Buffer; |
| 2191 | raw_string_ostream StrBuf(Buffer); |
| 2192 | StrBuf << "Function defines " << NumBbs |
| 2193 | << " basic blocks, which is too big for a function containing " |
| 2194 | << NumBytesDefiningFunction << " bytes"; |
| 2195 | Error(StrBuf.str()); |
Karl Schimpf | 7a99327 | 2015-08-17 12:43:29 -0700 | [diff] [blame] | 2196 | NumBbs = MaxRecordsInBlock; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | if (NumBbs == 0) { |
| 2200 | Error("Functions must contain at least one basic block."); |
| 2201 | NumBbs = 1; |
| 2202 | } |
| 2203 | |
| 2204 | DeclaredNumberBbs = NumBbs; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2205 | // Install the basic blocks, skipping bb0 which was created in the |
| 2206 | // constructor. |
| 2207 | for (size_t i = 1; i < NumBbs; ++i) |
| 2208 | installNextBasicBlock(); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2209 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2210 | } |
| 2211 | case naclbitc::FUNC_CODE_INST_BINOP: { |
Karl Schimpf | e73ee85 | 2016-03-25 07:12:26 -0700 | [diff] [blame] | 2212 | // Note: Old bitcode files may have an additional 'flags' operand, which is |
| 2213 | // ignored. |
| 2214 | |
| 2215 | // BINOP: [opval, opval, opcode, [flags]] |
| 2216 | |
| 2217 | if (!isValidRecordSizeInRange(3, 4, "binop")) |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2218 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2219 | Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); |
| 2220 | Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2221 | Ice::Type Type1 = Op1->getType(); |
| 2222 | Ice::Type Type2 = Op2->getType(); |
| 2223 | if (Type1 != Type2) { |
| 2224 | std::string Buffer; |
| 2225 | raw_string_ostream StrBuf(Buffer); |
| 2226 | StrBuf << "Binop argument types differ: " << Type1 << " and " << Type2; |
| 2227 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2228 | appendErrorInstruction(Type1); |
| 2229 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | Ice::InstArithmetic::OpKind Opcode; |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 2233 | if (!convertBinopOpcode(Values[2], Type1, Opcode)) { |
| 2234 | appendErrorInstruction(Type1); |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2235 | return; |
Karl Schimpf | 9bb188d | 2014-12-10 12:54:34 -0800 | [diff] [blame] | 2236 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2237 | CurrentNode->appendInst(Ice::InstArithmetic::create( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2238 | Func.get(), Opcode, getNextInstVar(Type1), Op1, Op2)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2239 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2240 | } |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2241 | case naclbitc::FUNC_CODE_INST_CAST: { |
| 2242 | // CAST: [opval, destty, castopc] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2243 | if (!isValidRecordSize(3, "cast")) |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2244 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2245 | Ice::Operand *Src = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 2246 | Ice::Type CastType = Context->getSimpleTypeByID(Values[1]); |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2247 | Ice::InstCast::OpKind CastKind; |
Karl Schimpf | bf17037 | 2014-12-15 10:16:31 -0800 | [diff] [blame] | 2248 | if (!convertCastOpToIceOp(Values[2], Src->getType(), CastType, CastKind)) { |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2249 | appendErrorInstruction(CastType); |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2250 | return; |
| 2251 | } |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2252 | CurrentNode->appendInst(Ice::InstCast::create( |
| 2253 | Func.get(), CastKind, getNextInstVar(CastType), Src)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2254 | return; |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2255 | } |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2256 | case naclbitc::FUNC_CODE_INST_VSELECT: { |
| 2257 | // VSELECT: [opval, opval, pred] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2258 | if (!isValidRecordSize(3, "select")) |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2259 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2260 | Ice::Operand *ThenVal = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2261 | Ice::Operand *ElseVal = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2262 | Ice::Operand *CondVal = getRelativeOperand(Values[2], BaseIndex); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2263 | Ice::Type ThenType = ThenVal->getType(); |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2264 | Ice::Type ElseType = ElseVal->getType(); |
| 2265 | if (ThenType != ElseType) { |
| 2266 | std::string Buffer; |
| 2267 | raw_string_ostream StrBuf(Buffer); |
| 2268 | StrBuf << "Select operands not same type. Found " << ThenType << " and " |
| 2269 | << ElseType; |
| 2270 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2271 | appendErrorInstruction(ThenType); |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2274 | Ice::Type CondType = CondVal->getType(); |
| 2275 | if (isVectorType(CondType)) { |
| 2276 | if (!isVectorType(ThenType) || |
| 2277 | typeElementType(CondType) != Ice::IceType_i1 || |
| 2278 | typeNumElements(ThenType) != typeNumElements(CondType)) { |
| 2279 | std::string Buffer; |
| 2280 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2281 | StrBuf << "Select condition type " << CondType |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2282 | << " not allowed for values of type " << ThenType; |
| 2283 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2284 | appendErrorInstruction(ThenType); |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2285 | return; |
| 2286 | } |
| 2287 | } else if (CondVal->getType() != Ice::IceType_i1) { |
| 2288 | std::string Buffer; |
| 2289 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2290 | StrBuf << "Select condition " << CondVal |
| 2291 | << " not type i1. Found: " << CondVal->getType(); |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2292 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2293 | appendErrorInstruction(ThenType); |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2294 | return; |
| 2295 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2296 | CurrentNode->appendInst(Ice::InstSelect::create( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2297 | Func.get(), getNextInstVar(ThenType), CondVal, ThenVal, ElseVal)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2298 | return; |
Karl Schimpf | 1d6f0e4 | 2014-09-04 12:22:14 -0700 | [diff] [blame] | 2299 | } |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2300 | case naclbitc::FUNC_CODE_INST_EXTRACTELT: { |
| 2301 | // EXTRACTELT: [opval, opval] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2302 | if (!isValidRecordSize(2, "extract element")) |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2303 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2304 | Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2305 | Ice::Operand *Index = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2306 | Ice::Type VecType = Vec->getType(); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2307 | VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index); |
| 2308 | if (IndexCheckValue != VectorIndexValid) { |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2309 | std::string Buffer; |
| 2310 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2311 | dumpVectorIndexCheckValue(StrBuf, IndexCheckValue); |
| 2312 | StrBuf << ": extractelement " << VecType << " " << *Vec << ", " |
| 2313 | << Index->getType() << " " << *Index; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2314 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2315 | appendErrorInstruction(VecType); |
| 2316 | return; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2317 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2318 | CurrentNode->appendInst(Ice::InstExtractElement::create( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2319 | Func.get(), getNextInstVar(typeElementType(VecType)), Vec, Index)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2320 | return; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2321 | } |
| 2322 | case naclbitc::FUNC_CODE_INST_INSERTELT: { |
| 2323 | // INSERTELT: [opval, opval, opval] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2324 | if (!isValidRecordSize(3, "insert element")) |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2325 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2326 | Ice::Operand *Vec = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2327 | Ice::Operand *Elt = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2328 | Ice::Operand *Index = getRelativeOperand(Values[2], BaseIndex); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2329 | Ice::Type VecType = Vec->getType(); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2330 | VectorIndexCheckValue IndexCheckValue = validateVectorIndex(Vec, Index); |
| 2331 | if (IndexCheckValue != VectorIndexValid) { |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2332 | std::string Buffer; |
| 2333 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2334 | dumpVectorIndexCheckValue(StrBuf, IndexCheckValue); |
| 2335 | StrBuf << ": insertelement " << VecType << " " << *Vec << ", " |
| 2336 | << Elt->getType() << " " << *Elt << ", " << Index->getType() << " " |
| 2337 | << *Index; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2338 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2339 | appendErrorInstruction(Elt->getType()); |
| 2340 | return; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2341 | } |
Karl Schimpf | 43632b9 | 2015-09-21 15:32:30 -0700 | [diff] [blame] | 2342 | if (Ice::typeElementType(VecType) != Elt->getType()) { |
| 2343 | std::string Buffer; |
| 2344 | raw_string_ostream StrBuf(Buffer); |
| 2345 | StrBuf << "Insertelement: Element type " |
| 2346 | << Ice::typeString(Elt->getType()) << " doesn't match vector type " |
| 2347 | << Ice::typeString(VecType); |
| 2348 | Error(StrBuf.str()); |
| 2349 | appendErrorInstruction(Elt->getType()); |
| 2350 | return; |
| 2351 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2352 | CurrentNode->appendInst(Ice::InstInsertElement::create( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2353 | Func.get(), getNextInstVar(VecType), Vec, Elt, Index)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2354 | return; |
Karl Schimpf | 71ba822 | 2014-09-03 09:46:24 -0700 | [diff] [blame] | 2355 | } |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2356 | case naclbitc::FUNC_CODE_INST_CMP2: { |
| 2357 | // CMP2: [opval, opval, pred] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2358 | if (!isValidRecordSize(3, "compare")) |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2359 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2360 | Ice::Operand *Op1 = getRelativeOperand(Values[0], BaseIndex); |
| 2361 | Ice::Operand *Op2 = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2362 | Ice::Type Op1Type = Op1->getType(); |
| 2363 | Ice::Type Op2Type = Op2->getType(); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2364 | Ice::Type DestType = getCompareResultType(Op1Type); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2365 | if (Op1Type != Op2Type) { |
| 2366 | std::string Buffer; |
| 2367 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2368 | StrBuf << "Compare argument types differ: " << Op1Type << " and " |
| 2369 | << Op2Type; |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2370 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2371 | appendErrorInstruction(DestType); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2372 | Op2 = Op1; |
| 2373 | } |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2374 | if (DestType == Ice::IceType_void) { |
| 2375 | std::string Buffer; |
| 2376 | raw_string_ostream StrBuf(Buffer); |
| 2377 | StrBuf << "Compare not defined for type " << Op1Type; |
| 2378 | Error(StrBuf.str()); |
| 2379 | return; |
| 2380 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2381 | Ice::Variable *Dest = getNextInstVar(DestType); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2382 | if (isIntegerType(Op1Type)) { |
| 2383 | Ice::InstIcmp::ICond Cond; |
| 2384 | if (!convertNaClBitcICmpOpToIce(Values[2], Cond)) { |
| 2385 | std::string Buffer; |
| 2386 | raw_string_ostream StrBuf(Buffer); |
| 2387 | StrBuf << "Compare record contains unknown integer predicate index: " |
| 2388 | << Values[2]; |
| 2389 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2390 | appendErrorInstruction(DestType); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2391 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2392 | CurrentNode->appendInst( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2393 | Ice::InstIcmp::create(Func.get(), Cond, Dest, Op1, Op2)); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2394 | } else if (isFloatingType(Op1Type)) { |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2395 | Ice::InstFcmp::FCond Cond; |
| 2396 | if (!convertNaClBitcFCompOpToIce(Values[2], Cond)) { |
| 2397 | std::string Buffer; |
| 2398 | raw_string_ostream StrBuf(Buffer); |
| 2399 | StrBuf << "Compare record contains unknown float predicate index: " |
| 2400 | << Values[2]; |
| 2401 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2402 | appendErrorInstruction(DestType); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2403 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2404 | CurrentNode->appendInst( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2405 | Ice::InstFcmp::create(Func.get(), Cond, Dest, Op1, Op2)); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2406 | } else { |
| 2407 | // Not sure this can happen, but be safe. |
| 2408 | std::string Buffer; |
| 2409 | raw_string_ostream StrBuf(Buffer); |
| 2410 | StrBuf << "Compare on type not understood: " << Op1Type; |
| 2411 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2412 | appendErrorInstruction(DestType); |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2413 | return; |
| 2414 | } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2415 | return; |
Karl Schimpf | 83f9f0c | 2014-09-05 08:30:55 -0700 | [diff] [blame] | 2416 | } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2417 | case naclbitc::FUNC_CODE_INST_RET: { |
| 2418 | // RET: [opval?] |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2419 | InstIsTerminating = true; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2420 | if (!isValidRecordSizeInRange(0, 1, "return")) |
Karl Schimpf | c0fdc27 | 2014-09-02 10:47:28 -0700 | [diff] [blame] | 2421 | return; |
Jim Stichnoth | bfb410d | 2014-11-05 16:04:05 -0800 | [diff] [blame] | 2422 | if (Values.empty()) { |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2423 | CurrentNode->appendInst(Ice::InstRet::create(Func.get())); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2424 | } else { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2425 | Ice::Operand *RetVal = getRelativeOperand(Values[0], BaseIndex); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2426 | CurrentNode->appendInst(Ice::InstRet::create(Func.get(), RetVal)); |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2427 | } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2428 | return; |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2429 | } |
| 2430 | case naclbitc::FUNC_CODE_INST_BR: { |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2431 | InstIsTerminating = true; |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2432 | if (Values.size() == 1) { |
| 2433 | // BR: [bb#] |
| 2434 | Ice::CfgNode *Block = getBranchBasicBlock(Values[0]); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 2435 | if (Block == nullptr) |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2436 | return; |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2437 | CurrentNode->appendInst(Ice::InstBr::create(Func.get(), Block)); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2438 | } else { |
| 2439 | // BR: [bb#, bb#, opval] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2440 | if (!isValidRecordSize(3, "branch")) |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2441 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2442 | Ice::Operand *Cond = getRelativeOperand(Values[2], BaseIndex); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2443 | if (Cond->getType() != Ice::IceType_i1) { |
| 2444 | std::string Buffer; |
| 2445 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2446 | StrBuf << "Branch condition " << *Cond |
| 2447 | << " not i1. Found: " << Cond->getType(); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2448 | Error(StrBuf.str()); |
| 2449 | return; |
| 2450 | } |
| 2451 | Ice::CfgNode *ThenBlock = getBranchBasicBlock(Values[0]); |
| 2452 | Ice::CfgNode *ElseBlock = getBranchBasicBlock(Values[1]); |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 2453 | if (ThenBlock == nullptr || ElseBlock == nullptr) |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2454 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2455 | CurrentNode->appendInst( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2456 | Ice::InstBr::create(Func.get(), Cond, ThenBlock, ElseBlock)); |
Karl Schimpf | c836acb | 2014-09-05 08:32:47 -0700 | [diff] [blame] | 2457 | } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2458 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2459 | } |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2460 | case naclbitc::FUNC_CODE_INST_SWITCH: { |
| 2461 | // SWITCH: [Condty, Cond, BbIndex, NumCases Case ...] |
| 2462 | // where Case = [1, 1, Value, BbIndex]. |
| 2463 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2464 | // Note: Unlike most instructions, we don't infer the type of Cond, but |
| 2465 | // provide it as a separate field. There are also unnecessary data fields |
| 2466 | // (i.e. constants 1). These were not cleaned up in PNaCl bitcode because |
| 2467 | // the bitcode format was already frozen when the problem was noticed. |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2468 | InstIsTerminating = true; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2469 | if (!isValidRecordSizeAtLeast(4, "switch")) |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2470 | return; |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2471 | |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 2472 | Ice::Type CondTy = Context->getSimpleTypeByID(Values[0]); |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2473 | if (!Ice::isScalarIntegerType(CondTy)) { |
| 2474 | std::string Buffer; |
| 2475 | raw_string_ostream StrBuf(Buffer); |
| 2476 | StrBuf << "Case condition must be non-wide integer. Found: " << CondTy; |
| 2477 | Error(StrBuf.str()); |
| 2478 | return; |
| 2479 | } |
| 2480 | Ice::SizeT BitWidth = Ice::getScalarIntBitWidth(CondTy); |
| 2481 | Ice::Operand *Cond = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2482 | |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2483 | if (CondTy != Cond->getType()) { |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2484 | std::string Buffer; |
| 2485 | raw_string_ostream StrBuf(Buffer); |
| 2486 | StrBuf << "Case condition expects type " << CondTy |
| 2487 | << ". Found: " << Cond->getType(); |
| 2488 | Error(StrBuf.str()); |
| 2489 | return; |
| 2490 | } |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2491 | Ice::CfgNode *DefaultLabel = getBranchBasicBlock(Values[2]); |
Karl Schimpf | 0042fea | 2015-08-21 09:41:01 -0700 | [diff] [blame] | 2492 | if (DefaultLabel == nullptr) |
| 2493 | return; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2494 | uint64_t NumCasesRaw = Values[3]; |
| 2495 | if (NumCasesRaw > std::numeric_limits<uint32_t>::max()) { |
| 2496 | std::string Buffer; |
| 2497 | raw_string_ostream StrBuf(Buffer); |
| 2498 | StrBuf << "Too many cases specified in switch: " << NumCasesRaw; |
| 2499 | Error(StrBuf.str()); |
| 2500 | NumCasesRaw = std::numeric_limits<uint32_t>::max(); |
| 2501 | } |
| 2502 | uint32_t NumCases = NumCasesRaw; |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2503 | |
| 2504 | // Now recognize each of the cases. |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2505 | if (!isValidRecordSize(4 + NumCases * 4, "switch")) |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2506 | return; |
Karl Schimpf | 0042fea | 2015-08-21 09:41:01 -0700 | [diff] [blame] | 2507 | std::unique_ptr<Ice::InstSwitch> Switch( |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2508 | Ice::InstSwitch::create(Func.get(), NumCases, Cond, DefaultLabel)); |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2509 | unsigned ValCaseIndex = 4; // index to beginning of case entry. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2510 | for (uint32_t CaseIndex = 0; CaseIndex < NumCases; |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2511 | ++CaseIndex, ValCaseIndex += 4) { |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 2512 | if (Values[ValCaseIndex] != 1 || Values[ValCaseIndex + 1] != 1) { |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2513 | std::string Buffer; |
| 2514 | raw_string_ostream StrBuf(Buffer); |
| 2515 | StrBuf << "Sequence [1, 1, value, label] expected for case entry " |
| 2516 | << "in switch record. (at index" << ValCaseIndex << ")"; |
| 2517 | Error(StrBuf.str()); |
| 2518 | return; |
| 2519 | } |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 2520 | BitcodeInt Value(BitWidth, |
Karl Schimpf | 3281748 | 2014-12-15 09:52:26 -0800 | [diff] [blame] | 2521 | NaClDecodeSignRotatedValue(Values[ValCaseIndex + 2])); |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2522 | Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]); |
Karl Schimpf | 0042fea | 2015-08-21 09:41:01 -0700 | [diff] [blame] | 2523 | if (Label == nullptr) |
| 2524 | return; |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2525 | Switch->addBranch(CaseIndex, Value.getSExtValue(), Label); |
| 2526 | } |
Karl Schimpf | 0042fea | 2015-08-21 09:41:01 -0700 | [diff] [blame] | 2527 | CurrentNode->appendInst(Switch.release()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2528 | return; |
Karl Schimpf | d1a971a | 2014-09-17 15:38:17 -0700 | [diff] [blame] | 2529 | } |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2530 | case naclbitc::FUNC_CODE_INST_UNREACHABLE: { |
| 2531 | // UNREACHABLE: [] |
Karl Schimpf | 6c17dd8 | 2015-06-30 10:25:27 -0700 | [diff] [blame] | 2532 | InstIsTerminating = true; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2533 | if (!isValidRecordSize(0, "unreachable")) |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2534 | return; |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2535 | CurrentNode->appendInst(Ice::InstUnreachable::create(Func.get())); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2536 | return; |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2537 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2538 | case naclbitc::FUNC_CODE_INST_PHI: { |
| 2539 | // PHI: [ty, val1, bb1, ..., valN, bbN] for n >= 2. |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2540 | if (!isValidRecordSizeAtLeast(3, "phi")) |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2541 | return; |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2542 | Ice::Type Ty = Context->getSimpleTypeByID(Values[0]); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2543 | if ((Values.size() & 0x1) == 0) { |
| 2544 | // Not an odd number of values. |
| 2545 | std::string Buffer; |
| 2546 | raw_string_ostream StrBuf(Buffer); |
| 2547 | StrBuf << "function block phi record size not valid: " << Values.size(); |
| 2548 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2549 | appendErrorInstruction(Ty); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2550 | return; |
| 2551 | } |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2552 | if (Ty == Ice::IceType_void) { |
| 2553 | Error("Phi record using type void not allowed"); |
| 2554 | return; |
| 2555 | } |
| 2556 | Ice::Variable *Dest = getNextInstVar(Ty); |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2557 | Ice::InstPhi *Phi = |
| 2558 | Ice::InstPhi::create(Func.get(), Values.size() >> 1, Dest); |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2559 | for (size_t i = 1; i < Values.size(); i += 2) { |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2560 | Ice::Operand *Op = |
| 2561 | getRelativeOperand(NaClDecodeSignRotatedValue(Values[i]), BaseIndex); |
| 2562 | if (Op->getType() != Ty) { |
| 2563 | std::string Buffer; |
| 2564 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2565 | StrBuf << "Value " << *Op << " not type " << Ty |
| 2566 | << " in phi instruction. Found: " << Op->getType(); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2567 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2568 | appendErrorInstruction(Ty); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2569 | return; |
| 2570 | } |
| 2571 | Phi->addArgument(Op, getBasicBlock(Values[i + 1])); |
| 2572 | } |
| 2573 | CurrentNode->appendInst(Phi); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2574 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2575 | } |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2576 | case naclbitc::FUNC_CODE_INST_ALLOCA: { |
| 2577 | // ALLOCA: [Size, align] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2578 | if (!isValidRecordSize(2, "alloca")) |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2579 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2580 | Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 07af2ac | 2015-09-30 15:33:41 -0700 | [diff] [blame] | 2581 | uint32_t Alignment = Context->extractAlignment(this, "Alloca", Values[1]); |
Karl Schimpf | 4019f08 | 2014-12-15 13:45:00 -0800 | [diff] [blame] | 2582 | Ice::Type PtrTy = Ice::getPointerType(); |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2583 | if (ByteCount->getType() != Ice::IceType_i32) { |
| 2584 | std::string Buffer; |
| 2585 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 2586 | StrBuf << "Alloca on non-i32 value. Found: " << *ByteCount; |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2587 | Error(StrBuf.str()); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2588 | appendErrorInstruction(PtrTy); |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2589 | return; |
| 2590 | } |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2591 | CurrentNode->appendInst(Ice::InstAlloca::create( |
David Sehr | 2f3b8ec | 2015-11-16 16:51:39 -0800 | [diff] [blame] | 2592 | Func.get(), getNextInstVar(PtrTy), ByteCount, Alignment)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2593 | return; |
Karl Schimpf | 742d72d | 2014-09-09 11:40:09 -0700 | [diff] [blame] | 2594 | } |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2595 | case naclbitc::FUNC_CODE_INST_LOAD: { |
| 2596 | // LOAD: [address, align, ty] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2597 | if (!isValidRecordSize(3, "load")) |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2598 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2599 | Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2600 | Ice::Type Ty = Context->getSimpleTypeByID(Values[2]); |
Karl Schimpf | 07af2ac | 2015-09-30 15:33:41 -0700 | [diff] [blame] | 2601 | uint32_t Alignment = Context->extractAlignment(this, "Load", Values[1]); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2602 | if (!isValidPointerType(Address, "Load")) { |
| 2603 | appendErrorInstruction(Ty); |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2604 | return; |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2605 | } |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2606 | if (!isValidLoadStoreAlignment(Alignment, Ty, "Load")) { |
| 2607 | appendErrorInstruction(Ty); |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2608 | return; |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2609 | } |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2610 | CurrentNode->appendInst(Ice::InstLoad::create( |
| 2611 | Func.get(), getNextInstVar(Ty), Address, Alignment)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2612 | return; |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2613 | } |
| 2614 | case naclbitc::FUNC_CODE_INST_STORE: { |
| 2615 | // STORE: [address, value, align] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2616 | if (!isValidRecordSize(3, "store")) |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2617 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2618 | Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2619 | Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex); |
Karl Schimpf | 07af2ac | 2015-09-30 15:33:41 -0700 | [diff] [blame] | 2620 | uint32_t Alignment = Context->extractAlignment(this, "Store", Values[2]); |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2621 | if (!isValidPointerType(Address, "Store")) |
| 2622 | return; |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2623 | if (!isValidLoadStoreAlignment(Alignment, Value->getType(), "Store")) |
| 2624 | return; |
Karl Schimpf | 4766156 | 2014-09-11 14:42:49 -0700 | [diff] [blame] | 2625 | CurrentNode->appendInst( |
Jim Stichnoth | 8e92838 | 2015-02-02 17:03:08 -0800 | [diff] [blame] | 2626 | Ice::InstStore::create(Func.get(), Value, Address, Alignment)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2627 | return; |
Karl Schimpf | 41689df | 2014-09-10 14:36:07 -0700 | [diff] [blame] | 2628 | } |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2629 | case naclbitc::FUNC_CODE_INST_CALL: |
| 2630 | case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: { |
| 2631 | // CALL: [cc, fnid, arg0, arg1...] |
| 2632 | // CALL_INDIRECT: [cc, fn, returnty, args...] |
| 2633 | // |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2634 | // Note: The difference between CALL and CALL_INDIRECT is that CALL has a |
| 2635 | // reference to an explicit function declaration, while the CALL_INDIRECT |
| 2636 | // is just an address. For CALL, we can infer the return type by looking up |
| 2637 | // the type signature associated with the function declaration. For |
| 2638 | // CALL_INDIRECT we can only infer the type signature via argument types, |
| 2639 | // and the corresponding return type stored in CALL_INDIRECT record. |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2640 | Ice::SizeT ParamsStartIndex = 2; |
| 2641 | if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2642 | if (!isValidRecordSizeAtLeast(2, "call")) |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2643 | return; |
| 2644 | } else { |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2645 | if (!isValidRecordSizeAtLeast(3, "call indirect")) |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2646 | return; |
| 2647 | ParamsStartIndex = 3; |
| 2648 | } |
| 2649 | |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2650 | uint32_t CalleeIndex = convertRelativeToAbsIndex(Values[1], BaseIndex); |
| 2651 | Ice::Operand *Callee = getOperand(CalleeIndex); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2652 | |
| 2653 | // Pull out signature/return type of call (if possible). |
| 2654 | Ice::FunctionDeclaration *Fcn = nullptr; |
Karl Schimpf | ff94f59 | 2015-09-18 10:37:44 -0700 | [diff] [blame] | 2655 | const Ice::FuncSigType *Signature = nullptr; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2656 | Ice::Type ReturnType = Ice::IceType_void; |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 2657 | const Ice::Intrinsics::FullIntrinsicInfo *IntrinsicInfo = nullptr; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2658 | if (Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL) { |
Karl Schimpf | 958ff34 | 2015-09-22 14:09:06 -0700 | [diff] [blame] | 2659 | Fcn = Context->getFunctionByID(CalleeIndex); |
Karl Schimpf | ff94f59 | 2015-09-18 10:37:44 -0700 | [diff] [blame] | 2660 | Signature = &Fcn->getSignature(); |
| 2661 | ReturnType = Signature->getReturnType(); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2662 | Ice::SizeT NumParams = Values.size() - ParamsStartIndex; |
| 2663 | if (NumParams != Signature->getNumArgs()) { |
| 2664 | std::string Buffer; |
| 2665 | raw_string_ostream StrBuf(Buffer); |
| 2666 | StrBuf << "Call to " << printName(Fcn) << " has " << NumParams |
| 2667 | << " parameters. Signature expects: " << Signature->getNumArgs(); |
| 2668 | Error(StrBuf.str()); |
| 2669 | if (ReturnType != Ice::IceType_void) |
| 2670 | setNextLocalInstIndex(nullptr); |
| 2671 | return; |
| 2672 | } |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2673 | |
| 2674 | // Check if this direct call is to an Intrinsic (starts with "llvm.") |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 2675 | IntrinsicInfo = Fcn->getIntrinsicInfo(getTranslator().getContext()); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2676 | if (IntrinsicInfo && IntrinsicInfo->getNumArgs() != NumParams) { |
| 2677 | std::string Buffer; |
| 2678 | raw_string_ostream StrBuf(Buffer); |
| 2679 | StrBuf << "Call to " << printName(Fcn) << " has " << NumParams |
| 2680 | << " parameters. Intrinsic expects: " << Signature->getNumArgs(); |
Jim Stichnoth | a67fc44 | 2015-03-03 16:13:11 -0800 | [diff] [blame] | 2681 | Error(StrBuf.str()); |
Karl Schimpf | ff94f59 | 2015-09-18 10:37:44 -0700 | [diff] [blame] | 2682 | if (ReturnType != Ice::IceType_void) |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2683 | setNextLocalInstIndex(nullptr); |
Jim Stichnoth | a67fc44 | 2015-03-03 16:13:11 -0800 | [diff] [blame] | 2684 | return; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2685 | } |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2686 | } else { // Record.GetCode() == naclbitc::FUNC_CODE_INST_CALL_INDIRECT |
| 2687 | // There is no signature. Assume defined by parameter types. |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 2688 | ReturnType = Context->getSimpleTypeByID(Values[2]); |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2689 | if (Callee != nullptr) |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2690 | isValidPointerType(Callee, "Call indirect"); |
| 2691 | } |
| 2692 | |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2693 | if (Callee == nullptr) |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2694 | return; |
| 2695 | |
| 2696 | // Extract out the the call parameters. |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2697 | SmallVector<Ice::Operand *, 8> Params; |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2698 | for (Ice::SizeT Index = ParamsStartIndex; Index < Values.size(); ++Index) { |
| 2699 | Ice::Operand *Op = getRelativeOperand(Values[Index], BaseIndex); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2700 | if (Op == nullptr) { |
| 2701 | std::string Buffer; |
| 2702 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2703 | StrBuf << "Parameter " << (Index - ParamsStartIndex + 1) << " of " |
| 2704 | << printName(Fcn) << " is not defined"; |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2705 | Error(StrBuf.str()); |
| 2706 | if (ReturnType != Ice::IceType_void) |
| 2707 | setNextLocalInstIndex(nullptr); |
| 2708 | return; |
| 2709 | } |
| 2710 | Params.push_back(Op); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
Karl Schimpf | ff94f59 | 2015-09-18 10:37:44 -0700 | [diff] [blame] | 2713 | // Check return type. |
| 2714 | if (IntrinsicInfo == nullptr && !isCallReturnType(ReturnType)) { |
| 2715 | std::string Buffer; |
| 2716 | raw_string_ostream StrBuf(Buffer); |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2717 | StrBuf << "Return type of " << printName(Fcn) |
| 2718 | << " is invalid: " << ReturnType; |
Karl Schimpf | ff94f59 | 2015-09-18 10:37:44 -0700 | [diff] [blame] | 2719 | Error(StrBuf.str()); |
| 2720 | ReturnType = Ice::IceType_i32; |
| 2721 | } |
| 2722 | |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2723 | // Type check call parameters. |
| 2724 | for (Ice::SizeT Index = 0; Index < Params.size(); ++Index) { |
| 2725 | Ice::Operand *Op = Params[Index]; |
| 2726 | Ice::Type OpType = Op->getType(); |
| 2727 | if (Signature) |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 2728 | verifyCallArgTypeMatches(Fcn, Index, OpType, |
| 2729 | Signature->getArgType(Index)); |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 2730 | else if (!isCallParameterType(OpType)) { |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2731 | std::string Buffer; |
| 2732 | raw_string_ostream StrBuf(Buffer); |
| 2733 | StrBuf << "Argument " << *Op << " of " << printName(Fcn) |
| 2734 | << " has invalid type: " << Op->getType(); |
| 2735 | Error(StrBuf.str()); |
| 2736 | appendErrorInstruction(ReturnType); |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 2737 | return; |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2741 | // Extract call information. |
| 2742 | uint64_t CCInfo = Values[0]; |
| 2743 | CallingConv::ID CallingConv; |
| 2744 | if (!naclbitc::DecodeCallingConv(CCInfo >> 1, CallingConv)) { |
| 2745 | std::string Buffer; |
| 2746 | raw_string_ostream StrBuf(Buffer); |
| 2747 | StrBuf << "Function call calling convention value " << (CCInfo >> 1) |
| 2748 | << " not understood."; |
| 2749 | Error(StrBuf.str()); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2750 | appendErrorInstruction(ReturnType); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2751 | return; |
| 2752 | } |
Jim Stichnoth | 2d6c826 | 2016-02-07 09:50:27 -0800 | [diff] [blame] | 2753 | const bool IsTailCall = (CCInfo & 1); |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2754 | |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2755 | // Create the call instruction. |
Karl Schimpf | e3f64d0 | 2014-10-07 10:38:22 -0700 | [diff] [blame] | 2756 | Ice::Variable *Dest = (ReturnType == Ice::IceType_void) |
| 2757 | ? nullptr |
| 2758 | : getNextInstVar(ReturnType); |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2759 | std::unique_ptr<Ice::InstCall> Instr; |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2760 | if (IntrinsicInfo) { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2761 | Instr.reset(Ice::InstIntrinsicCall::create( |
| 2762 | Func.get(), Params.size(), Dest, Callee, IntrinsicInfo->Info)); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2763 | } else { |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2764 | Instr.reset(Ice::InstCall::create(Func.get(), Params.size(), Dest, Callee, |
| 2765 | IsTailCall)); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2766 | } |
Karl Schimpf | fc0a52d | 2015-09-28 11:22:55 -0700 | [diff] [blame] | 2767 | for (Ice::Operand *Param : Params) |
Jim Stichnoth | 8cfeb69 | 2016-02-05 09:50:02 -0800 | [diff] [blame] | 2768 | Instr->addArg(Param); |
| 2769 | CurrentNode->appendInst(Instr.release()); |
Karl Schimpf | 8df26f3 | 2014-09-19 09:33:26 -0700 | [diff] [blame] | 2770 | return; |
| 2771 | } |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 2772 | case naclbitc::FUNC_CODE_INST_FORWARDTYPEREF: { |
| 2773 | // FORWARDTYPEREF: [opval, ty] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2774 | if (!isValidRecordSize(2, "forward type ref")) |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 2775 | return; |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 2776 | Ice::Type OpType = Context->getSimpleTypeByID(Values[1]); |
Jim Stichnoth | 0d4fc92 | 2015-12-13 21:36:33 -0800 | [diff] [blame] | 2777 | setOperand(Values[0], createInstVar(OpType)); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2778 | return; |
Karl Schimpf | 8f07aa8 | 2014-09-17 09:07:20 -0700 | [diff] [blame] | 2779 | } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2780 | default: |
| 2781 | // Generate error message! |
| 2782 | BlockParserBaseClass::ProcessRecord(); |
Karl Schimpf | aff9fa2 | 2014-10-29 14:32:07 -0700 | [diff] [blame] | 2783 | return; |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2784 | } |
Karl Schimpf | d6064a1 | 2014-08-27 15:34:58 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2787 | /// Parses constants within a function block. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 2788 | class ConstantsParser final : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 2789 | ConstantsParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 2790 | ConstantsParser(const ConstantsParser &) = delete; |
| 2791 | ConstantsParser &operator=(const ConstantsParser &) = delete; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2792 | |
| 2793 | public: |
| 2794 | ConstantsParser(unsigned BlockID, FunctionParser *FuncParser) |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 2795 | : BlockParserBaseClass(BlockID, FuncParser), |
| 2796 | Timer(Ice::TimerStack::TT_parseConstants, getTranslator().getContext()), |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 2797 | FuncParser(FuncParser) {} |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2798 | |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 2799 | ~ConstantsParser() override = default; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2800 | |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2801 | const char *getBlockName() const override { return "constants"; } |
| 2802 | |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2803 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 2804 | Ice::TimerMarker Timer; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2805 | // The parser of the function block this constants block appears in. |
| 2806 | FunctionParser *FuncParser; |
| 2807 | // The type to use for succeeding constants. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 2808 | Ice::Type NextConstantType = Ice::IceType_void; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2809 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 2810 | void ProcessRecord() override; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2811 | |
| 2812 | Ice::GlobalContext *getContext() { return getTranslator().getContext(); } |
| 2813 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2814 | // Returns true if the type to use for succeeding constants is defined. If |
| 2815 | // false, also generates an error message. |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2816 | bool isValidNextConstantType() { |
| 2817 | if (NextConstantType != Ice::IceType_void) |
| 2818 | return true; |
| 2819 | Error("Constant record not preceded by set type record"); |
| 2820 | return false; |
| 2821 | } |
| 2822 | }; |
| 2823 | |
| 2824 | void ConstantsParser::ProcessRecord() { |
| 2825 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 2826 | switch (Record.GetCode()) { |
| 2827 | case naclbitc::CST_CODE_SETTYPE: { |
| 2828 | // SETTYPE: [typeid] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2829 | if (!isValidRecordSize(1, "set type")) |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2830 | return; |
Karl Schimpf | 645aa1a | 2014-10-08 09:05:53 -0700 | [diff] [blame] | 2831 | NextConstantType = Context->getSimpleTypeByID(Values[0]); |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2832 | if (NextConstantType == Ice::IceType_void) |
| 2833 | Error("constants block set type not allowed for void type"); |
| 2834 | return; |
| 2835 | } |
| 2836 | case naclbitc::CST_CODE_UNDEF: { |
| 2837 | // UNDEF |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2838 | if (!isValidRecordSize(0, "undef")) |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2839 | return; |
| 2840 | if (!isValidNextConstantType()) |
| 2841 | return; |
| 2842 | FuncParser->setNextConstantID( |
| 2843 | getContext()->getConstantUndef(NextConstantType)); |
| 2844 | return; |
| 2845 | } |
| 2846 | case naclbitc::CST_CODE_INTEGER: { |
| 2847 | // INTEGER: [intval] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2848 | if (!isValidRecordSize(1, "integer")) |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2849 | return; |
| 2850 | if (!isValidNextConstantType()) |
| 2851 | return; |
Karl Schimpf | 4019f08 | 2014-12-15 13:45:00 -0800 | [diff] [blame] | 2852 | if (Ice::isScalarIntegerType(NextConstantType)) { |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 2853 | BitcodeInt Value(Ice::getScalarIntBitWidth(NextConstantType), |
Karl Schimpf | 3281748 | 2014-12-15 09:52:26 -0800 | [diff] [blame] | 2854 | NaClDecodeSignRotatedValue(Values[0])); |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 2855 | if (Ice::Constant *C = getContext()->getConstantInt( |
| 2856 | NextConstantType, Value.getSExtValue())) { |
| 2857 | FuncParser->setNextConstantID(C); |
| 2858 | return; |
| 2859 | } |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2860 | } |
| 2861 | std::string Buffer; |
| 2862 | raw_string_ostream StrBuf(Buffer); |
| 2863 | StrBuf << "constant block integer record for non-integer type " |
| 2864 | << NextConstantType; |
| 2865 | Error(StrBuf.str()); |
| 2866 | return; |
| 2867 | } |
| 2868 | case naclbitc::CST_CODE_FLOAT: { |
| 2869 | // FLOAT: [fpval] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2870 | if (!isValidRecordSize(1, "float")) |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2871 | return; |
| 2872 | if (!isValidNextConstantType()) |
| 2873 | return; |
| 2874 | switch (NextConstantType) { |
| 2875 | case Ice::IceType_f32: { |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 2876 | const BitcodeInt Value(32, static_cast<uint32_t>(Values[0])); |
| 2877 | float FpValue = Value.convertToFp<int32_t, float>(); |
Karl Schimpf | 3281748 | 2014-12-15 09:52:26 -0800 | [diff] [blame] | 2878 | FuncParser->setNextConstantID(getContext()->getConstantFloat(FpValue)); |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2879 | return; |
| 2880 | } |
| 2881 | case Ice::IceType_f64: { |
Karl Schimpf | a5295b0 | 2015-12-01 11:24:53 -0800 | [diff] [blame] | 2882 | const BitcodeInt Value(64, Values[0]); |
| 2883 | double FpValue = Value.convertToFp<uint64_t, double>(); |
Karl Schimpf | 3281748 | 2014-12-15 09:52:26 -0800 | [diff] [blame] | 2884 | FuncParser->setNextConstantID(getContext()->getConstantDouble(FpValue)); |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2885 | return; |
| 2886 | } |
| 2887 | default: { |
| 2888 | std::string Buffer; |
| 2889 | raw_string_ostream StrBuf(Buffer); |
| 2890 | StrBuf << "constant block float record for non-floating type " |
| 2891 | << NextConstantType; |
| 2892 | Error(StrBuf.str()); |
| 2893 | return; |
| 2894 | } |
| 2895 | } |
| 2896 | } |
| 2897 | default: |
| 2898 | // Generate error message! |
| 2899 | BlockParserBaseClass::ProcessRecord(); |
| 2900 | return; |
| 2901 | } |
| 2902 | } |
| 2903 | |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2904 | // Parses valuesymtab blocks appearing in a function block. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 2905 | class FunctionValuesymtabParser final : public ValuesymtabParser { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 2906 | FunctionValuesymtabParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 2907 | FunctionValuesymtabParser(const FunctionValuesymtabParser &) = delete; |
| 2908 | void operator=(const FunctionValuesymtabParser &) = delete; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2909 | |
| 2910 | public: |
| 2911 | FunctionValuesymtabParser(unsigned BlockID, FunctionParser *EnclosingParser) |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 2912 | : ValuesymtabParser(BlockID, EnclosingParser), |
| 2913 | Timer(Ice::TimerStack::TT_parseFunctionValuesymtabs, |
| 2914 | getTranslator().getContext()) {} |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2915 | |
| 2916 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 2917 | Ice::TimerMarker Timer; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2918 | // Returns the enclosing function parser. |
| 2919 | FunctionParser *getFunctionParser() const { |
| 2920 | return reinterpret_cast<FunctionParser *>(GetEnclosingParser()); |
| 2921 | } |
| 2922 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 2923 | const char *getTableKind() const override { return "Function"; } |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 2924 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 2925 | void setValueName(NaClBcIndexSize_t Index, StringType &Name) override; |
| 2926 | void setBbName(NaClBcIndexSize_t Index, StringType &Name) override; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2927 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 2928 | // Reports that the assignment of Name to the value associated with index is |
| 2929 | // not possible, for the given Context. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2930 | void reportUnableToAssign(const char *Context, NaClBcIndexSize_t Index, |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2931 | StringType &Name) { |
| 2932 | std::string Buffer; |
| 2933 | raw_string_ostream StrBuf(Buffer); |
| 2934 | StrBuf << "Function-local " << Context << " name '" << Name |
| 2935 | << "' can't be associated with index " << Index; |
| 2936 | Error(StrBuf.str()); |
| 2937 | } |
| 2938 | }; |
| 2939 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2940 | void FunctionValuesymtabParser::setValueName(NaClBcIndexSize_t Index, |
| 2941 | StringType &Name) { |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2942 | // Note: We check when Index is too small, so that we can error recover |
| 2943 | // (FP->getOperand will create fatal error). |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 2944 | if (Index < getFunctionParser()->getNumGlobalIDs()) { |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 2945 | reportUnableToAssign("Global value", Index, Name); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2946 | return; |
| 2947 | } |
| 2948 | Ice::Operand *Op = getFunctionParser()->getOperand(Index); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 2949 | if (auto *V = dyn_cast<Ice::Variable>(Op)) { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 2950 | if (Ice::BuildDefs::dump()) { |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 2951 | std::string Nm(Name.data(), Name.size()); |
| 2952 | V->setName(getFunctionParser()->getFunc(), Nm); |
| 2953 | } |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2954 | } else { |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 2955 | reportUnableToAssign("Local value", Index, Name); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2956 | } |
| 2957 | } |
| 2958 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 2959 | void FunctionValuesymtabParser::setBbName(NaClBcIndexSize_t Index, |
| 2960 | StringType &Name) { |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 2961 | if (!Ice::BuildDefs::dump()) |
| 2962 | return; |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2963 | if (Index >= getFunctionParser()->getFunc()->getNumNodes()) { |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 2964 | reportUnableToAssign("Basic block", Index, Name); |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2965 | return; |
| 2966 | } |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2967 | std::string Nm(Name.data(), Name.size()); |
Karl Schimpf | 98ed446 | 2015-08-14 13:08:42 -0700 | [diff] [blame] | 2968 | if (Ice::BuildDefs::dump()) |
| 2969 | getFunctionParser()->getFunc()->getNodes()[Index]->setName(Nm); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2972 | bool FunctionParser::ParseBlock(unsigned BlockID) { |
Jim Stichnoth | f5fdd23 | 2016-05-09 12:24:36 -0700 | [diff] [blame] | 2973 | #ifndef PNACL_LLVM |
| 2974 | constexpr bool PNaClAllowLocalSymbolTables = true; |
| 2975 | #endif // !PNACL_LLVM |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2976 | switch (BlockID) { |
| 2977 | case naclbitc::CONSTANTS_BLOCK_ID: { |
| 2978 | ConstantsParser Parser(BlockID, this); |
| 2979 | return Parser.ParseThisBlock(); |
| 2980 | } |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2981 | case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
| 2982 | if (PNaClAllowLocalSymbolTables) { |
| 2983 | FunctionValuesymtabParser Parser(BlockID, this); |
| 2984 | return Parser.ParseThisBlock(); |
| 2985 | } |
| 2986 | break; |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2987 | } |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 2988 | default: |
| 2989 | break; |
| 2990 | } |
| 2991 | return BlockParserBaseClass::ParseBlock(BlockID); |
Karl Schimpf | f12355e | 2014-09-08 13:41:09 -0700 | [diff] [blame] | 2992 | } |
| 2993 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 2994 | /// Parses the module block in the bitcode file. |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 2995 | class ModuleParser final : public BlockParserBaseClass { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 2996 | ModuleParser() = delete; |
| 2997 | ModuleParser(const ModuleParser &) = delete; |
| 2998 | ModuleParser &operator=(const ModuleParser &) = delete; |
| 2999 | |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3000 | public: |
| 3001 | ModuleParser(unsigned BlockID, TopLevelParser *Context) |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 3002 | : BlockParserBaseClass(BlockID, Context), |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 3003 | Timer(Ice::TimerStack::TT_parseModule, |
Karl Schimpf | 3dd127e | 2016-03-31 13:22:11 -0700 | [diff] [blame] | 3004 | Context->getTranslator().getContext()), |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 3005 | IsParseParallel(Ice::getFlags().isParseParallel()) {} |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 3006 | ~ModuleParser() override = default; |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3007 | const char *getBlockName() const override { return "module"; } |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3008 | NaClBitstreamCursor &getCursor() const { return Record.GetCursor(); } |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3009 | |
Karl Schimpf | 5ee234a | 2014-09-12 10:41:40 -0700 | [diff] [blame] | 3010 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 3011 | Ice::TimerMarker Timer; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 3012 | // True if we have already installed names for unnamed global declarations, |
| 3013 | // and have generated global constant initializers. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 3014 | bool GlobalDeclarationNamesAndInitializersInstalled = false; |
Karl Schimpf | c49eeae | 2015-09-17 10:19:48 -0700 | [diff] [blame] | 3015 | // True if we have already processed the symbol table for the module. |
| 3016 | bool FoundValuesymtab = false; |
Karl Schimpf | 3dd127e | 2016-03-31 13:22:11 -0700 | [diff] [blame] | 3017 | const bool IsParseParallel; |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 3018 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3019 | // Generates names for unnamed global addresses (i.e. functions and global |
| 3020 | // variables). Then lowers global variable declaration initializers to the |
| 3021 | // target. May be called multiple times. Only the first call will do the |
| 3022 | // installation. |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 3023 | void installGlobalNamesAndGlobalVarInitializers() { |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 3024 | if (!GlobalDeclarationNamesAndInitializersInstalled) { |
Karl Schimpf | 6ca7d2b | 2015-02-10 14:43:45 -0800 | [diff] [blame] | 3025 | Context->installGlobalNames(); |
| 3026 | Context->createValueIDs(); |
Karl Schimpf | bba7768 | 2016-01-15 07:33:15 -0800 | [diff] [blame] | 3027 | Context->verifyFunctionTypeSignatures(); |
Karl Schimpf | c49eeae | 2015-09-17 10:19:48 -0700 | [diff] [blame] | 3028 | std::unique_ptr<Ice::VariableDeclarationList> Globals = |
| 3029 | Context->getGlobalVariables(); |
| 3030 | if (Globals) |
| 3031 | getTranslator().lowerGlobals(std::move(Globals)); |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 3032 | GlobalDeclarationNamesAndInitializersInstalled = true; |
| 3033 | } |
| 3034 | } |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 3035 | bool ParseBlock(unsigned BlockID) override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3036 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3037 | void ExitBlock() override { |
| 3038 | installGlobalNamesAndGlobalVarInitializers(); |
| 3039 | Context->getTranslator().getContext()->waitForWorkerThreads(); |
| 3040 | } |
Karl Schimpf | 6ff33d2 | 2014-09-22 10:28:42 -0700 | [diff] [blame] | 3041 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 3042 | void ProcessRecord() override; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3043 | }; |
| 3044 | |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3045 | class ModuleValuesymtabParser : public ValuesymtabParser { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 3046 | ModuleValuesymtabParser() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 3047 | ModuleValuesymtabParser(const ModuleValuesymtabParser &) = delete; |
| 3048 | void operator=(const ModuleValuesymtabParser &) = delete; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3049 | |
| 3050 | public: |
| 3051 | ModuleValuesymtabParser(unsigned BlockID, ModuleParser *MP) |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 3052 | : ValuesymtabParser(BlockID, MP), |
| 3053 | Timer(Ice::TimerStack::TT_parseModuleValuesymtabs, |
| 3054 | getTranslator().getContext()) {} |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3055 | |
Jim Stichnoth | e587d94 | 2015-06-22 15:49:04 -0700 | [diff] [blame] | 3056 | ~ModuleValuesymtabParser() override = default; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3057 | |
| 3058 | private: |
Karl Schimpf | 5845587 | 2014-11-03 11:29:39 -0800 | [diff] [blame] | 3059 | Ice::TimerMarker Timer; |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3060 | const char *getTableKind() const override { return "Module"; } |
| 3061 | void setValueName(NaClBcIndexSize_t Index, StringType &Name) override; |
| 3062 | void setBbName(NaClBcIndexSize_t Index, StringType &Name) override; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3063 | }; |
| 3064 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 3065 | void ModuleValuesymtabParser::setValueName(NaClBcIndexSize_t Index, |
| 3066 | StringType &Name) { |
Sean Klein | 0a3c523 | 2016-03-11 09:49:36 -0800 | [diff] [blame] | 3067 | Ice::GlobalDeclaration *Decl = Context->getGlobalDeclarationByID(Index); |
| 3068 | if (llvm::isa<Ice::VariableDeclaration>(Decl) && |
| 3069 | Decl->isPNaClABIExternalName(Name.str())) { |
| 3070 | // Force linkage of (specific) Global Variables be external for the PNaCl |
| 3071 | // ABI. PNaCl bitcode has a linkage field for Functions, but not for |
| 3072 | // GlobalVariables (because the latter is not needed for pexes, so it has |
| 3073 | // been removed). |
| 3074 | Decl->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3075 | } |
| 3076 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 3077 | // Unconditionally capture the name if it is provided in the input file, |
| 3078 | // regardless of whether dump is enabled or whether the symbol is internal vs |
| 3079 | // external. This fits in well with the lit tests, and most symbols in a |
| 3080 | // conforming pexe are nameless and don't take this path. |
| 3081 | Decl->setName(getTranslator().getContext(), |
| 3082 | StringRef(Name.data(), Name.size())); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 3085 | void ModuleValuesymtabParser::setBbName(NaClBcIndexSize_t Index, |
| 3086 | StringType &Name) { |
Karl Schimpf | 52863b1 | 2015-09-16 13:51:21 -0700 | [diff] [blame] | 3087 | reportUnableToAssign("Basic block", Index, Name); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3090 | class CfgParserWorkItem final : public Ice::OptWorkItem { |
| 3091 | CfgParserWorkItem() = delete; |
| 3092 | CfgParserWorkItem(const CfgParserWorkItem &) = delete; |
| 3093 | CfgParserWorkItem &operator=(const CfgParserWorkItem &) = delete; |
| 3094 | |
| 3095 | public: |
| 3096 | CfgParserWorkItem(unsigned BlockID, NaClBcIndexSize_t FcnId, |
| 3097 | ModuleParser *ModParser, std::unique_ptr<uint8_t[]> Buffer, |
| 3098 | uintptr_t BufferSize, uint64_t StartBit, uint32_t SeqNumber) |
| 3099 | : BlockID(BlockID), FcnId(FcnId), ModParser(ModParser), |
| 3100 | Buffer(std::move(Buffer)), BufferSize(BufferSize), StartBit(StartBit), |
| 3101 | SeqNumber(SeqNumber) {} |
| 3102 | std::unique_ptr<Ice::Cfg> getParsedCfg() override; |
| 3103 | ~CfgParserWorkItem() override = default; |
| 3104 | |
| 3105 | private: |
| 3106 | const unsigned BlockID; |
| 3107 | const NaClBcIndexSize_t FcnId; |
| 3108 | // Note: ModParser can't be const because the function parser needs to |
| 3109 | // access non-const member functions (of ModuleParser and TopLevelParser). |
| 3110 | // TODO(kschimpf): Fix this issue. |
| 3111 | ModuleParser *ModParser; |
| 3112 | const std::unique_ptr<uint8_t[]> Buffer; |
| 3113 | const uintptr_t BufferSize; |
| 3114 | const uint64_t StartBit; |
| 3115 | const uint32_t SeqNumber; |
| 3116 | }; |
| 3117 | |
| 3118 | std::unique_ptr<Ice::Cfg> CfgParserWorkItem::getParsedCfg() { |
| 3119 | NaClBitstreamCursor &OldCursor(ModParser->getCursor()); |
Karl Schimpf | 2a9d186 | 2016-04-02 10:06:56 -0700 | [diff] [blame] | 3120 | llvm::NaClBitstreamReader Reader(OldCursor.getStartWordByteForBit(StartBit), |
| 3121 | Buffer.get(), Buffer.get() + BufferSize, |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3122 | OldCursor.getBitStreamReader()); |
| 3123 | NaClBitstreamCursor NewCursor(Reader); |
| 3124 | NewCursor.JumpToBit(NewCursor.getWordBitNo(StartBit)); |
| 3125 | FunctionParser Parser(BlockID, ModParser, FcnId, NewCursor); |
| 3126 | return Parser.parseFunction(SeqNumber); |
| 3127 | } |
| 3128 | |
Jim Stichnoth | 8e8042c | 2014-09-25 17:51:47 -0700 | [diff] [blame] | 3129 | bool ModuleParser::ParseBlock(unsigned BlockID) { |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3130 | switch (BlockID) { |
| 3131 | case naclbitc::BLOCKINFO_BLOCK_ID: |
| 3132 | return NaClBitcodeParser::ParseBlock(BlockID); |
| 3133 | case naclbitc::TYPE_BLOCK_ID_NEW: { |
| 3134 | TypesParser Parser(BlockID, this); |
| 3135 | return Parser.ParseThisBlock(); |
| 3136 | } |
| 3137 | case naclbitc::GLOBALVAR_BLOCK_ID: { |
| 3138 | GlobalsParser Parser(BlockID, this); |
| 3139 | return Parser.ParseThisBlock(); |
| 3140 | } |
| 3141 | case naclbitc::VALUE_SYMTAB_BLOCK_ID: { |
Karl Schimpf | c49eeae | 2015-09-17 10:19:48 -0700 | [diff] [blame] | 3142 | if (FoundValuesymtab) |
| 3143 | Fatal("Duplicate valuesymtab in module"); |
| 3144 | |
Karl Schimpf | 28fc2d7 | 2015-09-22 13:14:55 -0700 | [diff] [blame] | 3145 | // If we have already processed a function block (i.e. we have already |
| 3146 | // installed global names and variable initializers) we can no longer accept |
| 3147 | // the value symbol table. Names have already been generated. |
| 3148 | if (GlobalDeclarationNamesAndInitializersInstalled) |
| 3149 | Fatal("Module valuesymtab not allowed after function blocks"); |
| 3150 | |
Karl Schimpf | c49eeae | 2015-09-17 10:19:48 -0700 | [diff] [blame] | 3151 | FoundValuesymtab = true; |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 3152 | ModuleValuesymtabParser Parser(BlockID, this); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3153 | return Parser.ParseThisBlock(); |
| 3154 | } |
| 3155 | case naclbitc::FUNCTION_BLOCK_ID: { |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 3156 | installGlobalNamesAndGlobalVarInitializers(); |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3157 | Ice::GlobalContext *Ctx = Context->getTranslator().getContext(); |
| 3158 | uint32_t SeqNumber = Context->getTranslator().getNextSequenceNumber(); |
| 3159 | NaClBcIndexSize_t FcnId = Context->getNextFunctionBlockValueID(); |
Karl Schimpf | 3dd127e | 2016-03-31 13:22:11 -0700 | [diff] [blame] | 3160 | if (IsParseParallel) { |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3161 | // Skip the block and copy into a buffer. Note: We copy into a buffer |
| 3162 | // using the top-level parser to make sure that the underlying |
| 3163 | // buffer reading from the data streamer is not thread safe. |
| 3164 | NaClBitstreamCursor &Cursor = Record.GetCursor(); |
| 3165 | uint64_t StartBit = Cursor.GetCurrentBitNo(); |
| 3166 | if (SkipBlock()) |
| 3167 | return true; |
| 3168 | const uint64_t EndBit = Cursor.GetCurrentBitNo(); |
| 3169 | const uintptr_t StartByte = Cursor.getStartWordByteForBit(StartBit); |
| 3170 | const uintptr_t EndByte = Cursor.getEndWordByteForBit(EndBit); |
| 3171 | const uintptr_t BufferSize = EndByte - StartByte; |
| 3172 | std::unique_ptr<uint8_t[]> Buffer((uint8_t *)(new uint8_t[BufferSize])); |
| 3173 | for (size_t i = Cursor.fillBuffer(Buffer.get(), BufferSize, StartByte); |
| 3174 | i < BufferSize; ++i) { |
| 3175 | Buffer[i] = 0; |
| 3176 | } |
| 3177 | Ctx->optQueueBlockingPush(Ice::makeUnique<CfgParserWorkItem>( |
| 3178 | BlockID, FcnId, this, std::move(Buffer), BufferSize, StartBit, |
| 3179 | SeqNumber)); |
| 3180 | return false; |
| 3181 | } else { |
| 3182 | FunctionParser Parser(BlockID, this, FcnId); |
| 3183 | std::unique_ptr<Ice::Cfg> Func = Parser.parseFunction(SeqNumber); |
| 3184 | bool Failed = Func->hasError(); |
| 3185 | getTranslator().translateFcn(std::move(Func)); |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 3186 | return Failed && !Ice::getFlags().getAllowErrorRecovery(); |
Karl Schimpf | e8457a2 | 2016-03-31 10:20:23 -0700 | [diff] [blame] | 3187 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3188 | } |
| 3189 | default: |
| 3190 | return BlockParserBaseClass::ParseBlock(BlockID); |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | void ModuleParser::ProcessRecord() { |
| 3195 | const NaClBitcodeRecord::RecordVector &Values = Record.GetValues(); |
| 3196 | switch (Record.GetCode()) { |
| 3197 | case naclbitc::MODULE_CODE_VERSION: { |
| 3198 | // VERSION: [version#] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3199 | if (!isValidRecordSize(1, "version")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3200 | return; |
Karl Schimpf | 74cd883 | 2015-06-23 11:05:01 -0700 | [diff] [blame] | 3201 | uint64_t Version = Values[0]; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3202 | if (Version != 1) { |
| 3203 | std::string Buffer; |
| 3204 | raw_string_ostream StrBuf(Buffer); |
| 3205 | StrBuf << "Unknown bitstream version: " << Version; |
| 3206 | Error(StrBuf.str()); |
| 3207 | } |
| 3208 | return; |
| 3209 | } |
| 3210 | case naclbitc::MODULE_CODE_FUNCTION: { |
| 3211 | // FUNCTION: [type, callingconv, isproto, linkage] |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3212 | if (!isValidRecordSize(4, "address")) |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3213 | return; |
Karl Schimpf | 9d98d79 | 2014-10-13 15:01:08 -0700 | [diff] [blame] | 3214 | const Ice::FuncSigType &Signature = Context->getFuncSigTypeByID(Values[0]); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3215 | CallingConv::ID CallingConv; |
| 3216 | if (!naclbitc::DecodeCallingConv(Values[1], CallingConv)) { |
| 3217 | std::string Buffer; |
| 3218 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3219 | StrBuf << "Function address has unknown calling convention: " |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3220 | << Values[1]; |
| 3221 | Error(StrBuf.str()); |
| 3222 | return; |
| 3223 | } |
| 3224 | GlobalValue::LinkageTypes Linkage; |
| 3225 | if (!naclbitc::DecodeLinkage(Values[3], Linkage)) { |
| 3226 | std::string Buffer; |
| 3227 | raw_string_ostream StrBuf(Buffer); |
Karl Schimpf | 2f6f860 | 2014-12-01 13:39:00 -0800 | [diff] [blame] | 3228 | StrBuf << "Function address has unknown linkage. Found " << Values[3]; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3229 | Error(StrBuf.str()); |
| 3230 | return; |
| 3231 | } |
Karl Schimpf | 0c729c8 | 2015-01-28 10:58:25 -0800 | [diff] [blame] | 3232 | bool IsProto = Values[2] == 1; |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 3233 | auto *Func = Ice::FunctionDeclaration::create( |
John Porto | 1bec8bc | 2015-06-22 10:51:13 -0700 | [diff] [blame] | 3234 | Context->getTranslator().getContext(), Signature, CallingConv, Linkage, |
| 3235 | IsProto); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3236 | Context->setNextFunctionID(Func); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3237 | return; |
| 3238 | } |
| 3239 | default: |
| 3240 | BlockParserBaseClass::ProcessRecord(); |
| 3241 | return; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | bool TopLevelParser::ParseBlock(unsigned BlockID) { |
| 3246 | if (BlockID == naclbitc::MODULE_BLOCK_ID) { |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 3247 | if (ParsedModuleBlock) |
| 3248 | Fatal("Input can't contain more than one module"); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3249 | ModuleParser Parser(BlockID, this); |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 3250 | bool ParseFailed = Parser.ParseThisBlock(); |
| 3251 | ParsedModuleBlock = true; |
| 3252 | return ParseFailed; |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3253 | } |
| 3254 | // Generate error message by using default block implementation. |
| 3255 | BlockParserBaseClass Parser(BlockID, this); |
| 3256 | return Parser.ParseThisBlock(); |
| 3257 | } |
| 3258 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 3259 | } // end of anonymous namespace |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3260 | |
| 3261 | namespace Ice { |
| 3262 | |
Karl Schimpf | 2e7daef | 2015-01-09 13:04:13 -0800 | [diff] [blame] | 3263 | void PNaClTranslator::translateBuffer(const std::string &IRFilename, |
| 3264 | MemoryBuffer *MemBuf) { |
Jan Voung | c1f07ea | 2015-03-06 14:53:30 -0800 | [diff] [blame] | 3265 | std::unique_ptr<MemoryObject> MemObj(getNonStreamedMemoryObject( |
| 3266 | reinterpret_cast<const unsigned char *>(MemBuf->getBufferStart()), |
| 3267 | reinterpret_cast<const unsigned char *>(MemBuf->getBufferEnd()))); |
| 3268 | translate(IRFilename, std::move(MemObj)); |
| 3269 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3270 | |
Jan Voung | c1f07ea | 2015-03-06 14:53:30 -0800 | [diff] [blame] | 3271 | void PNaClTranslator::translate(const std::string &IRFilename, |
| 3272 | std::unique_ptr<MemoryObject> &&MemObj) { |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 3273 | // On error, we report_fatal_error to avoid destroying the MemObj. That may |
| 3274 | // still be in use by IceBrowserCompileServer. Otherwise, we need to change |
| 3275 | // the MemObj to be ref-counted, or have a wrapper, or simply leak. We also |
| 3276 | // need a hook to tell the IceBrowserCompileServer to unblock its |
| 3277 | // QueueStreamer. |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 3278 | // https://code.google.com/p/nativeclient/issues/detail?id=4163 |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3279 | // Read header and verify it is good. |
| 3280 | NaClBitcodeHeader Header; |
Karl Schimpf | b33a2af | 2015-05-12 08:26:23 -0700 | [diff] [blame] | 3281 | if (Header.Read(MemObj.get())) { |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 3282 | llvm::report_fatal_error("Invalid PNaCl bitcode header"); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3283 | } |
Karl Schimpf | b33a2af | 2015-05-12 08:26:23 -0700 | [diff] [blame] | 3284 | if (!Header.IsSupported()) { |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 3285 | getContext()->getStrError() << Header.Unsupported(); |
Karl Schimpf | b33a2af | 2015-05-12 08:26:23 -0700 | [diff] [blame] | 3286 | if (!Header.IsReadable()) { |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 3287 | llvm::report_fatal_error("Invalid PNaCl bitcode header"); |
Karl Schimpf | b33a2af | 2015-05-12 08:26:23 -0700 | [diff] [blame] | 3288 | } |
| 3289 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3290 | |
| 3291 | // Create a bitstream reader to read the bitcode file. |
Karl Schimpf | b33a2af | 2015-05-12 08:26:23 -0700 | [diff] [blame] | 3292 | NaClBitstreamReader InputStreamFile(MemObj.release(), Header); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3293 | NaClBitstreamCursor InputStream(InputStreamFile); |
| 3294 | |
Karl Schimpf | 22ed4eb | 2015-03-04 12:17:20 -0800 | [diff] [blame] | 3295 | TopLevelParser Parser(*this, InputStream, ErrorStatus); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3296 | while (!InputStream.AtEndOfStream()) { |
| 3297 | if (Parser.Parse()) { |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 3298 | ErrorStatus.assign(EC_Bitcode); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3299 | return; |
| 3300 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
Karl Schimpf | 0b8763e | 2015-09-22 10:41:11 -0700 | [diff] [blame] | 3303 | if (!Parser.parsedModuleBlock()) { |
| 3304 | std::string Buffer; |
| 3305 | raw_string_ostream StrBuf(Buffer); |
| 3306 | StrBuf << IRFilename << ": Does not contain a module!"; |
| 3307 | llvm::report_fatal_error(StrBuf.str()); |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3308 | } |
Jan Voung | c1f07ea | 2015-03-06 14:53:30 -0800 | [diff] [blame] | 3309 | if (InputStreamFile.getBitcodeBytes().getExtent() % 4 != 0) { |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 3310 | llvm::report_fatal_error("Bitcode stream should be a multiple of 4 bytes"); |
Jan Voung | c1f07ea | 2015-03-06 14:53:30 -0800 | [diff] [blame] | 3311 | } |
Karl Schimpf | 8d7abae | 2014-07-07 14:50:30 -0700 | [diff] [blame] | 3312 | } |
| 3313 | |
Jim Stichnoth | 989a703 | 2014-08-08 10:13:44 -0700 | [diff] [blame] | 3314 | } // end of namespace Ice |