lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, Nov 1, 2009 at 6:57 PM, David Given <dg@cowlark.com> wrote:
> [...]
>>> (I have had thoughts about using tail calls to do the transfer of
>>> execution from one basic block to another, but I haven't come up with a
>>> decent way of doing it without doing memory allocations on entry to each
>>> function.)
>>
>> Dunno about these allocations. But tail calls are very fast with
>> LJ2. The compiler turns them into an inlined goto, i.e. a no-op.
>
> In which case it's probably worth having another look at. Having
> actually put a couple of minutes thought into it I realise that I don't
> think I do need memory allocations at all. Each bb state transition
> would involve passing a huge pile of arguments:

Are you sure you need any arguments? It seems like you could use
upvalues external to all your state functions, instead. For example,
doing a blind-rewrite your nsieve example from later in your post:

-- code begin
local fp, stack, sp
local H0, H1, H2, H3, H4, H5, H6, H7, H8, H9, H10
local state_0, state_1, state_3, state_4, state_5, state_6, state_7

fp, stack, H0 = ...

state_0 = function()
  sp = 0
  sp = fp + sp
  H7 = _malloc
  H1, H2 = H7(sp, stack, H0)
  H3 = H1
  H4 = H2
  H7 = 1
  H8 = _memset
  H9, H10 = H8(sp, stack, H3, H4, H7, H0)
  H6 = 0
  H5 = 2
  return state_1()
end

state_1 = function()
  H7 = H5 < H0 and 1 or 0
  if H7 ~= 0 then
    return state_3()
  else
    return state_7()
  end
end

state_2 = function()
  H7 = 1
  H8 = H5 + H7
  H5 = H8
  return state_1()
end

state_3 = function()
  H7 = H1 + H5
  H8 = H2[H7 + 0]
  if H8 ~= 0 then
    return state_4();  else  return state_2();  end
end

state_4 = function()
  H8 = 1
  H9 = H6 + H8
  H8 = 1
  H10 = shl(H5, H8)
  H6 = H9
  H7 = H10
  return state_5()
end

state_5 = function()
  H8 = H7 < H0 and 1 or 0
  if H8 ~= 0 then
    return state_6()
  else
    return state_2()
  end
end

state_6 = function()
  H8 = H1 + H7
  H9 = 0
  H2[H8 + 0] = H9
  H8 = H7 + H5
  H7 = H8
  return state_5()
end

state_7 = function()
  H1 = _free
  H1(sp, stack, H3, H4)
  H2 = static_1743_1_anon
  H1 = 1
  H3 = _printf
  H4 = H3(sp, stack, H1, H2, H0, H6)
end

return state_0()
-- code end

...as I say, I wrote this "blind", I have not tried actually running
it. But it seems like something like it should do what you want.

-Duncan