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