John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [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 | |
| 7 | #ifndef _SYMBOL_TABLE_INCLUDED_ |
| 8 | #define _SYMBOL_TABLE_INCLUDED_ |
| 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 | |
| 33 | #include <assert.h> |
| 34 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 35 | #include "InfoSink.h" |
| 36 | #include "intermediate.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 37 | |
| 38 | // |
| 39 | // Symbol base class. (Can build functions or variables out of these...) |
| 40 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 41 | class TSymbol |
| 42 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 43 | public: |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 44 | POOL_ALLOCATOR_NEW_DELETE(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 45 | TSymbol(const TString *n) : name(n) { } |
| 46 | virtual ~TSymbol() { /* don't delete name, it's from the pool */ } |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 47 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 48 | const TString& getName() const { return *name; } |
| 49 | virtual const TString& getMangledName() const { return getName(); } |
| 50 | virtual bool isFunction() const { return false; } |
| 51 | virtual bool isVariable() const { return false; } |
| 52 | void setUniqueId(int id) { uniqueId = id; } |
| 53 | int getUniqueId() const { return uniqueId; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 54 | TSymbol(const TSymbol&); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | const TString *name; |
| 58 | unsigned int uniqueId; // For real comparing during code generation |
| 59 | }; |
| 60 | |
| 61 | // |
| 62 | // Variable class, meaning a symbol that's not a function. |
| 63 | // |
| 64 | // There could be a separate class heirarchy for Constant variables; |
| 65 | // Only one of int, bool, or float, (or none) is correct for |
| 66 | // any particular use, but it's easy to do this way, and doesn't |
| 67 | // seem worth having separate classes, and "getConst" can't simply return |
| 68 | // different values for different types polymorphically, so this is |
| 69 | // just simple and pragmatic. |
| 70 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 71 | class TVariable : public TSymbol |
| 72 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 73 | public: |
| 74 | TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { } |
| 75 | virtual ~TVariable() { } |
| 76 | virtual bool isVariable() const { return true; } |
| 77 | TType& getType() { return type; } |
| 78 | const TType& getType() const { return type; } |
| 79 | bool isUserType() const { return userType; } |
| 80 | void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); } |
| 81 | void updateArrayInformationType(TType *t) { arrayInformationType = t; } |
| 82 | TType* getArrayInformationType() { return arrayInformationType; } |
| 83 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 84 | ConstantUnion* getConstPointer() |
| 85 | { |
| 86 | if (!unionArray) |
| 87 | unionArray = new ConstantUnion[type.getObjectSize()]; |
| 88 | |
| 89 | return unionArray; |
| 90 | } |
| 91 | |
| 92 | ConstantUnion* getConstPointer() const { return unionArray; } |
| 93 | |
| 94 | void shareConstPointer( ConstantUnion *constArray) |
| 95 | { |
| 96 | if (unionArray == constArray) |
| 97 | return; |
| 98 | |
| 99 | delete[] unionArray; |
| 100 | unionArray = constArray; |
| 101 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 102 | |
| 103 | protected: |
| 104 | TType type; |
| 105 | bool userType; |
| 106 | // we are assuming that Pool Allocator will free the memory allocated to unionArray |
| 107 | // when this object is destroyed |
| 108 | ConstantUnion *unionArray; |
| 109 | TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol |
| 110 | }; |
| 111 | |
| 112 | // |
| 113 | // The function sub-class of symbols and the parser will need to |
| 114 | // share this definition of a function parameter. |
| 115 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 116 | struct TParameter |
| 117 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 118 | TString *name; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 119 | TType *type; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // |
| 123 | // The function sub-class of a symbol. |
| 124 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 125 | class TFunction : public TSymbol |
| 126 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 127 | public: |
| 128 | TFunction(TOperator o) : |
| 129 | TSymbol(0), |
| 130 | returnType(TType(EbtVoid, EbpUndefined)), |
| 131 | op(o), |
| 132 | defined(false) { } |
| 133 | TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) : |
| 134 | TSymbol(name), |
| 135 | returnType(retType), |
| 136 | mangledName(TFunction::mangleName(*name)), |
| 137 | op(tOp), |
| 138 | defined(false) { } |
| 139 | virtual ~TFunction(); |
| 140 | virtual bool isFunction() const { return true; } |
| 141 | |
| 142 | static TString mangleName(const TString& name) { return name + '('; } |
| 143 | static TString unmangleName(const TString& mangledName) |
| 144 | { |
| 145 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 146 | } |
| 147 | |
| 148 | void addParameter(TParameter& p) |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 149 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 150 | parameters.push_back(p); |
| 151 | mangledName = mangledName + p.type->getMangledName(); |
| 152 | } |
| 153 | |
| 154 | const TString& getMangledName() const { return mangledName; } |
| 155 | const TType& getReturnType() const { return returnType; } |
| 156 | |
| 157 | void relateToOperator(TOperator o) { op = o; } |
| 158 | TOperator getBuiltInOp() const { return op; } |
| 159 | |
| 160 | void relateToExtension(const TString& ext) { extension = ext; } |
| 161 | const TString& getExtension() const { return extension; } |
| 162 | |
| 163 | void setDefined() { defined = true; } |
| 164 | bool isDefined() { return defined; } |
| 165 | |
| 166 | int getParamCount() const { return static_cast<int>(parameters.size()); } |
| 167 | const TParameter& getParam(int i) const { return parameters[i]; } |
| 168 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 169 | protected: |
| 170 | typedef TVector<TParameter> TParamList; |
| 171 | TParamList parameters; |
| 172 | TType returnType; |
| 173 | TString mangledName; |
| 174 | TOperator op; |
| 175 | TString extension; |
| 176 | bool defined; |
| 177 | }; |
| 178 | |
| 179 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 180 | class TSymbolTableLevel |
| 181 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 182 | public: |
| 183 | typedef TMap<TString, TSymbol*> tLevel; |
| 184 | typedef tLevel::const_iterator const_iterator; |
| 185 | typedef const tLevel::value_type tLevelPair; |
| 186 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 187 | |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 188 | POOL_ALLOCATOR_NEW_DELETE(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 189 | TSymbolTableLevel() { } |
| 190 | ~TSymbolTableLevel(); |
| 191 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 192 | bool insert(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 193 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 194 | symbol.setUniqueId(++uniqueId); |
| 195 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 196 | // |
| 197 | // returning true means symbol was added to the table |
| 198 | // |
| 199 | tInsertResult result; |
| 200 | result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); |
| 201 | |
| 202 | return result.second; |
| 203 | } |
| 204 | |
| 205 | TSymbol* find(const TString& name) const |
| 206 | { |
| 207 | tLevel::const_iterator it = level.find(name); |
| 208 | if (it == level.end()) |
| 209 | return 0; |
| 210 | else |
| 211 | return (*it).second; |
| 212 | } |
| 213 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 214 | void relateToOperator(const char* name, TOperator op); |
| 215 | void relateToExtension(const char* name, const TString& ext); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 216 | |
| 217 | protected: |
| 218 | tLevel level; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 219 | static int uniqueId; // for unique identification in code generation |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 220 | }; |
| 221 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 222 | class TSymbolTable |
| 223 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 224 | public: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 225 | TSymbolTable() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 226 | { |
| 227 | // |
| 228 | // The symbol table cannot be used until push() is called, but |
| 229 | // the lack of an initial call to push() can be used to detect |
| 230 | // that the symbol table has not been preloaded with built-ins. |
| 231 | // |
| 232 | } |
| 233 | |
| 234 | ~TSymbolTable() |
| 235 | { |
| 236 | // level 0 is always built In symbols, so we never pop that out |
| 237 | while (table.size() > 1) |
| 238 | pop(); |
| 239 | } |
| 240 | |
| 241 | // |
| 242 | // When the symbol table is initialized with the built-ins, there should |
| 243 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 244 | // globals are at level 1. |
| 245 | // |
| 246 | bool isEmpty() { return table.size() == 0; } |
| 247 | bool atBuiltInLevel() { return table.size() == 1; } |
| 248 | bool atGlobalLevel() { return table.size() <= 2; } |
| 249 | void push() |
| 250 | { |
| 251 | table.push_back(new TSymbolTableLevel); |
| 252 | precisionStack.push_back( PrecisionStackLevel() ); |
| 253 | } |
| 254 | |
| 255 | void pop() |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 256 | { |
| 257 | delete table[currentLevel()]; |
| 258 | table.pop_back(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 259 | precisionStack.pop_back(); |
| 260 | } |
| 261 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 262 | bool insert(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 263 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 264 | return table[currentLevel()]->insert(symbol); |
| 265 | } |
| 266 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 267 | bool insertConstInt(const char *name, int value) |
| 268 | { |
| 269 | TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); |
| 270 | constant->getConstPointer()->setIConst(value); |
| 271 | return insert(*constant); |
| 272 | } |
| 273 | |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 274 | bool insertBuiltIn(TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 275 | { |
| 276 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue); |
| 277 | |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 278 | TParameter param1 = {0, ptype1}; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 279 | function->addParameter(param1); |
| 280 | |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 281 | if(ptype2) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 282 | { |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 283 | TParameter param2 = {0, ptype2}; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 284 | function->addParameter(param2); |
| 285 | } |
| 286 | |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 287 | if(ptype3) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 288 | { |
Nicolas Capens | 434311c | 2014-05-28 19:23:55 -0400 | [diff] [blame] | 289 | TParameter param3 = {0, ptype3}; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 290 | function->addParameter(param3); |
| 291 | } |
| 292 | |
| 293 | return insert(*function); |
| 294 | } |
| 295 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 296 | TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false) const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 297 | { |
| 298 | int level = currentLevel(); |
| 299 | TSymbol* symbol; |
| 300 | do { |
| 301 | symbol = table[level]->find(name); |
| 302 | --level; |
| 303 | } while (symbol == 0 && level >= 0); |
| 304 | level++; |
| 305 | if (builtIn) |
| 306 | *builtIn = level == 0; |
| 307 | if (sameScope) |
| 308 | *sameScope = level == currentLevel(); |
| 309 | return symbol; |
| 310 | } |
| 311 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 312 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 313 | { |
| 314 | return table[0]->find(name); |
| 315 | } |
| 316 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 317 | TSymbolTableLevel *getGlobalLevel() const |
| 318 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 319 | assert(table.size() >= 2); |
| 320 | return table[1]; |
| 321 | } |
| 322 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 323 | TSymbolTableLevel *getOuterLevel() const |
| 324 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 325 | assert(table.size() >= 2); |
| 326 | return table[currentLevel() - 1]; |
| 327 | } |
| 328 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 329 | void relateToOperator(const char* name, TOperator op) |
| 330 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 331 | table[0]->relateToOperator(name, op); |
| 332 | } |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 333 | |
| 334 | void relateToExtension(const char* name, const TString& ext) |
| 335 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 336 | table[0]->relateToExtension(name, ext); |
| 337 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 338 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 339 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 340 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 341 | if (IsSampler(type.type)) |
| 342 | return true; // Skip sampler types for the time being |
| 343 | if (type.type != EbtFloat && type.type != EbtInt) |
| 344 | return false; // Only set default precision for int/float |
| 345 | if (type.size != 1 || type.matrix || type.array) |
| 346 | return false; // Not allowed to set for aggregate types |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 347 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 348 | precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value |
| 349 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // Searches down the precisionStack for a precision qualifier for the specified TBasicType |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 353 | TPrecision getDefaultPrecision( TBasicType type) |
| 354 | { |
| 355 | // unsigned integers use the same precision as signed |
| 356 | if (type == EbtUInt) type = EbtInt; |
| 357 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 358 | if( type != EbtFloat && type != EbtInt ) return EbpUndefined; |
| 359 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 360 | assert( level >= 0); // Just to be safe. Should not happen. |
| 361 | PrecisionStackLevel::iterator it; |
| 362 | TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this? |
| 363 | while( level >= 0 ){ |
| 364 | it = precisionStack[level].find( type ); |
| 365 | if( it != precisionStack[level].end() ){ |
| 366 | prec = (*it).second; |
| 367 | break; |
| 368 | } |
| 369 | level--; |
| 370 | } |
| 371 | return prec; |
| 372 | } |
| 373 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame^] | 374 | protected: |
| 375 | int currentLevel() const { return static_cast<int>(table.size() - 1); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 376 | |
| 377 | std::vector<TSymbolTableLevel*> table; |
| 378 | typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; |
| 379 | std::vector< PrecisionStackLevel > precisionStack; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | #endif // _SYMBOL_TABLE_INCLUDED_ |