diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 12:40:19 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 12:40:19 -0600 |
commit | 53c5a841d7131cc119c6bafdf5387d732eb98c02 (patch) | |
tree | da5542f49abb4b3004aec86e4931dcf61d183e24 | |
parent | 47ba6bf1100fe1da8d80607053d88cfd2abe25ff (diff) | |
download | chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.tar.gz chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.tar.bz2 chill-53c5a841d7131cc119c6bafdf5387d732eb98c02.zip |
Added variable & typedef scoping
-rw-r--r-- | include/chillAST/chillAST_def.hh | 1 | ||||
-rw-r--r-- | include/chillAST/chillAST_node.hh | 38 | ||||
-rw-r--r-- | include/chillAST/chillASTs.hh | 177 | ||||
-rwxr-xr-x | lib/chillcg/src/CG_chillBuilder.cc | 14 | ||||
-rw-r--r-- | src/chillASTs.cc | 243 |
5 files changed, 100 insertions, 373 deletions
diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh index 7d729d6..59a7f95 100644 --- a/include/chillAST/chillAST_def.hh +++ b/include/chillAST/chillAST_def.hh @@ -180,6 +180,7 @@ typedef std::vector<chillAST_TypedefDecl *> chillAST_TypedefTable; chillAST_VarDecl *symbolTableFindName(chillAST_SymbolTable *table, const char *name); chillAST_VarDecl *symbolTableFindVariableNamed(chillAST_SymbolTable *table, const char *name); +chillAST_TypedefDecl *typedefTableFindName(chillAST_TypedefTable *table, const char *name); void printSymbolTable(chillAST_SymbolTable *st); void printSymbolTableMoreInfo(chillAST_SymbolTable *st); diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index 6cc6cfd..9788588 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -143,15 +143,20 @@ public: virtual bool isAUnion() { return false; }; - virtual bool hasSymbolTable() { return false; }; // most nodes do NOT have a symbol table - virtual bool hasTypedefTable() { return false; }; // most nodes do NOT have a typedef table - virtual chillAST_SymbolTable *getSymbolTable() { return NULL; } // most nodes do NOT have a symbol table + virtual chillAST_SymbolTable* getSymbolTable() { return symbolTable; } + virtual chillAST_TypedefTable* getTypedefTable() {return typedefTable; } + virtual void addVariableToScope(chillAST_VarDecl *vd); + virtual void addTypedefToScope(chillAST_TypedefDecl *tdd); + chillAST_TypedefDecl* findTypeDecleration(const char *t); + chillAST_VarDecl* findVariableDecleration(const char *t); + chillAST_VarDecl* getVariableDeclaration(const char *vn); + chillAST_TypedefDecl* getTypeDeclaration(const char *tn); virtual chillAST_VarDecl *findVariableNamed(const char *name); // recursive chillAST_RecordDecl *findRecordDeclNamed(const char *name); // recursive - // void addDecl( chillAST_VarDecl *vd); // recursive, adds to first symbol table it can find + // void addDecl( chillAST_VarDecl *vd); // recursive, adds to first symbol table it can find int getNumChildren() { return children.size(); }; @@ -168,6 +173,7 @@ public: void setMetaComment(const char *c) { metacomment = strdup(c); }; virtual void chillMergeChildInfo(chillAST_Node){ + // TODO if (par) par->add to definition for vardecl/typedecl // TODO if (par) par->getSourceFile()->addFunc(this); for FuncDecl // TODO if (par) par->getSourceFile()->addMacro(this); For MacroDecl // TODO if (parent) parent->addVariableToSymbolTable(this); // should percolate up until something has a symbol table @@ -366,7 +372,7 @@ public: }; virtual chillAST_VarDecl *findArrayDecl(const char *name) { // scoping TODO - if (!hasSymbolTable()) return parent->findArrayDecl(name); // most things + if (!getSymbolTable()) return parent->findArrayDecl(name); // most things else fprintf(stderr, "(%s) forgot to implement gatherArrayVarDecls()\n", getTypeString()); } @@ -426,7 +432,7 @@ public: virtual void printonly(int indent = 0, FILE *fp = stderr) { print(indent, fp); }; - virtual void get_top_level_loops(std::vector<chillAST_ForStmt *> &loops) { + virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) { int n = children.size(); //fprintf(stderr, "get_top_level_loops of a %s with %d children\n", getTypeString(), n); for (int i = 0; i < n; i++) { @@ -507,26 +513,6 @@ public: exit(-1); } - virtual chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - if (parent != NULL) return parent->findDatatype(t); // most nodes do this - return NULL; - } - - - virtual chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { - if (!parent) { - fprintf(stderr, "%s with no parent addVariableToSymbolTable()\n", getTypeString()); - exit(-1); - } - //fprintf(stderr, "%s::addVariableToSymbolTable() (default) headed up\n", getTypeString()); - return parent->addVariableToSymbolTable(vd); // default, defer to parent - } - - virtual void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - parent->addTypedefToTypedefTable(tdd); // default, defer to parent - } - void walk_parents() { fprintf(stderr, "wp: (%s) ", getTypeString()); print(); diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index 637df4b..5c9010c 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -63,7 +63,6 @@ public: chillAST_RecordDecl *getStructDef(); - bool isAStruct() { return isStruct; }; bool isAUnion() { return isUnion; }; @@ -86,7 +85,6 @@ public: chillAST_VarDecl *findSubpart(const char *name); - //TODO hide data, set/get type and alias chillAST_TypedefDecl(); chillAST_TypedefDecl(char *t, const char *nt); @@ -143,6 +141,7 @@ public: fprintf(stderr, "byref %d\n", tf); }; + bool nameis(const char *n) { return !strcmp(n, varname); }; bool isABuiltin; // if variable is builtin, we don't need to declare it void *uniquePtr; // DO NOT REFERENCE THROUGH THIS! just used to differentiate declarations bool isArray() { return (numdimensions != 0); }; @@ -296,38 +295,6 @@ class chillAST_CompoundStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_COMPOUNDSTMT;} // variables that are special for this type of node - chillAST_SymbolTable *symbol_table; // symbols defined inside this compound statement - chillAST_TypedefTable *typedef_table; - - bool hasSymbolTable() { return true; }; - - bool hasTypeDefTable() { return true; }; - - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "chillAST_CompoundStmt::findDatatype( %s )\n", t); - if (typedef_table) { - for (int i = 0; i < typedef_table->size(); i++) { - chillAST_TypedefDecl *tdd = (*typedef_table)[i]; - if (tdd->nameis(t)) return tdd; - } - } - if (parent) return parent->findDatatype(t); - return NULL; // should not happen - } - - chillAST_SymbolTable *getSymbolTable() { return symbol_table; } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_CompoundStmt method - //fprintf(stderr, "\nchillAST_CompoundStmt addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - typedef_table = addTypedefToTable(typedef_table, tdd); - } - // constructors chillAST_CompoundStmt(); // never has any args ??? @@ -430,21 +397,11 @@ public: char *returnType; char *functionName; - // parameters - int numParameters() { return parameters.size(); }; - chillAST_SymbolTable parameters; - - // this is probably a mistake, but symbol_table here is pointing to BODY'S symbol table - //chillAST_SymbolTable *symbol_table; // symbols defined inside this function. REALLY the body's symbol table? + //! parameters + int numParameters() { return symbolTable->size(); }; + // chillAST_TypedefTable *typedef_table; // TODO typedef here doesn't make sense - chillAST_TypedefTable *typedef_table; // function typedef table - - - bool hasSymbolTable() { return true; }; // COULD HAVE - bool hasTypeDefTable() { return true; }; // COULD HAVE - - - //char *parametertypes; // a single string?? + //char *parametertypes; // a single string?? void printParameterTypes(FILE *fp); void setName(char *n) { functionName = strdup(n); /* probable memory leak */ }; @@ -468,25 +425,12 @@ public: void *uniquePtr; // DO NOT REFERENCE THROUGH THIS! USED AS A UNIQUE ID - - - - chillAST_FunctionDecl(); // { asttype = CHILLAST_NODE_FUNCTIONDECL; numparameters = 0;}; - chillAST_FunctionDecl(const char *rt, const char *fname); - chillAST_FunctionDecl(const char *rt, const char *fname, void *unique); void addParameter(chillAST_VarDecl *p); - chillAST_VarDecl *hasParameterNamed(const char *name); - - chillAST_VarDecl *findParameterNamed(const char *name) { return hasParameterNamed(name); }; - void addDecl(chillAST_VarDecl *vd); // just adds to symbol table?? TODO - chillAST_VarDecl *funcHasVariableNamed(const char *name); // functiondecl::hasVariableNamed - //chillAST_VarDecl *findVariableNamed( const char *name ) { return hasVariableNamed( name ); }; - void addChild(chillAST_Node *node); // special because inserts into BODY void insertChild(int i, chillAST_Node *node); // special because inserts into BODY @@ -520,53 +464,7 @@ public: chillAST_Node *constantFold(); - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - if (!typedef_table) { // not here - if (parent) return parent->findDatatype(t); // not here, check parents - else return NULL; // not defined here and no parent - } - - //fprintf(stderr, "%d typedefs\n", typedef_table->size()); - for (int i = 0; i < typedef_table->size(); i++) { - chillAST_TypedefDecl *tdd = (*typedef_table)[i]; - if (tdd->nameis(t)) return tdd; - } - if (parent) return parent->findDatatype(t); - return NULL; // should not happen - } - - chillAST_SymbolTable *getParameterSymbolTable() { return ¶meters; } - - chillAST_SymbolTable *getSymbolTable() { return body->getSymbolTable(); } //symbol_table; } // - void setSymbolTable(chillAST_SymbolTable *tab) { - // no longer keeping a local ?? symbol_table = tab; - if (!body) { // can never happen now - body = new chillAST_CompoundStmt(); - } // only if func is empty! - body->symbol_table = tab; - } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_FunctionDecl method - //fprintf(stderr, "\nchillAST_FunctionDecl addVariableToSymbolTable( %s )\n", vd->varname); - - // this is all dealing with the body's symbol table - // the function has a symbol table called "parameters" but that is a special case - - addSymbolToTable(getSymbolTable(), vd); - if (!vd->parent) { - //fprintf(stderr, "setting parent of vardecl to be the function whose symbol table it is going into\n"); // ?? - vd->setParent(this); - insertChild(0, vd); - } - //printSymbolTable( getSymbolTable() ); - return getSymbolTable(); - } - - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - typedef_table = addTypedefToTable(typedef_table, tdd); - } + chillAST_SymbolTable *getParameterTable() { return getSymbolTable(); } void replaceChild(chillAST_Node *old, chillAST_Node *newchild) { body->replaceChild(old, newchild); @@ -596,42 +494,6 @@ public: } // get, set filename ? - chillAST_SymbolTable *global_symbol_table; // (global) symbols defined inside this source file - chillAST_TypedefTable *global_typedef_table; // source file - chillAST_VarDecl *findVariableNamed(const char *name); // looks in global_symbol_table; - - bool hasSymbolTable() { return true; }; // "has" vs "can have" TODO - bool hasTypeDefTable() { return true; }; - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_SourceFile method - fprintf(stderr, "\nchillAST_SourceFile addVariableToSymbolTable( %s )\n", vd->varname); - global_symbol_table = addSymbolToTable(global_symbol_table, vd); - //addChild( vd ); // ?? - //printSymbolTable( global_symbol_table ); - return global_symbol_table; - } - - void addTypedefToTypedefTable(chillAST_TypedefDecl *tdd) { - //fprintf(stderr, "SOURCEFILE adding typedef %s to typedeftable\n", tdd->getStructName()); - global_typedef_table = addTypedefToTable(global_typedef_table, tdd); - //fprintf(stderr, "now global typedef table has %d entries\n", global_typedef_table->size()); - } - - chillAST_Node *findDatatype(char *t) { - fprintf(stderr, "%s looking for datatype %s\n", getTypeString(), t); - fprintf(stderr, "%d global typedefs\n", global_typedef_table->size()); - for (int i = 0; i < global_typedef_table->size(); i++) { - - chillAST_TypedefDecl *tdd = (*global_typedef_table)[i]; - //fprintf(stderr, "comparing to %s\n", tdd->getStructName()); - if (tdd->nameis(t)) { - //fprintf(stderr, "found it\n"); - return (chillAST_Node *) tdd; - } - } - return NULL; - } - std::vector<chillAST_FunctionDecl *> functions; // at top level, or anywhere? std::vector<chillAST_MacroDefinition *> macrodefinitions; @@ -666,7 +528,6 @@ public: class chillAST_MacroDefinition : public chillAST_Node { private: chillAST_Node *body; // rhs always a compound statement? - chillAST_SymbolTable *symbol_table; public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MACRODEFINITION;} char *macroName; @@ -684,11 +545,6 @@ public: chillAST_MacroDefinition(const char *name, const char *rhs); - void addParameter(chillAST_VarDecl *p); // parameters have no TYPE ?? - chillAST_VarDecl *hasParameterNamed(const char *name); - - chillAST_VarDecl *findParameterNamed(const char *name) { return hasParameterNamed(name); }; - void addChild(chillAST_Node *node); // special because inserts into BODY void insertChild(int i, chillAST_Node *node); // special because inserts into BODY @@ -699,19 +555,6 @@ public: void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc void dump(int indent = 0, FILE *fp = stderr); // in chill_ast.cc - bool hasSymbolTable() { return true; }; - - //const std::vector<chillAST_VarDecl *> getSymbolTable() { return symbol_table; } - chillAST_SymbolTable *getSymbolTable() { return symbol_table; } - - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_MacroDefinition method ?? - //fprintf(stderr, "\nchillAST_MacroDefinition addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - - chillAST_Node *clone(); // none of these make sense for macros @@ -746,7 +589,6 @@ public: chillAST_Node *body; // always a compoundstmt? IR_CONDITION_TYPE conditionoperator; // from ir_code.hh - chillAST_SymbolTable *symbol_table; // symbols defined inside this forstmt (in init but not body?) body is compound stmt bool hasSymbolTable() { return true; }; // constructors @@ -846,13 +688,6 @@ public: void loseLoopWithLoopVar(char *var); // chillAST_ForStmt void replaceChild(chillAST_Node *old, chillAST_Node *newchild); - chillAST_SymbolTable *addVariableToSymbolTable(chillAST_VarDecl *vd) { // chillAST_ForStmt method - //fprintf(stderr, "\nchillAST_ForStmt addVariableToSymbolTable( %s )\n", vd->varname); - symbol_table = addSymbolToTable(symbol_table, vd); - //printSymbolTable( symbol_table ); - return symbol_table; - } - void gatherStatements(std::vector<chillAST_Node *> &statements); bool lowerBound(int &l); diff --git a/lib/chillcg/src/CG_chillBuilder.cc b/lib/chillcg/src/CG_chillBuilder.cc index ccc7813..63761c5 100755 --- a/lib/chillcg/src/CG_chillBuilder.cc +++ b/lib/chillcg/src/CG_chillBuilder.cc @@ -310,8 +310,8 @@ namespace omega { //fprintf(stderr, "\nfunction is:\n"); currentfunction->print(); printf("\n\n"); fflush(stdout); - symtab_ = &(currentfunction->parameters); // getSymbolTable(); // TODO rename - symtab2_ = currentfunction->getBody()->getSymbolTable(); // TODO rename + symtab_ = currentfunction->getSymbolTable(); + symtab2_ = currentfunction->getBody()->getSymbolTable(); //printf("\nsymtab_:\n"); fflush(stdout); //printSymbolTable( symtab_ ); @@ -1037,7 +1037,7 @@ namespace omega { // that the ident is a direct child of the current function chillAST_VarDecl *vd = new chillAST_VarDecl( "int", _s.c_str(), "", currentfunction->getBody()); // parent not available TODO - currentfunction->addVariableToSymbolTable( vd ); // use symtab2_ ?? + currentfunction->addVariableToScope( vd ); // use symtab2_ ? chillAST_DeclRefExpr *dre = new chillAST_DeclRefExpr( "int", _s.c_str(), (chillAST_Node*)vd); // parent not available @@ -1049,7 +1049,7 @@ namespace omega { // variable was already defined as either a parameter or internal variable to the function. // NOW WHAT?? gotta return something - chillAST_VarDecl *vd = currentfunction->funcHasVariableNamed( _s.c_str() ); + chillAST_VarDecl *vd = currentfunction->getVariableDeclaration( _s.c_str() ); //fprintf(stderr, "vd %p\n", vd); chillAST_DeclRefExpr *dre = new chillAST_DeclRefExpr( "int", _s.c_str(), (chillAST_Node*)vd); // parent not available @@ -1704,7 +1704,7 @@ namespace omega { chillAST_SymbolTable *st = currentfunction->getBody()->getSymbolTable(); //printSymbolTable(st); - currentfunction->getBody()->addVariableToSymbolTable( vd ); // TODO + currentfunction->getBody()->addVariableToScope( vd ); // TODO currentfunction->getBody()->insertChild(0, vd); // TODO //printSymbolTable(st); @@ -1723,8 +1723,8 @@ namespace omega { // we need to add this to function ?? TODO - currentfunction->getBody()->addVariableToSymbolTable( vd ); // TODO - currentfunction->getBody()->insertChild(0, vd); // TODO + currentfunction->getBody()->addVariableToScope( vd ); + currentfunction->getBody()->insertChild(0, vd); //printf("\nafter adding vardecl, source is:\n"); currentfunction->getBody()->print(); fflush(stdout); diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 3665571..851e148 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -127,7 +127,17 @@ chillAST_VarDecl *symbolTableFindName(chillAST_SymbolTable *table, const char *n int numvars = table->size(); for (int i = 0; i < numvars; i++) { chillAST_VarDecl *vd = (*table)[i]; - if (!strcmp(name, vd->varname)) return vd; + if (vd->nameis(name)) return vd; + } + return NULL; +} + +chillAST_TypedefDecl *typedefTableFindName(chillAST_TypedefTable *table, const char *name) { + if (!table) return NULL; + int numvars = table->size(); + for (int i = 0; i < numvars; i++) { + chillAST_TypedefDecl *td = (*table)[i]; + if (td->nameis(name)) return td; } return NULL; } @@ -258,7 +268,7 @@ void chillindent(int howfar, FILE *fp) { for (int i = 0; i < howfar; i++) fprint chillAST_VarDecl *chillAST_Node::findVariableNamed(const char *name) { // generic, recursive CHILL_DEBUG_PRINT("nodetype %s findVariableNamed( %s )\n", getTypeString(), name); - if (hasSymbolTable()) { // look in my symbol table if I have one + if (getSymbolTable()) { // look in my symbol table if I have one CHILL_DEBUG_PRINT("%s has a symbol table\n", getTypeString()); chillAST_VarDecl *vd = symbolTableFindVariableNamed(getSymbolTable(), name); if (vd) { @@ -322,7 +332,6 @@ void chillAST_Node::printPreprocAFTER(int indent, FILE *fp) { for (int i = 0; i < preprocessinginfo.size(); i++) { if (preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_LINEAFTER || preprocessinginfo[i]->position == CHILLAST_PREPROCESSING_TOTHERIGHT) { - //fprintf(stderr, "after %d\n", preprocessinginfo[i]->position); preprocessinginfo[i]->print(indent, fp); } } @@ -331,8 +340,8 @@ void chillAST_Node::printPreprocAFTER(int indent, FILE *fp) { chillAST_SourceFile::chillAST_SourceFile(const char *filename) { if(filename) SourceFileName = strdup(filename); else SourceFileName = strdup("No Source File"); - global_symbol_table = NULL; - global_typedef_table = NULL; + symbolTable = new chillAST_SymbolTable(); + typedefTable = new chillAST_TypedefTable(); FileToWrite = NULL; frontend = strdup("unknown"); }; @@ -423,36 +432,6 @@ chillAST_Node *chillAST_SourceFile::findCall(const char *name) { return func; } - -chillAST_VarDecl *chillAST_SourceFile::findVariableNamed(const char *name) { - CHILL_DEBUG_PRINT("SOURCEFILE SPECIAL %s findVariableNamed( %s )\n", getTypeString(), name); - if (hasSymbolTable()) { // look in my symbol table if I have one - CHILL_DEBUG_PRINT("%s has a symbol table\n", getTypeString()); - chillAST_VarDecl *vd = symbolTableFindVariableNamed(getSymbolTable(), name); - if (vd) { - CHILL_DEBUG_PRINT("found it\n"); - return vd; // found locally - } - CHILL_DEBUG_PRINT("%s has a symbol table but couldn't find %s\n", getTypeString(), name); - } - - CHILL_DEBUG_PRINT("looking for %s in SourceFile global_symbol_table\n", name); - chillAST_VarDecl *vd = symbolTableFindVariableNamed(global_symbol_table, name); - if (vd) { - CHILL_DEBUG_PRINT("found it\n"); - return vd; // found locally - } - - if (!parent) { - CHILL_DEBUG_PRINT("%s has no parent\n", getTypeString()); - return NULL; // no more recursion available - } - // recurse upwards - CHILL_DEBUG_PRINT("recursing from %s up to parent\n", getTypeString()); - return parent->findVariableNamed(name); -} - - chillAST_TypedefDecl::chillAST_TypedefDecl() { underlyingtype = newtype = arraypart = NULL; parent = NULL; @@ -673,8 +652,8 @@ chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname, else functionName = strdup("YouScrewedUp"); forwarddecl = externfunc = builtin = false; + symbolTable = new chillAST_SymbolTable(); this->setFunctionCPU(); - typedef_table = NULL; body = new chillAST_CompoundStmt(); returnType = strdup(rt); this->setFunctionCPU(); @@ -684,97 +663,35 @@ chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname, void chillAST_FunctionDecl::addParameter(chillAST_VarDecl *p) { CHILL_DEBUG_PRINT("%s chillAST_FunctionDecl::addParameter( 0x%x param %s) total of %d parameters\n", functionName, - p, p->varname, 1 + parameters.size()); + p, p->varname, 1 + getSymbolTable()->size()); - if (symbolTableFindName(¶meters, p->varname)) { // NOT recursive. just in FunctionDecl + if (symbolTableFindName(getSymbolTable(), p->varname)) { // NOT recursive. just in FunctionDecl CHILL_DEBUG_PRINT("chillAST_FunctionDecl::addParameter( %s ), parameter already exists?\n", p->varname); // exit(-1); // ?? return; // error? } - parameters.push_back(p); - //addSymbolToTable( parameters, p ); + getSymbolTable()->push_back(p); CHILL_DEBUG_PRINT("setting %s isAParameter\n", p->varname); p->isAParameter = true; - - p->setParent(this); // ?? unclear TODO - //p->dump(); printf("\naddparameter done\n\n"); fflush(stdout); + p->setParent(this); // this is a combined list! } - +// TODO This couldn't be more wrong! void chillAST_FunctionDecl::addDecl(chillAST_VarDecl *vd) { // to symbol table ONLY CHILL_DEBUG_PRINT("chillAST_FunctionDecl::addDecl( %s )\n", vd->varname); - if (!body) { - //fprintf(stderr, "had no body\n"); + if (!body) body = new chillAST_CompoundStmt(); - - //body->symbol_table = symbol_table; // probably wrong if this ever does something - } - - //fprintf(stderr, "before body->addvar(), func symbol table had %d entries\n", symbol_table->size()); - //fprintf(stderr, "before body->addvar(), body symbol table was %p\n", body->symbol_table); - //fprintf(stderr, "before body->addvar(), body symbol table had %d entries\n", body->symbol_table->size()); - //adds to body symbol table, and makes sure function has a copy. probably dumb - body->symbol_table = body->addVariableToSymbolTable(vd); - //fprintf(stderr, "after body->addvar(), func symbol table had %d entries\n", symbol_table->size()); -} - -chillAST_VarDecl *chillAST_FunctionDecl::hasParameterNamed(const char *name) { - int numparams = parameters.size(); - for (int i = 0; i < numparams; i++) { - if (!strcmp(name, parameters[i]->varname)) return parameters[i]; // need to check type? - } - return NULL; + body->addVariableToScope(vd); } - -// similar to symbolTableHasVariableNamed() but returns the variable definition -chillAST_VarDecl *chillAST_FunctionDecl::funcHasVariableNamed(const char *name) { // NOT recursive - //fprintf(stderr, "chillAST_FunctionDecl::funcHasVariableNamed( %s )\n", name ); - - // first check the parameters - int numparams = parameters.size(); - for (int i = 0; i < numparams; i++) { - chillAST_VarDecl *vd = parameters[i]; - if (!strcmp(name, vd->varname)) { - //fprintf(stderr, "yep, it's parameter %d\n", i); - return vd; // need to check type? - } - } - //fprintf(stderr, "no parameter named %s\n", name); - - chillAST_SymbolTable *st = getSymbolTable(); - if (!st) { - fprintf(stderr, "and no symbol_table, so no variable named %s\n", name); - return NULL; // no symbol table so no variable by that name - } - - - int numvars = st->size(); - //fprintf(stderr, "checking against %d variables\n", numvars); - for (int i = 0; i < numvars; i++) { - chillAST_VarDecl *vd = (*st)[i]; - //fprintf(stderr, "comparing '%s' to '%s'\n", name, vd->varname); - if (!strcmp(name, vd->varname)) { - //fprintf(stderr, "yep, it's variable %d\n", i); - CHILL_DEBUG_PRINT("%s was already defined in the function body\n", vd->varname); - return vd; // need to check type? - } - } - CHILL_DEBUG_PRINT("not a parameter or variable named %s\n", name); - return NULL; -} - - void chillAST_FunctionDecl::setBody(chillAST_Node *bod) { - //fprintf(stderr, "%s chillAST_FunctionDecl::setBody( 0x%x ) total of %d children\n", functionName, bod, 1+children.size()); if (bod->isCompoundStmt()) body = (chillAST_CompoundStmt *) bod; else { + CHILL_ERROR("Should always be a compound statements"); body = new chillAST_CompoundStmt(); body->addChild(bod); } - //symbol_table = body->getSymbolTable(); - //addChild(bod); bod->setParent(this); // well, ... } @@ -817,10 +734,10 @@ void chillAST_FunctionDecl::addChild(chillAST_Node *node) { void chillAST_FunctionDecl::printParameterTypes(FILE *fp) { // also prints names //fprintf(stderr, "\n\n%s chillAST_FunctionDecl::printParameterTypes()\n", functionName); fprintf(fp, "( "); - int numparameters = parameters.size(); + int numparameters = getSymbolTable()->size(); for (int i = 0; i < numparameters; i++) { if (i != 0) fprintf(fp, ", "); - chillAST_VarDecl *p = parameters[i]; + chillAST_VarDecl *p = (*getSymbolTable())[i]; p->print(0, fp); // note: no indent, as this is in the function parens } fprintf(fp, " )"); // end of input parameters @@ -888,12 +805,11 @@ void chillAST_FunctionDecl::dump(int indent, FILE *fp) { chillindent(indent, fp); fprintf(fp, "(FunctionDecl %s %s(", returnType, functionName); - int numparameters = parameters.size(); + int numparameters = getSymbolTable()->size(); for (int i = 0; i < numparameters; i++) { if (i != 0) fprintf(fp, ", "); - chillAST_VarDecl *p = parameters[i]; - //fprintf(stderr, "param type %s vartype %s\n", p->getTypeString(), p->vartype); - p->print(0, fp); // note: no indent, as this is in the function parens, ALSO print, not dump + chillAST_VarDecl *p = (*getSymbolTable())[i]; + p->print(0, fp); } fprintf(fp, ")\n"); // end of input parameters @@ -911,7 +827,7 @@ void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) { //fprintf(stderr, "chillAST_FunctionDecl::gatherVarDecls()\n"); //if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size()); //fprintf(stderr, "functiondecl has %d parameters\n", numParameters()); - for (int i = 0; i < numParameters(); i++) parameters[i]->gatherVarDecls(decls); + for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherVarDecls(decls); //fprintf(stderr, "after parms, %d decls\n", decls.size()); for (int i = 0; i < children.size(); i++) children[i]->gatherVarDecls(decls); //fprintf(stderr, "after children, %d decls\n", decls.size()); @@ -926,7 +842,7 @@ void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) { void chillAST_FunctionDecl::gatherScalarVarDecls(vector<chillAST_VarDecl *> &decls) { //if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size()); - for (int i = 0; i < numParameters(); i++) parameters[i]->gatherScalarVarDecls(decls); + for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherScalarVarDecls(decls); for (int i = 0; i < children.size(); i++) children[i]->gatherScalarVarDecls(decls); body->gatherScalarVarDecls(decls); // todo, figure out if functiondecl has actual children } @@ -935,7 +851,7 @@ void chillAST_FunctionDecl::gatherScalarVarDecls(vector<chillAST_VarDecl *> &dec void chillAST_FunctionDecl::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decls) { //if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size()); - for (int i = 0; i < numParameters(); i++) parameters[i]->gatherArrayVarDecls(decls); + for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherArrayVarDecls(decls); for (int i = 0; i < children.size(); i++) children[i]->gatherArrayVarDecls(decls); body->gatherArrayVarDecls(decls); // todo, figure out if functiondecl has actual children } @@ -943,11 +859,11 @@ void chillAST_FunctionDecl::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decl chillAST_VarDecl *chillAST_FunctionDecl::findArrayDecl(const char *name) { //fprintf(stderr, "chillAST_FunctionDecl::findArrayDecl( %s )\n", name ); - chillAST_VarDecl *p = hasParameterNamed(name); + chillAST_VarDecl *p = getVariableDeclaration(name); //if (p) fprintf(stderr, "function %s has parameter named %s\n", functionName, name ); if (p && p->isArray()) return p; - chillAST_VarDecl *v = funcHasVariableNamed(name); + chillAST_VarDecl *v = body->getVariableDeclaration(name); //if (v) fprintf(stderr, "function %s has symbol table variable named %s\n", functionName, name ); if (v && v->isArray()) return v; @@ -1060,12 +976,6 @@ bool chillAST_FunctionDecl::findLoopIndexesToReplace(chillAST_SymbolTable *symta chillAST_Node *chillAST_FunctionDecl::constantFold() { - //fprintf(stderr, "chillAST_FunctionDecl::constantFold()\n"); - // parameters can't have constants? - int numparameters = parameters.size(); - for (int i = 0; i < numparameters; i++) { - parameters[i]->constantFold(); - } if (body) body = (chillAST_CompoundStmt *) body->constantFold(); return this; } @@ -1074,8 +984,7 @@ chillAST_MacroDefinition::chillAST_MacroDefinition(const char *mname = NULL, con if (mname) macroName = strdup(mname); else macroName = strdup("UNDEFINEDMACRO"); if(rhs) rhsString = strdup(rhs); else rhsString = NULL; metacomment = NULL; - symbol_table = NULL; - + symbolTable = new chillAST_SymbolTable(); isFromSourceFile = true; // default filename = NULL; }; @@ -1087,7 +996,7 @@ chillAST_Node *chillAST_MacroDefinition::clone() { return this; chillAST_MacroDefinition *clo = new chillAST_MacroDefinition( macroName ); clo->setParent(parent); - for (int i=0; i<parameters.size(); i++) clo->addParameter( parameters[i] ); + for (int i=0; i<parameters.size(); i++) clo->addVariableToScope( parameters[i] ); clo->setBody( body->clone() ); return clo; @@ -1104,27 +1013,6 @@ void chillAST_MacroDefinition::setBody(chillAST_Node *bod) { bod->setParent(this); // well, ... } - -void chillAST_MacroDefinition::addParameter(chillAST_VarDecl *p) { - //fprintf(stderr, "%s chillAST_MacroDefinition::addParameter( 0x%x ) total of %d children\n", functionName, p, 1+children.size()); - parameters.push_back(p); - fprintf(stderr, "macro setting %s isAParameter\n", p->varname); - p->isAParameter = true; - p->setParent(this); - - addVariableToSymbolTable(p); -} - - -chillAST_VarDecl *chillAST_MacroDefinition::hasParameterNamed(const char *name) { - int numparams = parameters.size(); - for (int i = 0; i < numparams; i++) { - if (!strcmp(name, parameters[i]->varname)) return parameters[i]; // need to check type? - } - return NULL; -} - - void chillAST_MacroDefinition::insertChild(int i, chillAST_Node *node) { body->insertChild(i, node); } @@ -1177,7 +1065,7 @@ chillAST_ForStmt::chillAST_ForStmt() { body = new chillAST_CompoundStmt(); conditionoperator = IR_COND_UNKNOWN; - symbol_table = NULL; + symbolTable = new chillAST_SymbolTable(); } @@ -1189,6 +1077,7 @@ chillAST_ForStmt::chillAST_ForStmt(chillAST_Node *ini, chillAST_Node *con, chill init->setParent(this); cond->setParent(this); incr->setParent(this); + symbolTable = new chillAST_SymbolTable(); //fprintf(stderr, "chillAST_ForStmt::chillAST_ForStmt() bod %p\n", bod); @@ -1618,7 +1507,7 @@ bool chillAST_ForStmt::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bo contain->print(0, stderr); contain->insertChild(0, newguy); // TODO ugly order - contain->addVariableToSymbolTable(newguy); // adds to first enclosing symbolTable + contain->addVariableToScope(newguy); // adds to first enclosing symbolTable if (!symbolTableFindName(contain->getSymbolTable(), vname)) { fprintf(stderr, "container doesn't have a var names %s afterwards???\n", vname); @@ -4249,8 +4138,8 @@ void chillAST_CallExpr::dump(int indent, FILE *fp) { callee->dump(indent + 1, fp); if (fd) { - int numparams = fd->parameters.size(); - for (int i = 0; i < numparams; i++) fd->parameters[i]->dump(indent + 1, fp); + int numparams = fd->getSymbolTable()->size(); + for (int i = 0; i < numparams; i++) (*(fd->getSymbolTable()))[i]->dump(indent + 1, fp); } } chillindent(indent, fp); @@ -4390,12 +4279,6 @@ chillAST_VarDecl::chillAST_VarDecl(const char *t, const char *n, const char *a) byreference = false; isABuiltin = false; isRestrict = isDevice = isShared = false; // fprintf(stderr, "RDS = false\n"); - - if (parent) { - //fprintf(stderr, "chillAST_VarDecl::chillAST_VarDecl( %s ), adding to symbol table???\n", varname); - parent->addVariableToSymbolTable(this); // should percolate up until something has a symbol table - - } }; @@ -4428,7 +4311,6 @@ chillAST_VarDecl::chillAST_VarDecl(chillAST_RecordDecl *astruct, const char *nam uniquePtr = NULL; knownArraySizes = false; - //fprintf(stderr, "arraypart len %d\n", strlen(a)); for (int i = 0; i < strlen(array); i++) { if (array[i] == '[') { numdimensions++; @@ -4443,13 +4325,6 @@ chillAST_VarDecl::chillAST_VarDecl(chillAST_RecordDecl *astruct, const char *nam isABuiltin = false; isRestrict = isDevice = isShared = false; // fprintf(stderr, "RDS = false\n"); typedefinition = NULL; - - //fprintf(stderr, "chillAST_VarDecl::chillAST_VarDecl( chillAST_RecordDecl *astruct, ...) MIGHT add struct to some symbol table\n"); - //if (parent) fprintf(stderr, "yep, adding it\n"); - - if (parent) parent->addVariableToSymbolTable(this); // should percolate up until something has a symbol table - - }; @@ -4629,8 +4504,6 @@ chillAST_VarDecl::chillAST_VarDecl(const char *t, const char *n, const char *a, // currently this is bad, because a struct does not have a symbol table, so the // members of a struct are passed up to the func or sourcefile. - if (parent) parent->addVariableToSymbolTable(this); // should percolate up until something has a symbol table - CHILL_DEBUG_PRINT("LEAVING\n"); //parent->print(); fprintf(stderr, "\n\n"); @@ -4801,8 +4674,8 @@ chillAST_RecordDecl *chillAST_VarDecl::getStructDef() { chillAST_CompoundStmt::chillAST_CompoundStmt() { //fprintf(stderr, "chillAST_CompoundStmt::chillAST_CompoundStmt() %p\n", this); parent = NULL; - symbol_table = new chillAST_SymbolTable; - typedef_table = NULL; + symbolTable = new chillAST_SymbolTable; + typedefTable = new chillAST_TypedefTable; }; @@ -5671,3 +5544,35 @@ int chillAST_UnaryOperator::getPrec() { if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1; return INT8_MAX; } + +void chillAST_Node::addVariableToScope(chillAST_VarDecl *vd) { + CHILL_DEBUG_PRINT("addVariableToScope( %s )\n", vd->varname); + if (!symbolTable) return; + symbolTable = addSymbolToTable(symbolTable, vd); + vd->parent = this; +} +void chillAST_Node::addTypedefToScope(chillAST_TypedefDecl *tdd) { + if (!typedefTable) return; + typedefTable = addTypedefToTable(typedefTable, tdd); + tdd->parent = this; +} +chillAST_TypedefDecl* chillAST_Node::findTypeDecleration(const char *t) { + fprintf(stderr, " %s \n", t); + chillAST_TypedefDecl* td = getTypeDeclaration(t); + if (!td && parent) return parent->findTypeDecleration(t); + return td; // should not happen +} +chillAST_VarDecl* chillAST_Node::findVariableDecleration(const char *t) { + fprintf(stderr, " %s \n", t); + chillAST_VarDecl* td = getVariableDeclaration(t); + if (!td && parent) return parent->findVariableDecleration(t); + return td; // should not happen +} + +chillAST_VarDecl* chillAST_Node::getVariableDeclaration(const char *t) { + return symbolTableFindName(getSymbolTable(),t); +} + +chillAST_TypedefDecl* chillAST_Node::getTypeDeclaration(const char *t){ + return typedefTableFindName(getTypedefTable(),t); +} |