Juliaのジェネリック関数の抽象構文ツリーにアクセスするにはどうすればよいですか?
2230 次
3 に答える
5
要約すると、Simonはジェネリック関数に関連付けられた特定のメソッドのASTを探していたようです。LambdaStaticData
次のように、特定のメソッドのASTを含むオブジェクトを取得できます。
julia> f(x,y)=x+y
julia> f0 = methods(f, (Any, Any))[1]
((Any,Any),(),AST(:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end)))),())
julia> f0[3]
AST(:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end))))
julia> typeof(ans)
LambdaStaticData
どうやら、このASTはExprオブジェクトまたは圧縮されたASTオブジェクトのいずれかであり、バイトのシーケンスとして表されます。
julia> typeof(f0[3].ast)
Array{Uint8,1}
fromのshow()
メソッドは、次の場合にこれを解凍する方法を示しています。LambdaStaticData
base/show.jl
julia> ccall(:jl_uncompress_ast, Any, (Any, Any), f0[3], f0[3].ast)
:($(expr(:lambda, {x, y}, {{}, {{x, Any, 0}, {y, Any, 0}}, {}}, quote # none, line 1:
return +(x,y)
end)))
julia> typeof(ans)
Expr
于 2013-02-14T20:15:20.517 に答える
3
ジュリアには4つの関数とそれらの関数に類似した4つのマクロがあり、ジェネリック関数のメソッドについて多くのことを調べるために使用されます。
julia> f(x, y) = x + y
f (generic function with 1 method)
julia> methods(f)
# 1 method for generic function "f":
f(x,y) at none:1
下げられたコード:
julia> code_lowered(f, (Int, Int))
1-element Array{Any,1}:
:($(Expr(:lambda, {:x,:y}, {{},{{:x,:Any,0},{:y,:Any,0}},{}}, :(begin # none, line 1:
return x + y
end))))
julia> @code_lowered f(1, 1) # Both `Int`s
...same output.
入力されたコード:
julia> code_typed(f, (Int, Int))
1-element Array{Any,1}:
:($(Expr(:lambda, {:x,:y}, {{},{{:x,Int64,0},{:y,Int64,0}},{}}, :(begin # none, line 1:
return (top(box))(Int64,(top(add_int))(x::Int64,y::Int64))::Int64
end::Int64))))
julia> @code_lowered f(1, 1) # Both `Int`s
...same output.
LLVMコード:
julia> code_llvm(f, (Int, Int))
define i64 @julia_f_24771(i64, i64) {
top:
%2 = add i64 %1, %0, !dbg !1014
ret i64 %2, !dbg !1014
}
julia> @code_llvm f(1, 1) # Both `Int`s
...same output.
ネイティブコード:
julia> code_native(f, (Int, Int))
.text
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
add RDI, RSI
mov RAX, RDI
pop RBP
ret
julia> @code_llvm f(1, 1) # Both `Int`s
...same output.
タイプの不安定性の警告(v0.4 +):
julia> @code_warntype f(1, 1)
Variables:
x::Int64
y::Int64
Body:
begin # In[17], line 1:
return (top(box))(Int64,(top(add_int))(x::Int64,y::Int64))
end::Int64
于 2015-02-02T00:20:36.450 に答える
0
多重ディスパッチのため、ジェネリック関数に関連付けられたASTがあるかどうかはわかりません。関数定義を作成している場合はfbody
、を実行することでASTを取得できるはずですdump(quote(fbody))
。
于 2013-01-02T18:51:45.183 に答える