Eval a CodeInfo object?

Digging through the Julia AST docs I’ve got curious how far we can go with the lowered code representation. In particular:

  1. Can we evaluate CodeInfo object as a whole?
  2. Can we evaluate each its expression (in .code field) step by step?
  3. Can we run type inference and/or compile CodeInfo object into a callable object (e.g. a function)?

I did all of these extensively in Espresso.jl for surface AST, but it has its drawbacks, so I’d like to know what I can do with the lowered version.

3 Likes

I have the same question, and Google gave me this thread and no other answer.

An example from the documentation lead me to try this:

julia> g() = 42
g (generic function with 1 method)

julia> ci = code_typed(g, ())[1][1]
CodeInfo(
1 ─     return 42
)

julia> eval(Expr(:thunk, ci))
42

So it seems I’m able to evaluate a CodeInfo object, but I can’t figure out how to turn one into a function.

Any help would be appreciated.

It seems an example of how to turn a CodeInfo object into a function can be found here:

:slight_smile:

2 Likes

Based on the above-mentioned code by @NHDaly, I wrote a small package that can be used to turn CodeInfo objects back into functions. (Still unregistered, with very little documentation and testing.)

4 Likes

Wow, looks great! Thanks for your effort!

Cool, that looks really handy! :slight_smile: I’m glad it was helpful for you.

1 Like