Skip to content

Commit

Permalink
Merge pull request lcompilers#1320 from czgdp1807/sprint_08
Browse files Browse the repository at this point in the history
Fixes for ASR and LLVM backend
  • Loading branch information
czgdp1807 committed Nov 24, 2022
2 parents 12e4dd7 + ba487f1 commit ffd8770
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 22 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ RUN(NAME kwargs_01 LABELS cpython llvm)
RUN(NAME test_01_goto LABELS cpython llvm c)

RUN(NAME func_inline_01 LABELS llvm wasm)
RUN(NAME func_inline_02 LABELS cpython llvm c)
RUN(NAME func_static_01 LABELS cpython llvm c wasm)
RUN(NAME func_static_02 LABELS cpython llvm c wasm)
RUN(NAME func_dep_03 LABELS cpython llvm c)
Expand Down
7 changes: 5 additions & 2 deletions integration_tests/expr_13.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ltypes import CPtr, empty_c_void_p, i32, Pointer, ccall
from ltypes import CPtr, empty_c_void_p, i32, Pointer, ccall, Const

@ccall
def deref_array(x: Pointer[CPtr], idx: i32) -> CPtr:
Expand All @@ -12,13 +12,16 @@ def get_arrays(num_arrays: i32) -> Pointer[CPtr]:
def sum_array(x: CPtr, size: i32) -> i32:
pass

def sum_array_python(x: Const[CPtr], size: i32) -> i32:
return sum_array(x, size)

def test_pointer_to_cptr():
x: Pointer[CPtr] = empty_c_void_p()
x = get_arrays(2)
i: i32
sums: i32[2]
for i in range(2):
sums[i] = sum_array(deref_array(x, i), 10)
sums[i] = sum_array_python(deref_array(x, i), 10)

assert sums[0] == sums[1]

Expand Down
17 changes: 17 additions & 0 deletions integration_tests/func_inline_02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ltypes import i32, Const, inline

@inline
def f(x: i32) -> i32:
offset: Const[i32] = 1
return x + offset

def test_f():
x: i32 = 0
x = f(x)
print(x)
assert x == 1
x = f(x)
print(x)
assert x == 2

test_f()
5 changes: 4 additions & 1 deletion src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3567,7 +3567,7 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

void generate_function(const ASR::Function_t &x){
void generate_function(const ASR::Function_t &x) {
bool interactive = (x.m_abi == ASR::abiType::Interactive);
if (x.m_deftype == ASR::deftypeType::Implementation ) {

Expand Down Expand Up @@ -5791,6 +5791,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if (orig_arg->m_abi == ASR::abiType::BindC
&& orig_arg->m_value_attr) {
ASR::ttype_t* arg_type = arg->m_type;
if( ASR::is_a<ASR::Const_t>(*arg_type) ) {
arg_type = ASR::down_cast<ASR::Const_t>(arg_type)->m_type;
}
if (is_a<ASR::Complex_t>(*arg_type)) {
int c_kind = ASRUtils::extract_kind_from_ttype_t(arg_type);
if (c_kind == 4) {
Expand Down
7 changes: 4 additions & 3 deletions src/libasr/pass/inline_function_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,19 @@ class InlineFunctionCallVisitor : public PassUtils::PassVisitor<InlineFunctionCa
success = false;
break;
}
ASR::ttype_t* local_var_type = func_var->m_type;
ASR::symbol_t* local_var = (ASR::symbol_t*) ASR::make_Variable_t(
al, func_var->base.base.loc, current_scope,
s2c(al, local_var_name), ASR::intentType::Local,
nullptr, nullptr, ASR::storage_typeType::Default,
func_var->m_type, ASR::abiType::Source, ASR::accessType::Public,
local_var_type, ASR::abiType::Source, ASR::accessType::Public,
ASR::presenceType::Required, false);
current_scope->add_symbol(local_var_name, local_var);
arg2value[func_var_name] = local_var;
if( m_symbolic_value ) {
if( m_symbolic_value && !ASR::is_a<ASR::Const_t>(*local_var_type) ) {
exprs_to_be_visited.push_back(std::make_pair(m_symbolic_value, local_var));
}
if( m_value ) {
if( m_value && !ASR::is_a<ASR::Const_t>(*local_var_type) ) {
exprs_to_be_visited.push_back(std::make_pair(m_value, local_var));
}
}
Expand Down
38 changes: 22 additions & 16 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3103,6 +3103,10 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
if( is_item ) {
Vec<ASR::dimension_t> empty_dims;
empty_dims.reserve(al, 1);
if( ASR::is_a<ASR::CPtr_t>(*ASRUtils::type_get_past_pointer(type)) ) {
throw SemanticError("Indexing CPtr typed expressions is not supported yet",
x.base.base.loc);
}
type = ASRUtils::duplicate_type(al, ASRUtils::type_get_past_pointer(type), &empty_dims);
tmp = ASR::make_ArrayItem_t(al, x.base.base.loc, v_Var, args.p,
args.size(), type, ASR::arraystorageType::RowMajor, nullptr);
Expand Down Expand Up @@ -3253,6 +3257,24 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
}
}
}

std::string sym_name = x.m_name;
if (overload) {
std::string overload_number;
if (overload_defs.find(sym_name) == overload_defs.end()){
overload_number = "0";
Vec<ASR::symbol_t *> v;
v.reserve(al, 1);
overload_defs[sym_name] = v;
} else {
overload_number = std::to_string(overload_defs[sym_name].size());
}
sym_name = "__lpython_overloaded_" + overload_number + "__" + sym_name;
}
if (parent_scope->get_scope().find(sym_name) != parent_scope->get_scope().end()) {
throw SemanticError("Function " + std::string(x.m_name) + " is already defined", x.base.base.loc);
}

for (size_t i=0; i<x.m_args.n_args; i++) {
char *arg=x.m_args.m_args[i].m_arg;
Location loc = x.m_args.m_args[i].loc;
Expand Down Expand Up @@ -3312,22 +3334,6 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
args.push_back(al, ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc,
var)));
}
std::string sym_name = x.m_name;
if (overload) {
std::string overload_number;
if (overload_defs.find(sym_name) == overload_defs.end()){
overload_number = "0";
Vec<ASR::symbol_t *> v;
v.reserve(al, 1);
overload_defs[sym_name] = v;
} else {
overload_number = std::to_string(overload_defs[sym_name].size());
}
sym_name = "__lpython_overloaded_" + overload_number + "__" + sym_name;
}
if (parent_scope->get_scope().find(sym_name) != parent_scope->get_scope().end()) {
throw SemanticError("Subroutine already defined", tmp->loc);
}
ASR::accessType s_access = ASR::accessType::Public;
ASR::deftypeType deftype = ASR::deftypeType::Implementation;
if (current_procedure_abi_type == ASR::abiType::BindC &&
Expand Down
7 changes: 7 additions & 0 deletions tests/errors/cptr_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ltypes import CPtr, empty_c_void_p

def f():
x: CPtr = empty_c_void_p()
print(x[0])

f()
10 changes: 10 additions & 0 deletions tests/errors/func_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ltypes import i32

def f(i: i32):
print(i)

def g(i: i32):
print(i + 1)

def f(i: i32):
print(i + 2)
13 changes: 13 additions & 0 deletions tests/reference/asr-cptr_01-4e660f1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-cptr_01-4e660f1",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/cptr_01.py",
"infile_hash": "0c5f0767aac11155e331581cf0158bb7a9b98ef11bb1716877477b00",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-cptr_01-4e660f1.stderr",
"stderr_hash": "0477f93b29ff4932b3471a59731a173fb19d6e44273236829eeaffbe",
"returncode": 2
}
5 changes: 5 additions & 0 deletions tests/reference/asr-cptr_01-4e660f1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semantic error: Indexing CPtr typed expressions is not supported yet
--> tests/errors/cptr_01.py:5:11
|
5 | print(x[0])
| ^^^^
13 changes: 13 additions & 0 deletions tests/reference/asr-func_01-d87aa4a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-func_01-d87aa4a",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/errors/func_01.py",
"infile_hash": "fe27c8537c6e7447cb8b3ea3b4a9553f6786ff70600c30fdf196b602",
"outfile": null,
"outfile_hash": null,
"stdout": null,
"stdout_hash": null,
"stderr": "asr-func_01-d87aa4a.stderr",
"stderr_hash": "2a773033fab41aadd3ddf3732cfb473ba5da6c9649453516286dacf1",
"returncode": 2
}
9 changes: 9 additions & 0 deletions tests/reference/asr-func_01-d87aa4a.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
semantic error: Function f is already defined
--> tests/errors/func_01.py:9:1 - 10:16
|
9 | def f(i: i32):
| ^^^^^^^^^^^^^^...
...
|
10 | print(i + 2)
| ...^^^^^^^^^^^^^^^^
8 changes: 8 additions & 0 deletions tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,14 @@ asr = true
filename = "errors/bindc_02.py"
asr = true

[[test]]
filename = "errors/cptr_01.py"
asr = true

[[test]]
filename = "errors/func_01.py"
asr = true

# tests/runtime_errors
[[test]]
filename = "runtime_errors/test_list_01.py"
Expand Down

0 comments on commit ffd8770

Please sign in to comment.