3

I am studying Julia static analysis, and I have the following function:

function f(x,y,z)
  d=x+y
  d=d*2*z
  end

i use code_typed to analyze it.

julia> y=code_typed(f)
1-element Vector{Any}:
 CodeInfo(
1 ─ %1 = (x + y)::Any
│   %2 = (%1 * 2)::Any
│   %3 = (%2 * z)::Any
└──      return %3
) => Any

i can get slots and slot types of it.

julia> y[1].first.slotnames
5-element Vector{Symbol}:
 Symbol("#self#")
 :x
 :y
 :z
 :d

julia> y[1].first.slottypes
5-element Vector{Any}:
 Core.Const(f)
 Any
 Any
 Any
 Any

but do i have any way to know which is argument and which is local variables among the slots?

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
moreHope
  • 85
  • 5

1 Answers1

3

You can use Base.argnames to find out arguments of your function:

julia> Base.method_argnames.(methods(f))
1-element Vector{Vector{Symbol}}:
 [Symbol("#self#"), :x, :y, :z]

You can extract this from the CodeInfo object as well:

julia> Base.method_argnames(y[1].first.parent.def)
4-element Vector{Symbol}:
 Symbol("#self#")
 :x
 :y
 :z
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62