Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support nullptr testing #1805

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 6 commits into from
May 17, 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
9 changes: 9 additions & 0 deletions integration_tests/bindc_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
queries: CPtr = empty_c_void_p()
x: Pointer[i16] = c_p_pointer(queries, i16)
print(queries, x)


def test_issue_1781():
p: CPtr = empty_c_void_p()
assert p == empty_c_void_p()
assert not (p != empty_c_void_p())


test_issue_1781()
5 changes: 4 additions & 1 deletion integration_tests/bindc_03.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from lpython import c_p_pointer, CPtr, pointer, i32, Pointer, ccall, p_c_pointer, dataclass, ccallable
from lpython import (c_p_pointer, CPtr, pointer, i32,
Pointer, ccall, p_c_pointer, dataclass,
ccallable, empty_c_void_p)

@dataclass
class ArrayWrapped:
Expand Down Expand Up @@ -46,6 +48,7 @@ def run():
size: i32
size = 10
a = get_array(size)
assert a != empty_c_void_p()
array_wrapped.array = a
f(array_wrapped.array)
array_wrapped1 = array_wrapped
Expand Down
2 changes: 2 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ expr
| StringOrd(expr arg, ttype type, expr? value)
| StringChr(expr arg, ttype type, expr? value)

| CPtrCompare(expr left, cmpop op, expr right, ttype type, expr? value)
Copy link
Contributor

Choose a reason for hiding this comment

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

The cmpop has various things like >. What does that mean for CPtr? I think we only want == and !=.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

C has pointer comparisons for different cmpop as seen here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. We can allow it in ASR, since eventually we want to have a C frontend too. But in LPython I would mainly support == and != for now for CPtr. The other ones we can, but don't have to right now.


| DictConstant(expr* keys, expr* values, ttype type)
| DictLen(expr arg, ttype type, expr? value)

Expand Down
8 changes: 8 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,10 @@ R"(#include <stdio.h>
handle_Compare(x);
}

void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
handle_Compare(x);
}

template<typename T>
void handle_Compare(const T &x) {
CHECK_FAST_C_CPP(compiler_options, x)
Expand Down Expand Up @@ -1663,6 +1667,10 @@ R"(#include <stdio.h>
}
}

void visit_PointerNullConstant(const ASR::PointerNullConstant_t& /*x*/) {
src = "NULL";
}

void visit_GetPointer(const ASR::GetPointer_t& x) {
CHECK_FAST_C_CPP(compiler_options, x)
self().visit_expr(*x.m_arg);
Expand Down
43 changes: 43 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5134,6 +5134,49 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
if (x.m_value) {
this->visit_expr_wrapper(x.m_value, true);
return;
}
this->visit_expr_wrapper(x.m_left, true);
llvm::Value *left = tmp;
left = builder->CreatePtrToInt(left, getIntType(8, false));
this->visit_expr_wrapper(x.m_right, true);
llvm::Value *right = tmp;
right = builder->CreatePtrToInt(right, getIntType(8, false));
switch (x.m_op) {
case (ASR::cmpopType::Eq) : {
tmp = builder->CreateICmpEQ(left, right);
break;
}
case (ASR::cmpopType::Gt) : {
tmp = builder->CreateICmpSGT(left, right);
break;
}
case (ASR::cmpopType::GtE) : {
tmp = builder->CreateICmpSGE(left, right);
break;
}
case (ASR::cmpopType::Lt) : {
tmp = builder->CreateICmpSLT(left, right);
break;
}
case (ASR::cmpopType::LtE) : {
tmp = builder->CreateICmpSLE(left, right);
break;
}
case (ASR::cmpopType::NotEq) : {
tmp = builder->CreateICmpNE(left, right);
break;
}
default : {
throw CodeGenError("Comparison operator not implemented",
x.base.base.loc);
}
}
}

void visit_RealCompare(const ASR::RealCompare_t &x) {
if (x.m_value) {
this->visit_expr_wrapper(x.m_value, true);
Expand Down
14 changes: 13 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5766,6 +5766,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
x.base.base.loc);
}
tmp = ASR::make_ListCompare_t(al, x.base.base.loc, left, asr_op, right, type, value);
} else if (ASR::is_a<ASR::CPtr_t>(*dest_type)) {
if (asr_op != ASR::cmpopType::Eq && asr_op != ASR::cmpopType::NotEq) {
throw SemanticError("Only Equal and Not-equal operators are supported for CPtr",
x.base.base.loc);
}
tmp = ASR::make_CPtrCompare_t(al, x.base.base.loc, left, asr_op, right, type, value);
} else {
throw SemanticError("Compare not supported for type: " + ASRUtils::type_to_str_python(dest_type),
x.base.base.loc);
Expand Down Expand Up @@ -6804,7 +6810,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
} else if (call_name == "empty_c_void_p") {
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
// with the type
tmp = nullptr;
ASR::ttype_t* type;
if (ann_assign_target_type) {
type = ann_assign_target_type;
} else {
type = ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc));
}
tmp = ASR::make_PointerNullConstant_t(al, x.base.base.loc, type);
return;
} else if (call_name == "TypeVar") {
// Ignore TypeVar for now, we handle it based on the identifier itself
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,15 @@ def p_c_pointer(ptr, cptr):
cptr.value = id(ptr)

def empty_c_void_p():
return ctypes.c_void_p()
class ctypes_c_void_p(ctypes.c_void_p):

def __eq__(self, value):
return self.value == value.value

def __repr__(self):
return str(self.value)

return ctypes_c_void_p()

def sizeof(arg):
return ctypes.sizeof(convert_type_to_ctype(arg))
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-bindc_01-6d521a9.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-bindc_01-6d521a9",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/../integration_tests/bindc_01.py",
"infile_hash": "f628ce81b32f2730f936232bb235f39d4372912bc332f3c97e983ad7",
"infile_hash": "3cfb601d3294c470842a85777832f5582ab52cb5bd64c0e02d40deb6",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-bindc_01-6d521a9.stdout",
"stdout_hash": "c1c5afabc9ecb18c731ad21825ee23c181c1965e4acf5fd2776b2008",
"stdout_hash": "d02c57ff6ddb41568c291b11a31301870bf2bc3a970461a71ec23a9d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
120 changes: 105 additions & 15 deletions tests/reference/asr-bindc_01-6d521a9.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
_global_symbols:
(Module
(SymbolTable
4
5
{
_lpython_main_program:
(Function
(SymbolTable
3
4
{

})
Expand All @@ -30,18 +30,24 @@
[]
.false.
)
[]
[test_issue_1781]
[]
[(CPtrToPointer
(Var 4 queries)
(Var 4 x)
(Var 5 queries)
(Var 5 x)
()
)
(Print
()
[(Var 4 queries)
(Var 4 x)]
[(Var 5 queries)
(Var 5 x)]
()
()
)
(SubroutineCall
5 test_issue_1781
()
[]
()
)]
()
Expand All @@ -52,22 +58,106 @@
),
queries:
(Variable
4
5
queries
[]
Local
()
()
(PointerNullConstant
(CPtr)
)
(PointerNullConstant
(CPtr)
)
Default
(CPtr)
Source
Public
Required
.false.
),
test_issue_1781:
(Function
(SymbolTable
2
{
p:
(Variable
2
p
[]
Local
()
()
Default
(CPtr)
Source
Public
Required
.false.
)
})
test_issue_1781
(FunctionType
[]
()
Source
Implementation
()
.false.
.false.
.false.
.false.
.false.
[]
[]
.false.
)
[]
[]
[(=
(Var 2 p)
(PointerNullConstant
(CPtr)
)
()
)
(Assert
(CPtrCompare
(Var 2 p)
Eq
(PointerNullConstant
(CPtr)
)
(Logical 4 [])
()
)
()
)
(Assert
(LogicalNot
(CPtrCompare
(Var 2 p)
NotEq
(PointerNullConstant
(CPtr)
)
(Logical 4 [])
()
)
(Logical 4 [])
()
)
()
)]
()
Public
.false.
.false.
()
),
x:
(Variable
4
5
x
[]
Local
Expand All @@ -91,13 +181,13 @@
main_program:
(Program
(SymbolTable
2
3
{
_lpython_main_program:
(ExternalSymbol
2
3
_lpython_main_program
4 _lpython_main_program
5 _lpython_main_program
_global_symbols
[]
_lpython_main_program
Expand All @@ -107,7 +197,7 @@
main_program
[_global_symbols]
[(SubroutineCall
2 _lpython_main_program
3 _lpython_main_program
()
[]
()
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-bindc_02-bc1a7ea.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-bindc_02-bc1a7ea.stdout",
"stdout_hash": "a48a2ddd1469559be941968442243d048382d13bccf878ab3dd788d7",
"stdout_hash": "a74aa56cff206d4ef8fb0766f1cf596c122255882a7df3f5e4fcf4e7",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
15 changes: 13 additions & 2 deletions tests/reference/asr-bindc_02-bc1a7ea.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@
[]
[]
[(=
(Var 193 yq)
(PointerNullConstant
(CPtr)
)
()
)
(=
(ArrayItem
(Var 193 y)
[(()
Expand Down Expand Up @@ -284,8 +291,12 @@
queries
[]
Local
()
()
(PointerNullConstant
(CPtr)
)
(PointerNullConstant
(CPtr)
)
Default
(CPtr)
Source
Expand Down
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_02-2ab459a.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"outfile": null,
"outfile_hash": null,
"stdout": "asr-structs_02-2ab459a.stdout",
"stdout_hash": "f56302bba116e03b67d1812db40060ab8017dd5ce79b2bdff7baf644",
"stdout_hash": "ae8e8d2163b51eb20e19e6257618899aa4fbe78452e760a38608651d",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading