Skip to content

Convert initialisation at declaration to assignments and fix Variable.value and Variable.m_symbolic_value settings #662

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 10 commits into from
Jun 21, 2022
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
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ 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_07 LABELS cpython llvm)
RUN(NAME expr_08 LABELS llvm c)
RUN(NAME expr_09 LABELS cpython llvm)
RUN(NAME expr_10 LABELS cpython)
RUN(NAME test_types_01 LABELS cpython llvm)
RUN(NAME test_str_01 LABELS cpython llvm)
Expand Down
17 changes: 17 additions & 0 deletions integration_tests/expr_07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ltypes import i32

def g(x: i32):
print(x)

x: i32 = 7

def f():
a: i32 = 5
x: i32 = 3
x = 5
b: i32 = x + 1
assert b == 6
print(a, b)
g(a*b + 3)

f()
11 changes: 11 additions & 0 deletions integration_tests/expr_09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from ltypes import i32

def main0():
i1: i32 = 10
i2: i32 = 4
i1 = 3
i2 = 5
print(-i1 ^ -i2)
assert -i1 ^ -i2 == 6

main0()
49 changes: 49 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
return ;
}

// TODO: Remove this check after supporting ListConstant
if( ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(x.m_value)) ) {
return ;
}

if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
ASR::Variable_t *asr_target = EXPR2VAR(x.m_target);
Expand Down Expand Up @@ -4714,6 +4719,35 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
pop_nested_stack(s);
}

void handle_bitwise_args(const ASR::FunctionCall_t& x, llvm::Value*& arg1,
llvm::Value*& arg2) {
LFORTRAN_ASSERT(x.n_args == 2);
tmp = nullptr;
this->visit_expr_wrapper(x.m_args[0].m_value, true);
arg1 = tmp;
tmp = nullptr;
this->visit_expr_wrapper(x.m_args[1].m_value, true);
arg2 = tmp;
}

void handle_bitwise_xor(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateXor(arg1, arg2);
}

void handle_bitwise_and(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateAnd(arg1, arg2);
}

void handle_bitwise_or(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateOr(arg1, arg2);
}

void visit_FunctionCall(const ASR::FunctionCall_t &x) {
if( ASRUtils::is_intrinsic_optimization(x.m_name) ) {
ASR::Function_t* routine = ASR::down_cast<ASR::Function_t>(
Expand Down Expand Up @@ -4744,6 +4778,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if( s == nullptr ) {
s = ASR::down_cast<ASR::Function_t>(symbol_get_past_external(x.m_name));
}
if( ASRUtils::is_intrinsic_function2(s) ) {
std::string symbol_name = ASRUtils::symbol_name(x.m_name);
if( startswith(symbol_name, "_bitwise_xor") ) {
handle_bitwise_xor(x);
return ;
}
if( startswith(symbol_name, "_bitwise_and") ) {
handle_bitwise_and(x);
return ;
}
if( startswith(symbol_name, "_bitwise_or") ) {
handle_bitwise_or(x);
return ;
}
}
if (parent_function){
push_nested_stack(parent_function);
} else if (parent_subroutine){
Expand Down
33 changes: 27 additions & 6 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
AttributeHandler attr_handler;
std::map<int, ASR::symbol_t*> &ast_overload;
std::string parent_dir;
Vec<ASR::stmt_t*> *current_body;

CommonVisitor(Allocator &al, SymbolTable *symbol_table,
diag::Diagnostics &diagnostics, bool main_module,
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir)
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
ast_overload{ast_overload}, parent_dir{parent_dir} {
ast_overload{ast_overload}, parent_dir{parent_dir},
current_body{nullptr} {
current_module_dependencies.reserve(al, 4);
}

Expand Down Expand Up @@ -1379,7 +1381,12 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
throw SemanticAbort();
}
init_expr = value;
value = ASRUtils::expr_value(value);
// Set compile time to value to nullptr
// Once constant variables are supported
// in LPython set value according to the
// nature of the variable (nullptr if non-constant,
// otherwise ASRUtils::expr_value(init_expr).
value = nullptr;
}
ASR::intentType s_intent = ASRUtils::intent_local;
ASR::storage_typeType storage_type =
Expand All @@ -1392,7 +1399,21 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
s2c(al, var_name), s_intent, init_expr, value, storage_type, type,
current_procedure_abi_type, s_access, s_presence,
value_attr);
current_scope->add_symbol(var_name, ASR::down_cast<ASR::symbol_t>(v));
ASR::symbol_t* v_sym = ASR::down_cast<ASR::symbol_t>(v);
// Convert initialisation at declaration to assignment
// only for non-global variables. For global variables
// keep relying on `m_symbolic_value`.
if( init_expr && current_body) {
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, v_sym));
init_expr = cast_helper(ASRUtils::expr_type(v_expr), init_expr, true);
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, v_expr,
init_expr, nullptr);
current_body->push_back(al, ASRUtils::STMT(assign));
ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
v_variable->m_symbolic_value = nullptr;
v_variable->m_value = nullptr;
}
current_scope->add_symbol(var_name, v_sym);

tmp = nullptr;
}
Expand Down Expand Up @@ -2341,7 +2362,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {

public:
ASR::asr_t *asr;
Vec<ASR::stmt_t*> *current_body;

BodyVisitor(Allocator &al, ASR::asr_t *unit, diag::Diagnostics &diagnostics,
bool main_module, std::map<int, ASR::symbol_t*> &ast_overload)
Expand All @@ -2354,18 +2374,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// The `body` Vec must already be reserved
void transform_stmts(Vec<ASR::stmt_t*> &body, size_t n_body, AST::stmt_t **m_body) {
tmp = nullptr;
Vec<ASR::stmt_t*>* current_body_copy = current_body;
current_body = &body;
for (size_t i=0; i<n_body; i++) {
// Visit the statement
current_body = &body;
this->visit_stmt(*m_body[i]);
current_body = nullptr;
if (tmp != nullptr) {
ASR::stmt_t* tmp_stmt = ASRUtils::STMT(tmp);
body.push_back(al, tmp_stmt);
}
// To avoid last statement to be entered twice once we exit this node
tmp = nullptr;
}
current_body = current_body_copy;
}

void visit_Module(const AST::Module_t &x) {
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-assign2-8d1a2ee.stdout",
"stdout_hash": "b358f13ad062675500fda44dc9108ac54f4d4a6dde14422717b86567",
"stdout_hash": "1faef0f925e9b0aef9c1c606170bf8a3795c2a076ea3de13e2f83143",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-assign2-8d1a2ee.stdout
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(TranslationUnit (SymbolTable 1 {f: (Variable 1 f Local (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) (RealConstant 1.23456788999999989e+00 (Real 4 [])) Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local (RealConstant 1.23456789012340007e+00 (Real 8 [])) (RealConstant 1.23456789012340007e+00 (Real 8 [])) Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local (IntegerConstant 5 (Integer 4 [])) (IntegerConstant 5 (Integer 4 [])) Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) () Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])
(TranslationUnit (SymbolTable 1 {f: (Variable 1 f Local (Cast (RealConstant 1.23456788999999989e+00 (Real 8 [])) RealToReal (Real 4 []) (RealConstant 1.23456788999999989e+00 (Real 4 []))) () Default (Real 4 []) Source Public Required .false.), f2: (Variable 1 f2 Local (RealConstant 1.23456789012340007e+00 (Real 8 [])) () Default (Real 8 []) Source Public Required .false.), i: (Variable 1 i Local (IntegerConstant 5 (Integer 4 [])) () Default (Integer 4 []) Source Public Required .false.), i2: (Variable 1 i2 Local (Cast (IntegerConstant 53430903434 (Integer 4 [])) IntegerToInteger (Integer 8 []) ()) () Default (Integer 8 []) Source Public Required .false.), main_program: (Program (SymbolTable 2 {}) main_program [] [])}) [])
2 changes: 1 addition & 1 deletion tests/reference/asr-expr_05-3a37324.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-expr_05-3a37324.stdout",
"stdout_hash": "c78678e2d895a4499b390aea812c19cf738391194e4a0f8e772a3567",
"stdout_hash": "b34fcc31e71f1377ef2b2746907745a8e1323db669b43669d6e338c0",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading