Skip to content

Allow global variable initialisation only using the compile_item value #1672

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 2 commits into from
Apr 8, 2023
Merged
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
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,4 @@ RUN(NAME global_syms_03_b LABELS cpython llvm c)
RUN(NAME global_syms_03_c LABELS cpython llvm c)
RUN(NAME global_syms_04 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME global_syms_05 LABELS cpython llvm c)
RUN(NAME global_syms_06 LABELS cpython llvm c)
9 changes: 9 additions & 0 deletions integration_tests/global_syms_06.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using your PR, please show the indented ASR for this code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(TranslationUnit
    (SymbolTable
        1
        {
            _global_symbols:
                (Module
                    (SymbolTable
                        5
                        {
                            _lpython_main_program:
                                (Function
                                    (SymbolTable
                                        4
                                        {
                                            
                                        })
                                    _lpython_main_program
                                    (FunctionType
                                        []
                                        ()
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        []
                                        .false.
                                    )
                                    [test]
                                    []
                                    [(=
                                        (Var 5 x)
                                        (FunctionCall
                                            5 test
                                            ()
                                            []
                                            (Integer 4 [])
                                            ()
                                            ()
                                        )
                                        ()
                                    )
                                    (=
                                        (Var 5 j)
                                        (Var 5 i)
                                        ()
                                    )]
                                    ()
                                    Public
                                    .false.
                                    .false.
                                ),
                            i:
                                (Variable
                                    5
                                    i
                                    []
                                    Local
                                    (IntegerConstant 10 (Integer 4 []))
                                    (IntegerConstant 10 (Integer 4 []))
                                    Default
                                    (Integer 4 [])
                                    Source
                                    Public
                                    Required
                                    .false.
                                ),
                            j:
                                (Variable
                                    5
                                    j
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (Integer 4 [])
                                    Source
                                    Public
                                    Required
                                    .false.
                                ),
                            test:
                                (Function
                                    (SymbolTable
                                        2
                                        {
                                            _lpython_return_variable:
                                                (Variable
                                                    2
                                                    _lpython_return_variable
                                                    []
                                                    ReturnVar
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 4 [])
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                ),
                                            temp:
                                                (Variable
                                                    2
                                                    temp
                                                    []
                                                    Local
                                                    ()
                                                    ()
                                                    Default
                                                    (Integer 4 [])
                                                    Source
                                                    Public
                                                    Required
                                                    .false.
                                                )
                                        })
                                    test
                                    (FunctionType
                                        []
                                        (Integer 4 [])
                                        Source
                                        Implementation
                                        ()
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        .false.
                                        []
                                        []
                                        .false.
                                    )
                                    []
                                    []
                                    [(=
                                        (Var 2 temp)
                                        (IntegerConstant 0 (Integer 4 []))
                                        ()
                                    )
                                    (=
                                        (Var 2 _lpython_return_variable)
                                        (Var 2 temp)
                                        ()
                                    )
                                    (Return)]
                                    (Var 2 _lpython_return_variable)
                                    Public
                                    .false.
                                    .false.
                                ),
                            x:
                                (Variable
                                    5
                                    x
                                    []
                                    Local
                                    ()
                                    ()
                                    Default
                                    (Integer 4 [])
                                    Source
                                    Public
                                    Required
                                    .false.
                                )
                        })
                    _global_symbols
                    []
                    .false.
                    .false.
                ),
            main_program:
                (Program
                    (SymbolTable
                        3
                        {
                            _lpython_main_program:
                                (ExternalSymbol
                                    3
                                    _lpython_main_program
                                    5 _lpython_main_program
                                    _global_symbols
                                    []
                                    _lpython_main_program
                                    Public
                                )
                        })
                    main_program
                    [_global_symbols]
                    [(SubroutineCall
                        3 _lpython_main_program
                        ()
                        []
                        ()
                    )]
                )
        })
    []
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from lpython import i32

def test() -> i32:
temp: i32 = 0
return temp

x: i32 = test()
i: i32 = 10
j: i32 = i
23 changes: 18 additions & 5 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2245,15 +2245,15 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
ASR::symbol_t* v_sym = ASR::down_cast<ASR::symbol_t>(v);
ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);

if( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type)) &&
(is_runtime_expression || !is_variable_const)) {
if( init_expr && (current_body || ASR::is_a<ASR::List_t>(*type) ||
is_runtime_expression) && !is_variable_const) {
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(al, loc, v_sym));
cast_helper(v_expr, init_expr, true);
ASR::asr_t* assign = ASR::make_Assignment_t(al, loc, v_expr,
init_expr, nullptr);
if (current_body) {
current_body->push_back(al, ASRUtils::STMT(assign));
} else if (ASR::is_a<ASR::List_t>(*type)) {
} else if (ASR::is_a<ASR::List_t>(*type) || is_runtime_expression) {
global_init.push_back(al, assign);
}

Expand Down Expand Up @@ -3978,10 +3978,23 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// Erase the function in TranslationUnit
unit->m_global_scope->erase_symbol(func_name);
}
global_init.p = nullptr;
global_init.n = 0;
}

if (global_init.n > 0) {
// copy all the item's from `items` (global_statements)
// into `global_init`
for (auto &i: items) {
global_init.push_back(al, i);
}
unit->m_items = global_init.p;
unit->n_items = global_init.size();
} else {
unit->m_items = items.p;
unit->n_items = items.size();
}

unit->m_items = items.p;
unit->n_items = items.size();
if (items.n > 0 && main_module_sym) {
std::string func_name = "global_statements";
// Wrap all the global statements into a Function
Expand Down