13 #include "utils/logger.hpp"
22 static std::shared_ptr<ast::Expression>
unwrap(
const std::shared_ptr<ast::Expression>& expr) {
23 if (expr && expr->is_wrapped_expression()) {
24 const auto& e = std::dynamic_pointer_cast<ast::WrappedExpression>(expr);
25 return e->get_expression();
38 const std::shared_ptr<ast::FromStatement>&
node) {
43 const auto& increment =
unwrap(
node->get_increment());
47 if (!from->is_integer() || !to->is_integer() ||
48 (increment !=
nullptr && !increment->is_integer())) {
52 int start = std::dynamic_pointer_cast<ast::Integer>(from)->eval();
53 int end = std::dynamic_pointer_cast<ast::Integer>(to)->eval();
55 if (increment !=
nullptr) {
56 step = std::dynamic_pointer_cast<ast::Integer>(increment)->eval();
61 for (
int i = start;
i <= end;
i +=
step) {
63 const auto new_block = std::unique_ptr<ast::StatementBlock>(
66 statements.insert(statements.end(),
67 new_block->get_statements().begin(),
68 new_block->get_statements().end());
73 return std::make_shared<ast::ExpressionStatement>(block);
81 node.visit_children(*
this);
83 const auto& statements =
node.get_statements();
85 for (
auto iter = statements.begin(); iter != statements.end(); ++iter) {
86 if ((*iter)->is_from_statement()) {
87 const auto& statement = std::dynamic_pointer_cast<ast::FromStatement>((*iter));
91 if (!verbatim_blocks.empty()) {
92 logger->debug(
"LoopUnrollVisitor : can not unroll because of verbatim block");
98 if (new_statement !=
nullptr) {
99 node.reset_statement(iter, new_statement);
101 const auto& before =
to_nmodl(statement);
102 const auto& after =
to_nmodl(new_statement);
103 logger->debug(
"LoopUnrollVisitor : \n {} \n unrolled to \n {}", before, after);
Auto generated AST classes declaration.
Represents block encapsulating list of statements.
void visit_statement_block(ast::StatementBlock &node) override
visit node of type ast::StatementBlock
Helper visitor to replace index of array variable with integer.
void visit_statement_block(ast::StatementBlock &node) override
Parse verbatim blocks and rename variable if it is used.
virtual std::shared_ptr< StatementBlock > get_statement_block() const
Return associated statement block for the AST node.
virtual std::string get_node_name() const
Return name of of the node.
@ VERBATIM
type of ast::Verbatim
std::vector< std::shared_ptr< Statement > > StatementVector
Unroll for loop in the AST.
void move(Item *q1, Item *q2, Item *q3)
static std::shared_ptr< ast::ExpressionStatement > unroll_for_loop(const std::shared_ptr< ast::FromStatement > &node)
Unroll given for loop.
static std::shared_ptr< ast::Expression > unwrap(const std::shared_ptr< ast::Expression > &expr)
return underlying expression wrapped by WrappedExpression
encapsulates code generation backend implementations
std::vector< std::shared_ptr< const ast::Ast > > collect_nodes(const ast::Ast &node, const std::vector< ast::AstNodeType > &types)
traverse node recursively and collect nodes of given types
std::string to_nmodl(const ast::Ast &node, const std::set< ast::AstNodeType > &exclude_types)
Given AST node, return the NMODL string representation.
static Node * node(Object *)
Utility functions for visitors implementation.