Skip to content

Implement cpython support in test_c_interop_03 #603

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 2 commits into from
Jun 14, 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: 1 addition & 1 deletion integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ RUN(NAME test_math_02 LABELS cpython llvm)
RUN(NAME test_c_interop_01 LABELS cpython llvm c)
RUN(NAME test_c_interop_02 LABELS cpython llvm c
EXTRAFILES test_c_interop_02b.c)
RUN(NAME test_c_interop_03 LABELS llvm c
RUN(NAME test_c_interop_03 LABELS cpython llvm c
EXTRAFILES test_c_interop_03b.c)
RUN(NAME test_c_interop_04 LABELS cpython llvm c
EXTRAFILES test_c_interop_04b.c)
Expand Down
20 changes: 11 additions & 9 deletions integration_tests/test_c_interop_03.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ltypes import ccall, f32, f64, i32, i64, CPtr, pointer, Pointer, p_c_pointer
from ltypes import (ccall, f32, f64, i32, i64, CPtr, pointer, Pointer,
p_c_pointer, empty_c_void_p)

@ccall
def f_pi32_i32(x: CPtr) -> i32:
Expand All @@ -20,26 +21,27 @@ def test_c_callbacks():
xi32: i32
xi32 = 3
p: CPtr
p_c_pointer(pointer(xi32), p)
print(pointer(xi32), p)
p = empty_c_void_p()
p_c_pointer(pointer(xi32, i32), p)
print(pointer(xi32, i32), p)
assert f_pi32_i32(p) == 4

xi64: i64
xi64 = 3
p_c_pointer(pointer(xi64), p)
print(pointer(xi64), p)
p_c_pointer(pointer(xi64, i64), p)
print(pointer(xi64, i64), p)
assert f_pi64_i32(p) == 4

xf32: f32
xf32 = 3.3
p_c_pointer(pointer(xf32), p)
print(pointer(xf32), p)
p_c_pointer(pointer(xf32, f32), p)
print(pointer(xf32, f32), p)
assert abs(f_pf32_i32(p)-4.3) < 1e-6

xf64: f64
xf64 = 3.3
p_c_pointer(pointer(xf64), p)
print(pointer(xf64), p)
p_c_pointer(pointer(xf64, f64), p)
print(pointer(xf64, f64), p)
assert abs(f_pf64_i32(p)-4.3) < 1e-12

test_c_callbacks()
5 changes: 5 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,11 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
ASR::symbol_t *s = current_scope->resolve_symbol(name);
if (s) {
tmp = ASR::make_Var_t(al, x.base.base.loc, s);
} else if (name == "i32" || name == "i64" || name == "f32" || name == "f64") {
int64_t i = -1;
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
4, nullptr, 0));
tmp = ASR::make_IntegerConstant_t(al, x.base.base.loc, i, type);
} else {
throw SemanticError("Variable '" + name + "' not declared",
x.base.base.loc);
Expand Down
19 changes: 15 additions & 4 deletions src/runtime/ltypes/ltypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,26 @@ def ccall(f):
wrapped_f = CTypes(f)
return wrapped_f

def pointer(x):
def pointer(x, type=None):
from numpy import ndarray
if isinstance(x, ndarray):
return ctypes.c_void_p(x.ctypes.data)
#return x.ctypes.data_as(ctypes.POINTER(ctypes.c_int32))
else:
type_ = x.annotations[0]
if type_ == i32:
return ctypes.pointer(ctypes.c_int32(x))
if type == i32:
#return ctypes.c_void_p(ctypes.pointer(ctypes.c_int32(x)))
#return ctypes.pointer(ctypes.c_int32(x))
return ctypes.cast(ctypes.pointer(ctypes.c_int32(x)),
ctypes.c_void_p)
elif type == i64:
return ctypes.cast(ctypes.pointer(ctypes.c_int64(x)),
ctypes.c_void_p)
elif type == f32:
return ctypes.cast(ctypes.pointer(ctypes.c_float(x)),
ctypes.c_void_p)
elif type == f64:
return ctypes.cast(ctypes.pointer(ctypes.c_double(x)),
ctypes.c_void_p)
else:
raise Exception("Type not supported in pointer()")

Expand Down