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 typecheck.
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 = new TIntermSequence();
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)116 TIntermConstantUnion *CreateFloatNode(float value)
117 {
118 TConstantUnion *u = new TConstantUnion[1];
119 u[0].setFConst(value);
120
121 TType type(EbtFloat, EbpUndefined, EvqConst, 1);
122 return new TIntermConstantUnion(u, type);
123 }
124
CreateIndexNode(int index)125 TIntermConstantUnion *CreateIndexNode(int index)
126 {
127 TConstantUnion *u = new TConstantUnion[1];
128 u[0].setIConst(index);
129
130 TType type(EbtInt, EbpUndefined, EvqConst, 1);
131 return new TIntermConstantUnion(u, type);
132 }
133
CreateBoolNode(bool value)134 TIntermConstantUnion *CreateBoolNode(bool value)
135 {
136 TConstantUnion *u = new TConstantUnion[1];
137 u[0].setBConst(value);
138
139 TType type(EbtBool, EbpUndefined, EvqConst, 1);
140 return new TIntermConstantUnion(u, type);
141 }
142
CreateTempVariable(TSymbolTable * symbolTable,const TType * type)143 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type)
144 {
145 ASSERT(symbolTable != nullptr);
146 // TODO(oetuaho): Might be useful to sanitize layout qualifier etc. on the type of the created
147 // variable. This might need to be done in other places as well.
148 return new TVariable(symbolTable, kEmptyImmutableString, type, SymbolType::AngleInternal);
149 }
150
CreateTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier)151 TVariable *CreateTempVariable(TSymbolTable *symbolTable, const TType *type, TQualifier qualifier)
152 {
153 ASSERT(symbolTable != nullptr);
154 if (type->getQualifier() == qualifier)
155 {
156 return CreateTempVariable(symbolTable, type);
157 }
158 TType *typeWithQualifier = new TType(*type);
159 typeWithQualifier->setQualifier(qualifier);
160 return CreateTempVariable(symbolTable, typeWithQualifier);
161 }
162
CreateTempSymbolNode(const TVariable * tempVariable)163 TIntermSymbol *CreateTempSymbolNode(const TVariable *tempVariable)
164 {
165 ASSERT(tempVariable->symbolType() == SymbolType::AngleInternal);
166 ASSERT(tempVariable->getType().getQualifier() == EvqTemporary ||
167 tempVariable->getType().getQualifier() == EvqConst ||
168 tempVariable->getType().getQualifier() == EvqGlobal);
169 return new TIntermSymbol(tempVariable);
170 }
171
CreateTempDeclarationNode(const TVariable * tempVariable)172 TIntermDeclaration *CreateTempDeclarationNode(const TVariable *tempVariable)
173 {
174 TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
175 tempDeclaration->appendDeclarator(CreateTempSymbolNode(tempVariable));
176 return tempDeclaration;
177 }
178
CreateTempInitDeclarationNode(const TVariable * tempVariable,TIntermTyped * initializer)179 TIntermDeclaration *CreateTempInitDeclarationNode(const TVariable *tempVariable,
180 TIntermTyped *initializer)
181 {
182 ASSERT(initializer != nullptr);
183 TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
184 TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
185 TIntermBinary *tempInit = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
186 tempDeclaration->appendDeclarator(tempInit);
187 return tempDeclaration;
188 }
189
CreateTempAssignmentNode(const TVariable * tempVariable,TIntermTyped * rightNode)190 TIntermBinary *CreateTempAssignmentNode(const TVariable *tempVariable, TIntermTyped *rightNode)
191 {
192 ASSERT(rightNode != nullptr);
193 TIntermSymbol *tempSymbol = CreateTempSymbolNode(tempVariable);
194 return new TIntermBinary(EOpAssign, tempSymbol, rightNode);
195 }
196
DeclareTempVariable(TSymbolTable * symbolTable,const TType * type,TQualifier qualifier,TIntermDeclaration ** declarationOut)197 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
198 const TType *type,
199 TQualifier qualifier,
200 TIntermDeclaration **declarationOut)
201 {
202 TVariable *variable = CreateTempVariable(symbolTable, type, qualifier);
203 *declarationOut = CreateTempDeclarationNode(variable);
204 return variable;
205 }
206
DeclareTempVariable(TSymbolTable * symbolTable,TIntermTyped * initializer,TQualifier qualifier,TIntermDeclaration ** declarationOut)207 TVariable *DeclareTempVariable(TSymbolTable *symbolTable,
208 TIntermTyped *initializer,
209 TQualifier qualifier,
210 TIntermDeclaration **declarationOut)
211 {
212 TVariable *variable =
213 CreateTempVariable(symbolTable, new TType(initializer->getType()), qualifier);
214 *declarationOut = CreateTempInitDeclarationNode(variable, initializer);
215 return variable;
216 }
217
DeclareInterfaceBlock(TIntermBlock * root,TSymbolTable * symbolTable,TFieldList * fieldList,TQualifier qualifier,const TMemoryQualifier & memoryQualifier,uint32_t arraySize,const ImmutableString & blockTypeName,const ImmutableString & blockVariableName)218 const TVariable *DeclareInterfaceBlock(TIntermBlock *root,
219 TSymbolTable *symbolTable,
220 TFieldList *fieldList,
221 TQualifier qualifier,
222 const TMemoryQualifier &memoryQualifier,
223 uint32_t arraySize,
224 const ImmutableString &blockTypeName,
225 const ImmutableString &blockVariableName)
226 {
227 // Define an interface block.
228 TLayoutQualifier layoutQualifier = TLayoutQualifier::Create();
229 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
230 symbolTable, blockTypeName, fieldList, layoutQualifier, SymbolType::AngleInternal);
231
232 // Turn the inteface block into a declaration.
233 TType *interfaceBlockType = new TType(interfaceBlock, qualifier, layoutQualifier);
234 interfaceBlockType->setMemoryQualifier(memoryQualifier);
235 if (arraySize > 0)
236 {
237 interfaceBlockType->makeArray(arraySize);
238 }
239
240 TIntermDeclaration *interfaceBlockDecl = new TIntermDeclaration;
241 TVariable *interfaceBlockVar = new TVariable(symbolTable, blockVariableName, interfaceBlockType,
242 SymbolType::AngleInternal);
243 TIntermSymbol *interfaceBlockDeclarator = new TIntermSymbol(interfaceBlockVar);
244 interfaceBlockDecl->appendDeclarator(interfaceBlockDeclarator);
245
246 // Insert the declarations before the first function.
247 TIntermSequence *insertSequence = new TIntermSequence;
248 insertSequence->push_back(interfaceBlockDecl);
249
250 size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(root);
251 root->insertChildNodes(firstFunctionIndex, *insertSequence);
252
253 return interfaceBlockVar;
254 }
255
EnsureBlock(TIntermNode * node)256 TIntermBlock *EnsureBlock(TIntermNode *node)
257 {
258 if (node == nullptr)
259 return nullptr;
260 TIntermBlock *blockNode = node->getAsBlock();
261 if (blockNode != nullptr)
262 return blockNode;
263
264 blockNode = new TIntermBlock();
265 blockNode->setLine(node->getLine());
266 blockNode->appendStatement(node);
267 return blockNode;
268 }
269
ReferenceGlobalVariable(const ImmutableString & name,const TSymbolTable & symbolTable)270 TIntermSymbol *ReferenceGlobalVariable(const ImmutableString &name, const TSymbolTable &symbolTable)
271 {
272 const TVariable *var = static_cast<const TVariable *>(symbolTable.findGlobal(name));
273 ASSERT(var);
274 return new TIntermSymbol(var);
275 }
276
ReferenceBuiltInVariable(const ImmutableString & name,const TSymbolTable & symbolTable,int shaderVersion)277 TIntermSymbol *ReferenceBuiltInVariable(const ImmutableString &name,
278 const TSymbolTable &symbolTable,
279 int shaderVersion)
280 {
281 const TVariable *var =
282 static_cast<const TVariable *>(symbolTable.findBuiltIn(name, shaderVersion));
283 ASSERT(var);
284 return new TIntermSymbol(var);
285 }
286
CreateBuiltInFunctionCallNode(const char * name,TIntermSequence * arguments,const TSymbolTable & symbolTable,int shaderVersion)287 TIntermTyped *CreateBuiltInFunctionCallNode(const char *name,
288 TIntermSequence *arguments,
289 const TSymbolTable &symbolTable,
290 int shaderVersion)
291 {
292 const TFunction *fn = LookUpBuiltInFunction(name, arguments, symbolTable, shaderVersion);
293 ASSERT(fn);
294 TOperator op = fn->getBuiltInOp();
295 if (op != EOpCallBuiltInFunction && arguments->size() == 1)
296 {
297 return new TIntermUnary(op, arguments->at(0)->getAsTyped(), fn);
298 }
299 return TIntermAggregate::CreateBuiltInFunctionCall(*fn, arguments);
300 }
301
302 } // namespace sh
303