Skip to content

Commit

Permalink
Introducing basic_free_stack_if_not_null
Browse files Browse the repository at this point in the history
  • Loading branch information
anutosh491 committed Apr 4, 2024
1 parent 760c898 commit 3bd2764
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/libasr/pass/replace_symbolic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
return SubroutineCall(loc, basic_free_stack_sym, {x});
}

ASR::stmt_t *basic_free_stack_if_not_null(const Location &loc, ASR::expr_t *x) {
Vec<ASR::stmt_t*> func_body;
func_body.reserve(al, 1);
func_body.push_back(al, basic_free_stack(loc, x));
ASR::ttype_t *CPtr_type = ASRUtils::TYPE(ASR::make_CPtr_t(al, loc));
ASR::ttype_t *Logical_type = ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4));
ASR::expr_t* null = ASRUtils::EXPR(ASR::make_PointerNullConstant_t(al, loc, CPtr_type));
ASR::expr_t* cond = ASRUtils::EXPR(ASR::make_CPtrCompare_t(al, loc, x, ASR::cmpopType::NotEq,
null, Logical_type, nullptr));
return ASRUtils::STMT(ASR::make_If_t(al, loc, cond,
func_body.p, func_body.size(), nullptr, 0));
}

ASR::expr_t *basic_new_heap(const Location& loc) {
ASR::symbol_t* basic_new_heap_sym = create_bindc_function(loc,
"basic_new_heap", {}, ASRUtils::TYPE((ASR::make_CPtr_t(al, loc))));
Expand Down Expand Up @@ -324,7 +337,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
func_body.from_pointer_n_copy(al, xx.m_body, xx.n_body);

for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
func_body.push_back(al, basic_free_stack(x.base.base.loc,
func_body.push_back(al, basic_free_stack_if_not_null(x.base.base.loc,
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, symbol))));
}

Expand Down Expand Up @@ -1027,7 +1040,7 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
// freeing out variables
if (!symbolic_vars_to_free.empty()){
for (ASR::symbol_t* symbol : symbolic_vars_to_free) {
pass_result.push_back(al, basic_free_stack(x.base.base.loc,
pass_result.push_back(al, basic_free_stack_if_not_null(x.base.base.loc,
ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, symbol))));
}
pass_result.push_back(al, ASRUtils::STMT(ASR::make_Return_t(al, x.base.base.loc)));
Expand Down

0 comments on commit 3bd2764

Please sign in to comment.