From 463540edc8281e8c1aa4cad7b7314648725ae194 Mon Sep 17 00:00:00 2001 From: Shaikh Ubaid Date: Sun, 30 Jul 2023 11:21:35 +0530 Subject: [PATCH] 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