daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 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 | // |
| 21 | // * Pushing and popping of scope, so symbol table will really be a stack |
| 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 33 | #include "compiler/Common.h" |
| 34 | #include "compiler/intermediate.h" |
| 35 | #include "compiler/InfoSink.h" |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 36 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 37 | // |
| 38 | // Symbol base class. (Can build functions or variables out of these...) |
| 39 | // |
| 40 | class TSymbol { |
| 41 | public: |
| 42 | POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) |
| 43 | TSymbol(const TString *n) : name(n) { } |
| 44 | virtual ~TSymbol() { /* don't delete name, it's from the pool */ } |
| 45 | const TString& getName() const { return *name; } |
| 46 | virtual const TString& getMangledName() const { return getName(); } |
| 47 | virtual bool isFunction() const { return false; } |
| 48 | virtual bool isVariable() const { return false; } |
| 49 | void setUniqueId(int id) { uniqueId = id; } |
| 50 | int getUniqueId() const { return uniqueId; } |
| 51 | virtual void dump(TInfoSink &infoSink) const = 0; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 52 | TSymbol(const TSymbol&); |
| 53 | virtual TSymbol* clone(TStructureMap& remapper) = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 54 | |
| 55 | protected: |
| 56 | const TString *name; |
| 57 | unsigned int uniqueId; // For real comparing during code generation |
| 58 | }; |
| 59 | |
| 60 | // |
| 61 | // Variable class, meaning a symbol that's not a function. |
| 62 | // |
| 63 | // There could be a separate class heirarchy for Constant variables; |
| 64 | // Only one of int, bool, or float, (or none) is correct for |
| 65 | // any particular use, but it's easy to do this way, and doesn't |
| 66 | // seem worth having separate classes, and "getConst" can't simply return |
| 67 | // different values for different types polymorphically, so this is |
| 68 | // just simple and pragmatic. |
| 69 | // |
| 70 | class TVariable : public TSymbol { |
| 71 | public: |
| 72 | TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { } |
| 73 | virtual ~TVariable() { } |
| 74 | virtual bool isVariable() const { return true; } |
| 75 | TType& getType() { return type; } |
| 76 | const TType& getType() const { return type; } |
| 77 | bool isUserType() const { return userType; } |
| 78 | void changeQualifier(TQualifier qualifier) { type.changeQualifier(qualifier); } |
| 79 | void updateArrayInformationType(TType *t) { arrayInformationType = t; } |
| 80 | TType* getArrayInformationType() { return arrayInformationType; } |
| 81 | |
| 82 | virtual void dump(TInfoSink &infoSink) const; |
| 83 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 84 | ConstantUnion* getConstPointer() |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 85 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 86 | if (!unionArray) |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 87 | unionArray = new ConstantUnion[type.getObjectSize()]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | |
| 89 | return unionArray; |
| 90 | } |
| 91 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 92 | ConstantUnion* getConstPointer() const { return unionArray; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 93 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 94 | void shareConstPointer( ConstantUnion *constArray) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | { |
| 96 | delete unionArray; |
| 97 | unionArray = constArray; |
| 98 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 99 | TVariable(const TVariable&, TStructureMap& remapper); // copy constructor |
| 100 | virtual TVariable* clone(TStructureMap& remapper); |
| 101 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | protected: |
| 103 | TType type; |
| 104 | bool userType; |
| 105 | // we are assuming that Pool Allocator will free the memory allocated to unionArray |
| 106 | // when this object is destroyed |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 107 | ConstantUnion *unionArray; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 108 | TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol |
| 109 | }; |
| 110 | |
| 111 | // |
| 112 | // The function sub-class of symbols and the parser will need to |
| 113 | // share this definition of a function parameter. |
| 114 | // |
| 115 | struct TParameter { |
| 116 | TString *name; |
| 117 | TType* type; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 118 | void copyParam(const TParameter& param, TStructureMap& remapper) |
| 119 | { |
| 120 | name = NewPoolTString(param.name->c_str()); |
| 121 | type = param.type->clone(remapper); |
| 122 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | // |
| 126 | // The function sub-class of a symbol. |
| 127 | // |
| 128 | class TFunction : public TSymbol { |
| 129 | public: |
| 130 | TFunction(TOperator o) : |
| 131 | TSymbol(0), |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 132 | returnType(TType(EbtVoid, EbpUndefined)), |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 133 | op(o), |
| 134 | defined(false) { } |
| 135 | TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) : |
| 136 | TSymbol(name), |
| 137 | returnType(retType), |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 138 | mangledName(TFunction::mangleName(*name)), |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 139 | op(tOp), |
| 140 | defined(false) { } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 141 | virtual ~TFunction(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 142 | virtual bool isFunction() const { return true; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 143 | |
| 144 | static TString mangleName(const TString& name) { return name + '('; } |
| 145 | static TString unmangleName(const TString& mangledName) |
| 146 | { |
| 147 | return TString(mangledName.c_str(), mangledName.find_first_of('(')); |
| 148 | } |
| 149 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 150 | void addParameter(TParameter& p) |
| 151 | { |
| 152 | parameters.push_back(p); |
| 153 | mangledName = mangledName + p.type->getMangledName(); |
| 154 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 155 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 156 | const TString& getMangledName() const { return mangledName; } |
| 157 | const TType& getReturnType() const { return returnType; } |
| 158 | void relateToOperator(TOperator o) { op = o; } |
| 159 | TOperator getBuiltInOp() const { return op; } |
| 160 | void setDefined() { defined = true; } |
| 161 | bool isDefined() { return defined; } |
| 162 | |
| 163 | int getParamCount() const { return static_cast<int>(parameters.size()); } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 164 | TParameter& operator [](int i) { return parameters[i]; } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | const TParameter& operator [](int i) const { return parameters[i]; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 166 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 167 | virtual void dump(TInfoSink &infoSink) const; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 168 | TFunction(const TFunction&, TStructureMap& remapper); |
| 169 | virtual TFunction* clone(TStructureMap& remapper); |
| 170 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 171 | protected: |
| 172 | typedef TVector<TParameter> TParamList; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 173 | TParamList parameters; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 174 | TType returnType; |
| 175 | TString mangledName; |
| 176 | TOperator op; |
| 177 | bool defined; |
| 178 | }; |
| 179 | |
| 180 | |
| 181 | class TSymbolTableLevel { |
| 182 | public: |
alokp@chromium.org | 91a01a1 | 2010-05-12 18:39:04 +0000 | [diff] [blame] | 183 | typedef TMap<TString, TSymbol*> tLevel; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 184 | typedef tLevel::const_iterator const_iterator; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 185 | typedef const tLevel::value_type tLevelPair; |
| 186 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 187 | |
| 188 | POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) |
| 189 | TSymbolTableLevel() { } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 190 | ~TSymbolTableLevel(); |
| 191 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 192 | bool insert(TSymbol& symbol) |
| 193 | { |
| 194 | // |
| 195 | // returning true means symbol was added to the table |
| 196 | // |
| 197 | tInsertResult result; |
| 198 | result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 199 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 212 | const_iterator begin() const |
| 213 | { |
| 214 | return level.begin(); |
| 215 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 216 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 217 | const_iterator end() const |
| 218 | { |
| 219 | return level.end(); |
| 220 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 221 | |
| 222 | void relateToOperator(const char* name, TOperator op); |
| 223 | void dump(TInfoSink &infoSink) const; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 224 | TSymbolTableLevel* clone(TStructureMap& remapper); |
| 225 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 226 | protected: |
| 227 | tLevel level; |
| 228 | }; |
| 229 | |
| 230 | class TSymbolTable { |
| 231 | public: |
| 232 | TSymbolTable() : uniqueId(0) |
| 233 | { |
| 234 | // |
| 235 | // The symbol table cannot be used until push() is called, but |
| 236 | // the lack of an initial call to push() can be used to detect |
| 237 | // that the symbol table has not been preloaded with built-ins. |
| 238 | // |
| 239 | } |
| 240 | |
| 241 | TSymbolTable(TSymbolTable& symTable) |
| 242 | { |
| 243 | table.push_back(symTable.table[0]); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 244 | precisionStack.push_back( symTable.precisionStack[0] ); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 245 | uniqueId = symTable.uniqueId; |
| 246 | } |
| 247 | |
| 248 | ~TSymbolTable() |
| 249 | { |
| 250 | // level 0 is always built In symbols, so we never pop that out |
| 251 | while (table.size() > 1) |
| 252 | pop(); |
| 253 | } |
| 254 | |
| 255 | // |
| 256 | // When the symbol table is initialized with the built-ins, there should |
| 257 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 258 | // globals are at level 1. |
| 259 | // |
| 260 | bool isEmpty() { return table.size() == 0; } |
| 261 | bool atBuiltInLevel() { return atSharedBuiltInLevel() || atDynamicBuiltInLevel(); } |
| 262 | bool atSharedBuiltInLevel() { return table.size() == 1; } |
| 263 | bool atGlobalLevel() { return table.size() <= 3; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 264 | void push() |
| 265 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 266 | table.push_back(new TSymbolTableLevel); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 267 | precisionStack.push_back( PrecisionStackLevel() ); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 268 | } |
| 269 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 270 | void pop() |
| 271 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 272 | delete table[currentLevel()]; |
| 273 | table.pop_back(); |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 274 | precisionStack.pop_back(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | bool insert(TSymbol& symbol) |
| 278 | { |
| 279 | symbol.setUniqueId(++uniqueId); |
| 280 | return table[currentLevel()]->insert(symbol); |
| 281 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 282 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 283 | TSymbol* find(const TString& name, bool* builtIn = 0, bool *sameScope = 0) |
| 284 | { |
| 285 | int level = currentLevel(); |
| 286 | TSymbol* symbol; |
| 287 | do { |
| 288 | symbol = table[level]->find(name); |
| 289 | --level; |
| 290 | } while (symbol == 0 && level >= 0); |
| 291 | level++; |
| 292 | if (builtIn) |
| 293 | *builtIn = level == 0; |
| 294 | if (sameScope) |
| 295 | *sameScope = level == currentLevel(); |
| 296 | return symbol; |
| 297 | } |
| 298 | |
| 299 | TSymbolTableLevel* getGlobalLevel() { assert(table.size() >= 3); return table[2]; } |
| 300 | void relateToOperator(const char* name, TOperator op) { table[0]->relateToOperator(name, op); } |
| 301 | int getMaxSymbolId() { return uniqueId; } |
| 302 | void dump(TInfoSink &infoSink) const; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 303 | void copyTable(const TSymbolTable& copyOf); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 304 | |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 305 | void setDefaultPrecision( TBasicType type, TPrecision prec ){ |
| 306 | if( type != EbtFloat && type != EbtInt ) return; // Only set default precision for int/float |
| 307 | int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1; |
| 308 | precisionStack[indexOfLastElement][type] = prec; // Uses map operator [], overwrites the current value |
| 309 | } |
| 310 | |
| 311 | // Searches down the precisionStack for a precision qualifier for the specified TBasicType |
| 312 | TPrecision getDefaultPrecision( TBasicType type){ |
| 313 | if( type != EbtFloat && type != EbtInt ) return EbpUndefined; |
| 314 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 315 | assert( level >= 0); // Just to be safe. Should not happen. |
| 316 | PrecisionStackLevel::iterator it; |
| 317 | TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this? |
| 318 | while( level >= 0 ){ |
| 319 | it = precisionStack[level].find( type ); |
| 320 | if( it != precisionStack[level].end() ){ |
| 321 | prec = (*it).second; |
| 322 | break; |
| 323 | } |
| 324 | level--; |
| 325 | } |
| 326 | return prec; |
| 327 | } |
| 328 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 329 | protected: |
| 330 | int currentLevel() const { return static_cast<int>(table.size()) - 1; } |
| 331 | bool atDynamicBuiltInLevel() { return table.size() == 2; } |
| 332 | |
| 333 | std::vector<TSymbolTableLevel*> table; |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame^] | 334 | typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; |
| 335 | std::vector< PrecisionStackLevel > precisionStack; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 336 | int uniqueId; // for unique identification in code generation |
| 337 | }; |
| 338 | |
| 339 | #endif // _SYMBOL_TABLE_INCLUDED_ |