Question

I was reading Lua's source code and surprisingly found that it was implemented to generate some odd instructions (e.g. GETUPVAL, FORLOOP, etc.) Then those opcodes are executed by a virtual machine. Is there any benefit to do this (compared with pure AST-based interpreter?)

Thanks.

Was it helpful?

Solution

Using virtual machines is an old implementation technique for languages, dating as far back to at least UCSD Pascal, BCPL, and Smalltalk. Most scripting languages now use a virtual machine. Some use a general-purpose virtual machine. Lua uses a special purpose virtual machine that reflects the semantics of the language. Hence the specialized opcodes. Lua did introduce novelty in this area by moving in version 5.0 to a register-based virtual machine, instead of a traditional stack-based one.

OTHER TIPS

For general comparison of bytecode interpreters and abstract syntax tree interpreters, see Wikipedia:Interpreter.

Lua has been using a virtual machine since its birth, and introduced a register-based virtual machine in Lua 5.0 (which is the first register-based virtual machine to have a wide use.) The Evolution of Lua, written by the authors of Lua, provides lots of resources on that topic.

Another paper written by the authors of Lua, The Implementation of Lua 5.0 provides lots of details on the implementation, Chapter 7 The Virtual Machine would be especially helpful to you, this paper is also a great reference for reading Lua's source code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top