summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2016-09-25 14:30:34 -0600
committerTuowen Zhao <ztuowen@gmail.com>2016-09-25 14:30:34 -0600
commite7de858ce27ded585d6e2ec4a5cbc3467c6a7593 (patch)
tree6f20c14ae118bc83d3c69f12875ba7e369bb2716
parent2a5618583e1fdc8cde0308d3e5b1873bc94c5fb1 (diff)
downloadchill-e7de858ce27ded585d6e2ec4a5cbc3467c6a7593.tar.gz
chill-e7de858ce27ded585d6e2ec4a5cbc3467c6a7593.tar.bz2
chill-e7de858ce27ded585d6e2ec4a5cbc3467c6a7593.zip
CFamily AST printer
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/chillAST/chillAST_node.hh61
-rw-r--r--include/chillAST/chillASTs.hh143
-rwxr-xr-xinclude/ir_clang.hh3
-rw-r--r--include/printer/cfamily.h57
-rw-r--r--include/printer/dump.h35
-rw-r--r--include/printer/generic.h238
-rwxr-xr-xlib/chillcg/include/code_gen/CG_chillRepr.h4
-rw-r--r--src/chillASTs.cc474
-rwxr-xr-xsrc/ir_clang.cc8
-rw-r--r--src/printer/cfamily.cpp353
-rw-r--r--src/printer/dump.cpp8
-rw-r--r--src/printer/generic.cpp3
13 files changed, 621 insertions, 767 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ecd1846..284599f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,6 +36,7 @@ set(PYTHON_SRC
set(PRINTER_SRC
src/printer/generic.cpp
src/printer/dump.cpp
+ src/printer/cfamily.cpp
)
llvm_map_components_to_libnames(llvm_libs all)
diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh
index bed2f22..511386d 100644
--- a/include/chillAST/chillAST_node.hh
+++ b/include/chillAST/chillAST_node.hh
@@ -43,12 +43,12 @@ public:
typedefTable = NULL;
parameters = NULL;
}
+
//! the type of this current node
- virtual CHILLAST_NODE_TYPE getType() {return CHILLAST_NODE_UNKNOWN;};
+ 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;}
bool isSourceFile() { return (getType() == CHILLAST_NODE_SOURCEFILE); };
@@ -145,14 +145,21 @@ public:
virtual bool isAUnion() { return false; };
- virtual chillAST_SymbolTable* getSymbolTable() { return symbolTable; }
- virtual chillAST_TypedefTable* getTypedefTable() {return typedefTable; }
+ virtual chillAST_SymbolTable *getSymbolTable() { return symbolTable; }
+
+ virtual chillAST_TypedefTable *getTypedefTable() { return typedefTable; }
+
virtual void addVariableToScope(chillAST_VarDecl *vd);
+
virtual void addTypedefToScope(chillAST_TypedefDecl *tdd);
- chillAST_TypedefDecl* findTypeDecleration(const char *t);
- chillAST_VarDecl* findVariableDecleration(const char *t);
- chillAST_VarDecl* getVariableDeclaration(const char *vn);
- chillAST_TypedefDecl* getTypeDeclaration(const char *tn);
+
+ chillAST_TypedefDecl *findTypeDecleration(const char *t);
+
+ chillAST_VarDecl *findVariableDecleration(const char *t);
+
+ chillAST_VarDecl *getVariableDeclaration(const char *vn);
+
+ chillAST_TypedefDecl *getTypeDeclaration(const char *tn);
virtual chillAST_VarDecl *findVariableNamed(const char *name); // recursive
@@ -163,7 +170,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]; };
@@ -174,7 +181,7 @@ public:
void setMetaComment(const char *c) { metacomment = strdup(c); };
- virtual void chillMergeChildInfo(chillAST_Node){
+ virtual void chillMergeChildInfo(chillAST_Node) {
// TODO if (par) par->add to definition for vardecl/typedecl
// TODO if (par) par->getSourceFile()->addFunc(this); for FuncDecl
// TODO if (par) par->getSourceFile()->addMacro(this); For MacroDecl
@@ -193,8 +200,8 @@ public:
children.push_back(c);
}; // not usually useful
- virtual void addChildren(const chillAST_NodeList &c){
- for (int i =0;i<c.size();++i){
+ virtual void addChildren(const chillAST_NodeList &c) {
+ for (int i = 0; i < c.size(); ++i) {
addChild(c[i]);
}
}
@@ -227,7 +234,7 @@ public:
}
}
CHILL_ERROR("%s %p generic replaceChild called with oldchild that was not a child\n",
- getTypeString(), this);
+ getTypeString(), this);
CHILL_DEBUG_BEGIN
fprintf(stderr, "printing\n");
print();
@@ -401,15 +408,6 @@ public:
exit(-1);;
};
- // 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) {
- fflush(fp);
- fprintf(fp, "\n");
- chillindent(indent, fp);
- fprintf(fp, "(%s) forgot to implement print()\n", getTypeString());
- };
-
virtual void printName(int indent = 0, FILE *fp = stderr) {
fflush(fp);
fprintf(fp, "\n");
@@ -485,7 +483,6 @@ public:
}
-
void setParent(chillAST_Node *p) { parent = p; };
chillAST_Node *getParent() { return parent; };
@@ -494,7 +491,8 @@ public:
chillAST_SourceFile *getSourceFile() {
if (isSourceFile()) return ((chillAST_SourceFile *) this);
if (parent != NULL) return parent->getSourceFile();
- CHILL_ERROR("UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n", this, this->getTypeString());
+ CHILL_ERROR("UHOH, getSourceFile() called on node %p %s that does not have a parent and is not a source file\n",
+ this, this->getTypeString());
this->print();
exit(-1);
}
@@ -524,12 +522,17 @@ public:
fprintf(stderr, "\n\n");
}
- virtual chillAST_SymbolTable* getParameters() {return parameters;}
- virtual chillAST_VarDecl* getParameter(const char * name);
- virtual void addParameter(chillAST_VarDecl* name);
+ virtual chillAST_SymbolTable *getParameters() { return parameters; }
+
+ virtual chillAST_VarDecl *getParameter(const char *name);
+
+ virtual void addParameter(chillAST_VarDecl *name);
//! Emulation of the old dump function but using printer instead of hardcoded heuritics
- void dump(int indent=0,FILE *fp = stderr);
+ void dump(int indent = 0, FILE *fp = stderr);
+
+ //! Emulation of the old print function to print C-family like syntax but using printer
+ void print(int ident = 0, FILE *fp = stderr);
};
diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh
index 36442ee..8e5cee0 100644
--- a/include/chillAST/chillASTs.hh
+++ b/include/chillAST/chillASTs.hh
@@ -8,14 +8,16 @@
class chillAST_NULL : public chillAST_Node { // NOOP?
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_NULL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_NULL; }
+
chillAST_NULL() {
};
};
class chillAST_Preprocessing : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_PREPROCESSING;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_PREPROCESSING; }
+
// variables that are special for this type of node
CHILLAST_PREPROCESSING_POSITION position;
CHILLAST_PREPROCESSING_TYPE pptype;
@@ -28,8 +30,6 @@ public:
// other methods particular to this type of node
// 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
};
//typedef is a keyword in the C and C++ programming languages. The purpose of typedef is to assign alternative names to existing types, most often those whose standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.
@@ -40,7 +40,8 @@ private:
char *structname; // get rid of this?
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_TYPEDEFDECL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_TYPEDEFDECL; }
+
char *newtype; // the new type name ??
char *underlyingtype; // float, int, "struct bubba" ?
char *arraypart; // string like "[1234][56]" ??
@@ -87,7 +88,8 @@ public:
class chillAST_VarDecl : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_VARDECL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_VARDECL; }
+
char *vartype; // should probably be an enum, except it's used for unnamed structs too
chillAST_RecordDecl *vardef;// the thing that says what the struct looks like
@@ -190,7 +192,8 @@ public:
class chillAST_DeclRefExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_DECLREFEXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_DECLREFEXPR; }
+
// variables that are special for this type of node
char *declarationType;
char *declarationName;
@@ -224,7 +227,6 @@ public:
};
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr); // print CODE
char *stringRep(int indent = 0);
chillAST_Node *constantFold();
@@ -269,7 +271,8 @@ public:
class chillAST_CompoundStmt : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_COMPOUNDSTMT;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_COMPOUNDSTMT; }
+
// variables that are special for this type of node
// constructors
chillAST_CompoundStmt(); // never has any args ???
@@ -280,8 +283,6 @@ public:
// required methods
void replaceChild(chillAST_Node *old, chillAST_Node *newchild);
- void print(int indent = 0, FILE *fp = stderr);
-
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -321,9 +322,14 @@ private:
std::vector<chillAST_VarDecl *> subparts;
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_RECORDDECL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_RECORDDECL; }
+
chillAST_RecordDecl(const char *nam, const char *orig);
+ chillAST_SymbolTable *getSubparts() {
+ return &subparts;
+ }
+
void setName(const char *newname) { name = strdup(newname); };
char *getName() { return name; };
@@ -351,8 +357,6 @@ public:
chillAST_VarDecl *findSubpartByType(const char *typ);
- void print(int indent = 0, FILE *fp = stderr);
-
void printStructure(int indent = 0, FILE *fp = stderr);
};
@@ -365,12 +369,15 @@ private:
bool forwarddecl;
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FUNCTIONDECL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FUNCTIONDECL; }
+
char *returnType;
char *functionName;
void printParameterTypes(FILE *fp);
+ CHILLAST_FUNCTION_TYPE getFunctionType() { return function_type; }
+
void setName(char *n) { functionName = strdup(n); /* probable memory leak */ };
void setBuiltin() { builtin = true; }; // designate function as a builtin
@@ -403,8 +410,6 @@ public:
chillAST_CompoundStmt *getBody() { return (body); }
- void print(int indent = 0, FILE *fp = stderr); // in chill_ast.cc
-
void gatherVarDecls(std::vector<chillAST_VarDecl *> &decls);
void gatherVarDeclsMore(std::vector<chillAST_VarDecl *> &decls) { gatherVarDecls(decls); };
@@ -436,7 +441,7 @@ public:
class chillAST_SourceFile : public chillAST_Node {
// TODO included source file
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_SOURCEFILE;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_SOURCEFILE; }
// constructors
chillAST_SourceFile(const char *filename); // defined in chill_ast.cc
@@ -490,7 +495,8 @@ class chillAST_MacroDefinition : public chillAST_Node {
private:
chillAST_Node *body; // rhs always a compound statement?
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MACRODEFINITION;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_MACRODEFINITION; }
+
char *macroName;
char *rhsString;
@@ -536,7 +542,8 @@ public:
class chillAST_ForStmt : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FORSTMT;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FORSTMT; }
+
// variables that are special for this type of node
chillAST_Node *init;
chillAST_Node *cond;
@@ -651,8 +658,8 @@ public:
class chillAST_TernaryOperator : public chillAST_Node {
public:
- virtual int getPrec() {return INT8_MAX+15;}
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_TERNARYOPERATOR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_TERNARYOPERATOR; }
+
// variables that are special for this type of node
char *op; // TODO need enum so far, only "?" conditional operator
chillAST_Node *condition;
@@ -695,8 +702,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
-
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -730,8 +735,8 @@ public:
class chillAST_BinaryOperator : public chillAST_Node {
public:
- virtual int getPrec();
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_BINARYOPERATOR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_BINARYOPERATOR; }
+
// variables that are special for this type of node
char *op; // TODO need enum
chillAST_Node *lhs;
@@ -803,8 +808,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
-
char *stringRep(int indent = 0);
chillAST_Node *constantFold();
@@ -842,7 +845,7 @@ public:
class chillAST_ArraySubscriptExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_ARRAYSUBSCRIPTEXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_ARRAYSUBSCRIPTEXPR; }
// variables that are special for this type of node
//! always a decl ref expr? No, for multidimensional array, is another ASE
@@ -866,6 +869,7 @@ public:
//! Method for finding the basedecl, retursn the VARDECL of the thing the subscript is an index into
chillAST_VarDecl *multibase();
+
chillAST_Node *multibase2() { return base->multibase2(); }
chillAST_Node *getIndex(int dim);
@@ -875,7 +879,6 @@ public:
void replaceChild(chillAST_Node *old, chillAST_Node *newchild); // will examine index
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr) const; // print CODE in chill_ast.cc
char *stringRep(int indent = 0);
chillAST_Node *constantFold();
@@ -915,7 +918,8 @@ public:
class chillAST_MemberExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MEMBEREXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_MEMBEREXPR; }
+
// variables that are special for this type of node
chillAST_Node *base; // always a decl ref expr? No, can be Array Subscript Expr
char *member;
@@ -978,7 +982,8 @@ public:
class chillAST_IntegerLiteral : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_INTEGERLITERAL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_INTEGERLITERAL; }
+
// variables that are special for this type of node
int value;
@@ -1014,7 +1019,8 @@ public:
class chillAST_FloatingLiteral : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FLOATINGLITERAL;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FLOATINGLITERAL; }
+
// variables that are special for this type of node
double value;
@@ -1033,7 +1039,6 @@ public:
int getPrecision() { return precision; }
// 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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1059,8 +1064,8 @@ public:
class chillAST_UnaryOperator : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_UNARYOPERATOR;}
- virtual int getPrec();
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_UNARYOPERATOR; }
+
// variables that are special for this type of node
//! String representing the operator
char *op;
@@ -1078,7 +1083,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1111,7 +1115,8 @@ public:
class chillAST_ImplicitCastExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_IMPLICITCASTEXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_IMPLICITCASTEXPR; }
+
// variables that are special for this type of node
chillAST_Node *subexpr;
@@ -1155,8 +1160,8 @@ public:
class chillAST_CStyleCastExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CSTYLECASTEXPR;}
- virtual int getPrec() {return INT8_MAX+3;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CSTYLECASTEXPR; }
+
// variables that are special for this type of node
//! String representing the type it casts to
char *towhat;
@@ -1171,7 +1176,6 @@ public:
// required methods that I can't seem to get to inherit
void replaceChild(chillAST_Node *old, chillAST_Node *newchild);
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1202,8 +1206,8 @@ public:
class chillAST_CStyleAddressOf : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CSTYLEADDRESSOF;}
- virtual int getPrec() {return INT8_MAX+3;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CSTYLEADDRESSOF; }
+
// variables that are special for this type of node
chillAST_Node *subexpr;
@@ -1214,7 +1218,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1244,7 +1247,8 @@ public:
class chillAST_CudaMalloc : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAMALLOC;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDAMALLOC; }
+
// variables that are special for this type of node
chillAST_Node *devPtr; // Pointer to allocated device memory
chillAST_Node *sizeinbytes;
@@ -1256,7 +1260,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1284,7 +1287,8 @@ public:
class chillAST_CudaFree : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAFREE;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDAFREE; }
+
// variables that are special for this type of node
chillAST_VarDecl *variable;
@@ -1295,7 +1299,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1323,7 +1326,8 @@ public:
class chillAST_Malloc : public chillAST_Node { // malloc( sizeof(int) * 2048 );
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_MALLOC;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_MALLOC; }
+
// variables that are special for this type of node
//! The subexpression calculating bytes
chillAST_Node *sizeexpr; // bytes
@@ -1361,13 +1365,14 @@ public:
class chillAST_Free : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_FREE;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_FREE; }
};
class chillAST_CudaMemcpy : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDAMEMCPY;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDAMEMCPY; }
+
// variables that are special for this type of node
chillAST_VarDecl *dest; // Pointer to allocated device memory
chillAST_VarDecl *src;
@@ -1408,7 +1413,7 @@ public:
class chillAST_CudaSyncthreads : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CUDASYNCTHREADS;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CUDASYNCTHREADS; }
// variables that are special for this type of node
// constructors
@@ -1418,7 +1423,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
//chillAST_Node* constantFold() {};
//chillAST_Node* clone();
//void gatherArrayRefs( std::vector<chillAST_ArraySubscriptExpr*> &refs, bool writtento ){};
@@ -1439,7 +1443,8 @@ public:
class chillAST_ReturnStmt : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_RETURNSTMT;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_RETURNSTMT; }
+
// variables that are special for this type of node
//! expression to return
chillAST_Node *returnvalue;
@@ -1451,7 +1456,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1475,8 +1479,10 @@ public:
};
class chillAST_CallExpr : public chillAST_Node { // a function call
+ // Is macro call without pointer a part of this
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_CALLEXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_CALLEXPR; }
+
// variables that are special for this type of node
chillAST_Node *callee; // the function declaration (what about builtins?)
int numargs;
@@ -1495,7 +1501,6 @@ public:
// required methods that I can't seem to get to inherit
chillAST_Node *constantFold();
- void print(int indent = 0, FILE *fp = stderr); // print CODE in chill_ast.cc
void gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento);
void gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento);
@@ -1521,7 +1526,8 @@ public:
class chillAST_ParenExpr : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_PARENEXPR;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_PARENEXPR; }
+
// variables that are special for this type of node
chillAST_Node *subexpr;
@@ -1532,7 +1538,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
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1562,8 +1567,8 @@ public:
class chillAST_Sizeof : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_SIZEOF;}
- virtual int getPrec() {return INT8_MAX+3;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_SIZEOF; }
+
// variables that are special for this type of node
//! the object of sizeof function
char *thing;
@@ -1604,13 +1609,18 @@ public:
class chillAST_NoOp : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_NOOP;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_NOOP; }
+
chillAST_NoOp(); // { parent = p; };
// required methods that I can't seem to get to inherit
chillAST_Node *constantFold() {};
- chillAST_Node *clone() { chillAST_Node* n = new chillAST_NoOp(); n->setParent(parent); return n; }; // ??
+ chillAST_Node *clone() {
+ chillAST_Node *n = new chillAST_NoOp();
+ n->setParent(parent);
+ return n;
+ }; // ??
void gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {};
@@ -1636,7 +1646,8 @@ public:
class chillAST_IfStmt : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_IFSTMT;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_IFSTMT; }
+
// variables that are special for this type of node
chillAST_Node *cond;
chillAST_Node *thenpart;
@@ -1671,8 +1682,6 @@ public:
};
// required methods that I can't seem to get to inherit
- void print(int indent = 0, FILE *fp = stderr);
-
chillAST_Node *constantFold();
chillAST_Node *clone();
@@ -1702,7 +1711,7 @@ public:
class chillAST_something : public chillAST_Node {
public:
- virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_UNKNOWN;}
+ virtual CHILLAST_NODE_TYPE getType() { return CHILLAST_NODE_UNKNOWN; }
// variables that are special for this type of node
// constructors
diff --git a/include/ir_clang.hh b/include/ir_clang.hh
index 1237d25..81a6fde 100755
--- a/include/ir_clang.hh
+++ b/include/ir_clang.hh
@@ -256,7 +256,8 @@ struct IR_chillArrayRef : public IR_ArrayRef {
IR_Ref *clone() const;
- virtual void Dump() const;
+ // Not implemented
+ // virtual void Dump() const;
};
diff --git a/include/printer/cfamily.h b/include/printer/cfamily.h
index f6e2284..83e7765 100644
--- a/include/printer/cfamily.h
+++ b/include/printer/cfamily.h
@@ -12,49 +12,102 @@
*/
namespace chill {
- namespace printer{
+ namespace printer {
/*!
* \brief Print the AST for C like syntax, This replace the old print function
* Custom multiplexer should not be needed. This version should calculate the correct precedence for expressions.
* Expression should be encapsulated in {} or () or ended with ; with heuristics at the parent node
+ *
+ * All precedence calculation taken from http://en.cppreference.com/w/cpp/language/operator_precedence
*/
class CFamily : public GenericPrinter {
public:
CFamily() {}
- virtual int getPrec(chillAST_Node *n);
+
+ virtual int getPrecS(chillAST_BinaryOperator *n);
+
+ virtual int getPrecS(chillAST_CallExpr *n);
+
+ virtual int getPrecS(chillAST_CStyleAddressOf *n);
+
+ virtual int getPrecS(chillAST_CStyleCastExpr *n);
+
+ virtual int getPrecS(chillAST_TernaryOperator *n);
+
+ virtual int getPrecS(chillAST_UnaryOperator *n);
+
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);
+
+ //! Compound statement is responsible to break a new line if necessary
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);
+ /*!
+ * Prints the floatpoint literal, only the showpoint flag is currently set
+ * @param ident
+ * @param n
+ * @param 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);
};
}
diff --git a/include/printer/dump.h b/include/printer/dump.h
index dd8a0e8..fd382a3 100644
--- a/include/printer/dump.h
+++ b/include/printer/dump.h
@@ -17,40 +17,75 @@ namespace chill {
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
diff --git a/include/printer/generic.h b/include/printer/generic.h
index 70ebb41..6918136 100644
--- a/include/printer/generic.h
+++ b/include/printer/generic.h
@@ -16,7 +16,7 @@
namespace chill {
namespace printer {
class GenericPrinter {
- private:
+ protected:
std::string identSpace;
public:
GenericPrinter() { identSpace = " "; }
@@ -31,80 +31,165 @@ namespace chill {
for (int i = 0; i < numspaces; ++i)
identSpace += " ";
}
- 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; }
+
+ /*!
+ * Default return value for get prec, can be used as a reference
+ * @return default precedence
+ */
+ virtual int defGetPrecS() { return INT8_MAX; }
+
+ virtual int getPrecS(chillAST_ArraySubscriptExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_BinaryOperator *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CallExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CompoundStmt *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CStyleAddressOf *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CStyleCastExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CudaFree *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CudaKernelCall *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CudaMalloc *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CudaMemcpy *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_CudaSyncthreads *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_DeclRefExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_FloatingLiteral *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_ForStmt *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_Free *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_FunctionDecl *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_IfStmt *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_IntegerLiteral *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_ImplicitCastExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_MacroDefinition *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_Malloc *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_MemberExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_NULL *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_NoOp *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_ParenExpr *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_Preprocessing *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_RecordDecl *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_ReturnStmt *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_Sizeof *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_SourceFile *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_TypedefDecl *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_TernaryOperator *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_UnaryOperator *n) { return defGetPrecS(); }
+
+ virtual int getPrecS(chillAST_VarDecl *n) { return defGetPrecS(); }
//! 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;
+ //! default error output when encountering not recognized error code
+ /*!
+ * @param ident
+ * @param n
+ * @param o
+ */
+ virtual void errorPrintS(std::string ident, chillAST_Node *n, std::ostream &o);
+
+ virtual void printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) {
+ errorPrintS(ident, n, o);
+ }
+
+ virtual void printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+// virtual void printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_Free *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o) {
+ errorPrintS(ident, n, o);
+ }
+
+ virtual void printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_Malloc *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_NULL *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_NoOp *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) { errorPrintS(ident, n, o); }
+
+ virtual void printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) { errorPrintS(ident, n, o); }
//! Print the AST to string stream, multiplexer
/*!
* @param ident indentation of the node
@@ -129,7 +214,7 @@ namespace chill {
* @param n the chillAST_Node
*/
virtual void printOut(std::string ident, chillAST_Node *n) {
- print(ident,n,std::cout);
+ print(ident, n, std::cout);
}
//! Print the AST to stdErr
/*!
@@ -137,15 +222,16 @@ namespace chill {
* @param n the chillAST_Node
*/
virtual void printErr(std::string ident, chillAST_Node *n) {
- print(ident,n,std::cerr);
+ print(ident, n, std::cerr);
}
+
//! Print the subexpression with precedence
- virtual void printPrec(std::string ident,chillAST_Node *n,std::ostream &o, int prec) {
- if (getPrec(n) > prec) o<<"(";
- print(ident,n,prec);
- if (getPrec(n) > prec) o<<")";
+ virtual void printPrec(std::string ident, chillAST_Node *n, std::ostream &o, int prec) {
+ if (getPrec(n) > prec) o << "(";
+ print(ident, n, o);
+ if (getPrec(n) > prec) o << ")";
}
- };
+ };
}
}
diff --git a/lib/chillcg/include/code_gen/CG_chillRepr.h b/lib/chillcg/include/code_gen/CG_chillRepr.h
index 2d6d688..34e9876 100755
--- a/lib/chillcg/include/code_gen/CG_chillRepr.h
+++ b/lib/chillcg/include/code_gen/CG_chillRepr.h
@@ -28,10 +28,8 @@ namespace omega {
//
std::vector<chillAST_Node *> chillnodes; // TODO make private
void printChillNodes() const {
- for (int i = 0; i < chillnodes.size(); i++) {
+ for (int i = 0; i < chillnodes.size(); i++)
chillnodes[i]->print();
- printf("\n");
- }
fflush(stdout);
};
diff --git a/src/chillASTs.cc b/src/chillASTs.cc
index 69fe3ac..bee56df 100644
--- a/src/chillASTs.cc
+++ b/src/chillASTs.cc
@@ -5,6 +5,7 @@
#include <stack>
#include "chillAST.h"
#include "printer/dump.h"
+#include "printer/cfamily.h"
using namespace std;
@@ -435,8 +436,6 @@ chillAST_VarDecl *chillAST_RecordDecl::findSubpart(const char *nam) {
}
fprintf(stderr, "chillAST_RecordDecl::findSubpart() couldn't find member NAMED %s in ", nam);
print();
- printf("\n\n");
- fflush(stdout);
return NULL;
}
@@ -455,33 +454,6 @@ chillAST_VarDecl *chillAST_RecordDecl::findSubpartByType(const char *typ) {
return NULL;
}
-
-void chillAST_RecordDecl::print(int indent, FILE *fp) {
- CHILL_DEBUG_PRINT("chillAST_RecordDecl::print()\n");
- if (isUnnamed) return;
-
- chillindent(indent, fp);
- if (isStruct) {
- fprintf(fp, "struct ");
- if (strncmp("unnamed", name, 7)) fprintf(fp, "%s\n", name);
-
- chillindent(indent, fp);
- fprintf(fp, "{\n");
- for (int i = 0; i < subparts.size(); i++) {
- subparts[i]->print(indent + 1, fp);
- fprintf(fp, ";\n");
- }
- fprintf(fp, "} ");
- fprintf(fp,
- "\n"); // ?? need semicolon when defining struct. can't have it when part of a typedef.
- } else {
- CHILL_ERROR("/* UNKNOWN RECORDDECL print() */ ");
- exit(-1);
- }
- fflush(fp);
-}
-
-
chillAST_SymbolTable *chillAST_RecordDecl::addVariableToSymbolTable(chillAST_VarDecl *vd) {
// for now, just bail. or do we want the struct to have an actual symbol table?
//fprintf(stderr, "chillAST_RecordDecl::addVariableToSymbolTable() ignoring struct member %s vardecl\n", vd->varname);
@@ -505,20 +477,6 @@ 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)
@@ -576,52 +534,6 @@ void chillAST_FunctionDecl::addChild(chillAST_Node *node) {
node->setParent(this); // this, or body??
}
-void chillAST_FunctionDecl::print(int indent, FILE *fp) {
- //fprintf(fp, "\n// functiondecl %p \n", this);
- //chillindent(indent, fp);
- //fprintf(fp, "//(functiondecl) %d parameters\n", numparameters);
-
- fprintf(fp, "\n");
- chillindent(indent, fp);
-
- if (externfunc) fprintf(fp, "extern ");
-
- if (function_type == CHILLAST_FUNCTION_GPU) fprintf(fp, "__global__ ");
- fprintf(fp, "%s %s", returnType, functionName);
- printParameterTypes(fp);
-
-
-
- // non-parameter variables (now must have explicit vardecl in the body)
- //int numvars = symbol_table.size();
- //for (int i=0; i<numvars; i++) {
- // symbol_table[i]->print(1,fp);
- // fprintf(fp, ";\n");
- //}
-
- // now the body
- if (!(externfunc || forwarddecl)) {
- if (body) {
- fprintf(fp, "\n{\n");
- //chillindent(indent+1, fp); fprintf(fp, "//body\n"); fflush(fp);
- body->print(indent + 1, fp);
- fprintf(fp, "\n");
- //chillindent(indent+1, fp); fprintf(fp, "//END body\n"); fflush(fp);
-
- // tidy up
- chillindent(indent, fp);
- fprintf(fp, "}\n");
- } // if body
- else {
- fprintf(fp, "{}\n"); // should never happen, but not external and no body
- }
- } else { // extern func or forward decl. just end forward declaration
- fprintf(fp, "; // fwd decl\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());
@@ -1373,8 +1285,6 @@ void chillAST_ForStmt::loseLoopWithLoopVar(char *var) {
//fprintf(stderr, "loop condition RHS is ternary\nCondition RHS");
C->print();
- printf("\n");
- fflush(stdout);
chillAST_Node *l = C->lhs;
if (l->isParenExpr()) l = ((chillAST_ParenExpr *) l)->subexpr;
chillAST_Node *r = C->rhs;
@@ -1468,27 +1378,6 @@ chillAST_IntegerLiteral *chillAST_BinaryOperator::evalAsIntegerLiteral() {
return new chillAST_IntegerLiteral(evalAsInt()); // ??
}
-void chillAST_BinaryOperator::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- bool needparens = getPrec()<lhs->getPrec();
-
- if (needparens) fprintf(fp, "(");
- if (lhs) lhs->print(0, fp);
- else fprintf(fp, "(NULL)");
- if (needparens) fprintf(fp, ")");
-
- fprintf(fp, " %s ", op);
-
- needparens = getPrec()<=rhs->getPrec();
-
- if (needparens) fprintf(fp, "(");
- if (rhs) rhs->print(0, fp);
- else fprintf(fp, "(NULL)");
- if (needparens) fprintf(fp, ")");
- fflush(fp);
-}
-
-
char *chillAST_BinaryOperator::stringRep(int indent) {
std::string s = string(lhs->stringRep()) + " " + op + " " + string(lhs->stringRep());
return strdup(s.c_str());
@@ -1706,18 +1595,6 @@ chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_No
if (rhs) rhs->setParent(this);
}
-void chillAST_TernaryOperator::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "(");
- condition->print(0, fp);
- fprintf(fp, "%s", op);
- lhs->print(0, fp);
- fprintf(fp, ":");
- rhs->print(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-}
-
void chillAST_TernaryOperator::replaceChild(chillAST_Node *old, chillAST_Node *newchild) {
//fprintf(stderr, "\nbinop::replaceChild( old 0x%x, new ) lhs 0x%x rhd 0x%x\n", old, lhs, rhs);
@@ -2239,8 +2116,6 @@ chillAST_Node *chillAST_MemberExpr::multibase2() { /*fprintf(stderr, "ME MB2\n"
chillAST_VarDecl *chillAST_MemberExpr::getUnderlyingVarDecl() {
fprintf(stderr, "chillAST_MemberExpr:getUnderlyingVarDecl()\n");
print();
- printf("\n");
- fflush(stdout);
exit(-1);
// find the member with the correct name
@@ -2266,12 +2141,8 @@ chillAST_VarDecl *chillAST_MemberExpr::multibase() {
fprintf(stderr, "chillAST_MemberExpr::multibase() vardecl is not a struct??\n");
fprintf(stderr, "vd ");
vd->print();
- printf("\n");
- fflush(stdout);
fprintf(stderr, "vd ");
vd->dump();
- printf("\n");
- fflush(stdout);
exit(-1);
}
@@ -2326,14 +2197,6 @@ chillAST_DeclRefExpr *buildDeclRefExpr(chillAST_VarDecl *vd) {
return dre;
}
-void chillAST_DeclRefExpr::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- //fprintf(fp, "%s %s", declarationType, declarationName); // this is printing float *A
- fprintf(fp, "%s", declarationName); // this is printing A
- fflush(fp);
-}
-
-
char *chillAST_DeclRefExpr::stringRep(int indent) {
return strdup(declarationName);
}
@@ -2625,44 +2488,6 @@ chillAST_FloatingLiteral::chillAST_FloatingLiteral(chillAST_FloatingLiteral *old
filename = NULL;
}
-
-void chillAST_FloatingLiteral::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- //fprintf(fp, "%f", value);
- // attempt to be more like rose output
- char output[1024]; // warning, hardcoded
-
- if (allthedigits != NULL) {
- strcpy(output, allthedigits); // if they have specified 100 digits of pi, give 'em 100 digits
- //fprintf(stderr, "floatingliteral allthedigits = '%s'\n", allthedigits);
- } else {
- sprintf(output, "%f", value);
-
- // next part to avoid printing 123.4560000000000000000000000000
- char *dot = index(output, '.');
- if (dot) {
- char *end = output + strlen(output);
- char *onechar;
- char *lastnonzero = dot;
- for (onechar = output; onechar < end; onechar++) {
- if (*onechar != '0') lastnonzero = onechar;
- }
-
- if (lastnonzero == dot)
- lastnonzero[2] = '\0'; // may be after end of string, but that should be OK
- else lastnonzero[1] = '\0'; // may be after end of string, but that should be OK
- }
- }
- if (precision == 1) {
- int len = strlen(output);
- output[len] = 'f'; // explicit single precision
- output[len + 1] = '\0';
- }
-
- fprintf(fp, "%s", output);
- fflush(fp);
-}
-
chillAST_Node *chillAST_FloatingLiteral::constantFold() { return this; }; // NOOP
chillAST_Node *chillAST_FloatingLiteral::clone() {
@@ -2689,20 +2514,6 @@ void chillAST_UnaryOperator::gatherArrayRefs(std::vector<chillAST_ArraySubscript
subexpr->gatherArrayRefs(refs, isAssignmentOp()); //
}
-
-void chillAST_UnaryOperator::print(int indent, FILE *fp) {
- bool needparens = false;
- if (subexpr->isNotLeaf()) needparens = true; // may get more complicated
-
- chillindent(indent, fp); // will this ever be invoked?
- if (prefix) fprintf(fp, "%s", op);
- if (needparens) fprintf(fp, "(");
- subexpr->print(0, fp);
- if (needparens) fprintf(fp, ")");
- if (!prefix) fprintf(fp, "%s", op);
- fflush(fp);
-}
-
void chillAST_UnaryOperator::gatherVarLHSUsage(vector<chillAST_VarDecl *> &decls) {
if ((!strcmp("++", op)) || (!strcmp("--", op))) {
subexpr->gatherVarUsage(decls); // do all unary modify the subexpr? (no, - )
@@ -2886,30 +2697,6 @@ void chillAST_CStyleCastExpr::replaceVarDecls(chillAST_VarDecl *olddecl, chillAS
subexpr->replaceVarDecls(olddecl, newdecl);
}
-void chillAST_CStyleCastExpr::print(int indent, FILE *fp) {
- //fprintf(stderr, "CStyleCastExpr::print()\n");
- chillindent(indent, fp);
-
- // special cases? should probably walk the AST and change the literal itself
- if (!strcmp("float", towhat) && subexpr->isIntegerLiteral()) { // (float) 3 => 3.0f
- subexpr->print(0, fp);
- fprintf(fp, ".0f");
- } else if (!strcmp("double", towhat) && subexpr->isIntegerLiteral()) { // (double) 3 => 3.0
- subexpr->print(0, fp);
- fprintf(fp, ".0");
- } else if (!strcmp("float", towhat) && subexpr->isFloatingLiteral()) { // (float) 3.0 => 3.0f
- subexpr->print(0, fp);
- fprintf(fp, "f");
- } else { // general case
- fprintf(fp, "(%s) ", towhat);
- if (subexpr->getPrec()<getPrec()) fprintf(fp,"(");
- if (subexpr->isVarDecl()) fprintf(fp, "%s", ((chillAST_VarDecl *) subexpr)->varname);
- else subexpr->print(indent, fp);
- if (subexpr->getPrec()<getPrec()) fprintf(fp,")");
- }
- fflush(fp);
-};
-
class chillAST_Node *chillAST_CStyleCastExpr::constantFold() {
subexpr = subexpr->constantFold();
return this;
@@ -2964,15 +2751,6 @@ chillAST_CStyleAddressOf::chillAST_CStyleAddressOf(chillAST_Node *sub) {
//fprintf(stderr, "chillAST_CStyleCastExpr (%s) sub 0x%x\n", towhat, sub );
}
-void chillAST_CStyleAddressOf::print(int indent, FILE *fp) {
- //fprintf(stderr, "CStyleAddressOf::print()\n");
- chillindent(indent, fp);
- fprintf(fp, "(&");
- subexpr->print(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-};
-
class chillAST_Node *chillAST_CStyleAddressOf::constantFold() {
subexpr = subexpr->constantFold();
return this;
@@ -3066,16 +2844,6 @@ chillAST_CudaMalloc::chillAST_CudaMalloc(chillAST_Node *devmemptr, chillAST_Node
sizeinbytes = size; // probably a multiply like sizeof(int) * 1024
};
-void chillAST_CudaMalloc::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "cudaMalloc(");
- devPtr->print(0, fp);
- fprintf(fp, ",");
- sizeinbytes->print(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-};
-
class chillAST_Node *chillAST_CudaMalloc::constantFold() {
devPtr = devPtr->constantFold();
return this;
@@ -3127,12 +2895,6 @@ chillAST_CudaFree::chillAST_CudaFree(chillAST_VarDecl *var) {
variable = var;
};
-void chillAST_CudaFree::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "cudaFree(%s)", variable->varname);
- fflush(fp);
-};
-
class chillAST_Node *chillAST_CudaFree::constantFold() {
return this;
}
@@ -3237,30 +2999,11 @@ void chillAST_CudaMemcpy::gatherVarUsage(vector<chillAST_VarDecl *> &decls) {
chillAST_CudaSyncthreads::chillAST_CudaSyncthreads() {
}
-void chillAST_CudaSyncthreads::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "__syncthreads()");
- fflush(fp);
-}
-
chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval) {
returnvalue = retval;
if (returnvalue) returnvalue->setParent(this);
}
-
-void chillAST_ReturnStmt::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- if (returnvalue != NULL) {
- fprintf(fp, "return(");
- returnvalue->print(0, fp);
- fprintf(fp, ")"); // parent will add ";\n" ??
- } else {
- fprintf(fp, "return");
- }
- fflush(fp);
-}
-
class chillAST_Node *chillAST_ReturnStmt::constantFold() {
if (returnvalue) returnvalue = returnvalue->constantFold();
return this;
@@ -3319,55 +3062,6 @@ void chillAST_CallExpr::addArg(chillAST_Node *a) {
numargs += 1;
}
-
-void chillAST_CallExpr::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- chillAST_FunctionDecl *FD = NULL;
- chillAST_MacroDefinition *MD = NULL;
-
- if (callee->isDeclRefExpr()) {
- chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) callee;
- //fprintf(stderr, "DRE decl is 0x%x\n", DRE->decl);
- if (!DRE->decl) {
- // a macro?
- fprintf(fp, "%s ", DRE->declarationName);
- return; // ??
- }
-
- //fprintf(stderr, "DRE decl of type %s\n", DRE->decl->getTypeString());
- if ((DRE->decl)->isFunctionDecl()) FD = (chillAST_FunctionDecl *) DRE->decl;
- else {
- fprintf(stderr, "chillAST_CallExpr::print() DRE decl of type %s\n", DRE->decl->getTypeString());
- exit(-1);
- }
- } else if (callee->isFunctionDecl()) FD = (chillAST_FunctionDecl *) callee;
- else if (callee->isMacroDefinition()) {
- MD = (chillAST_MacroDefinition *) callee;
- fprintf(fp, "%s(", MD->macroName);
- } else {
- fprintf(stderr, "\nchillAST_CallExpr::print() callee of unhandled type %s\n", callee->getTypeString());
- callee->dump();
- exit(-1);
- }
-
- if (FD) {
- fprintf(fp, "%s", FD->functionName);
- fflush(fp);
- if (grid && block) {
- fprintf(fp, "<<<%s,%s>>>(", grid->varname, block->varname); // a
- } else fprintf(fp, "(");
- }
-
-
- //callee->print( indent, fp);
- for (int i = 0; i < args.size(); i++) {
- if (i != 0) fprintf(fp, ", ");
- args[i]->print(0, fp);
- }
- fprintf(fp, ")"); //a
- fflush(fp);
-}
-
void chillAST_CallExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {
for (int i = 0; i < args.size(); i++) {
args[i]->gatherArrayRefs(refs, writtento);
@@ -3751,22 +3445,6 @@ chillAST_CompoundStmt::chillAST_CompoundStmt() {
typedefTable = new chillAST_TypedefTable;
};
-
-void chillAST_CompoundStmt::print(int indent, FILE *fp) {
- int numchildren = children.size();
- for (int i = 0; i < numchildren; i++) {
- children[i]->print(indent, fp);
- if (children[i]->getType() != CHILLAST_NODE_FORSTMT
- && children[i]->getType() != CHILLAST_NODE_IFSTMT
- && children[i]->getType() != CHILLAST_NODE_COMPOUNDSTMT
- //&& children[i]->getType() != CHILLAST_NODE_VARDECL // vardecl does its own ";\n"
- ) {
- fprintf(fp, ";\n"); // probably wrong
- }
- }
- fflush(fp);
-}
-
void chillAST_CompoundStmt::replaceChild(chillAST_Node *old, chillAST_Node *newchild) {
//fprintf(stderr, "chillAST_CompoundStmt::replaceChild( old %s, new %s)\n", old->getTypeString(), newchild->getTypeString() );
vector<chillAST_Node *> dupe = children;
@@ -3979,15 +3657,6 @@ chillAST_ParenExpr::chillAST_ParenExpr(chillAST_Node *sub) {
subexpr->setParent(this);
}
-void chillAST_ParenExpr::print(int indent, FILE *fp) {
- //fprintf(stderr, "chillAST_ParenExpr::print()\n");
- chillindent(indent, fp); // hard to believe this will ever do anything
- fprintf(fp, "(");
- subexpr->print(0, fp);
- fprintf(fp, ")");
- fflush(fp);
-}
-
void chillAST_ParenExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {
subexpr->gatherArrayRefs(refs, writtento);
}
@@ -4265,61 +3934,6 @@ chillAST_Node *chillAST_IfStmt::clone() {
return IS;
}
-void chillAST_IfStmt::print(int indent, FILE *fp) {
- chillindent(indent, fp);
- fprintf(fp, "if (");
- if (cond) cond->print(0, fp);
- else fprintf(fp, "(NULL cond)");
-
- bool needbracket = true;
- if (thenpart) {
- if (thenpart->isBinaryOperator()) needbracket = false;
- if (thenpart->isCompoundStmt()) { // almost always true
- chillAST_CompoundStmt *CS = (chillAST_CompoundStmt *) thenpart;
- if (CS->children.size() == 1 && CS->children[0]->isBinaryOperator()) needbracket = false;
- }
-
- if (needbracket) fprintf(fp, ") {\n");
- else fprintf(fp, ")\n");
-
- thenpart->print(indent + 1, fp); // end of line
-
- if (needbracket) {
- //fprintf(fp, "\n");
- chillindent(indent, fp);
- fprintf(fp, "}\n");
- }
- } else fprintf(fp, "(NULL thenpart)");
-
-
- needbracket = true;
- if (elsepart) {
- if (elsepart->isBinaryOperator()) needbracket = false;
- if (elsepart->isCompoundStmt()) { // almost always true
- chillAST_CompoundStmt *CS = (chillAST_CompoundStmt *) elsepart;
-
- if (CS->children.size() == 1 && CS->children[0]->isBinaryOperator()) needbracket = false;
-
- }
-
- fprintf(fp, "\n");
- chillindent(indent, fp);
-
- if (needbracket) fprintf(fp, "else {\n");
- else fprintf(fp, "else\n");
-
- elsepart->print(indent + 1, fp);
-
- if (needbracket) {
- fprintf(fp, "\n");
- chillindent(indent, fp);
- fprintf(fp, "}\n");
- }
- }
- //else fprintf(fp, "else { /* NOTHING */ }");
-}
-
-
bool chillAST_IfStmt::findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync) {
thenpart->findLoopIndexesToReplace(symtab);
elsepart->findLoopIndexesToReplace(symtab);
@@ -4478,78 +4092,6 @@ chillAST_Preprocessing::chillAST_Preprocessing(CHILLAST_PREPROCESSING_POSITION p
blurb = strdup(text);
}
-void chillAST_Preprocessing::print(int indent, FILE *fp) { // probably very wrong
- if (position == CHILLAST_PREPROCESSING_LINEAFTER) {
- fprintf(fp, "\n");
- chillindent(indent, fp);
- }
- if (position == CHILLAST_PREPROCESSING_LINEBEFORE) { // ???
- //fprintf(fp, "\n");
- chillindent(indent, fp);
- }
-
- fprintf(fp, "%s", blurb);
-
- if (position == CHILLAST_PREPROCESSING_TOTHERIGHT) {
- fprintf(fp, "\n");
- }
-
-
- if (position == CHILLAST_PREPROCESSING_LINEBEFORE) {
- }
-
-
-
-}
-
-//! I'm just a bit lazy to write ifs ...
-const char* binaryPrec[] = {
- " :: ",
- " . -> ",
- "",
- " .* ->* ",
- " * / % ",
- " + - ",
- " << >> ",
- " < <= > >=",
- " == != ",
- " & ",
- " ^ ",
- " | ",
- " && ",
- " || ",
- " = += -= *= /= %= <<= >>= &= ^= |= ",
- " , "
-};
-
-bool opInSet(const char* set,char* op) {
- string tmp = op;
- tmp=" "+tmp+" ";
- return strstr(set, tmp.c_str()) != NULL;
-}
-
-int chillAST_BinaryOperator::getPrec() {
- for (int i = 0; i< 16;++i)
- if (opInSet(binaryPrec[i],op)) return INT8_MAX+i+1;
- return INT8_MAX;
-}
-
-const char* unaryPrec[] = {
- "",
- " -- ++ ",
- " -- ++ + - ! ~ * & ",
-};
-
-int chillAST_UnaryOperator::getPrec() {
- if (prefix)
- for (int i = 2;i>=0;--i)
- if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1;
- else
- for (int i = 0;i<3;--i)
- if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1;
- return INT8_MAX;
-}
-
void chillAST_Node::addVariableToScope(chillAST_VarDecl *vd) {
CHILL_DEBUG_PRINT("addVariableToScope( %s )\n", vd->varname);
if (!symbolTable) return;
@@ -4609,8 +4151,22 @@ void chillAST_Node::dump(int indent, FILE *fp) {
if (fp == stderr) {
chill::printer::Dump d;
d.printErr("",this);
+ fprintf(stderr,"\n");
} else {
chill::printer::Dump d;
d.printOut("",this);
+ fprintf(stdout,"\n");
+ }
+}
+
+void chillAST_Node::print(int indent, FILE *fp) {
+ if (fp == stderr) {
+ chill::printer::CFamily d;
+ d.printErr("",this);
+ fprintf(stderr,"\n");
+ } else {
+ chill::printer::CFamily d;
+ d.printOut("",this);
+ fprintf(stdout,"\n");
}
} \ No newline at end of file
diff --git a/src/ir_clang.cc b/src/ir_clang.cc
index 408148f..e0699a5 100755
--- a/src/ir_clang.cc
+++ b/src/ir_clang.cc
@@ -1636,14 +1636,6 @@ bool IR_chillArrayRef::operator!=(const IR_Ref &that) const {
return !op;
}
-void IR_chillArrayRef::Dump() const {
- //fprintf(stderr, "IR_chillArrayRef::Dump() this 0x%x chillASE 0x%x\n", this, chillASE);
- chillASE->print();
- printf("\n");
- fflush(stdout);
-}
-
-
bool IR_chillArrayRef::operator==(const IR_Ref &that) const {
//fprintf(stderr, "IR_xxxxArrayRef::operator==\n");
//printf("I am\n"); chillASE->print(); printf("\n");
diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp
index a15c106..e1e2b9a 100644
--- a/src/printer/cfamily.cpp
+++ b/src/printer/cfamily.cpp
@@ -3,123 +3,170 @@
//
#include "printer/cfamily.h"
+#include <iomanip>
+
using namespace std;
using namespace chill::printer;
+bool opInSet(const char *set, char *op) {
+ string tmp = op;
+ tmp = " " + tmp + " ";
+ return strstr(set, tmp.c_str()) != NULL;
+}
+
bool ifSemicolonFree(CHILLAST_NODE_TYPE t) {
return t == CHILLAST_NODE_FUNCTIONDECL || t == CHILLAST_NODE_IFSTMT ||
t == CHILLAST_NODE_FORSTMT || t == CHILLAST_NODE_MACRODEFINITION;
}
-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 CFamily::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) {
+ print(ident, n->base, o);
+ o << "[";
+ print(ident, n->index, o);
+ o << "]";
+}
+
+//! I'm just a bit lazy to write ifs ...
+const char *binaryPrec[] = {
+ " :: ",
+ " . -> ",
+ "",
+ " .* ->* ",
+ " * / % ",
+ " + - ",
+ " << >> ",
+ " < <= > >=",
+ " == != ",
+ " & ",
+ " ^ ",
+ " | ",
+ " && ",
+ " || ",
+ " = += -= *= /= %= <<= >>= &= ^= |= ",
+ " , "
+};
+
+int CFamily::getPrecS(chillAST_BinaryOperator *n) {
+ for (int i = 0; i < 16; ++i)
+ if (opInSet(binaryPrec[i], n->op)) return defGetPrecS() + i + 1;
+ CHILL_ERROR("Unrecognized binary operator: %s\n", n->op);
+ return defGetPrecS();
+}
+
+void CFamily::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) {
+ int prec = getPrec(n);
+ if (n->lhs) printPrec(ident, n->lhs, o, prec);
+ else o << "(NULL)";
+ o << " " << n->op << " ";
+ if (n->rhs) printPrec(ident, n->rhs, o, prec);
+ else o << "(NULL)";
}
-void dumpVector(GenericPrinter *p, string ident, chillAST_TypedefTable *n, ostream &o) {
- for (int i = 0; i < n->size(); ++i)
- p->print("", (*n)[i], o);
+int CFamily::getPrecS(chillAST_CallExpr *n) {
+ return defGetPrecS() + 2;
}
-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 << ") ";
+void CFamily::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) {
+ chillAST_FunctionDecl *FD = NULL;
+ chillAST_MacroDefinition *MD = NULL;
+ if (n->callee->isDeclRefExpr()) {
+ chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) (n->callee);
+ if (!(DRE->decl)) {
+ o << DRE->declarationName;
+ return;
+ }
+ if (DRE->decl->isFunctionDecl()) FD = (chillAST_FunctionDecl *) (DRE->decl);
+ else
+ CHILL_ERROR("Function DRE of type %s\n", DRE->decl->getTypeString());
+ } else if (n->callee->isFunctionDecl())
+ FD = (chillAST_FunctionDecl *) n->callee;
+ else if (n->callee->isMacroDefinition())
+ MD = (chillAST_MacroDefinition *) n->callee;
+ if (FD) {
+ o << FD->functionName;
+ if (n->grid && n->block)
+ o << "<<<" << n->grid->varname << "," << n->block->varname << ">>>";
+ o << "(";
}
- if (n->getSymbolTable()) {
- o << "(VarScope: ";
- dumpVector(this, ident, n->getSymbolTable(), o);
- o << ") ";
+ if (MD && n->args.size())
+ o << "(";
+ for (int i = 0; i < n->args.size(); ++i) {
+ if (i != 0) o << ", ";
+ print(ident, n->args[i], o);
}
- if (n->getTypedefTable()) {
- o << "(TypeDef: ";
- dumpVector(this, ident, n->getTypedefTable(), o);
- o << ") ";
+ if (FD || n->args.size())
+ o << ")";
+}
+
+void CFamily::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) {
+ chillAST_NodeList *c = n->getChildren();
+ string nid = ident + identSpace;
+ if (c->size() > 1) o << "{";
+ for (int i = 0; i < c->size(); ++i) {
+ o << "\n" << nid;
+ print(nid, c->at(i), o);
+ if (!ifSemicolonFree(c->at(i)->getType())) o << ";";
}
- o << ": ";
- // Recurse
- GenericPrinter::print(ident, n, o);
- o << ") ";
-}
-
-void Dump::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) {
- print(ident,n->base,o);
- o<<"[";
- print(ident,n->index,o);
- 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) ";
+ if (c->size() > 1) o << "\n" << ident << "}";
}
-void Dump::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) {
- if (n->callee)
- print(ident, n->callee, o);
+int CFamily::getPrecS(chillAST_CStyleAddressOf *n) {
+ return defGetPrecS() + 3;
}
-void Dump::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) {
- dumpVector(this, ident, n->getChildren(), o);
+void CFamily::printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) {
+ int prec = getPrec(n);
+ printPrec(ident, n->subexpr, o, prec);
}
-void Dump::printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) {
- print(ident, n->subexpr, o);
+int CFamily::getPrecS(chillAST_CStyleCastExpr *n) {
+ return defGetPrecS() + 3;
}
-void Dump::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) {
- o << n->towhat << " ";
- print(ident, n->subexpr, o);
+void CFamily::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) {
+ o << "(" << n->towhat << ")";
+ printPrec(ident, n->subexpr, o, getPrec(n));
}
-void Dump::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) {
- o << n->variable->varname << " ";
+void CFamily::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) {
+ o << "cudaFree(" << n->variable->varname << ")";
}
-void Dump::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) {
+void CFamily::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) {
+void CFamily::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) {
+ o << "cudaMalloc(";
print(ident, n->devPtr, o);
+ o << ", ";
print(ident, n->sizeinbytes, o);
+ o << ")";
}
-void Dump::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) {
o << "cudaMemcpy(" << n->dest->varname << ", " << n->src->varname << ", ";
print(ident, n->size, o);
o << ", " << n->cudaMemcpyKind << ")";
}
-void Dump::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {}
+void CFamily::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {
+ o << "__syncthreads()";
+}
-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 CFamily::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) {
+ o << n->declarationName;
}
-void Dump::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) {
- if (n->precision == 1) o << "float ";
- else o << "double ";
- o << n->value;
+void CFamily::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) {
+ // Althedigits contaminates the result
+ o << showpoint << n->value;
+ if (n->getPrecision() == 1)
+ o << "f";
}
-void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
if (n->metacomment)
o << "// " << n->metacomment << "\n";
o << "for (";
@@ -128,10 +175,8 @@ void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
print(ident, n->getCond(), o);
o << ";";
print(ident, n->getInc(), o);
- o << ")";
- if (n->getBody()->getType() == CHILLAST_NODE_COMPOUNDSTMT) {
- if (n->getBody()->getChildren()->size() < 2) o << "\n" << ident << identSpace;
- else o << " ";
+ o << ") ";
+ if (n->getBody()->isCompoundStmt()) {
print(ident, n->getBody(), o);
} else {
CHILL_ERROR("Body of for loop not COMPOUNDSTMT\n");
@@ -139,50 +184,78 @@ void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) {
}
}
-void Dump::printS(std::string ident, chillAST_Free *n, std::ostream &o) {}
+void CFamily::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 CFamily::printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) {
+ if (n->isExtern()) o << "extern ";
+ if (n->getFunctionType() == CHILLAST_FUNCTION_GPU) o << "__global__ ";
+ o << n->returnType << " " << n->functionName << "(";
+
+ chillAST_SymbolTable *pars = n->getParameters();
+ for (int i = 0; i < pars->size(); ++i) {
+ if (i != 0)
+ o << ", ";
+ print(ident, pars->at(i), o);
+ }
+ o << ")";
+ if (!(n->isExtern() || n->isForward())) {
+ o << " ";
+ if (n->getBody())
+ print(ident, n->getBody(), o);
+ else {
+ CHILL_ERROR("Non-extern or forward function decl doesn't have a body");
+ o << "{}";
+ }
+ } else {
+ 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)
+void CFamily::printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) {
+ o << "if (";
+ print(ident, n->getCond(), o);
+ o << ") ";
+ if (!n->getThen()) {
+ CHILL_ERROR("If statement is without then part!");
+ exit(-1);
+ }
+ print(ident, n->getThen(), o);
+ if (!(n->getThen()->isCompoundStmt()))
+ CHILL_ERROR("Then part is not a CompoundStmt!\n");
+ if (n->getElse()) {
+ o << "else ";
print(ident, n->elsepart, o);
+ }
}
-void Dump::printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) {
+void CFamily::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) {
+void CFamily::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) {
+void CFamily::printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) {
o << "#define" << n->macroName << " ";
int np = n->getParameters()->size();
if (np) {
o << "(" << n->getParameters()->at(0)->varname;
for (int i = 1; i < np; ++i)
o << ", " << n->getParameters()->at(i)->varname;
- o<<")";
+ o << ")";
}
// TODO newline for multiline macro
print(ident, n->getBody(), o);
}
-void Dump::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) {
o << "malloc(";
print(ident, n->sizeexpr, o);
o << ")";
}
-void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) {
int prec = getPrec(n);
if (n->base) printPrec(ident, n->base, o, prec);
else o << "(NULL)";
@@ -192,48 +265,67 @@ void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) {
else o << "(NULL)";
}
-void Dump::printS(std::string ident, chillAST_NULL *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_NULL *n, std::ostream &o) {
o << "/* (NULL statement) */";
}
-void Dump::printS(std::string ident, chillAST_NoOp *n, std::ostream &o) {}
+void CFamily::printS(std::string ident, chillAST_NoOp *n, std::ostream &o) {}
-void Dump::printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) {
+ o << "(";
print(ident, n->subexpr, o);
+ o << ")";
}
-void Dump::printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) {}
+void CFamily::printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) {
+ CHILL_ERROR("Not implemented\n");
+}
-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 CFamily::printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) {
+ if (n->isUnnamed) return;
+ if (n->isAStruct()) {
+ string nid = ident + identSpace;
+ o << "struct " << n->getName() << " {";
+ chillAST_SymbolTable *sp = n->getSubparts();
+ for (int i = 0; i < sp->size(); ++i) {
+ o << "\n" << nid;
+ print(nid, sp->at(i), o);
+ o << ";";
+ }
+ o << "\n" << ident << "}";
+ } else {
+ CHILL_ERROR("Encountered Unkown record type");
+ exit(-1);
+ }
}
-void Dump::printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) {
- if (n->returnvalue) print(ident, n->returnvalue, o);
+void CFamily::printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) {
+ o << "return";
+ if (n->returnvalue) {
+ o << " ";
+ print(ident, n->returnvalue, o);
+ }
}
-void Dump::printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) {
o << "sizeof(" << n->thing << ")";
}
-void Dump::printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) {
o << "// this source is derived from CHILL AST originally from file '"
<< n->filename << "' as parsed by frontend compiler " << n->frontend << "\n\n";
int nchild = n->getChildren()->size();
for (int i = 0; i < nchild; ++i) {
if (n->getChild(i)->isFromSourceFile) {
o << ident;
- print(indent, n->getChild(i), o);
- if (!isSemiColonFree(n->getChild(i)->getType())) o << ";\n";
- else o<<"\n";
+ print(ident, n->getChild(i), o);
+ if (!ifSemicolonFree(n->getChild(i)->getType())) o << ";\n";
+ else o << "\n";
}
}
}
-void Dump::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) {
if (n->isAStruct())
o << "/* A typedef STRUCT */\n";
o << ident << "typedef ";
@@ -246,20 +338,43 @@ void Dump::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) {
o << n->newtype;
}
-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);
+int CFamily::getPrecS(chillAST_TernaryOperator *n) {
+ return defGetPrecS() + 15;
}
-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 CFamily::printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) {
+ int prec = getPrec(n);
+ printPrec(ident, n->condition, o, prec);
+ o << "" << n->op << "";
+ printPrec(ident, n->lhs, o, prec);
+ o << ":";
+ printPrec(ident, n->rhs, o, prec);
+}
+
+const char *unaryPrec[] = {
+ "",
+ " -- ++ ",
+ " -- ++ + - ! ~ * & ",
+};
+
+int CFamily::getPrecS(chillAST_UnaryOperator *n) {
+ if (n->prefix) {
+ for (int i = 2; i >= 0; --i)
+ if (opInSet(unaryPrec[i], n->op)) return defGetPrecS() + i + 1;
+ } else
+ for (int i = 1; i < 3; ++i)
+ if (opInSet(unaryPrec[i], n->op)) return defGetPrecS() + i + 1;
+ return defGetPrecS();
+}
+
+void CFamily::printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) {
+ int prec = getPrec(n);
+ if (n->prefix) o << n->op;
+ printPrec(ident, n->subexpr, o, prec);
+ if (!n->prefix) o << n->op;
}
-void Dump::printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) {
+void CFamily::printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) {
if (n->isDevice) o << "__device__ ";
if (n->isShared) o << "__shared__ ";
if (n->isRestrict) o << "__restrict__ ";
diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp
index bf3f6b8..66bcc7e 100644
--- a/src/printer/dump.cpp
+++ b/src/printer/dump.cpp
@@ -10,15 +10,17 @@ 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);
+ p->print(ident, (*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);
+ p->print(ident, (*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);
+ p->print(ident, (*n)[i], o);
}
void Dump::print(string ident, chillAST_Node *n, ostream &o) {
diff --git a/src/printer/generic.cpp b/src/printer/generic.cpp
index 67c694f..79ee312 100644
--- a/src/printer/generic.cpp
+++ b/src/printer/generic.cpp
@@ -198,3 +198,6 @@ int GenericPrinter::getPrec(chillAST_Node *n) {
}
}
+void GenericPrinter::errorPrintS(std::string ident, chillAST_Node *n, std::ostream &o) {
+ CHILL_ERROR("Unhandled case in printer: %s\n", n->getTypeString());
+}