Skip to content

Commit

Permalink
Initial attempt at implementing VERBATIM.
Browse files Browse the repository at this point in the history
  • Loading branch information
1uc committed Jul 22, 2024
1 parent e76b886 commit 1bd6043
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/codegen/codegen_coreneuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ std::string CodegenCoreneuronCppVisitor::process_verbatim_token(const std::strin
return get_variable_name(token, use_instance);
}

void CodegenCoreneuronCppVisitor::visit_verbatim(const Verbatim& node) {
const auto& text = node.get_statement()->eval();
const auto& result = process_verbatim_text(text);

const auto& statements = stringutils::split_string(result, '\n');
for (const auto& statement: statements) {
const auto& trimed_stmt = stringutils::trim_newline(statement);
if (trimed_stmt.find_first_not_of(' ') != std::string::npos) {
printer->add_line(trimed_stmt);
}
}
}


/**
* \details This can be override in the backend. For example, parameters can be constant
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/codegen_coreneuron_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class CodegenCoreneuronCppVisitor: public CodegenCppVisitor {
*/
std::string process_verbatim_token(const std::string& token);


/**
* Check if variable is qualified as constant
* \param name The name of variable
Expand Down Expand Up @@ -1017,6 +1016,7 @@ class CodegenCoreneuronCppVisitor: public CodegenCppVisitor {

void visit_derivimplicit_callback(const ast::DerivimplicitCallback& node) override;
void visit_for_netcon(const ast::ForNetcon& node) override;
void visit_verbatim(const ast::Verbatim& node) override;
void visit_watch_statement(const ast::WatchStatement& node) override;

ParamVector functor_params() override;
Expand Down
16 changes: 2 additions & 14 deletions src/codegen/codegen_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#include <filesystem>

#include "codegen/codegen_coreneuron_cpp_visitor.hpp"
#include "config/config.h"

#include "ast/all.hpp"
#include "codegen/codegen_helper_visitor.hpp"
#include "codegen/codegen_utils.hpp"
#include "utils/string_utils.hpp"
#include "visitors/defuse_analyze_visitor.hpp"
#include "visitors/rename_visitor.hpp"
#include "visitors/symtab_visitor.hpp"
Expand Down Expand Up @@ -966,20 +968,6 @@ void CodegenCppVisitor::visit_function_call(const FunctionCall& node) {
}


void CodegenCppVisitor::visit_verbatim(const Verbatim& node) {
const auto& text = node.get_statement()->eval();
const auto& result = process_verbatim_text(text);

const auto& statements = stringutils::split_string(result, '\n');
for (const auto& statement: statements) {
const auto& trimed_stmt = stringutils::trim_newline(statement);
if (trimed_stmt.find_first_not_of(' ') != std::string::npos) {
printer->add_line(trimed_stmt);
}
}
}


void CodegenCppVisitor::visit_update_dt(const ast::UpdateDt& node) {
// dt change statement should be pulled outside already
}
Expand Down
2 changes: 0 additions & 2 deletions src/codegen/codegen_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,6 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor {
*/
virtual std::string process_verbatim_text(std::string const& text) = 0;


/**
* Arguments for register_mech or point_register_mech function
*/
Expand Down Expand Up @@ -1452,7 +1451,6 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor {
void visit_unary_operator(const ast::UnaryOperator& node) override;
void visit_unit(const ast::Unit& node) override;
void visit_var_name(const ast::VarName& node) override;
void visit_verbatim(const ast::Verbatim& node) override;
void visit_while_statement(const ast::WhileStatement& node) override;
void visit_update_dt(const ast::UpdateDt& node) override;
void visit_protect_statement(const ast::ProtectStatement& node) override;
Expand Down
59 changes: 57 additions & 2 deletions src/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,65 @@ std::string CodegenNeuronCppVisitor::nrn_thread_internal_arguments() {
return {};
}

std::vector<std::string> CodegenNeuronCppVisitor::print_verbatim_setup(

Check warning on line 494 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L494

Added line #L494 was not covered by tests
const std::string& verbatim) {
// Note, the logic for reducing the number of macros printed, is aims to
// improve legibility of the generated code by reducing number of lines of
// code. It would be correct to print all macros, because that's essentially
// what NOCMODL does. Therefore, the logic isn't sharp.

std::vector<std::string> macros_defined;
auto print_macro = [this, &verbatim, &macros_defined](const std::string& macro_name,
const std::string& macro_value) {
if (verbatim.find(macro_name) != std::string::npos) {
printer->fmt_line("#define {} {}", macro_name, macro_value);
macros_defined.push_back(macro_name);

Check warning on line 506 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L501-L506

Added lines #L501 - L506 were not covered by tests
}
};

Check warning on line 508 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L508

Added line #L508 was not covered by tests

printer->add_line("// Setup for VERBATIM");
for (const auto& var: codegen_float_variables) {
auto name = get_name(var);
print_macro(name, get_variable_name(name));

Check warning on line 513 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L510-L513

Added lines #L510 - L513 were not covered by tests
}

for (const auto& var: codegen_int_variables) {
auto name = get_name(var);
print_macro(name, get_variable_name(name));

Check warning on line 518 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L516-L518

Added lines #L516 - L518 were not covered by tests
}

if (verbatim.find("_nt") != std::string::npos) {
print_macro("_nt", "nt");

Check warning on line 522 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L521-L522

Added lines #L521 - L522 were not covered by tests
}

return macros_defined;

Check warning on line 525 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L525

Added line #L525 was not covered by tests
}

void CodegenNeuronCppVisitor::print_verbatim_cleanup(

Check warning on line 528 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L528

Added line #L528 was not covered by tests
const std::vector<std::string>& macros_defined) {
for (const auto& macro: macros_defined) {
printer->fmt_line("#undef {}", macro);

Check warning on line 531 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L530-L531

Added lines #L530 - L531 were not covered by tests
}
printer->add_line("// End of cleanup for VERBATIM");

Check warning on line 533 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L533

Added line #L533 was not covered by tests
}


/// TODO: Write for NEURON
std::string CodegenNeuronCppVisitor::process_verbatim_text(std::string const& text) {
return {};
return text;

Check warning on line 538 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L538

Added line #L538 was not covered by tests
}


void CodegenNeuronCppVisitor::visit_verbatim(const Verbatim& node) {
const auto& verbatim_code = node.get_statement()->eval();

Check warning on line 543 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L542-L543

Added lines #L542 - L543 were not covered by tests

auto macros_defined = print_verbatim_setup(verbatim_code);
printer->add_line("// Begin VERBATIM");
const auto& lines = stringutils::split_string(verbatim_code, '\n');
for (const auto& line: lines) {
printer->add_line(line);

Check warning on line 549 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L545-L549

Added lines #L545 - L549 were not covered by tests
}
printer->add_line("// End VERBATIM");
print_verbatim_cleanup(macros_defined);

Check warning on line 552 in src/codegen/codegen_neuron_cpp_visitor.cpp

View check run for this annotation

Codecov / codecov/patch

src/codegen/codegen_neuron_cpp_visitor.cpp#L551-L552

Added lines #L551 - L552 were not covered by tests
}


Expand Down
8 changes: 4 additions & 4 deletions src/codegen/codegen_neuron_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor {
*/
std::string process_verbatim_text(std::string const& text) override;

std::vector<std::string> print_verbatim_setup(const std::string& verbatim);
void print_verbatim_cleanup(const std::vector<std::string>& macros_defined);


/**
* Arguments for register_mech or point_register_mech function
Expand Down Expand Up @@ -698,12 +701,9 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor {
/* Overloaded visitor routines */
/****************************************************************************************/


void visit_verbatim(const ast::Verbatim& node) override;
void visit_watch_statement(const ast::WatchStatement& node) override;




public:
/****************************************************************************************/
/* Public printing routines for code generation for use in unit tests */
Expand Down

0 comments on commit 1bd6043

Please sign in to comment.