summaryrefslogtreecommitdiff
path: root/src/printer
diff options
context:
space:
mode:
Diffstat (limited to 'src/printer')
-rw-r--r--src/printer/cfamily.cpp8
-rw-r--r--src/printer/dump.cpp226
-rw-r--r--src/printer/generic.cpp200
3 files changed, 434 insertions, 0 deletions
diff --git a/src/printer/cfamily.cpp b/src/printer/cfamily.cpp
new file mode 100644
index 0000000..8bfeb82
--- /dev/null
+++ b/src/printer/cfamily.cpp
@@ -0,0 +1,8 @@
+//
+// Created by ztuowen on 9/24/16.
+//
+
+#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;
+ }
+}
+