forked from tshort/StaticCompiler.jl
/
overdub.jl
33 lines (26 loc) · 864 Bytes
/
overdub.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Implements contextual dispatch through Cassette.jl
using Cassette
##
# Convert two-arg `ccall` to single arg.
##
function transform(ctx, ref)
CI = ref.code_info
ismatch = x -> begin
Base.Meta.isexpr(x, :foreigncall) &&
Base.Meta.isexpr(x.args[1], :call)
end
replace = x -> begin
y = Expr(x.head, Any[x.args[1].args[2], x.args[2:end]...])
Expr(x.head, x.args[1].args[2], x.args[2:end]...)
end
Cassette.replace_match!(replace, ismatch, CI.code)
return CI
end
const Pass = Cassette.@pass transform
Cassette.@context Ctx
const ctx = Cassette.disablehooks(Ctx(pass = Pass))
###
# Rewrite functions
###
#@inline Cassette.overdub(ctx::Ctx, ::typeof(+), a::T, b::T) where T<:Union{Float32, Float64} = add_float_contract(a, b)
contextualize(f::F) where F = (args...) -> Cassette.overdub(ctx, f, args...)