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.
| @@ -9,16 +9,16 @@ | ||
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 11 | 11 | | `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 | | |
| 14 | 14 | | `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 | | |
| 22 | 22 | |
| 23 | 23 | Every file opens with a comment that says what it is, what it depends |
| 24 | 24 | on and what it leaves out. Read those first; they are the map. |
| @@ -42,6 +42,8 @@ | ||
| 42 | 42 | ar^.used := 0; |
| 43 | 43 | end; |
| 44 | 44 | |
| 45 | +(* Gives the arena's block back to the system; what it handed out is | |
| 46 | + gone with it. *) | |
| 45 | 47 | proc done*(ar: ^Arena); |
| 46 | 48 | begin |
| 47 | 49 | if ar^.buf # nil then Mem.unmap(ar^.buf, ar^.cap); end; |
| @@ -9,6 +9,7 @@ | ||
| 9 | 9 | len*, cap: INT; |
| 10 | 10 | end; |
| 11 | 11 | |
| 12 | +(* An empty buffer, which takes its memory from a. *) | |
| 12 | 13 | proc init*(b: ^Buf; a: ^Mem.Allocator); |
| 13 | 14 | begin |
| 14 | 15 | b^ := {a: a}; |
| @@ -31,6 +32,8 @@ | ||
| 31 | 32 | return true; |
| 32 | 33 | end; |
| 33 | 34 | |
| 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. *) | |
| 34 | 37 | proc add*(b: ^Buf; c: U8); |
| 35 | 38 | begin |
| 36 | 39 | if room(b, 1) then |
| @@ -39,6 +42,8 @@ | ||
| 39 | 42 | end; |
| 40 | 43 | end; |
| 41 | 44 | |
| 45 | +(* Adds the bytes of s at the end; if room for them can't be had, none | |
| 46 | + is added. *) | |
| 42 | 47 | proc str*(b: ^Buf; s: []U8); |
| 43 | 48 | var i: INT; |
| 44 | 49 | begin |
| @@ -55,11 +60,13 @@ | ||
| 55 | 60 | return b^.data[0..b^.len]; |
| 56 | 61 | end; |
| 57 | 62 | |
| 63 | +(* Empties the buffer, keeping its memory for what comes next. *) | |
| 58 | 64 | proc reset*(b: ^Buf); |
| 59 | 65 | begin |
| 60 | 66 | b^.len := 0; |
| 61 | 67 | end; |
| 62 | 68 | |
| 69 | +(* Gives the buffer's memory back, leaving it empty. *) | |
| 63 | 70 | proc done*(b: ^Buf); |
| 64 | 71 | begin |
| 65 | 72 | if b^.data # nil then Mem.free(b^.a, b^.data); end; |
| @@ -1,18 +1,22 @@ | ||
| 1 | 1 | (* Formatted output. Each {} in the format takes the next argument: |
| 2 | 2 | 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. *) | |
| 5 | 5 | module Fmt; |
| 6 | 6 | import Out, Buf, Arena, Rt; |
| 7 | 7 | |
| 8 | 8 | 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. *) | |
| 9 | 13 | 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 *) | |
| 13 | 17 | 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 *) | |
| 16 | 20 | real(r: F64; places: INT); |
| 17 | 21 | end; |
| 18 | 22 | |
| @@ -36,10 +40,8 @@ | ||
| 36 | 40 | if b = nil then Out.str(s); else Buf.str(b, s); end; |
| 37 | 41 | end; |
| 38 | 42 | |
| 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. *) | |
| 43 | 45 | proc putReal(b: ^Buf.Buf; x: F64; places: INT); |
| 44 | 46 | const zeros = "00000000"; |
| 45 | 47 | var d: [24]U8; at, i, e: INT; scale, whole, frac: I64; |
| @@ -115,11 +117,13 @@ | ||
| 115 | 117 | put(b, f[start..LEN(f)]); |
| 116 | 118 | end; |
| 117 | 119 | |
| 120 | +(* Writes f to standard output, each {} replaced by the next of args. *) | |
| 118 | 121 | proc print*(f: []U8; args: []Arg); |
| 119 | 122 | begin |
| 120 | 123 | format(nil, f, args); |
| 121 | 124 | end; |
| 122 | 125 | |
| 126 | +(* Appends f to b, each {} replaced by the next of args. *) | |
| 123 | 127 | proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); |
| 124 | 128 | begin |
| 125 | 129 | format(b, f, args); |
| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | ones; a block bigger than that gets pages of its own. Every block |
| 4 | 4 | has a 16-byte header before it: its size class, or minus the size |
| 5 | 5 | 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. *) | |
| 9 | 9 | module Heap; |
| 10 | 10 | import Mem, Lock, Rt; |
| 11 | 11 | |
| @@ -26,6 +26,7 @@ | ||
| 26 | 26 | left: INT; |
| 27 | 27 | lock: Lock.Lock; |
| 28 | 28 | std: Mem.Allocator; |
| 29 | + (* The heap, for NEW(p, Heap.heap) and Mem's procedures. *) | |
| 29 | 30 | heap*: ^Mem.Allocator; |
| 30 | 31 | |
| 31 | 32 | (* The block p, with size for its header, given out. *) |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | a: ^Mem.Allocator; |
| 14 | 14 | end; |
| 15 | 15 | |
| 16 | +(* An empty list, its nodes from a. *) | |
| 16 | 17 | proc init*(l: ^List; a: ^Mem.Allocator); |
| 17 | 18 | begin |
| 18 | 19 | l^ := {a: a}; |
| @@ -59,6 +60,7 @@ | ||
| 59 | 60 | l^.head := q; |
| 60 | 61 | end; |
| 61 | 62 | |
| 63 | +(* Takes every value off, giving the nodes back. *) | |
| 62 | 64 | proc clear*(l: ^List); |
| 63 | 65 | var _ok: BOOL; _v: T; |
| 64 | 66 | begin |
| @@ -5,8 +5,10 @@ | ||
| 5 | 5 | module Lock; |
| 6 | 6 | import Sys, Rt; |
| 7 | 7 | |
| 8 | +(* A lock, free when zeroed. *) | |
| 8 | 9 | type Lock* = record held: INT; end; |
| 9 | 10 | |
| 11 | +(* Takes l, waiting while another thread holds it. *) | |
| 10 | 12 | proc take*(l: ^Lock); |
| 11 | 13 | var tries: INT; |
| 12 | 14 | begin |
| @@ -17,6 +19,7 @@ | ||
| 17 | 19 | end; |
| 18 | 20 | end; |
| 19 | 21 | |
| 22 | +(* Gives up l, which the caller holds. *) | |
| 20 | 23 | proc give*(l: ^Lock); |
| 21 | 24 | begin |
| 22 | 25 | XCHG(ADR(l^.held), 0); |
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | len*: INT; |
| 18 | 18 | end; |
| 19 | 19 | |
| 20 | +(* An empty map, its memory from a. *) | |
| 20 | 21 | proc init*(m: ^Map; a: ^Mem.Allocator); |
| 21 | 22 | begin |
| 22 | 23 | m^ := {a: a}; |
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | import Rt; |
| 11 | 11 | |
| 12 | 12 | const |
| 13 | - pi* = 3.141592653589793; | |
| 13 | + pi* = 3.141592653589793; (* the F64 nearest to pi *) | |
| 14 | 14 | ln2hi = 6.93147180369123816490E-01; (* ln 2 in two parts, whose sum is more exact than one F64 *) |
| 15 | 15 | ln2lo = 1.90821492927058770002E-10; |
| 16 | 16 | |
| @@ -35,6 +35,7 @@ | ||
| 35 | 35 | return bits(x) shr 63 = 1; |
| 36 | 36 | end; |
| 37 | 37 | |
| 38 | +(* The square root of x, exact to the last bit; NaN for x below 0. *) | |
| 38 | 39 | proc sqrt*(x: F64): F64; |
| 39 | 40 | code |
| 40 | 41 | sqrtsd xmm0, xmm0; |
| @@ -230,6 +231,7 @@ | ||
| 230 | 231 | return w + (((1.0 - w) - hz) + (z * r - x * y)); |
| 231 | 232 | end; |
| 232 | 233 | |
| 234 | +(* The sine of x, in radians; NaN for an infinity or NaN. *) | |
| 233 | 235 | proc sin*(x: F64): F64; |
| 234 | 236 | var n: INT; y, yy: F64; |
| 235 | 237 | begin |
| @@ -244,6 +246,7 @@ | ||
| 244 | 246 | return -kernelCos(y, yy); |
| 245 | 247 | end; |
| 246 | 248 | |
| 249 | +(* The cosine of x, in radians; NaN for an infinity or NaN. *) | |
| 247 | 250 | proc cos*(x: F64): F64; |
| 248 | 251 | var n: INT; y, yy: F64; |
| 249 | 252 | begin |
| @@ -320,6 +323,7 @@ | ||
| 320 | 323 | return d <= want * 4.5E-16; |
| 321 | 324 | end; |
| 322 | 325 | |
| 326 | +(* The module's test. *) | |
| 323 | 327 | proc main*; |
| 324 | 328 | var inf, nan, hard: F64; |
| 325 | 329 | begin |
| @@ -9,7 +9,9 @@ | ||
| 9 | 9 | free*: proc(a: ^Allocator; p: ^U8); |
| 10 | 10 | end; |
| 11 | 11 | |
| 12 | -const Page* = 4096; | |
| 12 | +const | |
| 13 | + (* The size of a page of memory, in bytes. *) | |
| 14 | + Page* = 4096; | |
| 13 | 15 | |
| 14 | 16 | (* size bytes, rounded up to whole pages, zeroed; or nil. *) |
| 15 | 17 | proc pages*(size: INT): ^U8; |
| @@ -17,16 +19,21 @@ | ||
| 17 | 19 | return Sys.mmap((size + Page - 1) div Page * Page); |
| 18 | 20 | end; |
| 19 | 21 | |
| 22 | +(* Gives back to the system the pages that pages gave as p, given the | |
| 23 | + same size. *) | |
| 20 | 24 | proc unmap*(p: ^U8; size: INT); |
| 21 | 25 | begin |
| 22 | 26 | Sys.munmap(p, (size + Page - 1) div Page * Page); |
| 23 | 27 | end; |
| 24 | 28 | |
| 29 | +(* a^.alloc(a, size, align): size bytes from a, at a multiple of | |
| 30 | + align; or nil. *) | |
| 25 | 31 | proc alloc*(a: ^Allocator; size, align: INT): ^U8; |
| 26 | 32 | begin |
| 27 | 33 | return a^.alloc(a, size, align); |
| 28 | 34 | end; |
| 29 | 35 | |
| 36 | +(* a^.free(a, p): gives p back to a, which gave it. *) | |
| 30 | 37 | proc free*(a: ^Allocator; p: ^U8); |
| 31 | 38 | begin |
| 32 | 39 | a^.free(a, p); |
| @@ -1,9 +1,10 @@ | ||
| 1 | 1 | (* 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. *) | |
| 7 | 8 | module Out; |
| 8 | 9 | import Rt, Sys, Lock; |
| 9 | 10 | |
| @@ -28,6 +29,7 @@ | ||
| 28 | 29 | n := 0; |
| 29 | 30 | end; |
| 30 | 31 | |
| 32 | +(* Writes out what the buffer holds. *) | |
| 31 | 33 | proc flush*; |
| 32 | 34 | begin |
| 33 | 35 | Lock.take(ADR(lock)); |
| @@ -35,6 +37,7 @@ | ||
| 35 | 37 | Lock.give(ADR(lock)); |
| 36 | 38 | end; |
| 37 | 39 | |
| 40 | +(* Writes the bytes of s. *) | |
| 38 | 41 | proc str*(s: []U8); |
| 39 | 42 | var i: INT; |
| 40 | 43 | begin |
| @@ -48,6 +51,7 @@ | ||
| 48 | 51 | Lock.give(ADR(lock)); |
| 49 | 52 | end; |
| 50 | 53 | |
| 54 | +(* Writes the byte c. *) | |
| 51 | 55 | proc char*(c: U8); |
| 52 | 56 | var b: [1]U8; |
| 53 | 57 | begin |
| @@ -55,6 +59,7 @@ | ||
| 55 | 59 | str(b); |
| 56 | 60 | end; |
| 57 | 61 | |
| 62 | +(* Writes a line feed. *) | |
| 58 | 63 | proc ln*; |
| 59 | 64 | begin |
| 60 | 65 | char('\n'); |
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | import Sys; |
| 15 | 15 | |
| 16 | 16 | const |
| 17 | - Full*: ERROR = "too many procedures to run at exit"; | |
| 17 | + Full*: ERROR = "too many procedures to run at exit"; (* from atExit *) | |
| 18 | 18 | MaxAtExit = 32; |
| 19 | 19 | SigFpe = 8; |
| 20 | 20 | SigSegv = 11; |
| @@ -21,7 +21,8 @@ | ||
| 21 | 21 | end; |
| 22 | 22 | end; |
| 23 | 23 | |
| 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. *) | |
| 25 | 26 | proc sort*(s: []T); |
| 26 | 27 | var i: INT; t: T; |
| 27 | 28 | begin |
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | return 0; |
| 24 | 24 | end; |
| 25 | 25 | |
| 26 | +(* Whether a and b hold the same bytes: a = b, as a procedure value. *) | |
| 26 | 27 | proc eq*(a, b: []U8): BOOL; |
| 27 | 28 | begin |
| 28 | 29 | return a = b; |
| @@ -49,6 +50,7 @@ | ||
| 49 | 50 | return -1; |
| 50 | 51 | end; |
| 51 | 52 | |
| 53 | +(* Whether s starts with p. *) | |
| 52 | 54 | proc hasPrefix*(s, p: []U8): BOOL; |
| 53 | 55 | begin |
| 54 | 56 | return (LEN(s) >= LEN(p)) and (s[0..LEN(p)] = p); |
| @@ -3,9 +3,9 @@ | ||
| 3 | 3 | module Sys; |
| 4 | 4 | |
| 5 | 5 | 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 *) | |
| 9 | 9 | (* the bytes Out keeps before it writes them; a target with little |
| 10 | 10 | memory keeps fewer *) |
| 11 | 11 | outSize* = 4096; |
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | (* What open is asked for: reading, writing or both, and what else |
| 19 | 19 | it does to the file. *) |
| 20 | 20 | Access* = (reading, writing, creating, exclusive, truncating, appending); |
| 21 | - Accesses* = set of Access; | |
| 21 | + Accesses* = set of Access; (* what open is given *) | |
| 22 | 22 | |
| 23 | 23 | (* What info says of a file. *) |
| 24 | 24 | Info* = record |
| @@ -153,6 +153,7 @@ | ||
| 153 | 153 | return SYSCALL(8, fd, pos, 0); (* SEEK_SET *) |
| 154 | 154 | end; |
| 155 | 155 | |
| 156 | +(* Closes the file descriptor fd. *) | |
| 156 | 157 | proc close*(fd: INT); |
| 157 | 158 | begin |
| 158 | 159 | SYSCALL(3, fd); |
| @@ -168,6 +169,7 @@ | ||
| 168 | 169 | return ^U8(p); |
| 169 | 170 | end; |
| 170 | 171 | |
| 172 | +(* Gives back to the system the size bytes at p that mmap gave. *) | |
| 171 | 173 | proc munmap*(p: ^U8; size: INT); |
| 172 | 174 | begin |
| 173 | 175 | SYSCALL(11, p, size); |
| @@ -297,6 +299,7 @@ | ||
| 297 | 299 | return "", false; |
| 298 | 300 | end; |
| 299 | 301 | |
| 302 | +(* Closes the directory d that openDir opened. *) | |
| 300 | 303 | proc closeDir*(d: ^Dir); |
| 301 | 304 | begin |
| 302 | 305 | SYSCALL(3, d^.fd); |
| @@ -12,9 +12,9 @@ | ||
| 12 | 12 | import C; |
| 13 | 13 | |
| 14 | 14 | 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 *) | |
| 18 | 18 | (* the bytes Out keeps before it writes them; a target with little |
| 19 | 19 | memory keeps fewer *) |
| 20 | 20 | outSize* = 4096; |
| @@ -31,7 +31,7 @@ | ||
| 31 | 31 | (* What open is asked for: reading, writing or both, and what else |
| 32 | 32 | it does to the file. *) |
| 33 | 33 | Access* = (reading, writing, creating, exclusive, truncating, appending); |
| 34 | - Accesses* = set of Access; | |
| 34 | + Accesses* = set of Access; (* what open is given *) | |
| 35 | 35 | |
| 36 | 36 | (* What info says of a file. *) |
| 37 | 37 | Info* = record |
| @@ -186,6 +186,7 @@ | ||
| 186 | 186 | return r; |
| 187 | 187 | end; |
| 188 | 188 | |
| 189 | +(* Closes the file descriptor fd. *) | |
| 189 | 190 | proc close*(fd: INT); |
| 190 | 191 | begin |
| 191 | 192 | C.close(I32(fd)); |
| @@ -201,6 +202,7 @@ | ||
| 201 | 202 | return p; |
| 202 | 203 | end; |
| 203 | 204 | |
| 205 | +(* Gives back to the system the size bytes at p that mmap gave. *) | |
| 204 | 206 | proc munmap*(p: ^U8; size: INT); |
| 205 | 207 | begin |
| 206 | 208 | C.munmap(p, UINT(size)); |
| @@ -316,6 +318,7 @@ | ||
| 316 | 318 | return "", false; |
| 317 | 319 | end; |
| 318 | 320 | |
| 321 | +(* Closes the directory d that openDir opened. *) | |
| 319 | 322 | proc closeDir*(d: ^Dir); |
| 320 | 323 | begin |
| 321 | 324 | C.closedir(d^.d); |
| @@ -861,11 +864,13 @@ | ||
| 861 | 864 | return 0; |
| 862 | 865 | end; |
| 863 | 866 | |
| 867 | +(* Whether the file descriptor fd is a terminal. *) | |
| 864 | 868 | proc isTerminal*(fd: INT): BOOL; |
| 865 | 869 | begin |
| 866 | 870 | return C.isatty(I32(fd)) = 1; |
| 867 | 871 | end; |
| 868 | 872 | |
| 873 | +(* Gives the processor to another thread. *) | |
| 869 | 874 | proc yield*; |
| 870 | 875 | begin |
| 871 | 876 | C.sched_yield(); |
| @@ -4,8 +4,8 @@ | ||
| 4 | 4 | rather than spoiling what lies below; above it, a little stack for |
| 5 | 5 | Rt's signal handlers, so that they can say so. join waits on the |
| 6 | 6 | 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. *) | |
| 9 | 9 | module Thread; |
| 10 | 10 | import Sys, Rt; |
| 11 | 11 | link static; |
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | Private = 128; |
| 24 | 24 | |
| 25 | 25 | type |
| 26 | + (* A thread, as spawn gives it, until join. *) | |
| 26 | 27 | Thread* = record |
| 27 | 28 | tid: INT; (* the kernel's id, until the thread ends *) |
| 28 | 29 | base: ^U8; (* its stack's mapping, the guard page first *) |
| @@ -32,7 +33,8 @@ | ||
| 32 | 33 | end; |
| 33 | 34 | (* What sigaltstack takes *) |
| 34 | 35 | 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. *) | |
| 36 | 38 | Mutex* = record state: INT; end; |
| 37 | 39 | |
| 38 | 40 | (* clone(flags, stack, tid, tid), the new thread starting with fn(arg) |
| @@ -120,6 +122,7 @@ | ||
| 120 | 122 | Sys.munmap(base, size); |
| 121 | 123 | end; |
| 122 | 124 | |
| 125 | +(* Takes m, sleeping while another thread holds it. *) | |
| 123 | 126 | proc lock*(m: ^Mutex); |
| 124 | 127 | var c: INT; |
| 125 | 128 | begin |
| @@ -131,6 +134,8 @@ | ||
| 131 | 134 | end; |
| 132 | 135 | end; |
| 133 | 136 | |
| 137 | +(* Gives up m, which the caller holds, waking a thread that waits | |
| 138 | + for it. *) | |
| 134 | 139 | proc unlock*(m: ^Mutex); |
| 135 | 140 | begin |
| 136 | 141 | if XADD(ADR(m^.state), -1) # 1 then |
| @@ -93,9 +93,17 @@ | ||
| 93 | 93 | PutN(align, 8); |
| 94 | 94 | end; |
| 95 | 95 | |
| 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); | |
| 97 | 105 | 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); | |
| 99 | 107 | PutN(off, 8); PutN(size, 8); PutN(lnk, 4); PutN(info, 4); |
| 100 | 108 | PutN(align, 8); PutN(entsize, 8); |
| 101 | 109 | end; |
| @@ -301,9 +309,6 @@ | ||
| 301 | 309 | end; |
| 302 | 310 | |
| 303 | 311 | 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; | |
| 307 | 312 | var |
| 308 | 313 | nph, nsh, c, nsyms, fd: longint; |
| 309 | 314 | roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: int64; |
| @@ -329,7 +334,8 @@ | ||
| 329 | 334 | end; |
| 330 | 335 | |
| 331 | 336 | { 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 } | |
| 333 | 339 | PutN(2, 2); { ET_EXEC } |
| 334 | 340 | PutN(62, 2); { EM_X86_64 } |
| 335 | 341 | PutN(1, 4); |
| @@ -340,8 +346,9 @@ | ||
| 340 | 346 | nsh := 10; |
| 341 | 347 | if dynamic then nsh := 13; |
| 342 | 348 | 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 } | |
| 344 | 350 | |
| 351 | + { flags: 4 read, 5 read and run, 6 read and write } | |
| 345 | 352 | if dynamic then Phdr(PtInterp, 4, extraAdr - Base, extraAdr, length(Interp) + 1, length(Interp) + 1, 1); |
| 346 | 353 | { the first segment maps the headers too, so text can follow them } |
| 347 | 354 | Phdr(PtLoad, 5, 0, Base, textOff + textSize, textOff + textSize, Page); |
| @@ -372,7 +379,7 @@ | ||
| 372 | 379 | if strsLen + length(s) + 1 > MemSize(strs) then strs := ReAllocMem(strs, strsLen * 2 + 256); |
| 373 | 380 | Move(s[1], strs^[strsLen], length(s)); |
| 374 | 381 | 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 } | |
| 376 | 383 | secText: sec := 1; |
| 377 | 384 | secRodata: sec := 2; |
| 378 | 385 | else sec := 3; |
| @@ -396,21 +403,25 @@ | ||
| 396 | 403 | PadTo((len + 7) div 8 * 8); |
| 397 | 404 | shOff := len; |
| 398 | 405 | 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); | |
| 406 | 417 | 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); | |
| 410 | 421 | 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); | |
| 414 | 425 | |
| 415 | 426 | p := path + #0; |
| 416 | 427 | fd := FpOpen(@p[1], O_WRONLY or O_CREAT or O_TRUNC, &755); |
| @@ -10,8 +10,8 @@ | ||
| 10 | 10 | is a new gen.pas, with an encoder of its own. |
| 11 | 11 | |
| 12 | 12 | 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 | |
| 15 | 15 | needs it in a register, so that it can be an instruction's operand. |
| 16 | 16 | |
| 17 | 17 | Calls use the System V convention. Arguments are computed left to |
| @@ -993,6 +993,9 @@ | ||
| 993 | 993 | |
| 994 | 994 | { Calls } |
| 995 | 995 | |
| 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. } | |
| 996 | 999 | procedure Call(var c: TCall); |
| 997 | 1000 | var |
| 998 | 1001 | res: PType; i, ni, nf, k, pad, r: longint; |
| @@ -1014,6 +1017,8 @@ | ||
| 1014 | 1017 | else begin onStack[i] := ni >= 6; if ni < 6 then inc(ni) end; |
| 1015 | 1018 | if onStack[i] then inc(k); |
| 1016 | 1019 | 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 } | |
| 1017 | 1022 | pad := ord(odd(pushed + k)); |
| 1018 | 1023 | if pad <> 0 then AluI(aluSub, RSP, 8); |
| 1019 | 1024 | for i := c.n - 1 downto 0 do |
| @@ -1032,6 +1037,8 @@ | ||
| 1032 | 1037 | if c.ext then CallM(MemRip(c.chunk, 0)) |
| 1033 | 1038 | else if c.chunk >= 0 then CallChunk(c.chunk) |
| 1034 | 1039 | else begin |
| 1040 | + { r11 is scratch: caller-saved, carrying no argument, and free, | |
| 1041 | + as the value stack was spilled above } | |
| 1035 | 1042 | Load(R11, Mem(RBP, c.target), 8, false); |
| 1036 | 1043 | CallR(R11); |
| 1037 | 1044 | end; |
| @@ -1194,6 +1201,8 @@ | ||
| 1194 | 1201 | Jmp(exitL); |
| 1195 | 1202 | end; |
| 1196 | 1203 | |
| 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. } | |
| 1197 | 1206 | procedure EndProc(c: longint); |
| 1198 | 1207 | var n: int64; |
| 1199 | 1208 | begin |
| @@ -1218,6 +1227,8 @@ | ||
| 1218 | 1227 | if n < 16 then n := 16; |
| 1219 | 1228 | if n > high(longint) then Mark('frame too big'); |
| 1220 | 1229 | Put4At(frameFix, n); |
| 1230 | + { the listing's text for the sub, which starts 3 bytes (REX.W, opcode, | |
| 1231 | + ModRM) before its immediate } | |
| 1221 | 1232 | Restate(frameFix - 3, 'sub rsp, ' + IntStr(n)); |
| 1222 | 1233 | EndChunk(c); |
| 1223 | 1234 | end; |
| @@ -1028,6 +1028,8 @@ | ||
| 1028 | 1028 | end else if (s^.form = fReal) and (t^.form = fInt) then begin |
| 1029 | 1029 | if x.mode = mConst then begin |
| 1030 | 1030 | r := RealOf(x); |
| 1031 | + { NaN or out of range folds to low(int64), the "integer | |
| 1032 | + indefinite" cvttsd2si gives at run time } | |
| 1031 | 1033 | if (r <> r) or (r >= 9.2233720368547758e18) or (r < -9.2233720368547758e18) then |
| 1032 | 1034 | x.val := Cut(low(int64), t) |
| 1033 | 1035 | else x.val := Cut(trunc(r), t); |
| @@ -40,6 +40,7 @@ | ||
| 40 | 40 | |
| 41 | 41 | implementation |
| 42 | 42 | |
| 43 | +{ Marks live each chunk reached from entry through the fixups. } | |
| 43 | 44 | procedure MarkLive(entry: longint); |
| 44 | 45 | var stack: PLongs; sp, c, f, t: longint; |
| 45 | 46 | begin |
| @@ -52,6 +53,7 @@ | ||
| 52 | 53 | dec(sp); |
| 53 | 54 | c := stack^[sp]; |
| 54 | 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 } | |
| 55 | 57 | if chunks^[c].sec <> secText then continue; |
| 56 | 58 | for f := chunks^[c].firstFix to chunks^[c].firstFix + chunks^[c].numFix - 1 do begin |
| 57 | 59 | t := fixups^[f].chunk; |
| @@ -154,6 +156,9 @@ | ||
| 154 | 156 | { the pool of paths and names starts at k } |
| 155 | 157 | k := 40 + 16 * n + 20 * np + 12 * nv; |
| 156 | 158 | 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 } | |
| 157 | 162 | for f := 0 to nfiles - 1 do begin |
| 158 | 163 | i := at; |
| 159 | 164 | Name(ShownPath(NameOf(files^[f]))); |
| @@ -188,7 +193,7 @@ | ||
| 188 | 193 | procedure LinkAll(headerSize, roExtra, dynSize: int64); |
| 189 | 194 | var c, f, at: longint; e, p, v: int64; |
| 190 | 195 | begin |
| 191 | - textOff := (headerSize + 15) div 16 * 16; | |
| 196 | + textOff := (headerSize + 15) div 16 * 16; { as each code chunk is } | |
| 192 | 197 | e := Place(secText, Base + textOff); |
| 193 | 198 | textSize := e - (Base + textOff); |
| 194 | 199 | if placesChunk >= 0 then MakePlaces(Base + textOff, e); |
| @@ -202,6 +207,7 @@ | ||
| 202 | 207 | bssSize := e - bssAdr; |
| 203 | 208 | |
| 204 | 209 | textImg := GetMem(textSize + 1); |
| 210 | + { the gaps between code chunks are int3, a trap if ever run } | |
| 205 | 211 | FillChar(textImg^, textSize + 1, $CC); |
| 206 | 212 | roImg := GetMem(roSize + 1); |
| 207 | 213 | FillChar(roImg^, roSize + 1, 0); |
| @@ -52,7 +52,8 @@ | ||
| 52 | 52 | base: longint; { the deferred statements in force outside it } |
| 53 | 53 | end; |
| 54 | 54 | |
| 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) } | |
| 56 | 57 | TDeferred = record |
| 57 | 58 | o: PObj; { the name it imports as } |
| 58 | 59 | m: PModule; { the module that imports it } |
| @@ -72,9 +73,9 @@ | ||
| 72 | 73 | curProc: PObj; { the procedure being compiled, or nil } |
| 73 | 74 | declExported: boolean; { the export mark of the declaration being read } |
| 74 | 75 | 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 } | |
| 76 | 77 | numDefers: longint; |
| 77 | - deferring: boolean; { compiling a deferred statement } | |
| 78 | + deferring: boolean; { compiling a defer statement's statement } | |
| 78 | 79 | directDefer: boolean; { ... as the statement itself, not in a list } |
| 79 | 80 | deferSlot: int64; { keeps a result while deferred statements run } |
| 80 | 81 | curResult: PType; { the result type of the procedure being compiled } |
| @@ -84,7 +85,7 @@ | ||
| 84 | 85 | declaring: array[0..63] of longint; { the names the declaration being read declares } |
| 85 | 86 | numDeclaring: longint; |
| 86 | 87 | 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 } | |
| 88 | 89 | numDeferred: longint; |
| 89 | 90 | making: boolean; { reading an instance's arguments, at its first use } |
| 90 | 91 | stray: boolean; { an "end" in this procedure stood left of its statement's line } |
| @@ -1260,6 +1261,13 @@ | ||
| 1260 | 1261 | Fix(lp.brk); |
| 1261 | 1262 | end; |
| 1262 | 1263 | |
| 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: } | |
| 1263 | 1271 | procedure ForStatement(const start: TPos; ind: longint); |
| 1264 | 1272 | var |
| 1265 | 1273 | o: PObj; v, a, b, s: TItem; bslot, step: int64; top, exitL: longint; |
| @@ -2229,6 +2237,11 @@ | ||
| 2229 | 2237 | Exists := (FpStat(@p[1], st) = 0) and ((st.st_mode and S_IFMT) = S_IFREG); |
| 2230 | 2238 | end; |
| 2231 | 2239 | |
| 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. } | |
| 2232 | 2245 | function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; |
| 2233 | 2246 | var |
| 2234 | 2247 | st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; |
| @@ -2562,6 +2575,12 @@ | ||
| 2562 | 2575 | i := numDeferred - 1; |
| 2563 | 2576 | while (i >= 0) and (deferred[i].m <> curModule) do dec(i); |
| 2564 | 2577 | 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. } | |
| 2565 | 2584 | SaveScan(st); |
| 2566 | 2585 | d := 0; begun := false; |
| 2567 | 2586 | repeat |
| @@ -7,10 +7,10 @@ | ||
| 7 | 7 | Compiles the main module and every module it imports, writes the |
| 8 | 8 | entry point, links what it reaches, and writes the executable: by |
| 9 | 9 | 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. | |
| 14 | 14 | |
| 15 | 15 | The program starts by saving argc and argv, runs every module's body |
| 16 | 16 | (those of the modules the program imports, imported modules first), |
| @@ -20,7 +20,9 @@ | ||
| 20 | 20 | C up and calls this as C's main; first it gives Rt C's exit, which |
| 21 | 21 | Rt.exit then ends the program with. |
| 22 | 22 | |
| 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. } | |
| 24 | 26 | |
| 25 | 27 | program raz; |
| 26 | 28 |
| @@ -562,6 +562,10 @@ | ||
| 562 | 562 | n: longint; { words in use; 0 is zero } |
| 563 | 563 | w: array[0..95] of longword; { least significant first } |
| 564 | 564 | 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. } | |
| 565 | 569 | |
| 566 | 570 | { a := a * m + add } |
| 567 | 571 | procedure BigMul(var a: TBig; m, add: longword); |
| @@ -253,11 +253,17 @@ | ||
| 253 | 253 | TopScope := scopes[depth - 1]; |
| 254 | 254 | end; |
| 255 | 255 | |
| 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. } | |
| 256 | 260 | function Slot(s, name: longint): longint; |
| 257 | 261 | begin |
| 258 | 262 | Slot := longint((longword(s) * 2654435761 xor longword(name) * 40503) and longword(hashMask)); |
| 259 | 263 | end; |
| 260 | 264 | |
| 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. } | |
| 261 | 267 | procedure Rehash; |
| 262 | 268 | var old: PObjTab; n, i, j: longint; |
| 263 | 269 | begin |
| @@ -489,7 +495,7 @@ | ||
| 489 | 495 | declared in each file stands for the other's. } |
| 490 | 496 | procedure Mix(var h: qword; v: int64); |
| 491 | 497 | begin |
| 492 | - h := (h xor qword(v)) * 1099511628211; | |
| 498 | + h := (h xor qword(v)) * 1099511628211; { FNV-1a's 64-bit prime } | |
| 493 | 499 | end; |
| 494 | 500 | |
| 495 | 501 | procedure Shape(var h: qword; t: PType; top: boolean); |
| @@ -521,7 +527,7 @@ | ||
| 521 | 527 | function Fingerprint(o: PObj): qword; |
| 522 | 528 | var h: qword; f: PObj; |
| 523 | 529 | begin |
| 524 | - h := 14695981039346656037; | |
| 530 | + h := 14695981039346656037; { FNV-1a's 64-bit offset basis } | |
| 525 | 531 | Mix(h, ord(o^.cls)); |
| 526 | 532 | if o^.cls = oCase then begin |
| 527 | 533 | Mix(h, o^.val); |
| @@ -193,16 +193,24 @@ | ||
| 193 | 193 | reg := reg and 7; |
| 194 | 194 | if m.base = RIP then begin |
| 195 | 195 | 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). } | |
| 198 | 201 | AddFixup(pc, m.chunk, m.disp); |
| 199 | 202 | Put4(0); |
| 200 | 203 | exit; |
| 201 | 204 | end; |
| 202 | 205 | 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 } | |
| 203 | 209 | if (m.disp = 0) and (b <> RBP) then md := 0 |
| 204 | 210 | else if (m.disp >= -128) and (m.disp <= 127) then md := 1 |
| 205 | 211 | 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 } | |
| 206 | 214 | if (m.index <> NoIndex) or (b = RSP) then begin |
| 207 | 215 | Put1(md shl 6 or reg shl 3 or 4); |
| 208 | 216 | case m.scale of 1: sc := 0; 2: sc := 1; 4: sc := 2; else sc := 3; end; |
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | -1, 0 or 1 as a sorts before, with or after b, byte by byte. |
| 8 | 8 | |
| 9 | 9 | proc eq*(a, b: []U8): BOOL; |
| 10 | + Whether a and b hold the same bytes: a = b, as a procedure value. | |
| 10 | 11 | |
| 11 | 12 | proc hash*(s: []U8): U64; |
| 12 | 13 | FNV-1a, 64 bits. |
| @@ -15,6 +16,7 @@ | ||
| 15 | 16 | Where sub first starts in s, or -1. |
| 16 | 17 | |
| 17 | 18 | proc hasPrefix*(s, p: []U8): BOOL; |
| 19 | + Whether s starts with p. | |
| 18 | 20 | |
| 19 | 21 | proc encode*(c: I32): ([4]U8, INT); |
| 20 | 22 | c in UTF-8: its bytes, and how many. A number that isn't a |
| @@ -5,8 +5,8 @@ | ||
| 5 | 5 | rather than spoiling what lies below; above it, a little stack for |
| 6 | 6 | Rt's signal handlers, so that they can say so. join waits on the |
| 7 | 7 | 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. | |
| 10 | 10 | |
| 11 | 11 | const Failed*: ERROR = "can't start a thread"; |
| 12 | 12 | |
| @@ -17,9 +17,11 @@ | ||
| 17 | 17 | fn: proc(arg: ^U8); (* what it runs, and with what *) |
| 18 | 18 | arg: ^U8; |
| 19 | 19 | end; |
| 20 | + A thread, as spawn gives it, until join. | |
| 20 | 21 | |
| 21 | 22 | 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. | |
| 23 | 25 | |
| 24 | 26 | proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); |
| 25 | 27 | Starts p(arg) in a new thread. |
| @@ -28,7 +30,10 @@ | ||
| 28 | 30 | Waits for t to end, then frees its stack. |
| 29 | 31 | |
| 30 | 32 | proc lock*(m: ^Mutex); |
| 33 | + Takes m, sleeping while another thread holds it. | |
| 31 | 34 | |
| 32 | 35 | proc unlock*(m: ^Mutex); |
| 36 | + Gives up m, which the caller holds, waking a thread that waits | |
| 37 | + for it. | |
| 33 | 38 | |
| 34 | 39 | proc main*; |
| @@ -3,4 +3,4 @@ | ||
| 3 | 3 | in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926 |
| 4 | 4 | and 1926 more the same |
| 5 | 5 | 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? |