From a6fc657b406f6119d60cb43da425d682e157c59b Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Tue, 4 Oct 2011 06:36:31 -0700 Subject: [PATCH] Remove 'externGlobals' member from Module; instead find them when needed via new SymbolTable::GetMatchingVariables method. --- builtins.cpp | 10 ++++------ ctx.cpp | 2 +- decl.cpp | 4 +--- expr.cpp | 2 +- module.cpp | 9 ++++++++- module.h | 9 --------- sym.cpp | 5 +++-- sym.h | 32 ++++++++++++++++++++++++++------ 8 files changed, 44 insertions(+), 29 deletions(-) diff --git a/builtins.cpp b/builtins.cpp index c82cb071..2f1535b3 100644 --- a/builtins.cpp +++ b/builtins.cpp @@ -377,8 +377,8 @@ lAddBitcode(const unsigned char *bitcode, int length, static void lDefineConstantInt(const char *name, int val, llvm::Module *module, SymbolTable *symbolTable) { - Symbol *pw = new Symbol(name, SourcePos(), AtomicType::UniformConstInt32); - pw->isStatic = true; + Symbol *pw = new Symbol(name, SourcePos(), AtomicType::UniformConstInt32, + SC_STATIC); pw->constValue = new ConstExpr(pw->type, val, SourcePos()); LLVM_TYPE_CONST llvm::Type *ltype = LLVMTypes::Int32Type; llvm::Constant *linit = LLVMInt32(val); @@ -395,8 +395,7 @@ lDefineConstantIntFunc(const char *name, int val, llvm::Module *module, SymbolTable *symbolTable) { std::vector args; FunctionType *ft = new FunctionType(AtomicType::UniformInt32, args, SourcePos()); - Symbol *sym = new Symbol(name, SourcePos(), ft); - sym->isStatic = true; + Symbol *sym = new Symbol(name, SourcePos(), ft, SC_STATIC); llvm::Function *func = module->getFunction(name); assert(func != NULL); // it should be declared already... @@ -413,8 +412,7 @@ lDefineConstantIntFunc(const char *name, int val, llvm::Module *module, static void lDefineProgramIndex(llvm::Module *module, SymbolTable *symbolTable) { Symbol *pidx = new Symbol("programIndex", SourcePos(), - AtomicType::VaryingConstInt32); - pidx->isStatic = true; + AtomicType::VaryingConstInt32, SC_STATIC); int pi[ISPC_MAX_NVEC]; for (int i = 0; i < g->target.vectorWidth; ++i) diff --git a/ctx.cpp b/ctx.cpp index e8234f9f..a867c248 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -170,7 +170,7 @@ FunctionEmitContext::FunctionEmitContext(const Type *rt, llvm::Function *functio diFile, funcStartPos.first_line, retType, - funSym->isStatic, + funSym->storageClass == SC_STATIC, true, /* is definition */ flags, g->opt.level > 0, diff --git a/decl.cpp b/decl.cpp index 82a59e3d..0ad0ea08 100644 --- a/decl.cpp +++ b/decl.cpp @@ -101,9 +101,7 @@ Declarator::AddArrayDimension(int size) { void Declarator::InitFromDeclSpecs(DeclSpecs *ds) { sym->type = GetType(ds); - - if (ds->storageClass == SC_STATIC) - sym->isStatic = true; + sym->storageClass = ds->storageClass; } diff --git a/expr.cpp b/expr.cpp index 8b665ff0..5526ecd0 100644 --- a/expr.cpp +++ b/expr.cpp @@ -1497,7 +1497,7 @@ lStoreAssignResult(llvm::Value *rv, llvm::Value *lv, const Type *type, assert(baseSym->varyingCFDepth <= ctx->VaryingCFDepth()); if (!g->opt.disableMaskedStoreToStore && baseSym->varyingCFDepth == ctx->VaryingCFDepth() && - baseSym->isStatic == false && + baseSym->storageClass != SC_STATIC && dynamic_cast(baseSym->type) == NULL) { // If the variable is declared at the same varying control flow // depth as where it's being assigned, then we don't need to do any diff --git a/module.cpp b/module.cpp index bc8d04b7..4531bfd2 100644 --- a/module.cpp +++ b/module.cpp @@ -505,7 +505,6 @@ Module::AddGlobal(DeclSpecs *ds, Declarator *decl) { // make sure it's a compile-time constant! llvm::Constant *llvmInitializer = NULL; if (ds->storageClass == SC_EXTERN || ds->storageClass == SC_EXTERN_C) { - externGlobals.push_back(decl->sym); if (decl->initExpr != NULL) Error(decl->pos, "Initializer can't be provided with \"extern\" " "global variable \"%s\".", decl->sym->name.c_str()); @@ -1268,6 +1267,12 @@ lIsExternC(const Symbol *sym) { } +static bool +lIsExternGlobal(const Symbol *sym) { + return sym->storageClass == SC_EXTERN || sym->storageClass == SC_EXTERN_C; +} + + bool Module::writeHeader(const char *fn) { FILE *f = fopen(fn, "w"); @@ -1336,6 +1341,8 @@ Module::writeHeader(const char *fn) { &exportedEnumTypes, &exportedVectorTypes); // And do the same for the 'extern' globals + std::vector externGlobals; + symbolTable->GetMatchingVariables(lIsExternGlobal, &externGlobals); for (unsigned int i = 0; i < externGlobals.size(); ++i) lGetExportedTypes(externGlobals[i]->type, &exportedStructTypes, &exportedEnumTypes, &exportedVectorTypes); diff --git a/module.h b/module.h index 1530a722..f095c1c0 100644 --- a/module.h +++ b/module.h @@ -99,15 +99,6 @@ public: private: const char *filename; - /** This member records the global variables that have been defined - with 'extern' linkage, so that it's easy to include their - declarations in generated header files. - - @todo FIXME: it would be nice to eliminate this and then query the - symbol table or the llvm Module for them when/if we need them. - */ - std::vector externGlobals; - bool writeHeader(const char *filename); bool writeObjectFileOrAssembly(OutputType outputType, const char *filename); void execPreprocessor(const char *infilename, llvm::raw_string_ostream* ostream) const; diff --git a/sym.cpp b/sym.cpp index 352f538b..fff6a1ac 100644 --- a/sym.cpp +++ b/sym.cpp @@ -43,13 +43,14 @@ /////////////////////////////////////////////////////////////////////////// // Symbol -Symbol::Symbol(const std::string &n, SourcePos p, const Type *t) +Symbol::Symbol(const std::string &n, SourcePos p, const Type *t, + StorageClass sc) : pos(p), name(n) { storagePtr = NULL; function = NULL; type = t; constValue = NULL; - isStatic = false; + storageClass = sc; varyingCFDepth = 0; } diff --git a/sym.h b/sym.h index da00805a..58a07b2f 100644 --- a/sym.h +++ b/sym.h @@ -41,6 +41,7 @@ #define ISPC_SYM_H #include "ispc.h" +#include "decl.h" #include class StructType; @@ -63,7 +64,8 @@ class Symbol { public: /** The Symbol constructor takes the name of the symbol, its position in a source file, and its type (if known). */ - Symbol(const std::string &name, SourcePos pos, const Type *t = NULL); + Symbol(const std::string &name, SourcePos pos, const Type *t = NULL, + StorageClass sc = SC_NONE); /** This method should only be called for function symbols; for them, it returns a mangled version of the function name with the argument @@ -93,8 +95,8 @@ public: storagePtr member will be its constant value. (This messiness is due to needing an ispc ConstExpr for the early constant folding optimizations). */ - bool isStatic; /*!< Records whether this symbol had a static qualifier in - its declaration. */ + StorageClass storageClass;/*!< Records the storage class (if any) provided with the + symbol's declaration. */ int varyingCFDepth; /*!< This member records the number of levels of nested 'varying' control flow within which the symbol was declared. Having this value available makes it possible to avoid performing @@ -186,6 +188,14 @@ public: void GetMatchingFunctions(Predicate pred, std::vector *matches) const; + /** Returns all of the variable symbols in the symbol table that match + the given predicate. The predicate is defined as in the + GetMatchingFunctions() method. + */ + template + void GetMatchingVariables(Predicate pred, + std::vector *matches) const; + /** Adds the named type to the symbol table. This is used for both struct definitions (where struct Foo causes type \c Foo to be added to the symbol table) as well as for typedefs. @@ -251,9 +261,9 @@ private: }; -template -void SymbolTable::GetMatchingFunctions(Predicate pred, - std::vector *matches) const { +template void +SymbolTable::GetMatchingFunctions(Predicate pred, + std::vector *matches) const { // Iterate through all function symbols and apply the given predicate. // If it returns true, add the Symbol * to the provided vector. std::map >::const_iterator iter; @@ -266,4 +276,14 @@ void SymbolTable::GetMatchingFunctions(Predicate pred, } } + +template void +SymbolTable::GetMatchingVariables(Predicate pred, + std::vector *matches) const { + for (unsigned int i = 0; i < variables.size(); ++i) + for (unsigned int j = 0; j < variables[i]->size(); ++j) + if (pred((*variables[i])[j])) + matches->push_back((*variables[i])[j]); +} + #endif // ISPC_SYM_H