Skip to content

Commit

Permalink
update builtins, use @invokelatest instead of the function version
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Jan 28, 2025
1 parent 64cfc37 commit 5fa1aaf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/breakpoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function shouldbreak(frame::Frame, pc::Int)
isassigned(bps, pc) || return false
bp = bps[pc]
bp.isactive || return false
return Base.invokelatest(bp.condition, frame)::Bool
return invokelatest(bp.condition, frame)::Bool
end

function prepare_slotfunction(framecode::FrameCode, body::Union{Symbol,Expr})
Expand Down
8 changes: 8 additions & 0 deletions src/builtins.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
else
return Some{Any}(Core.ifelse(getargs(args, frame)...))
end
elseif f === Core.memorynew
if nargs == 2
return Some{Any}(Core.memorynew(@lookup(frame, args[2]), @lookup(frame, args[3])))
else
return Some{Any}(Core.memorynew(getargs(args, frame)...))
end
elseif @static isdefined(Core, :memoryref_isassigned) && f === Core.memoryref_isassigned
if nargs == 3
return Some{Any}(Core.memoryref_isassigned(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4])))
Expand Down Expand Up @@ -285,6 +291,8 @@ function maybe_evaluate_builtin(frame, call_expr, expand::Bool)
else
return Some{Any}(isdefined(getargs(args, frame)...))
end
elseif f === isdefinedglobal
return Some{Any}(isdefinedglobal(getargs(args, frame)...))
elseif f === modifyfield!
if nargs == 4
return Some{Any}(modifyfield!(@lookup(frame, args[2]), @lookup(frame, args[3]), @lookup(frame, args[4]), @lookup(frame, args[5])))
Expand Down
14 changes: 7 additions & 7 deletions src/interpret.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ isassign(frame::Frame) = isassign(frame, frame.pc)
isassign(frame::Frame, pc::Int) = (pc in frame.framecode.used)

lookup_var(frame::Frame, val::SSAValue) = frame.framedata.ssavalues[val.id]
lookup_var(frame::Frame, ref::GlobalRef) = invokelatest(getfield, ref.mod, ref.name)
lookup_var(frame::Frame, ref::GlobalRef) = @invokelatest getfield(ref.mod, ref.name)
function lookup_var(frame::Frame, slot::SlotNumber)
val = frame.framedata.locals[slot.id]
val !== nothing && return val.value
Expand Down Expand Up @@ -117,7 +117,7 @@ function lookup_or_eval(@nospecialize(recurse), frame::Frame, @nospecialize(node
elseif f === Val && length(ex.args) == 2
return Val(ex.args[2])
else
Base.invokelatest(error, "unknown call f introduced by ccall lowering ", f)
@invokelatest error("unknown call f introduced by ccall lowering ", f)
end
else
return lookup_expr(frame, ex)
Expand Down Expand Up @@ -147,7 +147,7 @@ function resolvefc(frame::Frame, @nospecialize(expr))
(isa(a, QuoteNode) && a.value === Core.tuple) || error("unexpected ccall to ", expr)
return Expr(:call, GlobalRef(Core, :tuple), (expr::Expr).args[2:end]...)
end
Base.invokelatest(error, "unexpected ccall to ", expr)
@invokelatest error("unexpected ccall to ", expr)
end

function collect_args(@nospecialize(recurse), frame::Frame, call_expr::Expr; isfc::Bool=false)
Expand Down Expand Up @@ -229,7 +229,7 @@ function native_call(fargs::Vector{Any}, frame::Frame)
return Core.eval(moduleof(frame), ex)
end
end
return Base.invokelatest(f, fargs...)
return @invokelatest f(fargs...)
end

function evaluate_call_compiled!(::Compiled, frame::Frame, call_expr::Expr; enter_generated::Bool=false)
Expand Down Expand Up @@ -311,8 +311,8 @@ function evaluate_methoddef(frame::Frame, node::Expr)
if f isa Symbol || f isa GlobalRef
mod = f isa Symbol ? moduleof(frame) : f.mod
name = f isa Symbol ? f : f.name
if Base.isbindingresolved(mod, name) && invokelatest(isdefined, mod, name) # `isdefined` accesses the binding, making it impossible to create a new one
f = invokelatest(getfield, mod, name)
if Base.isbindingresolved(mod, name) && @invokelatest isdefined(mod, name) # `isdefined` accesses the binding, making it impossible to create a new one
f = @invokelatest getfield(mod, name)
else
f = Core.eval(mod, Expr(:function, name)) # create a new function
end
Expand Down Expand Up @@ -656,7 +656,7 @@ e.g., [`JuliaInterpreter.finish!`](@ref)).
"""
function get_return(frame)
node = pc_expr(frame)
is_return(node) || Base.invokelatest(error, "expected return statement, got ", node)
is_return(node) || @invokelatest error("expected return statement, got ", node)
return lookup_return(frame, node)
end
get_return(t::Tuple{Module,Expr,Frame}) = get_return(t[end])
10 changes: 6 additions & 4 deletions src/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ function smallest_ref(stmts, arg, idmin)
end

function lookup_global_ref(a::GlobalRef)
if Base.isbindingresolved(a.mod, a.name) && invokelatest(isdefined, a.mod, a.name) && invokelatest(isconst, a.mod, a.name)
return QuoteNode(invokelatest(getfield, a.mod, a.name))
if (Base.isbindingresolved(a.mod, a.name) &&
(@invokelatest isdefined(a.mod, a.name)) &&
(@invokelatest isconst(a.mod, a.name)))
return QuoteNode(@invokelatest getfield(a.mod, a.name))
end
return a
end
Expand Down Expand Up @@ -114,12 +116,12 @@ function optimize!(code::CodeInfo, scope)
arg1 = stmt.args[1]
if (arg1 === :llvmcall || lookup_stmt(code.code, arg1) === Base.llvmcall) && isempty(sparams) && scope isa Method
# Call via `invokelatest` to avoid compiling it until we need it
Base.invokelatest(build_compiled_llvmcall!, stmt, code, idx, evalmod)
@invokelatest build_compiled_llvmcall!(stmt, code, idx, evalmod)
methodtables[idx] = Compiled()
end
elseif stmt.head === :foreigncall && scope isa Method
# Call via `invokelatest` to avoid compiling it until we need it
Base.invokelatest(build_compiled_foreigncall!, stmt, code, sparams, evalmod)
@invokelatest build_compiled_foreigncall!(stmt, code, sparams, evalmod)
methodtables[idx] = Compiled()
end
end
Expand Down

0 comments on commit 5fa1aaf

Please sign in to comment.