Report 2026-10.10: constants don't overflow, pointer slices don't run backwards Two defects mended, as the freeze allows: arithmetic on untyped constants that leaves the I64s is an error, not a silent wrap; and a slice of a pointer whose hi is below its lo is a failed check, where it made a slice of negative length. Ordinary accesses are promised only to do what one thread would see; volatile and atomic ones stay exact. The library's module names are kept for it, the names of modules staying flat. Appendix A names the core every target has.
| @@ -93,9 +93,11 @@ | ||
| 93 | 93 | getting memory) and what it may leave out. No language change. |
| 94 | 94 | For review before anything else. |
| 95 | 95 | - [ ] 2. The report, the new language, for review before any code: the |
| 96 | - system integer, the size of an address, which `LEN`, indexes and | |
| 97 | - sizes give (the owner's choice), and how it converts to and from | |
| 98 | - `I64`; reals a | |
| 96 | + system integers `INT` and `UINT`, the size of an address, which | |
| 97 | + `LEN`, indexes, sizes and `p - q` give, and through which | |
| 98 | + integers and pointers convert (the owner's choices), and how | |
| 99 | + they convert to and from `I64`; a module's own choice to leave | |
| 100 | + out its checks of indexes and slices; reals a | |
| 99 | 101 | target may leave out; object files, a module's interface and its |
| 100 | 102 | IR, and a generic's instances in them, named by their arguments |
| 101 | 103 | and the fingerprints they were built from. |
| @@ -12,7 +12,7 @@ | ||
| 12 | 12 | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | |
| 15 | -| `src/gen.pas` | 1,596 | 1,379 | Items: loading, storing, copying, frames, calls | | |
| 15 | +| `src/gen.pas` | 1,619 | 1,398 | Items: loading, storing, copying, frames, calls | | |
| 16 | 16 | | `src/parse.pas` | 2,443 | 2,202 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 17 | 17 | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | |
| 18 | 18 | | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| @@ -1,15 +1,15 @@ | ||
| 1 | 1 | # The programming language raz |
| 2 | 2 | |
| 3 | 3 | raz is a small systems language for x86-64 Linux. A raz program can lay |
| 4 | -a record over a device's registers, read and write memory exactly as | |
| 5 | -written, take part in lock-free algorithms, make system calls and call | |
| 4 | +a record over a device's registers, read and write a device's memory | |
| 5 | +exactly as written, take part in lock-free algorithms, make system calls and call | |
| 6 | 6 | C libraries. It has no garbage collector and no hidden run time: what |
| 7 | 7 | the program does is what its text says. |
| 8 | 8 | |
| 9 | 9 | This report defines the language. It is short on purpose. Whatever it |
| 10 | 10 | doesn't allow, raz doesn't have. |
| 11 | 11 | |
| 12 | -This is version 2026-10.9 of the report; section 12 says what each | |
| 12 | +This is version 2026-10.10 of the report; section 12 says what each | |
| 13 | 13 | version changed. |
| 14 | 14 | |
| 15 | 15 | The language is frozen, softly, from this version on: it changes only |
| @@ -75,7 +75,9 @@ | ||
| 75 | 75 | An integer is an untyped constant, which takes the type it is used as, |
| 76 | 76 | up to the largest `I64`. A hexadecimal number above that, such as |
| 77 | 77 | `0FFFFFFFFFFFFFFFFH`, has type `U64`: so `-1` never fits a `U64`, and |
| 78 | -the big number goes nowhere else without a conversion. | |
| 78 | +the big number goes nowhere else without a conversion. Arithmetic on | |
| 79 | +untyped constants doesn't wrap: a result beyond the `I64`s, such as | |
| 80 | +`9223372036854775807 + 1`, is an error. | |
| 79 | 81 | |
| 80 | 82 | A real is an untyped constant too, which takes `F32` or `F64`, |
| 81 | 83 | whichever it is used as, rounded to the nearest value of that type; |
| @@ -205,10 +207,15 @@ | ||
| 205 | 207 | that a directory holding a link to `lib/Sys.raz` builds a program over |
| 206 | 208 | the C library's `Sys` on Linux too. |
| 207 | 209 | |
| 208 | -Since the names of a program's modules are one space, the modules of | |
| 209 | -a library share a prefix of their own, as an X11 library's `Xauth` and | |
| 210 | -`Xkeys` do, and programs leave the library's short names (`Buf`, | |
| 211 | -`Map`, `Out`, `C` and the rest of section 11) to it. | |
| 210 | +**Names are flat.** The names of a program's modules are one space, | |
| 211 | +with no packages or paths. So the names of the library's modules | |
| 212 | +(section 11) are kept for it: a program or another library doesn't | |
| 213 | +name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Heap`, | |
| 214 | +`List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, `Sort`, | |
| 215 | +`Str`, `Sys` or `Thread`, nor any name the library takes later. The | |
| 216 | +modules of any other library share a prefix of their own, as an X11 | |
| 217 | +library's `Xauth` and `Xkeys` do, and a program that wants a shorter | |
| 218 | +name imports one: `import Auth := Xauth;`. | |
| 212 | 219 | |
| 213 | 220 | The compiler holds every file of such a module to the same interface. |
| 214 | 221 | When it takes one of `Name.*.raz`, it also reads the declarations of |
| @@ -545,7 +552,9 @@ | ||
| 545 | 552 | - `p - q`, with both of the same pointer type, is the number of |
| 546 | 553 | elements between them, an `I64`. |
| 547 | 554 | - `p[i]` is `(p + i)^`. `p[lo..hi]` is the slice of the `hi - lo` |
| 548 | - elements from `p + lo`. Neither is checked. A pointer to an array | |
| 555 | + elements from `p + lo`. Neither is checked against a length, which a | |
| 556 | + pointer doesn't have, but a slice of a pointer that runs backwards, | |
| 557 | + `hi < lo`, is a failed check (section 6.5). A pointer to an array | |
| 549 | 558 | isn't indexed this way, since a step would be a whole array: its |
| 550 | 559 | elements are `p^[i]`. |
| 551 | 560 | - Pointers compare with `= # < <= > >=` as unsigned addresses. |
| @@ -763,6 +772,17 @@ | ||
| 763 | 772 | `FENCE` also orders the program's ordinary accesses against those of |
| 764 | 773 | other processors and devices. |
| 765 | 774 | |
| 775 | +**Ordinary variables.** Only volatile accesses, `CAS`, `XCHG`, `XADD` | |
| 776 | +and `FENCE` are exact. Any other access is ordinary: an implementation | |
| 777 | +may keep a variable in a register, leave out a write that nothing | |
| 778 | +reads, and change the order of ordinary accesses, as long as each | |
| 779 | +thread, on its own, does what this report says. A variable that one | |
| 780 | +thread writes while another reads or writes it, unless both use | |
| 781 | +volatile accesses or the atomic built-ins, or a `FENCE` orders them, | |
| 782 | +holds no particular value then: a read of it gives a value it had or | |
| 783 | +was given, or a mixture of their bytes. Nothing else of the program | |
| 784 | +becomes undefined by it. | |
| 785 | + | |
| 766 | 786 | ### 7.7 Defer |
| 767 | 787 | |
| 768 | 788 | `defer s;` runs the statement `s` when the program leaves the statement |
| @@ -1495,6 +1515,14 @@ | ||
| 1495 | 1515 | `Program`, and `Sys.wait` says which program changed and how. `Str` |
| 1496 | 1516 | gains UTF-8 and reading a number, `Map` removing and walking, `Fmt` |
| 1497 | 1517 | reals; `Sort`, `Math`, `Files` and `Opt` are new (section 11). |
| 1518 | +- **2026-10.10.** Said at last: what the compiler did and the report | |
| 1519 | + didn't say, all through; what an implementation chooses, and the | |
| 1520 | + core every target has (appendix A); the whole grammar (appendix B); | |
| 1521 | + the library's module names are kept for it (section 3). Mended: | |
| 1522 | + arithmetic on untyped constants that overflows is an error (section | |
| 1523 | + 2); a slice of a pointer can't run backwards (section 6.4). Ordinary | |
| 1524 | + accesses are no longer promised to be made exactly as written, only | |
| 1525 | + volatile and atomic ones (section 7.6). | |
| 1498 | 1526 | |
| 1499 | 1527 | ## Appendix A. What an implementation chooses |
| 1500 | 1528 | |
| @@ -1521,6 +1549,13 @@ | ||
| 1521 | 1549 | every implementation; the words that say so are free. |
| 1522 | 1550 | - **The command line.** How the compiler is run, its options, and |
| 1523 | 1551 | where its library lives (sections 1 and 3). |
| 1552 | + | |
| 1553 | +**The core.** A target may leave out what the system under it can't | |
| 1554 | +give: `Sys` past writing, ending the program and getting memory, | |
| 1555 | +`Thread`, `link` and `extern`. What it must have, besides the whole | |
| 1556 | +language, is the core of the library: `Rt`, `Out`, `Mem`, `Arena`, | |
| 1557 | +`Lock`, `Str`, `Buf` and `Fmt`, and of `Sys` what they need. A | |
| 1558 | +program that keeps to the core runs on every target. | |
| 1524 | 1559 | |
| 1525 | 1560 | ## Appendix B. The grammar |
| 1526 | 1561 |
| @@ -149,18 +149,20 @@ | ||
| 149 | 149 | failed, on standard error, then ends the program by exit with |
| 150 | 150 | status 2. what is 0 for an index a and 1 for a slice a..b, of |
| 151 | 151 | something whose length is len; 2 for a, which is no position of |
| 152 | - the enumeration called name. *) | |
| 152 | + the enumeration called name; 3 for a slice a..b of a pointer, which | |
| 153 | + runs backwards. *) | |
| 153 | 154 | proc fail*(what, a, b, len: I64; at: U64; name: ^U8); |
| 154 | 155 | begin |
| 155 | 156 | where(at); |
| 156 | 157 | if what = 0 then |
| 157 | 158 | put("index "); |
| 158 | 159 | putInt(a); |
| 159 | - elsif what = 1 then | |
| 160 | + elsif (what = 1) or (what = 3) then | |
| 160 | 161 | put("slice "); |
| 161 | 162 | putInt(a); |
| 162 | 163 | put(".."); |
| 163 | 164 | putInt(b); |
| 165 | + if what = 3 then put(" runs backwards"); end; | |
| 164 | 166 | else |
| 165 | 167 | putInt(a); |
| 166 | 168 | put(" isn't a position of "); |
| @@ -673,8 +673,12 @@ | ||
| 673 | 673 | Value(hi); |
| 674 | 674 | MovRR(RDX, RAX); |
| 675 | 675 | PopR(RCX); |
| 676 | - if t^.form = fPtr then PopR(R8) | |
| 677 | - else begin | |
| 676 | + if t^.form = fPtr then begin | |
| 677 | + { the bounds can't be known, but a slice can't run backwards } | |
| 678 | + PopR(R8); | |
| 679 | + Alu(aluCmp, RCX, RDX); | |
| 680 | + Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1); | |
| 681 | + end else begin | |
| 678 | 682 | m := MemOf(x, R8); |
| 679 | 683 | ml := m; ml.disp := ml.disp + 8; |
| 680 | 684 | if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end |
| @@ -708,7 +712,9 @@ | ||
| 708 | 712 | if x.mode = mConst then x.val := x.val xor low(int64) |
| 709 | 713 | else begin Value(x); if IsF32(x.typ) then Btc(RAX, 31) else Btc(RAX, 63) end; |
| 710 | 714 | end else if IsInt(x.typ) then begin |
| 711 | - if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 715 | + if (x.mode = mConst) and (x.typ^.form = fConst) and (x.val = low(int64)) then | |
| 716 | + MarkAt(x.pos, 'the constant is too big for I64') | |
| 717 | + else if x.mode = mConst then x.val := Cut(-x.val, x.typ) | |
| 712 | 718 | else begin Value(x); Unary(3, RAX); Norm(x.typ) end; |
| 713 | 719 | end else MarkAt(x.pos, 'can''t negate ' + TypeName(x.typ)); |
| 714 | 720 | end; |
| @@ -803,10 +809,27 @@ | ||
| 803 | 809 | else begin ShiftI(shShl, RCX, k); Alu(aluSub, RAX, RCX) end; |
| 804 | 810 | end; |
| 805 | 811 | |
| 812 | +{ Whether a op b, for + - *, overflows an I64: an untyped constant | |
| 813 | + doesn't wrap, it is an error (report section 4). } | |
| 814 | +function Overflows(op: TSym; a, b: int64): boolean; | |
| 815 | +begin | |
| 816 | + case op of | |
| 817 | + sPlus: Overflows := ((b > 0) and (a > high(int64) - b)) or ((b < 0) and (a < low(int64) - b)); | |
| 818 | + sMinus: Overflows := ((b < 0) and (a > high(int64) + b)) or ((b > 0) and (a < low(int64) + b)); | |
| 819 | + sTimes: | |
| 820 | + if (a = 0) or (b = 0) then Overflows := false | |
| 821 | + else if a = -1 then Overflows := b = low(int64) | |
| 822 | + else if b = -1 then Overflows := a = low(int64) | |
| 823 | + else Overflows := (a * b) div b <> a; | |
| 824 | + else Overflows := false; | |
| 825 | + end; | |
| 826 | +end; | |
| 827 | + | |
| 806 | 828 | function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; |
| 807 | 829 | var u: boolean; |
| 808 | 830 | begin |
| 809 | 831 | u := (t^.form = fInt) and not t^.signed; |
| 832 | + if (t^.form = fConst) and Overflows(op, a, b) then MarkAt(p, 'the constant is too big for I64'); | |
| 810 | 833 | case op of |
| 811 | 834 | sPlus: FoldInt := a + b; |
| 812 | 835 | sMinus: FoldInt := a - b; |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 32 | 32 | 0.3.0-5-g38d29a8 five commits past it } |
| 33 | 33 | Version = {$I version.inc}; |
| 34 | - Report = '2026-10.9'; { the version of doc/raz.md it follows } | |
| 34 | + Report = '2026-10.10'; { the version of doc/raz.md it follows } | |
| 35 | 35 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 36 | 36 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 37 | 37 |
| @@ -0,0 +1,10 @@ | ||
| 1 | +test/err/GenConstOverflow.raz:5:12: error: the constant is too big for I64 | |
| 2 | + 5 | Over = Big + 1; | |
| 3 | + | ^^^^^^^ | |
| 4 | +test/err/GenConstOverflow.raz:6:13: error: the constant is too big for I64 | |
| 5 | + 6 | Twice = 4611686018427387904 * 2; | |
| 6 | + | ^^^^^^^^^^^^^^^^^^^^^^^ | |
| 7 | +test/err/GenConstOverflow.raz:7:13: error: the constant is too big for I64 | |
| 8 | + 7 | Under = -Big - 2; | |
| 9 | + | ^^^^^^^^ | |
| 10 | +raz: 3 errors |
| @@ -0,0 +1,14 @@ | ||
| 1 | +module GenConstOverflow; | |
| 2 | +(* An untyped constant doesn't wrap *) | |
| 3 | +const | |
| 4 | + Big = 9223372036854775807; | |
| 5 | + Over = Big + 1; | |
| 6 | + Twice = 4611686018427387904 * 2; | |
| 7 | + Under = -Big - 2; | |
| 8 | + Fine = U8(250) + 10; | |
| 9 | + | |
| 10 | +proc main*; | |
| 11 | +begin | |
| 12 | +end; | |
| 13 | + | |
| 14 | +end GenConstOverflow. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 0 | |
| \ No newline at end of file |
| @@ -0,0 +1,16 @@ | ||
| 1 | +module SlicePtrBackwards; | |
| 2 | +(* A slice of a pointer isn't checked against a length, but it can't | |
| 3 | + run backwards *) | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +proc main*; | |
| 7 | +var a: [8]U8; p: ^U8; hi: I64; | |
| 8 | +begin | |
| 9 | + p := ADR(a[0]); | |
| 10 | + for hi := 4 to 0 by -2 do | |
| 11 | + Out.int(LEN(p[2..hi])); | |
| 12 | + Out.str(" "); | |
| 13 | + end; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +end SlicePtrBackwards. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/SlicePtrBackwards.raz:11:23: slice 2..0 runs backwards |