Record constants, constant tables, and Str.copy Report 2026-10.16. AggregateConst reads an array or record constant part by part (ConstPart) into a rodata chunk: numbers, sets and the like as before, a string into a []U8 or a [n]U8, an array constant into a slice, procedures, ERRORs and nil. An address in it is a data fixup (chunk.pas's AddDataFixup), a 64-bit field the linker fills with the chunk's address; MarkLive follows them. MakeItem makes a record constant a read-only variable, as an array constant is. Str.copy copies a slice into memory from an allocator. conform/run/RecordConst; conform/err/ArrayConstElem now refuses a variant. 7,891 lines of code.
| @@ -86,12 +86,13 @@ | ||
| 86 | 86 | defect, and the library grows only by what breaks no program written |
| 87 | 87 | for it. A wish for more, the owner's or yours, goes on the list in |
| 88 | 88 | `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, |
| 89 | -by the owner's word: report 2026-10.12; the compiler follows 2026-10.15, | |
| 89 | +by the owner's word: report 2026-10.12; the compiler follows 2026-10.16, | |
| 90 | 90 | whose `Sys` writes without waiting, for rat's pastes (10.13), and |
| 91 | 91 | whose blank, `_ := f(x)`, drops a result that is otherwise warned of |
| 92 | 92 | (10.14), and whose `Math` has `pow`, the logarithms to 2 and 10, |
| 93 | -rounding and the inverse functions, and `Sys` `space` (10.15), also | |
| 94 | -by the owner's word. Its parser | |
| 93 | +rounding and the inverse functions, and `Sys` `space` (10.15), and | |
| 94 | +whose constants may be records and tables of them, with `Str.copy` | |
| 95 | +(10.16), also by the owner's word. Its parser | |
| 95 | 96 | reaches the machine only through `gen.pas`; its steps 5 to 10 await |
| 96 | 97 | the owner's review, and git has them. Keep |
| 97 | 98 | `doc/compiler.md`'s table of lines and its routine names true when the |
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | module ArrayConstElem; |
| 2 | -type R = record a: I64; end; | |
| 3 | -const P: [1]R = [{a: 1}]; | |
| 2 | +type S = variant a; b; end; | |
| 3 | +const P: [1]S = [a]; | |
| 4 | 4 | end ArrayConstElem. |
| @@ -0,0 +1,9 @@ | ||
| 1 | +firefox 2 | |
| 2 | +mpv 0 float | |
| 3 | + 0 | |
| 4 | +30 | |
| 5 | +home7 | |
| 6 | +3 | |
| 7 | +bye113 | |
| 8 | +hello 5 | |
| 9 | +nil run |
| @@ -0,0 +1,34 @@ | ||
| 1 | +module RecordConst; | |
| 2 | +import Out; | |
| 3 | +type | |
| 4 | + Act = (quit, focus, spawn); | |
| 5 | + Rule = record cls: []U8; tag: INT; float: BOOL; end; | |
| 6 | + Key = record mods: U8; sym: U32; act: Act; arg: [8]U8; run: proc(n: INT); end; | |
| 7 | +const | |
| 8 | + Rules: [3]Rule = [{cls: "firefox", tag: 2}, {cls: "mpv", float: true}, {}]; | |
| 9 | + Nums: [3]I64 = [10, 20, 30]; | |
| 10 | + Lists: [2][]I64 = [Nums, Nums]; | |
| 11 | + Home: Rule = {cls: "home", tag: 7}; | |
| 12 | + Grid: [2][2]U8 = [[1, 2], [3, 4]]; | |
| 13 | +proc hello(n: INT); | |
| 14 | +begin | |
| 15 | + Out.str("hello "); Out.int(n); Out.ln; | |
| 16 | +end; | |
| 17 | +const Keys: [2]Key = [{mods: 4, sym: 113, act: quit, arg: "bye", run: hello}, {act: spawn, run: nil}]; | |
| 18 | +proc main*; | |
| 19 | +var i: INT; r: Rule; | |
| 20 | +begin | |
| 21 | + for i := 0 to LEN(Rules) - 1 do | |
| 22 | + Out.str(Rules[i].cls); Out.str(" "); Out.int(Rules[i].tag); Out.str(" "); | |
| 23 | + if Rules[i].float then Out.str("float"); end; | |
| 24 | + Out.ln; | |
| 25 | + end; | |
| 26 | + Out.int(Lists[1][2]); Out.ln; | |
| 27 | + r := Home; | |
| 28 | + Out.str(r.cls); Out.int(Home.tag); Out.ln; | |
| 29 | + Out.int(I64(Grid[1][0])); Out.ln; | |
| 30 | + Out.str(Keys[0].arg[0..3]); Out.int(INT(Keys[0].sym)); Out.ln; | |
| 31 | + Keys[0].run(5); | |
| 32 | + if Keys[1].run = nil then Out.str("nil run"); Out.ln; end; | |
| 33 | +end; | |
| 34 | +end RecordConst. |
| @@ -11,12 +11,12 @@ | ||
| 11 | 11 | | `src/diag.pas` | 310 | 229 | Messages: positions, source excerpts, colour, counts | |
| 12 | 12 | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | |
| 13 | 13 | | `src/sym.pas` | 615 | 510 | Types, objects, scopes, layout | |
| 14 | -| `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes | | |
| 14 | +| `src/chunk.pas` | 298 | 232 | The compiled program: chunks, fixups, line notes | | |
| 15 | 15 | | `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | 16 | | `src/gen.pas` | 1,332 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 17 | 17 | | `src/item.pas` | 1,342 | 1,118 | Items: types, constants, variables, operators, conversions, calls | |
| 18 | -| `src/parse.pas` | 2,712 | 2,407 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | -| `src/link.pas` | 234 | 180 | Tree shaking, layout, patching, the places | | |
| 18 | +| `src/parse.pas` | 2,771 | 2,459 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 19 | +| `src/link.pas` | 243 | 188 | Tree shaking, layout, patching, the places | | |
| 20 | 20 | | `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers | |
| 21 | 21 | | `src/raz.pas` | 197 | 147 | The command line | |
| 22 | 22 | |
| @@ -219,7 +219,10 @@ | ||
| 219 | 219 | |
| 220 | 220 | A *fixup* (`TFixup`) is a 32-bit field in the code that will hold the |
| 221 | 221 | distance to another chunk: a call's target, or a rip-relative address. |
| 222 | -`AddFixup` records one; `link.pas` fills them in. `NoteLine` records | |
| 222 | +`AddFixup` records one; `link.pas` fills them in. A constant's | |
| 223 | +addresses are *data fixups*, `AddDataFixup`'s: 64-bit fields in | |
| 224 | +rodata that will hold a chunk's address itself, since the executable is | |
| 225 | +loaded where the linker placed it. `NoteLine` records | |
| 223 | 226 | which line and column of the source the code from `pc` on belongs to, |
| 224 | 227 | for gdb and for the places. `ExternChunk` makes, once per C symbol, |
| 225 | 228 | the quadword the dynamic loader will fill with that symbol's address. |
| @@ -380,9 +383,11 @@ | ||
| 380 | 383 | variable into an item for part of it, mostly by adding to `off`. An |
| 381 | 384 | item for another module's exported variable, or part of it, is `ro`: |
| 382 | 385 | it isn't a variable (`isVar`), and `NotReadOnly` says why where one is |
| 383 | -wanted; what it points at is. An array constant is a variable too, in | |
| 384 | -read-only data: `ArrayConst` (`parse.pas`) reads its literal into the | |
| 385 | -bytes of a chunk, as a string constant's are, and `MakeItem` makes it | |
| 386 | +wanted; what it points at is. An array or record constant is a | |
| 387 | +variable too, in read-only data: `AggregateConst` (`parse.pas`) reads | |
| 388 | +its literal, part by part in `ConstPart`, into the bytes of a chunk, | |
| 389 | +as a string constant's are; an address in it, of a string, an array | |
| 390 | +constant, a procedure or an `ERROR`, becomes a data fixup. `MakeItem` makes it | |
| 386 | 391 | an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| 387 | 392 | passed, never changed. `Index` and `Slice` have gen make the bounds |
| 388 | 393 | checks, unless the module says `check off;` (`checks`, which |
| @@ -505,11 +510,12 @@ | ||
| 505 | 510 | ## link.pas: tree shaking and layout |
| 506 | 511 | |
| 507 | 512 | `MarkLive` starts from the entry chunk and follows fixups, marking each |
| 508 | -chunk it reaches; nothing else goes into the executable. `LinkAll` | |
| 513 | +chunk it reaches, through the data fixups of constants too; nothing | |
| 514 | +else goes into the executable. `LinkAll` | |
| 509 | 515 | places the live chunks section by section (`Place`), each at its |
| 510 | 516 | alignment, then copies their bytes into the images of text and rodata, |
| 511 | 517 | patching every fixup with the distance from the end of its field to its |
| 512 | -target. | |
| 518 | +target, and every data fixup with its target's address. | |
| 513 | 519 | |
| 514 | 520 | Between placing text and placing rodata, `MakePlaces` writes the |
| 515 | 521 | places, a rodata chunk: for each line note in live code, its address, |
| @@ -1,11 +1,23 @@ | ||
| 1 | 1 | (* Operations on strings, which are []U8. = and # compare them; this |
| 2 | 2 | module orders, searches and hashes them, reads and writes their |
| 3 | - UTF-8, and reads a number from one. *) | |
| 3 | + UTF-8, reads a number from one, and copies one to keep. *) | |
| 4 | 4 | module Str; |
| 5 | -import Rt; | |
| 5 | +import Mem, Heap, Rt; | |
| 6 | 6 | |
| 7 | 7 | const least = -9223372036854775807 - 1; (* the smallest I64 *) |
| 8 | 8 | |
| 9 | +(* s's bytes, copied into memory from a, to keep after s's own are | |
| 10 | + gone; "" and false if a has no room. *) | |
| 11 | +proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); | |
| 12 | +var p: ^U8; i: INT; | |
| 13 | +begin | |
| 14 | + if LEN(s) = 0 then return "", true; end; | |
| 15 | + p := Mem.alloc(a, LEN(s), 1); | |
| 16 | + if p = nil then return "", false; end; | |
| 17 | + for i := 0 to LEN(s) - 1 do p[i] := s[i]; end; | |
| 18 | + return p[0..LEN(s)], true; | |
| 19 | +end; | |
| 20 | + | |
| 9 | 21 | (* -1, 0 or 1 as a sorts before, with or after b, byte by byte. *) |
| 10 | 22 | proc cmp*(a, b: []U8): INT; |
| 11 | 23 | var i, n: INT; |
| @@ -258,7 +270,12 @@ | ||
| 258 | 270 | |
| 259 | 271 | (* The module's test: stops the program if something is wrong. *) |
| 260 | 272 | proc main*; |
| 273 | +var buf: [3]U8; t: []U8; ok: BOOL; | |
| 261 | 274 | begin |
| 275 | + buf := "abc"; | |
| 276 | + t, ok := copy(Heap.heap, buf); | |
| 277 | + buf[0] := 'x'; | |
| 278 | + if not ok or (t # "abc") then Rt.stop("Str.copy"); end; | |
| 262 | 279 | if (cmp("a", "b") # -1) or (cmp("b", "a") # 1) or (cmp("ab", "ab") # 0) |
| 263 | 280 | or (cmp("a", "ab") # -1) or (cmp("", "") # 0) then Rt.stop("Str.cmp"); end; |
| 264 | 281 | if not eq("x", "x") or eq("x", "y") then Rt.stop("Str.eq"); end; |
| @@ -28,7 +28,7 @@ | ||
| 28 | 28 | TChunk = record |
| 29 | 29 | sec: TSection; |
| 30 | 30 | start, size, align: longint; { in its section's buffer } |
| 31 | - firstFix, numFix: longint; { its fixups, text chunks only } | |
| 31 | + firstFix, numFix: longint; { its fixups: text's in fixups, rodata's in dataFixups } | |
| 32 | 32 | module, name: longint; { for the symbol table; -1 if none } |
| 33 | 33 | adr: int64; { its address, set by the linker } |
| 34 | 34 | live: boolean; { reached from the entry point } |
| @@ -53,7 +53,8 @@ | ||
| 53 | 53 | end; |
| 54 | 54 | |
| 55 | 55 | TFixup = record |
| 56 | - at: longint; { the 32-bit field, as an offset in text } | |
| 56 | + at: longint; { the 32-bit field, as an offset in text; in | |
| 57 | + dataFixups, the 64-bit one, from its chunk's start } | |
| 57 | 58 | chunk: longint; { the chunk it refers to } |
| 58 | 59 | addend: longint; { added to the chunk's address } |
| 59 | 60 | end; |
| @@ -74,6 +75,11 @@ | ||
| 74 | 75 | numChunks: longint; |
| 75 | 76 | fixups: PFixups; |
| 76 | 77 | numFixups: longint; |
| 78 | + { Addresses in read-only data, as in an array constant of strings: | |
| 79 | + each 8 bytes, the chunk's absolute address, since the executable | |
| 80 | + is loaded where the linker placed it } | |
| 81 | + dataFixups: PFixups; | |
| 82 | + numDataFixups: longint; | |
| 77 | 83 | lines: PLines; |
| 78 | 84 | numLines: longint; |
| 79 | 85 | curFile: longint; { the file being compiled, for NoteLine } |
| @@ -89,6 +95,7 @@ | ||
| 89 | 95 | function ExternChunk(name: longint): longint; |
| 90 | 96 | procedure NoteLine(line, col: longint); |
| 91 | 97 | procedure AddFixup(at, chunk, addend: longint); |
| 98 | +procedure AddDataFixup(c, at, chunk, addend: longint); | |
| 92 | 99 | procedure NoteVar(chunk, name, off, kind: longint); |
| 93 | 100 | function ChunkName(c: longint): shortstring; |
| 94 | 101 | |
| @@ -181,6 +188,8 @@ | ||
| 181 | 188 | rodata := ReAllocMem(rodata, roCap); |
| 182 | 189 | end; |
| 183 | 190 | Move(p^, rodata^[rolen], n); |
| 191 | + chunks^[c].firstFix := numDataFixups; | |
| 192 | + chunks^[c].numFix := 0; | |
| 184 | 193 | chunks^[c].start := rolen; |
| 185 | 194 | chunks^[c].size := n; |
| 186 | 195 | chunks^[c].align := align; |
| @@ -235,6 +244,19 @@ | ||
| 235 | 244 | fixups^[numFixups].chunk := chunk; |
| 236 | 245 | fixups^[numFixups].addend := addend; |
| 237 | 246 | inc(numFixups); |
| 247 | +end; | |
| 248 | + | |
| 249 | +{ The address of chunk, plus addend, at offset at of rodata chunk c, | |
| 250 | + whose data is set: its fixups follow one another. } | |
| 251 | +procedure AddDataFixup(c, at, chunk, addend: longint); | |
| 252 | +begin | |
| 253 | + if numDataFixups and 255 = 0 then | |
| 254 | + dataFixups := ReAllocMem(dataFixups, (numDataFixups + 256) * SizeOf(TFixup)); | |
| 255 | + dataFixups^[numDataFixups].at := at; | |
| 256 | + dataFixups^[numDataFixups].chunk := chunk; | |
| 257 | + dataFixups^[numDataFixups].addend := addend; | |
| 258 | + inc(numDataFixups); | |
| 259 | + inc(chunks^[c].numFix); | |
| 238 | 260 | end; |
| 239 | 261 | |
| 240 | 262 | procedure NoteVar(chunk, name, off, kind: longint); |
| @@ -230,8 +230,8 @@ | ||
| 230 | 230 | oConst: |
| 231 | 231 | if o^.typ = tStr then begin |
| 232 | 232 | x.mode := mStr; x.val := o^.val; x.chunk := o^.chunk |
| 233 | - end else if o^.typ^.form = fArray then begin | |
| 234 | - { an array constant: a variable in read-only data, not one of | |
| 233 | + end else if o^.typ^.form in [fArray, fRecord] then begin | |
| 234 | + { an array or record constant: a variable in read-only data, not one of | |
| 235 | 235 | the program's, so nothing can change it } |
| 236 | 236 | x.mode := mVar; x.base := bGlobal; x.chunk := o^.chunk |
| 237 | 237 | end else begin |
| @@ -42,7 +42,7 @@ | ||
| 42 | 42 | |
| 43 | 43 | { Marks live each chunk reached from entry through the fixups. } |
| 44 | 44 | procedure MarkLive(entry: longint); |
| 45 | -var stack: PLongs; sp, c, f, t: longint; | |
| 45 | +var stack: PLongs; sp, c, f, t: longint; fx: PFixups; | |
| 46 | 46 | begin |
| 47 | 47 | stack := GetMem(numChunks * 4); |
| 48 | 48 | sp := 0; |
| @@ -53,10 +53,15 @@ | ||
| 53 | 53 | dec(sp); |
| 54 | 54 | c := stack^[sp]; |
| 55 | 55 | if chunks^[c].size < 0 then Fatal(ChunkName(c) + ' is declared but never defined'); |
| 56 | - { only code has fixups: data holds no addresses, so it uses nothing } | |
| 57 | - if chunks^[c].sec <> secText then continue; | |
| 56 | + { code and constants have fixups; variables start at zero, holding | |
| 57 | + no addresses } | |
| 58 | + case chunks^[c].sec of | |
| 59 | + secText: fx := fixups; | |
| 60 | + secRodata: fx := dataFixups; | |
| 61 | + else continue; | |
| 62 | + end; | |
| 58 | 63 | for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin |
| 59 | - t := fixups^[f].chunk; | |
| 64 | + t := fx^[f].chunk; | |
| 60 | 65 | if not chunks^[t].live then begin |
| 61 | 66 | chunks^[t].live := true; |
| 62 | 67 | stack^[sp] := t; |
| @@ -225,9 +230,13 @@ | ||
| 225 | 230 | PLongint(@textImg^[p + at])^ := longint(v); |
| 226 | 231 | end; |
| 227 | 232 | end; |
| 228 | - secRodata: | |
| 229 | - Move(rodata^[chunks^[c].start], roImg^[chunks^[c].adr - (Base + roOff)], | |
| 230 | - chunks^[c].size); | |
| 233 | + secRodata: begin | |
| 234 | + p := chunks^[c].adr - (Base + roOff); | |
| 235 | + Move(rodata^[chunks^[c].start], roImg^[p], chunks^[c].size); | |
| 236 | + for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do | |
| 237 | + PInt64(@roImg^[p + dataFixups^[f].at])^ := | |
| 238 | + chunks^[dataFixups^[f].chunk].adr + dataFixups^[f].addend; | |
| 239 | + end; | |
| 231 | 240 | end; |
| 232 | 241 | end; |
| 233 | 242 |
| @@ -1918,39 +1918,100 @@ | ||
| 1918 | 1918 | Declare(o); |
| 1919 | 1919 | end; |
| 1920 | 1920 | |
| 1921 | -{ The value of n, an array constant of type t: an array literal of | |
| 1922 | - constants, which become the bytes of a read-only chunk, as a string | |
| 1923 | - constant's do (report section 4). The bytes are the compiler's own | |
| 1921 | +{ An array or record constant: a literal of constants, which become | |
| 1922 | + the bytes of a read-only chunk, as a string constant's do (report | |
| 1923 | + section 4). An address in it, of a string, an array constant, a | |
| 1924 | + procedure or an ERROR, waits in fixes until the chunk is made, then | |
| 1925 | + is a fixup the linker patches. The bytes are the compiler's own | |
| 1924 | 1926 | machine's, little-endian, as the target's are. } |
| 1925 | -function ArrayConst(t: PType; n: longint): longint; | |
| 1926 | -var et: PType; e: TItem; p: PBytes; k, es: int64; f: single; c: longint; start: TPos; | |
| 1927 | +var | |
| 1928 | + constBytes: PBytes; | |
| 1929 | + fixes: array[0..4095] of record off: int64; chunk: longint; end; | |
| 1930 | + numFixes: longint; | |
| 1931 | + | |
| 1932 | +procedure ConstFix(off: int64; chunk: longint); | |
| 1933 | +begin | |
| 1934 | + if numFixes > High(fixes) then MarkAt(TokPos, 'more than ' + IntStr(High(fixes) + 1) + ' addresses in one constant'); | |
| 1935 | + fixes[numFixes].off := off; | |
| 1936 | + fixes[numFixes].chunk := chunk; | |
| 1937 | + inc(numFixes); | |
| 1938 | +end; | |
| 1939 | + | |
| 1940 | +{ The part of type t at offset off of the constant } | |
| 1941 | +procedure ConstPart(t: PType; off: int64); | |
| 1942 | +var e: TItem; f: PObj; k: int64; r: single; start: TPos; | |
| 1927 | 1943 | begin |
| 1928 | - et := t^.base; | |
| 1929 | - es := et^.size; | |
| 1930 | 1944 | start := TokPos; |
| 1931 | - if tok <> sLbrak then Expected('an array literal'); | |
| 1932 | - Next; | |
| 1933 | - p := nil; k := 0; | |
| 1934 | - if tok <> sRbrak then | |
| 1935 | - repeat | |
| 1936 | - Expression(e, et); | |
| 1937 | - Coerce(e, et); | |
| 1938 | - if e.mode <> mConst then MarkAt(e.pos, 'expected a constant, found ' + What(e)); | |
| 1939 | - ReAllocMem(p, (k + 1) * es); | |
| 1940 | - if (et^.form = fReal) and (es = 4) then begin | |
| 1941 | - f := RealOf(e); | |
| 1942 | - Move(f, p^[k * es], 4); | |
| 1943 | - end else Move(e.val, p^[k * es], es); | |
| 1944 | - inc(k); | |
| 1945 | - if tok <> sComma then break; | |
| 1946 | - Next; | |
| 1947 | - until false; | |
| 1948 | - Expect(sRbrak); | |
| 1949 | - if k <> t^.len then MarkAt(SpanFrom(start), 'expected ' + IntStr(t^.len) + ' elements, found ' + IntStr(k)); | |
| 1945 | + if (t^.form = fArray) and (tok = sLbrak) then begin | |
| 1946 | + Next; | |
| 1947 | + k := 0; | |
| 1948 | + if tok <> sRbrak then | |
| 1949 | + repeat | |
| 1950 | + if k < t^.len then ConstPart(t^.base, off + k * t^.base^.size) | |
| 1951 | + else ConstPart(t^.base, off); | |
| 1952 | + inc(k); | |
| 1953 | + if tok <> sComma then break; | |
| 1954 | + Next; | |
| 1955 | + until false; | |
| 1956 | + Expect(sRbrak); | |
| 1957 | + if k <> t^.len then MarkAt(SpanFrom(start), 'expected ' + IntStr(t^.len) + ' elements, found ' + IntStr(k)); | |
| 1958 | + exit; | |
| 1959 | + end; | |
| 1960 | + if (t^.form = fRecord) and not t^.tuple and (tok = sLbrace) then begin | |
| 1961 | + Next; | |
| 1962 | + if tok <> sRbrace then | |
| 1963 | + repeat | |
| 1964 | + if tok <> sIdent then Expected('a field'); | |
| 1965 | + f := FieldOf(t); | |
| 1966 | + Next; | |
| 1967 | + Expect(sColon); | |
| 1968 | + ConstPart(f^.typ, off + f^.val); | |
| 1969 | + if tok <> sComma then break; | |
| 1970 | + Next; | |
| 1971 | + until false; | |
| 1972 | + Expect(sRbrace); | |
| 1973 | + exit; | |
| 1974 | + end; | |
| 1975 | + Expression(e, t); | |
| 1976 | + if (e.mode = mStr) and ((t^.form = fSlice) or (t^.form = fArray)) and (t^.base = tU8) then begin | |
| 1977 | + if t^.form = fSlice then begin | |
| 1978 | + ConstFix(off, e.chunk); | |
| 1979 | + Move(e.val, constBytes^[off + 8], 8); | |
| 1980 | + end else if e.val > t^.len then MarkAt(e.pos, 'the string is longer than ' + TypeName(t)) | |
| 1981 | + else Move(rodata^[chunks^[e.chunk].start], constBytes^[off], e.val); | |
| 1982 | + end else if t^.form = fSlice then begin | |
| 1983 | + if (e.mode <> mVar) or (e.obj = nil) or (e.obj^.cls <> oConst) or (e.typ^.form <> fArray) | |
| 1984 | + or (e.pos.b - e.pos.a <> length(NameOf(e.obj^.name))) or not Same(e.typ^.base, t^.base) then | |
| 1985 | + MarkAt(e.pos, 'a constant slice is a string or an array constant of its elements, found ' + What(e)); | |
| 1986 | + ConstFix(off, e.chunk); | |
| 1987 | + Move(e.typ^.len, constBytes^[off + 8], 8); | |
| 1988 | + end else if t^.form = fVariant then MarkAt(e.pos, 'a constant holds no variant') | |
| 1989 | + else if t^.form in [fArray, fRecord] then MarkAt(e.pos, 'expected a literal of ' + TypeName(t)) | |
| 1990 | + else begin | |
| 1991 | + Coerce(e, t); | |
| 1992 | + if (e.mode = mProc) and not e.obj^.ext then ConstFix(off, e.obj^.chunk) | |
| 1993 | + else if e.mode <> mConst then MarkAt(e.pos, 'expected a constant, found ' + What(e)) | |
| 1994 | + else if (t^.form = fError) and (e.val > 0) then ConstFix(off, e.val - 1) | |
| 1995 | + else if (t^.form = fPtr) and (e.val <> 0) then MarkAt(e.pos, 'a constant pointer is nil') | |
| 1996 | + else if (t^.form = fReal) and (t^.size = 4) then begin | |
| 1997 | + r := RealOf(e); | |
| 1998 | + Move(r, constBytes^[off], 4); | |
| 1999 | + end else Move(e.val, constBytes^[off], t^.size); | |
| 2000 | + end; | |
| 2001 | +end; | |
| 2002 | + | |
| 2003 | +{ The chunk of n, a constant of type t whose literal is next } | |
| 2004 | +function AggregateConst(t: PType; n: longint): longint; | |
| 2005 | +var c, i: longint; | |
| 2006 | +begin | |
| 2007 | + constBytes := AllocMem(t^.size + 8); | |
| 2008 | + numFixes := 0; | |
| 2009 | + ConstPart(t, 0); | |
| 1950 | 2010 | c := NewChunk(secRodata, curModule^.sname, n); |
| 1951 | - SetData(c, p, t^.size, t^.align); | |
| 1952 | - FreeMem(p); | |
| 1953 | - ArrayConst := c; | |
| 2011 | + SetData(c, constBytes, t^.size, t^.align); | |
| 2012 | + for i := 0 to numFixes - 1 do AddDataFixup(c, fixes[i].off, fixes[i].chunk, 0); | |
| 2013 | + FreeMem(constBytes); | |
| 2014 | + AggregateConst := c; | |
| 1954 | 2015 | end; |
| 1955 | 2016 | |
| 1956 | 2017 | procedure ConstDecl(top: boolean); |
| @@ -1963,15 +2024,13 @@ | ||
| 1963 | 2024 | Next; |
| 1964 | 2025 | start := TokPos; |
| 1965 | 2026 | t := ParseType; |
| 1966 | - if (t^.form = fArray) and not (t^.base^.form in [fInt, fReal, fBool, fEnum, fSet]) then | |
| 1967 | - MarkAt(SpanFrom(start), 'an array constant holds integers, reals, BOOLs, enumerations or sets, found ' + TypeName(t^.base)) | |
| 1968 | - else if (t <> tError) and (t^.form <> fArray) then | |
| 1969 | - MarkAt(SpanFrom(start), 'only an ERROR or an array constant has a type'); | |
| 2027 | + if (t <> tError) and not ((t^.form = fArray) or (t^.form = fRecord) and not t^.tuple) then | |
| 2028 | + MarkAt(SpanFrom(start), 'only an ERROR, an array or a record constant has a type'); | |
| 1970 | 2029 | end; |
| 1971 | 2030 | Expect(sEql); |
| 1972 | - if (t <> nil) and (t^.form = fArray) then begin | |
| 2031 | + if (t <> nil) and (t^.form in [fArray, fRecord]) then begin | |
| 1973 | 2032 | o := NewObj(oConst, n, t); |
| 1974 | - o^.chunk := ArrayConst(t, n); | |
| 2033 | + o^.chunk := AggregateConst(t, n); | |
| 1975 | 2034 | o^.pos := at; |
| 1976 | 2035 | o^.exported := exp and top; |
| 1977 | 2036 | Declare(o); |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 35 | 35 | 0.3.0-5-g38d29a8 five commits past it } |
| 36 | 36 | Version = {$I version.inc}; |
| 37 | - Report = '2026-10.15'; { the version of doc/raz.md it follows } | |
| 37 | + Report = '2026-10.16'; { the version of doc/raz.md it follows } | |
| 38 | 38 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 39 | 39 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 40 | 40 |
| @@ -1,7 +1,11 @@ | ||
| 1 | 1 | module Str; |
| 2 | 2 | Operations on strings, which are []U8. = and # compare them; this |
| 3 | 3 | module orders, searches and hashes them, reads and writes their |
| 4 | - UTF-8, and reads a number from one. | |
| 4 | + UTF-8, reads a number from one, and copies one to keep. | |
| 5 | + | |
| 6 | +proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); | |
| 7 | + s's bytes, copied into memory from a, to keep after s's own are | |
| 8 | + gone; "" and false if a has no room. | |
| 5 | 9 | |
| 6 | 10 | proc cmp*(a, b: []U8): INT; |
| 7 | 11 | -1, 0 or 1 as a sorts before, with or after b, byte by byte. |
| @@ -1,4 +1,4 @@ | ||
| 1 | -conform/err/ArrayConstElem.raz:3:10: error: an array constant holds integers, reals, BOOLs, enumerations or sets, found R | |
| 2 | - 3 | const P: [1]R = [{a: 1}]; | |
| 3 | - | ^^^^ | |
| 1 | +conform/err/ArrayConstElem.raz:3:18: error: a constant holds no variant | |
| 2 | + 3 | const P: [1]S = [a]; | |
| 3 | + | ^ | |
| 4 | 4 | raz: 1 error |
| @@ -1,4 +1,4 @@ | ||
| 1 | -conform/err/StmtErrType.raz:2:10: error: only an ERROR or an array constant has a type | |
| 1 | +conform/err/StmtErrType.raz:2:10: error: only an ERROR, an array or a record constant has a type | |
| 2 | 2 | 2 | const k: I64 = "a"; |
| 3 | 3 | | ^^^ |
| 4 | 4 | raz: 1 error |