From 463540edc8281e8c1aa4cad7b7314648725ae194 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:21:35 +0530 Subject: [PATCH 1/6] Refactor: C: Define and use get_final_combined_src() --- src/libasr/codegen/asr_to_c.cpp | 51 ++---------------------------- src/libasr/codegen/asr_to_c_cpp.h | 52 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 49 deletions(-) diff --git a/src/libasr/codegen/asr_to_c.cpp b/src/libasr/codegen/asr_to_c.cpp index f25d565c46..ab0cb84e56 100644 --- a/src/libasr/codegen/asr_to_c.cpp +++ b/src/libasr/codegen/asr_to_c.cpp @@ -30,14 +30,11 @@ class ASRToCVisitor : public BaseCCPPVisitor { public: - std::unique_ptr c_utils_functions; - int counter; ASRToCVisitor(diag::Diagnostics &diag, CompilerOptions &co, int64_t default_lower_bound) : BaseCCPPVisitor(diag, co.platform, co, false, false, true, default_lower_bound), - c_utils_functions{std::make_unique()}, counter{0} { } @@ -602,12 +599,6 @@ R"( std::string indent(indentation_level * indentation_spaces, ' '); std::string tab(indentation_spaces, ' '); - std::string strcat_def = ""; - strcat_def += indent + "char* " + global_scope->get_unique_name("strcat_", false) + "(char* x, char* y) {\n"; - strcat_def += indent + tab + "char* str_tmp = (char*) malloc((strlen(x) + strlen(y) + 2) * sizeof(char));\n"; - strcat_def += indent + tab + "strcpy(str_tmp, x);\n"; - strcat_def += indent + tab + "return strcat(str_tmp, y);\n"; - strcat_def += indent + "}\n\n"; std::string unit_src_tmp; for (auto &item : x.m_global_scope->get_scope()) { @@ -700,48 +691,10 @@ R"( unit_src += src; } } - std::string to_include = ""; - for (auto &s: user_defines) { - to_include += "#define " + s + "\n"; - } - for (auto &s: headers) { - to_include += "#include <" + s + ">\n"; - } - for (auto &s: user_headers) { - to_include += "#include \"" + s + "\"\n"; - } - if( c_ds_api->get_func_decls().size() > 0 ) { - array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n"; - } - if( c_utils_functions->get_util_func_decls().size() > 0 ) { - array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n"; - } - std::string ds_funcs_defined = ""; - if( c_ds_api->get_generated_code().size() > 0 ) { - ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n"; - } - std::string util_funcs_defined = ""; - if( c_utils_functions->get_generated_code().size() > 0 ) { - util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n"; - } - if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) { - array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n"; - } - if( bind_py_utils_functions->get_generated_code().size() > 0 ) { - util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n"; - } - if( is_string_concat_present ) { - head += strcat_def; - } - // Include dimension_descriptor definition that is used by array types - if (array_types_decls.size() != 0) { - array_types_decls.insert(0, "struct dimension_descriptor\n" - "{\n int32_t lower_bound, length;\n};\n"); - } forward_decl_functions += "\n\n"; - src = to_include + head + array_types_decls + forward_decl_functions + unit_src + - ds_funcs_defined + util_funcs_defined; + src = get_final_combined_src(head, unit_src); + if (!emit_headers.empty()) { std::string to_includes_1 = ""; for (auto &s: headers) { diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index 4969740a29..b887fe5604 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -158,6 +158,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor std::string from_std_vector_helper; std::unique_ptr c_ds_api; + std::unique_ptr c_utils_functions; std::unique_ptr bind_py_utils_functions; std::string const_name; size_t const_vars_count; @@ -185,12 +186,63 @@ class BaseCCPPVisitor : public ASR::BaseVisitor gen_stdstring{gen_stdstring}, gen_stdcomplex{gen_stdcomplex}, is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound}, template_number{0}, c_ds_api{std::make_unique(is_c, platform)}, + c_utils_functions{std::make_unique()}, bind_py_utils_functions{std::make_unique()}, const_name{"constname"}, const_vars_count{0}, loop_end_count{0}, bracket_open{0}, is_string_concat_present{false} { } + std::string get_final_combined_src(std::string head, std::string unit_src) { + std::string to_include = ""; + for (auto &s: user_defines) { + to_include += "#define " + s + "\n"; + } + for (auto &s: headers) { + to_include += "#include <" + s + ">\n"; + } + for (auto &s: user_headers) { + to_include += "#include \"" + s + "\"\n"; + } + if( c_ds_api->get_func_decls().size() > 0 ) { + array_types_decls += "\n" + c_ds_api->get_func_decls() + "\n"; + } + if( c_utils_functions->get_util_func_decls().size() > 0 ) { + array_types_decls += "\n" + c_utils_functions->get_util_func_decls() + "\n"; + } + std::string ds_funcs_defined = ""; + if( c_ds_api->get_generated_code().size() > 0 ) { + ds_funcs_defined = "\n" + c_ds_api->get_generated_code() + "\n"; + } + std::string util_funcs_defined = ""; + if( c_utils_functions->get_generated_code().size() > 0 ) { + util_funcs_defined = "\n" + c_utils_functions->get_generated_code() + "\n"; + } + if( bind_py_utils_functions->get_util_func_decls().size() > 0 ) { + array_types_decls += "\n" + bind_py_utils_functions->get_util_func_decls() + "\n"; + } + if( bind_py_utils_functions->get_generated_code().size() > 0 ) { + util_funcs_defined = "\n" + bind_py_utils_functions->get_generated_code() + "\n"; + } + if( is_string_concat_present ) { + std::string strcat_def = ""; + strcat_def += " char* " + global_scope->get_unique_name("strcat_", false) + "(char* x, char* y) {\n"; + strcat_def += " char* str_tmp = (char*) malloc((strlen(x) + strlen(y) + 2) * sizeof(char));\n"; + strcat_def += " strcpy(str_tmp, x);\n"; + strcat_def += " return strcat(str_tmp, y);\n"; + strcat_def += " }\n\n"; + head += strcat_def; + } + + // Include dimension_descriptor definition that is used by array types + if (array_types_decls.size() != 0) { + array_types_decls = "\nstruct dimension_descriptor\n" + "{\n int32_t lower_bound, length;\n};\n" + array_types_decls; + } + + return to_include + head + array_types_decls + forward_decl_functions + unit_src + + ds_funcs_defined + util_funcs_defined; + } void visit_TranslationUnit(const ASR::TranslationUnit_t &x) { global_scope = x.m_global_scope; // All loose statements must be converted to a function, so the items From 88be2bb0c6227824eb066dd70562a1e80c8044e6 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:22:25 +0530 Subject: [PATCH 2/6] CPP: Support CUtils generated funs in CPP --- src/libasr/codegen/asr_to_cpp.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 6b77759541..9f7e64463e 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -73,16 +73,13 @@ class ASRToCPPVisitor : public BaseCCPPVisitor { public: - std::string array_types_decls; std::map>> eltypedims2arraytype; ASRToCPPVisitor(diag::Diagnostics &diag, CompilerOptions &co, int64_t default_lower_bound) : BaseCCPPVisitor(diag, co.platform, co, true, true, false, - default_lower_bound), - array_types_decls(std::string("\nstruct dimension_descriptor\n" - "{\n int32_t lower_bound, length;\n};\n")) {} + default_lower_bound) {} std::string convert_dims(size_t n_dims, ASR::dimension_t *m_dims, size_t& size) { @@ -327,11 +324,18 @@ class ASRToCPPVisitor : public BaseCCPPVisitor // All loose statements must be converted to a function, so the items // must be empty: LCOMPILERS_ASSERT(x.n_items == 0); - std::string unit_src = ""; indentation_level = 0; indentation_spaces = 4; - std::string headers = + SymbolTable* current_scope_copy = current_scope; + current_scope = global_scope; + c_ds_api->set_indentation(indentation_level, indentation_spaces); + c_ds_api->set_global_scope(global_scope); + c_utils_functions->set_indentation(indentation_level, indentation_spaces); + c_utils_functions->set_global_scope(global_scope); + c_ds_api->set_c_utils_functions(c_utils_functions.get()); + + std::string head = R"(#include #include #include @@ -356,7 +360,7 @@ Kokkos::View from_std_vector(const std::vector &v) // Pre-declare all functions first, then generate code // Otherwise some function might not be found. - unit_src += "// Forward declarations\n"; + std::string unit_src = "// Forward declarations\n"; unit_src += declare_all_functions(*x.m_global_scope); // Now pre-declare all functions from modules and programs for (auto &item : x.m_global_scope->get_scope()) { @@ -417,7 +421,8 @@ Kokkos::View from_std_vector(const std::vector &v) } } - src = headers + array_types_decls + unit_src; + src = get_final_combined_src(head, unit_src); + current_scope = current_scope_copy; } void visit_Program(const ASR::Program_t &x) { From 0ad8e54fda96a607877c93de35e8f365e5af3502 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:23:06 +0530 Subject: [PATCH 3/6] CPP: Support the list type --- src/libasr/codegen/asr_to_cpp.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/libasr/codegen/asr_to_cpp.cpp b/src/libasr/codegen/asr_to_cpp.cpp index 9f7e64463e..a7e9555904 100644 --- a/src/libasr/codegen/asr_to_cpp.cpp +++ b/src/libasr/codegen/asr_to_cpp.cpp @@ -300,6 +300,11 @@ class ASRToCPPVisitor : public BaseCCPPVisitor std::string encoded_type_name = "x" + der_type_name; std::string type_name = std::string("struct ") + der_type_name; handle_array(v.m_type, "struct", false) + } else if (ASR::is_a(*v.m_type)) { + ASR::List_t* t = ASR::down_cast(v_m_type); + std::string list_type_c = c_ds_api->get_list_type(t); + sub = format_type_c("", list_type_c, v.m_name, + false, false); } else { diag.codegen_error_label("Type number '" + std::to_string(v.m_type->type) From d90c5a7c963c014858ff9624d8511e12df83aca5 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:57:35 +0530 Subject: [PATCH 4/6] C_CPP: Forward decl only for C --- src/libasr/codegen/asr_to_c_cpp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libasr/codegen/asr_to_c_cpp.h b/src/libasr/codegen/asr_to_c_cpp.h index b887fe5604..9c5d478aed 100644 --- a/src/libasr/codegen/asr_to_c_cpp.h +++ b/src/libasr/codegen/asr_to_c_cpp.h @@ -617,7 +617,7 @@ R"(#include } func += ")"; bracket_open--; - if (f_type->m_abi == ASR::abiType::Source) { + if (is_c && f_type->m_abi == ASR::abiType::Source) { forward_decl_functions += func + ";\n"; } if( is_c || template_for_Kokkos.empty() ) { From 91905cf046fbd80669b5ad0086eefbf61b727a03 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:58:34 +0530 Subject: [PATCH 5/6] TEST: CPP: Add test_list_repeat2.py as ref test --- tests/tests.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/tests.toml b/tests/tests.toml index 7180531b45..19cec7d482 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -298,6 +298,10 @@ asr = true # integration_tests +[[test]] +filename = "../integration_tests/test_list_repeat2.py" +cpp = true + [[test]] filename = "../integration_tests/test_builtin.py" asr = true From 9860c3e5c024254d140466095b5fb87f404ca01f Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 12:00:21 +0530 Subject: [PATCH 6/6] TEST: Update reference tests --- tests/reference/c-expr_12-93c7780.json | 2 +- tests/reference/c-expr_12-93c7780.stdout | 1 + tests/reference/cpp-assert1-ba60925.json | 2 +- tests/reference/cpp-assert1-ba60925.stdout | 5 - tests/reference/cpp-expr12-fd2ea87.json | 2 +- tests/reference/cpp-expr12-fd2ea87.stdout | 5 - tests/reference/cpp-expr15-1661c0d.json | 2 +- tests/reference/cpp-expr15-1661c0d.stdout | 5 - tests/reference/cpp-expr2-09c05ad.json | 2 +- tests/reference/cpp-expr2-09c05ad.stdout | 5 - tests/reference/cpp-expr5-1de0e30.json | 2 +- tests/reference/cpp-expr5-1de0e30.stdout | 5 - tests/reference/cpp-expr6-f337f4f.json | 2 +- tests/reference/cpp-expr6-f337f4f.stdout | 5 - tests/reference/cpp-expr7-529bd53.json | 2 +- tests/reference/cpp-expr7-529bd53.stdout | 5 - tests/reference/cpp-expr8-704cece.json | 2 +- tests/reference/cpp-expr8-704cece.stdout | 5 - tests/reference/cpp-expr9-48868e9.json | 2 +- tests/reference/cpp-expr9-48868e9.stdout | 5 - tests/reference/cpp-expr_11-422c839.json | 2 +- tests/reference/cpp-expr_11-422c839.stdout | 5 - tests/reference/cpp-loop1-0a8cf3b.json | 2 +- tests/reference/cpp-loop1-0a8cf3b.stdout | 5 - tests/reference/cpp-loop3-6020091.json | 2 +- tests/reference/cpp-loop3-6020091.stdout | 5 - tests/reference/cpp-loop4-cdb2174.json | 2 +- tests/reference/cpp-loop4-cdb2174.stdout | 5 - tests/reference/cpp-print_01-026ef17.json | 2 +- tests/reference/cpp-print_01-026ef17.stdout | 5 - .../cpp-test_builtin_pow-56b3f92.json | 2 +- .../cpp-test_builtin_pow-56b3f92.stdout | 6 +- .../cpp-test_list_repeat2-698d7f4.json | 13 + .../cpp-test_list_repeat2-698d7f4.stdout | 256 ++++++++++++++++++ .../cpp-test_unary_op_03-fd9669a.json | 2 +- .../cpp-test_unary_op_03-fd9669a.stdout | 5 - 36 files changed, 288 insertions(+), 97 deletions(-) create mode 100644 tests/reference/cpp-test_list_repeat2-698d7f4.json create mode 100644 tests/reference/cpp-test_list_repeat2-698d7f4.stdout diff --git a/tests/reference/c-expr_12-93c7780.json b/tests/reference/c-expr_12-93c7780.json index 11969c5667..4ace38788e 100644 --- a/tests/reference/c-expr_12-93c7780.json +++ b/tests/reference/c-expr_12-93c7780.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "c-expr_12-93c7780.stdout", - "stdout_hash": "4ff750b49402a8f60ae61abdc16289388dfb3230995dda8b75fcd8cd", + "stdout_hash": "6162cc3c452b530aef96db7443c95a0db16bce78b119b9a4ccf91f6a", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/c-expr_12-93c7780.stdout b/tests/reference/c-expr_12-93c7780.stdout index 567498a526..7fbf21f513 100644 --- a/tests/reference/c-expr_12-93c7780.stdout +++ b/tests/reference/c-expr_12-93c7780.stdout @@ -6,6 +6,7 @@ #include #include + struct dimension_descriptor { int32_t lower_bound, length; diff --git a/tests/reference/cpp-assert1-ba60925.json b/tests/reference/cpp-assert1-ba60925.json index 10445d167c..9e0008db37 100644 --- a/tests/reference/cpp-assert1-ba60925.json +++ b/tests/reference/cpp-assert1-ba60925.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-assert1-ba60925.stdout", - "stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2", + "stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-assert1-ba60925.stdout b/tests/reference/cpp-assert1-ba60925.stdout index a479c7e856..ea744708f9 100644 --- a/tests/reference/cpp-assert1-ba60925.stdout +++ b/tests/reference/cpp-assert1-ba60925.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations namespace { } diff --git a/tests/reference/cpp-expr12-fd2ea87.json b/tests/reference/cpp-expr12-fd2ea87.json index a281fb596f..cb24f5bb57 100644 --- a/tests/reference/cpp-expr12-fd2ea87.json +++ b/tests/reference/cpp-expr12-fd2ea87.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr12-fd2ea87.stdout", - "stdout_hash": "aa612843d76d425d683903976105593cdfed30ae247de18e7b88e32c", + "stdout_hash": "ade4edd1e3586b786d3860b244053fe7c2f78e518b4392fe95626727", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr12-fd2ea87.stdout b/tests/reference/cpp-expr12-fd2ea87.stdout index d031bec64b..70f3de843d 100644 --- a/tests/reference/cpp-expr12-fd2ea87.stdout +++ b/tests/reference/cpp-expr12-fd2ea87.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); int32_t check(); diff --git a/tests/reference/cpp-expr15-1661c0d.json b/tests/reference/cpp-expr15-1661c0d.json index bcf0e23b62..3624ed5f08 100644 --- a/tests/reference/cpp-expr15-1661c0d.json +++ b/tests/reference/cpp-expr15-1661c0d.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr15-1661c0d.stdout", - "stdout_hash": "fd8cc5d59bdc0e053f8d5bc81aa076bcd34b0a95e03b985803057995", + "stdout_hash": "9f8482285dd48f735d4d59d18a878e7fca978188cef61719e9a2cad3", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr15-1661c0d.stdout b/tests/reference/cpp-expr15-1661c0d.stdout index 24c1136e04..dfb4a705bc 100644 --- a/tests/reference/cpp-expr15-1661c0d.stdout +++ b/tests/reference/cpp-expr15-1661c0d.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); double test1(); diff --git a/tests/reference/cpp-expr2-09c05ad.json b/tests/reference/cpp-expr2-09c05ad.json index daff141530..7f2a65d8fe 100644 --- a/tests/reference/cpp-expr2-09c05ad.json +++ b/tests/reference/cpp-expr2-09c05ad.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr2-09c05ad.stdout", - "stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2", + "stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr2-09c05ad.stdout b/tests/reference/cpp-expr2-09c05ad.stdout index a479c7e856..ea744708f9 100644 --- a/tests/reference/cpp-expr2-09c05ad.stdout +++ b/tests/reference/cpp-expr2-09c05ad.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations namespace { } diff --git a/tests/reference/cpp-expr5-1de0e30.json b/tests/reference/cpp-expr5-1de0e30.json index 14074debf6..0db3289f22 100644 --- a/tests/reference/cpp-expr5-1de0e30.json +++ b/tests/reference/cpp-expr5-1de0e30.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr5-1de0e30.stdout", - "stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2", + "stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr5-1de0e30.stdout b/tests/reference/cpp-expr5-1de0e30.stdout index a479c7e856..ea744708f9 100644 --- a/tests/reference/cpp-expr5-1de0e30.stdout +++ b/tests/reference/cpp-expr5-1de0e30.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations namespace { } diff --git a/tests/reference/cpp-expr6-f337f4f.json b/tests/reference/cpp-expr6-f337f4f.json index 7868d73950..8115193f00 100644 --- a/tests/reference/cpp-expr6-f337f4f.json +++ b/tests/reference/cpp-expr6-f337f4f.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr6-f337f4f.stdout", - "stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2", + "stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr6-f337f4f.stdout b/tests/reference/cpp-expr6-f337f4f.stdout index a479c7e856..ea744708f9 100644 --- a/tests/reference/cpp-expr6-f337f4f.stdout +++ b/tests/reference/cpp-expr6-f337f4f.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations namespace { } diff --git a/tests/reference/cpp-expr7-529bd53.json b/tests/reference/cpp-expr7-529bd53.json index 66aa86a880..41ddb51ccf 100644 --- a/tests/reference/cpp-expr7-529bd53.json +++ b/tests/reference/cpp-expr7-529bd53.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr7-529bd53.stdout", - "stdout_hash": "e6a69b9bce9fb3c85fd36e37f7fa56debd52c4b8440a7269950efea5", + "stdout_hash": "0ddb624844237f1333b49b580bb956d0f202df4fa834cff3dc8b4e75", "stderr": "cpp-expr7-529bd53.stderr", "stderr_hash": "6e9790ac88db1a9ead8f64a91ba8a6605de67167037908a74b77be0c", "returncode": 0 diff --git a/tests/reference/cpp-expr7-529bd53.stdout b/tests/reference/cpp-expr7-529bd53.stdout index 70b3d608d2..b56b58c2d4 100644 --- a/tests/reference/cpp-expr7-529bd53.stdout +++ b/tests/reference/cpp-expr7-529bd53.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void main0(); diff --git a/tests/reference/cpp-expr8-704cece.json b/tests/reference/cpp-expr8-704cece.json index 8f3d887f17..93e5f33d99 100644 --- a/tests/reference/cpp-expr8-704cece.json +++ b/tests/reference/cpp-expr8-704cece.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr8-704cece.stdout", - "stdout_hash": "0114926f9f033b9cbda860412fa9fadb1cebc84c57d89a6a6f6ce763", + "stdout_hash": "0f4e0ff2a1834e007029e4c961274f83da2e9111631f1df71ce989ac", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr8-704cece.stdout b/tests/reference/cpp-expr8-704cece.stdout index 115449c344..21488af77a 100644 --- a/tests/reference/cpp-expr8-704cece.stdout +++ b/tests/reference/cpp-expr8-704cece.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations float _lfortran_caimag(std::complex x); double _lfortran_zaimag(std::complex x); diff --git a/tests/reference/cpp-expr9-48868e9.json b/tests/reference/cpp-expr9-48868e9.json index 5df90ed76a..32db3b2790 100644 --- a/tests/reference/cpp-expr9-48868e9.json +++ b/tests/reference/cpp-expr9-48868e9.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr9-48868e9.stdout", - "stdout_hash": "371d774714e3c5db529b75d5aab6661e5708f516368103f912a7411b", + "stdout_hash": "539f9a3a88fb6c7d4af637faada51269bde7815558614a4938561dde", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr9-48868e9.stdout b/tests/reference/cpp-expr9-48868e9.stdout index 0a2a657f13..e74dc0e643 100644 --- a/tests/reference/cpp-expr9-48868e9.stdout +++ b/tests/reference/cpp-expr9-48868e9.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void main0(); diff --git a/tests/reference/cpp-expr_11-422c839.json b/tests/reference/cpp-expr_11-422c839.json index 9b27b77930..6429a0e9d7 100644 --- a/tests/reference/cpp-expr_11-422c839.json +++ b/tests/reference/cpp-expr_11-422c839.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-expr_11-422c839.stdout", - "stdout_hash": "999fc082abb09a070b2dbd0d5bc7d0ef69488fcad0a87f440cb08280", + "stdout_hash": "ee523e892c8a9e54cc670f9305d4c791e165b79679b71f44e6d9167c", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-expr_11-422c839.stdout b/tests/reference/cpp-expr_11-422c839.stdout index aa4e6fbe88..faa0a85b47 100644 --- a/tests/reference/cpp-expr_11-422c839.stdout +++ b/tests/reference/cpp-expr_11-422c839.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void f(); diff --git a/tests/reference/cpp-loop1-0a8cf3b.json b/tests/reference/cpp-loop1-0a8cf3b.json index d3f6226c98..94a14b81c0 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.json +++ b/tests/reference/cpp-loop1-0a8cf3b.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop1-0a8cf3b.stdout", - "stdout_hash": "4c75cec53e90b86c74544ce8998f69c7d9c79452b52ab8bfdcafc4e2", + "stdout_hash": "c202de96b07e1cf07918733e723bd99ea3315075e50e1b4951eaa895", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop1-0a8cf3b.stdout b/tests/reference/cpp-loop1-0a8cf3b.stdout index fc3e68ec68..a1cec8c0d8 100644 --- a/tests/reference/cpp-loop1-0a8cf3b.stdout +++ b/tests/reference/cpp-loop1-0a8cf3b.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void main0(); diff --git a/tests/reference/cpp-loop3-6020091.json b/tests/reference/cpp-loop3-6020091.json index ccdcf22d88..8705b64cc5 100644 --- a/tests/reference/cpp-loop3-6020091.json +++ b/tests/reference/cpp-loop3-6020091.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop3-6020091.stdout", - "stdout_hash": "407c745929ee126426a33b3ba8f73b8f326d259e714ac66b078befe2", + "stdout_hash": "7ac638e8146f048bd5444436ee2b2ac4f85ffa7a1d791cf526adacb4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop3-6020091.stdout b/tests/reference/cpp-loop3-6020091.stdout index a479c7e856..ea744708f9 100644 --- a/tests/reference/cpp-loop3-6020091.stdout +++ b/tests/reference/cpp-loop3-6020091.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations namespace { } diff --git a/tests/reference/cpp-loop4-cdb2174.json b/tests/reference/cpp-loop4-cdb2174.json index 4b4e5c6720..7f8d0fcd8e 100644 --- a/tests/reference/cpp-loop4-cdb2174.json +++ b/tests/reference/cpp-loop4-cdb2174.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-loop4-cdb2174.stdout", - "stdout_hash": "ee9611ec973970d72e0dc93fceaeb3a5c15d714b8a7dde370023b01c", + "stdout_hash": "035b662f52288e24d70badd9d31be5732b0dab53842213af73de9ffe", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-loop4-cdb2174.stdout b/tests/reference/cpp-loop4-cdb2174.stdout index 5531d6062f..bf0a3e86e8 100644 --- a/tests/reference/cpp-loop4-cdb2174.stdout +++ b/tests/reference/cpp-loop4-cdb2174.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void test_for(); diff --git a/tests/reference/cpp-print_01-026ef17.json b/tests/reference/cpp-print_01-026ef17.json index 3c5eb6edb4..4f718f43b0 100644 --- a/tests/reference/cpp-print_01-026ef17.json +++ b/tests/reference/cpp-print_01-026ef17.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-print_01-026ef17.stdout", - "stdout_hash": "a8fd58187340563f530a1d5fc2c7291b959de59068731d60083f5bba", + "stdout_hash": "5c0f2040403d427b91360a090c99838d85416907ce1e3c4e7ae1906b", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-print_01-026ef17.stdout b/tests/reference/cpp-print_01-026ef17.stdout index 65f4ee5824..5309a6848e 100644 --- a/tests/reference/cpp-print_01-026ef17.stdout +++ b/tests/reference/cpp-print_01-026ef17.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void f(); diff --git a/tests/reference/cpp-test_builtin_pow-56b3f92.json b/tests/reference/cpp-test_builtin_pow-56b3f92.json index 440be19917..bd2d753467 100644 --- a/tests/reference/cpp-test_builtin_pow-56b3f92.json +++ b/tests/reference/cpp-test_builtin_pow-56b3f92.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-test_builtin_pow-56b3f92.stdout", - "stdout_hash": "a23deac4907f0e4994c37e965d39ad3cb583c42c4c4f0a3c279bed60", + "stdout_hash": "0097d37ea4d51dd804fdf434749b0574e2fa754fbac6295e7373cfed", "stderr": "cpp-test_builtin_pow-56b3f92.stderr", "stderr_hash": "859ce76c74748f2d32c7eab92cfbba789a78d4cbf5818646b99806ea", "returncode": 0 diff --git a/tests/reference/cpp-test_builtin_pow-56b3f92.stdout b/tests/reference/cpp-test_builtin_pow-56b3f92.stdout index 809283e385..dbadfad5ee 100644 --- a/tests/reference/cpp-test_builtin_pow-56b3f92.stdout +++ b/tests/reference/cpp-test_builtin_pow-56b3f92.stdout @@ -1,3 +1,4 @@ +#include #include #include #include @@ -17,11 +18,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void test_pow(); diff --git a/tests/reference/cpp-test_list_repeat2-698d7f4.json b/tests/reference/cpp-test_list_repeat2-698d7f4.json new file mode 100644 index 0000000000..c7014a4164 --- /dev/null +++ b/tests/reference/cpp-test_list_repeat2-698d7f4.json @@ -0,0 +1,13 @@ +{ + "basename": "cpp-test_list_repeat2-698d7f4", + "cmd": "lpython --no-color --show-cpp {infile}", + "infile": "tests/../integration_tests/test_list_repeat2.py", + "infile_hash": "3f1fa9fd655b76efa451a83734ddc61af0a917090588ef53afe30b04", + "outfile": null, + "outfile_hash": null, + "stdout": "cpp-test_list_repeat2-698d7f4.stdout", + "stdout_hash": "14efeab8658ae9018f5e5c846626c48309521131d0506519fc810845", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/cpp-test_list_repeat2-698d7f4.stdout b/tests/reference/cpp-test_list_repeat2-698d7f4.stdout new file mode 100644 index 0000000000..621c2e8b4e --- /dev/null +++ b/tests/reference/cpp-test_list_repeat2-698d7f4.stdout @@ -0,0 +1,256 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +template +Kokkos::View from_std_vector(const std::vector &v) +{ + Kokkos::View r("r", v.size()); + for (size_t i=0; i < v.size(); i++) { + r(i) = v[i]; + } + return r; +} + + +struct dimension_descriptor +{ + int32_t lower_bound, length; +}; + +struct list_r32 { + int32_t capacity; + int32_t current_end_point; + float* data; +}; + +inline bool compare_list_r32_(struct list_r32 a, struct list_r32 b); +inline bool compare_r32(float a, float b); +inline void print_list_r32_(struct list_r32 a); +inline void print_r32(float a); +inline void list_init_r32(struct list_r32* x, int32_t capacity); +inline void list_deepcopy_r32(struct list_r32* src, struct list_r32* dest); +inline void resize_if_needed_r32(struct list_r32* x); +inline void list_append_r32(struct list_r32* x, float element); +inline void list_insert_r32(struct list_r32* x, int pos, float element); +inline int list_find_item_r32(struct list_r32* x, float element); +inline void list_remove_r32(struct list_r32* x, float element); +inline void list_clear_r32(struct list_r32* x); +inline struct list_r32* list_concat_r32(struct list_r32* left, struct list_r32* right); +inline struct list_r32* list_repeat_r32(struct list_r32* x, int32_t freq); +inline struct list_r32* list_section_r32(struct list_r32* x, int32_t idx1, int32_t idx2, int32_t step, bool i1_present, bool i2_present); + +// Forward declarations +void __main____global_statements(); +float add_list(struct list_r32 x); +struct list_r32 create_list(int32_t n); +void main0(); +namespace { +} + +// Implementations +struct list_r32 create_list(int32_t n) +{ + struct list_r32 _lpython_return_variable; + int32_t i; + struct list_r32 x; + struct list_r32 constname0; + list_init_r32(&constname0, 1); + constname0.data[0] = 0.00000000000000000e+00; + constname0.current_end_point = 1; + list_deepcopy_r32(&(*list_repeat_r32(&constname0, n)), &x); + + for (i=0; i<=n - 1; i++) { + x.data[i] = (float)(i); + } + list_deepcopy_r32(&x, &_lpython_return_variable); + + return _lpython_return_variable; +} + +float add_list(struct list_r32 x) +{ + float _lpython_return_variable; + int32_t i; + float sum; + sum = 0.00000000000000000e+00; + for (i=0; i<=x.current_end_point - 1; i++) { + sum = sum + x.data[i]; + } + _lpython_return_variable = sum; + return _lpython_return_variable; +} + +void main0() +{ + struct list_r32 x; + struct list_r32 constname01 = create_list(10); + list_deepcopy_r32(&constname01, &x); + + std::cout << add_list(x) << std::endl; +} + +void __main____global_statements() +{ + main0(); +} + +namespace { + +void main2() { + __main____global_statements(); +} + +} +int main(int argc, char* argv[]) +{ + Kokkos::initialize(argc, argv); + main2(); + Kokkos::finalize(); + return 0; +} + +bool compare_r32(float a, float b) { + return a == b; +} + +bool compare_list_r32_(struct list_r32 a, struct list_r32 b) { + if (a.current_end_point != b.current_end_point) + return false; + for (int i=0; icapacity = capacity; + x->current_end_point = 0; + x->data = (float*) malloc(capacity * sizeof(float)); +} + +void list_deepcopy_r32(struct list_r32* src, struct list_r32* dest) { + dest->capacity = src->capacity; + dest->current_end_point = src->current_end_point; + dest->data = (float*) malloc(src->capacity * sizeof(float)); + memcpy(dest->data, src->data, src->capacity * sizeof(float)); +} + +void resize_if_needed_r32(struct list_r32* x) { + if (x->capacity == x->current_end_point) { + x->capacity = 2 * x->capacity + 1; + x->data = (float*) realloc(x->data, x->capacity * sizeof(float)); + } +} + +void list_append_r32(struct list_r32* x, float element) { + resize_if_needed_r32(x); + x->data[x->current_end_point] = element; + x->current_end_point += 1; +} + +void list_insert_r32(struct list_r32* x, int pos, float element) { + resize_if_needed_r32(x); + int pos_ptr = pos; + float tmp_ptr = x->data[pos]; + float tmp; + while (x->current_end_point > pos_ptr) { + tmp = x->data[pos_ptr + 1]; + x->data[pos_ptr + 1] = tmp_ptr; + tmp_ptr = tmp; + pos_ptr++; + } + + x->data[pos] = element; + x->current_end_point += 1; +} + +int list_find_item_r32(struct list_r32* x, float element) { + int el_pos = 0; + while (x->current_end_point > el_pos) { + if (compare_r32(x->data[el_pos], element)) return el_pos; + el_pos++; + } + return -1; +} + +void list_remove_r32(struct list_r32* x, float element) { + int el_pos = list_find_item_r32(x, element); + while (x->current_end_point > el_pos) { + int tmp = el_pos + 1; + x->data[el_pos] = x->data[tmp]; + el_pos = tmp; + } + x->current_end_point -= 1; +} + +void list_clear_r32(struct list_r32* x) { + free(x->data); + x->capacity = 4; + x->current_end_point = 0; + x->data = (float*) malloc(x->capacity * sizeof(float)); +} + +struct list_r32* list_concat_r32(struct list_r32* left, struct list_r32* right) { + struct list_r32 *result = (struct list_r32*)malloc(sizeof(struct list_r32)); + list_init_r32(result, left->current_end_point + right->current_end_point); + memcpy(result->data, left->data, left->current_end_point * sizeof(float)); + memcpy(result->data + left->current_end_point, right->data, right->current_end_point * sizeof(float)); + result->current_end_point = left->current_end_point + right->current_end_point; + return result; +} + +struct list_r32* list_repeat_r32(struct list_r32* x, int32_t freq) { + struct list_r32 *result = (struct list_r32*)malloc(sizeof(struct list_r32)); + list_init_r32(result, x->current_end_point * freq); + for (int i=0; idata[i*x->current_end_point], x->data, x->current_end_point * sizeof(float)); + } + result->current_end_point = x->current_end_point * freq; + return result; +} + +struct list_r32* list_section_r32(struct list_r32* x, int32_t idx1, int32_t idx2, int32_t step, bool i1_present, bool i2_present) { + int s_len = x->current_end_point; + if (step == 0) { + printf("slice step cannot be zero"); + exit(1); + } + idx1 = idx1 < 0 ? idx1 + s_len : idx1; + idx2 = idx2 < 0 ? idx2 + s_len : idx2; + idx1 = i1_present ? idx1 : (step > 0 ? 0 : s_len-1); + idx2 = i2_present ? idx2 : (step > 0 ? s_len : -1); + idx2 = step > 0 ? (idx2 > s_len ? s_len : idx2) : idx2; + idx1 = step < 0 ? (idx1 >= s_len ? s_len-1 : idx1) : idx1; + struct list_r32 *__tmp = (struct list_r32*) malloc(sizeof(struct list_r32)); + list_init_r32(__tmp, 4); + int s_i = idx1; + while((step > 0 && s_i >= idx1 && s_i < idx2) || + (step < 0 && s_i <= idx1 && s_i > idx2)) { + list_append_r32(__tmp, x->data[s_i]); + s_i+=step; + } + return __tmp; +} + + diff --git a/tests/reference/cpp-test_unary_op_03-fd9669a.json b/tests/reference/cpp-test_unary_op_03-fd9669a.json index e302d48b0b..2695ec5011 100644 --- a/tests/reference/cpp-test_unary_op_03-fd9669a.json +++ b/tests/reference/cpp-test_unary_op_03-fd9669a.json @@ -6,7 +6,7 @@ "outfile": null, "outfile_hash": null, "stdout": "cpp-test_unary_op_03-fd9669a.stdout", - "stdout_hash": "1105b1727159860e90e30c9035022ba0b63f471aa698f31717c8b826", + "stdout_hash": "aff27257321144de834a75c76c4782e9890acb6e963a7628c6e173f4", "stderr": null, "stderr_hash": null, "returncode": 0 diff --git a/tests/reference/cpp-test_unary_op_03-fd9669a.stdout b/tests/reference/cpp-test_unary_op_03-fd9669a.stdout index 312d1950e5..c1090bdb3d 100644 --- a/tests/reference/cpp-test_unary_op_03-fd9669a.stdout +++ b/tests/reference/cpp-test_unary_op_03-fd9669a.stdout @@ -17,11 +17,6 @@ Kokkos::View from_std_vector(const std::vector &v) return r; } - -struct dimension_descriptor -{ - int32_t lower_bound, length; -}; // Forward declarations void __main____global_statements(); void f();