blob: 9e9beb5bc162aac1302fe1edb0036d7d45df663c [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
Jamie Madill703cdd62013-07-08 15:07:30 -040035#include "common/angleutils.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000036#include "compiler/InfoSink.h"
alokp@chromium.orge4249f02010-07-26 18:13:52 +000037#include "compiler/intermediate.h"
alokp@chromium.org43884872010-03-30 00:08:52 +000038
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039//
40// Symbol base class. (Can build functions or variables out of these...)
41//
42class TSymbol {
43public:
44 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
45 TSymbol(const TString *n) : name(n) { }
46 virtual ~TSymbol() { /* don't delete name, it's from the pool */ }
47 const TString& getName() const { return *name; }
48 virtual const TString& getMangledName() const { return getName(); }
49 virtual bool isFunction() const { return false; }
50 virtual bool isVariable() const { return false; }
51 void setUniqueId(int id) { uniqueId = id; }
52 int getUniqueId() const { return uniqueId; }
Jamie Madill2aeb26a2013-07-08 14:02:55 -040053 void relateToExtension(const TString& ext) { extension = ext; }
54 const TString& getExtension() const { return extension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040055
Jamie Madill703cdd62013-07-08 15:07:30 -040056private:
57 DISALLOW_COPY_AND_ASSIGN(TSymbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059 const TString *name;
60 unsigned int uniqueId; // For real comparing during code generation
Jamie Madill2aeb26a2013-07-08 14:02:55 -040061 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000062};
63
64//
65// Variable class, meaning a symbol that's not a function.
66//
67// There could be a separate class heirarchy for Constant variables;
68// Only one of int, bool, or float, (or none) is correct for
69// any particular use, but it's easy to do this way, and doesn't
70// seem worth having separate classes, and "getConst" can't simply return
71// different values for different types polymorphically, so this is
72// just simple and pragmatic.
73//
74class TVariable : public TSymbol {
75public:
Jamie Madill18464b52013-07-08 14:01:55 -040076 TVariable(const TString *name, const TType& t, bool uT = false ) : TSymbol(name), type(t), userType(uT), unionArray(0) { }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077 virtual ~TVariable() { }
78 virtual bool isVariable() const { return true; }
79 TType& getType() { return type; }
80 const TType& getType() const { return type; }
81 bool isUserType() const { return userType; }
alokp@chromium.org58e54292010-08-24 21:40:03 +000082 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
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 {
alokp@chromium.org6c82caf2010-10-14 16:08:56 +000096 if (unionArray == constArray)
97 return;
98
99 delete[] unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100 unionArray = constArray;
101 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000102
Jamie Madill703cdd62013-07-08 15:07:30 -0400103private:
104 DISALLOW_COPY_AND_ASSIGN(TVariable);
105
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106 TType type;
107 bool userType;
108 // we are assuming that Pool Allocator will free the memory allocated to unionArray
109 // when this object is destroyed
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000110 ConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111};
112
113//
114// The function sub-class of symbols and the parser will need to
115// share this definition of a function parameter.
116//
117struct TParameter {
118 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400119 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120};
121
122//
123// The function sub-class of a symbol.
124//
125class TFunction : public TSymbol {
126public:
127 TFunction(TOperator o) :
128 TSymbol(0),
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000129 returnType(TType(EbtVoid, EbpUndefined)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 op(o),
131 defined(false) { }
132 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) :
133 TSymbol(name),
134 returnType(retType),
alokp@chromium.org43884872010-03-30 00:08:52 +0000135 mangledName(TFunction::mangleName(*name)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 op(tOp),
137 defined(false) { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000138 virtual ~TFunction();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 virtual bool isFunction() const { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000140
141 static TString mangleName(const TString& name) { return name + '('; }
142 static TString unmangleName(const TString& mangledName)
143 {
144 return TString(mangledName.c_str(), mangledName.find_first_of('('));
145 }
146
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147 void addParameter(TParameter& p)
148 {
149 parameters.push_back(p);
150 mangledName = mangledName + p.type->getMangledName();
151 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 const TString& getMangledName() const { return mangledName; }
154 const TType& getReturnType() const { return returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000155
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156 void relateToOperator(TOperator o) { op = o; }
157 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000158
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159 void setDefined() { defined = true; }
160 bool isDefined() { return defined; }
161
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000162 size_t getParamCount() const { return parameters.size(); }
163 const TParameter& getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000164
Jamie Madill703cdd62013-07-08 15:07:30 -0400165private:
166 DISALLOW_COPY_AND_ASSIGN(TFunction);
167
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168 typedef TVector<TParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000169 TParamList parameters;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170 TType returnType;
171 TString mangledName;
172 TOperator op;
173 bool defined;
174};
175
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000176//
177// Interface block name sub-symbol
178//
179class TInterfaceBlockName : public TSymbol
180{
181public:
182 TInterfaceBlockName(const TString *name)
183 : TSymbol(name)
184 {}
185
186 virtual ~TInterfaceBlockName() {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000187};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188
189class TSymbolTableLevel {
190public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +0000191 typedef TMap<TString, TSymbol*> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000192 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 typedef const tLevel::value_type tLevelPair;
194 typedef std::pair<tLevel::iterator, bool> tInsertResult;
195
196 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
197 TSymbolTableLevel() { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000198 ~TSymbolTableLevel();
199
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -0400200 bool insert(const TString &name, TSymbol &symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201 {
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400202 symbol.setUniqueId(++uniqueId);
203
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204 //
205 // returning true means symbol was added to the table
206 //
207 tInsertResult result;
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -0400208 result = level.insert(tLevelPair(name, &symbol));
alokp@chromium.org43884872010-03-30 00:08:52 +0000209
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210 return result.second;
211 }
212
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -0400213 bool insert(TSymbol &symbol)
214 {
215 return insert(symbol.getMangledName(), symbol);
216 }
217
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218 TSymbol* find(const TString& name) const
219 {
220 tLevel::const_iterator it = level.find(name);
221 if (it == level.end())
222 return 0;
223 else
224 return (*it).second;
225 }
226
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227 void relateToOperator(const char* name, TOperator op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000228 void relateToExtension(const char* name, const TString& ext);
alokp@chromium.org43884872010-03-30 00:08:52 +0000229
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230protected:
231 tLevel level;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400232 static int uniqueId; // for unique identification in code generation
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233};
234
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000235enum ESymbolLevel
236{
237 COMMON_BUILTINS = 0,
238 ESSL1_BUILTINS = 1,
239 ESSL3_BUILTINS = 2,
240 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
241 GLOBAL_LEVEL = 3
242};
243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244class TSymbolTable {
245public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400246 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247 {
248 //
249 // The symbol table cannot be used until push() is called, but
250 // the lack of an initial call to push() can be used to detect
251 // that the symbol table has not been preloaded with built-ins.
252 //
253 }
254
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 ~TSymbolTable()
256 {
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000257 while (table.size() > 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258 pop();
259 }
260
261 //
262 // When the symbol table is initialized with the built-ins, there should
263 // 'push' calls, so that built-ins are at level 0 and the shader
264 // globals are at level 1.
265 //
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000266 bool isEmpty() { return table.empty(); }
267 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
268 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000269 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000270 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271 table.push_back(new TSymbolTableLevel);
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000272 precisionStack.push_back( PrecisionStackLevel() );
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000273 }
274
alokp@chromium.org43884872010-03-30 00:08:52 +0000275 void pop()
276 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277 delete table[currentLevel()];
278 table.pop_back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000279 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280 }
281
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400282 bool declare(TSymbol &symbol)
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000283 {
284 return insert(currentLevel(), symbol);
285 }
286
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400287 bool insert(ESymbolLevel level, TSymbol &symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288 {
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000289 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000291
Nicolas Capens49a88872013-06-20 09:54:03 -0400292 bool insertConstInt(ESymbolLevel level, const char *name, int value)
293 {
294 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
295 constant->getConstPointer()->setIConst(value);
296 return insert(level, *constant);
297 }
298
Nicolas Capens8f80e022013-07-11 11:20:50 -0400299 bool insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0)
Nicolas Capens49a88872013-06-20 09:54:03 -0400300 {
301 TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
302
Nicolas Capens8f80e022013-07-11 11:20:50 -0400303 TParameter param1 = {NULL, ptype1};
Nicolas Capens49a88872013-06-20 09:54:03 -0400304 function->addParameter(param1);
305
Nicolas Capens8f80e022013-07-11 11:20:50 -0400306 if(ptype2)
Nicolas Capens49a88872013-06-20 09:54:03 -0400307 {
Nicolas Capens8f80e022013-07-11 11:20:50 -0400308 TParameter param2 = {NULL, ptype2};
Nicolas Capens49a88872013-06-20 09:54:03 -0400309 function->addParameter(param2);
310 }
311
Nicolas Capens8f80e022013-07-11 11:20:50 -0400312 if(ptype3)
Nicolas Capens49a88872013-06-20 09:54:03 -0400313 {
Nicolas Capens8f80e022013-07-11 11:20:50 -0400314 TParameter param3 = {NULL, ptype3};
Nicolas Capens49a88872013-06-20 09:54:03 -0400315 function->addParameter(param3);
316 }
317
318 return insert(level, *function);
319 }
320
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000321 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false);
322 TSymbol *findBuiltIn(const TString &name, int shaderVersion);
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000323
324 TSymbolTableLevel *getOuterLevel() {
325 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000326 return table[currentLevel() - 1];
327 }
328
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000329 void relateToOperator(ESymbolLevel level, const char* name, TOperator op) {
330 table[level]->relateToOperator(name, op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000331 }
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000332 void relateToExtension(ESymbolLevel level, const char* name, const TString& ext) {
333 table[level]->relateToExtension(name, ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000334 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000336 bool setDefaultPrecision( const TPublicType& type, TPrecision prec ){
337 if (IsSampler(type.type))
338 return true; // Skip sampler types for the time being
339 if (type.type != EbtFloat && type.type != EbtInt)
340 return false; // Only set default precision for int/float
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000341 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000342 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000343 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000344 precisionStack[indexOfLastElement][type.type] = prec; // Uses map operator [], overwrites the current value
345 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000346 }
347
348 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
349 TPrecision getDefaultPrecision( TBasicType type){
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000350
351 // unsigned integers use the same precision as signed
352 if (type == EbtUInt) type = EbtInt;
353
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000354 if( type != EbtFloat && type != EbtInt ) return EbpUndefined;
355 int level = static_cast<int>(precisionStack.size()) - 1;
356 assert( level >= 0); // Just to be safe. Should not happen.
357 PrecisionStackLevel::iterator it;
358 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
359 while( level >= 0 ){
360 it = precisionStack[level].find( type );
361 if( it != precisionStack[level].end() ){
362 prec = (*it).second;
363 break;
364 }
365 level--;
366 }
367 return prec;
368 }
369
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000370protected:
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000371 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
373 std::vector<TSymbolTableLevel*> table;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000374 typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
375 std::vector< PrecisionStackLevel > precisionStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376};
377
378#endif // _SYMBOL_TABLE_INCLUDED_