confused about down-recursion in LuaJIT

40 views
Skip to first unread message

tweyseo

unread,
Apr 29, 2019, 11:44:13 PM4/29/19
to openresty-en
I found the case for down-recursion:
require("jit.dump").on("six", "t1.dlog")--require("jit.v").on("t1.log")--
---
local function f1(v)
   
if v == 0 then return 0 end
    f1
(v - 1)
end

for i = 1, 40 do
    f1
(i)
end

the v.log:
[TRACE   1 t1.lua:3 up-recursion]
[TRACE --- (1/3) t1.lua:4 -- down-recursion, restarting at t1.lua:6]
[TRACE   2 t1.lua:6 down-recursion]

and the dump.log:
---- TRACE 1 start t1.lua:3
---- TRACE 1 IR
....        SNAP   #0   [ ---- ---- ]
0001 >  num SLOAD  #1    T
....        SNAP   #1   [ ---- ---- ]
0002 >  num NE     0001  +0
....        SNAP   #2   [ ---- ---- ]
0003    fun SLOAD  #0    R
0004 >  fun EQ     0003  t1.lua:3
0005    num SUB    0001  +1
....        SNAP   #3   [ t1.lua:3|---- t1.lua:3|---- ]
0006 >  num NE     0005  +0
0007    num SUB    0005  +1
....        SNAP   #4   [ t1.lua:3|---- t1.lua:3|0005 t1.lua:3|---- ]
0008 >  num NE     0007  +0
0009    num SUB    0007  +1
....        SNAP   #5   [ t1.lua:3|---- t1.lua:3|0005 t1.lua:3|0007
t1
.lua:3|0009 ]
---- TRACE 1 stop -> up-recursion

---- TRACE 1 exit 3
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 1 exit 4
---- TRACE 1 exit 1
---- TRACE 1 exit 3
---- TRACE 2 start 1/3 t1.lua:4
---- TRACE 2 abort t1.lua:6 -- down-recursion, restarting

---- TRACE 2 start t1.lua:6
---- TRACE 2 IR
....        SNAP   #0   [ ---- ]
....        SNAP   #1   [ ---- ]
0002 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #2   [ ---- ---- ]
0003 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #3   [ ---- ---- ]
0004 >  p32 RETF   proto: 0x109482c8  [0x10948328]
....        SNAP   #4   [ ---- ]
---- TRACE 2 stop -> down-recursion

---- TRACE 2 exit 3

But i was confused with this behavior, someone who can explain it for me? And 
another confusion was why there was 3 different guards for "if v == 0" ?

tweyseo

unread,
May 5, 2019, 9:57:59 PM5/5/19
to openresty-en
Can someone give me some ideas?
Reply all
Reply to author
Forward
0 new messages