daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | 1fbc287 | 2014-01-03 14:12:09 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 7 | #ifndef COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |
| 8 | #define COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | |
| 10 | // |
| 11 | // Symbol table for parsing. Has these design characteristics: |
| 12 | // |
| 13 | // * Same symbol table can be used to compile many shaders, to preserve |
| 14 | // effort of creating and loading with the large numbers of built-in |
| 15 | // symbols. |
| 16 | // |
| 17 | // * Name mangling will be used to give each function a unique name |
| 18 | // so that symbol table lookups are never ambiguous. This allows |
| 19 | // a simpler symbol table structure. |
| 20 | // |
| 21 | // * Pushing and popping of scope, so symbol table will really be a stack |
| 22 | // of symbol tables. Searched from the top, with new inserts going into |
| 23 | // the top. |
| 24 | // |
| 25 | // * Constants: Compile time constant symbols will keep their values |
| 26 | // in the symbol table. The parser can substitute constants at parse |
| 27 | // time, including doing constant folding and constant propagation. |
| 28 | // |
| 29 | // * No temporaries: Temporaries made from operations (+, --, .xy, etc.) |
| 30 | // are tracked in the intermediate representation, not the symbol table. |
| 31 | // |
| 32 | |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 33 | #include <array> |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 34 | #include <assert.h> |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 35 | #include <set> |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 36 | |
Jamie Madill | 703cdd6 | 2013-07-08 15:07:30 -0400 | [diff] [blame] | 37 | #include "common/angleutils.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 38 | #include "compiler/translator/InfoSink.h" |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 39 | #include "compiler/translator/IntermNode.h" |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 40 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 41 | namespace sh |
| 42 | { |
| 43 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 44 | // Symbol base class. (Can build functions or variables out of these...) |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 45 | class TSymbol : angle::NonCopyable |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 46 | { |
| 47 | public: |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 48 | POOL_ALLOCATOR_NEW_DELETE(); |
Olli Etuaho | 476197f | 2016-10-11 13:59:08 +0100 | [diff] [blame] | 49 | TSymbol(const TString *n); |
| 50 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 51 | virtual ~TSymbol() |
| 52 | { |
| 53 | // don't delete name, it's from the pool |
| 54 | } |
Alok Priyadarshi | 8156b6b | 2013-09-23 14:56:58 -0400 | [diff] [blame] | 55 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 56 | const TString &getName() const |
| 57 | { |
| 58 | return *name; |
| 59 | } |
| 60 | virtual const TString &getMangledName() const |
| 61 | { |
| 62 | return getName(); |
| 63 | } |
| 64 | virtual bool isFunction() const |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | virtual bool isVariable() const |
| 69 | { |
| 70 | return false; |
| 71 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 72 | int getUniqueId() const |
| 73 | { |
| 74 | return uniqueId; |
| 75 | } |
| 76 | void relateToExtension(const TString &ext) |
| 77 | { |
| 78 | extension = ext; |
| 79 | } |
| 80 | const TString &getExtension() const |
| 81 | { |
| 82 | return extension; |
| 83 | } |
Nicolas Capens | ba60ad3 | 2013-06-04 15:55:47 -0400 | [diff] [blame] | 84 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 85 | private: |
Olli Etuaho | 476197f | 2016-10-11 13:59:08 +0100 | [diff] [blame] | 86 | const int uniqueId; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 87 | const TString *name; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 88 | TString extension; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 91 | // Variable class, meaning a symbol that's not a function. |
| 92 | // |
| 93 | // There could be a separate class heirarchy for Constant variables; |
| 94 | // Only one of int, bool, or float, (or none) is correct for |
| 95 | // any particular use, but it's easy to do this way, and doesn't |
| 96 | // seem worth having separate classes, and "getConst" can't simply return |
| 97 | // different values for different types polymorphically, so this is |
| 98 | // just simple and pragmatic. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 99 | class TVariable : public TSymbol |
| 100 | { |
| 101 | public: |
| 102 | TVariable(const TString *name, const TType &t, bool uT = false) |
| 103 | : TSymbol(name), |
| 104 | type(t), |
| 105 | userType(uT), |
| 106 | unionArray(0) |
| 107 | { |
| 108 | } |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 109 | ~TVariable() override {} |
| 110 | bool isVariable() const override { return true; } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 111 | TType &getType() |
| 112 | { |
| 113 | return type; |
| 114 | } |
| 115 | const TType &getType() const |
| 116 | { |
| 117 | return type; |
| 118 | } |
| 119 | bool isUserType() const |
| 120 | { |
| 121 | return userType; |
| 122 | } |
| 123 | void setQualifier(TQualifier qualifier) |
| 124 | { |
| 125 | type.setQualifier(qualifier); |
| 126 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 128 | const TConstantUnion *getConstPointer() const { return unionArray; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 129 | |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 130 | void shareConstPointer(const TConstantUnion *constArray) { unionArray = constArray; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 131 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 132 | private: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 133 | TType type; |
| 134 | bool userType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 135 | // we are assuming that Pool Allocator will free the memory |
| 136 | // allocated to unionArray when this object is destroyed. |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 137 | const TConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 140 | // Immutable version of TParameter. |
| 141 | struct TConstParameter |
| 142 | { |
| 143 | TConstParameter() |
| 144 | : name(nullptr), |
| 145 | type(nullptr) |
| 146 | { |
| 147 | } |
| 148 | explicit TConstParameter(const TString *n) |
| 149 | : name(n), |
| 150 | type(nullptr) |
| 151 | { |
| 152 | } |
| 153 | explicit TConstParameter(const TType *t) |
| 154 | : name(nullptr), |
| 155 | type(t) |
| 156 | { |
| 157 | } |
| 158 | TConstParameter(const TString *n, const TType *t) |
| 159 | : name(n), |
| 160 | type(t) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | // Both constructor arguments must be const. |
| 165 | TConstParameter(TString *n, TType *t) = delete; |
| 166 | TConstParameter(const TString *n, TType *t) = delete; |
| 167 | TConstParameter(TString *n, const TType *t) = delete; |
| 168 | |
| 169 | const TString *name; |
| 170 | const TType *type; |
| 171 | }; |
| 172 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 173 | // The function sub-class of symbols and the parser will need to |
| 174 | // share this definition of a function parameter. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 175 | struct TParameter |
| 176 | { |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 177 | // Destructively converts to TConstParameter. |
| 178 | // This method resets name and type to nullptrs to make sure |
| 179 | // their content cannot be modified after the call. |
| 180 | TConstParameter turnToConst() |
| 181 | { |
| 182 | const TString *constName = name; |
| 183 | const TType *constType = type; |
| 184 | name = nullptr; |
| 185 | type = nullptr; |
| 186 | return TConstParameter(constName, constType); |
| 187 | } |
| 188 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 189 | TString *name; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 190 | TType *type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | // The function sub-class of a symbol. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 194 | class TFunction : public TSymbol |
| 195 | { |
| 196 | public: |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 197 | TFunction(const TString *name, |
| 198 | const TType *retType, |
| 199 | TOperator tOp = EOpNull, |
| 200 | const char *ext = "") |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 201 | : TSymbol(name), |
| 202 | returnType(retType), |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 203 | mangledName(nullptr), |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 204 | op(tOp), |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 205 | defined(false), |
| 206 | mHasPrototypeDeclaration(false) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 207 | { |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 208 | relateToExtension(ext); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 209 | } |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 210 | ~TFunction() override; |
| 211 | bool isFunction() const override { return true; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 212 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 213 | static TString mangleName(const TString &name) |
| 214 | { |
| 215 | return name + '('; |
| 216 | } |
| 217 | static TString unmangleName(const TString &mangledName) |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 218 | { |
| 219 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 220 | } |
| 221 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 222 | void addParameter(const TConstParameter &p) |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 223 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 224 | parameters.push_back(p); |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 225 | mangledName = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 226 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 227 | |
Olli Etuaho | 476197f | 2016-10-11 13:59:08 +0100 | [diff] [blame] | 228 | void swapParameters(const TFunction ¶metersSource); |
| 229 | |
Corentin Wallez | e5a1f27 | 2015-08-21 02:58:25 +0200 | [diff] [blame] | 230 | const TString &getMangledName() const override |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 231 | { |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 232 | if (mangledName == nullptr) |
| 233 | { |
| 234 | mangledName = buildMangledName(); |
| 235 | } |
| 236 | return *mangledName; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 237 | } |
| 238 | const TType &getReturnType() const |
| 239 | { |
Dmitry Skiba | 7f17a50 | 2015-06-22 15:08:39 -0700 | [diff] [blame] | 240 | return *returnType; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 241 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 242 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 243 | TOperator getBuiltInOp() const |
| 244 | { |
| 245 | return op; |
| 246 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 247 | |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 248 | void setDefined() { defined = true; } |
| 249 | bool isDefined() { return defined; } |
| 250 | void setHasPrototypeDeclaration() { mHasPrototypeDeclaration = true; } |
| 251 | bool hasPrototypeDeclaration() const { return mHasPrototypeDeclaration; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 252 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 253 | size_t getParamCount() const |
| 254 | { |
| 255 | return parameters.size(); |
| 256 | } |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 257 | const TConstParameter &getParam(size_t i) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 258 | { |
| 259 | return parameters[i]; |
| 260 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 261 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 262 | private: |
Olli Etuaho | 476197f | 2016-10-11 13:59:08 +0100 | [diff] [blame] | 263 | void clearParameters(); |
| 264 | |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 265 | const TString *buildMangledName() const; |
| 266 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 267 | typedef TVector<TConstParameter> TParamList; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 268 | TParamList parameters; |
Dmitry Skiba | 7f17a50 | 2015-06-22 15:08:39 -0700 | [diff] [blame] | 269 | const TType *returnType; |
Dmitry Skiba | 5883220 | 2015-07-06 16:11:13 -0700 | [diff] [blame] | 270 | mutable const TString *mangledName; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 271 | TOperator op; |
| 272 | bool defined; |
Olli Etuaho | 5d65318 | 2016-01-04 14:43:28 +0200 | [diff] [blame] | 273 | bool mHasPrototypeDeclaration; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 274 | }; |
| 275 | |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 276 | // Interface block name sub-symbol |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 277 | class TInterfaceBlockName : public TSymbol |
| 278 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 279 | public: |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 280 | TInterfaceBlockName(const TString *name) |
| 281 | : TSymbol(name) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 282 | { |
| 283 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 284 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 285 | virtual ~TInterfaceBlockName() |
| 286 | { |
| 287 | } |
shannonwoods@chromium.org | 5668c5d | 2013-05-30 00:11:48 +0000 | [diff] [blame] | 288 | }; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 289 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 290 | class TSymbolTableLevel |
| 291 | { |
| 292 | public: |
| 293 | typedef TMap<TString, TSymbol *> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 294 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | typedef const tLevel::value_type tLevelPair; |
| 296 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 297 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 298 | TSymbolTableLevel() |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 299 | : mGlobalInvariant(false) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 300 | { |
| 301 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 302 | ~TSymbolTableLevel(); |
| 303 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 304 | bool insert(TSymbol *symbol); |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 305 | |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 306 | // Insert a function using its unmangled name as the key. |
| 307 | bool insertUnmangled(TFunction *function); |
| 308 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 309 | TSymbol *find(const TString &name) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 310 | |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 311 | void addInvariantVarying(const std::string &name) |
| 312 | { |
| 313 | mInvariantVaryings.insert(name); |
| 314 | } |
| 315 | |
| 316 | bool isVaryingInvariant(const std::string &name) |
| 317 | { |
| 318 | return (mGlobalInvariant || mInvariantVaryings.count(name) > 0); |
| 319 | } |
| 320 | |
| 321 | void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; } |
| 322 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 323 | protected: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 324 | tLevel level; |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 325 | std::set<std::string> mInvariantVaryings; |
| 326 | bool mGlobalInvariant; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
Gus Fernandez | 964df49 | 2014-10-13 11:54:39 -0700 | [diff] [blame] | 329 | // Define ESymbolLevel as int rather than an enum since level can go |
| 330 | // above GLOBAL_LEVEL and cause atBuiltInLevel() to fail if the |
| 331 | // compiler optimizes the >= of the last element to ==. |
| 332 | typedef int ESymbolLevel; |
| 333 | const int COMMON_BUILTINS = 0; |
| 334 | const int ESSL1_BUILTINS = 1; |
| 335 | const int ESSL3_BUILTINS = 2; |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 336 | const int ESSL3_1_BUILTINS = 3; |
| 337 | const int LAST_BUILTIN_LEVEL = ESSL3_1_BUILTINS; |
| 338 | const int GLOBAL_LEVEL = 4; |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 339 | |
Jamie Madill | f0d10f8 | 2015-03-31 12:56:52 -0400 | [diff] [blame] | 340 | class TSymbolTable : angle::NonCopyable |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 341 | { |
| 342 | public: |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 343 | TSymbolTable() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 344 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 345 | // The symbol table cannot be used until push() is called, but |
| 346 | // the lack of an initial call to push() can be used to detect |
| 347 | // that the symbol table has not been preloaded with built-ins. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 350 | ~TSymbolTable(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 351 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 352 | // When the symbol table is initialized with the built-ins, there should |
| 353 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 354 | // globals are at level 1. |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 355 | bool isEmpty() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 356 | { |
| 357 | return table.empty(); |
| 358 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 359 | bool atBuiltInLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 360 | { |
| 361 | return currentLevel() <= LAST_BUILTIN_LEVEL; |
| 362 | } |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 363 | bool atGlobalLevel() const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 364 | { |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 365 | return currentLevel() == GLOBAL_LEVEL; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 366 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 367 | void push() |
alokp@chromium.org | e4249f0 | 2010-07-26 18:13:52 +0000 | [diff] [blame] | 368 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 369 | table.push_back(new TSymbolTableLevel); |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 370 | precisionStack.push_back(new PrecisionStackLevel); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 371 | } |
| 372 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 373 | void pop() |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 374 | { |
| 375 | delete table.back(); |
| 376 | table.pop_back(); |
| 377 | |
| 378 | delete precisionStack.back(); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 379 | precisionStack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 382 | bool declare(TSymbol *symbol) |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 383 | { |
| 384 | return insert(currentLevel(), symbol); |
| 385 | } |
| 386 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 387 | bool insert(ESymbolLevel level, TSymbol *symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 388 | { |
shannonwoods@chromium.org | 1c84809 | 2013-05-30 00:02:34 +0000 | [diff] [blame] | 389 | return table[level]->insert(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 390 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 391 | |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 392 | bool insert(ESymbolLevel level, const char *ext, TSymbol *symbol) |
| 393 | { |
| 394 | symbol->relateToExtension(ext); |
| 395 | return table[level]->insert(symbol); |
| 396 | } |
| 397 | |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 398 | bool insertConstInt(ESymbolLevel level, const char *name, int value, TPrecision precision) |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 399 | { |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 400 | TVariable *constant = |
| 401 | new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 1)); |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 402 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 403 | unionArray[0].setIConst(value); |
| 404 | constant->shareConstPointer(unionArray); |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 405 | return insert(level, constant); |
Nicolas Capens | 49a8887 | 2013-06-20 09:54:03 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 408 | bool insertConstIntExt(ESymbolLevel level, const char *ext, const char *name, int value) |
| 409 | { |
| 410 | TVariable *constant = |
| 411 | new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
Olli Etuaho | 5c0e023 | 2015-11-11 15:55:59 +0200 | [diff] [blame] | 412 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 413 | unionArray[0].setIConst(value); |
| 414 | constant->shareConstPointer(unionArray); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 415 | return insert(level, ext, constant); |
| 416 | } |
| 417 | |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 418 | bool insertConstIvec3(ESymbolLevel level, |
| 419 | const char *name, |
| 420 | const std::array<int, 3> &values, |
| 421 | TPrecision precision) |
| 422 | { |
| 423 | TVariable *constantIvec3 = |
| 424 | new TVariable(NewPoolTString(name), TType(EbtInt, precision, EvqConst, 3)); |
| 425 | |
| 426 | TConstantUnion *unionArray = new TConstantUnion[3]; |
| 427 | for (size_t index = 0u; index < 3u; ++index) |
| 428 | { |
| 429 | unionArray[index].setIConst(values[index]); |
| 430 | } |
| 431 | constantIvec3->shareConstPointer(unionArray); |
| 432 | |
| 433 | return insert(level, constantIvec3); |
| 434 | } |
| 435 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 436 | void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name, |
| 437 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0); |
Nicolas Capens | 759b994 | 2014-02-14 17:57:14 -0500 | [diff] [blame] | 438 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 439 | void insertBuiltIn(ESymbolLevel level, const TType *rvalue, const char *name, |
| 440 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 441 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 442 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 443 | insertBuiltIn(level, EOpNull, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 444 | } |
| 445 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 446 | void insertBuiltIn(ESymbolLevel level, const char *ext, const TType *rvalue, const char *name, |
| 447 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 448 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 449 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 450 | insertBuiltIn(level, EOpNull, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
| 451 | } |
| 452 | |
Dmitry Skiba | efa3d8e | 2015-06-22 14:52:10 -0700 | [diff] [blame] | 453 | void insertBuiltIn(ESymbolLevel level, TOperator op, const TType *rvalue, const char *name, |
| 454 | const TType *ptype1, const TType *ptype2 = 0, const TType *ptype3 = 0, const TType *ptype4 = 0, const TType *ptype5 = 0) |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 455 | { |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 456 | insertUnmangledBuiltIn(name); |
Nicolas Capens | c9d9b30 | 2015-02-20 23:02:15 -0500 | [diff] [blame] | 457 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | 482907e | 2015-02-23 16:56:33 -0500 | [diff] [blame] | 458 | } |
| 459 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 460 | TSymbol *find(const TString &name, int shaderVersion, |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 461 | bool *builtIn = NULL, bool *sameScope = NULL) const; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 462 | |
| 463 | TSymbol *findGlobal(const TString &name) const; |
| 464 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 465 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 466 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 467 | TSymbolTableLevel *getOuterLevel() |
| 468 | { |
shannonwoods@chromium.org | 6e10a0e | 2013-05-30 00:02:13 +0000 | [diff] [blame] | 469 | assert(currentLevel() >= 1); |
daniel@transgaming.com | 5dd6d09 | 2012-03-20 20:10:28 +0000 | [diff] [blame] | 470 | return table[currentLevel() - 1]; |
| 471 | } |
| 472 | |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 473 | void dump(TInfoSink &infoSink) const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 474 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 475 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 476 | { |
Martin Radev | 4a9cd80 | 2016-09-01 16:51:51 +0300 | [diff] [blame] | 477 | if (!SupportsPrecision(type.getBasicType())) |
Zhenyao Mo | a5a1dfc | 2013-09-23 14:57:03 -0400 | [diff] [blame] | 478 | return false; |
Martin Radev | 4a9cd80 | 2016-09-01 16:51:51 +0300 | [diff] [blame] | 479 | if (type.getBasicType() == EbtUInt) |
Olli Etuaho | 0980e29 | 2015-11-20 14:57:34 +0200 | [diff] [blame] | 480 | return false; // ESSL 3.00.4 section 4.5.4 |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 481 | if (type.isAggregate()) |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 482 | return false; // Not allowed to set for aggregate types |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 483 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 484 | // Uses map operator [], overwrites the current value |
Martin Radev | 4a9cd80 | 2016-09-01 16:51:51 +0300 | [diff] [blame] | 485 | (*precisionStack[indexOfLastElement])[type.getBasicType()] = prec; |
shannon.woods@transgaming.com | d25a6b3 | 2013-02-28 23:19:13 +0000 | [diff] [blame] | 486 | return true; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 489 | // Searches down the precisionStack for a precision qualifier |
| 490 | // for the specified TBasicType |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 491 | TPrecision getDefaultPrecision(TBasicType type) const; |
shannonwoods@chromium.org | 6b70991 | 2013-05-30 00:20:04 +0000 | [diff] [blame] | 492 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 493 | // This records invariant varyings declared through |
| 494 | // "invariant varying_name;". |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 495 | void addInvariantVarying(const std::string &originalName) |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 496 | { |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 497 | ASSERT(atGlobalLevel()); |
| 498 | table[currentLevel()]->addInvariantVarying(originalName); |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 499 | } |
| 500 | // If this returns false, the varying could still be invariant |
| 501 | // if it is set as invariant during the varying variable |
| 502 | // declaration - this piece of information is stored in the |
| 503 | // variable's type, not here. |
Jamie Madill | 2c43325 | 2014-12-03 12:36:54 -0500 | [diff] [blame] | 504 | bool isVaryingInvariant(const std::string &originalName) const |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 505 | { |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 506 | ASSERT(atGlobalLevel()); |
| 507 | return table[currentLevel()]->isVaryingInvariant(originalName); |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Qiankun Miao | f69682b | 2016-08-16 14:50:42 +0800 | [diff] [blame] | 510 | void setGlobalInvariant(bool invariant) |
| 511 | { |
| 512 | ASSERT(atGlobalLevel()); |
| 513 | table[currentLevel()]->setGlobalInvariant(invariant); |
| 514 | } |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 515 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 516 | static int nextUniqueId() |
| 517 | { |
| 518 | return ++uniqueIdCounter; |
| 519 | } |
| 520 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 521 | bool hasUnmangledBuiltIn(const char *name) |
| 522 | { |
| 523 | return mUnmangledBuiltinNames.count(std::string(name)) > 0; |
| 524 | } |
| 525 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 526 | private: |
| 527 | ESymbolLevel currentLevel() const |
| 528 | { |
| 529 | return static_cast<ESymbolLevel>(table.size() - 1); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 532 | // Used to insert unmangled functions to check redeclaration of built-ins in ESSL 3.00. |
| 533 | void insertUnmangledBuiltIn(const char *name) |
| 534 | { |
| 535 | mUnmangledBuiltinNames.insert(std::string(name)); |
| 536 | } |
| 537 | |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 538 | std::vector<TSymbolTableLevel *> table; |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 539 | typedef TMap<TBasicType, TPrecision> PrecisionStackLevel; |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 540 | std::vector< PrecisionStackLevel *> precisionStack; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 541 | |
Olli Etuaho | c4a96d6 | 2015-07-23 17:37:39 +0530 | [diff] [blame] | 542 | std::set<std::string> mUnmangledBuiltinNames; |
| 543 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 544 | static int uniqueIdCounter; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 547 | } // namespace sh |
| 548 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 549 | #endif // COMPILER_TRANSLATOR_SYMBOLTABLE_H_ |