Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Lua designed to generate intermediate opcodes?

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.

like image 847
babel92 Avatar asked Jul 29 '13 17:07

babel92


2 Answers

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.

like image 169
lhf Avatar answered Sep 27 '22 21:09

lhf


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.

like image 35
Yu Hao Avatar answered Sep 27 '22 20:09

Yu Hao