Skip to content

Commit

Permalink
Declare global variables globally in C
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Jun 20, 2022
1 parent 3895593 commit d3b8d09
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,5 @@ integration_tests/structs_02
integration_tests/structs_02.c
integration_tests/structs_03
integration_tests/structs_03.c
integration_tests/expr_08
integration_tests/expr_08.c
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ RUN(NAME expr_03 LABELS cpython llvm c)
RUN(NAME expr_04 LABELS cpython llvm c)
RUN(NAME expr_05 LABELS cpython llvm)
RUN(NAME expr_06 LABELS cpython llvm)
RUN(NAME expr_08 LABELS llvm c)
RUN(NAME test_types_01 LABELS cpython llvm)
RUN(NAME test_str_01 LABELS cpython llvm)
RUN(NAME test_str_02 LABELS cpython llvm)
Expand Down
7 changes: 7 additions & 0 deletions src/libasr/codegen/asr_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ R"(
}
}

for (auto &item : x.m_global_scope->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
unit_src += convert_variable_decl(*v) + ";\n";
}
}

for (auto &item : x.m_global_scope->get_scope()) {
if (ASR::is_a<ASR::DerivedType_t>(*item.second)) {
visit_symbol(*item.second);
Expand Down
26 changes: 0 additions & 26 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,18 +334,6 @@ R"(#include <stdio.h>
sub += ";\n";
} else {
sub += "\n";
if( to_lower(x.m_name) == "_lpython_main_program" ) {
for (auto &item : global_scope->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
if (v->m_intent == LFortran::ASRUtils::intent_local) {
SymbolInfo s;
s.needs_declaration = true;
sym_info[get_hash((ASR::asr_t*)v)] = s;
}
}
}
}
for (auto &item : x.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
Expand All @@ -364,20 +352,6 @@ R"(#include <stdio.h>
}

std::string decl;
if( to_lower(x.m_name) == "_lpython_main_program" ) {
for (auto &item : global_scope->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
if (v->m_intent == LFortran::ASRUtils::intent_local) {
if (sym_info[get_hash((ASR::asr_t*) v)].needs_declaration) {
std::string indent(indentation_level*indentation_spaces, ' ');
decl += indent;
decl += self().convert_variable_decl(*v) + ";\n";
}
}
}
}
}
for (auto &item : x.m_symtab->get_scope()) {
if (ASR::is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
Expand Down

0 comments on commit d3b8d09

Please sign in to comment.