diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chillASTs.cc | 267 | ||||
-rwxr-xr-x | src/ir_clang.cc | 29 | ||||
-rw-r--r-- | src/printer/cfamily.cpp (renamed from src/printers/cfamily.cpp) | 2 | ||||
-rw-r--r-- | src/printer/dump.cpp | 226 | ||||
-rw-r--r-- | src/printer/generic.cpp | 200 | ||||
-rw-r--r-- | src/printers/dump.cpp | 160 |
6 files changed, 476 insertions, 408 deletions
diff --git a/src/chillASTs.cc b/src/chillASTs.cc index c86a563..be0f5d5 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -4,6 +4,7 @@ #include <chilldebug.h> #include <stack> #include "chillAST.h" +#include "printer/dump.h" using namespace std; @@ -395,18 +396,6 @@ void chillAST_SourceFile::printToFile(char *filename) { } - -void chillAST_SourceFile::dump(int indent, FILE *fp) { - fflush(fp); - fprintf(fp, "\n//CHILL AST originally from file '%s'\n", SourceFileName); - int numchildren = children.size(); - for (int i = 0; i < numchildren; i++) { - children[i]->dump(indent, fp); - } - fflush(fp); -}; - - chillAST_MacroDefinition *chillAST_SourceFile::findMacro(const char *name) { int numMacros = macrodefinitions.size(); for (int i = 0; i < numMacros; i++) { @@ -639,6 +628,20 @@ void chillAST_RecordDecl::printStructure(int indent, FILE *fp) { fflush(fp); } +void chillAST_FunctionDecl::printParameterTypes( FILE *fp ) { // also prints names + //fprintf(stderr, "\n\n%s chillAST_FunctionDecl::printParameterTypes()\n", functionName); + fprintf(fp, "( "); + int numparameters = parameters->size(); + for (int i=0; i<numparameters; i++) { + if (i!=0) fprintf(fp, ", "); + chillAST_VarDecl *p = (*parameters)[i]; + p->print(0, fp); // note: no indent, as this is in the function parens + } + fprintf(fp, " )"); // end of input parameters + +} + + chillAST_FunctionDecl::chillAST_FunctionDecl(const char *rt, const char *fname, void *unique) { CHILL_DEBUG_PRINT("chillAST_FunctionDecl::chillAST_FunctionDecl with unique %p\n", unique); if (fname) @@ -1256,8 +1259,8 @@ void chillAST_ForStmt::addSyncs() { if (parent->isCompoundStmt()) { //fprintf(stderr, "ForStmt parent is CompoundStmt 0x%x\n", parent); - vector<chillAST_Node *> chillin = parent->getChildren(); - int numc = chillin.size(); + vector<chillAST_Node *> *chillin = parent->getChildren(); + int numc = chillin->size(); //fprintf(stderr, "ForStmt parent is CompoundStmt 0x%x with %d children\n", parent, numc); for (int i = 0; i < numc; i++) { if (this == parent->getChild(i)) { @@ -1270,7 +1273,7 @@ void chillAST_ForStmt::addSyncs() { } chillin = parent->getChildren(); - int nowc = chillin.size(); + int nowc = chillin->size(); //fprintf(stderr, "old, new number of children = %d %d\n", numc, nowc); } else { @@ -1673,29 +1676,6 @@ chillAST_IntegerLiteral *chillAST_BinaryOperator::evalAsIntegerLiteral() { return new chillAST_IntegerLiteral(evalAsInt()); // ?? } -void chillAST_BinaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(BinaryOperator '%s'\n", op); - - if (lhs) lhs->dump(indent + 1, fp); // lhs could be null - else { - chillindent(indent + 1, fp); - fprintf(fp, "(NULL)\n"); - } - fflush(fp); - - if (rhs) rhs->dump(indent + 1, fp); // rhs could be null - else { - chillindent(indent + 1, fp); - fprintf(fp, "(NULL)\n"); - } - fflush(fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - void chillAST_BinaryOperator::print(int indent, FILE *fp) { printPreprocBEFORE(indent, fp); @@ -1950,17 +1930,6 @@ chillAST_TernaryOperator::chillAST_TernaryOperator(const char *oper, chillAST_No if (rhs) rhs->setParent(this); } -void chillAST_TernaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(TernaryOperator '%s'\n", op); - condition->dump(indent + 1, fp); - lhs->dump(indent + 1, fp); - rhs->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - void chillAST_TernaryOperator::print(int indent, FILE *fp) { printPreprocBEFORE(indent, fp); chillindent(indent, fp); @@ -2195,45 +2164,6 @@ void chillAST_ArraySubscriptExpr::gatherIndeces(std::vector<chillAST_Node *> &in ind.push_back(index); } - -void chillAST_ArraySubscriptExpr::dump(int indent, FILE *fp) { -// fprintf(stderr, "\n%p chillAST_ArraySubscriptExpr::dump() basedecl %p\n", basedecl); - - char *local; - if (basedecl && basedecl->vartype) { - local = strdup(basedecl->vartype); - } else { - fprintf(stderr, "%p chillAST_ArraySubscriptExpr::dump(), no basedecl ???\n", this); - local = strdup(""); - //fprintf(stderr, "base is "); base->dump(); printf("\n"); base->print(); printf("\n"); fflush(stdout); - //print(); printf("\n"); fflush(stdout); - } - - - char *space = rindex(local, ' '); // can't use index because it's a class member! - if (space) *space = '\0'; // turn "float *" into "float" - - chillindent(indent, fp); - //fprintf(fp, "(ArraySubscriptExpr '%s' ", local); - if (basedecl) { - //fprintf(stderr, " chillAST_ArraySubscriptExpr::dump() basedecl is of type %s\n", basedecl->getTypeString()); - fprintf(fp, "(ArraySubscriptExpr (%s) '%s' ", basedecl->varname, local); - } else fprintf(stderr, " chillAST_ArraySubscriptExpr::dump() has no basedecl\n"); - free(local); - - if (imwrittento) { - if (imreadfrom) fprintf(fp, "lvalue AND rvalue\n"); - else fprintf(fp, "lvalue\n"); - } else fprintf(fp, "rvalue\n"); - base->dump(indent + 1, fp); - index->dump(indent + 1, fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - - void chillAST_ArraySubscriptExpr::print(int indent, FILE *fp) { base->print(indent, fp); fprintf(fp, "["); @@ -2955,13 +2885,6 @@ void chillAST_IntegerLiteral::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_IntegerLiteral::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(IntegerLiteral 'int' %d)\n", value); - fflush(fp); -} - - class chillAST_Node *chillAST_IntegerLiteral::constantFold() { return this; } // can never do anything @@ -3089,19 +3012,6 @@ void chillAST_FloatingLiteral::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_FloatingLiteral::dump(int indent, FILE *fp) { - chillindent(indent, fp); - // 2x2 cases ??? - if (precision == 1) - fprintf(fp, "(FloatingLiteral 'float' "); - else fprintf(fp, "(FloatingLiteral 'double' "); - - if (float0double1 == 0) fprintf(fp, "%f)\n", value); // %f gives enough digits - else fprintf(fp, "%f)\n", doublevalue); // %f gives enough digits - fflush(fp); -} - - chillAST_Node *chillAST_FloatingLiteral::constantFold() { return this; }; // NOOP chillAST_Node *chillAST_FloatingLiteral::clone() { @@ -3142,20 +3052,6 @@ void chillAST_UnaryOperator::print(int indent, FILE *fp) { fflush(fp); } - -void chillAST_UnaryOperator::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(UnaryOperator "); - if (prefix) fprintf(fp, "prefix "); - else fprintf(fp, "postfix "); - fprintf(fp, "%s\n", op); - subexpr->dump(indent + 1, fp); - - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - - void chillAST_UnaryOperator::gatherVarLHSUsage(vector<chillAST_VarDecl *> &decls) { if ((!strcmp("++", op)) || (!strcmp("--", op))) { subexpr->gatherVarUsage(decls); // do all unary modify the subexpr? (no, - ) @@ -3377,16 +3273,6 @@ void chillAST_CStyleCastExpr::print(int indent, FILE *fp) { fflush(fp); }; - -void chillAST_CStyleCastExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CStyleCastExpr (%s) \n", towhat); - subexpr->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - class chillAST_Node *chillAST_CStyleCastExpr::constantFold() { subexpr = subexpr->constantFold(); return this; @@ -3450,15 +3336,6 @@ void chillAST_CStyleAddressOf::print(int indent, FILE *fp) { fflush(fp); }; -void chillAST_CStyleAddressOf::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CStyleAddressOf \n"); - subexpr->print(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); - fflush(fp); -} - class chillAST_Node *chillAST_CStyleAddressOf::constantFold() { subexpr = subexpr->constantFold(); return this; @@ -3635,12 +3512,6 @@ void chillAST_CudaFree::print(int indent, FILE *fp) { fflush(fp); }; -void chillAST_CudaFree::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CudaFree %s )\n", variable->varname); - fflush(fp); -}; - class chillAST_Node *chillAST_CudaFree::constantFold() { return this; } @@ -3765,13 +3636,6 @@ void chillAST_CudaSyncthreads::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_CudaSyncthreads::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(syncthreads)\n"); - fflush(fp); -} - - chillAST_ReturnStmt::chillAST_ReturnStmt(chillAST_Node *retval) { returnvalue = retval; if (returnvalue) returnvalue->setParent(this); @@ -3899,29 +3763,6 @@ void chillAST_CallExpr::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_CallExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CallExpr "); - //fprintf(stderr, "callee type %s\n", callee->getTypeString()); - chillAST_FunctionDecl *fd = NULL; - if (callee->isDeclRefExpr()) { // always? - chillAST_DeclRefExpr *dre = (chillAST_DeclRefExpr *) callee; - fd = dre->getFunctionDecl(); // if NULL, we've got a Vardecl instead - if (fd) { - //fd->print(); - fprintf(fp, "%s\n", fd->returnType); - } - - callee->dump(indent + 1, fp); - if (fd) { - int numparams = fd->getSymbolTable()->size(); - for (int i = 0; i < numparams; i++) (*(fd->getSymbolTable()))[i]->dump(indent + 1, fp); - } - } - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - void chillAST_CallExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { for (int i = 0; i < args.size(); i++) { args[i]->gatherArrayRefs(refs, writtento); @@ -4422,24 +4263,6 @@ void chillAST_VarDecl::printName(int in, FILE *fp) { fprintf(fp, "%s", varname); }; - -void chillAST_VarDecl::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(VarDecl \"'%s' '%s' '%s'\" n_dim %d ) ", vartype, varname, arraypart, numdimensions); - - //fprintf(fp, "vardef %p\n", vardef); - //if (vardef) fprintf(fp, "(typedef or struct!)\n"); - //fprintf(fp, "typedefinition %p\n", typedefinition); - //if (isStruct) fprintf(fp, "isStruct\n"); - - //if (isAParameter) fprintf(fp, "PARAMETER\n"); - //else fprintf(fp, "NOT PARAMETER\n"); - fflush(fp); - - //exit(-1); // see what called this -}; - - chillAST_RecordDecl *chillAST_VarDecl::getStructDef() { if (vardef) return vardef; if (typedefinition) return typedefinition->getStructDef(); @@ -4562,27 +4385,6 @@ void chillAST_CompoundStmt::loseLoopWithLoopVar(char *var) { //fprintf(stderr, "CompoundStmt node 0x%x done recursing\n", this ); } - -void chillAST_CompoundStmt::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(CompoundStmt \n"); - int numchildren = children.size(); - - //for (int i=0; i<numchildren; i++) { - // fprintf(fp, "%d %s 0x%x\n", i, children[i]->getTypeString(), children[i]); - //} - //fprintf(fp, "\n"); - - for (int i = 0; i < numchildren; i++) { - children[i]->dump(indent + 1, fp); - fprintf(fp, "\n"); // ??? - fflush(fp); - } - chillindent(indent, fp); - fprintf(fp, ")\n"); -}; - - chillAST_Node *chillAST_CompoundStmt::constantFold() { //fprintf(stderr, "chillAST_CompoundStmt::constantFold()\n"); for (int i = 0; i < children.size(); i++) children[i] = children[i]->constantFold(); @@ -4714,15 +4516,6 @@ void chillAST_ParenExpr::print(int indent, FILE *fp) { fflush(fp); } -void chillAST_ParenExpr::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(ParenExpr \n"); - subexpr->dump(indent + 1, fp); - chillindent(indent, fp); - fprintf(fp, ")\n"); -} - - void chillAST_ParenExpr::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) { subexpr->gatherArrayRefs(refs, writtento); } @@ -4786,12 +4579,6 @@ void chillAST_Sizeof::print(int indent, FILE *fp) { fflush(fp); } - -void chillAST_Sizeof::dump(int indent, FILE *fp) { - chillindent(indent, fp); - fprintf(fp, "(Sizeof %s )\n", thing); -} - void chillAST_Sizeof::gatherArrayRefs(std::vector<chillAST_ArraySubscriptExpr *> &refs, bool writtento) {} void chillAST_Sizeof::gatherScalarRefs(std::vector<chillAST_DeclRefExpr *> &refs, bool writtento) {} @@ -4852,9 +4639,9 @@ void insertNewDeclAtLocationOfOldIfNeeded(chillAST_VarDecl *newdecl, chillAST_Va // find actual location of old decl and insert new one there //fprintf(stderr, "oldparent is of type %s\n", oldparent->getTypeString()); // better be compoundstmt ?? - vector<chillAST_Node *> children = oldparent->getChildren(); + vector<chillAST_Node *> *children = oldparent->getChildren(); - int numchildren = children.size(); + int numchildren = children->size(); //fprintf(stderr, "oldparent has %d children\n", numchildren); if (numchildren == 0) { @@ -4871,11 +4658,11 @@ void insertNewDeclAtLocationOfOldIfNeeded(chillAST_VarDecl *newdecl, chillAST_Va for (int i = 0; i < numchildren; i++) { chillAST_Node *child = oldparent->getChild(i); //fprintf(stderr, "child %d @ 0x%x is of type %s\n", i, child, child->getTypeString()); - if (children[i] == olddecl) { + if ((*children)[i] == olddecl) { index = i; //fprintf(stderr, "found old decl at index %d\n", index); } - if (children[i] == newdecl) { + if ((*children)[i] == newdecl) { newalreadythere = true; //fprintf(stderr, "new already there @ index %d\n", i); } @@ -5353,4 +5140,14 @@ void chillAST_Node::addParameter(chillAST_VarDecl *vd) { chillAST_VarDecl* chillAST_Node::getParameter(const char *t) { return symbolTableFindName(getParameters(),t); +} + +void chillAST_Node::dump(int indent, FILE *fp) { + if (fp == stderr) { + chill::printer::Dump d; + d.print("",this,std::cerr); + } else { + chill::printer::Dump d; + d.print("",this,std::cout); + } }
\ No newline at end of file diff --git a/src/ir_clang.cc b/src/ir_clang.cc index 44ab3ee..f348099 100755 --- a/src/ir_clang.cc +++ b/src/ir_clang.cc @@ -2250,6 +2250,11 @@ IR_clangCode::IR_clangCode(const char *fname, const char *proc_name) : IR_Code() pInstance->setCurrentFunction(localFD); + CHILL_DEBUG_BEGIN + fprintf(stderr, "Dumping AST for localFD\n"); + localFD->dump(); + CHILL_DEBUG_END + chillAST_Node *b = localFD->getBody(); // we do this just because it will get done next CHILL_DEBUG_PRINT("calling new CG_chillBuilder() umwut?\n"); @@ -2600,7 +2605,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl // bundled up into an IR_Block // ifs and loops will get their own entry - std::vector<chillAST_Node *> children; + std::vector<chillAST_Node *> *children; if (blockast->getType() == CHILLAST_NODE_FORSTMT) { @@ -2639,7 +2644,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl children = blockast->getChildren(); } - int numchildren = children.size(); + int numchildren = children->size(); //fprintf(stderr, "basic block has %d statements\n", numchildren); //fprintf(stderr, "basic block is:\n"); //fprintf(stderr, "{\n"); @@ -2650,7 +2655,7 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl IR_chillBlock *basicblock = new IR_chillBlock(this); // no statements for (int i = 0; i < numchildren; i++) { //fprintf(stderr, "child %d is of type %s\n", i, children[i]->getTypeString()); - CHILLAST_NODE_TYPE typ = children[i]->getType(); + CHILLAST_NODE_TYPE typ = (*children)[i]->getType(); if (typ == CHILLAST_NODE_LOOP) { if (numchildren == 1) { CHILL_DEBUG_PRINT("found a For statement (Loop)\n"); @@ -2667,16 +2672,16 @@ std::vector<IR_Control *> IR_clangCode::FindOneLevelControlStructure(const IR_Bl } CHILL_DEBUG_PRINT("pushing the loop at %d\n", i); - controls.push_back(new IR_chillLoop(this, (chillAST_ForStmt *) children[i])); + controls.push_back(new IR_chillLoop(this, (chillAST_ForStmt *) (*children)[i])); } //else if (typ == CHILLAST_NODE_IFSTMT ) // TODO else { // straight line code - basicblock->addStatement(children[i]); + basicblock->addStatement((*children)[i]); CHILL_DEBUG_BEGIN fprintf(stderr, "straight line code\n"); fprintf(stderr, "child %d = \n", i); - children[i]->print(); + (*children)[i]->print(); printf("\n"); fflush(stdout); fprintf(stderr, "child %d is part of a basic block\n", i); @@ -2869,13 +2874,13 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { fflush(stdout); fprintf(stderr, "\n}\n"); - std::vector<chillAST_Node *> oldparentcode = par->getChildren(); // probably only works for compoundstmts + std::vector<chillAST_Node *> *oldparentcode = par->getChildren(); // probably only works for compoundstmts //fprintf(stderr, "ir_clang.cc oldparentcode\n"); // find loop in the parent int index = -1; - int numstatements = oldparentcode.size(); - for (int i = 0; i < numstatements; i++) if (oldparentcode[i] == forstmt) { index = i; } + int numstatements = oldparentcode->size(); + for (int i = 0; i < numstatements; i++) if ((*oldparentcode)[i] == forstmt) { index = i; } if (index == -1) { fprintf(stderr, "ir_clang.cc can't find the loop in its parent\n"); exit(-1); @@ -2923,11 +2928,11 @@ void IR_clangCode::ReplaceCode(IR_Control *old, omega::CG_outputRepr *repr) { //fprintf(stderr, "\nafter inserting %d statements into the Clang IR,", numnew); CHILL_DEBUG_BEGIN fprintf(stderr, "new parent2 is\n\n{\n"); - std::vector<chillAST_Node *> newparentcode = par->getChildren(); - for (int i = 0; i < newparentcode.size(); i++) { + std::vector<chillAST_Node *> *newparentcode = par->getChildren(); + for (int i = 0; i < newparentcode->size(); i++) { fflush(stdout); //fprintf(stderr, "%d ", i); - newparentcode[i]->print(); + (*newparentcode)[i]->print(); printf(";\n"); fflush(stdout); } diff --git a/src/printers/cfamily.cpp b/src/printer/cfamily.cpp index 305feee..8bfeb82 100644 --- a/src/printers/cfamily.cpp +++ b/src/printer/cfamily.cpp @@ -2,7 +2,7 @@ // Created by ztuowen on 9/24/16. // -#include "printers/cfamily.h" +#include "printer/cfamily.h" using namespace std; using namespace chill::printer; diff --git a/src/printer/dump.cpp b/src/printer/dump.cpp new file mode 100644 index 0000000..2b2a35c --- /dev/null +++ b/src/printer/dump.cpp @@ -0,0 +1,226 @@ +// +// Created by ztuowen on 9/24/16. +// + +#include "printer/dump.h" +#include "chillAST.h" + +using namespace chill::printer; +using namespace std; + +void dumpVector(GenericPrinter *p, string ident, chillAST_NodeList *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} +void dumpVector(GenericPrinter *p, string ident, chillAST_SymbolTable *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} +void dumpVector(GenericPrinter *p, string ident, chillAST_TypedefTable *n, ostream &o) { + for (int i = 0; i < n->size(); ++i) + p->print("", (*n)[i], o); +} + +void Dump::print(string ident, chillAST_Node *n, ostream &o) { + o << "(" << n->getTypeString() << " "; + if (n->getParameters()) { + o << "(Params: "; + dumpVector(this, ident, n->getParameters(), o); + o << ") "; + } + if (n->getSymbolTable()) { + o << "(VarScope: "; + dumpVector(this, ident, n->getSymbolTable(), o); + o << ") "; + } + if (n->getTypedefTable()) { + o << "(TypeDef: "; + dumpVector(this, ident, n->getTypedefTable(), o); + o << ") "; + } + o << ": "; + // Recurse + GenericPrinter::print(ident, n, o); + o << ") "; +} + +void Dump::printS(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostream &o) { + if (n->basedecl) + o << "(" << n->basedecl->varname << ") "; + if (n->basedecl && n->basedecl->vartype) + o << n->basedecl->vartype; + if (n->imwrittento) { + if (n->imreadfrom) + o << "lvalue AND rvalue "; + else + o << "lvalue "; + } else o << "rvalue "; + print(ident, n->base, o); + print(ident, n->index, o); +} + +void Dump::printS(std::string ident, chillAST_BinaryOperator *n, std::ostream &o) { + o << n->op << " "; + if (n->lhs) print(ident, n->lhs, o); + else o << "(NULL) "; + if (n->rhs) print(ident, n->rhs, o); + else o << "(NULL) "; +} + +void Dump::printS(std::string ident, chillAST_CallExpr *n, std::ostream &o) { + if (n->callee) + print(ident, n->callee, o); +} + +void Dump::printS(std::string ident, chillAST_CompoundStmt *n, std::ostream &o) { + dumpVector(this, ident, n->getChildren(), o); +} + +void Dump::printS(std::string ident, chillAST_CStyleAddressOf *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_CStyleCastExpr *n, std::ostream &o) { + o << n->towhat << " "; + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_CudaFree *n, std::ostream &o) { + o << n->variable->varname << " "; +} + +void Dump::printS(std::string ident, chillAST_CudaKernelCall *n, std::ostream &o) { + CHILL_ERROR("Not implemented"); +} + +void Dump::printS(std::string ident, chillAST_CudaMalloc *n, std::ostream &o) { + print(ident, n->devPtr, o); + print(ident, n->sizeinbytes, o); +} + +void Dump::printS(std::string ident, chillAST_CudaMemcpy *n, std::ostream &o) { + o << n->cudaMemcpyKind << " "; + print(ident, n->dest, o); + print(ident, n->src, o); + print(ident, n->size, o); +} + +void Dump::printS(std::string ident, chillAST_CudaSyncthreads *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_DeclRefExpr *n, std::ostream &o) { + chillAST_VarDecl *vd = n->getVarDecl(); + if (vd) + if (vd->isAParameter) o << "ParmVar "; else o << "Var "; + o << n->declarationName << " "; + chillAST_FunctionDecl *fd = n->getFunctionDecl(); + if (fd) dumpVector(this, ident, fd->getParameters(), o); +} + +void Dump::printS(std::string ident, chillAST_FloatingLiteral *n, std::ostream &o) { + if (n->precision == 1) o << "float "; + else o << "double "; + if (n->float0double1) o << n->value; + else o << n->doublevalue; +} + +void Dump::printS(std::string ident, chillAST_ForStmt *n, std::ostream &o) { + print(ident, n->init, o); + print(ident, n->cond, o); + print(ident, n->incr, o); + print(ident, n->body, o); +} + +void Dump::printS(std::string ident, chillAST_Free *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_FunctionDecl *n, std::ostream &o) { + if (n->filename) o << n->filename << " "; + if (n->isFromSourceFile) o << "FromSourceFile" << " "; + o << n->returnType << " " << n->functionName << " "; + if (n->getBody()) print(ident, n->getBody(), o); +} + +void Dump::printS(std::string ident, chillAST_IfStmt *n, std::ostream &o) { + print(ident, n->cond, o); + print(ident, n->thenpart, o); + if (n->elsepart) + print(ident, n->elsepart, o); +} + +void Dump::printS(std::string ident, chillAST_IntegerLiteral *n, std::ostream &o) { + o << n->value << " "; +} + +void Dump::printS(std::string ident, chillAST_ImplicitCastExpr *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_MacroDefinition *n, std::ostream &o) { + o << n->macroName << " "; + dumpVector(this, ident, n->getParameters(), o); + print(ident, n->getBody(), o); + if (n->rhsString) o << " (aka " << n->rhsString << ") "; +} + +void Dump::printS(std::string ident, chillAST_Malloc *n, std::ostream &o) { + print(ident, n->sizeexpr, o); +} + +void Dump::printS(std::string ident, chillAST_MemberExpr *n, std::ostream &o) { + print(ident, n->base, o); + if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "-> "; + else o << ". "; + o << n->member << " "; +} + +void Dump::printS(std::string ident, chillAST_NULL *n, std::ostream &o) { + o << "(NULL) "; +} + +void Dump::printS(std::string ident, chillAST_NoOp *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_ParenExpr *n, std::ostream &o) { + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_Preprocessing *n, std::ostream &o) {} + +void Dump::printS(std::string ident, chillAST_RecordDecl *n, std::ostream &o) { + // TODO access control + o << n->getName() << " "; + o << n->isAStruct() << " "; + o << n->isAUnion() << " "; +} + +void Dump::printS(std::string ident, chillAST_ReturnStmt *n, std::ostream &o) { + if (n->returnvalue) print(ident, n->returnvalue, o); +} + +void Dump::printS(std::string ident, chillAST_Sizeof *n, std::ostream &o) { + o << n->thing << " "; +} + +void Dump::printS(std::string ident, chillAST_SourceFile *n, std::ostream &o) { + dumpVector(this, ident, n->getChildren(), o); +} + +void Dump::printS(std::string ident, chillAST_TypedefDecl *n, std::ostream &o) { + o << n->underlyingtype << " " << n->newtype << " " << n->arraypart << " "; +} + +void Dump::printS(std::string ident, chillAST_TernaryOperator *n, std::ostream &o) { + o << n->op << " "; + print(ident, n->condition, o); + print(ident, n->lhs, o); + print(ident, n->rhs, o); +} + +void Dump::printS(std::string ident, chillAST_UnaryOperator *n, std::ostream &o) { + if (n->prefix) o << "prefix "; + else o << "postfix "; + print(ident, n->subexpr, o); +} + +void Dump::printS(std::string ident, chillAST_VarDecl *n, std::ostream &o) { + o << "\"'" << n->vartype << "' '" << n->varname << "' '" << n->arraypart << "'\" dim " << n->numdimensions << " "; +} + diff --git a/src/printer/generic.cpp b/src/printer/generic.cpp new file mode 100644 index 0000000..67c694f --- /dev/null +++ b/src/printer/generic.cpp @@ -0,0 +1,200 @@ +// +// Created by ztuowen on 9/24/16. +// + +#include "printer/generic.h" + +using namespace chill::printer; + +void GenericPrinter::print(std::string ident, chillAST_Node *n, std::ostream &o) { + switch (n->getType()) { + case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: + printS(ident, dynamic_cast<chillAST_ArraySubscriptExpr *>(n), o); + break; + case CHILLAST_NODE_BINARYOPERATOR: + printS(ident, dynamic_cast<chillAST_BinaryOperator *>(n), o); + break; + case CHILLAST_NODE_CALLEXPR: + printS(ident, dynamic_cast<chillAST_CallExpr *>(n), o); + break; + case CHILLAST_NODE_COMPOUNDSTMT: + printS(ident, dynamic_cast<chillAST_CompoundStmt *>(n), o); + break; + case CHILLAST_NODE_CSTYLEADDRESSOF: + printS(ident, dynamic_cast<chillAST_CStyleAddressOf *>(n), o); + break; + case CHILLAST_NODE_CSTYLECASTEXPR: + printS(ident, dynamic_cast<chillAST_CStyleCastExpr *>(n), o); + break; + case CHILLAST_NODE_CUDAFREE: + printS(ident, dynamic_cast<chillAST_CudaFree *>(n), o); + break; +// case CHILLAST_NODE_CUDAKERNELCALL: +// printS(ident, dynamic_cast<chillAST_CudaKernelCall *>(n), o); +// break; + case CHILLAST_NODE_CUDAMALLOC: + printS(ident, dynamic_cast<chillAST_CudaMalloc *>(n), o); + break; + case CHILLAST_NODE_CUDAMEMCPY: + printS(ident, dynamic_cast<chillAST_CudaMemcpy *>(n), o); + break; + case CHILLAST_NODE_CUDASYNCTHREADS: + printS(ident, dynamic_cast<chillAST_CudaSyncthreads *>(n), o); + break; + case CHILLAST_NODE_DECLREFEXPR: + printS(ident, dynamic_cast<chillAST_DeclRefExpr *>(n), o); + break; + case CHILLAST_NODE_FLOATINGLITERAL: + printS(ident, dynamic_cast<chillAST_FloatingLiteral *>(n), o); + break; + case CHILLAST_NODE_LOOP: +// case CHILLAST_NODE_FORSTMT: + printS(ident, dynamic_cast<chillAST_ForStmt *>(n), o); + break; + case CHILLAST_NODE_FREE: + printS(ident, dynamic_cast<chillAST_Free *>(n), o); + break; + case CHILLAST_NODE_FUNCTIONDECL: + printS(ident, dynamic_cast<chillAST_FunctionDecl *>(n), o); + break; + case CHILLAST_NODE_IFSTMT: + printS(ident, dynamic_cast<chillAST_IfStmt *>(n), o); + break; + case CHILLAST_NODE_IMPLICITCASTEXPR: + printS(ident, dynamic_cast<chillAST_ImplicitCastExpr *>(n), o); + break; + case CHILLAST_NODE_INTEGERLITERAL: + printS(ident, dynamic_cast<chillAST_IntegerLiteral *>(n), o); + break; + case CHILLAST_NODE_MACRODEFINITION: + printS(ident, dynamic_cast<chillAST_MacroDefinition *>(n), o); + break; + case CHILLAST_NODE_MALLOC: + printS(ident, dynamic_cast<chillAST_Malloc *>(n), o); + break; + case CHILLAST_NODE_MEMBEREXPR: + printS(ident, dynamic_cast<chillAST_MemberExpr *>(n), o); + break; + case CHILLAST_NODE_NOOP: + printS(ident, dynamic_cast<chillAST_NoOp *>(n), o); + break; + case CHILLAST_NODE_NULL: + printS(ident, dynamic_cast<chillAST_NULL *>(n), o); + break; + case CHILLAST_NODE_PARENEXPR: + printS(ident, dynamic_cast<chillAST_ParenExpr *>(n), o); + break; + case CHILLAST_NODE_PREPROCESSING: + printS(ident, dynamic_cast<chillAST_Preprocessing *>(n), o); + break; + case CHILLAST_NODE_RECORDDECL: + printS(ident, dynamic_cast<chillAST_RecordDecl *>(n), o); + break; + case CHILLAST_NODE_RETURNSTMT: + printS(ident, dynamic_cast<chillAST_ReturnStmt *>(n), o); + break; + case CHILLAST_NODE_SIZEOF: + printS(ident, dynamic_cast<chillAST_Sizeof *>(n), o); + break; + case CHILLAST_NODE_TRANSLATIONUNIT: +// case CHILLAST_NODE_SOURCEFILE: + printS(ident, dynamic_cast<chillAST_SourceFile *>(n), o); + break; + case CHILLAST_NODE_TERNARYOPERATOR: + printS(ident, dynamic_cast<chillAST_TernaryOperator *>(n), o); + break; + case CHILLAST_NODE_TYPEDEFDECL: + printS(ident, dynamic_cast<chillAST_TypedefDecl *>(n), o); + break; + case CHILLAST_NODE_UNARYOPERATOR: + printS(ident, dynamic_cast<chillAST_UnaryOperator *>(n), o); + break; + case CHILLAST_NODE_VARDECL: + printS(ident, dynamic_cast<chillAST_VarDecl *>(n), o); + break; + case CHILLAST_NODE_UNKNOWN: + default: + CHILL_ERROR("Printing an unknown type of Node: %s\n", n->getTypeString()); + } + o.flush(); +} + +int GenericPrinter::getPrec(chillAST_Node *n) { + switch (n->getType()) { + case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR: + return getPrecS(dynamic_cast<chillAST_ArraySubscriptExpr *>(n)); + case CHILLAST_NODE_BINARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_BinaryOperator *>(n)); + case CHILLAST_NODE_CALLEXPR: + return getPrecS(dynamic_cast<chillAST_CallExpr *>(n)); + case CHILLAST_NODE_COMPOUNDSTMT: + return getPrecS(dynamic_cast<chillAST_CompoundStmt *>(n)); + case CHILLAST_NODE_CSTYLEADDRESSOF: + return getPrecS(dynamic_cast<chillAST_CStyleAddressOf *>(n)); + case CHILLAST_NODE_CSTYLECASTEXPR: + return getPrecS(dynamic_cast<chillAST_CStyleCastExpr *>(n)); + case CHILLAST_NODE_CUDAFREE: + return getPrecS(dynamic_cast<chillAST_CudaFree *>(n)); +// case CHILLAST_NODE_CUDAKERNELCALL: +// return getPrecS(dynamic_cast<chillAST_CudaKernelCall *>(n)); + case CHILLAST_NODE_CUDAMALLOC: + return getPrecS(dynamic_cast<chillAST_CudaMalloc *>(n)); + case CHILLAST_NODE_CUDAMEMCPY: + return getPrecS(dynamic_cast<chillAST_CudaMemcpy *>(n)); + case CHILLAST_NODE_CUDASYNCTHREADS: + return getPrecS(dynamic_cast<chillAST_CudaSyncthreads *>(n)); + case CHILLAST_NODE_DECLREFEXPR: + return getPrecS(dynamic_cast<chillAST_DeclRefExpr *>(n)); + case CHILLAST_NODE_FLOATINGLITERAL: + return getPrecS(dynamic_cast<chillAST_FloatingLiteral *>(n)); + case CHILLAST_NODE_LOOP: +// case CHILLAST_NODE_FORSTMT: + return getPrecS(dynamic_cast<chillAST_ForStmt *>(n)); + case CHILLAST_NODE_FREE: + return getPrecS(dynamic_cast<chillAST_Free *>(n)); + case CHILLAST_NODE_FUNCTIONDECL: + return getPrecS(dynamic_cast<chillAST_FunctionDecl *>(n)); + case CHILLAST_NODE_IFSTMT: + return getPrecS(dynamic_cast<chillAST_IfStmt *>(n)); + case CHILLAST_NODE_IMPLICITCASTEXPR: + return getPrecS(dynamic_cast<chillAST_ImplicitCastExpr *>(n)); + case CHILLAST_NODE_INTEGERLITERAL: + return getPrecS(dynamic_cast<chillAST_IntegerLiteral *>(n)); + case CHILLAST_NODE_MACRODEFINITION: + return getPrecS(dynamic_cast<chillAST_MacroDefinition *>(n)); + case CHILLAST_NODE_MALLOC: + return getPrecS(dynamic_cast<chillAST_Malloc *>(n)); + case CHILLAST_NODE_MEMBEREXPR: + return getPrecS(dynamic_cast<chillAST_MemberExpr *>(n)); + case CHILLAST_NODE_NOOP: + return getPrecS(dynamic_cast<chillAST_NoOp *>(n)); + case CHILLAST_NODE_NULL: + return getPrecS(dynamic_cast<chillAST_NULL *>(n)); + case CHILLAST_NODE_PARENEXPR: + return getPrecS(dynamic_cast<chillAST_ParenExpr *>(n)); + case CHILLAST_NODE_PREPROCESSING: + return getPrecS(dynamic_cast<chillAST_Preprocessing *>(n)); + case CHILLAST_NODE_RECORDDECL: + return getPrecS(dynamic_cast<chillAST_RecordDecl *>(n)); + case CHILLAST_NODE_RETURNSTMT: + return getPrecS(dynamic_cast<chillAST_ReturnStmt *>(n)); + case CHILLAST_NODE_SIZEOF: + return getPrecS(dynamic_cast<chillAST_Sizeof *>(n)); + case CHILLAST_NODE_TRANSLATIONUNIT: +// case CHILLAST_NODE_SOURCEFILE: + return getPrecS(dynamic_cast<chillAST_SourceFile *>(n)); + case CHILLAST_NODE_TERNARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_TernaryOperator *>(n)); + case CHILLAST_NODE_TYPEDEFDECL: + return getPrecS(dynamic_cast<chillAST_TypedefDecl *>(n)); + case CHILLAST_NODE_UNARYOPERATOR: + return getPrecS(dynamic_cast<chillAST_UnaryOperator *>(n)); + case CHILLAST_NODE_VARDECL: + return getPrecS(dynamic_cast<chillAST_VarDecl *>(n)); + case CHILLAST_NODE_UNKNOWN: + default: + CHILL_ERROR("Getting precedence an unknown type of Node: %s\n", n->getTypeString()); + return INT8_MAX; + } +} + diff --git a/src/printers/dump.cpp b/src/printers/dump.cpp deleted file mode 100644 index 334552f..0000000 --- a/src/printers/dump.cpp +++ /dev/null @@ -1,160 +0,0 @@ -// -// Created by ztuowen on 9/24/16. -// - -#include "printers/dump.h" - -using namespace chill::printer; -using namespace std; - -void dumpVector(GenericPrinter *p, string ident, chillAST_NodeList *n, ostringstream &o) { - for (int i = 0; i < n->size(); ++i) - p->print("", (*n)[i], o); -} - -void Dump::print(string ident, chillAST_Node *n, ostringStream &o) { - o << "(" << n->getTypeString() << " "; - if (n->getParameters()) { - o << "(Params: "; - dumpVector(this, ident, n->getParameters(), o); - o << ") "; - } - if (n->getSymbolTable()) { - o << "(VarScope: "; - dumpVector(this, ident, n->getSymbolTable(), o); - o << ") "; - } - if (n->getTypedefTable()) { - o << "(TypeDef: "; - dumpVector(this, ident, n->getTypedefTable(), o); - o << ") "; - } - o << ": "; - // Recurse - GenericPrinter::print(ident, n, o); - o << ") "; -} - -void Dump::print(std::string ident, chillAST_ArraySubscriptExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_BinaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CallExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CompoundStmt *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CStyleAddressOf *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CStyleCastExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaFree *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaKernelCall *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_CudaMalloc *n, std::ostringstream &o) { - print(ident, n->devPtr, o); - print(ident, n->sizeinbytes, o); -} - -void Dump::print(std::string ident, chillAST_CudaMemcpy *n, std::ostringstream &o) { - o << cudaMemcpyKind << " "; - print(ident, n->dest, o); - print(ident, n->src, o); - print(ident, n->size, o); -}; - -void Dump::print(std::string ident, chillAST_CudaSyncthreads *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_DeclRefExpr *n, std::ostringstream &o) { - chillAST_VarDecl *vd = n->getVarDecl(); - if (vd) - if (vd->isAParameter) o << "ParmVar "; else o << "Var "; - o << n->declarationName << " "; - chillAST_FunctionDecl *fd = n->getFunctionDecl(); - if (fd) dumpVector(this, ident, fd->getParameters(), o); -} - -void Dump::print(std::string ident, chillAST_FloatingLiteral *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_ForStmt *n, std::ostringstream &o) { - print(ident, n->init, o); - print(ident, n->cond, o); - print(ident, n->incr, o); - print(ident, n->body, o); -} - -void Dump::print(std::string ident, chillAST_Free *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_FunctionDecl *n, std::ostringstream &o) { - if (n->filename) o << filename << " "; - if (n->isFromSourceFile) o << "FromSourceFile" << " "; - o << n->returnType << " " << n->functionName << " "; - if (n->body) print(ident, n->body, o); -} - -void Dump::print(std::string ident, chillAST_IfStmt *n, std::ostringstream &o) { - print(ident, n->cond, o); - print(ident, n->thenpart, o); - if (n->elsepart) - print(ident, n->elsepart, o); -} - -void Dump::print(std::string ident, chillAST_IntegerLiteral *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_ImplicitCastExpr *n, std::ostringstream &o) { - print(ident, n->subexpr, o); -} - -void Dump::print(std::string ident, chillAST_MacroDefinition *n, std::ostringstream &o) { - o << n->macroName << " "; - dumpVector(this, ident, n->getParameters(), o); - print(ident, n->getBody(), o); - if (n->rhsString) o << " (aka " << n->rhsString << ") "; -} - -void Dump::print(std::string ident, chillAST_Malloc *n, std::ostringstream &o) { - print(ident, n->sizeexpr, o); -} - -void Dump::print(std::string ident, chillAST_MemberExpr *n, std::ostringstream &o) { - print(ident, n->base, o); - if (n->exptype == CHILLAST_MEMBER_EXP_ARROW) o << "-> "; - else o << ". "; - o << n->member << " "; -} - -void Dump::print(std::string ident, chillAST_NULL *n, std::ostringstream &o) { - o << "(NULL) "; -} - -void Dump::print(std::string ident, chillAST_NoOp *n, std::ostringstream &o) {}; - -void Dump::print(std::string ident, chillAST_ParenExpr *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_Preprocessing *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_RecordDecl *n, std::ostringstream &o) { - // TODO access control - o << n->getName() << " "; - o << n->isAStruct() << " "; - o << n->isAUnion() << " "; -} - -void Dump::print(std::string ident, chillAST_ReturnStmt *n, std::ostringstream &o) { - if (n->returnvalue) print(ident, n->returnvalue, o); -} - -void Dump::print(std::string ident, chillAST_Sizeof *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_SourceFile *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_TypedefDecl *n, std::ostringstream &o) { - o << n->underlyingtype << " " << n->newtype << " " << n->arraypart << " "; -} - -void Dump::print(std::string ident, chillAST_TernaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_UnaryOperator *n, std::ostringstream &o); - -void Dump::print(std::string ident, chillAST_VarDecl *n, std::ostringstream &o); - |