Entry point
A program’s start-up is declared with three attributes. The compiler reads only these names for start-up and teardown; everything else about them is ordinary language.
| Attribute | Marks | Runs |
|---|---|---|
@entry |
the program’s entry function | after every @init, instead of the default start |
@init |
a module-level boot function | before @entry, imports first |
@fini |
a module-level teardown function | after @entry, in reverse order |
@entry
Section titled “@entry”@entry fn main() -> i32 { return 0}- An executable must have exactly one; a second is
E0753. A static library or object file must have none; an@entrythere isE0754. - A plain
fn mainwithout the attribute is not an entry point. Building such a file as an executable isE0739. - The form is
@entry fn name() -> i32, or with the process arguments:
@entry fn main(argc: i32, argv: **u8) -> i32 { return 0}argc is the number of arguments; argv holds argc C strings followed by a NULL pointer, with the program path first. Any other parameter list, a generic function or a method is E0752.
- The return type may be omitted, and the exit status is then 0.
- The entry function is identified only by its attribute, so it may be overloaded like any other function.
- A shared library may have one, and it takes no parameters there (
E0755). - Windows executables take no entry arguments (
E0755), and Windows shared libraries run no hooks yet.
@init and @fini
Section titled “@init and @fini”global ready: bool = false
@init fn boot() { ready = true}
@fini fn shutDown() { ready = false}- Both take no parameters, have no return type and have a body. Anything else is
E0729; a bodyless@init fn boot();is the parser’sE0002. - Both are module level only: a class method, a generic item or a non-function item is
E0729. - Run order: the stdlib group first, then the user group. Within a group a module’s imports initialize before the module, and modules run in source order. Each module runs once; in an import cycle a module reached again while it is still being visited is skipped.
@finiruns in the reverse of the@initorder.- Calling an
@init,@finior@entryfunction, or using its name as a value, isE0727.
What the compiler generates
Section titled “What the compiler generates”What start-up code is emitted depends on --emit:
exe: the OS entry symbol (_start, ormainCRTStartupon Windows) is generated by the compiler. It calls_xl_initAll(every@init), then the@entryfunction, then_xl_finiAll(every@finiin reverse), and exits with thei32the entry returns.shared:_xl_initAllis registered as the load hook and runs before@entrywhen the library is loaded (ELF.init_array, Mach-O__mod_init_func);_xl_finiAllis the unload hook (ELF.fini_array, Mach-O__mod_term_func).staticandobj: no entry code, and an@entryfunction isE0754._xl_initAlland_xl_finiAllare weak exported symbols: the host calls_xl_initAllonce before it uses the library and_xl_finiAllonce after.ir,bcandheaderdo not raiseE0754.
The entry symbols (_start, mainCRTStartup) and _xl_initAll are compiler-owned: a user export(c), @mangle or declare that uses one of these names is E0726.
Entry stubs exist for Linux and macOS on x86-64 and arm64, and for Windows on x86-64. See Known limitations for the targets that are not covered.