Skip to content
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

C: Use unique names for variables #2142

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,7 @@ int main(int argc, char *argv[])
bool print_targets = false;
bool print_rtl_header_dir = false;
bool print_rtl_dir = false;
bool separate_compilation = false;

std::string arg_fmt_file;
// int arg_fmt_indent = 4;
Expand Down Expand Up @@ -1581,7 +1582,7 @@ int main(int argc, char *argv[])
// LSP specific options
app.add_flag("--show-errors", show_errors, "Show errors when LSP is running in the background");
app.add_flag("--show-document-symbols", show_document_symbols, "Show symbols in lpython file");

app.add_flag("--separate-compilation", separate_compilation, "Generates unique names for all the symbols");
/*
* Subcommands:
*/
Expand Down Expand Up @@ -1614,7 +1615,7 @@ int main(int argc, char *argv[])
app.require_subcommand(0, 1);
CLI11_PARSE(app, argc, argv);

lcompilers_unique_ID = LCompilers::get_unique_ID();
lcompilers_unique_ID = separate_compilation ? LCompilers::get_unique_ID(): "";


if( compiler_options.fast && compiler_options.enable_bounds_checking ) {
Expand Down
63 changes: 34 additions & 29 deletions src/libasr/codegen/asr_to_c.cpp

Large diffs are not rendered by default.

54 changes: 37 additions & 17 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
} \


extern std::string lcompilers_unique_ID;

namespace LCompilers {


Expand Down Expand Up @@ -176,6 +178,7 @@ class BaseCCPPVisitor : public ASR::BaseVisitor<Struct>

SymbolTable* current_scope;
bool is_string_concat_present;
std::map<uint64_t, std::string> orig_to_new_name;

BaseCCPPVisitor(diag::Diagnostics &diag, Platform &platform,
CompilerOptions &_compiler_options, bool gen_stdstring, bool gen_stdcomplex, bool is_c,
Expand Down Expand Up @@ -564,8 +567,9 @@ R"(#include <stdio.h>
pArgs = PyTuple_New()" + std::to_string(x.n_args) + R"();
)";
for (size_t i = 0; i < x.n_args; ++i) {
self().visit_expr(*x.m_args[i]);
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i]);
std::string arg_name = std::string(arg->m_name);
std::string arg_name = src;
std::string indent = "\n ";
if (ASRUtils::is_array(arg->m_type)) {
arg_conv += indent + bind_py_utils_functions->get_conv_dims_to_1D_arr() + "(" + arg_name + "->n_dims, " + arg_name + "->dims, __new_dims);";
Expand Down Expand Up @@ -780,9 +784,12 @@ R"(#include <stdio.h>
visited_return = true;
}

std::string return_var_name = "";
if (!visited_return && x.m_return_var) {
self().visit_expr(*x.m_return_var);
return_var_name = src;
current_body += indent + "return "
+ ASRUtils::EXPR2VAR(x.m_return_var)->m_name
+ return_var_name
+ ";\n";
}

Expand Down Expand Up @@ -1687,16 +1694,26 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
last_expr_precedence = 2;
}

std::string find_unique_sym_name(const ASR::symbol_t *sym) {
std::string name = ASRUtils::symbol_name(sym);
uint64_t h = get_hash((ASR::asr_t*)sym);
if (orig_to_new_name.find(h) != orig_to_new_name.end()) {
name = orig_to_new_name[h];
}
return name;
}

void visit_Var(const ASR::Var_t &x) {
const ASR::symbol_t *s = ASRUtils::symbol_get_past_external(x.m_v);
ASR::Variable_t* sv = ASR::down_cast<ASR::Variable_t>(s);
std::string v_name = find_unique_sym_name(s);
if( (sv->m_intent == ASRUtils::intent_in ||
sv->m_intent == ASRUtils::intent_inout) &&
is_c && ASRUtils::is_array(sv->m_type) &&
ASRUtils::is_pointer(sv->m_type)) {
src = "(*" + std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name) + ")";
src = "(*" + v_name + ")";
} else {
src = std::string(ASR::down_cast<ASR::Variable_t>(s)->m_name);
src = v_name;
}
last_expr_precedence = 2;
ASR::ttype_t* var_type = sv->m_type;
Expand All @@ -1715,7 +1732,8 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
std::string der_expr, member;
this->visit_expr(*x.m_v);
der_expr = std::move(src);
member = ASRUtils::symbol_name(ASRUtils::symbol_get_past_external(x.m_m));
ASR::symbol_t *sym = ASRUtils::symbol_get_past_external(x.m_m);
member = find_unique_sym_name(sym);
if( ASR::is_a<ASR::ArrayItem_t>(*x.m_v) ||
ASR::is_a<ASR::UnionInstanceMember_t>(*x.m_v) ||
ASR::is_a<ASR::StructInstanceMember_t>(*x.m_v) ) {
Expand All @@ -1730,7 +1748,7 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
std::string der_expr, member;
this->visit_expr(*x.m_v);
der_expr = std::move(src);
member = ASRUtils::symbol_name(x.m_m);
member = find_unique_sym_name(x.m_m);
src = der_expr + "." + member;
}

Expand Down Expand Up @@ -2276,19 +2294,19 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
std::string indent(indentation_level*indentation_spaces, ' ');
std::string out = "";
for (size_t i=0; i<x.n_args; i++) {
ASR::symbol_t* tmp_sym = nullptr;
ASR::ttype_t* type = nullptr;
ASR::expr_t* tmp_expr = x.m_args[i].m_a;
std::string sym = "";
if( ASR::is_a<ASR::Var_t>(*tmp_expr) ) {
const ASR::Var_t* tmp_var = ASR::down_cast<ASR::Var_t>(tmp_expr);
tmp_sym = tmp_var->m_v;
self().visit_expr(*tmp_expr);
sym = src;
type = ASRUtils::expr_type(tmp_expr);
} else {
throw CodeGenError("Cannot deallocate variables in expression " +
std::to_string(tmp_expr->type),
tmp_expr->base.loc);
}
std::string sym = ASRUtils::symbol_name(tmp_sym);

if (ASRUtils::is_array(type)) {
std::string size_str = "1";
out += indent + sym + "->n_dims = " + std::to_string(x.m_args[i].n_dims) + ";\n";
Expand Down Expand Up @@ -2494,8 +2512,9 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
void visit_Return(const ASR::Return_t & /* x */) {
std::string indent(indentation_level*indentation_spaces, ' ');
if (current_function && current_function->m_return_var) {
self().visit_expr(*current_function->m_return_var);
src = indent + "return "
+ ASRUtils::EXPR2VAR(current_function->m_return_var)->m_name
+ src
+ ";\n";
} else {
src = indent + "return;\n";
Expand Down Expand Up @@ -2545,11 +2564,11 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
std::string loop_end_decl = "";
std::string indent(indentation_level*indentation_spaces, ' ');
std::string out = indent + "for (";
ASR::Variable_t *loop_var = ASRUtils::EXPR2VAR(x.m_head.m_v);
std::string lvname=loop_var->m_name;
ASR::expr_t *a=x.m_head.m_start;
ASR::expr_t *b=x.m_head.m_end;
ASR::expr_t *c=x.m_head.m_increment;
self().visit_expr(*x.m_head.m_v);
std::string lvname = src;
ASR::expr_t *a = x.m_head.m_start;
ASR::expr_t *b = x.m_head.m_end;
ASR::expr_t *c = x.m_head.m_increment;
LCOMPILERS_ASSERT(a);
LCOMPILERS_ASSERT(b);
int increment;
Expand Down Expand Up @@ -2679,8 +2698,9 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
std::string out = indent + sym_name + "(";
for (size_t i=0; i<x.n_args; i++) {
if (ASR::is_a<ASR::Var_t>(*x.m_args[i].m_value)) {
self().visit_expr(*x.m_args[i].m_value);
ASR::Variable_t *arg = ASRUtils::EXPR2VAR(x.m_args[i].m_value);
std::string arg_name = arg->m_name;
std::string arg_name = src;
if( ASRUtils::is_array(arg->m_type) &&
ASRUtils::is_pointer(arg->m_type) ) {
out += "&" + arg_name;
Expand Down
9 changes: 6 additions & 3 deletions src/libasr/codegen/c_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <libasr/asr_utils.h>
#include <libasr/pass/intrinsic_function_registry.h>

extern std::string lcompilers_unique_ID;

namespace LCompilers {

static inline std::string format_type_c(const std::string &dims, const std::string &type,
Expand Down Expand Up @@ -411,7 +413,7 @@ class CCPPDSUtils {
}
case ASR::ttypeType::Character : {
if (is_c) {
result = "_lfortran_strcpy(&" + target + ", " + value + ", 1);";
result = "_lfortran_strcpy(&" + target + ", " + value + ", 0);";
} else {
result = target + " = " + value + ";";
}
Expand Down Expand Up @@ -844,8 +846,9 @@ class CCPPDSUtils {
func_decls += "inline " + signature + ";\n";
std::string tmp_generated = indent + signature + " {\n";
for(size_t i=0; i < struct_type_t->n_members; i++) {
std::string mem_name = std::string(struct_type_t->m_members[i]);
ASR::symbol_t* member = struct_type_t->m_symtab->get_symbol(mem_name);
std::string org_name = std::string(struct_type_t->m_members[i]);
std::string mem_name = org_name + lcompilers_unique_ID;
ASR::symbol_t* member = struct_type_t->m_symtab->get_symbol(org_name);
ASR::ttype_t* member_type_asr = ASRUtils::symbol_type(member);
if( CUtils::is_non_primitive_DT(member_type_asr) ||
ASR::is_a<ASR::Character_t>(*member_type_asr) ) {
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-bindc_06-a30d20f.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-bindc_06-a30d20f.stdout",
"stdout_hash": "6a93f686a1c2fe616c5de50dd3e339f5db7d226f48b29e5569412402",
"stdout_hash": "ad6ba6e497742e491cf7b81493f3a0f4e3f49b61c70d3f36483032ac",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-bindc_06-a30d20f.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void test_arrays()
int32_t i;
bool is_small;
compare_operator->op_code = 0;
_lfortran_strcpy(&compare_operator->op_name, "<", 1);
_lfortran_strcpy(&compare_operator->op_name, "<", 0);
for (i=0; i<=40 - 1; i++) {
array1->data[(i - array1->dims[0].lower_bound)] = i + 1;
array2->data[(i - array2->dims[0].lower_bound)] = (double)(2*i + 1);
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-loop2-ce7de51.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-loop2-ce7de51.stdout",
"stdout_hash": "fd4cf89a39c2fbde40b4174802c6f727476ad8ef2d4e04b2ec113f1a",
"stdout_hash": "a98a13cb3d52b4c153e5a7c96afaf0dc6a78c5461892fd38c6f2d9c2",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/c-loop2-ce7de51.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void resize_if_needed_str(struct list_str* x) {
void list_append_str(struct list_str* x, char* element) {
resize_if_needed_str(x);
x->data[x->current_end_point] = NULL;
_lfortran_strcpy(&x->data[x->current_end_point], element, 1);
_lfortran_strcpy(&x->data[x->current_end_point], element, 0);
x->current_end_point += 1;
}

Expand All @@ -169,7 +169,7 @@ void list_insert_str(struct list_str* x, int pos, char* element) {
}

x->data[pos] = NULL;
_lfortran_strcpy(&x->data[pos], element, 1);
_lfortran_strcpy(&x->data[pos], element, 0);
x->current_end_point += 1;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/reference/c-print_01-4d44628.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "c-print_01-4d44628.stdout",
"stdout_hash": "810f8a9e10df203f3c395a412dc6a4612938edf5be594380f52c3a95",
"stdout_hash": "3b5a48df2d30779b38b9f4a28bb1f368078daf40e41ee42040c3a013",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
6 changes: 3 additions & 3 deletions tests/reference/c-print_01-4d44628.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ void f()
char * x = NULL;
char * y = NULL;
printf("%s\n", "Hello World!");
_lfortran_strcpy(&x, ",", 1);
_lfortran_strcpy(&y, "!!", 1);
_lfortran_strcpy(&x, ",", 0);
_lfortran_strcpy(&y, "!!", 0);
printf("%s%s%s\n", "a", x, "b");
_lfortran_strcpy(&x, "-+-+-", 1);
_lfortran_strcpy(&x, "-+-+-", 0);
printf("%s%s%s%s%s\n", "a", x, "b", x, "c");
printf("%s%s%s%s%s%s", "d", "=", "e", "=", "f", "+\n");
printf("%s%s%s%s%s%s", "x", "*\n", "y", "*\n", "z", y);
Expand Down