FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Support CPtr <-> u64 casting by Smit-create · Pull Request #1848 · lcompilers/lpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .asdl  (1) .cpp  (2) .h  (1) .py  (2) All 4 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 7 additions & 1 deletion integration_tests/bindc_03.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL