summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/chillAST/chillAST_def.hh6
-rw-r--r--include/chillAST/chillAST_node.hh47
-rw-r--r--include/chillAST/chillASTs.hh41
-rw-r--r--include/printer.h202
-rw-r--r--include/printers/cfamily.h56
-rw-r--r--include/printers/dump.h62
-rw-r--r--src/chillASTs.cc292
-rw-r--r--src/printers/cfamily.cpp8
-rw-r--r--src/printers/dump.cpp160
9 files changed, 542 insertions, 332 deletions
diff --git a/include/chillAST/chillAST_def.hh b/include/chillAST/chillAST_def.hh
index 59a7f95..665f7ff 100644
--- a/include/chillAST/chillAST_def.hh
+++ b/include/chillAST/chillAST_def.hh
@@ -12,7 +12,7 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
-#include <vector> // std::vector
+#include <vector> // std::vector
#include <ir_enums.hh> // for IR_CONDITION_*
@@ -40,7 +40,7 @@ enum CHILLAST_NODE_TYPE {
CHILLAST_NODE_IMPLICITCASTEXPR,
CHILLAST_NODE_RETURNSTMT,
CHILLAST_NODE_CALLEXPR,
- CHILLAST_NODE_DECLSTMT,
+ //CHILLAST_NODE_DECLSTMT, not used
CHILLAST_NODE_PARENEXPR,
CHILLAST_NODE_CSTYLECASTEXPR,
CHILLAST_NODE_CSTYLEADDRESSOF,
@@ -143,6 +143,8 @@ class chillAST_FloatingLiteral;
class chillAST_ImplicitCastExpr;
+class chillAST_IfStmt;
+
class chillAST_CStyleCastExpr;
class chillAST_CStyleAddressOf;
diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh
index 9788588..2c4ee8d 100644
--- a/include/chillAST/chillAST_node.hh
+++ b/include/chillAST/chillAST_node.hh
@@ -4,15 +4,19 @@
#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;
//! this node's children the only entity that holds childs/subexpressions
chillAST_NodeList children;
+ //! The parameters that this nodes accept, which every elements is in the scope, but they are not defined in children
+ chillAST_SymbolTable *parameters;
//! Symbol Scoping
chillAST_SymbolTable *symbolTable;
//! typedef Scoping
@@ -40,9 +44,12 @@ public:
filename = NULL;
symbolTable = NULL;
typedefTable = NULL;
+ parameters = NULL;
}
//! the type of this current node
virtual CHILLAST_NODE_TYPE getType() {return CHILLAST_NODE_UNKNOWN;};
+ //! Get the human readable type name
+ const char *getTypeString() { return ChillAST_Node_Names[getType()]; };
//! the precedence of the current node, 0 being the highest
virtual int getPrec() {return INT8_MAX;}
@@ -434,14 +441,11 @@ public:
virtual void getTopLevelLoops(std::vector<chillAST_ForStmt *> &loops) {
int n = children.size();
- //fprintf(stderr, "get_top_level_loops of a %s with %d children\n", getTypeString(), n);
for (int i = 0; i < n; i++) {
- //fprintf(stderr, "child %d is a %s\n", i, children[i]->getTypeString());
if (children[i]->isForStmt()) {
loops.push_back(((chillAST_ForStmt *) (children[i])));
}
}
- //fprintf(stderr, "found %d top level loops\n", loops.size());
}
@@ -495,7 +499,6 @@ public:
}
- const char *getTypeString() { return ChillAST_Node_Names[getType()]; };
void setParent(chillAST_Node *p) { parent = p; };
@@ -505,42 +508,28 @@ public:
chillAST_SourceFile *getSourceFile() {
if (isSourceFile()) return ((chillAST_SourceFile *) this);
if (parent != NULL) return parent->getSourceFile();
- fprintf(stderr, "UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n",
- this, this->getTypeString());
+ CHILL_ERROR("UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n", this, this->getTypeString());
this->print();
- printf("\n\n");
- fflush(stdout);
exit(-1);
}
- void walk_parents() {
- fprintf(stderr, "wp: (%s) ", getTypeString());
- print();
- printf("\n");
- fflush(stdout);
- if (isSourceFile()) {
- fprintf(stderr, "(top sourcefile)\n\n");
- return;
- }
-
- if (parent) parent->walk_parents();
- else fprintf(stderr, "UHOH, %s has no parent??\n", getTypeString());
- return;
- }
-
+ // TODO DOC
virtual chillAST_Node *getEnclosingStatement(int level = 0);
+ // TODO DOC
virtual chillAST_VarDecl *multibase() {
fprintf(stderr, "(%s) forgot to implement multibase()\n", getTypeString());
exit(-1);
}
+ // TODO DOC
virtual chillAST_Node *multibase2() {
fprintf(stderr, "(%s) forgot to implement multibase2()\n", getTypeString());
exit(-1);
}
+ //! Get a vector of statements
virtual void gatherStatements(std::vector<chillAST_Node *> &statements) {
fprintf(stderr, "(%s) forgot to implement gatherStatements()\n", getTypeString());
dump();
@@ -549,19 +538,13 @@ public:
fprintf(stderr, "\n\n");
}
-
- virtual bool isSameAs(chillAST_Node *other) { // for tree comparison
- fprintf(stderr, "(%s) forgot to implement isSameAs()\n", getTypeString());
- dump();
- fflush(stdout);
- print();
- fprintf(stderr, "\n\n");
- }
-
void printPreprocBEFORE(int indent, FILE *fp);
void printPreprocAFTER(int indent, FILE *fp);
+ virtual chillAST_SymbolTable* getParameters() {return parameters;}
+ virtual chillAST_VarDecl* getParameter(const char * name);
+ virtual void addParameter(chillAST_VarDecl* name);
};
diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh
index 5c9010c..d61d183 100644
--- a/include/chillAST/chillASTs.hh
+++ b/include/chillAST/chillASTs.hh
@@ -18,11 +18,6 @@ public:
fflush(fp);
}
- void dump(int indent = 0, FILE *fp = stderr) {
- chillindent(indent, fp);
- fprintf(fp, "(NULL statement) ");
- fflush(fp);
- }
};
class chillAST_Preprocessing : public chillAST_Node {
@@ -96,10 +91,6 @@ public:
return underlyingtype;
};
- void dump(int indent = 0, FILE *fp = stderr) {
- fprintf(fp, "(TypedefDecl %s %s %s)\n", underlyingtype, newtype, arraypart);
- };
-
void print(int indent = 0, FILE *fp = stderr);
};
@@ -248,7 +239,6 @@ public:
// required methods that I can't seem to get to inherit
void print(int indent = 0, FILE *fp = stderr); // print CODE
- void dump(int indent = 0, FILE *fp = stderr); // print ast
char *stringRep(int indent = 0);
chillAST_Node *constantFold();
@@ -377,8 +367,6 @@ public:
chillAST_VarDecl *findSubpartByType(const char *typ);
- void dump(int indent = 0, FILE *fp = stderr);
-
void print(int indent = 0, FILE *fp = stderr);
void printStructure(int indent = 0, FILE *fp = stderr);
@@ -397,11 +385,6 @@ public:
char *returnType;
char *functionName;
- //! parameters
- int numParameters() { return symbolTable->size(); };
- // chillAST_TypedefTable *typedef_table; // TODO typedef here doesn't make sense
-
- //char *parametertypes; // a single string??
void printParameterTypes(FILE *fp);
void setName(char *n) { functionName = strdup(n); /* probable memory leak */ };
@@ -427,8 +410,6 @@ public:
chillAST_FunctionDecl(const char *rt, const char *fname, void *unique);
- void addParameter(chillAST_VarDecl *p);
-
void addDecl(chillAST_VarDecl *vd); // just adds to symbol table?? TODO
void addChild(chillAST_Node *node); // special because inserts into BODY
@@ -439,7 +420,6 @@ public:
chillAST_CompoundStmt *getBody() { return (body); }
void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
- void dump(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
void gatherVarDecls(std::vector<chillAST_VarDecl *> &decls);
@@ -464,8 +444,6 @@ public:
chillAST_Node *constantFold();
- chillAST_SymbolTable *getParameterTable() { return getSymbolTable(); }
-
void replaceChild(chillAST_Node *old, chillAST_Node *newchild) {
body->replaceChild(old, newchild);
}
@@ -534,9 +512,6 @@ public:
char *rhsString;
// parameters - these will be odd, in that they HAVE NO TYPE
- int numParameters() { return parameters.size(); };
- std::vector<chillAST_VarDecl *> parameters;
-
void setName(char *n) { macroName = strdup(n); /* probable memory leak */ };
void setRhsString(char *n) { rhsString = strdup(n); /* probable memory leak */ };
@@ -553,7 +528,6 @@ public:
chillAST_Node *getBody() { return (body); }
void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
- void dump(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
chillAST_Node *clone();
@@ -623,7 +597,6 @@ public:
// required methods that I can't seem to get to inherit
- 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 printControl(int indent = 0, FILE *fp = stderr); // print just for ( ... ) but not body
@@ -889,8 +862,6 @@ public:
void gatherStatements(std::vector<chillAST_Node *> &statements); //
- bool isSameAs(chillAST_Node *other);
-
};
class chillAST_ArraySubscriptExpr : public chillAST_Node {
@@ -998,7 +969,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();
@@ -1132,7 +1102,6 @@ public:
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
- bool isSameAs(chillAST_Node *other);
};
class chillAST_UnaryOperator : public chillAST_Node {
@@ -1186,8 +1155,6 @@ public:
int evalAsInt();
- bool isSameAs(chillAST_Node *other);
-
};
class chillAST_ImplicitCastExpr : public chillAST_Node {
@@ -1209,7 +1176,6 @@ public:
void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
void printonly(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
- void dump(int indent = 0, FILE *fp = stderr) { print(indent, fp); }; // print ast in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1343,7 +1309,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();
@@ -1426,7 +1391,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();
@@ -1475,7 +1439,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();
@@ -1548,7 +1511,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();
@@ -1710,7 +1672,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() { chillAST_Node* n = new chillAST_NoOp(); n->setParent(parent); return n; }; // ??
@@ -1774,8 +1735,6 @@ public:
};
// required methods that I can't seem to get to inherit
- void dump(int indent = 0, FILE *fp = stderr);
-
void print(int indent = 0, FILE *fp = stderr);
chillAST_Node *constantFold();
diff --git a/include/printer.h b/include/printer.h
new file mode 100644
index 0000000..5930768
--- /dev/null
+++ b/include/printer.h
@@ -0,0 +1,202 @@
+//
+// 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/printers/cfamily.h
new file mode 100644
index 0000000..e979db7
--- /dev/null
+++ b/include/printers/cfamily.h
@@ -0,0 +1,56 @@
+//
+// Created by ztuowen on 9/24/16.
+//
+
+#ifndef CHILL_CFAMILY_H
+#define CHILL_CFAMILY_H
+
+#include "printer.h"
+
+/*!
+ * \file
+ * \brief Print the AST for C like syntax, This replace the old print function
+ */
+
+namespace chill {
+ namespace printer{
+ class CFamily : public GenericPrinter {
+ public:
+ CFamily() {}
+ virtual int getPrec(chillAST_Node *n);
+ 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_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_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);
+ virtual void print(string ident, chillAST_Node *n, ostringStream &o);
+ };
+ }
+}
+
+#endif //CHILL_CFAMILY_H
diff --git a/include/printers/dump.h b/include/printers/dump.h
new file mode 100644
index 0000000..285de8e
--- /dev/null
+++ b/include/printers/dump.h
@@ -0,0 +1,62 @@
+//
+// 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/src/chillASTs.cc b/src/chillASTs.cc
index 851e148..c86a563 100644
--- a/src/chillASTs.cc
+++ b/src/chillASTs.cc
@@ -639,12 +639,6 @@ void chillAST_RecordDecl::printStructure(int indent, FILE *fp) {
fflush(fp);
}
-
-void chillAST_RecordDecl::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
-
-}
-
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)
@@ -652,7 +646,7 @@ chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname,
else
functionName = strdup("YouScrewedUp");
forwarddecl = externfunc = builtin = false;
- symbolTable = new chillAST_SymbolTable();
+ parameters = new chillAST_SymbolTable();
this->setFunctionCPU();
body = new chillAST_CompoundStmt();
returnType = strdup(rt);
@@ -660,31 +654,6 @@ chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname,
uniquePtr = unique; // a quick way to check equivalence. DO NOT ACCESS THROUGH THIS
};
-
-void chillAST_FunctionDecl::addParameter(chillAST_VarDecl *p) {
- CHILL_DEBUG_PRINT("%s chillAST_FunctionDecl::addParameter( 0x%x param %s) total of %d parameters\n", functionName,
- p, p->varname, 1 + getSymbolTable()->size());
-
- if (symbolTableFindName(getSymbolTable(), p->varname)) { // NOT recursive. just in FunctionDecl
- CHILL_DEBUG_PRINT("chillAST_FunctionDecl::addParameter( %s ), parameter already exists?\n", p->varname);
- // exit(-1); // ??
- return; // error?
- }
-
- getSymbolTable()->push_back(p);
- CHILL_DEBUG_PRINT("setting %s isAParameter\n", p->varname);
- p->isAParameter = true;
- p->setParent(this); // this is a combined list!
-}
-
-// TODO This couldn't be more wrong!
-void chillAST_FunctionDecl::addDecl(chillAST_VarDecl *vd) { // to symbol table ONLY
- CHILL_DEBUG_PRINT("chillAST_FunctionDecl::addDecl( %s )\n", vd->varname);
- if (!body)
- body = new chillAST_CompoundStmt();
- body->addVariableToScope(vd);
-}
-
void chillAST_FunctionDecl::setBody(chillAST_Node *bod) {
if (bod->isCompoundStmt()) body = (chillAST_CompoundStmt *) bod;
else {
@@ -695,7 +664,6 @@ void chillAST_FunctionDecl::setBody(chillAST_Node *bod) {
bod->setParent(this); // well, ...
}
-
void chillAST_FunctionDecl::insertChild(int i, chillAST_Node *node) {
fprintf(stderr, "chillAST_FunctionDecl::insertChild() ");
node->print(0, stderr);
@@ -730,21 +698,6 @@ void chillAST_FunctionDecl::addChild(chillAST_Node *node) {
node->setParent(this); // this, or body??
}
-
-void chillAST_FunctionDecl::printParameterTypes(FILE *fp) { // also prints names
- //fprintf(stderr, "\n\n%s chillAST_FunctionDecl::printParameterTypes()\n", functionName);
- fprintf(fp, "( ");
- int numparameters = getSymbolTable()->size();
- for (int i = 0; i < numparameters; i++) {
- if (i != 0) fprintf(fp, ", ");
- chillAST_VarDecl *p = (*getSymbolTable())[i];
- p->print(0, fp); // note: no indent, as this is in the function parens
- }
- fprintf(fp, " )"); // end of input parameters
-
-}
-
-
void chillAST_FunctionDecl::print(int indent, FILE *fp) {
//fprintf(fp, "\n// functiondecl %p \n", this);
//chillindent(indent, fp);
@@ -795,39 +748,11 @@ void chillAST_FunctionDecl::print(int indent, FILE *fp) {
fflush(fp);
}
-
-void chillAST_FunctionDecl::dump(int indent, FILE *fp) {
- fprintf(fp, "\n");
- fprintf(fp, "// isFromSourceFile ");
- if (filename) fprintf(fp, "%s ", filename);
- if (isFromSourceFile) fprintf(fp, "true\n");
- else fprintf(fp, "false\n");
- chillindent(indent, fp);
- fprintf(fp, "(FunctionDecl %s %s(", returnType, functionName);
-
- int numparameters = getSymbolTable()->size();
- for (int i = 0; i < numparameters; i++) {
- if (i != 0) fprintf(fp, ", ");
- chillAST_VarDecl *p = (*getSymbolTable())[i];
- p->print(0, fp);
- }
- fprintf(fp, ")\n"); // end of input parameters
-
- // now the body -
- if (body) body->dump(indent + 1, fp);
-
- // tidy up
- chillindent(indent, fp);
- fprintf(fp, ")\n");
- fflush(fp);
-}
-
-
void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) {
//fprintf(stderr, "chillAST_FunctionDecl::gatherVarDecls()\n");
//if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size());
//fprintf(stderr, "functiondecl has %d parameters\n", numParameters());
- for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherVarDecls(decls);
+ for (int i = 0; i < getParameters()->size(); i++) (*getParameters())[i]->gatherVarDecls(decls);
//fprintf(stderr, "after parms, %d decls\n", decls.size());
for (int i = 0; i < children.size(); i++) children[i]->gatherVarDecls(decls);
//fprintf(stderr, "after children, %d decls\n", decls.size());
@@ -842,7 +767,7 @@ void chillAST_FunctionDecl::gatherVarDecls(vector<chillAST_VarDecl *> &decls) {
void chillAST_FunctionDecl::gatherScalarVarDecls(vector<chillAST_VarDecl *> &decls) {
//if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size());
- for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherScalarVarDecls(decls);
+ for (int i = 0; i < getParameters()->size(); i++) (*getSymbolTable())[i]->gatherScalarVarDecls(decls);
for (int i = 0; i < children.size(); i++) children[i]->gatherScalarVarDecls(decls);
body->gatherScalarVarDecls(decls); // todo, figure out if functiondecl has actual children
}
@@ -851,7 +776,7 @@ void chillAST_FunctionDecl::gatherScalarVarDecls(vector<chillAST_VarDecl *> &dec
void chillAST_FunctionDecl::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decls) {
//if (0 < children.size()) fprintf(stderr, "functiondecl has %d children\n", children.size());
- for (int i = 0; i < numParameters(); i++) (*getSymbolTable())[i]->gatherArrayVarDecls(decls);
+ for (int i = 0; i < getParameters()->size(); i++) (*getSymbolTable())[i]->gatherArrayVarDecls(decls);
for (int i = 0; i < children.size(); i++) children[i]->gatherArrayVarDecls(decls);
body->gatherArrayVarDecls(decls); // todo, figure out if functiondecl has actual children
}
@@ -942,32 +867,22 @@ void chillAST_FunctionDecl::cleanUpVarDecls() {
par->removeChild(par->findChild(deletethese[i]));
}
-
- //fprintf(stderr, "\n\nnow check for vars used but not defined\n");
// now check for vars used but not defined?
for (int j = 0; j < used.size(); j++) {
- //fprintf(stderr, "%s is used\n", used[j]->varname);
bool definedandused = false;
for (int i = 0; i < defined.size(); i++) {
if (used[j] == defined[i]) {
- //fprintf(stderr, "%s is defined\n", defined[i]->varname);
definedandused = true;
break;
}
}
if (!definedandused) {
- //fprintf(stderr, "%s is used but not defined?\n", used[j]->varname);
- // add it to the beginning of the function
insertChild(0, used[j]);
}
}
}
-//void chillAST_FunctionDecl::replaceVarDecls( chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl ) {
-// for (int i=0; i<children.size(); i++) children[i]->replaceVarDecls( olddecl, newdecl );
-//}
-
bool chillAST_FunctionDecl::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync) {
if (body) body->findLoopIndexesToReplace(symtab, false);
@@ -984,7 +899,7 @@ chillAST_MacroDefinition::chillAST_MacroDefinition(const char *mname = NULL, con
if (mname) macroName = strdup(mname); else macroName = strdup("UNDEFINEDMACRO");
if(rhs) rhsString = strdup(rhs); else rhsString = NULL;
metacomment = NULL;
- symbolTable = new chillAST_SymbolTable();
+ parameters = new chillAST_SymbolTable();
isFromSourceFile = true; // default
filename = NULL;
};
@@ -996,7 +911,7 @@ chillAST_Node *chillAST_MacroDefinition::clone() {
return this;
chillAST_MacroDefinition *clo = new chillAST_MacroDefinition( macroName );
clo->setParent(parent);
- for (int i=0; i<parameters.size(); i++) clo->addVariableToScope( parameters[i] );
+ for (int i=0; i<parameters->size(); i++) clo->addVariableToScope( (*parameters)[i] );
clo->setBody( body->clone() );
return clo;
@@ -1022,35 +937,17 @@ void chillAST_MacroDefinition::addChild(chillAST_Node *node) {
node->setParent(this); // this, or body??
}
-
-void chillAST_MacroDefinition::dump(int indent, FILE *fp) {
- fprintf(fp, "\n");
- chillindent(indent, fp);
- fprintf(fp, "(MacroDefinition %s(", macroName);
- for (int i = 0; i < numParameters(); i++) {
- fprintf(fp, "\n");
- chillindent(indent + 1, fp);
- fprintf(fp, "(%s)", parameters[i]->varname);
- }
- fprintf(fp, ")\n");
- body->dump(indent + 1, fp);
- if (rhsString) fprintf(fp, " (aka %s)");
- fprintf(fp, "\n");
- fflush(fp);
-}
-
-
void chillAST_MacroDefinition::print(int indent, FILE *fp) { // UHOH TODO
- CHILL_DEBUG_PRINT("macro has %d parameters\n", numParameters());
+ CHILL_DEBUG_PRINT("macro has %d parameters\n", parameters->size());
printPreprocBEFORE(indent, fp);
fprintf(fp, "#define %s", macroName);
- if (0 != numParameters()) {
+ if (0 != parameters->size()) {
fprintf(fp, "(");
- for (int i = 0; i < numParameters(); i++) {
+ for (int i = 0; i < parameters->size(); i++) {
if (i) fprintf(fp, ",");
- fprintf(fp, "%s", parameters[i]->varname);
+ fprintf(fp, "%s", (*parameters)[i]->varname);
}
fprintf(fp, ") ");
}
@@ -1251,19 +1148,6 @@ void chillAST_ForStmt::print(int indent, FILE *fp) {
fflush(fp); //
}
-void chillAST_ForStmt::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(ForStmt \n");
-
- init->dump(indent + 1, fp);
- cond->dump(indent + 1, fp);
- incr->dump(indent + 1, fp);
- body->dump(indent + 1, fp);
-
- chillindent(indent, fp);
- fprintf(fp, ")\n");
-}
-
chillAST_Node *chillAST_ForStmt::constantFold() {
init = init->constantFold();
cond = cond->constantFold();
@@ -2052,14 +1936,6 @@ void chillAST_BinaryOperator::replaceVarDecls(chillAST_VarDecl *olddecl, chillAS
//}
}
-
-bool chillAST_BinaryOperator::isSameAs(chillAST_Node *other) {
- if (!other->isBinaryOperator()) return false;
- chillAST_BinaryOperator *o = (chillAST_BinaryOperator *) other;
- if (strcmp(op, o->op)) return false; // different operators
- return lhs->isSameAs(o->lhs) && rhs->isSameAs(o->rhs); // recurse
-}
-
chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_Node *c, chillAST_Node *l,
chillAST_Node *r) {
if (op)
@@ -2589,23 +2465,6 @@ chillAST_MemberExpr::chillAST_MemberExpr(chillAST_Node *bas, const char *mem, vo
}
}
-
-void chillAST_MemberExpr::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(MemberExpr \n");
-
- base->dump(indent + 1, fp);
- chillindent(indent + 1, fp);
- if (exptype == CHILLAST_MEMBER_EXP_ARROW) fprintf(fp, "->");
- else fprintf(fp, ".");
-
- fprintf(fp, "%s\n", member);
-
- chillindent(indent, fp);
- fprintf(fp, ")\n");
-}
-
-
void chillAST_MemberExpr::print(int indent, FILE *fp) {
if (base) base->print(indent, fp);
else {
@@ -2833,26 +2692,6 @@ char *chillAST_DeclRefExpr::stringRep(int indent) {
return strdup(declarationName);
}
-
-void chillAST_DeclRefExpr::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(DeclRefExpr '%s' ", declarationType);
- chillAST_VarDecl *vd = getVarDecl();
- if (vd) {
- if (vd->isAParameter) fprintf(fp, "ParmVar ");
- else fprintf(fp, "Var ");
- }
- fprintf(fp, "'%s' ", declarationName); // variable or function name
-
- if (chillAST_FunctionDecl *fd = getFunctionDecl()) {
- // print parameter types for functions
- fd->printParameterTypes(fp);
- }
-
- fprintf(fp, ")\n");
- fflush(fp);
-}
-
class chillAST_Node *chillAST_DeclRefExpr::constantFold() { // can never do anything?
return this;
}
@@ -3276,18 +3115,6 @@ chillAST_Node *chillAST_FloatingLiteral::clone() {
return newone;
}
-bool chillAST_FloatingLiteral::isSameAs(chillAST_Node *other) {
- if (!other->isFloatingLiteral()) return false;
- chillAST_FloatingLiteral *o = (chillAST_FloatingLiteral *) other;
- // should we care about single vs double precision?
- if (float0double1 != o->float0double1) return false;
- if (float0double1 == 0) {
- return value == o->value; // WARNING, comparing floats with ==
- }
- return doublevalue == o->doublevalue; // WARNING, comparing doubless with ==
-}
-
-
chillAST_UnaryOperator::chillAST_UnaryOperator(const char *oper, bool pre, chillAST_Node *sub) {
op = strdup(oper);
prefix = pre;
@@ -3421,14 +3248,6 @@ int chillAST_UnaryOperator::evalAsInt() {
}
-bool chillAST_UnaryOperator::isSameAs(chillAST_Node *other) {
- if (!other->isUnaryOperator()) return false;
- chillAST_UnaryOperator *o = (chillAST_UnaryOperator *) other;
- if (strcmp(op, o->op)) return false; // different operators
- return subexpr->isSameAs(o->subexpr); // recurse
-}
-
-
chillAST_ImplicitCastExpr::chillAST_ImplicitCastExpr(chillAST_Node *sub) {
subexpr = sub;
subexpr->setParent(this);
@@ -3744,17 +3563,6 @@ void chillAST_Malloc::print(int indent, FILE *fp) {
fflush(fp);
};
-
-void chillAST_Malloc::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(Malloc \n");
- sizeexpr->dump(indent + 1, fp);
- chillindent(indent, fp);
- fprintf(fp, ")\n");
- fflush(fp);
-};
-
-
chillAST_CudaMalloc::chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node *size) {
devPtr = devmemptr;
sizeinbytes = size; // probably a multiply like sizeof(int) * 1024
@@ -3770,16 +3578,6 @@ void chillAST_CudaMalloc::print(int indent, FILE *fp) {
fflush(fp);
};
-void chillAST_CudaMalloc::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(CudaMalloc \n");
- devPtr->dump(indent + 1, fp);
- fprintf(fp, "\n");
- sizeinbytes->dump(indent + 1, fp);
- fprintf(fp, ")\n");
- fflush(fp);
-};
-
class chillAST_Node *chillAST_CudaMalloc::constantFold() {
devPtr = devPtr->constantFold();
return this;
@@ -3899,16 +3697,7 @@ void chillAST_CudaMemcpy::print(int indent, FILE *fp) {
fflush(fp);
};
-void chillAST_CudaMemcpy::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(CudaMemcpy \n");
- dest->dump(indent + 1, fp);
- src->dump(indent + 1, fp);
- size->dump(indent + 1, fp);
- chillindent(indent + 1, fp);
- fprintf(fp, ",%s\n", cudaMemcpyKind);
- fflush(fp);
-};
+
class chillAST_Node *chillAST_CudaMemcpy::constantFold() {
dest = (chillAST_VarDecl *) dest->constantFold();
@@ -4002,19 +3791,6 @@ void chillAST_ReturnStmt::print(int indent, FILE *fp) {
fflush(fp);
}
-
-void chillAST_ReturnStmt::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(ReturnStmt");
- if (returnvalue) {
- fprintf(fp, "\n");
- returnvalue->dump(indent + 1, fp);
- chillindent(indent, fp);
- }
- fprintf(fp, ")\n");
-}
-
-
class chillAST_Node *chillAST_ReturnStmt::constantFold() {
if (returnvalue) returnvalue = returnvalue->constantFold();
return this;
@@ -5238,27 +5014,6 @@ chillAST_Node *chillAST_IfStmt::clone() {
return IS;
}
-
-void chillAST_IfStmt::dump(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(if ");
- fprintf(fp, "\n");
-
- cond->dump(indent + 1, fp);
- fprintf(fp, "\n");
-
- thenpart->dump(indent + 1, fp);
- fprintf(fp, "\n");
-
- if (elsepart) {
- elsepart->dump(indent + 1, fp);
- fprintf(fp, "\n");
- }
- chillindent(indent, fp);
- fprintf(fp, ")\n");
-}
-
-
void chillAST_IfStmt::print(int indent, FILE *fp) {
printPreprocBEFORE(indent, fp);
chillindent(indent, fp);
@@ -5570,9 +5325,32 @@ chillAST_VarDecl* chillAST_Node::findVariableDecleration(const char *t) {
}
chillAST_VarDecl* chillAST_Node::getVariableDeclaration(const char *t) {
- return symbolTableFindName(getSymbolTable(),t);
+ chillAST_VarDecl* vd = symbolTableFindName(getSymbolTable(),t);
+ if (!vd) vd = getParameter(t);
+ return vd;
}
chillAST_TypedefDecl* chillAST_Node::getTypeDeclaration(const char *t){
return typedefTableFindName(getTypedefTable(),t);
}
+
+void chillAST_Node::addParameter(chillAST_VarDecl *vd) {
+ if (!parameters) {
+ CHILL_ERROR("Calling addParameter on construct without parameters");
+ exit(-1);
+ }
+
+ if (symbolTableFindName(getParameters(), vd->varname)) { // NOT recursive. just in FunctionDecl
+ CHILL_ERROR("parameter %s already exists?\n", vd->varname);
+ return;
+ }
+
+ CHILL_DEBUG_PRINT("setting %s isAParameter\n", vd->varname);
+ getParameters()->push_back(vd);
+ vd->isAParameter = true;
+ vd->setParent(this); // this is a combined list!
+}
+
+chillAST_VarDecl* chillAST_Node::getParameter(const char *t) {
+ return symbolTableFindName(getParameters(),t);
+} \ No newline at end of file
diff --git a/src/printers/cfamily.cpp b/src/printers/cfamily.cpp
new file mode 100644
index 0000000..305feee
--- /dev/null
+++ b/src/printers/cfamily.cpp
@@ -0,0 +1,8 @@
+//
+// Created by ztuowen on 9/24/16.
+//
+
+#include "printers/cfamily.h"
+
+using namespace std;
+using namespace chill::printer;
diff --git a/src/printers/dump.cpp b/src/printers/dump.cpp
new file mode 100644
index 0000000..334552f
--- /dev/null
+++ b/src/printers/dump.cpp
@@ -0,0 +1,160 @@
+//
+// 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);
+