Skip to content

Commit 2790b06

Browse files
authored
Merge pull request #1318 from Smit-create/c8
C: Fix const var names and their usage
2 parents d9525b6 + a98d558 commit 2790b06

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
6262
bool intrinsic_module = false;
6363
const ASR::Function_t *current_function = nullptr;
6464
std::map<uint64_t, SymbolInfo> sym_info;
65+
std::map<uint64_t, std::string> const_var_names;
6566

6667
// Output configuration:
6768
// Use std::string or char*
@@ -80,7 +81,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
8081

8182
std::unique_ptr<CCPPDSUtils> c_ds_api;
8283
std::string const_name;
83-
size_t const_list_count;
84+
size_t const_vars_count;
8485

8586
SymbolTable* current_scope;
8687
bool is_string_concat_present;
@@ -93,7 +94,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
9394
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
9495
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>(is_c)},
9596
const_name{"constname"},
96-
const_list_count{0}, is_string_concat_present{false} {
97+
const_vars_count{0}, is_string_concat_present{false} {
9798
}
9899

99100
void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
@@ -518,8 +519,8 @@ R"(#include <stdio.h>
518519
last_expr_precedence = 2;
519520
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
520521
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(x.m_type);
521-
const_name += std::to_string(const_list_count);
522-
const_list_count += 1;
522+
const_name += std::to_string(const_vars_count);
523+
const_vars_count += 1;
523524
const_name = current_scope->get_unique_name(const_name);
524525
std::string indent(indentation_level*indentation_spaces, ' ');
525526
current_body += indent + c_ds_api->get_list_type(list_type) + " " +
@@ -628,15 +629,16 @@ R"(#include <stdio.h>
628629
ASR::TupleConstant_t *tup_c = ASR::down_cast<ASR::TupleConstant_t>(x.m_target);
629630
std::string src_tmp = "", val_name = "";
630631
if (ASR::is_a<ASR::TupleConstant_t>(*x.m_value)) {
631-
val_name = const_name + std::to_string(const_list_count);
632-
self().visit_TupleConstant(*ASR::down_cast<ASR::TupleConstant_t>(x.m_value));
632+
ASR::TupleConstant_t *tup_const = ASR::down_cast<ASR::TupleConstant_t>(x.m_value);
633+
self().visit_TupleConstant(*tup_const);
634+
val_name = const_var_names[get_hash((ASR::asr_t*)tup_const)];
633635
src_tmp += src;
634636
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value)) {
635637
self().visit_FunctionCall(*ASR::down_cast<ASR::FunctionCall_t>(x.m_value));
636638
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(tup_c->m_type);
637639
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
638-
const_name += std::to_string(const_list_count);
639-
const_list_count += 1;
640+
const_name += std::to_string(const_vars_count);
641+
const_vars_count += 1;
640642
const_name = current_scope->get_unique_name(const_name);
641643
src_tmp += indent + tuple_type_c + " " + const_name + " = " + src + ";\n";
642644
val_name = const_name;
@@ -687,7 +689,9 @@ R"(#include <stdio.h>
687689
std::string list_dc_func = c_ds_api->get_list_deepcopy_func(list_target);
688690
if( ASR::is_a<ASR::ListConstant_t>(*x.m_value) ) {
689691
src += value;
690-
src += indent + list_dc_func + "(&" + const_name + ", &" + target + ");\n\n";
692+
ASR::ListConstant_t *l_const = ASR::down_cast<ASR::ListConstant_t>(x.m_value);
693+
std::string var_name = const_var_names[get_hash((ASR::asr_t*)l_const)];
694+
src += indent + list_dc_func + "(&" + var_name + ", &" + target + ");\n\n";
691695
} else if (ASR::is_a<ASR::ListConcat_t>(*x.m_value)) {
692696
src += indent + list_dc_func + "(" + value + ", &" + target + ");\n\n";
693697
} else {
@@ -768,39 +772,42 @@ R"(#include <stdio.h>
768772
void visit_ListConstant(const ASR::ListConstant_t& x) {
769773
std::string indent(indentation_level * indentation_spaces, ' ');
770774
std::string tab(indentation_spaces, ' ');
771-
const_name += std::to_string(const_list_count);
772-
const_list_count += 1;
775+
const_name += std::to_string(const_vars_count);
776+
const_vars_count += 1;
773777
const_name = current_scope->get_unique_name(const_name);
778+
std::string var_name = const_name;
779+
const_var_names[get_hash((ASR::asr_t*)&x)] = var_name;
774780
ASR::List_t* t = ASR::down_cast<ASR::List_t>(x.m_type);
775781
std::string list_type_c = c_ds_api->get_list_type(t);
776782
std::string src_tmp = "";
777-
src_tmp += indent + list_type_c + " " + const_name + ";\n";
783+
src_tmp += indent + list_type_c + " " + var_name + ";\n";
778784
std::string list_init_func = c_ds_api->get_list_init_func(t);
779-
src_tmp += indent + list_init_func + "(&" + const_name + ", " +
785+
src_tmp += indent + list_init_func + "(&" + var_name + ", " +
780786
std::to_string(x.n_args) + ");\n";
781787
for( size_t i = 0; i < x.n_args; i++ ) {
782788
self().visit_expr(*x.m_args[i]);
783789
if( ASR::is_a<ASR::Character_t>(*t->m_type) ) {
784-
src_tmp += const_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
790+
src_tmp += var_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
785791
}
786792
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type, src,
787-
const_name + ".data[" + std::to_string(i) +"]") + "\n";
793+
var_name + ".data[" + std::to_string(i) +"]") + "\n";
788794
}
789-
src_tmp += indent + const_name + ".current_end_point = " + std::to_string(x.n_args) + ";\n";
795+
src_tmp += indent + var_name + ".current_end_point = " + std::to_string(x.n_args) + ";\n";
790796
src = src_tmp;
791797
}
792798

793799
void visit_TupleConstant(const ASR::TupleConstant_t& x) {
794800
std::string indent(indentation_level * indentation_spaces, ' ');
795801
std::string tab(indentation_spaces, ' ');
796-
const_name += std::to_string(const_list_count);
797-
const_list_count += 1;
802+
const_name += std::to_string(const_vars_count);
803+
const_vars_count += 1;
798804
const_name = current_scope->get_unique_name(const_name);
799805
std::string var_name = const_name;
806+
const_var_names[get_hash((ASR::asr_t*)&x)] = var_name;
800807
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(x.m_type);
801808
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
802809
std::string src_tmp = "";
803-
src_tmp += indent + tuple_type_c + " " + const_name + ";\n";
810+
src_tmp += indent + tuple_type_c + " " + var_name + ";\n";
804811
for (size_t i = 0; i < x.n_elements; i++) {
805812
self().visit_expr(*x.m_elements[i]);
806813
std::string ele = ".element_" + std::to_string(i);

0 commit comments

Comments
 (0)