A No-Frills Introduction to Lua5.1VM Instructions
Lua5.1虚拟机指令简明手册
作者Kein-Hong Man,esq.<khman AT users.sf>
版本0.1,20060313
Contents目录
1Introduction序言2
2Lua Instruction Basics Lua指令基础3
3Really Simple Chunks十分简单的程序块5
4Lua Binary Chunks Lua二进制程序快7
5Instruction Notation指令记法15
6Loading Constants加载常量16
7Upvalues and Globals Upvalue和全局变量20
8Table Instructions表指令22
9Arithmetic and String Instructions算术和字符串指令23
10Jumps and Calls跳转和调用28
11Relational and Logic Instructions关系和逻辑指令35
12Loop Instructions循环指令42
13Table Creation表创建48
14Closures and Closing创建和结束闭包52
15Comparing Lua5.0.2and Lua5.1比较Lua5.0.2和Lua5.156
python虚拟机
16Digging Deeper深入探究57
17Acknowledgements致谢57
18ChangeLog&ToDos变更纪录&待做的57
“A No-Frills Introduction to Lua  5.1VM Instructions”is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike License  2.0.You are free to copy, distribute and display the work,and make derivative works as long as you give the original author credit,you do not use this work for commercial purposes,and if you alter,transform, or build upon this work,you distribute the resulting work only under a license identical to
-1-
this one.See the following URLs for more information:
“Lua5.1虚拟机指令简明手册”遵循Creative Commons Attribution-NonCommercial-ShareAlike许可2.0。只要你给出原作者署名,不把本作品用作商业目的,即可自由拷贝、分发和展示本作品,以及制作衍生品,并且如果你修改、改变或拓展本作品,你只能遵循该同样的许可发布衍生品。更多信息见下面的URL:
/licenses/by-nc-sa/2.0/
/licenses/by-nc-sa/2.0/legalcode
-2-
1Introduction序言
This is a no-frills introduction to the instruction set of the Lua5.1virtual machine.Compared to Perl or Python,the compactness of Lua makes it relatively easier for someone to peek under the hood and understand its internals.I think that one cannot completely grok a scripting language,or any complex system for that matter,without slitting the animal open and examining the entrails,organs and other yucky stuff that isn’t normally seen.So this document is supposed to help with the“peek under the hood”bit.
这是Lua5.1虚拟机指令集的简明介绍。与Perl或Python相比,Lua相当简洁,人们可以深入内部一探究竟。对于脚本语言或任何复杂系统,如不能了解其内部机制算不上真正掌握了。希望本文对该目的有些微帮助。
This introductory guide covers Lua5.1only.Please see the older document for the guide to Lua5.0.2virtual machine instructions.This is intentional;the internals of Lua is not fixed or standardized in any way,so users must not expect compatibility from one version of Lua to another as far as internals are concerned.
本文只涉及Lua5.1版,Lua5.0.2版虚拟机指令请看本指南的旧版。这么做是有意为之;Lua内部并非稳定不变或具有任何形式的标准化,所以千万不要认为Lua内部在版本之间是兼容的。
Output from ChunkSpy(URL:luaforge/projects/chunkspy/),a Lua5 binary chunk disassembler which I wrote while studying Lua internals,was used to generate the examples shown in this document.The brief disassembly mode of ChunkSpy is very similar to the output of the listing mode of luac,so you do not need to learn a new listing syntax.ChunkSpy can be downloaded from LuaForge(URL:luaforge/);it is licensed under the same type of MIT-style license as Lua5itself.
ChunkSpy(地址:luaforge/projects/chunkspy/)是我学习Lua内部时写的Lua5二进制块反汇编器,本文中的例子就是由它生成的。ChunkSpy的概要反汇编模式极似luac的清单模式的输出,不需你再学习新的清单语法。ChunkSpy可在LuaForge(地址:luaforge/)处下载,它遵循与Lua5一样的MIT式许可。
ChunkSpy has an interactive mode:you can enter a source chunk and get an immediate disassembly.This allows you to use this document as a tutorial by entering the examples into ChunkSpy and seeing the results yourself.The interactive mode is also very useful when you are exp
loring the behaviour of the Lua code generator on many short code snippets. ChunkSpy具有交互模式:输入源代码块立刻得到反汇编。这样可以把本文用作教程,把例子输入ChunkSpy查看结果。当你分析Lua代码生成器(汇编器)如何生成简短代码片段时,交互模式也非常有用。
This is a quick introduction,so it isn’t intended to be a comprehensive or expert treatment of the Lua virtual machine(from this point on,“Lua”refers to“Lua5”unless otherwise stated) or its instructions.It is intended to be a simple,easy-to-digest beginner’s guide to the Lua virtual machine instruction set–it won’t do cartwheels or blow smoke rings.
-3-
这只是个快速的入门,并不致力于全面或专业地论述Lua虚拟机(从现在起,除非另作说明,“Lua”指的是“Lua5”)或其指令,而是简单、易于消化的Lua虚拟机指令集的新手指南-不会有特技或吐烟圈等高难度动作。
The objective of this introduction is to cover all the Lua virtual machine instructions and the structure of Lua5binary chunks with a minimum of fuss.Then,if you want more detail,you can use luac or ChunkSpy to study non-trivial chunks of code,or you can dive into the Lua source code itself for the real thing.
本文的目标是尽量浅显地讲解全部Lua虚拟机指令以及Lua5二进制块的结构。要了解更多细节,可用luac或ChunkSpy学习不一般的代码块,或者也可以深入钻研Lua 源代码。
This is currently a draft,and I am not a Lua internals expert.So feedback is welcome.If you find any errors,or if you have anything to contribute please send me an e-mail(to khman AT users.sf or mkh AT )so that I can correct it.Thanks.
目前本文只是草稿,而且我也不是Lua内部的专家。所以欢迎反馈。如果你发现任何错误,或者要捐献任何东西,请给我发电邮(至khman AT users.sf或mkh AT )以便我可以修正它。谢谢。
-4-
2Lua Instruction Basics Lua指令基础
The Lua virtual machine instruction set we will look at is a particular implementation of the Lua language.It is by no means the only way to skin the chicken.The instruction set just happens to be the way the authors of Lua chose to implement version5of Lua.The following sections are based on the instruction set used in Lua5.1.The instruction set might change in the future–do not expect it to b
e set in stone.This is because the implementation details of virtual machines are not a concern to most users of scripting languages.For most applications, there is no need to specify how bytecode is generated or how the virtual machine runs,as long as the language works as advertised.So remember that there is no official specification of the Lua virtual machine instruction set,there is no need for one;the only official specification is of the Lua language.
我们要研究的Lua虚拟机指令集只是Lua语言的某个具体实现,但绝不是唯一方式。只是Lua的作者恰好选了这种方式来实现Lua5版。后面的章节基于Lua5.1的指令集。未来指令集可能会改变-不要期望它会一成不变。这是因为大多脚本语言用户不会关注虚拟机的实现细节。多数应用都不必指定字节码如何生成或虚拟机如何运行,只要语言如公示那样运行即可。因此要记住,并没有Lua虚拟机指令集的官方规范,也没必要;仅有的官方规范是Lua语言。
In the course of studying disassemblies of Lua binary chunks,you will notice that many generated instruction sequences aren’t as perfect as you would like them to be.This is perfectly normal from an engineering standpoint.The canonical Lua implementation is not meant to be an optimizing bytecode compiler or a JIT compiler.Instead it is supposed to load, parse and run Lua source code efficiently.It is the totality of the implementation that counts. If you really need the performance,you are supposed to drop down into native C functions anyway.
在学习Lua二进制块的反汇编过程中,你会发现生成的很多指令序列不如预想的那样理想。这从工程学的角度看很平常。权威的Lua实现并非意味着就是最优化的字节码编译器或即时(JIT)编译器。只假定它能有效地加载、解析并运行Lua源代码。这就是该实现考虑的全部。如果你确实需要高性能,只能使用原生的C函数。
Lua instructions have a fixed size,using a32bit unsigned integer data type by default.In binary chunks,endianness is significant,but while in memory,an instruction can be portably decoded or encoded in C using the usual integer shift and mask operations.The details can be found in lopcodes.h,while the Instruction type definition is defined in llimits.h. Lua的指令具有固定的尺寸,缺省使用一个32位的无符号整型数据类型。在二进制块中字节序(endianness)很重要,但在内存中时,可用寻常的整型移位和掩码操作轻易编解码指令。细节可看lopcodes.h,Instruction类型定义在llimits.h中。
There are three instruction types and38opcodes(numbered0through37)are currently in use as of Lua5.1.The instruction types are enumerated as iABC,iABx,iAsBx,and may be visually represented as follows:
当前Lua5.1使用3中指令类型和38个操作码(编号从0到37)。指令类型被枚举为iABC、iABx、iAsBx,可如下面直观地描绘:
-5-

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。