xref: /aosp_15_r20/external/angle/src/compiler/translator/tree_util/IntermNode_util.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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     // Make sure as a constructor, the type does not inherit qualifiers that are otherwise specified
54     // on interface blocks and varyings.
55     constType.setInvariant(false);
56     constType.setPrecise(false);
57     constType.setInterpolant(false);
58     constType.setMemoryQualifier(TMemoryQualifier::Create());
59     constType.setLayoutQualifier(TLayoutQualifier::Create());
60     constType.setInterfaceBlock(nullptr);
61 
62     if (!type.isArray() && type.getBasicType() != EbtStruct)
63     {
64         size_t size       = constType.getObjectSize();
65         TConstantUnion *u = new TConstantUnion[size];
66         for (size_t i = 0; i < size; ++i)
67         {
68             switch (type.getBasicType())
69             {
70                 case EbtFloat:
71                     u[i].setFConst(0.0f);
72                     break;
73                 case EbtInt:
74                     u[i].setIConst(0);
75                     break;
76                 case EbtUInt:
77                     u[i].setUConst(0u);
78                     break;
79                 case EbtBool:
80                     u[i].setBConst(false);
81                     break;
82                 default:
83                     // CreateZeroNode is called by ParseContext that keeps parsing even when an
84                     // error occurs, so it is possible for CreateZeroNode to be called with
85                     // non-basic types. This happens only on error condition but CreateZeroNode
86                     // needs to return a value with the correct type to continue the type check.
87                     // That's why we handle non-basic type by setting whatever value, we just need
88                     // the type to be right.
89                     u[i].setIConst(42);
90                     break;
91             }
92         }
93 
94         TIntermConstantUnion *node = new TIntermConstantUnion(u, constType);
95         return node;
96     }
97 
98     TIntermSequence arguments;
99 
100     if (type.isArray())
101     {
102         TType elementType(type);
103         elementType.toArrayElementType();
104 
105         size_t arraySize = type.getOutermostArraySize();
106         for (size_t i = 0; i < arraySize; ++i)
107         {
108             arguments.push_back(CreateZeroNode(elementType));
109         }
110     }
111     else
112     {
113         ASSERT(type.getBasicType() == EbtStruct);
114 
115         const TStructure *structure = type.getStruct();
116         for (const auto &field : structure->fields())
117         {
118             arguments.push_back(CreateZeroNode(*field->type()));
119         }
120     }
121 
122     return TIntermAggregate::CreateConstructor(constType, &arguments);
123 }
124 
CreateFloatNode(float value,TPrecision precision)125 TIntermConstantUnion *CreateFloatNode(float value, TPrecision precision)
126 {
127     TConstantUnion *u = new TConstantUnion[1];
128     u[0].setFConst(value);
129 
130     TType type(EbtFloat, precision, EvqConst, 1);
131     return new TIntermConstantUnion(u, type);
132 }
133 
CreateVecNode(const float values[],unsigned int vecSize,TPrecision precision)134 TIntermConstantUnion *CreateVecNode(const float values[],
135                                     unsigned int vecSize,
136                                     TPrecision precision)
137 {
138     TConstantUnion *u = new TConstantUnion[vecSize];
139     for (unsigned int channel = 0; channel < vecSize; ++channel)
140     {
141         u[channel].setFConst(values[channel]);
142     }
143 
144     TType type(EbtFloat, precision, EvqConst, static_cast<uint8_t>(vecSize));
145     return new TIntermConstantUnion(u, type);
146 }
147 
CreateUVecNode(const unsigned int values[],unsigned int vecSize,TPrecision precision)148 TIntermConstantUnion *CreateUVecNode(const unsigned int values[],
149                                      unsigned int vecSize,
150                                      TPrecision precision)
151 {
152     TConstantUnion *u = new TConstantUnion[vecSize];
153     for (unsigned int channel = 0; channel < vecSize; ++channel)
154     {
155         u[channel].setUConst(values[channel]);
156     }
157 
158     TType type(EbtUInt, precision, EvqConst, static_cast<uint8_t>(vecSize));
159     return new TIntermConstantUnion(u, type);
160 }
161 
CreateIndexNode(int index)162 TIntermConstantUnion *CreateIndexNode(int index)
163 {
164     TConstantUnion *u = new TConstantUnion[1];
165     u[0].setIConst(index);
166 
167     TType type(EbtInt, EbpHigh, EvqConst, 1);
168     return new TIntermConstantUnion(u, type);
169 }
170 
CreateUIntNode(unsigned int value)171 TIntermConstantUnion *CreateUIntNode(unsigned int value)
172 {
173     TConstantUnion *u = new TConstantUnion[1];
174     u[0].setUConst(value);
175 
176     TType type(EbtUInt, EbpHigh, EvqConst, 1);
177     return new TIntermConstantUnion(u, type);
178 }
179 
CreateBoolNode(bool value)180 TIntermConstantUnion *CreateBoolNode(bool value)
181 {
182     TConstantUnion *u = new TConstantUnion[1];
183     u[0].setBConst(value);
184 
185     TType type(EbtBool, EbpUndefined, EvqConst, 1);
186     return new TIntermConstantUnion(u, type);
187 }
188 
CreateTempVariable(TSymbolTable * symbolTable,const TType * type)189 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type)
190 {
191     ASSERT(symbolTable != nullptr);
192     return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
193 }
194 
CreateTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier)195 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier)
196 {
197     ASSERT(symbolTable != nullptr);
198     if (type->getQualifier() != qualifier || type->getInterfaceBlock() != nullptr)
199     {
200         TType *newType = new TType(*type);
201         newType->setQualifier(qualifier);
202         newType->setInterfaceBlock(nullptr);
203         type = newType;
204     }
205     return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
206 }
207 
CreateTempSymbolNode(const TVariable * tempVariable)208 TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable)
209 {
210     ASSERT(tempVariable->symbolType() == SymbolType::AngleInternal);
211     ASSERT(tempVariable->getType().getQualifier() == EvqTemporary ||
212            tempVariable->getType().getQualifier() == EvqConst ||
213            tempVariable->getType().getQualifier() == EvqGlobal);
214     return new TIntermSymbol(tempVariable);
215 }
216 
CreateTempDeclarationNode(const TVariable * tempVariable)217 TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable)
218 {
219     TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
220     tempDeclaration->appendDeclarator(CreateTempSymbolNode(tempVariable));
221     return tempDeclaration;
222 }
223 
CreateTempInitDeclarationNode(const TVariable * tempVariable,TIntermTyped * initializer)224 TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable,
225                                                   TIntermTyped *initializer)
226 {
227     ASSERT(initializer != nullptr);
228     TIntermSymbol *tempSymbol           = CreateTempSymbolNode(tempVariable);
229     TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
230     TIntermBinary *tempInit             = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
231     tempDeclaration->appendDeclarator(tempInit);
232     return tempDeclaration;
233 }
234 
CreateTempAssignmentNode(const TVariable * tempVariable,TIntermTyped * rightNode)235 TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode)
236 {
237     ASSERT(rightNode != nullptr);
238     TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
239     return new TIntermBinary(EOpAssign, tempSymbol, rightNode);
240 }
241 
DeclareTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier,TIntermDeclaration ** declarationOut)242 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
243                                const TType *type,
244                                TQualifier qualifier,
245                                TIntermDeclaration **declarationOut)
246 {
247     TVariable *variable = CreateTempVariable(symbolTable, type, qualifier);
248     *declarationOut     = CreateTempDeclarationNode(variable);
249     return variable;
250 }
251 
DeclareTempVariable(TSymbolTable * symbolTable,TIntermTyped * initializer,TQualifier qualifier,TIntermDeclaration ** declarationOut)252 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
253                                TIntermTyped *initializer,
254                                TQualifier qualifier,
255                                TIntermDeclaration **declarationOut)
256 {
257     TVariable *variable =
258         CreateTempVariable(symbolTable, new TType(initializer->getType()), qualifier);
259     *declarationOut = CreateTempInitDeclarationNode(variable, initializer);
260     return variable;
261 }
262 
DeclareStructure(TIntermBlock * root,TSymbolTable * symbolTable,TFieldList * fieldList,TQualifier qualifier,const TMemoryQualifier & memoryQualifier,uint32_t arraySize,const ImmutableString & structTypeName,const ImmutableString * structInstanceName)263 std::pair<const TVariable *, const TVariable *> DeclareStructure(
264     TIntermBlock *root,
265     TSymbolTable *symbolTable,
266     TFieldList *fieldList,
267     TQualifier qualifier,
268     const TMemoryQualifier &memoryQualifier,
269     uint32_t arraySize,
270     const ImmutableString &structTypeName,
271     const ImmutableString *structInstanceName)
272 {
273     TStructure *structure =
274         new TStructure(symbolTable, structTypeName, fieldList, SymbolType::AngleInternal);
275 
276     auto makeStructureType = [&](bool isStructSpecifier) {
277         TType *structureType = new TType(structure, isStructSpecifier);
278         structureType->setQualifier(qualifier);
279         structureType->setMemoryQualifier(memoryQualifier);
280         if (arraySize > 0)
281         {
282             structureType->makeArray(arraySize);
283         }
284         return structureType;
285     };
286 
287     TIntermSequence insertSequence;
288 
289     TVariable *typeVar = new TVariable(symbolTable, kEmptyImmutableString, makeStructureType(true),
290                                        SymbolType::Empty);
291     insertSequence.push_back(new TIntermDeclaration{typeVar});
292 
293     TVariable *instanceVar = nullptr;
294     if (structInstanceName)
295     {
296         instanceVar = new TVariable(symbolTable, *structInstanceName, makeStructureType(false),
297                                     SymbolType::AngleInternal);
298         insertSequence.push_back(new TIntermDeclaration{instanceVar});
299     }
300 
301     size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
302     root->insertChildNodes(firstFunctionIndex, insertSequence);
303 
304     return {typeVar, instanceVar};
305 }
306 
DeclareInterfaceBlock(TIntermBlock * root,TSymbolTable * symbolTable,TFieldList * fieldList,TQualifier qualifier,const TLayoutQualifier & layoutQualifier,const TMemoryQualifier & memoryQualifier,uint32_t arraySize,const ImmutableString & blockTypeName,const ImmutableString & blockVariableName)307 const TVariable *DeclareInterfaceBlock(TIntermBlock *root,
308                                        TSymbolTable *symbolTable,
309                                        TFieldList *fieldList,
310                                        TQualifier qualifier,
311                                        const TLayoutQualifier &layoutQualifier,
312                                        const TMemoryQualifier &memoryQualifier,
313                                        uint32_t arraySize,
314                                        const ImmutableString &blockTypeName,
315                                        const ImmutableString &blockVariableName)
316 {
317     // Define an interface block.
318     TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
319         symbolTable, blockTypeName, fieldList, layoutQualifier, SymbolType::AngleInternal);
320 
321     // Turn the inteface block into a declaration.
322     TType *interfaceBlockType = new TType(interfaceBlock, qualifier, layoutQualifier);
323     interfaceBlockType->setMemoryQualifier(memoryQualifier);
324     if (arraySize > 0)
325     {
326         interfaceBlockType->makeArray(arraySize);
327     }
328 
329     TIntermDeclaration *interfaceBlockDecl = new TIntermDeclaration;
330     TVariable *interfaceBlockVar =
331         new TVariable(symbolTable, blockVariableName, interfaceBlockType,
332                       blockVariableName.empty() ? SymbolType::Empty : SymbolType::AngleInternal);
333     TIntermSymbol *interfaceBlockDeclarator = new TIntermSymbol(interfaceBlockVar);
334     interfaceBlockDecl->appendDeclarator(interfaceBlockDeclarator);
335 
336     // Insert the declarations before the first function.
337     TIntermSequence insertSequence;
338     insertSequence.push_back(interfaceBlockDecl);
339 
340     size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
341     root->insertChildNodes(firstFunctionIndex, insertSequence);
342 
343     return interfaceBlockVar;
344 }
345 
EnsureBlock(TIntermNode * node)346 TIntermBlock *EnsureBlock(TIntermNode *node)
347 {
348     if (node == nullptr)
349         return nullptr;
350     TIntermBlock *blockNode = node->getAsBlock();
351     if (blockNode != nullptr)
352     {
353         return blockNode;
354     }
355     blockNode = new TIntermBlock();
356     blockNode->setLine(node->getLine());
357     blockNode->appendStatement(node);
358     return blockNode;
359 }
360 
EnsureLoopBodyBlock(TIntermNode * node)361 TIntermBlock *EnsureLoopBodyBlock(TIntermNode *node)
362 {
363     if (node == nullptr)
364     {
365         return new TIntermBlock();
366     }
367     return EnsureBlock(node);
368 }
369 
ReferenceGlobalVariable(const ImmutableString & name,const TSymbolTable & symbolTable)370 TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name, const TSymbolTable &symbolTable)
371 {
372     const TSymbol *symbol = symbolTable.findGlobal(name);
373     ASSERT(symbol && symbol->isVariable());
374     return new TIntermSymbol(static_cast<const TVariable *>(symbol));
375 }
376 
ReferenceBuiltInVariable(const ImmutableString & name,const TSymbolTable & symbolTable,int shaderVersion)377 TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name,
378                                         const TSymbolTable &symbolTable,
379                                         int shaderVersion)
380 {
381     const TVariable *var =
382         static_cast<const TVariable *>(symbolTable.findBuiltIn(name, shaderVersion));
383     ASSERT(var);
384     return new TIntermSymbol(var);
385 }
386 
CreateBuiltInFunctionCallNode(const char * name,TIntermSequence * arguments,const TSymbolTable & symbolTable,int shaderVersion)387 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
388                                             TIntermSequence *arguments,
389                                             const TSymbolTable &symbolTable,
390                                             int shaderVersion)
391 {
392     const TFunction *fn = LookUpBuiltInFunction(name, arguments, symbolTable, shaderVersion);
393     ASSERT(fn);
394     TOperator op = fn->getBuiltInOp();
395     if (BuiltInGroup::IsMath(op) && arguments->size() == 1)
396     {
397         return new TIntermUnary(op, arguments->at(0)->getAsTyped(), fn);
398     }
399     return TIntermAggregate::CreateBuiltInFunctionCall(*fn, arguments);
400 }
401 
CreateBuiltInFunctionCallNode(const char * name,const std::initializer_list<TIntermNode * > & arguments,const TSymbolTable & symbolTable,int shaderVersion)402 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
403                                             const std::initializer_list<TIntermNode *> &arguments,
404                                             const TSymbolTable &symbolTable,
405                                             int shaderVersion)
406 {
407     TIntermSequence argSequence(arguments);
408     return CreateBuiltInFunctionCallNode(name, &argSequence, symbolTable, shaderVersion);
409 }
410 
CreateBuiltInUnaryFunctionCallNode(const char * name,TIntermTyped * argument,const TSymbolTable & symbolTable,int shaderVersion)411 TIntermTyped *CreateBuiltInUnaryFunctionCallNode(const char *name,
412                                                  TIntermTyped *argument,
413                                                  const TSymbolTable &symbolTable,
414                                                  int shaderVersion)
415 {
416     return CreateBuiltInFunctionCallNode(name, {argument}, symbolTable, shaderVersion);
417 }
418 
419 // Returns true if a block ends in a branch (break, continue, return, etc).  This is only correct
420 // after PruneNoOps, because it expects empty blocks after a branch to have been already pruned,
421 // i.e. a block can only end in a branch if its last statement is a branch or is a block ending in
422 // branch.
EndsInBranch(TIntermBlock * block)423 bool EndsInBranch(TIntermBlock *block)
424 {
425     while (block != nullptr)
426     {
427         // Get the last statement of the block.
428         TIntermSequence &statements = *block->getSequence();
429         if (statements.empty())
430         {
431             return false;
432         }
433 
434         TIntermNode *lastStatement = statements.back();
435 
436         // If it's a branch itself, we have the answer.
437         if (lastStatement->getAsBranchNode())
438         {
439             return true;
440         }
441 
442         // Otherwise, see if it's a block that ends in a branch
443         block = lastStatement->getAsBlock();
444     }
445 
446     return false;
447 }
448 
449 }  // namespace sh
450