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