Re: Understanding SNAP

  • From: Peter Cawley <corsix@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 4 Oct 2017 22:46:14 +0100

On Mon, Oct 2, 2017 at 12:45 PM, Raj <rajlistuser@xxxxxxxxx> wrote:

I guess the num type is floating point.

Yes, "num" is short for "Lua number", i.e. IEEE754 double-precision
floating point.

The first step would be to identify the local variables which are
getting passed from the parent trace to sub trace.

Everything you need from the IR is given by SNAP 0 in the child trace:

....        SNAP   #0   [ ---- ---- ---- 0001 0002 ---- 0001 0003 app.lua:121|
 0003 0004 nil  nil  true app.lua:199|
 0003 0005 0006 0007 0008 app.lua:220|
 0003 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017
app.lua:330|
 0003 ---- 0009 0011 ---- 0013 ---- ctable.lua:307|
 0011 0018 0013 "required" 0019 0020 0021 0022 0022 ]

The five active locals of the function starting at app.lua:121 are
0003 0004 nil  nil  true.
The five active locals of the function starting at app.lua:199 are
0003 0005 0006 0007 0008.
The 14 active locals of the function starting at app.lua:220 are 0003
0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017.
The seven active locals of the function starting at app.lua:330 are
0003 ---- 0009 0011 ---- 0013 ----.
The first nine active locals of the function starting at
ctable.lua:307 are 0011 0018 0013 "required" 0019 0020 0021 0022 0022.

You can ignore anything which is ---- / nil / true / "required", as
these things aren't inherited. Everything else is inherited. Some IR
instructions appear more than once, meaning that more than one local
variable needs addressing in order to prevent inheritance. If you look
at where app.lua:199 calls app.lua:220, you should be able to identify
five active locals in app.lua:199. The lexicographically first local
corresponds to 0003, the lexicographically last local corresponds to
0008 (not that this correspondence is overly useful to answering your
question, but it might be nice to know). For a more complex case, look
at where app.lua:330 calls ctable.lua:307, you should be able to
identify seven active locals in app.lua:330: the first is 0003, the
second isn't inherited, the third and fourth are 0009 and 0011, the
fifth isn't inherited, the sixth is 0013, and the seventh isn't
inherited.

Assuming my attempt so far is correct, from this IR how can I get to
the variable in lua source?

As mentioned above, the only IR you need is SNAP 0. Once you have
that, you need to go to your Lua source, identify which locals are
live, consider them in lexicographical order of definition, and line
them up 1:1 with SNAP entries - noting that function arguments count
as locals for this purpose (and obviously are lexicographically before
everything declared using the "local" keyword), for-loops count as an
extra three, etc.

Other related posts: