Skip to content

Commit 4b8d1ac

Browse files
authored
Merge pull request #1207 from Smit-create/c_tuple
C: Add Tuple support
2 parents 87ca4fe + ad663d6 commit 4b8d1ac

File tree

5 files changed

+737
-519
lines changed

5 files changed

+737
-519
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ RUN(NAME test_list_07 LABELS cpython llvm)
184184
RUN(NAME test_list_08 LABELS cpython llvm)
185185
RUN(NAME test_list_09 LABELS cpython llvm c)
186186
RUN(NAME test_list_10 LABELS cpython llvm)
187-
RUN(NAME test_tuple_01 LABELS cpython llvm)
188-
RUN(NAME test_tuple_02 LABELS cpython llvm)
187+
RUN(NAME test_tuple_01 LABELS cpython llvm c)
188+
RUN(NAME test_tuple_02 LABELS cpython llvm c)
189189
RUN(NAME test_dict_01 LABELS cpython llvm)
190190
RUN(NAME test_dict_02 LABELS cpython llvm)
191191
RUN(NAME test_dict_03 LABELS cpython llvm)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <libasr/containers.h>
66
#include <libasr/codegen/asr_to_c.h>
77
#include <libasr/codegen/asr_to_c_cpp.h>
8+
#include <libasr/codegen/c_utils.h>
89
#include <libasr/exception.h>
910
#include <libasr/asr_utils.h>
1011
#include <libasr/string_utils.h>
@@ -227,7 +228,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
227228
}
228229
}
229230
if( size == 0 ) {
230-
std::string element_type_str = get_c_type_from_ttype_t(element_type);
231+
std::string element_type_str = CUtils::get_c_type_from_ttype_t(element_type);
231232
dims = "(" + element_type_str + "*)" + " malloc(sizeof(" + element_type_str + ")" + array_size + ")";
232233
is_fixed_size = false;
233234
return dims;
@@ -522,10 +523,15 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
522523
}
523524
} else if (ASR::is_a<ASR::List_t>(*v.m_type)) {
524525
ASR::List_t* t = ASR::down_cast<ASR::List_t>(v.m_type);
525-
std::string list_element_type = get_c_type_from_ttype_t(t->m_type);
526+
std::string list_element_type = CUtils::get_c_type_from_ttype_t(t->m_type);
526527
std::string list_type_c = list_api->get_list_type(t, list_element_type);
527528
sub = format_type_c("", list_type_c, v.m_name,
528529
false, false);
530+
} else if (ASR::is_a<ASR::Tuple_t>(*v.m_type)) {
531+
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(v.m_type);
532+
std::string tuple_type_c = tuple_api->get_tuple_type(t);
533+
sub = format_type_c("", tuple_type_c, v.m_name,
534+
false, false);
529535
} else if (ASR::is_a<ASR::CPtr_t>(*v.m_type)) {
530536
sub = format_type_c("", "void*", v.m_name, false, false);
531537
} else if (ASR::is_a<ASR::Const_t>(*v.m_type)) {
@@ -535,7 +541,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
535541
sub = "#define " + std::string(v.m_name) + " " + src + "\n";
536542
return sub;
537543
} else {
538-
std::string const_underlying_type = get_c_type_from_ttype_t(
544+
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
539545
ASR::down_cast<ASR::Const_t>(v.m_type)->m_type);
540546
sub = format_type_c("", "const " + const_underlying_type + " ",
541547
v.m_name, false, false);
@@ -576,6 +582,8 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
576582
list_api->set_global_scope(global_scope);
577583
c_utils_functions->set_indentation(indentation_level, indentation_spaces);
578584
c_utils_functions->set_global_scope(global_scope);
585+
tuple_api->set_indentation(indentation_level, indentation_spaces);
586+
tuple_api->set_global_scope(global_scope);
579587

580588
std::string head =
581589
R"(
@@ -715,10 +723,16 @@ R"(
715723
if( c_utils_functions->get_util_func_decls().size() > 0 ) {
716724
array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n";
717725
}
726+
if( tuple_api->get_tuple_func_decls().size() > 0 ) {
727+
array_types_decls += "\n" + tuple_api->get_tuple_func_decls() + "\n";
728+
}
718729
std::string list_funcs_defined = "";
719730
if( list_api->get_generated_code().size() > 0 ) {
720731
list_funcs_defined = "\n" + list_api->get_generated_code() + "\n";
721732
}
733+
if( tuple_api->get_generated_code().size() > 0 ) {
734+
list_funcs_defined = "\n" + tuple_api->get_generated_code() + "\n";
735+
}
722736
std::string util_funcs_defined = "";
723737
if( c_utils_functions->get_generated_code().size() > 0 ) {
724738
util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n";
@@ -988,6 +1002,9 @@ R"(
9881002
case ASR::ttypeType::CPtr: {
9891003
return "%p";
9901004
}
1005+
case ASR::ttypeType::Complex: {
1006+
return "(%f, %f)";
1007+
}
9911008
case ASR::ttypeType::Pointer: {
9921009
if( !deref_ptr ) {
9931010
return "%p";
@@ -1027,6 +1044,11 @@ R"(
10271044
ASR::ttype_t* value_type = ASRUtils::expr_type(x.m_values[i]);
10281045
out += get_print_type(value_type, ASR::is_a<ASR::ArrayItem_t>(*x.m_values[i]));
10291046
v.push_back(src);
1047+
if (value_type->type == ASR::ttypeType::Complex) {
1048+
v.pop_back();
1049+
v.push_back("creal(" + src + ")");
1050+
v.push_back("cimag(" + src + ")");
1051+
}
10301052
if (i+1!=x.n_values) {
10311053
out += "\%s";
10321054
v.push_back(separator);
@@ -1052,7 +1074,7 @@ R"(
10521074
visit_expr(*x.m_v);
10531075
std::string var_name = src;
10541076
std::string args = "";
1055-
std::string result_type = get_c_type_from_ttype_t(x.m_type);
1077+
std::string result_type = CUtils::get_c_type_from_ttype_t(x.m_type);
10561078
if (x.m_dim == nullptr) {
10571079
std::string array_size_func = c_utils_functions->get_array_size();
10581080
ASR::dimension_t* m_dims = nullptr;
@@ -1072,13 +1094,13 @@ R"(
10721094
std::string shape = src;
10731095

10741096
ASR::ttype_t* array_type_asr = ASRUtils::expr_type(x.m_array);
1075-
std::string array_type_name = get_c_type_from_ttype_t(array_type_asr);
1097+
std::string array_type_name = CUtils::get_c_type_from_ttype_t(array_type_asr);
10761098
std::string array_encoded_type_name = ASRUtils::get_type_code(array_type_asr, true, false);
10771099
std::string array_type = get_array_type(array_type_name, array_encoded_type_name, true);
10781100
std::string return_type = get_array_type(array_type_name, array_encoded_type_name, false);
10791101

10801102
ASR::ttype_t* shape_type_asr = ASRUtils::expr_type(x.m_shape);
1081-
std::string shape_type_name = get_c_type_from_ttype_t(shape_type_asr);
1103+
std::string shape_type_name = CUtils::get_c_type_from_ttype_t(shape_type_asr);
10821104
std::string shape_encoded_type_name = ASRUtils::get_type_code(shape_type_asr, true, false);
10831105
std::string shape_type = get_array_type(shape_type_name, shape_encoded_type_name, true);
10841106

@@ -1091,7 +1113,7 @@ R"(
10911113
visit_expr(*x.m_v);
10921114
std::string var_name = src;
10931115
std::string args = "";
1094-
std::string result_type = get_c_type_from_ttype_t(x.m_type);
1116+
std::string result_type = CUtils::get_c_type_from_ttype_t(x.m_type);
10951117
visit_expr(*x.m_dim);
10961118
std::string idx = src;
10971119
if( x.m_bound == ASR::arrayboundType::LBound ) {
@@ -1116,7 +1138,7 @@ R"(
11161138
array_const.pop_back();
11171139

11181140
ASR::ttype_t* array_type_asr = x.m_type;
1119-
std::string array_type_name = get_c_type_from_ttype_t(array_type_asr);
1141+
std::string array_type_name = CUtils::get_c_type_from_ttype_t(array_type_asr);
11201142
std::string array_encoded_type_name = ASRUtils::get_type_code(array_type_asr, true, false);
11211143
std::string return_type = get_array_type(array_type_name, array_encoded_type_name, false);
11221144

0 commit comments

Comments
 (0)