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