From 4d6f1bca6b3b2fa6e5e7eb6374aafeac9037871a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:46:03 +0000 Subject: [PATCH] Report 2026-10.37: CAS, XCHG and XADD take a variable XADD(count, 1), XADD(p^, 1): atomic pushes the variable's address, as ADR would, and chooseRegs keeps such a local in memory. A pointer that isn't a variable, ADR(x), is still taken until v0.24.0; a pointer variable is the new form, so XADD(p, 1) is an error whose help says p^. conform/run/AtomicVar holds it. --- diff --git a/CLAUDE.md b/CLAUDE.md index be7e1b3..a4a73bd 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -374,6 +374,12 @@ type there, and its help says it would be `nil`. A slice where its element's pointer is wanted, and a pointer where a slice is, are errors whose help names `PTR(s)` and `LEN(s)`, or `p[0..n]`. +- `CAS(v, old, new)`, `XCHG(v, x)` and `XADD(v, x)` (report 2026-10.37) + take a variable, whose address is pushed as `ADR`'s is (`atomic`); a + local named in one is kept out of a register, as for `ADR` + (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still + taken as the old form until v0.24.0; a pointer variable is the new + form, so `XADD(p, 1)` is an error whose help says `p^`. - `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s own width, `rol al, cl` for a `U8`, with no mask of the count: x86 takes it modulo 32 or 64, and a byte's or a word's rotation goes diff --git a/conform/run/AtomicVar.expect b/conform/run/AtomicVar.expect new file mode 100644 index 0000000..4cb8455 --- /dev/null +++ b/conform/run/AtomicVar.expect @@ -0,0 +1,2 @@ +40 5 true false 3 7 +2 true true diff --git a/conform/run/AtomicVar.raz b/conform/run/AtomicVar.raz new file mode 100644 index 0000000..5d02bf2 --- /dev/null +++ b/conform/run/AtomicVar.raz @@ -0,0 +1,30 @@ +(* CAS, XCHG and XADD take a variable (report 6.8): a local, a field, + a module variable, or one reached through a pointer, of an INT, a + UINT or a pointer type; each returns the value before. *) +module AtomicVar; +import Fmt, Heap; + +type R = record n: INT; p: ^INT; end; + +var gp: ^INT; + +proc main*; +var n, k, i: INT; u: UINT; r: R; q, old: ^INT; ok, swapped: BOOL; +begin + n := 5; + k := XADD(n, 3); + for i := 1 to 10 do _ := XADD(n, 1); end; + ok := CAS(n, 18, 40); + swapped := CAS(n, 18, 50); + u := 1; + _ := XADD(u, 2); + _ := XCHG(r.n, 7); + NEW(q, Heap.heap); + _ := XADD(q^, 2); + _ := CAS(gp, nil, q); + old := XCHG(r.p, q); + Fmt.print("{} {} {} {} {} {}\n", [Fmt.int(n), Fmt.int(k), Fmt.bool(ok), Fmt.bool(swapped), Fmt.uint(u), Fmt.int(r.n)]); + Fmt.print("{} {} {}\n", [Fmt.int(q^), Fmt.bool(gp = q), Fmt.bool(old = nil)]); +end; + +end AtomicVar. diff --git a/doc/compiler.md b/doc/compiler.md index ab2c5e0..7277799 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1963,6 +1963,14 @@ parses its own arguments, since `SIZE` takes a type and `INC` a variable. +`CAS`, `XCHG` and `XADD` (report 2026-10.37) take a variable, as `INC` +does: `atomic` pushes its address, as `ADR` would, and `Gen.atomic` +works through it as before, so the code is the same as for +`XADD(ADR(n), 1)`. A first argument that is a pointer but no variable, +`ADR(n)`, is the form before 10.37, kept until v0.24.0. Since the +address is taken, `chooseRegs` keeps a local named in one in memory, +as it does one named in `ADR`. + `SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36) read the type with `parseType` and then the field with `typeField`. The name before a `.` in a type can only be a module's, so `parseType` reads `M.T` whole, diff --git a/doc/raz-tutorial.7 b/doc/raz-tutorial.7 index c91ebf5..45f3cfe 100644 --- a/doc/raz-tutorial.7 +++ b/doc/raz-tutorial.7 @@ -1312,7 +1312,7 @@ sum := 0; for i := 1 to n do sum := sum + i; - _ := XADD(ADR(hits), 1); + _ := XADD(hits, 1); end; Thread.lock(m); total := total + sum; diff --git a/doc/raz.7 b/doc/raz.7 index 9298052..c8a8c48 100644 --- a/doc/raz.7 +++ b/doc/raz.7 @@ -483,17 +483,17 @@ plus or minus .Ar k , or 1; it wraps. -.It Fn CAS p old new -If -.Ar p Ns ^ +.It Fn CAS v old new +If the variable +.Ar v is .Ar old , set it to .Ar new and return true; atomically. -.It Fn XCHG p x , Fn XADD p x -Set, or add to, -.Ar p Ns ^ , +.It Fn XCHG v x , Fn XADD v x +Set, or add to, the variable +.Ar v , returning the value before; atomically. .It Fn ABS x .Ar x diff --git a/doc/raz.md b/doc/raz.md index a2a9e0d..91e50a2 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -64,8 +64,11 @@ section (sections 4 and 5.9); it breaks no program. 2026-10.37 mends a defect: an array a call returns, or a record or variant holding one, lives to the end of the procedure the call is in, -not of its statement, so a slice of it may be kept (section 8); it -breaks no program. +not of its statement, so a slice of it may be kept (section 8); and +`CAS`, `XCHG` and `XADD` take the variable they work on, not a pointer +to it (section 6.8). It breaks a program that gives them a pointer; +this compiler, raz 0.23, still takes one that isn't a variable, +`ADR(x)` say, as before. The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program @@ -1019,9 +1022,9 @@ | `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` | | `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` | | `FENCE` | a full memory barrier | -| `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically | -| `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically | -| `XADD(p, x)` | adds `x` to `p^` and returns the value before, atomically | +| `CAS(v, old, new)` | if `v = old`, sets `v := new` and returns `true`; atomically | +| `XCHG(v, x)` | sets `v := x` and returns the value before, atomically | +| `XADD(v, x)` | adds `x` to `v` and returns the value before, atomically | | `ABS(x)` | `x` without its sign, of `x`'s type; for an integer, `-x` if `x < 0`, so the smallest `I64` gives itself, as `-x` does; for a real, the sign cleared; `x` of a numeric type (section 6.1) | | `ROL(x, n)`, `ROR(x, n)` | `x` rotated left, or right, by `n` bits modulo its width; `x` of an unsigned integer type (section 6.1) | | `CLZ(x)`, `CTZ(x)` | the number of zero bits above the highest one in `x`, or below the lowest one, an `INT`; the width of `x`'s type if `x` is 0; `x` of an unsigned integer type | @@ -1111,9 +1114,12 @@ read `v` and then write it: two accesses, even through a `^VOLATILE` pointer, and not atomic; `XADD` is the atomic one. -For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T` -is `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`), on -every target. They are sequentially consistent, on every target; how a +For `CAS`, `XCHG` and `XADD`, `v` is a variable, as for `INC`, of type +`INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`), on every +target, and may be volatile: a counter shared between threads is +`XADD(count, 1)`, one reached through a pointer `XADD(p^, 1)`. It is +evaluated once, and must have an address, so a field of a packed +record and a view are errors, as they are for `ADR`. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation's choice (appendix A). ### 6.9 Views diff --git a/doc/report-history.md b/doc/report-history.md index a06a35c..d465191 100644 --- a/doc/report-history.md +++ b/doc/report-history.md @@ -265,3 +265,7 @@ temporary holding one; a slice of it may be kept in a variable for the rest of the procedure (sections 7.3, 8 and 10). It breaks no program. + And `CAS`, `XCHG` and `XADD` take the variable they work on, as `INC` + does, not a pointer to it (section 6.8): `XADD(count, 1)`, + `XADD(p^, 1)`. It breaks a program that gives them a pointer; raz + 0.23 still takes one that isn't a variable, `ADR(x)`, until 0.24. diff --git a/src/Parse.raz b/src/Parse.raz index 8cdfccf..3e43a14 100644 --- a/src/Parse.raz +++ b/src/Parse.raz @@ -108,7 +108,7 @@ curResult: PType; (* the result type of the procedure being compiled *) inExtern: BOOL; (* reading an extern's signature: ... allowed *) tArgv, tBodySig: PType; - nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr: INT; + nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr, nameCas, nameXchg, nameXadd: INT; declaring: [MaxNames]INT; (* the names the declaration being read declares *) numDeclaring: INT; skipped: BOOL; (* the last statement had an error and was skipped *) @@ -1085,21 +1085,57 @@ return Item.endCall(c, x); end; +(* Whether an atomic built-in, which, works on a variable of type t: + an INT or a UINT, or for CAS and XCHG a pointer *) +proc atomicType(t: PType; which: INT): BOOL; +begin + return Item.isWord(t) or ((t^.form = Sym.fPtr) and (which # Sym.bXadd)); +end; + +(* The name of the atomic built-in which, for a message *) +proc atomicName(which: INT): []U8; +begin + if which = Sym.bCas then return "CAS"; end; + if which = Sym.bXchg then return "XCHG"; end; + return "XADD"; +end; + +(* CAS(v, old, new), XCHG(v, x) or XADD(v, x) (report 6.8): v is a + variable, whose address goes on the stack, as ADR's would. A pointer + that isn't a variable, ADR(v) say, is the form before report + 2026-10.37, still taken: what it points at is the variable. *) proc atomic(var x: TItem; which: INT): ERROR; var p, a: TItem; t: PType; i, n: INT; begin try expression(p, nil); - if p.typ^.form # Sym.fPtr then - return Scan.markAt(p.pos, say1("expected a pointer, found {}", Item.what(p))); - end; - t := p.typ^.base; - if not (Item.isWord(t) or ((t^.form = Sym.fPtr) and (which # Sym.bXadd))) then - if which = Sym.bXadd then - return Scan.markAt(p.pos, say1("expected a pointer to an INT or a UINT, found {}", Sym.typeName(p.typ))); + if (p.mode = Item.mVar) and p.isVar then + t := p.typ; + if not atomicType(t, which) then + if (t^.form = Sym.fPtr) and atomicType(t^.base, which) then + return Scan.markHelp(p.pos, say1("{} works on an INT or a UINT variable, not a pointer", atomicName(which)), + say1("for what the pointer points at, write {}^", Diag.source(p.pos))); + elsif which = Sym.bXadd then + return Scan.markAt(p.pos, say1("expected an INT or a UINT variable, found {}", Item.what(p))); + end; + return Scan.markAt(p.pos, say1("expected an INT, a UINT or a pointer variable, found {}", Item.what(p))); end; - return Scan.markAt(p.pos, say1("expected a pointer to an INT, a UINT or a pointer, found {}", Sym.typeName(p.typ))); + try Item.notReadOnly(p); + if p.odd then return Scan.markAt(p.pos, "a field of a packed record has no address"); end; + if p.view then return Item.noAddress(p, "an atomic can't take a view"); end; + try Item.address(p); + else + if p.typ^.form # Sym.fPtr then + return Scan.markAt(p.pos, say1("expected a variable, found {}", Item.what(p))); + end; + t := p.typ^.base; + if not atomicType(t, which) then + if which = Sym.bXadd then + return Scan.markAt(p.pos, say1("expected a pointer to an INT or a UINT, found {}", Sym.typeName(p.typ))); + end; + return Scan.markAt(p.pos, say1("expected a pointer to an INT, a UINT or a pointer, found {}", Sym.typeName(p.typ))); + end; + try Item.value(p); end; - try Item.value(p); if which = Sym.bCas then n := 2; else n := 1; end; for i := 1 to n do try expect(Scan.sComma); @@ -3086,7 +3122,8 @@ (* Puts the busiest variables of the procedure, whose parameters are params and whose locals follow before, in registers: read ahead from its "begin" to its end, a use counts 1, in a loop 4, in two 16, in - three or more 64, and a variable ADR is taken of can't have one. A register costs a save + three or more 64, and a variable ADR, CAS, XCHG or XADD is given + can't have one, since they need its address. A register costs a save and a restore, so a variable used less than three times keeps its place in memory. A slice takes two, its pointer's and its length's; a real one of the xmm registers, stored and loaded again around each @@ -3152,7 +3189,7 @@ DEC(d); end; | sIdent: - if Scan.id = nameAdr then + if (Scan.id = nameAdr) or (Scan.id = nameCas) or (Scan.id = nameXchg) or (Scan.id = nameXadd) then Scan.next; if Scan.tok = Scan.sLparen then Scan.next; end; (* a hugely negative weight bars it from a register, whatever @@ -4739,6 +4776,9 @@ nameRt := internName("Rt"); nameFail := internName("fail"); nameAdr := internName("ADR"); + nameCas := internName("CAS"); + nameXchg := internName("XCHG"); + nameXadd := internName("XADD"); nameBody := internName("body"); nameStatic := internName("static"); nameCheck := internName("check"); diff --git a/test/err/AtomicPtr.err b/test/err/AtomicPtr.err new file mode 100644 index 0000000..9658fc6 --- /dev/null +++ b/test/err/AtomicPtr.err @@ -0,0 +1,5 @@ +test/err/AtomicPtr.raz:5:15: error: XADD works on an INT or a UINT variable, not a pointer + 5 | _ := XADD(q, 1); + | ^ +help: for what the pointer points at, write q^ +raz: 1 error diff --git a/test/err/AtomicPtr.raz b/test/err/AtomicPtr.raz new file mode 100644 index 0000000..6a76302 --- /dev/null +++ b/test/err/AtomicPtr.raz @@ -0,0 +1,7 @@ +module AtomicPtr; +var q: ^INT; +proc main*; +begin + _ := XADD(q, 1); +end; +end AtomicPtr. diff --git a/test/err/DeclAtomicSize.err b/test/err/DeclAtomicSize.err index e57a298..fda9ee1 100644 --- a/test/err/DeclAtomicSize.err +++ b/test/err/DeclAtomicSize.err @@ -1,4 +1,4 @@ -test/err/DeclAtomicSize.raz:8:15: error: expected a pointer to an INT or a UINT, found ^U8 +test/err/DeclAtomicSize.raz:8:15: error: expected an INT or a UINT variable, found ^U8 8 | n := XADD(p, 1); | ^ raz: 1 error diff --git a/test/msg/DeclAtomicPtr.err b/test/msg/DeclAtomicPtr.err index fa9d7e3..3c78562 100644 --- a/test/msg/DeclAtomicPtr.err +++ b/test/msg/DeclAtomicPtr.err @@ -1,4 +1,4 @@ -conform/err/DeclAtomicPtr.raz:5:15: error: expected a pointer, found I64 +conform/err/DeclAtomicPtr.raz:5:15: error: expected an INT or a UINT variable, found I64 5 | n := XADD(n, 1); | ^ raz: 1 error