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.
| @@ -374,6 +374,12 @@ | ||
| 374 | 374 | type there, and its help says it would be `nil`. A slice where its |
| 375 | 375 | element's pointer is wanted, and a pointer where a slice is, are |
| 376 | 376 | 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^`. | |
| 377 | 383 | - `ROL(x, n)` and `ROR(x, n)` (report 2026-10.35) are done at `x`'s |
| 378 | 384 | own width, `rol al, cl` for a `U8`, with no mask of the count: x86 |
| 379 | 385 | takes it modulo 32 or 64, and a byte's or a word's rotation goes |
| @@ -0,0 +1,2 @@ | ||
| 1 | +40 5 true false 3 7 | |
| 2 | +2 true true |
| @@ -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. |
| @@ -1963,6 +1963,14 @@ | ||
| 1963 | 1963 | parses its own arguments, since `SIZE` takes a type and `INC` a |
| 1964 | 1964 | variable. |
| 1965 | 1965 | |
| 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 | + | |
| 1966 | 1974 | `SIZE(T.f)` and `OFFSET(T, f)` (report 2026-10.36) read the type with |
| 1967 | 1975 | `parseType` and then the field with `typeField`. The name before a `.` |
| 1968 | 1976 | in a type can only be a module's, so `parseType` reads `M.T` whole, |
| @@ -1312,7 +1312,7 @@ | ||
| 1312 | 1312 | sum := 0; |
| 1313 | 1313 | for i := 1 to n do |
| 1314 | 1314 | sum := sum + i; |
| 1315 | - _ := XADD(ADR(hits), 1); | |
| 1315 | + _ := XADD(hits, 1); | |
| 1316 | 1316 | end; |
| 1317 | 1317 | Thread.lock(m); |
| 1318 | 1318 | total := total + sum; |
| @@ -483,17 +483,17 @@ | ||
| 483 | 483 | plus or minus |
| 484 | 484 | .Ar k , |
| 485 | 485 | 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 | |
| 489 | 489 | is |
| 490 | 490 | .Ar old , |
| 491 | 491 | set it to |
| 492 | 492 | .Ar new |
| 493 | 493 | 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 , | |
| 497 | 497 | returning the value before; atomically. |
| 498 | 498 | .It Fn ABS x |
| 499 | 499 | .Ar x |
| @@ -64,8 +64,11 @@ | ||
| 64 | 64 | section (sections 4 and 5.9); it breaks no program. |
| 65 | 65 | 2026-10.37 mends a defect: an array a call returns, or a record or |
| 66 | 66 | 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. | |
| 69 | 72 | |
| 70 | 73 | The language is frozen, softly, from this version on: it changes only |
| 71 | 74 | to mend a defect, and its library grows only by what breaks no program |
| @@ -1019,9 +1022,9 @@ | ||
| 1019 | 1022 | | `INC(v)`, `INC(v, k)` | adds 1, or `k`, to the integer variable `v`; it wraps, like `+` | |
| 1020 | 1023 | | `DEC(v)`, `DEC(v, k)` | subtracts 1, or `k`, from `v` | |
| 1021 | 1024 | | `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 | | |
| 1025 | 1028 | | `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) | |
| 1026 | 1029 | | `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) | |
| 1027 | 1030 | | `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 @@ | ||
| 1111 | 1114 | read `v` and then write it: two accesses, even through a `^VOLATILE` |
| 1112 | 1115 | pointer, and not atomic; `XADD` is the atomic one. |
| 1113 | 1116 | |
| 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 | |
| 1117 | 1123 | machine without atomic instructions makes them so is the |
| 1118 | 1124 | implementation's choice (appendix A). |
| 1119 | 1125 | ### 6.9 Views |
| @@ -265,3 +265,7 @@ | ||
| 265 | 265 | temporary holding one; a slice of it may be kept in a variable for |
| 266 | 266 | the rest of the procedure (sections 7.3, 8 and 10). It breaks no |
| 267 | 267 | 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. |
| @@ -108,7 +108,7 @@ | ||
| 108 | 108 | curResult: PType; (* the result type of the procedure being compiled *) |
| 109 | 109 | inExtern: BOOL; (* reading an extern's signature: ... allowed *) |
| 110 | 110 | tArgv, tBodySig: PType; |
| 111 | - nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr: INT; | |
| 111 | + nameRt, nameFail, nameBody, nameStatic, nameCheck, nameOff, nameAdr, nameCas, nameXchg, nameXadd: INT; | |
| 112 | 112 | declaring: [MaxNames]INT; (* the names the declaration being read declares *) |
| 113 | 113 | numDeclaring: INT; |
| 114 | 114 | skipped: BOOL; (* the last statement had an error and was skipped *) |
| @@ -1085,21 +1085,57 @@ | ||
| 1085 | 1085 | return Item.endCall(c, x); |
| 1086 | 1086 | end; |
| 1087 | 1087 | |
| 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. *) | |
| 1088 | 1107 | proc atomic(var x: TItem; which: INT): ERROR; |
| 1089 | 1108 | var p, a: TItem; t: PType; i, n: INT; |
| 1090 | 1109 | begin |
| 1091 | 1110 | 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))); | |
| 1099 | 1121 | 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); | |
| 1101 | 1138 | end; |
| 1102 | - try Item.value(p); | |
| 1103 | 1139 | if which = Sym.bCas then n := 2; else n := 1; end; |
| 1104 | 1140 | for i := 1 to n do |
| 1105 | 1141 | try expect(Scan.sComma); |
| @@ -3086,7 +3122,8 @@ | ||
| 3086 | 3122 | (* Puts the busiest variables of the procedure, whose parameters are |
| 3087 | 3123 | params and whose locals follow before, in registers: read ahead from |
| 3088 | 3124 | 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 | |
| 3090 | 3127 | and a restore, so a variable used less than three times keeps its |
| 3091 | 3128 | place in memory. A slice takes two, its pointer's and its length's; |
| 3092 | 3129 | a real one of the xmm registers, stored and loaded again around each |
| @@ -3152,7 +3189,7 @@ | ||
| 3152 | 3189 | DEC(d); |
| 3153 | 3190 | end; |
| 3154 | 3191 | | 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 | |
| 3156 | 3193 | Scan.next; |
| 3157 | 3194 | if Scan.tok = Scan.sLparen then Scan.next; end; |
| 3158 | 3195 | (* a hugely negative weight bars it from a register, whatever |
| @@ -4739,6 +4776,9 @@ | ||
| 4739 | 4776 | nameRt := internName("Rt"); |
| 4740 | 4777 | nameFail := internName("fail"); |
| 4741 | 4778 | nameAdr := internName("ADR"); |
| 4779 | + nameCas := internName("CAS"); | |
| 4780 | + nameXchg := internName("XCHG"); | |
| 4781 | + nameXadd := internName("XADD"); | |
| 4742 | 4782 | nameBody := internName("body"); |
| 4743 | 4783 | nameStatic := internName("static"); |
| 4744 | 4784 | nameCheck := internName("check"); |
| @@ -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 |
| @@ -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. |
| @@ -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 | |
| 2 | 2 | 8 | n := XADD(p, 1); |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |
| @@ -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 | |
| 2 | 2 | 5 | n := XADD(n, 1); |
| 3 | 3 | | ^ |
| 4 | 4 | raz: 1 error |