native function type

This commit is contained in:
2026-02-17 17:40:44 -06:00
parent b25285f2e1
commit ad419797b4
8 changed files with 603 additions and 265 deletions

View File

@@ -509,62 +509,21 @@ Build.build_static = function(packages, target, output, buildtype) {
// il_parts: {data: text, functions: [text, ...]}
// cc: C compiler path
// tmp_prefix: prefix for temp files (e.g. /tmp/cell_native_<hash>)
function compile_native_batched(il_parts, cc, tmp_prefix) {
var nfuncs = length(il_parts.functions)
var nbatch = 8
var o_paths = []
var s_paths = []
var asm_cmds = []
var batch_fns = null
var batch_il = null
var asm_text = null
var s_path = null
var o_path = null
var end = 0
var bi = 0
var fi = 0
var ai = 0
var rc = null
var parallel_cmd = null
function compile_native_single(il_parts, cc, tmp_prefix) {
var helpers_il = (il_parts.helpers && length(il_parts.helpers) > 0)
? text(il_parts.helpers, "\n") : ""
var prefix = null
if (nfuncs < nbatch) nbatch = nfuncs
if (nbatch < 1) nbatch = 1
// Generate .s files: run QBE on each batch
while (bi < nbatch) {
batch_fns = []
end = nfuncs * (bi + 1) / nbatch
while (fi < end) {
batch_fns[] = il_parts.functions[fi]
fi = fi + 1
}
// Batch 0 includes helper functions; others reference them as external symbols
prefix = (bi == 0 && helpers_il != "") ? helpers_il + "\n\n" : ""
batch_il = il_parts.data + "\n\n" + prefix + text(batch_fns, "\n")
asm_text = os.qbe(batch_il)
s_path = tmp_prefix + '_b' + text(bi) + '.s'
o_path = tmp_prefix + '_b' + text(bi) + '.o'
fd.slurpwrite(s_path, stone(blob(asm_text)))
s_paths[] = s_path
o_paths[] = o_path
bi = bi + 1
}
// Assemble all batches in parallel
while (ai < length(s_paths)) {
asm_cmds[] = cc + ' -c ' + s_paths[ai] + ' -o ' + o_paths[ai]
ai = ai + 1
}
parallel_cmd = text(asm_cmds, ' & ') + ' & wait'
rc = os.system(parallel_cmd)
var all_fns = text(il_parts.functions, "\n")
var full_il = il_parts.data + "\n\n" + helpers_il + "\n\n" + all_fns
var asm_text = os.qbe(full_il)
var s_path = tmp_prefix + '.s'
var o_path = tmp_prefix + '.o'
var rc = null
fd.slurpwrite(s_path, stone(blob(asm_text)))
rc = os.system(cc + ' -c ' + s_path + ' -o ' + o_path)
if (rc != 0) {
print('Parallel assembly failed'); disrupt
print('Assembly failed'); disrupt
}
return o_paths
return [o_path]
}
// Post-process QBE IL: insert dead labels after ret/jmp (QBE requirement)
@@ -651,7 +610,7 @@ Build.compile_native = function(src_path, target, buildtype, pkg) {
var tmp = '/tmp/cell_native_' + hash
var rt_o_path = '/tmp/cell_qbe_rt.o'
var o_paths = compile_native_batched(il_parts, cc, tmp)
var o_paths = compile_native_single(il_parts, cc, tmp)
// Compile QBE runtime stubs if needed
var rc = null
@@ -734,7 +693,7 @@ Build.compile_native_ir = function(optimized, src_path, opts) {
var tmp = '/tmp/cell_native_' + hash
var rt_o_path = '/tmp/cell_qbe_rt.o'
var o_paths = compile_native_batched(il_parts, cc, tmp)
var o_paths = compile_native_single(il_parts, cc, tmp)
// Compile QBE runtime stubs if needed
var rc = null