Files
ziran/web/bundle.c
T
waoziandClaude Opus 5.5 9ad5f481ef Name the tools by what they turn into what, and run verified ZX natively
The tools are renamed after their input and output: zi2zx lowers source
or ZIR to executable ZX, zx2native turns ZX into a native ELF, zx2wasm
into a wasm32 module, and zi2c, zi2go and zi2zir keep their roles under
cmd/ folders of the same names. zi2asm and the cmd/zir-* folders are
gone, and tests/tool_names.sh keeps the old names out. zx2native keeps
the direct encoder as its default; the machine pipeline replaces it once
it reaches parity.

The embedding API can run a verified bundle as x86-64 machine code in
memory. Code is written to RW pages, relocated, then switched to RX,
never RWX, and each instance gets its own data
(cmd/zir/zir_native_memory.c, zir_host_native.c). Backend selection is
BundleBackendPolicy: Engine and Native are strict, and Auto chooses by
platform only, never by whether one program fits. A native gap is a
named error, never a switch to the engine. Desktop x86-64 Auto stays on
the engine until the native backend is complete.

cmd/compiler_machine_image.zi adds the target-neutral machine image
contract. The machine verifiers reject unknown relocation kinds,
out-of-range symbols and addends, non-host external calls, native-width
mismatches and missing stack or fuel facts. The machine pipeline lowers
and encodes more of LIR. Docs, site pages, Kryon's build references and
the benchmarks use the new names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-14 22:07:42 -03:00

93 lines
3.3 KiB
C

/* Browser clients supply ordinary source files and module search roots in
* Emscripten's filesystem. This is the zi2zx pipeline, without UI
* policy: the program becomes revision-28 executable ZX. */
#include "zir.h"
#include "zir_check.h"
#include "zir_diagnostic.h"
#include "zir_load.h"
#include "zir_serial.h"
#include "zx_exec_run.h"
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
paths(char *text, const char ***items)
{
size_t count = 0;
for(const char *at = text; *at != '\0'; at++)
if(*at != '\n' && (at == text || at[-1] == '\n')) count++;
if(count > INT_MAX || count > SIZE_MAX / sizeof(**items)) return -1;
if(count == 0) return 0;
*items = malloc(count * sizeof(**items));
if(*items == NULL) return -1;
int index = 0;
char *save = NULL;
for(char *line = strtok_r(text, "\n", &save); line != NULL;
line = strtok_r(NULL, "\n", &save))
(*items)[index++] = line;
return index;
}
/* Search roots and inputs are newline-separated absolute paths. Named roots
* such as "package=/packages/package" use the normal module loader. */
int
BuildBundle(const char *root, const char *search, const char *inputs,
const char *entry_module, const char *entry_function,
const char *output)
{
ProgramSet set = {0};
ZirProgram merged = {0};
unsigned char *bytes = NULL;
size_t size = 0;
const char **module_paths = NULL, **input_paths = NULL;
char *search_copy = NULL, *input_copy = NULL;
FILE *file = NULL;
int status = 1;
if(root == NULL || search == NULL || inputs == NULL || output == NULL ||
entry_module == NULL || entry_function == NULL) return 1;
remove(output);
search_copy = strdup(search);
input_copy = strdup(inputs);
if(search_copy == NULL || input_copy == NULL) goto done;
int module_count = paths(search_copy, &module_paths);
int input_count = paths(input_copy, &input_paths);
if(module_count < 0 || input_count <= 0) goto done;
SetDiagnosticFormat("text");
if(!ProgramsLoad(&set, root, module_paths, module_count,
input_paths, input_count) ||
!CheckCanonicalPrograms(set.programs, set.count,
(const char *const *)set.paths)) goto done;
for(int p = 0; p < set.count; p++)
merged.module_count += set.programs[p]->module_count;
merged.context = set.context;
merged.modules = calloc((size_t)merged.module_count, sizeof(*merged.modules));
if(merged.modules == NULL) goto done;
int at = 0;
for(int p = 0; p < set.count; p++)
for(int m = 0; m < set.programs[p]->module_count; m++)
merged.modules[at++] = set.programs[p]->modules[m];
ZirProgram *program = &merged;
if(!LinkImports(&program, 1)) goto done;
bytes = ZxExecBuild(&merged, entry_module, entry_function, (Slice){0}, &size);
if(bytes == NULL) goto done;
file = fopen(output, "wb");
if(file == NULL || fwrite(bytes, 1, size, file) != size) goto done;
status = 0;
done:
if(file != NULL && fclose(file) != 0) status = 1;
if(status != 0) remove(output);
free(bytes);
free(merged.modules);
ProgramsFree(&set);
free(module_paths);
free(input_paths);
free(search_copy);
free(input_copy);
fflush(NULL);
return status;
}