diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-25 00:52:59 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-25 00:52:59 -0600 |
commit | fad36d9324fd13d720bc9366f4de6e968d8b8963 (patch) | |
tree | 0047265c05ca0b43ef1c5db5713ac6ada506d44a | |
parent | b05825a90c3e175db3e1a2172fe52d51badafc5e (diff) | |
download | chill-fad36d9324fd13d720bc9366f4de6e968d8b8963.tar.gz chill-fad36d9324fd13d720bc9366f4de6e968d8b8963.tar.bz2 chill-fad36d9324fd13d720bc9366f4de6e968d8b8963.zip |
dump tested
-rw-r--r-- | CMakeLists.txt | 54 | ||||
-rw-r--r-- | include/chillAST/chillAST_def.hh | 2 | ||||
-rw-r--r-- | include/chillAST/chillAST_node.hh | 14 | ||||
-rw-r--r-- | include/chillAST/chillASTs.hh | 20 | ||||
-rw-r--r-- | include/printer.h | 202 | ||||
-rw-r--r-- | include/printer/cfamily.h (renamed from include/printers/cfamily.h) | 2 | ||||
-rw-r--r-- | include/printer/dump.h | 65 | ||||
-rw-r--r-- | include/printer/generic.h | 125 | ||||
-rw-r--r-- | include/printers/dump.h | 62 | ||||
-rwxr-xr-x | lib/chillcg/include/code_gen/CG_chillRepr.h | 2 | ||||
-rw-r--r-- | src/chillASTs.cc | 267 | ||||
-rwxr-xr-x | src/ir_clang.cc | 29 | ||||
-rw-r--r-- | src/printer/cfamily.cpp (renamed from src/printers/cfamily.cpp) | 2 | ||||
-rw-r--r-- | src/printer/dump.cpp | 226 | ||||
-rw-r--r-- | src/printer/generic.cpp | 200 | ||||
-rw-r--r-- | src/printers/dump.cpp | 160 |
16 files changed, 703 insertions, 729 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index c668668..ecd1846 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,29 +9,34 @@ add_definitions(${LLVM_DEFINITIONS}) string(TIMESTAMP build_date "\\\"%m/%d/%Y\\\"") -set(CORE_LIBS - omega codegen chillcg parseRel) +set(CORE_LIBS + omega codegen chillcg parseRel) set(CORE_SRC - src/dep.cc - src/irtools.cc + src/dep.cc + src/irtools.cc src/transformations/loop.cc src/transformations/loop_basic.cc src/transformations/loop_datacopy.cc src/transformations/loop_extra.cc src/transformations/loop_tile.cc src/transformations/loop_unroll.cc - src/omegatools.cc - ) + src/omegatools.cc + ) set(IR_CHILL_SRC - src/ir_clang.cc + src/ir_clang.cc src/chillASTs.cc - ) + ) set(PYTHON_SRC - src/chillmodule.cc - ) + src/chillmodule.cc + ) + +set(PRINTER_SRC + src/printer/generic.cpp + src/printer/dump.cpp + ) llvm_map_components_to_libnames(llvm_libs all) set(clang_libs @@ -63,28 +68,29 @@ set(CMAKE_CXX_FLAGS "${COMMON_FLAGS} ${CMAKE_CXX_FLAGS}") if (DEFINED OMEGAHOME) link_directories(${OMEGAHOME}/lib) set(OMEGA_INC ${OMEGAHOME}/include) -else() +else () set(OMEGA_INC - ${CMAKE_CURRENT_SOURCE_DIR}/lib/omega/include - ${CMAKE_CURRENT_SOURCE_DIR}/lib/parserel/include - ${CMAKE_CURRENT_SOURCE_DIR}/lib/codegen/include - ${CMAKE_CURRENT_SOURCE_DIR}/lib/chillcg/include - ) -endif() + ${CMAKE_CURRENT_SOURCE_DIR}/lib/omega/include + ${CMAKE_CURRENT_SOURCE_DIR}/lib/parserel/include + ${CMAKE_CURRENT_SOURCE_DIR}/lib/codegen/include + ${CMAKE_CURRENT_SOURCE_DIR}/lib/chillcg/include + ) +endif () include_directories( - include - lib/chillcg/include - lib/iegenlib/src - ${OMEGA_INC} - ${LLVM_INCLUDE_DIRS} - ${PYTHON_INCLUDE_DIRS}) + include + lib/chillcg/include + lib/iegenlib/src + ${OMEGA_INC} + ${LLVM_INCLUDE_DIRS} + ${PYTHON_INCLUDE_DIRS}) add_executable(chill src/chill.cc ${CORE_SRC} ${PYTHON_SRC} - ${IR_CHILL_SRC}) + ${IR_CHILL_SRC} + ${PRINTER_SRC}) target_link_libraries(chill ${CORE_LIBS} diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh index 665f7ff..f336e14 100644 --- a/include/chillAST/chillAST_def.hh +++ b/include/chillAST/chillAST_def.hh @@ -14,7 +14,7 @@ #include <assert.h> #include <vector> // std::vector -#include <ir_enums.hh> // for IR_CONDITION_* +#include <ir_enums.hh> // for IR_CONDITION_* enum CHILLAST_NODE_TYPE { CHILLAST_NODE_UNKNOWN = 0, diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index 2c4ee8d..80f1be4 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -4,12 +4,10 @@ #define _CHILLAST_NODE_H_ #include "chillAST_def.hh" -#include "printer.h" //! generic node of the actual chillAST, a multiway tree node. class chillAST_Node { public: - friend class chill::printer::GenericPrinter; // TODO decide how to hide some data //! this Node's parent chillAST_Node *parent; @@ -121,8 +119,6 @@ public: bool isCudaSYNCTHREADS() { return (getType() == CHILLAST_NODE_CUDASYNCTHREADS); }; - bool isDeclStmt() { return (getType() == CHILLAST_NODE_DECLSTMT); }; // doesn't exist - bool isConstant() { return (getType() == CHILLAST_NODE_INTEGERLITERAL) || (getType() == CHILLAST_NODE_FLOATINGLITERAL); } @@ -168,7 +164,7 @@ public: int getNumChildren() { return children.size(); }; - chillAST_NodeList& getChildren() { return children; }; // not usually useful + chillAST_NodeList* getChildren() { return &children; }; // not usually useful void setChildren(chillAST_NodeList &c) { children = c; }; // does not set parent. probably should chillAST_Node *getChild(int which) { return children[which]; }; @@ -406,12 +402,6 @@ public: exit(-1);; }; - //! Print AST - virtual void dump(int indent = 0, FILE *fp = stderr) { - fflush(fp); - CHILL_ERROR("(%s) forgot to implement dump()\n", getTypeString()); - };// print ast - // TODO We might want to print the code a bit differently, This can be only a generic dump //! Print CODE virtual void print(int indent = 0, FILE *fp = stderr) { @@ -546,6 +536,8 @@ public: virtual chillAST_VarDecl* getParameter(const char * name); virtual void addParameter(chillAST_VarDecl* name); + void dump(int indent=0,FILE *fp = stderr); + }; diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index d61d183..7e32a3a 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -166,8 +166,6 @@ public: chillAST_VarDecl(chillAST_RecordDecl *astruct, const char *n, const char *arraypart); - void dump(int indent = 0, FILE *fp = stderr); - void print(int indent = 0, FILE *fp = stderr); void printName(int indent = 0, FILE *fp = stderr); @@ -294,8 +292,6 @@ public: // required methods void replaceChild(chillAST_Node *old, chillAST_Node *newchild); - void dump(int indent = 0, FILE *fp = stderr); - void print(int indent = 0, FILE *fp = stderr); chillAST_Node *constantFold(); @@ -374,7 +370,7 @@ public: class chillAST_FunctionDecl : public chillAST_Node { private: - chillAST_CompoundStmt *body; // always a compound statement? + chillAST_CompoundStmt *body; // always a compound statement? CHILLAST_FUNCTION_TYPE function_type; // CHILLAST_FUNCTION_CPU or CHILLAST_FUNCTION_GPU bool externfunc; // function is external bool builtin; // function is a builtin @@ -456,7 +452,6 @@ public: // constructors chillAST_SourceFile(const char *filename); // defined in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc void printToFile(char *filename = NULL); @@ -715,7 +710,6 @@ public: // required methods that I can't seem to get to inherit - void dump(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc void printonly(int indent = 0, FILE *fp = stderr); @@ -825,7 +819,6 @@ public: // required methods that I can't seem to get to inherit - void dump(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc void printonly(int indent = 0, FILE *fp = stderr); @@ -903,7 +896,6 @@ public: void printonly(int indent = 0, FILE *fp = stderr); void print(int indent = 0, FILE *fp = stderr) const; // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc char *stringRep(int indent = 0); chillAST_Node *constantFold(); @@ -1022,7 +1014,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1080,7 +1071,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1126,7 +1116,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1222,7 +1211,6 @@ public: void replaceChild(chillAST_Node *old, chillAST_Node *newchild); void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1266,7 +1254,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1348,7 +1335,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1477,7 +1463,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc //chillAST_Node* constantFold() {}; //chillAST_Node* clone(); //void gatherArrayRefs( std::vector<chillAST_ArraySubscriptExpr*> &refs, bool writtento ){}; @@ -1555,7 +1540,6 @@ public: chillAST_Node *constantFold(); void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc void gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento); void gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento); @@ -1593,7 +1577,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); @@ -1637,7 +1620,6 @@ public: // required methods that I can't seem to get to inherit void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc - void dump(int indent = 0, FILE *fp = stderr); // print ast in chill_ast.cc chillAST_Node *constantFold(); chillAST_Node *clone(); diff --git a/include/printer.h b/include/printer.h deleted file mode 100644 index 5930768..0000000 --- a/include/printer.h +++ /dev/null @@ -1,202 +0,0 @@ -// -// Created by ztuowen on 9/24/16. -// - -#ifndef CHILL_PRINTER_H_H -#define CHILL_PRINTER_H_H - -#include "chillAST.h" -#include "chillAST/chillAST_node.hh" -#include <string> -#include <sstream> - -/*! - * \file - * \brief this is a generic AST printer that prints the code out to a C-family like syntax - */ -namespace chill { - namespace printer { - class GenericPrinter { - private: - std::string indentSpace; - public: - GenericPrinter() { indentSpace = " "; } - - void setIndentSpace(int numspaces) { - indentSpace = ""; - for (int i = 0; i < numspaces; ++i) - indentSpace += " "; - } - //! return the Precedence of the corresponding AST node - /*! - * @param n the chillAST_Node - * @return a int representing the subnodes's precedence, 0 being the highest, INT8_MAX being the default - */ - virtual int getPrec(chillAST_Node *n) { return INT8_MAX; } - virtual void print(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_BinaryOperator *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CallExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CompoundStmt *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CStyleAddressOf *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CStyleCastExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CudaFree *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CudaKernelCall *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CudaMalloc *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CudaMemcpy *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_CudaSyncthreads *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_DeclRefExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_FloatingLiteral *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_ForStmt *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_Free *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_FunctionDecl *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_IfStmt *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_IntegerLiteral *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_ImplicitCastExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_MacroDefinition *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_Malloc *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_MemberExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_NULL *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_NoOp *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_ParenExpr *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_Preprocessing *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_RecordDecl *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_ReturnStmt *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_Sizeof *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_SourceFile *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_TypedefDecl *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_TernaryOperator *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_UnaryOperator *n, std::ostringstream &o)=0; - virtual void print(std::string ident, chillAST_VarDecl *n, std::ostringstream &o)=0; - //! Print the AST to string stream - /*! - * @param ident indentation of the node - * @param n the chillAST_Node - * @param o the string stream - */ - virtual void print(std::string ident, chillAST_Node *n, std::ostringstream &o){ - switch (n->getType()) { - case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: - print(ident, dynamic_cast<chillAST_ArraySubscriptExpr*>(n),o); - break; - case CHILLAST_NODE_BINARYOPERATOR: - print(ident, dynamic_cast<chillAST_BinaryOperator*>(n),o); - break; - case CHILLAST_NODE_CALLEXPR: - print(ident, dynamic_cast<chillAST_CallExpr*>(n),o); - break; - case CHILLAST_NODE_COMPOUNDSTMT: - print(ident, dynamic_cast<chillAST_CompoundStmt*>(n),o); - break; - case CHILLAST_NODE_CSTYLEADDRESSOF: - print(ident, dynamic_cast<chillAST_CStyleAddressOf*>(n),o); - break; - case CHILLAST_NODE_CSTYLECASTEXPR: - print(ident, dynamic_cast<chillAST_CStyleCastExpr*>(n),o); - break; - case CHILLAST_NODE_CUDAFREE: - print(ident, dynamic_cast<chillAST_CudaFree*>(n),o); - break; - case CHILLAST_NODE_CUDAKERNELCALL: - print(ident, dynamic_cast<chillAST_CudaKernelCall*>(n),o); - break; - case CHILLAST_NODE_CUDAMALLOC: - print(ident, dynamic_cast<chillAST_CudaMalloc*>(n),o); - break; - case CHILLAST_NODE_CUDAMEMCPY: - print(ident, dynamic_cast<chillAST_CudaMemcpy*>(n),o); - break; - case CHILLAST_NODE_CUDASYNCTHREADS: - print(ident, dynamic_cast<chillAST_CudaSyncthreads*>(n),o); - break; - case CHILLAST_NODE_DECLREFEXPR: - print(ident, dynamic_cast<chillAST_DeclRefExpr*>(n),o); - break; - case CHILLAST_NODE_FLOATINGLITERAL: - print(ident, dynamic_cast<chillAST_FloatingLiteral*>(n),o); - break; - case CHILLAST_NODE_LOOP: - case CHILLAST_NODE_FORSTMT: - print(ident, dynamic_cast<chillAST_ForStmt*>(n),o); - break; - case CHILLAST_NODE_FREE: - print(ident, dynamic_cast<chillAST_Free*>(n),o); - break; - case CHILLAST_NODE_FUNCTIONDECL: - print(ident, dynamic_cast<chillAST_FunctionDecl*>(n),o); - break; - case CHILLAST_NODE_IFSTMT: - print(ident, dynamic_cast<chillAST_IfStmt*>(n),o); - break; - case CHILLAST_NODE_IMPLICITCASTEXPR: - print(ident, dynamic_cast<chillAST_ImplicitCastExpr*>(n),o); - break; - case CHILLAST_NODE_INTEGERLITERAL: - print(ident, dynamic_cast<chillAST_IntegerLiteral*>(n),o); - break; - case CHILLAST_NODE_MACRODEFINITION: - print(ident, dynamic_cast<chillAST_MacroDefinition*>(n),o); - break; - case CHILLAST_NODE_MALLOC: - print(ident, dynamic_cast<chillAST_Malloc*>(n),o); - break; - case CHILLAST_NODE_MEMBEREXPR: - print(ident, dynamic_cast<chillAST_MemberExpr*>(n),o); - break; - case CHILLAST_NODE_NOOP: - print(ident, dynamic_cast<chillAST_NoOp*>(n),o); - break; - case CHILLAST_NODE_NULL: - print(ident, dynamic_cast<chillAST_NULL*>(n),o); - break; - case CHILLAST_NODE_PARENEXPR: - print(ident, dynamic_cast<chillAST_ParenExpr*>(n),o); - break; - case CHILLAST_NODE_PREPROCESSING: - print(ident, dynamic_cast<chillAST_Preprocessing*>(n),o); - break; - case CHILLAST_NODE_RECORDDECL: - print(ident, dynamic_cast<chillAST_RecordDecl*>(n),o); - break; - case CHILLAST_NODE_RETURNSTMT: - print(ident, dynamic_cast<chillAST_ReturnStmt*>(n),o); - break; - case CHILLAST_NODE_SIZEOF: - print(ident, dynamic_cast<chillAST_Sizeof*>(n),o); - break; - case CHILLAST_NODE_TRANSLATIONUNIT: - case CHILLAST_NODE_SOURCEFILE: - print(ident, dynamic_cast<chillAST_SourceFile*>(n),o); - break; - case CHILLAST_NODE_TERNARYOPERATOR: - print(ident, dynamic_cast<chillAST_TernaryOperator*>(n),o); - break; - case CHILLAST_NODE_TYPEDEFDECL: - print(ident, dynamic_cast<chillAST_TypedefDecl*>(n),o); - break; - case CHILLAST_NODE_UNARYOPERATOR: - print(ident, dynamic_cast<chillAST_UnaryOperator*>(n),o); - break; - case CHILLAST_NODE_VARDECL: - print(ident, dynamic_cast<chillAST_VarDecl*>(n),o); - break; - case CHILLAST_NODE_UNKNOWN: - default: - CHILL_ERROR("Printing an unknown type of Node: %s\n", n->getTypeString()); - } - } - //! Print the AST to string, overload the print function - /*! - * @param ident indentation of the node - * @param n the chillAST_Node - * @return a string of the corresponding code - */ - virtual std::string print(std::string ident, chillAST_Node *n) { - std::ostringstream os; - print(ident, n, os); - return os.str(); - } - }; - } -} - -#endif //CHILL_PRINTER_H_H diff --git a/include/printers/cfamily.h b/include/printer/cfamily.h index e979db7..b36a873 100644 --- a/include/printers/cfamily.h +++ b/include/printer/cfamily.h @@ -5,7 +5,7 @@ #ifndef CHILL_CFAMILY_H #define CHILL_CFAMILY_H -#include "printer.h" +#include "printer/generic.h" /*! * \file diff --git a/include/printer/dump.h b/include/printer/dump.h new file mode 100644 index 0000000..bf7168b --- /dev/null +++ b/include/printer/dump.h @@ -0,0 +1,65 @@ +// +// Created by ztuowen on 9/24/16. +// + +#ifndef CHILL_DUMP_H +#define CHILL_DUMP_H + +#include "printer/generic.h" + +/*! + * \file + * \brief this replace the old dump function in the chillAST + */ + +namespace chill { + namespace printer { + class Dump : public GenericPrinter { + public: + Dump() {} + virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o); + virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o); + /*! + * Just prints everything. Indent is igored due to need to limit the number of output + * @param ident + * @param n + * @param o + */ + virtual void print(std::string ident, chillAST_Node *n, std::ostream &o); + }; + } +} + +#endif //CHILL_DUMP_H diff --git a/include/printer/generic.h b/include/printer/generic.h new file mode 100644 index 0000000..f3fd8f2 --- /dev/null +++ b/include/printer/generic.h @@ -0,0 +1,125 @@ +// +// Created by ztuowen on 9/24/16. +// + +#ifndef CHILL_PRINTER_H_H +#define CHILL_PRINTER_H_H + +#include "chillAST.h" +#include <string> +#include <sstream> + +/*! + * \file + * \brief this is a generic AST printSer that printSs the code out to a C-family like syntax + */ +namespace chill { + namespace printer { + class GenericPrinter { + private: + std::string indentSpace; + public: + GenericPrinter() { indentSpace = " "; } + + void setIndentSpace(int numspaces) { + indentSpace = ""; + for (int i = 0; i < numspaces; ++i) + indentSpace += " "; + } + virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_BinaryOperator *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CallExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CompoundStmt *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CStyleAddressOf *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CStyleCastExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CudaFree *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CudaKernelCall *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CudaMalloc *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CudaMemcpy *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_CudaSyncthreads *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_DeclRefExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_FloatingLiteral *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_ForStmt *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_Free *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_FunctionDecl *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_IfStmt *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_IntegerLiteral *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_ImplicitCastExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_MacroDefinition *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_Malloc *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_MemberExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_NULL *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_NoOp *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_ParenExpr *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_Preprocessing *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_RecordDecl *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_ReturnStmt *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_Sizeof *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_SourceFile *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_TypedefDecl *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_TernaryOperator *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_UnaryOperator *n) { return INT8_MAX; } + virtual int getPrecS(chillAST_VarDecl *n) { return INT8_MAX; } + //! return the Precedence of the corresponding AST node + /*! + * @param n the chillAST_Node + * @return a int representing the subnodes's precedence, 0 being the highest, INT8_MAX being the default + */ + virtual int getPrec(chillAST_Node *n); + virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o)=0; + virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o)=0; + //! Print the AST to string stream, multiplexer + /*! + * @param ident indentation of the node + * @param n the chillAST_Node + * @param o the string stream + */ + virtual void print(std::string ident, chillAST_Node *n, std::ostream &o); + //! Print the AST to string, overload the printS function + /*! + * @param ident indentation of the node + * @param n the chillAST_Node + * @return a string of the corresponding code + */ + virtual std::string print(std::string ident, chillAST_Node *n) { + std::ostringstream os; + print(ident, n, os); + return os.str(); + } + }; + } +} + +#endif //CHILL_PRINTER_H_H diff --git a/include/printers/dump.h b/include/printers/dump.h deleted file mode 100644 index 285de8e..0000000 --- a/include/printers/dump.h +++ /dev/null @@ -1,62 +0,0 @@ -// -// Created by ztuowen on 9/24/16. -// - -#ifndef CHILL_DUMP_H -#define CHILL_DUMP_H -/*! - * \file - * \brief this replace the old dump function in the chillAST - */ - -namespace chill { - namespace printer{ - class Dump : public GenericPrinter { - public: - Dump() {} - virtual void print(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_BinaryOperator *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CallExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CompoundStmt *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CStyleAddressOf *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CStyleCastExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CudaFree *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CudaKernelCall *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CudaMalloc *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CudaMemcpy *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_CudaSyncthreads *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_DeclRefExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_FloatingLiteral *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_ForStmt *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_Free *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_FunctionDecl *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_IfStmt *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_IntegerLiteral *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_ImplicitCastExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_MacroDefinition *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_Malloc *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_MemberExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_NULL *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_NoOp *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_ParenExpr *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_Preprocessing *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_RecordDecl *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_ReturnStmt *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_Sizeof *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_SourceFile *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_TypedefDecl *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_TernaryOperator *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_UnaryOperator *n, std::ostringstream &o); - virtual void print(std::string ident, chillAST_VarDecl *n, std::ostringstream &o); - /*! - * Just prints everything. Indent is igored due to need to limit the number of output - * @param ident - * @param n - * @param o - */ - virtual void print(string ident, chillAST_Node *n, ostringStream &o); - }; - } -} - -#endif //CHILL_DUMP_H diff --git a/lib/chillcg/include/code_gen/CG_chillRepr.h b/lib/chillcg/include/code_gen/CG_chillRepr.h index bcc267f..2d6d688 100755 --- a/lib/chillcg/include/code_gen/CG_chillRepr.h +++ b/lib/chillcg/include/code_gen/CG_chillRepr.h @@ -43,7 +43,7 @@ namespace omega { stmtclassname = strdup(chillast->getTypeString()); //fprintf(stderr, "made new chillRepr of class %s\n", stmtclassname); if (chillast->getType() == CHILLAST_NODE_COMPOUNDSTMT) { - std::vector<chillAST_Node *> children = chillast->getChildren(); + std::vector<chillAST_Node *> &children = *(chillast->getChildren()); int numchildren = children.size(); for (int i = 0; i < numchildren; i++) { chillnodes.push_back(children[i]); diff --git a/src/chillASTs.cc b/src/chillASTs.cc index c86a563..be0f5d5 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -4,6 +4,7 @@ #include <chilldebug.h> #include <stack> #include "chillAST.h" +#include "printer/dump.h" using namespace std; @@ -395,18 +396,6 @@ void chillAST_SourceFile::printToFile(char *filename) { } - -void chillAST_SourceFile::dump(int indent, FILE *fp) { - fflush(fp); - fprintf(fp, "\n//CHILL AST originally from file '%s'\n", SourceFileName); - int numchildren = children.size(); - for (int i = 0; i < numchildren; i++) { - children[i]->dump(indent, fp); - } - fflush(fp); -}; - - chillAST_MacroDefinition *chillAST_SourceFile::findMacro(const char *name) { int numMacros = macrodefinitions.size(); for (int i = 0; i < numMacros; i++) { @@ -639,6 +628,20 @@ void chillAST_RecordDecl::printStructure(int indent, FILE *fp) { fflush(fp); } +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(); + for (int i=0; i<numparameters; i++) { + if (i!=0) fprintf(fp, ", "); + chillAST_VarDecl *p = (*parameters)[i]; + p->print(0, fp); // note: no indent, as this is in the function parens + } + fprintf(fp, " )"); // end of input parameters + +} + + chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname, void *unique) { CHILL_DEBUG_PRINT("chillAST_FunctionDecl::chillAST_FunctionDecl with unique %p\n", unique); if (fname) @@ -1256,8 +1259,8 @@ void chillAST_ForStmt::addSyncs() { if (parent->isCompoundStmt()) { //fprintf(stderr, "ForStmt parent is CompoundStmt 0x%x\n", parent); - vector<chillAST_Node *> chillin = parent->getChildren(); - int numc = chillin.size(); + vector<chillAST_Node *> *chillin = parent->getChildren(); + int numc = chillin->size(); //fprintf(stderr, "ForStmt parent is CompoundStmt 0x%x with %d children\n", parent, numc); for (int i = 0; i < numc; i++) { if (this == parent->getChild(i)) { @@ -1270,7 +1273,7 @@ void chillAST_ForStmt::addSyncs() { } chillin = parent->getChildren(); - int nowc = chillin.size(); + int nowc = chillin->size(); //fprintf(stderr, "old, new number of children = %d %d\n", numc, nowc); } else { @@ -1673,29 +1676,6 @@ chillAST_IntegerLiteral *chillAST_BinaryOperator::evalAsIntegerLiteral() { return new chillAST_IntegerLiteral(evalAsInt()); // ?? } -void chillAST_BinaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(BinaryOperator '%s'\n", op); - - if (lhs) lhs->dump(indent + 1, fp); // lhs could be null - else { - chillindent(indent + 1, fp); - fprintf(fp, "(NULL)\n"); - } - fflush(fp); - - if (rhs) rhs->dump(indent + 1, fp); // rhs could be null - else { - chillindent(indent + 1, fp); - fprintf(fp, "(NULL)\n"); - } - fflush(fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - void chillAST_BinaryOperator::print(int indent, FILE *fp) { printPreprocBEFORE(indent, fp); @@ -1950,17 +1930,6 @@ chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_No if (rhs) rhs->setParent(this); } -void chillAST_TernaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(TernaryOperator '%s'\n", op); - condition->dump(indent + 1, fp); - lhs->dump(indent + 1, fp); - rhs->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - void chillAST_TernaryOperator::print(int indent, FILE *fp) { printPreprocBEFORE(indent, fp); chillindent(indent, fp); @@ -2195,45 +2164,6 @@ void chillAST_ArraySubscriptExpr::gatherIndeces(std::vector<chillAST_Node *> &in ind.push_back(index); } - -void chillAST_ArraySubscriptExpr::dump(int indent, FILE *fp) { -// fprintf(stderr, "\n%p chillAST_ArraySubscriptExpr::dump() basedecl %p\n", basedecl); - - char *local; - if (basedecl && basedecl->vartype) { - local = strdup(basedecl->vartype); - } else { - fprintf(stderr, "%p chillAST_ArraySubscriptExpr::dump(), no basedecl ???\n", this); - local = strdup(""); - //fprintf(stderr, "base is "); base->dump(); printf("\n"); base->print(); printf("\n"); fflush(stdout); - //print(); printf("\n"); fflush(stdout); - } - - - char *space = rindex(local, ' '); // can't use index because it's a class member! - if (space) *space = '\0'; // turn "float *" into "float" - - chillindent(indent, fp); - //fprintf(fp, "(ArraySubscriptExpr '%s' ", local); - if (basedecl) { - //fprintf(stderr, " chillAST_ArraySubscriptExpr::dump() basedecl is of type %s\n", basedecl->getTypeString()); - fprintf(fp, "(ArraySubscriptExpr (%s) '%s' ", basedecl->varname, local); - } else fprintf(stderr, " chillAST_ArraySubscriptExpr::dump() has no basedecl\n"); - free(local); - - if (imwrittento) { - if (imreadfrom) fprintf(fp, "lvalue AND rvalue\n"); - else fprintf(fp, "lvalue\n"); - } else fprintf(fp, "rvalue\n"); - base->dump(indent + 1, fp); - index->dump(indent + 1, fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - - void chillAST_ArraySubscriptExpr::print(int indent, FILE *fp) { base->print(indent, fp); fprintf(fp, "["); @@ -2955,13 +2885,6 @@ void chillAST_IntegerLiteral::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_IntegerLiteral::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(IntegerLiteral 'int' %d)\n", value); - fflush(fp); -} - - class chillAST_Node *chillAST_IntegerLiteral::constantFold() { return this; } // can never do anything @@ -3089,19 +3012,6 @@ void chillAST_FloatingLiteral::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_FloatingLiteral::dump(int indent, FILE *fp) { - chillindent(indent, fp); - // 2x2 cases ??? - if (precision == 1) - fprintf(fp, "(FloatingLiteral 'float' "); - else fprintf(fp, "(FloatingLiteral 'double' "); - - if (float0double1 == 0) fprintf(fp, "%f)\n", value); // %f gives enough digits - else fprintf(fp, "%f)\n", doublevalue); // %f gives enough digits - fflush(fp); -} - - chillAST_Node *chillAST_FloatingLiteral::constantFold() { return this; }; // NOOP chillAST_Node *chillAST_FloatingLiteral::clone() { @@ -3142,20 +3052,6 @@ void chillAST_UnaryOperator::print(int indent, FILE *fp) { fflush(fp); } - -void chillAST_UnaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(UnaryOperator "); - if (prefix) fprintf(fp, "prefix "); - else fprintf(fp, "postfix "); - fprintf(fp, "%s\n", op); - subexpr->dump(indent + 1, fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - - void chillAST_UnaryOperator::gatherVarLHSUsage(vector<chillAST_VarDecl *> &decls) { if ((!strcmp("++", op)) || (!strcmp("--", op))) { subexpr->gatherVarUsage(decls); // do all unary modify the subexpr? (no, - ) @@ -3377,16 +3273,6 @@ void chillAST_CStyleCastExpr::print(int indent, FILE *fp) { fflush(fp); }; - -void chillAST_CStyleCastExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CStyleCastExpr (%s) \n", towhat); - subexpr->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - class chillAST_Node *chillAST_CStyleCastExpr::constantFold() { subexpr = subexpr->constantFold(); return this; @@ -3450,15 +3336,6 @@ void chillAST_CStyleAddressOf::print(int indent, FILE *fp) { fflush(fp); }; -void chillAST_CStyleAddressOf::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CStyleAddressOf \n"); - subexpr->print(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - class chillAST_Node *chillAST_CStyleAddressOf::constantFold() { subexpr = subexpr->constantFold(); return this; @@ -3635,12 +3512,6 @@ void chillAST_CudaFree::print(int indent, FILE *fp) { fflush(fp); }; -void chillAST_CudaFree::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CudaFree %s )\n", variable->varname); - fflush(fp); -}; - class chillAST_Node *chillAST_CudaFree::constantFold() { return this; } @@ -3765,13 +3636,6 @@ void chillAST_CudaSyncthreads::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_CudaSyncthreads::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(syncthreads)\n"); - fflush(fp); -} - - chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval) { returnvalue = retval; if (returnvalue) returnvalue->setParent(this); @@ -3899,29 +3763,6 @@ void chillAST_CallExpr::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_CallExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CallExpr "); - //fprintf(stderr, "callee type %s\n", callee->getTypeString()); - chillAST_FunctionDecl *fd = NULL; - if (callee->isDeclRefExpr()) { // always? - chillAST_DeclRefExpr *dre = (chillAST_DeclRefExpr *) callee; - fd = dre->getFunctionDecl(); // if NULL, we've got a Vardecl instead - if (fd) { - //fd->print(); - fprintf(fp, "%s\n", fd->returnType); - } - - callee->dump(indent + 1, fp); - if (fd) { - int numparams = fd->getSymbolTable()->size(); - for (int i = 0; i < numparams; i++) (*(fd->getSymbolTable()))[i]->dump(indent + 1, fp); - } - } - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - void chillAST_CallExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { for (int i = 0; i < args.size(); i++) { args[i]->gatherArrayRefs(refs, writtento); @@ -4422,24 +4263,6 @@ void chillAST_VarDecl::printName(int in, FILE *fp) { fprintf(fp, "%s", varname); }; - -void chillAST_VarDecl::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(VarDecl \"'%s' '%s' '%s'\" n_dim %d ) ", vartype, varname, arraypart, numdimensions); - - //fprintf(fp, "vardef %p\n", vardef); - //if (vardef) fprintf(fp, "(typedef or struct!)\n"); - //fprintf(fp, "typedefinition %p\n", typedefinition); - //if (isStruct) fprintf(fp, "isStruct\n"); - - //if (isAParameter) fprintf(fp, "PARAMETER\n"); - //else fprintf(fp, "NOT PARAMETER\n"); - fflush(fp); - - //exit(-1); // see what called this -}; - - chillAST_RecordDecl *chillAST_VarDecl::getStructDef() { if (vardef) return vardef; if (typedefinition) return typedefinition->getStructDef(); @@ -4562,27 +4385,6 @@ void chillAST_CompoundStmt::loseLoopWithLoopVar(char *var) { //fprintf(stderr, "CompoundStmt node 0x%x done recursing\n", this ); } - -void chillAST_CompoundStmt::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CompoundStmt \n"); - int numchildren = children.size(); - - //for (int i=0; i<numchildren; i++) { - // fprintf(fp, "%d %s 0x%x\n", i, children[i]->getTypeString(), children[i]); - //} - //fprintf(fp, "\n"); - - for (int i = 0; i < numchildren; i++) { - children[i]->dump(indent + 1, fp); - fprintf(fp, "\n"); // ??? - fflush(fp); - } - chillindent(indent, fp); - fprintf(fp, ")\n"); -}; - - chillAST_Node *chillAST_CompoundStmt::constantFold() { //fprintf(stderr, "chillAST_CompoundStmt::constantFold()\n"); for (int i = 0; i < children.size(); i++) children[i] = children[i]->constantFold(); @@ -4714,15 +4516,6 @@ void chillAST_ParenExpr::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_ParenExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(ParenExpr \n"); - subexpr->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - - void chillAST_ParenExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { subexpr->gatherArrayRefs(refs, writtento); } @@ -4786,12 +4579,6 @@ void chillAST_Sizeof::print(int indent, FILE *fp) { fflush(fp); } - -void chillAST_Sizeof::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(Sizeof %s )\n", thing); -} - void chillAST_Sizeof::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {} void chillAST_Sizeof::gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento) {} @@ -4852,9 +4639,9 @@ void insertNewDeclAtLocationOfOldIfNeeded(chillAST_VarDecl *newdecl, chillAST_Va // find actual location of old decl and insert new one there //fprintf(stderr, "oldparent is of type %s\n", oldparent->getTypeString()); // better be compoundstmt ?? - vector<chillAST_Node *> children = oldparent->getChildren(); + vector<chillAST_Node *> *children = oldparent->getChildren(); - int numchildren = children.size(); + int numchildren = children->size(); //fprintf(stderr, "oldparent has %d children\n", numchildren); if (numchildren == 0) { @@ -4871,11 +4658,11 @@ void insertNewDeclAtLocationOfOldIfNeeded(chillAST_VarDecl *newdecl, chillAST_Va for (int i = 0; i < numchildren; i++) { chillAST_Node *child = oldparent->getChild(i); //fprintf(stderr, "child %d @ 0x%x is of type %s\n", i, child, child->getTypeString()); - if (children[i] == olddecl) { + if ((*children)[i] == olddecl) { index = i; //fprintf(stderr, "found old decl at index %d\n", index); } - if (children[i] == newdecl) { + if ((*children)[i] == newdecl) { newalreadythere = true; //fprintf(stderr, "new already there @ index %d\n", i); } @@ -5353,4 +5140,14 @@ void chillAST_Node::addParameter(chillAST_VarDecl *vd) { 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.print("",this,std::cerr); + } else { + chill::printer::Dump d; + d.print("",this,std::cout); + } }
\ No newline at end of file diff --git a/src/ir_clang.cc b/src/ir_clang.cc index 44ab3ee..f348099 100755 --- a/src/ir_clang.cc +++ b/src/ir_clang.cc @@ -2250,6 +2250,11 @@ IR_clangCode::IR_clangCode(const char *fname, const char *proc_name) : IR_Code() pInstance->setCurrentFunction(localFD); + CHILL_DEBUG_BEGIN + fprintf(stderr, "Dumping AST for localFD\n"); + localFD->dump(); + CHILL_DEBUG_END + chillAST_Node *b = localFD->getBody(); // we do this just because it will get done next CHILL_DEBUG_PRINT("calling new CG_chillBuilder() umwut?\n"); @@ -2600,7 +2605,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl // bundled up into an IR_Block // ifs and loops will get their own entry - std::vector<chillAST_Node *> children; + std::vector<chillAST_Node *> *children; if (blockast->getType() == CHILLAST_NODE_FORSTMT) { @@ -2639,7 +2644,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl children = blockast->getChildren(); } - int numchildren = children.size(); + int numchildren = children->size(); //fprintf(stderr, "basic block has %d statements\n", numchildren); //fprintf(stderr, "basic block is:\n"); //fprintf(stderr, "{\n"); @@ -2650,7 +2655,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl IR_chillBlock *basicblock = new IR_chillBlock(this); // no statements for (int i = 0; i < numchildren; i++) { //fprintf(stderr, "child %d is of type %s\n", i, children[i]->getTypeString()); - CHILLAST_NODE_TYPE typ = children[i]->getType(); + CHILLAST_NODE_TYPE typ = (*children)[i]->getType(); if (typ == CHILLAST_NODE_LOOP) { if (numchildren == 1) { CHILL_DEBUG_PRINT("found a For statement (Loop)\n"); @@ -2667,16 +2672,16 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl } CHILL_DEBUG_PRINT("pushing the loop at %d\n", i); - controls.push_back(new IR_chillLoop(this, (chillAST_ForStmt *) children[i])); + controls.push_back(new IR_chillLoop(this, (chillAST_ForStmt *) (*children)[i])); } //else if (typ == CHILLAST_NODE_IFSTMT ) // TODO else { // straight line code - basicblock->addStatement(children[i]); + basicblock->addStatement((*children)[i]); CHILL_DEBUG_BEGIN fprintf(stderr, "straight line code\n"); fprintf(stderr, "child %d = \n", i); - children[i]->print(); + (*children)[i]->print(); printf("\n"); fflush(stdout); fprintf(stderr, "child %d is part of a basic block\n", i); @@ -2869,13 +2874,13 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { fflush(stdout); fprintf(stderr, "\n}\n"); - std::vector<chillAST_Node *> oldparentcode = par->getChildren(); // probably only works for compoundstmts + std::vector<chillAST_Node *> *oldparentcode = par->getChildren(); // probably only works for compoundstmts //fprintf(stderr, "ir_clang.cc oldparentcode\n"); // find loop in the parent int index = -1; - int numstatements = oldparentcode.size(); - for (int i = 0; i < numstatements; i++) if (oldparentcode[i] == forstmt) { index = i; } + int numstatements = oldparentcode->size(); + for (int i = 0; i < numstatements; i++) if ((*oldparentcode)[i] == forstmt) { index = i; } if (index == -1) { fprintf(stderr, "ir_clang.cc can't find the loop in its parent\n"); exit(-1); @@ -2923,11 +2928,11 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { //fprintf(stderr, "\nafter inserting %d statements into the Clang IR,", numnew); CHILL_DEBUG_BEGIN fprintf(stderr, "new parent2 is\n\n{\n"); - std::vector<chillAST_Node *> newparentcode = par->getChildren(); - for (int i = 0; i < newparentcode.size(); i++) { + std::vector<chillAST_Node *> *newparentcode = par->getChildren(); + for (int i = 0; i < newparentcode->size(); i++) { fflush(stdout); //fprintf(stderr, "%d ", i); - newparentcode[i]->print(); + (*newparentcode)[i]->print(); printf(";\n"); fflush(stdout); } diff --git a/src/printers/cfamily.cpp b/src/printer/cfamily.cpp index 305feee..8bfeb82 100644 --- a/src/printers/cfamily.cpp +++ b/src/printer/cfamily.cpp @@ -2,7 +2,7 @@ // Created by ztuowen on 9/24/16. // -#include "printers/cfamily.h" +#include "printer/cfamily.h" using namespace std; using namespace chill::printer; diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp new file mode 100644 index 0000000..2b2a35c --- /dev/null +++ b/src/printer/dump.cpp @@ -0,0 +1,226 @@ +// +// Created by ztuowen on 9/24/16. +// + +#include "printer/dump.h" +#include "chillAST.h" + +using namespace chill::printer; +using namespace std; + +void dumpVector(GenericPrinter *p, string ident, chillAST_NodeList *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} +void dumpVector(GenericPrinter *p, string ident, chillAST_SymbolTable *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} +void dumpVector(GenericPrinter *p, string ident, chillAST_TypedefTable *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} + +void Dump::print(string ident, chillAST_Node *n, ostream &o) { + o << "(" << n->getTypeString() << " "; + if (n->getParameters()) { + o << "(Params: "; + dumpVector(this, ident, n->getParameters(), o); + o << ") "; + } + if (n->getSymbolTable()) { + o << "(VarScope: "; + dumpVector(this, ident, n->getSymbolTable(), o); + o << ") "; + } + if (n->getTypedefTable()) { + o << "(TypeDef: "; + dumpVector(this, ident, n->getTypedefTable(), o); + o << ") "; + } + o << ": "; + // Recurse + GenericPrinter::print(ident, n, o); + o << ") "; +} + +void Dump::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) { + if (n->basedecl) + o << "(" << n->basedecl->varname << ") "; + if (n->basedecl && n->basedecl->vartype) + o << n->basedecl->vartype; + if (n->imwrittento) { + if (n->imreadfrom) + o << "lvalue AND rvalue "; + else + o << "lvalue "; + } else o << "rvalue "; + print(ident, n->base, o); + print(ident, n->index, o); +} + +void Dump::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) { + o << n->op << " "; + if (n->lhs) print(ident, n->lhs, o); + else o << "(NULL) "; + if (n->rhs) print(ident, n->rhs, o); + else o << "(NULL) "; +} + +void Dump::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { + if (n->callee) + print(ident, n->callee, o); +} + +void Dump::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) { + dumpVector(this, ident, n->getChildren(), o); +} + +void Dump::printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) { + o << n->towhat << " "; + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { + o << n->variable->varname << " "; +} + +void Dump::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { + CHILL_ERROR("Not implemented"); +} + +void Dump::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) { + print(ident, n->devPtr, o); + print(ident, n->sizeinbytes, 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); +} + +void Dump::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) { + chillAST_VarDecl *vd = n->getVarDecl(); + if (vd) + if (vd->isAParameter) o << "ParmVar "; else o << "Var "; + o << n->declarationName << " "; + chillAST_FunctionDecl *fd = n->getFunctionDecl(); + if (fd) dumpVector(this, ident, fd->getParameters(), o); +} + +void Dump::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { + if (n->precision == 1) o << "float "; + else o << "double "; + if (n->float0double1) o << n->value; + else o << n->doublevalue; +} + +void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) { + print(ident, n->init, o); + print(ident, n->cond, o); + print(ident, n->incr, o); + print(ident, n->body, o); +} + +void Dump::printS(std::string ident, chillAST_Free *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) { + if (n->filename) o << n->filename << " "; + if (n->isFromSourceFile) o << "FromSourceFile" << " "; + o << n->returnType << " " << n->functionName << " "; + if (n->getBody()) print(ident, n->getBody(), o); +} + +void Dump::printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) { + print(ident, n->cond, o); + print(ident, n->thenpart, o); + if (n->elsepart) + print(ident, n->elsepart, o); +} + +void Dump::printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) { + o << n->value << " "; +} + +void Dump::printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) { + 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) { + print(ident, n->sizeexpr, o); +} + +void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) { + print(ident, n->base, o); + if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "-> "; + else o << ". "; + o << n->member << " "; +} + +void Dump::printS(std::string ident, chillAST_NULL *n, std::ostream &o) { + o << "(NULL) "; +} + +void Dump::printS(std::string ident, chillAST_NoOp *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) { + // TODO access control + o << n->getName() << " "; + o << n->isAStruct() << " "; + o << n->isAUnion() << " "; +} + +void Dump::printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) { + if (n->returnvalue) print(ident, n->returnvalue, o); +} + +void Dump::printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) { + o << n->thing << " "; +} + +void Dump::printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) { + dumpVector(this, ident, n->getChildren(), o); +} + +void Dump::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) { + o << n->underlyingtype << " " << n->newtype << " " << n->arraypart << " "; +} + +void Dump::printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) { + o << n->op << " "; + print(ident, n->condition, o); + print(ident, n->lhs, o); + print(ident, n->rhs, o); +} + +void Dump::printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) { + if (n->prefix) o << "prefix "; + else o << "postfix "; + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) { + o << "\"'" << n->vartype << "' '" << n->varname << "' '" << n->arraypart << "'\" dim " << n->numdimensions << " "; +} + diff --git a/src/printer/generic.cpp b/src/printer/generic.cpp new file mode 100644 index 0000000..67c694f --- /dev/null +++ b/src/printer/generic.cpp @@ -0,0 +1,200 @@ +// +// Created by ztuowen on 9/24/16. +// + +#include "printer/generic.h" + +using namespace chill::printer; + +void GenericPrinter::print(std::string ident, chillAST_Node *n, std::ostream &o) { + switch (n->getType()) { + case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: + printS(ident, dynamic_cast<chillAST_ArraySubscriptExpr *>(n), o); + break; + case CHILLAST_NODE_BINARYOPERATOR: + printS(ident, dynamic_cast<chillAST_BinaryOperator *>(n), o); + break; + case CHILLAST_NODE_CALLEXPR: + printS(ident, dynamic_cast<chillAST_CallExpr *>(n), o); + break; + case CHILLAST_NODE_COMPOUNDSTMT: + printS(ident, dynamic_cast<chillAST_CompoundStmt *>(n), o); + break; + case CHILLAST_NODE_CSTYLEADDRESSOF: + printS(ident, dynamic_cast<chillAST_CStyleAddressOf *>(n), o); + break; + case CHILLAST_NODE_CSTYLECASTEXPR: + printS(ident, dynamic_cast<chillAST_CStyleCastExpr *>(n), o); + break; + case CHILLAST_NODE_CUDAFREE: + printS(ident, dynamic_cast<chillAST_CudaFree *>(n), o); + break; +// case CHILLAST_NODE_CUDAKERNELCALL: +// printS(ident, dynamic_cast<chillAST_CudaKernelCall *>(n), o); +// break; + case CHILLAST_NODE_CUDAMALLOC: + printS(ident, dynamic_cast<chillAST_CudaMalloc *>(n), o); + break; + case CHILLAST_NODE_CUDAMEMCPY: + printS(ident, dynamic_cast<chillAST_CudaMemcpy *>(n), o); + break; + case CHILLAST_NODE_CUDASYNCTHREADS: + printS(ident, dynamic_cast<chillAST_CudaSyncthreads *>(n), o); + break; + case CHILLAST_NODE_DECLREFEXPR: + printS(ident, dynamic_cast<chillAST_DeclRefExpr *>(n), o); + break; + case CHILLAST_NODE_FLOATINGLITERAL: + printS(ident, dynamic_cast<chillAST_FloatingLiteral *>(n), o); + break; + case CHILLAST_NODE_LOOP: +// case CHILLAST_NODE_FORSTMT: + printS(ident, dynamic_cast<chillAST_ForStmt *>(n), o); + break; + case CHILLAST_NODE_FREE: + printS(ident, dynamic_cast<chillAST_Free *>(n), o); + break; + case CHILLAST_NODE_FUNCTIONDECL: + printS(ident, dynamic_cast<chillAST_FunctionDecl *>(n), o); + break; + case CHILLAST_NODE_IFSTMT: + printS(ident, dynamic_cast<chillAST_IfStmt *>(n), o); + break; + case CHILLAST_NODE_IMPLICITCASTEXPR: + printS(ident, dynamic_cast<chillAST_ImplicitCastExpr *>(n), o); + break; + case CHILLAST_NODE_INTEGERLITERAL: + printS(ident, dynamic_cast<chillAST_IntegerLiteral *>(n), o); + break; + case CHILLAST_NODE_MACRODEFINITION: + printS(ident, dynamic_cast<chillAST_MacroDefinition *>(n), o); + break; + case CHILLAST_NODE_MALLOC: + printS(ident, dynamic_cast<chillAST_Malloc *>(n), o); + break; + case CHILLAST_NODE_MEMBEREXPR: + printS(ident, dynamic_cast<chillAST_MemberExpr *>(n), o); + break; + case CHILLAST_NODE_NOOP: + printS(ident, dynamic_cast<chillAST_NoOp *>(n), o); + break; + case CHILLAST_NODE_NULL: + printS(ident, dynamic_cast<chillAST_NULL *>(n), o); + break; + case CHILLAST_NODE_PARENEXPR: + printS(ident, dynamic_cast<chillAST_ParenExpr *>(n), o); + break; + case CHILLAST_NODE_PREPROCESSING: + printS(ident, dynamic_cast<chillAST_Preprocessing *>(n), o); + break; + case CHILLAST_NODE_RECORDDECL: + printS(ident, dynamic_cast<chillAST_RecordDecl *>(n), o); + break; + case CHILLAST_NODE_RETURNSTMT: + printS(ident, dynamic_cast<chillAST_ReturnStmt *>(n), o); + break; + case CHILLAST_NODE_SIZEOF: + printS(ident, dynamic_cast<chillAST_Sizeof *>(n), o); + break; + case CHILLAST_NODE_TRANSLATIONUNIT: +// case CHILLAST_NODE_SOURCEFILE: + printS(ident, dynamic_cast<chillAST_SourceFile *>(n), o); + break; + case CHILLAST_NODE_TERNARYOPERATOR: + printS(ident, dynamic_cast<chillAST_TernaryOperator *>(n), o); + break; + case CHILLAST_NODE_TYPEDEFDECL: + printS(ident, dynamic_cast<chillAST_TypedefDecl *>(n), o); + break; + case CHILLAST_NODE_UNARYOPERATOR: + printS(ident, dynamic_cast<chillAST_UnaryOperator *>(n), o); + break; + case CHILLAST_NODE_VARDECL: + printS(ident, dynamic_cast<chillAST_VarDecl *>(n), o); + break; + case CHILLAST_NODE_UNKNOWN: + default: + CHILL_ERROR("Printing an unknown type of Node: %s\n", n->getTypeString()); + } + o.flush(); +} + +int GenericPrinter::getPrec(chillAST_Node *n) { + switch (n->getType()) { + case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: + return getPrecS(dynamic_cast<chillAST_ArraySubscriptExpr *>(n)); + case CHILLAST_NODE_BINARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_BinaryOperator *>(n)); + case CHILLAST_NODE_CALLEXPR: + return getPrecS(dynamic_cast<chillAST_CallExpr *>(n)); + case CHILLAST_NODE_COMPOUNDSTMT: + return getPrecS(dynamic_cast<chillAST_CompoundStmt *>(n)); + case CHILLAST_NODE_CSTYLEADDRESSOF: + return getPrecS(dynamic_cast<chillAST_CStyleAddressOf *>(n)); + case CHILLAST_NODE_CSTYLECASTEXPR: + return getPrecS(dynamic_cast<chillAST_CStyleCastExpr *>(n)); + case CHILLAST_NODE_CUDAFREE: + return getPrecS(dynamic_cast<chillAST_CudaFree *>(n)); +// case CHILLAST_NODE_CUDAKERNELCALL: +// return getPrecS(dynamic_cast<chillAST_CudaKernelCall *>(n)); + case CHILLAST_NODE_CUDAMALLOC: + return getPrecS(dynamic_cast<chillAST_CudaMalloc *>(n)); + case CHILLAST_NODE_CUDAMEMCPY: + return getPrecS(dynamic_cast<chillAST_CudaMemcpy *>(n)); + case CHILLAST_NODE_CUDASYNCTHREADS: + return getPrecS(dynamic_cast<chillAST_CudaSyncthreads *>(n)); + case CHILLAST_NODE_DECLREFEXPR: + return getPrecS(dynamic_cast<chillAST_DeclRefExpr *>(n)); + case CHILLAST_NODE_FLOATINGLITERAL: + return getPrecS(dynamic_cast<chillAST_FloatingLiteral *>(n)); + case CHILLAST_NODE_LOOP: +// case CHILLAST_NODE_FORSTMT: + return getPrecS(dynamic_cast<chillAST_ForStmt *>(n)); + case CHILLAST_NODE_FREE: + return getPrecS(dynamic_cast<chillAST_Free *>(n)); + case CHILLAST_NODE_FUNCTIONDECL: + return getPrecS(dynamic_cast<chillAST_FunctionDecl *>(n)); + case CHILLAST_NODE_IFSTMT: + return getPrecS(dynamic_cast<chillAST_IfStmt *>(n)); + case CHILLAST_NODE_IMPLICITCASTEXPR: + return getPrecS(dynamic_cast<chillAST_ImplicitCastExpr *>(n)); + case CHILLAST_NODE_INTEGERLITERAL: + return getPrecS(dynamic_cast<chillAST_IntegerLiteral *>(n)); + case CHILLAST_NODE_MACRODEFINITION: + return getPrecS(dynamic_cast<chillAST_MacroDefinition *>(n)); + case CHILLAST_NODE_MALLOC: + return getPrecS(dynamic_cast<chillAST_Malloc *>(n)); + case CHILLAST_NODE_MEMBEREXPR: + return getPrecS(dynamic_cast<chillAST_MemberExpr *>(n)); + case CHILLAST_NODE_NOOP: + return getPrecS(dynamic_cast<chillAST_NoOp *>(n)); + case CHILLAST_NODE_NULL: + return getPrecS(dynamic_cast<chillAST_NULL *>(n)); + case CHILLAST_NODE_PARENEXPR: + return getPrecS(dynamic_cast<chillAST_ParenExpr *>(n)); + case CHILLAST_NODE_PREPROCESSING: + return getPrecS(dynamic_cast<chillAST_Preprocessing *>(n)); + case CHILLAST_NODE_RECORDDECL: + return getPrecS(dynamic_cast<chillAST_RecordDecl *>(n)); + case CHILLAST_NODE_RETURNSTMT: + return getPrecS(dynamic_cast<chillAST_ReturnStmt *>(n)); + case CHILLAST_NODE_SIZEOF: + return getPrecS(dynamic_cast<chillAST_Sizeof *>(n)); + case CHILLAST_NODE_TRANSLATIONUNIT: +// case CHILLAST_NODE_SOURCEFILE: + return getPrecS(dynamic_cast<chillAST_SourceFile *>(n)); + case CHILLAST_NODE_TERNARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_TernaryOperator *>(n)); + case CHILLAST_NODE_TYPEDEFDECL: + return getPrecS(dynamic_cast<chillAST_TypedefDecl *>(n)); + case CHILLAST_NODE_UNARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_UnaryOperator *>(n)); + case CHILLAST_NODE_VARDECL: + return getPrecS(dynamic_cast<chillAST_VarDecl *>(n)); + case CHILLAST_NODE_UNKNOWN: + default: + CHILL_ERROR("Getting precedence an unknown type of Node: %s\n", n->getTypeString()); + return INT8_MAX; + } +} + diff --git a/src/printers/dump.cpp b/src/printers/dump.cpp deleted file mode 100644 index 334552f..0000000 --- a/src/printers/dump.cpp +++ /dev/null @@ -1,160 +0,0 @@ -// -// Created by ztuowen on 9/24/16. -// - -#include "printers/dump.h" - -using namespace chill::printer; -using namespace std; - -void dumpVector(GenericPrinter *p, string ident, chillAST_NodeList *n, ostringstream &o) { - for (int i = 0; i < n->size(); ++i) - p->print("", (*n)[i], o); -} - -void Dump::print(string ident, chillAST_Node *n, ostringStream &o) { - o << "(" << n->getTypeString() << " "; - if (n->getParameters()) { - o << "(Params: "; - dumpVector(this, ident, n->getParameters(), o); - o << ") "; - } - if (n->getSymbolTable()) { - o << "(VarScope: "; - dumpVector(this, ident, n->getSymbolTable(), o); - o << ") "; - } - if (n->getTypedefTable()) { - o << "(TypeDef: "; - dumpVector(this, ident, n->getTypedefTable(), o); - o << ") "; - } - o << ": "; - // Recurse - GenericPrinter::print(ident, n, o); - o << ") "; -} - -void Dump::print(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_BinaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CallExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CompoundStmt *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CStyleAddressOf *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CStyleCastExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaFree *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaKernelCall *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaMalloc *n, std::ostringstream &o) { - print(ident, n->devPtr, o); - print(ident, n->sizeinbytes, o); -} - -void Dump::print(std::string ident, chillAST_CudaMemcpy *n, std::ostringstream &o) { - o << cudaMemcpyKind << " "; - print(ident, n->dest, o); - print(ident, n->src, o); - print(ident, n->size, o); -}; - -void Dump::print(std::string ident, chillAST_CudaSyncthreads *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_DeclRefExpr *n, std::ostringstream &o) { - chillAST_VarDecl *vd = n->getVarDecl(); - if (vd) - if (vd->isAParameter) o << "ParmVar "; else o << "Var "; - o << n->declarationName << " "; - chillAST_FunctionDecl *fd = n->getFunctionDecl(); - if (fd) dumpVector(this, ident, fd->getParameters(), o); -} - -void Dump::print(std::string ident, chillAST_FloatingLiteral *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_ForStmt *n, std::ostringstream &o) { - print(ident, n->init, o); - print(ident, n->cond, o); - print(ident, n->incr, o); - print(ident, n->body, o); -} - -void Dump::print(std::string ident, chillAST_Free *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_FunctionDecl *n, std::ostringstream &o) { - if (n->filename) o << filename << " "; - if (n->isFromSourceFile) o << "FromSourceFile" << " "; - o << n->returnType << " " << n->functionName << " "; - if (n->body) print(ident, n->body, o); -} - -void Dump::print(std::string ident, chillAST_IfStmt *n, std::ostringstream &o) { - print(ident, n->cond, o); - print(ident, n->thenpart, o); - if (n->elsepart) - print(ident, n->elsepart, o); -} - -void Dump::print(std::string ident, chillAST_IntegerLiteral *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_ImplicitCastExpr *n, std::ostringstream &o) { - print(ident, n->subexpr, o); -} - -void Dump::print(std::string ident, chillAST_MacroDefinition *n, std::ostringstream &o) { - o << n->macroName << " "; - dumpVector(this, ident, n->getParameters(), o); - print(ident, n->getBody(), o); - if (n->rhsString) o << " (aka " << n->rhsString << ") "; -} - -void Dump::print(std::string ident, chillAST_Malloc *n, std::ostringstream &o) { - print(ident, n->sizeexpr, o); -} - -void Dump::print(std::string ident, chillAST_MemberExpr *n, std::ostringstream &o) { - print(ident, n->base, o); - if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "-> "; - else o << ". "; - o << n->member << " "; -} - -void Dump::print(std::string ident, chillAST_NULL *n, std::ostringstream &o) { - o << "(NULL) "; -} - -void Dump::print(std::string ident, chillAST_NoOp *n, std::ostringstream &o) {}; - -void Dump::print(std::string ident, chillAST_ParenExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_Preprocessing *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_RecordDecl *n, std::ostringstream &o) { - // TODO access control - o << n->getName() << " "; - o << n->isAStruct() << " "; - o << n->isAUnion() << " "; -} - -void Dump::print(std::string ident, chillAST_ReturnStmt *n, std::ostringstream &o) { - if (n->returnvalue) print(ident, n->returnvalue, o); -} - -void Dump::print(std::string ident, chillAST_Sizeof *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_SourceFile *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_TypedefDecl *n, std::ostringstream &o) { - o << n->underlyingtype << " " << n->newtype << " " << n->arraypart << " "; -} - -void Dump::print(std::string ident, chillAST_TernaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_UnaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_VarDecl *n, std::ostringstream &o); - |