From f7c9c6e95f5a7333db5cef9d92c6c3438adbd6ee Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Sat, 1 Oct 2016 11:29:57 -0600 Subject: fixes --- CMakeLists.txt | 2 +- include/chillAST/chillAST_node.hh | 3 +- include/chillAST/chillASTs.hh | 229 ++++++++++++------------------- lib/chillcg/src/CG_chillBuilder.cc | 6 +- src/ast/node.cpp | 76 ++++++++--- src/chillASTs.cc | 268 +++++++------------------------------ src/printer/cfamily.cpp | 34 +++-- src/printer/dump.cpp | 12 +- 8 files changed, 217 insertions(+), 413 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d6a26b..5c6226a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ set(clang_libs set(COMMON_FLAGS "-DCHILL_BUILD_DATE=\"${build_date}\" -DCHILL_BUILD_VERSION=\"\\\"${CHILL_VERSION}\\\"\"") -set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} ${CMAKE_CXX_FLAGS}") +set(CMAKE_CXX_FLAGS "-std=c++11 ${COMMON_FLAGS} ${CMAKE_CXX_FLAGS}") if (DEFINED OMEGAHOME) link_directories(${OMEGAHOME}/lib) diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index ce099e8..dcaa9d0 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -83,7 +83,7 @@ public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_UNKNOWN; }; //! Get the human readable type name - const char *getTypeString() { return ChillAST_Node_Names[getType()]; }; + const char *getTypeString(); bool isSourceFile() { return (getType() == CHILLAST_NODE_SOURCEFILE); }; @@ -300,7 +300,6 @@ public: //! gather both scalar and array references virtual void gatherDeclRefExprs(std::vector &refs); - virtual void gatherVarUsage(std::vector &decls); //! Gather all variable that is used as a lefthand side operand diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index bbfeba4..ca3816f 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -24,6 +24,7 @@ public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_PREPROCESSING; } chillAST_Preprocessing(); + chillAST_Preprocessing(CHILLAST_PREPROCESSING_POSITION pos, CHILLAST_PREPROCESSING_TYPE t, char *text); }; @@ -400,9 +401,14 @@ public: void addDecl(chillAST_VarDecl *vd); // just adds to symbol table?? TODO - void setBody(chillAST_Node *bod) { if (bod->isCompoundStmt()) { children[0] = bod; bod->setParent(this); }} + void setBody(chillAST_Node *bod) { + if (bod->isCompoundStmt()) { + children[0] = bod; + bod->setParent(this); + } + } - chillAST_CompoundStmt *getBody() { return (chillAST_CompoundStmt*)children[0]; } + chillAST_CompoundStmt *getBody() { return (chillAST_CompoundStmt *) children[0]; } void gatherVarDecls(std::vector &decls); @@ -411,8 +417,6 @@ public: void gatherArrayVarDecls(std::vector &decls); chillAST_VarDecl *findArrayDecl(const char *name); - //void gatherArrayRefs( std::vector &refs, bool writtento ); - //void gatherScalarRefs( std::vector &refs, bool writtento ) void gatherVarUsage(std::vector &decls); @@ -458,23 +462,16 @@ public: void addMacro(chillAST_MacroDefinition *md) { macrodefinitions.push_back(md); - //fprintf(stderr, "addMacro(), now %d macros\n", macrodefinitions.size()); } void addFunc(chillAST_FunctionDecl *fd) { - //fprintf(stderr, "chillAST_SourceFile::addFunc( %s %p)\n", fd->functionName, fd); - bool already = false; for (int i = 0; i < functions.size(); i++) { - //fprintf(stderr, "function %d is %s %p\n", i, functions[i]->functionName, functions[i]); if (functions[i] == fd) { - //fprintf(stderr, "function %s was already in source functions\n", fd->functionName); already = true; } } if (!already) functions.push_back(fd); - - // PROBABLY fd was created with sourcefile as its parent. Don't add it twice addChild((chillAST_Node *) fd); } @@ -489,14 +486,10 @@ public: char *macroName; - // parameters - these will be odd, in that they HAVE NO TYPE void setName(char *n) { macroName = strdup(n); /* probable memory leak */ }; chillAST_MacroDefinition(const char *name, const char *rhs); - void addChild(chillAST_Node *node); // special because inserts into BODY - void insertChild(int i, chillAST_Node *node); // special because inserts into BODY - void setBody(chillAST_Node *bod); chillAST_Node *getBody() { return (body); } @@ -504,23 +497,9 @@ public: chillAST_Node *clone(); // none of these make sense for macros - void gatherVarDecls(std::vector &decls) {}; - - void gatherScalarVarDecls(std::vector &decls) {}; - - void gatherArrayVarDecls(std::vector &decls) {}; - chillAST_VarDecl *findArrayDecl(const char *name) {}; - void gatherVarUsage(std::vector &decls) {}; - - void gatherDeclRefExprs(std::vector &refs) {}; - - void cleanUpVarDecls(); - bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) {}; - - chillAST_Node *constantFold() {}; }; class chillAST_ForStmt : public chillAST_Node { @@ -543,16 +522,20 @@ public: void removeSyncComment(); chillAST_Node *getInit() { return getChild(0); }; - void setInit(chillAST_Node *i) { setChild(0,i); } + + void setInit(chillAST_Node *i) { setChild(0, i); } chillAST_Node *getCond() { return getChild(1); }; - void setCond(chillAST_Node *c) { setChild(1,c); } + + void setCond(chillAST_Node *c) { setChild(1, c); } chillAST_Node *getInc() { return getChild(2); }; - void setInc(chillAST_Node *i) { setChild(2,i); } + + void setInc(chillAST_Node *i) { setChild(2, i); } chillAST_Node *getBody() { return getChild(3); }; - void setBody(chillAST_Node *b) { setChild(3,b); }; + + void setBody(chillAST_Node *b) { setChild(3, b); }; bool isNotLeaf() { return true; }; @@ -635,11 +618,11 @@ public: chillAST_Node *getLHS() { return getChild(2); }; - void setCond(chillAST_Node *newc) { setChild(0,newc); } + void setCond(chillAST_Node *newc) { setChild(0, newc); } - void setLHS(chillAST_Node *newlhs) { setChild(1,newlhs); } + void setLHS(chillAST_Node *newlhs) { setChild(1, newlhs); } - void setRHS(chillAST_Node *newrhs) { setChild(2,newrhs); } + void setRHS(chillAST_Node *newrhs) { setChild(2, newrhs); } // required methods that I can't seem to get to inherit chillAST_Node *constantFold(); @@ -662,6 +645,7 @@ public: // constructors chillAST_BinaryOperator(); + chillAST_BinaryOperator(chillAST_Node *lhs, const char *op, chillAST_Node *rhs); // other methods particular to this type of node @@ -677,9 +661,9 @@ public: chillAST_Node *getRHS() { return getChild(1); }; - void setLHS(chillAST_Node *newlhs) { setChild(0,newlhs); } + void setLHS(chillAST_Node *newlhs) { setChild(0, newlhs); } - void setRHS(chillAST_Node *newrhs) { setChild(1,newrhs); } + void setRHS(chillAST_Node *newrhs) { setChild(1, newrhs); } char *getOp() { return op; }; // dangerous. could get changed! bool isAugmentedAssignmentOp() { @@ -718,7 +702,6 @@ public: bool isStructOp() { return (!strcmp(op, ".")) || (!strcmp(op, "->")); }; - // required methods that I can't seem to get to inherit chillAST_Node *constantFold(); @@ -882,18 +865,6 @@ public: chillAST_Node *clone(); - void gatherArrayRefs(std::vector &refs, bool w) {}; // does nothing - void gatherScalarRefs(std::vector &refs, bool writtento) {}; // does nothing - - void gatherVarDecls(std::vector &decls) {}; // does nothing - - void gatherScalarVarDecls(std::vector &decls) {}; // does nothing - void gatherArrayVarDecls(std::vector &decls) {}; // does nothing - - void gatherVarUsage(std::vector &decls) {}; // does nothing - void gatherDeclRefExprs(std::vector &refs) {}; // does nothing - void replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl) {}; - bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here @@ -926,17 +897,6 @@ public: chillAST_Node *clone(); - void gatherArrayRefs(std::vector &refs, bool w) {}; // does nothing - void gatherScalarRefs(std::vector &refs, bool writtento) {}; // does nothing - - void gatherVarDecls(std::vector &decls) {}; // does nothing - void gatherScalarVarDecls(std::vector &decls) {}; // does nothing ; - void gatherArrayVarDecls(std::vector &decls) {}; // does nothing ; - - void gatherVarUsage(std::vector &decls) {}; // does nothing - void gatherDeclRefExprs(std::vector &refs) {}; // does nothing - void replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl) {}; - bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here chillAST_Node *findref() { return this; };// find the SINGLE constant or data reference at this node or below @@ -955,6 +915,7 @@ public: // constructors chillAST_UnaryOperator(); + chillAST_UnaryOperator(const char *oper, bool pre, chillAST_Node *sub); // other methods particular to this type of node @@ -963,8 +924,9 @@ public: (!strcmp(op, "--"))); // are there more ??? TODO } - chillAST_Node* getSubExpr() { return getChild(0); } - void setSubExpr(chillAST_Node* sub) { setChild(0,sub); } + chillAST_Node *getSubExpr() { return getChild(0); } + + void setSubExpr(chillAST_Node *sub) { setChild(0, sub); } // required methods that I can't seem to get to inherit chillAST_Node *constantFold(); @@ -989,6 +951,7 @@ public: // constructors chillAST_ImplicitCastExpr(); + chillAST_ImplicitCastExpr(chillAST_Node *sub); // other methods particular to this type of node @@ -996,8 +959,9 @@ public: bool isLeaf() { return false; }; - chillAST_Node* getSubExpr() { return getChild(0); } - void setSubExpr(chillAST_Node* sub) { return setChild(0,sub); } + chillAST_Node *getSubExpr() { return getChild(0); } + + void setSubExpr(chillAST_Node *sub) { return setChild(0, sub); } // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1019,14 +983,16 @@ public: // constructors chillAST_CStyleCastExpr(); + chillAST_CStyleCastExpr(const char *to, chillAST_Node *sub); // other methods particular to this type of node // required methods that I can't seem to get to inherit - chillAST_Node* getSubExpr() { return getChild(0); } - void setSubExpr(chillAST_Node* sub) { return setChild(0,sub); } + chillAST_Node *getSubExpr() { return getChild(0); } + + void setSubExpr(chillAST_Node *sub) { return setChild(0, sub); } chillAST_Node *clone(); @@ -1044,12 +1010,14 @@ public: // constructors chillAST_CStyleAddressOf(); + chillAST_CStyleAddressOf(chillAST_Node *sub); // other methods particular to this type of node - chillAST_Node* getSubExpr() { return getChild(0); } - void setSubExpr(chillAST_Node* sub) { return setChild(0,sub); } + chillAST_Node *getSubExpr() { return getChild(0); } + + void setSubExpr(chillAST_Node *sub) { return setChild(0, sub); } // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1067,13 +1035,17 @@ public: // constructors chillAST_CudaMalloc(); + chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node *size); // other methods particular to this type of node - chillAST_Node* getDevPtr() {return getChild(0);} - void setDevPtr(chillAST_Node* devptr) { setChild(0,devptr);} - chillAST_Node* getSize() {return getChild(1);} - void setSize(chillAST_Node* size) { setChild(1,size);} + chillAST_Node *getDevPtr() { return getChild(0); } + + void setDevPtr(chillAST_Node *devptr) { setChild(0, devptr); } + + chillAST_Node *getSize() { return getChild(1); } + + void setSize(chillAST_Node *size) { setChild(1, size); } // required methods that I can't seem to get to inherit @@ -1093,35 +1065,20 @@ class chillAST_CudaFree : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDAFREE; } - // variables that are special for this type of node - // TODO Seriously wrong, this is not a new variable declaration - chillAST_VarDecl *variable; - // constructors - chillAST_CudaFree(chillAST_VarDecl *var); + chillAST_CudaFree(); + + chillAST_CudaFree(chillAST_Node *var); // other methods particular to this type of node + chillAST_Node *getPointer() { return getChild(0); } + void setPointer(chillAST_Node *ptr) { setChild(0, ptr); } - // required methods that I can't seem to get to inherit - chillAST_Node *constantFold(); + // required methods that I can't seem to get to inherit chillAST_Node *clone(); - void gatherArrayRefs(std::vector &refs, bool writtento); - - void gatherScalarRefs(std::vector &refs, bool writtento); - - void gatherVarDecls(std::vector &decls); - - void gatherScalarVarDecls(std::vector &decls); - - void gatherArrayVarDecls(std::vector &decls); - - void gatherVarUsage(std::vector &decls); - - //void gatherDeclRefExprs( std::vector&refs ); - //void replaceVarDecls( chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here @@ -1135,11 +1092,13 @@ public: // constructors chillAST_Malloc(); + chillAST_Malloc(chillAST_Node *numthings); // malloc (sizeof(int) *1024) // other methods particular to this type of node - chillAST_Node* getSize() { return getChild(0); } - void setSize(chillAST_Node* size) { setChild(0,size); } + chillAST_Node *getSize() { return getChild(0); } + + void setSize(chillAST_Node *size) { setChild(0, size); } // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1160,37 +1119,28 @@ public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDAMEMCPY; } // variables that are special for this type of node - // TODO Very wrong, this is not a declaration - chillAST_VarDecl *dest; // Pointer to allocated device memory - chillAST_VarDecl *src; - chillAST_Node *size; char *cudaMemcpyKind; // could use the actual enum // constructors - chillAST_CudaMemcpy(chillAST_VarDecl *d, chillAST_VarDecl *s, chillAST_Node *size, char *kind); + chillAST_CudaMemcpy(); + chillAST_CudaMemcpy(chillAST_Node *d, chillAST_Node *s, chillAST_Node *size, char *kind); // other methods particular to this type of node + chillAST_Node *getDest() {return getChild(0);} + void setDest(chillAST_Node* dest) { setChild(0,dest); } + chillAST_Node *getSrc() {return getChild(1);} + void setSrc(chillAST_Node* src) { setChild(1,src); } + chillAST_Node *getSize() {return getChild(2);} + void setSize(chillAST_Node* size) { setChild(2,size); } // required methods that I can't seem to get to inherit - chillAST_Node *constantFold(); - chillAST_Node *clone(); void gatherArrayRefs(std::vector &refs, bool writtento); void gatherScalarRefs(std::vector &refs, bool writtento); - void gatherVarDecls(std::vector &decls); - - void gatherScalarVarDecls(std::vector &decls); - - void gatherArrayVarDecls(std::vector &decls); - - void gatherVarUsage(std::vector &decls); - - //void gatherDeclRefExprs( std::vector&refs ); - //void replaceVarDecls( chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here @@ -1199,6 +1149,7 @@ public: class chillAST_CudaSyncthreads : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDASYNCTHREADS; } + // constructors chillAST_CudaSyncthreads(); }; @@ -1206,13 +1157,16 @@ public: class chillAST_ReturnStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_RETURNSTMT; } + // constructors chillAST_ReturnStmt(); + chillAST_ReturnStmt(chillAST_Node *retval); // other methods particular to this type of node - chillAST_Node* getRetVal() { return getChild(0); } - void setRetVal(chillAST_Node *ret) { setChild(0,ret); } + chillAST_Node *getRetVal() { return getChild(0); } + + void setRetVal(chillAST_Node *ret) { setChild(0, ret); } // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1229,39 +1183,22 @@ public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CALLEXPR; } // variables that are special for this type of node - chillAST_Node *callee; // the function declaration (what about builtins?) - int numargs; - std::vector args; chillAST_VarDecl *grid; chillAST_VarDecl *block; // constructors + chillAST_CallExpr(); chillAST_CallExpr(chillAST_Node *function); - void addArg(chillAST_Node *newarg); + void addArg(chillAST_Node *newarg) { addChild(newarg); } // other methods particular to this type of node // TODO get/set grid, block + chillAST_Node *getCallee() { return getChild(0); } - // required methods that I can't seem to get to inherit - chillAST_Node *constantFold(); - - void gatherArrayRefs(std::vector &refs, bool writtento); - - void gatherScalarRefs(std::vector &refs, bool writtento); - - void gatherVarDecls(std::vector &decls); - - void gatherScalarVarDecls(std::vector &decls); - - void gatherArrayVarDecls(std::vector &decls); - - void gatherVarUsage(std::vector &decls); - - void gatherDeclRefExprs(std::vector &refs); - - void replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); + void setCallee(chillAST_Node *c) { setChild(0, c); } + // required methods that I can't seem to get to inherit bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here chillAST_Node *clone(); @@ -1270,13 +1207,16 @@ public: class chillAST_ParenExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_PARENEXPR; } + // constructors chillAST_ParenExpr(); + chillAST_ParenExpr(chillAST_Node *sub); // other methods particular to this type of node - chillAST_Node* getSubExpr() { return getChild(0); } - void setSubExpr(chillAST_Node* sub) { setChild(0, sub); } + chillAST_Node *getSubExpr() { return getChild(0); } + + void setSubExpr(chillAST_Node *sub) { setChild(0, sub); } // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1334,6 +1274,7 @@ public: // constructors chillAST_IfStmt(); + chillAST_IfStmt(chillAST_Node *c, chillAST_Node *t, chillAST_Node *e); // other methods particular to this type of node @@ -1343,11 +1284,11 @@ public: chillAST_Node *getElse() { return getChild(2); }; - void setCond(chillAST_Node *b) { setChild(0,b); }; + void setCond(chillAST_Node *b) { setChild(0, b); }; - void setThen(chillAST_Node *b) { setChild(1,b); }; + void setThen(chillAST_Node *b) { setChild(1, b); }; - void setElse(chillAST_Node *b) { setChild(2,b); }; + void setElse(chillAST_Node *b) { setChild(2, b); }; // required methods that I can't seem to get to inherit chillAST_Node *clone(); @@ -1356,7 +1297,6 @@ public: void gatherScalarRefs(std::vector &refs, bool writtento); - //void replaceVarDecls( chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false); }; @@ -1364,6 +1304,7 @@ public: class chillAST_something : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_UNKNOWN; } + // constructors chillAST_something(); }; diff --git a/lib/chillcg/src/CG_chillBuilder.cc b/lib/chillcg/src/CG_chillBuilder.cc index 558fe38..51a02be 100755 --- a/lib/chillcg/src/CG_chillBuilder.cc +++ b/lib/chillcg/src/CG_chillBuilder.cc @@ -238,9 +238,9 @@ namespace omega { //fprintf(stderr, "substituting for oldvar %s in ", oldvar ); //CE->print(); printf("\n"); fflush(stdout); - int nargs = CE->numargs; - for (int i=0; iargs[i] = substituteChill( oldvar, newvar, CE->args[i], CE); + int nargs = CE->getNumChildren(); + for (int i=1; isetChild(i,substituteChill( oldvar, newvar, CE->getChild(i), CE)); } return CE; } diff --git a/src/ast/node.cpp b/src/ast/node.cpp index 3d28d97..dc0a913 100644 --- a/src/ast/node.cpp +++ b/src/ast/node.cpp @@ -9,11 +9,59 @@ using namespace std; +int chillAST_Node::chill_scalar_counter = 0; +int chillAST_Node::chill_array_counter = 1; + +const char *ChillAST_Node_Names[] = { + "Unknown AST node type", + "SourceFile", + "TypedefDecl", + "VarDecl", + // "ParmVarDecl", not used any more + "FunctionDecl", + "RecordDecl", + "MacroDefinition", + "CompoundStmt", + "ForStmt", + "TernaryOperator", + "BinaryOperator", + "UnaryOperator", + "ArraySubscriptExpr", + "MemberExpr", + "DeclRefExpr", + "IntegerLiteral", + "FloatingLiteral", + "ImplicitCastExpr", // not sure we need this + "ReturnStmt", + "CallExpr", + "DeclStmt", + "ParenExpr", + "CStyleCastExpr", + "CStyleAddressOf", + "IfStmt", + "SizeOf", + "Malloc", + "Free", + "NoOp", +// CUDA specific + "CudaMalloc", + "CudaFree", + "CudaMemcpy", + "CudaKernelCall", + "CudaSyncthreads", + "fake1", + "fake2", + "fake3" +}; + +const char* chillAST_Node::getTypeString() { + return ChillAST_Node_Names[getType()]; +}; + void chillAST_Node::fixChildInfo() {} 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"); @@ -47,27 +95,13 @@ int chillAST_Node::findChild(chillAST_Node *c) { } 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); + for (int i = 0; i < getNumChildren(); i++) { + if (getChild(i) == old) { + setChild(i,newchild); 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 + CHILL_ERROR("Replace Child Falure, oldchild not a child\n"); exit(-1); }; @@ -78,9 +112,7 @@ void chillAST_Node::loseLoopWithLoopVar(char *var) { } } -//! recursive walk parent links, looking for loops, and grabbing the declRefExpr in the loop init and cond. -void chillAST_Node::gatherLoopIndeces( - std::vector &indeces) { +void chillAST_Node::gatherLoopIndeces(std::vector &indeces) { // you can quit when you get to certain nodes CHILL_DEBUG_PRINT("%s::gatherLoopIndeces()\n", getTypeString()); diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 70c37e8..bea2360 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -9,52 +9,6 @@ using namespace std; -int chillAST_Node::chill_scalar_counter = 0; -int chillAST_Node::chill_array_counter = 1; - - -const char *ChillAST_Node_Names[] = { - "Unknown AST node type", - "SourceFile", - "TypedefDecl", - "VarDecl", - // "ParmVarDecl", not used any more - "FunctionDecl", - "RecordDecl", - "MacroDefinition", - "CompoundStmt", - "ForStmt", - "TernaryOperator", - "BinaryOperator", - "UnaryOperator", - "ArraySubscriptExpr", - "MemberExpr", - "DeclRefExpr", - "IntegerLiteral", - "FloatingLiteral", - "ImplicitCastExpr", // not sure we need this - "ReturnStmt", - "CallExpr", - "DeclStmt", - "ParenExpr", - "CStyleCastExpr", - "CStyleAddressOf", - "IfStmt", - "SizeOf", - "Malloc", - "Free", - "NoOp", -// CUDA specific - "CudaMalloc", - "CudaFree", - "CudaMemcpy", - "CudaKernelCall", - "CudaSyncthreads", - "fake1", - "fake2", - "fake3" -}; - bool streq(const char *a, const char *b) { return !strcmp(a, b); } @@ -625,15 +579,6 @@ void chillAST_MacroDefinition::setBody(chillAST_Node *bod) { bod->setParent(this); // well, ... } -void chillAST_MacroDefinition::insertChild(int i, chillAST_Node *node) { - body->insertChild(i, node); -} - -void chillAST_MacroDefinition::addChild(chillAST_Node *node) { - body->addChild(node); - node->setParent(this); // this, or body?? -} - chillAST_ForStmt::chillAST_ForStmt() { children.push_back(NULL); // init children.push_back(NULL); // cond @@ -646,7 +591,7 @@ chillAST_ForStmt::chillAST_ForStmt() { chillAST_ForStmt::chillAST_ForStmt(chillAST_Node *ini, chillAST_Node *con, chillAST_Node *inc, chillAST_Node *bod) - :chillAST_ForStmt() { + : chillAST_ForStmt() { setInit(ini); setCond(con); setInc(inc); @@ -763,7 +708,8 @@ void chillAST_ForStmt::printControl(int in, FILE *fp) { } chillAST_Node *chillAST_ForStmt::clone() { - chillAST_ForStmt *fs = new chillAST_ForStmt(getInit()->clone(), getCond()->clone(), getInc()->clone(), getBody()->clone()); + chillAST_ForStmt *fs = new chillAST_ForStmt(getInit()->clone(), getCond()->clone(), getInc()->clone(), + getBody()->clone()); fs->isFromSourceFile = isFromSourceFile; if (filename) fs->filename = strdup(filename); fs->setParent(getParent()); @@ -1113,7 +1059,7 @@ chillAST_BinaryOperator::chillAST_BinaryOperator() { } chillAST_BinaryOperator::chillAST_BinaryOperator(chillAST_Node *l, const char *oper, chillAST_Node *r) - :chillAST_BinaryOperator() { + : chillAST_BinaryOperator() { //fprintf(stderr, "chillAST_BinaryOperator::chillAST_BinaryOperator( l %p %s r %p, parent %p) this %p\n", l, oper, r, par, this); CHILL_DEBUG_PRINT("( l %s r )\n", oper); @@ -1259,7 +1205,7 @@ chillAST_TernaryOperator::chillAST_TernaryOperator() { } chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_Node *c, chillAST_Node *l, - chillAST_Node *r):chillAST_TernaryOperator() { + chillAST_Node *r) : chillAST_TernaryOperator() { if (op) op = strdup(oper); setCond(c); setLHS(l); @@ -2074,7 +2020,8 @@ chillAST_UnaryOperator::chillAST_UnaryOperator() { children.push_back(NULL); } -chillAST_UnaryOperator::chillAST_UnaryOperator(const char *oper, bool pre, chillAST_Node *sub):chillAST_UnaryOperator() { +chillAST_UnaryOperator::chillAST_UnaryOperator(const char *oper, bool pre, chillAST_Node *sub) + : chillAST_UnaryOperator() { op = strdup(oper); prefix = pre; setSubExpr(sub); @@ -2144,7 +2091,7 @@ chillAST_ImplicitCastExpr::chillAST_ImplicitCastExpr() { children.push_back(NULL); } -chillAST_ImplicitCastExpr::chillAST_ImplicitCastExpr(chillAST_Node *sub):chillAST_ImplicitCastExpr() { +chillAST_ImplicitCastExpr::chillAST_ImplicitCastExpr(chillAST_Node *sub) : chillAST_ImplicitCastExpr() { setSubExpr(sub); } @@ -2159,7 +2106,7 @@ chillAST_CStyleCastExpr::chillAST_CStyleCastExpr() { children.push_back(NULL); } -chillAST_CStyleCastExpr::chillAST_CStyleCastExpr(const char *to, chillAST_Node *sub):chillAST_CStyleCastExpr() { +chillAST_CStyleCastExpr::chillAST_CStyleCastExpr(const char *to, chillAST_Node *sub) : chillAST_CStyleCastExpr() { towhat = strdup(to); setSubExpr(sub); } @@ -2175,7 +2122,8 @@ class chillAST_Node *chillAST_CStyleCastExpr::clone() { chillAST_CStyleAddressOf::chillAST_CStyleAddressOf() { children.push_back(NULL); } -chillAST_CStyleAddressOf::chillAST_CStyleAddressOf(chillAST_Node *sub):chillAST_CStyleAddressOf() { + +chillAST_CStyleAddressOf::chillAST_CStyleAddressOf(chillAST_Node *sub) : chillAST_CStyleAddressOf() { setSubExpr(sub); } @@ -2191,7 +2139,7 @@ chillAST_Malloc::chillAST_Malloc() { children.push_back(NULL); } -chillAST_Malloc::chillAST_Malloc(chillAST_Node *numthings):chillAST_Malloc() { +chillAST_Malloc::chillAST_Malloc(chillAST_Node *numthings) : chillAST_Malloc() { setSize(numthings); isFromSourceFile = true; // default filename = NULL; @@ -2210,7 +2158,7 @@ chillAST_CudaMalloc::chillAST_CudaMalloc() { children.push_back(NULL); } -chillAST_CudaMalloc::chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node *size):chillAST_CudaMalloc() { +chillAST_CudaMalloc::chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node *size) : chillAST_CudaMalloc() { setDevPtr(devmemptr); setSize(size); }; @@ -2224,71 +2172,45 @@ class chillAST_Node *chillAST_CudaMalloc::clone() { } void chillAST_CudaMalloc::gatherArrayRefs(std::vector &refs, bool w) { - chillAST_Node::gatherArrayRefs(refs,false); + chillAST_Node::gatherArrayRefs(refs, false); } void chillAST_CudaMalloc::gatherScalarRefs(std::vector &refs, bool writtento) { - chillAST_Node::gatherScalarRefs(refs,false); + chillAST_Node::gatherScalarRefs(refs, false); } -chillAST_CudaFree::chillAST_CudaFree(chillAST_VarDecl *var) { - variable = var; -}; - -class chillAST_Node *chillAST_CudaFree::constantFold() { - return this; +chillAST_CudaFree::chillAST_CudaFree() { + children.push_back(NULL); } +chillAST_CudaFree::chillAST_CudaFree(chillAST_Node *var) : chillAST_CudaFree() { + setParent(var); +}; + class chillAST_Node *chillAST_CudaFree::clone() { - chillAST_CudaFree *CF = new chillAST_CudaFree(variable); + chillAST_CudaFree *CF = new chillAST_CudaFree(getPointer()->clone()); CF->setParent(getParent()); CF->isFromSourceFile = isFromSourceFile; if (filename) CF->filename = strdup(filename); return CF; } -void chillAST_CudaFree::gatherArrayRefs(std::vector &refs, bool w) {} - -void chillAST_CudaFree::gatherScalarRefs(std::vector &refs, bool writtento) {} - -void chillAST_CudaFree::gatherVarDecls(vector &decls) { - variable->gatherVarDecls(decls); -} - - -void chillAST_CudaFree::gatherScalarVarDecls(vector &decls) { - variable->gatherScalarVarDecls(decls); +chillAST_CudaMemcpy::chillAST_CudaMemcpy() { + addChild(NULL); + addChild(NULL); + addChild(NULL); } - -void chillAST_CudaFree::gatherArrayVarDecls(vector &decls) { - variable->gatherArrayVarDecls(decls); -} - - -void chillAST_CudaFree::gatherVarUsage(vector &decls) { - variable->gatherVarUsage(decls); -} - - -chillAST_CudaMemcpy::chillAST_CudaMemcpy(chillAST_VarDecl *d, chillAST_VarDecl *s, chillAST_Node *siz, char *kind) { - dest = d; - src = s; - //fprintf(stderr, "chillAST_CudaMemcpy::chillAST_CudaMemcpy( dest %s, src %s, ...)\n", d->varname, s->varname ); - size = siz; +chillAST_CudaMemcpy::chillAST_CudaMemcpy(chillAST_Node *d, chillAST_Node *s, chillAST_Node *siz, char *kind) { + setDest(d); + setSrc(s); + setSize(siz); cudaMemcpyKind = kind; }; -class chillAST_Node *chillAST_CudaMemcpy::constantFold() { - dest = (chillAST_VarDecl *) dest->constantFold(); - src = (chillAST_VarDecl *) src->constantFold(); - size = size->constantFold(); - return this; -} - class chillAST_Node *chillAST_CudaMemcpy::clone() { - chillAST_CudaMemcpy *CMCPY = new chillAST_CudaMemcpy((chillAST_VarDecl *) (dest->clone()), - (chillAST_VarDecl *) (src->clone()), size->clone(), + chillAST_CudaMemcpy *CMCPY = new chillAST_CudaMemcpy(getDest()->clone(), + getSrc()->clone(), getSize()->clone(), strdup(cudaMemcpyKind)); CMCPY->setParent(getParent()); CMCPY->isFromSourceFile = isFromSourceFile; @@ -2297,52 +2219,21 @@ class chillAST_Node *chillAST_CudaMemcpy::clone() { } void chillAST_CudaMemcpy::gatherArrayRefs(std::vector &refs, bool w) { - dest->gatherArrayRefs(refs, false); - src->gatherArrayRefs(refs, false); - size->gatherArrayRefs(refs, false); + chillAST_Node::gatherArrayRefs(refs, false); } void chillAST_CudaMemcpy::gatherScalarRefs(std::vector &refs, bool writtento) { - dest->gatherScalarRefs(refs, false); - src->gatherScalarRefs(refs, false); - size->gatherScalarRefs(refs, false); -} - -void chillAST_CudaMemcpy::gatherVarDecls(vector &decls) { - dest->gatherVarDecls(decls); - src->gatherVarDecls(decls); - size->gatherVarDecls(decls); -} - - -void chillAST_CudaMemcpy::gatherScalarVarDecls(vector &decls) { - dest->gatherScalarVarDecls(decls); - src->gatherScalarVarDecls(decls); - size->gatherScalarVarDecls(decls); -} - - -void chillAST_CudaMemcpy::gatherArrayVarDecls(vector &decls) { - dest->gatherArrayVarDecls(decls); - src->gatherArrayVarDecls(decls); - size->gatherArrayVarDecls(decls); -} - - -void chillAST_CudaMemcpy::gatherVarUsage(vector &decls) { - dest->gatherVarUsage(decls); - src->gatherVarUsage(decls); - size->gatherVarUsage(decls); + chillAST_Node::gatherScalarRefs(refs, false); } - chillAST_CudaSyncthreads::chillAST_CudaSyncthreads() { } chillAST_ReturnStmt::chillAST_ReturnStmt() { children.push_back(NULL); } -chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval):chillAST_ReturnStmt() { + +chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval) : chillAST_ReturnStmt() { setRetVal(retval); } @@ -2356,87 +2247,21 @@ class chillAST_Node *chillAST_ReturnStmt::clone() { return RS; } -chillAST_CallExpr::chillAST_CallExpr(chillAST_Node *c) { //, int numofargs, chillAST_Node **theargs ) { - - //fprintf(stderr, "chillAST_CallExpr::chillAST_CallExpr callee type %s\n", c->getTypeString()); - callee = c; - //callee->setParent( this ); // ?? - numargs = 0; - grid = block = NULL; -} - - -void chillAST_CallExpr::addArg(chillAST_Node *a) { - args.push_back(a); - a->setParent(this); - numargs += 1; -} - -void chillAST_CallExpr::gatherArrayRefs(std::vector &refs, bool writtento) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherArrayRefs(refs, writtento); - } -} - -void chillAST_CallExpr::gatherScalarRefs(std::vector &refs, bool writtento) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherScalarRefs(refs, writtento); - } -} - - -void chillAST_CallExpr::gatherVarDecls(vector &decls) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherVarDecls(decls); - } +chillAST_CallExpr::chillAST_CallExpr() { + addChild(NULL); } - -void chillAST_CallExpr::gatherScalarVarDecls(vector &decls) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherScalarVarDecls(decls); - } -} - - -void chillAST_CallExpr::gatherArrayVarDecls(vector &decls) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherArrayVarDecls(decls); - } -} - - -void chillAST_CallExpr::gatherDeclRefExprs(vector &refs) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherDeclRefExprs(refs); - } -} - -void chillAST_CallExpr::replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl) { - for (int i = 0; i < args.size(); i++) args[i]->replaceVarDecls(olddecl, newdecl); -} - -void chillAST_CallExpr::gatherVarUsage(vector &decls) { - for (int i = 0; i < args.size(); i++) { - args[i]->gatherVarUsage(decls); - } -} - - -chillAST_Node *chillAST_CallExpr::constantFold() { - numargs = args.size(); // wrong place for this - for (int i = 0; i < numargs; i++) { - args[i] = args[i]->constantFold(); - } - return this; +chillAST_CallExpr::chillAST_CallExpr(chillAST_Node *c) : chillAST_CallExpr() { + setCallee(c); + grid = block = NULL; } chillAST_Node *chillAST_CallExpr::clone() { //fprintf(stderr, "chillAST_CallExpr::clone()\n"); //print(0, stderr); fprintf(stderr, "\n"); - chillAST_CallExpr *CE = new chillAST_CallExpr(callee->clone()); - for (int i = 0; i < args.size(); i++) CE->addArg(args[i]->clone()); + chillAST_CallExpr *CE = new chillAST_CallExpr(getCallee()->clone()); + for (int i = 1; i < getNumChildren(); i++) CE->addArg(getChild(i)->clone()); CE->isFromSourceFile = isFromSourceFile; if (filename) CE->filename = strdup(filename); return CE; @@ -2964,6 +2789,7 @@ bool chillAST_CompoundStmt::findLoopIndexesToReplace(chillAST_SymbolTable *symta chillAST_ParenExpr::chillAST_ParenExpr() { children.push_back(NULL); } + chillAST_ParenExpr::chillAST_ParenExpr(chillAST_Node *sub) { setSubExpr(sub); } @@ -3082,18 +2908,18 @@ chillAST_IfStmt::chillAST_IfStmt() { children.push_back(NULL); } -chillAST_IfStmt::chillAST_IfStmt(chillAST_Node *c, chillAST_Node *t, chillAST_Node *e):chillAST_IfStmt() { +chillAST_IfStmt::chillAST_IfStmt(chillAST_Node *c, chillAST_Node *t, chillAST_Node *e) : chillAST_IfStmt() { setCond(c); setThen(t); setElse(e); } void chillAST_IfStmt::gatherArrayRefs(std::vector &refs, bool writtento) { - chillAST_Node::gatherArrayRefs(refs,0); + chillAST_Node::gatherArrayRefs(refs, 0); } void chillAST_IfStmt::gatherScalarRefs(std::vector &refs, bool writtento) { - chillAST_Node::gatherScalarRefs(refs,0); + chillAST_Node::gatherScalarRefs(refs, 0); } chillAST_Node *chillAST_IfStmt::clone() { diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp index 3427c80..d8f9057 100644 --- a/src/printer/cfamily.cpp +++ b/src/printer/cfamily.cpp @@ -70,8 +70,8 @@ int CFamily::getPrecS(chillAST_CallExpr *n) { void CFamily::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { chillAST_FunctionDecl *FD = NULL; chillAST_MacroDefinition *MD = NULL; - if (n->callee->isDeclRefExpr()) { - chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) (n->callee); + if (n->getCallee()->isDeclRefExpr()) { + chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) (n->getCallee()); if (!(DRE->decl)) { o << DRE->declarationName; return; @@ -79,23 +79,23 @@ void CFamily::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { if (DRE->decl->isFunctionDecl()) FD = (chillAST_FunctionDecl *) (DRE->decl); else CHILL_ERROR("Function DRE of type %s\n", DRE->decl->getTypeString()); - } else if (n->callee->isFunctionDecl()) - FD = (chillAST_FunctionDecl *) n->callee; - else if (n->callee->isMacroDefinition()) - MD = (chillAST_MacroDefinition *) n->callee; + } else if (n->getCallee()->isFunctionDecl()) + FD = (chillAST_FunctionDecl *) n->getCallee(); + else if (n->getCallee()->isMacroDefinition()) + MD = (chillAST_MacroDefinition *) n->getCallee(); if (FD) { o << FD->functionName; if (n->grid && n->block) o << "<<<" << n->grid->varname << "," << n->block->varname << ">>>"; o << "("; } - if (MD && n->args.size()) + if (MD && n->getNumChildren()-1) o << "("; - for (int i = 0; i < n->args.size(); ++i) { + for (int i = 1; i < n->getNumChildren(); ++i) { if (i != 0) o << ", "; - print(ident, n->args[i], o); + print(ident, n->getChild(i), o); } - if (FD || n->args.size()) + if (FD || n->getNumChildren()-1) o << ")"; } @@ -130,7 +130,9 @@ void CFamily::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream } void CFamily::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { - o << "cudaFree(" << n->variable->varname << ")"; + o << "cudaFree("; + print(ident, n->getPointer(), o); + o << ")"; } void CFamily::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { @@ -146,8 +148,12 @@ void CFamily::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) } void CFamily::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) { - o << "cudaMemcpy(" << n->dest->varname << ", " << n->src->varname << ", "; - print(ident, n->size, o); + o << "cudaMemcpy("; + print(ident, n->getDest(), o); + o << ", "; + print(ident, n->getSrc(), o); + o << ", "; + print(ident, n->getSize(), o); o << ", " << n->cudaMemcpyKind << ")"; } @@ -161,7 +167,7 @@ void CFamily::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o void CFamily::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { if (n->allthedigits) - o<allthedigits; + o << n->allthedigits; else { o << showpoint << n->value; if (n->getPrecision() == 1) diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp index 0e66f29..f3fe2e5 100644 --- a/src/printer/dump.cpp +++ b/src/printer/dump.cpp @@ -70,8 +70,8 @@ void Dump::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o } void Dump::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { - if (n->callee) - print(ident, n->callee, o); + if (n->getCallee()) + print(ident, n->getCallee(), o); } void Dump::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) { @@ -88,7 +88,7 @@ void Dump::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o } void Dump::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { - o << n->variable->varname << " "; + print(ident, n->getPointer(), o); } void Dump::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { @@ -102,9 +102,9 @@ void Dump::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) { void Dump::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) { o << n->cudaMemcpyKind << " "; - print(ident, n->dest, o); - print(ident, n->src, o); - print(ident, n->size, o); + print(ident, n->getDest(), o); + print(ident, n->getSrc(), o); + print(ident, n->getSize(), o); } void Dump::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {} -- cgit v1.2.3-70-g09d2