From 2411fde85f3a9ff9bd48baa74bcebaac2839efc6 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:46:13 +0100 Subject: [PATCH] Step 5: the system integer INT and UINT (8 bytes here) give LEN of a slice, p - q, ARGC, SYSCALL and pointer conversions, which now take only INT/UINT (or an untyped const). INT widens to I64, UINT to U64, in assignment, args and returns; not in exprs, for, INC/DEC, a, b := f() or return f(x). main and Rt.exit take INT; NEW passes INT sizes; an int const past an extern's params goes as INT. Also from report 2026-10.11: another module's exported var is read-only (no :=, var arg, INC, NEW, ADR, array slice; match on a copy); check off; drops a module's index/slice checks; shifts count mod the operand's width; the lowest of I8/I16/I32 div or mod -1 now faults as I64's did; CAS/XCHG/XADD take INT, UINT, ptrs only. Lib, examples, tools and tests to sec. 11: INT for lengths, counts, fds, pids, statuses; I32 for a char (Str.encode/decode); I64 kept for file size/pos, mtime, clock/now, Str.toInt, Opt.number, Out.int, Fmt.int; size_t as UINT in C. conform/run/Generic's Stack exports a pointer, as the new rule asks. Report 7.7's Head example: its vars are INTs, as Sys now gives. New tests for the above; vim/emacs know INT. --- diff --git a/CLAUDE.md b/CLAUDE.md index 56b1a2b..cbefd12 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -139,7 +139,7 @@ program the entry point stores C's `exit` in `Rt.cExit`, found by name like `Rt.exit`, before any module body runs. - Past an extern's parameters, a string goes as a C string and an - integer constant as an `INT` (an `I64` until 2026-10.11 is built). + integer constant as an `INT`. - A real constant is held as an F64; one typed `F32` holds a value an F32 can hold. An `F32` in a register is its low 32 bits; the upper ones are left as they fall. @@ -153,7 +153,16 @@ threads have none, so a stack overflow in one ends the program by the signal with no message. - The smallest value of a signed type divided by -1 stops the program - (SIGFPE), as 6.1 says; until 2026-10.11 is built, only `I64`'s does. + (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64 + bits the code makes the division fault, as the machine wouldn't. In + a constant it is an error, as dividing by zero is. +- `INT` widens to `I64` (and `UINT` to `U64`) wherever a value is + assigned, passed or returned, `return a, b` included; not in `a, b := + f(x)`, `return f(x)`, a `for` or `INC`/`DEC`. An untyped constant + converts to a pointer, as an `INT`. +- Another module's exported variable, or a field or array element of + it, is read-only: an `extern` one too. A slice in it may be sliced, + since that can't change the variable; an array in it may not. - Warnings come only for a module with no errors. A procedure naming itself doesn't use it; a type named by a pointer in its own declaration does. An enumeration's names are never warned of, nor a diff --git a/PLAN.md b/PLAN.md index 5e61c4f..990accf 100644 --- a/PLAN.md +++ b/PLAN.md @@ -119,7 +119,7 @@ the IR written down; `doc/ir.md` is removed, and git keeps it. - [x] 4. The tests split: a conformance suite any implementation passes, apart from this compiler's own messages. -- [ ] 5. The system integer. +- [x] 5. The system integer. - [ ] 6. Optional reals: a target says whether it has them. - [ ] 6a. `+` joins string constants (report, section 4). - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only diff --git a/conform/err/AtomicI64.raz b/conform/err/AtomicI64.raz new file mode 100644 index 0000000..88e229d --- /dev/null +++ b/conform/err/AtomicI64.raz @@ -0,0 +1,7 @@ +module AtomicI64; +proc main*; +var n: I64; _ok: BOOL; +begin + _ok := CAS(ADR(n), 0, 1); +end; +end AtomicI64. diff --git a/conform/err/ExportAdr.raz b/conform/err/ExportAdr.raz new file mode 100644 index 0000000..1988584 --- /dev/null +++ b/conform/err/ExportAdr.raz @@ -0,0 +1,8 @@ +module ExportAdr; +import Owner; +proc main*; +var _p: ^INT; +begin + _p := ADR(Owner.n); +end; +end ExportAdr. diff --git a/conform/err/ExportAsSlice.raz b/conform/err/ExportAsSlice.raz new file mode 100644 index 0000000..06bbbf3 --- /dev/null +++ b/conform/err/ExportAsSlice.raz @@ -0,0 +1,8 @@ +module ExportAsSlice; +import Owner; +proc sum(_s: []INT): INT; begin return 0; end; +proc main*; +begin + sum(Owner.a); +end; +end ExportAsSlice. diff --git a/conform/err/ExportAssign.raz b/conform/err/ExportAssign.raz new file mode 100644 index 0000000..28eb7cf --- /dev/null +++ b/conform/err/ExportAssign.raz @@ -0,0 +1,7 @@ +module ExportAssign; +import Owner; +proc main*; +begin + Owner.n := 1; +end; +end ExportAssign. diff --git a/conform/err/ExportElem.raz b/conform/err/ExportElem.raz new file mode 100644 index 0000000..b123764 --- /dev/null +++ b/conform/err/ExportElem.raz @@ -0,0 +1,7 @@ +module ExportElem; +import Owner; +proc main*; +begin + Owner.a[2] := 1; +end; +end ExportElem. diff --git a/conform/err/ExportInc.raz b/conform/err/ExportInc.raz new file mode 100644 index 0000000..d26dac4 --- /dev/null +++ b/conform/err/ExportInc.raz @@ -0,0 +1,7 @@ +module ExportInc; +import Owner; +proc main*; +begin + INC(Owner.n); +end; +end ExportInc. diff --git a/conform/err/ExportSlice.raz b/conform/err/ExportSlice.raz new file mode 100644 index 0000000..1d2d8e6 --- /dev/null +++ b/conform/err/ExportSlice.raz @@ -0,0 +1,8 @@ +module ExportSlice; +import Owner; +proc main*; +var _s: []INT; +begin + _s := Owner.a[0..2]; +end; +end ExportSlice. diff --git a/conform/err/ExportVarParam.raz b/conform/err/ExportVarParam.raz new file mode 100644 index 0000000..3a5e8bc --- /dev/null +++ b/conform/err/ExportVarParam.raz @@ -0,0 +1,8 @@ +module ExportVarParam; +import Owner; +proc put(var x: INT); begin x := 1; end; +proc main*; +begin + put(Owner.n); +end; +end ExportVarParam. diff --git a/conform/err/IntFor.raz b/conform/err/IntFor.raz new file mode 100644 index 0000000..1df6e21 --- /dev/null +++ b/conform/err/IntFor.raz @@ -0,0 +1,7 @@ +module IntFor; +proc main*; +var i: I64; s: []U8; +begin + for i := 0 to LEN(s) - 1 do end; +end; +end IntFor. diff --git a/conform/err/IntFromPointer.raz b/conform/err/IntFromPointer.raz new file mode 100644 index 0000000..7ec7549 --- /dev/null +++ b/conform/err/IntFromPointer.raz @@ -0,0 +1,7 @@ +module IntFromPointer; +proc main*; +var i: I64; p: ^U8; +begin + i := I64(p); +end; +end IntFromPointer. diff --git a/conform/err/IntInc.raz b/conform/err/IntInc.raz new file mode 100644 index 0000000..89f91d2 --- /dev/null +++ b/conform/err/IntInc.raz @@ -0,0 +1,7 @@ +module IntInc; +proc main*; +var i: I64; s: []U8; +begin + INC(i, LEN(s)); +end; +end IntInc. diff --git a/conform/err/IntMix.raz b/conform/err/IntMix.raz new file mode 100644 index 0000000..557970d --- /dev/null +++ b/conform/err/IntMix.raz @@ -0,0 +1,7 @@ +module IntMix; +proc main*; +var i: I64; s: []U8; +begin + if i < LEN(s) then end; +end; +end IntMix. diff --git a/conform/err/IntNarrow.raz b/conform/err/IntNarrow.raz new file mode 100644 index 0000000..71612ce --- /dev/null +++ b/conform/err/IntNarrow.raz @@ -0,0 +1,7 @@ +module IntNarrow; +proc main*; +var i: I64; n: INT; +begin + n := i; +end; +end IntNarrow. diff --git a/conform/err/IntPointer.raz b/conform/err/IntPointer.raz new file mode 100644 index 0000000..69e0296 --- /dev/null +++ b/conform/err/IntPointer.raz @@ -0,0 +1,7 @@ +module IntPointer; +proc main*; +var i: I64; p: ^U8; +begin + p := ^U8(i); +end; +end IntPointer. diff --git a/conform/err/IntResults.raz b/conform/err/IntResults.raz new file mode 100644 index 0000000..87900bf --- /dev/null +++ b/conform/err/IntResults.raz @@ -0,0 +1,8 @@ +module IntResults; +proc two: (INT, INT); begin return 1, 2; end; +proc main*; +var a, b: I64; +begin + a, b := two(); +end; +end IntResults. diff --git a/conform/err/mods/Owner.raz b/conform/err/mods/Owner.raz new file mode 100644 index 0000000..722efbe --- /dev/null +++ b/conform/err/mods/Owner.raz @@ -0,0 +1,3 @@ +module Owner; +var n*: INT; a*: [4]INT; +end Owner. diff --git a/conform/run/Args.raz b/conform/run/Args.raz index f069599..a642652 100644 --- a/conform/run/Args.raz +++ b/conform/run/Args.raz @@ -1,8 +1,8 @@ module Args; import Out; -proc main*: I64; -var i, n: I64; a: ^^U8; s: ^U8; +proc main*: INT; +var i, n: INT; a: ^^U8; s: ^U8; begin a := ARGV(); for i := 1 to ARGC() - 1 do diff --git a/conform/run/AtExit.raz b/conform/run/AtExit.raz index 7abbb41..e4618f5 100644 --- a/conform/run/AtExit.raz +++ b/conform/run/AtExit.raz @@ -23,7 +23,7 @@ begin end; -proc main*: I64; +proc main*: INT; var i, full: I64; begin Rt.atExit(first); diff --git a/conform/run/Generic.raz b/conform/run/Generic.raz index e8a1bf0..e634f4a 100644 --- a/conform/run/Generic.raz +++ b/conform/run/Generic.raz @@ -6,12 +6,12 @@ proc main*; var l: Strs.List; v: I64; s: []U8; ok: BOOL; b: Buf.Buf; n: ^Ints.Node; begin - ok := Ints.push(ADR(Stack.s), 7); - ok := More.push(ADR(Stack.s), 8); - Fmt.print("{} {} {}\n", [Fmt.int(Stack.top()), Fmt.int(Stack.s.len), Fmt.bool(ok)]); - n := Stack.s.head; - v, ok := Ints.pop(ADR(Stack.s)); - Fmt.print("{} {}\n", [Fmt.int(v), Fmt.int(Stack.s.len)]); + ok := Ints.push(Stack.p, 7); + ok := More.push(Stack.p, 8); + Fmt.print("{} {} {}\n", [Fmt.int(Stack.top()), Fmt.int(Stack.p^.len), Fmt.bool(ok)]); + n := Stack.p^.head; + v, ok := Ints.pop(Stack.p); + Fmt.print("{} {}\n", [Fmt.int(v), Fmt.int(Stack.p^.len)]); Strs.init(ADR(l), Heap.heap); ok := Strs.push(ADR(l), "b"); ok := Strs.push(ADR(l), "a"); diff --git a/conform/run/HeapCheck.raz b/conform/run/HeapCheck.raz index ac430ad..94a8b56 100644 --- a/conform/run/HeapCheck.raz +++ b/conform/run/HeapCheck.raz @@ -19,7 +19,7 @@ show(Mem.alloc(Heap.heap, 10, 8192)); p := Mem.alloc(Heap.heap, 10, 64); show(p); - if I64(p) mod 64 = 0 then Out.str("aligned\n"); end; + if INT(p) mod 64 = 0 then Out.str("aligned\n"); end; Mem.free(Heap.heap, p); q := Mem.alloc(Heap.heap, 100000, 8); show(q); diff --git a/conform/run/MapRemove.raz b/conform/run/MapRemove.raz index 75277f8..d40f37d 100644 --- a/conform/run/MapRemove.raz +++ b/conform/run/MapRemove.raz @@ -18,7 +18,7 @@ proc main*; var - m: Ints.Map; want: [N]I64; i, k, v, n, sum, seed: I64; + m: Ints.Map; want: [N]I64; i: INT; k, v, n, sum, seed: I64; found, _ok: BOOL; begin Ints.init(ADR(m), Heap.heap); diff --git a/conform/run/Procs.raz b/conform/run/Procs.raz index a5b8313..19e48b7 100644 --- a/conform/run/Procs.raz +++ b/conform/run/Procs.raz @@ -88,7 +88,7 @@ return a + b; end; -proc main*: I64; +proc main*: INT; var a, b: I64; p, q: Pair; f: Op; bg: Big; begin if even(10) and odd(7) and not even(3) then Out.str("parity ok"); Out.ln; end; diff --git a/conform/run/Strings.raz b/conform/run/Strings.raz index ee9084b..80fa234 100644 --- a/conform/run/Strings.raz +++ b/conform/run/Strings.raz @@ -6,7 +6,7 @@ var buf: [8]U8; proc count(xs: []I64): I64; -var i, s: I64; +var i: INT; s: I64; begin s := 0; for i := 0 to LEN(xs) - 1 do s := s + xs[i]; end; diff --git a/conform/run/SystemInt.expect b/conform/run/SystemInt.expect new file mode 100644 index 0000000..555005b --- /dev/null +++ b/conform/run/SystemInt.expect @@ -0,0 +1,5 @@ +5 8 3 7 +7 28 1 +6 -32 6 2 +70 +6 5 2 11 120 diff --git a/conform/run/SystemInt.raz b/conform/run/SystemInt.raz new file mode 100644 index 0000000..7bdf9b1 --- /dev/null +++ b/conform/run/SystemInt.raz @@ -0,0 +1,74 @@ +module SystemInt; +(* INT and UINT: what gives them, where they widen, shifts modulo the + width, and another module's variables read but not changed *) +import Out, Shared; + +var cells: [10]I32; + +proc wide(x: I64): I64; +begin + return x + 1; +end; + +(* an INT returned as an I64 *) +proc len(s: []U8): I64; +begin + return LEN(s); +end; + +proc main*; +var + n: INT; i: I64; u: UINT; w: U64; b: U8; h: I32; k: INT; + p, q: ^I32; copy: [3]INT; +begin + n := LEN("four"); (* untyped, so it fits any integer *) + b := SIZE(I64); + i := n; (* INT into I64 *) + i := wide(n); + u := 7; + w := u; (* UINT into U64 *) + Out.int(i); Out.str(" "); Out.int(I64(b)); Out.str(" "); Out.int(len("abc")); + Out.str(" "); Out.int(I64(w)); Out.ln; + + p := ADR(cells[2]); + q := ADR(cells[9]); + n := q - p; (* an INT *) + u := UINT(q) - UINT(p); + Out.int(n); Out.str(" "); Out.int(I64(u)); Out.str(" "); + Out.int(INT(^I32(INT(p) + 4) - p)); Out.ln; + + (* a shift counts modulo the width of its operand's type *) + b := 3; + k := 9; + Out.int(I64(b shl k)); Out.str(" "); + h := -64; + k := 33; + Out.int(I64(h shr k)); Out.str(" "); + Out.int(I64(U8(3) shl 9)); Out.str(" "); + n := 1; + k := 65; + Out.int(n shl k); Out.ln; + + (* an index of any integer type, by its value *) + cells[U8(3)] := 30; + cells[I8(4)] := 40; + Out.int(I64(cells[UINT(3)] + cells[U64(4)])); Out.ln; + + Shared.bump; + Shared.bump; + copy := Shared.words; + copy[0] := 9; (* the importer's own copy *) + Shared.bytes[0] := 'x'; (* what the slice points at *) + match Shared.shape of + | circle(r): r := r + 1; Out.int(r); Out.str(" "); + | dot: + end; + match Shared.shape of + | circle(r): Out.int(r); Out.str(" "); + | dot: + end; + Out.int(Shared.count); Out.str(" "); Out.int(copy[0] + copy[1]); Out.str(" "); + Out.int(I64(Shared.bytes[0])); Out.ln; +end; + +end SystemInt. diff --git a/conform/run/mods/Shared.raz b/conform/run/mods/Shared.raz new file mode 100644 index 0000000..65a9b79 --- /dev/null +++ b/conform/run/mods/Shared.raz @@ -0,0 +1,22 @@ +module Shared; +(* Exported variables, which only this module changes *) + +type Shape* = variant dot; circle(r: INT); end; + +var + count*: INT; + words*: [3]INT; + bytes*: []U8; + shape*: Shape; + store: [4]U8; + +proc bump*; +begin + INC(count); + words[1] := count; +end; + +begin + bytes := store; + shape := circle(5); +end Shared. diff --git a/conform/run/mods/Stack.raz b/conform/run/mods/Stack.raz index 6048ee4..c47aea8 100644 --- a/conform/run/mods/Stack.raz +++ b/conform/run/mods/Stack.raz @@ -1,7 +1,9 @@ module Stack; import Ints := List(I64), Heap; -var s*: Ints.List; +var + s: Ints.List; + p*: ^Ints.List; (* s, which only this module could change by its name *) proc top*(): I64; begin @@ -10,4 +12,5 @@ begin Ints.init(ADR(s), Heap.heap); + p := ADR(s); end Stack. diff --git a/doc/compiler.md b/doc/compiler.md index 00f3935..3755a59 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1,6 +1,6 @@ # Reading the raz compiler -The compiler is nine files of Free Pascal, under 6,000 lines of code. +The compiler is nine files of Free Pascal, under 6,500 lines of code. This guide walks through them in the order they are best read: from the characters of a source file to the bytes of an executable. It assumes you know the language from `doc/raz.md`, and enough x86-64 to read a @@ -10,10 +10,10 @@ |---|---|---|---| | `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | -| `src/sym.pas` | 593 | 494 | Types, objects, scopes, layout | +| `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout | | `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,631 | 1,408 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,444 | 2,203 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/gen.pas` | 1,733 | 1,481 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,464 | 2,221 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 227 | 179 | The command line, the entry points | @@ -270,14 +270,21 @@ on the right of `*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow. Narrow integers are brought back to their width after each operation (`Norm`), which is how `U8` arithmetic wraps. +Since they are computed in 64 bits, a shift's count is cut to their +width first, and `DivTrap` makes the smallest value of one divided by +-1 fault as an `I64`'s does. `=` and `#` on arrays, slices and strings make a slice of each side (`SeqSlice`), compare the lengths, and then the bytes with `repe cmpsb`, right where the comparison stands: no call. Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a -variable into an item for part of it, mostly by adding to `off`. -`Index` and `Slice` emit the bounds checks, and `Convert` the check of -an enumeration's position; `Check` jumps over a call to `Rt.fail` when +variable into an item for part of it, mostly by adding to `off`. An +item for another module's exported variable, or part of it, is `ro`: +it isn't a variable (`isVar`), and `NotReadOnly` says why where one is +wanted; what it points at is. +`Index` and `Slice` emit the bounds checks, unless the module says +`check off;` (`checks`, which `CompileFile` sets for each module), and +`Convert` the check of an enumeration's position; `Check` jumps over a call to `Rt.fail` when the check holds. The call passes what failed, the values involved and its own address, by which `Rt` finds the place; that code has the place of the offending expression (`LineCol` in `scan.pas`), and the @@ -285,7 +292,9 @@ Assignment and conversion: `Coerce` makes a value fit the type it's going to, or reports that it can't; it's where a string literal becomes -a slice, an array a slice, an untyped constant a typed one. `Assign` +a slice, an array a slice, an untyped constant a typed one, and an +`INT` an `I64` (`Widens`). `CoerceExact` is for where the types must +be the same, as in a `for` loop, `INC` and several results. `Assign` stores, copying records and arrays with `rep movsb`. `Convert` is the written conversion, `T(x)`. diff --git a/doc/raz.md b/doc/raz.md index f40fd37..955c20a 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -931,7 +931,7 @@ (* Prints the first line of the file at path. *) proc head(path: []U8): BOOL; -var fd, n, i: I64; buf: [256]U8; +var fd, n, i: INT; buf: [256]U8; begin fd := Sys.openRead(path); if fd < 0 then return false; end; diff --git a/editor/emacs/raz-mode.el b/editor/emacs/raz-mode.el index 3652c46..8775d86 100644 --- a/editor/emacs/raz-mode.el +++ b/editor/emacs/raz-mode.el @@ -28,7 +28,7 @@ "variant" "VOLATILE" "while" "xor")) (defconst raz-types - '("I8" "I16" "I32" "I64" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) + '("I8" "I16" "I32" "I64" "INT" "UINT" "U8" "U16" "U32" "U64" "BOOL" "F32" "F64" "ERROR")) (defconst raz-builtins '("SIZE" "ADR" "LEN" "NEW" "MSG" "SYSCALL" "FENCE" "CAS" "XCHG" "XADD" diff --git a/editor/vim/syntax/raz.vim b/editor/vim/syntax/raz.vim index 6a30213..925e712 100644 --- a/editor/vim/syntax/raz.vim +++ b/editor/vim/syntax/raz.vim @@ -12,7 +12,7 @@ syntax keyword razKeyword match mod module not of or packed proc record syntax keyword razKeyword repeat return set shl shr then to type until var syntax keyword razKeyword variant VOLATILE while xor -syntax keyword razType I8 I16 I32 I64 U8 U16 U32 U64 BOOL F32 F64 ERROR +syntax keyword razType I8 I16 I32 I64 INT UINT U8 U16 U32 U64 BOOL F32 F64 ERROR syntax keyword razBuiltin SIZE ADR LEN NEW MSG SYSCALL FENCE CAS XCHG XADD syntax keyword razBuiltin ARGC ARGV INC DEC syntax keyword razConstant true false nil diff --git a/examples/Args.raz b/examples/Args.raz index e79fb27..af12048 100644 --- a/examples/Args.raz +++ b/examples/Args.raz @@ -3,16 +3,16 @@ import Out; (* The length of a C string. *) -proc len(s: ^U8): I64; -var n: I64; +proc len(s: ^U8): INT; +var n: INT; begin n := 0; while s[n] # 0 do n := n + 1; end; return n; end; -proc main*: I64; -var i: I64; s: ^U8; +proc main*: INT; +var i: INT; s: ^U8; begin for i := 1 to ARGC() - 1 do s := ARGV()[i]; diff --git a/examples/Cpu.raz b/examples/Cpu.raz index 60b5538..df62220 100644 --- a/examples/Cpu.raz +++ b/examples/Cpu.raz @@ -16,7 +16,7 @@ 0C3H; (* ret *) proc main*; -var buf: [12]U8; i: I64; ok: BOOL; +var buf: [12]U8; i: INT; ok: BOOL; begin vendor(ADR(buf[0])); ok := true; diff --git a/examples/Flags.raz b/examples/Flags.raz index e2dea05..9cb677d 100644 --- a/examples/Flags.raz +++ b/examples/Flags.raz @@ -9,9 +9,9 @@ Perms = set of Perm; Mode = record user, group, other: Perms; end; -proc digit(p: Perms): I64; +proc digit(p: Perms): INT; begin - return I64(U8(p)); + return INT(U8(p)); end; proc show(p: Perms); diff --git a/examples/Printf.raz b/examples/Printf.raz index 90d6c0f..1ff611e 100644 --- a/examples/Printf.raz +++ b/examples/Printf.raz @@ -4,7 +4,7 @@ import Out, C; proc main*; -var i: I64; +var i: INT; begin for i := 1 to 3 do Out.str("raz says "); Out.int(i); Out.ln; diff --git a/examples/Pthreads.raz b/examples/Pthreads.raz index a38ad4f..268dcf2 100644 --- a/examples/Pthreads.raz +++ b/examples/Pthreads.raz @@ -8,13 +8,13 @@ N = 1000000; type - Part = record lo, hi, sum: I64; end; - Numbers = [N]I64; + Part = record lo, hi, sum: INT; end; + Numbers = [N]INT; var numbers: ^Numbers; proc work(arg: ^U8): ^U8; -var p: ^Part; i: I64; scratch: ^U8; +var p: ^Part; i: INT; scratch: ^U8; begin p := ^Part(arg); for i := p^.lo to p^.hi - 1 do @@ -28,8 +28,8 @@ return nil; end; -proc main*: I64; -var t: [Workers]U64; parts: [Workers]Part; i, total: I64; +proc main*: INT; +var t: [Workers]UINT; parts: [Workers]Part; i, total: INT; begin NEW(numbers, Heap.heap); for i := 0 to N - 1 do numbers^[i] := i + 1; end; diff --git a/examples/Sh.raz b/examples/Sh.raz index d613a07..656e8e3 100644 --- a/examples/Sh.raz +++ b/examples/Sh.raz @@ -21,13 +21,13 @@ type Command = record args: [maxWords][]U8; - n: I64; + n: INT; input, output, errors: []U8; (* the files of <, > or >>, and 2>; "" for none *) append: BOOL; (* output is >> *) end; Pipeline = record cmds: [maxCmds]Command; - n: I64; + n: INT; background: BOOL; end; (* What the parser reads: a word, |, &, ; or a line's end, <, >, >>, 2>, @@ -35,17 +35,17 @@ Token = (word, pipe, amp, sep, from, into, onto, errTo, eof); var - status: I64; (* of the last pipeline: $? *) + status: INT; (* of the last pipeline: $? *) interactive: BOOL; (* commands come from a terminal: job control *) - group: I64; (* the shell's process group *) + group: INT; (* the shell's process group *) arena: Arena.Arena; (* the words of what is being run *) s: []U8; (* the script being read *) - at: I64; (* where in it *) + at: INT; (* where in it *) text: []U8; (* the last word read *) (* "Sh: " and a message on standard error. *) proc say(f: []U8; args: []Fmt.Arg); -var b: Buf.Buf; _n: I64; +var b: Buf.Buf; _n: INT; begin Buf.init(ADR(b), Heap.heap); Fmt.write(ADR(b), "Sh: ", []); @@ -64,7 +64,7 @@ (* $NAME or $? at s[at], its value added to b; a $ before anything else is itself. *) proc dollar(b: ^Buf.Buf); -var start: I64; +var start: INT; begin INC(at); if (at < LEN(s)) and (s[at] = '?') then @@ -172,7 +172,7 @@ (* The program a command names: the name itself if it has a /, else the first executable file of that name in a directory of $PATH. *) proc find(name: []U8): ([]U8, BOOL); -var path, dir: []U8; i, start, err: I64; b: Buf.Buf; info: Sys.Info; +var path, dir: []U8; i, start, err: INT; b: Buf.Buf; info: Sys.Info; begin if Str.find(name, "/") >= 0 then return name, true; end; path := Sys.env("PATH"); @@ -191,8 +191,8 @@ end; (* The fd of a redirection's file, or -1 after saying why not. *) -proc redirect(path: []U8; how: Sys.Accesses): I64; -var fd: I64; +proc redirect(path: []U8; how: Sys.Accesses): INT; +var fd: INT; begin fd := Sys.open(path, how); if fd < 0 then say("{}: can't open it", [Fmt.str(path)]); return -1; end; @@ -202,7 +202,7 @@ (* cd, wait and exit, which change the shell itself, so it can't start them as programs; true if c is one. *) proc builtin(c: ^Command): BOOL; -var dir: []U8; n, pid, _code: I64; ok: BOOL; _st: Sys.Status; +var dir: []U8; n: I64; pid, _code: INT; ok: BOOL; _st: Sys.Status; begin if c^.args[0] = "cd" then if c^.n > 1 then dir := c^.args[1]; else dir := Sys.env("HOME"); end; @@ -217,7 +217,7 @@ n, ok := Str.toInt(c^.args[1]); if not ok then say("exit: {} isn't a number", [Fmt.str(c^.args[1])]); n := 2; end; end; - Rt.exit(n); + Rt.exit(INT(n)); else return false; end; @@ -238,7 +238,7 @@ has the terminal until they end. *) proc start(p: ^Pipeline); var - pids: [maxCmds]I64; fds: [3]I64; i, j, inNext, r, w, leader, joins, how, _pid: I64; + pids: [maxCmds]INT; fds: [3]INT; i, j, inNext, r, w, leader, joins, how, _pid: INT; c: ^Command; path: []U8; found, _ok: BOOL; st: Sys.Status; begin if (p^.n = 1) and not p^.background and builtin(ADR(p^.cmds[0])) then return; end; @@ -322,7 +322,7 @@ (* Before a prompt: the background programs that have ended. *) proc reap; -var pid, _code: I64; _st: Sys.Status; +var pid, _code: INT; _st: Sys.Status; begin repeat pid, _st, _code := Sys.wait(-1, false); @@ -334,7 +334,7 @@ shell in a process group of its own, which ignores the keys that stop or end the pipeline in front of it. *) proc interact; -var line: Buf.Buf; keys: [5]I64; i: I64; _ok: BOOL; +var line: Buf.Buf; keys: [5]INT; i: INT; _ok: BOOL; begin interactive := Sys.isTerminal(Sys.stdin); if interactive then @@ -353,7 +353,7 @@ if interactive then show("\n", []); end; end; -proc main*: I64; +proc main*: INT; var p: Opt.Parser; script, file: []U8; given, found: BOOL; b: Buf.Buf; begin if not Arena.init(ADR(arena), 1 shl 24) then Rt.stop("Sh: no memory"); end; diff --git a/examples/Shapes.raz b/examples/Shapes.raz index d5e95da..4b4da71 100644 --- a/examples/Shapes.raz +++ b/examples/Shapes.raz @@ -4,13 +4,13 @@ type Shape = variant - circle(r: I64); - rect(w, h: I64); + circle(r: INT); + rect(w, h: INT); dot; end; (* Twice the area, rounded down, of a circle taken as pi = 22/7. *) -proc area(s: Shape): I64; +proc area(s: Shape): INT; begin match s of | circle(r): return 44 * r * r div 7; @@ -19,8 +19,8 @@ end; end; -proc total(shapes: []Shape): I64; -var i, sum: I64; +proc total(shapes: []Shape): INT; +var i, sum: INT; begin sum := 0; for i := 0 to LEN(shapes) - 1 do sum := sum + area(shapes[i]); end; diff --git a/examples/Volatile.raz b/examples/Volatile.raz index fe0bc9f..6953cd4 100644 --- a/examples/Volatile.raz +++ b/examples/Volatile.raz @@ -15,7 +15,7 @@ var page: [64]U8; proc main*; -var dev: ^VOLATILE Uart; i: I64; +var dev: ^VOLATILE Uart; i: INT; begin dev := ^VOLATILE Uart(ADR(page)); dev^.control := 1; @@ -26,7 +26,7 @@ end; Out.ln; FENCE; - Out.int(I64(page[8])); Out.str(" "); Out.int(I64(dev^.status)); Out.ln; + Out.int(INT(page[8])); Out.str(" "); Out.int(INT(dev^.status)); Out.ln; end; end Volatile. diff --git a/examples/Window.raz b/examples/Window.raz index 3d74e8e..bf6cf35 100644 --- a/examples/Window.raz +++ b/examples/Window.raz @@ -34,13 +34,13 @@ extern proc XCreateSimpleWindow(d: ^U8; parent: U64; x, y: I32; width, height, border: U32; borderColor, background: U64): U64; extern proc XStoreName(d: ^U8; w: U64; name: ^U8): I32; -extern proc XSelectInput(d: ^U8; w: U64; mask: I64): I32; +extern proc XSelectInput(d: ^U8; w: U64; mask: INT): I32; extern proc XMapWindow(d: ^U8; w: U64): I32; extern proc XNextEvent(d: ^U8; ev: ^U8): I32; extern proc XDrawString(d: ^U8; w: U64; gc: ^U8; x, y: I32; s: ^U8; n: I32): I32; extern proc XCloseDisplay(d: ^U8): I32; -proc main*: I64; +proc main*: INT; const text = "hello, world: press a key"; var d: ^U8; s: I32; w: U64; @@ -62,7 +62,7 @@ end; until I32(ev[0]) = KeyPress; key := ^KeyEvent(ADR(ev)); - Out.str("key "); Out.int(I64(key^.keycode)); Out.ln; + Out.str("key "); Out.int(INT(key^.keycode)); Out.ln; XCloseDisplay(d); return 0; end; diff --git a/examples/Words.raz b/examples/Words.raz index 0523de4..0d3b96b 100644 --- a/examples/Words.raz +++ b/examples/Words.raz @@ -1,15 +1,15 @@ (* Generic modules: counts the words of a text in a Map from []U8 to - I64, hashed and compared by Str's procedures, keeps them in order of + INT, hashed and compared by Str's procedures, keeps them in order of first sight in a List([]U8), and prints them with Fmt. *) module Words; -import Fmt, Str, Heap, Count := Map([]U8, I64, Str.hash, Str.eq), Names := List([]U8); +import Fmt, Str, Heap, Count := Map([]U8, INT, Str.hash, Str.eq), Names := List([]U8); const Text = "the cat and the hat and the bat"; proc main*; var counts: Count.Map; order: Names.List; n: ^Names.Node; - i, start, k, total: I64; found, _ok: BOOL; w: []U8; + i, start, k, total: INT; found, _ok: BOOL; w: []U8; begin Count.init(ADR(counts), Heap.heap); Names.init(ADR(order), Heap.heap); diff --git a/examples/Workers.raz b/examples/Workers.raz index a566fde..f1a9629 100644 --- a/examples/Workers.raz +++ b/examples/Workers.raz @@ -8,13 +8,13 @@ Jobs = 100; var - next: I64; (* the next job, taken with XADD *) + next: INT; (* the next job, taken with XADD *) m: Thread.Mutex; - total: I64; + total: INT; (* job n: the sum of the squares up to n *) -proc job(n: I64): I64; -var i, s: I64; +proc job(n: INT): INT; +var i, s: INT; begin s := 0; for i := 1 to n do s := s + i * i; end; @@ -22,7 +22,7 @@ end; proc work(_arg: ^U8); -var n, r: I64; +var n, r: INT; begin n := XADD(ADR(next), 1); while n < Jobs do @@ -35,7 +35,7 @@ end; proc main*; -var t: [Workers]^Thread.Thread; i: I64; err: ERROR; +var t: [Workers]^Thread.Thread; i: INT; err: ERROR; begin for i := 0 to Workers - 1 do t[i], err := Thread.spawn(work, nil); diff --git a/lib/Arena.raz b/lib/Arena.raz index e8d701d..a59803d 100644 --- a/lib/Arena.raz +++ b/lib/Arena.raz @@ -8,11 +8,11 @@ Arena* = record base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) buf: ^U8; - used, cap: I64; + used, cap: INT; end; -proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; -var ar: ^Arena; at: I64; +proc alloc(a: ^Mem.Allocator; size, align: INT): ^U8; +var ar: ^Arena; at: INT; begin ar := ^Arena(a); at := (ar^.used + align - 1) div align * align; @@ -26,7 +26,7 @@ end; (* An arena of size bytes; false if the system has none. *) -proc init*(ar: ^Arena; size: I64): BOOL; +proc init*(ar: ^Arena; size: INT): BOOL; begin ar^.base := {alloc: alloc, free: free}; ar^.buf := Mem.pages(size); diff --git a/lib/Buf.raz b/lib/Buf.raz index 276ba65..c46c54a 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -6,7 +6,7 @@ Buf* = record a: ^Mem.Allocator; data: ^U8; - len*, cap: I64; + len*, cap: INT; end; proc init*(b: ^Buf; a: ^Mem.Allocator); @@ -15,8 +15,8 @@ end; (* Room for n more bytes; false if the allocator has none. *) -proc room*(b: ^Buf; n: I64): BOOL; -var cap, i: I64; p: ^U8; +proc room*(b: ^Buf; n: INT): BOOL; +var cap, i: INT; p: ^U8; begin if b^.len + n <= b^.cap then return true; end; cap := b^.cap * 2; @@ -40,7 +40,7 @@ end; proc str*(b: ^Buf; s: []U8); -var i: I64; +var i: INT; begin if room(b, LEN(s)) then for i := 0 to LEN(s) - 1 do b^.data[b^.len + i] := s[i]; end; @@ -70,7 +70,7 @@ (* The module's test, over an arena. *) proc main*; -var ar: Arena.Arena; b: Buf; i: I64; s: []U8; +var ar: Arena.Arena; b: Buf; i: INT; s: []U8; begin if not Arena.init(ADR(ar), 1 shl 20) then Rt.stop("Buf: no arena"); end; init(ADR(b), ADR(ar.base)); diff --git a/lib/C.raz b/lib/C.raz index 5f9d180..ebdd87c 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -3,26 +3,26 @@ linked dynamically and starts through the C library. *) module C; -extern proc write*(fd: I32; buf: ^U8; n: U64): I64; -extern proc read*(fd: I32; buf: ^U8; n: U64): I64; +extern proc write*(fd: I32; buf: ^U8; n: UINT): INT; +extern proc read*(fd: I32; buf: ^U8; n: UINT): INT; extern proc open*(path: ^U8; flags: I32; ...): I32; extern proc creat*(path: ^U8; mode: U32): I32; extern proc close*(fd: I32): I32; -extern proc mmap*(addr: ^U8; n: U64; prot, flags, fd: I32; off: I64): ^U8; -extern proc munmap*(addr: ^U8; n: U64): I32; +extern proc mmap*(addr: ^U8; n: UINT; prot, flags, fd: I32; off: I64): ^U8; +extern proc munmap*(addr: ^U8; n: UINT): I32; extern proc isatty*(fd: I32): I32; extern proc exit*(status: I32); extern proc _exit*(status: I32); extern proc __errno_location*: ^I32; (* errno is this thread's: through here *) extern proc sched_yield*: I32; -extern proc pthread_create*(t: ^U64; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; -extern proc pthread_join*(t: U64; result: ^^U8): I32; +extern proc pthread_create*(t: ^UINT; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; +extern proc pthread_join*(t: UINT; result: ^^U8): I32; extern proc rename*(old, new: ^U8): I32; extern proc unlink*(path: ^U8): I32; extern proc mkdir*(path: ^U8; mode: U32): I32; extern proc rmdir*(path: ^U8): I32; extern proc chmod*(path: ^U8; mode: U32): I32; -extern proc readlink*(path, buf: ^U8; n: U64): I64; +extern proc readlink*(path, buf: ^U8; n: UINT): INT; extern proc lseek*(fd: I32; off: I64; whence: I32): I64; extern proc opendir*(path: ^U8): ^U8; (* DIR * *) extern proc readdir*(d: ^U8): ^U8; (* struct dirent * *) @@ -30,11 +30,11 @@ extern proc tcgetattr*(fd: I32; t: ^U8): I32; extern proc tcsetattr*(fd: I32; when: I32; t: ^U8): I32; extern proc cfmakeraw*(t: ^U8); -extern proc ioctl*(fd: I32; request: U64; ...): I32; -extern proc poll*(fds: ^U8; n: U64; ms: I32): I32; +extern proc ioctl*(fd: I32; request: UINT; ...): I32; +extern proc poll*(fds: ^U8; n: UINT; ms: I32): I32; extern proc socket*(domain, kind, protocol: I32): I32; extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; -extern proc send*(fd: I32; buf: ^U8; n: U64; flags: I32): I64; +extern proc send*(fd: I32; buf: ^U8; n: UINT; flags: I32): INT; extern proc bind*(fd: I32; addr: ^U8; len: U32): I32; extern proc listen*(fd, backlog: I32): I32; extern proc accept*(fd: I32; addr: ^U8; len: ^U32): I32; @@ -43,9 +43,9 @@ extern proc inet_pton*(family: I32; src, dst: ^U8): I32; extern proc clock_gettime*(clock: I32; t: ^U8): I32; extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32; -extern proc getrandom*(buf: ^U8; n: U64; flags: U32): I64; +extern proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT; extern proc getenv*(name: ^U8): ^U8; -extern proc getcwd*(buf: ^U8; n: U64): ^U8; +extern proc getcwd*(buf: ^U8; n: UINT): ^U8; extern proc chdir*(path: ^U8): I32; extern proc stat*(path, st: ^U8): I32; extern proc lstat*(path, st: ^U8): I32; diff --git a/lib/Files.raz b/lib/Files.raz index d101433..9b3b648 100644 --- a/lib/Files.raz +++ b/lib/Files.raz @@ -4,8 +4,8 @@ import Sys, Buf; (* Argument i, a C string, as a slice. *) -proc arg*(i: I64): []U8; -var s: ^U8; n: I64; +proc arg*(i: INT): []U8; +var s: ^U8; n: INT; begin s := ARGV()[i]; n := 0; @@ -15,7 +15,7 @@ (* Adds the file at path to b; false if it can't be read. *) proc read*(path: []U8; b: ^Buf.Buf): BOOL; -var fd, n: I64; chunk: [4096]U8; +var fd, n: INT; chunk: [4096]U8; begin fd := Sys.openRead(path); if fd < 0 then return false; end; @@ -29,7 +29,7 @@ (* Makes the file at path hold data; false if it can't be written. *) proc write*(path, data: []U8): BOOL; -var fd, n, at: I64; +var fd, n, at: INT; begin fd := Sys.create(path); if fd < 0 then return false; end; diff --git a/lib/Fmt.raz b/lib/Fmt.raz index 5fdf778..1aef8de 100644 --- a/lib/Fmt.raz +++ b/lib/Fmt.raz @@ -13,12 +13,12 @@ str(s: []U8); char(c: U8); bool(b: BOOL); - real(r: F64; places: I64); + real(r: F64; places: INT); end; (* The digits of u in base, at the end of the array: it and where they start. *) -proc digits(u: U64; base: U64): ([24]U8, I64); -var d: [24]U8; i: I64; v: U8; +proc digits(u: U64; base: U64): ([24]U8, INT); +var d: [24]U8; i: INT; v: U8; begin i := 24; repeat @@ -40,9 +40,9 @@ nearest; from 1E15 on, with the exponent that makes it less than 10, as 1.50e17. The whole part and the fraction are taken apart, so that each fits an I64. *) -proc putReal(b: ^Buf.Buf; x: F64; places: I64); +proc putReal(b: ^Buf.Buf; x: F64; places: INT); const zeros = "00000000"; -var d: [24]U8; at, scale, i, whole, frac, e: I64; +var d: [24]U8; at, i, e: INT; scale, whole, frac: I64; begin if x # x then put(b, "nan"); return; end; if x < 0.0 then put(b, "-"); x := -x; end; @@ -73,7 +73,7 @@ end; proc arg(b: ^Buf.Buf; a: Arg); -var d: [24]U8; at: I64; one: [1]U8; +var d: [24]U8; at: INT; one: [1]U8; begin match a of | int(i): @@ -94,7 +94,7 @@ end; proc format(b: ^Buf.Buf; f: []U8; args: []Arg); -var i, start, n: I64; +var i, start, n: INT; begin i := 0; start := 0; n := 0; while i < LEN(f) do diff --git a/lib/Heap.raz b/lib/Heap.raz index eddbfea..540569b 100644 --- a/lib/Heap.raz +++ b/lib/Heap.raz @@ -18,30 +18,30 @@ type Block = record next: ^Block; end; - Head = record size, mark: I64; end; + Head = record size, mark: INT; end; var lists: [Classes]^Block; cur: ^U8; (* where the next new small block comes from *) - left: I64; + left: INT; lock: Lock.Lock; std: Mem.Allocator; heap*: ^Mem.Allocator; (* The block p, with size for its header, given out. *) -proc give(p: ^U8; size: I64): ^U8; +proc give(p: ^U8; size: INT): ^U8; var h: ^Head; begin h := ^Head(p); h^.size := size; - h^.mark := Out xor I64(h); + h^.mark := Out xor INT(h); return p + Header; end; (* size bytes aligned to align, or nil: also for a size below zero or too big, and an alignment that isn't a power of two up to a page. *) -proc alloc(_a: ^Mem.Allocator; size, align: I64): ^U8; -var c, n: I64; b: ^Block; p: ^U8; +proc alloc(_a: ^Mem.Allocator; size, align: INT): ^U8; +var c, n: INT; b: ^Block; p: ^U8; begin if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) or (align & (align - 1) # 0) then @@ -78,18 +78,18 @@ end; proc free(_a: ^Mem.Allocator; p: ^U8); -var h: ^Head; b: ^Block; c: I64; +var h: ^Head; b: ^Block; c: INT; begin if p = nil then return; end; h := ^Head(p - Header); Lock.take(ADR(lock)); - if h^.mark # Out xor I64(h) then + if h^.mark # Out xor INT(h) then Rt.stop("Heap: a block freed twice, or never given out"); end; h^.mark := 0; if h^.size < 0 then Lock.give(ADR(lock)); - Mem.unmap(^U8(I64(h) - I64(h) mod Mem.Page), -h^.size); + Mem.unmap(^U8(INT(h) - INT(h) mod Mem.Page), -h^.size); else c := h^.size; (* before next, which takes its place, is set *) b := ^Block(h); @@ -102,12 +102,12 @@ (* The module's test: blocks of every class and big ones, written, freed and taken again. *) proc main*; -var p: [40]^U8; i, j, n: I64; again: ^U8; +var p: [40]^U8; i, j, n: INT; again: ^U8; begin for i := 0 to 39 do n := (i * i * 37) mod 9000 + 1; p[i] := Mem.alloc(heap, n, 8); - if (p[i] = nil) or (I64(p[i]) mod 8 # 0) then Rt.stop("Heap: alloc"); end; + if (p[i] = nil) or (INT(p[i]) mod 8 # 0) then Rt.stop("Heap: alloc"); end; for j := 0 to n - 1 do p[i][j] := U8(i); end; end; for i := 0 to 39 do diff --git a/lib/List.raz b/lib/List.raz index 80f8112..ae188a8 100644 --- a/lib/List.raz +++ b/lib/List.raz @@ -9,7 +9,7 @@ end; List* = record head*: ^Node; - len*: I64; + len*: INT; a: ^Mem.Allocator; end; diff --git a/lib/Lock.raz b/lib/Lock.raz index cbb1179..5c97f2c 100644 --- a/lib/Lock.raz +++ b/lib/Lock.raz @@ -5,10 +5,10 @@ module Lock; import Sys, Rt; -type Lock* = record held: I64; end; +type Lock* = record held: INT; end; proc take*(l: ^Lock); -var tries: I64; +var tries: INT; begin tries := 0; while not CAS(ADR(l^.held), 0, 1) do diff --git a/lib/Map.raz b/lib/Map.raz index 2fef081..85ec0ba 100644 --- a/lib/Map.raz +++ b/lib/Map.raz @@ -1,6 +1,6 @@ (* A hash map from K to V, by open addressing; it hashes and compares keys with the procedures it is given: - import Count := Map([]U8, I64, Str.hash, Str.eq); *) + import Count := Map([]U8, INT, Str.hash, Str.eq); *) module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); import Mem; @@ -13,8 +13,8 @@ Map* = record a: ^Mem.Allocator; slots: ^Entry; - cap: I64; (* a power of two, or 0 *) - len*: I64; + cap: INT; (* a power of two, or 0 *) + len*: INT; end; proc init*(m: ^Map; a: ^Mem.Allocator); @@ -23,10 +23,10 @@ end; (* The slot of k, or of the empty slot where it would go. *) -proc slot(m: ^Map; k: K): I64; -var i: I64; +proc slot(m: ^Map; k: K): INT; +var i: INT; begin - i := I64(hash(k) & U64(m^.cap - 1)); + i := INT(hash(k) & U64(m^.cap - 1)); while m^.slots[i].used and not eq(m^.slots[i].key, k) do i := (i + 1) & (m^.cap - 1); end; @@ -34,7 +34,7 @@ end; proc grow(m: ^Map): BOOL; -var old: ^Entry; n, i, j: I64; +var old: ^Entry; n, i, j: INT; begin old := m^.slots; n := m^.cap; @@ -54,7 +54,7 @@ (* k now maps to v; false if there was no room. *) proc put*(m: ^Map; k: K; v: V): BOOL; -var i: I64; +var i: INT; begin if (m^.len + 1) * 4 > m^.cap * 3 then if not grow(m) then return false; end; @@ -72,7 +72,7 @@ after it that were pushed on past its slot move back, so that a search for each still finds it before an empty slot. *) proc remove*(m: ^Map; k: K): BOOL; -var i, j, home: I64; +var i, j, home: INT; begin if m^.cap = 0 then return false; end; i := slot(m, k); @@ -80,7 +80,7 @@ j := (i + 1) & (m^.cap - 1); while m^.slots[j].used do (* the entry at j may fill i if i lies between its home and j *) - home := I64(hash(m^.slots[j].key) & U64(m^.cap - 1)); + home := INT(hash(m^.slots[j].key) & U64(m^.cap - 1)); if (j - home) & (m^.cap - 1) >= (j - i) & (m^.cap - 1) then m^.slots[i] := m^.slots[j]; i := j; @@ -94,7 +94,7 @@ (* What k maps to, and whether it maps to anything. *) proc get*(m: ^Map; k: K): (V, BOOL); -var i: I64; none: V; +var i: INT; none: V; begin if m^.cap = 0 then return none, false; end; i := slot(m, k); @@ -104,7 +104,7 @@ (* The entry after i, walking from 0: the number to pass for the one after it, or -1 if there are no more, then its key and value. *) -proc next*(m: ^Map; i: I64): (I64, K, V); +proc next*(m: ^Map; i: INT): (INT, K, V); var k: K; v: V; begin while i < m^.cap do diff --git a/lib/Math.raz b/lib/Math.raz index 2c97675..a081eda 100644 --- a/lib/Math.raz +++ b/lib/Math.raz @@ -45,14 +45,14 @@ var t: F64; begin if not (x > -4503599627370496.0) or not (x < 4503599627370496.0) or (x = 0.0) then return x; end; - t := F64(I64(x)); + t := F64(INT(x)); if t > x then t := t - 1.0; end; return t; end; (* x times 2^k, for k from -1075 to 1024: 2^k is made from its bits, in two steps where k is past what an F64's exponent holds. *) -proc scale(x: F64; k: I64): F64; +proc scale(x: F64; k: INT): F64; begin if k > 1023 then x := x * fromBits(U64(2046) shl 52); DEC(k, 1023); end; if k < -1022 then x := x * fromBits(U64(1 + 53) shl 52); INC(k, 1022 - 53); end; @@ -61,13 +61,13 @@ (* e^x = 2^k e^r, with r = x - k ln 2 at most ln 2 / 2 across. *) proc exp*(x: F64): F64; -var k: I64; hi, lo, r, rr, c: F64; +var k: INT; hi, lo, r, rr, c: F64; begin if x # x then return x; end; if x > 709.782712893383973096 then return x * fromBits(U64(2046) shl 52); end; (* too big: inf *) if x < -745.13321910194110842 then return 0.0; end; - if x < 0.0 then k := I64(x * 1.44269504088896338700 - 0.5); - else k := I64(x * 1.44269504088896338700 + 0.5); + if x < 0.0 then k := INT(x * 1.44269504088896338700 - 0.5); + else k := INT(x * 1.44269504088896338700 + 0.5); end; hi := x - F64(k) * ln2hi; lo := F64(k) * ln2lo; @@ -83,7 +83,7 @@ found through s = f / (2 + f), f = m - 1, whose series converges fast. *) proc ln*(x: F64): F64; -var u: U64; k: I64; f, hfsq, s, z, w, r: F64; +var u: U64; k: INT; f, hfsq, s, z, w, r: F64; begin if x # x then return x; end; if x < 0.0 then return (x - x) / 0.0; end; (* NaN *) @@ -95,7 +95,7 @@ end; (* the exponent, counted from m's lower end, and the bits of m *) u := bits(x) + (3FF0000000000000H - 3FE6A09E00000000H); - INC(k, I64(u shr 52) - 3FFH); + INC(k, INT(u shr 52) - 3FFH); f := fromBits((u & 0FFFFFFFFFFFFFH) + 3FE6A09E00000000H) - 1.0; hfsq := 0.5 * f * f; s := f / (2.0 + f); @@ -114,8 +114,8 @@ 0C3H; (* ret *) (* 64 bits of t from bit s on, the first bit of t[0] being 0. *) -proc window(t: []U64; s: I64): U64; -var q, r: I64; +proc window(t: []U64; s: INT): U64; +var q, r: INT; begin q := s div 64; r := s mod 64; @@ -129,10 +129,10 @@ turns, which change no sine, and are left out; the 192 after them give the two bits of n mod 4 and the fraction past n, exact far beyond an F64. *) -proc reduceLarge(x: F64): (I64, F64, F64); +proc reduceLarge(x: F64): (INT, F64, F64); var t: [21]U64; (* 2/pi's bits after its point, behind a word of zeros *) - m, w0, w1, w2, p0, p1, p2, lo: U64; e, s, n, k: I64; y: F64; neg, up: BOOL; + m, w0, w1, w2, p0, p1, p2, lo: U64; e, s, n, k: INT; y: F64; neg, up: BOOL; begin t := [0, 0A2F9836E4E441529H, 0FC2757D1F534DDC0H, 0DB6295993C439041H, 0FE5163ABDEBBC561H, 0B7246E3A424DD2E0H, 06492EEA09D1921CH, 0FE1DEB1CB129A73EH, 0E88235F52EBB4484H, @@ -142,7 +142,7 @@ neg := x < 0.0; if neg then x := -x; end; m := bits(x) & 0FFFFFFFFFFFFFH | 10000000000000H; - e := I64(bits(x) shr 52) - 1075; + e := INT(bits(x) shr 52) - 1075; (* W, the 192 bits of 2/pi from the one worth 2^(1 - e), so that x 2/pi is m W 2^-190, of which the bits from 2^192 up are left out *) s := e + 62; @@ -157,7 +157,7 @@ (* n is the two bits from 2^190, the fraction those below; from a half up, n is the next and the fraction 1 less, taken as 1 less the fraction, negated: its bits complemented, one in 2^190 short *) - n := I64(p2 shr 62); + n := INT(p2 shr 62); p2 := p2 & 3FFFFFFFFFFFFFFFH; up := p2 shr 61 = 1; if up then @@ -182,12 +182,12 @@ than one F64; and n. pi/2 is taken in three parts of 33 bits, each with what is left of it after, so that n times each is exact while n is below 2^20, about x = 1.6E6. *) -proc reduce(x: F64): (I64, F64, F64); -var n: I64; fn, r, t, w, y: F64; +proc reduce(x: F64): (INT, F64, F64); +var n: INT; fn, r, t, w, y: F64; begin if (x > 1.6E6) or (x < -1.6E6) then return reduceLarge(x); end; - if x < 0.0 then n := I64(x * 6.36619772367581382433E-01 - 0.5); - else n := I64(x * 6.36619772367581382433E-01 + 0.5); + if x < 0.0 then n := INT(x * 6.36619772367581382433E-01 - 0.5); + else n := INT(x * 6.36619772367581382433E-01 + 0.5); end; fn := F64(n); r := x - fn * 1.57079632673412561417E+00; @@ -227,7 +227,7 @@ end; proc sin*(x: F64): F64; -var n: I64; y, yy: F64; +var n: INT; y, yy: F64; begin if (x = 0.0) or (x # x) then return x; end; if (x - x) # 0.0 then return x - x; end; (* an infinity: NaN *) @@ -241,7 +241,7 @@ end; proc cos*(x: F64): F64; -var n: I64; y, yy: F64; +var n: INT; y, yy: F64; begin if (x - x) # 0.0 then return x - x; end; (* an infinity or NaN: NaN *) n, y, yy := reduce(x); diff --git a/lib/Mem.raz b/lib/Mem.raz index 55ceb3c..12d52a8 100644 --- a/lib/Mem.raz +++ b/lib/Mem.raz @@ -5,24 +5,24 @@ type Allocator* = record - alloc*: proc(a: ^Allocator; size, align: I64): ^U8; (* nil if out of room *) + alloc*: proc(a: ^Allocator; size, align: INT): ^U8; (* nil if out of room *) free*: proc(a: ^Allocator; p: ^U8); end; const Page* = 4096; (* size bytes, rounded up to whole pages, zeroed; or nil. *) -proc pages*(size: I64): ^U8; +proc pages*(size: INT): ^U8; begin return Sys.mmap((size + Page - 1) div Page * Page); end; -proc unmap*(p: ^U8; size: I64); +proc unmap*(p: ^U8; size: INT); begin Sys.munmap(p, (size + Page - 1) div Page * Page); end; -proc alloc*(a: ^Allocator; size, align: I64): ^U8; +proc alloc*(a: ^Allocator; size, align: INT): ^U8; begin return a^.alloc(a, size, align); end; @@ -34,7 +34,7 @@ (* The module's test. *) proc main*; -var p: ^U8; i: I64; +var p: ^U8; i: INT; begin p := pages(10000); if p = nil then Rt.stop("Mem.pages"); end; diff --git a/lib/Opt.raz b/lib/Opt.raz index 8a1d166..58ab612 100644 --- a/lib/Opt.raz +++ b/lib/Opt.raz @@ -25,7 +25,7 @@ name: []U8; (* the program's, as the messages begin *) table: []Row; scope: []U8; - at: I64; (* the next word *) + at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) spelt: []U8; (* the option just read as written, without its dashes *) ended: BOOL; (* the scope's options have ended *) @@ -36,8 +36,8 @@ const read1 = 1; readEnd = 0; readSkip = -1; (* b written whole to fd, then freed. *) -proc say(fd: I64; b: ^Buf.Buf); -var s: []U8; n: I64; +proc say(fd: INT; b: ^Buf.Buf); +var s: []U8; n: INT; begin s := Buf.bytes(b); while LEN(s) > 0 do @@ -61,7 +61,7 @@ "usage: " and the first, the others under it; or, for a scope with none, "usage: keep start". *) proc usageOf(p: ^Parser; b: ^Buf.Buf); -var i, lines: I64; c: U8; +var i, lines: INT; c: U8; begin for i := 0 to LEN(p^.table) - 1 do match p^.table[i] of @@ -124,8 +124,8 @@ end; end; -proc leftWidth(r: ^Row): I64; -var b: Buf.Buf; n: I64; +proc leftWidth(r: ^Row): INT; +var b: Buf.Buf; n: INT; begin Buf.init(ADR(b), Heap.heap); left(ADR(b), r); @@ -136,8 +136,8 @@ (* A line of the list: the left column, padded to width and three spaces more, and the line of help. *) -proc item(b: ^Buf.Buf; r: ^Row; width: I64); -var i: I64; +proc item(b: ^Buf.Buf; r: ^Row; width: INT); +var i: INT; begin Buf.str(b, " "); left(b, r); @@ -153,7 +153,7 @@ (* The scope's help: its synopsis lines, then its subcommands, its options and the help itself, each with its line; and status 0. *) proc printHelp(p: ^Parser); -var b: Buf.Buf; i, width: I64; r: ^Row; +var b: Buf.Buf; i, width: INT; r: ^Row; begin Buf.init(ADR(b), Heap.heap); usageOf(p, ADR(b)); @@ -184,8 +184,8 @@ (* The scope's option with the letter c, or with the long name long: its row, or -1, and its letter and the name of its value. *) -proc find(p: ^Parser; c: U8; long: []U8): (I64, U8, []U8); -var i: I64; +proc find(p: ^Parser; c: U8; long: []U8): (INT, U8, []U8); +var i: INT; begin for i := 0 to LEN(p^.table) - 1 do match p^.table[i] of @@ -202,8 +202,8 @@ (* The next option, a word or a letter of one, into p: read1, or readEnd at the end of the options. A mistake is refused, or, if quiet, passed over, as readSkip. *) -proc read(p: ^Parser; quiet: BOOL): I64; -var w, val: []U8; r, eq: I64; c: U8; +proc read(p: ^Parser; quiet: BOOL): INT; +var w, val: []U8; r, eq: INT; c: U8; begin p^.help := false; p^.value := ""; @@ -272,7 +272,7 @@ program sees one: a copy of the parser reads them, passing over mistakes. *) proc answerHelp(p: ^Parser); -var q: Parser; k: I64; +var q: Parser; k: INT; begin q := p^; repeat @@ -282,7 +282,7 @@ end; (* A mistake in the table: the program's, with status 1. *) -proc badTable(p: ^Parser; row: I64; why: []U8); +proc badTable(p: ^Parser; row: INT; why: []U8); var b: Buf.Buf; begin Buf.init(ADR(b), Heap.heap); @@ -292,7 +292,7 @@ end; proc check(p: ^Parser); -var i, j: I64; +var i, j: INT; begin for i := 0 to LEN(p^.table) - 1 do match p^.table[i] of @@ -455,12 +455,12 @@ mode: []U8; (* OPTTEST=... *) args: [][]U8; out, err: []U8; - status: I64; + status: INT; end; (* Everything left to read on fd, into buf: how much. *) -proc readAll(fd: I64; buf: []U8): I64; -var n, k: I64; +proc readAll(fd: INT; buf: []U8): INT; +var n, k: INT; begin repeat k := Sys.read(fd, buf[n..LEN(buf)]); @@ -470,8 +470,8 @@ return n; end; -proc run(c: Case; n: I64); -var outR, outW, errR, errW, pid, status, no, ne: I64; st: Sys.Status; out, err: [4096]U8; +proc run(c: Case; n: INT); +var outR, outW, errR, errW, pid, status, no, ne: INT; st: Sys.Status; out, err: [4096]U8; begin outR, outW := Sys.pipe(); errR, errW := Sys.pipe(); @@ -494,7 +494,7 @@ keepHelp = "usage: keep [-v] [--wait secs] start ...\n\n start start a job\n -v, --verbose say more\n --wait secs give the job secs to start\n -h, --help print this and exit\n"; startHelp = "usage: keep start [-b label] name command [argument ...]\n\n -b, --bar label show the job in the bar as label\n -h, --help print this and exit\n"; k = "OPTTEST=keep"; -var cases: []Case; i: I64; mode: []U8; +var cases: []Case; i: INT; mode: []U8; begin mode := Sys.env("OPTTEST"); if mode = "keep" then keep; return; diff --git a/lib/Out.raz b/lib/Out.raz index e0b3c18..0113ade 100644 --- a/lib/Out.raz +++ b/lib/Out.raz @@ -9,14 +9,14 @@ var buf: [Sys.outSize]U8; - n: I64; (* bytes in buf *) + n: INT; (* bytes in buf *) tty: BOOL; (* standard output is a terminal *) lock: Lock.Lock; (* Writes out the buffer, with the lock held. What can't be written is dropped. *) proc out; -var i, k: I64; +var i, k: INT; begin i := 0; while i < n do @@ -36,7 +36,7 @@ end; proc str*(s: []U8); -var i: I64; +var i: INT; begin Lock.take(ADR(lock)); for i := 0 to LEN(s) - 1 do @@ -62,7 +62,7 @@ (* n in decimal *) proc int*(n: I64); -var buf: [20]U8; i: I64; u: U64; +var buf: [20]U8; i: INT; u: U64; begin u := U64(n); if n < 0 then u := 0 - u; end; @@ -80,8 +80,8 @@ end; (* n in hexadecimal, digits digits at least *) -proc hex*(n: U64; digits: I64); -var buf: [16]U8; i: I64; d: U8; +proc hex*(n: U64; digits: INT); +var buf: [16]U8; i: INT; d: U8; begin i := 16; repeat diff --git a/lib/Rt.raz b/lib/Rt.raz index a8d0023..00f525c 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -23,23 +23,23 @@ Place = record at, line, col, path: U32; end; (* What rt_sigaction and sigaltstack take *) Action = record - handler: proc(sig: I64; info, ctx: ^U8); + handler: proc(sig: INT; info, ctx: ^U8); flags: U64; restorer: proc; mask: U64; end; - Stack = record sp: ^U8; flags, size: I64; end; + Stack = record sp: ^U8; flags, size: INT; end; var atExits: [MaxAtExit]proc; - numAtExit: I64; + numAtExit: INT; cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *) (* Set first thing too: the number of places, where the program's code starts and stops, the places, and the paths. The linker makes them. *) - places: ^I64; + places: ^INT; line: [256]U8; (* the message being made *) - lineLen: I64; + lineLen: INT; act: Action; alt: Stack; altStack: [16384]U8; (* where the handlers run, even when the stack is full *) @@ -58,7 +58,7 @@ uses C, so that C flushes its buffers too. Each procedure is taken off the list before it runs, so it runs once even if it fails or calls exit itself. *) -proc exit*(status: I64); +proc exit*(status: INT); var p: proc; begin while numAtExit > 0 do @@ -82,7 +82,7 @@ (* Messages, made in line, one byte kept for the line feed, and written by say in one piece *) proc put(s: []U8); -var i: I64; +var i: INT; begin for i := 0 to LEN(s) - 1 do if lineLen < LEN(line) - 1 then @@ -93,9 +93,9 @@ end; (* The digits are made from the last, those of a negative n from its - negative remainders, so that the lowest I64 comes out too. *) -proc putInt(n: I64); -var buf: [20]U8; i: I64; neg: BOOL; + negative remainders, so that the lowest INT comes out too. *) +proc putInt(n: INT); +var buf: [20]U8; i: INT; neg: BOOL; begin neg := n < 0; i := 20; @@ -109,7 +109,7 @@ end; proc putStr(s: ^U8); -var n: I64; +var n: INT; begin n := 0; while s[n] # 0 do INC(n); end; @@ -126,22 +126,22 @@ (* Puts where the code at address a comes from, as file:line:col: and a space, if a is in the program's code: the place with the highest address up to a. *) -proc where(a: U64); -var p, best: ^Place; i, n: I64; +proc where(a: UINT); +var p, best: ^Place; i, n: INT; begin - if (places = nil) or (a < U64(places[1])) or (a >= U64(places[2])) then return; end; + if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; n := places[0]; p := ^Place(places + 3); best := nil; for i := 0 to n - 1 do - if (U64(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end; + if (UINT(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end; end; if best = nil then return; end; - putStr(^U8(p + n) + I64(best^.path)); + putStr(^U8(p + n) + INT(best^.path)); put(":"); - putInt(I64(best^.line)); + putInt(INT(best^.line)); put(":"); - putInt(I64(best^.col)); + putInt(INT(best^.col)); put(": "); end; @@ -151,7 +151,7 @@ something whose length is len; 2 for a, which is no position of the enumeration called name; 3 for a slice a..b of a pointer, which runs backwards. *) -proc fail*(what, a, b, len: I64; at: U64; name: ^U8); +proc fail*(what, a, b, len: INT; at: UINT; name: ^U8); begin where(at); if what = 0 then @@ -186,18 +186,18 @@ reset as it was called, so the instruction faults again and the signal ends the program. info and ctx are the kernel's siginfo and ucontext. *) -proc onSignal(sig: I64; info, ctx: ^U8); -var adr, sp, ip: U64; q: ^U64; why: []U8; +proc onSignal(sig: INT; info, ctx: ^U8); +var adr, sp, ip: UINT; q: ^UINT; why: []U8; begin - q := ^U64(info + 16); (* si_addr *) + q := ^UINT(info + 16); (* si_addr *) adr := q^; - q := ^U64(ctx + 160); (* uc_mcontext's rsp, then rip *) + q := ^UINT(ctx + 160); (* uc_mcontext's rsp, then rip *) sp := q[0]; ip := q[1]; if sig = SigFpe then why := "a division fault"; elsif ip < Low then why := "a nil procedure"; - q := ^U64(sp); + q := ^UINT(sp); ip := q^ - 1; (* the call, by its return address *) elsif adr < Low then why := "a nil pointer"; elsif adr - sp + 100000H < 200000H then why := "a stack overflow"; (* within a megabyte of rsp *) diff --git a/lib/Sort.raz b/lib/Sort.raz index 7398425..76bcb2f 100644 --- a/lib/Sort.raz +++ b/lib/Sort.raz @@ -8,8 +8,8 @@ (* The element at i sinks through s[0..n], a heap with the largest at its root, to where it is no smaller than its children, 2i+1 and 2i+2. *) -proc sink(s: []T; i, n: I64); -var c: I64; t: T; +proc sink(s: []T; i, n: INT); +var c: INT; t: T; begin c := 2 * i + 1; while c < n do @@ -23,7 +23,7 @@ (* s made a heap, then its largest taken, in turn, to the end. *) proc sort*(s: []T); -var i: I64; t: T; +var i: INT; t: T; begin for i := LEN(s) div 2 - 1 to 0 by -1 do sink(s, i, LEN(s)); end; for i := LEN(s) - 1 to 1 by -1 do diff --git a/lib/Str.raz b/lib/Str.raz index d6dd5a2..8dfb6fe 100644 --- a/lib/Str.raz +++ b/lib/Str.raz @@ -7,8 +7,8 @@ const least = -9223372036854775807 - 1; (* the smallest I64 *) (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) -proc cmp*(a, b: []U8): I64; -var i, n: I64; +proc cmp*(a, b: []U8): INT; +var i, n: INT; begin n := LEN(a); if LEN(b) < n then n := LEN(b); end; @@ -30,7 +30,7 @@ (* FNV-1a, 64 bits. *) proc hash*(s: []U8): U64; -var h: U64; i: I64; +var h: U64; i: INT; begin h := 0CBF29CE484222325H; for i := 0 to LEN(s) - 1 do @@ -40,8 +40,8 @@ end; (* Where sub first starts in s, or -1. *) -proc find*(s, sub: []U8): I64; -var i: I64; +proc find*(s, sub: []U8): INT; +var i: INT; begin for i := 0 to LEN(s) - LEN(sub) do if s[i..i + LEN(sub)] = sub then return i; end; @@ -57,7 +57,7 @@ (* c in UTF-8: its bytes, and how many. A number that isn't a character, a surrogate or one past 10FFFFH, becomes FFFDH, the character that stands for a wrong one. *) -proc encode*(c: I64): ([4]U8, I64); +proc encode*(c: I32): ([4]U8, INT); var b: [4]U8; begin if (c < 0) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then c := 0FFFDH; end; @@ -85,11 +85,11 @@ if its first byte doesn't start one rightly, being a continuation, or followed by too few continuations, or spelling a surrogate or a character in more bytes than it needs; 0 and 0 if s is empty. *) -proc decode*(s: []U8): (I64, I64); -var c, n, least, i: I64; +proc decode*(s: []U8): (I32, INT); +var c, least: I32; n, i: INT; begin if LEN(s) = 0 then return 0, 0; end; - c := I64(s[0]); + c := I32(s[0]); if c < 80H then return c, 1; elsif c < 0C0H then return 0FFFDH, 1; elsif c < 0E0H then n := 2; c := c & 1FH; least := 80H; @@ -100,7 +100,7 @@ if LEN(s) < n then return 0FFFDH, 1; end; for i := 1 to n - 1 do if s[i] & 0C0H # 80H then return 0FFFDH, 1; end; - c := c shl 6 + I64(s[i] & 3FH); + c := c shl 6 + I32(s[i] & 3FH); end; if (c < least) or (c > 10FFFFH) or (c >= 0D800H) and (c <= 0DFFFH) then return 0FFFDH, 1; end; return c, n; @@ -109,7 +109,7 @@ (* s as a decimal number, with a - or + before it if any, and nothing else; false if it isn't one, or an I64 can't hold it. *) proc toInt*(s: []U8): (I64, BOOL); -var n, i, d: I64; neg: BOOL; +var n, d: I64; i: INT; neg: BOOL; begin neg := (LEN(s) > 0) and (s[0] = '-'); if (LEN(s) > 0) and ((s[0] = '-') or (s[0] = '+')) then i := 1; end; @@ -133,7 +133,7 @@ the lowest: how a file or a message in little-endian order is read, whatever the machine's own order. *) proc le*(s: []U8): U64; -var v: U64; i: I64; +var v: U64; i: INT; begin if LEN(s) > 8 then Rt.stop("Str.le: more than 8 bytes"); end; for i := LEN(s) - 1 to 0 by -1 do v := v shl 8 | U64(s[i]); end; @@ -142,7 +142,7 @@ (* As le, the first byte the highest: big-endian, network order. *) proc be*(s: []U8): U64; -var v: U64; i: I64; +var v: U64; i: INT; begin if LEN(s) > 8 then Rt.stop("Str.be: more than 8 bytes"); end; for i := 0 to LEN(s) - 1 do v := v shl 8 | U64(s[i]); end; @@ -152,7 +152,7 @@ (* v into the bytes of s, at most 8, the lowest first; what doesn't fit is left out. *) proc putLE*(s: []U8; v: U64); -var i: I64; +var i: INT; begin if LEN(s) > 8 then Rt.stop("Str.putLE: more than 8 bytes"); end; for i := 0 to LEN(s) - 1 do s[i] := U8(v); v := v shr 8; end; @@ -160,7 +160,7 @@ (* As putLE, the highest first. *) proc putBE*(s: []U8; v: U64); -var i: I64; +var i: INT; begin if LEN(s) > 8 then Rt.stop("Str.putBE: more than 8 bytes"); end; for i := LEN(s) - 1 to 0 by -1 do s[i] := U8(v); v := v shr 8; end; @@ -182,7 +182,7 @@ end; proc testUtf8; -var b: [4]U8; n, c, i: I64; +var b: [4]U8; n: INT; c, i: I32; begin b, n := encode('a'); if (n # 1) or (b[0] # 'a') then Rt.stop("Str.encode: a"); end; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 3c01069..8e0d099 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -24,7 +24,7 @@ Info* = record size*: I64; (* in bytes *) mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) - mode*: I64; (* its permission bits, rwxrwxrwx *) + mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) @@ -32,7 +32,7 @@ (* An open directory: getdents64's records, read a block at a time. *) Dir* = record - fd, pos, len: I64; + fd, pos, len: INT; buf: [8192]U8; end; @@ -43,10 +43,10 @@ env*: [][]U8; (* NAME=value, each added to this program's environment *) onlyEnv*: BOOL; (* env is its whole environment *) dir*: []U8; (* its working directory; "" for this program's *) - fds*: [3]I64; (* its standard input, output and error; 0 for this program's own *) - group*: I64; (* the process group it joins: 0 for this program's, -1 a new one it leads *) + fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) + group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) foreground*: BOOL; (* its group takes the terminal on standard input *) - tty*: I64; (* a pseudo-terminal's fd, to start a session on; 0 for none *) + tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); @@ -79,11 +79,11 @@ linked: [4096]U8; (* what readLink gives *) saved: Termios; (* the terminal as rawMode found it *) caughtSigs: U64; (* the signals catch has blocked, bit n - 1 for signal n *) - sigFd: I64; (* the signalfd that gives them, once caughtSigs isn't empty *) + sigFd: INT; (* the signalfd that gives them, once caughtSigs isn't empty *) (* Writes s to the file descriptor fd; the number of bytes written, or minus an errno. *) -proc write*(fd: I64; s: []U8): I64; +proc write*(fd: INT; s: []U8): INT; begin if LEN(s) = 0 then return 0; end; return SYSCALL(1, fd, ADR(s[0]), LEN(s)); @@ -91,7 +91,7 @@ (* Reads into buf from fd; the number of bytes read, 0 at the end, or minus an errno. *) -proc read*(fd: I64; buf: []U8): I64; +proc read*(fd: INT; buf: []U8): INT; begin if LEN(buf) = 0 then return 0; end; return SYSCALL(0, fd, ADR(buf[0]), LEN(buf)); @@ -99,7 +99,7 @@ (* path, a C string in buf, or false if it doesn't fit. *) proc cstr(path, buf: []U8): BOOL; -var i: I64; +var i: INT; begin if LEN(path) >= LEN(buf) then return false; end; for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; @@ -126,7 +126,7 @@ (* The file at path, opened as how asks: its file descriptor, or minus an errno. A file it creates is readable by all, writable by its owner. *) -proc open*(path: []U8; how: Accesses): I64; +proc open*(path: []U8; how: Accesses): INT; var buf: [4096]U8; begin if not cstr(path, buf) then return -36; end; (* ENAMETOOLONG *) @@ -134,55 +134,55 @@ end; (* The file at path, opened for reading. *) -proc openRead*(path: []U8): I64; +proc openRead*(path: []U8): INT; begin return open(path, {reading}); end; (* The file at path, made empty or new, opened for writing. *) -proc create*(path: []U8): I64; +proc create*(path: []U8): INT; begin return open(path, {writing, creating, truncating}); end; (* Moves fd to pos from the start of its file, or from the end if fromEnd: the new position, or minus an errno. *) -proc seek*(fd, pos: I64; fromEnd: BOOL): I64; +proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; begin if fromEnd then return SYSCALL(8, fd, pos, 2); end; (* lseek, SEEK_END *) return SYSCALL(8, fd, pos, 0); (* SEEK_SET *) end; -proc close*(fd: I64); +proc close*(fd: INT); begin SYSCALL(3, fd); end; (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) -proc mmap*(size: I64): ^U8; -var p: I64; +proc mmap*(size: INT): ^U8; +var p: INT; begin p := SYSCALL(9, 0, size, 3, 22H, -1, 0); (* read, write; private, anonymous *) if p < 0 then return nil; end; return ^U8(p); end; -proc munmap*(p: ^U8; size: I64); +proc munmap*(p: ^U8; size: INT); begin SYSCALL(11, p, size); end; (* Whether the file descriptor fd is a terminal: whether it has a terminal's settings to read (ioctl TCGETS). *) -proc isTerminal*(fd: I64): BOOL; -var t: [8]I64; (* struct termios: 60 bytes *) +proc isTerminal*(fd: INT): BOOL; +var t: [8]INT; (* struct termios: 60 bytes *) begin return SYSCALL(16, fd, 5401H, ADR(t)) = 0; end; (* Renames old to new, replacing what new names: 0, or minus an errno. *) -proc rename*(old, new: []U8): I64; +proc rename*(old, new: []U8): INT; var a, b: [4096]U8; begin if not cstr(old, a) or not cstr(new, b) then return -36; end; @@ -190,7 +190,7 @@ end; (* Removes the file at path; not a directory. *) -proc remove*(path: []U8): I64; +proc remove*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -198,7 +198,7 @@ end; (* Makes a directory at path, readable by all, writable by its owner. *) -proc makeDir*(path: []U8): I64; +proc makeDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -206,7 +206,7 @@ end; (* Removes the directory at path, which must be empty. *) -proc removeDir*(path: []U8): I64; +proc removeDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -214,7 +214,7 @@ end; (* Sets the permission bits of what path names. *) -proc chmod*(path: []U8; mode: I64): I64; +proc chmod*(path: []U8; mode: INT): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -222,7 +222,7 @@ end; (* Makes path a symbolic link to target. *) -proc symlink*(target, path: []U8): I64; +proc symlink*(target, path: []U8): INT; var a, b: [4096]U8; begin if not cstr(target, a) or not cstr(path, b) then return -36; end; @@ -231,8 +231,8 @@ (* What the symbolic link at path names, good until the next call; and 0, or minus an errno. *) -proc readLink*(path: []U8): ([]U8, I64); -var a: [4096]U8; n: I64; +proc readLink*(path: []U8): ([]U8, INT); +var a: [4096]U8; n: INT; begin if not cstr(path, a) then return "", -36; end; n := SYSCALL(89, ADR(a), ADR(linked), LEN(linked)); (* readlink *) @@ -244,8 +244,8 @@ (* The size of what path names, whether it is a directory or a regular file, following a symbolic link, and whether path is one; and 0, or minus an errno. *) -proc info*(path: []U8): (Info, I64); -var a: [4096]U8; st: [18]I64; i: Info; r: I64; (* struct stat: 144 bytes *) +proc info*(path: []U8): (Info, INT); +var a: [4096]U8; st: [18]INT; i: Info; r: INT; (* struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; r := SYSCALL(6, ADR(a), ADR(st)); (* lstat *) @@ -262,7 +262,7 @@ end; (* Opens the directory at path, for nextEntry. *) -proc openDir*(d: ^Dir; path: []U8): I64; +proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; begin d^.pos := 0; @@ -276,7 +276,7 @@ (* The next name in the directory, good until the next call, but . and .., in no order; false at the end. *) proc nextEntry*(d: ^Dir): ([]U8, BOOL); -var n, at: I64; +var n, at: INT; begin while true do if d^.pos >= d^.len then @@ -289,7 +289,7 @@ at := d^.pos + 19; n := 0; while d^.buf[at + n] # 0 do INC(n); end; - INC(d^.pos, I64(d^.buf[d^.pos + 16]) + I64(d^.buf[d^.pos + 17]) * 256); + INC(d^.pos, INT(d^.buf[d^.pos + 16]) + INT(d^.buf[d^.pos + 17]) * 256); if (d^.buf[at..at + n] # ".") and (d^.buf[at..at + n] # "..") then return d^.buf[at..at + n], true; end; @@ -305,7 +305,7 @@ (* Puts the terminal on fd into raw mode: no echo, no lines, no signals, no flow control, no CR to NL, no NL to CR NL; false if fd isn't a terminal. *) -proc rawMode*(fd: I64): BOOL; +proc rawMode*(fd: INT): BOOL; var t: Termios; begin if SYSCALL(16, fd, 5401H, ADR(saved)) # 0 then return false; end; (* TCGETS *) @@ -320,25 +320,25 @@ end; (* The terminal on fd as rawMode found it. *) -proc restoreMode*(fd: I64); +proc restoreMode*(fd: INT); begin SYSCALL(16, fd, 5402H, ADR(saved)); end; (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't say. *) -proc termSize*(fd: I64): (I64, I64); +proc termSize*(fd: INT): (INT, INT); var w: Winsize; begin if (SYSCALL(16, fd, 5413H, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then (* TIOCGWINSZ *) return 80, 24; end; - return I64(w.cols), I64(w.rows); + return INT(w.cols), INT(w.rows); end; (* Whether fd has something to read within ms milliseconds; ms < 0 waits as long as it takes. *) -proc ready*(fd, ms: I64): BOOL; +proc ready*(fd, ms: INT): BOOL; var p: Pollfd; begin p := {fd: I32(fd), events: 1}; (* POLLIN *) @@ -363,17 +363,17 @@ end; (* Waits ms milliseconds. *) -proc sleep*(ms: I64); +proc sleep*(ms: INT); var t: Timespec; at: I64; begin - at := clock() + ms * 1000000; + at := clock() + I64(ms) * 1000000; t := {sec: at div 1000000000, nsec: at mod 1000000000}; repeat until SYSCALL(230, 1, 1, ADR(t), 0) # -4; (* clock_nanosleep until t, again after EINTR *) end; (* Fills b with random bytes, from the kernel. *) proc random*(b: []U8); -var n, r: I64; +var n, r: INT; begin n := 0; while n < LEN(b) do (* a signal may cut it short *) @@ -386,8 +386,8 @@ bytes, or, if serving, bound to it and taking connections: its file descriptor, or minus an errno. It is closed in a program exec starts. *) -proc socket(family, kind: I64; a: ^U8; size: I64; serving: BOOL): I64; -var fd, r, one: I64; +proc socket(family, kind: INT; a: ^U8; size: INT; serving: BOOL): INT; +var fd, r, one: INT; begin fd := SYSCALL(41, family, kind | 80000H, 0); (* socket, SOCK_CLOEXEC *) if fd < 0 then return fd; end; @@ -405,7 +405,7 @@ (* path as a Unix socket's address in a, or false if it is too long. *) proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; -var i: I64; +var i: INT; begin if LEN(path) >= LEN(a^.path) then return false; end; a^.family := 1; (* AF_UNIX *) @@ -414,7 +414,7 @@ end; (* A stream socket connected to the Unix socket at path. *) -proc connect*(path: []U8): I64; +proc connect*(path: []U8): INT; var a: SockaddrUn; begin if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *) @@ -423,7 +423,7 @@ (* A stream socket made at path, which must not be there yet, taking connections. *) -proc listen*(path: []U8): I64; +proc listen*(path: []U8): INT; var a: SockaddrUn; begin if not unixAddr(path, ADR(a)) then return -36; end; @@ -431,18 +431,18 @@ end; (* The value of the hexadecimal digit c, or -1 if it isn't one. *) -proc hexDigit(c: U8): I64; +proc hexDigit(c: U8): INT; begin - if (c >= '0') and (c <= '9') then return I64(c - '0'); end; - if (c >= 'a') and (c <= 'f') then return I64(c - 'a') + 10; end; - if (c >= 'A') and (c <= 'F') then return I64(c - 'A') + 10; end; + if (c >= '0') and (c <= '9') then return INT(c - '0'); end; + if (c >= 'a') and (c <= 'f') then return INT(c - 'a') + 10; end; + if (c >= 'A') and (c <= 'F') then return INT(c - 'A') + 10; end; return -1; end; (* The IPv4 address s, four numbers and dots, in out's 4 bytes, or false if it isn't one. *) proc ipv4(s, out: []U8): BOOL; -var b: [4]U8; n, i, v, d: I64; +var b: [4]U8; n, i, v, d: INT; begin i := 0; for n := 0 to 3 do @@ -453,7 +453,7 @@ v := 0; d := 0; while (i < LEN(s)) and (d < 3) and (s[i] >= '0') and (s[i] <= '9') do - v := v * 10 + I64(s[i] - '0'); + v := v * 10 + INT(s[i] - '0'); INC(i); INC(d); end; @@ -469,7 +469,7 @@ standing for groups of zeros, in out's 16 bytes, or false if it isn't one. *) proc ipv6(s, out: []U8): BOOL; -var g: [8]I64; n, gap, i, v, d, k: I64; +var g: [8]INT; n, gap, i, v, d, k: INT; begin n := 0; gap := -1; (* the group the :: is before *) @@ -512,7 +512,7 @@ (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size of its struct sockaddr_in or sockaddr_in6, or 0 if either is wrong. *) -proc inetAddr(addr: []U8; port: I64; a: ^Sockaddr): I64; +proc inetAddr(addr: []U8; port: INT; a: ^Sockaddr): INT; begin if (port < 0) or (port > 65535) then return 0; end; a^.port[0] := U8(port shr 8); (* in the network's order, the high byte first *) @@ -524,37 +524,37 @@ (* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) if they are wrong. *) -proc inet(addr: []U8; port, kind: I64; serving: BOOL): I64; -var a: Sockaddr; size: I64; +proc inet(addr: []U8; port, kind: INT; serving: BOOL): INT; +var a: Sockaddr; size: INT; begin size := inetAddr(addr, port, ADR(a)); if size = 0 then return -22; end; - return socket(I64(a.family), kind, ^U8(ADR(a)), size, serving); + return socket(INT(a.family), kind, ^U8(ADR(a)), size, serving); end; (* A stream socket connected to port at addr, an IPv4 or IPv6 address as numbers. *) -proc connectTcp*(addr: []U8; port: I64): I64; +proc connectTcp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 1, false); (* SOCK_STREAM *) end; (* A socket of datagrams to and from port at addr. *) -proc connectUdp*(addr: []U8; port: I64): I64; +proc connectUdp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 2, false); (* SOCK_DGRAM *) end; (* A stream socket taking connections on port at addr; "0.0.0.0" or "::" is every address the machine has. *) -proc listenTcp*(addr: []U8; port: I64): I64; +proc listenTcp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 1, true); end; (* The next connection to the socket fd, waiting for one: a socket of its own, closed in a program exec starts; or minus an errno. *) -proc accept*(fd: I64): I64; +proc accept*(fd: INT): INT; begin return SYSCALL(288, fd, 0, 0, 80000H); (* accept4, SOCK_CLOEXEC *) end; @@ -562,7 +562,7 @@ (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) where write would stop the program with SIGPIPE: when the other end has closed it. *) -proc send*(fd: I64; s: []U8): I64; +proc send*(fd: INT; s: []U8): INT; begin if LEN(s) = 0 then return 0; end; return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) @@ -571,8 +571,8 @@ (* The first of fds that has something to read within ms milliseconds, or that has reached its end or failed, so that a read won't wait; -1 if none has. ms < 0 waits as long as it takes. At most 64 fds. *) -proc readyOf*(fds: []I64; ms: I64): I64; -var p: [64]Pollfd; n, i: I64; +proc readyOf*(fds: []INT; ms: INT): INT; +var p: [64]Pollfd; n, i: INT; begin n := LEN(fds); if n > LEN(p) then n := LEN(p); end; @@ -587,7 +587,7 @@ (* The value of the environment variable name; "" if it isn't set. The environment follows the arguments and their nil. *) proc env*(name: []U8): []U8; -var e: ^^U8; s: ^U8; i, n, m: I64; +var e: ^^U8; s: ^U8; i, n, m: INT; begin e := ARGV() + (ARGC() + 1); i := 0; @@ -607,8 +607,8 @@ (* The working directory, good until the next call; and 0, or minus an errno. *) -proc workDir*: ([]U8, I64); -var r: I64; +proc workDir*: ([]U8, INT); +var r: INT; begin r := SYSCALL(79, ADR(cwd), LEN(cwd)); (* getcwd: its length with its zero *) if r < 0 then return "", r; end; @@ -616,7 +616,7 @@ end; (* Makes path the working directory. *) -proc setWorkDir*(path: []U8): I64; +proc setWorkDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -626,8 +626,8 @@ (* A pseudo-terminal: the file descriptor of its controlling side, or minus an errno. Its other side, the terminal its programs run on, is unlocked, for spawn to open. *) -proc openPty*: I64; -var a: [4096]U8; fd, unlock: I64; +proc openPty*: INT; +var a: [4096]U8; fd, unlock: INT; begin cstr("/dev/ptmx", a); fd := SYSCALL(2, ADR(a), 80102H); (* open: O_RDWR, O_NOCTTY, O_CLOEXEC *) @@ -642,7 +642,7 @@ (* Tells the terminal of the pseudo-terminal fd that it has cols columns and rows rows: 0, or minus an errno. *) -proc setTermSize*(fd, cols, rows: I64): I64; +proc setTermSize*(fd, cols, rows: INT): INT; var w: Winsize; begin w := {rows: U16(rows), cols: U16(cols)}; @@ -651,7 +651,7 @@ (* Copies s and a zero to at: the byte after them. *) proc put(s: []U8; at: ^U8): ^U8; -var i: I64; +var i: INT; begin for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; at[LEN(s)] := 0; @@ -661,7 +661,7 @@ (* Whether the NAME=value s, a C string, names a variable that one of env gives. *) proc replaced(s: ^U8; env: [][]U8): BOOL; -var i, n: I64; +var i, n: INT; begin for i := 0 to LEN(env) - 1 do n := 0; @@ -675,8 +675,8 @@ strings and a nil, then the environment old with each NAME=value of env in place of its variable or added, and a nil; or nil. The environment follows the arguments. *) -proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); -var p: ^^U8; s: ^U8; size, n, i, k: I64; +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, INT); +var p: ^^U8; s: ^U8; size, n, i, k: INT; begin n := 0; while old[n] # nil do INC(n); end; @@ -700,30 +700,30 @@ (* A pipe: the fd to read and the fd to write, both closed in a program exec starts; or minus an errno and -1. *) -proc pipe*: (I64, I64); -var fds: [2]I32; r: I64; +proc pipe*: (INT, INT); +var fds: [2]I32; r: INT; begin r := SYSCALL(293, ADR(fds), 80000H); (* pipe2, O_CLOEXEC *) if r < 0 then return r, -1; end; - return I64(fds[0]), I64(fds[1]); + return INT(fds[0]), INT(fds[1]); end; (* This program's process id. *) -proc pid*: I64; +proc pid*: INT; begin return SYSCALL(39); (* getpid *) end; (* Moves the program pid, or this one if pid is 0, to the process group group, or to a new one it leads if group is 0. *) -proc setGroup*(pid, group: I64): I64; +proc setGroup*(pid, group: INT): INT; begin return SYSCALL(109, pid, group); (* setpgid *) end; (* Gives the keys of the terminal on fd, and its signals, to the process group group. *) -proc foreground*(fd, group: I64): I64; +proc foreground*(fd, group: INT): INT; var g: I32; begin g := I32(group); @@ -732,22 +732,22 @@ (* The process group the terminal on fd gives its keys to, or minus an errno. *) -proc foregroundOf*(fd: I64): I64; -var g: I32; r: I64; +proc foregroundOf*(fd: INT): INT; +var g: I32; r: INT; begin r := SYSCALL(16, fd, 540FH, ADR(g)); (* TIOCGPGRP *) if r < 0 then return r; end; - return I64(g); + return INT(g); end; (* Starts the program p describes: its process id, or minus an errno. A program that can't be started ends with status 127. Every signal is blocked from the fork until the program has its own: its group, its terminal's foreground, and signals as they are at first. *) -proc spawn*(p: Program): I64; +proc spawn*(p: Program): INT; var a, dir: [4096]U8; name: [32]U8; dfl: [4]U64; (* struct sigaction: SIG_DFL *) - argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: I64; + argv: ^^U8; none: ^U8; all, old, empty: U64; size, pid, n, i, fd, g: INT; begin if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) if p.tty # 0 then @@ -808,21 +808,21 @@ its exit status; killed or stopped, with the signal's number. Without block it doesn't wait: 0 and running if none has changed. Minus an errno, and running, if there is none to wait for. *) -proc wait*(pid: I64; block: BOOL): (I64, Status, I64); -var status: I32; r, options: I64; +proc wait*(pid: INT; block: BOOL): (INT, Status, INT); +var status: I32; r, options: INT; begin options := 3; (* WNOHANG, WUNTRACED *) if block then options := 2; end; r := SYSCALL(61, pid, ADR(status), options, 0); (* wait4 *) if r <= 0 then return r, running, 0; end; - if status & 7FH = 0 then return r, exited, I64(status) shr 8 & 0FFH; end; - if status & 0FFH = 7FH then return r, stopped, I64(status) shr 8 & 0FFH; end; - return r, killed, I64(status) & 7FH; + if status & 7FH = 0 then return r, exited, INT(status) shr 8 & 0FFH; end; + if status & 0FFH = 7FH then return r, stopped, INT(status) shr 8 & 0FFH; end; + return r, killed, INT(status) & 7FH; end; (* Sends the program pid, or the group -pid, the signal: 0, or minus an errno. *) -proc kill*(pid, signal: I64): I64; +proc kill*(pid, signal: INT): INT; begin return SYSCALL(62, pid, signal); end; @@ -831,8 +831,8 @@ they would, and gives them on an fd, which a later catch adds to and gives again; or minus an errno. Only the thread that catches them blocks them, and threads it starts later. *) -proc catch*(sigs: []I64): I64; -var m: U64; i, fd: I64; +proc catch*(sigs: []INT): INT; +var m: U64; i, fd: INT; begin m := caughtSigs; for i := 0 to LEN(sigs) - 1 do @@ -853,16 +853,16 @@ end; (* The next signal catch's fd has, or 0 if none is waiting. *) -proc caught*(fd: I64): I64; +proc caught*(fd: INT): INT; var info: [128]U8; (* struct signalfd_siginfo: the number first, little-endian, below 65 *) begin if read(fd, info) # 128 then return 0; end; - return I64(info[0]); + return INT(info[0]); end; (* Makes the signal sig do nothing, or, if not yes, what it does at first: 0, or minus an errno. *) -proc ignore*(sig: I64; yes: BOOL): I64; +proc ignore*(sig: INT; yes: BOOL): INT; var act: [4]U64; (* struct sigaction: its handler, SIG_IGN or SIG_DFL, first *) begin if yes then act[0] := 1; end; @@ -876,7 +876,7 @@ end; (* Ends the program, at once, with status. *) -proc exit*(status: I64); +proc exit*(status: INT); begin SYSCALL(231, status); end; @@ -890,7 +890,7 @@ one is sent; and a program starts with them neither caught nor ignored. *) proc testPrograms; -var pty, child, n, m, r, w, sigs, c: I64; st: Status; buf: [64]U8; +var pty, child, n, m, r, w, sigs, c: INT; st: Status; buf: [64]U8; begin pty := openPty(); if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; @@ -960,7 +960,7 @@ sees; addresses read and refused; a TCP connection on the loopback; and a datagram to a port where no one listens, refused. *) proc testNet; -var t, l, c, a, u: I64; x, y: [16]U8; s, s6: Sockaddr; +var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr; begin t := clock(); sleep(20); @@ -1001,7 +1001,7 @@ directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; diff --git a/lib/Sys.raz b/lib/Sys.raz index 248228d..1ffb6d2 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -37,7 +37,7 @@ Info* = record size*: I64; (* in bytes *) mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *) - mode*: I64; (* its permission bits, rwxrwxrwx *) + mode*: INT; (* its permission bits, rwxrwxrwx *) dir*: BOOL; (* a directory *) file*: BOOL; (* a regular file: not a directory, device, pipe or socket *) symlink*: BOOL; (* the path itself is a symbolic link *) @@ -55,10 +55,10 @@ env*: [][]U8; (* NAME=value, each added to this program's environment *) onlyEnv*: BOOL; (* env is its whole environment *) dir*: []U8; (* its working directory; "" for this program's *) - fds*: [3]I64; (* its standard input, output and error; 0 for this program's own *) - group*: I64; (* the process group it joins: 0 for this program's, -1 a new one it leads *) + fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) + group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) foreground*: BOOL; (* its group takes the terminal on standard input *) - tty*: I64; (* a pseudo-terminal's fd, to start a session on; 0 for none *) + tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) end; (* How a program that wait waited for changed. *) Status* = (running, exited, killed, stopped); @@ -96,38 +96,38 @@ sigRead, sigWrite: I32; (* the pipe the handler of caught signals writes their numbers to, once open *) (* minus errno, as a system call returns it *) -proc failed: I64; +proc failed: INT; var e: ^I32; begin e := C.__errno_location(); - return -I64(e^); + return -INT(e^); end; (* Writes s to the file descriptor fd; the number of bytes written, or minus an errno. *) -proc write*(fd: I64; s: []U8): I64; -var r: I64; +proc write*(fd: INT; s: []U8): INT; +var r: INT; begin if LEN(s) = 0 then return 0; end; - r := C.write(I32(fd), ADR(s[0]), U64(LEN(s))); + r := C.write(I32(fd), ADR(s[0]), UINT(LEN(s))); if r < 0 then return failed(); end; return r; end; (* Reads into buf from fd; the number of bytes read, 0 at the end, or minus an errno. *) -proc read*(fd: I64; buf: []U8): I64; -var r: I64; +proc read*(fd: INT; buf: []U8): INT; +var r: INT; begin if LEN(buf) = 0 then return 0; end; - r := C.read(I32(fd), ADR(buf[0]), U64(LEN(buf))); + r := C.read(I32(fd), ADR(buf[0]), UINT(LEN(buf))); if r < 0 then return failed(); end; return r; end; (* path, a C string in buf, or false if it doesn't fit. *) proc cstr(path, buf: []U8): BOOL; -var i: I64; +var i: INT; begin if LEN(path) >= LEN(buf) then return false; end; for i := 0 to LEN(path) - 1 do buf[i] := path[i]; end; @@ -154,30 +154,30 @@ (* The file at path, opened as how asks: its file descriptor, or minus an errno. A file it creates is readable by all, writable by its owner. *) -proc open*(path: []U8; how: Accesses): I64; -var buf: [4096]U8; r: I64; +proc open*(path: []U8; how: Accesses): INT; +var buf: [4096]U8; r: INT; begin if not cstr(path, buf) then return -36; end; - r := I64(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *) + r := INT(C.open(ADR(buf[0]), flags(how), 1A4H)); (* mode 644 *) if r < 0 then return failed(); end; return r; end; (* The file at path, opened for reading. *) -proc openRead*(path: []U8): I64; +proc openRead*(path: []U8): INT; begin return open(path, {reading}); end; (* The file at path, made empty or new, opened for writing. *) -proc create*(path: []U8): I64; +proc create*(path: []U8): INT; begin return open(path, {writing, creating, truncating}); end; (* Moves fd to pos from the start of its file, or from the end if fromEnd: the new position, or minus an errno. *) -proc seek*(fd, pos: I64; fromEnd: BOOL): I64; +proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; var r: I64; begin if fromEnd then r := C.lseek(I32(fd), pos, 2); (* SEEK_END *) @@ -186,28 +186,28 @@ return r; end; -proc close*(fd: I64); +proc close*(fd: INT); begin C.close(I32(fd)); end; (* size bytes of fresh, zeroed memory from the system, a multiple of pages, or nil. *) -proc mmap*(size: I64): ^U8; +proc mmap*(size: INT): ^U8; var p: ^U8; begin - p := C.mmap(nil, U64(size), 3, 22H, -1, 0); (* read, write; private, anonymous *) - if I64(p) = -1 then return nil; end; + p := C.mmap(nil, UINT(size), 3, 22H, -1, 0); (* read, write; private, anonymous *) + if INT(p) = -1 then return nil; end; return p; end; -proc munmap*(p: ^U8; size: I64); +proc munmap*(p: ^U8; size: INT); begin - C.munmap(p, U64(size)); + C.munmap(p, UINT(size)); end; (* Renames old to new, replacing what new names: 0, or minus an errno. *) -proc rename*(old, new: []U8): I64; +proc rename*(old, new: []U8): INT; var a, b: [4096]U8; begin if not cstr(old, a) or not cstr(new, b) then return -36; end; @@ -216,7 +216,7 @@ end; (* Removes the file at path; not a directory. *) -proc remove*(path: []U8): I64; +proc remove*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -225,7 +225,7 @@ end; (* Makes a directory at path, readable by all, writable by its owner. *) -proc makeDir*(path: []U8): I64; +proc makeDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -234,7 +234,7 @@ end; (* Removes the directory at path, which must be empty. *) -proc removeDir*(path: []U8): I64; +proc removeDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -243,7 +243,7 @@ end; (* Sets the permission bits of what path names. *) -proc chmod*(path: []U8; mode: I64): I64; +proc chmod*(path: []U8; mode: INT): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -252,7 +252,7 @@ end; (* Makes path a symbolic link to target. *) -proc symlink*(target, path: []U8): I64; +proc symlink*(target, path: []U8): INT; var a, b: [4096]U8; begin if not cstr(target, a) or not cstr(path, b) then return -36; end; @@ -262,11 +262,11 @@ (* What the symbolic link at path names, good until the next call; and 0, or minus an errno. *) -proc readLink*(path: []U8): ([]U8, I64); -var a: [4096]U8; n: I64; +proc readLink*(path: []U8): ([]U8, INT); +var a: [4096]U8; n: INT; begin if not cstr(path, a) then return "", -36; end; - n := C.readlink(ADR(a[0]), ADR(linked[0]), U64(LEN(linked))); + n := C.readlink(ADR(a[0]), ADR(linked[0]), UINT(LEN(linked))); if n < 0 then return "", failed(); end; if n = LEN(linked) then return "", -36; end; (* it may be cut short *) return linked[0..n], 0; @@ -275,8 +275,8 @@ (* The size of what path names, whether it is a directory or a regular file, following a symbolic link, and whether path is one; and 0, or minus an errno. *) -proc info*(path: []U8): (Info, I64); -var a: [4096]U8; st: [18]I64; i: Info; (* glibc's struct stat: 144 bytes *) +proc info*(path: []U8): (Info, INT); +var a: [4096]U8; st: [18]INT; i: Info; (* glibc's struct stat: 144 bytes *) begin if not cstr(path, a) then return i, -36; end; if C.lstat(ADR(a[0]), ^U8(ADR(st))) # 0 then return i, failed(); end; @@ -291,7 +291,7 @@ end; (* Opens the directory at path, for nextEntry. *) -proc openDir*(d: ^Dir; path: []U8): I64; +proc openDir*(d: ^Dir; path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -303,7 +303,7 @@ (* The next name in the directory, good until the next call, but . and .., in no order; false at the end. *) proc nextEntry*(d: ^Dir): ([]U8, BOOL); -var e: ^U8; n: I64; +var e: ^U8; n: INT; begin while true do e := C.readdir(d^.d); @@ -324,7 +324,7 @@ (* Puts the terminal on fd into raw mode, as cfmakeraw makes it: no echo, no lines, no signals, no flow control, no CR to NL, no NL to CR NL; false if fd isn't a terminal. *) -proc rawMode*(fd: I64): BOOL; +proc rawMode*(fd: INT): BOOL; var t: [256]U8; begin if C.tcgetattr(I32(fd), ADR(saved[0])) # 0 then return false; end; @@ -334,25 +334,25 @@ end; (* The terminal on fd as rawMode found it. *) -proc restoreMode*(fd: I64); +proc restoreMode*(fd: INT); begin C.tcsetattr(I32(fd), 0, ADR(saved[0])); end; (* The columns and rows of the terminal on fd; 80 by 24 if it doesn't say. *) -proc termSize*(fd: I64): (I64, I64); +proc termSize*(fd: INT): (INT, INT); var w: Winsize; begin if (C.ioctl(I32(fd), TIOCGWINSZ, ADR(w)) # 0) or (w.cols = 0) or (w.rows = 0) then return 80, 24; end; - return I64(w.cols), I64(w.rows); + return INT(w.cols), INT(w.rows); end; (* Whether fd has something to read within ms milliseconds; ms < 0 waits as long as it takes. *) -proc ready*(fd, ms: I64): BOOL; +proc ready*(fd, ms: INT): BOOL; var p: Pollfd; r: I32; begin p := {fd: I32(fd), events: 1}; (* POLLIN *) @@ -382,21 +382,21 @@ end; (* Waits ms milliseconds. *) -proc sleep*(ms: I64); +proc sleep*(ms: INT); var t: Timespec; at: I64; begin - at := clock() + ms * 1000000; + at := clock() + I64(ms) * 1000000; t := {sec: at div 1000000000, nsec: at mod 1000000000}; repeat until C.clock_nanosleep(1, 1, ^U8(ADR(t)), nil) # 4; (* until t, again after EINTR *) end; (* Fills b with random bytes, from the kernel. *) proc random*(b: []U8); -var n, r: I64; +var n, r: INT; begin n := 0; while n < LEN(b) do (* a signal may cut it short *) - r := C.getrandom(ADR(b[n]), U64(LEN(b) - n), 0); + r := C.getrandom(ADR(b[n]), UINT(LEN(b) - n), 0); if r > 0 then INC(n, r); end; end; end; @@ -405,8 +405,8 @@ bytes, or, if serving, bound to it and taking connections: its file descriptor, or minus an errno. It is closed in a program exec starts. *) -proc socket(family, kind: I64; a: ^U8; size: I64; serving: BOOL): I64; -var fd, r, one: I32; err: I64; +proc socket(family, kind: INT; a: ^U8; size: INT; serving: BOOL): INT; +var fd, r, one: I32; err: INT; begin fd := C.socket(I32(family), I32(kind) | 80000H, 0); (* SOCK_CLOEXEC *) if fd < 0 then return failed(); end; @@ -423,12 +423,12 @@ C.close(fd); return err; end; - return I64(fd); + return INT(fd); end; (* path as a Unix socket's address in a, or false if it is too long. *) proc unixAddr(path: []U8; a: ^SockaddrUn): BOOL; -var i: I64; +var i: INT; begin if LEN(path) >= LEN(a^.path) then return false; end; a^.family := 1; (* AF_UNIX *) @@ -437,7 +437,7 @@ end; (* A stream socket connected to the Unix socket at path. *) -proc connect*(path: []U8): I64; +proc connect*(path: []U8): INT; var a: SockaddrUn; begin if not unixAddr(path, ADR(a)) then return -36; end; (* ENAMETOOLONG *) @@ -446,7 +446,7 @@ (* A stream socket made at path, which must not be there yet, taking connections. *) -proc listen*(path: []U8): I64; +proc listen*(path: []U8): INT; var a: SockaddrUn; begin if not unixAddr(path, ADR(a)) then return -36; end; @@ -456,7 +456,7 @@ (* addr, an IPv4 or IPv6 address as numbers, and port, in a: the size of its struct sockaddr_in or sockaddr_in6, or 0 if either is wrong. *) -proc inetAddr(addr: []U8; port: I64; a: ^Sockaddr): I64; +proc inetAddr(addr: []U8; port: INT; a: ^Sockaddr): INT; var s: [64]U8; begin if (port < 0) or (port > 65535) or not cstr(addr, s) then return 0; end; @@ -469,52 +469,52 @@ (* A socket of kind at addr and port, as socket makes it; -22 (EINVAL) if they are wrong. *) -proc inet(addr: []U8; port, kind: I64; serving: BOOL): I64; -var a: Sockaddr; size: I64; +proc inet(addr: []U8; port, kind: INT; serving: BOOL): INT; +var a: Sockaddr; size: INT; begin size := inetAddr(addr, port, ADR(a)); if size = 0 then return -22; end; - return socket(I64(a.family), kind, ^U8(ADR(a)), size, serving); + return socket(INT(a.family), kind, ^U8(ADR(a)), size, serving); end; (* A stream socket connected to port at addr, an IPv4 or IPv6 address as numbers. *) -proc connectTcp*(addr: []U8; port: I64): I64; +proc connectTcp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 1, false); (* SOCK_STREAM *) end; (* A socket of datagrams to and from port at addr. *) -proc connectUdp*(addr: []U8; port: I64): I64; +proc connectUdp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 2, false); (* SOCK_DGRAM *) end; (* A stream socket taking connections on port at addr; "0.0.0.0" or "::" is every address the machine has. *) -proc listenTcp*(addr: []U8; port: I64): I64; +proc listenTcp*(addr: []U8; port: INT): INT; begin return inet(addr, port, 1, true); end; (* The next connection to the socket fd, waiting for one: a socket of its own, closed in a program exec starts; or minus an errno. *) -proc accept*(fd: I64): I64; +proc accept*(fd: INT): INT; var r: I32; begin r := C.accept4(I32(fd), nil, nil, 80000H); (* SOCK_CLOEXEC *) if r < 0 then return failed(); end; - return I64(r); + return INT(r); end; (* Writes s to the socket fd, as write does, but returns -32 (EPIPE) where write would stop the program with SIGPIPE: when the other end has closed it. *) -proc send*(fd: I64; s: []U8): I64; -var r: I64; +proc send*(fd: INT; s: []U8): INT; +var r: INT; begin if LEN(s) = 0 then return 0; end; - r := C.send(I32(fd), ADR(s[0]), U64(LEN(s)), 4000H); (* MSG_NOSIGNAL, glibc's *) + r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), 4000H); (* MSG_NOSIGNAL, glibc's *) if r < 0 then return failed(); end; return r; end; @@ -522,13 +522,13 @@ (* The first of fds that has something to read within ms milliseconds, or that has reached its end or failed, so that a read won't wait; -1 if none has. ms < 0 waits as long as it takes. At most 64 fds. *) -proc readyOf*(fds: []I64; ms: I64): I64; -var p: [64]Pollfd; n, i: I64; r: I32; +proc readyOf*(fds: []INT; ms: INT): INT; +var p: [64]Pollfd; n, i: INT; r: I32; begin n := LEN(fds); if n > LEN(p) then n := LEN(p); end; for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *) - repeat r := C.poll(^U8(ADR(p)), U64(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) + repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) if r <= 0 then return -1; end; for i := 0 to n - 1 do if p[i].revents # 0 then return fds[i]; end; @@ -538,7 +538,7 @@ (* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; -var a: [4096]U8; s: ^U8; n: I64; +var a: [4096]U8; s: ^U8; n: INT; begin if not cstr(name, a) then return ""; end; s := C.getenv(ADR(a[0])); @@ -550,8 +550,8 @@ (* The working directory, good until the next call; and 0, or minus an errno. *) -proc workDir*: ([]U8, I64); -var n: I64; +proc workDir*: ([]U8, INT); +var n: INT; begin if C.getcwd(ADR(cwd[0]), LEN(cwd)) = nil then return "", failed(); end; n := 0; @@ -560,7 +560,7 @@ end; (* Makes path the working directory. *) -proc setWorkDir*(path: []U8): I64; +proc setWorkDir*(path: []U8): INT; var a: [4096]U8; begin if not cstr(path, a) then return -36; end; @@ -571,8 +571,8 @@ (* A pseudo-terminal: the file descriptor of its controlling side, or minus an errno. Its other side, the terminal its programs run on, is unlocked, for spawn to open. *) -proc openPty*: I64; -var fd: I32; err: I64; +proc openPty*: INT; +var fd: I32; err: INT; begin fd := C.posix_openpt(102H); (* O_RDWR, O_NOCTTY: glibc's *) if fd < 0 then return failed(); end; @@ -581,12 +581,12 @@ C.close(fd); return err; end; - return I64(fd); + return INT(fd); end; (* Tells the terminal of the pseudo-terminal fd that it has cols columns and rows rows: 0, or minus an errno. *) -proc setTermSize*(fd, cols, rows: I64): I64; +proc setTermSize*(fd, cols, rows: INT): INT; var w: Winsize; begin w := {rows: U16(rows), cols: U16(cols)}; @@ -596,7 +596,7 @@ (* Copies s and a zero to at: the byte after them. *) proc put(s: []U8; at: ^U8): ^U8; -var i: I64; +var i: INT; begin for i := 0 to LEN(s) - 1 do at[i] := s[i]; end; at[LEN(s)] := 0; @@ -606,7 +606,7 @@ (* Whether the NAME=value s, a C string, names a variable that one of env gives. *) proc replaced(s: ^U8; env: [][]U8): BOOL; -var i, n: I64; +var i, n: INT; begin for i := 0 to LEN(env) - 1 do n := 0; @@ -620,8 +620,8 @@ strings and a nil, then the environment old with each NAME=value of env in place of its variable or added, and a nil; or nil. The environment follows the arguments. *) -proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, I64); -var p: ^^U8; s: ^U8; size, n, i, k: I64; +proc argsAndEnv(args, env: [][]U8; old: ^^U8): (^^U8, INT); +var p: ^^U8; s: ^U8; size, n, i, k: INT; begin n := 0; while old[n] # nil do INC(n); end; @@ -645,22 +645,22 @@ (* A pipe: the fd to read and the fd to write, both closed in a program exec starts; or minus an errno and -1. *) -proc pipe*: (I64, I64); +proc pipe*: (INT, INT); var fds: [2]I32; begin if C.pipe2(ADR(fds[0]), 80000H) # 0 then return failed(), -1; end; (* O_CLOEXEC, glibc's *) - return I64(fds[0]), I64(fds[1]); + return INT(fds[0]), INT(fds[1]); end; (* This program's process id. *) -proc pid*: I64; +proc pid*: INT; begin - return I64(C.getpid()); + return INT(C.getpid()); end; (* Moves the program pid, or this one if pid is 0, to the process group group, or to a new one it leads if group is 0. *) -proc setGroup*(pid, group: I64): I64; +proc setGroup*(pid, group: INT): INT; begin if C.setpgid(I32(pid), I32(group)) # 0 then return failed(); end; return 0; @@ -668,7 +668,7 @@ (* Gives the keys of the terminal on fd, and its signals, to the process group group. *) -proc foreground*(fd, group: I64): I64; +proc foreground*(fd, group: INT): INT; begin if C.tcsetpgrp(I32(fd), I32(group)) # 0 then return failed(); end; return 0; @@ -676,21 +676,21 @@ (* The process group the terminal on fd gives its keys to, or minus an errno. *) -proc foregroundOf*(fd: I64): I64; +proc foregroundOf*(fd: INT): INT; var g: I32; begin g := C.tcgetpgrp(I32(fd)); if g < 0 then return failed(); end; - return I64(g); + return INT(g); end; (* Starts the program p describes: its process id, or minus an errno. A program that can't be started ends with status 127. Every signal is blocked from the fork until the program has its own: its group, its terminal's foreground, and signals as they are at first. *) -proc spawn*(p: Program): I64; +proc spawn*(p: Program): INT; var - a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: I64; + a, dir, name: [4096]U8; argv: ^^U8; none, s: ^U8; size, n, err, i, g: INT; all, old, empty: [16]U64; dfl: Sigaction; pid, fd: I32; (* sigset_t: 128 bytes *) begin if not cstr(p.path, a) or not cstr(p.dir, dir) then return -36; end; (* ENAMETOOLONG *) @@ -737,14 +737,14 @@ if (pid > 0) and (p.tty = 0) then (* as the program does, for whichever of the two runs first *) g := p.group; - if g = -1 then g := I64(pid); elsif g = 0 then g := I64(C.getpgrp()); end; + if g = -1 then g := INT(pid); elsif g = 0 then g := INT(C.getpgrp()); end; if p.group # 0 then C.setpgid(pid, I32(g)); end; if p.foreground then C.tcsetpgrp(0, I32(g)); end; end; C.sigprocmask(2, ^U8(ADR(old)), nil); munmap(^U8(argv), size); if pid < 0 then return err; end; - return I64(pid); + return INT(pid); end; (* Waits for the program pid, or any of this program's if pid is -1, to @@ -753,7 +753,7 @@ block it doesn't wait: 0 and running if none has changed. Minus an errno, and running, if there is none to wait for. The status is read as glibc's macros read it. *) -proc wait*(pid: I64; block: BOOL): (I64, Status, I64); +proc wait*(pid: INT; block: BOOL): (INT, Status, INT); var status, options, r: I32; begin options := 3; (* WNOHANG, WUNTRACED *) @@ -761,14 +761,14 @@ r := C.waitpid(I32(pid), ADR(status), options); if r < 0 then return failed(), running, 0; end; if r = 0 then return 0, running, 0; end; - if status & 7FH = 0 then return I64(r), exited, I64(status) shr 8 & 0FFH; end; - if status & 0FFH = 7FH then return I64(r), stopped, I64(status) shr 8 & 0FFH; end; - return I64(r), killed, I64(status) & 7FH; + if status & 7FH = 0 then return INT(r), exited, INT(status) shr 8 & 0FFH; end; + if status & 0FFH = 7FH then return INT(r), stopped, INT(status) shr 8 & 0FFH; end; + return INT(r), killed, INT(status) & 7FH; end; (* Sends the program pid, or the group -pid, the signal: 0, or minus an errno. *) -proc kill*(pid, signal: I64): I64; +proc kill*(pid, signal: INT): INT; begin if C.kill(I32(pid), I32(signal)) # 0 then return failed(); end; return 0; @@ -789,8 +789,8 @@ (* Catches the signals sigs: they don't do what they would, but give their numbers on an fd, which a later catch adds to and gives again; or minus an errno. *) -proc catch*(sigs: []I64): I64; -var fds: [2]I32; act: Sigaction; i: I64; +proc catch*(sigs: []INT): INT; +var fds: [2]I32; act: Sigaction; i: INT; begin for i := 0 to LEN(sigs) - 1 do if (sigs[i] < 1) or (sigs[i] > 64) or (sigs[i] = sigKill) or (sigs[i] = sigStop) @@ -807,20 +807,20 @@ for i := 0 to LEN(sigs) - 1 do if C.sigaction(I32(sigs[i]), ^U8(ADR(act)), nil) # 0 then return failed(); end; end; - return I64(sigRead); + return INT(sigRead); end; (* The next signal catch's fd has, or 0 if none is waiting. *) -proc caught*(fd: I64): I64; +proc caught*(fd: INT): INT; var b: [1]U8; begin if read(fd, b) # 1 then return 0; end; - return I64(b[0]); + return INT(b[0]); end; (* Makes the signal sig do nothing, or, if not yes, what it does at first: 0, or minus an errno. *) -proc ignore*(sig: I64; yes: BOOL): I64; +proc ignore*(sig: INT; yes: BOOL): INT; var act: Sigaction; begin if yes then act.handler := Handler(1); end; (* SIG_IGN *) @@ -828,7 +828,7 @@ return 0; end; -proc isTerminal*(fd: I64): BOOL; +proc isTerminal*(fd: INT): BOOL; begin return C.isatty(I32(fd)) = 1; end; @@ -839,7 +839,7 @@ end; (* Ends the program, at once, with status. *) -proc exit*(status: I64); +proc exit*(status: INT); begin C._exit(I32(status)); end; @@ -853,7 +853,7 @@ one is sent; and a program starts with them neither caught nor ignored. *) proc testPrograms; -var pty, child, n, m, r, w, sigs, c: I64; st: Status; buf: [64]U8; +var pty, child, n, m, r, w, sigs, c: INT; st: Status; buf: [64]U8; begin pty := openPty(); if (pty < 0) or (setTermSize(pty, 100, 30) # 0) then exit(2); end; @@ -923,7 +923,7 @@ sees; addresses read and refused; a TCP connection on the loopback; and a datagram to a port where no one listens, refused. *) proc testNet; -var t, l, c, a, u: I64; x, y: [16]U8; s, s6: Sockaddr; +var t: I64; l, c, a, u: INT; x, y: [16]U8; s, s6: Sockaddr; begin t := clock(); sleep(20); @@ -964,7 +964,7 @@ and closed; a directory made, listed and emptied; no terminal; a socket, sent to after its end; programs; time and the network. *) proc main*; -var fd, n, err, w, h, l, c, a: I64; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; +var fd, n, err, w, h, l, c, a: INT; buf: [16]U8; p: ^U8; i: Info; d: Dir; name: []U8; ok: BOOL; begin fd := create("/tmp/raz-sys-test"); if fd < 0 then exit(2); end; diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index 2b04f6c..a102d01 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -24,23 +24,23 @@ type Thread* = record - tid: I64; (* the kernel's id, until the thread ends *) + tid: INT; (* the kernel's id, until the thread ends *) base: ^U8; (* its stack's mapping, the guard page first *) - size: I64; + size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) arg: ^U8; end; (* What sigaltstack takes *) - AltStack = record sp: ^U8; flags, size: I64; end; + AltStack = record sp: ^U8; flags, size: INT; end; (* 0 free, 1 taken, 2 taken with a thread waiting *) - Mutex* = record state: I64; end; + Mutex* = record state: INT; end; (* clone(flags, stack, tid, tid), the new thread starting with fn(arg) on stack and ending, when fn returns, with the exit system call. fn and arg go on the new stack first, since the new thread has no registers but rsp of its own to find them by. Returns the new thread's id, or minus an errno. *) -proc start(flags: I64; stack: ^U8; ptid, ctid: ^I64; fn: proc(arg: ^U8); arg: ^U8): I64; +proc start(flags: INT; stack: ^U8; ptid, ctid: ^INT; fn: proc(arg: ^U8); arg: ^U8): INT; code 48H, 83H, 0EEH, 10H, (* sub rsi, 16 *) 4CH, 89H, 06H, (* mov [rsi], r8 *) 4CH, 89H, 4EH, 08H, (* mov [rsi + 8], r9 *) @@ -58,7 +58,7 @@ 0FH, 05H, (* syscall *) 0C3H; (* parent: ret *) -proc futex(word: ^I64; op, val: I64): I64; +proc futex(word: ^INT; op, val: INT): INT; begin return SYSCALL(202, word, op, val, 0); end; @@ -76,7 +76,7 @@ (* Starts p(arg) in a new thread. *) proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); -var base: ^U8; t: ^Thread; size: I64; +var base: ^U8; t: ^Thread; size: INT; begin size := Page + Stack + Alt + Page; base := Sys.mmap(size); @@ -103,7 +103,7 @@ (* Waits for t to end, then frees its stack. *) proc join*(t: ^Thread); -var tid: ^VOLATILE I64; v: I64; base: ^U8; size: I64; +var tid: ^VOLATILE INT; v: INT; base: ^U8; size: INT; begin tid := ADR(t^.tid); v := tid^; @@ -117,7 +117,7 @@ end; proc lock*(m: ^Mutex); -var c: I64; +var c: INT; begin if CAS(ADR(m^.state), 0, 1) then return; end; c := XCHG(ADR(m^.state), 2); @@ -145,7 +145,7 @@ Thread [n alive], Thread fail *) var testLock: Mutex; - testCount: I64; + testCount: INT; wave: [1000]^Thread; proc testWork(_arg: ^U8); @@ -155,18 +155,18 @@ unlock(ADR(testLock)); end; -proc testArg(i: I64): I64; -var s: ^U8; n, v: I64; +proc testArg(i: INT): INT; +var s: ^U8; n, v: INT; begin s := ARGV()[i]; v := 0; n := 0; - while s[n] # 0 do v := v * 10 + I64(s[n]) - '0'; INC(n); end; + while s[n] # 0 do v := v * 10 + INT(s[n]) - '0'; INC(n); end; return v; end; proc main*; -var n, alive, started, k, i: I64; t: ^Thread; err: ERROR; fail: BOOL; +var n, alive, started, k, i: INT; t: ^Thread; err: ERROR; fail: BOOL; begin n := 2000; alive := 50; fail := (ARGC() = 2) and (ARGV()[1][0] = 'f'); diff --git a/src/gen.pas b/src/gen.pas index f4d0cfe..1038c8f 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -44,6 +44,8 @@ vol: boolean; { mVar: reached through ^VOLATILE } isVar: boolean; { mVar: a variable of the program, not a temporary } odd: boolean; { mVar: in a packed record, so maybe misaligned } + ro: boolean; { mVar: another module's exported variable, or part + of one, which only that module changes } obj: PObj; { mProc, mBuiltin, mCase } pos: TPos; { where it is in the source, for messages } end; @@ -64,6 +66,8 @@ var rtFail, argcChunk, argvChunk: longint; depth: longint; { values pushed and not yet popped } + checks: boolean; { the module's index and slice checks are made: it + doesn't say check off } procedure MakeConst(var x: TItem; t: PType; v: int64); function What(const x: TItem): shortstring; @@ -76,6 +80,8 @@ procedure Address(var x: TItem; r: longint); procedure Save(var x: TItem); procedure Coerce(var x: TItem; t: PType); +procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); +procedure NotReadOnly(const x: TItem); procedure Assign(var dst, src: TItem); procedure CopyToTemp(var x: TItem); function ToSlot(var x: TItem): int64; @@ -92,6 +98,7 @@ procedure Complement(var x: TItem); procedure Op1(op: TSym; var x: TItem; var L: longint); procedure Op2(op: TSym; var x, y: TItem; L: longint); +function IsWord(t: PType): boolean; procedure Convert(var x: TItem; t: PType); procedure BeginCall(var c: TCall; var x: TItem); @@ -251,7 +258,8 @@ end; oVar: begin x.mode := mVar; - x.isVar := true; + x.ro := (o^.lev = 0) and (o^.module <> curModule); + x.isVar := not x.ro; if o^.ext then begin Load(RAX, MemRip(o^.chunk, 0), 8, false); x.base := bReg; x.reg := RAX; @@ -497,6 +505,20 @@ TempItem(x, t, s); end; +{ An INT goes silently into an I64, and a UINT into a U64, losing + nothing on any target (report section 5.10). } +function Widens(s, t: PType): boolean; +begin + Widens := ((s = tInt) and Same(t, tI64)) or ((s = tUInt) and Same(t, tU64)); +end; + +{ Another module's exported variable can be read, not changed, nor + handed to anything that could change it (report section 3). } +procedure NotReadOnly(const x: TItem); +begin + if x.ro then MarkAt(x.pos, Source(x.pos) + ' is ' + NameOf(x.obj^.module^.name) + '''s, and can''t be changed outside it'); +end; + procedure Coerce(var x: TItem; t: PType); var s: PType; tmp: int64; begin @@ -521,7 +543,8 @@ end; if x.mode in [mType, mBuiltin, mNone, mCase] then NotValue(x); if Same(s, t) then exit; - if (s = tRConst) and (t^.form = fReal) then RealTo(x, t) + if Widens(s, t) then x.typ := t { the same bits, on a 64-bit target } + else if (s = tRConst) and (t^.form = fReal) then RealTo(x, t) else if (s^.form = fConst) and (t^.form = fInt) then begin if (x.mode = mConst) and not Fits(x.val, t) then MarkAt(x.pos, IntStr(x.val) + ' doesn''t fit ' + TypeName(t)); x.typ := t; @@ -531,6 +554,7 @@ and Same(s^.base, t^.base) then x.typ := t else if (s^.form = fArray) and (t^.form = fSlice) and Same(s^.base, t^.base) then begin + NotReadOnly(x); if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); Address(x, RAX); @@ -539,6 +563,20 @@ MarkAt(x.pos, 'can''t use ' + TypeName(s) + ' as ' + TypeName(t)); end; +{ Coerce where the types must be the same, as in a for loop, INC and + several results, which don't widen an INT to an I64 (report sections + 5.10, 6.1 and 7.2); help, if not empty, says why. } +procedure CoerceExact(var x: TItem; t: PType; const help: shortstring); +var h: shortstring; +begin + if Widens(x.typ, t) then begin + h := help; + if h = '' then h := 'they don''t mix here: write ' + TypeName(t) + '(' + Source(x.pos) + ')'; + MarkHelp(x.pos, 'expected ' + TypeName(t) + ', found ' + What(x), h); + end; + Coerce(x, t); +end; + procedure Assign(var dst, src: TItem); var m: TMem; begin @@ -613,7 +651,7 @@ if t^.base = nil then MarkAt(x.pos, TypeName(t) + ' points to a type never declared'); Value(x); x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; - x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; + x.typ := t^.base; x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; end; { A field of a packed record, of more than one byte's alignment, may @@ -654,7 +692,7 @@ PopR(RCX); Alu(aluAdd, RAX, RCX); x.mode := mVar; x.base := bReg; x.reg := RAX; x.off := 0; - x.vol := t^.vol; x.isVar := true; x.odd := false; + x.vol := t^.vol; x.isVar := true; x.odd := false; x.ro := false; end else if (t^.form = fArray) and (y.mode = mConst) then begin if (y.val < 0) or (y.val >= t^.len) then MarkAt(y.pos, 'index ' + IntStr(y.val) + ' is out of range for a length of ' + IntStr(t^.len)); x.off := x.off + y.val * s; @@ -663,8 +701,10 @@ end; end else if t^.form = fArray then begin Value(y); - CmpI(RAX, t^.len); - Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1); + if checks then begin + CmpI(RAX, t^.len); + Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1); + end; if s <> 1 then ImulI(RAX, s); if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX)); Alu(aluAdd, RAX, RCX); @@ -674,11 +714,13 @@ Value(y); m := MemOf(x, RCX); ml := m; ml.disp := ml.disp + 8; - AluM(aluCmp, RAX, ml); - Check(ccB, 0, y.pos, RAX, ml, 0, -1); + if checks then begin + AluM(aluCmp, RAX, ml); + Check(ccB, 0, y.pos, RAX, ml, 0, -1); + end; if s <> 1 then ImulI(RAX, s); AluM(aluAdd, RAX, m); - x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; + x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; x.ro := false; end; x.typ := t^.base; end; @@ -698,22 +740,27 @@ if not IsInt(hi.typ) then MarkAt(hi.pos, 'an index is an integer, found ' + What(hi)); if x.vol then MarkAt(x.pos, 'can''t make a slice through a volatile pointer'); if x.odd then MarkAt(x.pos, 'can''t make a slice of a field of a packed record'); + if t^.form = fArray then NotReadOnly(x); Value(hi); MovRR(RDX, RAX); PopR(RCX); if t^.form = fPtr then begin { the bounds can't be known, but a slice can't run backwards } PopR(R8); - Alu(aluCmp, RCX, RDX); - Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1); + if checks then begin + Alu(aluCmp, RCX, RDX); + Check(ccLE, 3, lo.pos, RCX, NoMem, 0, -1); + end; end else begin m := MemOf(x, R8); ml := m; ml.disp := ml.disp + 8; - if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end - else AluM(aluCmp, RDX, ml); - Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); - Alu(aluCmp, RCX, RDX); - Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); + if checks then begin + if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end + else AluM(aluCmp, RDX, ml); + Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); + Alu(aluCmp, RCX, RDX); + Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); + end; end; tmp := AllocTemp(16, 8); MovRR(RAX, RDX); @@ -853,6 +900,12 @@ end; end; +{ A type's width in bits; an untyped constant's is an I64's } +function Bits(t: PType): longint; +begin + Bits := t^.size * 8; +end; + function FoldInt(op: TSym; a, b: int64; t: PType; const p: TPos): int64; var u: boolean; begin @@ -867,16 +920,16 @@ if u then begin if op = sDiv then FoldInt := int64(qword(a) div qword(b)) else FoldInt := int64(qword(a) mod qword(b)); - end else if (b = -1) and (a = low(int64)) then MarkAt(p, 'division overflows') + end else if (b = -1) and (a = int64(-1) shl (Bits(t) - 1)) then MarkAt(p, 'division overflows') else if op = sDiv then FoldInt := a div b else FoldInt := a mod b; end; sAmp: FoldInt := a and b; sBar: FoldInt := a or b; sXor: FoldInt := a xor b; - sShl: FoldInt := a shl (b and 63); - sShr: if u then FoldInt := int64(qword(a) shr (b and 63)) - else FoldInt := SarInt64(a, b and 63); + sShl: FoldInt := a shl (b and (Bits(t) - 1)); + sShr: if u then FoldInt := int64(qword(a) shr (b and (Bits(t) - 1))) + else FoldInt := SarInt64(a, b and (Bits(t) - 1)); sEql: FoldInt := ord(a = b); sNeq: FoldInt := ord(a <> b); sLss: if u then FoldInt := ord(qword(a) < qword(b)) else FoldInt := ord(a < b); @@ -913,6 +966,22 @@ { and, or and not are for BOOLs; &, |, xor and ~ for the bits of integers: x is the wrong kind for op, and the message names the operator it may have meant } +{ Before rax div rcx, of a signed type narrower than 64 bits: its + smallest value divided by -1 overflows it, which must stop the program + as it does for an I64 (report section 6.1). idiv on the 64-bit values + wouldn't fault, so then rax is made the smallest I64, and it does. } +procedure DivTrap(t: PType); +var L: longint; +begin + L := NoLabel; + CmpI(RCX, -1); + Jcc(ccNE, L); + CmpI(RAX, int64(-1) shl (Bits(t) - 1)); + Jcc(ccNE, L); + ShiftI(shShl, RAX, 64 - Bits(t)); + FixLink(L); +end; + procedure WrongLogic(op: TSym; var x: TItem); const Bits = 'the bits of integers take '; begin @@ -1013,10 +1082,10 @@ t := tBool; end else if (xt^.form = fPtr) and IsInt(yt) and (op in [sPlus, sMinus]) then begin t := xt; scale := xt^.base^.size; isPtr := true; - if y.mode = mConst then y.typ := tI64; + if y.mode = mConst then y.typ := tInt; end else if (xt^.form = fPtr) and (yt^.form = fPtr) and (op = sMinus) then begin if not Same(xt, yt) then Mismatch(x, y, xt, yt); - t := tI64; scale := xt^.base^.size; isPtr := true; + t := tInt; scale := xt^.base^.size; isPtr := true; end else if (xt^.form in [fPtr, fProc, fNil, fError]) and (yt^.form in [fPtr, fProc, fNil, fError]) then begin if not (op in Rel) then MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(xt)); if (xt^.form in [fProc, fError]) or (yt^.form in [fProc, fError]) then @@ -1122,9 +1191,9 @@ case op of sPlus: begin if scale <> 1 then ImulI(RCX, scale); Alu(aluAdd, RAX, RCX) end; sMinus: begin - if (scale <> 1) and (t <> tI64) then ImulI(RCX, scale); + if (scale <> 1) and (t <> tInt) then ImulI(RCX, scale); Alu(aluSub, RAX, RCX); - if (t = tI64) and isPtr and (scale <> 1) then begin + if (t = tInt) and isPtr and (scale <> 1) then begin sh := Log2(scale); if sh >= 0 then ShiftI(shSar, RAX, sh) else begin MovRI(RCX, scale); Cqo; Unary(7, RCX) end; @@ -1133,14 +1202,21 @@ sTimes: if k >= 1 then ShiftI(shShl, RAX, k) else Imul(RAX, RCX); sDiv, sMod: if k >= 1 then DivPow2(op, k, unsign) else begin if unsign then begin MovRI(RDX, 0); Unary(6, RCX) end - else begin Cqo; Unary(7, RCX) end; + else begin + if (t^.size < 8) and ((y.mode <> mConst) or (y.val = -1)) then DivTrap(t); + Cqo; Unary(7, RCX) + end; if op = sMod then MovRR(RAX, RDX); end; sAmp: Alu(aluAnd, RAX, RCX); sBar: Alu(aluOr, RAX, RCX); sXor: Alu(aluXor, RAX, RCX); - sShl: Shift(shShl, RAX); - sShr: if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); + sShl, sShr: begin + { the count modulo the width: the machine takes it modulo 64 } + if Bits(t) < 64 then AluI(aluAnd, RCX, Bits(t) - 1); + if op = sShl then Shift(shShl, RAX) + else if unsign then Shift(shShr, RAX) else Shift(shSar, RAX); + end; else MarkAt(Join(x.pos, y.pos), SymText(op) + ' doesn''t apply to ' + TypeName(t)); end; Norm(t); @@ -1149,6 +1225,13 @@ if op in Rel + [sIn] then x.typ := tBool else x.typ := t; end; +{ INT and UINT, the integers as big as an address, through which + integers and pointers convert (report section 6.7) } +function IsWord(t: PType): boolean; +begin + IsWord := Same(t, tInt) or Same(t, tUInt); +end; + procedure Convert(var x: TItem; t: PType); var s: PType; r: double; L1, L2: longint; f: boolean; begin @@ -1213,11 +1296,11 @@ end else begin Value(x); MovqXR(0, RAX); Cvtts2si(RAX, 0, IsF32(s)); Norm(t) end; - end else if (IsInt(s) or (s^.form in [fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin + end else if (IsWord(s) or (s^.form in [fConst, fPtr, fProc, fNil])) and (t^.form in [fPtr, fProc]) then begin if x.mode <> mConst then Value(x); - end else if (s^.form in [fPtr, fProc]) and (t^.form = fInt) then begin - Value(x); Norm(t) - end else + end else if (s^.form in [fPtr, fProc]) and IsWord(t) then + Value(x) + else MarkAt(x.pos, 'can''t convert ' + TypeName(s) + ' to ' + TypeName(t)); x.typ := t; end; @@ -1255,11 +1338,11 @@ if c.n > High(c.slots) then MarkAt(a.pos, 'too many arguments'); if p = nil then begin { past the parameters of a C procedure that ends in ...: a string - goes as a C string, an integer constant as an I64, a real + goes as a C string, an integer constant as an INT, a real constant as an F64; C takes no F32 there } if not c.sig^.variadic then WrongCount(c, a.pos, 'too many arguments'); if a.mode = mStr then Coerce(a, tPtrU8) - else if a.typ^.form = fConst then Coerce(a, tI64) + else if a.typ^.form = fConst then Coerce(a, tInt) else if a.typ = tRConst then Coerce(a, tReal) else if IsF32(a.typ) then MarkAt(a.pos, 'an F32 goes to ... as F64(x)') else if not IsScalar(a.typ) then MarkAt(a.pos, 'only a single value goes to ...'); @@ -1270,6 +1353,7 @@ end; c.isF[c.n] := (p^.typ^.form = fReal) and not p^.isVar; if p^.isVar then begin + NotReadOnly(a); if (a.mode <> mVar) or not a.isVar then MarkAt(a.pos, 'a var parameter needs a variable'); if a.odd then MarkAt(a.pos, 'a field of a packed record can''t be a var parameter'); if not Same(a.typ, p^.typ) then MarkAt(a.pos, 'a var parameter needs exactly ' + TypeName(p^.typ) + ', found ' + What(a)); @@ -1393,6 +1477,7 @@ begin if x.typ^.form = fEnum then exit(ToSlot(x)); if (x.mode <> mVar) or (x.typ^.form <> fVariant) then MarkAt(x.pos, 'match needs a variant or an enumeration, found ' + What(x)); + if x.ro then CopyToTemp(x); { its arms' names stand for a copy's fields } Address(x, RAX); p := AllocTemp(8, 8); Store(Mem(RBP, p), RAX, 8); @@ -1571,7 +1656,7 @@ procedure IncDec(var v, k: TItem; dec: boolean); var n: longint; begin - Coerce(k, v.typ); + CoerceExact(k, v.typ, ''); Value(k); MovRR(RCX, RAX); Address(v, RDX); diff --git a/src/parse.pas b/src/parse.pas index 6347aef..44375af 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -76,7 +76,7 @@ curResult: PType; { the result type of the procedure being compiled } inExtern: boolean; { reading an extern's signature: ... allowed } tArgv, tBodySig: PType; - nameRt, nameFail, nameMain, nameBody, nameStatic: longint; + nameRt, nameFail, nameMain, nameBody, nameStatic, nameCheck, nameOff: longint; declaring: array[0..63] of longint; { the names the declaration being read declares } numDeclaring: longint; skipped: boolean; { the last statement had an error and was skipped } @@ -667,7 +667,7 @@ if i > 0 then Expect(sComma); Expression(a, nil); if not (IsScalar(a.typ) and (a.typ^.form <> fReal)) then MarkAt(a.pos, 'expected an integer or a pointer, found ' + What(a)); - if a.typ^.form = fConst then Coerce(a, tI64); + if a.typ^.form = fConst then Coerce(a, tInt); slots[i] := ToSlot(a); end; end; @@ -678,8 +678,9 @@ Expression(p, nil); if p.typ^.form <> fPtr then MarkAt(p.pos, 'expected a pointer, found ' + What(p)); t := p.typ^.base; - if not (((t^.form = fInt) or ((t^.form = fPtr) and (which <> bXadd))) and (t^.size = 8)) then - MarkAt(p.pos, 'expected an 8-byte integer or a pointer, found ' + TypeName(t)); + if not (IsWord(t) or ((t^.form = fPtr) and (which <> bXadd))) then + if which = bXadd then MarkAt(p.pos, 'expected a pointer to an INT or a UINT, found ' + TypeName(p.typ)) + else MarkAt(p.pos, 'expected a pointer to an INT, a UINT or a pointer, found ' + TypeName(p.typ)); s[0] := ToSlot(p); if which = bCas then n := 2 else n := 1; for i := 1 to n do begin @@ -710,6 +711,7 @@ var y, z: TItem; t: PType; f: PObj; s: int64; c: TCall; L: longint; begin Expression(x, nil); + NotReadOnly(x); if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fPtr) then MarkAt(x.pos, 'NEW needs a pointer variable'); t := x.typ^.base; Save(x); @@ -728,9 +730,9 @@ MakeConst(z, y.typ, 0); z.mode := mReg; PassArg(c, z); - MakeConst(z, tI64, t^.size); + MakeConst(z, tInt, t^.size); PassArg(c, z); - MakeConst(z, tI64, t^.align); + MakeConst(z, tInt, t^.align); PassArg(c, z); EndCall(c, z); L := NoLabel; @@ -764,7 +766,7 @@ MakeConst(x, tNone, 0); case which of bFence: begin Mfence; x.mode := mNone end; - bArgc: begin Load(RAX, MemRip(argcChunk, 0), 8, false); x.typ := tI64; x.mode := mReg end; + bArgc: begin Load(RAX, MemRip(argcChunk, 0), 8, false); x.typ := tInt; x.mode := mReg end; bArgv: begin Load(RAX, MemRip(argvChunk, 0), 8, false); x.typ := tArgv; x.mode := mReg end; end; exit; @@ -777,6 +779,7 @@ end; bAdr: begin Expression(y, nil); + NotReadOnly(y); if (y.mode <> mVar) or not y.isVar then MarkAt(y.pos, 'ADR needs a variable'); if y.odd then MarkAt(y.pos, 'a field of a packed record has no address'); t := NewPtr(y.typ, y.vol); @@ -791,7 +794,7 @@ else if (y.typ^.form = fSlice) and (y.mode = mVar) then begin Address(y, RAX); Load(RAX, Mem(RAX, 8), 8, false); - MakeConst(x, tI64, 0); + MakeConst(x, tInt, 0); x.mode := mReg; end else MarkAt(y.pos, 'LEN needs an array or a slice, found ' + What(y)); end; @@ -802,19 +805,20 @@ Next; Expression(y, nil); if not (IsScalar(y.typ) and (y.typ^.form <> fReal)) then MarkAt(y.pos, 'expected an integer or a pointer, found ' + What(y)); - if y.typ^.form = fConst then Coerce(y, tI64); + if y.typ^.form = fConst then Coerce(y, tInt); slots[n] := ToSlot(y); inc(n); end; for i := 0 to n - 1 do Load(SysRegs[i], Mem(RBP, slots[i]), 8, false); Syscall; - MakeConst(x, tI64, 0); + MakeConst(x, tInt, 0); x.mode := mReg; end; bCas, bXchg, bXadd: Atomic(x, which); bMsg: begin Expression(x, tError); Msg(x) end; bInc, bDec: begin Expression(x, nil); + NotReadOnly(x); if (x.mode <> mVar) or not x.isVar or (x.typ^.form <> fInt) then MarkAt(x.pos, IncOrDec(which) + ' needs an integer variable'); Save(x); @@ -1261,10 +1265,11 @@ MakeItem(v, o); Save(v); Expression(a, o^.typ); + CoerceExact(a, o^.typ, ''); Assign(v, a); Expect(sTo); Expression(b, o^.typ); - Coerce(b, o^.typ); + CoerceExact(b, o^.typ, ''); bslot := ToSlot(b); step := 1; if tok = sBy then begin @@ -1397,6 +1402,7 @@ d[0] := x; dn[0] := first; repeat + NotReadOnly(d[n]); if (d[n].mode <> mVar) or not d[n].isVar then MarkAt(d[n].pos, 'can''t assign to ' + Subject(d[n], dn[n])); Pin(d[n]); inc(n); @@ -1414,6 +1420,7 @@ for i := 0 to n - 1 do begin z := y; Field(z, f); + CoerceExact(z, d[i].typ, 'several results go to variables of just their types'); Assign(d[i], z); f := f^.next; end; @@ -1477,6 +1484,7 @@ sIdent: begin LeftSide(x, called); if tok = sBecomes then begin + NotReadOnly(x); if (x.mode <> mVar) or not x.isVar then MarkAt(x.pos, 'can''t assign to ' + Subject(x, first)); Save(x); Next; @@ -2078,13 +2086,14 @@ function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; var st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; - oldMod: PModule; oldCheck: boolean; m: PModule; + oldMod: PModule; oldCheck, oldChecks: boolean; m: PModule; lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; generic: boolean; argsAt: TScanState; begin SaveScan(st); SaveScopes(d, saved); - oldMod := curModule; oldCheck := checkMode; oldPath := curFile; + oldMod := curModule; oldCheck := checkMode; oldPath := curFile; oldChecks := checks; + checks := true; curFile := Name(path); if not Open(path) then Fatal('can''t read ' + path); New(m); @@ -2167,6 +2176,15 @@ Next; Expect(sSemicolon); end; + { check off;: the module's index and slice checks left out; neither + word is reserved } + if (tok = sIdent) and (id = nameCheck) then begin + Next; + if (tok <> sIdent) or (id <> nameOff) then Expected('"off"'); + Next; + Expect(sSemicolon); + checks := false; + end; Declarations(true); if tok = sBegin then begin if checkMode then SkipBody @@ -2203,7 +2221,7 @@ if lastModule = nil then firstModule := m else lastModule^.next := m; lastModule := m; end; - curModule := oldMod; checkMode := oldCheck; curFile := oldPath; + curModule := oldMod; checkMode := oldCheck; curFile := oldPath; checks := oldChecks; RestoreScopes(d, saved); RestoreScan(st); CompileFile := m; @@ -2430,6 +2448,8 @@ nameMain := Name('main'); nameBody := Name('body'); nameStatic := Name('static'); + nameCheck := Name('check'); + nameOff := Name('off'); rtFail := NewChunk(secText, nameRt, nameFail); argcChunk := NewChunk(secBss, -1, Name('argc')); SetBss(argcChunk, 8, 8); diff --git a/src/raz.pas b/src/raz.pas index 8cec050..21b938c 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -153,8 +153,8 @@ hasResult := false; if main <> nil then begin if (main^.cls <> oProc) or not main^.exported then Error(main^.pos, 'main must be an exported procedure') - else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tI64)) then - Error(main^.pos, 'main must be proc main* or proc main*: I64'); + else if (main^.typ^.len <> 0) or ((main^.typ^.base <> nil) and (main^.typ^.base <> tInt)) then + Error(main^.pos, 'main must be proc main* or proc main*: INT'); if errors > 0 then Finish; hasResult := main^.typ^.base <> nil; end; diff --git a/src/sym.pas b/src/sym.pas index d790247..b8a2121 100644 --- a/src/sym.pas +++ b/src/sym.pas @@ -129,6 +129,7 @@ var tI8, tI16, tI32, tI64, tU8, tU16, tU32, tU64: PType; + tInt, tUInt: PType; { INT and UINT, as big as an address: 8 bytes here } tBool, tF32, tReal, tRConst, tError, tConst, tNil, tStr, tNone, tPtrU8, tBytes: PType; tPoison: PType; { of a name reported as not declared } curModule: PModule; @@ -557,6 +558,8 @@ tU16 := Basic('U16', fInt, 2, false); tU32 := Basic('U32', fInt, 4, false); tU64 := Basic('U64', fInt, 8, false); + tInt := Basic('INT', fInt, 8, true); + tUInt := Basic('UINT', fInt, 8, false); tBool := Basic('BOOL', fBool, 1, false); tF32 := Basic('F32', fReal, 4, false); tReal := Basic('F64', fReal, 8, false); diff --git a/test/doc/Opt.expect b/test/doc/Opt.expect index de2b53e..fe4ef02 100644 --- a/test/doc/Opt.expect +++ b/test/doc/Opt.expect @@ -24,7 +24,7 @@ name: []U8; (* the program's, as the messages begin *) table: []Row; scope: []U8; - at: I64; (* the next word *) + at: INT; (* the next word *) group: []U8; (* the letters left of a word of them, as -lw *) spelt: []U8; (* the option just read as written, without its dashes *) ended: BOOL; (* the scope's options have ended *) diff --git a/test/doc/Str.expect b/test/doc/Str.expect index b26d269..ad54ec8 100644 --- a/test/doc/Str.expect +++ b/test/doc/Str.expect @@ -3,7 +3,7 @@ module orders, searches and hashes them, reads and writes their UTF-8, and reads a number from one. -proc cmp*(a, b: []U8): I64; +proc cmp*(a, b: []U8): INT; -1, 0 or 1 as a sorts before, with or after b, byte by byte. proc eq*(a, b: []U8): BOOL; @@ -11,17 +11,17 @@ proc hash*(s: []U8): U64; FNV-1a, 64 bits. -proc find*(s, sub: []U8): I64; +proc find*(s, sub: []U8): INT; Where sub first starts in s, or -1. proc hasPrefix*(s, p: []U8): BOOL; -proc encode*(c: I64): ([4]U8, I64); +proc encode*(c: I32): ([4]U8, INT); c in UTF-8: its bytes, and how many. A number that isn't a character, a surrogate or one past 10FFFFH, becomes FFFDH, the character that stands for a wrong one. -proc decode*(s: []U8): (I64, I64); +proc decode*(s: []U8): (I32, INT); The character s starts with, and its length in bytes: FFFDH and 1 if its first byte doesn't start one rightly, being a continuation, or followed by too few continuations, or spelling a surrogate or a diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index 4fd5dc1..fdae2cb 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -11,14 +11,14 @@ const Failed*: ERROR = "can't start a thread"; type Thread* = record - tid: I64; (* the kernel's id, until the thread ends *) + tid: INT; (* the kernel's id, until the thread ends *) base: ^U8; (* its stack's mapping, the guard page first *) - size: I64; + size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) arg: ^U8; end; -type Mutex* = record state: I64; end; +type Mutex* = record state: INT; end; 0 free, 1 taken, 2 taken with a thread waiting proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); diff --git a/test/err/DeclAtomicSize.err b/test/err/DeclAtomicSize.err index 5e74c6f..e57a298 100644 --- a/test/err/DeclAtomicSize.err +++ b/test/err/DeclAtomicSize.err @@ -1,4 +1,4 @@ -test/err/DeclAtomicSize.raz:8:15: error: expected an 8-byte integer or a pointer, found U8 +test/err/DeclAtomicSize.raz:8:15: error: expected a pointer to an INT or a UINT, found ^U8 8 | n := XADD(p, 1); | ^ raz: 1 error diff --git a/test/err/DivLowestConst.err b/test/err/DivLowestConst.err new file mode 100644 index 0000000..bc2229e --- /dev/null +++ b/test/err/DivLowestConst.err @@ -0,0 +1,4 @@ +test/err/DivLowestConst.raz:5:11: error: division overflows + 5 | _x := I8(-128) div (-1); + | ^^^^^^^^^^^^^^^^^ +raz: 1 error diff --git a/test/err/DivLowestConst.raz b/test/err/DivLowestConst.raz new file mode 100644 index 0000000..01676c4 --- /dev/null +++ b/test/err/DivLowestConst.raz @@ -0,0 +1,7 @@ +module DivLowestConst; +proc main*; +var _x: I8; +begin + _x := I8(-128) div (-1); +end; +end DivLowestConst. diff --git a/test/genbench.py b/test/genbench.py index 39f52da..de43072 100644 --- a/test/genbench.py +++ b/test/genbench.py @@ -8,22 +8,22 @@ import Out; type - Pair = record x, y: I64; end; - Shape = variant circle(r: I64); rect(w, h: I64); dot; end; + Pair = record x, y: INT; end; + Shape = variant circle(r: INT); rect(w, h: INT); dot; end; var - g: [16]I64; - data: [64]I64; + g: [16]INT; + data: [64]INT; """) -print("""proc p0(a, b: I64; _s: []I64): I64; +print("""proc p0(a, b: INT; _s: []INT): INT; begin return a + b; end; """) for i in range(1, n + 1): print(f"""(* procedure {i} *) -proc p{i}(a, b: I64; s: []I64): I64; -var i, t: I64; r: Pair; sh: Shape; +proc p{i}(a, b: INT; s: []INT): INT; +var i, t: INT; r: Pair; sh: Shape; begin t := 0; for i := 0 to LEN(s) - 1 do @@ -51,7 +51,7 @@ end; """) print(f"""proc main*; -var i: I64; +var i: INT; begin for i := 0 to 63 do data[i] := i; end; Out.int(p{n}(1, 2, data[0..4])); diff --git a/test/msg/AtomicI64.err b/test/msg/AtomicI64.err new file mode 100644 index 0000000..29d2507 --- /dev/null +++ b/test/msg/AtomicI64.err @@ -0,0 +1,4 @@ +conform/err/AtomicI64.raz:5:16: error: expected a pointer to an INT, a UINT or a pointer, found ^I64 + 5 | _ok := CAS(ADR(n), 0, 1); + | ^^^^^^ +raz: 1 error diff --git a/test/msg/ExportAdr.err b/test/msg/ExportAdr.err new file mode 100644 index 0000000..74da26a --- /dev/null +++ b/test/msg/ExportAdr.err @@ -0,0 +1,4 @@ +conform/err/ExportAdr.raz:6:15: error: Owner.n is Owner's, and can't be changed outside it + 6 | _p := ADR(Owner.n); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportAsSlice.err b/test/msg/ExportAsSlice.err new file mode 100644 index 0000000..64152e7 --- /dev/null +++ b/test/msg/ExportAsSlice.err @@ -0,0 +1,4 @@ +conform/err/ExportAsSlice.raz:6:9: error: Owner.a is Owner's, and can't be changed outside it + 6 | sum(Owner.a); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportAssign.err b/test/msg/ExportAssign.err new file mode 100644 index 0000000..f97a418 --- /dev/null +++ b/test/msg/ExportAssign.err @@ -0,0 +1,4 @@ +conform/err/ExportAssign.raz:5:5: error: Owner.n is Owner's, and can't be changed outside it + 5 | Owner.n := 1; + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportElem.err b/test/msg/ExportElem.err new file mode 100644 index 0000000..f5b5365 --- /dev/null +++ b/test/msg/ExportElem.err @@ -0,0 +1,4 @@ +conform/err/ExportElem.raz:5:5: error: Owner.a[2] is Owner's, and can't be changed outside it + 5 | Owner.a[2] := 1; + | ^^^^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportInc.err b/test/msg/ExportInc.err new file mode 100644 index 0000000..ce7be35 --- /dev/null +++ b/test/msg/ExportInc.err @@ -0,0 +1,4 @@ +conform/err/ExportInc.raz:5:9: error: Owner.n is Owner's, and can't be changed outside it + 5 | INC(Owner.n); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportSlice.err b/test/msg/ExportSlice.err new file mode 100644 index 0000000..56fe0ff --- /dev/null +++ b/test/msg/ExportSlice.err @@ -0,0 +1,4 @@ +conform/err/ExportSlice.raz:6:11: error: Owner.a is Owner's, and can't be changed outside it + 6 | _s := Owner.a[0..2]; + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/ExportVarParam.err b/test/msg/ExportVarParam.err new file mode 100644 index 0000000..b823066 --- /dev/null +++ b/test/msg/ExportVarParam.err @@ -0,0 +1,4 @@ +conform/err/ExportVarParam.raz:6:9: error: Owner.n is Owner's, and can't be changed outside it + 6 | put(Owner.n); + | ^^^^^^^ +raz: 1 error diff --git a/test/msg/IntFor.err b/test/msg/IntFor.err new file mode 100644 index 0000000..db68f7f --- /dev/null +++ b/test/msg/IntFor.err @@ -0,0 +1,5 @@ +conform/err/IntFor.raz:5:19: error: expected I64, found INT + 5 | for i := 0 to LEN(s) - 1 do end; + | ^^^^^^^^^^ +help: they don't mix here: write I64(LEN(s) - 1) +raz: 1 error diff --git a/test/msg/IntFromPointer.err b/test/msg/IntFromPointer.err new file mode 100644 index 0000000..53f012d --- /dev/null +++ b/test/msg/IntFromPointer.err @@ -0,0 +1,4 @@ +conform/err/IntFromPointer.raz:5:14: error: can't convert ^U8 to I64 + 5 | i := I64(p); + | ^ +raz: 1 error diff --git a/test/msg/IntInc.err b/test/msg/IntInc.err new file mode 100644 index 0000000..241bf3b --- /dev/null +++ b/test/msg/IntInc.err @@ -0,0 +1,5 @@ +conform/err/IntInc.raz:5:12: error: expected I64, found INT + 5 | INC(i, LEN(s)); + | ^^^^^^ +help: they don't mix here: write I64(LEN(s)) +raz: 1 error diff --git a/test/msg/IntMix.err b/test/msg/IntMix.err new file mode 100644 index 0000000..a26e314 --- /dev/null +++ b/test/msg/IntMix.err @@ -0,0 +1,5 @@ +conform/err/IntMix.raz:5:8: error: I64 and INT are different types + 5 | if i < LEN(s) then end; + | ^^^^^^^^^^ +help: convert one to the other's type, as in I64(LEN(s)) +raz: 1 error diff --git a/test/msg/IntNarrow.err b/test/msg/IntNarrow.err new file mode 100644 index 0000000..1111982 --- /dev/null +++ b/test/msg/IntNarrow.err @@ -0,0 +1,4 @@ +conform/err/IntNarrow.raz:5:10: error: can't use I64 as INT + 5 | n := i; + | ^ +raz: 1 error diff --git a/test/msg/IntPointer.err b/test/msg/IntPointer.err new file mode 100644 index 0000000..2e4d46b --- /dev/null +++ b/test/msg/IntPointer.err @@ -0,0 +1,4 @@ +conform/err/IntPointer.raz:5:14: error: can't convert I64 to ^U8 + 5 | p := ^U8(i); + | ^ +raz: 1 error diff --git a/test/msg/IntResults.err b/test/msg/IntResults.err new file mode 100644 index 0000000..617eb63 --- /dev/null +++ b/test/msg/IntResults.err @@ -0,0 +1,5 @@ +conform/err/IntResults.raz:6:13: error: expected I64, found INT + 6 | a, b := two(); + | ^^^^^ +help: several results go to variables of just their types +raz: 1 error diff --git a/test/msg/ScanMain.err b/test/msg/ScanMain.err index 5222a9d..2dd31a7 100644 --- a/test/msg/ScanMain.err +++ b/test/msg/ScanMain.err @@ -1,4 +1,4 @@ -conform/err/ScanMain.raz:2:6: error: main must be proc main* or proc main*: I64 +conform/err/ScanMain.raz:2:6: error: main must be proc main* or proc main*: INT 2 | proc main*(_n: I64); | ^^^^ raz: 1 error diff --git a/test/run/Atomic.raz b/test/run/Atomic.raz index 290df92..ccd5314 100644 --- a/test/run/Atomic.raz +++ b/test/run/Atomic.raz @@ -1,7 +1,7 @@ module Atomic; import Out; -var counter: I64; flag: U64; p: ^I64; +var counter: INT; flag: UINT; p: ^INT; proc add3*(a, b, c: I64): I64; code 48H, 8DH, 04H, 37H, (* lea rax, [rdi+rsi] *) @@ -9,7 +9,7 @@ 0C3H; (* ret *) proc main*; -var old: I64; ok: BOOL; v: ^VOLATILE I64; x: I64; +var old: INT; ok: BOOL; v: ^VOLATILE INT; x: INT; begin old := XADD(ADR(counter), 5); old := XADD(ADR(counter), 2); @@ -28,7 +28,7 @@ v^ := 5; v^ := v^ + 1; Out.int(x); Out.ln; - v := ^VOLATILE I64(ADR(counter)); + v := ^VOLATILE INT(ADR(counter)); Out.int(v^); Out.ln; end; diff --git a/test/run/CheckOff.expect b/test/run/CheckOff.expect new file mode 100644 index 0000000..806de03 --- /dev/null +++ b/test/run/CheckOff.expect @@ -0,0 +1 @@ +42 4 diff --git a/test/run/CheckOff.raz b/test/run/CheckOff.raz new file mode 100644 index 0000000..08001c8 --- /dev/null +++ b/test/run/CheckOff.raz @@ -0,0 +1,18 @@ +module CheckOff; +(* check off leaves out the checks of its own module only: here + Unchecked reads past an array, into the field after it, and makes a + slice past its string's end; then this module's own index fails *) +import Out, Unchecked; + +proc main*; +var p: Unchecked.Pair; i: INT; +begin + p.b := 42; + Out.int(Unchecked.get(ADR(p), 2)); Out.str(" "); + Out.int(Unchecked.width("abc", 1, 5)); Out.ln; + Out.flush; + i := 2; + Out.int(p.a[i]); +end; + +end CheckOff. diff --git a/test/run/CheckOff.status b/test/run/CheckOff.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/CheckOff.status @@ -0,0 +1 @@ +2 diff --git a/test/run/CheckOff.stderr b/test/run/CheckOff.stderr new file mode 100644 index 0000000..9de665b --- /dev/null +++ b/test/run/CheckOff.stderr @@ -0,0 +1 @@ +test/run/CheckOff.raz:15:17: index 2 is out of range for a length of 2 diff --git a/test/run/Data.raz b/test/run/Data.raz index 2eb31b4..34c1e56 100644 --- a/test/run/Data.raz +++ b/test/run/Data.raz @@ -66,7 +66,7 @@ m.a := 1; m.b := -2; m.c := 65535; Out.int(m.b); Out.str(" "); Out.int(I64(m.c)); Out.ln; bp := ^U8(ADR(m)); - Out.int(I64(ADR(m.c)) - I64(bp)); Out.ln; + Out.int(INT(ADR(m.c)) - INT(bp)); Out.ln; end; end Data. diff --git a/test/run/DivLowestI16.expect b/test/run/DivLowestI16.expect new file mode 100644 index 0000000..0605667 --- /dev/null +++ b/test/run/DivLowestI16.expect @@ -0,0 +1 @@ +-2 \ No newline at end of file diff --git a/test/run/DivLowestI16.raz b/test/run/DivLowestI16.raz new file mode 100644 index 0000000..71f35dc --- /dev/null +++ b/test/run/DivLowestI16.raz @@ -0,0 +1,15 @@ +module DivLowestI16; +(* mod too: the lowest I16 mod -1 overflows the division as div does *) +import Out; + +proc main*; +var a, b: I16; +begin + a := -32768; + b := I16(ARGC() - 2); + Out.int(I64(a mod 3)); + Out.flush; + Out.int(I64(a mod b)); +end; + +end DivLowestI16. diff --git a/test/run/DivLowestI16.status b/test/run/DivLowestI16.status new file mode 100644 index 0000000..7296f25 --- /dev/null +++ b/test/run/DivLowestI16.status @@ -0,0 +1 @@ +136 diff --git a/test/run/DivLowestI16.stderr b/test/run/DivLowestI16.stderr new file mode 100644 index 0000000..3044000 --- /dev/null +++ b/test/run/DivLowestI16.stderr @@ -0,0 +1 @@ +test/run/DivLowestI16.raz:12:5: stopped by a division fault diff --git a/test/run/DivLowestI32.expect b/test/run/DivLowestI32.expect new file mode 100644 index 0000000..15aff61 --- /dev/null +++ b/test/run/DivLowestI32.expect @@ -0,0 +1 @@ +-1073741824 \ No newline at end of file diff --git a/test/run/DivLowestI32.raz b/test/run/DivLowestI32.raz new file mode 100644 index 0000000..8237684 --- /dev/null +++ b/test/run/DivLowestI32.raz @@ -0,0 +1,14 @@ +module DivLowestI32; +(* The lowest I32 divided by the constant -1 *) +import Out; + +proc main*; +var a: I32; +begin + a := I32(ARGC()) - 2147483647 - 2; + Out.int(I64(a div 2)); + Out.flush; + Out.int(I64(a div (-1))); +end; + +end DivLowestI32. diff --git a/test/run/DivLowestI32.status b/test/run/DivLowestI32.status new file mode 100644 index 0000000..7296f25 --- /dev/null +++ b/test/run/DivLowestI32.status @@ -0,0 +1 @@ +136 diff --git a/test/run/DivLowestI32.stderr b/test/run/DivLowestI32.stderr new file mode 100644 index 0000000..11ed03d --- /dev/null +++ b/test/run/DivLowestI32.stderr @@ -0,0 +1 @@ +test/run/DivLowestI32.raz:11:5: stopped by a division fault diff --git a/test/run/DivLowestI8.expect b/test/run/DivLowestI8.expect new file mode 100644 index 0000000..4b6f9c3 --- /dev/null +++ b/test/run/DivLowestI8.expect @@ -0,0 +1 @@ +64 \ No newline at end of file diff --git a/test/run/DivLowestI8.raz b/test/run/DivLowestI8.raz new file mode 100644 index 0000000..71551a4 --- /dev/null +++ b/test/run/DivLowestI8.raz @@ -0,0 +1,16 @@ +module DivLowestI8; +(* The lowest of every signed type divided by -1 stops the program, not + only the lowest I64's: here an I8's, by a divisor not known *) +import Out; + +proc main*; +var a, b: I8; +begin + a := -128; + b := I8(ARGC() - 2); + Out.int(I64(a div (-2))); + Out.flush; + Out.int(I64(a div b)); +end; + +end DivLowestI8. diff --git a/test/run/DivLowestI8.status b/test/run/DivLowestI8.status new file mode 100644 index 0000000..7296f25 --- /dev/null +++ b/test/run/DivLowestI8.status @@ -0,0 +1 @@ +136 diff --git a/test/run/DivLowestI8.stderr b/test/run/DivLowestI8.stderr new file mode 100644 index 0000000..17e094b --- /dev/null +++ b/test/run/DivLowestI8.stderr @@ -0,0 +1 @@ +test/run/DivLowestI8.raz:13:5: stopped by a division fault diff --git a/test/run/DivZero.raz b/test/run/DivZero.raz index 358d875..8b74148 100644 --- a/test/run/DivZero.raz +++ b/test/run/DivZero.raz @@ -1,8 +1,8 @@ module DivZero; (* Dividing by zero stops the program with SIGFPE, after saying where *) -proc main*: I64; -var a, b: I64; +proc main*: INT; +var a, b: INT; begin a := 7; b := ARGC() - 1; diff --git a/test/run/Edge.raz b/test/run/Edge.raz index 6870a5e..227264b 100644 --- a/test/run/Edge.raz +++ b/test/run/Edge.raz @@ -31,7 +31,7 @@ end; proc sumInner(xs: []Inner): I64; -var i, j, s: I64; +var i, j: INT; s: I64; begin s := 0; for i := 0 to LEN(xs) - 1 do diff --git a/test/run/NilProc.raz b/test/run/NilProc.raz index 738534d..ced4a72 100644 --- a/test/run/NilProc.raz +++ b/test/run/NilProc.raz @@ -1,9 +1,9 @@ module NilProc; (* Calling a nil procedure: the place is the call's *) -var f: proc(n: I64): I64; +var f: proc(n: INT): INT; -proc main*: I64; +proc main*: INT; begin return f(1) + 1; diff --git a/test/run/Spawn.raz b/test/run/Spawn.raz index 22ccd7d..baba4b1 100644 --- a/test/run/Spawn.raz +++ b/test/run/Spawn.raz @@ -11,7 +11,7 @@ var m: Thread.Mutex; - count, atomic: I64; + count: I64; atomic: INT; proc work(arg: ^U8); var i: I64; p, list: ^Node; diff --git a/test/run/StackOverflow.raz b/test/run/StackOverflow.raz index 0677b85..10a83c1 100644 --- a/test/run/StackOverflow.raz +++ b/test/run/StackOverflow.raz @@ -4,14 +4,14 @@ zeroes it from the bottom: with a small one, where the stack starts could put the guard page under the body's pushes instead. *) -proc down(n: I64): I64; -var pad: [65536]I64; +proc down(n: INT): INT; +var pad: [65536]INT; begin pad[n mod 65536] := n; return down(n + 1) + pad[0]; end; -proc main*: I64; +proc main*: INT; begin return down(0); end; diff --git a/test/run/mods/Unchecked.raz b/test/run/mods/Unchecked.raz new file mode 100644 index 0000000..ead9ee6 --- /dev/null +++ b/test/run/mods/Unchecked.raz @@ -0,0 +1,17 @@ +module Unchecked; +(* A module without index and slice checks *) +check off; + +type Pair* = record a*: [2]INT; b*: INT; end; + +proc get*(p: ^Pair; i: INT): INT; +begin + return p^.a[i]; +end; + +proc width*(s: []U8; lo, hi: INT): INT; +begin + return LEN(s[lo..hi]); +end; + +end Unchecked. diff --git a/tools/Doc.raz b/tools/Doc.raz index a6bf77b..8f58dd0 100644 --- a/tools/Doc.raz +++ b/tools/Doc.raz @@ -15,7 +15,7 @@ var sc: Scan.Scanner; (* the text being read, at the current symbol *) src: []U8; - docStart, docEnd: I64; (* the comment just before it, or -1 *) + docStart, docEnd: INT; (* the comment just before it, or -1 *) (* On to the next symbol that isn't a comment, keeping where the last comment that starts a line was: one after code on its line belongs @@ -53,8 +53,8 @@ (* On to the ; that ends what starts here, past anything bracketed and any record or variant; returns where it ends. *) -proc toSemicolon: I64; -var depth: I64; +proc toSemicolon: INT; +var depth: INT; begin depth := 0; while sc.kind # Scan.eof do @@ -68,7 +68,7 @@ end; (* stop, or the end of a comment that follows it on its line. *) -proc trailing(stop: I64): I64; +proc trailing(stop: INT): INT; var t: Scan.Scanner; begin while (stop < LEN(src)) and ((src[stop] = ' ') or (src[stop] = 9)) do INC(stop); end; @@ -80,7 +80,7 @@ end; (* Where the line holding p starts. *) -proc lineStart(p: I64): I64; +proc lineStart(p: INT): INT; begin while (p > 0) and (src[p - 1] # 10) do DEC(p); end; return p; @@ -88,8 +88,8 @@ (* src[start..stop] as written, each line after the first moved left as far as the first one's column. *) -proc block(start, stop: I64); -var col, i, n: I64; +proc block(start, stop: INT); +var col, i, n: INT; begin col := start - lineStart(start); i := start; @@ -106,8 +106,8 @@ (* The comment from start to stop, without its brackets, each line indented by four. *) -proc comment(start, stop: I64); -var i, j: I64; atLine: BOOL; +proc comment(start, stop: INT); +var i, j: INT; atLine: BOOL; begin if start < 0 then return; end; i := start + 2; @@ -128,7 +128,7 @@ end; (* One declaration, from its first token, the comment before it kept. *) -proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: I64); +proc declaration(prefix: []U8; exported: BOOL; start, ds, de, stop: INT); begin if not exported then return; end; Out.ln; @@ -149,7 +149,7 @@ (* A procedure's body: to the end that closes its begin, the procedure name after it and its ;. *) proc skipBody; -var depth: I64; sawBegin: BOOL; +var depth: INT; sawBegin: BOOL; begin depth := 0; sawBegin := false; @@ -172,7 +172,7 @@ end; proc procedure; -var start, ds, de, stop: I64; exp: BOOL; +var start, ds, de, stop: INT; exp: BOOL; begin start := sc.start; ds := docStart; de := docEnd; exp := name(); @@ -186,7 +186,7 @@ end; proc section; -var word: []U8; start, ds, de, stop: I64; exp: BOOL; +var word: []U8; start, ds, de, stop: INT; exp: BOOL; begin word := text(); next; @@ -206,7 +206,7 @@ end; proc external; -var start, ds, de, stop: I64; exp: BOOL; +var start, ds, de, stop: INT; exp: BOOL; begin start := sc.start; ds := docStart; de := docEnd; next; @@ -217,7 +217,7 @@ end; proc document(path: []U8); -var b: Buf.Buf; start, ds, de: I64; +var b: Buf.Buf; start, ds, de: INT; begin Buf.init(ADR(b), Heap.heap); if not Files.read(path, ADR(b)) then Rt.stop("Doc: can't read the file"); end; @@ -243,7 +243,7 @@ (* line, without the spaces at its ends *) proc trim(line: []U8): []U8; -var i, j: I64; +var i, j: INT; begin i := 0; j := LEN(line); @@ -255,7 +255,7 @@ (* The report's examples that are whole modules, each to dir. *) proc examples(path, dir: []U8); var - b, out: Buf.Buf; s, line, first, last, name: []U8; i, start, k, n: I64; + b, out: Buf.Buf; s, line, first, last, name: []U8; i, start, k, n: INT; inBlock, whole: BOOL; begin Buf.init(ADR(b), Heap.heap); diff --git a/tools/Mutate.raz b/tools/Mutate.raz index 9626ac4..8d5b176 100644 --- a/tools/Mutate.raz +++ b/tools/Mutate.raz @@ -14,17 +14,17 @@ state: U64; (* the generator: xorshift64 *) (* A number from the generator, below n. *) -proc random(n: I64): I64; +proc random(n: INT): INT; begin state := state xor (state shl 13); state := state xor (state shr 7); state := state xor (state shl 17); - return I64(state mod U64(n)); + return INT(state mod U64(n)); end; (* The seed, as a nonzero state. *) proc seed(s: []U8); -var i: I64; +var i: INT; begin state := 88172645463325252; for i := 0 to LEN(s) - 1 do state := state * 31 + U64(s[i]); end; @@ -33,8 +33,8 @@ end; (* The symbols that aren't comments. *) -proc count(): I64; -var sc: Scan.Scanner; n: I64; +proc count(): INT; +var sc: Scan.Scanner; n: INT; begin Scan.init(ADR(sc), src); Scan.next(ADR(sc)); @@ -47,7 +47,7 @@ end; (* Symbol k, counting from 0 and leaving out comments. *) -proc symbol(k: I64): Scan.Scanner; +proc symbol(k: INT): Scan.Scanner; var sc: Scan.Scanner; begin Scan.init(ADR(sc), src); @@ -59,7 +59,7 @@ end; proc main*; -var buf: Buf.Buf; n, op, i: I64; a, b, c: Scan.Scanner; +var buf: Buf.Buf; n, op, i: INT; a, b, c: Scan.Scanner; begin if ARGC() # 3 then Rt.stop("usage: Mutate File.raz seed"); end; Buf.init(ADR(buf), Heap.heap); diff --git a/tools/Pretty.raz b/tools/Pretty.raz index 4adbed0..e2eecce 100644 --- a/tools/Pretty.raz +++ b/tools/Pretty.raz @@ -34,25 +34,25 @@ sc: Scan.Scanner; (* the source, at the symbol being written *) src: []U8; out: Buf.Buf; - lineAt: I64; (* where the line being written starts in out *) - lineDelta: I64; (* how far this line's indent moved *) + lineAt: INT; (* where the line being written starts in out *) + lineDelta: INT; (* how far this line's indent moved *) (* the blocks open, each with the indent of the statement that opened it *) - bases: [100]I64; - depth: I64; - brackets: I64; (* brackets open *) + bases: [100]INT; + depth: INT; + brackets: INT; (* brackets open *) inSection: BOOL; (* among the declarations of const, type or var *) - stmtIndent: I64; (* the indent of the current statement's first line *) - stmtDelta: I64; (* and how far that moved *) + stmtIndent: INT; (* the indent of the current statement's first line *) + stmtDelta: INT; (* and how far that moved *) (* the last symbol that wasn't a comment, and the one before it *) prev, prev2: Scan.Scanner; prevBinary, prevUnary: BOOL; afterComment: BOOL; (* the last symbol written was a comment *) - last: I64; (* where the last symbol written ends in src *) + last: INT; (* where the last symbol written ends in src *) -proc spaces(n: I64); +proc spaces(n: INT); begin while n > 0 do Buf.add(ADR(out), ' '); DEC(n); end; end; @@ -75,15 +75,15 @@ end; (* Where the line holding p starts in src. *) -proc lineStart(p: I64): I64; +proc lineStart(p: INT): INT; begin while (p > 0) and (src[p - 1] # '\n') do DEC(p); end; return p; end; (* The column src[p] is at, a tab going on to the next multiple of 8. *) -proc column(p: I64): I64; -var i, col: I64; +proc column(p: INT): INT; +var i, col: INT; begin col := 0; for i := lineStart(p) to p - 1 do @@ -153,21 +153,21 @@ end; (* The indent of the block's own lines, or of its end. *) -proc inner: I64; +proc inner: INT; begin if depth > 0 then return bases[depth - 1] + width; end; if inSection then return width; end; return 0; end; -proc outer: I64; +proc outer: INT; begin if depth > 0 then return bases[depth - 1]; end; return 0; end; (* The indent of the line the current symbol starts, which was at old. *) -proc indent(old: I64): I64; +proc indent(old: INT): INT; var n: Scan.Scanner; begin if continues() then @@ -192,7 +192,7 @@ (* Starts the line of the current symbol, at its indent. *) proc startLine; -var old, new: I64; +var old, new: INT; begin old := column(sc.start); new := indent(old); @@ -207,7 +207,7 @@ (* The spaces between the last symbol written and the current one, on the same line. *) proc gap(binaryNow: BOOL); -var n: I64; +var n: INT; begin if sc.kind = Scan.comment then n := column(sc.start) + lineDelta - (out.len - lineAt); @@ -226,7 +226,7 @@ (* The current symbol as written; each line of a comment after its first is moved as far as the line the comment starts on. *) proc write; -var i, k: I64; +var i, k: INT; begin i := sc.start; while i < sc.stop do @@ -270,8 +270,8 @@ end; (* The line breaks in src[from..upto]. *) -proc lineBreaks(from, upto: I64); -var i: I64; +proc lineBreaks(from, upto: INT); +var i: INT; begin for i := from to upto - 1 do if src[i] = '\n' then newLine; end; @@ -317,7 +317,7 @@ end; proc main*; -var b: Buf.Buf; i: I64; path: []U8; +var b: Buf.Buf; i: INT; path: []U8; begin Buf.init(ADR(b), Heap.heap); Buf.init(ADR(out), Heap.heap); diff --git a/tools/Scan.raz b/tools/Scan.raz index aad4c50..13bfcae 100644 --- a/tools/Scan.raz +++ b/tools/Scan.raz @@ -13,9 +13,9 @@ character; symbol an operator or delimiter. *) Scanner* = record src*: []U8; - pos*: I64; (* where the next symbol is looked for *) + pos*: INT; (* where the next symbol is looked for *) kind*: Kind; (* the current symbol *) - start*, stop*: I64; (* its text: src[start..stop] *) + start*, stop*: INT; (* its text: src[start..stop] *) lineBreak*: BOOL; (* a line break comes before it, or it is the first *) end; @@ -41,7 +41,7 @@ (* Whether w is one of the words of list, which are separated by single spaces. *) proc among*(w, list: []U8): BOOL; -var i, j: I64; +var i, j: INT; begin i := 0; while i < LEN(list) do @@ -66,8 +66,8 @@ end; (* Past the comment that starts at p, its nested comments with it. *) -proc pastComment(src: []U8; p: I64): I64; -var depth: I64; +proc pastComment(src: []U8; p: INT): INT; +var depth: INT; begin depth := 0; repeat @@ -84,7 +84,7 @@ (* Past the number that starts at p: digits and letters, then a real's point, digits and exponent, as in 0FFH and 2.0E-3. *) -proc pastNumber(src: []U8; p: I64): I64; +proc pastNumber(src: []U8; p: INT): INT; begin while (p < LEN(src)) and isWord(src[p]) do INC(p); end; if (p + 1 < LEN(src)) and (src[p] = '.') and isDigit(src[p + 1]) then @@ -99,7 +99,7 @@ end; (* Past the string or character that starts at p, or to its line's end. *) -proc pastLiteral(src: []U8; p: I64): I64; +proc pastLiteral(src: []U8; p: INT): INT; var q: U8; begin q := src[p]; @@ -113,14 +113,14 @@ end; (* Whether src[p..] starts with the symbol t. *) -proc at(src: []U8; p: I64; t: []U8): BOOL; +proc at(src: []U8; p: INT; t: []U8): BOOL; begin return (p + LEN(t) <= LEN(src)) and (src[p..p + LEN(t)] = t); end; (* On to the next symbol. *) proc next*(s: ^Scanner); -var src: []U8; p: I64; c: U8; +var src: []U8; p: INT; c: U8; begin src := s^.src; p := s^.pos;