Working on raz #
This file is for whoever prepares a defect fix or a documentation fix
for raz, a person or the AI tool helping them. CONTRIBUTING.md says
how one is offered: through a GitHub issue, never a pull request, with
a Signed-off-by: line, and with you answering for every line a tool
wrote.
Read first #
doc/raz.md, the report, volume 1: it defines the language.doc/raz-lib.md, volume 2: the library.doc/compiler.md, the guide to the compiler, file by file, and, under "This compiler's choices", what the report leaves to an implementation.STANDARDS.md: how raz is written here.doc/report-history.md, what each version of the report changed;PLAN.md, what is left before 1.0.
Build and check #
make raz # the compiler, ./raz, built from src/ by the release BOOT names; its library is lib/
make check # every example and test against its expected output
make fmt # lib/, examples/, tools/, site/, src/ and src/test/ are formatted
make bench # must report at least 400,000 lines a second
make prev-bench # the compiler's speed against BOOT's: more than a tenth slower fails
make fixpoint # BOOT's release builds src, that builds src, that again: the last two agree
make loc # lines of the compiler, by rloc; the last, code, is the budget
make kernels # test/bench's kernels against FPC -O2's twins (needs fp-compiler)
make gdb # gdb stops at a line of a test and names it
make stress # 40,000 native threads; make check runs a small version
make alpine # test/libc in an Alpine container, over musl (docker)
make pdf # the report as PDF
make install # raz on PATH (PREFIX=..., ~/.local by default); make uninstall
On a Mac (docker needed):
make docker-check # make check in a linux/amd64 container; under Rosetta it skips 9 tests, each saying why
make mac-check # on Apple Silicon: test/mac and conform/run cross-compiled in the container, run natively, and lldb
make lldb # gdb's test, for lldb, on a program made for darwin-arm64
make check also runs tools/Doc.raz and tools/Pretty.raz, the
report's whole examples, the tutorial's (doc/raz-tutorial.7, each
against the output the page shows after it, its status in test/man),
each library module's own main (its test), and the vim and emacs
support headless where they are installed (vim, emacs-nox): each
example must indent back exactly as written.
test/libc runs its programs over lib/Sys.raz, the C one, through a
link in its mods; make check runs them again with -libc musl
where musl's loader is installed (musl, musl-tools), and compares
tools/cabi.c's output, with cc and musl-gcc, with
lib/CAbi.linux-x86_64.raz, which is never edited by hand.
fpc is needed only for make kernels; rloc only for make loc.
Tests #
The conformance suite, conform/, holds what any implementation must
do; test/ holds what is this compiler's alone: its messages, its
limits, its target. A test goes in conform/ when the report alone
fixes its output and status, and keeps to the language and the library
a target can't leave out. conform/check.sh and test/check.sh say
how each is laid out.
test/run/X.razwithX.expect(standard output), and optionalX.status,X.stderrandX.args; modules they import intest/run/mods.test/err/X.razmust fail to compile with the text inX.err(all of standard error), given the compiler options inX.argsif there is one.test/msgholds this compiler'sX.stderrandX.errfor the programs inconform/.test/pretty/X.razmust format toX.expect.examples/works the same way. Examples,test/libc, the tools and the library's tests are compiled with-Werror: no warnings.- A test or example without
.expectis only compiled (examples/Window.razneeds a display).
What a patch must keep #
- BOOT.
src/, the compiler, is raz, built by the release the fileBOOTnames, by its tag and its tarball's SHA-256.src/boot.shfetches that tarball intobuild/boot/, checks the sum and puts itsrazthere with today'slib/beside it. Sosrc/, and the modules oflib/it imports, use nothing that release doesn't know.lib/Sys.razandCAbiare read for an interface check when BOOT's release buildssrc/, so they keep to its language too. - Checks.
make checkandmake fmtpass; the fixpoint holds. - Budget. At most 18,000 lines of code in
src/(blank and comment lines not counted, asmake loccounts them; the compiler is about 16,700 now). - Speed. At least 400,000 lines of raz a second (
make bench), and no more than a tenth slower than BOOT's release (make prev-bench). - The report stays under 40 pages as
make pdfrenders it. - Clarity first, before speed and size: the compiler must be plain
code that a beginner can follow. Each unit opens with what it is,
what it depends on and what it leaves out; comments say why, most of
all where the code is obscure or needs a compiler writer's knowledge.
Every rewrite of the code the compiler makes is explained where it is
done, with the code before and after, and in
doc/compiler.md. - Parameters (
STANDARDS.md): a value,var,const, or, only when it is kept, a pointer; noADRwherevarserves. - The guide stays true. The parser reaches the machine only through
Gen(src/Gen.x86_64.raz;src/Gen.arm64.razfor darwin-arm64), and the executable's format only throughExe(src/Exe.linux.raz,src/Exe.darwin.raz). When the compiler changes, keepdoc/compiler.md's table of lines and its routine names true. The arm64 back end is built only into a compiler for darwin-arm64 (src/cross.sh); a change must leave x86-64's output as it was unless it means to change it. - Documentation matches the report's plain, short prose.
src/ has one module to a unit of the Pascal compiler it was ported
from, with the same names. That compiler, the seed, built src/ up to
v0.19.0 and is in git at that tag (seed/); a comment in src/ that
names "the seed" means it. Each release since was built by the one
before it.
sbcl/ is an example, not part of the build: a second compiler, in
Common Lisp, written from the report alone and kept as a reference
(sbcl/README.md).
The freeze #
The language is frozen. It changes only to mend a defect: the compiler or the library doing other than the report says. The library's core (appendix A) changes only to mend a defect or plug a very big gap; the rest of the library grows only conservatively, by what breaks no program. Proposals and new features aren't taken, and a wish isn't recorded.
That a word is reserved, however common (code, to, packed,
forward, set, ...), and that module names share one space with the
library's (report 3, "Names are flat"), so that a program's module may
clash with one of the library's, are settled. Don't raise them, as a
defect, a proposal or an awkwardness, in raz or in a program written
in it: a name that is taken is renamed.
Readings of the report already taken #
These are how the compiler behaves, and a fix keeps them. Where one names registers or instructions, it describes the x86-64 back end.
- A named and an anonymous type built alike are the same;
type A = Bis an alias. A hex literal above high(I64) is aU64. p^.fandp^[i]are explicit;p[i]on a pointer to an array is an error. A statement starts with a name, so^T(x)^ := visn't one.- A
BOOLconverts to every integer type, 0 or 1, inconvert's integer branch with no code of its own, as aBOOL's value is already 0 or 1; of a constant it folds. No integer toBOOL: the error's help saysx # 0. for: the step is a constant; the loop never steps past the limit, so it can't wrap; afterwards the variable holds the last value it took; the body may assign it.- Two results that are each a single value of a word at most, not a real, come back in rax and rdx, and so does a slice, as from a code procedure; other several results, and records, through a hidden pointer.
NEW(p, a)requiresa: ^Mem.Allocatorand calls itsallocfield: the one library type the compiler knows by name.Rt.failis found by name too.- A set literal takes its type from its first name when nothing else
gives it;
{}needs a known type. String constants can be indexed and sliced. - A deferred statement's frame slots stay reserved for the rest of its procedure.
- A generic instance first used in a procedure, or in the module's
body, is made before that code begins:
instancesForreads ahead for the import's name, since code can't be compiled inside other code. Its arguments are read then, in the scope there. - A program uses C if a module it uses (not one read only for an
interface check) has an
externor alinkof a library.staticinlink static;is not a reserved word. It is linked against glibc, or musl (/lib/ld-musl-x86_64.so.1, needinglibc.so) by-libc muslor on a machine with musl's loader and not glibc's. Sysgives minus Linux's errnos on every target;Sys.razmaps the C library's throughCAbi.errnos.- C symbols are reached through quadwords the loader fills in
(GLOB_DAT), with no PLT; the executable exports nothing. In a C
program the entry point stores C's
exitinRt.cExit, found by name likeRt.exit, before any module body runs; and, if the program hasThread,pthread_createandpthread_joininThread.cCreateandThread.cJoin, so thatThreadmakes C's threads there and serves programs with C and without. - Past an extern's parameters, a string goes as a C string and an
integer constant as an
INT. - A real constant is held as an F64; one typed
F32holds a value an F32 can hold (report 2). AnF32in a register is its low 32 bits; the upper ones are left as they fall. HeapandOutlock withLock, a spinlock that yields, since they serve C's threads andThread's alike;Thread.Mutexis a futex.- A failed check says what failed and with what values, at the place
of the offending expression. A signal is told at the place of the
statement it stopped in, or of the procedure for a stack overflow in
its prologue, and the program ends by
Rt.exit(2), after itsatExitprocedures, as a failed check does (a fault insideOut's orHeap's lock would hang there). The handlers run on a stack of their own:Rt's for the main thread, and one in eachThreadthread's mapping, made by clone or by C. Threads a program makes withpthread_createitself have none, so a stack overflow in one still ends the program by the signal with no message. - The smallest value of a signed type divided by -1 stops the program
(SIGFPE), as 6.1 says,
modasdiv; for a type narrower than 64 bits the code makes the division fault, as the machine wouldn't. In a constant it is an error, as dividing by zero is. INTwidens toI64(andUINTtoU64) wherever its value goes to one: assigned, passed, returned, ina, b := f(x)andreturn f(x), afor's bounds,INC/DEC's step, and where it meets anI64in an operator (5.10 and 6.1). Never to aU64, nor anI64back to anINT.U8,I8andI16widen toINT,U8andU16toUINT, one step only (aU8into anI64is written), and not inreturn f(x). An operator's result on them (made) doesn't widen; a conversion's does,U8(a + b)too.- A view,
v.(T), isv's item withTfor its type: nothing is copied. A variable kept in a register goes to memory for it, as forADR. Another module's variable may be viewed, and not written. A view that runs past its end fails as kind 7 ofRt.fail. - A returned slice of the frame (7.3) is caught by
Item.frame, which sees only the expression: a slice held first in a local slice variable or a record, stored, or come back from a call, isn't, and is left undefined (10). - An empty slice constant,
const E: []T = [];, is 16 zero bytes in read-only data, read as an array constant is;LENof it is no constant. A constant's slice part takes[], names a constant whole, of any module, by its type, not its text, and takes an array literal, an array constant of its own in a chunk with no name, nested to any depth. AnERRORconstant given another takes its chunk: the same error. - An array a call returns, or a record or variant holding one, and a
constparameter's temporary holding one, is a slot of the procedure (8:Sym.holdsArray,allocPerm): a slice of it may be kept to the end of the procedure, and returning it is caught only as 7.3 catches a slice of the frame. Buf.add,strandspare, andVec's growing, stop the program when the allocator has no room;roomasks first.Sys.handle's handler only notes the signal, withXCHG; it is installed withoutSA_RESTART, so a call it breaks into is -4, whichOutandFilestry again. Over C,readyandreadyFortrypollagain after EINTR only when no signalhandletook broke in.- An enumeration's name shared in its scope with another
enumeration's is written with its type; shared with anything else,
the name alone is that other declaration. A
matcharm on an enumeration or a variant (7.4) takes a case alone, found among the matched type's own whatever the scope holds, or qualified as anywhere else (Kind.done,Xml.done,Xml.Kind.done), read byqualIdent; a qualified case of another type is an error naming both. Widthfollows glibc'swcwidthbut gives a control 0, and 0 to the format characters glibc counts as 1 (Egyptian hieroglyph controls, interlinear annotation), but for the prepended concatenation marks.- A pointer's base, and a procedure type's parameter or result (4 and
5.9), may name a type declared later in its
typesection, if the name is a bare one that nothing in scope declares yet ([]Lateror[2]Laterdoesn't wait): a placeholder of formfNonestands for it until the section's end (laterType,resolveLaters), which lays several results out again. A procedure type outside atypesection, a variable's, waits for nothing. - An
externmodule variable is read-only for an importer, as the report says of every exported variable (3). - Warnings come only for a module with no errors. A procedure naming
itself doesn't use it; a type named by a pointer, or by a procedure
type's parameter or result, in its own declaration does. An
enumeration's names are never warned of, nor a generic module's
parameters; a generic module is warned of in its first instance
only. Assigning a variable whole (
x :=,a, b :=) doesn't read it; anything else does. - A
constparameter is avarone never changed: passed by address always, itsObjisVarandisConst, an item of itro. A slice of its array may be made, to read; through it the caller's array may be written, which the report says (8). An argument that isn't a variable, or is a packed field, a view or volatile, goes in a temporary of the statement.matchon aconstparameter or a constant gives names that are only read. Procedure types compareconstasvar. It is unused if never read, as a value parameter. - A variant in a constant must be typed;
const D = dot;is an error that says to write its type. Its case is read by name (constCase). - The library takes a
varfor what it changes andconstfor a record or array it only reads.src/takes anItemasconstonly where profiling shows a gain, on the paths every compile goes through; the routines that word an error take a value. (AMappassed as a value, four words, made a loop ofgethalf again as slow asconst.) A trace shows novarorconstparameter.Fmt'sformatandputkeep^Buf.Buf, sincenilis standard output there. PTR(s)is a slice's first word, read with no check; of an array or a string constant, its address, refused wherever a slice of all of it is (a packed record's misaligned field, a view, through^VOLATILE, another module's array).PTR("")is the string's place in read-only data, notnil; an empty slice cut from another keeps its place.PTR([])is an error, since[]has no type there, and its help says it would benil. A slice where its element's pointer is wanted, and a pointer where a slice is, are errors whose help namesPTR(s)andLEN(s), orp[0..n].CAS(v, old, new),XCHG(v, x)andXADD(v, x)take a variable, whose address is pushed asADR's is (atomic); a local named in one is kept out of a register, as forADR(chooseRegs). A pointer that isn't a variable,ADR(x), is refused with help, and so is a pointer variable,XADD(p, 1), whose help saysp^.import SYSTEMbinds toSym.system, no file, whose scope holdsADR,PTR,SYSCALLandARGV; it is never warned of as unused, and an alias is refused. What it fences (Item.needSystem,doc/compiler.md) is refused elsewhere:extern,link, code procedures andcheck off;are said and read on, so one missing import is one error.[]const Tis a slice type withrdonlyset: not the same as[]T, which goes into it with no code (coerce), never back (help says why). An element read through one has the item'selemRo, whichnotReadOnlyrefuses, so every write route (:=,INC, avarargument,ADR, a view) is caught in one place;COPYandFILLrefuse one as their destination (destSlice); a slice of one, or of an array read through one or of another module's array, is one (slice);BYTESof one is a[]const U8;matchon an element gives names only read. A string literal (tConstByteswhere indexed), a constant's slice (ofConst),MSGand a slice of aconstparameter's array are[]const; a constant's slice part must be declared[]const.- A procedure or a global in a code procedure (8) is found by
Parse.asmName, besideasmConst, after the procedure's labels, whichX64.findLabelsenters first: a label wins wherever it is written.call pandjmp pare E8 and E9 with a fixup,[v]rip-relative, as ordinary code's;[v]stands alone ([g + 8]is an error), a label is only for a jump, and C's names are refused, their chunk holding C's address. A variable another module exports may be written throughlea: its type isn't checked. A code procedure's body has a scope of its own, so a name not yet declared is poison there only. - The limits (10): one count,
Parse.nest, of statements, expressions and types nested, to 256; 1024 values waiting in one expression (Gen.MaxStack), past which the compile ends; the other limits areParse's constants, listed indoc/compiler.mdunder "This compiler's choices". ROL(x, n)andROR(x, n)are done atx's own width,rol al, clfor aU8, with no mask of the count: x86 takes it modulo 32 or 64, and a byte's or a word's rotation goes round modulo 8 or 16 itself. A negative count is taken modulo the width as a shift's is,n & (w - 1):ROL(x, -1)isROR(x, 1). The result is a built-in's, not an operator's, and widens (madeis false). Of two constants they fold, in a constant too.ABS(x)is ofx's type and widens (madeis false). A signed integer's ismov rax, r; neg r; cmovs r, rax, then cut to a narrow width asnegis, so the smallest value gives itself with no check, as-xdoes; an unsigned one's is no code; a real's isbtrof its sign bit in rax, asneg'sbtc. Of a constant it folds, a real's top bit cleared; of the smallest untyped constant it isneg's error, "too big for I64". A string is refused as no number.MIN(T)andMAX(T)are untyped constants, butMAX(U64)andMAX(UINT), abovehigh(I64), keep their type, as a hex literal above it is aU64. ATnot an integer type is an error;MAX(x)of a variable is "x isn't a type".CLZ(x),CTZ(x)andPOPCOUNT(x)give anINT, a constant of one whenxis. They use only baseline x86-64, nolzcnt,tzcntorpopcnt:bsrorbsfon all 64 bits (a narrow value's upper ones are zero), withjnzpast setting the count of 0 to the width;POPCOUNTbyBytes.count's sums of two, four and eight bits and a multiply by0101...H, in rax and rdx. A set'sPOPCOUNTcounts its bits the same way.SIZE(T.f)andOFFSET(T, f): the type is read byparseType, where a name before a.must be a module's, soM.Tis read whole and a.past the type is a field's,SIZE(M.T.f)too.Tmust be a record type (a variant is refused, with help); a patha.bis refused, its help naminga's type.BYTES(x): of a slice, itsPTRandLEN * SIZE(T), with no check of what holds the slice (aconstone, another module's, a field of a packed record), as forPTR; of anything else, a variable's address and size, refused, asADRrefuses, for what is only read, and, beyondADR, for a volatile variable; a constant of any kind is no variable.Tis refused by the view'snotPlain. The result keepsx'sframe, so the bytes of a local are caught bynotOfFrameas far as a slice of it is.FILL(d, x)takesdasCOPYdoes (destSlice), thenxas:=would, with:='s messages, read once before any element is written. A scalar element of 1, 2, 4 or 8 bytes is stored fromrax, a real's bits as an integer's: by a store each into an array of 16 elements or fewer, else by a loop for fewer than 16 at run time andrep stosof its width from 16 up. A constant zero into an array iszeroBytes, asNEW's zeroing is; into a slice it is stored as any scalar, which is as fast. Another element is copied intod[0]and forward from there byrep movsb. A record literal of zeros isn't seen as a zero. A const parameter's or another module's array, asd, says why it can't be changed, forCOPYtoo.seqSlicecopies no slice that is a temporary already (a slice just cut): nothingsorxdoes can change it.- A call of a procedure whose body is only
return e, with no locals, a single value its result and notryine, is made in place (Parse.inlined): the arguments read and converted as for a call, each once and in order, into temporaries the parameters are named as (avarorconstone, a record or an array, by the address the call would pass), theneread again in its own module's scope, with that module's checks. Another module's procedure and a generic instance's too; not one already being made in place, nor a call through a procedure value. The procedure has no frame: it is in neither the trap report's calls nor gdb's backtrace; a failed check ineis told ate's line, a signal at the caller's statement. A test that wants its frame calls it through a procedure value.