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 | |
| 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. |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 63 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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 |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 68 | // different values for different types polymorphically, so this is |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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() { } |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 76 | virtual bool isVariable() const { return true; } |
| 77 | TType& getType() { return type; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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() |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 85 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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; |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 100 | unionArray = constArray; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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 | // |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 123 | // The function sub-class of a symbol. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 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) { } |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 133 | TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull, const char *ext = "") : |
| 134 | TSymbol(name), |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 135 | returnType(retType), |
| 136 | mangledName(TFunction::mangleName(*name)), |
| 137 | op(tOp), |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 138 | extension(ext), |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 139 | defined(false) { } |
| 140 | virtual ~TFunction(); |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 141 | virtual bool isFunction() const { return true; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 142 | |
| 143 | static TString mangleName(const TString& name) { return name + '('; } |
| 144 | static TString unmangleName(const TString& mangledName) |
| 145 | { |
| 146 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 147 | } |
| 148 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 149 | void addParameter(TParameter& p) |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 150 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 151 | parameters.push_back(p); |
| 152 | mangledName = mangledName + p.type->getMangledName(); |
| 153 | } |
| 154 | |
| 155 | const TString& getMangledName() const { return mangledName; } |
| 156 | const TType& getReturnType() const { return returnType; } |
| 157 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 158 | TOperator getBuiltInOp() const { return op; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 159 | const TString& getExtension() const { return extension; } |
| 160 | |
| 161 | void setDefined() { defined = true; } |
| 162 | bool isDefined() { return defined; } |
| 163 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 164 | int getParamCount() const { return static_cast<int>(parameters.size()); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 165 | const TParameter& getParam(int i) const { return parameters[i]; } |
| 166 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 167 | protected: |
| 168 | typedef TVector<TParameter> TParamList; |
| 169 | TParamList parameters; |
| 170 | TType returnType; |
| 171 | TString mangledName; |
| 172 | TOperator op; |
| 173 | TString extension; |
| 174 | bool defined; |
| 175 | }; |
| 176 | |
| 177 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 178 | class TSymbolTableLevel |
| 179 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 180 | public: |
| 181 | typedef TMap<TString, TSymbol*> tLevel; |
| 182 | typedef tLevel::const_iterator const_iterator; |
| 183 | typedef const tLevel::value_type tLevelPair; |
| 184 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 185 | |
Nicolas Capens | 978ddc5 | 2014-11-11 12:42:08 -0500 | [diff] [blame] | 186 | POOL_ALLOCATOR_NEW_DELETE(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 187 | TSymbolTableLevel() { } |
| 188 | ~TSymbolTableLevel(); |
| 189 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 190 | bool insert(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 191 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 192 | symbol.setUniqueId(++uniqueId); |
| 193 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 194 | // |
| 195 | // returning true means symbol was added to the table |
| 196 | // |
| 197 | tInsertResult result; |
| 198 | result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); |
| 199 | |
| 200 | return result.second; |
| 201 | } |
| 202 | |
| 203 | TSymbol* find(const TString& name) const |
| 204 | { |
| 205 | tLevel::const_iterator it = level.find(name); |
| 206 | if (it == level.end()) |
| 207 | return 0; |
| 208 | else |
| 209 | return (*it).second; |
| 210 | } |
| 211 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 212 | protected: |
| 213 | tLevel level; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 214 | static int uniqueId; // for unique identification in code generation |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 215 | }; |
| 216 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 217 | enum ESymbolLevel |
| 218 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 219 | COMMON_BUILTINS, |
| 220 | ESSL1_BUILTINS, |
| 221 | ESSL3_BUILTINS, |
| 222 | LAST_BUILTIN_LEVEL = ESSL3_BUILTINS, |
| 223 | GLOBAL_LEVEL |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 224 | }; |
| 225 | |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 226 | inline bool IsGenType(const TType *type) |
| 227 | { |
| 228 | if(type) |
| 229 | { |
| 230 | TBasicType basicType = type->getBasicType(); |
| 231 | return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType; |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | inline bool IsVecType(const TType *type) |
| 238 | { |
| 239 | if(type) |
| 240 | { |
| 241 | TBasicType basicType = type->getBasicType(); |
| 242 | return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | inline TType *GenType(TType *type, int size) |
| 249 | { |
| 250 | ASSERT(size >= 1 && size <= 4); |
| 251 | |
| 252 | if(!type) |
| 253 | { |
| 254 | return nullptr; |
| 255 | } |
| 256 | |
| 257 | ASSERT(!IsVecType(type)); |
| 258 | |
| 259 | switch(type->getBasicType()) |
| 260 | { |
| 261 | case EbtGenType: return new TType(EbtFloat, size); |
| 262 | case EbtGenIType: return new TType(EbtInt, size); |
| 263 | case EbtGenUType: return new TType(EbtUInt, size); |
| 264 | case EbtGenBType: return new TType(EbtBool, size); |
| 265 | default: return type; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | inline TType *VecType(TType *type, int size) |
| 270 | { |
| 271 | ASSERT(size >= 2 && size <= 4); |
| 272 | |
| 273 | if(!type) |
| 274 | { |
| 275 | return nullptr; |
| 276 | } |
| 277 | |
| 278 | ASSERT(!IsGenType(type)); |
| 279 | |
| 280 | switch(type->getBasicType()) |
| 281 | { |
| 282 | case EbtVec: return new TType(EbtFloat, size); |
| 283 | case EbtIVec: return new TType(EbtInt, size); |
| 284 | case EbtUVec: return new TType(EbtUInt, size); |
| 285 | case EbtBVec: return new TType(EbtBool, size); |
| 286 | default: return type; |
| 287 | } |
| 288 | } |
| 289 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 290 | class TSymbolTable |
| 291 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 292 | public: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 293 | TSymbolTable() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 294 | { |
| 295 | // |
| 296 | // The symbol table cannot be used until push() is called, but |
| 297 | // the lack of an initial call to push() can be used to detect |
| 298 | // that the symbol table has not been preloaded with built-ins. |
| 299 | // |
| 300 | } |
| 301 | |
| 302 | ~TSymbolTable() |
| 303 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 304 | while(currentLevel() > LAST_BUILTIN_LEVEL) |
| 305 | { |
| 306 | pop(); |
| 307 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 310 | bool isEmpty() { return table.empty(); } |
| 311 | bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; } |
| 312 | bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 313 | void push() |
| 314 | { |
| 315 | table.push_back(new TSymbolTableLevel); |
| 316 | precisionStack.push_back( PrecisionStackLevel() ); |
| 317 | } |
| 318 | |
| 319 | void pop() |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 320 | { |
| 321 | delete table[currentLevel()]; |
| 322 | table.pop_back(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 323 | precisionStack.pop_back(); |
| 324 | } |
| 325 | |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 326 | bool declare(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 327 | { |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 328 | return insert(currentLevel(), symbol); |
| 329 | } |
| 330 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 331 | bool insert(ESymbolLevel level, TSymbol &symbol) |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 332 | { |
| 333 | return table[level]->insert(symbol); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 336 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 337 | { |
Nicolas Capens | 31ad2aa | 2015-02-26 13:14:27 -0500 | [diff] [blame] | 338 | TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1)); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 339 | constant->getConstPointer()->setIConst(value); |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 340 | return insert(level, *constant); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 343 | 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) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 344 | { |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 345 | if(ptype1->getBasicType() == EbtGSampler2D) |
| 346 | { |
| 347 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
| 348 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4); |
| 349 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4); |
| 350 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 351 | } |
| 352 | else if(ptype1->getBasicType() == EbtGSampler3D) |
| 353 | { |
| 354 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
| 355 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4); |
| 356 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4); |
| 357 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 358 | } |
| 359 | else if(ptype1->getBasicType() == EbtGSamplerCube) |
| 360 | { |
| 361 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
| 362 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4); |
| 363 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4); |
| 364 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 365 | } |
| 366 | else if(ptype1->getBasicType() == EbtGSampler2DArray) |
| 367 | { |
| 368 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
| 369 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4); |
| 370 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4); |
| 371 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4); |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 372 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 373 | else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3)) |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 374 | { |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 375 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 376 | insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1)); |
| 377 | insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2)); |
| 378 | insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3)); |
| 379 | 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] | 380 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 381 | else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3)) |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 382 | { |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 383 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 384 | insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2)); |
| 385 | insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3)); |
| 386 | 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] | 387 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 388 | else |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 389 | { |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 390 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 391 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 392 | TParameter param1 = {0, ptype1}; |
| 393 | function->addParameter(param1); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 394 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 395 | if(ptype2) |
| 396 | { |
| 397 | TParameter param2 = {0, ptype2}; |
| 398 | function->addParameter(param2); |
| 399 | } |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 400 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 401 | if(ptype3) |
| 402 | { |
| 403 | TParameter param3 = {0, ptype3}; |
| 404 | function->addParameter(param3); |
| 405 | } |
| 406 | |
| 407 | if(ptype4) |
| 408 | { |
| 409 | TParameter param4 = {0, ptype4}; |
| 410 | function->addParameter(param4); |
| 411 | } |
| 412 | |
| 413 | insert(level, *function); |
| 414 | } |
| 415 | } |
| 416 | |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 417 | void insertBuiltIn(ESymbolLevel level, TOperator op, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0) |
| 418 | { |
| 419 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4); |
| 420 | } |
| 421 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 422 | void insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0, TType *ptype4 = 0) |
| 423 | { |
| 424 | insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 427 | 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] | 428 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 429 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 430 | TSymbolTableLevel *getOuterLevel() const |
| 431 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 432 | assert(currentLevel() >= 1); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 433 | return table[currentLevel() - 1]; |
| 434 | } |
| 435 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 436 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 437 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 438 | if (IsSampler(type.type)) |
| 439 | return true; // Skip sampler types for the time being |
| 440 | if (type.type != EbtFloat && type.type != EbtInt) |
| 441 | return false; // Only set default precision for int/float |
| 442 | if (type.size != 1 || type.matrix || type.array) |
| 443 | return false; // Not allowed to set for aggregate types |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 444 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 445 | precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value |
| 446 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | // Searches down the precisionStack for a precision qualifier for the specified TBasicType |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 450 | TPrecision getDefaultPrecision( TBasicType type) |
| 451 | { |
| 452 | // unsigned integers use the same precision as signed |
| 453 | if (type == EbtUInt) type = EbtInt; |
| 454 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 455 | if( type != EbtFloat && type != EbtInt ) return EbpUndefined; |
| 456 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 457 | assert( level >= 0); // Just to be safe. Should not happen. |
| 458 | PrecisionStackLevel::iterator it; |
| 459 | TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this? |
| 460 | while( level >= 0 ){ |
| 461 | it = precisionStack[level].find( type ); |
| 462 | if( it != precisionStack[level].end() ){ |
| 463 | prec = (*it).second; |
| 464 | break; |
| 465 | } |
| 466 | level--; |
| 467 | } |
| 468 | return prec; |
| 469 | } |
| 470 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 471 | protected: |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 472 | ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 473 | |
| 474 | std::vector<TSymbolTableLevel*> table; |
| 475 | typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; |
| 476 | std::vector< PrecisionStackLevel > precisionStack; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 477 | }; |
| 478 | |
| 479 | #endif // _SYMBOL_TABLE_INCLUDED_ |