dymokomi/luce-js
Source of release 0.6.1
0.6.1 / tools / engine_tables.py
#!/usr/bin/env python3
"""Generate the engine's atom and opcode tables from QuickJS's quickjs-atom.h and
quickjs-opcode.h.
usage: tools/engine_tables.py QUICKJS_SOURCE_DIR
Writes src/engine/atoms_table.lucb and src/engine/opcodes_table.lucb.
"""
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
ENGINE = ROOT / "src/engine"
RESERVED = set("""alloc and as asm break catch const continue defer elif else enum errdefer
export extern false for free from func goto if import in interface let local match mutating
new none not or pub recover return self static struct test true try type union var volatile
while with assert discard error trap hash print format sizeof alignof offsetof hex bin pad
bool char str unit never void fmt i8 i16 i32 i64 isize u8 u16 u32 u64 usize f16 f32 f64 Error ErrorCode""".split())
def box(name, summary, description):
return (
"#" + "=" * 94 + "\n#\n"
f"# {name} - {summary}\n#\n"
"# DESCRIPTION:\n"
+ "".join(f"# {line}\n" for line in description)
+ "#\n#" + "=" * 94 + "\n\n"
)
def case_name(name):
return name + "_" if name in RESERVED else name
def atoms(source):
entries = re.findall(r'^DEF\((\w+),\s*"((?:[^"\\]|\\.)*)"\)', source, re.M)
out = [box("atoms_table", "The predefined atoms",
["Generated by tools/engine_tables.py from quickjs-atom.h; do not edit.",
"Atom 0 is `no_atom` (JS_ATOM_NULL); the predefined atoms follow in",
"order and `atom_end` is the first dynamically allocated atom."])]
out.append("## JS_ATOM_NULL: no atom.\nlet no_atom: Atom = 0\n")
for index, (name, _) in enumerate(entries, start=1):
if name == "null":
# the atom of the text "null" (JS_ATOM_null); JS_ATOM_NULL is `no_atom`
out.append(f"## The atom of the keyword \"null\" (JS_ATOM_null); JS_ATOM_NULL is `no_atom`.\n")
out.append(f"let atom_null_keyword: Atom = {index}\n")
continue
out.append(f"let atom_{name}: Atom = {index}\n")
out.append(f"## The first atom that is not predefined.\nlet atom_end: Atom = {len(entries) + 1}\n")
out.append("## The last keyword atom (JS_ATOM_LAST_KEYWORD).\nlet atom_last_keyword: Atom = atom_super\n")
out.append("## The last strict-mode keyword atom (JS_ATOM_LAST_STRICT_KEYWORD).\n"
"let atom_last_strict_keyword: Atom = atom_yield\n\n")
text = "".join(bytes(s, "ascii").decode("unicode_escape") + "\0" for _, s in entries)
literal = "".join(ch if 32 <= ord(ch) < 127 and ch not in '"\\' else f"\\x{ord(ch):02x}" for ch in text)
out.append("## The predefined atoms' texts in order, each ended by a NUL (js_atom_init).\n")
out.append(f"let js_atom_init = b\"{literal}\"\n")
return "".join(out)
def opcodes(source):
formats = re.findall(r"^FMT\((\w+)\)", source, re.M)
real, temp = [], []
short = False
for line in source.splitlines():
if line.startswith("#if SHORT_OPCODES"):
short = True
m = re.match(r"^(DEF|def)\(\s*(\w+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\w+)\)", line)
if m:
entry = (m.group(2), int(m.group(3)), int(m.group(4)), int(m.group(5)), m.group(6))
(real if m.group(1) == "DEF" else temp).append(entry)
nop = [e[0] for e in real].index("nop")
temp_start = nop + 1
out = [box("opcodes_table", "The bytecode opcodes and their shapes",
["Generated by tools/engine_tables.py from quickjs-opcode.h; do not edit.",
"`Op` holds the opcodes the interpreter runs, the short forms included.",
"`TempOp` holds the compiler's temporary opcodes of phases 1 and 2: in C",
"their values overlap the short opcodes (OP_TEMP_START = OP_nop + 1), so",
"they live in their own enum and bytecode in those phases is read through",
"`is_temp_op`."])]
out.append("## The bytecode operand formats (OPCodeFormat).\nenum OpFormat as u8:\n")
for index, name in enumerate(formats):
out.append(f" {case_name(name)} = {index}\n")
out.append("\n## The bytecode opcodes (OPCodeEnum, without the temporary ones).\nenum Op as u8:\n")
for index, e in enumerate(real):
out.append(f" {case_name(e[0])} = {index}\n")
out.append(f"\n## The number of real opcodes (OP_COUNT).\nlet op_count: u32 = {len(real)}\n")
out.append(f"## The first temporary opcode's value (OP_TEMP_START).\nlet op_temp_start: u32 = {temp_start}\n")
out.append(f"## One past the last temporary opcode's value (OP_TEMP_END).\nlet op_temp_end: u32 = {temp_start + len(temp)}\n\n")
out.append("## The compiler's temporary opcodes; values overlap the short opcodes.\nenum TempOp as u8:\n")
for index, e in enumerate(temp):
out.append(f" {case_name(e[0])} = {temp_start + index}\n")
out.append("\n## Whether byte `op` of phase 1 or 2 bytecode is a temporary opcode.\n"
"func is_temp_op(op: u8) -> bool:\n"
" return (u32)op >= op_temp_start and (u32)op < op_temp_end\n\n")
out.append("## An opcode's encoded size in bytes, its stack effect and its operand format (JSOpCode).\n"
"struct OpCodeInfo:\n let size: u8\n let n_pop: u8\n let n_push: u8\n let fmt: OpFormat\n\n")
out.append("## Every real opcode's shape, then every temporary opcode's (opcode_info).\n")
count = len(real) + len(temp)
out.append(f"let opcode_info: OpCodeInfo[{count}] = [\n")
for e in real + temp:
out.append(f" OpCodeInfo(size = {e[1]}, n_pop = {e[2]}, n_push = {e[3]}, fmt = OpFormat.{case_name(e[4])}),\n")
out.append("]\n\n")
out.append("## The shape of opcode byte `op`, reading the temporary opcodes' values as temporary.\n"
"func op_info(op: u8, temporary: bool) -> const OpCodeInfo*:\n"
" if temporary and is_temp_op(op):\n"
" return &opcode_info[(usize)op - op_temp_start + op_count]\n"
" return &opcode_info[(usize)op]\n\n")
out.append("## Each real opcode's name, for bytecode dumps.\n"
f"let opcode_names: str[{len(real)}] = [\n")
for e in real:
out.append(f" \"{e[0]}\",\n")
out.append("]\n")
return "".join(out)
def main():
source = Path(sys.argv[1])
(ENGINE / "atoms_table.lucb").write_text(atoms((source / "quickjs-atom.h").read_text()))
(ENGINE / "opcodes_table.lucb").write_text(opcodes((source / "quickjs-opcode.h").read_text()))
main()