A No-Frills Introduction to Lua 5.1 VM Instructions

Sommaire: A No-Frills Introduction to Lua 5.1 VM Instructions

Introduction
Lua Instruction Basics
Really Simple Chunks
Lua Binary Chunks
nstruction Notation
Loading Constants
Upvalues and Globals
Table Instructions
Arithmetic and String Instructions
Jumps and Calls
Relational and Logic Instructions
Loop Instructions
Table Creation
Closures and Closing
Comparing Lua 5.0.2 and Lua 5.1
Digging Deeper
Acknowledgements
ChangeLog & ToDos

Extrait du cours a No-Frills Introduction to Lua 5.1 VM Instructions

1 Introduction
This is a no-frills introduction to the instruction set of the Lua 5.1 virtual 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.
This introductory guide covers Lua 5.1 only. Please see the older document for the guide to Lua 5.0.2 virtual 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.
2 Lua Instruction Basics
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 version 5 of Lua. The following sections are based on the instruction set used in Lua 5.1. The instruction set might change in the future – do not expect it to be 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.
4 Lua Binary Chunks
Lua can dump functions as binary chunks, which can then be written to a file, loaded and run.
Binary chunks behave exactly like the source code from which they were compiled.
A binary chunk consist of two parts: a header block and a top-level function. The header portion contains 12 elements:
Header block of a Lua 5 binary chunk
Default values shown are for a 32-bit little-endian platform with IEEE 754 doubles as the number format. The header size is always 12 bytes.
4 bytes Header signature: ESC, “Lua” or 0x1B4C7561
• Binary chunk is recognized by checking for this signature
1 byte Version number, 0x51 (81 decimal) for Lua 5.1
• High hex digit is major version number
• Low hex digit is minor version number
1 byte Format version, 0=official version
1 byte Endianness flag (default 1)
• 0=big endian, 1=little endian
1 byte Size of int (in bytes) (default 4)
1 byte Size of size_t (in bytes) (default 4)
1 byte Size of Instruction (in bytes) (default 4)
1 byte Size of lua_Number (in bytes) (default 8)
1 byte Integral flag (default 0)
• 0=floating-point, 1=integral number type
On an x86 platform, the default header bytes will be (in hex):
1B4C7561 51000104 04040800
5 Instruction Notation
Before looking at some Lua virtual machine instructions, here is a little something about the notation used for describing instructions. Instruction descriptions are given as comments in the Lua source file lopcodes.h. The instruction descriptions are reproduced in the following chapters, with additional explanatory notes. Here are some basic symbols:
R(A) Register A (specified in instruction field A)
R(B) Register B (specified in instruction field B)
R(C) Register C (specified in instruction field C)
PC Program Counter
Kst(n) Element n in the constant list
Upvalue[n] Name of upvalue with index n
Gbl[sym] Global variable indexed by symbol sym
RK(B) Register B or a constant index
RK(C) Register C or a constant index
sBx Signed displacement (in field sBx) for all kinds of jumps
The notation used to describe instructions is a little like pseudo-C. The operators used in the notation are largely C operators, while conditional statements use C-style evaluation.
Booleans are evaluated C-style. Thus, the notation is a loose translation of the actual C code that implements an instruction.

……….

Si le lien ne fonctionne pas correctement, veuillez nous contacter (mentionner le lien dans votre message)
A No-Frills Introduction to Lua 5.1 VM Instructions (332 KO) (Cours PDF)
Introduction to Lua

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *