lua-users home
lua-l archive

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


On 16 February 2011 13:30, steve donovan <steve.j.donovan@gmail.com> wrote:
>> http://snippets.luacode.org/snippets/Lua_Preprocessor_118, though
>> providing a preprocess function that takes an environment argument.
>
> Yes, that's the way to go...  Still thinking about how to organize
> template 'instantiation' since a template function may depend on
> several other template functions.

I just do something like this:
local rlhat_templ = [[
$(include(check_mem_range, {addr=addr, memsize=memsize}))
$(include(check_mem_alignment, {addr=addr, mask=mask}))

local read_result = ffi_cast($(ffitype), cpu.memory + $(addr))[0]
]]
Where check_mem_* are template strings and include is my preprocessor
function, which takes an environment as its second argument. There's
not much need for explicitly creating a new environment - I could just
pass the current environment (accessible as ENV) to the sub-template.
I try to pass all helper functions in as part of the environment so
they can be reused easily across different templates. e.g. I
frequently have $(GETREG(rd)) which returns either 0 or the equivalent
of cpu.regs[$(rd)] depending on if rd == 0. My hope is this will allow
LuaJIT to optimise away instances of bit.bor($(imm), 0) and the like,
which occur pretty frequently. Though I haven't checked to see if it
does this yet.

Alex