Skip to content

Commit 8d679fa

Browse files
authored
Merge pull request #1805 from Smit-create/i-1781
Support nullptr testing
2 parents 6e5595e + 1b162b9 commit 8d679fa

15 files changed

+266
-27
lines changed

integration_tests/bindc_01.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
queries: CPtr = empty_c_void_p()
44
x: Pointer[i16] = c_p_pointer(queries, i16)
55
print(queries, x)
6+
7+
8+
def test_issue_1781():
9+
p: CPtr = empty_c_void_p()
10+
assert p == empty_c_void_p()
11+
assert not (p != empty_c_void_p())
12+
13+
14+
test_issue_1781()

integration_tests/bindc_03.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from lpython import c_p_pointer, CPtr, pointer, i32, Pointer, ccall, p_c_pointer, dataclass, ccallable
1+
from lpython import (c_p_pointer, CPtr, pointer, i32,
2+
Pointer, ccall, p_c_pointer, dataclass,
3+
ccallable, empty_c_void_p)
24

35
@dataclass
46
class ArrayWrapped:
@@ -46,6 +48,7 @@ def run():
4648
size: i32
4749
size = 10
4850
a = get_array(size)
51+
assert a != empty_c_void_p()
4952
array_wrapped.array = a
5053
f(array_wrapped.array)
5154
array_wrapped1 = array_wrapped

src/libasr/ASR.asdl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ expr
278278
| StringOrd(expr arg, ttype type, expr? value)
279279
| StringChr(expr arg, ttype type, expr? value)
280280

281+
| CPtrCompare(expr left, cmpop op, expr right, ttype type, expr? value)
282+
281283
| DictConstant(expr* keys, expr* values, ttype type)
282284
| DictLen(expr arg, ttype type, expr? value)
283285

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,10 @@ R"(#include <stdio.h>
15561556
handle_Compare(x);
15571557
}
15581558

1559+
void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
1560+
handle_Compare(x);
1561+
}
1562+
15591563
template<typename T>
15601564
void handle_Compare(const T &x) {
15611565
CHECK_FAST_C_CPP(compiler_options, x)
@@ -1663,6 +1667,10 @@ R"(#include <stdio.h>
16631667
}
16641668
}
16651669

1670+
void visit_PointerNullConstant(const ASR::PointerNullConstant_t& /*x*/) {
1671+
src = "NULL";
1672+
}
1673+
16661674
void visit_GetPointer(const ASR::GetPointer_t& x) {
16671675
CHECK_FAST_C_CPP(compiler_options, x)
16681676
self().visit_expr(*x.m_arg);

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5154,6 +5154,49 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
51545154
}
51555155
}
51565156

5157+
void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
5158+
if (x.m_value) {
5159+
this->visit_expr_wrapper(x.m_value, true);
5160+
return;
5161+
}
5162+
this->visit_expr_wrapper(x.m_left, true);
5163+
llvm::Value *left = tmp;
5164+
left = builder->CreatePtrToInt(left, getIntType(8, false));
5165+
this->visit_expr_wrapper(x.m_right, true);
5166+
llvm::Value *right = tmp;
5167+
right = builder->CreatePtrToInt(right, getIntType(8, false));
5168+
switch (x.m_op) {
5169+
case (ASR::cmpopType::Eq) : {
5170+
tmp = builder->CreateICmpEQ(left, right);
5171+
break;
5172+
}
5173+
case (ASR::cmpopType::Gt) : {
5174+
tmp = builder->CreateICmpSGT(left, right);
5175+
break;
5176+
}
5177+
case (ASR::cmpopType::GtE) : {
5178+
tmp = builder->CreateICmpSGE(left, right);
5179+
break;
5180+
}
5181+
case (ASR::cmpopType::Lt) : {
5182+
tmp = builder->CreateICmpSLT(left, right);
5183+
break;
5184+
}
5185+
case (ASR::cmpopType::LtE) : {
5186+
tmp = builder->CreateICmpSLE(left, right);
5187+
break;
5188+
}
5189+
case (ASR::cmpopType::NotEq) : {
5190+
tmp = builder->CreateICmpNE(left, right);
5191+
break;
5192+
}
5193+
default : {
5194+
throw CodeGenError("Comparison operator not implemented",
5195+
x.base.base.loc);
5196+
}
5197+
}
5198+
}
5199+
51575200
void visit_RealCompare(const ASR::RealCompare_t &x) {
51585201
if (x.m_value) {
51595202
this->visit_expr_wrapper(x.m_value, true);

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5841,6 +5841,12 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
58415841
x.base.base.loc);
58425842
}
58435843
tmp = ASR::make_ListCompare_t(al, x.base.base.loc, left, asr_op, right, type, value);
5844+
} else if (ASR::is_a<ASR::CPtr_t>(*dest_type)) {
5845+
if (asr_op != ASR::cmpopType::Eq && asr_op != ASR::cmpopType::NotEq) {
5846+
throw SemanticError("Only Equal and Not-equal operators are supported for CPtr",
5847+
x.base.base.loc);
5848+
}
5849+
tmp = ASR::make_CPtrCompare_t(al, x.base.base.loc, left, asr_op, right, type, value);
58445850
} else {
58455851
throw SemanticError("Compare not supported for type: " + ASRUtils::type_to_str_python(dest_type),
58465852
x.base.base.loc);
@@ -6882,7 +6888,13 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
68826888
} else if (call_name == "empty_c_void_p") {
68836889
// TODO: check that `empty_c_void_p uses` has arguments that are compatible
68846890
// with the type
6885-
tmp = nullptr;
6891+
ASR::ttype_t* type;
6892+
if (ann_assign_target_type) {
6893+
type = ann_assign_target_type;
6894+
} else {
6895+
type = ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc));
6896+
}
6897+
tmp = ASR::make_PointerNullConstant_t(al, x.base.base.loc, type);
68866898
return;
68876899
} else if (call_name == "TypeVar") {
68886900
// Ignore TypeVar for now, we handle it based on the identifier itself

src/runtime/lpython/lpython.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,15 @@ def p_c_pointer(ptr, cptr):
542542
cptr.value = id(ptr)
543543

544544
def empty_c_void_p():
545-
return ctypes.c_void_p()
545+
class ctypes_c_void_p(ctypes.c_void_p):
546+
547+
def __eq__(self, value):
548+
return self.value == value.value
549+
550+
def __repr__(self):
551+
return str(self.value)
552+
553+
return ctypes_c_void_p()
546554

547555
def sizeof(arg):
548556
return ctypes.sizeof(convert_type_to_ctype(arg))

tests/reference/asr-bindc_01-6d521a9.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-bindc_01-6d521a9",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/../integration_tests/bindc_01.py",
5-
"infile_hash": "f628ce81b32f2730f936232bb235f39d4372912bc332f3c97e983ad7",
5+
"infile_hash": "3cfb601d3294c470842a85777832f5582ab52cb5bd64c0e02d40deb6",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-bindc_01-6d521a9.stdout",
9-
"stdout_hash": "c1c5afabc9ecb18c731ad21825ee23c181c1965e4acf5fd2776b2008",
9+
"stdout_hash": "d02c57ff6ddb41568c291b11a31301870bf2bc3a970461a71ec23a9d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_01-6d521a9.stdout

Lines changed: 105 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
_global_symbols:
66
(Module
77
(SymbolTable
8-
4
8+
5
99
{
1010
_lpython_main_program:
1111
(Function
1212
(SymbolTable
13-
3
13+
4
1414
{
1515

1616
})
@@ -30,18 +30,24 @@
3030
[]
3131
.false.
3232
)
33-
[]
33+
[test_issue_1781]
3434
[]
3535
[(CPtrToPointer
36-
(Var 4 queries)
37-
(Var 4 x)
36+
(Var 5 queries)
37+
(Var 5 x)
3838
()
3939
)
4040
(Print
4141
()
42-
[(Var 4 queries)
43-
(Var 4 x)]
42+
[(Var 5 queries)
43+
(Var 5 x)]
44+
()
45+
()
46+
)
47+
(SubroutineCall
48+
5 test_issue_1781
4449
()
50+
[]
4551
()
4652
)]
4753
()
@@ -52,22 +58,106 @@
5258
),
5359
queries:
5460
(Variable
55-
4
61+
5
5662
queries
5763
[]
5864
Local
59-
()
60-
()
65+
(PointerNullConstant
66+
(CPtr)
67+
)
68+
(PointerNullConstant
69+
(CPtr)
70+
)
6171
Default
6272
(CPtr)
6373
Source
6474
Public
6575
Required
6676
.false.
6777
),
78+
test_issue_1781:
79+
(Function
80+
(SymbolTable
81+
2
82+
{
83+
p:
84+
(Variable
85+
2
86+
p
87+
[]
88+
Local
89+
()
90+
()
91+
Default
92+
(CPtr)
93+
Source
94+
Public
95+
Required
96+
.false.
97+
)
98+
})
99+
test_issue_1781
100+
(FunctionType
101+
[]
102+
()
103+
Source
104+
Implementation
105+
()
106+
.false.
107+
.false.
108+
.false.
109+
.false.
110+
.false.
111+
[]
112+
[]
113+
.false.
114+
)
115+
[]
116+
[]
117+
[(=
118+
(Var 2 p)
119+
(PointerNullConstant
120+
(CPtr)
121+
)
122+
()
123+
)
124+
(Assert
125+
(CPtrCompare
126+
(Var 2 p)
127+
Eq
128+
(PointerNullConstant
129+
(CPtr)
130+
)
131+
(Logical 4 [])
132+
()
133+
)
134+
()
135+
)
136+
(Assert
137+
(LogicalNot
138+
(CPtrCompare
139+
(Var 2 p)
140+
NotEq
141+
(PointerNullConstant
142+
(CPtr)
143+
)
144+
(Logical 4 [])
145+
()
146+
)
147+
(Logical 4 [])
148+
()
149+
)
150+
()
151+
)]
152+
()
153+
Public
154+
.false.
155+
.false.
156+
()
157+
),
68158
x:
69159
(Variable
70-
4
160+
5
71161
x
72162
[]
73163
Local
@@ -91,13 +181,13 @@
91181
main_program:
92182
(Program
93183
(SymbolTable
94-
2
184+
3
95185
{
96186
_lpython_main_program:
97187
(ExternalSymbol
98-
2
188+
3
99189
_lpython_main_program
100-
4 _lpython_main_program
190+
5 _lpython_main_program
101191
_global_symbols
102192
[]
103193
_lpython_main_program
@@ -107,7 +197,7 @@
107197
main_program
108198
[_global_symbols]
109199
[(SubroutineCall
110-
2 _lpython_main_program
200+
3 _lpython_main_program
111201
()
112202
[]
113203
()

tests/reference/asr-bindc_02-bc1a7ea.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-bindc_02-bc1a7ea.stdout",
9-
"stdout_hash": "a48a2ddd1469559be941968442243d048382d13bccf878ab3dd788d7",
9+
"stdout_hash": "a74aa56cff206d4ef8fb0766f1cf596c122255882a7df3f5e4fcf4e7",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/asr-bindc_02-bc1a7ea.stdout

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@
130130
[]
131131
[]
132132
[(=
133+
(Var 193 yq)
134+
(PointerNullConstant
135+
(CPtr)
136+
)
137+
()
138+
)
139+
(=
133140
(ArrayItem
134141
(Var 193 y)
135142
[(()
@@ -284,8 +291,12 @@
284291
queries
285292
[]
286293
Local
287-
()
288-
()
294+
(PointerNullConstant
295+
(CPtr)
296+
)
297+
(PointerNullConstant
298+
(CPtr)
299+
)
289300
Default
290301
(CPtr)
291302
Source

tests/reference/asr-structs_02-2ab459a.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-structs_02-2ab459a.stdout",
9-
"stdout_hash": "f56302bba116e03b67d1812db40060ab8017dd5ce79b2bdff7baf644",
9+
"stdout_hash": "ae8e8d2163b51eb20e19e6257618899aa4fbe78452e760a38608651d",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

0 commit comments

Comments
 (0)