Commit 180f43c6 by Olli Etuaho Committed by Commit Bot

Refactor creating temporary symbols

This makes creating temporary symbols easy also outside of traversers. This will be needed for improving variable initialization. TEST=angle_unittests BUG=chromium:735497 Change-Id: Id048fc338e0be6c76bb6f082421ae106618e5003 Reviewed-on: https://chromium-review.googlesource.com/707194 Commit-Queue: Olli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarCorentin Wallez <cwallez@chromium.org>
parent d63d0007
...@@ -170,6 +170,37 @@ TIntermConstantUnion *CreateBoolNode(bool value) ...@@ -170,6 +170,37 @@ TIntermConstantUnion *CreateBoolNode(bool value)
return node; return node;
} }
TIntermSymbol *CreateTempSymbolNode(const TSymbolUniqueId &id,
const TType &type,
TQualifier qualifier)
{
TInfoSinkBase symbolNameOut;
symbolNameOut << "s" << id.get();
TString symbolName = symbolNameOut.c_str();
TIntermSymbol *node = new TIntermSymbol(id.get(), symbolName, type);
node->setInternal(true);
ASSERT(qualifier == EvqTemporary || qualifier == EvqConst || qualifier == EvqGlobal);
node->getTypePointer()->setQualifier(qualifier);
// TODO(oetuaho): Might be useful to sanitize layout qualifier etc. on the type of the created
// symbol. This might need to be done in other places as well.
return node;
}
TIntermDeclaration *CreateTempInitDeclarationNode(const TSymbolUniqueId &id,
TIntermTyped *initializer,
TQualifier qualifier)
{
ASSERT(initializer != nullptr);
TIntermSymbol *tempSymbol = CreateTempSymbolNode(id, initializer->getType(), qualifier);
TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
TIntermBinary *tempInit = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
tempDeclaration->appendDeclarator(tempInit);
return tempDeclaration;
}
TIntermBlock *EnsureBlock(TIntermNode *node) TIntermBlock *EnsureBlock(TIntermNode *node)
{ {
if (node == nullptr) if (node == nullptr)
......
...@@ -30,6 +30,13 @@ TIntermTyped *CreateZeroNode(const TType &type); ...@@ -30,6 +30,13 @@ TIntermTyped *CreateZeroNode(const TType &type);
TIntermConstantUnion *CreateIndexNode(int index); TIntermConstantUnion *CreateIndexNode(int index);
TIntermConstantUnion *CreateBoolNode(bool value); TIntermConstantUnion *CreateBoolNode(bool value);
TIntermSymbol *CreateTempSymbolNode(const TSymbolUniqueId &id,
const TType &type,
TQualifier qualifier);
TIntermDeclaration *CreateTempInitDeclarationNode(const TSymbolUniqueId &id,
TIntermTyped *initializer,
TQualifier qualifier);
// If the input node is nullptr, return nullptr. // If the input node is nullptr, return nullptr.
// If the input node is a block node, return it. // If the input node is a block node, return it.
// If the input node is not a block node, put it inside a block node and return that. // If the input node is not a block node, put it inside a block node and return that.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "compiler/translator/IntermTraverse.h" #include "compiler/translator/IntermTraverse.h"
#include "compiler/translator/InfoSink.h" #include "compiler/translator/InfoSink.h"
#include "compiler/translator/IntermNode_util.h"
#include "compiler/translator/SymbolTable.h" #include "compiler/translator/SymbolTable.h"
namespace sh namespace sh
...@@ -169,21 +170,10 @@ void TIntermTraverser::insertStatementInParentBlock(TIntermNode *statement) ...@@ -169,21 +170,10 @@ void TIntermTraverser::insertStatementInParentBlock(TIntermNode *statement)
TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type, TQualifier qualifier) TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type, TQualifier qualifier)
{ {
// Each traversal uses at most one temporary variable, so the index stays the same within a
// single traversal.
TInfoSinkBase symbolNameOut;
ASSERT(mTemporaryId != nullptr); ASSERT(mTemporaryId != nullptr);
symbolNameOut << "s" << (mTemporaryId->get()); // nextTemporaryId() needs to be called when the code wants to start using another temporary
TString symbolName = symbolNameOut.c_str(); // symbol.
return CreateTempSymbolNode(*mTemporaryId, type, qualifier);
TIntermSymbol *node = new TIntermSymbol(mTemporaryId->get(), symbolName, type);
node->setInternal(true);
ASSERT(qualifier == EvqTemporary || qualifier == EvqConst || qualifier == EvqGlobal);
node->getTypePointer()->setQualifier(qualifier);
// TODO(oetuaho): Might be useful to sanitize layout qualifier etc. on the type of the created
// symbol. This might need to be done in other places as well.
return node;
} }
TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type) TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type)
...@@ -193,20 +183,17 @@ TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type) ...@@ -193,20 +183,17 @@ TIntermSymbol *TIntermTraverser::createTempSymbol(const TType &type)
TIntermDeclaration *TIntermTraverser::createTempDeclaration(const TType &type) TIntermDeclaration *TIntermTraverser::createTempDeclaration(const TType &type)
{ {
ASSERT(mTemporaryId != nullptr);
TIntermDeclaration *tempDeclaration = new TIntermDeclaration(); TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
tempDeclaration->appendDeclarator(createTempSymbol(type)); tempDeclaration->appendDeclarator(CreateTempSymbolNode(*mTemporaryId, type, EvqTemporary));
return tempDeclaration; return tempDeclaration;
} }
TIntermDeclaration *TIntermTraverser::createTempInitDeclaration(TIntermTyped *initializer, TIntermDeclaration *TIntermTraverser::createTempInitDeclaration(TIntermTyped *initializer,
TQualifier qualifier) TQualifier qualifier)
{ {
ASSERT(initializer != nullptr); ASSERT(mTemporaryId != nullptr);
TIntermSymbol *tempSymbol = createTempSymbol(initializer->getType(), qualifier); return CreateTempInitDeclarationNode(*mTemporaryId, initializer, qualifier);
TIntermDeclaration *tempDeclaration = new TIntermDeclaration();
TIntermBinary *tempInit = new TIntermBinary(EOpInitialize, tempSymbol, initializer);
tempDeclaration->appendDeclarator(tempInit);
return tempDeclaration;
} }
TIntermDeclaration *TIntermTraverser::createTempInitDeclaration(TIntermTyped *initializer) TIntermDeclaration *TIntermTraverser::createTempInitDeclaration(TIntermTyped *initializer)
......
...@@ -205,8 +205,8 @@ class TIntermTraverser : angle::NonCopyable ...@@ -205,8 +205,8 @@ class TIntermTraverser : angle::NonCopyable
TIntermSymbol *createTempSymbol(const TType &type); TIntermSymbol *createTempSymbol(const TType &type);
// Create a node that declares but doesn't initialize a temporary symbol. // Create a node that declares but doesn't initialize a temporary symbol.
TIntermDeclaration *createTempDeclaration(const TType &type); TIntermDeclaration *createTempDeclaration(const TType &type);
// Create a node that initializes the current temporary symbol with initializer having the given // Create a node that initializes the current temporary symbol with initializer. The symbol will
// qualifier. // have the given qualifier.
TIntermDeclaration *createTempInitDeclaration(TIntermTyped *initializer, TQualifier qualifier); TIntermDeclaration *createTempInitDeclaration(TIntermTyped *initializer, TQualifier qualifier);
// Create a node that initializes the current temporary symbol with initializer. // Create a node that initializes the current temporary symbol with initializer.
TIntermDeclaration *createTempInitDeclaration(TIntermTyped *initializer); TIntermDeclaration *createTempInitDeclaration(TIntermTyped *initializer);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment