如何根据函数和参数类型获取 julia 代码的 Exprs?

how can i get Exprs of julia code accoring to function and argtypes?

我是 Julia 的新人。我读了一篇关于 julia static-analysis 的文档。它提供了一个功能。

function foo(x,y)
  z = x + y
  return 2 * z
end

并使用 julia 自省函数 code_typed 得到输出:

code_typed(foo,(Int64,Int64))

1-element Array{Any,1}:
:($(Expr(:lambda, {:x,:y}, {{:z},{{:x,Int64,0},{:y,Int64,0},{:z,Int64,18}},{}},
 :(begin  # none, line 2:
        z = (top(box))(Int64,(top(add_int))(x::Int64,y::Int64))::Int64 # line 3:
        return (top(box))(Int64,(top(mul_int))(2,z::Int64))::Int64
    end::Int64))))

它有一个 Expr 。但是当我调用 code_typed 时,输出是:

code_typed(foo, (Int64,Int64))

1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = Base.add_int(x, y)::Int64
│   %2 = Base.mul_int(2, %1)::Int64
└──      return %2
) => Int64

它有一个CodeInfo.it与文档中的输出不同。

朱莉娅有零钱吗?以及如何根据我的函数和参数类型获取 Exprs?

该代码片段似乎取自 https://www.aosabook.org/en/500L/static-analysis.html,它于 2016 年发布(大约比 Julia 1.0 发布早两年)并引用了 2015 年的 Julia 0.3 版。

朱莉娅 1.0 给出

julia> code_typed(foo,(Int64,Int64))
1-element Array{Any,1}:
 CodeInfo(
2 1 ─ %1 = (Base.add_int)(x, y)::Int64                                   │╻ +
3 │   %2 = (Base.mul_int)(2, %1)::Int64                                  │╻ *
  └──      return %2                                                     │ 
) => Int64

而更多当前版本(例如 1.6 和 1.7)提供

julia> code_typed(foo,(Int64,Int64))
1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = Base.add_int(x, y)::Int64
│   %2 = Base.mul_int(2, %1)::Int64
└──      return %2
) => Int64

(一个更小的变化,但仍然)

如果出于任何原因,您希望此结果以 Expr 的数组或向量的形式出现,您似乎可以使用(例如)

获得此结果
julia> t = code_typed(foo,(Int64,Int64));

julia> t[1].first.code
3-element Vector{Any}:
 :(Base.add_int(_2, _3))
 :(Base.mul_int(2, %1))
 :(return %2)

虽然这可能被认为是一个实现细节,并且可能会在 Julia 的次要版本之间发生变化。