Skip to content

Commit

Permalink
Merge pull request #16167 from JuliaLang/jn/ccallable
Browse files Browse the repository at this point in the history
update `@ccallable` to expect a return type declaration
  • Loading branch information
vtjnash committed May 2, 2016
2 parents da7ac48 + d49d403 commit 1657070
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
8 changes: 2 additions & 6 deletions base/c.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ function ccallable(f::Function, rt::Type, argt::Type, name::Union{AbstractString
ccall(:jl_extern_c, Void, (Any, Any, Any, Cstring), f, rt, argt, name)
end

function ccallable(f::Function, argt::Type, name::Union{AbstractString,Symbol}=string(f))
ccall(:jl_extern_c, Void, (Any, Ptr{Void}, Any, Cstring), f, C_NULL, argt, name)
end

macro ccallable(def)
macro ccallable(rt, def)
if isa(def,Expr) && (def.head === :(=) || def.head === :function)
sig = def.args[1]
if sig.head === :call
Expand All @@ -227,7 +223,7 @@ macro ccallable(def)
end
return quote
$(esc(def))
ccallable($(esc(name)), $(Expr(:curly, :Tuple, map(esc, at)...)))
ccallable($(esc(name)), $(esc(rt)), $(Expr(:curly, :Tuple, map(esc, at)...)), $(string(name)))
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,32 @@ end
#15995
@deprecate symbol Symbol

macro ccallable(def)
depwarn("@ccallable requires a return type", Symbol("@ccallable"))
if isa(def,Expr) && (def.head === :(=) || def.head === :function)
sig = def.args[1]
if sig.head === :call
name = sig.args[1]
at = map(sig.args[2:end]) do a
if isa(a,Expr) && a.head === :(::)
a.args[2]
else
:Any
end
end
return quote
$(esc(def))
let name = $(esc(name)), tt = $(Expr(:curly, :Tuple, map(esc, at)...))
rt = return_types(name, tt)
length(rt) == 1 || error("function not ccallable")
ccallable(name, rt[1], tt)
end
end
end
end
error("expected method definition in @ccallable")
end

# During the 0.5 development cycle, do not add any deprecations below this line
# To be deprecated in 0.6

Expand Down
8 changes: 7 additions & 1 deletion src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,13 @@ static Type *julia_struct_to_llvm(jl_value_t *jt, bool *isboxed)
}
#ifndef NDEBUG
// If LLVM and Julia disagree about alignment, much trouble ensues, so check it!
unsigned llvm_alignment = jl_ExecutionEngine->getDataLayout().getABITypeAlignment((Type*)jst->struct_decl);
const DataLayout &DL =
#ifdef LLVM36
jl_ExecutionEngine->getDataLayout();
#else
*jl_ExecutionEngine->getDataLayout();
#endif
unsigned llvm_alignment = DL.getABITypeAlignment((Type*)jst->struct_decl);
unsigned julia_alignment = jst->alignment;
assert(llvm_alignment==julia_alignment);
#endif
Expand Down
14 changes: 6 additions & 8 deletions test/llvmcall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,15 @@ confuse_declname_parsing()


module ObjLoadTest
using Base.Test
using Base.llvmcall
here = dirname(@__FILE__)
using Base: Test, llvmcall, @ccallable
didcall = false
function callback()
@ccallable Void function jl_the_callback()
global didcall
didcall = true
nothing
end
Base.ccallable(callback,Void,Tuple{},:jl_the_callback)
Base.ccallable(callback,Void,Tuple{},:_jl_the_callback)
# Make sure everything up until here gets compiled
callback(); didcall = false
jl_the_callback(); didcall = false
function do_the_call()
llvmcall(
(""" declare void @jl_the_callback()""",
Expand All @@ -169,7 +165,7 @@ module ObjLoadTest
end

# Test for proper parenting
let
if VersionNumber(Base.libllvm_version) >= v"3.6" # llvm 3.6 changed the syntax for a gep, so just ignore this test on older versions
function foo()
# this IR snippet triggers an optimization relying
# on the llvmcall function having a parent module
Expand All @@ -179,4 +175,6 @@ let
Void, Tuple{})
end
code_llvm(DevNull, foo, ())
else
println("INFO: skipping gep parentage test on llvm < 3.6")
end

0 comments on commit 1657070

Please sign in to comment.