blob: fa6e64d7aa107dd2c7167d5b4eb11a6d1cf629c2 [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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000033#include "compiler/Common.h"
34#include "compiler/intermediate.h"
35#include "compiler/InfoSink.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000036
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037//
38// Symbol base class. (Can build functions or variables out of these...)
39//
40class TSymbol {
41public:
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.org43884872010-03-30 00:08:52 +000052 TSymbol(const TSymbol&);
53 virtual TSymbol* clone(TStructureMap& remapper) = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
55protected:
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//
70class TVariable : public TSymbol {
71public:
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.org6ff56fd2010-05-05 16:37:50 +000084 ConstantUnion* getConstPointer()
alokp@chromium.org43884872010-03-30 00:08:52 +000085 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000086 if (!unionArray)
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000087 unionArray = new ConstantUnion[type.getObjectSize()];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000088
89 return unionArray;
90 }
91
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000092 ConstantUnion* getConstPointer() const { return unionArray; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000094 void shareConstPointer( ConstantUnion *constArray)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 {
96 delete unionArray;
97 unionArray = constArray;
98 }
alokp@chromium.org43884872010-03-30 00:08:52 +000099 TVariable(const TVariable&, TStructureMap& remapper); // copy constructor
100 virtual TVariable* clone(TStructureMap& remapper);
101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102protected:
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.org6ff56fd2010-05-05 16:37:50 +0000107 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108 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//
115struct TParameter {
116 TString *name;
117 TType* type;
alokp@chromium.org43884872010-03-30 00:08:52 +0000118 void copyParam(const TParameter& param, TStructureMap& remapper)
119 {
120 name = NewPoolTString(param.name->c_str());
121 type = param.type->clone(remapper);
122 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123};
124
125//
126// The function sub-class of a symbol.
127//
128class TFunction : public TSymbol {
129public:
130 TFunction(TOperator o) :
131 TSymbol(0),
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000132 returnType(TType(EbtVoid, EbpUndefined)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 op(o),
134 defined(false) { }
135 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) :
136 TSymbol(name),
137 returnType(retType),
alokp@chromium.org43884872010-03-30 00:08:52 +0000138 mangledName(TFunction::mangleName(*name)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 op(tOp),
140 defined(false) { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000141 virtual ~TFunction();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142 virtual bool isFunction() const { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000143
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.com4f39fd92010-03-08 20:26:45 +0000150 void addParameter(TParameter& p)
151 {
152 parameters.push_back(p);
153 mangledName = mangledName + p.type->getMangledName();
154 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000155
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156 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.org43884872010-03-30 00:08:52 +0000164 TParameter& operator [](int i) { return parameters[i]; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165 const TParameter& operator [](int i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000166
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000167 virtual void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000168 TFunction(const TFunction&, TStructureMap& remapper);
169 virtual TFunction* clone(TStructureMap& remapper);
170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171protected:
172 typedef TVector<TParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000173 TParamList parameters;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174 TType returnType;
175 TString mangledName;
176 TOperator op;
177 bool defined;
178};
179
180
181class TSymbolTableLevel {
182public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +0000183 typedef TMap<TString, TSymbol*> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000184 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185 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.org43884872010-03-30 00:08:52 +0000190 ~TSymbolTableLevel();
191
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192 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.org43884872010-03-30 00:08:52 +0000199
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200 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.org43884872010-03-30 00:08:52 +0000212 const_iterator begin() const
213 {
214 return level.begin();
215 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216
alokp@chromium.org43884872010-03-30 00:08:52 +0000217 const_iterator end() const
218 {
219 return level.end();
220 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221
222 void relateToOperator(const char* name, TOperator op);
223 void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000224 TSymbolTableLevel* clone(TStructureMap& remapper);
225
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226protected:
227 tLevel level;
228};
229
230class TSymbolTable {
231public:
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.coma5d76232010-05-17 09:58:47 +0000244 precisionStack.push_back( symTable.precisionStack[0] );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245 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.org43884872010-03-30 00:08:52 +0000264 void push()
265 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266 table.push_back(new TSymbolTableLevel);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000267 precisionStack.push_back( PrecisionStackLevel() );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 }
269
alokp@chromium.org43884872010-03-30 00:08:52 +0000270 void pop()
271 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000272 delete table[currentLevel()];
273 table.pop_back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000274 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275 }
276
277 bool insert(TSymbol& symbol)
278 {
279 symbol.setUniqueId(++uniqueId);
280 return table[currentLevel()]->insert(symbol);
281 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283 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.org43884872010-03-30 00:08:52 +0000303 void copyTable(const TSymbolTable& copyOf);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000305 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.com4f39fd92010-03-08 20:26:45 +0000329protected:
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.coma5d76232010-05-17 09:58:47 +0000334 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
335 std::vector< PrecisionStackLevel > precisionStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 int uniqueId; // for unique identification in code generation
337};
338
339#endif // _SYMBOL_TABLE_INCLUDED_