|
39 | 39 |
|
40 | 40 | # OpaqueClosure construction from pre-inferred CodeInfo/IRCode
|
41 | 41 | using Core: CodeInfo, SSAValue
|
42 |
| -using Base: Compiler |
43 |
| -using .Compiler: IRCode |
44 |
| - |
45 |
| -function compute_ir_rettype(ir::IRCode) |
46 |
| - rt = Union{} |
47 |
| - for i = 1:length(ir.stmts) |
48 |
| - stmt = ir[SSAValue(i)][:stmt] |
49 |
| - if isa(stmt, Core.ReturnNode) && isdefined(stmt, :val) |
50 |
| - rt = Compiler.tmerge(Compiler.argextype(stmt.val, ir), rt) |
51 |
| - end |
52 |
| - end |
53 |
| - return Compiler.widenconst(rt) |
54 |
| -end |
55 |
| - |
56 |
| -function compute_oc_signature(ir::IRCode, nargs::Int, isva::Bool) |
57 |
| - argtypes = Vector{Any}(undef, nargs) |
58 |
| - for i = 1:nargs |
59 |
| - argtypes[i] = Compiler.widenconst(ir.argtypes[i+1]) |
60 |
| - end |
61 |
| - if isva |
62 |
| - lastarg = pop!(argtypes) |
63 |
| - if lastarg <: Tuple |
64 |
| - append!(argtypes, lastarg.parameters) |
65 |
| - else |
66 |
| - push!(argtypes, Vararg{Any}) |
67 |
| - end |
68 |
| - end |
69 |
| - return Tuple{argtypes...} |
70 |
| -end |
71 |
| - |
72 |
| -function Core.OpaqueClosure(ir::IRCode, @nospecialize env...; |
73 |
| - isva::Bool = false, |
74 |
| - slotnames::Union{Nothing,Vector{Symbol}}=nothing, |
75 |
| - kwargs...) |
76 |
| - # NOTE: we need ir.argtypes[1] == typeof(env) |
77 |
| - ir = Core.Compiler.copy(ir) |
78 |
| - # if the user didn't specify a definition MethodInstance or filename Symbol to use for the debuginfo, set a filename now |
79 |
| - ir.debuginfo.def === nothing && (ir.debuginfo.def = :var"generated IR for OpaqueClosure") |
80 |
| - nargtypes = length(ir.argtypes) |
81 |
| - nargs = nargtypes-1 |
82 |
| - sig = compute_oc_signature(ir, nargs, isva) |
83 |
| - rt = compute_ir_rettype(ir) |
84 |
| - src = ccall(:jl_new_code_info_uninit, Ref{CodeInfo}, ()) |
85 |
| - if slotnames === nothing |
86 |
| - src.slotnames = fill(:none, nargtypes) |
87 |
| - else |
88 |
| - length(slotnames) == nargtypes || error("mismatched `argtypes` and `slotnames`") |
89 |
| - src.slotnames = slotnames |
90 |
| - end |
91 |
| - src.slotflags = fill(zero(UInt8), nargtypes) |
92 |
| - src.slottypes = copy(ir.argtypes) |
93 |
| - src.isva = isva |
94 |
| - src.nargs = nargtypes |
95 |
| - src = Core.Compiler.ir_to_codeinf!(src, ir) |
96 |
| - src.rettype = rt |
97 |
| - return generate_opaque_closure(sig, Union{}, rt, src, nargs, isva, env...; kwargs...) |
98 |
| -end |
99 | 42 |
|
100 | 43 | function Core.OpaqueClosure(src::CodeInfo, @nospecialize env...; rettype, sig, nargs, isva=false, kwargs...)
|
101 | 44 | return generate_opaque_closure(sig, Union{}, rettype, src, nargs, isva, env...; kwargs...)
|
|
0 commit comments