blob: 278171c9107c4ac6cdbf34395daa30339c30ab21 [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"
Geoff Lang17732822013-08-29 13:46:49 -040036#include "compiler/translator/InfoSink.h"
37#include "compiler/translator/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:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040044 POOL_ALLOCATOR_NEW_DELETE();
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040045 TSymbol(const TString* n) : uniqueId(0), name(n) { }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 virtual ~TSymbol() { /* don't delete name, it's from the pool */ }
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040047
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 const TString& getName() const { return *name; }
49 virtual const TString& getMangledName() const { return getName(); }
50 virtual bool isFunction() const { return false; }
51 virtual bool isVariable() const { return false; }
52 void setUniqueId(int id) { uniqueId = id; }
53 int getUniqueId() const { return uniqueId; }
Jamie Madill2aeb26a2013-07-08 14:02:55 -040054 void relateToExtension(const TString& ext) { extension = ext; }
55 const TString& getExtension() const { return extension; }
Nicolas Capensba60ad32013-06-04 15:55:47 -040056
Jamie Madill703cdd62013-07-08 15:07:30 -040057private:
58 DISALLOW_COPY_AND_ASSIGN(TSymbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000059
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040060 int uniqueId; // For real comparing during code generation
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061 const TString *name;
Jamie Madill2aeb26a2013-07-08 14:02:55 -040062 TString extension;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063};
64
65//
66// Variable class, meaning a symbol that's not a function.
67//
68// There could be a separate class heirarchy for Constant variables;
69// Only one of int, bool, or float, (or none) is correct for
70// any particular use, but it's easy to do this way, and doesn't
71// seem worth having separate classes, and "getConst" can't simply return
72// different values for different types polymorphically, so this is
73// just simple and pragmatic.
74//
75class TVariable : public TSymbol {
76public:
Jamie Madill18464b52013-07-08 14:01:55 -040077 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 +000078 virtual ~TVariable() { }
79 virtual bool isVariable() const { return true; }
80 TType& getType() { return type; }
81 const TType& getType() const { return type; }
82 bool isUserType() const { return userType; }
alokp@chromium.org58e54292010-08-24 21:40:03 +000083 void setQualifier(TQualifier qualifier) { type.setQualifier(qualifier); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084
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
Jamie Madill703cdd62013-07-08 15:07:30 -0400104private:
105 DISALLOW_COPY_AND_ASSIGN(TVariable);
106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107 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};
113
114//
115// The function sub-class of symbols and the parser will need to
116// share this definition of a function parameter.
117//
118struct TParameter {
119 TString *name;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400120 TType *type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121};
122
123//
124// The function sub-class of a symbol.
125//
126class TFunction : public TSymbol {
127public:
128 TFunction(TOperator o) :
129 TSymbol(0),
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000130 returnType(TType(EbtVoid, EbpUndefined)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131 op(o),
132 defined(false) { }
133 TFunction(const TString *name, TType& retType, TOperator tOp = EOpNull) :
134 TSymbol(name),
135 returnType(retType),
alokp@chromium.org43884872010-03-30 00:08:52 +0000136 mangledName(TFunction::mangleName(*name)),
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 op(tOp),
138 defined(false) { }
alokp@chromium.org43884872010-03-30 00:08:52 +0000139 virtual ~TFunction();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 virtual bool isFunction() const { return true; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000141
142 static TString mangleName(const TString& name) { return name + '('; }
143 static TString unmangleName(const TString& mangledName)
144 {
145 return TString(mangledName.c_str(), mangledName.find_first_of('('));
146 }
147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148 void addParameter(TParameter& p)
149 {
150 parameters.push_back(p);
151 mangledName = mangledName + p.type->getMangledName();
152 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 const TString& getMangledName() const { return mangledName; }
155 const TType& getReturnType() const { return returnType; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000156
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157 void relateToOperator(TOperator o) { op = o; }
158 TOperator getBuiltInOp() const { return op; }
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000159
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000160 void setDefined() { defined = true; }
161 bool isDefined() { return defined; }
162
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000163 size_t getParamCount() const { return parameters.size(); }
164 const TParameter& getParam(size_t i) const { return parameters[i]; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000165
Jamie Madill703cdd62013-07-08 15:07:30 -0400166private:
167 DISALLOW_COPY_AND_ASSIGN(TFunction);
168
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 typedef TVector<TParameter> TParamList;
alokp@chromium.org43884872010-03-30 00:08:52 +0000170 TParamList parameters;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171 TType returnType;
172 TString mangledName;
173 TOperator op;
174 bool defined;
175};
176
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000177//
178// Interface block name sub-symbol
179//
180class TInterfaceBlockName : public TSymbol
181{
182public:
183 TInterfaceBlockName(const TString *name)
184 : TSymbol(name)
185 {}
186
187 virtual ~TInterfaceBlockName() {}
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +0000188};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
190class TSymbolTableLevel {
191public:
alokp@chromium.org91a01a12010-05-12 18:39:04 +0000192 typedef TMap<TString, TSymbol*> tLevel;
alokp@chromium.org43884872010-03-30 00:08:52 +0000193 typedef tLevel::const_iterator const_iterator;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194 typedef const tLevel::value_type tLevelPair;
195 typedef std::pair<tLevel::iterator, bool> tInsertResult;
196
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000197 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 //
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400207 tInsertResult result = level.insert(tLevelPair(name, &symbol));
alokp@chromium.org43884872010-03-30 00:08:52 +0000208
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209 return result.second;
210 }
211
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -0400212 bool insert(TSymbol &symbol)
213 {
214 return insert(symbol.getMangledName(), symbol);
215 }
216
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217 TSymbol* find(const TString& name) const
218 {
219 tLevel::const_iterator it = level.find(name);
220 if (it == level.end())
221 return 0;
222 else
223 return (*it).second;
224 }
225
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226 void relateToOperator(const char* name, TOperator op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000227 void relateToExtension(const char* name, const TString& ext);
alokp@chromium.org43884872010-03-30 00:08:52 +0000228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229protected:
230 tLevel level;
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400231 static int uniqueId; // for unique identification in code generation
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232};
233
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000234enum ESymbolLevel
235{
236 COMMON_BUILTINS = 0,
237 ESSL1_BUILTINS = 1,
238 ESSL3_BUILTINS = 2,
239 LAST_BUILTIN_LEVEL = ESSL3_BUILTINS,
240 GLOBAL_LEVEL = 3
241};
242
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243class TSymbolTable {
244public:
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400245 TSymbolTable()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246 {
247 //
248 // The symbol table cannot be used until push() is called, but
249 // the lack of an initial call to push() can be used to detect
250 // that the symbol table has not been preloaded with built-ins.
251 //
252 }
253
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400254 ~TSymbolTable();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255
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 //
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000261 bool isEmpty() { return table.empty(); }
262 bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
263 bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
alokp@chromium.org43884872010-03-30 00:08:52 +0000264 void push()
alokp@chromium.orge4249f02010-07-26 18:13:52 +0000265 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266 table.push_back(new TSymbolTableLevel);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400267 precisionStack.push_back(new PrecisionStackLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 }
269
alokp@chromium.org43884872010-03-30 00:08:52 +0000270 void pop()
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400271 {
272 delete table.back();
273 table.pop_back();
274
275 delete precisionStack.back();
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000276 precisionStack.pop_back();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277 }
278
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400279 bool declare(TSymbol &symbol)
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000280 {
281 return insert(currentLevel(), symbol);
282 }
283
Nicolas Capensbd10cf52013-06-20 09:51:51 -0400284 bool insert(ESymbolLevel level, TSymbol &symbol)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285 {
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000286 return table[level]->insert(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 }
alokp@chromium.org43884872010-03-30 00:08:52 +0000288
Nicolas Capens49a88872013-06-20 09:54:03 -0400289 bool insertConstInt(ESymbolLevel level, const char *name, int value)
290 {
291 TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
292 constant->getConstPointer()->setIConst(value);
293 return insert(level, *constant);
294 }
295
Nicolas Capens8f80e022013-07-11 11:20:50 -0400296 bool insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0)
Nicolas Capens49a88872013-06-20 09:54:03 -0400297 {
298 TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
299
Nicolas Capens8f80e022013-07-11 11:20:50 -0400300 TParameter param1 = {NULL, ptype1};
Nicolas Capens49a88872013-06-20 09:54:03 -0400301 function->addParameter(param1);
302
Nicolas Capens8f80e022013-07-11 11:20:50 -0400303 if(ptype2)
Nicolas Capens49a88872013-06-20 09:54:03 -0400304 {
Nicolas Capens8f80e022013-07-11 11:20:50 -0400305 TParameter param2 = {NULL, ptype2};
Nicolas Capens49a88872013-06-20 09:54:03 -0400306 function->addParameter(param2);
307 }
308
Nicolas Capens8f80e022013-07-11 11:20:50 -0400309 if(ptype3)
Nicolas Capens49a88872013-06-20 09:54:03 -0400310 {
Nicolas Capens8f80e022013-07-11 11:20:50 -0400311 TParameter param3 = {NULL, ptype3};
Nicolas Capens49a88872013-06-20 09:54:03 -0400312 function->addParameter(param3);
313 }
314
315 return insert(level, *function);
316 }
317
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000318 TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false);
319 TSymbol *findBuiltIn(const TString &name, int shaderVersion);
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000320
321 TSymbolTableLevel *getOuterLevel() {
322 assert(currentLevel() >= 1);
daniel@transgaming.com5dd6d092012-03-20 20:10:28 +0000323 return table[currentLevel() - 1];
324 }
325
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000326 void relateToOperator(ESymbolLevel level, const char* name, TOperator op) {
327 table[level]->relateToOperator(name, op);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000328 }
shannonwoods@chromium.org6e10a0e2013-05-30 00:02:13 +0000329 void relateToExtension(ESymbolLevel level, const char* name, const TString& ext) {
330 table[level]->relateToExtension(name, ext);
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000331 }
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400332 void dump(TInfoSink &infoSink) const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400334 bool setDefaultPrecision(const TPublicType& type, TPrecision prec) {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400335 if (!supportsPrecision(type.type))
336 return false;
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +0000337 if (type.isAggregate())
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000338 return false; // Not allowed to set for aggregate types
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000339 int indexOfLastElement = static_cast<int>(precisionStack.size()) - 1;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400340 (*precisionStack[indexOfLastElement])[type.type] = prec; // Uses map operator [], overwrites the current value
shannon.woods@transgaming.comd25a6b32013-02-28 23:19:13 +0000341 return true;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000342 }
343
344 // Searches down the precisionStack for a precision qualifier for the specified TBasicType
345 TPrecision getDefaultPrecision( TBasicType type){
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000346
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400347 if (!supportsPrecision(type))
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400348 return EbpUndefined;
349
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400350 // unsigned integers use the same precision as signed
351 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
352
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000353 int level = static_cast<int>(precisionStack.size()) - 1;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400354 assert(level >= 0); // Just to be safe. Should not happen.
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000355 PrecisionStackLevel::iterator it;
356 TPrecision prec = EbpUndefined; // If we dont find anything we return this. Should we error check this?
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400357 while (level >= 0) {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400358 it = precisionStack[level]->find(baseType);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400359 if (it != precisionStack[level]->end()) {
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000360 prec = (*it).second;
361 break;
362 }
363 level--;
364 }
365 return prec;
366 }
367
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400368private:
shannonwoods@chromium.org1c848092013-05-30 00:02:34 +0000369 ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400371 bool supportsPrecision(TBasicType type) {
372 // Only supports precision for int, float, and sampler types.
373 return type == EbtFloat || type == EbtInt || type == EbtUInt || IsSampler(type);
374 }
375
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376 std::vector<TSymbolTableLevel*> table;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400377 typedef TMap<TBasicType, TPrecision> PrecisionStackLevel;
378 std::vector< PrecisionStackLevel*> precisionStack;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379};
380
381#endif // _SYMBOL_TABLE_INCLUDED_