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 | // |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 21 | // * Pushing and popping of scope, so symbol table will really be a stack |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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 | |
Greg Hartman | d61ac5f | 2015-04-09 18:48:53 -0700 | [diff] [blame] | 33 | #ifndef __ANDROID__ |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 34 | #include <assert.h> |
Greg Hartman | d61ac5f | 2015-04-09 18:48:53 -0700 | [diff] [blame] | 35 | #else |
| 36 | #include "../../Common/DebugAndroid.hpp" |
| 37 | #endif |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 38 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 39 | #include "InfoSink.h" |
| 40 | #include "intermediate.h" |
Alexis Hetu | dd7ff7a | 2015-06-11 08:25:30 -0400 | [diff] [blame] | 41 | #include <set> |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 42 | |
| 43 | // |
| 44 | // Symbol base class. (Can build functions or variables out of these...) |
| 45 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 46 | class TSymbol |
| 47 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 48 | public: |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 49 | POOL_ALLOCATOR_NEW_DELETE(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 50 | TSymbol(const TString *n) : name(n) { } |
| 51 | virtual ~TSymbol() { /* don't delete name, it's from the pool */ } |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 52 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 53 | const TString& getName() const { return *name; } |
| 54 | virtual const TString& getMangledName() const { return getName(); } |
| 55 | virtual bool isFunction() const { return false; } |
| 56 | virtual bool isVariable() const { return false; } |
| 57 | void setUniqueId(int id) { uniqueId = id; } |
| 58 | int getUniqueId() const { return uniqueId; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 59 | TSymbol(const TSymbol&); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 60 | |
| 61 | protected: |
| 62 | const TString *name; |
| 63 | unsigned int uniqueId; // For real comparing during code generation |
| 64 | }; |
| 65 | |
| 66 | // |
| 67 | // Variable class, meaning a symbol that's not a function. |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 68 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 69 | // There could be a separate class heirarchy for Constant variables; |
| 70 | // Only one of int, bool, or float, (or none) is correct for |
| 71 | // any particular use, but it's easy to do this way, and doesn't |
| 72 | // seem worth having separate classes, and "getConst" can't simply return |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 73 | // different values for different types polymorphically, so this is |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 74 | // just simple and pragmatic. |
| 75 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 76 | class TVariable : public TSymbol |
| 77 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 78 | public: |
| 79 | TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { } |
| 80 | virtual ~TVariable() { } |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 81 | virtual bool isVariable() const { return true; } |
| 82 | TType& getType() { return type; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 83 | const TType& getType() const { return type; } |
| 84 | bool isUserType() const { return userType; } |
| 85 | void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); } |
| 86 | void updateArrayInformationType(TType *t) { arrayInformationType = t; } |
| 87 | TType* getArrayInformationType() { return arrayInformationType; } |
| 88 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 89 | ConstantUnion* getConstPointer() |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 90 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 91 | if (!unionArray) |
| 92 | unionArray = new ConstantUnion[type.getObjectSize()]; |
| 93 | |
| 94 | return unionArray; |
| 95 | } |
| 96 | |
| 97 | ConstantUnion* getConstPointer() const { return unionArray; } |
| 98 | |
| 99 | void shareConstPointer( ConstantUnion *constArray) |
| 100 | { |
| 101 | if (unionArray == constArray) |
| 102 | return; |
| 103 | |
| 104 | delete[] unionArray; |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 105 | unionArray = constArray; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 106 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 107 | |
| 108 | protected: |
| 109 | TType type; |
| 110 | bool userType; |
| 111 | // we are assuming that Pool Allocator will free the memory allocated to unionArray |
| 112 | // when this object is destroyed |
| 113 | ConstantUnion *unionArray; |
| 114 | TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol |
| 115 | }; |
| 116 | |
| 117 | // |
| 118 | // The function sub-class of symbols and the parser will need to |
| 119 | // share this definition of a function parameter. |
| 120 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 121 | struct TParameter |
| 122 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 123 | TString *name; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 124 | TType *type; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | // |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 128 | // The function sub-class of a symbol. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 129 | // |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 130 | class TFunction : public TSymbol |
| 131 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 132 | public: |
| 133 | TFunction(TOperator o) : |
| 134 | TSymbol(0), |
| 135 | returnType(TType(EbtVoid, EbpUndefined)), |
| 136 | op(o), |
Alexis Hetu | 407813b | 2016-02-24 16:46:13 -0500 | [diff] [blame] | 137 | defined(false), |
| 138 | prototypeDeclaration(false) { } |
| 139 | TFunction(const TString *name, const TType& retType, TOperator tOp = EOpNull, const char *ext = "") : |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 140 | TSymbol(name), |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 141 | returnType(retType), |
| 142 | mangledName(TFunction::mangleName(*name)), |
| 143 | op(tOp), |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 144 | extension(ext), |
Alexis Hetu | 407813b | 2016-02-24 16:46:13 -0500 | [diff] [blame] | 145 | defined(false), |
| 146 | prototypeDeclaration(false) { } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 147 | virtual ~TFunction(); |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 148 | virtual bool isFunction() const { return true; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 149 | |
| 150 | static TString mangleName(const TString& name) { return name + '('; } |
| 151 | static TString unmangleName(const TString& mangledName) |
| 152 | { |
| 153 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 154 | } |
| 155 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 156 | void addParameter(TParameter& p) |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 157 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 158 | parameters.push_back(p); |
| 159 | mangledName = mangledName + p.type->getMangledName(); |
| 160 | } |
| 161 | |
| 162 | const TString& getMangledName() const { return mangledName; } |
| 163 | const TType& getReturnType() const { return returnType; } |
| 164 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 165 | TOperator getBuiltInOp() const { return op; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 166 | const TString& getExtension() const { return extension; } |
| 167 | |
| 168 | void setDefined() { defined = true; } |
| 169 | bool isDefined() { return defined; } |
Alexis Hetu | 407813b | 2016-02-24 16:46:13 -0500 | [diff] [blame] | 170 | void setHasPrototypeDeclaration() { prototypeDeclaration = true; } |
| 171 | bool hasPrototypeDeclaration() const { return prototypeDeclaration; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 172 | |
Alexis Hetu | a818c45 | 2015-06-11 13:06:58 -0400 | [diff] [blame] | 173 | size_t getParamCount() const { return parameters.size(); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 174 | const TParameter& getParam(int i) const { return parameters[i]; } |
| 175 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 176 | protected: |
| 177 | typedef TVector<TParameter> TParamList; |
| 178 | TParamList parameters; |
| 179 | TType returnType; |
| 180 | TString mangledName; |
| 181 | TOperator op; |
| 182 | TString extension; |
| 183 | bool defined; |
Alexis Hetu | 407813b | 2016-02-24 16:46:13 -0500 | [diff] [blame] | 184 | bool prototypeDeclaration; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 188 | class TSymbolTableLevel |
| 189 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 190 | public: |
| 191 | typedef TMap<TString, TSymbol*> tLevel; |
| 192 | typedef tLevel::const_iterator const_iterator; |
| 193 | typedef const tLevel::value_type tLevelPair; |
| 194 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 195 | |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 196 | POOL_ALLOCATOR_NEW_DELETE(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 197 | TSymbolTableLevel() { } |
| 198 | ~TSymbolTableLevel(); |
| 199 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 200 | bool insert(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 201 | { |
Alexis Hetu | ad6b875 | 2015-06-09 16:15:30 -0400 | [diff] [blame] | 202 | symbol.setUniqueId(nextUniqueId()); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 203 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 204 | // |
| 205 | // returning true means symbol was added to the table |
| 206 | // |
| 207 | tInsertResult result; |
| 208 | result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); |
| 209 | |
| 210 | return result.second; |
| 211 | } |
| 212 | |
| 213 | TSymbol* find(const TString& name) const |
| 214 | { |
| 215 | tLevel::const_iterator it = level.find(name); |
| 216 | if (it == level.end()) |
| 217 | return 0; |
| 218 | else |
| 219 | return (*it).second; |
| 220 | } |
| 221 | |
Alexis Hetu | ad6b875 | 2015-06-09 16:15:30 -0400 | [diff] [blame] | 222 | static int nextUniqueId() |
| 223 | { |
| 224 | return ++uniqueId; |
| 225 | } |
| 226 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 227 | protected: |
| 228 | tLevel level; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 229 | static int uniqueId; // for unique identification in code generation |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 230 | }; |
| 231 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 232 | enum ESymbolLevel |
| 233 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 234 | COMMON_BUILTINS, |
| 235 | ESSL1_BUILTINS, |
| 236 | ESSL3_BUILTINS, |
| 237 | LAST_BUILTIN_LEVEL = ESSL3_BUILTINS, |
| 238 | GLOBAL_LEVEL |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 239 | }; |
| 240 | |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 241 | inline bool IsGenType(const TType *type) |
| 242 | { |
| 243 | if(type) |
| 244 | { |
| 245 | TBasicType basicType = type->getBasicType(); |
| 246 | return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType; |
| 247 | } |
| 248 | |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | inline bool IsVecType(const TType *type) |
| 253 | { |
| 254 | if(type) |
| 255 | { |
| 256 | TBasicType basicType = type->getBasicType(); |
| 257 | return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec; |
| 258 | } |
| 259 | |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | inline TType *GenType(TType *type, int size) |
| 264 | { |
| 265 | ASSERT(size >= 1 && size <= 4); |
| 266 | |
| 267 | if(!type) |
| 268 | { |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
| 272 | ASSERT(!IsVecType(type)); |
| 273 | |
| 274 | switch(type->getBasicType()) |
| 275 | { |
| 276 | case EbtGenType: return new TType(EbtFloat, size); |
| 277 | case EbtGenIType: return new TType(EbtInt, size); |
| 278 | case EbtGenUType: return new TType(EbtUInt, size); |
| 279 | case EbtGenBType: return new TType(EbtBool, size); |
| 280 | default: return type; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | inline TType *VecType(TType *type, int size) |
| 285 | { |
| 286 | ASSERT(size >= 2 && size <= 4); |
| 287 | |
| 288 | if(!type) |
| 289 | { |
| 290 | return nullptr; |
| 291 | } |
| 292 | |
| 293 | ASSERT(!IsGenType(type)); |
| 294 | |
| 295 | switch(type->getBasicType()) |
| 296 | { |
| 297 | case EbtVec: return new TType(EbtFloat, size); |
| 298 | case EbtIVec: return new TType(EbtInt, size); |
| 299 | case EbtUVec: return new TType(EbtUInt, size); |
| 300 | case EbtBVec: return new TType(EbtBool, size); |
| 301 | default: return type; |
| 302 | } |
| 303 | } |
| 304 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 305 | class TSymbolTable |
| 306 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 307 | public: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 308 | TSymbolTable() |
Alexis Hetu | dd7ff7a | 2015-06-11 08:25:30 -0400 | [diff] [blame] | 309 | : mGlobalInvariant(false) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 310 | { |
| 311 | // |
| 312 | // The symbol table cannot be used until push() is called, but |
| 313 | // the lack of an initial call to push() can be used to detect |
| 314 | // that the symbol table has not been preloaded with built-ins. |
| 315 | // |
| 316 | } |
| 317 | |
| 318 | ~TSymbolTable() |
| 319 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 320 | while(currentLevel() > LAST_BUILTIN_LEVEL) |
| 321 | { |
| 322 | pop(); |
| 323 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 326 | bool isEmpty() { return table.empty(); } |
| 327 | bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; } |
| 328 | bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 329 | void push() |
| 330 | { |
| 331 | table.push_back(new TSymbolTableLevel); |
| 332 | precisionStack.push_back( PrecisionStackLevel() ); |
| 333 | } |
| 334 | |
| 335 | void pop() |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 336 | { |
| 337 | delete table[currentLevel()]; |
| 338 | table.pop_back(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 339 | precisionStack.pop_back(); |
| 340 | } |
| 341 | |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 342 | bool declare(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 343 | { |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 344 | return insert(currentLevel(), symbol); |
| 345 | } |
| 346 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 347 | bool insert(ESymbolLevel level, TSymbol &symbol) |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 348 | { |
| 349 | return table[level]->insert(symbol); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 352 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 353 | { |
Nicolas Capens | 31ad2aa | 2015-02-26 13:14:27 -0500 | [diff] [blame] | 354 | TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1)); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 355 | constant->getConstPointer()->setIConst(value); |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 356 | return insert(level, *constant); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 357 | } |
| 358 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 359 | void insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 360 | { |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 361 | if(ptype1->getBasicType() == EbtGSampler2D) |
| 362 | { |
| 363 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 364 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5); |
| 365 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5); |
| 366 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 367 | } |
| 368 | else if(ptype1->getBasicType() == EbtGSampler3D) |
| 369 | { |
| 370 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 371 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5); |
| 372 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5); |
| 373 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 374 | } |
| 375 | else if(ptype1->getBasicType() == EbtGSamplerCube) |
| 376 | { |
| 377 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 378 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 379 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 380 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 381 | } |
| 382 | else if(ptype1->getBasicType() == EbtGSampler2DArray) |
| 383 | { |
| 384 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 385 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 386 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 387 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 388 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 389 | else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3)) |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 390 | { |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 391 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 392 | insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1)); |
| 393 | insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2)); |
| 394 | insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3)); |
| 395 | insertBuiltIn(level, op, ext, GenType(rvalue, 4), name, GenType(ptype1, 4), GenType(ptype2, 4), GenType(ptype3, 4)); |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 396 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 397 | else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3)) |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 398 | { |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 399 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 400 | insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2)); |
| 401 | insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3)); |
| 402 | insertBuiltIn(level, op, ext, VecType(rvalue, 4), name, VecType(ptype1, 4), VecType(ptype2, 4), VecType(ptype3, 4)); |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 403 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 404 | else |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 405 | { |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 406 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 407 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 408 | TParameter param1 = {0, ptype1}; |
| 409 | function->addParameter(param1); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 410 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 411 | if(ptype2) |
| 412 | { |
| 413 | TParameter param2 = {0, ptype2}; |
| 414 | function->addParameter(param2); |
| 415 | } |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 416 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 417 | if(ptype3) |
| 418 | { |
| 419 | TParameter param3 = {0, ptype3}; |
| 420 | function->addParameter(param3); |
| 421 | } |
| 422 | |
| 423 | if(ptype4) |
| 424 | { |
| 425 | TParameter param4 = {0, ptype4}; |
| 426 | function->addParameter(param4); |
| 427 | } |
| 428 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 429 | if(ptype5) |
| 430 | { |
| 431 | TParameter param5 = {0, ptype5}; |
| 432 | function->addParameter(param5); |
| 433 | } |
| 434 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 435 | insert(level, *function); |
| 436 | } |
| 437 | } |
| 438 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 439 | void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 440 | { |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 441 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 444 | void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0, TType *ptype5 = 0) |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 445 | { |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 446 | insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 449 | TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = nullptr, bool *sameScope = nullptr) const; |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 450 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 451 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 452 | TSymbolTableLevel *getOuterLevel() const |
| 453 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 454 | assert(currentLevel() >= 1); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 455 | return table[currentLevel() - 1]; |
| 456 | } |
| 457 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 458 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 459 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 460 | if (IsSampler(type.type)) |
| 461 | return true; // Skip sampler types for the time being |
| 462 | if (type.type != EbtFloat && type.type != EbtInt) |
| 463 | return false; // Only set default precision for int/float |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 464 | if (type.primarySize > 1 || type.secondarySize > 1 || type.array) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 465 | return false; // Not allowed to set for aggregate types |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 466 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 467 | precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value |
| 468 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | // Searches down the precisionStack for a precision qualifier for the specified TBasicType |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 472 | TPrecision getDefaultPrecision( TBasicType type) |
| 473 | { |
| 474 | // unsigned integers use the same precision as signed |
| 475 | if (type == EbtUInt) type = EbtInt; |
| 476 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 477 | if( type != EbtFloat && type != EbtInt ) return EbpUndefined; |
| 478 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 479 | assert( level >= 0); // Just to be safe. Should not happen. |
| 480 | PrecisionStackLevel::iterator it; |
| 481 | TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this? |
| 482 | while( level >= 0 ){ |
| 483 | it = precisionStack[level].find( type ); |
| 484 | if( it != precisionStack[level].end() ){ |
| 485 | prec = (*it).second; |
| 486 | break; |
| 487 | } |
| 488 | level--; |
| 489 | } |
| 490 | return prec; |
| 491 | } |
| 492 | |
Alexis Hetu | dd7ff7a | 2015-06-11 08:25:30 -0400 | [diff] [blame] | 493 | // This records invariant varyings declared through |
| 494 | // "invariant varying_name;". |
| 495 | void addInvariantVarying(const std::string &originalName) |
| 496 | { |
| 497 | mInvariantVaryings.insert(originalName); |
| 498 | } |
| 499 | // If this returns false, the varying could still be invariant |
| 500 | // if it is set as invariant during the varying variable |
| 501 | // declaration - this piece of information is stored in the |
| 502 | // variable's type, not here. |
| 503 | bool isVaryingInvariant(const std::string &originalName) const |
| 504 | { |
| 505 | return (mGlobalInvariant || |
| 506 | mInvariantVaryings.count(originalName) > 0); |
| 507 | } |
| 508 | |
| 509 | void setGlobalInvariant() { mGlobalInvariant = true; } |
| 510 | bool getGlobalInvariant() const { return mGlobalInvariant; } |
| 511 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 512 | protected: |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 513 | ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 514 | |
| 515 | std::vector<TSymbolTableLevel*> table; |
| 516 | typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; |
| 517 | std::vector< PrecisionStackLevel > precisionStack; |
Alexis Hetu | dd7ff7a | 2015-06-11 08:25:30 -0400 | [diff] [blame] | 518 | |
| 519 | std::set<std::string> mInvariantVaryings; |
| 520 | bool mGlobalInvariant; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | #endif // _SYMBOL_TABLE_INCLUDED_ |