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 | |
| 33 | #include "Common.h" |
| 34 | #include "intermediate.h" |
| 35 | #include "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 | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 84 | constUnion* getConstPointer() |
| 85 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 86 | if (!unionArray) |
| 87 | unionArray = new constUnion[type.getObjectSize()]; |
| 88 | |
| 89 | return unionArray; |
| 90 | } |
| 91 | |
| 92 | constUnion* getConstPointer() const { return unionArray; } |
| 93 | |
| 94 | void shareConstPointer( constUnion *constArray) |
| 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 |
| 107 | constUnion *unionArray; |
| 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), |
| 132 | returnType(TType(EbtVoid)), |
| 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 | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 183 | typedef std::map<TString, TSymbol*, std::less<TString>, pool_allocator<std::pair<const TString, TSymbol*> > > tLevel; |
| 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]); |
| 244 | uniqueId = symTable.uniqueId; |
| 245 | } |
| 246 | |
| 247 | ~TSymbolTable() |
| 248 | { |
| 249 | // level 0 is always built In symbols, so we never pop that out |
| 250 | while (table.size() > 1) |
| 251 | pop(); |
| 252 | } |
| 253 | |
| 254 | // |
| 255 | // When the symbol table is initialized with the built-ins, there should |
| 256 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 257 | // globals are at level 1. |
| 258 | // |
| 259 | bool isEmpty() { return table.size() == 0; } |
| 260 | bool atBuiltInLevel() { return atSharedBuiltInLevel() || atDynamicBuiltInLevel(); } |
| 261 | bool atSharedBuiltInLevel() { return table.size() == 1; } |
| 262 | bool atGlobalLevel() { return table.size() <= 3; } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 263 | void push() |
| 264 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 265 | table.push_back(new TSymbolTableLevel); |
| 266 | } |
| 267 | |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 268 | void pop() |
| 269 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 270 | delete table[currentLevel()]; |
| 271 | table.pop_back(); |
| 272 | } |
| 273 | |
| 274 | bool insert(TSymbol& symbol) |
| 275 | { |
| 276 | symbol.setUniqueId(++uniqueId); |
| 277 | return table[currentLevel()]->insert(symbol); |
| 278 | } |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 279 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 280 | TSymbol* find(const TString& name, bool* builtIn = 0, bool *sameScope = 0) |
| 281 | { |
| 282 | int level = currentLevel(); |
| 283 | TSymbol* symbol; |
| 284 | do { |
| 285 | symbol = table[level]->find(name); |
| 286 | --level; |
| 287 | } while (symbol == 0 && level >= 0); |
| 288 | level++; |
| 289 | if (builtIn) |
| 290 | *builtIn = level == 0; |
| 291 | if (sameScope) |
| 292 | *sameScope = level == currentLevel(); |
| 293 | return symbol; |
| 294 | } |
| 295 | |
| 296 | TSymbolTableLevel* getGlobalLevel() { assert(table.size() >= 3); return table[2]; } |
| 297 | void relateToOperator(const char* name, TOperator op) { table[0]->relateToOperator(name, op); } |
| 298 | int getMaxSymbolId() { return uniqueId; } |
| 299 | void dump(TInfoSink &infoSink) const; |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame^] | 300 | void copyTable(const TSymbolTable& copyOf); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 301 | |
| 302 | protected: |
| 303 | int currentLevel() const { return static_cast<int>(table.size()) - 1; } |
| 304 | bool atDynamicBuiltInLevel() { return table.size() == 2; } |
| 305 | |
| 306 | std::vector<TSymbolTableLevel*> table; |
| 307 | int uniqueId; // for unique identification in code generation |
| 308 | }; |
| 309 | |
| 310 | #endif // _SYMBOL_TABLE_INCLUDED_ |