diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-26 08:51:47 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-26 08:51:47 -0600 |
commit | eb1ab91e1a12520d825cae60ee49655a0e8c4b94 (patch) | |
tree | b9ed57a06489a57d4bc42cfd7ed3d80b7b1fec1a | |
parent | e7de858ce27ded585d6e2ec4a5cbc3467c6a7593 (diff) | |
download | chill-eb1ab91e1a12520d825cae60ee49655a0e8c4b94.tar.gz chill-eb1ab91e1a12520d825cae60ee49655a0e8c4b94.tar.bz2 chill-eb1ab91e1a12520d825cae60ee49655a0e8c4b94.zip |
Node cleanup
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | include/chillAST/chillAST_def.hh | 23 | ||||
-rw-r--r-- | include/chillAST/chillAST_node.hh | 196 | ||||
-rw-r--r-- | include/chillAST/chillASTs.hh | 13 | ||||
-rw-r--r-- | src/ast/node.cpp | 263 | ||||
-rw-r--r-- | src/chillASTs.cc | 191 | ||||
-rw-r--r-- | src/printer/cfamily.cpp | 11 | ||||
-rw-r--r-- | src/printer/dump.cpp | 1 |
8 files changed, 327 insertions, 375 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 284599f..5d6a26b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,7 +26,10 @@ set(CORE_SRC set(IR_CHILL_SRC src/ir_clang.cc + ) +set(AST_CHILL_SRC src/chillASTs.cc + src/ast/node.cpp ) set(PYTHON_SRC @@ -91,6 +94,7 @@ add_executable(chill ${CORE_SRC} ${PYTHON_SRC} ${IR_CHILL_SRC} + ${AST_CHILL_SRC} ${PRINTER_SRC}) target_link_libraries(chill diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh index f336e14..4ec60aa 100644 --- a/include/chillAST/chillAST_def.hh +++ b/include/chillAST/chillAST_def.hh @@ -23,7 +23,7 @@ enum CHILLAST_NODE_TYPE { CHILLAST_NODE_TYPEDEFDECL, CHILLAST_NODE_VARDECL, // CHILLAST_NODE_PARMVARDECL, not used any more - CHILLAST_NODE_FUNCTIONDECL, + CHILLAST_NODE_FUNCTIONDECL, CHILLAST_NODE_RECORDDECL, // struct or union (or class) CHILLAST_NODE_MACRODEFINITION, CHILLAST_NODE_COMPOUNDSTMT, @@ -41,7 +41,7 @@ enum CHILLAST_NODE_TYPE { CHILLAST_NODE_RETURNSTMT, CHILLAST_NODE_CALLEXPR, //CHILLAST_NODE_DECLSTMT, not used - CHILLAST_NODE_PARENEXPR, + CHILLAST_NODE_PARENEXPR, CHILLAST_NODE_CSTYLECASTEXPR, CHILLAST_NODE_CSTYLEADDRESSOF, CHILLAST_NODE_IFSTMT, @@ -51,7 +51,7 @@ enum CHILLAST_NODE_TYPE { CHILLAST_NODE_PREPROCESSING, // comments, #define, #include, whatever else works CHILLAST_NODE_NOOP, // NO OP // CUDA specific - CHILLAST_NODE_CUDAMALLOC, + CHILLAST_NODE_CUDAMALLOC, CHILLAST_NODE_CUDAFREE, CHILLAST_NODE_CUDAMEMCPY, CHILLAST_NODE_CUDAKERNELCALL, @@ -97,6 +97,7 @@ char *splitTypeInfo(char *underlyingtype); //! change "1024UL" to "1024" char *ulhack(char *brackets); + //! remove __restrict__ , MODIFIES the argument! char *restricthack(char *typeinfo); @@ -106,8 +107,10 @@ extern const char *ChillAST_Node_Names[]; // WARNING MUST BE KEPT IN SYNC WITH // fwd declarations //! the generic node, who specific types are derived from class chillAST_Node; + //! empty node class chillAST_NULL; + //! ast for an entire source file (translationunit) class chillAST_SourceFile; @@ -120,15 +123,19 @@ class chillAST_FunctionDecl; //! structs and unions (and classes?) class chillAST_RecordDecl; + class chillAST_MacroDefinition; //! A sequence of statements class chillAST_CompoundStmt; + //! a For LOOP class chillAST_ForStmt; class chillAST_UnaryOperator; + class chillAST_BinaryOperator; + class chillAST_TernaryOperator; class chillAST_ArraySubscriptExpr; @@ -169,6 +176,7 @@ class chillAST_CudaFree; class chillAST_CudaMemcpy; +// Not implemented class chillAST_CudaKernelCall; class chillAST_CudaSyncthreads; @@ -180,20 +188,21 @@ typedef std::vector<chillAST_VarDecl *> chillAST_SymbolTable; 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); +chillAST_TypedefDecl *typedefTableFindName(chillAST_TypedefTable *table, const char *name); chillAST_Node *lessthanmacro(chillAST_Node *left, chillAST_Node *right); + chillAST_SymbolTable *addSymbolToTable(chillAST_SymbolTable *st, chillAST_VarDecl *vd); + chillAST_TypedefTable *addTypedefToTable(chillAST_TypedefTable *tt, chillAST_TypedefDecl *td); void chillindent(int i, FILE *fp); + void insertNewDeclAtLocationOfOldIfNeeded(chillAST_VarDecl *newdecl, chillAST_VarDecl *olddecl); chillAST_DeclRefExpr *buildDeclRefExpr(chillAST_VarDecl *); diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index 511386d..3ea29d9 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -153,20 +153,17 @@ public: virtual void addTypedefToScope(chillAST_TypedefDecl *tdd); - chillAST_TypedefDecl *findTypeDecleration(const char *t); - - chillAST_VarDecl *findVariableDecleration(const char *t); - + //! Non recursive version that tries to find the declaration in this node chillAST_VarDecl *getVariableDeclaration(const char *vn); + //! Non recursive version that tries to find the declaration in this node 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 + //! Recursive version that will go to parent node if not finding in this + chillAST_VarDecl *findVariableDecleration(const char *t); + //! Recursive version that will go the parent node if not finding in this + chillAST_TypedefDecl *findTypeDecleration(const char *t); int getNumChildren() { return children.size(); }; @@ -181,72 +178,19 @@ 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 - } + virtual void mergeChildInfo(chillAST_Node); - virtual void addChild(chillAST_Node *c) { - c->parent = this; - // check to see if it's already there - for (int i = 0; i < children.size(); i++) { - if (c == children[i]) { - CHILL_ERROR("addchild ALREADY THERE\n"); - return; // already there - } - } - children.push_back(c); - }; // not usually useful - - virtual void addChildren(const chillAST_NodeList &c) { - for (int i = 0; i < c.size(); ++i) { - addChild(c[i]); - } - } + virtual void addChild(chillAST_Node *c); - virtual void insertChild(int i, chillAST_Node *node) { - //fprintf(stderr, "%s inserting child of type %s at location %d\n", getTypeString(), node->getTypeString(), i); - node->parent = this; - children.insert(children.begin() + i, node); - }; + virtual void addChildren(const chillAST_NodeList &c); - virtual void removeChild(int i) { - children.erase(children.begin() + i); - }; + virtual void insertChild(int i, chillAST_Node *node); - int findChild(chillAST_Node *c) { - for (int i = 0; i < children.size(); i++) { - if (children[i] == c) return i; - } - return -1; - } + virtual void removeChild(int i); - virtual void replaceChild(chillAST_Node *old, chillAST_Node *newchild) { - CHILL_DEBUG_PRINT("(%s) forgot to implement replaceChild() ... using generic\n", getTypeString()); - CHILL_DEBUG_PRINT("%d children\n", children.size()); - for (int i = 0; i < children.size(); i++) { - if (children[i] == old) { - children[i] = newchild; - newchild->setParent(this); - return; - } - } - CHILL_ERROR("%s %p generic replaceChild called with oldchild that was not a child\n", - getTypeString(), this); - CHILL_DEBUG_BEGIN - fprintf(stderr, "printing\n"); - print(); - fprintf(stderr, "\nchild: "); - if (!old) fprintf(stderr, "oldchild NULL!\n"); - old->print(); - fprintf(stderr, "\nnew: "); - newchild->print(); - fprintf(stderr, "\n"); - CHILL_DEBUG_END - exit(-1); - }; + int findChild(chillAST_Node *c); + + virtual void replaceChild(chillAST_Node *old, chillAST_Node *newchild); //! Spread the loop across a bunch of cores that will each calculate its own loop variable. /*! @@ -262,12 +206,7 @@ public: * * @param var */ - virtual void loseLoopWithLoopVar(char *var) { - std::vector<chillAST_Node *> dupe = children; // simple enough? - for (int i = 0; i < dupe.size(); i++) { // recurse on all children - dupe[i]->loseLoopWithLoopVar(var); - } - } + virtual void loseLoopWithLoopVar(char *var); virtual int evalAsInt() { CHILL_ERROR("(%s) can't be evaluated as an integer??\n", getTypeString()); @@ -314,37 +253,13 @@ public: }; //! recursive walk parent links, looking for loops, and grabbing the declRefExpr in the loop init and cond. - virtual void gatherLoopIndeces( - std::vector<chillAST_VarDecl *> &indeces) { - // you can quit when you get to certain nodes - - CHILL_DEBUG_PRINT("%s::gatherLoopIndeces()\n", getTypeString()); - - if (isSourceFile() || isFunctionDecl()) return; // end of the line - - if (!parent) return; // should not happen, but be careful - - // for most nodes, this just recurses upwards - parent->gatherLoopIndeces(indeces); - } + virtual void gatherLoopIndeces(std::vector<chillAST_VarDecl *> &indeces); //! recursive walk parent links, looking for loops - chillAST_ForStmt *findContainingLoop() { - CHILL_DEBUG_PRINT("%s::findContainingLoop() ", getTypeString()); - if (!parent) return NULL; - if (parent->isForStmt()) return (chillAST_ForStmt *) parent; - return parent->findContainingLoop(); // recurse upwards - } + chillAST_ForStmt *findContainingLoop(); //! recursive walk parent links, avoiding loops - chillAST_Node *findContainingNonLoop() { - fprintf(stderr, "%s::findContainingNonLoop() ", getTypeString()); - if (!parent) return NULL; - if (parent->isCompoundStmt() && parent->getParent()->isForStmt()) - return parent->getParent()->findContainingNonLoop(); // keep recursing - if (parent->isForStmt()) return parent->findContainingNonLoop(); // keep recursing - return (chillAST_Node *) parent; // return non-loop - } + chillAST_Node *findContainingNonLoop(); // TODO gather loop init and cond (and if cond) like gatherloopindeces @@ -415,87 +330,22 @@ public: fprintf(fp, "(%s) forgot to implement printName()\n", getTypeString()); };// print CODE - //! The AST's print version, for reason unknown and incorrect - virtual char *stringRep(int indent = 0) { - fflush(stdout); - // TODO chillindent(indent, fp); - CHILL_ERROR("(%s) forgot to implement stringRep()\n", getTypeString()); - exit(-1); - } + virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops); - virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) { - int n = children.size(); - for (int i = 0; i < n; i++) { - if (children[i]->isForStmt()) { - loops.push_back(((chillAST_ForStmt *) (children[i]))); - } - } - } + virtual void repairParentChild(); - - virtual void repairParentChild() { // for nodes where all subnodes are children - int n = children.size(); - for (int i = 0; i < n; i++) { - if (children[i]->parent != this) { - fprintf(stderr, "fixing child %s that didn't know its parent\n", children[i]->getTypeString()); - children[i]->parent = this; - } - } - } - - - virtual void - get_deep_loops(std::vector<chillAST_ForStmt *> &loops) { // this is probably broken - returns ALL loops under it - int n = children.size(); - //fprintf(stderr, "get_deep_loops of a %s with %d children\n", getTypeString(), n); - for (int i = 0; i < n; i++) { - //fprintf(stderr, "child %d is a %s\n", i, children[i]->getTypeString()); - children[i]->get_deep_loops(loops); - } - //fprintf(stderr, "found %d deep loops\n", loops.size()); - } + virtual void get_deep_loops(std::vector<chillAST_ForStmt *> &loops); // generic for chillAST_Node with children - virtual void find_deepest_loops(std::vector<chillAST_ForStmt *> &loops) { // returns DEEPEST nesting of loops - std::vector<chillAST_ForStmt *> deepest; // deepest below here - - int n = children.size(); - //fprintf(stderr, "find_deepest_loops of a %s with %d children\n", getTypeString(), n); - for (int i = 0; i < n; i++) { - std::vector<chillAST_ForStmt *> subloops; // loops below here among a child of mine - - //fprintf(stderr, "child %d is a %s\n", i, children[i]->getTypeString()); - children[i]->find_deepest_loops(subloops); - - if (subloops.size() > deepest.size()) { - deepest = subloops; - } - } - - // append deepest we see at this level to loops - for (int i = 0; i < deepest.size(); i++) { - loops.push_back(deepest[i]); - } - - //fprintf(stderr, "found %d deep loops\n", loops.size()); - - } - + virtual void find_deepest_loops(std::vector<chillAST_ForStmt *> &loops); void setParent(chillAST_Node *p) { parent = p; }; chillAST_Node *getParent() { return parent; }; //! This will be ideally replaced by call at to the top level - chillAST_SourceFile *getSourceFile() { - if (isSourceFile()) return ((chillAST_SourceFile *) this); - if (parent != NULL) return parent->getSourceFile(); - CHILL_ERROR("UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n", - this, this->getTypeString()); - this->print(); - exit(-1); - } + chillAST_SourceFile *getSourceFile(); // TODO DOC virtual chillAST_Node *getEnclosingStatement(int level = 0); diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index 8e5cee0..4ff70c9 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -227,8 +227,6 @@ public: }; // required methods that I can't seem to get to inherit - char *stringRep(int indent = 0); - chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -498,15 +496,10 @@ public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_MACRODEFINITION; } char *macroName; - char *rhsString; // parameters - these will be odd, in that they HAVE NO TYPE void setName(char *n) { macroName = strdup(n); /* probable memory leak */ }; - void setRhsString(char *n) { rhsString = strdup(n); /* probable memory leak */ }; - - char *getRhsString() { return rhsString; } - chillAST_MacroDefinition(const char *name, const char *rhs); void addChild(chillAST_Node *node); // special because inserts into BODY @@ -808,8 +801,6 @@ public: // required methods that I can't seem to get to inherit - char *stringRep(int indent = 0); - chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -879,8 +870,6 @@ public: void replaceChild(chillAST_Node *old, chillAST_Node *newchild); // will examine index // required methods that I can't seem to get to inherit - char *stringRep(int indent = 0); - chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -941,8 +930,6 @@ public: bool operator==(const chillAST_MemberExpr &); // required methods that I can't seem to get to inherit - char *stringRep(int indent = 0); - chillAST_Node *constantFold(); chillAST_Node *clone(); diff --git a/src/ast/node.cpp b/src/ast/node.cpp new file mode 100644 index 0000000..ea74159 --- /dev/null +++ b/src/ast/node.cpp @@ -0,0 +1,263 @@ +// +// Created by ztuowen on 9/26/16. +// + +#include "chillAST.h" +#include "printer/dump.h" +#include "printer/cfamily.h" + +void chillAST_Node::mergeChildInfo(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 +} + +void chillAST_Node::addChild(chillAST_Node *c) { + c->parent = this; + // check to see if it's already there + for (int i = 0; i < children.size(); i++) { + if (c == children[i]) { + CHILL_ERROR("addchild ALREADY THERE\n"); + return; // already there + } + } + children.push_back(c); +}; // not usually useful + +void chillAST_Node::addChildren(const chillAST_NodeList &c) { + for (int i = 0; i < c.size(); ++i) { + addChild(c[i]); + } +} + +void chillAST_Node::insertChild(int i, chillAST_Node *node) { + //fprintf(stderr, "%s inserting child of type %s at location %d\n", getTypeString(), node->getTypeString(), i); + node->parent = this; + children.insert(children.begin() + i, node); +}; + +void chillAST_Node::removeChild(int i) { + children.erase(children.begin() + i); +}; + +int chillAST_Node::findChild(chillAST_Node *c) { + for (int i = 0; i < children.size(); i++) { + if (children[i] == c) return i; + } + return -1; +} + +void chillAST_Node::replaceChild(chillAST_Node *old, chillAST_Node *newchild) { + CHILL_DEBUG_PRINT("(%s) forgot to implement replaceChild() ... using generic\n", getTypeString()); + CHILL_DEBUG_PRINT("%d children\n", children.size()); + for (int i = 0; i < children.size(); i++) { + if (children[i] == old) { + children[i] = newchild; + newchild->setParent(this); + return; + } + } + CHILL_ERROR("%s %p generic replaceChild called with oldchild that was not a child\n", + getTypeString(), this); + CHILL_DEBUG_BEGIN + fprintf(stderr, "printing\n"); + print(); + fprintf(stderr, "\nchild: "); + if (!old) fprintf(stderr, "oldchild NULL!\n"); + old->print(); + fprintf(stderr, "\nnew: "); + newchild->print(); + fprintf(stderr, "\n"); + CHILL_DEBUG_END + exit(-1); +}; + +void chillAST_Node::loseLoopWithLoopVar(char *var) { + std::vector<chillAST_Node *> dupe = children; // simple enough? + for (int i = 0; i < dupe.size(); i++) { // recurse on all children + dupe[i]->loseLoopWithLoopVar(var); + } +} + +//! recursive walk parent links, looking for loops, and grabbing the declRefExpr in the loop init and cond. +void chillAST_Node::gatherLoopIndeces( + std::vector<chillAST_VarDecl *> &indeces) { + // you can quit when you get to certain nodes + + CHILL_DEBUG_PRINT("%s::gatherLoopIndeces()\n", getTypeString()); + + if (isSourceFile() || isFunctionDecl()) return; // end of the line + + if (!parent) return; // should not happen, but be careful + + // for most nodes, this just recurses upwards + parent->gatherLoopIndeces(indeces); +} + +//! recursive walk parent links, looking for loops +chillAST_ForStmt *chillAST_Node::findContainingLoop() { + CHILL_DEBUG_PRINT("%s::findContainingLoop() ", getTypeString()); + if (!parent) return NULL; + if (parent->isForStmt()) return (chillAST_ForStmt *) parent; + return parent->findContainingLoop(); // recurse upwards +} + +chillAST_Node *chillAST_Node::findContainingNonLoop() { + fprintf(stderr, "%s::findContainingNonLoop() ", getTypeString()); + if (!parent) return NULL; + if (parent->isCompoundStmt() && parent->getParent()->isForStmt()) + return parent->getParent()->findContainingNonLoop(); // keep recursing + if (parent->isForStmt()) return parent->findContainingNonLoop(); // keep recursing + return (chillAST_Node *) parent; // return non-loop +} + +void chillAST_Node::getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) { + int n = children.size(); + for (int i = 0; i < n; i++) { + if (children[i]->isForStmt()) { + loops.push_back(((chillAST_ForStmt *) (children[i]))); + } + } +} + + +void chillAST_Node::repairParentChild() { // for nodes where all subnodes are children + int n = children.size(); + for (int i = 0; i < n; i++) { + if (children[i]->parent != this) { + fprintf(stderr, "fixing child %s that didn't know its parent\n", children[i]->getTypeString()); + children[i]->parent = this; + } + } +} + + +void chillAST_Node::get_deep_loops( + std::vector<chillAST_ForStmt *> &loops) { // this is probably broken - returns ALL loops under it + int n = children.size(); + //fprintf(stderr, "get_deep_loops of a %s with %d children\n", getTypeString(), n); + for (int i = 0; i < n; i++) { + //fprintf(stderr, "child %d is a %s\n", i, children[i]->getTypeString()); + children[i]->get_deep_loops(loops); + } + //fprintf(stderr, "found %d deep loops\n", loops.size()); +} + + +// generic for chillAST_Node with children +void chillAST_Node::find_deepest_loops(std::vector<chillAST_ForStmt *> &loops) { // returns DEEPEST nesting of loops + std::vector<chillAST_ForStmt *> deepest; // deepest below here + + int n = children.size(); + //fprintf(stderr, "find_deepest_loops of a %s with %d children\n", getTypeString(), n); + for (int i = 0; i < n; i++) { + std::vector<chillAST_ForStmt *> subloops; // loops below here among a child of mine + + //fprintf(stderr, "child %d is a %s\n", i, children[i]->getTypeString()); + children[i]->find_deepest_loops(subloops); + + if (subloops.size() > deepest.size()) { + deepest = subloops; + } + } + + // append deepest we see at this level to loops + for (int i = 0; i < deepest.size(); i++) { + loops.push_back(deepest[i]); + } + + //fprintf(stderr, "found %d deep loops\n", loops.size()); + +} + +chillAST_SourceFile *chillAST_Node::getSourceFile() { + if (isSourceFile()) return ((chillAST_SourceFile *) this); + if (parent != NULL) return parent->getSourceFile(); + CHILL_ERROR("UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n", + this, this->getTypeString()); + this->print(); + exit(-1); +} + +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) { + chillAST_VarDecl *vd = symbolTableFindName(getSymbolTable(), t); + if (!vd) vd = getParameter(t); + return vd; +} + +chillAST_TypedefDecl *chillAST_Node::getTypeDeclaration(const char *t) { + return typedefTableFindName(getTypedefTable(), t); +} + +void chillAST_Node::addParameter(chillAST_VarDecl *vd) { + if (!parameters) { + CHILL_ERROR("Calling addParameter on construct without parameters"); + exit(-1); + } + + if (symbolTableFindName(getParameters(), vd->varname)) { // NOT recursive. just in FunctionDecl + CHILL_ERROR("parameter %s already exists?\n", vd->varname); + return; + } + + CHILL_DEBUG_PRINT("setting %s isAParameter\n", vd->varname); + getParameters()->push_back(vd); + vd->isAParameter = true; + vd->setParent(this); // this is a combined list! +} + +chillAST_VarDecl *chillAST_Node::getParameter(const char *t) { + return symbolTableFindName(getParameters(), t); +} + +void chillAST_Node::dump(int indent, FILE *fp) { + if (fp == stderr) { + chill::printer::Dump d; + d.printErr("", this); + fprintf(stderr, "\n"); + } else { + chill::printer::Dump d; + d.printOut("", this); + fprintf(stdout, "\n"); + } +} + +void chillAST_Node::print(int indent, FILE *fp) { + if (fp == stderr) { + chill::printer::CFamily d; + d.printErr("", this); + fprintf(stderr, "\n"); + } else { + chill::printer::CFamily d; + d.printOut("", this); + fprintf(stdout, "\n"); + } +} diff --git a/src/chillASTs.cc b/src/chillASTs.cc index bee56df..7654ecf 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -55,8 +55,8 @@ const char *ChillAST_Node_Names[] = { "fake3" }; -bool streq(const char * a, const char * b) { - return !strcmp(a,b); +bool streq(const char *a, const char *b) { + return !strcmp(a, b); } //! Parse to the most basic type @@ -75,7 +75,7 @@ char *parseUnderlyingType(const char *sometype) { while (*p != '[') --p; else break; - *(p+1) = '\0'; + *(p + 1) = '\0'; return underlying; } @@ -136,7 +136,7 @@ chillAST_VarDecl *variableDeclFindSubpart(chillAST_VarDecl *decl, const char *na CHILL_DEBUG_PRINT("DIDN'T FIND a struct member named %s\n", varname); if (!subpart) return sp; - return variableDeclFindSubpart(sp,subpart); // return the subpart?? + return variableDeclFindSubpart(sp, subpart); // return the subpart?? } else { CHILL_ERROR("no recordDecl\n"); exit(-1); @@ -161,12 +161,11 @@ chillAST_VarDecl *symbolTableFindVariableNamed(chillAST_SymbolTable *table, cons if (!subpart) return vd; - return variableDeclFindSubpart(vd,subpart); + return variableDeclFindSubpart(vd, subpart); } //! remove UL from numbers, MODIFIES the argument! -char *ulhack(char *brackets) -{ +char *ulhack(char *brackets) { CHILL_DEBUG_PRINT("ulhack( \"%s\" -> \n", brackets); int len = strlen(brackets); for (int i = 0; i < len - 2; i++) { @@ -183,8 +182,7 @@ char *ulhack(char *brackets) //! remove __restrict__ , MODIFIES the argument! -char *restricthack(char *typeinfo) -{ +char *restricthack(char *typeinfo) { CHILL_DEBUG_PRINT("restricthack( \"%s\" -> \n", typeinfo); std::string r("__restrict__"); std::string t(typeinfo); @@ -244,52 +242,8 @@ bool isRestrict(const char *sometype) { // does not modify sometype void chillindent(int howfar, FILE *fp) { for (int i = 0; i < howfar; i++) fprintf(fp, " "); } -chillAST_VarDecl *chillAST_Node::findVariableNamed(const char *name) { // generic, recursive - CHILL_DEBUG_PRINT("nodetype %s findVariableNamed( %s )\n", getTypeString(), name); - 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) { - 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); - } - if (!parent) { - CHILL_DEBUG_PRINT("%s has no parent\n", getTypeString()); - return NULL; // no more recursion available - } - // recurse upwards - //fprintf(stderr, "recursing from %s up to parent %p\n", getTypeString(), parent); - CHILL_DEBUG_PRINT("recursing from %s up to parent\n", getTypeString()); - return parent->findVariableNamed(name); -} - - -chillAST_RecordDecl *chillAST_Node::findRecordDeclNamed(const char *name) { // recursive - fprintf(stderr, "%s::findRecordDeclNamed( %s )\n", getTypeString(), name); - // look in children - int numchildren = children.size(); - fprintf(stderr, "%d children\n", numchildren); - for (int i = 0; i < numchildren; i++) { - fprintf(stderr, "child %d %s\n", i, children[i]->getTypeString()); - if (children[i]->isRecordDecl()) { - chillAST_RecordDecl *RD = (chillAST_RecordDecl *) children[i]; - fprintf(stderr, "it is a recordDecl named '%s' vs '%s'\n", RD->getName(), name); - if (!strcmp(RD->getName(), name)) { - fprintf(stderr, "FOUND IT\n"); - return RD; - } - } - } - - if (!parent) return NULL; // no more recursion available - // recurse upwards - return parent->findRecordDeclNamed(name); -} - chillAST_SourceFile::chillAST_SourceFile(const char *filename) { - if(filename) SourceFileName = strdup(filename); + if (filename) SourceFileName = strdup(filename); else SourceFileName = strdup("No Source File"); symbolTable = new chillAST_SymbolTable(); typedefTable = new chillAST_TypedefTable(); @@ -683,7 +637,6 @@ chillAST_Node *chillAST_FunctionDecl::constantFold() { chillAST_MacroDefinition::chillAST_MacroDefinition(const char *mname = NULL, const char *rhs = NULL) { if (mname) macroName = strdup(mname); else macroName = strdup("UNDEFINEDMACRO"); - if(rhs) rhsString = strdup(rhs); else rhsString = NULL; metacomment = NULL; parameters = new chillAST_SymbolTable(); isFromSourceFile = true; // default @@ -695,10 +648,10 @@ chillAST_Node *chillAST_MacroDefinition::clone() { CHILL_ERROR("cloning a macro makes no sense\n"); return this; - chillAST_MacroDefinition *clo = new chillAST_MacroDefinition( macroName ); + chillAST_MacroDefinition *clo = new chillAST_MacroDefinition(macroName); clo->setParent(parent); - for (int i=0; i<parameters->size(); i++) clo->addVariableToScope( (*parameters)[i] ); - clo->setBody( body->clone() ); + for (int i = 0; i < parameters->size(); i++) clo->addVariableToScope((*parameters)[i]); + clo->setBody(body->clone()); return clo; } @@ -710,7 +663,6 @@ void chillAST_MacroDefinition::setBody(chillAST_Node *bod) { fprintf(stderr, "body is:\n"); body->print(0, stderr); fprintf(stderr, "\n\n"); - rhsString = body->stringRep(); bod->setParent(this); // well, ... } @@ -1064,7 +1016,7 @@ bool chillAST_ForStmt::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bo // find vardecl for named preferred index. it has to already exist fprintf(stderr, "RIGHT NOW, change all the references that this loop wants swapped out \n"); - chillAST_VarDecl *newguy = findVariableNamed(vname); // recursive + chillAST_VarDecl *newguy = findVariableDecleration(vname); // recursive if (!newguy) { fprintf(stderr, "there was no variable named %s anywhere I could find\n", vname); } @@ -1378,11 +1330,6 @@ chillAST_IntegerLiteral *chillAST_BinaryOperator::evalAsIntegerLiteral() { return new chillAST_IntegerLiteral(evalAsInt()); // ?? } -char *chillAST_BinaryOperator::stringRep(int indent) { - std::string s = string(lhs->stringRep()) + " " + op + " " + string(lhs->stringRep()); - return strdup(s.c_str()); -} - class chillAST_Node *chillAST_BinaryOperator::constantFold() { //fprintf(stderr, "\nchillAST_BinaryOperator::constantFold() "); //print(0,stderr); fprintf(stderr, "\n"); @@ -1691,7 +1638,8 @@ void chillAST_TernaryOperator::gatherScalarRefs(std::vector<chillAST_DeclRefExpr rhs->gatherScalarRefs(refs, 0); } -chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr(chillAST_Node *bas, chillAST_Node *indx, bool writtento, void *unique) { +chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr(chillAST_Node *bas, chillAST_Node *indx, bool writtento, + void *unique) { base = index = NULL; basedecl = NULL; //fprintf(stderr, "setting basedecl NULL for ASE %p\n", this); imwrittento = writtento; // ?? @@ -1804,17 +1752,6 @@ void chillAST_ArraySubscriptExpr::gatherIndeces(std::vector<chillAST_Node *> &in ind.push_back(index); } -char *chillAST_ArraySubscriptExpr::stringRep(int indent) { - fprintf(stderr, "chillAST_ArraySubscriptExpr::stringRep\n"); - - char *blurb; - char *b = base->stringRep(0); - char *i = index->stringRep(0); - std::string s = string(b) + "[" + string(i) + "]"; - fprintf(stderr, "ASE stringrep %s\n", s.c_str()); - return strdup(s.c_str()); -} - chillAST_VarDecl *chillAST_ArraySubscriptExpr::multibase() { //this should probably be a chillAST_Node function instead of having all these ifs return base->multibase(); @@ -2010,24 +1947,6 @@ chillAST_MemberExpr::chillAST_MemberExpr(chillAST_Node *bas, const char *mem, vo // TODO member can be another member expression, Right? -char *chillAST_MemberExpr::stringRep(int indent) { // char pointer to what we'd print - fprintf(stderr, "*chillAST_MemberExpr::stringRep()\n"); - if (base->isDeclRefExpr()) { // - chillAST_VarDecl *vd = (chillAST_VarDecl *) ((chillAST_DeclRefExpr *) base)->decl; - char *leak = (char *) malloc(128); - if (exptype == CHILLAST_MEMBER_EXP_ARROW) sprintf(leak, "%s->%s", vd->varname, member); - else sprintf(leak, "%s.%s", vd->varname, member); - printstring = leak; - return leak; - } - - - // else - // TODO - return strdup("chillAST_MemberExpr::getStringRep()hadanerror"); -} - - class chillAST_Node *chillAST_MemberExpr::constantFold() { base = base->constantFold(); //member = member->constantFold(); @@ -2197,10 +2116,6 @@ chillAST_DeclRefExpr *buildDeclRefExpr(chillAST_VarDecl *vd) { return dre; } -char *chillAST_DeclRefExpr::stringRep(int indent) { - return strdup(declarationName); -} - class chillAST_Node *chillAST_DeclRefExpr::constantFold() { // can never do anything? return this; } @@ -4092,81 +4007,3 @@ chillAST_Preprocessing::chillAST_Preprocessing(CHILLAST_PREPROCESSING_POSITION p blurb = strdup(text); } -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) { - chillAST_VarDecl* vd = symbolTableFindName(getSymbolTable(),t); - if (!vd) vd = getParameter(t); - return vd; -} - -chillAST_TypedefDecl* chillAST_Node::getTypeDeclaration(const char *t){ - return typedefTableFindName(getTypedefTable(),t); -} - -void chillAST_Node::addParameter(chillAST_VarDecl *vd) { - if (!parameters) { - CHILL_ERROR("Calling addParameter on construct without parameters"); - exit(-1); - } - - if (symbolTableFindName(getParameters(), vd->varname)) { // NOT recursive. just in FunctionDecl - CHILL_ERROR("parameter %s already exists?\n", vd->varname); - return; - } - - CHILL_DEBUG_PRINT("setting %s isAParameter\n", vd->varname); - getParameters()->push_back(vd); - vd->isAParameter = true; - vd->setParent(this); // this is a combined list! -} - -chillAST_VarDecl* chillAST_Node::getParameter(const char *t) { - return symbolTableFindName(getParameters(),t); -} - -void chillAST_Node::dump(int indent, FILE *fp) { - if (fp == stderr) { - chill::printer::Dump d; - d.printErr("",this); - fprintf(stderr,"\n"); - } else { - chill::printer::Dump d; - d.printOut("",this); - fprintf(stdout,"\n"); - } -} - -void chillAST_Node::print(int indent, FILE *fp) { - if (fp == stderr) { - chill::printer::CFamily d; - d.printErr("",this); - fprintf(stderr,"\n"); - } else { - chill::printer::CFamily d; - d.printOut("",this); - fprintf(stdout,"\n"); - } -}
\ No newline at end of file diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp index e1e2b9a..299c81c 100644 --- a/src/printer/cfamily.cpp +++ b/src/printer/cfamily.cpp @@ -160,10 +160,13 @@ void CFamily::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o } void CFamily::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { - // Althedigits contaminates the result - o << showpoint << n->value; - if (n->getPrecision() == 1) - o << "f"; + if (n->allthedigits) + o<<n->allthedigits; + else { + o << showpoint << n->value; + if (n->getPrecision() == 1) + o << "f"; + } } void CFamily::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) { diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp index 66bcc7e..a88f472 100644 --- a/src/printer/dump.cpp +++ b/src/printer/dump.cpp @@ -159,7 +159,6 @@ void Dump::printS(std::string ident, chillAST_MacroDefinition *n, std::ostream & o << n->macroName << " "; dumpVector(this, ident, n->getParameters(), o); print(ident, n->getBody(), o); - if (n->rhsString) o << " (aka " << n->rhsString << ") "; } void Dump::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) { |