diff --git a/CHANGELOG.md b/CHANGELOG.md index 69e439247..105b868cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/cmd/zi2c/main.c b/cmd/zi2c/main.c index 501e2e6f1..56831a3bb 100644 --- a/cmd/zi2c/main.c +++ b/cmd/zi2c/main.c @@ -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); diff --git a/cmd/zi2c/zir_c_lower.c b/cmd/zi2c/zir_c_lower.c index 9f9768dc7..ea3041ffd 100644 --- a/cmd/zi2c/zir_c_lower.c +++ b/cmd/zi2c/zir_c_lower.c @@ -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; } diff --git a/cmd/zi2c/zir_c_lower.h b/cmd/zi2c/zir_c_lower.h index 70d767b6f..e80c1ba38 100644 --- a/cmd/zi2c/zir_c_lower.h +++ b/cmd/zi2c/zir_c_lower.h @@ -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. */ diff --git a/cmd/zir/zir_emit.h b/cmd/zir/zir_emit.h index c5f3a7e0a..29fb16dfe 100644 --- a/cmd/zir/zir_emit.h +++ b/cmd/zir/zir_emit.h @@ -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. */ diff --git a/cmd/zir/zir_emit_statement.c b/cmd/zir/zir_emit_statement.c index 5422294d2..5a333b529 100644 --- a/cmd/zir/zir_emit_statement.c +++ b/cmd/zir/zir_emit_statement.c @@ -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: diff --git a/cmd/zir/zir_link.c b/cmd/zir/zir_link.c index cca8c41cd..f023a31ca 100644 --- a/cmd/zir/zir_link.c +++ b/cmd/zir/zir_link.c @@ -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; +} diff --git a/cmd/zir/zir_link.h b/cmd/zir/zir_link.h index 9e5318f80..d5357ab1c 100644 --- a/cmd/zir/zir_link.h +++ b/cmd/zir/zir_link.h @@ -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); diff --git a/docs/USAGE.md b/docs/USAGE.md index 6f03e65a8..b10775982 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -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 diff --git a/site/guide/usage.html b/site/guide/usage.html index 2d3540847..c5c2dff94 100644 --- a/site/guide/usage.html +++ b/site/guide/usage.html @@ -465,11 +465,20 @@ operand's own type after checking the count.
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
each; ZIRAN_JOBS=N lowers the count. Compiler commands run at niceness 10
diff --git a/site/guide/zx.html b/site/guide/zx.html
index 540ce64d8..3091577fb 100644
--- a/site/guide/zx.html
+++ b/site/guide/zx.html
@@ -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. ZIRAN_NATIVE_CACHE=DIR uses another directory and
ZIRAN_NATIVE_CACHE=off turns the cache off.
ziran build FILE.zi 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,
+ziran learn off stops it and ziran learn on resumes it; benchmarks run
+through bench/bench.zi never record. Recording never prints during a build;
+ziran learn status reports a record that failed. Each new build stores its verified
+.zx, ELF, zx2native --emit map ranges and lowered input once under
+$XDG_DATA_HOME/ziran/learn (or ~/.local/share/ziran/learn), 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. --limit-mb N caps the corpus (1024 MB
+by default), ziran learn status shows its size, ZIRAN_LEARN=on|off overrides
+the switch and ZIRAN_LEARN_DIR=DIR moves the corpus.
Use ziran run app.zx 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
diff --git a/tests/fixtures/native_entry/hello.c.in b/tests/fixtures/native_entry/hello.c.in
new file mode 100644
index 000000000..d7a7912c7
--- /dev/null
+++ b/tests/fixtures/native_entry/hello.c.in
@@ -0,0 +1,8 @@
+/* Generated by zi2c from hello.zi. */
+#include