lua-users home
lua-l archive

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


I had a go today porting a simple instruction set simulator written in
C to a pure Lua implementation, running under LuaJIT 2.0-beta6 and
using ffi for some of the data structures. I don't have code to post,
but the simulator it was ported from was almost identical in form to
https://github.com/atgreen/moxiedev/blob/master/src/sim/moxie/interp.c.
So at its core, it's just a loop which fetches each instruction,
decodes it and then does a switch on the opcode number. The
performance numbers I've managed so far aren't competitive with the C
implementation and I'm interested in suggestions for improving that.

For the Lua implementation, I have a table of functions so
optab[opcode_num] == opcode_implementation and use this to dispatch
each opcode. The basic structure is:

local rd, rs, rt, imm
local optab = {[NOP] = function() end, [ADDI] = function()
cpu.regs[rd] = cpu.regs[rs] + imm end, ....}
while true do
  opcode, rd, rs, rt, imm = decode_instruction(cpu.memory, cpu.pc)
  local opimpl = optab[opcode]
  if (opimpl) then opimpl() else warn_bad_opcode(opcode) end
  cpu.pc = cpu.pc + 4
end

In the above, cpu is an ffi-managed C struct.

Looking at luajit -jv and luajit -jdump, it's clear that the root
trace is always aborted. It is possible I'm doing something stupid in
my implementation, but intuitively it makes sense that a trace
compiler would struggle with a program with this structure. Any
suggestions on restructuring it for better optimisation by LuaJIT? I'd
imagine dynamic translation to generated Lua code would get some good
results, as it would allow the compiler to build traces which cover
sequences of code in the simulated program. However, that's not a
direction I'm going to take right now.

Also (to Mike), might it be worth adding a new FAQ entry to luajit.org
for a question like "My code doesn't run as fast as I think it should
in LuaJIT. Why not?". The answer could either link to a more detailed
explanation of how to investigate such issues and a list of common
problems, or at least give a pointer to -jv and -jdump and how to
interpret their output. I know they're mentioned under
LuaJIT->Running, but it's easy to miss.

Thanks,

Alex