@@ -7,7 +7,7 @@ evaluate these statements one-by-one, branching via the `goto` statements as app
77Using the ` summer ` example described in [ Lowered representation] ( @ref ) ,
88let's build a frame:
99
10- ``` julia
10+ ``` julia-repl
1111julia> frame = JuliaInterpreter.enter_call(summer, A)
1212Frame for summer(A::AbstractArray{T,N} where N) where T in Main at REPL[2]:2
1313 1* 2 1 ─ s = (zero)($(Expr(:static_parameter, 1)))
@@ -26,9 +26,9 @@ in that it has been processed by [`JuliaInterpreter.optimize!`](@ref) to speed u
2626` frame ` has another field, ` framedata ` , that holds values needed for or generated by execution.
2727The input arguments and local variables are in ` locals ` :
2828
29- ``` julia
29+ ``` julia-repl
3030julia> frame.framedata.locals
31- 5 - element Array {Union{Nothing, Some{Any}}, 1 }:
31+ 5-element Vector {Union{Nothing, Some{Any}}}:
3232 Some(summer)
3333 Some([1, 2, 5])
3434 nothing
@@ -41,19 +41,19 @@ is the input array. The remaining local variables (e.g., `s` and `a`), have not
4141only built the frame, but we haven't yet begun to execute it.
4242The static parameter, ` T ` , is stored in ` frame.framedata.sparams ` :
4343
44- ``` julia
44+ ``` julia-repl
4545julia> frame.framedata.sparams
46- 1 - element Array {Any, 1 }:
46+ 1-element Vector {Any}:
4747 Int64
4848```
4949
5050The ` Expr(:static_parameter, 1) ` statement refers to this value.
5151
5252The other main storage is for the generated SSA values:
5353
54- ``` julia
54+ ``` julia-repl
5555julia> frame.framedata.ssavalues
56- 16 - element Array {Any, 1 }:
56+ 16-element Vector {Any}:
5757 #undef
5858 #undef
5959 #undef
@@ -77,14 +77,14 @@ Since we haven't executed any statements yet, these are all undefined.
7777The other main entity is the so-called [ program counter] ( https://en.wikipedia.org/wiki/Program_counter ) ,
7878which just indicates the next statement to be executed:
7979
80- ``` julia
80+ ``` julia-repl
8181julia> frame.pc
82821
8383```
8484
8585Let's try executing the first statement:
8686
87- ``` julia
87+ ``` julia-repl
8888julia> JuliaInterpreter.step_expr!(frame)
89892
9090```
@@ -96,9 +96,9 @@ back to `frame`.)
9696Since the first statement is an assignment of a local variable, let's check the
9797locals again:
9898
99- ``` julia
99+ ``` julia-repl
100100julia> frame.framedata.locals
101- 5 - element Array {Union{Nothing, Some{Any}}, 1 }:
101+ 5-element Vector {Union{Nothing, Some{Any}}}:
102102 Some(summer)
103103 Some([1, 2, 5])
104104 Some(0)
@@ -111,12 +111,12 @@ You can see that the entry corresponding to `s` has been initialized.
111111The next statement just retrieves one of the slots (the input argument ` A ` ) and stores
112112it in an SSA value:
113113
114- ``` julia
114+ ``` julia-repl
115115julia> JuliaInterpreter.step_expr!(frame)
1161163
117117
118118julia> frame.framedata.ssavalues
119- 16 - element Array {Any, 1 }:
119+ 16-element Vector {Any}:
120120 #undef
121121 [1, 2, 5]
122122 #undef
@@ -179,45 +179,23 @@ such expressions.
179179
180180Here's a demonstration of the problem:
181181
182- ``` julia
183- ex = :(map (x-> x^ 2 , [1 , 2 , 3 ]))
184- frame = Frame (Main, ex)
182+ ``` julia-repl
183+ julia> ex = :(map(x->x^2, [1, 2, 3]));
184+
185+ julia> frame = Frame(Main, ex);
186+
185187julia> JuliaInterpreter.finish_and_return!(frame)
186188ERROR: this frame needs to be run a top level
187189```
188190
189191The reason for this error becomes clearer if we examine ` frame ` or look directly at the lowered code:
190192
191- ``` julia
192- julia> Meta. lower (Main, ex)
193- :($ (Expr (:thunk , CodeInfo (
194- @ none within ` top-level scope`
195- 1 ─ $ (Expr (:thunk , CodeInfo (
196- @ none within ` top-level scope`
197- 1 ─ global var"#3#4"
198- │ const var"#3#4"
199- │ % 3 = Core. _structtype (Main, Symbol (" #3#4" ), Core. svec (), Core. svec (), Core. svec (), false , 0 )
200- │ var"#3#4" = % 3
201- │ Core. _setsuper! (var"#3#4" , Core. Function)
202- │ Core. _typebody! (var"#3#4" , Core. svec ())
203- └── return nothing
204- )))
205- │ % 2 = Core. svec (var"#3#4" , Core. Any)
206- │ % 3 = Core. svec ()
207- │ % 4 = Core. svec (% 2 , % 3 , $ (QuoteNode (:(#= REPL[18]:1 =# ))))
208- │ $ (Expr (:method , false , :(% 4 ), CodeInfo (
209- @ REPL[18 ]: 1 within ` none`
210- 1 ─ % 1 = Core. apply_type (Base. Val, 2 )
211- │ % 2 = (% 1 )()
212- │ % 3 = Base. literal_pow (^ , x, % 2 )
213- └── return % 3
214- )))
215- │ # 3 = %new(var"#3#4")
216- │ % 7 = # 3
217- │ % 8 = Base. vect (1 , 2 , 3 )
218- │ % 9 = map (% 7 , % 8 )
219- └── return % 9
220- ))))
193+ ``` @setup world-age-example
194+ ex = :(map(x->x^2, [1, 2, 3]))
195+ ```
196+
197+ ``` @repl world-age-example
198+ Meta.lower(Main, ex)
221199```
222200
223201All of the code before the ` %7 ` line is devoted to defining the anonymous function ` x->x^2 ` :
@@ -226,9 +204,9 @@ function" for this type, equivalent to `(var"#3#4")(x) = x^2`.
226204
227205In some cases one can fix this simply by indicating that we want to run this frame at top level:
228206
229- ``` julia
207+ ``` julia-repl
230208julia> JuliaInterpreter.finish_and_return!(frame, true)
231- 3 - element Array {Int64, 1 }:
209+ 3-element Vector {Int64}:
232210 1
233211 4
234212 9
0 commit comments