blob: 188f810e37146e1adaf69290844c6886a4f8d180 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +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//
8// Symbol table for parsing. Most functionaliy and main ideas
9// are documented in the header file.
10//
11
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#if defined(_MSC_VER)
13#pragma warning(disable: 4718)
14#endif
15
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040017#include "compiler/translator/Cache.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000019#include <stdio.h>
kbr@chromium.org476541f2011-10-27 21:14:51 +000020#include <algorithm>
21
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
Jamie Madillbfa91f42014-06-05 15:45:18 -040025int TSymbolTable::uniqueIdCounter = 0;
Nicolas Capensbd10cf52013-06-20 09:51:51 -040026
Olli Etuaho476197f2016-10-11 13:59:08 +010027TSymbol::TSymbol(const TString *n) : uniqueId(TSymbolTable::nextUniqueId()), name(n)
28{
29}
30
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032// Functions have buried pointers to delete.
33//
34TFunction::~TFunction()
35{
Olli Etuaho476197f2016-10-11 13:59:08 +010036 clearParameters();
37}
38
39void TFunction::clearParameters()
40{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000041 for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i)
42 delete (*i).type;
Olli Etuaho476197f2016-10-11 13:59:08 +010043 parameters.clear();
44 mangledName = nullptr;
45}
46
47void TFunction::swapParameters(const TFunction &parametersSource)
48{
49 clearParameters();
50 for (auto parameter : parametersSource.parameters)
51 {
52 addParameter(parameter);
53 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054}
55
Dmitry Skiba58832202015-07-06 16:11:13 -070056const TString *TFunction::buildMangledName() const
57{
Cooper Partin149e6e62015-08-07 16:18:18 -070058 std::string newName = mangleName(getName()).c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070059
60 for (const auto &p : parameters)
61 {
Cooper Partin149e6e62015-08-07 16:18:18 -070062 newName += p.type->getMangledName().c_str();
Dmitry Skiba58832202015-07-06 16:11:13 -070063 }
64
Cooper Partin149e6e62015-08-07 16:18:18 -070065 return NewPoolTString(newName.c_str());
Dmitry Skiba58832202015-07-06 16:11:13 -070066}
67
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068//
69// Symbol table levels are a map of pointers to symbols that have to be deleted.
70//
71TSymbolTableLevel::~TSymbolTableLevel()
72{
daniel@transgaming.com0578f812010-05-17 09:58:39 +000073 for (tLevel::iterator it = level.begin(); it != level.end(); ++it)
74 delete (*it).second;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075}
76
Nicolas Capensadfffe42014-06-17 02:13:36 -040077bool TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040078{
Jamie Madillbfa91f42014-06-05 15:45:18 -040079 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040080 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040081
82 return result.second;
83}
84
Olli Etuahob2983c92015-03-18 14:02:46 +020085bool TSymbolTableLevel::insertUnmangled(TFunction *function)
86{
Olli Etuahob2983c92015-03-18 14:02:46 +020087 // returning true means symbol was added to the table
88 tInsertResult result = level.insert(tLevelPair(function->getName(), function));
89
90 return result.second;
91}
92
Jamie Madillbfa91f42014-06-05 15:45:18 -040093TSymbol *TSymbolTableLevel::find(const TString &name) const
94{
95 tLevel::const_iterator it = level.find(name);
96 if (it == level.end())
97 return 0;
98 else
99 return (*it).second;
100}
101
Zhenyao Moe740add2014-07-18 17:01:01 -0700102TSymbol *TSymbolTable::find(const TString &name, int shaderVersion,
103 bool *builtIn, bool *sameScope) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000104{
105 int level = currentLevel();
106 TSymbol *symbol;
107
108 do
109 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300110 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
111 level--;
112 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700113 level--;
114 if (level == ESSL1_BUILTINS && shaderVersion != 100)
115 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000116
117 symbol = table[level]->find(name);
118 }
119 while (symbol == 0 && --level >= 0);
120
121 if (builtIn)
122 *builtIn = (level <= LAST_BUILTIN_LEVEL);
123 if (sameScope)
124 *sameScope = (level == currentLevel());
125
126 return symbol;
127}
128
Zhenyao Mod7490962016-11-09 15:49:51 -0800129TSymbol *TSymbolTable::findGlobal(const TString &name) const
130{
131 ASSERT(table.size() > GLOBAL_LEVEL);
132 return table[GLOBAL_LEVEL]->find(name);
133}
134
Zhenyao Moe740add2014-07-18 17:01:01 -0700135TSymbol *TSymbolTable::findBuiltIn(
136 const TString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000137{
138 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
139 {
Martin Radeve93d24e2016-07-28 12:06:05 +0300140 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
141 level--;
142 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700143 level--;
144 if (level == ESSL1_BUILTINS && shaderVersion != 100)
145 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000146
147 TSymbol *symbol = table[level]->find(name);
148
149 if (symbol)
150 return symbol;
151 }
152
153 return 0;
154}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400155
156TSymbolTable::~TSymbolTable()
157{
158 while (table.size() > 0)
159 pop();
160}
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700161
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500162bool IsGenType(const TType *type)
163{
164 if (type)
165 {
166 TBasicType basicType = type->getBasicType();
167 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType;
168 }
169
170 return false;
171}
172
173bool IsVecType(const TType *type)
174{
175 if (type)
176 {
177 TBasicType basicType = type->getBasicType();
178 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec;
179 }
180
181 return false;
182}
183
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700184const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500185{
186 ASSERT(size >= 1 && size <= 4);
187
188 if (!type)
189 {
190 return nullptr;
191 }
192
193 ASSERT(!IsVecType(type));
194
195 switch(type->getBasicType())
196 {
Dmitry Skiba01971112015-07-10 14:54:00 -0400197 case EbtGenType: return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
198 case EbtGenIType: return TCache::getType(EbtInt, static_cast<unsigned char>(size));
199 case EbtGenUType: return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
200 case EbtGenBType: return TCache::getType(EbtBool, static_cast<unsigned char>(size));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500201 default: return type;
202 }
203}
204
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700205const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500206{
207 ASSERT(size >= 2 && size <= 4);
208
209 if (!type)
210 {
211 return nullptr;
212 }
213
214 ASSERT(!IsGenType(type));
215
216 switch(type->getBasicType())
217 {
Dmitry Skiba01971112015-07-10 14:54:00 -0400218 case EbtVec: return TCache::getType(EbtFloat, static_cast<unsigned char>(size));
219 case EbtIVec: return TCache::getType(EbtInt, static_cast<unsigned char>(size));
220 case EbtUVec: return TCache::getType(EbtUInt, static_cast<unsigned char>(size));
221 case EbtBVec: return TCache::getType(EbtBool, static_cast<unsigned char>(size));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500222 default: return type;
223 }
224}
225
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700226void TSymbolTable::insertBuiltIn(ESymbolLevel level, TOperator op, const char *ext, const TType *rvalue, const char *name,
227 const TType *ptype1, const TType *ptype2, const TType *ptype3, const TType *ptype4, const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700228{
229 if (ptype1->getBasicType() == EbtGSampler2D)
230 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530231 insertUnmangledBuiltIn(name);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700232 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400233 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5);
234 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5);
235 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700236 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500237 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700238 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530239 insertUnmangledBuiltIn(name);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700240 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400241 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5);
242 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5);
243 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700244 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500245 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700246 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530247 insertUnmangledBuiltIn(name);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700248 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400249 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5);
250 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5);
251 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700252 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500253 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700254 {
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530255 insertUnmangledBuiltIn(name);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700256 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Dmitry Skiba01971112015-07-10 14:54:00 -0400257 insertBuiltIn(level, gvec4 ? TCache::getType(EbtFloat, 4) : rvalue, name, TCache::getType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5);
258 insertBuiltIn(level, gvec4 ? TCache::getType(EbtInt, 4) : rvalue, name, TCache::getType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5);
259 insertBuiltIn(level, gvec4 ? TCache::getType(EbtUInt, 4) : rvalue, name, TCache::getType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700260 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300261 else if (IsGImage(ptype1->getBasicType()))
262 {
263 insertUnmangledBuiltIn(name);
264
265 const TType *floatType = TCache::getType(EbtFloat, 4);
266 const TType *intType = TCache::getType(EbtInt, 4);
267 const TType *unsignedType = TCache::getType(EbtUInt, 4);
268
269 const TType *floatImage =
270 TCache::getType(convertGImageToFloatImage(ptype1->getBasicType()));
271 const TType *intImage = TCache::getType(convertGImageToIntImage(ptype1->getBasicType()));
272 const TType *unsignedImage =
273 TCache::getType(convertGImageToUnsignedImage(ptype1->getBasicType()));
274
275 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
276 if (rvalue->getBasicType() == EbtGVec4)
277 {
278 // imageLoad
279 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
280 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
281 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
282 }
283 else if (rvalue->getBasicType() == EbtVoid)
284 {
285 // imageStore
286 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
287 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
288 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
289 }
290 else
291 {
292 // imageSize
293 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
294 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
295 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
296 }
297 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500298 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3))
299 {
300 ASSERT(!ptype4 && !ptype5);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530301 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500302 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1));
303 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2));
304 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3));
305 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500306 }
307 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
308 {
309 ASSERT(!ptype4 && !ptype5);
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530310 insertUnmangledBuiltIn(name);
Nicolas Capensc9d9b302015-02-20 23:02:15 -0500311 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2));
312 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3));
313 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500314 }
315 else
316 {
Dmitry Skiba7f17a502015-06-22 15:08:39 -0700317 TFunction *function = new TFunction(NewPoolTString(name), rvalue, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700318
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700319 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700320
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500321 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700322 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700323 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700324 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700325
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500326 if (ptype3)
327 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700328 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500329 }
330
331 if (ptype4)
332 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700333 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500334 }
335
336 if (ptype5)
337 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700338 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500339 }
340
Olli Etuahoc4a96d62015-07-23 17:37:39 +0530341 ASSERT(hasUnmangledBuiltIn(name));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500342 insert(level, function);
343 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700344}
345
Zhenyao Moe740add2014-07-18 17:01:01 -0700346TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700347{
348 if (!SupportsPrecision(type))
349 return EbpUndefined;
350
351 // unsigned integers use the same precision as signed
352 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
353
354 int level = static_cast<int>(precisionStack.size()) - 1;
355 assert(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200356 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700357 TPrecision prec = EbpUndefined;
358 while (level >= 0)
359 {
360 PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType);
361 if (it != precisionStack[level]->end())
362 {
363 prec = (*it).second;
364 break;
365 }
366 level--;
367 }
368 return prec;
369}
Jamie Madill45bcc782016-11-07 13:58:48 -0500370
371} // namespace sh