Skip to content
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

RFC: add unsafe_pointer_to_any (fixes #2458) #2468

Merged
merged 1 commit into from
Mar 6, 2013
Merged
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
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
@@ -1124,6 +1124,7 @@ export
strerror,
unsafe_ref,
unsafe_assign,
unsafe_pointer_to_any,

# Macros
@str,
3 changes: 3 additions & 0 deletions base/pointer.jl
Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@ unsafe_assign(p::Ptr{Any}, x::ANY, i::Integer) = pointerset(p, x, int(i))
unsafe_assign{T}(p::Ptr{T}, x, i::Integer) = pointerset(p, convert(T, x), int(i))
unsafe_assign{T}(p::Ptr{T}, x) = unsafe_assign(p, convert(T,x), 1)

# convert a boxed jl_value_t* to Any
unsafe_pointer_to_any(p::Ptr) = pointerany(p)

integer(x::Ptr) = convert(Uint, x)
unsigned(x::Ptr) = convert(Uint, x)

12 changes: 10 additions & 2 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ namespace JL_I {
abs_float, copysign_float,
flipsign_int,
// pointer access
pointerref, pointerset,
pointerref, pointerset, pointerany,
// checked arithmetic
checked_sadd, checked_uadd, checked_ssub, checked_usub,
checked_smul, checked_umul,
@@ -488,6 +488,14 @@ static Value *emit_intrinsic(intrinsic f, jl_value_t **args, size_t nargs,
jl_error("pointerset: wrong number of arguments");
return emit_pointerset(args[1], args[2], args[3], ctx);
}
if (f == pointerany) {
if (nargs != 1)
jl_error("pointerany: wrong number of arguments");
if (!jl_is_cpointer_type(expr_type(args[1], ctx)))
jl_error("pointerany: argument must be pointer type");
return builder.CreateBitCast(emit_unboxed(args[1], ctx),
jl_pvalue_llvmt);
}
if (nargs < 1) jl_error("invalid intrinsic call");
Value *x = auto_unbox(args[1], ctx);
Value *y = NULL;
@@ -1082,7 +1090,7 @@ extern "C" void jl_init_intrinsic_functions(void)
ADD_I(fptrunc32); ADD_I(fpext64);
ADD_I(abs_float); ADD_I(copysign_float);
ADD_I(flipsign_int);
ADD_I(pointerref); ADD_I(pointerset);
ADD_I(pointerref); ADD_I(pointerset); ADD_I(pointerany);
ADD_I(checked_sadd); ADD_I(checked_uadd);
ADD_I(checked_ssub); ADD_I(checked_usub);
ADD_I(checked_smul); ADD_I(checked_umul);
3 changes: 3 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
@@ -473,6 +473,9 @@ begin
@test a2 == [101,102,103]
end

@test unsafe_pointer_to_any(ccall(:jl_call1, Ptr{Void}, (Any,Any),
x -> x+1, 314158)) == 314159

# issue #1287, combinations of try, catch, return
begin
local f, g