The PyLua Compiler Pipeline
A deep architectural tour of how PyLua transforms raw UTF-8 source code into highly optimized bytecode prototypes through its single-pass compiler in src/parser/.
1. Single-Pass Compilation Architecture
Unlike heavyweight compilers that construct large Abstract Syntax Tree (AST) node graphs in memory, PyLua employs a single-pass recursive descent compiler. Code is emitted directly to bytecode instruction arrays during parsing, allowing PyLua to compile millions of lines of code per second while consuming virtually zero compilation heap memory.
┌────────────────────────┐
│ Source File (.lua) │
└───────────┬────────────┘
│ Byte Stream (ZIO buffer)
▼
┌────────────────────────┐
│ llex.c │ ──▶ Tokenizes keywords, literals, braces {}, and operators
│ Lexical Scanner │ Interns strings into global string cache (global_State.strt)
└───────────┬────────────┘
│ Token Stream (Token: TK_IF, TK_IMPORT, TK_NAME, etc.)
▼
┌────────────────────────┐
│ lparser_*.c │ ──▶ Recursive descent parser:
│ Syntax & Scoping │ • Manages FuncState and BlockCnt scope chains
└───────────┬────────────┘ • Performs Auto-Local register allocation
│ Expression Descriptors (expdesc)
▼
┌────────────────────────┐
│ lcode.c │ ──▶ Code Generator:
│ Code Generator │ • Constant folding (e.g., 2 + 2 ──▶ 4 at compile time)
└───────────┬────────────┘ • Emits 32-bit VM opcodes (OP_MOVE, OP_CALL, etc.)
│
▼
┌────────────────────────┐
│ Proto Object │ ──▶ Ready for execution in lvm.c or dumping via ldump.c
│ (Bytecode Prototype) │
└────────────────────────┘
2. Key Compiler Data Structures
1. Lexical State (`LexState`)
Defined in src/parser/llex.h, represents the active parser state during file compilation. Contains current token (ls->t), lookahead token (ls->lookahead), file name, and pointer to active function state.
2. Function Compilation State (`FuncState`)
Defined in src/parser/lparser.h. A new FuncState is pushed for every function and nested closure being compiled. It manages:
fs->f: The targetProtostructure being generated.fs->pc: Current program counter (number of emitted instructions).fs->freereg: Next available virtual register index in the stack frame.fs->nactvar: Number of active local variables in the current function.fs->bl: Pointer to innermost active block control structure (BlockCnt).fs->jpc: Pending jump list to be patched to the next emitted instruction.
3. Expression Descriptors (`expdesc`)
During parsing, expressions are not evaluated immediately. Instead, they are represented as an expdesc tagged union:
Tag (expkind) |
Meaning | When Generated |
|---|---|---|
VCONST |
Constant in constant table | Literal string, float, or integer |
VLOCAL |
Variable in stack register | Local variable or auto-local variable |
VUPVAL |
Upvalue of enclosing function | Variable captured across function scopes |
VINDEXED |
Table field lookup t[k] |
Table index expression |
VJMP |
Relational jump condition | Comparison expressions (e.g., x > 10) |
VRELOC |
Instruction that needs a destination | Arithmetic or call result |
3. The Prototype (`Proto`) Object
The output of compilation is a Proto (defined in include/lobject.h). It is an immutable template from which runtime closures (LClosure) are instantiated:
struct Proto {
Instruction *code; /* Array of 32-bit bytecode instructions */
TValue *k; /* Array of constants used by the function */
Proto **p; /* Array of nested function prototypes */
Upvaldesc *upvalues; /* List of upvalue descriptors */
lu_byte numparams; /* Number of fixed parameters */
lu_byte is_vararg; /* 1 if variadic (...) */
lu_byte maxstacksize; /* Maximum registers required for this function frame */
/* Debugging information: lineinfo, locvars, source filename */
};