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 | { |
Alexis Hetu | ad6b875 | 2015-06-09 16:15:30 -0400 | [diff] [blame^] | 196 | symbol.setUniqueId(nextUniqueId()); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 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 | |
Alexis Hetu | ad6b875 | 2015-06-09 16:15:30 -0400 | [diff] [blame^] | 216 | static int nextUniqueId() |
| 217 | { |
| 218 | return ++uniqueId; |
| 219 | } |
| 220 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 221 | protected: |
| 222 | tLevel level; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 223 | static int uniqueId; // for unique identification in code generation |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 224 | }; |
| 225 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 226 | enum ESymbolLevel |
| 227 | { |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 228 | COMMON_BUILTINS, |
| 229 | ESSL1_BUILTINS, |
| 230 | ESSL3_BUILTINS, |
| 231 | LAST_BUILTIN_LEVEL = ESSL3_BUILTINS, |
| 232 | GLOBAL_LEVEL |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 233 | }; |
| 234 | |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 235 | inline bool IsGenType(const TType *type) |
| 236 | { |
| 237 | if(type) |
| 238 | { |
| 239 | TBasicType basicType = type->getBasicType(); |
| 240 | return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType; |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | inline bool IsVecType(const TType *type) |
| 247 | { |
| 248 | if(type) |
| 249 | { |
| 250 | TBasicType basicType = type->getBasicType(); |
| 251 | return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | inline TType *GenType(TType *type, int size) |
| 258 | { |
| 259 | ASSERT(size >= 1 && size <= 4); |
| 260 | |
| 261 | if(!type) |
| 262 | { |
| 263 | return nullptr; |
| 264 | } |
| 265 | |
| 266 | ASSERT(!IsVecType(type)); |
| 267 | |
| 268 | switch(type->getBasicType()) |
| 269 | { |
| 270 | case EbtGenType: return new TType(EbtFloat, size); |
| 271 | case EbtGenIType: return new TType(EbtInt, size); |
| 272 | case EbtGenUType: return new TType(EbtUInt, size); |
| 273 | case EbtGenBType: return new TType(EbtBool, size); |
| 274 | default: return type; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | inline TType *VecType(TType *type, int size) |
| 279 | { |
| 280 | ASSERT(size >= 2 && size <= 4); |
| 281 | |
| 282 | if(!type) |
| 283 | { |
| 284 | return nullptr; |
| 285 | } |
| 286 | |
| 287 | ASSERT(!IsGenType(type)); |
| 288 | |
| 289 | switch(type->getBasicType()) |
| 290 | { |
| 291 | case EbtVec: return new TType(EbtFloat, size); |
| 292 | case EbtIVec: return new TType(EbtInt, size); |
| 293 | case EbtUVec: return new TType(EbtUInt, size); |
| 294 | case EbtBVec: return new TType(EbtBool, size); |
| 295 | default: return type; |
| 296 | } |
| 297 | } |
| 298 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 299 | class TSymbolTable |
| 300 | { |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 301 | public: |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 302 | TSymbolTable() |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 303 | { |
| 304 | // |
| 305 | // The symbol table cannot be used until push() is called, but |
| 306 | // the lack of an initial call to push() can be used to detect |
| 307 | // that the symbol table has not been preloaded with built-ins. |
| 308 | // |
| 309 | } |
| 310 | |
| 311 | ~TSymbolTable() |
| 312 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 313 | while(currentLevel() > LAST_BUILTIN_LEVEL) |
| 314 | { |
| 315 | pop(); |
| 316 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 319 | bool isEmpty() { return table.empty(); } |
| 320 | bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; } |
| 321 | bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 322 | void push() |
| 323 | { |
| 324 | table.push_back(new TSymbolTableLevel); |
| 325 | precisionStack.push_back( PrecisionStackLevel() ); |
| 326 | } |
| 327 | |
| 328 | void pop() |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 329 | { |
| 330 | delete table[currentLevel()]; |
| 331 | table.pop_back(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 332 | precisionStack.pop_back(); |
| 333 | } |
| 334 | |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 335 | bool declare(TSymbol &symbol) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 336 | { |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 337 | return insert(currentLevel(), symbol); |
| 338 | } |
| 339 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 340 | bool insert(ESymbolLevel level, TSymbol &symbol) |
Nicolas Capens | d603ecd | 2015-02-18 14:52:21 -0500 | [diff] [blame] | 341 | { |
| 342 | return table[level]->insert(symbol); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 345 | bool insertConstInt(ESymbolLevel level, const char *name, int value) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 346 | { |
Nicolas Capens | 31ad2aa | 2015-02-26 13:14:27 -0500 | [diff] [blame] | 347 | TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConstExpr, 1)); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 348 | constant->getConstPointer()->setIConst(value); |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 349 | return insert(level, *constant); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 352 | 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] | 353 | { |
Nicolas Capens | f8a4cba | 2015-02-18 17:30:46 -0500 | [diff] [blame] | 354 | if(ptype1->getBasicType() == EbtGSampler2D) |
| 355 | { |
| 356 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 357 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5); |
| 358 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5); |
| 359 | 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] | 360 | } |
| 361 | else if(ptype1->getBasicType() == EbtGSampler3D) |
| 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(EbtSampler3D), ptype2, ptype3, ptype4, ptype5); |
| 365 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5); |
| 366 | 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] | 367 | } |
| 368 | else if(ptype1->getBasicType() == EbtGSamplerCube) |
| 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(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 372 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 373 | 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] | 374 | } |
| 375 | else if(ptype1->getBasicType() == EbtGSampler2DArray) |
| 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(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 379 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 380 | 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] | 381 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 382 | else if(IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3)) |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 383 | { |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 384 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 385 | insertBuiltIn(level, op, ext, GenType(rvalue, 1), name, GenType(ptype1, 1), GenType(ptype2, 1), GenType(ptype3, 1)); |
| 386 | insertBuiltIn(level, op, ext, GenType(rvalue, 2), name, GenType(ptype1, 2), GenType(ptype2, 2), GenType(ptype3, 2)); |
| 387 | insertBuiltIn(level, op, ext, GenType(rvalue, 3), name, GenType(ptype1, 3), GenType(ptype2, 3), GenType(ptype3, 3)); |
| 388 | 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] | 389 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 390 | else if(IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3)) |
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 | ASSERT(!ptype4); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 393 | insertBuiltIn(level, op, ext, VecType(rvalue, 2), name, VecType(ptype1, 2), VecType(ptype2, 2), VecType(ptype3, 2)); |
| 394 | insertBuiltIn(level, op, ext, VecType(rvalue, 3), name, VecType(ptype1, 3), VecType(ptype2, 3), VecType(ptype3, 3)); |
| 395 | 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] | 396 | } |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 397 | else |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 398 | { |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 399 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue, op, ext); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 400 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 401 | TParameter param1 = {0, ptype1}; |
| 402 | function->addParameter(param1); |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 403 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 404 | if(ptype2) |
| 405 | { |
| 406 | TParameter param2 = {0, ptype2}; |
| 407 | function->addParameter(param2); |
| 408 | } |
Nicolas Capens | 8fa9d23 | 2015-02-19 15:53:16 -0500 | [diff] [blame] | 409 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 410 | if(ptype3) |
| 411 | { |
| 412 | TParameter param3 = {0, ptype3}; |
| 413 | function->addParameter(param3); |
| 414 | } |
| 415 | |
| 416 | if(ptype4) |
| 417 | { |
| 418 | TParameter param4 = {0, ptype4}; |
| 419 | function->addParameter(param4); |
| 420 | } |
| 421 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 422 | if(ptype5) |
| 423 | { |
| 424 | TParameter param5 = {0, ptype5}; |
| 425 | function->addParameter(param5); |
| 426 | } |
| 427 | |
Nicolas Capens | 33cda11 | 2015-02-24 17:35:47 -0500 | [diff] [blame] | 428 | insert(level, *function); |
| 429 | } |
| 430 | } |
| 431 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 432 | 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] | 433 | { |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 434 | insertBuiltIn(level, op, "", rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
Nicolas Capens | 18d5ef9 | 2015-02-24 17:38:08 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 437 | 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] | 438 | { |
Alexis Hetu | b5332c5 | 2015-06-04 13:04:17 -0400 | [diff] [blame] | 439 | insertBuiltIn(level, EOpNull, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Nicolas Capens | cc1e996 | 2015-03-02 11:17:58 -0500 | [diff] [blame] | 442 | 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] | 443 | TSymbol *findBuiltIn(const TString &name, int shaderVersion) const; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 444 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 445 | TSymbolTableLevel *getOuterLevel() const |
| 446 | { |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 447 | assert(currentLevel() >= 1); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 448 | return table[currentLevel() - 1]; |
| 449 | } |
| 450 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 451 | bool setDefaultPrecision(const TPublicType &type, TPrecision prec) |
| 452 | { |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 453 | if (IsSampler(type.type)) |
| 454 | return true; // Skip sampler types for the time being |
| 455 | if (type.type != EbtFloat && type.type != EbtInt) |
| 456 | return false; // Only set default precision for int/float |
Alexis Hetu | b14178b | 2015-04-13 13:23:20 -0400 | [diff] [blame] | 457 | if (type.primarySize > 1 || type.secondarySize > 1 || type.array) |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 458 | return false; // Not allowed to set for aggregate types |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 459 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 460 | precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value |
| 461 | return true; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // Searches down the precisionStack for a precision qualifier for the specified TBasicType |
Nicolas Capens | 3c20f80 | 2015-02-17 17:17:20 -0500 | [diff] [blame] | 465 | TPrecision getDefaultPrecision( TBasicType type) |
| 466 | { |
| 467 | // unsigned integers use the same precision as signed |
| 468 | if (type == EbtUInt) type = EbtInt; |
| 469 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 470 | if( type != EbtFloat && type != EbtInt ) return EbpUndefined; |
| 471 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 472 | assert( level >= 0); // Just to be safe. Should not happen. |
| 473 | PrecisionStackLevel::iterator it; |
| 474 | TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this? |
| 475 | while( level >= 0 ){ |
| 476 | it = precisionStack[level].find( type ); |
| 477 | if( it != precisionStack[level].end() ){ |
| 478 | prec = (*it).second; |
| 479 | break; |
| 480 | } |
| 481 | level--; |
| 482 | } |
| 483 | return prec; |
| 484 | } |
| 485 | |
Nicolas Capens | 0a7f0c2 | 2015-02-18 14:47:31 -0500 | [diff] [blame] | 486 | protected: |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 487 | ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 488 | |
| 489 | std::vector<TSymbolTableLevel*> table; |
| 490 | typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; |
| 491 | std::vector< PrecisionStackLevel > precisionStack; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | #endif // _SYMBOL_TABLE_INCLUDED_ |