raz / commit 5b2189a8

commit
5b2189a84659cb8970dc37a46889d13928dd83bf
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ae0cca00
PROPOSALS: undefined behaviour, any C library, PTR; three accepted

New: less that is undefined; Sys over any C library. PTR rewritten
with examples. Enumerations' names, a byte into an INT (INT at least
16 bits; U8, I8, I16 widen) and the width of a character: as accepted.
The second part of Sys for a shell, built, dropped. CLAUDE.md: the
report stays under 40 pages, checked after each milestone.

patch · log

2 files changed, +245 -19

CLAUDE.md modified +3 -1

@@ -67,7 +67,9 @@
6767 from 11,000, by the owner's word, who dropped the soft limit; blank and
6868 comment lines not counted), with the compiler documented and
6969 commented throughout, most of all where it is obscure or needs a
70- compiler writer's knowledge; at least 400,000 lines of raz a second.
70+ compiler writer's knowledge; at least 400,000 lines of raz a second;
71+ the report, `doc/raz.md`, under 40 pages as `make pdf` renders it,
72+ checked by hand after each milestone (29 at report 2026-10.29).
7173 - Clarity comes first, before speed and size: the compiler must be a
7274 model of plain code that a beginner can follow. Every rewrite of the
7375 code it makes is explained where it is done, with the code before

PROPOSALS.md modified +242 -18

@@ -379,13 +379,17 @@
379379 kinds, a style and the HTML element that makes it, names one of them
380380 again: libro's `Flow` has `bEl`, `iEl`, `pEl`, ... for it.
381381
382-The proposal: a name that two enumerations of a scope share is written
383-with its type, `Elem.bold`, where the type doesn't already say which
384-(`match` on a `Style`, a `Style` assigned, compared or passed, all say
385-which); a name only one enumeration has stays as it is. No program
386-changes, since every program today has each name once. It is more
387-work for the compiler's lookup, which must hold a name that is two
388-things until the type is known.
382+The proposal: a name of an enumeration may always be written with its
383+type, `Elem.bold`; and a name that two enumerations of a scope share,
384+or an enumeration and something else, must be, everywhere. A name only
385+one enumeration has stays as it is. The type is never guessed from
386+where the name stands, so the compiler's lookup stays a lookup: a name
387+it finds twice is an error that says to write the type. Taking the
388+type from the place (`match` on a `Style`, a `Style` assigned) could
389+come later, and would break nothing. No program changes, since every
390+program today has each name once.
391+
392+Accepted by the owner, for a thaw.
389393
390394 Met in libro's `Flow`.
391395
@@ -423,12 +427,30 @@
423427 made `INT` widen to `I64` wherever it meets one, for the same reason;
424428 the small types into `INT` are the same, and more common.
425429
426-The proposal: `U8`, `U16`, `U32`, `I8`, `I16` and `I32` widen to `INT`
427-where their value goes to one (assigned, passed, returned, compared,
428-in an operator with an `INT`), as `INT` to `I64` does (5.10). Not `U32`
429-to an `INT` of 32 bits, on a target that has one, and never a wider
430-type to a narrower, nor `INT` back to any. No program changes.
430+The proposal, in three parts:
431+
432+- `INT` and `UINT` are at least 16 bits, on every target (5.1).
433+- `U8`, `I8` and `I16` widen to `INT`, and `U8` and `U16` to `UINT`,
434+ where their value goes to one: assigned, passed, returned, in
435+ `a, b := f(x)`, a `for`'s bounds, `INC` and `DEC`'s step, and in an
436+ operator that meets an `INT`, as `INT` to `I64` does (5.10). These
437+ are the types an `INT` holds whole on every target, so a program
438+ that compiles on one compiles on all. `U16`, `I32` and `U32` still
439+ need `INT(x)`; were `INT` at least 32 bits, `U16` and `I32` would
440+ join, and the minimum can be raised later, never lowered.
441+- What an operator gives on two narrow operands doesn't widen. raz
442+ works in the operands' type, so with `a, b: U8`, `a + b` is a `U8`
443+ that wraps: 200 + 100 is 44. Widened silently, `n := a + b` would
444+ read as 300 and be 44, so it stays an error, and the program writes
445+ `n := INT(a) + b`, saying where the work is done in `INT`.
446+ `n := s[i]`, `n * 10 + s[i]` and `f(b)` widen, since a variable, an
447+ element, a field or a call's result is only moved.
431448
449+No program changes. `src/` and the library it imports can't use it
450+until a release makes a new seed, as with 2026-10.26.
451+
452+Accepted by the owner, for a thaw.
453+
432454 Met in libro's `Term`, `Flow` and `Epub`, and rml's `Rml`, wherever
433455 a byte is a number.
434456
@@ -442,12 +464,214 @@
442464 among them, 1 for the rest, from two sorted tables of ranges (357 and
443465 121) made with Python's `unicodedata`, Unicode 15.1.
444466
445-The proposal: that, as `Str.width(c: I32): INT` and
446-`Str.columns(s: []U8): INT`, or a module of its own, since the tables
447-are about 4 KB that only such programs want. It is the same answer as
448-glibc's `wcwidth` for nearly every character; the differences, the
449-ambiguous widths among them, are taken as 1, as most terminals take
450-them.
467+The proposal: a module of the standard library of its own, not `Str`,
468+which is in the core every target has, with `width(c: I32): INT` and
469+`columns(s: []U8): INT`. Its tables, about 4 KB, are written by a tool
470+in `tools/` from Unicode's `EastAsianWidth.txt` and `UnicodeData.txt`,
471+so that a new version of Unicode is the tool run again. It is the same
472+answer as glibc's `wcwidth` for nearly every character; the
473+differences, the ambiguous widths among them, are taken as 1, as most
474+terminals take them. It gives a character's width, not a cluster's:
475+an emoji joined by ZWJ, or a flag, comes out as its parts, as it does
476+in `wcwidth`.
451477
478+Accepted by the owner, on one condition: a program that doesn't call
479+it carries none of it, its tables included. A test shows that first.
480+It adds a module and breaks no program, so the freeze allows it.
481+
452482 Met in libro; every program with a screen, rmacs and ravite among
453483 them, needs the same.
484+
485+## Less that is undefined
486+
487+Section 10 lists what isn't defined: a stale or made-up pointer,
488+indexing through a pointer, a slice kept after its frame is gone, a
489+slice of an array a call returned kept past its statement, an index
490+out of range under `check off;`, writing a string's bytes, a code
491+procedure that breaks its rules, and C. Some of these can be closed at
492+little cost; the rest can be fenced, so that a reader sees which
493+modules can reach memory they shouldn't. Four pieces, each standing on
494+its own, the cheapest first.
495+
496+**1. An array a call returns lives to the end of its procedure.**
497+Section 8 says it lives to the end of its statement. Its slot in the
498+frame would stay reserved for the rest of the procedure instead, as a
499+deferred statement's slots already do, so that `s := f('a')[0..4]` is
500+defined until the procedure returns; in a loop the slot holds the
501+latest call's array. Section 10 loses a case, and what is left of it
502+is a slice leaving its frame (7.3), which `ret` and `keep`, above,
503+would close. It breaks nothing; the cost is a frame larger by each
504+such call's array.
505+
506+**2. Writing a string's bytes stops the program, or changes them.**
507+Two outcomes instead of any: where the target has read-only memory,
508+the string constants are in it, and a write stops the program as a
509+bad address (section 10), as this compiler's already does, since they
510+are in rodata; where it hasn't, the write changes the constant for the
511+rest of the run, everywhere its bytes are shared. Which of the two is
512+the target's (appendix A). It breaks nothing.
513+
514+**3. A race tears no slice.** 7.6 says a variable written by one
515+thread while another reads it holds a value it had, or a mixture of
516+their bytes, and that nothing else becomes undefined. For a slice
517+that is a defect: a mixture of one slice's address and another's
518+length is indexed, checked, against the wrong length, and reaches any
519+memory; a variant's tag torn from its fields is the same. The mend:
520+a slice, a variant, or a record holding either, read or written in a
521+race, is the exception of section 10, said so in 7.6. A pointer, a
522+word on every target, tears into nothing new. This is a defect, which
523+the freeze allows mending.
524+
525+**4. Fencing the rest: `unsafe;`.** A module that does any of the
526+following says `unsafe;` after its links and its `check` line, and it
527+is an error to do them in one that doesn't:
528+
529+- `ADR`, and `p + i`, `p - i`, `p[i]` and `p[lo..hi]` on a pointer;
530+- converting an integer to a pointer or a procedure, or one pointer
531+ or procedure type to another;
532+- a view, `v.(T)`, whose `T` holds a pointer, a slice or a procedure
533+ (one to integers, reals and their arrays stays safe);
534+- `check off;`, `extern`, `link`, code procedures and `SYSCALL`.
535+
536+`unsafe`, like `static` in `link static;`, is a word only there. In a
537+program none of whose modules says it, what is left undefined is
538+short: a pointer used after `Heap.free` or an arena's reset gave back
539+what it points at, and a slice leaving its frame (7.3). The library's
540+modules that need it (`Rt`, `Mem`, `Heap`, `Arena`, `Bytes`, `Sys`,
541+`Thread`, ...) say it, as a program trusts them; `grep unsafe` lists
542+the rest. It breaks every module that does one of these and doesn't
543+say it: rmacs, ravite, the tools and ten of `src/`'s modules (by
544+`ADR`), each mended by one line, which
545+the compiler's message names. The compiler's cost is a flag on the
546+module, checked at each of those places. Since the seed doesn't know
547+`unsafe;`, `src/` can't say it until a release makes a new seed: for
548+one release a missing `unsafe;` is a warning, then an error.
549+
550+Pieces 1 and 2 are small and change no program; 3 is a mend; 4 needs
551+a thaw.
552+
553+## Sys over any C library
554+
555+`lib/Sys.raz` is the `Sys` of a target with no file of its own, which
556+is every target but Linux; it is written over C so that one file
557+serves them all. But it holds glibc's facts for x86-64 Linux, as its
558+opening comment says: where a name lies in struct dirent, struct stat
559+and statvfs, struct sigaction, sockaddr_un and sockaddr_in, open's,
560+socket's and send's flags, the terminal's requests, the signals' and
561+the limits' numbers. These are the kernel's more than glibc's:
562+checked on this machine against musl 1.2, every one Sys uses agrees,
563+and musl lacks only `close_range`. On FreeBSD, macOS, or Linux on
564+ARM, nearly all differ, so the file meant for other systems serves
565+none of them. There is more of the same:
566+
567+- `C.__errno_location` is glibc's and musl's name; the BSDs and macOS
568+ call it `__error`. `close_range`, `memfd_create`, `getrandom`,
569+ `accept4` and `cfmakeraw` aren't in every C library.
570+- `doc/raz-lib.md` says a failure is "minus an errno, as on Linux",
571+ but `Sys` over C gives the C library's numbers, which elsewhere
572+ aren't Linux's (EAGAIN is 11 on Linux, 35 on the BSDs); `Files`
573+ writes -2 for ENOENT.
574+- A program that uses C needs `libc.so.6`, names
575+ `/lib64/ld-linux-x86-64.so.2` as its loader, and starts through
576+ `__libc_start_main`: constants of `Elf.raz` and `Raz.raz`. musl's
577+ loader is `/lib/ld-musl-x86_64.so.1` and its library `libc.so`
578+ (`__libc_start_main` is in both), so a raz program that uses C
579+ doesn't run on Alpine.
580+
581+Four pieces, each standing on its own:
582+
583+**1. The facts in a module of their own.** `Sys.raz` keeps its code,
584+over POSIX's names; what C fixes for each system, the layouts, the
585+numbers and the names that differ, goes in a new module, `CAbi`, one
586+file per target (`CAbi.linux-x86_64.raz`, `CAbi.freebsd-x86_64.raz`),
587+as `Sys` has: offsets, sizes and constants, and the externs whose
588+names differ behind one raz name (`CAbi.errno(): ^I32`). `Sys` reads
589+a struct stat as bytes at `CAbi`'s offsets. Porting `Sys` to a C
590+library is then writing that file, about a hundred lines, and nothing
591+else. It adds a module and breaks no program.
592+
593+**2. The facts written by C.** `tools/cabi.c`, a C program that
594+prints `CAbi`'s file for the system it is compiled on, from its own
595+headers, with `offsetof`, `sizeof` and the constants: no fact is typed
596+by hand, and a porter runs it once. `make check`, where there is a C
597+compiler, runs it and compares with `lib/`'s file, and does the same
598+with `musl-gcc` where that is installed.
599+
600+**3. Errnos as Linux's.** `Sys` exports names for the errnos programs
601+test (`Sys.noEntry = -2`, `again`, `intr`, `exists`, ...), with
602+Linux's numbers, and `Sys` over C maps the C library's errno to them
603+through a table in `CAbi`, so that "as on Linux" holds everywhere.
604+`Files` and the programs use the names. It adds constants and breaks
605+no program.
606+
607+**4. The compiler's C library.** The loader's path, the library's
608+name and the start-up symbol become facts of the target, not
609+constants: `linux-x86_64` links against glibc or musl, by an option,
610+or by default by which loader the machine has. Appendix A says the
611+C library is the implementation's to choose under "The target", and
612+`test/libc` runs under each one installed.
613+
614+Piece 4 has a gain today: raz programs that use C run on Alpine.
615+Pieces 1 to 3 pay when a second system is ported, and 3 needs 1.
616+
617+## A slice's pointer
618+
619+A slice is two words: the address its elements start at, and how many
620+there are (5.3). `LEN(s)` gives the second; nothing gives the first.
621+A program that needs it writes `ADR(s[0])`, the address of the first
622+element. That works only when there is a first element: when `s` is
623+empty, `s[0]` is an index out of range, and the program stops.
624+
625+Where it bites is a program that keeps bytes as a pointer and a
626+length, as an interpreter keeps a string value:
627+
628+```
629+type Value = record kind: Kind; n: INT; p: ^U8; end;
630+
631+proc strVal(s: []U8): Value;
632+begin
633+ return {kind: kStr, n: LEN(s), p: ADR(s[0])}; (* stops when s is "" *)
634+end;
635+```
636+
637+It compiles, and passes every test that has no empty string in it;
638+the first `""` stops the program. So every place that turns a slice
639+into a pointer must test first:
640+
641+```
642+proc strVal(s: []U8): Value;
643+begin
644+ if LEN(s) = 0 then return {kind: kStr}; end; (* n is 0, p nil *)
645+ return {kind: kStr, n: LEN(s), p: ADR(s[0])};
646+end;
647+```
648+
649+The library does the same before each call to C: `Sys.write` over C
650+returns 0 for an empty `s` before it writes `C.write(fd, ADR(s[0]),
651+LEN(s))`. The test is easy to forget and says nothing about what the
652+program means.
653+
654+The proposal: a built-in `PTR(s)`, the address the slice or array `s`
655+starts at, a `^T` for a `[]T` or `[n]T`, with no check. For a slice
656+that isn't empty it is `ADR(s[0])`. For an empty one it is what the
657+slice holds: `nil` for a slice variable as it starts (section 4), and
658+for one cut empty from another, `t[3..3]`, an address that mustn't be
659+read through, as the end of an array mustn't. With it:
660+
661+```
662+proc strVal(s: []U8): Value;
663+begin
664+ return {kind: kStr, n: LEN(s), p: PTR(s)};
665+end;
666+```
667+
668+The way back is there already: `v.p[0..v.n]` is the slice again
669+(6.4), empty when `v.n` is 0, whatever `v.p` is. Reading through the
670+pointer is as unchecked as through any pointer; `PTR` only stops the
671+program from failing where nothing is read.
672+
673+It costs a row in the table of 6.8. A predeclared name can be hidden
674+(section 3), so it breaks no program.
675+
676+Met in concha's `Ccore`, in `bufVal` and `listVal`, and in `Sys` over
677+C, before each call that takes a buffer.