diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-10-06 16:07:35 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-10-06 16:07:35 -0600 |
commit | f329ee2b4cfdde656d2fe30f2a2789d8a3774203 (patch) | |
tree | ea000895ad943188ebdded11f4311b4e1cf011bb | |
parent | b7a50e256d0ac6ec120050173b37f34d434ef325 (diff) | |
download | chill-f329ee2b4cfdde656d2fe30f2a2789d8a3774203.tar.gz chill-f329ee2b4cfdde656d2fe30f2a2789d8a3774203.tar.bz2 chill-f329ee2b4cfdde656d2fe30f2a2789d8a3774203.zip |
cleanup
-rw-r--r-- | include/chillAST/chillASTs.hh | 60 | ||||
-rw-r--r-- | include/chilldebug.h | 2 | ||||
-rwxr-xr-x | lib/chillcg/include/code_gen/CG_chillBuilder.h | 300 | ||||
-rwxr-xr-x | lib/chillcg/include/code_gen/CG_chillRepr.h | 22 | ||||
-rwxr-xr-x | lib/chillcg/src/CG_chillBuilder.cc | 497 | ||||
-rwxr-xr-x | lib/chillcg/src/CG_chillRepr.cc | 100 | ||||
-rw-r--r-- | lib/codegen/include/code_gen/CG_outputBuilder.h | 4 | ||||
-rw-r--r-- | lib/codegen/include/code_gen/CG_stringBuilder.h | 2 | ||||
-rw-r--r-- | lib/codegen/include/code_gen/CGdebug.h | 31 | ||||
-rwxr-xr-x | lib/codegen/src/CG_stringBuilder.cc | 6 | ||||
-rw-r--r-- | src/ast/node.cpp | 23 | ||||
-rw-r--r-- | src/chillASTs.cc | 343 | ||||
-rwxr-xr-x | src/ir_clang.cc | 67 | ||||
-rw-r--r-- | src/printer/cfamily.cpp | 6 | ||||
-rw-r--r-- | src/printer/dump.cpp | 6 | ||||
-rw-r--r-- | src/transformations/loop.cc | 4 |
16 files changed, 342 insertions, 1131 deletions
diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index 9ae3617..93b4420 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -704,10 +704,6 @@ class chillAST_ArraySubscriptExpr : public chillAST_Node { public: 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 - chillAST_Node *base; - chillAST_Node *index; bool imwrittento; bool imreadfrom; // WARNING: ONLY used when both writtento and readfrom are true x += 1 and so on chillAST_VarDecl *basedecl; // the vardecl that this refers to @@ -715,10 +711,17 @@ public: void *uniquePtr; // constructors + chillAST_ArraySubscriptExpr(); + chillAST_ArraySubscriptExpr(chillAST_Node *bas, chillAST_Node *indx, bool writtento, void *unique); chillAST_ArraySubscriptExpr(chillAST_VarDecl *v, std::vector<chillAST_Node *> indeces); + chillAST_Node *getBase() { return getChild(0); } + void setBase(chillAST_Node* b) { setChild(0, b); } + chillAST_Node *getIndex() { return getChild(1); } + void setIndex(chillAST_Node* i) { setChild(1, i); } + // other methods particular to this type of node bool operator!=(const chillAST_ArraySubscriptExpr &); @@ -727,17 +730,13 @@ 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 *multibase2() { return getBase()->multibase2(); } chillAST_Node *getIndex(int dim); void gatherIndeces(std::vector<chillAST_Node *> &ind); - void replaceChild(chillAST_Node *old, chillAST_Node *newchild); // will examine index - // required methods that I can't seem to get to inherit - chillAST_Node *constantFold(); - chillAST_Node *clone(); chillAST_Node *findref() { return this; }// find the SINGLE constant or data reference at this node or below @@ -745,27 +744,14 @@ public: void gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento); - void gatherVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherScalarVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherArrayVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherVarUsage(std::vector<chillAST_VarDecl *> &decls); - - void gatherDeclRefExprs(std::vector<chillAST_DeclRefExpr *> &refs); - - void replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); - bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here const char *getUnderlyingType() { - //fprintf(stderr, "ASE getUnderlyingType() base of type %s\n", base->getTypeString()); base->print(); printf("\n"); fflush(stderr); - return base->getUnderlyingType(); + return getBase()->getUnderlyingType(); }; - virtual chillAST_VarDecl *getUnderlyingVarDecl() { return base->getUnderlyingVarDecl(); }; + virtual chillAST_VarDecl *getUnderlyingVarDecl() { return getBase()->getUnderlyingVarDecl(); }; }; @@ -774,7 +760,6 @@ public: 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; char *printstring; @@ -783,44 +768,27 @@ public: CHILLAST_MEMBER_EXP_TYPE exptype; - // constructors + chillAST_MemberExpr(); chillAST_MemberExpr(chillAST_Node *bas, const char *mem, void *unique, CHILLAST_MEMBER_EXP_TYPE t = CHILLAST_MEMBER_EXP_DOT); + chillAST_Node *getBase() { return getChild(0); } + void setBase(chillAST_Node *b) { setChild(0,b); } + // other methods particular to this type of node bool operator!=(const chillAST_MemberExpr &); bool operator==(const chillAST_MemberExpr &); // required methods that I can't seem to get to inherit - chillAST_Node *constantFold(); - chillAST_Node *clone(); - void gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento); - - void gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento); - - void gatherVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherScalarVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherArrayVarDecls(std::vector<chillAST_VarDecl *> &decls); - - void gatherVarUsage(std::vector<chillAST_VarDecl *> &decls); - - void gatherDeclRefExprs(std::vector<chillAST_DeclRefExpr *> &refs); - - void replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl); - bool findLoopIndexesToReplace(chillAST_SymbolTable *symtab, bool forcesync = false) { return false; }; // no loops under here chillAST_VarDecl *getUnderlyingVarDecl(); - void replaceChild(chillAST_Node *old, chillAST_Node *newchild); - void setType(CHILLAST_MEMBER_EXP_TYPE t) { exptype = t; }; CHILLAST_MEMBER_EXP_TYPE getType(CHILLAST_MEMBER_EXP_TYPE t) { return exptype; }; diff --git a/include/chilldebug.h b/include/chilldebug.h index 010687a..6a1b95b 100644 --- a/include/chilldebug.h +++ b/include/chilldebug.h @@ -11,7 +11,7 @@ // This thing below potentially create leaks #define FILENAME basename(strdup(__FILE__)) -#ifdef DEBUGCHILL +#ifdef DEBUGCHILL #define CHILL_DEBUG_PRINT(format,args...) fprintf(stderr,"%15s | %15s | LN%-4d:\t" format,FILENAME,__FUNCTION__, \ __LINE__, ##args ) #define CHILL_DEBUG_BEGIN { \ diff --git a/lib/chillcg/include/code_gen/CG_chillBuilder.h b/lib/chillcg/include/code_gen/CG_chillBuilder.h index 172f198..22f495a 100755 --- a/lib/chillcg/include/code_gen/CG_chillBuilder.h +++ b/lib/chillcg/include/code_gen/CG_chillBuilder.h @@ -5,191 +5,145 @@ #include <code_gen/CG_outputBuilder.h> #include <code_gen/CG_chillRepr.h> #include <string> - - namespace omega { -class CG_chillBuilder : public CG_outputBuilder { -private: - - chillAST_SourceFile *toplevel; - chillAST_FunctionDecl *currentfunction; - - chillAST_SymbolTable *symtab_; // symbol table for FUNC (parameters?) - chillAST_SymbolTable *symtab2_; // symbol table for func BODY - -public: - CG_chillBuilder() ; - CG_chillBuilder(chillAST_SourceFile *top, chillAST_FunctionDecl *func ) ; - ~CG_chillBuilder(); + class CG_chillBuilder : public CG_outputBuilder { + private: + chillAST_SourceFile *toplevel; + chillAST_FunctionDecl *currentfunction; + chillAST_SymbolTable *symtab_; // symbol table for FUNC (parameters?) + chillAST_SymbolTable *symtab2_; // symbol table for func BODY + public: + CG_chillBuilder(); + + CG_chillBuilder(chillAST_SourceFile *top, chillAST_FunctionDecl *func); + + ~CG_chillBuilder(); + + const char *ClassName() { return "chillBuilder"; }; + + //! place holder generation TODO DOC and Usage + CG_outputRepr *CreatePlaceHolder(int indent, + CG_outputRepr *stmt, + Tuple<CG_outputRepr *> &funcList, + Tuple<std::string> &loop_vars) const; + + CG_outputRepr *CreateSubstitutedStmt(int indent, + CG_outputRepr *stmt, + const std::vector<std::string> &vars, + std::vector<CG_outputRepr *> &subs, + bool actuallyPrint = true) const; + + CG_outputRepr *CreateAssignment(int indent, CG_outputRepr *lhs, CG_outputRepr *rhs) const; + + CG_outputRepr *CreatePlusAssignment(int indent, + CG_outputRepr *lhs, + CG_outputRepr *rhs) const; + + CG_outputRepr *CreateInvoke(const std::string &funcName, + std::vector<CG_outputRepr *> &argList) const; + + CG_outputRepr *CreateComment(int indent, const std::string &commentText) const; + + CG_outputRepr *CreateAttribute(CG_outputRepr *control, + const std::string &commentText) const; + + CG_outputRepr *CreatePragmaAttribute(CG_outputRepr *scopeStmt, int looplevel, + const std::string &pragmaText) const; + + CG_outputRepr *CreatePrefetchAttribute(CG_outputRepr *scopeStmt, int looplevel, + const std::string &arrName, int hint) const; + + CG_outputRepr *CreateIf(int indent, + CG_outputRepr *guardCondition, + CG_outputRepr *true_stmtList, + CG_outputRepr *false_stmtList) const; + + CG_outputRepr *CreateInductive(CG_outputRepr *index, + CG_outputRepr *lower, + CG_outputRepr *upper, + CG_outputRepr *step) const; + + CG_outputRepr *CreateLoop(int indent, + CG_outputRepr *control, + CG_outputRepr *stmtList) const; + + CG_outputRepr *CreateInt(int num) const; + + CG_outputRepr *CreateFloat(float num) const; + + CG_outputRepr *CreateDouble(double num) const; + + bool isInteger(CG_outputRepr *op) const; + + CG_outputRepr *CreateIdent(const std::string &idStr) const; + + CG_outputRepr *CreateDotExpression(CG_outputRepr *l, CG_outputRepr *r) const; - const char *ClassName() { return "chillBuilder"; }; - - //--------------------------------------------------------------------------- - // place holder generation not in CG_outputBuilder ?? or CG_roseBuilder - //--------------------------------------------------------------------------- - CG_outputRepr* CreatePlaceHolder(int indent, - CG_outputRepr *stmt, - Tuple<CG_outputRepr*> &funcList, - Tuple<std::string> &loop_vars) const; - - //--------------------------------------------------------------------------- - // substitute variables in stmt - //--------------------------------------------------------------------------- - - - CG_outputRepr *CreateSubstitutedStmt(int indent, - CG_outputRepr *stmt, - const std::vector<std::string> &vars, - std::vector<CG_outputRepr *> &subs, - bool actuallyPrint =true) const; - - - - //--------------------------------------------------------------------------- - // assignment generation - //--------------------------------------------------------------------------- - CG_outputRepr* CreateAssignment(int indent, CG_outputRepr* lhs, CG_outputRepr* rhs) const; - - CG_outputRepr *CreatePlusAssignment(int indent, - CG_outputRepr *lhs, - CG_outputRepr *rhs) const; - - - //--------------------------------------------------------------------------- - // function invocation generation - //--------------------------------------------------------------------------- - CG_outputRepr* CreateInvoke(const std::string &funcName, - std::vector<CG_outputRepr*> &argList, - bool is_array = true) const; - - //--------------------------------------------------------------------------- - // comment generation - //--------------------------------------------------------------------------- - CG_outputRepr* CreateComment(int indent, const std::string &commentText) const; - - //--------------------------------------------------------------------------- - // Attribute generation - //--------------------------------------------------------------------------- - CG_outputRepr* CreateAttribute(CG_outputRepr *control, - const std::string &commentText) const; - - //--------------------------------------------------------------------------- - // Pragma Attribute - //--------------------------------------------------------------------------- - CG_outputRepr* CreatePragmaAttribute(CG_outputRepr *scopeStmt, int looplevel, - const std::string &pragmaText) const; - - //--------------------------------------------------------------------------- - // Prefetch Attribute - //--------------------------------------------------------------------------- - CG_outputRepr* CreatePrefetchAttribute(CG_outputRepr *scopeStmt, int looplevel, - const std::string &arrName, int hint) const; - - //--------------------------------------------------------------------------- - // if stmt gen operations - //--------------------------------------------------------------------------- - CG_outputRepr* CreateIf(int indent, - CG_outputRepr* guardCondition, - CG_outputRepr* true_stmtList, - CG_outputRepr* false_stmtList) const; - - //--------------------------------------------------------------------------- - // inductive variable generation, to be used in CreateLoop as control - //--------------------------------------------------------------------------- - CG_outputRepr* CreateInductive(CG_outputRepr* index, - CG_outputRepr* lower, - CG_outputRepr* upper, - CG_outputRepr* step) const; - - //--------------------------------------------------------------------------- - // loop stmt generation - //--------------------------------------------------------------------------- - CG_outputRepr* CreateLoop(int indent, - CG_outputRepr* control, - CG_outputRepr* stmtList) const; - - //--------------------------------------------------------------------------- - // basic operations - //--------------------------------------------------------------------------- - CG_outputRepr* CreateInt(int num ) const; // create Integer constant? - CG_outputRepr* CreateFloat(float num ) const; // - CG_outputRepr* CreateDouble(double num ) const; // should these all be chillRepr ??? - - - - - bool isInteger(CG_outputRepr *op) const; - CG_outputRepr* CreateIdent(const std::string &idStr) const; // create a new INTEGER identifier - - - CG_outputRepr* CreateDotExpression(CG_outputRepr *l, CG_outputRepr *r) const; - CG_outputRepr* CreateArrayRefExpression(const std::string &_s, - CG_outputRepr *rop) const; - CG_outputRepr* CreateArrayRefExpression(CG_outputRepr *lop, - CG_outputRepr *rop) const; - CG_outputRepr* ObtainInspectorData(const std::string &_s, const std::string &member_name) const; - bool QueryInspectorType(const std::string &varName) const; - CG_outputRepr* CreateNullStatement() const; - - //--------------------------------------------------------------------------- - // binary arithmetic operations - //--------------------------------------------------------------------------- - CG_outputRepr* CreatePlus( CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateMinus( CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateTimes( CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateIntegerDivide(CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateIntegerFloor( CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateIntegerMod( CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateIntegerCeil( CG_outputRepr* lop, CG_outputRepr* rop) const; - - //--------------------------------------------------------------------------- - // binary logical operations - //--------------------------------------------------------------------------- - CG_outputRepr* CreateAnd(CG_outputRepr* lop, CG_outputRepr* rop) const; - - //--------------------------------------------------------------------------- - // binary relational operations - //--------------------------------------------------------------------------- - //CG_outputRepr* CreateGE(CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateLE(CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateEQ(CG_outputRepr* lop, CG_outputRepr* rop) const; - CG_outputRepr* CreateNEQ(CG_outputRepr* lop, CG_outputRepr* rop) const; - - //--------------------------------------------------------------------------- - // stmt list gen operations - //--------------------------------------------------------------------------- - CG_outputRepr* + CG_outputRepr *CreateArrayRefExpression(const std::string &_s, + CG_outputRepr *rop) const; + + CG_outputRepr *CreateArrayRefExpression(CG_outputRepr *lop, + CG_outputRepr *rop) const; + + CG_outputRepr *ObtainInspectorData(const std::string &_s, const std::string &member_name) const; + + bool QueryInspectorType(const std::string &varName) const; + + CG_outputRepr *CreateNullStatement() const; + + CG_outputRepr *CreatePlus(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateMinus(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateTimes(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateIntegerDivide(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateIntegerFloor(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateIntegerMod(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateIntegerCeil(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateAnd(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateLE(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateEQ(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr *CreateNEQ(CG_outputRepr *lop, CG_outputRepr *rop) const; + + CG_outputRepr * CreateStmtList(CG_outputRepr *singleton = NULL) const; - CG_outputRepr* - StmtListInsertLast(CG_outputRepr* list, CG_outputRepr* node) const; - CG_outputRepr* - StmtListAppend(CG_outputRepr* list1, CG_outputRepr* list2) const; - CG_outputRepr *CreateAddressOf(CG_outputRepr *op) const ; - CG_outputRepr *CreateBreakStatement(void) const; - CG_outputRepr *CreateStatementFromExpression(CG_outputRepr *exp) const; + CG_outputRepr * + StmtListInsertLast(CG_outputRepr *list, CG_outputRepr *node) const; + + CG_outputRepr * + StmtListAppend(CG_outputRepr *list1, CG_outputRepr *list2) const; + + CG_outputRepr *CreateAddressOf(CG_outputRepr *op) const; + + CG_outputRepr *CreateBreakStatement(void) const; + + CG_outputRepr *CreateStatementFromExpression(CG_outputRepr *exp) const; + + CG_outputRepr *CreateStruct(const std::string struct_name, + std::vector<std::string> data_members, + std::vector<CG_outputRepr *> data_types); - //--------------------------------------------------------------------------- - // Utility Functions - //--------------------------------------------------------------------------- - //CompoundStmt *StmtV2Compound(StmtList *slist) const // ?? - //{ return new (astContext_)CompoundStmt(*astContext_, &(*slist)[0], (*slist).size(), SourceLocation(), SourceLocation()); } + CG_outputRepr *CreateClassInstance(std::string name, CG_outputRepr *class_def); - //bool substitute(clang::Expr *in, std::string sym, clang::Expr* expr, clang::Expr *parent) const; + CG_outputRepr *lookup_member_data(CG_outputRepr *scope, std::string varName, CG_outputRepr *instance); - CG_outputRepr *CreateStruct(const std::string struct_name, - std::vector<std::string> data_members, - std::vector<CG_outputRepr *> data_types); + CG_outputRepr *CreatePointer(std::string &name) const; - CG_outputRepr *CreateClassInstance(std::string name , CG_outputRepr *class_def); - CG_outputRepr *lookup_member_data(CG_outputRepr* scope, std::string varName, CG_outputRepr *instance); - CG_outputRepr* CreatePointer(std::string &name) const; - CG_outputRepr* ObtainInspectorRange(const std::string &_s, const std::string &_name) const; + CG_outputRepr *ObtainInspectorRange(const std::string &_s, const std::string &_name) const; -}; + }; } // namespace omega diff --git a/lib/chillcg/include/code_gen/CG_chillRepr.h b/lib/chillcg/include/code_gen/CG_chillRepr.h index ea2048b..a2363c9 100755 --- a/lib/chillcg/include/code_gen/CG_chillRepr.h +++ b/lib/chillcg/include/code_gen/CG_chillRepr.h @@ -2,7 +2,6 @@ #ifndef CG_chillRepr_h #define CG_chillRepr_h -// Repr using chillAst internally #include <stdio.h> #include <string.h> #include <code_gen/CG_outputRepr.h> @@ -11,10 +10,8 @@ #define __STDC_CONSTANT_MACROS #endif - #include "chillAST/chillASTs.hh" - namespace omega { class CG_chillRepr : public CG_outputRepr { @@ -22,16 +19,8 @@ namespace omega { public: CG_chillRepr() { stmtclassname = strdup("NOTHING"); } - - char *type() const { return strdup("chill"); }; - // - std::vector<chillAST_Node *> chillnodes; // TODO make private - void printChillNodes() const { - for (int i = 0; i < chillnodes.size(); i++) - chillnodes[i]->print(); - fflush(stdout); - }; + std::vector<chillAST_Node *> chillnodes; CG_chillRepr(std::vector<chillAST_Node *> cnodes) { chillnodes = cnodes; @@ -53,12 +42,11 @@ namespace omega { void addStatement(chillAST_Node *s) { chillnodes.push_back(s); }; std::vector<chillAST_Node *> getChillCode() const { return chillnodes; }; - + //! Return the containing code chillAST_Node *GetCode(); - ~CG_chillRepr(); - + //! Creating a deep copy of this node CG_outputRepr *clone() const; void clear(); @@ -67,9 +55,7 @@ namespace omega { //--------------------------------------------------------------------------- // Dump operations //--------------------------------------------------------------------------- - void dump() const { printChillNodes(); }; - - void Dump() const; + void dump() const; private: char *stmtclassname; // chill diff --git a/lib/chillcg/src/CG_chillBuilder.cc b/lib/chillcg/src/CG_chillBuilder.cc index e926b64..dc02447 100755 --- a/lib/chillcg/src/CG_chillBuilder.cc +++ b/lib/chillcg/src/CG_chillBuilder.cc @@ -17,270 +17,55 @@ #include <iostream> #include <stack> #include <code_gen/CG_chillBuilder.h> +#include <code_gen/CGdebug.h> +#include <code_gen/codegen_error.h> namespace omega { // substitute at chill AST level - // forward declarations - chillAST_Node *substituteChill( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubABinaryOperator( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubUnaryOperator( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubDeclRefExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubArraySubscriptExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubImplicitCastExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubCStyleCastExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubParenExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubCallExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubReturnStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubIfStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubCompoundStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *SubMemberExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent ); - chillAST_Node *substituteChill( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { if (n == NULL) { - fprintf(stderr, "substituteChill() pointer n == NULL\n"); // DIE - int *crash = 0; - crash[0] = 1; + CG_ERROR(" pointer n == NULL\n"); exit(-1); } - //fprintf(stderr, "substituteChill() subbing statement of type "); - //fprintf(stderr, "%s\n", n->getTypeString()); - //if (n->isImplicitCastExpr()) { - // chillAST_ImplicitCastExpr *ICE = (chillAST_ImplicitCastExpr *) n; - // fprintf(stderr, "ICE subexpr type %s\n", ICE->subexpr->getTypeString()); - //} - //fprintf(stderr, "subbing '%s' in statement ", oldvar); n->print(0, stderr); fprintf(stderr, "\n"); - chillAST_Node *r = n; - if (n->isBinaryOperator()) {r= SubABinaryOperator(oldvar, newvar, n, parent ); - } else if (n->isUnaryOperator()) {r= SubUnaryOperator(oldvar, newvar, n, parent ); - } else if (n->isDeclRefExpr()) {r= SubDeclRefExpr(oldvar, newvar, n, parent ); - } else if (n->isArraySubscriptExpr()) {r=SubArraySubscriptExpr(oldvar, newvar, n, parent ); - } else if (n->isImplicitCastExpr()) {r= SubImplicitCastExpr(oldvar, newvar, n, parent ); - } else if (n->isParenExpr()) {r= SubParenExpr(oldvar, newvar, n, parent ); - } else if (n->isCStyleCastExpr()) {r= SubCStyleCastExpr(oldvar, newvar, n, parent ); - } else if (n->isReturnStmt()) {r= SubReturnStmt(oldvar, newvar, n, parent ); - } else if (n->isIfStmt()) {r= SubIfStmt(oldvar, newvar, n, parent ); - } else if (n->isCallExpr()) {r= SubCallExpr(oldvar, newvar, n, parent ); - } else if (n->isCompoundStmt()) {r= SubCompoundStmt(oldvar, newvar, n, parent ); - } else if (n->isMemberExpr()) {r= SubMemberExpr(oldvar, newvar, n, parent ); - - } else if (n->isFloatingLiteral()) { //fprintf(stderr, "sub in FL\n"); // do nothing - } else if (n->isIntegerLiteral()) { // do nothing - - } else { - fprintf(stderr, "\nCG_chillBuilder.cc substituteChill() UNHANDLED statement of type "); - n->dump(); printf(" "); n->print(); printf("\n"); fflush(stdout); - fprintf(stderr, "%s\n", n->getTypeString()); - exit(-1); + switch (n->getType()) { + case CHILLAST_NODE_BINARYOPERATOR: + case CHILLAST_NODE_UNARYOPERATOR: + case CHILLAST_NODE_FORSTMT: + case CHILLAST_NODE_IFSTMT: + case CHILLAST_NODE_COMPOUNDSTMT: + case CHILLAST_NODE_RETURNSTMT: + case CHILLAST_NODE_PARENEXPR: + case CHILLAST_NODE_CALLEXPR: + case CHILLAST_NODE_IMPLICITCASTEXPR: + case CHILLAST_NODE_CSTYLECASTEXPR: + case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: + case CHILLAST_NODE_MEMBEREXPR: + for (int i=0;i<n->getNumChildren();++i) + n->setChild(i,substituteChill(oldvar, newvar,n->getChild(i))); + break; + case CHILLAST_NODE_DECLREFEXPR: { + chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) n; + if (!strcmp( oldvar, DRE->declarationName)) { + std::vector<chillAST_Node*> newnodes = newvar->chillnodes; + chillAST_Node *firstn = newnodes[0]->clone(); + r = firstn; + } + } + break; + case CHILLAST_NODE_INTEGERLITERAL: + case CHILLAST_NODE_FLOATINGLITERAL: + // No op + break; + default: + CHILL_ERROR("UNHANDLED statement of type %s %s\n",n->getTypeString()); + exit(-1); } - - /* - if (isa<DeclStmt>(s)) { SubDeclStmt(oldvar, newvar, n, parent ); - } else if (isa<UnaryOperator>(s)) { SubUnaryOperator(oldvar, newvar, n, parent ); - } else if (isa<ForStmt>(s)) { SubLoop(oldvar, newvar, n, parent ); - */ - return r; } - - - chillAST_Node *SubABinaryOperator( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_BinaryOperator *b = (chillAST_BinaryOperator *) n; - //fprintf(stderr,"SubABinaryOperator() 0x%x subbing old variable %s in \n", b, oldvar); - - //fprintf(stderr,"SubABinaryOperator() subbing old variable %s in \n", oldvar); - //if (b->lhs!=NULL && b->rhs!=NULL) { - // b->print(); printf("\n"); fflush(stdout); - //} - - chillAST_Node *lhs = b->getLHS(); - chillAST_Node *rhs = b->getRHS(); - - //if (!strcmp(b->op, "=") && rhs->isBinaryOperator() ) { - // chillAST_BinaryOperator *r = (chillAST_BinaryOperator *) rhs; - // fprintf(stderr, "a(%p) = b(%p) %s c(%p)\n", lhs, r->lhs, r->op, r->rhs ); - //} - - //fprintf(stderr, "op %s rhs type ", b->op); - //fprintf(stderr, "%s\n", rhs->getTypeString()); - //rhs->dump(); printf("\n"); fflush(stdout); - - - b->setLHS(substituteChill( oldvar, newvar, lhs, b)); - b->setRHS(substituteChill( oldvar, newvar, rhs, b)); - return b; - } - - - - chillAST_Node *SubUnaryOperator( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_UnaryOperator *u = (chillAST_UnaryOperator *) n; - chillAST_Node *sub = u->getSubExpr(); - u->setSubExpr(substituteChill( oldvar, newvar, sub, u)); - return u; - } - - - chillAST_Node *SubDeclRefExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - //fprintf(stderr, "SubDeclRefExpr() subbing statement of type %s\n", n->getTypeString()); - - chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) n; - //const char *variable = DRE->declarationName; // should be the same as oldvar ?? - - //fprintf(stderr, "looking for oldvar %s in old DRE code ", oldvar); - //n->print(); printf("\n"); fflush(stdout); - - //fprintf(stderr, "old DRE name was %s\n", DRE->declarationName); - if (!strcmp( oldvar, DRE->declarationName)) { - //fprintf(stderr, "yep. replacing\n"); - - - //fprintf(stderr, "\nNEED TO REPLACE VARIABLE %s with new thing ", oldvar); - //newvar->printChillNodes(); - - - // newvar->Dump(); printf("\n"); fflush(stdout); - // //fprintf(stderr, " in statement of type %s\n",s->getTypeString()); - //} - - std::vector<chillAST_Node*> newnodes = newvar->chillnodes; - //fprintf(stderr, "%d nodes in newvar\n", newnodes.size()); - chillAST_Node *firstn = newnodes[0]; - firstn->parent = parent; - return firstn; // it's that simple! - - } - //else fprintf(stderr, "nope. not the right thing to replace\n\n"); - - - return DRE; // unchanged - } // subdeclrefexpr - - - - - - chillAST_Node *SubArraySubscriptExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_ArraySubscriptExpr *ASE = (chillAST_ArraySubscriptExpr *) n; - - //fprintf(stderr, "subASE ASE 0x%x\n", ASE); - //fprintf(stderr, "SubArraySubscriptExpr subbing old variable %s with new thing in ASE 0x%x ", oldvar, ASE); - - //ASE->print(); printf("\n"); fflush(stdout); - - chillAST_Node *Base = ASE->base; - chillAST_Node *Index = ASE->index; - //fprintf(stderr, "Index is of type %s\n", Index->getTypeString()); - - ASE->base = substituteChill( oldvar, newvar, Base, ASE); // this should not do anything - ASE->index = substituteChill( oldvar, newvar, Index, ASE); // this should - - //if (Index != ASE->index) { - // fprintf(stderr, "ASE was "); - // Base->print(); - // printf("["); - // Index->print(); - // printf("]\n"); - // printf("SWAPPED INDEX ASE 0x%x is ", ASE); ASE->print(); printf("\n"); fflush(stdout); - //} - //else fprintf(stderr, "ASE is "); ASE->print(); printf("\n"); fflush(stdout); - return ASE; - } - - - - chillAST_Node *SubImplicitCastExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - //fprintf(stderr, "SubImplicitCastExpr subbing statement of type %s at 0x%x parent 0x%x\n", n->getTypeString(), n, parent); - chillAST_ImplicitCastExpr *IC = (chillAST_ImplicitCastExpr *) n; - chillAST_Node *oldsub = IC->getSubExpr(); - IC->setSubExpr(substituteChill( oldvar, newvar, oldsub, IC)); - - //if (oldsub != IC->subexpr) { - //fprintf(stderr, "ImplicitCastExpr has CHANGED\n"); - //IC->print(); printf("\n"); fflush(stdout); - //fprintf(stderr, "ICE was "); - //oldsub->print(); - //printf("\nSWAPPED subexpr ICE 0x%x is ", IC); IC->print(); printf("\n"); fflush(stdout); - //fprintf(stderr, "PARENT 0x%x is now ",IC->parent); - //IC->parent->print(); printf("\n"); fflush(stdout); - //} - return IC; - } - - chillAST_Node *SubCStyleCastExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - //fprintf(stderr, "SubCStyleCastExpr() subexpr is type "); - chillAST_CStyleCastExpr *CSCE = (chillAST_CStyleCastExpr *) n; - //fprintf(stderr, "%s\n", CSCE->subexpr->getTypeString()); - CSCE->setSubExpr(substituteChill( oldvar, newvar, CSCE->getSubExpr(), CSCE)); - return CSCE; - } - - - chillAST_Node *SubParenExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_ParenExpr *PE = (chillAST_ParenExpr *) n; - PE->setSubExpr(substituteChill( oldvar, newvar, PE->getSubExpr(), PE)); - return PE; - } - - chillAST_Node *SubCallExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_CallExpr *CE = (chillAST_CallExpr *) n; - - int nargs = CE->getNumChildren(); - for (int i=1; i<nargs; i++) { - CE->setChild(i,substituteChill( oldvar, newvar, CE->getChild(i), CE)); - } - return CE; - } - - - - chillAST_Node *SubReturnStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_ReturnStmt *RS = (chillAST_ReturnStmt *)n; - if (RS->getRetVal()) RS->setRetVal(substituteChill(oldvar, newvar, RS->getRetVal(), RS)); - return RS; - } - - - chillAST_Node *SubIfStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - //fprintf(stderr, "SubIfStmt()\n"); - chillAST_IfStmt *IS = (chillAST_IfStmt *)n; - //IS->print(0, stderr); fprintf(stderr, "\n\n"); - chillAST_Node *sub; - sub = IS->getCond(); - if ( sub ) IS->setCond( substituteChill(oldvar, newvar, sub, IS)); - sub = IS->getThen(); - if ( sub ) IS->setThen( substituteChill(oldvar, newvar, sub, IS)); - sub = IS->getElse(); - if ( sub ) IS->setElse( substituteChill(oldvar, newvar, sub, IS)); - - return IS; - } - - - chillAST_Node *SubCompoundStmt( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_CompoundStmt *CS = (chillAST_CompoundStmt *)n; - - int numchildren = CS->getNumChildren(); - for (int i=0; i<numchildren; i++) { - CS->setChild( i, substituteChill(oldvar, newvar, CS->getChild(i), CS )); - } - - return CS; - } - - - - chillAST_Node *SubMemberExpr( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) { - chillAST_MemberExpr *ME = (chillAST_MemberExpr *)n; - ME->base = substituteChill(oldvar, newvar, ME->base, ME ); - return ME; - } - - - CG_chillBuilder::CG_chillBuilder() { toplevel = NULL; currentfunction = NULL; // not very useful @@ -370,151 +155,43 @@ namespace omega { const std::vector<std::string> &vars, std::vector<CG_outputRepr*> &subs, bool actuallyPrint) const { - - int numvars = vars.size(); + int numvars = vars.size(); int numsubs = subs.size(); - fflush(stdout); - fprintf(stderr, "\n\nin CG_xxxxBuilder.cc (OMEGA) CG_xxxxBuilder::CreateSubstitutedStmt()\n"); - fprintf(stderr, "%d vars and %d substitutions\n", numvars, (int)subs.size()); - - - if (numvars != numsubs) { - //fprintf(stderr, "umwut?\n"); exit(-1); - } - - - //{ - // std::vector<chillAST_Node*> nodes = ((CG_chillRepr *) stmt)->getChillCode(); - // // - // fprintf(stderr, "%d nodes in old code. was:\n", nodes.size()); - // for(int i=0; i<nodes.size(); i++) - // { - // printf("stmt(%d) = ",i); - // nodes[i]->print(); - // printf("\n"); - // } - // //printf("\nreally subbing in %d top level statements\n", nodes.size()); - // fflush(stdout); - //} - - //for (int i=0; i< numsubs; i++) { - // fprintf(stderr, "sub %d ", i); - // if (subs[i]) { ((CG_chillRepr *)subs[i])->Dump(); fflush( stdout ); } - // else { - // //int *crash = NULL; *crash = 1; - // fprintf(stderr, "(NULL error!)"); - // } - // //fprintf(stderr, "\n"); - //} - //fprintf(stderr, "\n"); - - + + if (numvars != numsubs) + throw codegen_error("Unequal number of original vars and subs"); + if (numsubs == 0) { - std::vector<chillAST_Node*> nodes = ((CG_chillRepr *) stmt)->getChillCode(); - - // - //fprintf(stderr, "nosubs old code was:\n"); - //for(int i=0; i<nodes.size(); i++) - // { - // printf("stmt = "); - // nodes[i]->print(); - // printf("\n"); - // } - //printf("\nreally subbing in %d top level statements\n", nodes.size()); - //fflush(stdout); - - - // no cloning !! return new CG_chillRepr( nodes ); - - //fprintf(stderr, "since nothing actually being substituted, this is just a clone\n"); - //fprintf(stderr, "old code was AND new code is:\n"); - //for (int i=0; i<nodes.size(); i++) { - // fprintf(stderr, "stmt = "); - // nodes[i]->print(); fflush(stdout); - // fprintf(stderr, "\n"); - //} - //fprintf(stderr, "cloning()\n"); - //return stmt->clone(); - } - - - //fprintf(stderr, "numsubs %d\n", numsubs); - - // debugging: print the substitutions we'll do - - //if (numsubs > 0) { - // for (int i=0; i< numsubs; i++) { - // fprintf(stderr, "subbing "); - // if (subs[i]) { - // ((CG_chillRepr *)subs[i])->Dump(); fflush( stdout ); - // fprintf(stderr, "for %s\n", vars[i].c_str() ); - //} else { - // //int *crash = NULL; *crash = 1; - // fprintf(stderr, "(NULL error!)"); - // } - // //fprintf(stderr, "\n"); - // } - // fprintf(stderr, "\n"); - //} - - - - //fprintf(stderr, "OK, now to really substitute ...\n"); - //CG_outputRepr *newstmt = stmt->clone(); - //CG_chillRepr *n = (CG_chillRepr *) newstmt; - //vector<chillAST_Node*> newnodes = n->getChillCode(); - CG_chillRepr *old = (CG_chillRepr *) stmt; std::vector<chillAST_Node*> oldnodes = old->getChillCode(); - - + for (int j=0; j<numsubs; j++) { if (subs[j] != NULL) { - - //fprintf(stderr, "substitution %d %s -> ", j,vars[j].c_str()); - //if (subs[j]) { ((CG_chillRepr *)subs[j])->Dump(); fflush( stdout ); } - - // find the type of thing we'll be using to replace the old variable CG_chillRepr *CRSub = (CG_chillRepr *)(subs[j]); std::vector<chillAST_Node*> nodes = CRSub->chillnodes; if (1 != nodes.size() ) { // always just one? - fprintf(stderr, "CG_chillBuilder::CreateSubstitutedStmt(), replacement is not one statement??\n"); + CG_ERROR("Replacement is not one statement\n"); exit(-1); } - chillAST_Node *node = nodes[0]; // always just one? - - for (int i=0; i<oldnodes.size(); i++) { - //fprintf(stderr, " statement %d ", i); - //oldnodes[i]->print(); printf("\n\n"); fflush(stdout); + for (int i=0; i<oldnodes.size(); i++) { oldnodes[i] = substituteChill( vars[j].c_str(), CRSub, oldnodes[i]); } } } - - //fprintf(stderr, "\ncode after substituting variables:\n"); - //for(int i=0; i<oldnodes.size(); ++i){ printf("stmt = ");oldnodes[i]->print();printf("\n");} - //fflush(stdout); - return new CG_chillRepr( oldnodes ); } - - - - //----------------------------------------------------------------------------- - // assignment generation - //----------------------------------------------------------------------------- - CG_outputRepr* CG_chillBuilder::CreateAssignment(int indent, + + CG_outputRepr* CG_chillBuilder::CreateAssignment(int indent, CG_outputRepr *lhs, CG_outputRepr *rhs) const { - //fprintf(stderr, "CG_chillBuilder::CreateAssignment()\n"); if(lhs == NULL || rhs == NULL) { - fprintf(stderr, "Code generation: Missing lhs or rhs\n"); + CG_ERROR("Code generation: Missing lhs or rhs\n"); return NULL; } @@ -535,9 +212,8 @@ namespace omega { CG_outputRepr* CG_chillBuilder::CreatePlusAssignment(int indent, // += CG_outputRepr *lhs, CG_outputRepr *rhs) const { - //fprintf(stderr, "CG_chillBuilder::CreatePlusAssignment()\n"); if(lhs == NULL || rhs == NULL) { - fprintf(stderr, "Code generation: Missing lhs or rhs\n"); + CHILL_ERROR("Code generation: Missing lhs or rhs\n"); return NULL; } @@ -559,107 +235,40 @@ namespace omega { // function invocation generation //----------------------------------------------------------------------------- CG_outputRepr* CG_chillBuilder::CreateInvoke(const std::string &fname, - std::vector<CG_outputRepr*> &list, - bool is_array) const { // WHAT is an array? - fprintf(stderr, "CG_roseBuilder::CreateInvoke( fname %s, ...)\n", fname.c_str()); - //fprintf(stderr, "%d things in list\n", list.size()); - - // debugging output. print the "call" - //fprintf(stderr, "%s", fname.c_str()); - //if (is_array) fprintf(stderr, "["); else fprintf(stderr, "("); - //int numparams = list.size(); - //for (int i=0; i<numparams; i++) { - // CG_chillRepr *CR = (CG_chillRepr *) list[i]; - // if (i) printf(","); - // printf(" "); - // CR->GetCode()->print(); - // fflush(stdout); - //} - //if (numparams) printf(" "); - //if (is_array) printf("]\n"); else printf(")\n"); - //fflush(stdout); - + std::vector<CG_outputRepr*> &list) const { + CG_DEBUG_PRINT("CG_roseBuilder::CreateInvoke( fname %s, ...)\n", fname.c_str()); - - if (is_array) { - //fprintf(stderr, "CG_chillBuilder::CreateInvoke() %s is_array\n", fname.c_str()); - const char *arrayname = fname.c_str(); - - CG_chillRepr *CR = (CG_chillRepr *) list[0]; - chillAST_Node *cast = CR->GetCode(); - - //fprintf(stderr, "%s[", arrayname); - //cast->print(); printf("] ???\n"); fflush(stdout); - - // find the array variable (scope ?? TODO) - chillAST_VarDecl *array = currentfunction->findArrayDecl( arrayname ); - if (!array) { - fprintf(stderr, "CG_chillBuilder::CreateInvoke(), can't find array %s\n", fname.c_str()); - } - - // make a declrefexpr that refers to vardecl of array ? - chillAST_DeclRefExpr *DRE = new chillAST_DeclRefExpr( array ); - chillAST_ArraySubscriptExpr *ASE = new chillAST_ArraySubscriptExpr( DRE, cast, NULL, NULL); - return new CG_chillRepr( ASE ); - } - - if (fname == std::string("max") || fname == std::string("min")) { if (list.size() == 0) { return NULL; } else if (list.size() == 1) { return list[1]; } else { - //fprintf(stderr, "else\n"); int last = list.size()-1; CG_outputRepr *CGOR; CG_chillRepr *CGCR; - - //fprintf(stderr, "going to create call to %s( ", fname.c_str()); - //for (int i=0; i<list.size(); i++) { - // CGCR = (CG_chillRepr*) list[i]; - // CGCR->chillnodes[0]->print(0, stderr); - // if (i<(list.size()-1)) fprintf(stderr, ", "); - //} - //fprintf(stderr, ")\n"); - char macroname[32]; char op; - if (fname == std::string("max")) op = '>'; else op = '<'; - - // TODO >, check number of args etc + // TODO >, check number of args etc chillAST_Node *ternary = lessthanmacro( ((CG_chillRepr*) list[0])->chillnodes[0], ((CG_chillRepr*) list[1])->chillnodes[0]); - - //fprintf(stderr, "just made ternary "); - //ternary->print(0, stdout); - - CG_chillRepr *repr = new CG_chillRepr( ternary ); - //fprintf(stderr, "returning callexpr with ternary\n", macroname); return repr; } } - //else { // special case for reduce? - //} else { - //do a simple function call - fprintf(stderr, "building a function call expression\n"); - // try to find the function name, for a function in this file const char *name = fname.c_str(); - //fprintf(stderr, "fname '%s'\n", name); chillAST_SourceFile *src = toplevel; // todo don't be dumb chillAST_Node *def = src->findCall(name); - if (!def) { // can't find it - fprintf(stderr, "CG_chillBuilder::CreateInvoke( %s ), can't find a function or macro by that name\n", name); + if (!def) { + CG_ERROR("CG_chillBuilder::CreateInvoke( %s ), can't find a function or macro by that name\n", name); exit(-1); } if (def->isMacroDefinition() || def->isFunctionDecl()) { chillAST_CallExpr *CE = new chillAST_CallExpr( def ); - CE->setParent(toplevel); int numparams = list.size(); for (int i=0; i<numparams; i++) { CG_chillRepr *CR = (CG_chillRepr *) list[i]; @@ -668,8 +277,6 @@ namespace omega { return new CG_chillRepr( CE ); } - // chillAST_CallExpr::chillAST_CallExpr(chillAST_Node *function, chillAST_Node *p ); - // todo addarg() //int numargs; //std::vector<class chillAST_Node*> args; diff --git a/lib/chillcg/src/CG_chillRepr.cc b/lib/chillcg/src/CG_chillRepr.cc index a47ecbc..044610c 100755 --- a/lib/chillcg/src/CG_chillRepr.cc +++ b/lib/chillcg/src/CG_chillRepr.cc @@ -14,114 +14,46 @@ #include <code_gen/CG_chillRepr.h> #include <stdio.h> -#include <stdlib.h> // for exit() +#include <stdlib.h> +#include <code_gen/CGdebug.h> namespace omega { CG_chillRepr::~CG_chillRepr() { } - //void CG_chillRepr::AppendStmt(Stmt *s) const { - // tree_node_list_->push_back(s); - //} - - //void CG_chillRepr::AppendV(StmtList *sl) const { - // for(int i=0; i<sl->size(); ++i) tree_node_list_->push_back((*sl)[i]); - //} - - chillAST_Node * CG_chillRepr::GetCode() { - //fprintf(stderr, "CG_chillRepr::GetCode() this %p size %d\n", this, chillnodes.size()); - - if (0 == chillnodes.size()) return NULL; // error? - - if (1 == chillnodes.size()) return chillnodes[0]; - - // make a compoundstatement with all the code ??? probably should be that way already - fprintf(stderr, "CG_chillRepr GetCode(), multiple (%d) statements in the code??\n", chillnodes.size()); - for (int i=0; i<chillnodes.size(); i++) { - fprintf(stderr, "chillnode %d %p\n", i, chillnodes[i] ); + if (0 == chillnodes.size()) { + CG_ERROR("No contained chillnodes\n"); + return NULL; // error? } + if (1 == chillnodes.size()) return chillnodes[0]; + CG_DEBUG_PRINT("Creating a compound statements\n"); chillAST_CompoundStmt *CS = new chillAST_CompoundStmt(); for (int i=0; i<chillnodes.size(); i++) { CS->addChild( chillnodes[i] ); } return CS; } - - - CG_outputRepr* CG_chillRepr::clone() const { // make a deep/shallow COPY of all the nodes - //fprintf(stderr, "CG_chillRepr::clone() %d chill nodes\n", chillnodes.size()); - //for (int i=0; i<chillnodes.size(); i++) { chillnodes[i]->print(); printf("\n"); } fflush(stdout); - - CG_chillRepr *newrepr = new CG_chillRepr(); // empty + CG_outputRepr* CG_chillRepr::clone() const { + CG_chillRepr *newrepr = new CG_chillRepr(); - // deep ( nothing is the same as in the source ) - for (int i=0; i<chillnodes.size(); i++) newrepr->addStatement( chillnodes[i]->clone() ); + for (int i=0; i<chillnodes.size(); i++) + newrepr->addStatement( chillnodes[i]->clone() ); - // shallow (the submembers are the same ) - //for (int i=0; i<chillnodes.size(); i++) newrepr->addStatement( chillnodes[i] ); - //fprintf(stderr, "done cloning\n"); - return newrepr; + return newrepr; } - - void CG_chillRepr::clear() { chillnodes.clear(); } - - - // TODO this is duplicated and shouldn't be here anyway - static std::string binops[] = { - " ", " ", // BO_PtrMemD, BO_PtrMemI, // [C++ 5.5] Pointer-to-member operators. - "*", "/", "%", // BO_Mul, BO_Div, BO_Rem, // [C99 6.5.5] Multiplicative operators. - "+", "-", // BO_Add, BO_Sub, // [C99 6.5.6] Additive operators. - "<<", ">>", // BO_Shl, BO_Shr, // [C99 6.5.7] Bitwise shift operators. - "<", ">", "<=", ">=", // BO_LT, BO_GT, BO_LE, BO_GE, // [C99 6.5.8] Relational operators. - "==", "!=", // BO_EQ, BO_NE, // [C99 6.5.9] Equality operators. - "&", // BO_And, // [C99 6.5.10] Bitwise AND operator. - "??", // BO_Xor, // [C99 6.5.11] Bitwise XOR operator. - "|", // BO_Or, // [C99 6.5.12] Bitwise OR operator. - "&&", // BO_LAnd, // [C99 6.5.13] Logical AND operator. - "||", // BO_LOr, // [C99 6.5.14] Logical OR operator. - "=", "*=", // BO_Assign, BO_MulAssign, // [C99 6.5.16] Assignment operators. - "/=", "%=", // BO_DivAssign, BO_RemAssign, - "+=", "-=", // BO_AddAssign, BO_SubAssign, - "???", "???", // BO_ShlAssign, BO_ShrAssign, - "&&=", "???", // BO_AndAssign, BO_XorAssign, - "||=", // BO_OrAssign, - ","}; // BO_Comma // [C99 6.5.17] Comma operator. - - - static std::string unops[] = { - "++", "--", // [C99 6.5.2.4] Postfix increment and decrement - "++", "--", // [C99 6.5.3.1] Prefix increment and decrement - "@", "*", // [C99 6.5.3.2] Address and indirection - "+", "-", // [C99 6.5.3.3] Unary arithmetic - "~", "!", // [C99 6.5.3.3] Unary arithmetic - "__real", "__imag", // "__real expr"/"__imag expr" Extension. - "__extension" // __extension__ marker. - }; - - - - //void CG_chillRepr::dump() const { Dump(); } + void CG_chillRepr::dump() const { + for (int i = 0; i < chillnodes.size(); i++) + chillnodes[i]->print(0,stdout); + } - void CG_chillRepr::Dump() const { - CG_chillRepr *me = (CG_chillRepr *)this; // ?? - //fprintf(stderr, "repr of type "); - //fprintf(stderr, "%s\n", this->type()); - int numnodes = me->chillnodes.size(); - //fprintf(stderr, "repr %p %d nodes\n", this, numnodes); - for (int i=0; i<numnodes; i++) { - me->chillnodes[i]->print(); printf("\n"); fflush(stdout); - } - return; - } - } // namespace diff --git a/lib/codegen/include/code_gen/CG_outputBuilder.h b/lib/codegen/include/code_gen/CG_outputBuilder.h index cb0cd5d..58f8a2f 100644 --- a/lib/codegen/include/code_gen/CG_outputBuilder.h +++ b/lib/codegen/include/code_gen/CG_outputBuilder.h @@ -91,12 +91,10 @@ namespace omega { * @brief function invoation generation * @param funcName * @param argList - * @param is_array * @return */ virtual CG_outputRepr *CreateInvoke(const std::string &funcName, - std::vector<CG_outputRepr *> &argList, - bool is_array = false) const = 0; + std::vector<CG_outputRepr *> &argList) const = 0; /*! * @brief comment generation * @param indent diff --git a/lib/codegen/include/code_gen/CG_stringBuilder.h b/lib/codegen/include/code_gen/CG_stringBuilder.h index 390039a..ce33bde 100644 --- a/lib/codegen/include/code_gen/CG_stringBuilder.h +++ b/lib/codegen/include/code_gen/CG_stringBuilder.h @@ -21,7 +21,7 @@ public: CG_stringRepr *CreateSubstitutedStmt(int indent, CG_outputRepr *stmt, const std::vector<std::string> &vars, std::vector<CG_outputRepr *> &subs, bool actuallyPrint) const; CG_stringRepr *CreateAssignment(int indent, CG_outputRepr *lhs, CG_outputRepr *rhs) const; CG_stringRepr *CreatePlusAssignment(int indent, CG_outputRepr *lhs, CG_outputRepr *rhs) const; - CG_stringRepr *CreateInvoke(const std::string &funcName, std::vector<CG_outputRepr *> &argList,bool is_array=false) const; + CG_stringRepr *CreateInvoke(const std::string &funcName, std::vector<CG_outputRepr *> &argList) const; CG_stringRepr *CreateComment(int indent, const std::string &commentText) const; CG_stringRepr* CreateAttribute(CG_outputRepr *control, const std::string &commentText) const; diff --git a/lib/codegen/include/code_gen/CGdebug.h b/lib/codegen/include/code_gen/CGdebug.h new file mode 100644 index 0000000..cf72c36 --- /dev/null +++ b/lib/codegen/include/code_gen/CGdebug.h @@ -0,0 +1,31 @@ +#ifndef CGDEBUG_H +#define CGDEBUG_H + +#include <libgen.h> +#include <string.h> +#include <stdlib.h> + +#ifndef NDEBUG // means that CMAKE_BUILD_TYPE=Debug +#define DEBUGCODEGEN +#endif +// This thing below potentially create leaks +#define FILENAME basename(strdup(__FILE__)) + +#ifdef DEBUGCODEGEN +#define CG_DEBUG_PRINT(format,args...) fprintf(stderr,"%15s | %15s | LN%-4d:\t" format,FILENAME,__FUNCTION__, \ + __LINE__, ##args ) +#define CG_DEBUG_BEGIN { \ + fprintf(stderr,"=========\t%15s, %15s, LN%-4d\t=========\n",FILENAME,__FUNCTION__,__LINE__); +#define CG_DEBUG_END fprintf(stderr,"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");} +#else +#define CG_DEBUG_PRINT(format,args...) do {} while(0) /* Don't do anything */ +#define CG_DEBUG_BEGIN while(0) { +#define CG_DEBUG_END } +#endif + +// TODO below should be substituted by some error throwing? to be more consistent with cpp style +#define CG_ERROR(format,args...) fprintf(stderr,"ERROR:\t%s, %s, LN%d:\t" format,FILENAME,__FUNCTION__, \ + __LINE__, ##args ) + + +#endif diff --git a/lib/codegen/src/CG_stringBuilder.cc b/lib/codegen/src/CG_stringBuilder.cc index d0f6693..7a19f66 100755 --- a/lib/codegen/src/CG_stringBuilder.cc +++ b/lib/codegen/src/CG_stringBuilder.cc @@ -182,11 +182,9 @@ namespace omega { CG_stringRepr *CG_stringBuilder::CreateInvoke(const std::string &funcName, - std::vector<CG_outputRepr *> &list, - bool is_array) const { + std::vector<CG_outputRepr *> &list) const { fprintf(stderr, "CG_stringBuilder::CreateInvoke( %s, ..., is_array ", funcName.c_str()); - if (is_array) fprintf(stderr, " true )\n"); - else fprintf(stderr, " false )\n"); + fprintf(stderr, " false )\n"); std::string listStr = ""; diff --git a/src/ast/node.cpp b/src/ast/node.cpp index c8a3c08..6117f91 100644 --- a/src/ast/node.cpp +++ b/src/ast/node.cpp @@ -364,3 +364,26 @@ chillAST_Node* chillAST_Node::findContainingStmt() { return p->findContainingStmt(); } +chillAST_Node *chillAST_Node::getEnclosingStatement(int level) { // TODO do for subclasses? + if (isArraySubscriptExpr()) return parent->getEnclosingStatement(level + 1); + + if (level != 0) { + if (isBinaryOperator() || + isUnaryOperator() || + isTernaryOperator() || + isReturnStmt() || + isCallExpr() + ) + return this; + // things that are not endpoints. recurse through parent + if (isMemberExpr()) return parent->getEnclosingStatement(level + 1); + if (isImplicitCastExpr()) return parent->getEnclosingStatement(level + 1); + if (isSizeof()) return parent->getEnclosingStatement(level + 1); + if (isCStyleCastExpr()) return parent->getEnclosingStatement(level + 1); + return NULL; + } + CHILL_ERROR("level %d type %s, returning NULL\n", level, getTypeString()); + exit(-1); + return NULL; +} + diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 5324ef2..5eed0fe 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -1233,147 +1233,85 @@ class chillAST_Node *chillAST_TernaryOperator::clone() { chillAST_Node *c = getCond()->clone(); chillAST_Node *l = getLHS()->clone(); chillAST_Node *r = getRHS()->clone(); - chillAST_TernaryOperator *to = new chillAST_TernaryOperator(op, l, r, parent); + chillAST_TernaryOperator *to = new chillAST_TernaryOperator(op, c, l, r); to->isFromSourceFile = isFromSourceFile; filename = NULL; return to; } +chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr() { + children.push_back(NULL); // Base + children.push_back(NULL); // Index + basedecl = NULL; +} + chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr(chillAST_Node *bas, chillAST_Node *indx, bool writtento, - void *unique) { - base = index = NULL; - basedecl = NULL; //fprintf(stderr, "setting basedecl NULL for ASE %p\n", this); + void *unique):chillAST_ArraySubscriptExpr() { imwrittento = writtento; // ?? imreadfrom = false; // ?? - parent = NULL; - metacomment = NULL; if (bas) { - if (bas->isImplicitCastExpr()) base = ((chillAST_ImplicitCastExpr *) bas)->getSubExpr(); // probably wrong - else base = bas; - base->setParent(this); + if (bas->isImplicitCastExpr()) setBase(((chillAST_ImplicitCastExpr *) bas)->getSubExpr()); // probably wrong + else setBase(bas); basedecl = multibase(); } if (indx) { - if (indx->isImplicitCastExpr()) index = ((chillAST_ImplicitCastExpr *) indx)->getSubExpr(); // probably wrong - else index = indx; - index->setParent(this); + if (indx->isImplicitCastExpr()) setIndex(((chillAST_ImplicitCastExpr *) indx)->getSubExpr()); // probably wrong + else setIndex(indx); } uniquePtr = unique; } -chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr(chillAST_VarDecl *v, std::vector<chillAST_Node *> indeces) { - //fprintf(stderr, "\nchillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr() 4\n"); - //fprintf(stderr,"chillAST_ArraySubscriptExpr( chillAST_VarDecl *v, std::vector<int> indeces)\n"); - //if (parent == NULL) { - // fprintf(stderr, "dammit. ASE %p has no parent\n", this); - //} - - +chillAST_ArraySubscriptExpr::chillAST_ArraySubscriptExpr(chillAST_VarDecl *v, std::vector<chillAST_Node *> indeces):chillAST_ArraySubscriptExpr() { int numindeces = indeces.size(); for (int i = 0; i < numindeces; i++) { fprintf(stderr, "ASE index %d ", i); indeces[i]->print(0, stderr); fprintf(stderr, "\n"); - // printf("["); - // indeces[i]->print(); - // printf("]"); } - //fflush(stdout); - //fprintf(stderr, "\n"); - chillAST_DeclRefExpr *DRE = new chillAST_DeclRefExpr(v->vartype, v->varname, v); basedecl = v; // ?? - //fprintf(stderr, "%p ASE 3 basedecl = %p ", this, basedecl); - //fprintf(stderr, "of type %s\n", basedecl->getTypeString()); - //basedecl->print(); printf("\n"); - //basedecl->dump(); printf("\n"); fflush(stdout); - //fprintf(stderr, "basedecl varname %s\n", basedecl->varname); chillAST_ArraySubscriptExpr *rent = this; // parent for subnodes // these are on the top level ASE that we're creating here - base = (chillAST_Node *) DRE; - index = indeces[numindeces - 1]; - - base->setParent(this); - index->setParent(this); + setBase(DRE); + setIndex(indeces[numindeces - 1]); for (int i = numindeces - 2; i >= 0; i--) { chillAST_ArraySubscriptExpr *ASE = new chillAST_ArraySubscriptExpr(DRE, indeces[i], rent, 0); - rent->base = ASE; // - rent = ASE; + rent->setBase(ASE); } imwrittento = false; imreadfrom = false; - //fprintf(stderr, "ASE is "); print(); printf("\n\n"); fflush(stdout); - isFromSourceFile = true; // default + isFromSourceFile = true; filename = NULL; - - //fprintf(stderr, "\nASE %p parent %p ", this, parent); print(0,stderr); fprintf(stderr, "\n\n"); -} - - -chillAST_Node *chillAST_Node::getEnclosingStatement(int level) { // TODO do for subclasses? - - //fprintf(stderr, "chillAST_Node::getEnclosingStatement( level %d ) node type %s\n", level, getTypeString()); - //print(); printf("\n"); fflush(stdout); - - // so far, user will ONLY call this directly on an array subscript expression - if (isArraySubscriptExpr()) return parent->getEnclosingStatement(level + 1); - - if (level != 0) { - if (isBinaryOperator() || - isUnaryOperator() || - isTernaryOperator() || - isReturnStmt() || - isCallExpr() - ) - return this; - - - // things that are not endpoints. recurse through parent - if (isMemberExpr()) return parent->getEnclosingStatement(level + 1); - if (isImplicitCastExpr()) return parent->getEnclosingStatement(level + 1); - if (isSizeof()) return parent->getEnclosingStatement(level + 1); - if (isCStyleCastExpr()) return parent->getEnclosingStatement(level + 1); - return NULL; - } - - CHILL_ERROR("level %d type %s, returning NULL\n", level, getTypeString()); - exit(-1); - - return NULL; } - void chillAST_ArraySubscriptExpr::gatherIndeces(std::vector<chillAST_Node *> &ind) { - if (base->isArraySubscriptExpr()) ((chillAST_ArraySubscriptExpr *) base)->gatherIndeces(ind); - ind.push_back(index); + if (getBase()->isArraySubscriptExpr()) ((chillAST_ArraySubscriptExpr*)getBase())->gatherIndeces(ind); + ind.push_back(getIndex()); } chillAST_VarDecl *chillAST_ArraySubscriptExpr::multibase() { - //this should probably be a chillAST_Node function instead of having all these ifs - return base->multibase(); + return getBase()->multibase(); } chillAST_Node *chillAST_ArraySubscriptExpr::getIndex(int dim) { CHILL_DEBUG_PRINT("chillAST_ArraySubscriptExpr::getIndex( %d )\n", dim); - - chillAST_Node *b = base; - + chillAST_Node *b = getBase(); int depth = 0; std::vector<chillAST_Node *> ind; - chillAST_Node *curindex = index; + chillAST_Node *curindex = getIndex(); for (;;) { if (b->getType() == CHILLAST_NODE_IMPLICITCASTEXPR) b = ((chillAST_ImplicitCastExpr *) b)->getSubExpr(); else if (b->getType() == CHILLAST_NODE_ARRAYSUBSCRIPTEXPR) { ind.push_back(curindex); - curindex = ((chillAST_ArraySubscriptExpr *) b)->index; - b = ((chillAST_ArraySubscriptExpr *) b)->base; + curindex = ((chillAST_ArraySubscriptExpr *) b)->getIndex(); + b = ((chillAST_ArraySubscriptExpr *) b)->getBase(); depth++; } else { ind.push_back(curindex); @@ -1383,124 +1321,27 @@ chillAST_Node *chillAST_ArraySubscriptExpr::getIndex(int dim) { return ind[depth - dim]; } - -class chillAST_Node *chillAST_ArraySubscriptExpr::constantFold() { - base = base->constantFold(); - index = index->constantFold(); - return this; -} - class chillAST_Node *chillAST_ArraySubscriptExpr::clone() { - if (base->isDeclRefExpr()) { - chillAST_VarDecl *vd = (chillAST_VarDecl *) (((chillAST_DeclRefExpr *) base)->decl); - } - chillAST_Node *b = base->clone(); - - chillAST_Node *i = index->clone(); - + chillAST_Node *b = getBase()->clone(); + chillAST_Node *i = getIndex()->clone(); chillAST_ArraySubscriptExpr *ASE = new chillAST_ArraySubscriptExpr(b, i, imwrittento, uniquePtr); ASE->setParent(parent); - ASE->imreadfrom = false; // don't know this yet - ASE->isFromSourceFile = isFromSourceFile; if (filename) ASE->filename = strdup(filename); return ASE; } void chillAST_ArraySubscriptExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { - //fprintf(stderr, "chillAST_ArraySubscriptExpr::gatherArrayRefs setting imwrittento %d for ", writtento); -//fprintf(stderr, "%s ", base->getTypeString()); -//base->print(); printf("\n"); fflush(stdout); - - //fprintf(stderr, "found an array subscript. &refs 0x%x ", refs); - if (!imwrittento) imwrittento = writtento; // may be both written and not for += - fflush(stdout); - - //fprintf(stderr, "recursing on index "); index->print(0,stderr); fprintf(stderr, "\n"); - index->gatherArrayRefs(refs, 0); // recurse first - //fprintf(stderr, "adding this "); print(0,stderr); fprintf(stderr, "\n"); - //fprintf(stderr, "refs[%d] = 0x%x = ", refs.size(), this); print(); fflush(stdout); + if (!imwrittento) imwrittento = writtento; // may be both written and not for += TODO Purpose unclear + getIndex()->gatherArrayRefs(refs, 0); // recurse first refs.push_back(this); - - //fprintf(stderr, " size now %d\n", refs.size()); - } void chillAST_ArraySubscriptExpr::gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento) { - index->gatherScalarRefs(refs, 0); -} - -void chillAST_ArraySubscriptExpr::gatherVarDecls(vector<chillAST_VarDecl *> &decls) { - //fprintf(stderr, "chillAST_ArraySubscriptExpr::gatherVarDecls()\n"); - - base->gatherVarDecls(decls); - index->gatherVarDecls(decls); -} - - -void chillAST_ArraySubscriptExpr::gatherScalarVarDecls(vector<chillAST_VarDecl *> &decls) { - //fprintf(stderr, "chillAST_ArraySubscriptExpr::gatherScalarVarDecls()\n"); - //fprintf(stderr, "base %s index %s\n", base->getTypeString(), index->getTypeString()); - base->gatherScalarVarDecls(decls); - index->gatherScalarVarDecls(decls); -} - - -void chillAST_ArraySubscriptExpr::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decls) { - //fprintf(stderr, "chillAST_ArraySubscriptExpr::gatherArrayVarDecls()\n"); - //fprintf(stderr, "base %s index %s\n", base->getTypeString(), index->getTypeString()); - base->gatherArrayVarDecls(decls); - index->gatherArrayVarDecls(decls); -} - - -void chillAST_ArraySubscriptExpr::gatherDeclRefExprs(vector<chillAST_DeclRefExpr *> &refs) { - base->gatherDeclRefExprs(refs); - index->gatherDeclRefExprs(refs); -} - - -void chillAST_ArraySubscriptExpr::gatherVarUsage(vector<chillAST_VarDecl *> &decls) { - base->gatherVarUsage(decls); - index->gatherVarUsage(decls); -} - - -void chillAST_ArraySubscriptExpr::replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl) { - base->replaceVarDecls(olddecl, newdecl); - index->replaceVarDecls(olddecl, newdecl); + chillAST_Node::gatherScalarRefs(refs, false); } - -void chillAST_ArraySubscriptExpr::replaceChild(chillAST_Node *old, chillAST_Node *newchild) { - - if (old == index) { - index = newchild; - index->parent = this; - return; - } - - if (old == base) { - base = newchild; - base->parent = this; - return; - } - - CHILL_DEBUG_BEGIN - fprintf(stderr, "chillAST_ArraySubscriptExpr::replaceChild() old is not base or index\n"); - print(0, stderr); - fprintf(stderr, "\nchild: "); - if (!old) fprintf(stderr, "oldchild NULL!\n"); - old->print(0, stderr); - fprintf(stderr, "\nnew: "); - newchild->print(0, stderr); - fprintf(stderr, "\n"); - CHILL_DEBUG_END - exit(-1); // make easier for gdb -}; - - bool chillAST_ArraySubscriptExpr::operator!=(const chillAST_ArraySubscriptExpr &other) { bool opposite = *this == other; return !opposite; @@ -1511,99 +1352,32 @@ bool chillAST_ArraySubscriptExpr::operator==(const chillAST_ArraySubscriptExpr & return this->uniquePtr == other.uniquePtr; } - +chillAST_MemberExpr::chillAST_MemberExpr() { + children.push_back(NULL); +} chillAST_MemberExpr::chillAST_MemberExpr(chillAST_Node *bas, const char *mem, void *unique, - CHILLAST_MEMBER_EXP_TYPE t) { - base = bas; - if (bas) - base->setParent(this); + CHILLAST_MEMBER_EXP_TYPE t):chillAST_MemberExpr() { + setBase(bas); if (mem) member = strdup(mem); else member = NULL; uniquePtr = unique; exptype = t; - return; // ignore tests below ?? TODO ?? - - - // base needs to RESOLVE to a decl ref expr but may not BE one - // A.b . c lhs is a binop or memberexpr - - if (bas->isBinaryOperator()) { - //fprintf(stderr, "checking binop to see if it resolved to a declrefexpr\n"); - // cheat for now or just remove the check below - return; - } - - if (!(bas->isDeclRefExpr() || bas->isArraySubscriptExpr())) { - fprintf(stderr, "chillAST_MemberExpr::chillAST_MemberExpr(), base is of type %s\n", bas->getTypeString()); - fprintf(stderr, "chillAST_MemberExpr::chillAST_MemberExpr(), base is not DeclRefExpr\n"); - - base->print(); - printf(".%s\n", mem); - fflush(stdout); - exit(-1); - } + return; } // TODO member can be another member expression, Right? -class chillAST_Node *chillAST_MemberExpr::constantFold() { - base = base->constantFold(); - //member = member->constantFold(); - return this; -} - class chillAST_Node *chillAST_MemberExpr::clone() { - chillAST_Node *b = base->clone(); - char *m = strdup(member); // ?? - chillAST_MemberExpr *ME = new chillAST_MemberExpr(b, m, uniquePtr /* ?? */ ); + chillAST_Node *b = getBase()->clone(); + char *m = strdup(member); + chillAST_MemberExpr *ME = new chillAST_MemberExpr(b, m, uniquePtr); ME->isFromSourceFile = isFromSourceFile; if (filename) ME->filename = strdup(filename); return ME; } -void chillAST_MemberExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { - fprintf(stderr, "chillAST_MemberExpr::gatherArrayRefs() "); - print(0, stderr); - fprintf(stderr, "\n"); - fprintf(stderr, "base of of type %s\n", base->getTypeString()); - base->gatherArrayRefs(refs, writtento); // - -} - -void chillAST_MemberExpr::gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento) { - base->gatherScalarRefs(refs, writtento); -} - -void chillAST_MemberExpr::gatherVarDecls(vector<chillAST_VarDecl *> &decls) { - base->gatherVarDecls(decls); -} - -void chillAST_MemberExpr::gatherScalarVarDecls(vector<chillAST_VarDecl *> &decls) { - base->gatherScalarVarDecls(decls); -} - - -void chillAST_MemberExpr::gatherArrayVarDecls(vector<chillAST_VarDecl *> &decls) { - base->gatherArrayVarDecls(decls); -} - - -void chillAST_MemberExpr::gatherDeclRefExprs(vector<chillAST_DeclRefExpr *> &refs) { - base->gatherDeclRefExprs(refs); -} - - -void chillAST_MemberExpr::gatherVarUsage(vector<chillAST_VarDecl *> &decls) { - base->gatherVarUsage(decls); -} - - -void chillAST_MemberExpr::replaceVarDecls(chillAST_VarDecl *olddecl, chillAST_VarDecl *newdecl) { - base->replaceVarDecls(olddecl, newdecl); -} - bool chillAST_MemberExpr::operator!=(const chillAST_MemberExpr &other) { bool opposite = *this == other; return !opposite; @@ -1613,28 +1387,10 @@ bool chillAST_MemberExpr::operator==(const chillAST_MemberExpr &other) { return this->uniquePtr == other.uniquePtr; } - -void chillAST_MemberExpr::replaceChild(chillAST_Node *old, chillAST_Node *newchild) { - //printf("\nMemberExpr::replaceChild( )\n"); - //printf("old: "); - //old->print(); - //printf("\nnew: "); - //newchild->print(); - //printf("\n"); fflush(stdout); - - // will pointers match?? - if (base == old) { - //fprintf(stderr, "old matches base of MemberExpr\n"); - base = newchild; - } else { - base->replaceChild(old, newchild); - } -} - chillAST_Node *chillAST_MemberExpr::multibase2() { /*fprintf(stderr, "ME MB2\n" );*/ return (chillAST_Node *) this; } chillAST_VarDecl *chillAST_MemberExpr::getUnderlyingVarDecl() { - fprintf(stderr, "chillAST_MemberExpr:getUnderlyingVarDecl()\n"); + CHILL_ERROR("chillAST_MemberExpr:getUnderlyingVarDecl()\n"); print(); exit(-1); // find the member with the correct name @@ -1643,18 +1399,7 @@ chillAST_VarDecl *chillAST_MemberExpr::getUnderlyingVarDecl() { chillAST_VarDecl *chillAST_MemberExpr::multibase() { - //c.i[c.count] we want i member of c - //fprintf(stderr, "ME MB\n" ); - - //fprintf(stderr, "chillAST_MemberExpr::multibase()\n"); - //print(); printf("\n"); fflush(stdout); - //fprintf(stderr, "MemberExpr base is type %s, member %s\n", base->getTypeString(), member); - - //chillAST_VarDecl *vd = base->getUnderlyingVarDecl(); // this is the only thing that ever calls this ??? - chillAST_VarDecl *vd = base->multibase(); // ?? - - - //fprintf(stderr, "vd "); vd->print(); printf("\n"); fflush(stdout); + chillAST_VarDecl *vd = getBase()->multibase(); // ?? chillAST_RecordDecl *rd = vd->getStructDef(); if (!rd) { @@ -1665,22 +1410,14 @@ chillAST_VarDecl *chillAST_MemberExpr::multibase() { vd->dump(); exit(-1); } - // OK, we have the recorddecl that defines the structure // now find the member with the correct name chillAST_VarDecl *sub = rd->findSubpart(member); - //fprintf(stderr, "sub %s:\n", member); if (!sub) { fprintf(stderr, "can't find member %s in \n", member); rd->print(); } - //sub->print(); printf("\n"); fflush(stdout); - //sub->dump() ; printf("\n"); fflush(stdout); - return sub; - //find vardecl of member in def of base - - } chillAST_DeclRefExpr::chillAST_DeclRefExpr(const char *vartype, const char *varname, chillAST_Node *d) { diff --git a/src/ir_clang.cc b/src/ir_clang.cc index 2346673..671f195 100755 --- a/src/ir_clang.cc +++ b/src/ir_clang.cc @@ -741,18 +741,18 @@ chillAST_NodeList* ConvertTranslationUnit(TranslationUnitDecl *TUD, char *filena DeclContext::decl_iterator end = DC->decls_end(); for (DeclContext::decl_iterator DI = start; DI != end; ++DI) { Decl *D = *DI; + chillAST_Node *child; // Skip internal declarations of clang - if (D->isImplicit()) continue; if (isa<FunctionDecl>(D)) { - topnode->addChild(unwrap(ConvertFunctionDecl(dyn_cast<FunctionDecl>(D)))); + child = unwrap(ConvertFunctionDecl(dyn_cast<FunctionDecl>(D))); } else if (isa<VarDecl>(D)) { - topnode->addChild(unwrap(ConvertVarDecl(dyn_cast<VarDecl>(D)))); + child = unwrap(ConvertVarDecl(dyn_cast<VarDecl>(D))); } else if (isa<TypedefDecl>(D)) { - topnode->addChild(unwrap(ConvertTypeDefDecl(dyn_cast<TypedefDecl>(D)))); + child = unwrap(ConvertTypeDefDecl(dyn_cast<TypedefDecl>(D))); } else if (isa<RecordDecl>(D)) { CHILL_DEBUG_PRINT("\nTUD RecordDecl\n"); - topnode->addChild(unwrap(ConvertRecordDecl(dyn_cast<RecordDecl>(D)))); + child = unwrap(ConvertRecordDecl(dyn_cast<RecordDecl>(D))); } else if (isa<TypeAliasDecl>(D)) { CHILL_ERROR("TUD TypeAliasDecl TODO \n"); exit(-1); @@ -760,6 +760,8 @@ chillAST_NodeList* ConvertTranslationUnit(TranslationUnitDecl *TUD, char *filena CHILL_ERROR("\nTUD a declaration of type %s (%d) which I can't handle\n", D->getDeclKindName(), D->getKind()); exit(-1); } + topnode -> addChild(child); + if (D->isImplicit()) child->isFromSourceFile = false; } NL_RET(topnode); @@ -2256,36 +2258,23 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { chillAST_Node *par; switch (old->type()) { case IR_CONTROL_LOOP: { - //fprintf(stderr, "old is IR_CONTROL_LOOP\n"); cloop = (struct IR_chillLoop *) old; chillAST_ForStmt *forstmt = cloop->chillforstmt; - fprintf(stderr, "old was\n"); - forstmt->print(); - printf("\n"); - fflush(stdout); - - //fprintf(stderr, "\nnew code is\n"); - //for (int i=0; i<numnew; i++) { newcode[i]->print(); printf("\n"); } - //fflush(stdout); - - par = forstmt->parent; if (!par) { - fprintf(stderr, "old parent was NULL\n"); - fprintf(stderr, "ir_clang.cc that will not work very well.\n"); + CHILL_ERROR("old parent was NULL\n"); + CHILL_ERROR("ir_clang.cc that will not work very well.\n"); exit(-1); } - - fprintf(stderr, "\nold parent was\n\n{\n"); - par->print(); - printf("\n"); - fflush(stdout); - fprintf(stderr, "\n}\n"); + CHILL_DEBUG_BEGIN + fprintf(stderr, "\nold parent was\n\n{\n"); + par->print(); + fprintf(stderr, "\n}\n"); + CHILL_DEBUG_END std::vector<chillAST_Node *> *oldparentcode = par->getChildren(); // probably only works for compoundstmts - //fprintf(stderr, "ir_clang.cc oldparentcode\n"); // find loop in the parent int index = -1; @@ -2295,32 +2284,25 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { fprintf(stderr, "ir_clang.cc can't find the loop in its parent\n"); exit(-1); } - //fprintf(stderr, "loop is index %d\n", index); - // insert the new code par->setChild(index, newcode[0]); // overwrite old stmt - //fprintf(stderr, "inserting %s 0x%x as index %d of 0x%x\n", newcode[0]->getTypeString(), newcode[0], index, par); - // do we need to update the IR_cloop? + // do we need to update the IR_cloop? cloop->chillforstmt = (chillAST_ForStmt *) newcode[0]; // ?? DFL - - - //printf("inserting "); newcode[0]->print(); printf("\n"); if (numnew > 1) { - //oldparentcode.insert( oldparentcode.begin()+index+1, numnew-1, NULL); // allocate in bulk - // add the rest of the new statements - for (int i = 1; i < numnew; i++) { - printf("inserting "); - newcode[i]->print(); - printf("\n"); + CHILL_DEBUG_BEGIN + for (int i = 1; i < numnew; i++) { + fprintf(stderr, "inserting \n"); + newcode[i]->print(0, stderr); + } + CHILL_DEBUG_END + for (int i = 1; i < numnew; i++) par->insertChild(index + i, newcode[i]); // sets parent - } } // TODO add in (insert) variable declarations that go with the new loops - fflush(stdout); } break; @@ -2335,16 +2317,11 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { } fflush(stdout); - //fprintf(stderr, "\nafter inserting %d statements into the Clang IR,", numnew); CHILL_DEBUG_BEGIN fprintf(stderr, "new parent2 is\n\n{\n"); std::vector<chillAST_Node *> *newparentcode = par->getChildren(); for (int i = 0; i < newparentcode->size(); i++) { - fflush(stdout); - //fprintf(stderr, "%d ", i); (*newparentcode)[i]->print(); - printf(";\n"); - fflush(stdout); } fprintf(stderr, "}\n"); CHILL_DEBUG_END diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp index 738db47..d0f8030 100644 --- a/src/printer/cfamily.cpp +++ b/src/printer/cfamily.cpp @@ -21,9 +21,9 @@ bool ifSemicolonFree(CHILLAST_NODE_TYPE t) { } void CFamily::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) { - print(ident, n->base, o); + print(ident, n->getBase(), o); o << "["; - print(ident, n->index, o); + print(ident, n->getIndex(), o); o << "]"; } @@ -266,7 +266,7 @@ void CFamily::printS(std::string ident, chillAST_Malloc *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); + if (n->getBase()) printPrec(ident, n->getBase(), o, prec); else o << "(NULL)"; if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "->"; else o << "."; diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp index f3fe2e5..cd4c316 100644 --- a/src/printer/dump.cpp +++ b/src/printer/dump.cpp @@ -57,8 +57,8 @@ void Dump::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostrea else o << "lvalue "; } else o << "rvalue "; - print(ident, n->base, o); - print(ident, n->index, o); + print(ident, n->getBase(), o); + print(ident, n->getIndex(), o); } void Dump::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) { @@ -166,7 +166,7 @@ void Dump::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) { } void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) { - print(ident, n->base, o); + print(ident, n->getBase(), o); if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "-> "; else o << ". "; o << n->member << " "; diff --git a/src/transformations/loop.cc b/src/transformations/loop.cc index 5b1b80b..53bbf67 100644 --- a/src/transformations/loop.cc +++ b/src/transformations/loop.cc @@ -795,13 +795,13 @@ bool Loop::init_loop(std::vector<ir_tree_node *> &ir_tree, CG_outputRepr *code = static_cast<IR_Block *>(ir_stmt[loc]->content)->extract(); fprintf(stderr, "code = ocg->CreateSubstitutedStmt(...)\n"); - ((CG_chillRepr *) code)->Dump(); + ((CG_chillRepr *) code)->dump(); fflush(stdout); code = ocg->CreateSubstitutedStmt(0, code, vars_to_be_reversed, reverse_expr); fprintf(stderr, "stmt\n"); - ((CG_chillRepr *) code)->Dump(); + ((CG_chillRepr *) code)->dump(); fflush(stdout); stmt[loc].code = code; |