blob: 66e0bb98d5e9a313b0ef7fe863af29bfedf9e7e5 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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>
alokp@chromium.orge4249f02010-07-26 18:13:52 +000034
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000035#include "compiler/InfoSink.h"
alokp@chromium.orge4249f02010-07-26 18:13:52 +000036#include "compiler/intermediate.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; }
alokp@chromium.org58e54292010-08-24 21:40:03 +000079 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080 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 {
alokp@chromium.org6c82caf2010-10-14 16:08:56 +000097 if (unionArray == constArray)
98 return;
99
100 delete[] unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000101 unionArray = constArray;
102 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000103 TVariable(const TVariable&, TStructureMap& remapper); // copy constructor
104 virtual TVariable* clone(TStructureMap& remapper);
105
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106protected:
107 TType type;
108 bool userType;
109 // we are assuming that Pool Allocator will free the memory allocated to unionArray
110 // when this object is destroyed
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000111 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112 TType *arrayInformationType; // this is used for updating maxArraySize in all the references to a given symbol
113};
114
115//
116// The function sub-class of symbols and the parser will need to
117// share this definition of a function parameter.
118//
119struct TParameter {
120 TString *name;
121 TType* type;
alokp@chromium.org43884872010-03-30 00:08:52 +0000122 void copyParam(const TParameter& param, TStructureMap& remapper)
123 {
124 name = NewPoolTString(param.name->c_str());
125 type = param.type->clone(remapper);
126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127};
128
129//
130// The function sub-class of a symbol.
131//
132class TFunction : public TSymbol {
133public:
134 TFunction(TOperator o) :
135 TSymbol(0),
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000136 returnType(TType(EbtVoid, EbpUndefined)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 op(o),
138 defined(false) { }
139 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) :
140 TSymbol(name),
141 returnType(retType),
alokp@chromium.org43884872010-03-30 00:08:52 +0000142 mangledName(TFunction::mangleName(*name)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143 op(tOp),
144 defined(false) { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000145 virtual ~TFunction();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146 virtual bool isFunction() const { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000147
148 static TString mangleName(const TString& name) { return name + '('; }
149 static TString unmangleName(const TString& mangledName)
150 {
151 return TString(mangledName.c_str(), mangledName.find_first_of('('));
152 }
153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 void addParameter(TParameter& p)
155 {
156 parameters.push_back(p);
157 mangledName = mangledName + p.type->getMangledName();
158 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000159
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160 const TString& getMangledName() const { return mangledName; }
161 const TType& getReturnType() const { return returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000162
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000163 void relateToOperator(TOperator o) { op = o; }
164 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000165
166 void relateToExtension(const TString& ext) { extension = ext; }
167 const TString& getExtension() const { return extension; }
168
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 void setDefined() { defined = true; }
170 bool isDefined() { return defined; }
171
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000172 size_t getParamCount() const { return parameters.size(); }
173 const TParameter& getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175 virtual void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000176 TFunction(const TFunction&, TStructureMap& remapper);
177 virtual TFunction* clone(TStructureMap& remapper);
178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179protected:
180 typedef TVector<TParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000181 TParamList parameters;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182 TType returnType;
183 TString mangledName;
184 TOperator op;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000185 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186 bool defined;
187};
188
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000189//
190// Interface block name sub-symbol
191//
192class TInterfaceBlockName : public TSymbol
193{
194public:
195 TInterfaceBlockName(const TString *name)
196 : TSymbol(name)
197 {}
198
199 virtual ~TInterfaceBlockName() {}
200
201 virtual void dump(TInfoSink &infoSink) const;
202 virtual TInterfaceBlockName* clone(TStructureMap& remapper);
203};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204
205class TSymbolTableLevel {
206public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +0000207 typedef TMap<TString, TSymbol*> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000208 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209 typedef const tLevel::value_type tLevelPair;
210 typedef std::pair<tLevel::iterator, bool> tInsertResult;
211
212 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
213 TSymbolTableLevel() { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000214 ~TSymbolTableLevel();
215
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216 bool insert(TSymbol& symbol)
217 {
218 //
219 // returning true means symbol was added to the table
220 //
221 tInsertResult result;
222 result = level.insert(tLevelPair(symbol.getMangledName(), &symbol));
alokp@chromium.org43884872010-03-30 00:08:52 +0000223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224 return result.second;
225 }
226
227 TSymbol* find(const TString& name) const
228 {
229 tLevel::const_iterator it = level.find(name);
230 if (it == level.end())
231 return 0;
232 else
233 return (*it).second;
234 }
235
alokp@chromium.org43884872010-03-30 00:08:52 +0000236 const_iterator begin() const
237 {
238 return level.begin();
239 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
alokp@chromium.org43884872010-03-30 00:08:52 +0000241 const_iterator end() const
242 {
243 return level.end();
244 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245
246 void relateToOperator(const char* name, TOperator op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000247 void relateToExtension(const char* name, const TString& ext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248 void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000249 TSymbolTableLevel* clone(TStructureMap& remapper);
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251protected:
252 tLevel level;
253};
254
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000255enum ESymbolLevel
256{
257 COMMON_BUILTINS = 0,
258 ESSL1_BUILTINS = 1,
259 ESSL3_BUILTINS = 2,
260 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
261 GLOBAL_LEVEL = 3
262};
263
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264class TSymbolTable {
265public:
266 TSymbolTable() : uniqueId(0)
267 {
268 //
269 // The symbol table cannot be used until push() is called, but
270 // the lack of an initial call to push() can be used to detect
271 // that the symbol table has not been preloaded with built-ins.
272 //
273 }
274
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275 ~TSymbolTable()
276 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000277 while (table.size() > 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278 pop();
279 }
280
281 //
282 // When the symbol table is initialized with the built-ins, there should
283 // 'push' calls, so that built-ins are at level 0 and the shader
284 // globals are at level 1.
285 //
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000286 bool isEmpty() { return table.empty(); }
287 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
288 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000289 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000290 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291 table.push_back(new TSymbolTableLevel);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000292 precisionStack.push_back( PrecisionStackLevel() );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293 }
294
alokp@chromium.org43884872010-03-30 00:08:52 +0000295 void pop()
296 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297 delete table[currentLevel()];
298 table.pop_back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000299 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 }
301
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000302 bool declare(TSymbol& symbol)
303 {
304 return insert(currentLevel(), symbol);
305 }
306
307 bool insert(ESymbolLevel level, TSymbol& symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308 {
309 symbol.setUniqueId(++uniqueId);
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000310 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000312
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000313 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false);
314 TSymbol *findBuiltIn(const TString &name, int shaderVersion);
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000315
316 TSymbolTableLevel *getOuterLevel() {
317 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000318 return table[currentLevel() - 1];
319 }
320
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000321 void relateToOperator(ESymbolLevel level, const char* name, TOperator op) {
322 table[level]->relateToOperator(name, op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000323 }
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000324 void relateToExtension(ESymbolLevel level, const char* name, const TString& ext) {
325 table[level]->relateToExtension(name, ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327 int getMaxSymbolId() { return uniqueId; }
328 void dump(TInfoSink &infoSink) const;
alokp@chromium.org43884872010-03-30 00:08:52 +0000329 void copyTable(const TSymbolTable& copyOf);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000331 bool setDefaultPrecision( const TPublicType& type, TPrecision prec ){
332 if (IsSampler(type.type))
333 return true; // Skip sampler types for the time being
334 if (type.type != EbtFloat && type.type != EbtInt)
335 return false; // Only set default precision for int/float
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000336 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000337 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000338 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000339 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
340 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000341 }
342
343 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
344 TPrecision getDefaultPrecision( TBasicType type){
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000345
346 // unsigned integers use the same precision as signed
347 if (type == EbtUInt) type = EbtInt;
348
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000349 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
350 int level = static_cast<int>(precisionStack.size()) - 1;
351 assert( level >= 0); // Just to be safe. Should not happen.
352 PrecisionStackLevel::iterator it;
353 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
354 while( level >= 0 ){
355 it = precisionStack[level].find( type );
356 if( it != precisionStack[level].end() ){
357 prec = (*it).second;
358 break;
359 }
360 level--;
361 }
362 return prec;
363 }
364
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000365protected:
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000366 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367
368 std::vector<TSymbolTableLevel*> table;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000369 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
370 std::vector< PrecisionStackLevel > precisionStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371 int uniqueId; // for unique identification in code generation
372};
373
374#endif // _SYMBOL_TABLE_INCLUDED_