raz / commit 4d6f1bca

commit
4d6f1bca6b3b2fa6e5e7eb6374aafeac9037871a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ef0484e1
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.

patch · log

13 files changed, +137 -29

CLAUDE.md modified +6 -0

@@ -374,6 +374,12 @@
374374 type there, and its help says it would be `nil`. A slice where its
375375 element's pointer is wanted, and a pointer where a slice is, are
376376 errors whose help names `PTR(s)` and `LEN(s)`, or `p[0..n]`.
377+- `CAS(v, old, new)`, `XCHG(v, x)` and `XADD(v, x)` (report 2026-10.37)
378+ take a variable, whose address is pushed as `ADR`'s is (`atomic`); a
379+ local named in one is kept out of a register, as for `ADR`
380+ (`chooseRegs`). A pointer that isn't a variable, `ADR(x)`, is still
381+ taken as the old form until v0.24.0; a pointer variable is the new
382+ form, so `XADD(p, 1)` is an error whose help says `p^`.
377383 - `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s
378384 own width, `rol al, cl` for a `U8`, with no mask of the count: x86
379385 takes it modulo 32 or 64, and a byte's or a word's rotation goes

conform/run/AtomicVar.expect (history) added +2 -0

@@ -0,0 +1,2 @@
1+40 5 true false 3 7
2+2 true true

conform/run/AtomicVar.raz (history) added +30 -0

@@ -0,0 +1,30 @@
1+(* CAS, XCHG and XADD take a variable (report 6.8): a local, a field,
2+ a module variable, or one reached through a pointer, of an INT, a
3+ UINT or a pointer type; each returns the value before. *)
4+module AtomicVar;
5+import Fmt, Heap;
6+
7+type R = record n: INT; p: ^INT; end;
8+
9+var gp: ^INT;
10+
11+proc main*;
12+var n, k, i: INT; u: UINT; r: R; q, old: ^INT; ok, swapped: BOOL;
13+begin
14+ n := 5;
15+ k := XADD(n, 3);
16+ for i := 1 to 10 do _ := XADD(n, 1); end;
17+ ok := CAS(n, 18, 40);
18+ swapped := CAS(n, 18, 50);
19+ u := 1;
20+ _ := XADD(u, 2);
21+ _ := XCHG(r.n, 7);
22+ NEW(q, Heap.heap);
23+ _ := XADD(q^, 2);
24+ _ := CAS(gp, nil, q);
25+ old := XCHG(r.p, q);
26+ Fmt.print("{} {} {} {} {} {}\n", [Fmt.int(n), Fmt.int(k), Fmt.bool(ok), Fmt.bool(swapped), Fmt.uint(u), Fmt.int(r.n)]);
27+ Fmt.print("{} {} {}\n", [Fmt.int(q^), Fmt.bool(gp = q), Fmt.bool(old = nil)]);
28+end;
29+
30+end AtomicVar.

doc/compiler.md (history) modified +8 -0

@@ -1963,6 +1963,14 @@
19631963 parses its own arguments, since `SIZE` takes a type and `INC` a
19641964 variable.
19651965
1966+`CAS`, `XCHG` and `XADD` (report 2026-10.37) take a variable, as `INC`
1967+does: `atomic` pushes its address, as `ADR` would, and `Gen.atomic`
1968+works through it as before, so the code is the same as for
1969+`XADD(ADR(n), 1)`. A first argument that is a pointer but no variable,
1970+`ADR(n)`, is the form before 10.37, kept until v0.24.0. Since the
1971+address is taken, `chooseRegs` keeps a local named in one in memory,
1972+as it does one named in `ADR`.
1973+
19661974 `SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36) read the type with
19671975 `parseType` and then the field with `typeField`. The name before a `.`
19681976 in a type can only be a module's, so `parseType` reads `M.T` whole,

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

@@ -1312,7 +1312,7 @@
13121312 sum := 0;
13131313 for i := 1 to n do
13141314 sum := sum + i;
1315- _ := XADD(ADR(hits), 1);
1315+ _ := XADD(hits, 1);
13161316 end;
13171317 Thread.lock(m);
13181318 total := total + sum;

doc/raz.7 (history) modified +6 -6

@@ -483,17 +483,17 @@
483483 plus or minus
484484 .Ar k ,
485485 or 1; it wraps.
486-.It Fn CAS p old new
487-If
488-.Ar p Ns ^
486+.It Fn CAS v old new
487+If the variable
488+.Ar v
489489 is
490490 .Ar old ,
491491 set it to
492492 .Ar new
493493 and return true; atomically.
494-.It Fn XCHG p x , Fn XADD p x
495-Set, or add to,
496-.Ar p Ns ^ ,
494+.It Fn XCHG v x , Fn XADD v x
495+Set, or add to, the variable
496+.Ar v ,
497497 returning the value before; atomically.
498498 .It Fn ABS x
499499 .Ar x

doc/raz.md (history) modified +14 -8

@@ -64,8 +64,11 @@
6464 section (sections 4 and 5.9); it breaks no program.
6565 2026-10.37 mends a defect: an array a call returns, or a record or
6666 variant holding one, lives to the end of the procedure the call is in,
67-not of its statement, so a slice of it may be kept (section 8); it
68-breaks no program.
67+not of its statement, so a slice of it may be kept (section 8); and
68+`CAS`, `XCHG` and `XADD` take the variable they work on, not a pointer
69+to it (section 6.8). It breaks a program that gives them a pointer;
70+this compiler, raz 0.23, still takes one that isn't a variable,
71+`ADR(x)` say, as before.
6972
7073 The language is frozen, softly, from this version on: it changes only
7174 to mend a defect, and its library grows only by what breaks no program
@@ -1019,9 +1022,9 @@
10191022 | `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` |
10201023 | `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` |
10211024 | `FENCE` | a full memory barrier |
1022-| `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically |
1023-| `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically |
1024-| `XADD(p, x)` | adds `x` to `p^` and returns the value before, atomically |
1025+| `CAS(v, old, new)` | if `v = old`, sets `v := new` and returns `true`; atomically |
1026+| `XCHG(v, x)` | sets `v := x` and returns the value before, atomically |
1027+| `XADD(v, x)` | adds `x` to `v` and returns the value before, atomically |
10251028 | `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) |
10261029 | `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) |
10271030 | `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 @@
11111114 read `v` and then write it: two accesses, even through a `^VOLATILE`
11121115 pointer, and not atomic; `XADD` is the atomic one.
11131116
1114-For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T`
1115-is `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`), on
1116-every target. They are sequentially consistent, on every target; how a
1117+For `CAS`, `XCHG` and `XADD`, `v` is a variable, as for `INC`, of type
1118+`INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`), on every
1119+target, and may be volatile: a counter shared between threads is
1120+`XADD(count, 1)`, one reached through a pointer `XADD(p^, 1)`. It is
1121+evaluated once, and must have an address, so a field of a packed
1122+record and a view are errors, as they are for `ADR`. They are sequentially consistent, on every target; how a
11171123 machine without atomic instructions makes them so is the
11181124 implementation's choice (appendix A).
11191125 ### 6.9 Views

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

@@ -265,3 +265,7 @@
265265 temporary holding one; a slice of it may be kept in a variable for
266266 the rest of the procedure (sections 7.3, 8 and 10). It breaks no
267267 program.
268+ And `CAS`, `XCHG` and `XADD` take the variable they work on, as `INC`
269+ does, not a pointer to it (section 6.8): `XADD(count, 1)`,
270+ `XADD(p^, 1)`. It breaks a program that gives them a pointer; raz
271+ 0.23 still takes one that isn't a variable, `ADR(x)`, until 0.24.

src/Parse.raz (history) modified +52 -12

@@ -108,7 +108,7 @@
108108 curResult: PType; (* the result type of the procedure being compiled *)
109109 inExtern: BOOL; (* reading an extern's signature: ... allowed *)
110110 tArgv, tBodySig: PType;
111- nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr: INT;
111+ nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr, nameCas, nameXchg, nameXadd: INT;
112112 declaring: [MaxNames]INT; (* the names the declaration being read declares *)
113113 numDeclaring: INT;
114114 skipped: BOOL; (* the last statement had an error and was skipped *)
@@ -1085,21 +1085,57 @@
10851085 return Item.endCall(c, x);
10861086 end;
10871087
1088+(* Whether an atomic built-in, which, works on a variable of type t:
1089+ an INT or a UINT, or for CAS and XCHG a pointer *)
1090+proc atomicType(t: PType; which: INT): BOOL;
1091+begin
1092+ return Item.isWord(t) or ((t^.form = Sym.fPtr) and (which # Sym.bXadd));
1093+end;
1094+
1095+(* The name of the atomic built-in which, for a message *)
1096+proc atomicName(which: INT): []U8;
1097+begin
1098+ if which = Sym.bCas then return "CAS"; end;
1099+ if which = Sym.bXchg then return "XCHG"; end;
1100+ return "XADD";
1101+end;
1102+
1103+(* CAS(v, old, new), XCHG(v, x) or XADD(v, x) (report 6.8): v is a
1104+ variable, whose address goes on the stack, as ADR's would. A pointer
1105+ that isn't a variable, ADR(v) say, is the form before report
1106+ 2026-10.37, still taken: what it points at is the variable. *)
10881107 proc atomic(var x: TItem; which: INT): ERROR;
10891108 var p, a: TItem; t: PType; i, n: INT;
10901109 begin
10911110 try expression(p, nil);
1092- if p.typ^.form # Sym.fPtr then
1093- return Scan.markAt(p.pos, say1("expected a pointer, found {}", Item.what(p)));
1094- end;
1095- t := p.typ^.base;
1096- if not (Item.isWord(t) or ((t^.form = Sym.fPtr) and (which # Sym.bXadd))) then
1097- if which = Sym.bXadd then
1098- return Scan.markAt(p.pos, say1("expected a pointer to an INT or a UINT, found {}", Sym.typeName(p.typ)));
1111+ if (p.mode = Item.mVar) and p.isVar then
1112+ t := p.typ;
1113+ if not atomicType(t, which) then
1114+ if (t^.form = Sym.fPtr) and atomicType(t^.base, which) then
1115+ return Scan.markHelp(p.pos, say1("{} works on an INT or a UINT variable, not a pointer", atomicName(which)),
1116+ say1("for what the pointer points at, write {}^", Diag.source(p.pos)));
1117+ elsif which = Sym.bXadd then
1118+ return Scan.markAt(p.pos, say1("expected an INT or a UINT variable, found {}", Item.what(p)));
1119+ end;
1120+ return Scan.markAt(p.pos, say1("expected an INT, a UINT or a pointer variable, found {}", Item.what(p)));
10991121 end;
1100- return Scan.markAt(p.pos, say1("expected a pointer to an INT, a UINT or a pointer, found {}", Sym.typeName(p.typ)));
1122+ try Item.notReadOnly(p);
1123+ if p.odd then return Scan.markAt(p.pos, "a field of a packed record has no address"); end;
1124+ if p.view then return Item.noAddress(p, "an atomic can't take a view"); end;
1125+ try Item.address(p);
1126+ else
1127+ if p.typ^.form # Sym.fPtr then
1128+ return Scan.markAt(p.pos, say1("expected a variable, found {}", Item.what(p)));
1129+ end;
1130+ t := p.typ^.base;
1131+ if not atomicType(t, which) then
1132+ if which = Sym.bXadd then
1133+ return Scan.markAt(p.pos, say1("expected a pointer to an INT or a UINT, found {}", Sym.typeName(p.typ)));
1134+ end;
1135+ return Scan.markAt(p.pos, say1("expected a pointer to an INT, a UINT or a pointer, found {}", Sym.typeName(p.typ)));
1136+ end;
1137+ try Item.value(p);
11011138 end;
1102- try Item.value(p);
11031139 if which = Sym.bCas then n := 2; else n := 1; end;
11041140 for i := 1 to n do
11051141 try expect(Scan.sComma);
@@ -3086,7 +3122,8 @@
30863122 (* Puts the busiest variables of the procedure, whose parameters are
30873123 params and whose locals follow before, in registers: read ahead from
30883124 its "begin" to its end, a use counts 1, in a loop 4, in two 16, in
3089- three or more 64, and a variable ADR is taken of can't have one. A register costs a save
3125+ three or more 64, and a variable ADR, CAS, XCHG or XADD is given
3126+ can't have one, since they need its address. A register costs a save
30903127 and a restore, so a variable used less than three times keeps its
30913128 place in memory. A slice takes two, its pointer's and its length's;
30923129 a real one of the xmm registers, stored and loaded again around each
@@ -3152,7 +3189,7 @@
31523189 DEC(d);
31533190 end;
31543191 | sIdent:
3155- if Scan.id = nameAdr then
3192+ if (Scan.id = nameAdr) or (Scan.id = nameCas) or (Scan.id = nameXchg) or (Scan.id = nameXadd) then
31563193 Scan.next;
31573194 if Scan.tok = Scan.sLparen then Scan.next; end;
31583195 (* a hugely negative weight bars it from a register, whatever
@@ -4739,6 +4776,9 @@
47394776 nameRt := internName("Rt");
47404777 nameFail := internName("fail");
47414778 nameAdr := internName("ADR");
4779+ nameCas := internName("CAS");
4780+ nameXchg := internName("XCHG");
4781+ nameXadd := internName("XADD");
47424782 nameBody := internName("body");
47434783 nameStatic := internName("static");
47444784 nameCheck := internName("check");

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

@@ -0,0 +1,5 @@
1+test/err/AtomicPtr.raz:5:15: error: XADD works on an INT or a UINT variable, not a pointer
2+ 5 | _ := XADD(q, 1);
3+ | ^
4+help: for what the pointer points at, write q^
5+raz: 1 error

test/err/AtomicPtr.raz (history) added +7 -0

@@ -0,0 +1,7 @@
1+module AtomicPtr;
2+var q: ^INT;
3+proc main*;
4+begin
5+ _ := XADD(q, 1);
6+end;
7+end AtomicPtr.

test/err/DeclAtomicSize.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-test/err/DeclAtomicSize.raz:8:15: error: expected a pointer to an INT or a UINT, found ^U8
1+test/err/DeclAtomicSize.raz:8:15: error: expected an INT or a UINT variable, found ^U8
22 8 | n := XADD(p, 1);
33 | ^
44 raz: 1 error

test/msg/DeclAtomicPtr.err (history) modified +1 -1

@@ -1,4 +1,4 @@
1-conform/err/DeclAtomicPtr.raz:5:15: error: expected a pointer, found I64
1+conform/err/DeclAtomicPtr.raz:5:15: error: expected an INT or a UINT variable, found I64
22 5 | n := XADD(n, 1);
33 | ^
44 raz: 1 error