Skip to content

Support CPtr <-> u64 casting #1848

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 5 commits into from
May 23, 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
8 changes: 7 additions & 1 deletion integration_tests/bindc_03.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from lpython import (c_p_pointer, CPtr, pointer, i32,
Pointer, ccall, p_c_pointer, dataclass,
ccallable, empty_c_void_p)
ccallable, empty_c_void_p, cptr_to_u64,
u64_to_cptr, u64)

@dataclass
class ArrayWrapped:
Expand Down Expand Up @@ -51,6 +52,11 @@ def run():
assert a != empty_c_void_p()
array_wrapped.array = a
f(array_wrapped.array)
q: u64 = cptr_to_u64(a)
x: CPtr
x = u64_to_cptr(q)
array_wrapped.array = x
f(array_wrapped.array)
array_wrapped1 = array_wrapped
h(array_wrapped1.array)

Expand Down
2 changes: 2 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ cast_kind
| LogicalToCharacter
| UnsignedIntegerToInteger
| IntegerToUnsignedInteger
| CPtrToUnsignedInteger
| UnsignedIntegerToCPtr

dimension = (expr? start, expr? length)

Expand Down
10 changes: 10 additions & 0 deletions src/libasr/codegen/asr_to_c_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,16 @@ R"(#include <stdio.h>
last_expr_precedence = 2;
break;
}
case (ASR::cast_kindType::CPtrToUnsignedInteger) : {
src = "(uint64_t)(" + src + ")";
last_expr_precedence = 2;
break;
}
case (ASR::cast_kindType::UnsignedIntegerToCPtr) : {
src = "(void*)(" + src + ")";
last_expr_precedence = 2;
break;
}
default : throw CodeGenError("Cast kind " + std::to_string(x.m_kind) + " not implemented",
x.base.base.loc);
}
Expand Down
8 changes: 8 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6818,6 +6818,14 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
// tmp = tmp
break;
}
case (ASR::cast_kindType::CPtrToUnsignedInteger) : {
tmp = builder->CreatePtrToInt(tmp, getIntType(8, false));
break;
}
case (ASR::cast_kindType::UnsignedIntegerToCPtr) : {
tmp = builder->CreateIntToPtr(tmp, llvm::Type::getVoidTy(context)->getPointerTo());
break;
}
case (ASR::cast_kindType::ComplexToComplex) : {
llvm::Type *target_type;
int arg_kind = -1, dest_kind = -1;
Expand Down
39 changes: 38 additions & 1 deletion src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6985,7 +6985,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// This will all be removed once we port it to intrinsic functions
// Intrinsic functions
if (call_name == "size") {
parse_args(x, args);;
parse_args(x, args);
if( args.size() < 1 || args.size() > 2 ) {
throw SemanticError("array accepts only 1 (arr) or 2 (arr, axis) arguments, got " +
std::to_string(args.size()) + " arguments instead.",
Expand Down Expand Up @@ -7018,6 +7018,43 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
tmp = ASR::make_PointerNullConstant_t(al, x.base.base.loc, type);
return;
} else if (call_name == "cptr_to_u64") {
parse_args(x, args);
if (args.size() != 1) {
throw SemanticError("cptr_to_u64 accpets exactly 1 argument",
x.base.base.loc);
}

ASR::expr_t *var = args[0].m_value;
ASR::ttype_t *a_type = ASRUtils::expr_type(var);
if (!ASR::is_a<ASR::CPtr_t>(*a_type)) {
throw SemanticError("Argument of type `CPtr` is expected in cptr_to_u64",
x.base.base.loc);
}
ASR::ttype_t *u_type = ASRUtils::TYPE(ASR::make_UnsignedInteger_t(al,
x.base.base.loc, 8, nullptr, 0));

tmp = ASR::make_Cast_t(al, x.base.base.loc,
var, ASR::cast_kindType::CPtrToUnsignedInteger, u_type, nullptr);
return;
} else if (call_name == "u64_to_cptr") {
parse_args(x, args);
if (args.size() != 1) {
throw SemanticError("u64_to_cptr accpets exactly 1 argument",
x.base.base.loc);
}

ASR::expr_t *var = args[0].m_value;
ASR::ttype_t *a_type = ASRUtils::expr_type(var);
if (!ASR::is_a<ASR::UnsignedInteger_t>(*a_type)) {
throw SemanticError("Argument of type `u64` is expected in u64_to_cptr",
x.base.base.loc);
}
ASR::ttype_t *c_type = ASRUtils::TYPE(ASR::make_CPtr_t(al, x.base.base.loc));

tmp = ASR::make_Cast_t(al, x.base.base.loc,
var, ASR::cast_kindType::UnsignedIntegerToCPtr, c_type, nullptr);
return;
} else if (call_name == "TypeVar") {
// Ignore TypeVar for now, we handle it based on the identifier itself
tmp = nullptr;
Expand Down
6 changes: 6 additions & 0 deletions src/runtime/lpython/lpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,12 @@ def __repr__(self):

return ctypes_c_void_p()

def cptr_to_u64(cptr):
return ctypes.addressof(cptr)

def u64_to_cptr(ivalue):
return ctypes.c_void_p(ivalue)

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

Expand Down