Skip to content

Commit

Permalink
Pass a function as Any
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Oct 11, 2021
1 parent 4eb674d commit e4a1edd
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions test/backtrace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,42 +269,37 @@ NOTE: `f` and all functions reachable from `f` must not contain a yield point.
function withalloca end

function withalloca_wrapper(fptr, int)
ref = unsafe_pointer_to_objref(Ptr{Cvoid}(fptr))
f = ref[]
f = unsafe_pointer_to_objref(Ptr{Cvoid}(fptr))
f(Ptr{Cvoid}(int))
nothing
end

@eval function withalloca(f, nbytes)
ref = Ref(f)
GC.@preserve ref begin
Base.llvmcall(
(
$"""
define void @entry(i$(Sys.WORD_SIZE) %0,
i$(Sys.WORD_SIZE) %1,
i$(Sys.WORD_SIZE) %2) {
top:
%aptr = alloca i8, i$(Sys.WORD_SIZE) %2
%aint = ptrtoint i8* %aptr to i$(Sys.WORD_SIZE)
%fptr = inttoptr i$(Sys.WORD_SIZE) %0 to void (i$(Sys.WORD_SIZE),
i$(Sys.WORD_SIZE))*
call void %fptr(i$(Sys.WORD_SIZE) %1, i$(Sys.WORD_SIZE) %aint)
ret void
}
""",
"entry",
),
Cvoid,
Tuple{Ptr{Cvoid},Ptr{Cvoid},Int},
Base.unsafe_convert(
Ptr{Cvoid},
@cfunction(withalloca_wrapper, Cvoid, (UInt,UInt)),
),
pointer_from_objref(ref),
nbytes,
)
end
Base.llvmcall(
(
$"""
define void @entry(i$(Sys.WORD_SIZE) %0, {}* %1, i$(Sys.WORD_SIZE) %2) {
top:
%aptr = alloca i8, i$(Sys.WORD_SIZE) %2
%aint = ptrtoint i8* %aptr to i$(Sys.WORD_SIZE)
%wptr = inttoptr i$(Sys.WORD_SIZE) %0 to void (i$(Sys.WORD_SIZE),
i$(Sys.WORD_SIZE))*
%fptr = ptrtoint {}* %1 to i$(Sys.WORD_SIZE)
call void %wptr(i$(Sys.WORD_SIZE) %fptr, i$(Sys.WORD_SIZE) %aint)
ret void
}
""",
"entry",
),
Cvoid,
Tuple{Ptr{Cvoid},Any,Int},
Base.unsafe_convert(
Ptr{Cvoid},
@cfunction(withalloca_wrapper, Cvoid, (UInt,UInt)),
),
f,
nbytes,
)
end

function sandwiched_backtrace()
Expand Down

0 comments on commit e4a1edd

Please sign in to comment.