Skip to content
Merged
Show file tree
Hide file tree
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
27 changes: 18 additions & 9 deletions JuliaLowering/src/syntax_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,20 @@ function ccall_macro_lower(ctx, ex, convention, func, rettype, types, args, gc_s
]
]
elseif kf == K"$"
check = @SyntaxTree quote
func = $(func[1])
if !isa(func, Ptr{Cvoid})
name = :($(func[1]))
throw(ArgumentError("interpolated function `$name` was not a `Ptr{Cvoid}`, but $(typeof(func))"))
end
end
fid = @ast ctx func[1] "func"::K"Identifier"
check = @ast ctx func [K"block"
[K"=" fid func[1]]
[K"if"
[K"call" (!isa)::K"Value" fid [K"curly" Ptr::K"Value" Cvoid::K"Value"]]
[K"block"
[K"=" "name"::K"Identifier" [K"quote" func[1]]]
[K"call" throw::K"Value"
[K"call" ArgumentError::K"Value"
[K"string"
"interpolated function `"::K"String"
"name"::K"Identifier"
"` was not a `Ptr{Cvoid}`, but "::K"String"
[K"call" typeof::K"Value" fid]]]]]]]
push!(statements, check)
lowered_func = check[1][1]
else
Expand All @@ -208,9 +215,11 @@ function ccall_macro_lower(ctx, ex, convention, func, rettype, types, args, gc_s
for (i, (type, arg)) in enumerate(zip(types, args))
argi = @ast ctx arg "arg$i"::K"Identifier"
# TODO: Does it help to emit ssavar() here for the `argi`?
push!(statements, @SyntaxTree :(local $argi = Base.cconvert($type, $arg)))
push!(statements,
@ast ctx arg [K"local"
[K"=" argi [K"call" Base.cconvert::K"Value" type arg]]])
push!(roots, argi)
push!(cargs, @SyntaxTree :(Base.unsafe_convert($type, $argi)))
push!(cargs, @ast ctx ex [K"call" Base.unsafe_convert::K"Value" type argi])
end
effect_flags = UInt16(0)
push!(statements, @ast ctx ex [K"foreigncall"
Expand Down
80 changes: 32 additions & 48 deletions JuliaLowering/test/misc_ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -402,59 +402,43 @@ end
# Basic @ccall lowering
@ccall foo(x::X, y::Y)::R
#---------------------
1 JuliaLowering.Base
2 (call top.getproperty %:cconvert)
3 TestMod.X
4 TestMod.x
5 (= slot₁/arg1 (call %%%₄))
6 JuliaLowering.Base
7 (call top.getproperty %:cconvert)
8 TestMod.Y
9 TestMod.y
10 (= slot₂/arg2 (call %%%₉))
11 JuliaLowering.Base
12 (call top.getproperty %₁₁ :unsafe_convert)
13 TestMod.X
14 slot₁/arg1
15 (call %₁₂ %₁₃ %₁₄)
16 JuliaLowering.Base
17 (call top.getproperty %₁₆ :unsafe_convert)
18 TestMod.Y
19 slot₂/arg2
20 (call %₁₇ %₁₈ %₁₉)
21 slot₁/arg1
22 slot₂/arg2
23 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 0 :($(QuoteNode((:ccall, 0x0000, false)))) %₁₅ %₂₀ %₂₁ %₂₂)
24 (return %₂₃)
1 TestMod.X
2 TestMod.x
3 (= slot₁/arg1 (call Base.cconvert %%₂))
4 TestMod.Y
5 TestMod.y
6 (= slot₂/arg2 (call Base.cconvert %%₅))
7 TestMod.X
8 slot₁/arg1
9 (call Base.unsafe_convert %%₈)
10 TestMod.Y
11 slot₂/arg2
12 (call Base.unsafe_convert %₁₀ %₁₁)
13 slot₁/arg1
14 slot₂/arg2
15 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 0 :($(QuoteNode((:ccall, 0x0000, false)))) %%₁₂ %₁₃ %₁₄)
16 (return %₁₅)

########################################
# @ccall lowering with gc_safe
@ccall foo(x::X; y::Y)::R gc_safe=true
#---------------------
1 JuliaLowering.Base
2 (call top.getproperty %:cconvert)
3 TestMod.X
4 TestMod.x
5 (= slot₁/arg1 (call %%%₄))
6 JuliaLowering.Base
7 (call top.getproperty %:cconvert)
8 TestMod.Y
9 TestMod.y
10 (= slot₂/arg2 (call %%%₉))
11 JuliaLowering.Base
12 (call top.getproperty %₁₁ :unsafe_convert)
13 TestMod.X
14 slot₁/arg1
15 (call %₁₂ %₁₃ %₁₄)
16 JuliaLowering.Base
17 (call top.getproperty %₁₆ :unsafe_convert)
18 TestMod.Y
19 slot₂/arg2
20 (call %₁₇ %₁₈ %₁₉)
21 slot₁/arg1
22 slot₂/arg2
23 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 1 :($(QuoteNode((:ccall, 0x0000, true)))) %₁₅ %₂₀ %₂₁ %₂₂)
24 (return %₂₃)
1 TestMod.X
2 TestMod.x
3 (= slot₁/arg1 (call Base.cconvert %%₂))
4 TestMod.Y
5 TestMod.y
6 (= slot₂/arg2 (call Base.cconvert %%₅))
7 TestMod.X
8 slot₁/arg1
9 (call Base.unsafe_convert %%₈)
10 TestMod.Y
11 slot₂/arg2
12 (call Base.unsafe_convert %₁₀ %₁₁)
13 slot₁/arg1
14 slot₂/arg2
15 (foreigncall :foo (static_eval TestMod.R) (static_eval (call core.svec TestMod.X TestMod.Y)) 1 :($(QuoteNode((:ccall, 0x0000, true)))) %%₁₂ %₁₃ %₁₄)
16 (return %₁₅)

########################################
# non-macro ccall with vararg in signature, but none provided
Expand Down