• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // IntermNode_util.cpp: High-level utilities for creating AST nodes and node hierarchies. Mostly
7 // meant to be used in AST transforms.
8 
9 #include "compiler/translator/tree_util/IntermNode_util.h"
10 
11 #include "compiler/translator/FunctionLookup.h"
12 #include "compiler/translator/SymbolTable.h"
13 
14 namespace sh
15 {
16 
17 namespace
18 {
19 
LookUpBuiltInFunction(const char * name,const TIntermSequence * arguments,const TSymbolTable & symbolTable,int shaderVersion)20 const TFunction *LookUpBuiltInFunction(const char *name,
21                                        const TIntermSequence *arguments,
22                                        const TSymbolTable &symbolTable,
23                                        int shaderVersion)
24 {
25     const ImmutableString &mangledName = TFunctionLookup::GetMangledName(name, *arguments);
26     const TSymbol *symbol              = symbolTable.findBuiltIn(mangledName, shaderVersion);
27     if (symbol)
28     {
29         ASSERT(symbol->isFunction());
30         return static_cast<const TFunction *>(symbol);
31     }
32     return nullptr;
33 }
34 
35 }  // anonymous namespace
36 
CreateInternalFunctionPrototypeNode(const TFunction & func)37 TIntermFunctionPrototype *CreateInternalFunctionPrototypeNode(const TFunction &func)
38 {
39     return new TIntermFunctionPrototype(&func);
40 }
41 
CreateInternalFunctionDefinitionNode(const TFunction & func,TIntermBlock * functionBody)42 TIntermFunctionDefinition *CreateInternalFunctionDefinitionNode(const TFunction &func,
43                                                                 TIntermBlock *functionBody)
44 {
45     return new TIntermFunctionDefinition(new TIntermFunctionPrototype(&func), functionBody);
46 }
47 
CreateZeroNode(const TType & type)48 TIntermTyped *CreateZeroNode(const TType &type)
49 {
50     TType constType(type);
51     constType.setQualifier(EvqConst);
52 
53     if (!type.isArray() && type.getBasicType() != EbtStruct)
54     {
55         size_t size       = constType.getObjectSize();
56         TConstantUnion *u = new TConstantUnion[size];
57         for (size_t i = 0; i < size; ++i)
58         {
59             switch (type.getBasicType())
60             {
61                 case EbtFloat:
62                     u[i].setFConst(0.0f);
63                     break;
64                 case EbtInt:
65                     u[i].setIConst(0);
66                     break;
67                 case EbtUInt:
68                     u[i].setUConst(0u);
69                     break;
70                 case EbtBool:
71                     u[i].setBConst(false);
72                     break;
73                 default:
74                     // CreateZeroNode is called by ParseContext that keeps parsing even when an
75                     // error occurs, so it is possible for CreateZeroNode to be called with
76                     // non-basic types. This happens only on error condition but CreateZeroNode
77                     // needs to return a value with the correct type to continue the type check.
78                     // That's why we handle non-basic type by setting whatever value, we just need
79                     // the type to be right.
80                     u[i].setIConst(42);
81                     break;
82             }
83         }
84 
85         TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
86         return node;
87     }
88 
89     TIntermSequence arguments;
90 
91     if (type.isArray())
92     {
93         TType elementType(type);
94         elementType.toArrayElementType();
95 
96         size_t arraySize = type.getOutermostArraySize();
97         for (size_t i = 0; i < arraySize; ++i)
98         {
99             arguments.push_back(CreateZeroNode(elementType));
100         }
101     }
102     else
103     {
104         ASSERT(type.getBasicType() == EbtStruct);
105 
106         const TStructure *structure = type.getStruct();
107         for (const auto &field : structure->fields())
108         {
109             arguments.push_back(CreateZeroNode(*field->type()));
110         }
111     }
112 
113     return TIntermAggregate::CreateConstructor(constType, &arguments);
114 }
115 
CreateFloatNode(float value,TPrecision precision)116 TIntermConstantUnion *CreateFloatNode(float value, TPrecision precision)
117 {
118     TConstantUnion *u = new TConstantUnion[1];
119     u[0].setFConst(value);
120 
121     TType type(EbtFloat, precision, EvqConst, 1);
122     return new TIntermConstantUnion(u, type);
123 }
124 
CreateVecNode(const float values[],unsigned int vecSize,TPrecision precision)125 TIntermConstantUnion *CreateVecNode(const float values[],
126                                     unsigned int vecSize,
127                                     TPrecision precision)
128 {
129     TConstantUnion *u = new TConstantUnion[vecSize];
130     for (unsigned int channel = 0; channel < vecSize; ++channel)
131     {
132         u[channel].setFConst(values[channel]);
133     }
134 
135     TType type(EbtFloat, precision, EvqConst, static_cast<uint8_t>(vecSize));
136     return new TIntermConstantUnion(u, type);
137 }
138 
CreateIndexNode(int index)139 TIntermConstantUnion *CreateIndexNode(int index)
140 {
141     TConstantUnion *u = new TConstantUnion[1];
142     u[0].setIConst(index);
143 
144     TType type(EbtInt, EbpHigh, EvqConst, 1);
145     return new TIntermConstantUnion(u, type);
146 }
147 
CreateUIntNode(unsigned int value)148 TIntermConstantUnion *CreateUIntNode(unsigned int value)
149 {
150     TConstantUnion *u = new TConstantUnion[1];
151     u[0].setUConst(value);
152 
153     TType type(EbtUInt, EbpHigh, EvqConst, 1);
154     return new TIntermConstantUnion(u, type);
155 }
156 
CreateBoolNode(bool value)157 TIntermConstantUnion *CreateBoolNode(bool value)
158 {
159     TConstantUnion *u = new TConstantUnion[1];
160     u[0].setBConst(value);
161 
162     TType type(EbtBool, EbpUndefined, EvqConst, 1);
163     return new TIntermConstantUnion(u, type);
164 }
165 
CreateTempVariable(TSymbolTable * symbolTable,const TType * type)166 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type)
167 {
168     ASSERT(symbolTable != nullptr);
169     // TODO(oetuaho): Might be useful to sanitize layout qualifier etc. on the type of the created
170     // variable. This might need to be done in other places as well.
171     return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
172 }
173 
CreateTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier)174 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier)
175 {
176     ASSERT(symbolTable != nullptr);
177     if (type->getQualifier() == qualifier)
178     {
179         return CreateTempVariable(symbolTable, type);
180     }
181     TType *typeWithQualifier = new TType(*type);
182     typeWithQualifier->setQualifier(qualifier);
183     return CreateTempVariable(symbolTable, typeWithQualifier);
184 }
185 
CreateTempSymbolNode(const TVariable * tempVariable)186 TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable)
187 {
188     ASSERT(tempVariable->symbolType() == SymbolType::AngleInternal);
189     ASSERT(tempVariable->getType().getQualifier() == EvqTemporary ||
190            tempVariable->getType().getQualifier() == EvqConst ||
191            tempVariable->getType().getQualifier() == EvqGlobal);
192     return new TIntermSymbol(tempVariable);
193 }
194 
CreateTempDeclarationNode(const TVariable * tempVariable)195 TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable)
196 {
197     TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
198     tempDeclaration->appendDeclarator(CreateTempSymbolNode(tempVariable));
199     return tempDeclaration;
200 }
201 
CreateTempInitDeclarationNode(const TVariable * tempVariable,TIntermTyped * initializer)202 TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable,
203                                                   TIntermTyped *initializer)
204 {
205     ASSERT(initializer != nullptr);
206     TIntermSymbol *tempSymbol           = CreateTempSymbolNode(tempVariable);
207     TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
208     TIntermBinary *tempInit             = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
209     tempDeclaration->appendDeclarator(tempInit);
210     return tempDeclaration;
211 }
212 
CreateTempAssignmentNode(const TVariable * tempVariable,TIntermTyped * rightNode)213 TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode)
214 {
215     ASSERT(rightNode != nullptr);
216     TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
217     return new TIntermBinary(EOpAssign, tempSymbol, rightNode);
218 }
219 
DeclareTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier,TIntermDeclaration ** declarationOut)220 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
221                                const TType *type,
222                                TQualifier qualifier,
223                                TIntermDeclaration **declarationOut)
224 {
225     TVariable *variable = CreateTempVariable(symbolTable, type, qualifier);
226     *declarationOut     = CreateTempDeclarationNode(variable);
227     return variable;
228 }
229 
DeclareTempVariable(TSymbolTable * symbolTable,TIntermTyped * initializer,TQualifier qualifier,TIntermDeclaration ** declarationOut)230 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
231                                TIntermTyped *initializer,
232                                TQualifier qualifier,
233                                TIntermDeclaration **declarationOut)
234 {
235     TVariable *variable =
236         CreateTempVariable(symbolTable, new TType(initializer->getType()), qualifier);
237     *declarationOut = CreateTempInitDeclarationNode(variable, initializer);
238     return variable;
239 }
240 
DeclareStructure(TIntermBlock * root,TSymbolTable * symbolTable,TFieldList * fieldList,TQualifier qualifier,const TMemoryQualifier & memoryQualifier,uint32_t arraySize,const ImmutableString & structTypeName,const ImmutableString * structInstanceName)241 std::pair<const TVariable *, const TVariable *> DeclareStructure(
242     TIntermBlock *root,
243     TSymbolTable *symbolTable,
244     TFieldList *fieldList,
245     TQualifier qualifier,
246     const TMemoryQualifier &memoryQualifier,
247     uint32_t arraySize,
248     const ImmutableString &structTypeName,
249     const ImmutableString *structInstanceName)
250 {
251     TStructure *structure =
252         new TStructure(symbolTable, structTypeName, fieldList, SymbolType::AngleInternal);
253 
254     auto makeStructureType = [&](bool isStructSpecifier) {
255         TType *structureType = new TType(structure, isStructSpecifier);
256         structureType->setQualifier(qualifier);
257         structureType->setMemoryQualifier(memoryQualifier);
258         if (arraySize > 0)
259         {
260             structureType->makeArray(arraySize);
261         }
262         return structureType;
263     };
264 
265     TIntermSequence insertSequence;
266 
267     TVariable *typeVar = new TVariable(symbolTable, kEmptyImmutableString, makeStructureType(true),
268                                        SymbolType::Empty);
269     insertSequence.push_back(new TIntermDeclaration{typeVar});
270 
271     TVariable *instanceVar = nullptr;
272     if (structInstanceName)
273     {
274         instanceVar = new TVariable(symbolTable, *structInstanceName, makeStructureType(false),
275                                     SymbolType::AngleInternal);
276         insertSequence.push_back(new TIntermDeclaration{instanceVar});
277     }
278 
279     size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
280     root->insertChildNodes(firstFunctionIndex, insertSequence);
281 
282     return {typeVar, instanceVar};
283 }
284 
DeclareInterfaceBlock(TIntermBlock * root,TSymbolTable * symbolTable,TFieldList * fieldList,TQualifier qualifier,const TLayoutQualifier & layoutQualifier,const TMemoryQualifier & memoryQualifier,uint32_t arraySize,const ImmutableString & blockTypeName,const ImmutableString & blockVariableName)285 const TVariable *DeclareInterfaceBlock(TIntermBlock *root,
286                                        TSymbolTable *symbolTable,
287                                        TFieldList *fieldList,
288                                        TQualifier qualifier,
289                                        const TLayoutQualifier &layoutQualifier,
290                                        const TMemoryQualifier &memoryQualifier,
291                                        uint32_t arraySize,
292                                        const ImmutableString &blockTypeName,
293                                        const ImmutableString &blockVariableName)
294 {
295     // Define an interface block.
296     TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
297         symbolTable, blockTypeName, fieldList, layoutQualifier, SymbolType::AngleInternal);
298 
299     // Turn the inteface block into a declaration.
300     TType *interfaceBlockType = new TType(interfaceBlock, qualifier, layoutQualifier);
301     interfaceBlockType->setMemoryQualifier(memoryQualifier);
302     if (arraySize > 0)
303     {
304         interfaceBlockType->makeArray(arraySize);
305     }
306 
307     TIntermDeclaration *interfaceBlockDecl = new TIntermDeclaration;
308     TVariable *interfaceBlockVar =
309         new TVariable(symbolTable, blockVariableName, interfaceBlockType,
310                       blockVariableName.empty() ? SymbolType::Empty : SymbolType::AngleInternal);
311     TIntermSymbol *interfaceBlockDeclarator = new TIntermSymbol(interfaceBlockVar);
312     interfaceBlockDecl->appendDeclarator(interfaceBlockDeclarator);
313 
314     // Insert the declarations before the first function.
315     TIntermSequence insertSequence;
316     insertSequence.push_back(interfaceBlockDecl);
317 
318     size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
319     root->insertChildNodes(firstFunctionIndex, insertSequence);
320 
321     return interfaceBlockVar;
322 }
323 
EnsureBlock(TIntermNode * node)324 TIntermBlock *EnsureBlock(TIntermNode *node)
325 {
326     if (node == nullptr)
327         return nullptr;
328     TIntermBlock *blockNode = node->getAsBlock();
329     if (blockNode != nullptr)
330         return blockNode;
331 
332     blockNode = new TIntermBlock();
333     blockNode->setLine(node->getLine());
334     blockNode->appendStatement(node);
335     return blockNode;
336 }
337 
ReferenceGlobalVariable(const ImmutableString & name,const TSymbolTable & symbolTable)338 TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name, const TSymbolTable &symbolTable)
339 {
340     const TSymbol *symbol = symbolTable.findGlobal(name);
341     ASSERT(symbol && symbol->isVariable());
342     return new TIntermSymbol(static_cast<const TVariable *>(symbol));
343 }
344 
ReferenceBuiltInVariable(const ImmutableString & name,const TSymbolTable & symbolTable,int shaderVersion)345 TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name,
346                                         const TSymbolTable &symbolTable,
347                                         int shaderVersion)
348 {
349     const TVariable *var =
350         static_cast<const TVariable *>(symbolTable.findBuiltIn(name, shaderVersion));
351     ASSERT(var);
352     return new TIntermSymbol(var);
353 }
354 
CreateBuiltInFunctionCallNode(const char * name,TIntermSequence * arguments,const TSymbolTable & symbolTable,int shaderVersion)355 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
356                                             TIntermSequence *arguments,
357                                             const TSymbolTable &symbolTable,
358                                             int shaderVersion)
359 {
360     const TFunction *fn = LookUpBuiltInFunction(name, arguments, symbolTable, shaderVersion);
361     ASSERT(fn);
362     TOperator op = fn->getBuiltInOp();
363     if (BuiltInGroup::IsMath(op) && arguments->size() == 1)
364     {
365         return new TIntermUnary(op, arguments->at(0)->getAsTyped(), fn);
366     }
367     return TIntermAggregate::CreateBuiltInFunctionCall(*fn, arguments);
368 }
369 
CreateBuiltInUnaryFunctionCallNode(const char * name,TIntermTyped * argument,const TSymbolTable & symbolTable,int shaderVersion)370 TIntermTyped *CreateBuiltInUnaryFunctionCallNode(const char *name,
371                                                  TIntermTyped *argument,
372                                                  const TSymbolTable &symbolTable,
373                                                  int shaderVersion)
374 {
375     TIntermSequence seq = {argument};
376     return CreateBuiltInFunctionCallNode(name, &seq, symbolTable, shaderVersion);
377 }
378 
379 // Returns true if a block ends in a branch (break, continue, return, etc).  This is only correct
380 // after PruneNoOps, because it expects empty blocks after a branch to have been already pruned,
381 // i.e. a block can only end in a branch if its last statement is a branch or is a block ending in
382 // branch.
EndsInBranch(TIntermBlock * block)383 bool EndsInBranch(TIntermBlock *block)
384 {
385     while (block != nullptr)
386     {
387         // Get the last statement of the block.
388         TIntermSequence &statements = *block->getSequence();
389         if (statements.empty())
390         {
391             return false;
392         }
393 
394         TIntermNode *lastStatement = statements.back();
395 
396         // If it's a branch itself, we have the answer.
397         if (lastStatement->getAsBranchNode())
398         {
399             return true;
400         }
401 
402         // Otherwise, see if it's a block that ends in a branch
403         block = lastStatement->getAsBlock();
404     }
405 
406     return false;
407 }
408 
409 }  // namespace sh
410