Skip to content

Commit

Permalink
added debug infrastructure, moved some codegen from driver.cpp to cod…
Browse files Browse the repository at this point in the history
…egen class
  • Loading branch information
ctaylor committed Apr 21, 2023
1 parent be6e616 commit 1a1d06a
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 133 deletions.
4 changes: 4 additions & 0 deletions backend/include/hpx/codegenvisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ struct CodegenVisitor {

CodegenVisitor(
SymbolTable & symTable,
std::vector< Symbol * > & configVars,
ProgramTree & prgmTree,
chpl::uast::BuilderResult const& chapelBr,
std::string const& cppFilePathStr,
std::string const& chapelFilePathStr,
std::ostream & fstrm
);
Expand Down Expand Up @@ -103,12 +105,14 @@ struct CodegenVisitor {
void visit();

SymbolTable & symbolTable;
std::vector< Symbol * > & cfgVars;
ProgramTree & programTree;
chpl::uast::BuilderResult const& br;
std::size_t indent;
std::size_t scope;
std::optional<std::string> identifier;
std::ostream & fstrm_;
std::string cppFilePathStr;
std::string chplFilePathStr;
std::vector<bool> headers;
};
Expand Down
2 changes: 2 additions & 0 deletions backend/include/hpx/programtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ struct VariableDeclarationExpression : public ExpressionBase {
std::string identifier;
kind_types kind;
std::string chplLine;
int qualifier;
bool config;
};

struct ScalarDeclarationExpression : public VariableDeclarationExpression {
Expand Down
1 change: 1 addition & 0 deletions backend/include/hpx/symbolbuildingvisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ struct SymbolBuildingVisitor {
std::optional< std::reference_wrapper<Symbol> > sym;
std::optional<uast::AstNode const*> symnode;

std::vector< Symbol * > configVars;
SymbolTable symbolTable;
};

Expand Down
2 changes: 2 additions & 0 deletions backend/include/hpx/symboltypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ struct SymbolBase {
std::optional<kind_types> kind;
std::optional<std::string> identifier;
std::optional<std::vector<uast::AstNode const*>> literal;
int kindqualifier;
bool isConfig;
std::size_t scopeId;
};

Expand Down
1 change: 1 addition & 0 deletions backend/include/hpx/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace chplx::util {
// global options
extern bool suppressLineDirectives;
extern bool fullFilePath;
extern bool compilerDebug;

// emit line directive
std::string emitLineDirective(char const* name, int line);
Expand Down
74 changes: 57 additions & 17 deletions backend/src/codegenvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ namespace chpl { namespace ast { namespace visitors { namespace hpx {

CodegenVisitor::CodegenVisitor(
chpl::ast::visitors::hpx::SymbolTable & st,
std::vector< Symbol * > & configVars,
ProgramTree & prgmTree,
chpl::uast::BuilderResult const& chapelBr,
std::string const& cppFilePath,
std::string const& chapelFilePath,
std::ostream & fstrm)
: symbolTable(st), programTree(prgmTree), br(chapelBr), indent(0), scope(0),
: symbolTable(st), cfgVars(configVars), programTree(prgmTree), br(chapelBr),
indent(0), scope(0),
fstrm_(fstrm),
cppFilePathStr(cppFilePath),
chplFilePathStr(chapelFilePath),
headers(static_cast<std::size_t>(HeaderEnum::HeaderCount), false)
{
Expand Down Expand Up @@ -354,24 +358,8 @@ struct StatementVisitor {
std::shared_ptr<func_kind> const& fk =
std::get<std::shared_ptr<func_kind>>(*(node->symbol.kind));

// os << "std::function<";
//
FuncDeclArgVisitor fdav{os};
// std::visit(fdav, *(fk->retKind));

// os << "(";

const std::size_t args_sz = fk->args.size()-1;
// if(0 < args_sz) {
// std::visit(fdav, (*(fk->args[0].kind)));
//
// for(std::size_t i = 1; i < args_sz; ++i) {
// os << ',';
// std::visit(fdav, (*(fk->args[i].kind)) );
// }
// }
//
// os << ")> ";

os << "auto ";
std::string const& fn_sig_ref =
Expand Down Expand Up @@ -451,8 +439,60 @@ void CodegenVisitor::visit(StatementVisitor && v) {
}
}

static void generateSourceHeader(std::ostream & fos, std::string const& fps, std::string const& chplFile) {
fos << "// This program file was generated by the chplx compiler." << std::endl;
fos << "// The original Chapel program file can be found here: " << chplFile << std::endl;
fos << "//" << std::endl;

fos << "#include <hpx/hpx_init.hpp>" << std::endl << std::endl;

{
const auto pos = fps.find(".");
const std::string prefix = fps.substr(0, pos);
fos << "#include \"" << prefix << ".hpp\"" << std::endl;
}
}

static void generateSourceFooter(std::ostream & fos) {
fos << std::endl
<< "int main(int argc, char * argv[]) {" << std::endl
<< " return hpx::init(argc, argv);" << std::endl
<< "}" << std::endl;
}

static void generateHpxMainBeg(std::ostream & fos) {
fos << std::endl
<< "int hpx_main(int argc, char * argv[]) {" << std::endl
<< std::endl;
}

static void generateHpxMainEnd(std::ostream & fos) {
fos << std::endl
<< " return hpx::finalize();" << std::endl
<< "}" << std::endl;
}


void CodegenVisitor::visit() {

generateSourceHeader(fstrm_, cppFilePathStr, chplFilePathStr);
/*
if(0 < cfgVars.size()) {
fstrm_ << "hpx::program::options::options::options_description opts;\n options.add_options()";
for(auto const& opt : cfgVars) {
fstrm_ << " (\"" << (*opt->identifier) << "\"," << std::endl;
fstrm_ << " hpx::program_options::value<" << (*opt->kind) << ">(&" << (*opt->identifier) << "), \"\")";
}
fstrm_ << ';' << std::endl;
fstrm_ << "hpx::init_params init_args;" << std::endl << "init_args.desc_cmdline = options;" << std::endl;
}
*/
generateHpxMainBeg(fstrm_);

visit(StatementVisitor{symbolTable, br, fstrm_, indent, headers, true, false});

generateHpxMainEnd(fstrm_);
generateSourceFooter(fstrm_);
}

} /* namespace hpx */ } /* namespace visitors */ } /* namespace ast */ } /* namespace chpl */
62 changes: 20 additions & 42 deletions backend/src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,46 +42,14 @@ extern char *optarg;
extern int optind, opterr, optopt;
const char *optstring;

static void generateSourceHeader(std::ostream & fos, std::string const& fps, std::filesystem::path const& chplFile) {
fos << "// This program file was generated by the chplx compiler." << std::endl;
fos << "// The original Chapel program file can be found here: " << chplFile.filename() << std::endl;
fos << "//" << std::endl;

fos << "#include <hpx/hpx_init.hpp>" << std::endl << std::endl;

{
const auto pos = fps.find(".");
const std::string prefix = fps.substr(0, pos);
fos << "#include \"" << prefix << ".hpp\"" << std::endl;
}
}

static void generateSourceFooter(std::ostream & fos) {
fos << std::endl
<< "int main(int argc, char * argv[]) {" << std::endl
<< " return hpx::init(argc, argv);" << std::endl
<< "}" << std::endl;
}

static void generateHpxMainBeg(std::ostream & fos) {
fos << std::endl
<< "int hpx_main(int argc, char * argv[]) {" << std::endl
<< std::endl;
}

static void generateHpxMainEnd(std::ostream & fos) {
fos << std::endl
<< " return hpx::finalize();" << std::endl
<< "}" << std::endl;
}

// generate help string
void usage() {
constexpr char const* helpstring =
R"(chplx -help:
chplx [options] -f <full path to file name or file name>
options: -E: suppress generating #line directives (default: false)
-F: suppress generating full path for #line directives (default: true)
-d: debug compiler phases (default : false)
)";

std::cout << helpstring << std::flush;
Expand All @@ -100,7 +68,7 @@ int main(int argc, char ** argv) {
int opt = 0;

// Retrieve the options:
while ( (opt = getopt(argc, argv, "f:hEF")) != -1 ) { // for each option...
while ( (opt = getopt(argc, argv, "f:hEFd")) != -1 ) { // for each option...
switch ( opt ) {
case 'f':
filePath = std::string{optarg};
Expand All @@ -111,6 +79,9 @@ int main(int argc, char ** argv) {
case 'F':
chplx::util::fullFilePath = false;
break;
case 'd':
chplx::util::compilerDebug = true;
break;
default:
std::cout << "chplx: unknown command line option: -" << opt << std::endl << std::flush;
[[fallthrough]];
Expand Down Expand Up @@ -195,22 +166,29 @@ int main(int argc, char ** argv) {

chpl::ast::visitors::hpx::SymbolBuildingVisitor sbv{br, ofilePath};
AstNode const* ast = static_cast<AstNode const*>(mod);
if(chplx::util::compilerDebug) {
std::cout << "[SymbolBuildingVisitor] Enter" << std::endl;
}
ast->traverse(sbv);

std::ofstream ofs(ofilePath);
generateSourceHeader(ofs, ofilePath, chplFilePth);
generateHpxMainBeg(ofs);
if(chplx::util::compilerDebug) {
std::cout << "[SymbolBuildingVisitor] Exit" << std::endl;
}

chplx::ast::visitors::hpx::ProgramTreeBuildingVisitor pbv{{}, nullptr, sbv.symbolTable.symbolTableRef, br, sbv.symbolTable, program, { &(program.statements) }, {}};
if(chplx::util::compilerDebug) {
std::cout << "[ProgramTreeBuildingVisitor] Enter" << std::endl;
}
ast->traverse(pbv);
if(chplx::util::compilerDebug) {
std::cout << "[ProgramTreelBuildingVisitor] Exit" << std::endl;
}

chpl::ast::visitors::hpx::CodegenVisitor cgv{sbv.symbolTable, program, br, ofilePath, ofs};
std::ofstream ofs(ofilePath);

chpl::ast::visitors::hpx::CodegenVisitor cgv{sbv.symbolTable, sbv.configVars, program, br, ofilePath, chplFilePth.filename(), ofs};
cgv.indent += 1;
cgv.visit();

generateHpxMainEnd(ofs);
generateSourceFooter(ofs);

cgv.generateApplicationHeader(chplFilePth);

ofs.flush();
Expand Down
58 changes: 49 additions & 9 deletions backend/src/programtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,46 @@ struct ScalarDeclarationExpressionVisitor {
std::ostream & os;
};

void VisitQualifierPrefix(std::ostream & os, const int qualifier) {
if(qualifier == 1) {
os << "const ";
}
else if(qualifier == 4) {
os << "const ";
}
}

void VisitQualifierSuffix(std::ostream & os, const int qualifier) {
if(qualifier == 2) {
os << " const&";
}
else if(qualifier == 3) {
os << "&";
}
else if(qualifier == 6) {
os << "&";
}
else if(qualifier == 7) {
os << " const&";
}
else if(qualifier == 8) {
os << "&";
}
else if(qualifier == 9) {
os << "&";
}
}

void ScalarDeclarationExpression::emit(std::ostream & os) const {
if(-1 < qualifier) {
VisitQualifierPrefix(os, qualifier);
}

std::visit(ScalarDeclarationExpressionVisitor{os}, kind);

if(-1 < qualifier) {
VisitQualifierSuffix(os, qualifier);
}
os << " " << identifier << ";" << std::endl;
}

Expand All @@ -72,6 +110,10 @@ void ScalarDeclarationLiteralExpressionVisitor::operator()(string_kind const&) {
}

void ScalarDeclarationLiteralExpression::emit(std::ostream & os) const {
if(-1 < qualifier) {
VisitQualifierPrefix(os, qualifier);
}

std::visit(ScalarDeclarationExpressionVisitor{os}, kind);
os << " " << identifier;
}
Expand All @@ -80,6 +122,9 @@ void ArrayDeclarationExpression::emit(std::ostream & os) const {
std::shared_ptr<array_kind> const& akref =
std::get<std::shared_ptr<array_kind>>(kind);

if(-1 < qualifier) {
VisitQualifierPrefix(os, qualifier);
}

os << "std::vector<";
std::visit(ScalarDeclarationExpressionVisitor{os}, akref->kind);
Expand Down Expand Up @@ -140,15 +185,17 @@ void ArrayDeclarationLiteralExpression::emit(std::ostream & os) const {
std::get<std::shared_ptr<array_kind>>(kind);

std::stringstream typelist{}, literallist{};
// std::shared_ptr<kind_node_type> & symref =
// std::get<std::shared_ptr<kind_node_type>>(akref->kind);

std::vector<kind_types> & children =
std::get<std::shared_ptr<kind_node_type>>(akref->kind)->children;

const std::size_t children_sz = children.size();
std::size_t vec_count = 0;

if(-1 < qualifier) {
VisitQualifierPrefix(os, qualifier);
}

// the following 2 loops could be performed in
// parallel
//
Expand Down Expand Up @@ -290,13 +337,6 @@ void ReturnExpression::emit(std::ostream & os) const {
os << fmt::vformat(retfmt, store) << std::endl;
}

struct ExprVisitor {
template<typename T>
void operator()(T const&) {}

std::ostream & os;
};

void FunctionCallExpression::emit(std::ostream & os) const {
if(std::holds_alternative<std::shared_ptr<cxxfunc_kind>>(*symbol.kind)) {
const std::size_t args_sz = arguments.size();
Expand Down
Loading

0 comments on commit 1a1d06a

Please sign in to comment.