daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | // |
| 8 | // Symbol table for parsing. Most functionaliy and main ideas |
| 9 | // are documented in the header file. |
| 10 | // |
| 11 | |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 12 | #if defined(_MSC_VER) |
| 13 | #pragma warning(disable: 4718) |
| 14 | #endif |
| 15 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 16 | #include "compiler/translator/SymbolTable.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
apatrick@chromium.org | 8187fa8 | 2010-06-15 22:09:28 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 21 | int TSymbolTable::uniqueIdCounter = 0; |
Nicolas Capens | bd10cf5 | 2013-06-20 09:51:51 -0400 | [diff] [blame] | 22 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 23 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 24 | // Functions have buried pointers to delete. |
| 25 | // |
| 26 | TFunction::~TFunction() |
| 27 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 28 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 29 | delete (*i).type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // |
| 33 | // Symbol table levels are a map of pointers to symbols that have to be deleted. |
| 34 | // |
| 35 | TSymbolTableLevel::~TSymbolTableLevel() |
| 36 | { |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 37 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 38 | delete (*it).second; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 41 | bool TSymbolTableLevel::insert(TSymbol *symbol) |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 42 | { |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 43 | symbol->setUniqueId(TSymbolTable::nextUniqueId()); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 44 | |
| 45 | // returning true means symbol was added to the table |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 46 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 47 | |
| 48 | return result.second; |
| 49 | } |
| 50 | |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 51 | TSymbol *TSymbolTableLevel::find(const TString &name) const |
| 52 | { |
| 53 | tLevel::const_iterator it = level.find(name); |
| 54 | if (it == level.end()) |
| 55 | return 0; |
| 56 | else |
| 57 | return (*it).second; |
| 58 | } |
| 59 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 60 | // |
| 61 | // Change all function entries in the table with the non-mangled name |
| 62 | // to be related to the provided built-in operation. This is a low |
| 63 | // performance operation, and only intended for symbol tables that |
| 64 | // live across a large number of compiles. |
| 65 | // |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 66 | void TSymbolTableLevel::relateToOperator(const char *name, TOperator op) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 67 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 68 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 69 | { |
| 70 | if ((*it).second->isFunction()) |
| 71 | { |
| 72 | TFunction *function = static_cast<TFunction*>((*it).second); |
daniel@transgaming.com | 0578f81 | 2010-05-17 09:58:39 +0000 | [diff] [blame] | 73 | if (function->getName() == name) |
| 74 | function->relateToOperator(op); |
| 75 | } |
| 76 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 79 | // |
| 80 | // Change all function entries in the table with the non-mangled name |
| 81 | // to be related to the provided built-in extension. This is a low |
| 82 | // performance operation, and only intended for symbol tables that |
| 83 | // live across a large number of compiles. |
| 84 | // |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 85 | void TSymbolTableLevel::relateToExtension(const char *name, const TString &ext) |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 86 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 87 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 88 | { |
| 89 | TSymbol *symbol = it->second; |
| 90 | if (symbol->getName() == name) |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 91 | symbol->relateToExtension(ext); |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 95 | TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, |
| 96 | bool *builtIn, bool *sameScope) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 97 | { |
| 98 | int level = currentLevel(); |
| 99 | TSymbol *symbol; |
| 100 | |
| 101 | do |
| 102 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 103 | if (level == ESSL3_BUILTINS && shaderVersion != 300) |
| 104 | level--; |
| 105 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 106 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 107 | |
| 108 | symbol = table[level]->find(name); |
| 109 | } |
| 110 | while (symbol == 0 && --level >= 0); |
| 111 | |
| 112 | if (builtIn) |
| 113 | *builtIn = (level <= LAST_BUILTIN_LEVEL); |
| 114 | if (sameScope) |
| 115 | *sameScope = (level == currentLevel()); |
| 116 | |
| 117 | return symbol; |
| 118 | } |
| 119 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 120 | TSymbol *TSymbolTable::findBuiltIn( |
| 121 | const TString &name, int shaderVersion) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 122 | { |
| 123 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--) |
| 124 | { |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 125 | if (level == ESSL3_BUILTINS && shaderVersion != 300) |
| 126 | level--; |
| 127 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 128 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 129 | |
| 130 | TSymbol *symbol = table[level]->find(name); |
| 131 | |
| 132 | if (symbol) |
| 133 | return symbol; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 138 | |
| 139 | TSymbolTable::~TSymbolTable() |
| 140 | { |
| 141 | while (table.size() > 0) |
| 142 | pop(); |
| 143 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 144 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 145 | bool IsGenType(const TType *type) |
| 146 | { |
| 147 | if (type) |
| 148 | { |
| 149 | TBasicType basicType = type->getBasicType(); |
| 150 | return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType || basicType == EbtGenBType; |
| 151 | } |
| 152 | |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | bool IsVecType(const TType *type) |
| 157 | { |
| 158 | if (type) |
| 159 | { |
| 160 | TBasicType basicType = type->getBasicType(); |
| 161 | return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec || basicType == EbtBVec; |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | TType *SpecificType(TType *type, int size) |
| 168 | { |
| 169 | ASSERT(size >= 1 && size <= 4); |
| 170 | |
| 171 | if (!type) |
| 172 | { |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | ASSERT(!IsVecType(type)); |
| 177 | |
| 178 | switch(type->getBasicType()) |
| 179 | { |
| 180 | case EbtGenType: return new TType(EbtFloat, size); |
| 181 | case EbtGenIType: return new TType(EbtInt, size); |
| 182 | case EbtGenUType: return new TType(EbtUInt, size); |
| 183 | case EbtGenBType: return new TType(EbtBool, size); |
| 184 | default: return type; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | TType *VectorType(TType *type, int size) |
| 189 | { |
| 190 | ASSERT(size >= 2 && size <= 4); |
| 191 | |
| 192 | if (!type) |
| 193 | { |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | ASSERT(!IsGenType(type)); |
| 198 | |
| 199 | switch(type->getBasicType()) |
| 200 | { |
| 201 | case EbtVec: return new TType(EbtFloat, size); |
| 202 | case EbtIVec: return new TType(EbtInt, size); |
| 203 | case EbtUVec: return new TType(EbtUInt, size); |
| 204 | case EbtBVec: return new TType(EbtBool, size); |
| 205 | default: return type; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | void TSymbolTable::insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2, TType *ptype3, TType *ptype4, TType *ptype5) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 210 | { |
| 211 | if (ptype1->getBasicType() == EbtGSampler2D) |
| 212 | { |
| 213 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 214 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2D), ptype2, ptype3, ptype4, ptype5); |
| 215 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2D), ptype2, ptype3, ptype4, ptype5); |
| 216 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2D), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 217 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 218 | else if (ptype1->getBasicType() == EbtGSampler3D) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 219 | { |
| 220 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 221 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler3D), ptype2, ptype3, ptype4, ptype5); |
| 222 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler3D), ptype2, ptype3, ptype4, ptype5); |
| 223 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler3D), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 224 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 225 | else if (ptype1->getBasicType() == EbtGSamplerCube) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 226 | { |
| 227 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 228 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 229 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISamplerCube), ptype2, ptype3, ptype4, ptype5); |
| 230 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSamplerCube), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 231 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 232 | else if (ptype1->getBasicType() == EbtGSampler2DArray) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 233 | { |
| 234 | bool gvec4 = (rvalue->getBasicType() == EbtGVec4); |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 235 | insertBuiltIn(level, gvec4 ? new TType(EbtFloat, 4) : rvalue, name, new TType(EbtSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 236 | insertBuiltIn(level, gvec4 ? new TType(EbtInt, 4) : rvalue, name, new TType(EbtISampler2DArray), ptype2, ptype3, ptype4, ptype5); |
| 237 | insertBuiltIn(level, gvec4 ? new TType(EbtUInt, 4) : rvalue, name, new TType(EbtUSampler2DArray), ptype2, ptype3, ptype4, ptype5); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 238 | } |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 239 | else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3)) |
| 240 | { |
| 241 | ASSERT(!ptype4 && !ptype5); |
| 242 | insertBuiltIn(level, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1), SpecificType(ptype2, 1), SpecificType(ptype3, 1)); |
| 243 | insertBuiltIn(level, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2), SpecificType(ptype2, 2), SpecificType(ptype3, 2)); |
| 244 | insertBuiltIn(level, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3), SpecificType(ptype2, 3), SpecificType(ptype3, 3)); |
| 245 | insertBuiltIn(level, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4), SpecificType(ptype2, 4), SpecificType(ptype3, 4)); |
| 246 | } |
| 247 | else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3)) |
| 248 | { |
| 249 | ASSERT(!ptype4 && !ptype5); |
| 250 | insertBuiltIn(level, VectorType(rvalue, 2), name, VectorType(ptype1, 2), VectorType(ptype2, 2), VectorType(ptype3, 2)); |
| 251 | insertBuiltIn(level, VectorType(rvalue, 3), name, VectorType(ptype1, 3), VectorType(ptype2, 3), VectorType(ptype3, 3)); |
| 252 | insertBuiltIn(level, VectorType(rvalue, 4), name, VectorType(ptype1, 4), VectorType(ptype2, 4), VectorType(ptype3, 4)); |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | TFunction *function = new TFunction(NewPoolTString(name), *rvalue); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 257 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 258 | TParameter param1 = {0, ptype1}; |
| 259 | function->addParameter(param1); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 260 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 261 | if (ptype2) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 262 | { |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 263 | TParameter param2 = {0, ptype2}; |
| 264 | function->addParameter(param2); |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 265 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 266 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 267 | if (ptype3) |
| 268 | { |
| 269 | TParameter param3 = {0, ptype3}; |
| 270 | function->addParameter(param3); |
| 271 | } |
| 272 | |
| 273 | if (ptype4) |
| 274 | { |
| 275 | TParameter param4 = {0, ptype4}; |
| 276 | function->addParameter(param4); |
| 277 | } |
| 278 | |
| 279 | if (ptype5) |
| 280 | { |
| 281 | TParameter param5 = {0, ptype5}; |
| 282 | function->addParameter(param5); |
| 283 | } |
| 284 | |
| 285 | insert(level, function); |
| 286 | } |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 289 | TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 290 | { |
| 291 | if (!SupportsPrecision(type)) |
| 292 | return EbpUndefined; |
| 293 | |
| 294 | // unsigned integers use the same precision as signed |
| 295 | TBasicType baseType = (type == EbtUInt) ? EbtInt : type; |
| 296 | |
| 297 | int level = static_cast<int>(precisionStack.size()) - 1; |
| 298 | assert(level >= 0); // Just to be safe. Should not happen. |
| 299 | // If we dont find anything we return this. Should we error check this? |
| 300 | TPrecision prec = EbpUndefined; |
| 301 | while (level >= 0) |
| 302 | { |
| 303 | PrecisionStackLevel::iterator it = precisionStack[level]->find(baseType); |
| 304 | if (it != precisionStack[level]->end()) |
| 305 | { |
| 306 | prec = (*it).second; |
| 307 | break; |
| 308 | } |
| 309 | level--; |
| 310 | } |
| 311 | return prec; |
| 312 | } |