|
| 1 | +""" |
| 2 | + ret = finish!(recurse, frame, istoplevel=false) |
| 3 | + ret = finish!(frame, istoplevel=false) |
| 4 | +
|
| 5 | +Run `frame` until execution terminates. `ret` is either `nothing` (if execution terminates |
| 6 | +when it hits a `return` statement) or a reference to a breakpoint. |
| 7 | +In the latter case, `leaf(frame)` returns the frame in which it hit the breakpoint. |
| 8 | +
|
| 9 | +`recurse` controls call evaluation; `recurse = Compiled()` evaluates :call expressions |
| 10 | +by normal dispatch, whereas the default `recurse = finish_and_return!` uses recursive interpretation. |
| 11 | +""" |
| 12 | +function finish!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 13 | + local pc |
| 14 | + while true |
| 15 | + pc = step_expr!(recurse, frame, istoplevel) |
| 16 | + (pc == nothing || isa(pc, BreakpointRef)) && return pc |
| 17 | + shouldbreak(frame, pc) && return BreakpointRef(frame.framecode, pc) |
| 18 | + end |
| 19 | +end |
| 20 | +finish!(frame::Frame, istoplevel::Bool=false) = finish!(finish_and_return!, frame, istoplevel) |
| 21 | + |
| 22 | +""" |
| 23 | + ret = finish_and_return!(recurse, frame, istoplevel::Bool=false) |
| 24 | + ret = finish_and_return!(frame, istoplevel::Bool=false) |
| 25 | +
|
| 26 | +Call [`JuliaInterpreter.finish!`](@ref) and pass back the return value. If execution |
| 27 | +pauses at a breakpoint, the reference to the breakpoint is returned. |
| 28 | +""" |
| 29 | +function finish_and_return!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 30 | + pc = finish!(recurse, frame, istoplevel) |
| 31 | + isa(pc, BreakpointRef) && return pc |
| 32 | + return get_return(frame) |
| 33 | +end |
| 34 | +finish_and_return!(frame::Frame, istoplevel::Bool=false) = finish_and_return!(finish_and_return!, frame, istoplevel) |
| 35 | + |
| 36 | +""" |
| 37 | + bpref = dummy_breakpoint(recurse, frame::Frame) |
| 38 | +
|
| 39 | +Return a fake breakpoint. This can be useful as the `recurse` argument to `evaluate_call!` |
| 40 | +(or any of the higher-order commands) to ensure that you return immediately after stepping |
| 41 | +into a call. |
| 42 | +""" |
| 43 | +dummy_breakpoint(@nospecialize(recurse), frame::Frame) = BreakpointRef(frame.framecode, 0) |
| 44 | + |
| 45 | +""" |
| 46 | + ret = finish_stack!(recurse, frame, istoplevel=false) |
| 47 | + ret = finish_stack!(frame, istoplevel=false) |
| 48 | +
|
| 49 | +Unwind the callees of `frame`, finishing each before returning to the caller. |
| 50 | +`frame` itself is also finished |
| 51 | +If execution hits a breakpoint, `ret` will be a reference to the breakpoint. |
| 52 | +""" |
| 53 | +function finish_stack!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 54 | + frame0 = frame |
| 55 | + frame = leaf(frame) |
| 56 | + while true |
| 57 | + ret = finish_and_return!(recurse, frame, istoplevel) |
| 58 | + isa(ret, BreakpointRef) && return ret |
| 59 | + frame === frame0 && return ret |
| 60 | + recycle(frame) |
| 61 | + frame = caller(frame) |
| 62 | + frame === nothing && return ret |
| 63 | + pc = frame.pc |
| 64 | + if isassign(frame, pc) |
| 65 | + lhs = getlhs(pc) |
| 66 | + do_assignment!(frame, lhs, ret) |
| 67 | + end |
| 68 | + pc += 1 |
| 69 | + frame.pc = pc |
| 70 | + shouldbreak(frame) && return BreakpointRef(frame.framecode, pc) |
| 71 | + end |
| 72 | + return frame |
| 73 | +end |
| 74 | +finish_stack!(frame::Frame, istoplevel::Bool=false) = finish_stack!(finish_and_return!, frame, istoplevel) |
| 75 | + |
| 76 | +""" |
| 77 | + next_until!(predicate, recurse, frame, istoplevel=false) |
| 78 | + next_until!(predicate, frame, istoplevel=false) |
| 79 | +
|
| 80 | +Execute the current statement. Then step through statements of `frame` until the next |
| 81 | +statement satifies `predicate(stmt)`. |
| 82 | +""" |
| 83 | +function next_until!(@nospecialize(predicate), @nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 84 | + pc = step_expr!(recurse, frame, istoplevel) |
| 85 | + while pc !== nothing && !isa(pc, BreakpointRef) |
| 86 | + if predicate(pc_expr(frame, pc)) || shouldbreak(frame, pc) |
| 87 | + return pc |
| 88 | + end |
| 89 | + pc = step_expr!(recurse, frame, istoplevel) |
| 90 | + end |
| 91 | + return pc |
| 92 | +end |
| 93 | +next_until!(predicate, frame::Frame, istoplevel::Bool=false) = |
| 94 | + next_until!(predicate, finish_and_return!, frame, istoplevel) |
| 95 | + |
| 96 | +next_call!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) = |
| 97 | + next_until!(is_call_or_return, recurse, frame, istoplevel) |
| 98 | +next_call!(frame::Frame, istoplevel::Bool=false) = next_call!(finish_and_return!, frame, istoplevel) |
| 99 | + |
| 100 | +function maybe_next_call!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 101 | + pc = frame.pc |
| 102 | + is_call_or_return(pc_expr(frame, pc)) && return pc |
| 103 | + return next_call!(recurse, frame, istoplevel) |
| 104 | +end |
| 105 | +maybe_next_call!(frame::Frame, istoplevel::Bool=false) = |
| 106 | + maybe_next_call!(finish_and_return!, frame, istoplevel) |
| 107 | + |
| 108 | +""" |
| 109 | + through_methoddef_or_done!(recurse, frame) |
| 110 | + through_methoddef_or_done!(frame) |
| 111 | +
|
| 112 | +Runs `frame` at top level until it either finishes (e.g., hits a `return` statement) |
| 113 | +or defines a new method. |
| 114 | +""" |
| 115 | +function through_methoddef_or_done!(@nospecialize(recurse), frame::Frame) |
| 116 | + predicate(stmt) = isexpr(stmt, :method, 3) || isexpr(stmt, :thunk) |
| 117 | + pc = next_until!(predicate, recurse, frame, true) |
| 118 | + (pc === nothing || isa(pc, BreakpointRef)) && return pc |
| 119 | + return step_expr!(recurse, frame, true) # define the method and return |
| 120 | +end |
| 121 | +through_methoddef_or_done!(@nospecialize(recurse), t::Tuple{Module,Expr,Frame}) = |
| 122 | + through_methoddef_or_done!(recurse, t[end]) |
| 123 | +through_methoddef_or_done!(@nospecialize(recurse), modex::Tuple{Module,Expr,Expr}) = Core.eval(modex[1], modex[3]) |
| 124 | +through_methoddef_or_done!(arg) = through_methoddef_or_done!(finish_and_return!, arg) |
| 125 | + |
| 126 | +function changed_line!(expr, line, fls) |
| 127 | + if length(fls) == 1 && isa(expr, LineNumberNode) |
| 128 | + return expr.line != line |
| 129 | + elseif length(fls) == 1 && isa(expr, Expr) && isexpr(expr, :line) |
| 130 | + return expr.args[1] != line |
| 131 | + else |
| 132 | + if is_loc_meta(expr, :pop_loc) |
| 133 | + pop!(fls) |
| 134 | + elseif is_loc_meta(expr, :push_loc) |
| 135 | + push!(fls,(expr.args[2],0)) |
| 136 | + end |
| 137 | + return false |
| 138 | + end |
| 139 | +end |
| 140 | + |
| 141 | +function next_line!(@nospecialize(recurse), frame::Frame, istoplevel::Bool=false) |
| 142 | + initial = linenumber(frame) |
| 143 | + first = true |
| 144 | + pc = frame.pc |
| 145 | + while linenumber(frame, pc) == initial |
| 146 | + # If this is a return node, interrupt execution |
| 147 | + expr = pc_expr(frame, pc) |
| 148 | + (!first && isexpr(expr, :return)) && return pc |
| 149 | + first = false |
| 150 | + # If this is a goto node, step it and reevaluate |
| 151 | + if is_goto_node(expr) |
| 152 | + pc = step_expr!(recurse, frame, istoplevel) |
| 153 | + (pc === nothing || isa(pc, BreakpointRef)) && return pc |
| 154 | + elseif recurse !== nothing && is_wrapper_call(expr) |
| 155 | + # With splatting it can happen that we do something like ssa = tuple(#self#), _apply(ssa), which |
| 156 | + # confuses the logic here, just step into the first call that's not a builtin |
| 157 | + switched = false |
| 158 | + while is_wrapper_call(expr) |
| 159 | + ret = evaluate_call!(dummy_breakpoint, frame, expr) |
| 160 | + if frame.callee === nothing && !isa(ret, BreakpointRef) |
| 161 | + # This wasn't a real wrapper call |
| 162 | + if isassign(frame, pc) |
| 163 | + lhs = getlhs(pc) |
| 164 | + do_assignment!(frame, lhs, ret) |
| 165 | + end |
| 166 | + frame.pc = pc = pc + 1 |
| 167 | + break |
| 168 | + end |
| 169 | + frame = frame.callee |
| 170 | + switched = true |
| 171 | + expr = pc_expr(frame) |
| 172 | + end |
| 173 | + # Signal that we've switched frames |
| 174 | + switched && return BreakpointRef(frame.framecode, frame.pc) |
| 175 | + else |
| 176 | + pc = step_expr!(recurse, frame, istoplevel) |
| 177 | + (pc === nothing || isa(pc, BreakpointRef)) && return pc |
| 178 | + end |
| 179 | + shouldbreak(frame, pc) && return BreakpointRef(frame.framecode, pc) |
| 180 | + end |
| 181 | + maybe_next_call!(recurse, frame, pc) |
| 182 | +end |
| 183 | +next_line!(frame::Frame, istoplevel::Bool=false) = next_line!(finish_and_return!, frame, istoplevel) |
0 commit comments