Remove 'externGlobals' member from Module; instead find them when needed via new SymbolTable::GetMatchingVariables method.

This commit is contained in:
Matt Pharr
2011-10-04 06:36:31 -07:00
parent fa5050d5c7
commit a6fc657b40
8 changed files with 44 additions and 29 deletions

View File

@@ -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<const Type *> 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)

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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<const ReferenceType *>(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

View File

@@ -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<Symbol *> externGlobals;
symbolTable->GetMatchingVariables(lIsExternGlobal, &externGlobals);
for (unsigned int i = 0; i < externGlobals.size(); ++i)
lGetExportedTypes(externGlobals[i]->type, &exportedStructTypes,
&exportedEnumTypes, &exportedVectorTypes);

View File

@@ -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<Symbol *> externGlobals;
bool writeHeader(const char *filename);
bool writeObjectFileOrAssembly(OutputType outputType, const char *filename);
void execPreprocessor(const char *infilename, llvm::raw_string_ostream* ostream) const;

View File

@@ -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;
}

32
sym.h
View File

@@ -41,6 +41,7 @@
#define ISPC_SYM_H
#include "ispc.h"
#include "decl.h"
#include <map>
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<Symbol *> *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 <typename Predicate>
void GetMatchingVariables(Predicate pred,
std::vector<Symbol *> *matches) const;
/** Adds the named type to the symbol table. This is used for both
struct definitions (where <tt>struct Foo</tt> causes type \c Foo to
be added to the symbol table) as well as for <tt>typedef</tt>s.
@@ -251,9 +261,9 @@ private:
};
template <typename Predicate>
void SymbolTable::GetMatchingFunctions(Predicate pred,
std::vector<Symbol *> *matches) const {
template <typename Predicate> void
SymbolTable::GetMatchingFunctions(Predicate pred,
std::vector<Symbol *> *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<std::string, std::vector<Symbol *> >::const_iterator iter;
@@ -266,4 +276,14 @@ void SymbolTable::GetMatchingFunctions(Predicate pred,
}
}
template <typename Predicate> void
SymbolTable::GetMatchingVariables(Predicate pred,
std::vector<Symbol *> *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