简体   繁体   English

如何根据函数和参数类型获得 julia 代码的表达式?

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

i am new in Julia.我是朱莉娅的新人。 i read a doc about julia static-analysis.我阅读了关于 julia 静态分析的文档。 it gives a function.它提供了一个功能。

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

and use the julia introspection function code_typed get output:并使用 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))))

it has an Expr .它有一个 Expr 。 but when i call code_typed, the output is :但是当我调用 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

it has a CodeInfo.it is different with the output in doc.它有一个 CodeInfo。它与 doc 中的输出不同。

does julia have some change?朱莉娅有什么变化吗? and how can i get the Exprs according to my function and argtypes?以及如何根据我的函数和 argtypes 获得 Exprs?

That code snippet appears to be taken from https://www.aosabook.org/en/500L/static-analysis.html , which was published in 2016 (about two years before the release of Julia 1.0) and references Julia version 0.3 from 2015.该代码片段似乎取自https://www.aosabook.org/en/500L/static-analysis.html ,该代码于 2016 年发布(大约在 Julia 1.0 发布前两年),并引用了 Julia 0.3 版2015 年。

Julia 1.0 gives Julia 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

while more current versions such as 1.6 and 1.7 give而更当前的版本,如 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

(a much more minor change, but nonetheless) (一个更小的变化,但仍然如此)

If for any reason, you want this result in the form of an array or vector of Expr s, you seem to be able to get this using (eg)如果出于任何原因,您希望以数组或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)

though this is likely considered an implementation detail and liable to change between minor versions of Julia.尽管这可能被认为是一个实现细节,并且可能会在 Julia 的次要版本之间发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM