module Assembler: sig .. end
Assembler for a subset of the Lua instructions. The Lua VM references all constants by a numeric index in a constant list that is defined on a per function basis. Instructions that reference a constant therefore do so by numeric index. This module automatically hoists out all constants that appear in an instruciton and replaces it with the corresponding index into the constant list.
It may be possible to use this module without reading the "No Frills" guide, and the core meaning and documentation of each instruction is recreated in the documentation of this module, but the "No Frills" guide is the definitive source, and in cases of doubt should be referenced. In particular, the treatment of global variables is absent in this documentation, and the explanations of SetGlobal and GetGlobal should be referenced to understand how they work.
Lua Primitives
type lua_const = [ `L_Bool of bool | `L_Double of float | `L_Nill | `L_String of string ]
Primitive values supported by the Lua VM
type rk_source = [ `L_Bool of bool
| `L_Double of float
| `L_Nill
| `L_String of string
| `Register of int ]
Some instructions allow for direct references to constants to appear in the source operands along with registers.
In the "No Frills" guide, an instruction uses an rk_source when when the operands in the RTL representation of the instructions use RK(A) or RK(B)
Lua Instructions
Arithmetic Operations
type arith_op_types =
| |
Arith_Add |
| |
Arith_Sub |
| |
Arith_Mul |
| |
Arith_Div |
The type of arithmetic operation for an arithmetic operation
type arith_op = {
}
A common representation of an arithmetic expression.
Comparisons
type comparison_type =
| |
Comp_Eq |
| |
Comp_Lt |
| |
Comp_Le |
The comparisons possible in a Comparison instruction. The names are self-explanatory
type comparison_op = {
}
A common representation of comparison operations. See page 35 for details on how Lua encodes conditionals.
Supported Instructions
type return_val =
| |
No_Value |
| |
Return_One of int |
| |
Return_Many of int * int |
An abstraction away from the complexities of returning values from a function (variable number of return values is not supported).
type lua_ops =
| |
Load_Const of int * lua_const |
| |
Get_Global of int * string |
| |
Set_Global of string * int |
| |
Concat of int * int * int |
| |
Jump of int |
| |
Cmp of comparison_op |
| |
Move of int * int |
| |
Arith of arith_op |
| |
Call of int * int * int |
| |
Closure of int * int |
| |
Load_Table of int * int * rk_source |
| |
Set_Table of int * rk_source * rk_source |
| |
Set_List of int * int * int |
| |
Make_Table of int * int * int |
| |
Return of return_val |
Representations of the subset of instructions supported by the assembler. The names used here are almost exactly the same as those used in the "No Frills" guide.
API
type function_unit = {
}
A representation of a function unit (or function block as it is called in the "No Frills" guide). Varargs functions are not supported
val assemble : function_unit -> Pervasives.out_channel -> unit
assemble f c assembles the function f into bytecode and writes it to c. c must be a writable channel that was opened in binary mode.