Search code examples
juliaslot

How can I distinguish arguments and local variables in slot of Julia codeinfo?


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?


Solution

  • 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