Skip to content

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 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 @entry there is E0754.
  • A plain fn main without the attribute is not an entry point. Building such a file as an executable is E0739.
  • 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.
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’s E0002.
  • 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.
  • @fini runs in the reverse of the @init order.
  • Calling an @init, @fini or @entry function, or using its name as a value, is E0727.

What start-up code is emitted depends on --emit:

  • exe: the OS entry symbol (_start, or mainCRTStartup on Windows) is generated by the compiler. It calls _xl_initAll (every @init), then the @entry function, then _xl_finiAll (every @fini in reverse), and exits with the i32 the entry returns.
  • shared: _xl_initAll is registered as the load hook and runs before @entry when the library is loaded (ELF .init_array, Mach-O __mod_init_func); _xl_finiAll is the unload hook (ELF .fini_array, Mach-O __mod_term_func).
  • static and obj: no entry code, and an @entry function is E0754. _xl_initAll and _xl_finiAll are weak exported symbols: the host calls _xl_initAll once before it uses the library and _xl_finiAll once after.
  • ir, bc and header do not raise E0754.

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.