Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to evaluate ssavalues and locals #395

Merged
merged 3 commits into from
May 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,13 @@ function eval_code(frame::Frame, expr)
end
# see https://github.com/JuliaLang/julia/issues/31255 for the Symbol("") check
vars = filter(v -> v.name != Symbol(""), locals(frame))
defined_ssa = findall(!=(0), [isassigned(data.ssavalues, i) for i in 1:length(data.ssavalues)])
defined_locals = findall(x -> x isa Some, data.locals)
res = gensym()
eval_expr = Expr(:let,
Expr(:block, map(x->Expr(:(=), x...), [(v.name, maybe_quote(v.value isa Core.Box ? v.value.contents : v.value)) for v in vars])...),
Expr(:block, map(x->Expr(:(=), x...), [(v.name, maybe_quote(v.value isa Core.Box ? v.value.contents : v.value)) for v in vars])...,
map(x->Expr(:(=), x...), [(Symbol("%$i"), data.ssavalues[i]) for i in defined_ssa])...,
map(x->Expr(:(=), x...), [(Symbol("@_$i"), data.locals[i].value) for i in defined_locals])...),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this makes slots available by their number but not their name, right?

julia> function summer(A)
           s = zero(eltype(A))
           for i in eachindex(A)
               s += A[i]
           end
           return s
       end
summer (generic function with 1 method)

julia> src = Base.uncompressed_ast(@which summer([1,2,3]))
CodeInfo(
    @ REPL[10]:2 within `summer'
1 ─ %1  = Main.eltype(A)
│         s = Main.zero(%1)
│   @ REPL[10]:3 within `summer'
│   %3  = Main.eachindex(A)
│         @_4 = Base.iterate(%3)
│   %5  = @_4 === nothing
│   %6  = Base.not_int(%5)
└──       goto #4 if not %6
2 ┄ %8  = @_4
│         i = Core.getfield(%8, 1)
│   %10 = Core.getfield(%8, 2)
│   @ REPL[10]:4 within `summer'%11 = s
│   %12 = Base.getindex(A, i)
│         s = %11 + %12@_4 = Base.iterate(%3, %10)
│   %15 = @_4 === nothing%16 = Base.not_int(%15)
└──       goto #4 if not %16
3 ─       goto #2
    @ REPL[10]:6 within `summer'
4 ┄       return s
)

julia> src.slotnames
5-element Array{Symbol,1}:
 Symbol("#self#")
 :A
 :s
 Symbol("")
 :i

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for their name you just evaluate with the local variable name, right? I was a bit unsure if I should include this but it seems easy enough and when you see e.g. @_4 = Base.iterate(%3, %10) in the lowered code, perhaps you just want to look at what @_4 contains.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, duh, you're saying this gets covered the other branch. Carry on 😄

Expr(:block,
Expr(:(=), res, expr),
Expr(:tuple, res, Expr(:tuple, [v.name for v in vars]...))
Expand Down
11 changes: 11 additions & 0 deletions test/eval_code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,14 @@ fr, bp = debug_command(fr, :c)
@test eval_code(fr, "garbage") == ones(10)
eval_code(fr, "non_accessible_variable = 5.0")
@test eval_code(fr, "non_accessible_variable") == 5.0

if VERSION >= v"1.4" # for var"" syntax
# Evaluating SSAValues
f(x) = x^2
frame = JuliaInterpreter.enter_call(f, 5)
JuliaInterpreter.step_expr!(frame)
JuliaInterpreter.step_expr!(frame)
# This could change with changes to Julia lowering
@test eval_code(frame, "var\"%2\"") == Val(2)
@test eval_code(frame, "var\"@_1\"") == f
end