Merge branch 'mach' into bytecode_cleanup

This commit is contained in:
2026-02-12 07:50:09 -06:00
23 changed files with 1867 additions and 4331 deletions

View File

@@ -1,5 +1,5 @@
// Hidden vars come from env:
// CLI mode (cell_init): os, args, core_path, shop_path, use_mcode
// CLI mode (cell_init): os, args, core_path, shop_path, emit_qbe
// Actor spawn (script_startup): os, json, nota, wota, actorsym, init, core_path, shop_path
// args[0] = script name, args[1..] = user args
var load_internal = os.load_internal
@@ -24,7 +24,7 @@ function use_basic(path) {
return result
}
// Load a module from .mach bytecode, falling back to .ast.json
// Load a module from .mach bytecode (bootstrap modules have no source fallback)
function boot_load(name, env) {
var mach_path = core_path + '/' + name + ".mach"
var data = null
@@ -32,9 +32,8 @@ function boot_load(name, env) {
data = fd.slurp(mach_path)
return mach_load(data, env)
}
var ast_path = core_path + '/' + name + ".ast.json"
data = text(fd.slurp(ast_path))
return mach_eval_ast(name, data, env)
print("error: missing bootstrap bytecode: " + mach_path + "\n")
disrupt
}
var boot_env = {use: use_basic}
@@ -45,14 +44,11 @@ use_cache['tokenize'] = tokenize_mod
use_cache['parse'] = parse_mod
use_cache['fold'] = fold_mod
// Optionally load mcode compiler module
var mcode_mod = null
// Always load mcode compiler module
var mcode_mod = boot_load("mcode", boot_env)
use_cache['mcode'] = mcode_mod
var streamline_mod = null
var qbe_emit_mod = null
if (use_mcode) {
mcode_mod = boot_load("mcode", boot_env)
use_cache['mcode'] = mcode_mod
}
// Warn if any .cm source is newer than its .mach bytecode
function check_mach_stale() {
@@ -134,6 +130,8 @@ function load_module(name, env) {
var src_path = null
var src = null
var ast = null
var compiled = null
var optimized = null
if (fd.is_file(mach_path)) {
data = fd.slurp(mach_path)
return mach_load(data, env)
@@ -141,38 +139,33 @@ function load_module(name, env) {
src_path = core_path + '/' + name + ".cm"
src = text(fd.slurp(src_path))
ast = analyze(src, src_path)
return mach_eval_ast(name, json.encode(ast), env)
compiled = mcode_mod(ast)
optimized = streamline_mod(compiled)
return mach_eval_mcode(name, json.encode(optimized), env)
}
// Load optimization pipeline modules (needs analyze to be defined)
var qbe_macros = null
if (use_mcode) {
streamline_mod = load_module("streamline", boot_env)
use_cache['streamline'] = streamline_mod
if (emit_qbe) {
qbe_macros = load_module("qbe", boot_env)
qbe_emit_mod = load_module("qbe_emit", boot_env)
use_cache['qbe'] = qbe_macros
use_cache['qbe_emit'] = qbe_emit_mod
}
streamline_mod = load_module("streamline", boot_env)
use_cache['streamline'] = streamline_mod
if (emit_qbe) {
qbe_macros = load_module("qbe", boot_env)
qbe_emit_mod = load_module("qbe_emit", boot_env)
use_cache['qbe'] = qbe_macros
use_cache['qbe_emit'] = qbe_emit_mod
}
// Run AST through either mcode or mach pipeline
// Run AST through mcode pipeline → register VM
function run_ast(name, ast, env) {
var compiled = null
var optimized = null
var compiled = mcode_mod(ast)
var optimized = streamline_mod(compiled)
var qbe_il = null
if (use_mcode) {
compiled = mcode_mod(ast)
optimized = streamline_mod(compiled)
if (emit_qbe) {
qbe_il = qbe_emit_mod(optimized, qbe_macros)
print(qbe_il)
return null
}
return mcode_run(name, json.encode(optimized), env)
if (emit_qbe) {
qbe_il = qbe_emit_mod(optimized, qbe_macros)
print(qbe_il)
return null
}
return mach_eval_ast(name, json.encode(ast), env)
return mach_eval_mcode(name, json.encode(optimized), env)
}
// use() with ƿit pipeline for .cm modules