Commit e2858656 by Nicolas Capens

Split builtin symbols over multiple levels.

Bug 19331817 Change-Id: Ia1ab8f4da90c62ca846961c97691aacdbdf62a6a Reviewed-on: https://swiftshader-review.googlesource.com/2361Tested-by: 's avatarNicolas Capens <capn@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com> Reviewed-by: 's avatarNicolas Capens <capn@google.com>
parent d603ecd6
...@@ -181,6 +181,38 @@ void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext) ...@@ -181,6 +181,38 @@ void TSymbolTableLevel::relateToExtension(const char* name, const TString& ext)
} }
} }
TSymbol *TSymbolTable::find(const TString &name, int shaderVersion, bool *builtIn, bool *sameScope) const
{
int level = currentLevel();
TSymbol *symbol = nullptr;
do
{
symbol = table[level]->find(name);
--level;
}
while(!symbol && level >= 0);
level++;
if(builtIn)
{
*builtIn = (level == 0);
}
if(sameScope)
{
*sameScope = (level == currentLevel());
}
return symbol;
}
TSymbol *TSymbolTable::findBuiltIn(const TString &name, int shaderVersion) const
{
return table[0]->find(name);
}
TSymbol::TSymbol(const TSymbol& copyOf) TSymbol::TSymbol(const TSymbol& copyOf)
{ {
name = NewPoolTString(copyOf.name->c_str()); name = NewPoolTString(copyOf.name->c_str());
......
...@@ -219,6 +219,14 @@ protected: ...@@ -219,6 +219,14 @@ protected:
static int uniqueId; // for unique identification in code generation static int uniqueId; // for unique identification in code generation
}; };
enum ESymbolLevel
{
COMMON_BUILTINS = 0,
ESSL1_BUILTINS = 0,
LAST_BUILTIN_LEVEL = ESSL1_BUILTINS,
GLOBAL_LEVEL = 1
};
class TSymbolTable class TSymbolTable
{ {
public: public:
...@@ -233,19 +241,15 @@ public: ...@@ -233,19 +241,15 @@ public:
~TSymbolTable() ~TSymbolTable()
{ {
// level 0 is always built In symbols, so we never pop that out while(currentLevel() > LAST_BUILTIN_LEVEL)
while (table.size() > 1) {
pop(); pop();
} }
}
// bool isEmpty() { return table.empty(); }
// When the symbol table is initialized with the built-ins, there should bool atBuiltInLevel() { return currentLevel() <= LAST_BUILTIN_LEVEL; }
// 'push' calls, so that built-ins are at level 0 and the shader bool atGlobalLevel() { return currentLevel() <= GLOBAL_LEVEL; }
// globals are at level 1.
//
bool isEmpty() { return table.size() == 0; }
bool atBuiltInLevel() { return table.size() == 1; }
bool atGlobalLevel() { return table.size() <= 2; }
void push() void push()
{ {
table.push_back(new TSymbolTableLevel); table.push_back(new TSymbolTableLevel);
...@@ -264,19 +268,19 @@ public: ...@@ -264,19 +268,19 @@ public:
return insert(currentLevel(), symbol); return insert(currentLevel(), symbol);
} }
bool insert(int level, TSymbol &symbol) bool insert(ESymbolLevel level, TSymbol &symbol)
{ {
return table[level]->insert(symbol); return table[level]->insert(symbol);
} }
bool insertConstInt(const char *name, int value) bool insertConstInt(ESymbolLevel level, const char *name, int value)
{ {
TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1)); TVariable *constant = new TVariable(NewPoolTString(name), TType(EbtInt, EbpUndefined, EvqConst, 1));
constant->getConstPointer()->setIConst(value); constant->getConstPointer()->setIConst(value);
return insert(0, *constant); return insert(level, *constant);
} }
bool insertBuiltIn(TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0) bool insertBuiltIn(ESymbolLevel level, TType *rvalue, const char *name, TType *ptype1, TType *ptype2 = 0, TType *ptype3 = 0)
{ {
TFunction *function = new TFunction(NewPoolTString(name), *rvalue); TFunction *function = new TFunction(NewPoolTString(name), *rvalue);
...@@ -295,50 +299,26 @@ public: ...@@ -295,50 +299,26 @@ public:
function->addParameter(param3); function->addParameter(param3);
} }
return insert(0, *function); return insert(level, *function);
} }
TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false) const TSymbol *find(const TString &name, int shaderVersion, bool *builtIn = false, bool *sameScope = false) const;
{ TSymbol *findBuiltIn(const TString &name, int shaderVersion) const;
int level = currentLevel();
TSymbol* symbol;
do {
symbol = table[level]->find(name);
--level;
} while (symbol == 0 && level >= 0);
level++;
if (builtIn)
*builtIn = level == 0;
if (sameScope)
*sameScope = level == currentLevel();
return symbol;
}
TSymbol *findBuiltIn(const TString &name, int shaderVersion) const
{
return table[0]->find(name);
}
TSymbolTableLevel *getGlobalLevel() const
{
assert(table.size() >= 2);
return table[1];
}
TSymbolTableLevel *getOuterLevel() const TSymbolTableLevel *getOuterLevel() const
{ {
assert(table.size() >= 2); assert(currentLevel() >= 1);
return table[currentLevel() - 1]; return table[currentLevel() - 1];
} }
void relateToOperator(const char* name, TOperator op) void relateToOperator(ESymbolLevel level, const char *name, TOperator op)
{ {
table[0]->relateToOperator(name, op); table[level]->relateToOperator(name, op);
} }
void relateToExtension(const char* name, const TString& ext) void relateToExtension(ESymbolLevel level, const char *name, const TString &ext)
{ {
table[0]->relateToExtension(name, ext); table[level]->relateToExtension(name, ext);
} }
bool setDefaultPrecision(const TPublicType &type, TPrecision prec) bool setDefaultPrecision(const TPublicType &type, TPrecision prec)
...@@ -377,7 +357,7 @@ public: ...@@ -377,7 +357,7 @@ public:
} }
protected: protected:
int currentLevel() const { return static_cast<int>(table.size() - 1); } ESymbolLevel currentLevel() const { return static_cast<ESymbolLevel>(table.size() - 1); }
std::vector<TSymbolTableLevel*> table; std::vector<TSymbolTableLevel*> table;
typedef std::map< TBasicType, TPrecision > PrecisionStackLevel; typedef std::map< TBasicType, TPrecision > PrecisionStackLevel;
......
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