Skip to content

C: Fix const var names and their usage #1318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
bool intrinsic_module = false;
const ASR::Function_t *current_function = nullptr;
std::map<uint64_t, SymbolInfo> sym_info;
std::map<uint64_t, std::string> const_var_names;

// Output configuration:
// Use std::string or char*
Expand All @@ -80,7 +81,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>

std::unique_ptr<CCPPDSUtils> c_ds_api;
std::string const_name;
size_t const_list_count;
size_t const_vars_count;

SymbolTable* current_scope;
bool is_string_concat_present;
Expand All @@ -93,7 +94,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>
is_c{is_c}, global_scope{nullptr}, lower_bound{default_lower_bound},
template_number{0}, c_ds_api{std::make_unique<CCPPDSUtils>(is_c)},
const_name{"constname"},
const_list_count{0}, is_string_concat_present{false} {
const_vars_count{0}, is_string_concat_present{false} {
}

void visit_TranslationUnit(const ASR::TranslationUnit_t &x) {
Expand Down Expand Up @@ -518,8 +519,8 @@ R"(#include <stdio.h>
last_expr_precedence = 2;
if( ASR::is_a<ASR::List_t>(*x.m_type) ) {
ASR::List_t* list_type = ASR::down_cast<ASR::List_t>(x.m_type);
const_name += std::to_string(const_list_count);
const_list_count += 1;
const_name += std::to_string(const_vars_count);
const_vars_count += 1;
const_name = current_scope->get_unique_name(const_name);
std::string indent(indentation_level*indentation_spaces, ' ');
current_body += indent + c_ds_api->get_list_type(list_type) + " " +
Expand Down Expand Up @@ -628,15 +629,16 @@ R"(#include <stdio.h>
ASR::TupleConstant_t *tup_c = ASR::down_cast<ASR::TupleConstant_t>(x.m_target);
std::string src_tmp = "", val_name = "";
if (ASR::is_a<ASR::TupleConstant_t>(*x.m_value)) {
val_name = const_name + std::to_string(const_list_count);
self().visit_TupleConstant(*ASR::down_cast<ASR::TupleConstant_t>(x.m_value));
ASR::TupleConstant_t *tup_const = ASR::down_cast<ASR::TupleConstant_t>(x.m_value);
self().visit_TupleConstant(*tup_const);
val_name = const_var_names[get_hash((ASR::asr_t*)tup_const)];
src_tmp += src;
} else if (ASR::is_a<ASR::FunctionCall_t>(*x.m_value)) {
self().visit_FunctionCall(*ASR::down_cast<ASR::FunctionCall_t>(x.m_value));
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(tup_c->m_type);
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
const_name += std::to_string(const_list_count);
const_list_count += 1;
const_name += std::to_string(const_vars_count);
const_vars_count += 1;
const_name = current_scope->get_unique_name(const_name);
src_tmp += indent + tuple_type_c + " " + const_name + " = " + src + ";\n";
val_name = const_name;
Expand Down Expand Up @@ -687,7 +689,9 @@ R"(#include <stdio.h>
std::string list_dc_func = c_ds_api->get_list_deepcopy_func(list_target);
if( ASR::is_a<ASR::ListConstant_t>(*x.m_value) ) {
src += value;
src += indent + list_dc_func + "(&" + const_name + ", &" + target + ");\n\n";
ASR::ListConstant_t *l_const = ASR::down_cast<ASR::ListConstant_t>(x.m_value);
std::string var_name = const_var_names[get_hash((ASR::asr_t*)l_const)];
src += indent + list_dc_func + "(&" + var_name + ", &" + target + ");\n\n";
} else if (ASR::is_a<ASR::ListConcat_t>(*x.m_value)) {
src += indent + list_dc_func + "(" + value + ", &" + target + ");\n\n";
} else {
Expand Down Expand Up @@ -768,39 +772,42 @@ R"(#include <stdio.h>
void visit_ListConstant(const ASR::ListConstant_t& x) {
std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
const_name += std::to_string(const_list_count);
const_list_count += 1;
const_name += std::to_string(const_vars_count);
const_vars_count += 1;
const_name = current_scope->get_unique_name(const_name);
std::string var_name = const_name;
const_var_names[get_hash((ASR::asr_t*)&x)] = var_name;
ASR::List_t* t = ASR::down_cast<ASR::List_t>(x.m_type);
std::string list_type_c = c_ds_api->get_list_type(t);
std::string src_tmp = "";
src_tmp += indent + list_type_c + " " + const_name + ";\n";
src_tmp += indent + list_type_c + " " + var_name + ";\n";
std::string list_init_func = c_ds_api->get_list_init_func(t);
src_tmp += indent + list_init_func + "(&" + const_name + ", " +
src_tmp += indent + list_init_func + "(&" + var_name + ", " +
std::to_string(x.n_args) + ");\n";
for( size_t i = 0; i < x.n_args; i++ ) {
self().visit_expr(*x.m_args[i]);
if( ASR::is_a<ASR::Character_t>(*t->m_type) ) {
src_tmp += const_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
src_tmp += var_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
}
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type, src,
const_name + ".data[" + std::to_string(i) +"]") + "\n";
var_name + ".data[" + std::to_string(i) +"]") + "\n";
}
src_tmp += indent + const_name + ".current_end_point = " + std::to_string(x.n_args) + ";\n";
src_tmp += indent + var_name + ".current_end_point = " + std::to_string(x.n_args) + ";\n";
src = src_tmp;
}

void visit_TupleConstant(const ASR::TupleConstant_t& x) {
std::string indent(indentation_level * indentation_spaces, ' ');
std::string tab(indentation_spaces, ' ');
const_name += std::to_string(const_list_count);
const_list_count += 1;
const_name += std::to_string(const_vars_count);
const_vars_count += 1;
const_name = current_scope->get_unique_name(const_name);
std::string var_name = const_name;
const_var_names[get_hash((ASR::asr_t*)&x)] = var_name;
ASR::Tuple_t* t = ASR::down_cast<ASR::Tuple_t>(x.m_type);
std::string tuple_type_c = c_ds_api->get_tuple_type(t);
std::string src_tmp = "";
src_tmp += indent + tuple_type_c + " " + const_name + ";\n";
src_tmp += indent + tuple_type_c + " " + var_name + ";\n";
for (size_t i = 0; i < x.n_elements; i++) {
self().visit_expr(*x.m_elements[i]);
std::string ele = ".element_" + std::to_string(i);
Expand Down