Design Decisions
Why Koji is the way it is. Each decision says what Koji does, why, and what was considered instead. The other documents describe how to use the language; this one keeps the reasons, so that later decisions stay consistent with them.
Principles
Most decisions follow from a few ideas:
- Explicit over implicit: what can change, what can fail and what a function takes over are visible where they happen (
mut,Result,?,take). - Errors at compile time: a mistake the compiler can see (a typo, a missing case, a moved value) is a compile error, not a surprise when the program runs.
- Errors are values: expected failures are
Resultvalues that the code handles; bugs stop the program. - Sum types over strings and flags: a closed set of cases is a sum type, so
matchchecks that every case is handled. - Values, not references: assigning or passing a value gives an independent copy; nothing changes "behind the back" of a variable.
- One way to do each thing: fewer constructs, each with a clear purpose.
Variables
mut and immut
Decision: every declaration starts with mut (the variable can change) or immut (neither the variable nor its contents can change). x = 1 only changes an existing mut variable.
Why: the keyword makes the intent visible, and a typo (cuont = 1) is an error instead of a new variable. With value semantics, two levels are enough: changing a field and replacing the whole value are both ways of changing the variable.
Alternatives: var, let and const, as in TypeScript, have a third level (a variable that cannot be replaced but whose contents can change), which means little with value semantics. Declaring by assignment, as in Python, hides typos. var, const and let stay reserved words, so the error points to mut and immut.
for and match variables are immut
Decision: for x in items and the variables of a match pattern are immut; for mut x in items allows changing x (a copy: the list does not change).
Why: changing them is rarely intended, and changing a copy can be mistaken for changing the list.
Types
No any
Decision: there is no any type. Dynamic data is a sum type (json.Json).
Why: every value has a known type at compile time, so no value needs to carry its type when the program runs, and every use of dynamic data is checked.
Alternatives: any with runtime checks, as in TypeScript, moves type errors to runtime.
Nominal types, no interface, no anonymous objects
Decision: type declares every type. Shape types are nominal: two types with the same fields are different types. An object literal is a value of the type expected where it is written, and there is no subtyping.
Why: Celsius and Fahrenheit do not mix, and a value always has one known type. Generic constraints are deduced from the structure of each type (equatable, orderable), and other behavior is passed as a function, so interfaces are not needed.
Alternatives: structural types and interfaces, as in TypeScript and Go. interface stays a reserved word, as a precaution.
int, decimal and float
Decision: an int becomes a decimal or a float only as a single value (a variable, an argument, a field, the items of a list literal), never inside an existing value: an int[] is not a decimal[]. int / int is an int truncated toward zero, and % has the sign of the dividend.
Why: converting a single value is free; converting a list would copy it without the code saying so. Integer division follows Go and Rust.
Numbers are always finite
Decision: there is no NaN and no infinity, in a decimal or a float. A result that does not fit (float(1e308) * float(10), 9223372036854775807 + 1) panics, like a literal that does not fit (0x1p1024, 1e7000) is a compile error. In math, a result that does not exist in the real numbers is an Err(MathError): sqrt(-1) is Complex, pow(0, -1) is Infinite.
Why: NaN spreads silently through computations and breaks equality (NaN != NaN). The rule separates two kinds of problems: a representation limit (the result exists but does not fit: a larger type would hold it) panics, and a domain problem (the result does not exist in the reals) is an error the code can handle. Operators cannot return a Result, so they always panic, including on division by zero. 0 ** 0 is 1, the value of the expression in algebra (only the limit form 0⁰ is indeterminate). MathError.Indeterminate is kept for future functions.
Alternatives: IEEE 754 infinities and NaN, as in most languages.
decimal
Decision: decimal is a number with 34 significant digits in base 10 (the precision of IEEE 754 decimal128), computed by the runtime in software, in 16 bytes, and it is the type of a literal with a point or an exponent (1.5, 2e3): exactly the number written. A literal that does not hold exactly (more than 34 significant digits, or out of range) is a compile error. An operation rounds its exact result to 34 digits, half to even. A decimal is shown in full, as in everyday writing: 10, 1.5, 2500000, without trailing zeros (1.50 equals 1.5); a whole number is shown in full, like an int, up to the 34 digits a decimal holds (beyond them, in scientific notation: 1e300), and a fraction that needs six or more zeros after the point before its first digit is shown in scientific notation, as science writes it (1e-7). It panics when a result does not fit, and also when it is too small to be anything but zero. ** takes any decimal exponent: an int exponent is exact, another is computed with 64 digits. The standard library uses decimals where it does not compute heavily: seconds in time, the numbers of json, math.pi and math.e.
Why: Koji keeps the precision of the program above the influence of the hardware. A float is binary: 0.1 is not one tenth, and 0.1 + 0.2 is 0.30000000000000004 in a float. Precision is not the only reason:
- What is written is what is held. A decimal read from text and written back gives the same text; a float only looks like
0.1because its shortest decimal is shown. - Equality works.
0.1 + 0.2 == 0.3, so comparisons, the keys of a map and the cases of amatchbehave as people expect. - The same result on every machine. All the arithmetic of a decimal,
sqrtandpowincluded, is in the runtime of Koji; the functions of a float come from the system'slibm, whose results can differ between systems. - Data stays as it came. Numbers from JSON, a file or the user are kept exactly, and a program that reads and writes them back does not change them.
- What some domains require. Money, taxes and accounting round in base 10, often half to even, by rule.
- Fewer traps. A whole kind of bug, common for beginners, disappears, and each conversion to a float is written in the code, like the rest of what Koji does not hide.
The cost is speed, and a float is there when it matters. 34 digits hold every float exactly (the shortest decimal of a float has at most 17 digits), so converting a float to a decimal and back loses nothing, and a decimal never looks as close as a float would: 16 digits would round 0.30000000000000004 to 0.3. Measured, the common operations stay fast (an addition with the same exponent or an exact product in about 5 ns; a rounded product about 50 ns and a division about 70 ns, runtime/bench/decimal.c): a value is not normalized, so most operations need no division, and it is passed in two registers, never allocated. Half to even does not drift when many results are rounded. Equal values print the same; a number shown in full is the one people read (the exponent of a scientific notation is easy to miss), a whole number reads the same as an int of the same value, and scientific notation takes over for the small fractions science writes that way, where the zeros are only placeholders. A whole number with more than 34 digits would end in zeros the decimal does not keep, so its scientific notation shows exactly what is held, without a line of thousands of digits.
Alternatives: 16 digits (decimal64), which fits in 8 bytes but is about as precise as a float; precision without limit (Java's BigDecimal, Python's Decimal), whose division needs a precision chosen somewhere (in Python, a global context); rational numbers (Raku), exact for the four operations but whose denominators grow without limit; keeping only float.
float is written as it is stored
Decision: float stays the 64-bit binary type of the processor, for complex computations. It is written and shown in base 16, as it is stored: a mantissa after 0x and a power of 2 after p (0x1.999999999999ap-4 is the float nearest to 0.1). A float literal must be exact (at most 53 bits, in range). A decimal literal is not a float: float(0.1) converts it (computed while compiling), and decimal(x) of a float is the shortest decimal that is the same float again, so decimal(float(0.1) + float(0.2)) is 0.30000000000000004. A float and a decimal never mix: price * rate with a decimal and a float is an error, and one of them is converted.
Why: the value a program shows is the value it holds. A float shown in base 10 looks like the decimal it approximates (0.1), hiding the rounding that makes 0.1 + 0.2 != 0.3; in base 16 it is exact and short, and a hexadecimal literal is what C, Java and Go use for exact floats. Converting explicitly shows where a value is rounded. Floats keep the speed of the processor where it matters (simulations, graphics), and the conventions say which type to use.
Alternatives: renaming float to binary and writing it in base 2 (0b1.1p1), exact too but much longer; showing a float as its shortest decimal, as most languages do; letting a decimal literal become a float where a float is expected, which would round without the code saying so.
char
Decision: char is a primitive type, a Unicode code point. A one-character literal is a char where a char is expected.
Why: text processing (a lexer, a parser) works on characters, and a char cannot be confused with a string of any length.
Maps and sets
Decision: Map[K: equatable, V], written ['Ana': 30] ([:] when empty), and Set[T: equatable], from Set.from(list). There are no index signatures.
Why: a dictionary with keys of any equatable type, checked like any other generic type.
Generics
Decision: type parameters in brackets after the name (function first[T], type Pair[A, B]), with the constraints equatable and orderable, which the compiler deduces from the structure of each type. Type arguments are never written at a call: they come from the arguments and the expected type. A generic function used as a value needs an expected function type, which gives its type arguments.
Why: the same bracket syntax as Option[T], and no declarations to keep in sync with what a type can do.
Alternatives: angle brackets, which are ambiguous with comparisons; constraints that types must declare (interfaces, traits).
Ok()
Decision: a Result[void, E] succeeds with Ok(), without an argument; it is shown as Ok(void).
Why: there is no void value to write.
Control flow
No else if
Decision: if has an optional else with a block; there is no else if. More than two branches are a match with guards, or special cases handled first with an early return.
Why: two rules of thumb, built into the language:
- When a condition needs a third branch, it is a case analysis, and a case analysis reads better as
switch/casethan as anif/else if/elsechain. Koji'smatchtakes that role: with guards, it lays the cases side by side (x if (x > 18) => 'Adult'), and with patterns it also checks that every variant is handled. - Early returns should be the natural way to write: special cases first, each ending with
return(orbreakandcontinue), and the main code after them, unnested (see Conventions).else ifinvites the opposite, a chain where every branch stays nested until the end; without it, the early return is the obvious way to handle a special case.
Alternatives: else if, as in most languages; a switch statement next to match, which would be a second construct for the same job. A match without a value (match { x > 18 => ... }) could make guard chains shorter later.
Conditions in parentheses
Decision: conditions are written in parentheses: if (x > 0), while (running), match (value).
Why: the condition is clearly delimited from the block, like the arguments of a call, and not (a and b) reads without doubt about what not applies to.
Errors
No try / catch
Decision: expected failures (a missing file, invalid JSON, int('abc')) are Result values; bugs (an index out of range, a division by zero, an overflow) panic: the program stops, and a panic cannot be caught. panic(message) reports impossible states.
Why: the signature of a function says whether it can fail, and the compiler checks that the error is handled. Exceptions are invisible in signatures, and catching them makes bugs look like expected failures.
?
Decision: x? gives the Ok (or Some) value, or returns the Err (or None) from the function; at the top level of the main file, it stops the program with the message. When the error type differs from the function's, ? wraps it in the one variant of the function's error type that holds exactly that type; if there is no such variant, or several, it is a compile error, and mapErr converts explicitly.
Why: propagating errors stays short without hiding them, and the conversion is automatic only when there is a single possible meaning.
Typed errors in the standard library
Decision: every fallible function of the standard library returns a Result with an error type of its module (FileError, HttpError, JsonError...), and print shows them as readable messages (file 'notes.txt' not found).
Why: the code can match on what went wrong, and the message is ready for the user.
Functions and methods
Methods and self
Decision: a method is a function prefixed with its type, with an explicit self: function Person.greet(self). Without self, it is an associated function, called on the type (Person.create('Ana')). A method that changes its value declares mut self; the call needs no marker. There are no special constructors.
Why: methods are ordinary functions, declared next to the type, and what a method may change is in its signature.
Alternatives: classes, this, constructors: more concepts for the same result.
mut and take parameters
Decision: a parameter the function may change is mut, both in the declaration and at the call (addItem(mut items, 2)). A take parameter takes ownership of a move-only argument. Other parameters are read-only.
Why: with value semantics, a function cannot change the caller's variable unless the call says so, visibly. A mut argument is the variable itself: the function gets its value when the call happens, after the other arguments.
Closures capture a copy
Decision: a closure captures a read-only copy of the variables it uses, from the moment it is created. Top-level variables are not captured.
Why: no hidden shared state between a closure and the code around it.
Memory
Ownership and value semantics
Decision: memory is managed with ownership, without a garbage collector. Assigning or passing a value gives an independent copy, which shares the memory until one of the copies changes (copy-on-write, as in Swift). When its owner goes out of scope, a value is dropped. is only tests variants; == compares values.
Why: no aliasing bugs, deterministic memory use, and cheap copies.
Alternatives: a garbage collector with shared references (Java, Go); explicit borrowing with lifetimes (Rust), which is precise but harder to learn. Intentionally shared structures (graphs, caches) keep their values in a list and store indices.
Move-only types and drop
Decision: a type that declares function Type.drop(mut self) is move-only, and so is a type that holds a move-only value (a File). Assigning one moves it; using the previous variable is a compile error.
Why: a resource must be released exactly once; two copies of a file would close it twice. The property follows from the type's contents, so there is no separate marker to keep in sync.
No defer
Decision: there is no defer.
Why: dropping a value releases its resources, at the end of its scope, deterministically. It would be reconsidered only for cleanup that does not belong to any value.
Modules
Mandatory export
Decision: everything in a module is private unless marked with export. An exported mut variable is read-only outside its module, and importers see its current value. A public signature cannot use a private type. Exporting a sum type exports its variants.
Why: a module decides what it offers, and only its own code changes its state.
Imports belong to their block
Decision: an import applies to its whole block, before the other statements (like ES modules); an import inside a block is visible only there, and its module runs the first time the block runs.
Why: declarations can use an import written below them, and a module used only in one place is loaded only if that place runs.
Standard library
File modes
Decision: file.open(path, mode) takes a sum type, Read, Write or Append.
Why: an invalid mode does not compile, unlike a string such as 'rw'.
printError
Decision: a built-in printError(...) writes to the standard error output, with the same arguments as print: the values shown as their display() text, separated by spaces, and a line break.
Why: print and printError are a symmetric pair, and reporting an error is as basic as printing, so it needs no import. The name is in full words, like the rest of the language.
Alternatives: system.printError(...), which needs an import for something as basic as print; values for the standard outputs (file.stderr.write(...)), which would be shared move-only values, and whose write returns a Result that print does not; the abbreviation eprint.
file.remove
Decision: file.remove(path) removes a file and returns a Result[void, FileError]; it does not remove a directory (Err(IsDirectory(path))).
Why: removing a file is the common case, and a separate function for directories (a removeDir, when a program needs it) means a directory is never removed by mistake.
Alternatives: one remove for files and directories.
system.os
Decision: system.os is a constant of the sum type OperatingSystem = { Linux, MacOS, Windows, Other(name: string) }.
Why: a match on it is checked: every system is handled, or there is a _ arm. Other keeps the list open for systems without a variant. It is a constant, like math.pi, since it never changes while a program runs.
Alternatives: a string ('linux'), which a typo ('macOS') gets past the compiler.
system.run and system.capture
Decision: system.run(command, args) runs a program with the terminal of this one (the same input and outputs) and gives its exit code; system.capture(command, args) runs it and keeps what it prints, as an Output.
Why: the two differ in what happens to the program's output, and each name says it: run runs it as if it were typed in the terminal, the common case; capture keeps the output. They are verbs, like the other functions that do something (open, remove, exit), while the nouns of system (args, env, os) only read the state of the process.
Alternatives: system.output(...) for capturing (the name of the type it gives, and of Rust's Command::output), a noun that would hide that it runs a program; keeping run for capturing with a new execute or call for the terminal, names whose difference from run is not visible; a mode argument (run(command, args, Terminal)), which would make the result type depend on the mode.
system.command: a program with its input, directory and environment
Decision: system.command(program, args) gives a Command, a record whose fields say how it runs (input, directory, environment, cleared), changed before it runs; its methods output() and run() run it, as capture and run do (which are command(...).output() and command(...).run()). The input is written while the outputs are read, so that neither side waits for the other, and a program that does not read it does not stop this one (SIGPIPE is ignored while it is written).
Why: the tests of Koji, written in Koji, run the compiler and their programs with an input, in a directory of their own and with variables of their own (KOJI_TARGET, TMPDIR), as any program that drives others does. A record changed field by field needs nothing new of the language, where a builder would need methods for each option, and its defaults are those of capture.
Alternatives: more parameters for capture, every call writing all of them; a builder (command(...).input(...).directory(...), as Rust's Command), a method for each field; a shell (sh -c 'cd dir && ...'), which is not on Windows and must quote everything.
system.executable
Decision: system.executable() gives the path of the executable of the running program, as an Option[string] (None where the system cannot tell it). kojic uses it to find the runtime library next to itself.
Why: the name says what it gives, a file, like path.parent and path.name. It is a noun, like args and env, since it only reads the state of the process; a function, not a constant like system.os, since finding it asks the system.
Alternatives: system.current(), which reads as the current directory, as current_dir in Rust and getcwd in Python; system.programPath(), longer; system.self(), vague.
Conventions
Special cases first
Decision: the convention is to handle special cases first, with an early return (or break and continue), so the main code is not nested inside if and else.
Why: the main path reads from top to bottom at one level, and each special case is visible where it ends.
One line break at the end of a file
Decision: a file ends with exactly one line break.
Why: a text file is a sequence of terminated lines, as Unix tools expect: without the final line break, cat joins the last line with the next output and git diff reports it; extra blank lines are noise.
Tooling
kojic run and kojic build
Decision: kojic build compiles a program to an executable, through C, linked with a runtime library (written in Koji: see below); kojic run compiles it to a temporary executable and runs it. There is no interpreter.
Why: one implementation of the language, so a program behaves the same in development and in production. Generating C needs no code generator library, works wherever there is a C compiler, gets the optimizations of gcc and clang, and the generated code is readable.
Alternatives: an interpreter for kojic run, which starts faster but is a second implementation of every feature that must give the same results (Koji had one, in Rust, until the compiler written in Koji replaced it); a native backend (LLVM, Cranelift, or machine code directly), which can come later from the same intermediate representation.
The compiler is written in Koji, from a seed of its own executables
Decision: kojic is written in Koji (compiler/). It is built from the seed, an executable of the compiler for each platform its native backend reaches (bootstrap/seed/kojic-linux-x64.gz, kojic-linux-arm64.gz, compressed): the seed builds the runtime library (kojic library) and, with it, the compiler of compiler/ (bootstrap/build.sh), machine code and linking included, with no C compiler; building Koji needs gzip and the seed of its platform. The seeds are the compiler built by itself (the fixed point: bootstrap/build.sh --seed writes them again, the same byte for byte from one generation to the next), all of them from one machine (the native backend builds for each platform). They are written again only when the compiler needs a feature the seed does not compile, as Rust updates its stage 0, so that the history does not grow by megabytes with each change.
How it came: the seed was first the C the compiler generated for itself (bootstrap/kojic.c.gz), which a C compiler built: one file for every system, but a C compiler to build Koji. Once the native backend and Koji's linker built the compiler and its library on Linux, the seed became executables; the seed in C went with the C backend (see "Machine code").
Why: the compiler is the largest Koji program, and writing it in Koji tests the language and shows what it lacks. A seed in C needs only a C compiler to build Koji, on any system, and the compiler can use any feature of the language once the seed is written again with it.
Alternatives: keeping the compiler in Rust, a second implementation to maintain along with the one in Koji; a seed in C, which needs a C compiler (and so a C backend to write it); a seed in the intermediate representation, which would need an interpreter of it; a chain of releases to build from (like Go, which needs an earlier Go), the seed here being the one release in the repository.
A runtime of its own, written in Koji
Decision: the runtime and the standard library are written in Koji, not in another language. What Koji cannot express yet (raw memory, calls to the operating system) comes from an internal module, koji:unsafe, with an address type, reads and writes of bytes and words, and extern function declarations of C functions of the system's library; only the standard library imports it at first. Its modules are in std/, and the compiler carries them (compiler/stdlib.kj, generated from std/): import json compiles std/json.kj like any module. HTTPS uses a TLS written in Koji. What stays outside Koji: the system's C library, as for nearly every language, and the C that the compiler generates (its target, not code written by hand); if a small part can only be written in another language, it is Rust. The runtime in Rust shrinks as modules move to Koji, each one with the same behavior, the same tests, and no loss of speed.
Why: a serious language should not need another one to run its programs: the standard library is then Koji's to read, change and optimize, and Koji is tested on it. The low-level part stays small and out of the users' sight, as unsafe does in Go, so the language stays safe.
Alternatives: the runtime in Rust (as it was), which needs cargo and libraries of others; the runtime in C, which is not Koji either; TLS from the system (OpenSSL), as Python or Node do, which depends on a library in C.
byte
Decision: byte is a primitive type, an unsigned integer of 8 bits (0 to 255), for binary data (byte[]) and memory. An integer literal is a byte where a byte is expected; bytes go with bytes, and convert with byte(n) (which panics outside 0 to 255) and int(b). Arithmetic panics when the result is not a byte, like that of ints; the bits stay in 8 bits.
Why: binary data (files, the network, memory) is made of bytes, and Koji had no type for them: an int is 8 bytes. As char is not a number but a character, byte is a number of its own size.
Alternatives: a byte as an int from 0 to 255 (no type: readByte and writeByte in koji:unsafe, and lists of ints 8 times as large); arithmetic that wraps around (255 + 1 is 0), which hides mistakes, as for ints.
The epoch of Koji is 0000-01-01
Decision: a timestamp counts the seconds since 0000-01-01 00:00:00 UTC, in the proleptic Gregorian calendar (where the year before 1 is 0, as in ISO 8601): time.now(), datetime.format and datetime.parse use it. A timestamp of Unix (since 1970-01-01), which files, HTTP, most APIs and other languages use, is 62,167,219,200 seconds (719,528 days) less.
Why: the dates of history, from the year 0, have positive timestamps, and the epoch is the start of the calendar rather than an arbitrary date.
Alternatives: the epoch of Unix, which nearly every system uses, so that a timestamp needs no conversion when it is exchanged (the cost of this decision: every exchange adds or subtracts the difference); a date type without a timestamp.
koji:unsafe: the lowest level of the standard library
Decision: a module of the standard library may import koji:unsafe, which has Address (a place in memory, of its own type), CInt (the 32-bit int of C, for extern declarations only: an int in Koji), CSize (the size_t of C, likewise), CVariadic (the last parameter of a variadic function of C, fcntl: where the arguments of ... start), U128 (an unsigned integer of 128 bits, unsigned __int128 in C: +, - and * wrap around, /, % and the shifts beyond 127 panic, the comparisons are unsigned, a literal is one where a U128 is expected; wide(low, high), lowWord, highWord and leadingZeros; decimalLow, decimalHigh and decimalOf give the words of a decimal and back: what the decimals in Koji are made of), Location (the place of a call, main.kj:12, for panics: a last parameter of this type is given by the compiler, so f.read() calls File.read(f, here)), Never (the type of a function that does not return: exit of C, a function for C that ends the program), locationAddress (the text of a Location, as C has it), offset, distance, nullAddress and isNull (arithmetic of addresses, and the address 0), stringBytes, stringSize and newString (the UTF-8 bytes of a string, and a string from bytes; the address of the bytes does not keep the string alive, which is dropped after its last use, so a function that reads them takes the string), blankString, stringReferences, stringCapacity and setStringSize (a string to fill, and growing one in place), reserve, listItems and setListLength (room for more items in a list that is not shared, the address of its items, a word each, and how many of them were written), allocate, free, read and write (of an int, a float, a boolean, a char, a byte or an Address, with its size: immut n: int = read(address, 8), the type read being the one expected) and panicAt(here, message) (a panic at that place); and it may declare extern function name(...): type, a function of the system's C library or of the runtime, with values of C only: int, CInt, CSize, float, decimal, boolean, char, byte, Address and Location, a U128 or a string it reads (a string it returns is a new reference), and Never for a function that does not return. Other programs can use neither.
Why: the standard library is written in Koji, but some of it must reach the system (clocks, files, processes) or memory; this keeps that part small, visible and out of the users' sight, as unsafe does in Go.
Alternatives: these functions in the runtime in Rust (as they were); an FFI for every program, postponed until the language needs it.
math.abs of several types: the compiler chooses the function
Decision: abs, sqrt, pow, floor, ceil and round of math take an int, a decimal or a float (as they always did). math is written in Koji (std/math.kj), with a private function for each type (absInt, absDecimal, absFloat...); the compiler checks a call by the rules of the function (the same types and messages as before: math.pow does not mix a float and a decimal) and calls the function for the types of its arguments, with the Location of the call as its last argument, so that its panics name the line of the program (main.kj:3: integer overflow), not one of std/math.kj.
system.exit is checked by the compiler in the same way, since a function of Koji cannot say that it never returns: it calls leave of std/system.kj, and the code after it is unreachable.
Why: the module moves to Koji without changing for its users, and overloading stays out of the language: six functions do not justify it.
Alternatives: overloading of functions in the language (rules for choosing, for errors and for functions as values, for six functions); generic functions with a constraint for numbers, which Koji does not have; one function for each type in the API (math.absFloat), heavier for every user.
Built-in methods written in Koji: koji:core
Decision: methods of the built-in types can be written in Koji, in the internal module koji:core (std/koji/core.kj), which programs cannot import. The checker keeps their signatures; a call (text.trim()) is one of the function of the same name, with the value first (trim(text)) and, for a method that may panic, the Location of the call last. The module is checked only when a program uses one of them, and it has no top-level variables (it is never initialized). First the methods of strings that need no table of Unicode: trim, split, replace, startsWith, endsWith and indexOf, on the bytes of the string (stringBytes, stringSize and newString in koji:unsafe), with memcmp and memmem of the C library (linear searches).
The modules of the standard library may import koji:core too (system uses textOf, which reads bytes from the system as UTF-8, each invalid sequence becoming U+FFFD as Rust's from_utf8_lossy does).
Why: the runtime moves to Koji one function at a time, without changing the language or its messages: the same mechanism as math, for methods.
Alternatives: functions of Koji compiled under the C names of the runtime, which the generated code would call as it calls the runtime (the conventions of references of the runtime and of Koji would have to match at that border); a module of Koji imported by every program (compiled for each program, even one that never uses it).
The library of the runtime written in Koji
Decision: the part of the runtime written in Koji that C calls (the prelude, the generated code) is a library: the internal modules of std/koji/ but koji:core, compiled once (kojic library) into the archive of the runtime (libkoji_runtime.a), which every program links; since the runtime in Rust is gone, it is all of the runtime. Its functions are declared extern function name(...) { ... }: a function of Koji, given to C under its name (with values of C only, as for extern declarations). The library has no main: koji_library_init runs its modules, and the runtime calls it first. A program cannot import its modules: it calls their functions by their names of C. A function for C reads the strings it is given; one it declares take is given by C, and released after the call (a function of Koji does not take the reference of a string it is given, and shares the one it returns). First koji_decimal_neg, koji_decimal_abs and koji_decimal_from_int; the rest of the decimals follow. Its modules are compiled together, so one may import another (koji:list uses mixed of koji:text). A function for C may return Never (koji_std_panic). The functions given to the library, such as those of the type of the items of a list (KojiElementType: retain, release, equal...), are called through their addresses, with the types that extern declarations of Koji give. The tables of Unicode of upper, lower and the is... methods (std/koji/unicode.kj) were first written from the methods of char of Rust, so that the methods give what they gave when the runtime was in Rust; they are now written by a program of Koji, bootstrap/generate/unicode.kj, from the files of the Unicode Character Database, with the same tables, byte for byte, for Unicode 17.0.0 (Rust's properties are those of the database: Alphabetic, Lowercase, Uppercase, Cased and Case_Ignorable of DerivedCoreProperties.txt, the mappings of SpecialCasing.txt without conditions over those of UnicodeData.txt). Likewise, the tables of powers of 5 with which koji:decimal finds the shortest decimal of a float (Ryu, std/koji/ryu.kj) are written by bootstrap/generate/ryu.kj, and the authorities built in (std/koji/roots.kj) by bootstrap/generate/roots.kj: no generator is in another language.
Why: as the runtime of other languages, it is compiled once, not with each program, and the prelude calls it as the generated code does. Its C is in bootstrap/ for the same reason as the seed: building Koji needs no Koji compiler.
Alternatives: the functions compiled into each program (as koji:core), which makes every compilation slower as the runtime moves; a library built by the tests and bootstrap/build.sh with the compiler just built (the seed would need a library before the compiler that makes it).
Messages name files as they were written
Decision: type errors and panics name the main file as it was given on the command line, and a module as its import wrote it (lib/math.kj:3: division by zero).
Why: the same names everywhere, which the reader recognizes from the code.
HTTPS: TLS 1.3 only, written in Koji
Decision: https:// uses a client of TLS written in Koji (koji:tls, koji:x509, the cryptography of std/koji/), and only TLS 1.3. Its certificate authorities are those of the system (SSL_CERT_FILE, SSL_CERT_DIR, the usual files of Linux, macOS and the BSDs), with Mozilla's list built in for the systems that have none, as Go does. Its ciphers are AES-GCM with the instructions of the processor (AES-NI and PCLMULQDQ, or those of ARMv8, the only C of the cryptography, in the prelude) when it has them, and ChaCha20-Poly1305 otherwise, as Go, BoringSSL and rustls choose; never AES by tables. The end of the data is close_notify only: a connection closed without it is reported as maybe truncated, as rustls reports it, and std/http.kj ignores that after a body whose size it knows. A server without TLS 1.3 is refused with a message that says so.
Why: TLS 1.3 is what most servers speak, and what has the least to get wrong (no renegotiation, no CBC, no exchange of keys by RSA, the whole handshake after the ServerHello encrypted): one version, a smaller client and a smaller surface of attack. The cost is known: a few servers have only TLS 1.2 (in 2026, registry.npmjs.org and registry.yarnpkg.com), which the client in Rust (ureq) reached and which Koji does not.
Alternatives: TLS 1.2 too, in the modern subset rustls keeps (ECDHE, AEAD ciphers, Extended Master Secret, no renegotiation), which reaches every server but doubles the protocol to keep right; the TLS of the system (OpenSSL, Secure Transport), as Python and Node use it, which ties Koji to a library in C.
Tests: test blocks next to the code, one assert
Decision: a test is a block test 'name' { ... } at the top level of a file, next to the code it tests, which it sees whole (private functions and types included). test is a word only there (followed by a string, at the top level): elsewhere it stays a name. A test is checked like a function and left out of a program; kojic test compiles each file with its tests and runs each test in a process of its own, after the top-level statements of the file, showing what a failing test printed. A test fails when it panics. The one function for checking is the built-in assert(condition) or assert(condition, message): the compiler keeps the condition as written, and for a comparison the values of its two sides, shown when it fails (assertion failed: total == 10, left: 9, right: 10); a program's own assert takes its place.
Why: a test is written where its code is, with nothing to import or name (Zig's test blocks, Rust's tests in the same file), and can test what is private. One assert that explains its failure, as pytest does with Python's assert, says as much as a family of assertEqual, assertTrue... with nothing to learn. A process for each test gives isolation without catching panics, which Koji does not do: a test cannot break the next one, and the global state starts again. A contextual word breaks no program that uses test as a name, and the compiler itself does.
Alternatives: test functions in files of their own (math_test.kj, as Go), which see only what is exported; a testing module with test(name, () => { ... }) (as Deno), whose closures could not change the variables around them; several assertion functions (assertEqual, as JUnit or unittest); all the tests of a file in one process, faster but with no isolation after a panic.
The tests of Koji are written in Koji
Decision: the test suite is written in Koji and run by kojic test (build/kojic test test/*.kj, after bootstrap/build.sh): the programs that only print are cases in files of text (test/cases/: a program, its input, what it must print, the error that must end it, the other files it needs), run several at a time by tasks; the rest are test blocks (servers, pipes, foreign functions, the cryptography of the library called by its names, the snapshots of the stages, the fixed point). Like the generators of the tables of the library (bootstrap/generate/, which read the files of their sources: the Unicode Character Database, Mozilla's authorities), nothing of Koji is in another language.
Why: building and testing Koji then needs Koji alone: no cargo, no Rust, no C compiler (the tests of the cryptography were programs of C). The tests use the language as its programs do, and what they need of it (running programs with an input, TCP for the servers of the tests) became the standard library and foreign functions. The cases as text are read at a glance, need no quoting of their programs, and are what the tests in Rust gave the compiler (they were recorded from them, the same programs and results).
Alternatives: keeping the tests in Rust, which needs cargo to contribute; each case a test block with its program in a string, which would need its quotes escaped; a file per case, hundreds of small files.
Errors show their column and the line they are on
Decision: a compile error gives the file, the line and the column of what is wrong, then that line with it marked: type error: hello.kj:5:13: argument 1 of 'greet' must be string, found int, then 5 | print(greet(42)) and ^^ under 42. The mark is under the value, the argument or the element at fault, not the whole statement (the parser gives each node where it starts and ends). A panic keeps its form, panic: main.kj:12: message: the program running has the line only.
Why: a beginner, and the editor, see at once where the error is, as with gcc, rustc and Elm; file:line:column: is what editors and terminals turn into a link.
Alternatives: the line only (as before), which leaves the reader to find the place in it; a long explanation with each error (as Elm), more work for each message, for later.
The check goes on after an error, statement by statement, and reports every error, up to 20 (3 errors at the end). A variable whose declaration failed is declared all the same, with its written type or an unknown one that no later use reports, so that one mistake gives one error, not a cascade; an imported module with errors stops the check, since every name taken from it would be an error too. Syntax errors stop at the first: the parser cannot know where the program goes on.
The language server is a command of the compiler
Decision: kojic lsp is the language server (the Language Server Protocol, on the standard input and output), written in Koji in the compiler (compiler/lsp.kj): it checks the text the editor holds, saved or not, with the checker of the compiler, and gives every error as it is edited (the line, the column and the width of each), the type of what is under the cursor (from the types the checker kept, even when the file has errors elsewhere) and where a name is declared (a variable, a parameter, a function, a type, in the file or in the module it is imported from). The editor extension of VS Code (koji-vscode, its own repository; the TextMate grammar it copies stays here, in editors/textmate/, for the other editors too) runs it.
Why: the editor sees exactly what kojic check sees, with no second implementation of the language to keep in step; one program to install. Checking a whole file at each change is fast enough for the files of Koji (the compiler checks its own 15,000 lines in 0.08 s).
Alternatives: a server of its own, apart from the compiler (as rust-analyzer), which can be incremental but repeats the language; a server in another language (TypeScript), which would need the compiler anyway.
Concurrency: light tasks in groups, on one thread
Decision: concurrency is the task module: a task runs a function on a stack of its own (1 MB the system gives as it is used, with a guard page below), and code in a task is ordinary code, with no async or await. In this first phase, the tasks take turns on the one thread of the program: a task runs until it waits (for a timer, the network, another task) or has run long enough, and the runtime (koji:task, written in Koji, with the switch between stacks and the poller of the system in the prelude: epoll on Linux, kqueue on macOS and the BSDs) then runs the next one, and waits for the timers and sockets when none is ready. Tasks are structured: each belongs to a group (task.group), which waits for all of its tasks before it returns, so no task outlives the code that started it. Tasks share data through channels (task.channel, with a capacity, 0 for a meeting) and shared values (task.shared, whose update no other task interleaves); everything else a task uses is a copy, as in any closure. A program where every task waits for another stops with a panic that says so.
Why: Koji is for scripts, CLIs, courses and small services: sending requests at the same time, and later serving them, is what they need, and lightweight tasks with sequential code do it without dividing functions into two colors (as async does in JavaScript, Rust and Python), as Go and Erlang show. One thread first means no data races at all, and the runtime stays as it is (counts of references that are not atomic, the caches of the library): code that does not wait is never interrupted. Structured groups (as Swift, Kotlin and Trio have them) make a task's life visible in the code: no task is lost or left running, and an error has a place to go. Shared values cover the simple case (a counter, a cache), channels the coordination of tasks.
Alternatives: async and await (JavaScript, Rust, Python), which color functions; threads of the system (Java before 21, C), heavy and with data races; tasks over several threads at once (Go), the second phase, which needs atomic counts of references and a runtime safe for threads; tasks without groups (Go's go), which can be forgotten.
The value of Shared is the one reference of Koji, which its copies share: the cells of std/task.kj are lists of one item that only sharedGet and sharedSet of koji:unsafe read and change in place. The library is compiled without the shadow stacks of x86-64 (CET), which forbid switching stacks.
No call blocks the other tasks, and a task that computes is preempted
Decision: the calls that block (reading and writing files, directories, input(), waiting for a program, finding the addresses of a host, printing to a pipe that is full or a long text) are made by threads that help, while other tasks are alive: the task waits for its call as for a socket, and the threads run C only, never Koji, taking jobs from a pipe and giving them back through another that the poller watches (no locks). A task alone makes its calls itself, as before; with others alive, print writes at once a text of up to 4 KB to an output that takes it without waiting (poll, which a pipe then takes whole), and the tasks print one at a time, so their texts neither mix nor change order. A task that computes without waiting is preempted: the generated code checks a flag at the end of each turn of a loop and where a function that calls others starts, which a thread sets every 10 ms while several tasks are alive; the task then lets the others run (and the scheduler looks at the sockets and timers), and between two checks, nothing changes for it. Neither the library of the runtime nor the module task has the checks, so their steps stay whole. There is no yield for a program to call.
Why: a task never holds the others up, whatever it does: a server stays responsive with a handler that reads a large file or computes for a second, and nobody has to ask how often to give way, which is the runtime's job, as dropping values is. The checks are the cheapest way to preempt (a load and a branch, lost in the noise of the benchmarks), and they come where the generated code is in a known state, unlike preemption by signals. Threads that only run calls of C keep the runtime without data races (as libuv does for Node). The word yield stays free for generators, which produce values and have nothing to do with tasks.
Alternatives: task.yield() (as Go's runtime.Gosched, Kotlin's yield), which leaves the question to the programmer; preemption by signals at any instruction (Go since 1.14), which needs the stack maps of a garbage collector; asynchronous system calls (io_uring), for Linux only; a resolver of DNS written in Koji (as Go has), which misses what the system configures.
Anonymous functions take their parameter types from where they are written
Decision: a parameter of an anonymous function written without a type has the one of the function type expected where it is written (task.group((tasks) => ...), immut f: (int) => int = (x) => x * 2); with no such type, it needs one.
Why: the type is already written once, in the signature; writing it again at each call is noise, the more so for the functions given to tasks. It is what TypeScript, Kotlin and Swift do.
Alternatives: always writing the types (as before), clearer for a reader without an editor but heavy; inferring them from the body too (as ML), which gives errors far from their cause.
The HTTP server: a handler from requests to responses, a task for each connection
Decision: http.serve(port, handler) serves HTTP/1.1: the handler is a function from a Request (method, decoded path, query and headers by lower-case name, body) to a Response (the type the client returns, with its headers), built by hand or by http.text, http.html and http.json. Each connection is a task of the group of serve, kept open for further requests (keep-alive, pipelining) until the client closes it, asks to, or leaves it idle 60 seconds; a request has 30 seconds to come. The server protects itself on its own: 400 for a request with both a length and chunks (smuggling), for white space before the colon of a header and for folded headers; 413, 431, 501 and 505; header values of the handler with a line break have it replaced, so they cannot add headers. It listens on every IPv4 address. A panic in a handler ends the program.
Why: a function from a request to a response is the smallest server to learn, and the whole of what small services need (as Go's http.HandleFunc or Python's WSGI, without the objects); with tasks, the handler is ordinary code that may wait without holding the others up. The protections and the limits are the defaults that a beginner would not know to add.
Alternatives: a router and middleware in the module (as Express), which belong in a library on top; a handler that writes to a stream (Go's ResponseWriter), for streaming bodies, later; recovering from a panic in a handler with a 500 (as Go), which Koji, without catching panics, does not do.
Foreign functions: a block that declares them, with safe types only
Decision: foreign 'library' { ... } declares the functions of a library of the system (linked with -llibrary) and its handles, at the top level of a module. A function takes and returns only values that cross safely, converted at each call: integers (CInt and CSize checked to fit), floats, booleans, bytes, strings (a copy ending with a zero byte; one returned is copied, None for NULL), byte lists (a copy of their bytes), and handles: type Database = handle(sqlite3_close) is an opaque, move-only type whose destructor runs when it is dropped, which only a foreign function can give. A mut parameter is one the function writes. A blocking function runs on a thread that helps while other tasks are alive, and the functions of its block one at a time. The compiler writes, for each block, a module of Koji (compiler/foreign.kj): the function of the library, an extern with the types of the system, and a function of Koji around it that converts and checks, checked like the standard library.
Why: safety by default: what Koji code receives from a library is a copy or a handle it owns, so no pointer into the memory of Koji escapes, and a handle is closed once, at the end of its owner, like a file. The declarations are complete in Koji, defined by the calling conventions of the system rather than by C: they need no header nor C compiler, so they stay the same when Koji generates machine code itself, the aim being to depend on no other language, C included. Writing the conversions as Koji, in a module, reuses the checker and the code generator as they are.
Alternatives: checking the declarations against the C headers (which the C compiler could do while Koji generates C, but which ties the language to C; it may come back as a tool); a C file compiled with the program for what the safe types do not cover (which ties programs to a C compiler; structs and callbacks will come to the block instead); raw pointers in the language (as Rust's unsafe, or Go's unsafe.Pointer), which a beginner would reach for first.
Packages: repositories of git, tags as versions, minimal version selection
Decision: a directory with a koji.mod is a module (module github.com/ana/agenda), which requires others by the paths of their repositories and the least versions they work with (require github.com/bia/toml v1.4.0); a version is a tag of semver. The versions a program builds with are chosen by minimal version selection: of each module, the greatest of the versions that the program and its packages ask for. From v2, the path of a module ends with its major version (github.com/bia/toml/v2), a module of its own. kojic downloads the modules with git into a cache, and koji.sum keeps the SHA-256 of the files of each version used, checked at each build. A module imports another by its path; the path of a module is the file named as its last part (toml.kj), a path inside it the file of that path. A package with foreign functions is built only when the program allows it (allow foreign); replace takes a module from a directory. kojic init, kojic add and kojic update write koji.mod.
Why: no registry to run or trust: a package is a repository, published by a tag. Minimal version selection gives the same build to everyone without a lock file or a solver, and a release of a dependency changes nothing until the program asks for it (as Go). A major version in the path lets two of them live in one program and keeps an update from crossing an incompatible change. The digests catch a moved tag or a changed copy. Foreign functions are the one way a package can do what Koji cannot check, so the program says which ones it trusts, as the safety by default asks. git is a tool, like the C compiler, not a language: any server it reaches works, private ones included.
Alternatives: a central registry (npm, crates.io), which someone must run, and names that can be taken; ranges of versions solved for the newest (npm, Cargo), which need a lock file to build the same twice; one major version per dependency, simpler to explain, but which blocks a program whose dependencies need two; downloads over HTTPS by the client of Koji, for the hosts whose archives it knows only.
The command line: declarations written as a help shows them
Decision: the module cli declares the options and arguments of a program with the text a help shows for them ('-t, --times <n>', '<name>', '[files...]'), on a command whose methods each return it with one more declaration (cli.command(...).flag(...).argument(...)), and its subcommands; cli.parse reads the arguments, shows the help (made from the declarations, with the fallbacks) for --help, and for a mistake its message, the closest name meant (did you mean '--loud'?) and how to get the help, ending with the code 2. The values are read by name (parsed.flag('loud'), parsed.value('name'), parsed.number('times'), parsed.list('files')); an integer option is checked when the arguments are read. The rules of reading are those of most programs (GNU and POSIX): options anywhere, --name=value, -t3, flags together, --.
Why: a declaration that looks like its help is read at once, also by someone who never saw the module (as commander.js does); everything a small program of the command line needs, the help included, is in one place. Values by name keep it without reflection or macros, which Koji has not.
Alternatives: a record whose fields are the options (as Rust's clap with derive, Python's typer), which needs reflection; a function per kind of declaration with separate short and long names (as Go's flag), more to write; system.args() alone, as before, which leaves the help and the mistakes to each program.
Machine code: the compiler writes the object file itself
Decision: the native backend (compiler/native.kj, the only backend of kojic build and kojic run) generates the machine code of the IR and writes the object file itself (ELF for Linux on x86-64 first), which the linker of the system links with the runtime library: no C is generated nor compiled. It grows in steps, each checked against the C backend (the same output and panics, under valgrind): the integers, booleans, chars and strings, control flow, calls and globals first; then every type (records, lists, maps, closures, decimals, floats); then the allocation of registers; then arm64 and Mach-O; then the library of the runtime itself, whose C (the prelude) becomes Koji, foreign declarations of the system and code of the generator; then a linker of Koji's own. It became the default once it covered the language, its code within 1.4 times the instructions of gcc's, so that Koji stops needing C as soon as it can; the C backend stayed for the platforms the native one did not reach yet, and to compare the two. The C backend is retired: once the native backend reached Linux, macOS and Windows on their processors and the seed was executables, cgen.kj, the C prelude (runtime/prelude.h) and the seed in C went; a platform the native backend does not reach is an error of kojic build, never a wrong program. The native backend calls the functions of the runtime, the library exporting the descriptions of the items of lists; it inlines them where speed asks for it (the counts of references, a borrowed parameter that is neither retained nor released, a record made from the pool), and inlines the small functions of the program itself.
Why: Koji aims to depend on no other language, C included: the compiler is written in Koji, and with its own code generation it needs nothing of C to build a program but, for a while, the linker. Writing the object file (rather than assembly for an assembler) keeps the whole path in Koji. Growing it under the C backend keeps Koji usable and correct at each step, with the C backend as the reference of every behavior.
Alternatives: LLVM (excellent code, but a large dependency in C++); a backend of assembly text for the system's assembler, one tool more; replacing the C backend at once, which would leave Koji broken for months.
Code for one system: a condition on system.os that the compiler decides
Decision: code that exists only on some systems (the poller is epoll on Linux and kqueue on macOS, a struct of C has other fields on each) is written with system.os as the condition of an if or the value of a match: match (system.os) { Linux => ..., MacOS => ..., _ => ... }. The compiler knows the system it compiles for, so the condition is a constant: the native backend compiles the branch of that system only, and the functions of C that the other branches call are not even linked. (While the C backend lived, its output, which had to build on every system, kept every branch under a condition of the preprocessor.) Anywhere else, system.os is a value, as before.
Why: the runtime written in Koji calls the system, whose functions and records differ from one system to another; a test at run time would still link the functions of every system, which do not all exist on any of them. Using the function that already says which system it is keeps the language as it is: no new syntax for a decision of the build.
Alternatives: files for each system (poller_linux.kj, as Go), which duplicates whole modules for small differences; tests at run time with every function of C linked weakly (a missing one is null), which moves the failure from the link to the call; attributes or a preprocessor of Koji's own.
Instructions of the processor: functions of koji:unsafe that the compiler knows
Decision: the few things of the runtime that are instructions of the processor and nothing else (switching between the stacks of tasks, the rounds of AES and the products without carries of GHASH, the requests to valgrind) are functions of koji:unsafe that the compiler knows, a small and closed set (switchStacks, taskEntry, floatControl, valgrind, aesRound, aesLastRound, carrylessProduct, reverseBytes and aesAvailable so far): the native backend writes their instructions as machine code, inline (but the switch between stacks, a function of the library it writes), for each processor.
Why: they cannot be written with the operations of the language, and they are few; functions keep their uses checked by types and portable, each instruction written once for each processor in the compiler.
Alternatives: blocks of assembly in Koji (asm), which can do anything but are checked by nothing and written in the syntax of each processor; leaving them in C or in files of assembly, which keeps a language other than Koji in the runtime.
The threads of the runtime
Decision: the threads that help with the calls that block, and the one that ticks for preemption, run machine code that the native backend writes into the library (as it writes the switch between stacks): their loops, the job of each call that blocks, and running a job; no Koji runs on them, and all the Koji of a program runs on its one thread. What runs on that thread (making their pipes, starting them, giving them jobs, starting and stopping the clock) is Koji (koji:prelude).
Why: in Koji they would need code of Koji that runs on another thread, where it must neither allocate nor count references, with nothing to check it; written by the backend, they are a few loops around calls of the system, with no Koji in them, and the library needs no C compiler.
Alternatives: functions of koji:unsafe for the address of a function and a call through an address, with the rule, unchecked, that the code of other threads allocates nothing; keeping them in C (as at first: the library then needed a C compiler for them).
The library of the runtime built by the compiler
Decision: the native backend compiles the library of the runtime (kojic library): its object alone, in the archive that programs link, with no C compiled. What the C prelude of the C backend wrote in C is Koji in the library (koji:prelude, std/koji/prelude.kj, a module only kojic library compiles), and machine code the backend writes itself: the switch between the stacks of tasks, where a task starts, the heads of the pool, the descriptions of the items of the built-in types, and the few functions that call through an address (of a description, of the type of a record, of a closure), and the threads of the runtime (see "The threads of the runtime"). The seed of the bootstrap is the compiler's own executables (see "The compiler is written in Koji"), which build the library natively before the compiler.
Why: the library is Koji: building it with the compiler takes the C compiler away from it, as the backend did for programs; the archive is written by the compiler too (see "A linker of Koji's own").
Alternatives: committing the object of the library (a binary in the repository, one for each platform); keeping the library compiled through C.
A linker of Koji's own
Decision: kojic links the executables of the native backend itself (compiler/linker.kj): the object of the program and the objects of the runtime library (an archive of ar, which kojic library writes too, with the table of its symbols), into an executable of ELF that is not position-independent and that the dynamic loader links with the C library of the system (libc.so.6, libm.so.6; their functions through stubs and a table of addresses filled when the program starts, BIND_NOW), its entry calling __libc_start_main with main as the start files of C do. A symbol that neither the objects nor those libraries define (read from the libraries of the system) is an error of linking. The shared libraries of foreign blocks are linked the same way: foreign 'sqlite3' is libsqlite3.so, found in the directories of libraries of the system or of LIBRARY_PATH (through the script of the linker it may be, as libc.so is), named for the dynamic loader by its own name (its DT_SONAME, libsqlite3.so.0), its symbols read to check the program's. For Windows, the functions of a foreign block are imported from the DLL of its library (foreign 'user32': user32.dll), which need not be there when the program is built. The linker of the system (KOJI_CC, cc) stays for what Koji's does not do: a library found only as an archive (libfoo.a), the libraries of foreign blocks for macOS (where the install names of dylibs are in an SDK that Koji cannot check here), and KOJI_LINKER=system; KOJI_LINKER=koji makes a failure of Koji's linker an error rather than a fallback.
Why: with it, building a program on Linux needs nothing of the toolchain of C: no compiler, no linker, no ar; only the shared C library, which every program of the system uses. Linking the objects Koji makes, with the few relocations it uses, is a small and closed job; the executable also comes faster (no process for the linker), the same from the same program.
Alternatives: static executables (the C library of the system is not made to be linked statically everywhere, and its loader and its locales want a dynamic one); a position-independent executable (relocations of its own data at the start, for nothing an executable of Koji needs); keeping the linker of the system (one more program of C to build anything).
A machine IR between the IR and the processors
Decision: the native backend is split in three. The lowering to machine code (compiler/mir.kj and what lowers to it) turns the IR into a machine IR that knows no processor: virtual registers without number, of words and of floats; loads and stores of 1, 4 or 8 bytes at a register plus an offset; operations of two operands (a register or a constant), checked ones that panic on an overflow; comparisons and branches; calls by the System V and AAPCS64 conventions, which the machine IR states as arguments and results in registers of each class; slots of the frame; and the few instructions of the processor of koji:unsafe. All of the semantics of Koji (counts of references, moves, borrowing, records, lists, the pool) is decided there, once. A register allocator (linear scan over the ranges of liveness, the registers that calls keep for the values alive across a call, the others first, spills to slots of the frame) gives each virtual register a register of the processor. Then an emitter for each processor (x86-64, arm64) encodes the instructions, with the relocations of its objects; the object file of ELF (and later Mach-O) is written for either.
Why: the first native backend wrote the bytes of x86-64 in some 400 places, the semantics of Koji between them; arm64 would have meant a second copy of all of it, and its allocation of registers (5 registers kept across calls, the rest of the values through rax and the frame) leaves the code at 1.3 to 1.7 times the instructions of gcc's. With a machine IR, the semantics is written once, the allocator works for both processors, and an emitter is a small closed job (each instruction of the machine IR, a few encodings).
Counting references: a managed value is a word in a virtual register, like an int. The lowering keeps the rules of the other backends (see the reference counting above): which locals a function owns (all managed ones but the borrowed), where each is read for the last time (liveness.kj), and, forwards over the blocks, which may hold a reference at each point. A local is released only where it may hold one (at its last read, when it is given another value, when the function returns), and set to none at the end of the paths where it holds none when they meet others where it does, so the function zeroes nothing when it starts but the locals a TakeIns empties. MRetain and MRelease are instructions of the machine IR (a test, an increment or a decrement, and the call that frees at the last reference, which keeps every register like the check of preemption), so that the allocator keeps the values alive across them in any register.
How it came: function by function, beside the first backend in the same object and by the same convention (a function the machine IR did not lower yet went through the first backend), until it passed every test, lowered every function of the compiler, the library and the programs of the tests, and its code was faster (5% fewer instructions for the compiler and the library built with it); then it replaced the first backend, whose code for each instruction went (native.kj keeps the object, the data and the code of the runtime). Then arm64 came as an emitter (compiler/a64.kj, KOJI_TARGET=arm64): AAPCS64 (a U128 from an even register, a pair that does not fit on the stack whole with the arguments after it), the relocations of ELF for AArch64 (calls and addresses of the code relocated rather than patched), checked under qemu; the compiler built for arm64 builds the same compiler for x86-64 and the same object for arm64 as the one on x86-64. The functions the backend writes itself (the frees of records, the jobs, the stubs and threads of the runtime, main) are functions of the machine IR (compiler/mircode.kj builds them), so the emitter of each processor encodes them; only the switch between stacks and where a new task starts are code of each processor. Those two are written for arm64 too (the layout of stackStart in std/koji/task.kj), so the native library builds for it (KOJI_TARGET=arm64 kojic library): the compiler built for arm64 with it, under qemu, builds the same compiler as on x86-64. And the linker of Koji links its objects (_start and the stubs of arm64, its relocations, segments aligned for pages of 64 KB): built for arm64 by Koji alone, the compiler, under qemu, builds the same compiler for x86-64 as on x86-64, and itself for arm64 byte for byte. AES-GCM goes by the instructions of arm64 (AESE, AESMC, PMULL) where the system says it has them (getauxval): the cryptography of the library, TLS included, gives the same results under qemu as on x86-64. On Linux and arm64 the native backend is the default, as on x86-64 (KOJI_TARGET=x64 or arm64 compiles for the other). For macOS on arm64 (KOJI_TARGET=macos-arm64), the backend writes objects of Mach-O (compiler/macho.kj: the names of C with an underscore, a relocation to a section plus an offset as one to a symbol private to the object, an offset from another object's symbol as ARM64_RELOC_ADDEND), the native library as an archive of BSD (its table __.SYMDEF SORTED), the arguments after the fixed ones of a variadic function on the stack (Apple's convention), errno by __error, environ by _NSGetEnviron, and the system's linker links them (cc on macOS). Checked here by lld's linker for Mach-O, which links the compiler for macOS with a libSystem of the symbols it needs, none of Linux; nothing here runs a program of macOS, so its code is checked as that of arm64 on Linux (the same emitter). Koji's linker for Mach-O (compiler/machlink.kj) links them too, the default for macOS: a position-independent executable (segments of 16 KB, stubs and a table of addresses of libSystem bound when it starts, the addresses in the data rebased, by the opcodes of LC_DYLD_INFO_ONLY; LC_MAIN) closed by the ad-hoc signature macOS asks of arm64 (a CodeDirectory of the SHA-256 of each page, written by the linker). Checked against what lld's linker makes of the same objects: the same commands, the same signature (its hashes recomputed), the same rebasing. For macOS on x86-64 (KOJI_TARGET=macos-x64), the same objects with the relocations of x86-64 (X86_64_RELOC_BRANCH, SIGNED, GOT_LOAD, their addends in the code, a displacement counted from the end of its field) and Koji's linker with the stubs jmp [rip + entry]; checked the same way, and every reference of the code (to the code, the constants, the data, the stubs) the same in both linkers' executables of the compiler, each of its texts at the start of a string. Left: a run on macOS, which nothing here can do.
Windows: for Windows on x86-64 (KOJI_TARGET=windows-x64), the code of Koji keeps the convention of System V among its own functions (both sides are Koji's), and only a call of a function of C, of a DLL, follows the convention of Windows (MVariadic with all its arguments fixed: the first four in rcx, rdx, r8 and r9 or xmm0 to xmm3 by their positions, 32 bytes above the arguments for the callee): Windows keeps across calls every register System V keeps and more (rsi, rdi, xmm6 to xmm15), so the allocator need not know. The few places where Windows calls Koji (the start of a thread) go through an adapter that saves what Windows keeps. A frame of a page or more touches its pages in order (the stack grows through a guard page), and the switch between the stacks of tasks saves the limits of the stack in the block of the thread (StackBase, StackLimit), which the functions of the system check. The objects are of COFF (compiler/coff.kj), and Koji's linker writes the executable of PE (compiler/pelink.kj: the imports of ucrtbase, kernel32, ws2_32 and bcrypt, stubs jmp [rip + entry], the relocations of the data for the loader), from the entry koji_windows_start (the arguments from the C library). The library has branches of Windows where POSIX is not there (koji:windows, and match (system.os) elsewhere): the stacks by VirtualAlloc, the clock by QueryPerformanceCounter, sleeps by Sleep, the threads by CreateThread, the pipes and files by the C library of Windows, the directories by _findfirst64, programs run by CreateProcessA, the sockets by Winsock (which never wait, its own errors), the poller by WSAPoll (the pipe of the jobs looked at between short waits), random bytes by BCryptGenRandom, and the authorities of TLS from the list built in. A comparison with system.os known on the platform is a constant, so that nothing of the global is read before the modules start. Checked by running the programs under Wine: every test of the language (KOJI_TEST_WINDOWS=1 cargo test --test language), tasks, threads that help, TLS 1.3; and the compiler itself: built for Windows, it builds itself there the same, byte for byte, as the compiler on Linux builds it for Windows, so that Windows has a seed too (bootstrap\build.cmd). The names of POSIX that the C library of Windows has with an underscore (getcwd, _getcwd) are imported so, for the foreign blocks; std/path.kj takes \ and drives (C:\) as well.
Values of two words: a decimal or a U128 is a pair of virtual registers (low, high), passed as two words by the convention; MPairs tells the emitter which arguments of a call start a pair, so that one that does not fit whole in the registers goes on the stack whole, as System V wants. MWide computes the arithmetic of 128 bits (an add with its carry, the product, the shifts, the unsigned comparisons), which each processor encodes in a few instructions.
Calls of cold blocks: a value alive across a call needs a register that calls keep (5 on x86-64) or the frame. The panics and the slow paths of an instruction (a list shared when an item is set, a string without room, the pool empty) are blocks of their own laid out after the block of the IR they come from, so inside its loop: had their calls counted, every value alive in the loop would cross a call, and most of them would go to the frame. They do not count for the allocator; such a call saves the registers that calls do not keep and that hold a value alive across it (not its results) around itself, as the check of preemption does: the cost is on the path rarely taken.
Alternatives: a second backend for arm64 copying the first one (the semantics in two places that drift apart); improving the allocation inside the first backend (x86-64 only).