Skip to content

Commit 7597043

Browse files
authored
Merge pull request #1255 from Smit-create/c5
C: Codegen to Compare data types
2 parents fbd2e80 + 3b3960d commit 7597043

File tree

4 files changed

+153
-174
lines changed

4 files changed

+153
-174
lines changed

integration_tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ RUN(NAME test_list_01 LABELS cpython llvm c)
219219
RUN(NAME test_list_02 LABELS cpython llvm c)
220220
RUN(NAME test_list_03 LABELS cpython llvm c)
221221
RUN(NAME test_list_04 LABELS cpython llvm c)
222-
RUN(NAME test_list_05 LABELS cpython llvm)
222+
RUN(NAME test_list_05 LABELS cpython llvm c)
223223
RUN(NAME test_list_06 LABELS cpython llvm c)
224224
RUN(NAME test_list_07 LABELS cpython llvm)
225225
RUN(NAME test_list_08 LABELS cpython llvm)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,12 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
549549
}
550550
} else if (ASR::is_a<ASR::List_t>(*v_m_type)) {
551551
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v_m_type);
552-
std::string list_element_type = CUtils::get_c_type_from_ttype_t(t->m_type);
553-
std::string list_type_c = list_api->get_list_type(t, list_element_type);
552+
std::string list_type_c = c_ds_api->get_list_type(t);
554553
sub = format_type_c("", list_type_c, v.m_name,
555554
false, false);
556555
} else if (ASR::is_a<ASR::Tuple_t>(*v_m_type)) {
557556
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v_m_type);
558-
std::string tuple_type_c = tuple_api->get_tuple_type(t);
557+
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
559558
sub = format_type_c("", tuple_type_c, v.m_name,
560559
false, false);
561560
} else if (ASR::is_a<ASR::CPtr_t>(*v_m_type)) {
@@ -604,12 +603,10 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
604603
std::string unit_src = "";
605604
indentation_level = 0;
606605
indentation_spaces = 4;
607-
list_api->set_indentation(indentation_level, indentation_spaces);
608-
list_api->set_global_scope(global_scope);
606+
c_ds_api->set_indentation(indentation_level, indentation_spaces);
607+
c_ds_api->set_global_scope(global_scope);
609608
c_utils_functions->set_indentation(indentation_level, indentation_spaces);
610609
c_utils_functions->set_global_scope(global_scope);
611-
tuple_api->set_indentation(indentation_level, indentation_spaces);
612-
tuple_api->set_global_scope(global_scope);
613610

614611
std::string head =
615612
R"(
@@ -747,21 +744,15 @@ R"(
747744
for (auto s: headers) {
748745
to_include += "#include <" + s + ".h>\n";
749746
}
750-
if( list_api->get_list_func_decls().size() > 0 ) {
751-
array_types_decls += "\n" + list_api->get_list_func_decls() + "\n";
747+
if( c_ds_api->get_func_decls().size() > 0 ) {
748+
array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n";
752749
}
753750
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
754751
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
755752
}
756-
if( tuple_api->get_tuple_func_decls().size() > 0 ) {
757-
array_types_decls += "\n" + tuple_api->get_tuple_func_decls() + "\n";
758-
}
759-
std::string list_funcs_defined = "";
760-
if( list_api->get_generated_code().size() > 0 ) {
761-
list_funcs_defined = "\n" + list_api->get_generated_code() + "\n";
762-
}
763-
if( tuple_api->get_generated_code().size() > 0 ) {
764-
list_funcs_defined = "\n" + tuple_api->get_generated_code() + "\n";
753+
std::string ds_funcs_defined = "";
754+
if( c_ds_api->get_generated_code().size() > 0 ) {
755+
ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n";
765756
}
766757
std::string util_funcs_defined = "";
767758
if( c_utils_functions->get_generated_code().size() > 0 ) {
@@ -771,7 +762,7 @@ R"(
771762
head += strcat_def;
772763
}
773764
src = to_include + head + array_types_decls + unit_src +
774-
list_funcs_defined + util_funcs_defined;
765+
ds_funcs_defined + util_funcs_defined;
775766
}
776767

777768
void visit_Program(const ASR::Program_t &x) {

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
7878
size_t template_number;
7979
std::string from_std_vector_helper;
8080

81-
std::unique_ptr<CCPPList> list_api;
82-
std::unique_ptr<CCPPTuple> tuple_api;
81+
std::unique_ptr<CCPPDSUtils> c_ds_api;
8382
std::string const_name;
8483
size_t const_list_count;
8584

@@ -92,15 +91,13 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
9291
platform{platform},
9392
gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex},
9493
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
95-
template_number{0}, list_api{std::make_unique<CCPPList>()},
96-
tuple_api{std::make_unique<CCPPTuple>()}, const_name{"constname"},
94+
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>()},
95+
const_name{"constname"},
9796
const_list_count{0}, is_string_concat_present{false} {
9897
if( is_c ) {
99-
list_api->set_deepcopy_function(&CUtils::deepcopy);
100-
tuple_api->set_deepcopy_function(&CUtils::deepcopy);
98+
c_ds_api->set_deepcopy_function(&CUtils::deepcopy);
10199
} else {
102-
list_api->set_deepcopy_function(&CPPUtils::deepcopy);
103-
tuple_api->set_deepcopy_function(&CPPUtils::deepcopy);
100+
c_ds_api->set_deepcopy_function(&CPPUtils::deepcopy);
104101
}
105102
}
106103

@@ -112,10 +109,8 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
112109
std::string unit_src = "";
113110
indentation_level = 0;
114111
indentation_spaces = 4;
115-
list_api->set_indentation(indentation_level + 1, indentation_spaces);
116-
list_api->set_global_scope(global_scope);
117-
tuple_api->set_indentation(indentation_level + 1, indentation_spaces);
118-
tuple_api->set_global_scope(global_scope);
112+
c_ds_api->set_indentation(indentation_level + 1, indentation_spaces);
113+
c_ds_api->set_global_scope(global_scope);
119114

120115
std::string headers =
121116
R"(#include <stdio.h>
@@ -341,11 +336,10 @@ R"(#include <stdio.h>
341336
sub = "void* ";
342337
} else if (ASR::is_a<ASR::List_t>(*return_var->m_type)) {
343338
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(return_var->m_type);
344-
std::string list_element_type = CUtils::get_c_type_from_ttype_t(list_type->m_type);
345-
sub = list_api->get_list_type(list_type, list_element_type) + " ";
339+
sub = c_ds_api->get_list_type(list_type) + " ";
346340
} else if (ASR::is_a<ASR::Tuple_t>(*return_var->m_type)) {
347341
ASR::Tuple_t* tup_type = ASR::down_cast<ASR::Tuple_t>(return_var->m_type);
348-
sub = tuple_api->get_tuple_type(tup_type) + " ";
342+
sub = c_ds_api->get_tuple_type(tup_type) + " ";
349343
} else if (ASR::is_a<ASR::Const_t>(*return_var->m_type)) {
350344
ASR::Const_t* const_type = ASR::down_cast<ASR::Const_t>(return_var->m_type);
351345
std::string const_type_str = CUtils::get_c_type_from_ttype_t(const_type->m_type);
@@ -529,13 +523,12 @@ R"(#include <stdio.h>
529523
last_expr_precedence = 2;
530524
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
531525
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(x.m_type);
532-
std::string list_element_type = CUtils::get_c_type_from_ttype_t(list_type->m_type);
533526
const_name += std::to_string(const_list_count);
534527
const_list_count += 1;
535528
const_name = current_scope->get_unique_name(const_name);
536529
std::string indent(indentation_level*indentation_spaces, ' ');
537-
current_body += indent + list_api->get_list_type(list_type,
538-
list_element_type) + " " + const_name + " = " + src + ";\n";
530+
current_body += indent + c_ds_api->get_list_type(list_type) + " " +
531+
const_name + " = " + src + ";\n";
539532
src = const_name;
540533
}
541534
}
@@ -646,7 +639,7 @@ R"(#include <stdio.h>
646639
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value)) {
647640
self().visit_FunctionCall(*ASR::down_cast<ASR::FunctionCall_t>(x.m_value));
648641
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(tup_c->m_type);
649-
std::string tuple_type_c = tuple_api->get_tuple_type(t);
642+
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
650643
const_name += std::to_string(const_list_count);
651644
const_list_count += 1;
652645
const_name = current_scope->get_unique_name(const_name);
@@ -687,7 +680,7 @@ R"(#include <stdio.h>
687680
}
688681
if( is_target_list && is_value_list ) {
689682
ASR::List_t* list_target = ASR::down_cast<ASR::List_t>(ASRUtils::expr_type(x.m_target));
690-
std::string list_dc_func = list_api->get_list_deepcopy_func(list_target);
683+
std::string list_dc_func = c_ds_api->get_list_deepcopy_func(list_target);
691684
if( ASR::is_a<ASR::ListConstant_t>(*x.m_value) ) {
692685
src += value;
693686
src += indent + list_dc_func + "(&" + const_name + ", &" + target + ");\n\n";
@@ -773,11 +766,10 @@ R"(#include <stdio.h>
773766
const_list_count += 1;
774767
const_name = current_scope->get_unique_name(const_name);
775768
ASR::List_t* t = ASR::down_cast<ASR::List_t>(x.m_type);
776-
std::string list_element_type = CUtils::get_c_type_from_ttype_t(t->m_type);
777-
std::string list_type_c = list_api->get_list_type(t, list_element_type);
769+
std::string list_type_c = c_ds_api->get_list_type(t);
778770
std::string src_tmp = "";
779771
src_tmp += indent + list_type_c + " " + const_name + ";\n";
780-
std::string list_init_func = list_api->get_list_init_func(t);
772+
std::string list_init_func = c_ds_api->get_list_init_func(t);
781773
src_tmp += indent + list_init_func + "(&" + const_name + ", " +
782774
std::to_string(x.n_args) + ");\n";
783775
for( size_t i = 0; i < x.n_args; i++ ) {
@@ -802,7 +794,7 @@ R"(#include <stdio.h>
802794
const_list_count += 1;
803795
const_name = current_scope->get_unique_name(const_name);
804796
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(x.m_type);
805-
std::string tuple_type_c = tuple_api->get_tuple_type(t);
797+
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
806798
std::string src_tmp = "";
807799
src_tmp += indent + tuple_type_c + " " + const_name + ";\n";
808800
for (size_t i = 0; i < x.n_elements; i++) {
@@ -824,7 +816,7 @@ R"(#include <stdio.h>
824816
void visit_ListAppend(const ASR::ListAppend_t& x) {
825817
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
826818
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
827-
std::string list_append_func = list_api->get_list_append_func(t);
819+
std::string list_append_func = c_ds_api->get_list_append_func(t);
828820
self().visit_expr(*x.m_a);
829821
std::string list_var = std::move(src);
830822
self().visit_expr(*x.m_ele);
@@ -835,7 +827,7 @@ R"(#include <stdio.h>
835827

836828
void visit_ListConcat(const ASR::ListConcat_t& x) {
837829
ASR::List_t* t = ASR::down_cast<ASR::List_t>(x.m_type);
838-
std::string list_concat_func = list_api->get_list_concat_func(t);
830+
std::string list_concat_func = c_ds_api->get_list_concat_func(t);
839831
self().visit_expr(*x.m_left);
840832
std::string left = std::move(src);
841833
if (!ASR::is_a<ASR::ListConcat_t>(*x.m_left)) {
@@ -853,7 +845,7 @@ R"(#include <stdio.h>
853845
void visit_ListClear(const ASR::ListClear_t& x) {
854846
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
855847
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
856-
std::string list_clear_func = list_api->get_list_clear_func(t);
848+
std::string list_clear_func = c_ds_api->get_list_clear_func(t);
857849
self().visit_expr(*x.m_a);
858850
std::string list_var = std::move(src);
859851
std::string indent(indentation_level * indentation_spaces, ' ');
@@ -863,7 +855,7 @@ R"(#include <stdio.h>
863855
void visit_ListInsert(const ASR::ListInsert_t& x) {
864856
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
865857
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
866-
std::string list_insert_func = list_api->get_list_insert_func(t);
858+
std::string list_insert_func = c_ds_api->get_list_insert_func(t);
867859
self().visit_expr(*x.m_a);
868860
std::string list_var = std::move(src);
869861
self().visit_expr(*x.m_ele);
@@ -877,7 +869,7 @@ R"(#include <stdio.h>
877869
void visit_ListRemove(const ASR::ListRemove_t& x) {
878870
ASR::ttype_t* t_ttype = ASRUtils::expr_type(x.m_a);
879871
ASR::List_t* t = ASR::down_cast<ASR::List_t>(t_ttype);
880-
std::string list_remove_func = list_api->get_list_remove_func(t);
872+
std::string list_remove_func = c_ds_api->get_list_remove_func(t);
881873
self().visit_expr(*x.m_a);
882874
std::string list_var = std::move(src);
883875
self().visit_expr(*x.m_ele);

0 commit comments

Comments
 (0)