From e33d42148b55003002cc8179cbcf99222f755452 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:00:47 +0000 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index 1231d9c..daa16bc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -380,6 +380,12 @@ (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still taken as the old form until v0.24.0; a pointer variable is the new form, so `XADD(p, 1)` is an error whose help says `p^`. +- `import SYSTEM` (report 2026-10.37) binds to `Sym.system`, no file, + whose scope holds `ADR`, `PTR`, `SYSCALL` and `ARGV`; it is never + warned of as unused, and an alias is refused. In v0.23.0 nothing is + fenced yet: the bare built-ins, pointer arithmetic and conversions, + views, `extern`, `link`, code procedures and `check off;` are taken + in any module, until v0.24.0. - `[]const T` (report 2026-10.37) is a slice type with `rdonly` set: 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's diff --git a/conform/err/SystemAlias.raz b/conform/err/SystemAlias.raz new file mode 100644 index 0000000..a18091e --- /dev/null +++ b/conform/err/SystemAlias.raz @@ -0,0 +1,9 @@ +(* SYSTEM is imported only by its own name (report section 3). *) +module SystemAlias; +import S := SYSTEM; + +proc main*; +begin +end; + +end SystemAlias. diff --git a/conform/run/SystemImport.expect b/conform/run/SystemImport.expect new file mode 100644 index 0000000..aba04b9 --- /dev/null +++ b/conform/run/SystemImport.expect @@ -0,0 +1 @@ +120avp diff --git a/conform/run/SystemImport.raz b/conform/run/SystemImport.raz new file mode 100644 index 0000000..37c1aff --- /dev/null +++ b/conform/run/SystemImport.raz @@ -0,0 +1,25 @@ +(* import SYSTEM (report section 3): SYSTEM.ADR, SYSTEM.PTR, + SYSTEM.SYSCALL and SYSTEM.ARGV are the built-ins of section 6.8, + written by that name, and a local given to SYSTEM.ADR is that + variable, however busy. *) +module SystemImport; +import Out, SYSTEM; + +proc main*; +var k, i: INT; p: ^INT; a: [4]U8; s: []U8; q: ^U8; +begin + for i := 1 to 10 do INC(k); end; + p := SYSTEM.ADR(k); + p^ := p^ + 100; + for i := 1 to 10 do INC(k); end; + Out.int(k); + a := "abcd"; + s := a; + q := SYSTEM.PTR(s); + Out.char(q^); + if SYSTEM.ARGV() # nil then Out.char('v'); end; + if SYSTEM.SYSCALL(39) > 0 then Out.char('p'); end; (* getpid *) + Out.char(10); +end; + +end SystemImport. diff --git a/doc/compiler.md b/doc/compiler.md index 8b2a8ba..c9ede36 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is eleven files of raz, about 12,500 lines of code (the +The compiler is eleven files of raz, about 12,600 lines of code (the budget is 14,000). It is its own compiler: a release of it, the one `BOOT` names, builds `src/`, and `make fixpoint` checks that what it makes makes itself again, byte for byte. Each release was built by the @@ -18,12 +18,12 @@ |---|---|---|---| | `src/Diag.raz` | 422 | 324 | Messages: positions, source excerpts, colour, counts | | `src/Scan.raz` | 1,169 | 965 | Symbols, literals, comments, positions | -| `src/Sym.raz` | 737 | 590 | Types, objects, scopes, layout | +| `src/Sym.raz` | 792 | 633 | Types, objects, scopes, layout | | `src/Chunk.raz` | 363 | 279 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,620 | 1,329 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,833 | 1,982 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/Item.raz` | 2,442 | 1,959 | Items: types, constants, variables, operators, conversions, calls | -| `src/Parse.raz` | 4,748 | 4,046 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/Gen.raz` | 2,840 | 1,986 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Item.raz` | 2,501 | 2,006 | Items: types, constants, variables, operators, conversions, calls | +| `src/Parse.raz` | 4,797 | 4,084 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 407 | 315 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 449 | 355 | Static and dynamic ELF, symbol table, line numbers | | `src/Raz.raz` | 418 | 333 | The command line | @@ -1885,6 +1885,16 @@ which its type's fingerprint has. Last, modules, at the end of the file. `compileFile` reads one module. +`import SYSTEM` (report 2026-10.37) finds no file: the import binds the +name to `Sym.system`, a module made once in `Sym`'s body whose scope +holds four of the built-ins, `ADR`, `PTR`, `SYSCALL` and `ARGV` (made by +`enterIn`), so that `qualIdent` finds `SYSTEM.ADR` as it finds any +export and `builtin` compiles it as `ADR`. It is marked used at the +import, since a module may import it only for `extern` or `link`. An +alias, `import S := SYSTEM`, is refused, so a grep finds every such +module. Until v0.24.0 nothing needs it: the bare built-ins and the rest +it fences are still taken anywhere. + `importModule` finds a module's file, trying the target's names first, and compiles it if it isn't yet. A module found in an `-I` or `RAZ_PATH` directory that holds a file `RAZ-VERSION` gets a line diff --git a/doc/raz-tutorial.7 b/doc/raz-tutorial.7 index 45f3cfe..e62ac63 100644 --- a/doc/raz-tutorial.7 +++ b/doc/raz-tutorial.7 @@ -478,7 +478,7 @@ .Sh 6. RECORDS AND POINTERS .Bd -literal -offset indent module Boxes; -import Fmt; +import Fmt, SYSTEM; type Point = record x, y: INT; end; @@ -502,8 +502,8 @@ begin b := {lo: {x: 1, y: 1}, hi: {x: 4, y: 3}}; Fmt.print("{}\en", [Fmt.int(area(b))]); - grow(ADR(b), 1); - p := ADR(b.hi); + grow(SYSTEM.ADR(b), 1); + p := SYSTEM.ADR(b.hi); p^.x := 10; Fmt.print("{} {}\en", [Fmt.int(area(b)), Fmt.int(b.hi.x)]); end; @@ -521,7 +521,7 @@ .Cm ^Box is a pointer to a .Cm Box , -.Cm ADR(b) +.Cm SYSTEM.ADR(b) the address of .Cm b , and @@ -536,13 +536,13 @@ apart, as an interpreter keeps a string, or as C wants them: .Bd -literal -offset indent module Spans; -import Out; +import Out, SYSTEM; type Span = record p: ^U8; n: INT; end; proc spanOf(s: []U8): Span; begin - return {p: PTR(s), n: LEN(s)}; + return {p: SYSTEM.PTR(s), n: LEN(s)}; end; proc text(sp: Span): []U8; @@ -570,18 +570,18 @@ .Pp .Cm LEN(s) is the length, and -.Cm PTR(s) +.Cm SYSTEM.PTR(s) the pointer, a .Cm ^U8 for a .Cm []U8 , with no check. -.Cm ADR(s[0]) +.Cm SYSTEM.ADR(s[0]) looks the same, and passes every test but the one with an empty string: there .Cm s[0] is out of range, and the program stops. -.Cm PTR +.Cm SYSTEM.PTR of an empty slice is what the slice holds, which is .Cm nil for one never set, so nothing needs testing first. @@ -592,7 +592,7 @@ .Cm sp.p is. Of an array, -.Cm PTR(a) +.Cm SYSTEM.PTR(a) is the address of its first element. .Sh 7. BYTES AND VIEWS A file, a network packet or a device hands a program bytes, and the @@ -757,18 +757,18 @@ or an enumeration isn't, since some bytes are none of its values: view an integer, and compare or convert it. A view may start at any byte, so it has no address: -.Cm ADR +.Cm SYSTEM.ADR of it is an error. Copy it to a variable first. Before views, a program made a pointer for each access, as -.Cm "p := ^I64(ADR(buf[0]))" +.Cm "p := ^I64(SYSTEM.ADR(buf[0]))" and then .Cm "p^ := 258" , which checks nothing. .Sh 8. MEMORY AND ALLOCATORS .Bd -literal -offset indent module Stack; -import Out, Mem, Heap, Arena; +import Out, Mem, Heap, Arena, SYSTEM; type Node = record next: ^Node; val: INT; end; @@ -797,7 +797,7 @@ end; Out.ln; if not Arena.init(ar, 100000) then return; end; - for i := 1 to 1000 do top := push(top, i, ADR(ar.base)); end; + for i := 1 to 1000 do top := push(top, i, SYSTEM.ADR(ar.base)); end; Out.int(top^.val); Out.ln; Arena.done(ar); (* every node at once *) end; @@ -1043,7 +1043,7 @@ .Pp A module that is sure of its indexes may say .Cm "check off;" -after its imports, for speed; an index out of range there reads or +after its imports (it must import SYSTEM), for speed; an index out of range there reads or writes whatever it reaches. The program carries line numbers for .Xr gdb 1 , @@ -1229,7 +1229,7 @@ .Sh 14. TALKING TO C .Bd -literal -offset indent module Qsort; -import Out, Fmt; +import Out, Fmt, SYSTEM; link "libm.so.6"; @@ -1251,7 +1251,7 @@ var a: [5]INT; i: INT; begin a := [30, 10, 50, 20, 40]; - qsort(^U8(ADR(a)), LEN(a), SIZE(INT), order); + qsort(^U8(SYSTEM.ADR(a)), LEN(a), SIZE(INT), order); for i := 0 to 4 do Out.int(a[i]); Out.char(' '); end; Out.ln; Fmt.print("{}\en", [Fmt.real(cbrt(27.0), 3)]); @@ -1264,6 +1264,12 @@ 3.000 .Ed .Pp +A module that says +.Cm "import SYSTEM" +may declare +.Cm extern +procedures and name libraries with +.Cm link . An .Cm extern procedure is found by its name in the C library, or in a library a @@ -1280,7 +1286,7 @@ as a pointer to an .Cm INT . A slice goes to C as two arguments, -.Cm PTR(s) +.Cm SYSTEM.PTR(s) and .Cm LEN(s) , which an empty slice gives too (section 6). @@ -1296,7 +1302,7 @@ .Sh 15. THREADS .Bd -literal -offset indent module Hits; -import Out, Thread; +import Out, Thread, SYSTEM; const Workers = 4; @@ -1384,7 +1390,7 @@ .It "goto" Ta "goto" Ta "none" .It "union" Ta "variant record" Ta "variant" .It "struct p {...}" Ta "record ... end" Ta "record ... end" -.It "&x" Ta "@x" Ta "ADR(x)" +.It "&x" Ta "@x" Ta "SYSTEM.ADR(x)" .It "p->f" Ta "p^.f" Ta "p^.f" .It "NULL" Ta "nil" Ta "nil" .It "malloc(sizeof *p)" Ta "new(p)" Ta "NEW(p, Heap.heap)" diff --git a/doc/raz.7 b/doc/raz.7 index 62258c2..042c6b8 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -166,11 +166,30 @@ .Cm main . .It .Cm "link \(dqlibX.so.1\(dq;" -loads a C library; +loads a C library (it needs +.Cm "import SYSTEM" ); .Cm "link static;" says the program must not use C. .Cm "check off;" -leaves the module's index and slice checks out. +leaves the module's index and slice checks out (it too needs +.Cm "import SYSTEM" ). +.It +.Cm "import SYSTEM;" +is no file but the language's module of what reaches memory by other +roads than variables. +A module needs it for +.Cm SYSTEM.ADR , +.Cm SYSTEM.PTR , +.Cm SYSTEM.SYSCALL +and +.Cm SYSTEM.ARGV ; +pointer arithmetic, indexing and slicing; +conversions to and from pointers; +.Cm extern , +.Cm link , +code procedures and +.Cm "check off;" . +It can't be renamed; a module that imports one that says it needn't. .El .Sh TYPES [5] Sizes are x86-64's. @@ -332,6 +351,8 @@ .Cm p[lo..hi] , a slice; a record's fields through it are .Cm p^.f . +Arithmetic, indexing and slicing need +.Cm "import SYSTEM" . .It Arrays and slices: .Cm a[i] , @@ -383,6 +404,8 @@ .It "pointer, procedure" Ta "INT, UINT, pointer" Ta "the address" .El .Pp +The last two rows need +.Cm "import SYSTEM" . There is no other: none between .Cm BOOL and integers, and none to records, arrays, slices, variants or @@ -416,7 +439,7 @@ variable can't be viewed. .Sh BUILT-IN PROCEDURES [6.8] -.Bl -tag -width "SYSCALL(n, x1, ...)" +.Bl -tag -width "SYSTEM.SYSCALL(n, x1, ...)" .It Fn SIZE T The size of type .Ar T , @@ -433,12 +456,12 @@ in the record type .Ar T , an untyped constant. -.It Fn ADR x +.It Fn SYSTEM.ADR x The address of the variable .Ar x . .It Fn LEN a The length of an array, a string constant or a slice. -.It Fn PTR s +.It Fn SYSTEM.PTR s The address the slice .Ar s starts at, a @@ -525,11 +548,11 @@ .Ar T . .It Cm FENCE A full memory barrier. -.It Fn SYSCALL n x1 ... +.It Fn SYSTEM.SYSCALL n x1 ... Linux system call .Ar n , with up to six arguments; minus an errno on failure. -.It Fn ARGC , Fn ARGV +.It Fn ARGC , Fn SYSTEM.ARGV The command line: its count, and its C strings. .El .Sh STATEMENTS @@ -646,7 +669,7 @@ .Cm "rdtsc; shl rdx, 32; or rax, rdx; ret;" . .It .Cm "extern proc printf(f: ^U8; ...): I32;" -is C's [9]: its parameters integers, reals, pointers and procedures. +(in a module that imports SYSTEM) is C's [9]: its parameters integers, reals, pointers and procedures. .Cm "extern var environ: ^^U8;" is C's variable. A raz procedure of those types is a C callback. diff --git a/doc/raz.md b/doc/raz.md index 90275ca..2737bfa 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -238,7 +238,7 @@ not a variable: nothing that could write it, or let something else write it, may name it or a field or element of it. So it can't be assigned, passed as a `var` argument, changed by `INC`, `DEC` or -`NEW`, or given to `ADR`; an array in it is sliced, or used where a +`NEW`, or given to `SYSTEM.ADR`; an array in it is sliced, or used where a slice is expected (the silent conversion of an array, section 5.10), only as a `[]const T`, whose elements can't be written (section 5.3); a slice in it may be sliced, since that makes a new slice and can't @@ -255,6 +255,32 @@ `import O := Out;` uses the name `O` instead. A module can't import itself, directly or through others. +**`import SYSTEM`.** `SYSTEM` is no file but a module the language +gives, of the operations that reach memory other than through the +program's variables, or let something outside the program do so. A +module that does any of these says `import SYSTEM;`, and it is an +error to do them in one that doesn't: + +- `SYSTEM.ADR`, `SYSTEM.PTR`, `SYSTEM.SYSCALL` and `SYSTEM.ARGV` + (section 6.8), which are written so, by that name; +- `p + i`, `p - i`, `p[i]` and `p[lo..hi]` on a pointer (section 6.4); +- a conversion to a pointer or a procedure type from an integer or + from another pointer or procedure type, or from a pointer or a + procedure to an integer (section 6.7), and a string literal given + where a `^U8` is wanted (section 5.10); +- a view whose type holds a pointer, a view written over a variable + whose type holds anything but integers and reals, and `BYTES` of + such a variable (sections 6.8 and 6.9); +- `extern` and `link` of a library (section 9), code procedures + (section 8) and `check off;` (below). + +`SYSTEM` can't be imported under another name, so that every module +that reaches memory so says it by that name. What such a module does +is its own to get right: section 10 says what isn't defined. A module +that imports one that says `import SYSTEM` needn't say it itself; it +trusts that module as it trusts the library, whose `Rt`, `Mem`, +`Heap`, `Sys`, `Thread` and others say it. + **Generic modules** take types as parameters: ``` @@ -341,7 +367,7 @@ (the library, `doc/raz-lib.md`) are kept for it: a program or another library doesn't name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Guarded`, `Heap`, `List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, -`Sort`, `Str`, `Sys`, `Task` or `Thread`, nor any name the library takes later. The +`Sort`, `Str`, `Sys`, `SYSTEM`, `Task` or `Thread`, nor any name the library takes later. The modules of any other library share a prefix of their own, as an X11 library's `Xauth` and `Xkeys` do, and a program that wants a shorter name imports one: `import Auth := Xauth;`. @@ -371,11 +397,13 @@ are zero before any body runs. `return;` ends a body early. **`link "libX11.so.6";`** names a shared library to load at run time, -for the `extern` declarations of section 9. **`link static;`** says the +for the `extern` declarations of section 9, in a module that imports +`SYSTEM`. **`link static;`** says the module needs a program without C (section 10): it makes its own system calls, or its own threads, which the C library wouldn't know about. -**`check off;`**, after the links, leaves out of the module's own code +**`check off;`**, after the links, in a module that imports `SYSTEM`, +leaves out of the module's own code the checks of indexes and slice bounds of section 6.5, and that a slice of a pointer doesn't run backwards (section 6.4), for speed where the program is sure of them. An index out of range there reads or writes @@ -537,15 +565,15 @@ A slice is made from an array (`a[2..5]`, or an array used where a slice is wanted), from another slice, or from a pointer (`p[0..n]`). It -doesn't own what it points at. `LEN(s)` is its count, and `PTR(s)` its -pointer (section 6.8). +doesn't own what it points at. `LEN(s)` is its count, and +`SYSTEM.PTR(s)` its pointer (section 6.8). `[]const T` is a slice whose elements can't be written through it. It is laid out as `[]T` is. Its elements are read, and it is sliced, and a slice of it is a `[]const T` too; but an element of it isn't assigned, given to `COPY` or `FILL` as the destination, changed by `INC` or `DEC`, -passed as a `var` argument, or written through a view, and `ADR` of one -is an error. A `[]T` and an array of `T` go silently where a +passed as a `var` argument, or written through a view, and +`SYSTEM.ADR` of one is an error. A `[]T` and an array of `T` go silently where a `[]const T` is wanted, never the other way (section 5.10). So a procedure that only reads a slice takes a `[]const T`, and every caller's slice or array fits it: @@ -586,7 +614,7 @@ other, on every target, even one whose machine can't reach a misaligned word in one access: the compiled code reaches it in parts there. But a field of a packed record whose type is aligned to more -than 1 byte, and anything inside it, has no address: `ADR` of it, a +than 1 byte, and anything inside it, has no address: `SYSTEM.ADR` of it, a `var` parameter of it and a slice of it are errors. Nor can it be reached through a `^VOLATILE` pointer, since a misaligned access can't be promised as one access (section 7.6). Its value can be copied out @@ -730,7 +758,7 @@ it is written, named or not: two `record x: I64; end` are two types. A named and an anonymous type are the same if the named one is built like the anonymous one, so that -`p := ADR(n)` works for `p: NodePtr` with `type NodePtr = ^Node`. +`p := SYSTEM.ADR(n)` works for `p: NodePtr` with `type NodePtr = ^Node`. Sameness doesn't pass through the anonymous type: with `type P1 = ^Node; P2 = ^Node;`, each of `P1` and `P2` is the same as `^Node`, but they are two declarations, so not the same as each other. @@ -747,9 +775,10 @@ - `A` is `^T` and `B` is `^VOLATILE T`; - `A` is `[n]T` and `B` is `[]T` or `[]const T`, or `A` is `[]T` and `B` is `[]const T`; -- `A` is a string literal and `B` is `[]const U8`, `^U8` (a pointer to the - bytes followed by a zero byte, as C wants), or `[n]U8` with `n` at - least the string's length (the rest is zero); +- `A` is a string literal and `B` is `[]const U8`, `[n]U8` with `n` at + least the string's length (the rest is zero), or, in a module that + imports `SYSTEM`, `^U8` (a pointer to the bytes followed by a zero + byte, as C wants); - `A` is a procedure and `B` a procedure type with the same signature; - `A` is `INT` and `B` is `I64`, or `A` is `UINT` and `B` is `U64`, which loses nothing on any target; @@ -942,6 +971,10 @@ elements are `p^[i]`. - Pointers compare with `= # < <= > >=` as unsigned addresses. Procedure values compare with `=` and `#` only. + +`p + i`, `p - i`, `p[i]` and `p[lo..hi]` reach memory no variable of +the program names, and are allowed only in a module that imports +`SYSTEM` (section 3). `p^`, `p - q` and comparing are allowed in any. ### 6.5 Arrays and slices @@ -1023,7 +1056,10 @@ There are no others: none from an integer to `BOOL` (`x # 0` says it), none between pointers and integers other than `INT` and `UINT`, and none to -or from records, arrays, slices, variants or `ERROR`. An untyped integer constant converts to a pointer as an `INT` does, as +or from records, arrays, slices, variants or `ERROR`. The last three +rows, from an integer to a pointer or a procedure, back, and from one +pointer or procedure type to another, are allowed only in a module +that imports `SYSTEM` (section 3). An untyped integer constant converts to a pointer as an `INT` does, as in `^VOLATILE U32(0FEE00000H)`; it isn't assigned to a pointer without the conversion. A conversion of a constant is made by the compiler, by the same rules: `U8(300)` is 44. `U64(a = b)` is 0 or 1, and @@ -1037,15 +1073,15 @@ | `SIZE(T)` | the size of type `T` in bytes, an untyped constant; `T` is a type, not a variable | | `SIZE(T.f)` | the size of field `f` of the record type `T`, an untyped constant | | `OFFSET(T, f)` | the byte offset of field `f` in the record type `T`, an untyped constant | -| `ADR(x)` | the address of variable `x`, a `^T` | +| `SYSTEM.ADR(x)` | the address of variable `x`, a `^T` | | `LEN(a)` | the number of elements of an array or a string constant (an untyped constant) or of a slice (an `INT`) | -| `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)]` | +| `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)]` | | `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 | | `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 | | `MSG(e)` | the message of the error `e`, a `[]const U8` | | `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)` | | `FILL(d, x)` | sets every element of `d`, an array or a slice of elements of type `T`, to `x`, a value of `T` | -| `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`) | +| `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`) | | `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` | | `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` | | `FENCE` | a full memory barrier | @@ -1058,9 +1094,9 @@ | `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 | | `MIN(T)`, `MAX(T)` | the smallest, or the largest, value of the integer type `T`, an untyped constant; any other type is an error | | `ARGC()` | the number of command line arguments, an `INT` | -| `ARGV()` | the arguments, a `^^U8` (C strings) | +| `SYSTEM.ARGV()` | the arguments, a `^^U8` (C strings) | -`SYSCALL` exists only on Linux targets. Other systems have no stable +`SYSTEM.SYSCALL` exists only on Linux targets. Other systems have no stable system call interface, and their modules call the C library instead (section 9). @@ -1087,19 +1123,21 @@ `T` must be a type a view may take (section 6.9), since writing through the slice must not make a value mean nothing: a `BOOL`, a set, an enumeration, a slice, a variant, a procedure type, `ERROR`, or a record -or array holding one, is an error. The slice is made, and checked, as +or array holding one, is an error; and, outside a module that imports +`SYSTEM`, so is a pointer, or a record or array holding one. The slice +is made, and checked, as any slice of the variable is, so its bytes follow the rules for it: the bytes of a local can't be returned (section 7.3). Of a slice they are -its elements, as `PTR(s)` gives them, whatever holds the slice, and -can be written only if the slice can. Of any -other variable they are its own, as `ADR` gives them: a `const` -parameter, another module's variable, a view and a field of a packed -record are errors, as they are for `ADR`, and so is a volatile variable. +its elements, as `SYSTEM.PTR(s)` gives them, whatever holds the slice, +and can be written only if the slice can. Of any other variable they +are its own, as `SYSTEM.ADR` gives them: a `const` parameter, another +module's variable, a view and a field of a packed record are errors, +as they are for `SYSTEM.ADR`, and so is a volatile variable. `x` must be a variable: a constant, a string constant too, is an error. The bytes are in the target's order (appendix A). -`PTR(s)` is the first of the two words of `s` (section 5.3), and is not -checked. For a slice that isn't empty it is `ADR(s[0])`; for an empty +`SYSTEM.PTR(s)` is the first of the two words of `s` (section 5.3), and +is not checked. For a slice that isn't empty it is `SYSTEM.ADR(s[0])`; for an empty one, where `s[0]` would stop the program, it is what the slice holds: `nil` for a slice variable as it starts (section 4) and for `[]`, and for one cut empty from another, `t[3..3]`, an address that mustn't be @@ -1112,13 +1150,13 @@ proc valueOf(s: []const U8): Value; begin - return {n: LEN(s), p: PTR(s)}; (* "" too *) + return {n: LEN(s), p: SYSTEM.PTR(s)}; (* "" too *) end; ``` and `v.p[0..v.n]` is the slice again (section 6.4), empty when `v.n` is 0, whatever `v.p` is. Reading through the pointer is as unchecked as -through any pointer. `PTR(a)` of an array may be taken wherever the +through any pointer. `SYSTEM.PTR(a)` of an array may be taken wherever the slice `a[0..LEN(a)]` may be made, and is an error wherever it may not. `COPY` is a statement, as `INC` is. It copies the bytes of `LEN(s)` @@ -1147,7 +1185,7 @@ target, and may be volatile: a counter shared between threads is `XADD(count, 1)`, one reached through a pointer `XADD(p^, 1)`. It is evaluated once, and must have an address, so a field of a packed -record and a view are errors, as they are for `ADR`. They are sequentially consistent, on every target; how a +record and a view are errors, as they are for `SYSTEM.ADR`. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation's choice (appendix A). ### 6.9 Views @@ -1199,6 +1237,13 @@ name of one, a field or element of one, or what a pointer points at; not a constant, nor the result of a call or of an operator. +A view makes values from bytes, so where those bytes could be made +into an address the module must import `SYSTEM` (section 3): a view +whose `T` holds a pointer, and a view written over a variable `v` +whose type holds anything but integers and reals, which would change +a pointer, a `BOOL` or a slice behind its type. Reading any variable +through a view of integers and reals is allowed in any module. + **Where its bytes lie.** The `T` lies within the variable `v` is part of: @@ -1218,7 +1263,8 @@ `I64` whose first byte is `mem[3]`. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: -`ADR` of it, a `var` argument of it and a slice of it are errors. Copy +`SYSTEM.ADR` of it, a `var` argument of it and a slice of it are +errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can't be promised as one (section 7.6): a device's registers are reached through a `^VOLATILE` pointer to a @@ -1233,12 +1279,13 @@ and may still: `mem[a].(I64) := n` does what ``` -p := ^I64(ADR(mem[a])); +p := ^I64(SYSTEM.ADR(mem[a])); p^ := n; ``` does, but with `a` checked against the length of `mem`, no pointer -variable, and no address of a word that may be misaligned. +variable, and no address of a word that may be misaligned, and no +`import SYSTEM`. `v` is evaluated once, and its check made, before the bytes are read or written. @@ -1463,8 +1510,14 @@ thread writes while another reads or writes it, unless both use volatile accesses or the atomic built-ins, or a `FENCE` orders them, holds no particular value then: a read of it gives a value it had or -was given, or a mixture of their bytes. Nothing else of the program -becomes undefined by it. +was given, or a mixture of their bytes. For a variable of a basic, +enumeration or set type, or a record or array of these, nothing else +of the program becomes undefined by it. For one that holds a pointer, +a procedure, a slice, an `ERROR` or a variant, a mixture may be an +address no variable has, or a slice's address with another's length, +or a case's tag with another's fields, and what follows isn't defined +(section 10). A program that shares such a variable between threads +orders their accesses, with a lock or the atomic built-ins. ### 7.7 Defer @@ -1631,8 +1684,8 @@ A `const` parameter is the caller's variable too, read in place, whatever its size, with nothing copied; the procedure may only read it. It may not assign it or a part of it (a field, an element of an array -in it), give it or a part of it to a `var` parameter, take the `ADR` of -either, write it through a view (section 6.9), or assign the names a +in it), give it or a part of it to a `var` parameter, take the +`SYSTEM.ADR` of either, write it through a view (section 6.9), or assign the names a `match` arm gives its fields. What a slice or a pointer in it points at isn't part of it: the procedure may write through them, as it may through a value parameter's; but a slice it makes of an array in it is @@ -1714,7 +1767,8 @@ Procedures don't nest. **Code procedures.** A procedure whose body is `code` is machine code, -written by hand in the target's assembly language (appendix A): one +written by hand in the target's assembly language (appendix A), in a +module that imports `SYSTEM`: one instruction to each `;`, and labels for jumps, up to `end`: ``` @@ -1761,7 +1815,8 @@ ``` An `extern` procedure or variable is found by its name in the C -libraries the program links: the C library always, and each library +libraries the program links, and is declared only in a module that +imports `SYSTEM` (section 3), since C can reach any memory: the C library always, and each library named by `link`. An `extern` procedure's parameters and result are integers, `BOOL`, @@ -1786,9 +1841,9 @@ ``` var ev: [24]I64; (* XEvent: 192 bytes *) ... -XNextEvent(dpy, ADR(ev)); +XNextEvent(dpy, SYSTEM.ADR(ev)); if I32(ev[0]) = KeyPress then - key := ^XKeyEvent(ADR(ev)); + key := ^XKeyEvent(SYSTEM.ADR(ev)); ... end; ``` @@ -1837,12 +1892,16 @@ **Defined behaviour.** Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program's behaviour is defined by this report, with one exception: memory reached other than -through a checked variable. Dereferencing a stale or made-up pointer, -indexing through a pointer, a slice kept after the call whose frame -holds what it points at (section 7.3), an index out of range in a -module that says `check off;`, writing a string's bytes, a code procedure that -breaks its rules, and C code can read or write any memory, and what -follows isn't defined. Dereferencing `nil`, dividing +through a checked variable. In any module, three things reach it: a +pointer used after the memory it points at was given back (by +`Heap`'s `free` or an arena's reset, say), a slice kept after the +call whose frame holds what it points at (section 7.3), and a race on +a variable that holds a pointer, a slice or a variant (section 7.6). +In a module that imports `SYSTEM`, so does what that module does there +(section 3): a made-up pointer, indexing through a pointer, an index +out of range under `check off;`, writing a string's bytes through a +`^U8`, a code procedure that breaks its rules, and C code. Each can +read or write any memory, and what follows isn't defined. Dereferencing `nil`, dividing by zero, calling a `nil` procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to @@ -1914,7 +1973,7 @@ implementation's. - **What is the target's.** Code procedures (section 8) hold the target's machine code, written in its assembly language, and exist - only where the implementation says so; `SYSCALL` exists only on Linux targets (section 6.8). A record's + only where the implementation says so; `SYSTEM.SYSCALL` exists only on Linux targets (section 6.8). A record's layout is the target's C layout (section 5.5), and the order of a value's bytes is the target's (little-endian on x86-64). - **The assembly language.** How a code procedure's instructions are diff --git a/doc/report-history.md b/doc/report-history.md index f8e5d14..d031c26 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -277,3 +277,14 @@ array may be read as one (sections 3, 4, 5.10, 6.8 and 8). It breaks a program that writes through these; raz 0.23 still gives them their old types until 0.24. + And `import SYSTEM` (section 3): a module the language gives, with no + file, that a module imports, by that name only, to reach memory other + than through its variables: `SYSTEM.ADR`, `SYSTEM.PTR`, + `SYSTEM.SYSCALL` and `SYSTEM.ARGV`, a pointer's arithmetic and + indexing, conversions to and from pointers and procedures, views and + `BYTES` that would make or change a pointer, `extern`, `link`, code + procedures and `check off;` (sections 5.10, 6.4, 6.7, 6.8, 6.9, 8 + and 9). 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 undefined. + It breaks every module that does these without it; raz 0.23 still + takes them, and the built-ins unprefixed, until 0.24. diff --git a/src/Parse.raz b/src/Parse.raz index 7fd9ee7..4224ba9 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -4393,7 +4393,15 @@ o := Sym.newObj(Sym.oModule, alias, nil); o^.pos := start; try declare(o); - if not generic then + if mname = Sym.system^.name then + (* no file: the language's own, which a grep must find *) + if (alias # mname) or generic then + return nil, Scan.markHelp(iargs.at, "SYSTEM is imported only as itself", + "every module that reaches memory through SYSTEM says so by that name: write import SYSTEM"); + end; + o^.home := Sym.system; + named(o); (* not warned of when only its features are used *) + elsif not generic then o^.home := try importModule(mname, iargs); addImport(m, o^.home); else diff --git a/src/Sym.raz b/src/Sym.raz index 87a0a2d..07bc1cc 100644 --- a/src/Sym.raz +++ b/src/Sym.raz @@ -156,6 +156,10 @@ tBool*, tF32*, tReal*, tRConst*, tError*, tConst*, tNil*, tStr*, tNone*, tPtrU8*, tBytes*: ^Type; tPoison*: ^Type; (* of a name reported as not declared *) curModule*: ^Module; + (* SYSTEM, the module the language gives, of what reaches memory + other than through a variable (report 3): no file, its scope the + built-ins that may be written SYSTEM.ADR and the like *) + system*: ^Module; scopes: [MaxDepth]INT; depth: INT; @@ -690,13 +694,26 @@ return t; end; -proc enter(name: []U8; cls: Class; t: ^Type; v: I64); +(* The name s, interned *) +proc internOf(s: []U8): INT; +begin + return Scan.intern(ADR(s[0]), LEN(s)); +end; + +(* Declares name in the scope s, exported, as a built-in is *) +proc enterIn(s: INT; name: []U8; cls: Class; t: ^Type; v: I64); var o: ^Obj; begin - o := newObj(cls, Scan.intern(ADR(name[0]), LEN(name)), t); + o := newObj(cls, internOf(name), t); o^.val := v; o^.exported := true; - _ := insert(o, 0); + _ := insert(o, s); +end; + +(* Declares name in the universe *) +proc enter(name: []U8; cls: Class; t: ^Type; v: I64); +begin + enterIn(0, name, cls, t, v); end; begin @@ -762,4 +779,14 @@ enter("CLZ", oBuiltin, nil, bClz); enter("CTZ", oBuiltin, nil, bCtz); enter("POPCOUNT", oBuiltin, nil, bPopcount); + NEW(system, Heap.heap); + system^.name := internOf("SYSTEM"); + system^.sname := system^.name; + system^.scope := newScope(); + system^.body := -1; + system^.done := true; + enterIn(system^.scope, "ADR", oBuiltin, nil, bAdr); + enterIn(system^.scope, "PTR", oBuiltin, nil, bPtr); + enterIn(system^.scope, "SYSCALL", oBuiltin, nil, bSyscall); + enterIn(system^.scope, "ARGV", oBuiltin, nil, bArgv); end Sym. diff --git a/test/msg/SystemAlias.err b/test/msg/SystemAlias.err new file mode 100644 index 0000000..b2413a5 --- /dev/null +++ b/test/msg/SystemAlias.err @@ -0,0 +1,5 @@ +conform/err/SystemAlias.raz:3:8: error: SYSTEM is imported only as itself + 3 | import S := SYSTEM; + | ^^^^^^^^^^^ +help: every module that reaches memory through SYSTEM says so by that name: write import SYSTEM +raz: 1 error