blob: 87c6e8aadef00f42c05190cc881474c0cb0cf67d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000033#include <assert.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000034#include "compiler/Common.h"
35#include "compiler/intermediate.h"
36#include "compiler/InfoSink.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000037
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038//
39// Symbol base class. (Can build functions or variables out of these...)
40//
41class TSymbol {
42public:
43 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
44 TSymbol(const TString *n) : name(n) { }
45 virtual ~TSymbol() { /* don't delete name, it's from the pool */ }
46 const TString& getName() const { return *name; }
47 virtual const TString& getMangledName() const { return getName(); }
48 virtual bool isFunction() const { return false; }
49 virtual bool isVariable() const { return false; }
50 void setUniqueId(int id) { uniqueId = id; }
51 int getUniqueId() const { return uniqueId; }
52 virtual void dump(TInfoSink &infoSink) const = 0;
alokp@chromium.org43884872010-03-30 00:08:52 +000053 TSymbol(const TSymbol&);
54 virtual TSymbol* clone(TStructureMap& remapper) = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055
56protected:
57 const TString *name;
58 unsigned int uniqueId; // For real comparing during code generation
59};
60
61//
62// Variable class, meaning a symbol that's not a function.
63//
64// There could be a separate class heirarchy for Constant variables;
65// Only one of int, bool, or float, (or none) is correct for
66// any particular use, but it's easy to do this way, and doesn't
67// seem worth having separate classes, and "getConst" can't simply return
68// different values for different types polymorphically, so this is
69// just simple and pragmatic.
70//
71class TVariable : public TSymbol {
72public:
73 TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0), arrayInformationType(0) { }
74 virtual ~TVariable() { }
75 virtual bool isVariable() const { return true; }
76 TType& getType() { return type; }
77 const TType& getType() const { return type; }
78 bool isUserType() const { return userType; }
79 void changeQualifier(TQualifier qualifier) { type.changeQualifier(qualifier); }
80 void updateArrayInformationType(TType *t) { arrayInformationType = t; }
81 TType* getArrayInformationType() { return arrayInformationType; }
82
83 virtual void dump(TInfoSink &infoSink) const;
84
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000085 ConstantUnion* getConstPointer()
alokp@chromium.org43884872010-03-30 00:08:52 +000086 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 if (!unionArray)
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000088 unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089
90 return unionArray;
91 }
92
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000093 ConstantUnion* getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000095 void shareConstPointer( ConstantUnion *constArray)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000096 {
97 delete unionArray;
98 unionArray = constArray;
99 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000100 TVariable(const TVariable&, TStructureMap& remapper); // copy constructor
101 virtual TVariable* clone(TStructureMap& remapper);
102
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103protected:
104 TType type;
105 bool userType;
106 // we are assuming that Pool Allocator will free the memory allocated to unionArray
107 // when this object is destroyed
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000108 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109 TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol
110};
111
112//
113// The function sub-class of symbols and the parser will need to
114// share this definition of a function parameter.
115//
116struct TParameter {
117 TString *name;
118 TType* type;
alokp@chromium.org43884872010-03-30 00:08:52 +0000119 void copyParam(const TParameter& param, TStructureMap& remapper)
120 {
121 name = NewPoolTString(param.name->c_str());
122 type = param.type->clone(remapper);
123 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124};
125
126//
127// The function sub-class of a symbol.
128//
129class TFunction : public TSymbol {
130public:
131 TFunction(TOperator o) :
132 TSymbol(0),
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000133 returnType(TType(EbtVoid, EbpUndefined)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 op(o),
135 defined(false) { }
136 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) :
137 TSymbol(name),
138 returnType(retType),
alokp@chromium.org43884872010-03-30 00:08:52 +0000139 mangledName(TFunction::mangleName(*name)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 op(tOp),
141 defined(false) { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000142 virtual ~TFunction();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 virtual bool isFunction() const { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000144
145 static TString mangleName(const TString& name) { return name + '('; }
146 static TString unmangleName(const TString& mangledName)
147 {
148 return TString(mangledName.c_str(), mangledName.find_first_of('('));
149 }
150
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151 void addParameter(TParameter& p)
152 {
153 parameters.push_back(p);
154 mangledName = mangledName + p.type->getMangledName();
155 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000156
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157 const TString& getMangledName() const { return mangledName; }
158 const TType& getReturnType() const { return returnType; }
159 void relateToOperator(TOperator o) { op = o; }
160 TOperator getBuiltInOp() const { return op; }
161 void setDefined() { defined = true; }
162 bool isDefined() { return defined; }
163
164 int getParamCount() const { return static_cast<int>(parameters.size()); }
alokp@chromium.org43884872010-03-30 00:08:52 +0000165 TParameter& operator [](int i) { return parameters[i]; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000166 const TParameter& operator [](int i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000167
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168 virtual void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000169 TFunction(const TFunction&, TStructureMap& remapper);
170 virtual TFunction* clone(TStructureMap& remapper);
171
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172protected:
173 typedef TVector<TParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000174 TParamList parameters;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175 TType returnType;
176 TString mangledName;
177 TOperator op;
178 bool defined;
179};
180
181
182class TSymbolTableLevel {
183public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +0000184 typedef TMap<TString, TSymbol*> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000185 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186 typedef const tLevel::value_type tLevelPair;
187 typedef std::pair<tLevel::iterator, bool> tInsertResult;
188
189 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
190 TSymbolTableLevel() { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000191 ~TSymbolTableLevel();
192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 bool insert(TSymbol& symbol)
194 {
195 //
196 // returning true means symbol was added to the table
197 //
198 tInsertResult result;
199 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
alokp@chromium.org43884872010-03-30 00:08:52 +0000200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201 return result.second;
202 }
203
204 TSymbol* find(const TString& name) const
205 {
206 tLevel::const_iterator it = level.find(name);
207 if (it == level.end())
208 return 0;
209 else
210 return (*it).second;
211 }
212
alokp@chromium.org43884872010-03-30 00:08:52 +0000213 const_iterator begin() const
214 {
215 return level.begin();
216 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217
alokp@chromium.org43884872010-03-30 00:08:52 +0000218 const_iterator end() const
219 {
220 return level.end();
221 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222
223 void relateToOperator(const char* name, TOperator op);
224 void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000225 TSymbolTableLevel* clone(TStructureMap& remapper);
226
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227protected:
228 tLevel level;
229};
230
231class TSymbolTable {
232public:
233 TSymbolTable() : uniqueId(0)
234 {
235 //
236 // The symbol table cannot be used until push() is called, but
237 // the lack of an initial call to push() can be used to detect
238 // that the symbol table has not been preloaded with built-ins.
239 //
240 }
241
242 TSymbolTable(TSymbolTable& symTable)
243 {
244 table.push_back(symTable.table[0]);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000245 precisionStack.push_back( symTable.precisionStack[0] );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246 uniqueId = symTable.uniqueId;
247 }
248
249 ~TSymbolTable()
250 {
251 // level 0 is always built In symbols, so we never pop that out
252 while (table.size() > 1)
253 pop();
254 }
255
256 //
257 // When the symbol table is initialized with the built-ins, there should
258 // 'push' calls, so that built-ins are at level 0 and the shader
259 // globals are at level 1.
260 //
261 bool isEmpty() { return table.size() == 0; }
262 bool atBuiltInLevel() { return atSharedBuiltInLevel() || atDynamicBuiltInLevel(); }
263 bool atSharedBuiltInLevel() { return table.size() == 1; }
264 bool atGlobalLevel() { return table.size() <= 3; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000265 void push()
266 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000267 table.push_back(new TSymbolTableLevel);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000268 precisionStack.push_back( PrecisionStackLevel() );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269 }
270
alokp@chromium.org43884872010-03-30 00:08:52 +0000271 void pop()
272 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273 delete table[currentLevel()];
274 table.pop_back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000275 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276 }
277
278 bool insert(TSymbol& symbol)
279 {
280 symbol.setUniqueId(++uniqueId);
281 return table[currentLevel()]->insert(symbol);
282 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284 TSymbol* find(const TString& name, bool* builtIn = 0, bool *sameScope = 0)
285 {
286 int level = currentLevel();
287 TSymbol* symbol;
288 do {
289 symbol = table[level]->find(name);
290 --level;
291 } while (symbol == 0 && level >= 0);
292 level++;
293 if (builtIn)
294 *builtIn = level == 0;
295 if (sameScope)
296 *sameScope = level == currentLevel();
297 return symbol;
298 }
299
300 TSymbolTableLevel* getGlobalLevel() { assert(table.size() >= 3); return table[2]; }
301 void relateToOperator(const char* name, TOperator op) { table[0]->relateToOperator(name, op); }
302 int getMaxSymbolId() { return uniqueId; }
303 void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000304 void copyTable(const TSymbolTable& copyOf);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000306 void setDefaultPrecision( TBasicType type, TPrecision prec ){
307 if( type != EbtFloat && type != EbtInt ) return; // Only set default precision for int/float
308 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
309 precisionStack[indexOfLastElement][type] = prec; // Uses map operator [], overwrites the current value
310 }
311
312 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
313 TPrecision getDefaultPrecision( TBasicType type){
314 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
315 int level = static_cast<int>(precisionStack.size()) - 1;
316 assert( level >= 0); // Just to be safe. Should not happen.
317 PrecisionStackLevel::iterator it;
318 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
319 while( level >= 0 ){
320 it = precisionStack[level].find( type );
321 if( it != precisionStack[level].end() ){
322 prec = (*it).second;
323 break;
324 }
325 level--;
326 }
327 return prec;
328 }
329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330protected:
331 int currentLevel() const { return static_cast<int>(table.size()) - 1; }
332 bool atDynamicBuiltInLevel() { return table.size() == 2; }
333
334 std::vector<TSymbolTableLevel*> table;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000335 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
336 std::vector< PrecisionStackLevel > precisionStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337 int uniqueId; // for unique identification in code generation
338};
339
340#endif // _SYMBOL_TABLE_INCLUDED_