Write a closed C program's entry as C's main itself
A program linked with --entry and built with --exe, or with its own exported main, no longer gets a separate ziran_exe_main.c that calls <module>_main. zi2c writes the entry's body as int main(void), or as int main(int argc, char **argv) when the body uses its parameters: an s32 count is main's own parameter, other types the body reads are copied from main's, and an entry with no result returns 0. hello's C is now one int main in hello.c with no header. The entry keeps its own name and the wrapper when other code calls it or takes it as a value (ProgramUsesFunction in zir_link.c), when it is exported under another symbol, when it carries assembly or #target clones, in library builds and with --no-main, which now does what it says. tests/native_entry_cases.zi checks hello's C against a fixture and builds every native entry fixture as a closed program. Also regenerates site/guide/zx.html, which docs/ZX.md had left stale.
This commit is contained in:
1 parent
436d73cf3a
commit
f00a8d5b49
13 files changed
+308
-46
No files matched your search
@@ -12,6 +12,7 @@
|
||||
- Native x86-64 code reaches a global or a frame allocation directly when one shared LIR pass proves the access stays inside a live root, instead of resolving its checked pointer; the machine verifier now checks every load, store, zero and byte comparison and re-derives each direct access, and Wasm takes its index proofs from the same pass. Parameters, loaded pointers and heap blocks keep their resolves, so stale aliases, resized blocks and out-of-range runtime indices still trap. A LIR `zero` is now resolved like a store instead of writing through the raw token.
|
||||
- A closed C program, linked with `--entry` and built with `--exe` or its own `main`, carries only the code it uses: each module header declares just what another generated file names, the module's `.c` keeps what only it reads, header inline copies stay only for procedures other modules call, and a module whose header would declare nothing has none. For Kryon's examples, headers shrink from 18 to 8.5 KB (window), 288 to 91 KB (hello), 319 to 102 KB (box) and 692 to 420 KB (image).
|
||||
- A closed C program's runtime headers, such as `zir_bounds.h`, `zir_slice.h`, `zir_string.h` and `zir_vec.h`, keep only the helpers its generated files call; Kryon's window headers shrink to 6.6 KB.
|
||||
- A closed C program's entry is C's `main` itself: `hello` generates one `int main(void)` in `hello.c`, with no `ziran_exe_main.c` wrapper and no header. An entry that other code calls or takes as a value, that is exported under another symbol or that carries assembly keeps its own name and the wrapper, as do library builds and `--no-main`.
|
||||
- Input trust tiers keep memory safety invariant while naming validation failures: bounded environment/argv, binary, file, HTTP and host-byte readers return typed outcomes; local native-cache framing reports why edited entries were refused and recompiles them; the laws inventory stable artifact and host-boundary diagnostics.
|
||||
- `#insert NAME;` at file scope splices a compile-time string constant, such as a `#string` body or `#run` output, into the module as checked source; every target sees ordinary declarations, the same from source and saved IR.
|
||||
- Modules take compile-time parameters: `#module_parameters(NAME := value, NAME: Type = value)` as a module's first declaration, and `#import "module"(NAME = value)` chooses the values, so `#if` keeps unchosen features out of C, Go, ZX, Wasm and native code. One program binds a module's parameters once; an unknown name, a value that does not fit and imports that disagree fail as `module.parameter_unknown`, `module.parameter_type` and `module.parameter_conflict`. Saved `.zir` is version 65 and keeps the bindings.
|
||||
|
||||
+44
-17
@@ -54,14 +54,38 @@ lock_output(const char *directory)
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* The C main that runs the entry procedure, unless the entry is itself an
|
||||
* exported main. The entry takes nothing or (argc, argv) and returns
|
||||
* nothing or an integer. */
|
||||
/* The procedure a closed program writes as C's main itself: its --exe
|
||||
* entry, or else its exported main. It keeps a name of its own, and a
|
||||
* separate C main calls it, when other code calls it or takes it as a
|
||||
* value, when it is exported under another symbol, or when it carries
|
||||
* assembly or per-CPU clones. */
|
||||
static const ZirFunction *
|
||||
in_place_main(const ZirProgram *program, const ZirModule *entry_owner,
|
||||
const ZirFunction *entry_fn, int exe)
|
||||
{
|
||||
const ZirModule *owner = exe ? entry_owner : NULL;
|
||||
const ZirFunction *fn = exe ? entry_fn : NULL;
|
||||
for(int m = 0; fn == NULL && m < program->module_count; m++)
|
||||
for(int f = 0; fn == NULL && f < program->modules[m].function_count; f++)
|
||||
if(!program->modules[m].functions[f].is_template &&
|
||||
NativeProgramMain(&program->modules[m], &program->modules[m].functions[f])) {
|
||||
owner = &program->modules[m];
|
||||
fn = &program->modules[m].functions[f];
|
||||
}
|
||||
if(fn == NULL || (fn->exported && !NativeProgramMain(owner, fn)) ||
|
||||
(fn->assembly_text && *fn->assembly_text) || fn->simd_target ||
|
||||
ProgramUsesFunction(program, fn))
|
||||
return NULL;
|
||||
return fn;
|
||||
}
|
||||
|
||||
/* The C main that runs the entry procedure, when the entry is neither an
|
||||
* exported main nor written as C's main itself. The entry takes nothing or
|
||||
* (argc, argv) and returns nothing or an integer. */
|
||||
static int
|
||||
write_entry_main(const ZirModule *module, const ZirFunction *fn,
|
||||
const char *out_dir, const char *path)
|
||||
{
|
||||
if(!CheckNativeEntry(fn, "c")) return 0;
|
||||
if(NativeProgramMain(module, fn))
|
||||
return 1;
|
||||
char stem[ZIR_PATH_MAX];
|
||||
@@ -199,7 +223,7 @@ lower_programs(ZirProgram **progs, int count, const char *root, const char *out_
|
||||
if(workers > MAX_EMIT_WORKERS) workers = MAX_EMIT_WORKERS;
|
||||
if(workers < 2 || DiagnosticJsonEnabled()) {
|
||||
for(int i = 0; i < count; i++)
|
||||
if(!c_lower(progs[i], root, out_dir, syms, symbol_count, NULL)) return 0;
|
||||
if(!c_lower(progs[i], root, out_dir, syms, symbol_count, NULL, NULL)) return 0;
|
||||
return finish_simd_helpers(out_dir);
|
||||
}
|
||||
int recorded = 0;
|
||||
@@ -220,7 +244,7 @@ lower_programs(ZirProgram **progs, int count, const char *root, const char *out_
|
||||
for(int w = 0; w < started; w++) close(reports[w]);
|
||||
int lowered = 1;
|
||||
for(int i = started; lowered && i < count; i += workers)
|
||||
lowered = c_lower(progs[i], root, out_dir, syms, symbol_count, NULL);
|
||||
lowered = c_lower(progs[i], root, out_dir, syms, symbol_count, NULL, NULL);
|
||||
for(int k = recorded; lowered && k < compiler_runtime_emit_GeneratedPathCount(); k++) {
|
||||
const char *path = (const char *)compiler_runtime_emit_GeneratedPath(k);
|
||||
lowered = write_all(pipes[1], path, strlen(path)) &&
|
||||
@@ -439,21 +463,25 @@ main(int argc, char **argv)
|
||||
closed = !linked->modules[i].functions[f].is_template &&
|
||||
NativeProgramMain(&linked->modules[i],
|
||||
&linked->modules[i].functions[f]);
|
||||
if(exe && entry_fn == NULL) {
|
||||
DiagnosticTarget((ZirSourceSpan){0}, "zir_c.entry", "c", "entry.signature",
|
||||
"--exe cannot find %s", entry);
|
||||
goto done;
|
||||
}
|
||||
if(exe && !CheckNativeEntry(entry_fn, "c"))
|
||||
goto done;
|
||||
/* --no-main writes every procedure under its own name. */
|
||||
const ZirFunction *main_fn = closed && !no_main ?
|
||||
in_place_main(linked, entry_owner, entry_fn, exe) : NULL;
|
||||
if(!c_lower(linked, root, out_dir, syms, linked->module_count,
|
||||
closed ? entry_fn : NULL))
|
||||
closed ? entry_fn : NULL, main_fn))
|
||||
goto done;
|
||||
char wrapper[ZIR_PATH_MAX];
|
||||
snprintf(wrapper, sizeof(wrapper), "%s/ziran_exe_main.c", out_dir);
|
||||
remove(wrapper);
|
||||
if(exe) {
|
||||
if(entry_fn == NULL) {
|
||||
DiagnosticTarget((ZirSourceSpan){0}, "zir_c.entry", "c", "entry.signature",
|
||||
"--exe cannot find %s", entry);
|
||||
goto done;
|
||||
}
|
||||
if(!write_entry_main(entry_owner, entry_fn, out_dir, wrapper))
|
||||
goto done;
|
||||
}
|
||||
if(exe && entry_fn != main_fn &&
|
||||
!write_entry_main(entry_owner, entry_fn, out_dir, wrapper))
|
||||
goto done;
|
||||
} else {
|
||||
output_lock = lock_output(out_dir);
|
||||
if(output_lock < 0) goto done;
|
||||
@@ -480,7 +508,6 @@ main(int argc, char **argv)
|
||||
}
|
||||
result = 0;
|
||||
done:
|
||||
(void)no_main;
|
||||
ProgramsFree(&set);
|
||||
if(syms != NULL)
|
||||
c_free_syms(syms, syms_count);
|
||||
|
||||
+119
-10
@@ -191,6 +191,8 @@ static ZirSimdUses *lowering_simd;
|
||||
* knows what each header must declare; see zir_c_prune.h. */
|
||||
static ZirCPruneModule *lowering_prune;
|
||||
static const ZirFunction *lowering_entry;
|
||||
/* The procedure written as C's main itself, with no wrapper; see c_lower. */
|
||||
static const ZirFunction *lowering_main;
|
||||
|
||||
/* A host capability bound to an exported Ziran provider calls the
|
||||
* provider's C function, under whatever name collisions gave it. */
|
||||
@@ -862,12 +864,10 @@ c_text_reads(const char *text, const char *name)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Every procedure body, out of line, a #target clone or a header's inline
|
||||
* copy, starts by casting each parameter it never reads to void, so C that
|
||||
* includes or builds it passes -Wextra's -Wunused-parameter. */
|
||||
static void
|
||||
lower_body(FILE *out, const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
int symbol_count, const ZirFunction *function)
|
||||
/* The C statements of FUNCTION's body, owned by the caller. */
|
||||
static char *
|
||||
body_text(const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
int symbol_count, const ZirFunction *function)
|
||||
{
|
||||
BodySymbols context = {module, symbols, symbol_count,
|
||||
SpanPath(function->span)};
|
||||
@@ -884,6 +884,17 @@ lower_body(FILE *out, const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
"function has no checked typed body: %s", function->name);
|
||||
exit(1);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/* Every procedure body, out of line, a #target clone or a header's inline
|
||||
* copy, starts by casting each parameter it never reads to void, so C that
|
||||
* includes or builds it passes -Wextra's -Wunused-parameter. */
|
||||
static void
|
||||
lower_body(FILE *out, const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
int symbol_count, const ZirFunction *function)
|
||||
{
|
||||
char *text = body_text(module, symbols, symbol_count, function);
|
||||
char abi_args[ZIR_TEXT_MAX];
|
||||
ArrayAbiArgs(function, abi_args, sizeof(abi_args));
|
||||
const ZirParameters *parameters = ParametersOf(abi_args);
|
||||
@@ -904,6 +915,93 @@ lower_body(FILE *out, const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
free(text);
|
||||
}
|
||||
|
||||
/* FIRST, or else SECOND or SECOND_N, whichever the body text and the
|
||||
* body's own parameter NAMES leave free. */
|
||||
static void
|
||||
free_c_name(const char *text, char names[2][LOWER_NAME_MAX], const char *first,
|
||||
const char *second, char *out, size_t size)
|
||||
{
|
||||
copy_text(out, size, first);
|
||||
for(int serial = 1; c_text_reads(text, out) || !strcmp(out, names[0]) ||
|
||||
!strcmp(out, names[1]); serial++) {
|
||||
if(serial == 1) copy_text(out, size, second);
|
||||
else snprintf(out, size, "%s_%d", second, serial);
|
||||
}
|
||||
}
|
||||
|
||||
/* How a body uses one of its parameters: not at all, only to cast it to
|
||||
* void as `unused NAME` does, or for its value. */
|
||||
enum { MAIN_UNUSED, MAIN_VOIDED, MAIN_READ };
|
||||
|
||||
static int
|
||||
main_parameter_use(const char *text, const char *name)
|
||||
{
|
||||
if(!c_text_reads(text, name)) return MAIN_UNUSED;
|
||||
char cast[LOWER_NAME_MAX + 8];
|
||||
snprintf(cast, sizeof(cast), "(void)%s;", name);
|
||||
size_t length = strlen(cast);
|
||||
char *rest = AllocateOrExit(strlen(text) + 1);
|
||||
strcpy(rest, text);
|
||||
for(char *at = rest; (at = strstr(at, cast)) != NULL; at += length)
|
||||
memset(at, ' ', length);
|
||||
int read = c_text_reads(rest, name);
|
||||
free(rest);
|
||||
return read ? MAIN_READ : MAIN_VOIDED;
|
||||
}
|
||||
|
||||
/* Writes FUNCTION, a closed program's entry, as C's main itself. C passes
|
||||
* the argument count as int and the arguments as char **. The body keeps
|
||||
* its own parameter names and types: a parameter it only voids, or an s32
|
||||
* count, is main's own, and any other it reads is a copy of main's. Main
|
||||
* declares no parameters when the body uses neither, and an entry with no
|
||||
* result returns 0 from each return statement. */
|
||||
static void
|
||||
lower_main(FILE *out, const ZirModule *module, const ZirCModuleSyms *symbols,
|
||||
int symbol_count, const ZirFunction *function)
|
||||
{
|
||||
int status = !function->return_type[0] || !strcmp(function->return_type, "void");
|
||||
EmitStatusReturns(status ? function : NULL);
|
||||
char *text = body_text(module, symbols, symbol_count, function);
|
||||
EmitStatusReturns(NULL);
|
||||
const ZirParameters *parameters = ParametersOf(FunctionArgs(function));
|
||||
char names[2][LOWER_NAME_MAX] = {"", ""};
|
||||
int uses[2] = {MAIN_UNUSED, MAIN_UNUSED};
|
||||
for(int i = 0; i < parameters->count && i < 2; i++) {
|
||||
const ZirParameter *parameter = ¶meters->items[i];
|
||||
if(parameter->name == NULL || parameter->name[0] == '\0') continue;
|
||||
TargetBindingName(function, ZIR_C, parameter->name, names[i], sizeof(names[i]));
|
||||
uses[i] = main_parameter_use(text, names[i]);
|
||||
}
|
||||
if(uses[0] == MAIN_UNUSED && uses[1] == MAIN_UNUSED) {
|
||||
fputs("int\nmain(void)\n{\n", out);
|
||||
} else {
|
||||
const char *count_type = parameters->items[0].type;
|
||||
const char *values_type = !strcmp(parameters->items[1].type, "**s8") ?
|
||||
"int8_t **" : "uint8_t **";
|
||||
int own[2] = {uses[0] == MAIN_VOIDED ||
|
||||
(uses[0] == MAIN_READ && !strcmp(count_type, "s32")),
|
||||
uses[1] == MAIN_VOIDED};
|
||||
char count[LOWER_NAME_MAX], values[LOWER_NAME_MAX];
|
||||
if(own[0]) copy_text(count, sizeof(count), names[0]);
|
||||
else free_c_name(text, names, "argc", "argument_count", count, sizeof(count));
|
||||
if(own[1]) copy_text(values, sizeof(values), names[1]);
|
||||
else free_c_name(text, names, "argv", "arguments", values, sizeof(values));
|
||||
fprintf(out, "int\nmain(int %s, char **%s)\n{\n", count, values);
|
||||
if(uses[0] == MAIN_UNUSED)
|
||||
fprintf(out, " (void)%s;\n", count);
|
||||
else if(!own[0])
|
||||
fprintf(out, " %s %s = (%s)%s;\n", TargetType(count_type, ZIR_C),
|
||||
names[0], TargetType(count_type, ZIR_C), count);
|
||||
if(uses[1] == MAIN_UNUSED)
|
||||
fprintf(out, " (void)%s;\n", values);
|
||||
else if(!own[1])
|
||||
fprintf(out, " %s%s = (%s)%s;\n", values_type, names[1], values_type, values);
|
||||
}
|
||||
fputs(text, out);
|
||||
free(text);
|
||||
fputs("}\n", out);
|
||||
}
|
||||
|
||||
/* The x86-64 tiers a #target procedure gets a clone for, best first, with
|
||||
* the ziran_simd.h tier each needs above the build's own. */
|
||||
static const struct {
|
||||
@@ -1765,7 +1863,7 @@ inline_candidate(const ZirModule *m, const ZirFunction *fn)
|
||||
return 0;
|
||||
if(fn->is_template || !fn->is_public || fn->is_extern || fn->exported ||
|
||||
fn->simd_target || fn->is_global_initializer || fn->uses_host || fn->go_method[0] ||
|
||||
(fn->assembly_text && *fn->assembly_text) || NativeProgramMain(m, fn) ||
|
||||
(fn->assembly_text && *fn->assembly_text) || NativeProgramMain(m, fn) || fn == lowering_main ||
|
||||
ArrayElementType(fn->return_type, NULL, 0, NULL) || fn->inlining == ZIR_INLINE_COLD ||
|
||||
(fn->inlining != ZIR_INLINE_ALWAYS &&
|
||||
(fn->stmt_count > INLINE_STATEMENTS || fn->expr_count > INLINE_EXPRESSIONS)))
|
||||
@@ -2473,6 +2571,8 @@ lower_module_with_buffers(const ZirModule *m, const ZirCModuleSyms *restab,
|
||||
char cret[LOWER_NAME_MAX];
|
||||
if(fn->is_template || !fn->is_public)
|
||||
continue; /* private functions are file-static */
|
||||
if(fn == lowering_main)
|
||||
continue; /* nothing calls C's main */
|
||||
function_c_name(m, fn, cname, sizeof(cname));
|
||||
ArrayAbiArgs(fn, buffers->abi_args, sizeof(buffers->abi_args));
|
||||
convert_args(m, fn, buffers->abi_args, buffers->cargs, sizeof(buffers->cargs), 0);
|
||||
@@ -2742,6 +2842,12 @@ lower_module_with_buffers(const ZirModule *m, const ZirCModuleSyms *restab,
|
||||
emit_target_clones(c, m, restab, restab_count, fn, cret, cname, buffers->cargs, split);
|
||||
if(split && fn->is_public)
|
||||
fputs("#ifndef ZIRAN_TIER_UNIT\n", c);
|
||||
if(fn == lowering_main) {
|
||||
lower_main(c, m, restab, restab_count, fn);
|
||||
if(split && fn->is_public)
|
||||
fputs("#endif\n", c);
|
||||
continue;
|
||||
}
|
||||
if(fn->is_public)
|
||||
fprintf(c, "%s%s\n%s(%s)\n{\n", inlining_prefix(fn), cret[0] ? cret : "void",
|
||||
cname, buffers->cargs);
|
||||
@@ -2978,7 +3084,8 @@ write_pruned_program(const ZirProgram *program, const char *out_dir)
|
||||
|
||||
int
|
||||
c_lower_in_context(const ZirProgram *program, const char *root, const char *out_dir,
|
||||
const ZirCModuleSyms *restab, int restab_count, const ZirFunction *entry)
|
||||
const ZirCModuleSyms *restab, int restab_count, const ZirFunction *entry,
|
||||
const ZirFunction *main_fn)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -3003,6 +3110,7 @@ c_lower_in_context(const ZirProgram *program, const char *root, const char *out_
|
||||
lowering_prune = AllocateOrExit(size);
|
||||
memset(lowering_prune, 0, size);
|
||||
lowering_entry = entry;
|
||||
lowering_main = main_fn;
|
||||
}
|
||||
header_table_open(restab, restab_count);
|
||||
int lowered = 1;
|
||||
@@ -3018,6 +3126,7 @@ c_lower_in_context(const ZirProgram *program, const char *root, const char *out_
|
||||
}
|
||||
lowering_prune = NULL;
|
||||
lowering_entry = NULL;
|
||||
lowering_main = NULL;
|
||||
lowering_program = NULL;
|
||||
lowering_simd = NULL;
|
||||
/* Every file is out, so the program's helpers are known. */
|
||||
@@ -3099,10 +3208,10 @@ c_free_syms(ZirCModuleSyms *syms, int count)
|
||||
}
|
||||
}
|
||||
|
||||
int c_lower(const ZirProgram *program, const char *root, const char *out_dir, const ZirCModuleSyms *restab, int restab_count, const ZirFunction *entry)
|
||||
int c_lower(const ZirProgram *program, const char *root, const char *out_dir, const ZirCModuleSyms *restab, int restab_count, const ZirFunction *entry, const ZirFunction *main_fn)
|
||||
{
|
||||
CompilerContext *previous = CompilerContextSelect(program ? program->context : NULL);
|
||||
int result = c_lower_in_context(program, root, out_dir, restab, restab_count, entry);
|
||||
int result = c_lower_in_context(program, root, out_dir, restab, restab_count, entry, main_fn);
|
||||
CompilerContextSelect(previous);
|
||||
return result;
|
||||
}
|
||||
@@ -34,10 +34,13 @@ typedef struct ZirCModuleSyms {
|
||||
* cross-module calls; pass NULL/0 to resolve same-module only. ENTRY is
|
||||
* the entry procedure of a closed entry-linked program, one with its own C
|
||||
* main, whose headers keep only what its generated files use. NULL keeps
|
||||
* each header's whole interface. */
|
||||
* each header's whole interface. MAIN_FN, in a closed program, is the
|
||||
* procedure written as C's main itself, with no wrapper and no other
|
||||
* name; NULL writes every procedure under its own name. */
|
||||
int c_lower(const ZirProgram *program, const char *root,
|
||||
const char *out_dir, const ZirCModuleSyms *restab,
|
||||
int restab_count, const ZirFunction *entry);
|
||||
int restab_count, const ZirFunction *entry,
|
||||
const ZirFunction *main_fn);
|
||||
|
||||
/* Build the symbol table entry for one program into out. Returns 0 when
|
||||
* memory runs out. Release each entry with c_free_syms. */
|
||||
|
||||
@@ -151,6 +151,9 @@ int GoSimdWriteCheck(const char *out_dir, const char *pkg);
|
||||
/* A native program entry has a typed Ziran body plus a standard main adapter. */
|
||||
int NativeProgramMain(const ZirModule *module, const ZirFunction *fn);
|
||||
int CheckNativeEntry(const ZirFunction *fn, const char *target);
|
||||
/* While FN, an entry with no result, is written as C's main itself, each of
|
||||
* its return statements returns 0, a successful exit. NULL ends this. */
|
||||
void EmitStatusReturns(const ZirFunction *fn);
|
||||
void EmitNativeMainDeclaration(FILE *out);
|
||||
/* Declares a function bound to SYMBOL. Mach-O spells C symbols with a
|
||||
* leading underscore, which an assembler label must name explicitly. */
|
||||
|
||||
@@ -1561,6 +1561,14 @@ owned_destination_frees(Emitter *e, int index)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const ZirFunction *status_returns;
|
||||
|
||||
void
|
||||
EmitStatusReturns(const ZirFunction *fn)
|
||||
{
|
||||
status_returns = fn;
|
||||
}
|
||||
|
||||
static void emit_sequence(Emitter *e,int begin,int end);
|
||||
|
||||
static void
|
||||
@@ -1800,7 +1808,8 @@ emit_sequence_with_buffers(Emitter *e,int begin,int end, EmitSequenceBuffers *bu
|
||||
}
|
||||
else {
|
||||
drop_locals(e, 0);
|
||||
line(e,e->target==ZIR_GO?"return":"return;");
|
||||
line(e, e->target == ZIR_GO ? "return" :
|
||||
e->fn == status_returns ? "return 0;" : "return;");
|
||||
}
|
||||
leave_block_locals(e, saved);e->sequence_terminated=1;return;
|
||||
case ZIR_STMT_UNREACHABLE:
|
||||
|
||||
+48
-4
@@ -1366,11 +1366,14 @@ reexports_kept(const ZirProgram *program, const ZirModule *via,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Literal global initializers store function values outside function IR. */
|
||||
/* Literal global initializers store function values outside function IR.
|
||||
* This keeps each one, or with IMPORTED tells whether one comes through
|
||||
* that import, or with WANTED tells whether WANTED is one of them. */
|
||||
static int
|
||||
mark_global_functions(const ZirProgram *program, const ZirModule *module,
|
||||
const ZirGlobal *global, unsigned char **keep,
|
||||
int *changed, const ZirModule *imported)
|
||||
int *changed, const ZirModule *imported,
|
||||
const ZirFunction *wanted)
|
||||
{
|
||||
if(!global->init[0])
|
||||
return 0;
|
||||
@@ -1393,6 +1396,10 @@ mark_global_functions(const ZirProgram *program, const ZirModule *module,
|
||||
used |= reexports_module(imported, owner, 0);
|
||||
continue;
|
||||
}
|
||||
if(wanted != NULL) {
|
||||
used |= function == wanted;
|
||||
continue;
|
||||
}
|
||||
keep_function(program, owner, function, keep, changed);
|
||||
}
|
||||
free(probe->exprs);
|
||||
@@ -1424,7 +1431,7 @@ import_is_used(const ZirProgram *program, const ZirModule *module,
|
||||
for(int g = 0; g < module->global_count; g++)
|
||||
if(global_is_used(uses, module, &module->globals[g]) &&
|
||||
mark_global_functions(program, module, &module->globals[g],
|
||||
NULL, NULL, import->resolved_module) != 0)
|
||||
NULL, NULL, import->resolved_module, NULL) != 0)
|
||||
return 1;
|
||||
if(reexports_kept(program, import->resolved_module, keep_all, keep_types))
|
||||
return 1;
|
||||
@@ -1614,7 +1621,7 @@ resolve_reachability:
|
||||
for(int g = 0; g < module->global_count; g++)
|
||||
if(global_is_used(&uses, module, &module->globals[g]) &&
|
||||
mark_global_functions(program, module, &module->globals[g],
|
||||
keep, &changed, NULL) < 0)
|
||||
keep, &changed, NULL, NULL) < 0)
|
||||
goto failed;
|
||||
for(int f = 0; active && f < module->function_count; f++)
|
||||
if(module->functions[f].is_global_initializer)
|
||||
@@ -2192,3 +2199,40 @@ ZirProgram *NativeGoLink(const ZirProgram *program, const char *entry_module, co
|
||||
CompilerContextSelect(previous);
|
||||
return linked;
|
||||
}
|
||||
|
||||
static int
|
||||
program_uses_function(const ZirProgram *program, const ZirFunction *wanted)
|
||||
{
|
||||
for(int m = 0; m < program->module_count; m++) {
|
||||
const ZirModule *module = &program->modules[m];
|
||||
for(int f = 0; f < module->function_count; f++) {
|
||||
const ZirFunction *function = &module->functions[f];
|
||||
if(function->is_template) continue;
|
||||
for(int e = 0; e < function->expr_count; e++) {
|
||||
const ZirExpr *expression = &function->exprs[e];
|
||||
const ZirModule *owner = NULL;
|
||||
const ZirFunction *callee = NULL;
|
||||
if((expression->kind == ZIR_EXPR_CALL && expression->slot_type[0] == '\0') ||
|
||||
expression->is_function_value)
|
||||
if(ResolveFunction(module, expression->name, &owner, &callee) == 1 &&
|
||||
callee == wanted)
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* An initializer that runs out of memory counts as a use. */
|
||||
for(int g = 0; g < module->global_count; g++)
|
||||
if(mark_global_functions(program, module, &module->globals[g],
|
||||
NULL, NULL, NULL, wanted) != 0)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ProgramUsesFunction(const ZirProgram *program, const ZirFunction *function)
|
||||
{
|
||||
CompilerContext *previous = CompilerContextSelect(program->context);
|
||||
int used = program_uses_function(program, function);
|
||||
CompilerContextSelect(previous);
|
||||
return used;
|
||||
}
|
||||
@@ -10,6 +10,9 @@ ZirProgram *NativeLink(const ZirProgram *program, const char *entry_module,
|
||||
/* Go entry pruning also retains methods of reachable receiver records. */
|
||||
ZirProgram *NativeGoLink(const ZirProgram *program, const char *entry_module,
|
||||
const char *entry_function);
|
||||
/* True when code in a linked program calls FUNCTION directly or takes it as
|
||||
* a value, in a procedure body or a global initializer. */
|
||||
int ProgramUsesFunction(const ZirProgram *program, const ZirFunction *function);
|
||||
/* Bind one declared host capability to an exported Ziran implementation. */
|
||||
int BindHostProvider(ZirProgram *program, const char *spec);
|
||||
int BindHostModule(ZirProgram *program, const char *spec);
|
||||
|
||||
+15
-5
@@ -523,11 +523,21 @@ enforces the GPU storage and argument rules, but no target has a GPU device
|
||||
backend and CPU execution is never substituted. There are no GPU device
|
||||
selection environment variables.
|
||||
|
||||
C program entries use a standard `int main(int, char **)` adapter.
|
||||
The Ziran procedure keeps its declared parameter and result types for ordinary
|
||||
calls and function values. The adapter converts its integer result to a process
|
||||
status; a procedure returning nothing exits successfully. This also applies to
|
||||
a procedure exported under the symbol `main` through `#program_export "main"`.
|
||||
In a closed program the entry is C's `main` itself, with no adapter and no
|
||||
other name: `int main(void)`, or `int main(int argc, char **argv)` when the
|
||||
body uses its parameters. An `s32` count is main's own parameter, other
|
||||
parameter types the body reads are copied from main's, an integer result is
|
||||
the process status and a procedure returning nothing returns 0. This also
|
||||
applies to a procedure exported under the symbol `main` through
|
||||
`#program_export "main"`.
|
||||
|
||||
The entry keeps its own name, and a standard `int main(int, char **)` adapter
|
||||
calls it, when other code in the program calls it or takes it as a value, when
|
||||
it is exported under another symbol, when it carries assembly or `#target`
|
||||
clones, in a library build and with `--no-main`. The procedure keeps its
|
||||
declared parameter and result types for those calls and function values. The
|
||||
adapter converts its integer result to a process status; a procedure returning
|
||||
nothing exits successfully.
|
||||
|
||||
C-generated executable builds compile source files concurrently on at most half
|
||||
of the available CPUs, with no more workers than free memory holds at 512 MiB
|
||||
|
||||
+14
-5
@@ -465,11 +465,20 @@ operand's own type after checking the count.</p>
|
||||
enforces the GPU storage and argument rules, but no target has a GPU device
|
||||
backend and CPU execution is never substituted. There are no GPU device
|
||||
selection environment variables.</p>
|
||||
<p>C program entries use a standard <code>int main(int, char **)</code> adapter.
|
||||
The Ziran procedure keeps its declared parameter and result types for ordinary
|
||||
calls and function values. The adapter converts its integer result to a process
|
||||
status; a procedure returning nothing exits successfully. This also applies to
|
||||
a procedure exported under the symbol <code>main</code> through <code>#program_export "main"</code>.</p>
|
||||
<p>In a closed program the entry is C's <code>main</code> itself, with no adapter and no
|
||||
other name: <code>int main(void)</code>, or <code>int main(int argc, char **argv)</code> when the
|
||||
body uses its parameters. An <code>s32</code> count is main's own parameter, other
|
||||
parameter types the body reads are copied from main's, an integer result is
|
||||
the process status and a procedure returning nothing returns 0. This also
|
||||
applies to a procedure exported under the symbol <code>main</code> through
|
||||
<code>#program_export "main"</code>.</p>
|
||||
<p>The entry keeps its own name, and a standard <code>int main(int, char **)</code> adapter
|
||||
calls it, when other code in the program calls it or takes it as a value, when
|
||||
it is exported under another symbol, when it carries assembly or <code>#target</code>
|
||||
clones, in a library build and with <code>--no-main</code>. The procedure keeps its
|
||||
declared parameter and result types for those calls and function values. The
|
||||
adapter converts its integer result to a process status; a procedure returning
|
||||
nothing exits successfully.</p>
|
||||
<p>C-generated executable builds compile source files concurrently on at most half
|
||||
of the available CPUs, with no more workers than free memory holds at 512 MiB
|
||||
each; <code>ZIRAN_JOBS=N</code> lowers the count. Compiler commands run at niceness 10
|
||||
|
||||
@@ -37,6 +37,17 @@ the CPU's features and the build of the compiler that made it. It is checked
|
||||
against that key and its SHA-256 digest before it is used, and a damaged entry
|
||||
is removed and rebuilt. <code>ZIRAN_NATIVE_CACHE=DIR</code> uses another directory and
|
||||
<code>ZIRAN_NATIVE_CACHE=off</code> turns the cache off.</p>
|
||||
<p><code>ziran build FILE.zi</code> keeps a corpus of the programs it compiles to native code,
|
||||
as training data for improving Ziran's machine code. It is on by default,
|
||||
<code>ziran learn off</code> stops it and <code>ziran learn on</code> resumes it; benchmarks run
|
||||
through <code>bench/bench.zi</code> never record. Recording never prints during a build;
|
||||
<code>ziran learn status</code> reports a record that failed. Each new build stores its verified
|
||||
<code>.zx</code>, ELF, <code>zx2native --emit map</code> ranges and lowered input once under
|
||||
<code>$XDG_DATA_HOME/ziran/learn</code> (or <code>~/.local/share/ziran/learn</code>), named by their
|
||||
SHA-256 digests, so a rebuild writes nothing and a compiler that emits different
|
||||
code adds a second ELF beside the first. <code>--limit-mb N</code> caps the corpus (1024 MB
|
||||
by default), <code>ziran learn status</code> shows its size, <code>ZIRAN_LEARN=on|off</code> overrides
|
||||
the switch and <code>ZIRAN_LEARN_DIR=DIR</code> moves the corpus.</p>
|
||||
<p>Use <code>ziran run app.zx</code> for both console and graphical programs. The linker
|
||||
includes the reachable code of imported modules, including a UI library.
|
||||
Platform effects such as windows and input are supplied by an installed runtime
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
/* Generated by zi2c from hello.zi. */
|
||||
#include <stdio.h>
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
printf("Hello, World!\n");
|
||||
}
|
||||
@@ -30,6 +30,15 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
unused NativeRun(*test, bundle_hello[:])
|
||||
bundle_run := string.[runner, "run", "hello.zx"]
|
||||
unused NativeExpect(*test, bundle_run[:], "Hello, World!\n", test.work)
|
||||
// A closed program's entry is C's main itself, with no wrapper or header.
|
||||
NativeSet(*test, "CFLAGS", "-O2 -Werror=main -Werror=incompatible-pointer-types -Werror=return-type")
|
||||
hello_c := CheckPath(test.work, "hello-c")
|
||||
build_hello := string.[test.ziran, "build", "--target=c", "--exe", "--root", test.work, "--entry", "hello:main", "-o", hello_c, hello]
|
||||
unused NativeRun(*test, build_hello[:])
|
||||
hello_program := string.[CheckPath(hello_c, "hello")]
|
||||
unused NativeExpect(*test, hello_program[:], "Hello, World!\n")
|
||||
if !NativeAssert(*test, NativeRead(*test, CheckPath(hello_c, "hello.c")) == NativeRead(*test, CheckPath(test.repo, "tests/fixtures/native_entry/hello.c.in")) &&
|
||||
!PathExists(CheckPath(hello_c, "hello.h")) && !PathExists(CheckPath(hello_c, "ziran_exe_main.c")), "hello's C is more than one int main") { return 1 }
|
||||
missing := string.[runner, "run", "missing.zi"]
|
||||
if !NativeReject(*test, missing[:], "cannot open", test.work) { return 1 }
|
||||
cc := NativeCompiler()
|
||||
@@ -41,12 +50,13 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
node := CheckExecutable("node")
|
||||
names := string.["unsigned", "signed", "early", "fallthrough", "wide", "renamed", "renamedvoid", "implicit", "collision", "keywords", "recursive", "voidslot", "assembly", "longentry"]
|
||||
forms := string.["source", "saved"]
|
||||
compiled: s64; cross_linked: s64; web_executed: s64
|
||||
compiled: s64; cross_linked: s64; web_executed: s64; closed_programs: s64
|
||||
for name: names {
|
||||
folder := CheckPath(test.work, name)
|
||||
module := ifx name == "implicit" then "main" else name
|
||||
path := CheckPath(folder, CheckConcat(module, ".zi"))
|
||||
text := ""
|
||||
entry := ifx name == "wide" || name == "collision" then "Launch" else "main"
|
||||
if name == "longentry" {
|
||||
spelling: Vec(u8)
|
||||
for unused_index: 0..179 { unused unused_index; BuilderAppend(spelling, "a") }
|
||||
@@ -97,6 +107,21 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
result := NativeRun(*test, run[:], "", answer)
|
||||
if !NativeAssert(*test, result.output == expected, "native entry output changed") { return 1 }
|
||||
compiled += 1
|
||||
// Linked with --entry, the entry is C's main itself unless other code calls it,
|
||||
// it is exported under another symbol or it carries assembly. Longentry's
|
||||
// procedure name is longer than --entry takes.
|
||||
if name != "longentry" {
|
||||
closed := CheckPath(folder, CheckConcat("exe-", form))
|
||||
build := string.[test.ziran, "build", "--target=c", "--exe", "--root", root, "--entry", CheckConcat(module, CheckConcat(":", entry)), "-o", closed, input]
|
||||
unused NativeRun(*test, build[:])
|
||||
closed_run := string.[CheckPath(closed, module), "alpha", "π"]
|
||||
closed_answer: s32 = ifx name == "renamed" then 7 else ifx name == "renamedvoid" then 0 else answer
|
||||
closed_result := NativeRun(*test, closed_run[:], "", closed_answer)
|
||||
in_place := !PathExists(CheckPath(closed, "ziran_exe_main.c")) && !TextContains(NativeRead(*test, CheckPath(closed, CheckConcat(module, ".c"))), "zir_main_")
|
||||
own := name == "recursive" || name == "assembly" || name == "renamed" || name == "renamedvoid"
|
||||
if !NativeAssert(*test, closed_result.output == expected && in_place != own, CheckConcat("closed C entry changed: ", name)) { return 1 }
|
||||
closed_programs += 1
|
||||
}
|
||||
ndk := EnvironmentText("ANDROID_NDK_HOME")
|
||||
if form == "source" && name == "unsigned" && ndk != "" {
|
||||
triples := string.["aarch64-linux-android24", "armv7a-linux-androideabi24", "x86_64-linux-android24", "i686-linux-android24"]
|
||||
@@ -169,6 +194,6 @@ main :: (argc: s32, argv: **u8) -> s32 {
|
||||
if !NativeAssert(*test, found, "missing entry.signature capability") { return 1 }
|
||||
}
|
||||
if test.failed { return 1 }
|
||||
print("Native C entries preserve typed bodies, argument bytes, status, exports, assembly fallback and saved IR (% executables, % Android cross-links, % Wasm executions)\n", compiled, cross_linked, web_executed)
|
||||
print("Native C entries preserve typed bodies, argument bytes, status, exports, assembly fallback and saved IR, and a closed program's entry is C's main itself (% executables, % closed programs, % Android cross-links, % Wasm executions)\n", compiled, closed_programs, cross_linked, web_executed)
|
||||
return 0
|
||||
}
|
||||
Reference in new issue
Block a user