diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 10:39:43 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 10:39:43 -0600 |
commit | 47ba6bf1100fe1da8d80607053d88cfd2abe25ff (patch) | |
tree | 6b5bc16c5f69cd870457062990712464588ba7a9 | |
parent | f939f4bacb23cfe0c4fb96c5d71f30412fa828d7 (diff) | |
download | chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.tar.gz chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.tar.bz2 chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.zip |
added precedence to operators
-rw-r--r-- | include/chillAST/chillAST_node.hh | 14 | ||||
-rw-r--r-- | include/chillAST/chillASTs.hh | 39 | ||||
-rw-r--r-- | src/chillASTs.cc | 28 |
3 files changed, 33 insertions, 48 deletions
diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index 808a62c..6cc6cfd 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -11,8 +11,12 @@ public: // TODO decide how to hide some data //! this Node's parent chillAST_Node *parent; - //! this node's children + //! this node's children the only entity that holds childs/subexpressions chillAST_NodeList children; + //! Symbol Scoping + chillAST_SymbolTable *symbolTable; + //! typedef Scoping + chillAST_TypedefTable *typedefTable; //! whether it is from a source file, when false it is from included files bool isFromSourceFile; //! the name of file this node from @@ -34,6 +38,8 @@ public: metacomment = NULL; isFromSourceFile = true; filename = NULL; + symbolTable = NULL; + typedefTable = NULL; } //! the type of this current node virtual CHILLAST_NODE_TYPE getType() {return CHILLAST_NODE_UNKNOWN;}; @@ -420,11 +426,6 @@ public: virtual void printonly(int indent = 0, FILE *fp = stderr) { print(indent, fp); }; - //virtual void printString( std::string &s ) { - // fprintf(stderr,"(%s) forgot to implement printString()\n" ,getTypeString()); - //} - - virtual void get_top_level_loops(std::vector<chillAST_ForStmt *> &loops) { int n = children.size(); //fprintf(stderr, "get_top_level_loops of a %s with %d children\n", getTypeString(), n); @@ -494,6 +495,7 @@ public: 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(); diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index 2c7006d..637df4b 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -25,7 +25,6 @@ public: } }; - class chillAST_Preprocessing : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_PREPROCESSING;} @@ -45,7 +44,6 @@ public: //void dump( int indent=0, FILE *fp = stderr ); // print ast in chill_ast.cc }; - //typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another. class chillAST_TypedefDecl : public chillAST_Node { private: @@ -108,7 +106,6 @@ public: }; - class chillAST_VarDecl : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_VARDECL;} @@ -215,7 +212,6 @@ public: }; - class chillAST_DeclRefExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_DECLREFEXPR;} @@ -296,7 +292,6 @@ public: chillAST_Node *multibase2() { return (chillAST_Node *) multibase(); } }; - class chillAST_CompoundStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_COMPOUNDSTMT;} @@ -375,7 +370,6 @@ public: void gatherStatements(std::vector<chillAST_Node *> &statements); }; - class chillAST_RecordDecl : public chillAST_Node { // declaration of the shape of a struct or union private: char *name; // could be NULL? for unnamed structs? @@ -423,7 +417,6 @@ public: void printStructure(int indent = 0, FILE *fp = stderr); }; - class chillAST_FunctionDecl : public chillAST_Node { private: chillAST_CompoundStmt *body; // always a compound statement? @@ -580,9 +573,6 @@ public: } }; // end FunctionDecl - - - class chillAST_SourceFile : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_SOURCEFILE;} @@ -871,7 +861,6 @@ public: }; - class chillAST_TernaryOperator : public chillAST_Node { public: virtual int getPrec() {return INT8_MAX+15;} @@ -953,7 +942,6 @@ public: void loseLoopWithLoopVar(char *var) {}; // ternop can't have loop as child? }; - class chillAST_BinaryOperator : public chillAST_Node { public: virtual int getPrec(); @@ -1070,7 +1058,6 @@ public: }; - class chillAST_ArraySubscriptExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_ARRAYSUBSCRIPTEXPR;} @@ -1148,7 +1135,6 @@ public: }; - class chillAST_MemberExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MEMBEREXPR;} @@ -1217,7 +1203,6 @@ public: chillAST_Node *multibase2(); // this one will return the member expression }; - class chillAST_IntegerLiteral : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_INTEGERLITERAL;} @@ -1256,7 +1241,6 @@ public: chillAST_Node *findref() { return this; }// find the SINGLE constant or data reference at this node or below }; - class chillAST_FloatingLiteral : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FLOATINGLITERAL;} @@ -1316,13 +1300,13 @@ public: bool isSameAs(chillAST_Node *other); }; - class chillAST_UnaryOperator : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_UNARYOPERATOR;} + virtual int getPrec(); // variables that are special for this type of node //! String representing the operator - char *op; // TODO enum + char *op; //! true for prefix unary operator bool prefix; chillAST_Node *subexpr; @@ -1371,7 +1355,6 @@ public: }; - class chillAST_ImplicitCastExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_IMPLICITCASTEXPR;} @@ -1419,10 +1402,10 @@ public: }; - class chillAST_CStyleCastExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CSTYLECASTEXPR;} + virtual int getPrec() {return INT8_MAX+3;} // variables that are special for this type of node //! String representing the type it casts to char *towhat; @@ -1467,10 +1450,10 @@ public: }; - class chillAST_CStyleAddressOf : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CSTYLEADDRESSOF;} + virtual int getPrec() {return INT8_MAX+3;} // variables that are special for this type of node chillAST_Node *subexpr; @@ -1510,7 +1493,6 @@ public: }; - class chillAST_CudaMalloc : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAMALLOC;} @@ -1552,7 +1534,6 @@ public: }; - class chillAST_CudaFree : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAFREE;} @@ -1593,7 +1574,6 @@ public: }; - class chillAST_Malloc : public chillAST_Node { // malloc( sizeof(int) * 2048 ); public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MALLOC;} @@ -1637,14 +1617,12 @@ public: }; - class chillAST_Free : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FREE;} }; - class chillAST_CudaMemcpy : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAMEMCPY;} @@ -1688,7 +1666,6 @@ public: }; - class chillAST_CudaSyncthreads : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDASYNCTHREADS;} @@ -1721,7 +1698,6 @@ public: }; - class chillAST_ReturnStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_RETURNSTMT;} @@ -1760,7 +1736,6 @@ public: }; - class chillAST_CallExpr : public chillAST_Node { // a function call public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CALLEXPR;} @@ -1807,7 +1782,6 @@ public: chillAST_Node *clone(); }; - class chillAST_ParenExpr : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_PARENEXPR;} @@ -1850,10 +1824,10 @@ public: }; - class chillAST_Sizeof : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_SIZEOF;} + virtual int getPrec() {return INT8_MAX+3;} // variables that are special for this type of node //! the object of sizeof function char *thing; @@ -1894,7 +1868,6 @@ public: }; - class chillAST_NoOp : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_NOOP;} @@ -1929,7 +1902,6 @@ public: bool forcesync = false) { return false; };//no loops under here }; - class chillAST_IfStmt : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_IFSTMT;} @@ -1998,7 +1970,6 @@ public: }; - class chillAST_something : public chillAST_Node { public: virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_UNKNOWN;} diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 170e481..3665571 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -3660,13 +3660,11 @@ void chillAST_CStyleCastExpr::print(int indent, FILE *fp) { subexpr->print(0, fp); fprintf(fp, "f"); } else { // general case - fprintf(fp, "((%s) ", towhat); - //fprintf(fp, "\ntowhat '%s'\n", towhat ); - + fprintf(fp, "(%s) ", towhat); + if (subexpr->getPrec()<getPrec()) fprintf(fp,"("); if (subexpr->isVarDecl()) fprintf(fp, "%s", ((chillAST_VarDecl *) subexpr)->varname); else subexpr->print(indent, fp); - //fprintf(fp, "subexpr '%s' ", subexpr->getTypeString()); - fprintf(fp, ")"); + if (subexpr->getPrec()<getPrec()) fprintf(fp,")"); } fflush(fp); }; @@ -5653,9 +5651,23 @@ bool opInSet(const char* set,char* op) { } int chillAST_BinaryOperator::getPrec() { - for (int i = 0; i< 15;++i) - if (opInSet(binaryPrec[i],op)) return INT8_MAX+i; + for (int i = 0; i< 16;++i) + if (opInSet(binaryPrec[i],op)) return INT8_MAX+i+1; return INT8_MAX; } -// TODO add unary
\ No newline at end of file +const char* unaryPrec[] = { + "", + " -- ++ ", + " -- ++ + - ! ~ * & ", +}; + +int chillAST_UnaryOperator::getPrec() { + if (prefix) + for (int i = 2;i>=0;--i) + if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1; + else + for (int i = 0;i<3;--i) + if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1; + return INT8_MAX; +} |