Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 2 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 6 | // |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 14 | |
| 15 | // |
| 16 | // Symbol table for parsing. Most functionaliy and main ideas |
| 17 | // are documented in the header file. |
| 18 | // |
| 19 | |
| 20 | #if defined(_MSC_VER) |
| 21 | #pragma warning(disable: 4718) |
| 22 | #endif |
| 23 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 24 | #include "SymbolTable.h" |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 25 | |
| 26 | #include <stdio.h> |
Nicolas Capens | 94352d9 | 2015-06-11 08:53:40 -0500 | [diff] [blame] | 27 | #include <limits.h> |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | |
Nicolas Capens | 0b7471a | 2018-01-09 21:39:21 -0500 | [diff] [blame] | 30 | #if defined(_MSC_VER) && MSC_VER < 1900 |
| 31 | #define snprintf _snprintf |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 32 | #endif |
| 33 | |
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 34 | int TSymbolTableLevel::uniqueId = 0; |
| 35 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 36 | TType::TType(const TPublicType &p) : |
Nicolas Capens | bb2bcae | 2018-02-05 11:12:10 -0500 | [diff] [blame] | 37 | type(p.type), precision(p.precision), qualifier(p.qualifier), layoutQualifier(p.layoutQualifier), |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 38 | primarySize(p.primarySize), secondarySize(p.secondarySize), array(p.array), arraySize(p.arraySize), maxArraySize(0), |
Nicolas Capens | bb2bcae | 2018-02-05 11:12:10 -0500 | [diff] [blame] | 39 | arrayInformationType(0), interfaceBlock(0), structure(0), mangled(0) |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 40 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 41 | if (p.userDef) |
| 42 | { |
| 43 | structure = p.userDef->getStruct(); |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 44 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // |
| 48 | // Recursively generate mangled names. |
| 49 | // |
| 50 | void TType::buildMangledName(TString& mangledName) |
| 51 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 52 | if (isMatrix()) |
| 53 | mangledName += 'm'; |
| 54 | else if (isVector()) |
| 55 | mangledName += 'v'; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 56 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 57 | switch (type) { |
| 58 | case EbtFloat: mangledName += 'f'; break; |
| 59 | case EbtInt: mangledName += 'i'; break; |
| 60 | case EbtUInt: mangledName += 'u'; break; |
| 61 | case EbtBool: mangledName += 'b'; break; |
| 62 | case EbtSampler2D: mangledName += "s2"; break; |
Alexis Hetu | b027aa9 | 2015-01-19 15:56:12 -0500 | [diff] [blame] | 63 | case EbtSampler3D: mangledName += "s3"; break; |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 64 | case EbtSamplerCube: mangledName += "sC"; break; |
| 65 | case EbtSampler2DArray: mangledName += "s2a"; break; |
Alexis Hetu | 4676862 | 2018-01-16 22:09:28 -0500 | [diff] [blame] | 66 | case EbtSampler2DRect: mangledName += "s2r"; break; |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 67 | case EbtSamplerExternalOES: mangledName += "sext"; break; |
| 68 | case EbtISampler2D: mangledName += "is2"; break; |
| 69 | case EbtISampler3D: mangledName += "is3"; break; |
| 70 | case EbtISamplerCube: mangledName += "isC"; break; |
| 71 | case EbtISampler2DArray: mangledName += "is2a"; break; |
| 72 | case EbtUSampler2D: mangledName += "us2"; break; |
| 73 | case EbtUSampler3D: mangledName += "us3"; break; |
| 74 | case EbtUSamplerCube: mangledName += "usC"; break; |
| 75 | case EbtUSampler2DArray: mangledName += "us2a"; break; |
| 76 | case EbtSampler2DShadow: mangledName += "s2s"; break; |
| 77 | case EbtSamplerCubeShadow: mangledName += "sCs"; break; |
| 78 | case EbtSampler2DArrayShadow: mangledName += "s2as"; break; |
| 79 | case EbtStruct: mangledName += structure->mangledName(); break; |
| 80 | case EbtInterfaceBlock: mangledName += interfaceBlock->mangledName(); break; |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 81 | default: |
| 82 | break; |
| 83 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 84 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 85 | mangledName += static_cast<char>('0' + getNominalSize()); |
| 86 | if(isMatrix()) { |
| 87 | mangledName += static_cast<char>('0' + getSecondarySize()); |
| 88 | } |
| 89 | if (isArray()) { |
| 90 | char buf[20]; |
| 91 | snprintf(buf, sizeof(buf), "%d", arraySize); |
| 92 | mangledName += '['; |
| 93 | mangledName += buf; |
| 94 | mangledName += ']'; |
| 95 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Alexis Hetu | ab75279 | 2016-04-21 16:11:31 -0400 | [diff] [blame] | 98 | size_t TType::getStructSize() const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 99 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 100 | if (!getStruct()) { |
| 101 | assert(false && "Not a struct"); |
| 102 | return 0; |
| 103 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 104 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 105 | return getStruct()->objectSize(); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 106 | } |
| 107 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 108 | bool TStructure::containsArrays() const |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 109 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 110 | for(const auto& field : *mFields) |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 111 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 112 | const TType *fieldType = field->type(); |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 113 | if(fieldType->isArray() || fieldType->isStructureContainingArrays()) |
| 114 | return true; |
| 115 | } |
| 116 | return false; |
| 117 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 118 | |
Alexis Hetu | ec93b1d | 2016-12-09 16:01:29 -0500 | [diff] [blame] | 119 | bool TStructure::containsType(TBasicType type) const |
| 120 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 121 | for(const auto& field : *mFields) |
Alexis Hetu | ec93b1d | 2016-12-09 16:01:29 -0500 | [diff] [blame] | 122 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 123 | const TType *fieldType = field->type(); |
Alexis Hetu | ec93b1d | 2016-12-09 16:01:29 -0500 | [diff] [blame] | 124 | if(fieldType->getBasicType() == type || fieldType->isStructureContainingType(type)) |
| 125 | return true; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 130 | bool TStructure::containsSamplers() const |
| 131 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 132 | for(const auto& field : *mFields) |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 133 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 134 | const TType *fieldType = field->type(); |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 135 | if(IsSampler(fieldType->getBasicType()) || fieldType->isStructureContainingSamplers()) |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 140 | |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 141 | void TStructure::setMatrixPackingIfUnspecified(TLayoutMatrixPacking matrixPacking) |
| 142 | { |
| 143 | for(auto& field : *mFields) |
| 144 | { |
| 145 | field->type()->setMatrixPackingIfUnspecified(matrixPacking); |
| 146 | } |
| 147 | } |
| 148 | |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 149 | TString TFieldListCollection::buildMangledName() const |
| 150 | { |
| 151 | TString mangledName(mangledNamePrefix()); |
| 152 | mangledName += *mName; |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 153 | for(const auto& field : *mFields) |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 154 | { |
| 155 | mangledName += '-'; |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 156 | mangledName += field->type()->getMangledName(); |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 157 | } |
| 158 | return mangledName; |
| 159 | } |
| 160 | |
| 161 | size_t TFieldListCollection::calculateObjectSize() const |
| 162 | { |
| 163 | size_t size = 0; |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 164 | for(const auto& field : *mFields) |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 165 | { |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 166 | size_t fieldSize = field->type()->getObjectSize(); |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 167 | if(fieldSize > INT_MAX - size) |
| 168 | size = INT_MAX; |
| 169 | else |
| 170 | size += fieldSize; |
| 171 | } |
| 172 | return size; |
| 173 | } |
| 174 | |
| 175 | int TStructure::calculateDeepestNesting() const |
| 176 | { |
| 177 | int maxNesting = 0; |
Alexis Hetu | d274253 | 2018-01-23 16:53:41 -0500 | [diff] [blame] | 178 | for(const auto& field : *mFields) |
| 179 | maxNesting = std::max(maxNesting, field->type()->getDeepestStructNesting()); |
Alexis Hetu | a8b364b | 2015-06-10 11:48:40 -0400 | [diff] [blame] | 180 | return 1 + maxNesting; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 184 | // Functions have buried pointers to delete. |
| 185 | // |
| 186 | TFunction::~TFunction() |
| 187 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 188 | for (TParamList::iterator i = parameters.begin(); i != parameters.end(); ++i) |
| 189 | delete (*i).type; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // |
| 193 | // Symbol table levels are a map of pointers to symbols that have to be deleted. |
| 194 | // |
| 195 | TSymbolTableLevel::~TSymbolTableLevel() |
| 196 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 197 | for (tLevel::iterator it = level.begin(); it != level.end(); ++it) |
| 198 | delete (*it).second; |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 199 | } |
| 200 | |
Nicolas Capens | 0b7471a | 2018-01-09 21:39:21 -0500 | [diff] [blame] | 201 | bool TSymbolTableLevel::insert(TSymbol *symbol) |
| 202 | { |
| 203 | symbol->setUniqueId(nextUniqueId()); |
| 204 | |
| 205 | // returning true means symbol was added to the table |
| 206 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
| 207 | |
| 208 | return result.second; |
| 209 | } |
| 210 | |
| 211 | bool TSymbolTableLevel::insertUnmangled(TFunction *function) |
| 212 | { |
| 213 | function->setUniqueId(nextUniqueId()); |
| 214 | |
| 215 | // returning true means symbol was added to the table |
| 216 | tInsertResult result = level.insert(tLevelPair(function->getName(), function)); |
| 217 | |
| 218 | return result.second; |
| 219 | } |
| 220 | |
| 221 | TSymbol *TSymbolTableLevel::find(const TString &name) const |
| 222 | { |
| 223 | tLevel::const_iterator it = level.find(name); |
| 224 | if (it == level.end()) |
| 225 | return 0; |
| 226 | else |
| 227 | return (*it).second; |
| 228 | } |
| 229 | |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 230 | TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) const |
| 231 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 232 | int level = currentLevel(); |
| 233 | TSymbol *symbol = nullptr; |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 234 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 235 | do |
| 236 | { |
| 237 | while((level == ESSL3_BUILTINS && shaderVersion != 300) || |
| 238 | (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels |
| 239 | { |
| 240 | --level; |
| 241 | } |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 242 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 243 | symbol = table[level]->find(name); |
| 244 | } |
| 245 | while(!symbol && --level >= 0); // Doesn't decrement level when a symbol was found |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 246 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 247 | if(builtIn) |
| 248 | { |
| 249 | *builtIn = (level <= LAST_BUILTIN_LEVEL); |
| 250 | } |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 251 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 252 | if(sameScope) |
| 253 | { |
| 254 | *sameScope = (level == currentLevel()); |
| 255 | } |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 256 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 257 | return symbol; |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const |
| 261 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 262 | for(int level = LAST_BUILTIN_LEVEL; level >= 0; --level) |
| 263 | { |
| 264 | while((level == ESSL3_BUILTINS && shaderVersion != 300) || |
| 265 | (level == ESSL1_BUILTINS && shaderVersion != 100)) // Skip version specific levels |
| 266 | { |
| 267 | --level; |
| 268 | } |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 269 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 270 | TSymbol *symbol = table[level]->find(name); |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 271 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 272 | if(symbol) |
| 273 | { |
| 274 | return symbol; |
| 275 | } |
| 276 | } |
Nicolas Capens | 3d7f6ed | 2015-02-18 16:34:50 -0500 | [diff] [blame] | 277 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 278 | return 0; |
Nicolas Capens | e285865 | 2015-02-18 15:30:51 -0500 | [diff] [blame] | 279 | } |
| 280 | |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 281 | TSymbol::TSymbol(const TSymbol& copyOf) |
| 282 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 283 | name = NewPoolTString(copyOf.name->c_str()); |
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 284 | } |