Skip to content

Commit 9062ee2

Browse files
authored
fix at-ccall (#434)
1 parent 8df3bef commit 9062ee2

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/optimize.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,25 @@ function build_compiled_call!(stmt::Expr, fcall, code, idx, nargs::Int, sparams:
270270
if atype === Any
271271
push!(args, arg)
272272
else
273-
@assert arg isa SSAValue
274-
unsafe_convert_expr = code.code[arg.id]::Expr
275-
push!(delete_idx, arg.id) # delete the unsafe_convert
276-
cconvert_stmt = unsafe_convert_expr.args[3]::SSAValue
277-
push!(delete_idx, cconvert_stmt.id) # delete the cconvert
278-
cconvert_expr = code.code[cconvert_stmt.id]::Expr
279-
push!(args, cconvert_expr.args[3])
273+
if arg isa SSAValue
274+
unsafe_convert_expr = code.code[arg.id]::Expr
275+
push!(delete_idx, arg.id) # delete the unsafe_convert
276+
cconvert_stmt = unsafe_convert_expr.args[3]::SSAValue
277+
push!(delete_idx, cconvert_stmt.id) # delete the cconvert
278+
cconvert_expr = code.code[cconvert_stmt.id]::Expr
279+
push!(args, cconvert_expr.args[3])
280+
elseif arg isa SlotNumber
281+
index = findfirst(code.code) do expr
282+
expr isa Expr || return false
283+
lhs = expr.args[1]
284+
return lhs isa SlotNumber && lhs.id === arg.id
285+
end
286+
unsafe_convert_expr = code.code[index]::Expr
287+
push!(delete_idx, index) # delete the unsafe_convert
288+
push!(args, unsafe_convert_expr.args[2])
289+
else
290+
error("unexpected foreigncall argument type encountered: $(typeof(arg))")
291+
end
280292
end
281293
end
282294
else

test/interpret.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,3 +702,10 @@ end
702702
frame = JuliaInterpreter.enter_call(A396, 3)
703703
@test length(JuliaInterpreter.locals(frame)) > 0
704704
end
705+
706+
@static if VERSION >= v"1.5" && Sys.islinux()
707+
@testset "@ccall" begin
708+
f(s) = @ccall strlen(s::Cstring)::Csize_t
709+
@test @interpret(f("asd")) == 3
710+
end
711+
end

0 commit comments

Comments
 (0)