raz / commit e33d4214

commit
e33d42148b55003002cc8179cbcf99222f755452
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b5e76eab
Report 2026-10.37: import SYSTEM, and what reaches memory other than through a variable

SYSTEM is no file: the import binds to Sym.system, whose scope holds
ADR, PTR, SYSCALL and ARGV, written SYSTEM.ADR and the like; it can't
be aliased, and isn't warned of as unused. The report fences pointer
arithmetic and conversions, views and BYTES that make a pointer, C,
code procedures and check off behind it; section 10 says what is left
undefined outside such modules, and 7.6 that a race on a pointer, a
slice or a variant is. raz 0.23 still takes all of it without the
import, and the built-ins unprefixed, until 0.24. The man page and
the tutorial use the new forms.

patch · log

12 files changed, +275 -85

CLAUDE.md modified +6 -0

@@ -380,6 +380,12 @@
380380 (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still
381381 taken as the old form until v0.24.0; a pointer variable is the new
382382 form, so `XADD(p, 1)` is an error whose help says `p^`.
383+- `import SYSTEM` (report 2026-10.37) binds to `Sym.system`, no file,
384+ whose scope holds `ADR`, `PTR`, `SYSCALL` and `ARGV`; it is never
385+ warned of as unused, and an alias is refused. In v0.23.0 nothing is
386+ fenced yet: the bare built-ins, pointer arithmetic and conversions,
387+ views, `extern`, `link`, code procedures and `check off;` are taken
388+ in any module, until v0.24.0.
383389 - `[]const T` (report 2026-10.37) is a slice type with `rdonly` set: not
384390 the same as `[]T`, which goes into it with no code (`coerce`), never
385391 back (help says why). An element read through one has the item's

conform/err/SystemAlias.raz (history) added +9 -0

@@ -0,0 +1,9 @@
1+(* SYSTEM is imported only by its own name (report section 3). *)
2+module SystemAlias;
3+import S := SYSTEM;
4+
5+proc main*;
6+begin
7+end;
8+
9+end SystemAlias.

conform/run/SystemImport.expect (history) added +1 -0

@@ -0,0 +1 @@
1+120avp

conform/run/SystemImport.raz (history) added +25 -0

@@ -0,0 +1,25 @@
1+(* import SYSTEM (report section 3): SYSTEM.ADR, SYSTEM.PTR,
2+ SYSTEM.SYSCALL and SYSTEM.ARGV are the built-ins of section 6.8,
3+ written by that name, and a local given to SYSTEM.ADR is that
4+ variable, however busy. *)
5+module SystemImport;
6+import Out, SYSTEM;
7+
8+proc main*;
9+var k, i: INT; p: ^INT; a: [4]U8; s: []U8; q: ^U8;
10+begin
11+ for i := 1 to 10 do INC(k); end;
12+ p := SYSTEM.ADR(k);
13+ p^ := p^ + 100;
14+ for i := 1 to 10 do INC(k); end;
15+ Out.int(k);
16+ a := "abcd";
17+ s := a;
18+ q := SYSTEM.PTR(s);
19+ Out.char(q^);
20+ if SYSTEM.ARGV() # nil then Out.char('v'); end;
21+ if SYSTEM.SYSCALL(39) > 0 then Out.char('p'); end; (* getpid *)
22+ Out.char(10);
23+end;
24+
25+end SystemImport.

doc/compiler.md (history) modified +15 -5

@@ -1,6 +1,6 @@
11 # Reading the raz compiler
22
3-The compiler is eleven files of raz, about 12,500 lines of code (the
3+The compiler is eleven files of raz, about 12,600 lines of code (the
44 budget is 14,000). It is its own compiler: a release of it, the one
55 `BOOT` names, builds `src/`, and `make fixpoint` checks that what it
66 makes makes itself again, byte for byte. Each release was built by the
@@ -18,12 +18,12 @@
1818 |---|---|---|---|
1919 | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts |
2020 | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions |
21-| `src/Sym.raz` | 737 | 590 | Types, objects, scopes, layout |
21+| `src/Sym.raz` | 792 | 633 | Types, objects, scopes, layout |
2222 | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes |
2323 | `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler |
24-| `src/Gen.raz` | 2,833 | 1,982 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25-| `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls |
26-| `src/Parse.raz` | 4,748 | 4,046 | Declarations, statements, expressions, built-ins, recovery, warnings |
24+| `src/Gen.raz` | 2,840 | 1,986 | The stack machine, for x86-64: values, operations, checks, calls, frames |
25+| `src/Item.raz` | 2,501 | 2,006 | Items: types, constants, variables, operators, conversions, calls |
26+| `src/Parse.raz` | 4,797 | 4,084 | Declarations, statements, expressions, built-ins, recovery, warnings |
2727 | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places |
2828 | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers |
2929 | `src/Raz.raz` | 418 | 333 | The command line |
@@ -1885,6 +1885,16 @@
18851885 which its type's fingerprint has.
18861886
18871887 Last, modules, at the end of the file. `compileFile` reads one module.
1888+`import SYSTEM` (report 2026-10.37) finds no file: the import binds the
1889+name to `Sym.system`, a module made once in `Sym`'s body whose scope
1890+holds four of the built-ins, `ADR`, `PTR`, `SYSCALL` and `ARGV` (made by
1891+`enterIn`), so that `qualIdent` finds `SYSTEM.ADR` as it finds any
1892+export and `builtin` compiles it as `ADR`. It is marked used at the
1893+import, since a module may import it only for `extern` or `link`. An
1894+alias, `import S := SYSTEM`, is refused, so a grep finds every such
1895+module. Until v0.24.0 nothing needs it: the bare built-ins and the rest
1896+it fences are still taken anywhere.
1897+
18881898 `importModule` finds a module's file, trying the target's names first,
18891899 and compiles it if it isn't yet. A module found in an `-I` or
18901900 `RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line

doc/raz-tutorial.7 (history) modified +26 -20

@@ -478,7 +478,7 @@
478478 .Sh 6. RECORDS AND POINTERS
479479 .Bd -literal -offset indent
480480 module Boxes;
481-import Fmt;
481+import Fmt, SYSTEM;
482482
483483 type
484484 Point = record x, y: INT; end;
@@ -502,8 +502,8 @@
502502 begin
503503 b := {lo: {x: 1, y: 1}, hi: {x: 4, y: 3}};
504504 Fmt.print("{}\en", [Fmt.int(area(b))]);
505- grow(ADR(b), 1);
506- p := ADR(b.hi);
505+ grow(SYSTEM.ADR(b), 1);
506+ p := SYSTEM.ADR(b.hi);
507507 p^.x := 10;
508508 Fmt.print("{} {}\en", [Fmt.int(area(b)), Fmt.int(b.hi.x)]);
509509 end;
@@ -521,7 +521,7 @@
521521 .Cm ^Box
522522 is a pointer to a
523523 .Cm Box ,
524-.Cm ADR(b)
524+.Cm SYSTEM.ADR(b)
525525 the address of
526526 .Cm b ,
527527 and
@@ -536,13 +536,13 @@
536536 apart, as an interpreter keeps a string, or as C wants them:
537537 .Bd -literal -offset indent
538538 module Spans;
539-import Out;
539+import Out, SYSTEM;
540540
541541 type Span = record p: ^U8; n: INT; end;
542542
543543 proc spanOf(s: []U8): Span;
544544 begin
545- return {p: PTR(s), n: LEN(s)};
545+ return {p: SYSTEM.PTR(s), n: LEN(s)};
546546 end;
547547
548548 proc text(sp: Span): []U8;
@@ -570,18 +570,18 @@
570570 .Pp
571571 .Cm LEN(s)
572572 is the length, and
573-.Cm PTR(s)
573+.Cm SYSTEM.PTR(s)
574574 the pointer, a
575575 .Cm ^U8
576576 for a
577577 .Cm []U8 ,
578578 with no check.
579-.Cm ADR(s[0])
579+.Cm SYSTEM.ADR(s[0])
580580 looks the same, and passes every test but the one with an empty
581581 string: there
582582 .Cm s[0]
583583 is out of range, and the program stops.
584-.Cm PTR
584+.Cm SYSTEM.PTR
585585 of an empty slice is what the slice holds, which is
586586 .Cm nil
587587 for one never set, so nothing needs testing first.
@@ -592,7 +592,7 @@
592592 .Cm sp.p
593593 is.
594594 Of an array,
595-.Cm PTR(a)
595+.Cm SYSTEM.PTR(a)
596596 is the address of its first element.
597597 .Sh 7. BYTES AND VIEWS
598598 A file, a network packet or a device hands a program bytes, and the
@@ -757,18 +757,18 @@
757757 or an enumeration isn't, since some bytes are none of its values:
758758 view an integer, and compare or convert it.
759759 A view may start at any byte, so it has no address:
760-.Cm ADR
760+.Cm SYSTEM.ADR
761761 of it is an error.
762762 Copy it to a variable first.
763763 Before views, a program made a pointer for each access, as
764-.Cm "p := ^I64(ADR(buf[0]))"
764+.Cm "p := ^I64(SYSTEM.ADR(buf[0]))"
765765 and then
766766 .Cm "p^ := 258" ,
767767 which checks nothing.
768768 .Sh 8. MEMORY AND ALLOCATORS
769769 .Bd -literal -offset indent
770770 module Stack;
771-import Out, Mem, Heap, Arena;
771+import Out, Mem, Heap, Arena, SYSTEM;
772772
773773 type Node = record next: ^Node; val: INT; end;
774774
@@ -797,7 +797,7 @@
797797 end;
798798 Out.ln;
799799 if not Arena.init(ar, 100000) then return; end;
800- for i := 1 to 1000 do top := push(top, i, ADR(ar.base)); end;
800+ for i := 1 to 1000 do top := push(top, i, SYSTEM.ADR(ar.base)); end;
801801 Out.int(top^.val); Out.ln;
802802 Arena.done(ar); (* every node at once *)
803803 end;
@@ -1043,7 +1043,7 @@
10431043 .Pp
10441044 A module that is sure of its indexes may say
10451045 .Cm "check off;"
1046-after its imports, for speed; an index out of range there reads or
1046+after its imports (it must import SYSTEM), for speed; an index out of range there reads or
10471047 writes whatever it reaches.
10481048 The program carries line numbers for
10491049 .Xr gdb 1 ,
@@ -1229,7 +1229,7 @@
12291229 .Sh 14. TALKING TO C
12301230 .Bd -literal -offset indent
12311231 module Qsort;
1232-import Out, Fmt;
1232+import Out, Fmt, SYSTEM;
12331233
12341234 link "libm.so.6";
12351235
@@ -1251,7 +1251,7 @@
12511251 var a: [5]INT; i: INT;
12521252 begin
12531253 a := [30, 10, 50, 20, 40];
1254- qsort(^U8(ADR(a)), LEN(a), SIZE(INT), order);
1254+ qsort(^U8(SYSTEM.ADR(a)), LEN(a), SIZE(INT), order);
12551255 for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end;
12561256 Out.ln;
12571257 Fmt.print("{}\en", [Fmt.real(cbrt(27.0), 3)]);
@@ -1264,6 +1264,12 @@
12641264 3.000
12651265 .Ed
12661266 .Pp
1267+A module that says
1268+.Cm "import SYSTEM"
1269+may declare
1270+.Cm extern
1271+procedures and name libraries with
1272+.Cm link .
12671273 An
12681274 .Cm extern
12691275 procedure is found by its name in the C library, or in a library a
@@ -1280,7 +1286,7 @@
12801286 as a pointer to an
12811287 .Cm INT .
12821288 A slice goes to C as two arguments,
1283-.Cm PTR(s)
1289+.Cm SYSTEM.PTR(s)
12841290 and
12851291 .Cm LEN(s) ,
12861292 which an empty slice gives too (section 6).
@@ -1296,7 +1302,7 @@
12961302 .Sh 15. THREADS
12971303 .Bd -literal -offset indent
12981304 module Hits;
1299-import Out, Thread;
1305+import Out, Thread, SYSTEM;
13001306
13011307 const Workers = 4;
13021308
@@ -1384,7 +1390,7 @@
13841390 .It "goto" Ta "goto" Ta "none"
13851391 .It "union" Ta "variant record" Ta "variant"
13861392 .It "struct p {...}" Ta "record ... end" Ta "record ... end"
1387-.It "&x" Ta "@x" Ta "ADR(x)"
1393+.It "&x" Ta "@x" Ta "SYSTEM.ADR(x)"
13881394 .It "p->f" Ta "p^.f" Ta "p^.f"
13891395 .It "NULL" Ta "nil" Ta "nil"
13901396 .It "malloc(sizeof *p)" Ta "new(p)" Ta "NEW(p, Heap.heap)"

doc/raz.7 (history) modified +31 -8

@@ -166,11 +166,30 @@
166166 .Cm main .
167167 .It
168168 .Cm "link \(dqlibX.so.1\(dq;"
169-loads a C library;
169+loads a C library (it needs
170+.Cm "import SYSTEM" );
170171 .Cm "link static;"
171172 says the program must not use C.
172173 .Cm "check off;"
173-leaves the module's index and slice checks out.
174+leaves the module's index and slice checks out (it too needs
175+.Cm "import SYSTEM" ).
176+.It
177+.Cm "import SYSTEM;"
178+is no file but the language's module of what reaches memory by other
179+roads than variables.
180+A module needs it for
181+.Cm SYSTEM.ADR ,
182+.Cm SYSTEM.PTR ,
183+.Cm SYSTEM.SYSCALL
184+and
185+.Cm SYSTEM.ARGV ;
186+pointer arithmetic, indexing and slicing;
187+conversions to and from pointers;
188+.Cm extern ,
189+.Cm link ,
190+code procedures and
191+.Cm "check off;" .
192+It can't be renamed; a module that imports one that says it needn't.
174193 .El
175194 .Sh TYPES
176195 [5] Sizes are x86-64's.
@@ -332,6 +351,8 @@
332351 .Cm p[lo..hi] ,
333352 a slice; a record's fields through it are
334353 .Cm p^.f .
354+Arithmetic, indexing and slicing need
355+.Cm "import SYSTEM" .
335356 .It
336357 Arrays and slices:
337358 .Cm a[i] ,
@@ -383,6 +404,8 @@
383404 .It "pointer, procedure" Ta "INT, UINT, pointer" Ta "the address"
384405 .El
385406 .Pp
407+The last two rows need
408+.Cm "import SYSTEM" .
386409 There is no other: none between
387410 .Cm BOOL
388411 and integers, and none to records, arrays, slices, variants or
@@ -416,7 +439,7 @@
416439 variable can't be viewed.
417440 .Sh BUILT-IN PROCEDURES
418441 [6.8]
419-.Bl -tag -width "SYSCALL(n, x1, ...)"
442+.Bl -tag -width "SYSTEM.SYSCALL(n, x1, ...)"
420443 .It Fn SIZE T
421444 The size of type
422445 .Ar T ,
@@ -433,12 +456,12 @@
433456 in the record type
434457 .Ar T ,
435458 an untyped constant.
436-.It Fn ADR x
459+.It Fn SYSTEM.ADR x
437460 The address of the variable
438461 .Ar x .
439462 .It Fn LEN a
440463 The length of an array, a string constant or a slice.
441-.It Fn PTR s
464+.It Fn SYSTEM.PTR s
442465 The address the slice
443466 .Ar s
444467 starts at, a
@@ -525,11 +548,11 @@
525548 .Ar T .
526549 .It Cm FENCE
527550 A full memory barrier.
528-.It Fn SYSCALL n x1 ...
551+.It Fn SYSTEM.SYSCALL n x1 ...
529552 Linux system call
530553 .Ar n ,
531554 with up to six arguments; minus an errno on failure.
532-.It Fn ARGC , Fn ARGV
555+.It Fn ARGC , Fn SYSTEM.ARGV
533556 The command line: its count, and its C strings.
534557 .El
535558 .Sh STATEMENTS
@@ -646,7 +669,7 @@
646669 .Cm "rdtsc; shl rdx, 32; or rax, rdx; ret;" .
647670 .It
648671 .Cm "extern proc printf(f: ^U8; ...): I32;"
649-is C's [9]: its parameters integers, reals, pointers and procedures.
672+(in a module that imports SYSTEM) is C's [9]: its parameters integers, reals, pointers and procedures.
650673 .Cm "extern var environ: ^^U8;"
651674 is C's variable.
652675 A raz procedure of those types is a C callback.

doc/raz.md (history) modified +107 -48

@@ -238,7 +238,7 @@
238238 not a variable: nothing that could write it, or let something else
239239 write it, may name it or a field or element of it. So it can't be
240240 assigned, passed as a `var` argument, changed by `INC`, `DEC` or
241-`NEW`, or given to `ADR`; an array in it is sliced, or used where a
241+`NEW`, or given to `SYSTEM.ADR`; an array in it is sliced, or used where a
242242 slice is expected (the silent conversion of an array, section 5.10),
243243 only as a `[]const T`, whose elements can't be written (section 5.3);
244244 a slice in it may be sliced, since that makes a new slice and can't
@@ -255,6 +255,32 @@
255255 `import O := Out;` uses the name `O` instead. A module can't import
256256 itself, directly or through others.
257257
258+**`import SYSTEM`.** `SYSTEM` is no file but a module the language
259+gives, of the operations that reach memory other than through the
260+program's variables, or let something outside the program do so. A
261+module that does any of these says `import SYSTEM;`, and it is an
262+error to do them in one that doesn't:
263+
264+- `SYSTEM.ADR`, `SYSTEM.PTR`, `SYSTEM.SYSCALL` and `SYSTEM.ARGV`
265+ (section 6.8), which are written so, by that name;
266+- `p + i`, `p - i`, `p[i]` and `p[lo..hi]` on a pointer (section 6.4);
267+- a conversion to a pointer or a procedure type from an integer or
268+ from another pointer or procedure type, or from a pointer or a
269+ procedure to an integer (section 6.7), and a string literal given
270+ where a `^U8` is wanted (section 5.10);
271+- a view whose type holds a pointer, a view written over a variable
272+ whose type holds anything but integers and reals, and `BYTES` of
273+ such a variable (sections 6.8 and 6.9);
274+- `extern` and `link` of a library (section 9), code procedures
275+ (section 8) and `check off;` (below).
276+
277+`SYSTEM` can't be imported under another name, so that every module
278+that reaches memory so says it by that name. What such a module does
279+is its own to get right: section 10 says what isn't defined. A module
280+that imports one that says `import SYSTEM` needn't say it itself; it
281+trusts that module as it trusts the library, whose `Rt`, `Mem`,
282+`Heap`, `Sys`, `Thread` and others say it.
283+
258284 **Generic modules** take types as parameters:
259285
260286 ```
@@ -341,7 +367,7 @@
341367 (the library, `doc/raz-lib.md`) are kept for it: a program or another library doesn't
342368 name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Guarded`,
343369 `Heap`, `List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`,
344-`Sort`, `Str`, `Sys`, `Task` or `Thread`, nor any name the library takes later. The
370+`Sort`, `Str`, `Sys`, `SYSTEM`, `Task` or `Thread`, nor any name the library takes later. The
345371 modules of any other library share a prefix of their own, as an X11
346372 library's `Xauth` and `Xkeys` do, and a program that wants a shorter
347373 name imports one: `import Auth := Xauth;`.
@@ -371,11 +397,13 @@
371397 are zero before any body runs. `return;` ends a body early.
372398
373399 **`link "libX11.so.6";`** names a shared library to load at run time,
374-for the `extern` declarations of section 9. **`link static;`** says the
400+for the `extern` declarations of section 9, in a module that imports
401+`SYSTEM`. **`link static;`** says the
375402 module needs a program without C (section 10): it makes its own system
376403 calls, or its own threads, which the C library wouldn't know about.
377404
378-**`check off;`**, after the links, leaves out of the module's own code
405+**`check off;`**, after the links, in a module that imports `SYSTEM`,
406+leaves out of the module's own code
379407 the checks of indexes and slice bounds of section 6.5, and that a slice
380408 of a pointer doesn't run backwards (section 6.4), for speed where the
381409 program is sure of them. An index out of range there reads or writes
@@ -537,15 +565,15 @@
537565
538566 A slice is made from an array (`a[2..5]`, or an array used where a
539567 slice is wanted), from another slice, or from a pointer (`p[0..n]`). It
540-doesn't own what it points at. `LEN(s)` is its count, and `PTR(s)` its
541-pointer (section 6.8).
568+doesn't own what it points at. `LEN(s)` is its count, and
569+`SYSTEM.PTR(s)` its pointer (section 6.8).
542570
543571 `[]const T` is a slice whose elements can't be written through it. It
544572 is laid out as `[]T` is. Its elements are read, and it is sliced, and a
545573 slice of it is a `[]const T` too; but an element of it isn't assigned,
546574 given to `COPY` or `FILL` as the destination, changed by `INC` or `DEC`,
547-passed as a `var` argument, or written through a view, and `ADR` of one
548-is an error. A `[]T` and an array of `T` go silently where a
575+passed as a `var` argument, or written through a view, and
576+`SYSTEM.ADR` of one is an error. A `[]T` and an array of `T` go silently where a
549577 `[]const T` is wanted, never the other way (section 5.10). So a
550578 procedure that only reads a slice takes a `[]const T`, and every
551579 caller's slice or array fits it:
@@ -586,7 +614,7 @@
586614 other, on every target, even one whose machine can't reach a
587615 misaligned word in one access: the compiled code reaches it in parts
588616 there. But a field of a packed record whose type is aligned to more
589-than 1 byte, and anything inside it, has no address: `ADR` of it, a
617+than 1 byte, and anything inside it, has no address: `SYSTEM.ADR` of it, a
590618 `var` parameter of it and a slice of it are errors. Nor can it be
591619 reached through a `^VOLATILE` pointer, since a misaligned access can't
592620 be promised as one access (section 7.6). Its value can be copied out
@@ -730,7 +758,7 @@
730758 it is written, named or not: two `record x: I64; end` are two types.
731759 A named and an anonymous type are the
732760 same if the named one is built like the anonymous one, so that
733-`p := ADR(n)` works for `p: NodePtr` with `type NodePtr = ^Node`.
761+`p := SYSTEM.ADR(n)` works for `p: NodePtr` with `type NodePtr = ^Node`.
734762 Sameness doesn't pass through the anonymous type: with `type P1 =
735763 ^Node; P2 = ^Node;`, each of `P1` and `P2` is the same as `^Node`, but
736764 they are two declarations, so not the same as each other.
@@ -747,9 +775,10 @@
747775 - `A` is `^T` and `B` is `^VOLATILE T`;
748776 - `A` is `[n]T` and `B` is `[]T` or `[]const T`, or `A` is `[]T` and
749777 `B` is `[]const T`;
750-- `A` is a string literal and `B` is `[]const U8`, `^U8` (a pointer to the
751- bytes followed by a zero byte, as C wants), or `[n]U8` with `n` at
752- least the string's length (the rest is zero);
778+- `A` is a string literal and `B` is `[]const U8`, `[n]U8` with `n` at
779+ least the string's length (the rest is zero), or, in a module that
780+ imports `SYSTEM`, `^U8` (a pointer to the bytes followed by a zero
781+ byte, as C wants);
753782 - `A` is a procedure and `B` a procedure type with the same signature;
754783 - `A` is `INT` and `B` is `I64`, or `A` is `UINT` and `B` is `U64`,
755784 which loses nothing on any target;
@@ -942,6 +971,10 @@
942971 elements are `p^[i]`.
943972 - Pointers compare with `= # < <= > >=` as unsigned addresses.
944973 Procedure values compare with `=` and `#` only.
974+
975+`p + i`, `p - i`, `p[i]` and `p[lo..hi]` reach memory no variable of
976+the program names, and are allowed only in a module that imports
977+`SYSTEM` (section 3). `p^`, `p - q` and comparing are allowed in any.
945978
946979 ### 6.5 Arrays and slices
947980
@@ -1023,7 +1056,10 @@
10231056
10241057 There are no others: none from an integer to `BOOL` (`x # 0` says it),
10251058 none between pointers and integers other than `INT` and `UINT`, and none to
1026-or from records, arrays, slices, variants or `ERROR`. An untyped integer constant converts to a pointer as an `INT` does, as
1059+or from records, arrays, slices, variants or `ERROR`. The last three
1060+rows, from an integer to a pointer or a procedure, back, and from one
1061+pointer or procedure type to another, are allowed only in a module
1062+that imports `SYSTEM` (section 3). An untyped integer constant converts to a pointer as an `INT` does, as
10271063 in `^VOLATILE U32(0FEE00000H)`; it isn't assigned to a pointer without
10281064 the conversion. A conversion of a constant is made by the compiler, by
10291065 the same rules: `U8(300)` is 44. `U64(a = b)` is 0 or 1, and
@@ -1037,15 +1073,15 @@
10371073 | `SIZE(T)` | the size of type `T` in bytes, an untyped constant; `T` is a type, not a variable |
10381074 | `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant |
10391075 | `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant |
1040-| `ADR(x)` | the address of variable `x`, a `^T` |
1076+| `SYSTEM.ADR(x)` | the address of variable `x`, a `^T` |
10411077 | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) |
1042-| `PTR(s)` | the address the slice `s` starts at, a `^T` for `s: []T`, unchecked; for an array `a`, that of `a[0..LEN(a)]` |
1078+| `SYSTEM.PTR(s)` | the address the slice `s` starts at, a `^T` for `s: []T` or `[]const T`, unchecked; for an array `a`, that of `a[0..LEN(a)]` |
10431079 | `BYTES(x)` | the bytes of `x` as a `[]U8`: for a slice `s: []T`, its `LEN(s) * SIZE(T)` bytes, a `[]const U8` for a `[]const T`; for a variable of any other type `T`, its `SIZE(T)` bytes |
10441080 | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room |
10451081 | `MSG(e)` | the message of the error `e`, a `[]const U8` |
10461082 | `COPY(d, s)` | copies the elements of `s` to the start of `d`, both arrays or slices of one type `T`; `LEN(s)` must be at most `LEN(d)` |
10471083 | `FILL(d, x)` | sets every element of `d`, an array or a slice of elements of type `T`, to `x`, a value of `T` |
1048-| `SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six arguments, each a pointer or an integer no wider than `INT`; the raw result, an `INT` (a negative number is `-errno`) |
1084+| `SYSTEM.SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six arguments, each a pointer or an integer no wider than `INT`; the raw result, an `INT` (a negative number is `-errno`) |
10491085 | `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` |
10501086 | `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` |
10511087 | `FENCE` | a full memory barrier |
@@ -1058,9 +1094,9 @@
10581094 | `POPCOUNT(x)` | the number of one bits in `x`, or of members of the set `x`, an `INT`; `x` of an unsigned integer type or a set |
10591095 | `MIN(T)`, `MAX(T)` | the smallest, or the largest, value of the integer type `T`, an untyped constant; any other type is an error |
10601096 | `ARGC()` | the number of command line arguments, an `INT` |
1061-| `ARGV()` | the arguments, a `^^U8` (C strings) |
1097+| `SYSTEM.ARGV()` | the arguments, a `^^U8` (C strings) |
10621098
1063-`SYSCALL` exists only on Linux targets. Other systems have no stable
1099+`SYSTEM.SYSCALL` exists only on Linux targets. Other systems have no stable
10641100 system call interface, and their modules call the C library instead
10651101 (section 9).
10661102
@@ -1087,19 +1123,21 @@
10871123 `T` must be a type a view may take (section 6.9), since writing through
10881124 the slice must not make a value mean nothing: a `BOOL`, a set, an
10891125 enumeration, a slice, a variant, a procedure type, `ERROR`, or a record
1090-or array holding one, is an error. The slice is made, and checked, as
1126+or array holding one, is an error; and, outside a module that imports
1127+`SYSTEM`, so is a pointer, or a record or array holding one. The slice
1128+is made, and checked, as
10911129 any slice of the variable is, so its bytes follow the rules for it: the
10921130 bytes of a local can't be returned (section 7.3). Of a slice they are
1093-its elements, as `PTR(s)` gives them, whatever holds the slice, and
1094-can be written only if the slice can. Of any
1095-other variable they are its own, as `ADR` gives them: a `const`
1096-parameter, another module's variable, a view and a field of a packed
1097-record are errors, as they are for `ADR`, and so is a volatile variable.
1131+its elements, as `SYSTEM.PTR(s)` gives them, whatever holds the slice,
1132+and can be written only if the slice can. Of any other variable they
1133+are its own, as `SYSTEM.ADR` gives them: a `const` parameter, another
1134+module's variable, a view and a field of a packed record are errors,
1135+as they are for `SYSTEM.ADR`, and so is a volatile variable.
10981136 `x` must be a variable: a constant, a string constant too, is an error.
10991137 The bytes are in the target's order (appendix A).
11001138
1101-`PTR(s)` is the first of the two words of `s` (section 5.3), and is not
1102-checked. For a slice that isn't empty it is `ADR(s[0])`; for an empty
1139+`SYSTEM.PTR(s)` is the first of the two words of `s` (section 5.3), and
1140+is not checked. For a slice that isn't empty it is `SYSTEM.ADR(s[0])`; for an empty
11031141 one, where `s[0]` would stop the program, it is what the slice holds:
11041142 `nil` for a slice variable as it starts (section 4) and for `[]`, and
11051143 for one cut empty from another, `t[3..3]`, an address that mustn't be
@@ -1112,13 +1150,13 @@
11121150
11131151 proc valueOf(s: []const U8): Value;
11141152 begin
1115- return {n: LEN(s), p: PTR(s)}; (* "" too *)
1153+ return {n: LEN(s), p: SYSTEM.PTR(s)}; (* "" too *)
11161154 end;
11171155 ```
11181156
11191157 and `v.p[0..v.n]` is the slice again (section 6.4), empty when `v.n`
11201158 is 0, whatever `v.p` is. Reading through the pointer is as unchecked as
1121-through any pointer. `PTR(a)` of an array may be taken wherever the
1159+through any pointer. `SYSTEM.PTR(a)` of an array may be taken wherever the
11221160 slice `a[0..LEN(a)]` may be made, and is an error wherever it may not.
11231161
11241162 `COPY` is a statement, as `INC` is. It copies the bytes of `LEN(s)`
@@ -1147,7 +1185,7 @@
11471185 target, and may be volatile: a counter shared between threads is
11481186 `XADD(count, 1)`, one reached through a pointer `XADD(p^, 1)`. It is
11491187 evaluated once, and must have an address, so a field of a packed
1150-record and a view are errors, as they are for `ADR`. They are sequentially consistent, on every target; how a
1188+record and a view are errors, as they are for `SYSTEM.ADR`. They are sequentially consistent, on every target; how a
11511189 machine without atomic instructions makes them so is the
11521190 implementation's choice (appendix A).
11531191 ### 6.9 Views
@@ -1199,6 +1237,13 @@
11991237 name of one, a field or element of one, or what a pointer points at;
12001238 not a constant, nor the result of a call or of an operator.
12011239
1240+A view makes values from bytes, so where those bytes could be made
1241+into an address the module must import `SYSTEM` (section 3): a view
1242+whose `T` holds a pointer, and a view written over a variable `v`
1243+whose type holds anything but integers and reals, which would change
1244+a pointer, a `BOOL` or a slice behind its type. Reading any variable
1245+through a view of integers and reals is allowed in any module.
1246+
12021247 **Where its bytes lie.** The `T` lies within the variable `v` is part
12031248 of:
12041249
@@ -1218,7 +1263,8 @@
12181263 `I64` whose first byte is `mem[3]`. As a field of a packed record is
12191264 (section 5.5), it is read and written on every target, in parts where
12201265 the machine needs that; and, as such a field, a view has no address:
1221-`ADR` of it, a `var` argument of it and a slice of it are errors. Copy
1266+`SYSTEM.ADR` of it, a `var` argument of it and a slice of it are
1267+errors. Copy
12221268 it out to a variable, and back in. A view of a volatile variable is an
12231269 error too, since its access can't be promised as one (section 7.6): a
12241270 device's registers are reached through a `^VOLATILE` pointer to a
@@ -1233,12 +1279,13 @@
12331279 and may still: `mem[a].(I64) := n` does what
12341280
12351281 ```
1236-p := ^I64(ADR(mem[a]));
1282+p := ^I64(SYSTEM.ADR(mem[a]));
12371283 p^ := n;
12381284 ```
12391285
12401286 does, but with `a` checked against the length of `mem`, no pointer
1241-variable, and no address of a word that may be misaligned.
1287+variable, and no address of a word that may be misaligned, and no
1288+`import SYSTEM`.
12421289
12431290 `v` is evaluated once, and its check made, before the bytes are read
12441291 or written.
@@ -1463,8 +1510,14 @@
14631510 thread writes while another reads or writes it, unless both use
14641511 volatile accesses or the atomic built-ins, or a `FENCE` orders them,
14651512 holds no particular value then: a read of it gives a value it had or
1466-was given, or a mixture of their bytes. Nothing else of the program
1467-becomes undefined by it.
1513+was given, or a mixture of their bytes. For a variable of a basic,
1514+enumeration or set type, or a record or array of these, nothing else
1515+of the program becomes undefined by it. For one that holds a pointer,
1516+a procedure, a slice, an `ERROR` or a variant, a mixture may be an
1517+address no variable has, or a slice's address with another's length,
1518+or a case's tag with another's fields, and what follows isn't defined
1519+(section 10). A program that shares such a variable between threads
1520+orders their accesses, with a lock or the atomic built-ins.
14681521
14691522 ### 7.7 Defer
14701523
@@ -1631,8 +1684,8 @@
16311684 A `const` parameter is the caller's variable too, read in place,
16321685 whatever its size, with nothing copied; the procedure may only read it.
16331686 It may not assign it or a part of it (a field, an element of an array
1634-in it), give it or a part of it to a `var` parameter, take the `ADR` of
1635-either, write it through a view (section 6.9), or assign the names a
1687+in it), give it or a part of it to a `var` parameter, take the
1688+`SYSTEM.ADR` of either, write it through a view (section 6.9), or assign the names a
16361689 `match` arm gives its fields. What a slice or a pointer in it points at
16371690 isn't part of it: the procedure may write through them, as it may
16381691 through a value parameter's; but a slice it makes of an array in it is
@@ -1714,7 +1767,8 @@
17141767 Procedures don't nest.
17151768
17161769 **Code procedures.** A procedure whose body is `code` is machine code,
1717-written by hand in the target's assembly language (appendix A): one
1770+written by hand in the target's assembly language (appendix A), in a
1771+module that imports `SYSTEM`: one
17181772 instruction to each `;`, and labels for jumps, up to `end`:
17191773
17201774 ```
@@ -1761,7 +1815,8 @@
17611815 ```
17621816
17631817 An `extern` procedure or variable is found by its name in the C
1764-libraries the program links: the C library always, and each library
1818+libraries the program links, and is declared only in a module that
1819+imports `SYSTEM` (section 3), since C can reach any memory: the C library always, and each library
17651820 named by `link`.
17661821
17671822 An `extern` procedure's parameters and result are integers, `BOOL`,
@@ -1786,9 +1841,9 @@
17861841 ```
17871842 var ev: [24]I64; (* XEvent: 192 bytes *)
17881843 ...
1789-XNextEvent(dpy, ADR(ev));
1844+XNextEvent(dpy, SYSTEM.ADR(ev));
17901845 if I32(ev[0]) = KeyPress then
1791- key := ^XKeyEvent(ADR(ev));
1846+ key := ^XKeyEvent(SYSTEM.ADR(ev));
17921847 ...
17931848 end;
17941849 ```
@@ -1837,12 +1892,16 @@
18371892 **Defined behaviour.** Every variable starts at zero, integers wrap,
18381893 and indexing arrays and slices is checked, so a program's behaviour is
18391894 defined by this report, with one exception: memory reached other than
1840-through a checked variable. Dereferencing a stale or made-up pointer,
1841-indexing through a pointer, a slice kept after the call whose frame
1842-holds what it points at (section 7.3), an index out of range in a
1843-module that says `check off;`, writing a string's bytes, a code procedure that
1844-breaks its rules, and C code can read or write any memory, and what
1845-follows isn't defined. Dereferencing `nil`, dividing
1895+through a checked variable. In any module, three things reach it: a
1896+pointer used after the memory it points at was given back (by
1897+`Heap`'s `free` or an arena's reset, say), a slice kept after the
1898+call whose frame holds what it points at (section 7.3), and a race on
1899+a variable that holds a pointer, a slice or a variant (section 7.6).
1900+In a module that imports `SYSTEM`, so does what that module does there
1901+(section 3): a made-up pointer, indexing through a pointer, an index
1902+out of range under `check off;`, writing a string's bytes through a
1903+`^U8`, a code procedure that breaks its rules, and C code. Each can
1904+read or write any memory, and what follows isn't defined. Dereferencing `nil`, dividing
18461905 by zero, calling a `nil` procedure and running out of stack stop the
18471906 program, on every target, with status 2, as a failed check does.
18481907 Before it stops, the program prints to
@@ -1914,7 +1973,7 @@
19141973 implementation's.
19151974 - **What is the target's.** Code procedures (section 8) hold the
19161975 target's machine code, written in its assembly language, and exist
1917- only where the implementation says so; `SYSCALL` exists only on Linux targets (section 6.8). A record's
1976+ only where the implementation says so; `SYSTEM.SYSCALL` exists only on Linux targets (section 6.8). A record's
19181977 layout is the target's C layout (section 5.5), and the order of a
19191978 value's bytes is the target's (little-endian on x86-64).
19201979 - **The assembly language.** How a code procedure's instructions are

doc/report-history.md (history) modified +11 -0

@@ -277,3 +277,14 @@
277277 array may be read as one (sections 3, 4, 5.10, 6.8 and 8). It breaks
278278 a program that writes through these; raz 0.23 still gives them their
279279 old types until 0.24.
280+ And `import SYSTEM` (section 3): a module the language gives, with no
281+ file, that a module imports, by that name only, to reach memory other
282+ than through its variables: `SYSTEM.ADR`, `SYSTEM.PTR`,
283+ `SYSTEM.SYSCALL` and `SYSTEM.ARGV`, a pointer's arithmetic and
284+ indexing, conversions to and from pointers and procedures, views and
285+ `BYTES` that would make or change a pointer, `extern`, `link`, code
286+ procedures and `check off;` (sections 5.10, 6.4, 6.7, 6.8, 6.9, 8
287+ and 9). Section 10 says what is left undefined outside such modules,
288+ and 7.6 that a race on a pointer, a slice or a variant is undefined.
289+ It breaks every module that does these without it; raz 0.23 still
290+ takes them, and the built-ins unprefixed, until 0.24.

src/Parse.raz (history) modified +9 -1

@@ -4393,7 +4393,15 @@
43934393 o := Sym.newObj(Sym.oModule, alias, nil);
43944394 o^.pos := start;
43954395 try declare(o);
4396- if not generic then
4396+ if mname = Sym.system^.name then
4397+ (* no file: the language's own, which a grep must find *)
4398+ if (alias # mname) or generic then
4399+ return nil, Scan.markHelp(iargs.at, "SYSTEM is imported only as itself",
4400+ "every module that reaches memory through SYSTEM says so by that name: write import SYSTEM");
4401+ end;
4402+ o^.home := Sym.system;
4403+ named(o); (* not warned of when only its features are used *)
4404+ elsif not generic then
43974405 o^.home := try importModule(mname, iargs);
43984406 addImport(m, o^.home);
43994407 else

src/Sym.raz (history) modified +30 -3

@@ -156,6 +156,10 @@
156156 tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*: ^Type;
157157 tPoison*: ^Type; (* of a name reported as not declared *)
158158 curModule*: ^Module;
159+ (* SYSTEM, the module the language gives, of what reaches memory
160+ other than through a variable (report 3): no file, its scope the
161+ built-ins that may be written SYSTEM.ADR and the like *)
162+ system*: ^Module;
159163
160164 scopes: [MaxDepth]INT;
161165 depth: INT;
@@ -690,13 +694,26 @@
690694 return t;
691695 end;
692696
693-proc enter(name: []U8; cls: Class; t: ^Type; v: I64);
697+(* The name s, interned *)
698+proc internOf(s: []U8): INT;
699+begin
700+ return Scan.intern(ADR(s[0]), LEN(s));
701+end;
702+
703+(* Declares name in the scope s, exported, as a built-in is *)
704+proc enterIn(s: INT; name: []U8; cls: Class; t: ^Type; v: I64);
694705 var o: ^Obj;
695706 begin
696- o := newObj(cls, Scan.intern(ADR(name[0]), LEN(name)), t);
707+ o := newObj(cls, internOf(name), t);
697708 o^.val := v;
698709 o^.exported := true;
699- _ := insert(o, 0);
710+ _ := insert(o, s);
711+end;
712+
713+(* Declares name in the universe *)
714+proc enter(name: []U8; cls: Class; t: ^Type; v: I64);
715+begin
716+ enterIn(0, name, cls, t, v);
700717 end;
701718
702719 begin
@@ -762,4 +779,14 @@
762779 enter("CLZ", oBuiltin, nil, bClz);
763780 enter("CTZ", oBuiltin, nil, bCtz);
764781 enter("POPCOUNT", oBuiltin, nil, bPopcount);
782+ NEW(system, Heap.heap);
783+ system^.name := internOf("SYSTEM");
784+ system^.sname := system^.name;
785+ system^.scope := newScope();
786+ system^.body := -1;
787+ system^.done := true;
788+ enterIn(system^.scope, "ADR", oBuiltin, nil, bAdr);
789+ enterIn(system^.scope, "PTR", oBuiltin, nil, bPtr);
790+ enterIn(system^.scope, "SYSCALL", oBuiltin, nil, bSyscall);
791+ enterIn(system^.scope, "ARGV", oBuiltin, nil, bArgv);
765792 end Sym.

test/msg/SystemAlias.err (history) added +5 -0

@@ -0,0 +1,5 @@
1+conform/err/SystemAlias.raz:3:8: error: SYSTEM is imported only as itself
2+ 3 | import S := SYSTEM;
3+ | ^^^^^^^^^^^
4+help: every module that reaches memory through SYSTEM says so by that name: write import SYSTEM
5+raz: 1 error