compile optimization

This commit is contained in:
2026-02-10 16:37:11 -06:00
parent 3d71f4a363
commit d75ce916d7
18 changed files with 1528 additions and 113 deletions

View File

@@ -41,11 +41,17 @@ var boot_env = {use: use_basic}
var tokenize_mod = boot_load("tokenize", boot_env)
var parse_mod = boot_load("parse", boot_env)
var fold_mod = boot_load("fold", boot_env)
use_cache['tokenize'] = tokenize_mod
use_cache['parse'] = parse_mod
use_cache['fold'] = fold_mod
// Optionally load mcode compiler module
var mcode_mod = null
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
@@ -55,6 +61,9 @@ function check_mach_stale() {
["parse.cm", "parse.mach"],
["fold.cm", "fold.mach"],
["mcode.cm", "mcode.mach"],
["streamline.cm", "streamline.mach"],
["qbe.cm", "qbe.mach"],
["qbe_emit.cm", "qbe_emit.mach"],
["internal/bootstrap.cm", "internal/bootstrap.mach"],
["internal/engine.cm", "internal/engine.mach"]
]
@@ -118,26 +127,78 @@ function analyze(src, filename) {
return ast
}
// Load a module from .mach bytecode, falling back to source compilation
function load_module(name, env) {
var mach_path = core_path + '/' + name + ".mach"
var data = null
var src_path = null
var src = null
var ast = null
if (fd.is_file(mach_path)) {
data = fd.slurp(mach_path)
return mach_load(data, 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)
}
// 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
}
}
// Run AST through either mcode or mach pipeline
function run_ast(name, ast, env) {
var compiled = null
var optimized = null
var qbe_il = null
if (use_mcode) {
compiled = mcode_mod(ast)
return mcode_run(name, json.encode(compiled), env)
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)
}
return mach_eval_ast(name, json.encode(ast), env)
}
// use() with ƿit pipeline for .cm modules
function use_fn(path) {
var file_path = path + '.cm'
var file_path = null
var mach_path = null
var data = null
var script = null
var ast = null
var result = null
if (use_cache[path])
return use_cache[path]
// Check CWD first, then core_path
// Try .mach bytecode first (CWD then core_path)
mach_path = path + '.mach'
if (!fd.is_file(mach_path))
mach_path = core_path + '/' + path + '.mach'
if (fd.is_file(mach_path)) {
data = fd.slurp(mach_path)
result = mach_load(data, {use: use_fn})
use_cache[path] = result
return result
}
// Try .cm source (CWD then core_path)
file_path = path + '.cm'
if (!fd.is_file(file_path))
file_path = core_path + '/' + path + '.cm'