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" |
| 36 | |
| 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; |
| 52 | TSymbol(const TSymbol&); |
| 53 | virtual TSymbol* clone(TStructureMap& remapper) = 0; |
| 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 | |
| 84 | constUnion* getConstPointer() { |
| 85 | if (!unionArray) |
| 86 | unionArray = new constUnion[type.getObjectSize()]; |
| 87 | |
| 88 | return unionArray; |
| 89 | } |
| 90 | |
| 91 | constUnion* getConstPointer() const { return unionArray; } |
| 92 | |
| 93 | void shareConstPointer( constUnion *constArray) |
| 94 | { |
| 95 | delete unionArray; |
| 96 | unionArray = constArray; |
| 97 | } |
| 98 | TVariable(const TVariable&, TStructureMap& remapper); // copy constructor |
| 99 | virtual TVariable* clone(TStructureMap& remapper); |
| 100 | |
| 101 | protected: |
| 102 | TType type; |
| 103 | bool userType; |
| 104 | // we are assuming that Pool Allocator will free the memory allocated to unionArray |
| 105 | // when this object is destroyed |
| 106 | constUnion *unionArray; |
| 107 | TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol |
| 108 | }; |
| 109 | |
| 110 | // |
| 111 | // The function sub-class of symbols and the parser will need to |
| 112 | // share this definition of a function parameter. |
| 113 | // |
| 114 | struct TParameter { |
| 115 | TString *name; |
| 116 | TType* type; |
| 117 | void copyParam(const TParameter& param, TStructureMap& remapper) { |
| 118 | name = NewPoolTString(param.name->c_str()); |
| 119 | type = param.type->clone(remapper); |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | // |
| 124 | // The function sub-class of a symbol. |
| 125 | // |
| 126 | class TFunction : public TSymbol { |
| 127 | public: |
| 128 | TFunction(TOperator o) : |
| 129 | TSymbol(0), |
| 130 | returnType(TType(EbtVoid)), |
| 131 | op(o), |
| 132 | defined(false) { } |
| 133 | TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) : |
| 134 | TSymbol(name), |
| 135 | returnType(retType), |
| 136 | mangledName(*name + '('), |
| 137 | op(tOp), |
| 138 | defined(false) { } |
| 139 | virtual ~TFunction(); |
| 140 | virtual bool isFunction() const { return true; } |
| 141 | |
| 142 | void addParameter(TParameter& p) |
| 143 | { |
| 144 | parameters.push_back(p); |
| 145 | mangledName = mangledName + p.type->getMangledName(); |
| 146 | } |
| 147 | |
| 148 | const TString& getMangledName() const { return mangledName; } |
| 149 | const TType& getReturnType() const { return returnType; } |
| 150 | void relateToOperator(TOperator o) { op = o; } |
| 151 | TOperator getBuiltInOp() const { return op; } |
| 152 | void setDefined() { defined = true; } |
| 153 | bool isDefined() { return defined; } |
| 154 | |
| 155 | int getParamCount() const { return static_cast<int>(parameters.size()); } |
| 156 | TParameter& operator [](int i) { return parameters[i]; } |
| 157 | const TParameter& operator [](int i) const { return parameters[i]; } |
| 158 | |
| 159 | virtual void dump(TInfoSink &infoSink) const; |
| 160 | TFunction(const TFunction&, TStructureMap& remapper); |
| 161 | virtual TFunction* clone(TStructureMap& remapper); |
| 162 | |
| 163 | protected: |
| 164 | typedef TVector<TParameter> TParamList; |
| 165 | TParamList parameters; |
| 166 | TType returnType; |
| 167 | TString mangledName; |
| 168 | TOperator op; |
| 169 | bool defined; |
| 170 | }; |
| 171 | |
| 172 | |
| 173 | class TSymbolTableLevel { |
| 174 | public: |
| 175 | typedef std::map<TString, TSymbol*, std::less<TString>, pool_allocator<std::pair<const TString, TSymbol*> > > tLevel; |
| 176 | typedef tLevel::const_iterator const_iterator; |
| 177 | typedef const tLevel::value_type tLevelPair; |
| 178 | typedef std::pair<tLevel::iterator, bool> tInsertResult; |
| 179 | |
| 180 | POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator) |
| 181 | TSymbolTableLevel() { } |
| 182 | ~TSymbolTableLevel(); |
| 183 | |
| 184 | bool insert(TSymbol& symbol) |
| 185 | { |
| 186 | // |
| 187 | // returning true means symbol was added to the table |
| 188 | // |
| 189 | tInsertResult result; |
| 190 | result = level.insert(tLevelPair(symbol.getMangledName(), &symbol)); |
| 191 | |
| 192 | return result.second; |
| 193 | } |
| 194 | |
| 195 | TSymbol* find(const TString& name) const |
| 196 | { |
| 197 | tLevel::const_iterator it = level.find(name); |
| 198 | if (it == level.end()) |
| 199 | return 0; |
| 200 | else |
| 201 | return (*it).second; |
| 202 | } |
| 203 | |
| 204 | const_iterator begin() const |
| 205 | { |
| 206 | return level.begin(); |
| 207 | } |
| 208 | |
| 209 | const_iterator end() const |
| 210 | { |
| 211 | return level.end(); |
| 212 | } |
| 213 | |
| 214 | void relateToOperator(const char* name, TOperator op); |
| 215 | void dump(TInfoSink &infoSink) const; |
| 216 | TSymbolTableLevel* clone(TStructureMap& remapper); |
| 217 | |
| 218 | protected: |
| 219 | tLevel level; |
| 220 | }; |
| 221 | |
| 222 | class TSymbolTable { |
| 223 | public: |
| 224 | TSymbolTable() : uniqueId(0) |
| 225 | { |
| 226 | // |
| 227 | // The symbol table cannot be used until push() is called, but |
| 228 | // the lack of an initial call to push() can be used to detect |
| 229 | // that the symbol table has not been preloaded with built-ins. |
| 230 | // |
| 231 | } |
| 232 | |
| 233 | TSymbolTable(TSymbolTable& symTable) |
| 234 | { |
| 235 | table.push_back(symTable.table[0]); |
| 236 | uniqueId = symTable.uniqueId; |
| 237 | } |
| 238 | |
| 239 | ~TSymbolTable() |
| 240 | { |
| 241 | // level 0 is always built In symbols, so we never pop that out |
| 242 | while (table.size() > 1) |
| 243 | pop(); |
| 244 | } |
| 245 | |
| 246 | // |
| 247 | // When the symbol table is initialized with the built-ins, there should |
| 248 | // 'push' calls, so that built-ins are at level 0 and the shader |
| 249 | // globals are at level 1. |
| 250 | // |
| 251 | bool isEmpty() { return table.size() == 0; } |
| 252 | bool atBuiltInLevel() { return atSharedBuiltInLevel() || atDynamicBuiltInLevel(); } |
| 253 | bool atSharedBuiltInLevel() { return table.size() == 1; } |
| 254 | bool atGlobalLevel() { return table.size() <= 3; } |
| 255 | void push() { |
| 256 | table.push_back(new TSymbolTableLevel); |
| 257 | } |
| 258 | |
| 259 | void pop() { |
| 260 | delete table[currentLevel()]; |
| 261 | table.pop_back(); |
| 262 | } |
| 263 | |
| 264 | bool insert(TSymbol& symbol) |
| 265 | { |
| 266 | symbol.setUniqueId(++uniqueId); |
| 267 | return table[currentLevel()]->insert(symbol); |
| 268 | } |
| 269 | |
| 270 | TSymbol* find(const TString& name, bool* builtIn = 0, bool *sameScope = 0) |
| 271 | { |
| 272 | int level = currentLevel(); |
| 273 | TSymbol* symbol; |
| 274 | do { |
| 275 | symbol = table[level]->find(name); |
| 276 | --level; |
| 277 | } while (symbol == 0 && level >= 0); |
| 278 | level++; |
| 279 | if (builtIn) |
| 280 | *builtIn = level == 0; |
| 281 | if (sameScope) |
| 282 | *sameScope = level == currentLevel(); |
| 283 | return symbol; |
| 284 | } |
| 285 | |
| 286 | TSymbolTableLevel* getGlobalLevel() { assert(table.size() >= 3); return table[2]; } |
| 287 | void relateToOperator(const char* name, TOperator op) { table[0]->relateToOperator(name, op); } |
| 288 | int getMaxSymbolId() { return uniqueId; } |
| 289 | void dump(TInfoSink &infoSink) const; |
| 290 | void copyTable(const TSymbolTable& copyOf); |
| 291 | |
| 292 | protected: |
| 293 | int currentLevel() const { return static_cast<int>(table.size()) - 1; } |
| 294 | bool atDynamicBuiltInLevel() { return table.size() == 2; } |
| 295 | |
| 296 | std::vector<TSymbolTableLevel*> table; |
| 297 | int uniqueId; // for unique identification in code generation |
| 298 | }; |
| 299 | |
| 300 | #endif // _SYMBOL_TABLE_INCLUDED_ |