Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
[compat]
CodeTracking = "2"
Documenter = "1"
julia = "~1.10"
10 changes: 5 additions & 5 deletions docs/src/ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ branching into `@goto/@label` equivalents:

This has very close correspondence to the lowered representation:

```julia
```julia-repl
julia> code = @code_lowered debuginfo=:source summer(A)
CodeInfo(
@ REPL[1]:2 within `summer'
Expand Down Expand Up @@ -136,9 +136,9 @@ similar to how we named it in our alternative implementation above.)

Let's look at a couple of the fields of the `CodeInfo`. First, the statements themselves:

```julia
```julia-repl
julia> code.code
16-element Array{Any,1}:
16-element Vector{Any}:
:(_3 = Main.zero($(Expr(:static_parameter, 1))))
:(_2)
:(_4 = Base.iterate(%2))
Expand All @@ -162,9 +162,9 @@ present in the statement list.
The most noteworthy change here is the appearance of more objects like `_3`, which are
references that index into local variable slots:

```julia
```julia-repl
julia> code.slotnames
5-element Array{Any,1}:
5-element Vector{Any}:
Symbol("#self#")
:A
:s
Expand Down
74 changes: 26 additions & 48 deletions docs/src/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ evaluate these statements one-by-one, branching via the `goto` statements as app
Using the `summer` example described in [Lowered representation](@ref),
let's build a frame:

```julia
```julia-repl
julia> frame = JuliaInterpreter.enter_call(summer, A)
Frame for summer(A::AbstractArray{T,N} where N) where T in Main at REPL[2]:2
1* 2 1 ─ s = (zero)($(Expr(:static_parameter, 1)))
Expand All @@ -26,9 +26,9 @@ in that it has been processed by [`JuliaInterpreter.optimize!`](@ref) to speed u
`frame` has another field, `framedata`, that holds values needed for or generated by execution.
The input arguments and local variables are in `locals`:

```julia
```julia-repl
julia> frame.framedata.locals
5-element Array{Union{Nothing, Some{Any}},1}:
5-element Vector{Union{Nothing, Some{Any}}}:
Some(summer)
Some([1, 2, 5])
nothing
Expand All @@ -41,19 +41,19 @@ is the input array. The remaining local variables (e.g., `s` and `a`), have not
only built the frame, but we haven't yet begun to execute it.
The static parameter, `T`, is stored in `frame.framedata.sparams`:

```julia
```julia-repl
julia> frame.framedata.sparams
1-element Array{Any,1}:
1-element Vector{Any}:
Int64
```

The `Expr(:static_parameter, 1)` statement refers to this value.

The other main storage is for the generated SSA values:

```julia
```julia-repl
julia> frame.framedata.ssavalues
16-element Array{Any,1}:
16-element Vector{Any}:
#undef
#undef
#undef
Expand All @@ -77,14 +77,14 @@ Since we haven't executed any statements yet, these are all undefined.
The other main entity is the so-called [program counter](https://en.wikipedia.org/wiki/Program_counter),
which just indicates the next statement to be executed:

```julia
```julia-repl
julia> frame.pc
1
```

Let's try executing the first statement:

```julia
```julia-repl
julia> JuliaInterpreter.step_expr!(frame)
2
```
Expand All @@ -96,9 +96,9 @@ back to `frame`.)
Since the first statement is an assignment of a local variable, let's check the
locals again:

```julia
```julia-repl
julia> frame.framedata.locals
5-element Array{Union{Nothing, Some{Any}},1}:
5-element Vector{Union{Nothing, Some{Any}}}:
Some(summer)
Some([1, 2, 5])
Some(0)
Expand All @@ -111,12 +111,12 @@ You can see that the entry corresponding to `s` has been initialized.
The next statement just retrieves one of the slots (the input argument `A`) and stores
it in an SSA value:

```julia
```julia-repl
julia> JuliaInterpreter.step_expr!(frame)
3

julia> frame.framedata.ssavalues
16-element Array{Any,1}:
16-element Vector{Any}:
#undef
[1, 2, 5]
#undef
Expand Down Expand Up @@ -179,45 +179,23 @@ such expressions.

Here's a demonstration of the problem:

```julia
ex = :(map(x->x^2, [1, 2, 3]))
frame = Frame(Main, ex)
```julia-repl
julia> ex = :(map(x->x^2, [1, 2, 3]));

julia> frame = Frame(Main, ex);

julia> JuliaInterpreter.finish_and_return!(frame)
ERROR: this frame needs to be run a top level
```

The reason for this error becomes clearer if we examine `frame` or look directly at the lowered code:

```julia
julia> Meta.lower(Main, ex)
:($(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ $(Expr(:thunk, CodeInfo(
@ none within `top-level scope`
1 ─ global var"#3#4"
│ const var"#3#4"
│ %3 = Core._structtype(Main, Symbol("#3#4"), Core.svec(), Core.svec(), Core.svec(), false, 0)
│ var"#3#4" = %3
│ Core._setsuper!(var"#3#4", Core.Function)
│ Core._typebody!(var"#3#4", Core.svec())
└── return nothing
)))
│ %2 = Core.svec(var"#3#4", Core.Any)
│ %3 = Core.svec()
│ %4 = Core.svec(%2, %3, $(QuoteNode(:(#= REPL[18]:1 =#))))
│ $(Expr(:method, false, :(%4), CodeInfo(
@ REPL[18]:1 within `none`
1 ─ %1 = Core.apply_type(Base.Val, 2)
│ %2 = (%1)()
│ %3 = Base.literal_pow(^, x, %2)
└── return %3
)))
│ #3 = %new(var"#3#4")
│ %7 = #3
│ %8 = Base.vect(1, 2, 3)
│ %9 = map(%7, %8)
└── return %9
))))
```@setup world-age-example
ex = :(map(x->x^2, [1, 2, 3]))
```

```@repl world-age-example
Meta.lower(Main, ex)
```

All of the code before the `%7` line is devoted to defining the anonymous function `x->x^2`:
Expand All @@ -226,9 +204,9 @@ function" for this type, equivalent to `(var"#3#4")(x) = x^2`.

In some cases one can fix this simply by indicating that we want to run this frame at top level:

```julia
```julia-repl
julia> JuliaInterpreter.finish_and_return!(frame, true)
3-element Array{Int64,1}:
3-element Vector{Int64}:
1
4
9
Expand Down
4 changes: 2 additions & 2 deletions src/construct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ Optionally supply an initial `LineNumberNode` `lnn`.

In a fresh session,

```
```julia-repl
julia> expr = quote
public_fn(x::Integer) = true
module Private
Expand Down Expand Up @@ -412,7 +412,7 @@ For evaluation by JuliaInterpreter, the returned module/expression pairs can be
the `Frame` constructor. However, some expressions cannot be converted into `Frame`s and may need
special handling:

```julia
```julia-repl
julia> for (mod, ex) in ExprSplitter(Main, expr)
if ex.head === :global
# global declarations can't be lowered to a CodeInfo.
Expand Down
Loading