Skip to content

Commit

Permalink
Merge pull request #2468 from stevengj/pointerany
Browse files Browse the repository at this point in the history
RFC: add unsafe_pointer_to_any (fixes #2458)
  • Loading branch information
JeffBezanson committed Mar 6, 2013
2 parents 97cf261 + 428405f commit ea73f25
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ export
strerror,
unsafe_ref,
unsafe_assign,
unsafe_pointer_to_any,

# Macros
@str,
Expand Down
3 changes: 3 additions & 0 deletions base/pointer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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)

This comment has been minimized.

Copy link
@vtjnash

vtjnash Mar 6, 2013

Member

does this need to be added to inference.jl also?

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Mar 6, 2013

Author Member

Not really, since we don't know anything about the result type.


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

Expand Down
12 changes: 10 additions & 2 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -493,6 +493,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;
Expand Down Expand Up @@ -1087,7 +1095,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);
Expand Down
3 changes: 3 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea73f25

Please sign in to comment.