raz / commit aa755916

commit
aa75591613e407537490a7a51405b560345845ee
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b847cb77
Comments: the review's gaps in src and lib

src: ELF numbers named in comments, section names found in ShNames
rather than hand-counted; ModRM's RBP/R13 and RSP/R12 cases and the
rip operand's end; Call's alignment pad and r11, EndProc, InstancesFor's
depth count, CompileFile, the for loop's layout, the two kinds of
deferred in parse.pas; hash constants, TBig's size, the real fold.
lib: docs for the bare exports; Out's buffer is Sys.outSize; Fmt's
rules where users see them; Sys.raz's isTerminal and yield as Linux's.
test/doc and compiler.md's table follow.

patch · log

29 files changed, +212 -84

doc/compiler.md (history) modified +9 -9

@@ -9,16 +9,16 @@
99 | File | Lines | Code | What |
1010 |---|---|---|---|
1111 | `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts |
12-| `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
13-| `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
12+| `src/scan.pas` | 855 | 722 | Symbols, literals, comments, positions |
13+| `src/sym.pas` | 602 | 497 | Types, objects, scopes, layout |
1414 | `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes |
15-| `src/x64.pas` | 1,031 | 870 | The instruction encoder, jumps within a procedure, the assembler |
16-| `src/gen.pas` | 1,321 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17-| `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls |
18-| `src/parse.pas` | 2,559 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings |
19-| `src/link.pas` | 228 | 180 | Tree shaking, layout, patching, the places |
20-| `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers |
21-| `src/raz.pas` | 195 | 147 | The command line |
15+| `src/x64.pas` | 1,039 | 870 | The instruction encoder, jumps within a procedure, the assembler |
16+| `src/gen.pas` | 1,332 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames |
17+| `src/item.pas` | 1,320 | 1,100 | Items: types, constants, variables, operators, conversions, calls |
18+| `src/parse.pas` | 2,578 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings |
19+| `src/link.pas` | 234 | 180 | Tree shaking, layout, patching, the places |
20+| `src/elf.pas` | 435 | 352 | Static and dynamic ELF, symbol table, line numbers |
21+| `src/raz.pas` | 197 | 147 | The command line |
2222
2323 Every file opens with a comment that says what it is, what it depends
2424 on and what it leaves out. Read those first; they are the map.

lib/Arena.raz (history) modified +2 -0

@@ -42,6 +42,8 @@
4242 ar^.used := 0;
4343 end;
4444
45+(* Gives the arena's block back to the system; what it handed out is
46+ gone with it. *)
4547 proc done*(ar: ^Arena);
4648 begin
4749 if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end;

lib/Buf.raz (history) modified +7 -0

@@ -9,6 +9,7 @@
99 len*, cap: INT;
1010 end;
1111
12+(* An empty buffer, which takes its memory from a. *)
1213 proc init*(b: ^Buf; a: ^Mem.Allocator);
1314 begin
1415 b^ := {a: a};
@@ -31,6 +32,8 @@
3132 return true;
3233 end;
3334
35+(* Adds the byte c at the end; if room for it can't be had, it is
36+ dropped and the buffer is as it was. *)
3437 proc add*(b: ^Buf; c: U8);
3538 begin
3639 if room(b, 1) then
@@ -39,6 +42,8 @@
3942 end;
4043 end;
4144
45+(* Adds the bytes of s at the end; if room for them can't be had, none
46+ is added. *)
4247 proc str*(b: ^Buf; s: []U8);
4348 var i: INT;
4449 begin
@@ -55,11 +60,13 @@
5560 return b^.data[0..b^.len];
5661 end;
5762
63+(* Empties the buffer, keeping its memory for what comes next. *)
5864 proc reset*(b: ^Buf);
5965 begin
6066 b^.len := 0;
6167 end;
6268
69+(* Gives the buffer's memory back, leaving it empty. *)
6370 proc done*(b: ^Buf);
6471 begin
6572 if b^.data # nil then Mem.free(b^.a, b^.data); end;

lib/Fmt.raz (history) modified +15 -11

@@ -1,18 +1,22 @@
11 (* Formatted output. Each {} in the format takes the next argument:
22 Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]);
3- {{ and }} stand for { and }. print writes to standard output,
4- write appends to a Buf. *)
3+ {{ and }} stand for { and }. A {} with no argument left prints
4+ nothing, and arguments left over are not printed. *)
55 module Fmt;
66 import Out, Buf, Arena, Rt;
77
88 type
9+ (* An argument, and how it is printed. A real has places digits
10+ after the point, at most 9, rounded to the nearest, half away
11+ from zero; from 1E15 on, it has the exponent that makes it less
12+ than 10, as 1.50e17; nan and inf are spelled so. *)
913 Arg* = variant
10- int(i: I64);
11- uint(u: U64);
12- hex(h: U64);
14+ int(i: I64); (* in decimal *)
15+ uint(u: U64); (* in decimal *)
16+ hex(h: U64); (* in hexadecimal, capitals, no 0x *)
1317 str(s: []U8);
14- char(c: U8);
15- bool(b: BOOL);
18+ char(c: U8); (* the one byte *)
19+ bool(b: BOOL); (* true or false *)
1620 real(r: F64; places: INT);
1721 end;
1822
@@ -36,10 +40,8 @@
3640 if b = nil then Out.str(s); else Buf.str(b, s); end;
3741 end;
3842
39-(* x with places digits after the point, at most 9, rounded to the
40- nearest; from 1E15 on, with the exponent that makes it less than 10,
41- as 1.50e17. The whole part and the fraction are taken apart, so that
42- each fits an I64. *)
43+(* x as Arg's real says. The whole part and the fraction are taken
44+ apart, so that each fits an I64: hence the exponent from 1E15. *)
4345 proc putReal(b: ^Buf.Buf; x: F64; places: INT);
4446 const zeros = "00000000";
4547 var d: [24]U8; at, i, e: INT; scale, whole, frac: I64;
@@ -115,11 +117,13 @@
115117 put(b, f[start..LEN(f)]);
116118 end;
117119
120+(* Writes f to standard output, each {} replaced by the next of args. *)
118121 proc print*(f: []U8; args: []Arg);
119122 begin
120123 format(nil, f, args);
121124 end;
122125
126+(* Appends f to b, each {} replaced by the next of args. *)
123127 proc write*(b: ^Buf.Buf; f: []U8; args: []Arg);
124128 begin
125129 format(b, f, args);

lib/Heap.raz (history) modified +4 -3

@@ -3,9 +3,9 @@
33 ones; a block bigger than that gets pages of its own. Every block
44 has a 16-byte header before it: its size class, or minus the size
55 of its pages for a big one, which start less than a page before it;
6- then a marker, which says the block is out,
7- so that freeing it twice, or freeing what the heap never gave out,
8- stops the program. One lock keeps it whole across threads. *)
6+ then a marker, which says the block is out, so that freeing it
7+ twice, or freeing what the heap never gave out, stops the program.
8+ One lock keeps it whole across threads. *)
99 module Heap;
1010 import Mem, Lock, Rt;
1111
@@ -26,6 +26,7 @@
2626 left: INT;
2727 lock: Lock.Lock;
2828 std: Mem.Allocator;
29+ (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *)
2930 heap*: ^Mem.Allocator;
3031
3132 (* The block p, with size for its header, given out. *)

lib/List.raz (history) modified +2 -0

@@ -13,6 +13,7 @@
1313 a: ^Mem.Allocator;
1414 end;
1515
16+(* An empty list, its nodes from a. *)
1617 proc init*(l: ^List; a: ^Mem.Allocator);
1718 begin
1819 l^ := {a: a};
@@ -59,6 +60,7 @@
5960 l^.head := q;
6061 end;
6162
63+(* Takes every value off, giving the nodes back. *)
6264 proc clear*(l: ^List);
6365 var _ok: BOOL; _v: T;
6466 begin

lib/Lock.raz (history) modified +3 -0

@@ -5,8 +5,10 @@
55 module Lock;
66 import Sys, Rt;
77
8+(* A lock, free when zeroed. *)
89 type Lock* = record held: INT; end;
910
11+(* Takes l, waiting while another thread holds it. *)
1012 proc take*(l: ^Lock);
1113 var tries: INT;
1214 begin
@@ -17,6 +19,7 @@
1719 end;
1820 end;
1921
22+(* Gives up l, which the caller holds. *)
2023 proc give*(l: ^Lock);
2124 begin
2225 XCHG(ADR(l^.held), 0);

lib/Map.raz (history) modified +1 -0

@@ -17,6 +17,7 @@
1717 len*: INT;
1818 end;
1919
20+(* An empty map, its memory from a. *)
2021 proc init*(m: ^Map; a: ^Mem.Allocator);
2122 begin
2223 m^ := {a: a};

lib/Math.raz (history) modified +5 -1

@@ -10,7 +10,7 @@
1010 import Rt;
1111
1212 const
13- pi* = 3.141592653589793;
13+ pi* = 3.141592653589793; (* the F64 nearest to pi *)
1414 ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *)
1515 ln2lo = 1.90821492927058770002E-10;
1616
@@ -35,6 +35,7 @@
3535 return bits(x) shr 63 = 1;
3636 end;
3737
38+(* The square root of x, exact to the last bit; NaN for x below 0. *)
3839 proc sqrt*(x: F64): F64;
3940 code
4041 sqrtsd xmm0, xmm0;
@@ -230,6 +231,7 @@
230231 return w + (((1.0 - w) - hz) + (z * r - x * y));
231232 end;
232233
234+(* The sine of x, in radians; NaN for an infinity or NaN. *)
233235 proc sin*(x: F64): F64;
234236 var n: INT; y, yy: F64;
235237 begin
@@ -244,6 +246,7 @@
244246 return -kernelCos(y, yy);
245247 end;
246248
249+(* The cosine of x, in radians; NaN for an infinity or NaN. *)
247250 proc cos*(x: F64): F64;
248251 var n: INT; y, yy: F64;
249252 begin
@@ -320,6 +323,7 @@
320323 return d <= want * 4.5E-16;
321324 end;
322325
326+(* The module's test. *)
323327 proc main*;
324328 var inf, nan, hard: F64;
325329 begin

lib/Mem.raz (history) modified +8 -1

@@ -9,7 +9,9 @@
99 free*: proc(a: ^Allocator; p: ^U8);
1010 end;
1111
12-const Page* = 4096;
12+const
13+ (* The size of a page of memory, in bytes. *)
14+ Page* = 4096;
1315
1416 (* size bytes, rounded up to whole pages, zeroed; or nil. *)
1517 proc pages*(size: INT): ^U8;
@@ -17,16 +19,21 @@
1719 return Sys.mmap((size + Page - 1) div Page * Page);
1820 end;
1921
22+(* Gives back to the system the pages that pages gave as p, given the
23+ same size. *)
2024 proc unmap*(p: ^U8; size: INT);
2125 begin
2226 Sys.munmap(p, (size + Page - 1) div Page * Page);
2327 end;
2428
29+(* a^.alloc(a, size, align): size bytes from a, at a multiple of
30+ align; or nil. *)
2531 proc alloc*(a: ^Allocator; size, align: INT): ^U8;
2632 begin
2733 return a^.alloc(a, size, align);
2834 end;
2935
36+(* a^.free(a, p): gives p back to a, which gave it. *)
3037 proc free*(a: ^Allocator; p: ^U8);
3138 begin
3239 a^.free(a, p);

lib/Out.raz (history) modified +10 -5

@@ -1,9 +1,10 @@
11 (* Writing text and numbers to standard output, through a buffer of
2- 4 KB. The buffer is written out when it is full, at each line feed
3- when standard output is a terminal, by flush, and when the program
4- ends by Rt.exit. C's stdio keeps a buffer of its own: a program that
5- writes with both flushes one before writing with the other. A lock
6- keeps what each call writes together when threads write at once. *)
2+ Sys.outSize bytes. The buffer is written out when it is full, at
3+ each line feed when standard output is a terminal, by flush, and
4+ when the program ends by Rt.exit. C's stdio keeps a buffer of its
5+ own: a program that writes with both flushes one before writing
6+ with the other. A lock keeps what each call writes together when
7+ threads write at once. *)
78 module Out;
89 import Rt, Sys, Lock;
910
@@ -28,6 +29,7 @@
2829 n := 0;
2930 end;
3031
32+(* Writes out what the buffer holds. *)
3133 proc flush*;
3234 begin
3335 Lock.take(ADR(lock));
@@ -35,6 +37,7 @@
3537 Lock.give(ADR(lock));
3638 end;
3739
40+(* Writes the bytes of s. *)
3841 proc str*(s: []U8);
3942 var i: INT;
4043 begin
@@ -48,6 +51,7 @@
4851 Lock.give(ADR(lock));
4952 end;
5053
54+(* Writes the byte c. *)
5155 proc char*(c: U8);
5256 var b: [1]U8;
5357 begin
@@ -55,6 +59,7 @@
5559 str(b);
5660 end;
5761
62+(* Writes a line feed. *)
5863 proc ln*;
5964 begin
6065 char('\n');

lib/Rt.raz (history) modified +1 -1

@@ -14,7 +14,7 @@
1414 import Sys;
1515
1616 const
17- Full*: ERROR = "too many procedures to run at exit";
17+ Full*: ERROR = "too many procedures to run at exit"; (* from atExit *)
1818 MaxAtExit = 32;
1919 SigFpe = 8;
2020 SigSegv = 11;

lib/Sort.raz (history) modified +2 -1

@@ -21,7 +21,8 @@
2121 end;
2222 end;
2323
24-(* s made a heap, then its largest taken, in turn, to the end. *)
24+(* Sorts s in place, so that no element is less than the one before
25+ it. Not stable. *)
2526 proc sort*(s: []T);
2627 var i: INT; t: T;
2728 begin

lib/Str.raz (history) modified +2 -0

@@ -23,6 +23,7 @@
2323 return 0;
2424 end;
2525
26+(* Whether a and b hold the same bytes: a = b, as a procedure value. *)
2627 proc eq*(a, b: []U8): BOOL;
2728 begin
2829 return a = b;
@@ -49,6 +50,7 @@
4950 return -1;
5051 end;
5152
53+(* Whether s starts with p. *)
5254 proc hasPrefix*(s, p: []U8): BOOL;
5355 begin
5456 return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p);

lib/Sys.linux-x86_64.raz (history) modified +7 -4

@@ -3,9 +3,9 @@
33 module Sys;
44
55 const
6- stdin* = 0;
7- stdout* = 1;
8- stderr* = 2;
6+ stdin* = 0; (* standard input's file descriptor *)
7+ stdout* = 1; (* standard output's *)
8+ stderr* = 2; (* standard error's *)
99 (* the bytes Out keeps before it writes them; a target with little
1010 memory keeps fewer *)
1111 outSize* = 4096;
@@ -18,7 +18,7 @@
1818 (* What open is asked for: reading, writing or both, and what else
1919 it does to the file. *)
2020 Access* = (reading, writing, creating, exclusive, truncating, appending);
21- Accesses* = set of Access;
21+ Accesses* = set of Access; (* what open is given *)
2222
2323 (* What info says of a file. *)
2424 Info* = record
@@ -153,6 +153,7 @@
153153 return SYSCALL(8, fd, pos, 0); (* SEEK_SET *)
154154 end;
155155
156+(* Closes the file descriptor fd. *)
156157 proc close*(fd: INT);
157158 begin
158159 SYSCALL(3, fd);
@@ -168,6 +169,7 @@
168169 return ^U8(p);
169170 end;
170171
172+(* Gives back to the system the size bytes at p that mmap gave. *)
171173 proc munmap*(p: ^U8; size: INT);
172174 begin
173175 SYSCALL(11, p, size);
@@ -297,6 +299,7 @@
297299 return "", false;
298300 end;
299301
302+(* Closes the directory d that openDir opened. *)
300303 proc closeDir*(d: ^Dir);
301304 begin
302305 SYSCALL(3, d^.fd);

lib/Sys.raz (history) modified +9 -4

@@ -12,9 +12,9 @@
1212 import C;
1313
1414 const
15- stdin* = 0;
16- stdout* = 1;
17- stderr* = 2;
15+ stdin* = 0; (* standard input's file descriptor *)
16+ stdout* = 1; (* standard output's *)
17+ stderr* = 2; (* standard error's *)
1818 (* the bytes Out keeps before it writes them; a target with little
1919 memory keeps fewer *)
2020 outSize* = 4096;
@@ -31,7 +31,7 @@
3131 (* What open is asked for: reading, writing or both, and what else
3232 it does to the file. *)
3333 Access* = (reading, writing, creating, exclusive, truncating, appending);
34- Accesses* = set of Access;
34+ Accesses* = set of Access; (* what open is given *)
3535
3636 (* What info says of a file. *)
3737 Info* = record
@@ -186,6 +186,7 @@
186186 return r;
187187 end;
188188
189+(* Closes the file descriptor fd. *)
189190 proc close*(fd: INT);
190191 begin
191192 C.close(I32(fd));
@@ -201,6 +202,7 @@
201202 return p;
202203 end;
203204
205+(* Gives back to the system the size bytes at p that mmap gave. *)
204206 proc munmap*(p: ^U8; size: INT);
205207 begin
206208 C.munmap(p, UINT(size));
@@ -316,6 +318,7 @@
316318 return "", false;
317319 end;
318320
321+(* Closes the directory d that openDir opened. *)
319322 proc closeDir*(d: ^Dir);
320323 begin
321324 C.closedir(d^.d);
@@ -861,11 +864,13 @@
861864 return 0;
862865 end;
863866
867+(* Whether the file descriptor fd is a terminal. *)
864868 proc isTerminal*(fd: INT): BOOL;
865869 begin
866870 return C.isatty(I32(fd)) = 1;
867871 end;
868872
873+(* Gives the processor to another thread. *)
869874 proc yield*;
870875 begin
871876 C.sched_yield();

lib/Thread.linux-x86_64.raz (history) modified +8 -3

@@ -4,8 +4,8 @@
44 rather than spoiling what lies below; above it, a little stack for
55 Rt's signal handlers, so that they can say so. join waits on the
66 thread's id, which the kernel clears, and wakes it for, when the
7- thread ends; a Mutex sleeps on a futex. A program with C threads uses C's,
8- through pthread_create, and so can't use these. *)
7+ thread ends; a Mutex sleeps on a futex. A program with C threads
8+ uses C's, through pthread_create, and so can't use these. *)
99 module Thread;
1010 import Sys, Rt;
1111 link static;
@@ -23,6 +23,7 @@
2323 Private = 128;
2424
2525 type
26+ (* A thread, as spawn gives it, until join. *)
2627 Thread* = record
2728 tid: INT; (* the kernel's id, until the thread ends *)
2829 base: ^U8; (* its stack's mapping, the guard page first *)
@@ -32,7 +33,8 @@
3233 end;
3334 (* What sigaltstack takes *)
3435 AltStack = record sp: ^U8; flags, size: INT; end;
35- (* 0 free, 1 taken, 2 taken with a thread waiting *)
36+ (* A lock whose waiters sleep, free when zeroed. Its state is 0
37+ free, 1 taken, 2 taken with a thread waiting. *)
3638 Mutex* = record state: INT; end;
3739
3840 (* clone(flags, stack, tid, tid), the new thread starting with fn(arg)
@@ -120,6 +122,7 @@
120122 Sys.munmap(base, size);
121123 end;
122124
125+(* Takes m, sleeping while another thread holds it. *)
123126 proc lock*(m: ^Mutex);
124127 var c: INT;
125128 begin
@@ -131,6 +134,8 @@
131134 end;
132135 end;
133136
137+(* Gives up m, which the caller holds, waking a thread that waits
138+ for it. *)
134139 proc unlock*(m: ^Mutex);
135140 begin
136141 if XADD(ADR(m^.state), -1) # 1 then

src/elf.pas modified +32 -21

@@ -93,9 +93,17 @@
9393 PutN(align, 8);
9494 end;
9595
96-procedure Shdr(name, typ: longint; flags, adr, off, size: int64; lnk, info: longint; align, entsize: int64);
96+const
97+ { the section names, in .shstrtab; a section header names its own }
98+ ShNames = #0'.text'#0'.rodata'#0'.bss'#0'.symtab'#0'.strtab'#0'.shstrtab'#0 +
99+ '.dynamic'#0'.dynstr'#0'.dynsym'#0'.debug_line'#0'.debug_info'#0'.debug_abbrev'#0;
100+
101+{ A section header. Its name is given as a string and stored as its
102+ offset in ShNames: the #0 before it, counted from 1, is that offset
103+ counted from 0; the empty name, found nowhere, is 0. }
104+procedure Shdr(const name: shortstring; typ: longint; flags, adr, off, size: int64; lnk, info: longint; align, entsize: int64);
97105 begin
98- PutN(name, 4); PutN(typ, 4); PutN(flags, 8); PutN(adr, 8);
106+ PutN(Pos(#0 + name + #0, ShNames), 4); PutN(typ, 4); PutN(flags, 8); PutN(adr, 8);
99107 PutN(off, 8); PutN(size, 8); PutN(lnk, 4); PutN(info, 4);
100108 PutN(align, 8); PutN(entsize, 8);
101109 end;
@@ -301,9 +309,6 @@
301309 end;
302310
303311 procedure WriteElf(const path: shortstring; entry: longint);
304-const
305- ShNames = #0'.text'#0'.rodata'#0'.bss'#0'.symtab'#0'.strtab'#0'.shstrtab'#0 +
306- '.dynamic'#0'.dynstr'#0'.dynsym'#0'.debug_line'#0'.debug_info'#0'.debug_abbrev'#0;
307312 var
308313 nph, nsh, c, nsyms, fd: longint;
309314 roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: int64;
@@ -329,7 +334,8 @@
329334 end;
330335
331336 { ELF header }
332- PutN($464C457F, 4); PutN(2, 1); PutN(1, 1); PutN(1, 1); PutN(0, 9);
337+ PutN($464C457F, 4); { #127 'ELF' }
338+ PutN(2, 1); PutN(1, 1); PutN(1, 1); PutN(0, 9); { 64-bit, little-endian, version 1 }
333339 PutN(2, 2); { ET_EXEC }
334340 PutN(62, 2); { EM_X86_64 }
335341 PutN(1, 4);
@@ -340,8 +346,9 @@
340346 nsh := 10;
341347 if dynamic then nsh := 13;
342348 PutN(64, 2); PutN(56, 2); PutN(nph, 2);
343- PutN(64, 2); PutN(nsh, 2); PutN(6, 2);
349+ PutN(64, 2); PutN(nsh, 2); PutN(6, 2); { .shstrtab is section 6 }
344350
351+ { flags: 4 read, 5 read and run, 6 read and write }
345352 if dynamic then Phdr(PtInterp, 4, extraAdr - Base, extraAdr, length(Interp) + 1, length(Interp) + 1, 1);
346353 { the first segment maps the headers too, so text can follow them }
347354 Phdr(PtLoad, 5, 0, Base, textOff + textSize, textOff + textSize, Page);
@@ -372,7 +379,7 @@
372379 if strsLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strsLen * 2 + 256);
373380 Move(s[1], strs^[strsLen], length(s));
374381 strs^[strsLen + length(s)] := 0;
375- case chunks^[c].sec of
382+ case chunks^[c].sec of { the section's index in the headers below }
376383 secText: sec := 1;
377384 secRodata: sec := 2;
378385 else sec := 3;
@@ -396,21 +403,25 @@
396403 PadTo((len + 7) div 8 * 8);
397404 shOff := len;
398405 Patch(40, shOff, 8);
399- Shdr(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
400- Shdr(1, 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0);
401- Shdr(7, 1, 2, Base + roOff, roOff, roSize, 0, 0, 16, 0);
402- Shdr(15, 8, 3, bssAdr + dynSize, roOff + roSize, bssSize - dynSize, 0, 0, 16, 0);
403- Shdr(20, 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24);
404- Shdr(28, 3, 0, 0, strOff, strsLen, 0, 0, 1, 0);
405- Shdr(36, 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0);
406+ { types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM;
407+ flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's
408+ link is its string table's index: 5 .strtab, 8 .dynstr. The order
409+ here is the index the code above relies on. }
410+ Shdr('', 0, 0, 0, 0, 0, 0, 0, 0, 0);
411+ Shdr('.text', 1, 6, Base + textOff, textOff, textSize, 0, 0, 16, 0);
412+ Shdr('.rodata', 1, 2, Base + roOff, roOff, roSize, 0, 0, 16, 0);
413+ Shdr('.bss', 8, 3, bssAdr + dynSize, roOff + roSize, bssSize - dynSize, 0, 0, 16, 0);
414+ Shdr('.symtab', 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24);
415+ Shdr('.strtab', 3, 0, 0, strOff, strsLen, 0, 0, 1, 0);
416+ Shdr('.shstrtab', 3, 0, 0, shstrOff, length(ShNames), 0, 0, 1, 0);
406417 if dynamic then begin { the loader's tables, for readelf and objdump }
407- Shdr(46, 6, 3, bssAdr, bssAdr - Base, dynSize, 8, 0, 8, 16);
408- Shdr(55, 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0);
409- Shdr(63, 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24);
418+ Shdr('.dynamic', 6, 3, bssAdr, bssAdr - Base, dynSize, 8, 0, 8, 16);
419+ Shdr('.dynstr', 3, 2, extraAdr + strAt, extraAdr + strAt - Base, strLen, 0, 0, 1, 0);
420+ Shdr('.dynsym', 11, 2, extraAdr + symAt, extraAdr + symAt - Base, nsym * 24, 8, 1, 8, 24);
410421 end;
411- Shdr(71, 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0);
412- Shdr(83, 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0);
413- Shdr(95, 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0);
422+ Shdr('.debug_line', 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0);
423+ Shdr('.debug_info', 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0);
424+ Shdr('.debug_abbrev', 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0);
414425
415426 p := path + #0;
416427 fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755);

src/gen.pas modified +13 -2

@@ -10,8 +10,8 @@
1010 is a new gen.pas, with an encoder of its own.
1111
1212 The stack is kept in registers, as in Wirth's compilers: the value at
13- depth d in register d, of r8 to r11, a deeper one spilled to the frame
14- while its register is wanted. A constant is only noted until something
13+ depth d in r8 to r11, register d mod 4, a deeper one's register taken
14+ by spilling the shallower value it held to the frame. A constant is only noted until something
1515 needs it in a register, so that it can be an instruction's operand.
1616
1717 Calls use the System V convention. Arguments are computed left to
@@ -993,6 +993,9 @@
993993
994994 { Calls }
995995
996+{ The call c, its arguments already in their frame slots: they go to
997+ registers or the stack, the procedure is called, and its result, if
998+ any, is pushed on the value stack. }
996999 procedure Call(var c: TCall);
9971000 var
9981001 res: PType; i, ni, nf, k, pad, r: longint;
@@ -1014,6 +1017,8 @@
10141017 else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end;
10151018 if onStack[i] then inc(k);
10161019 end;
1020+ { System V wants rsp a multiple of 16 at the call: a quadword of
1021+ padding makes the count pushed since the frame began even }
10171022 pad := ord(odd(pushed + k));
10181023 if pad <> 0 then AluI(aluSub, RSP, 8);
10191024 for i := c.n - 1 downto 0 do
@@ -1032,6 +1037,8 @@
10321037 if c.ext then CallM(MemRip(c.chunk, 0))
10331038 else if c.chunk >= 0 then CallChunk(c.chunk)
10341039 else begin
1040+ { r11 is scratch: caller-saved, carrying no argument, and free,
1041+ as the value stack was spilled above }
10351042 Load(R11, Mem(RBP, c.target), 8, false);
10361043 CallR(R11);
10371044 end;
@@ -1194,6 +1201,8 @@
11941201 Jmp(exitL);
11951202 end;
11961203
1204+{ The end of the procedure in chunk c: the code that returns, and the
1205+ frame's size, known only now, patched into its prologue. }
11971206 procedure EndProc(c: longint);
11981207 var n: int64;
11991208 begin
@@ -1218,6 +1227,8 @@
12181227 if n < 16 then n := 16;
12191228 if n > high(longint) then Mark('frame too big');
12201229 Put4At(frameFix, n);
1230+ { the listing's text for the sub, which starts 3 bytes (REX.W, opcode,
1231+ ModRM) before its immediate }
12211232 Restate(frameFix - 3, 'sub rsp, ' + IntStr(n));
12221233 EndChunk(c);
12231234 end;

src/item.pas modified +2 -0

@@ -1028,6 +1028,8 @@
10281028 end else if (s^.form = fReal) and (t^.form = fInt) then begin
10291029 if x.mode = mConst then begin
10301030 r := RealOf(x);
1031+ { NaN or out of range folds to low(int64), the "integer
1032+ indefinite" cvttsd2si gives at run time }
10311033 if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then
10321034 x.val := Cut(low(int64), t)
10331035 else x.val := Cut(trunc(r), t);

src/link.pas modified +7 -1

@@ -40,6 +40,7 @@
4040
4141 implementation
4242
43+{ Marks live each chunk reached from entry through the fixups. }
4344 procedure MarkLive(entry: longint);
4445 var stack: PLongs; sp, c, f, t: longint;
4546 begin
@@ -52,6 +53,7 @@
5253 dec(sp);
5354 c := stack^[sp];
5455 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 }
5557 if chunks^[c].sec <> secText then continue;
5658 for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin
5759 t := fixups^[f].chunk;
@@ -154,6 +156,9 @@
154156 { the pool of paths and names starts at k }
155157 k := 40 + 16 * n + 20 * np + 12 * nv;
156158 names := k;
159+ { the paths first, at the offsets offs gave the places: Name puts
160+ each in the pool, and also writes its offset at at, which a path
161+ doesn't want, so at is put back for the procedures to overwrite }
157162 for f := 0 to nfiles - 1 do begin
158163 i := at;
159164 Name(ShownPath(NameOf(files^[f])));
@@ -188,7 +193,7 @@
188193 procedure LinkAll(headerSize, roExtra, dynSize: int64);
189194 var c, f, at: longint; e, p, v: int64;
190195 begin
191- textOff := (headerSize + 15) div 16 * 16;
196+ textOff := (headerSize + 15) div 16 * 16; { as each code chunk is }
192197 e := Place(secText, Base + textOff);
193198 textSize := e - (Base + textOff);
194199 if placesChunk >= 0 then MakePlaces(Base + textOff, e);
@@ -202,6 +207,7 @@
202207 bssSize := e - bssAdr;
203208
204209 textImg := GetMem(textSize + 1);
210+ { the gaps between code chunks are int3, a trap if ever run }
205211 FillChar(textImg^, textSize + 1, $CC);
206212 roImg := GetMem(roSize + 1);
207213 FillChar(roImg^, roSize + 1, 0);

src/parse.pas modified +23 -4

@@ -52,7 +52,8 @@
5252 base: longint; { the deferred statements in force outside it }
5353 end;
5454
55- { A generic module's import whose instance isn't made yet }
55+ { A generic module's import whose instance isn't made yet (deferred,
56+ numDeferred); not to be confused with a defer statement (defers) }
5657 TDeferred = record
5758 o: PObj; { the name it imports as }
5859 m: PModule; { the module that imports it }
@@ -72,9 +73,9 @@
7273 curProc: PObj; { the procedure being compiled, or nil }
7374 declExported: boolean; { the export mark of the declaration being read }
7475 curLoop: PLoop; { the innermost loop, or nil }
75- defers: array[0..63] of longint; { the deferred statements in force }
76+ defers: array[0..63] of longint; { the defer statements in force, their code's addresses }
7677 numDefers: longint;
77- deferring: boolean; { compiling a deferred statement }
78+ deferring: boolean; { compiling a defer statement's statement }
7879 directDefer: boolean; { ... as the statement itself, not in a list }
7980 deferSlot: int64; { keeps a result while deferred statements run }
8081 curResult: PType; { the result type of the procedure being compiled }
@@ -84,7 +85,7 @@
8485 declaring: array[0..63] of longint; { the names the declaration being read declares }
8586 numDeclaring: longint;
8687 skipped: boolean; { the last statement had an error and was skipped }
87- deferred: array[0..63] of TDeferred;
88+ deferred: array[0..63] of TDeferred; { generic imports not yet made, not defer statements }
8889 numDeferred: longint;
8990 making: boolean; { reading an instance's arguments, at its first use }
9091 stray: boolean; { an "end" in this procedure stood left of its statement's line }
@@ -1260,6 +1261,13 @@
12601261 Fix(lp.brk);
12611262 end;
12621263
1264+{ for v := a to b by s do ... end, laid out as
1265+ v := a; bslot := b
1266+ top: if v is past bslot then goto exit
1267+ the statements
1268+ if the distance from v to bslot < |s| then goto exit (ForNext)
1269+ v := v + s; goto top
1270+ exit: }
12631271 procedure ForStatement(const start: TPos; ind: longint);
12641272 var
12651273 o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL: longint;
@@ -2229,6 +2237,11 @@
22292237 Exists := (FpStat(@p[1], st) = 0) and ((st.st_mode and S_IFMT) = S_IFREG);
22302238 end;
22312239
2240+{ Compiles module n from path, an instance if args are given, or only
2241+ reads its interface if checkOnly; returns it. An import calls this in
2242+ the middle of reading another file, so it is re-entrant: it saves the
2243+ scanner's state, the open scopes, the current module and file and
2244+ whether checks are on, and puts them back at the end. }
22322245 function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule;
22332246 var
22342247 st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint;
@@ -2562,6 +2575,12 @@
25622575 i := numDeferred - 1;
25632576 while (i >= 0) and (deferred[i].m <> curModule) do dec(i);
25642577 if i < 0 then exit;
2578+ { The procedure ends at the "end" that closes its first "begin": d
2579+ counts the words still open that "end" or "until" closes, and begun
2580+ says that begin was met, so that a record's end in its declarations
2581+ doesn't stop it. A forward or code procedure has no raz body to
2582+ read, so it stops at that word. The body reads to the end of the
2583+ file, as wholeRest says. }
25652584 SaveScan(st);
25662585 d := 0; begun := false;
25672586 repeat

src/raz.pas modified +7 -5

@@ -7,10 +7,10 @@
77 Compiles the main module and every module it imports, writes the
88 entry point, links what it reaches, and writes the executable: by
99 default the main module's file without its .raz; with -S, it lists
10- the main module's code instead. raz run writes it
11- to a memfd instead, a file that lives only in memory, and executes
12- it in raz's place with the arguments after Main.raz, so its status
13- is the program's; its argv[0] is Main.raz.
10+ the main module's code instead. raz run writes it to a memfd
11+ instead, a file that lives only in memory, and executes it in raz's
12+ place with the arguments after Main.raz, so its status is the
13+ program's; its argv[0] is Main.raz.
1414
1515 The program starts by saving argc and argv, runs every module's body
1616 (those of the modules the program imports, imported modules first),
@@ -20,7 +20,9 @@
2020 C up and calls this as C's main; first it gives Rt C's exit, which
2121 Rt.exit then ends the program with.
2222
23- Depends on: everything else. }
23+ Depends on: everything else.
24+ Leaves out: the work itself; the other units compile, link and
25+ write, and this only reads the command line and runs them in order. }
2426
2527 program raz;
2628

src/scan.pas modified +4 -0

@@ -562,6 +562,10 @@
562562 n: longint; { words in use; 0 is zero }
563563 w: array[0..95] of longword; { least significant first }
564564 end;
565+ { 96 words, 3072 bits, are plenty: a shortstring holds at most 255
566+ digits, so ReadReal's den is at most 10^(330 + 255), about 2^1944,
567+ and num below 10^310; the shift that follows makes the larger one at
568+ most 54 bits longer than den, about 2000 bits in all. }
565569
566570 { a := a * m + add }
567571 procedure BigMul(var a: TBig; m, add: longword);

src/sym.pas modified +8 -2

@@ -253,11 +253,17 @@
253253 TopScope := scopes[depth - 1];
254254 end;
255255
256+{ Where the search for name in scope s starts in the hash table: each
257+ number is multiplied by an odd constant near 2^32 or 2^16 over the
258+ golden ratio (Knuth's multiplicative hashing), which spreads the low
259+ bits the mask keeps. }
256260 function Slot(s, name: longint): longint;
257261 begin
258262 Slot := longint((longword(s) * 2654435761 xor longword(name) * 40503) and longword(hashMask));
259263 end;
260264
265+{ Doubles the table, which Insert keeps at most half full so that the
266+ linear probing stays short, and puts every entry in its new slot. }
261267 procedure Rehash;
262268 var old: PObjTab; n, i, j: longint;
263269 begin
@@ -489,7 +495,7 @@
489495 declared in each file stands for the other's. }
490496 procedure Mix(var h: qword; v: int64);
491497 begin
492- h := (h xor qword(v)) * 1099511628211;
498+ h := (h xor qword(v)) * 1099511628211; { FNV-1a's 64-bit prime }
493499 end;
494500
495501 procedure Shape(var h: qword; t: PType; top: boolean);
@@ -521,7 +527,7 @@
521527 function Fingerprint(o: PObj): qword;
522528 var h: qword; f: PObj;
523529 begin
524- h := 14695981039346656037;
530+ h := 14695981039346656037; { FNV-1a's 64-bit offset basis }
525531 Mix(h, ord(o^.cls));
526532 if o^.cls = oCase then begin
527533 Mix(h, o^.val);

src/x64.pas modified +10 -2

@@ -193,16 +193,24 @@
193193 reg := reg and 7;
194194 if m.base = RIP then begin
195195 Put1(reg shl 3 or 5);
196- { the rip-relative displacement counts from the end of the
197- instruction, which these instructions end with }
196+ { mod 00 with r/m 101 is rip + disp32. The displacement counts
197+ from the end of the instruction, and the fixup takes it to end
198+ with these four bytes: no rip operand has an immediate after it,
199+ since only the compiler's loads, stores, lea and call make one
200+ (the assembler can't name a chunk). }
198201 AddFixup(pc, m.chunk, m.disp);
199202 Put4(0);
200203 exit;
201204 end;
202205 b := m.base and 7;
206+ { mod 00 takes no displacement, but with r/m 101 (RBP or R13) it means
207+ rip-relative, or disp32 under a SIB byte, so those bases take a
208+ zero disp8 }
203209 if (m.disp = 0) and (b <> RBP) then md := 0
204210 else if (m.disp >= -128) and (m.disp <= 127) then md := 1
205211 else md := 2;
212+ { r/m 100 (RSP or R12) means a SIB byte follows, so those bases need
213+ one, with index 100 meaning none }
206214 if (m.index <> NoIndex) or (b = RSP) then begin
207215 Put1(md shl 6 or reg shl 3 or 4);
208216 case m.scale of 1: sc := 0; 2: sc := 1; 4: sc := 2; else sc := 3; end;

test/doc/Str.expect (history) modified +2 -0

@@ -7,6 +7,7 @@
77 -1, 0 or 1 as a sorts before, with or after b, byte by byte.
88
99 proc eq*(a, b: []U8): BOOL;
10+ Whether a and b hold the same bytes: a = b, as a procedure value.
1011
1112 proc hash*(s: []U8): U64;
1213 FNV-1a, 64 bits.
@@ -15,6 +16,7 @@
1516 Where sub first starts in s, or -1.
1617
1718 proc hasPrefix*(s, p: []U8): BOOL;
19+ Whether s starts with p.
1820
1921 proc encode*(c: I32): ([4]U8, INT);
2022 c in UTF-8: its bytes, and how many. A number that isn't a

test/doc/Thread.expect (history) modified +8 -3

@@ -5,8 +5,8 @@
55 rather than spoiling what lies below; above it, a little stack for
66 Rt's signal handlers, so that they can say so. join waits on the
77 thread's id, which the kernel clears, and wakes it for, when the
8- thread ends; a Mutex sleeps on a futex. A program with C threads uses C's,
9- through pthread_create, and so can't use these.
8+ thread ends; a Mutex sleeps on a futex. A program with C threads
9+ uses C's, through pthread_create, and so can't use these.
1010
1111 const Failed*: ERROR = "can't start a thread";
1212
@@ -17,9 +17,11 @@
1717 fn: proc(arg: ^U8); (* what it runs, and with what *)
1818 arg: ^U8;
1919 end;
20+ A thread, as spawn gives it, until join.
2021
2122 type Mutex* = record state: INT; end;
22- 0 free, 1 taken, 2 taken with a thread waiting
23+ A lock whose waiters sleep, free when zeroed. Its state is 0
24+ free, 1 taken, 2 taken with a thread waiting.
2325
2426 proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR);
2527 Starts p(arg) in a new thread.
@@ -28,7 +30,10 @@
2830 Waits for t to end, then frees its stack.
2931
3032 proc lock*(m: ^Mutex);
33+ Takes m, sleeping while another thread holds it.
3134
3235 proc unlock*(m: ^Mutex);
36+ Gives up m, which the caller holds, waking a thread that waits
37+ for it.
3338
3439 proc main*;

test/run/ThreadOverflow.stderr (history) modified +1 -1

@@ -3,4 +3,4 @@
33 in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926
44 and 1926 more the same
55 in ThreadOverflow.work at test/run/ThreadOverflow.raz:15:5: _arg = nil
6- in Thread.run at lib/Thread.linux-x86_64.raz:78:5: arg = 0x?, t = 0x?
6+ in Thread.run at lib/Thread.linux-x86_64.raz:80:5: arg = 0x?, t = 0x?