Skip to content

Commit f49908e

Browse files
authored
Merge pull request #662 from czgdp1807/init_decl1
Convert initialisation at declaration to assignments and fix Variable.value and Variable.m_symbolic_value settings
2 parents 1ed8cb8 + 9022335 commit f49908e

26 files changed

+163
-21
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ RUN(NAME expr_03 LABELS cpython llvm c)
132132
RUN(NAME expr_04 LABELS cpython llvm c)
133133
RUN(NAME expr_05 LABELS cpython llvm)
134134
RUN(NAME expr_06 LABELS cpython llvm)
135+
RUN(NAME expr_07 LABELS cpython llvm)
135136
RUN(NAME expr_08 LABELS llvm c)
137+
RUN(NAME expr_09 LABELS cpython llvm)
136138
RUN(NAME expr_10 LABELS cpython)
137139
RUN(NAME test_types_01 LABELS cpython llvm)
138140
RUN(NAME test_str_01 LABELS cpython llvm)

integration_tests/expr_07.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from ltypes import i32
2+
3+
def g(x: i32):
4+
print(x)
5+
6+
x: i32 = 7
7+
8+
def f():
9+
a: i32 = 5
10+
x: i32 = 3
11+
x = 5
12+
b: i32 = x + 1
13+
assert b == 6
14+
print(a, b)
15+
g(a*b + 3)
16+
17+
f()

integration_tests/expr_09.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ltypes import i32
2+
3+
def main0():
4+
i1: i32 = 10
5+
i2: i32 = 4
6+
i1 = 3
7+
i2 = 5
8+
print(-i1 ^ -i2)
9+
assert -i1 ^ -i2 == 6
10+
11+
main0()

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,11 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
27422742
return ;
27432743
}
27442744

2745+
// TODO: Remove this check after supporting ListConstant
2746+
if( ASR::is_a<ASR::List_t>(*ASRUtils::expr_type(x.m_value)) ) {
2747+
return ;
2748+
}
2749+
27452750
if( ASR::is_a<ASR::Pointer_t>(*ASRUtils::expr_type(x.m_target)) &&
27462751
ASR::is_a<ASR::GetPointer_t>(*x.m_value) ) {
27472752
ASR::Variable_t *asr_target = EXPR2VAR(x.m_target);
@@ -4714,6 +4719,35 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
47144719
pop_nested_stack(s);
47154720
}
47164721

4722+
void handle_bitwise_args(const ASR::FunctionCall_t& x, llvm::Value*& arg1,
4723+
llvm::Value*& arg2) {
4724+
LFORTRAN_ASSERT(x.n_args == 2);
4725+
tmp = nullptr;
4726+
this->visit_expr_wrapper(x.m_args[0].m_value, true);
4727+
arg1 = tmp;
4728+
tmp = nullptr;
4729+
this->visit_expr_wrapper(x.m_args[1].m_value, true);
4730+
arg2 = tmp;
4731+
}
4732+
4733+
void handle_bitwise_xor(const ASR::FunctionCall_t& x) {
4734+
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
4735+
handle_bitwise_args(x, arg1, arg2);
4736+
tmp = builder->CreateXor(arg1, arg2);
4737+
}
4738+
4739+
void handle_bitwise_and(const ASR::FunctionCall_t& x) {
4740+
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
4741+
handle_bitwise_args(x, arg1, arg2);
4742+
tmp = builder->CreateAnd(arg1, arg2);
4743+
}
4744+
4745+
void handle_bitwise_or(const ASR::FunctionCall_t& x) {
4746+
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
4747+
handle_bitwise_args(x, arg1, arg2);
4748+
tmp = builder->CreateOr(arg1, arg2);
4749+
}
4750+
47174751
void visit_FunctionCall(const ASR::FunctionCall_t &x) {
47184752
if( ASRUtils::is_intrinsic_optimization(x.m_name) ) {
47194753
ASR::Function_t* routine = ASR::down_cast<ASR::Function_t>(
@@ -4744,6 +4778,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
47444778
if( s == nullptr ) {
47454779
s = ASR::down_cast<ASR::Function_t>(symbol_get_past_external(x.m_name));
47464780
}
4781+
if( ASRUtils::is_intrinsic_function2(s) ) {
4782+
std::string symbol_name = ASRUtils::symbol_name(x.m_name);
4783+
if( startswith(symbol_name, "_bitwise_xor") ) {
4784+
handle_bitwise_xor(x);
4785+
return ;
4786+
}
4787+
if( startswith(symbol_name, "_bitwise_and") ) {
4788+
handle_bitwise_and(x);
4789+
return ;
4790+
}
4791+
if( startswith(symbol_name, "_bitwise_or") ) {
4792+
handle_bitwise_or(x);
4793+
return ;
4794+
}
4795+
}
47474796
if (parent_function){
47484797
push_nested_stack(parent_function);
47494798
} else if (parent_subroutine){

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,14 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
281281
AttributeHandler attr_handler;
282282
std::map<int, ASR::symbol_t*> &ast_overload;
283283
std::string parent_dir;
284+
Vec<ASR::stmt_t*> *current_body;
284285

285286
CommonVisitor(Allocator &al, SymbolTable *symbol_table,
286287
diag::Diagnostics &diagnostics, bool main_module,
287288
std::map<int, ASR::symbol_t*> &ast_overload, std::string parent_dir)
288289
: diag{diagnostics}, al{al}, current_scope{symbol_table}, main_module{main_module},
289-
ast_overload{ast_overload}, parent_dir{parent_dir} {
290+
ast_overload{ast_overload}, parent_dir{parent_dir},
291+
current_body{nullptr} {
290292
current_module_dependencies.reserve(al, 4);
291293
}
292294

@@ -1379,7 +1381,12 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
13791381
throw SemanticAbort();
13801382
}
13811383
init_expr = value;
1382-
value = ASRUtils::expr_value(value);
1384+
// Set compile time to value to nullptr
1385+
// Once constant variables are supported
1386+
// in LPython set value according to the
1387+
// nature of the variable (nullptr if non-constant,
1388+
// otherwise ASRUtils::expr_value(init_expr).
1389+
value = nullptr;
13831390
}
13841391
ASR::intentType s_intent = ASRUtils::intent_local;
13851392
ASR::storage_typeType storage_type =
@@ -1392,7 +1399,21 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
13921399
s2c(al, var_name), s_intent, init_expr, value, storage_type, type,
13931400
current_procedure_abi_type, s_access, s_presence,
13941401
value_attr);
1395-
current_scope->add_symbol(var_name, ASR::down_cast<ASR::symbol_t>(v));
1402+
ASR::symbol_t* v_sym = ASR::down_cast<ASR::symbol_t>(v);
1403+
// Convert initialisation at declaration to assignment
1404+
// only for non-global variables. For global variables
1405+
// keep relying on `m_symbolic_value`.
1406+
if( init_expr && current_body) {
1407+
ASR::expr_t* v_expr = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, v_sym));
1408+
init_expr = cast_helper(ASRUtils::expr_type(v_expr), init_expr, true);
1409+
ASR::asr_t* assign = ASR::make_Assignment_t(al, x.base.base.loc, v_expr,
1410+
init_expr, nullptr);
1411+
current_body->push_back(al, ASRUtils::STMT(assign));
1412+
ASR::Variable_t* v_variable = ASR::down_cast<ASR::Variable_t>(v_sym);
1413+
v_variable->m_symbolic_value = nullptr;
1414+
v_variable->m_value = nullptr;
1415+
}
1416+
current_scope->add_symbol(var_name, v_sym);
13961417

13971418
tmp = nullptr;
13981419
}
@@ -2341,7 +2362,6 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
23412362

23422363
public:
23432364
ASR::asr_t *asr;
2344-
Vec<ASR::stmt_t*> *current_body;
23452365

23462366
BodyVisitor(Allocator &al, ASR::asr_t *unit, diag::Diagnostics &diagnostics,
23472367
bool main_module, std::map<int, ASR::symbol_t*> &ast_overload)
@@ -2354,18 +2374,19 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
23542374
// The `body` Vec must already be reserved
23552375
void transform_stmts(Vec<ASR::stmt_t*> &body, size_t n_body, AST::stmt_t **m_body) {
23562376
tmp = nullptr;
2377+
Vec<ASR::stmt_t*>* current_body_copy = current_body;
2378+
current_body = &body;
23572379
for (size_t i=0; i<n_body; i++) {
23582380
// Visit the statement
2359-
current_body = &body;
23602381
this->visit_stmt(*m_body[i]);
2361-
current_body = nullptr;
23622382
if (tmp != nullptr) {
23632383
ASR::stmt_t* tmp_stmt = ASRUtils::STMT(tmp);
23642384
body.push_back(al, tmp_stmt);
23652385
}
23662386
// To avoid last statement to be entered twice once we exit this node
23672387
tmp = nullptr;
23682388
}
2389+
current_body = current_body_copy;
23692390
}
23702391

23712392
void visit_Module(const AST::Module_t &x) {

tests/reference/asr-assign2-8d1a2ee.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-assign2-8d1a2ee.stdout",
9-
"stdout_hash": "b358f13ad062675500fda44dc9108ac54f4d4a6dde14422717b86567",
9+
"stdout_hash": "1faef0f925e9b0aef9c1c606170bf8a3795c2a076ea3de13e2f83143",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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 [] [])}) [])
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 []))) () 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 [] [])}) [])

tests/reference/asr-expr_05-3a37324.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-expr_05-3a37324.stdout",
9-
"stdout_hash": "c78678e2d895a4499b390aea812c19cf738391194e4a0f8e772a3567",
9+
"stdout_hash": "b34fcc31e71f1377ef2b2746907745a8e1323db669b43669d6e338c0",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)