Skip to content

Commit

Permalink
Refactor: C: Define and use get_final_combined_src()
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaikh-Ubaid committed Jul 30, 2023
1 parent 0580c44 commit 463540e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 49 deletions.
51 changes: 2 additions & 49 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
{
public:

std::unique_ptr<CUtils::CUtilFunctions> 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<CUtils::CUtilFunctions>()},
counter{0} {
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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) {
Expand Down
52 changes: 52 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
std::string from_std_vector_helper;

std::unique_ptr<CCPPDSUtils> c_ds_api;
std::unique_ptr<CUtils::CUtilFunctions> c_utils_functions;
std::unique_ptr<BindPyUtils::BindPyUtilFunctions> bind_py_utils_functions;
std::string const_name;
size_t const_vars_count;
Expand Down Expand Up @@ -185,12 +186,63 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
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<CCPPDSUtils>(is_c, platform)},
c_utils_functions{std::make_unique<CUtils::CUtilFunctions>()},
bind_py_utils_functions{std::make_unique<BindPyUtils::BindPyUtilFunctions>()},
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
Expand Down

0 comments on commit 463540e

Please sign in to comment.