| | @@ -1,7 +1,9 @@ |
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | | -The compiler is eleven files of Free Pascal, under 10,000 lines of code. |
| 4 | | -This guide walks through them in the order they are best read: from the |
| 3 | +The compiler is eleven files of raz, about 10,500 lines of code (the |
| 4 | +budget is a soft 10,000, never past 11,000). It is its own compiler: the |
| 5 | +frozen Pascal compiler in `seed/` builds `src/` the first time, and |
| 6 | +after that `src/` builds itself. This guide walks through the files in the order they are best read: from the |
| 5 | 7 | characters of a source file to the bytes of an executable. It assumes |
| 6 | 8 | you know the language from `doc/raz.md`, and enough x86-64 to read a |
| 7 | 9 | disassembly. The manual pages say the rest: `raz(1)` the command, |
| | @@ -10,17 +12,17 @@ |
| 10 | 12 | |
| 11 | 13 | | File | Lines | Code | What | |
| 12 | 14 | |---|---|---|---| |
| 13 | | -| `seed/diag.pas` | 316 | 233 | Messages: positions, source excerpts, colour, counts | |
| 14 | | -| `seed/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | |
| 15 | | -| `seed/sym.pas` | 646 | 528 | Types, objects, scopes, layout | |
| 16 | | -| `seed/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | |
| 17 | | -| `seed/x64.pas` | 1,208 | 998 | The instruction encoder, jumps within a procedure, the assembler | |
| 18 | | -| `seed/gen.pas` | 2,322 | 1,670 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 19 | | -| `seed/item.pas` | 1,575 | 1,299 | Items: types, constants, variables, operators, conversions, calls | |
| 20 | | -| `seed/parse.pas` | 3,280 | 2,851 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 21 | | -| `seed/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | |
| 22 | | -| `seed/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | |
| 23 | | -| `seed/raz.pas` | 245 | 188 | The command line | |
| 15 | +| `src/Diag.raz` | 410 | 315 | Messages: positions, source excerpts, colour, counts | |
| 16 | +| `src/Scan.raz` | 1,002 | 842 | Symbols, literals, comments, positions | |
| 17 | +| `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout | |
| 18 | +| `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | |
| 19 | +| `src/X64.raz` | 1,472 | 1,222 | The instruction encoder, jumps within a procedure, the assembler | |
| 20 | +| `src/Gen.raz` | 2,350 | 1,693 | The stack machine, for x86-64: values, operations, checks, calls, frames | |
| 21 | +| `src/Item.raz` | 1,729 | 1,438 | Items: types, constants, variables, operators, conversions, calls | |
| 22 | +| `src/Parse.raz` | 3,775 | 3,298 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 23 | +| `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | |
| 24 | +| `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 25 | +| `src/Raz.raz` | 273 | 214 | The command line | |
| 24 | 26 | |
| 25 | 27 | Every file opens with a comment that says what it is, what it depends |
| 26 | 28 | on and what it leaves out. Read those first; they are the map. |
| | @@ -30,18 +32,21 @@ |
| 30 | 32 | There is one pass and no tree. The parser reads a construct and, in |
| 31 | 33 | the same breath, has the code generator emit its machine code. Nothing |
| 32 | 34 | is kept to be looked at again, except the declarations, which live in |
| 33 | | -the symbol table for as long as the compiler runs. |
| 35 | +the symbol table for as long as the compiler runs. An error is shown |
| 36 | +at once and returned as an `ERROR` (`Scan.Failed`), which each routine |
| 37 | +passes up with `try` to where the parser reads on, at the next |
| 38 | +statement or declaration. |
| 34 | 39 | |
| 35 | | -Three layers do it. `parse.pas` knows the language's syntax. `item.pas` |
| 40 | +Three layers do it. `Parse.raz` knows the language's syntax. `Item.raz` |
| 36 | 41 | knows what the program means: each operand is an *item*, as in Wirth's |
| 37 | 42 | compilers, and the routines there check the items' types, fold |
| 38 | 43 | constants and work out where variables are. Neither knows the machine: |
| 39 | | -they drive a stack machine, `gen.pas`, by its calls (push a value, |
| 44 | +they drive a stack machine, `Gen.raz`, by its calls (push a value, |
| 40 | 45 | add the two on top, store the top in a variable, jump if it is false), |
| 41 | 46 | each with the kind of the value (a word, a 64-bit integer, an `F32`, |
| 42 | | -an `F64`). `gen.pas` is the only one that knows x86-64; a port to |
| 43 | | -another machine is a new `gen.pas`, with an encoder of its own in place |
| 44 | | -of `x64.pas`. |
| 47 | +an `F64`). `Gen.raz` is the only one that knows x86-64; a port to |
| 48 | +another machine is a new `Gen.raz`, with an encoder of its own in place |
| 49 | +of `X64.raz`. |
| 45 | 50 | |
| 46 | 51 | On x86-64 the stack lives in registers, as in Wirth's compilers |
| 47 | 52 | (Niklaus Wirth's Pascal and Oberon compilers): the value at depth `d`, |
| | @@ -79,23 +84,23 @@ |
| 79 | 84 | |
| 80 | 85 | `raz Hello.raz` does this: |
| 81 | 86 | |
| 82 | | -1. `raz.pas` reads the command line and calls `CompileMain` |
| 83 | | - (`parse.pas`). |
| 84 | | -2. `CompileMain` imports `Rt` first, since every program needs it, and |
| 85 | | - then compiles `Hello.raz` with `CompileFile`. |
| 86 | | -3. `CompileFile` opens the file in the scanner and parses the module. |
| 87 | | - At `import Out;` it calls `ImportModule`, which finds `Out.raz` and |
| 88 | | - compiles it there and then, saving and restoring the scanner and the |
| 87 | +1. `Raz.raz` reads the command line and calls `compileMain` |
| 88 | + (`Parse.raz`). |
| 89 | +2. `compileMain` imports `Rt` first, since every program needs it, and |
| 90 | + then compiles `Hello.raz` with `compileFile`. |
| 91 | +3. `compileFile` opens the file in the scanner and parses the module. |
| 92 | + At `import Out;` it calls `importModule`, which finds `Out.raz` and |
| 93 | + compiles it there and then, saving and restoring the scanner (`Scan.saveScan`, `Scan.restoreScan`) and the |
| 89 | 94 | scopes around it. So modules are finished in an order where each |
| 90 | 95 | comes after those it imports; that is the order their bodies run. |
| 91 | 96 | 4. Each procedure becomes a chunk of code; `Out.str("hello, world\n")` |
| 92 | 97 | becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. |
| 93 | | -5. Back in `raz.pas`, `Use` marks the modules the program uses; the |
| 94 | | - entry point, `_start`, is written by `gen.pas`'s `EntryPoint` as one |
| 98 | +5. Back in `Raz.raz`, `use` marks the modules the program uses; the |
| 99 | + entry point, `_start`, is written by `Gen.raz`'s `entryPoint` as one |
| 95 | 100 | more chunk: save `argc` and `argv` and the address of the places |
| 96 | 101 | (below), call every used module's body, call `main`, call `Rt.exit`. |
| 97 | | -6. `MarkLive` (`link.pas`) marks what `_start` reaches; then |
| 98 | | - `WriteElf` (`elf.pas`) has `link.pas` place it and patch it, and |
| 102 | +6. `markLive` (`Link.raz`) marks what `_start` reaches; then |
| 103 | + `writeElf` (`Elf.raz`) has `Link.linkAll` place it and patch it, and |
| 99 | 104 | writes headers, code, data, symbols and line numbers. |
| 100 | 105 | |
| 101 | 106 | ## A procedure, compiled |
| | @@ -111,9 +116,9 @@ |
| 111 | 116 | under each line of source, named by the compiler as it made them): |
| 112 | 117 | |
| 113 | 118 | ``` |
| 114 | | -push rbp ; EnterBody: the frame |
| 119 | +push rbp ; enterBody: the frame |
| 115 | 120 | mov rbp,rsp |
| 116 | | -sub rsp,0x10 ; its size, patched in by EndProc |
| 121 | +sub rsp,0x10 ; its size, patched in by endProc |
| 117 | 122 | mov r9,rsi ; b, at depth 1; a, at depth 0, and |
| 118 | 123 | shl r9,0x1 ; the 2 are only noted; times 2 |
| 119 | 124 | mov r8,rdi ; is a shift; a is loaded for + |
| | @@ -124,13 +129,12 @@ |
| 124 | 129 | ``` |
| 125 | 130 | |
| 126 | 131 | `scale` calls nothing, so its parameters stay in `rdi` and `rsi`, |
| 127 | | -where they arrive (`KeepArrived`): they are register variables that |
| 132 | +where they arrive (`keepArrived`): they are register variables that |
| 128 | 133 | cost nothing to keep. Every line of it can be traced to a routine in |
| 129 | | -`gen.pas`: the moves are `Loaded`'s, the shift and the add `Op`'s, the |
| 130 | | -result `ReturnJump`'s, and the frame is `EnterBody`'s and `EndProc`'s. |
| 134 | +`Gen.raz`: the moves are `loaded`'s, the shift and the add `op`'s, the |
| 135 | +result `returnJump`'s, and the frame is `enterBody`'s and `endProc`'s. |
| 131 | 136 | It is not the fastest code. It is code whose every instruction you |
| 132 | 137 | can account for, which is the point. |
| 133 | | - |
| 134 | 138 | ## Better code, in one procedure |
| 135 | 139 | |
| 136 | 140 | `Str.decode` reads one UTF-8 character from a slice of bytes, and for |
| | @@ -246,18 +250,18 @@ |
| 246 | 250 | slots of the four variables, all of them in registers, and the slots |
| 247 | 251 | the registers are saved in, which are written before they are read. |
| 248 | 252 | Now a variable in a register has no slot, and only the locals in |
| 249 | | - memory are zeroed (`PlaceLocals`, `UseRegs`, `EnterBody`). |
| 253 | + memory are zeroed (`placeLocals`, `useRegs`, `enterBody`). |
| 250 | 254 | 2. **The hidden pointer**: the two results went through `rcx` to the |
| 251 | 255 | caller's memory. Two words come back in `rax` and `rdx` now |
| 252 | | - (`InTwoRegs`), so `return 0, 0` is two instructions; and the caller |
| 253 | | - pops them straight into its variables (`mWords`, `MultiAssign`). |
| 256 | + (`inTwoRegs`), so `return 0, 0` is two instructions; and the caller |
| 257 | + pops them straight into its variables (`pushWords`, `multiAssign`). |
| 254 | 258 | 3. **`cmp r8, 0`** is `test r8, r8`, the same flags in fewer bytes |
| 255 | | - (`CmpI`). |
| 259 | + (`cmpI`). |
| 256 | 260 | 4. **Copies through `rax`**: `s[0]`'s address is worked out in the |
| 257 | | - register it is used from (`IndexSlice`), and a result is put in `rax` |
| 258 | | - from where it is (`PopTo`). |
| 261 | + register it is used from (`indexSlice`), and a result is put in `rax` |
| 262 | + from where it is (`popTo`). |
| 259 | 263 | 5. **The `jmp 421` after `return`**, which nothing could reach: a branch |
| 260 | | - of an `if` that ends gets no jump past the others (`IfStatement`). |
| 264 | + of an `if` that ends gets no jump past the others (`ifStatement`). |
| 261 | 265 | |
| 262 | 266 | What is left is worth knowing too. The slice's two words still go to |
| 263 | 267 | the frame, since only single values live in registers; `movzx` then |
| | @@ -300,13 +304,13 @@ |
| 300 | 304 | add r9, r12 |
| 301 | 305 | ``` |
| 302 | 306 | |
| 303 | | - Its item is at `bReg`, and its `TAdr` names both registers (`hi` |
| 304 | | - for the length's): `PushVar` reads either word (`RegAt`), |
| 305 | | - `IndexSlice` and `Slice` compare with the length's and add the |
| 307 | + Its item is at `bReg`, and its `Adr` names both registers (`hi` |
| 308 | + for the length's): `pushVar` reads either word (`regAt`), |
| 309 | + `indexSlice` and `slice` compare with the length's and add the |
| 306 | 310 | pointer's, a failed check takes the length from its register |
| 307 | | - (`InRegister`), and `CopyTo` loads both. What wants a slice's |
| 311 | + (`inRegister`), and `copyTo` loads both. What wants a slice's |
| 308 | 312 | memory, a comparison or `COPY`, has its words stored in a |
| 309 | | - temporary first (`CopyToTemp`). rat 5% faster, the heap sort 5%, |
| 313 | + temporary first (`copyToTemp`). rat 5% faster, the heap sort 5%, |
| 310 | 314 | the search 4%. |
| 311 | 315 | |
| 312 | 316 | 2. **Parameters left where they arrive.** `find(s, sub: []U8)` stored |
| | @@ -321,12 +325,12 @@ |
| 321 | 325 | ``` |
| 322 | 326 | |
| 323 | 327 | In a procedure that names no procedure, a procedure variable, `NEW`, |
| 324 | | - `SYSCALL` or `COPY`, as `ChooseRegs` sees reading ahead, the |
| 328 | + `SYSCALL` or `COPY`, as `chooseRegs` sees reading ahead, the |
| 325 | 329 | parameters that arrive in `rdi` and `rsi` stay there, which only a |
| 326 | 330 | call or a copy needs: nothing is saved or moved, and `s[i]` is read |
| 327 | | - through `rdi` itself (`KeepArrived`). Where gen needs them after |
| 331 | + through `rdi` itself (`keepArrived`). Where Gen needs them after |
| 328 | 332 | all, to copy a record or compare two slices, it sets `lost`, and |
| 329 | | - `Body` compiles the procedure again with the parameters in memory, |
| 333 | + `body` compiles the procedure again with the parameters in memory, |
| 330 | 334 | as for a variable demoted. The trap report shows a parameter kept |
| 331 | 335 | so: `Rt.fail` is given `rdi` and `rsi` as the failing code had |
| 332 | 336 | them, and the signal handler takes them from the context the |
| | @@ -347,11 +351,11 @@ |
| 347 | 351 | ``` |
| 348 | 352 | |
| 349 | 353 | Now an element of a slice in registers is only noted, as the |
| 350 | | - operand `[rdi + r12 * 8]` (`lIdx`, made by `IndexSlice`), and a |
| 354 | + operand `[rdi + r12 * 8]` (`lIdx`, made by `indexSlice`), and a |
| 351 | 355 | variable in memory, or at an address noted, as the operand that |
| 352 | | - reads it (`lMem`, made by `PushVar`): each is read where it is |
| 353 | | - used, and `Op`, `Compare` and `JumpCmp` take an 8-byte one as their |
| 354 | | - right operand as it is (`TopInMem`): |
| 356 | + reads it (`lMem`, made by `pushVar`): each is read where it is |
| 357 | + used, and `op`, `compare` and `jumpCmp` take an 8-byte one as their |
| 358 | + right operand as it is (`topInMem`): |
| 355 | 359 | |
| 356 | 360 | ``` |
| 357 | 361 | mov r8, rbx |
| | @@ -359,8 +363,8 @@ |
| 359 | 363 | ``` |
| 360 | 364 | |
| 361 | 365 | A value noted in memory must be read before anything can change |
| 362 | | - it: before a call (`SpillBelow`) and an atomic built-in (`Atomic`), |
| 363 | | - both through `ReadMems`. The heap sort 14% faster, the search 5%; |
| 366 | + it: before a call (`spillBelow`) and an atomic built-in (`atomic`), |
| 367 | + both through `readMems`. The heap sort 14% faster, the search 5%; |
| 364 | 368 | rat within noise, its text 3.9% smaller. Loop tops aligned to 16 |
| 365 | 369 | bytes were tried with it, and taken out: the search lost 3%, and |
| 366 | 370 | rat gained nothing. |
| | @@ -376,9 +380,9 @@ |
| 376 | 380 | |
| 377 | 381 | and `i`'s step as well. An assignment whose expression starts with |
| 378 | 382 | its variable, in a register, and changes it by `+`, `-`, `|` or |
| 379 | | - `xor`, naming it nowhere else (`ChangesItself`, read ahead in the |
| 380 | | - statement), has gen work the value out in the variable's own |
| 381 | | - register (`inPlace`, `lInto`), and `ForStep` steps one so too: |
| 383 | + `xor`, naming it nowhere else (`changesItself`, read ahead in the |
| 384 | + statement), has Gen work the value out in the variable's own |
| 385 | + register (`inPlace`, `lInto`), and `Item.forStep` steps one so too: |
| 382 | 386 | |
| 383 | 387 | ``` |
| 384 | 388 | add r12, 1 |
| | @@ -389,7 +393,7 @@ |
| 389 | 393 | slow as before, for it crossed a 64-byte line that the processor |
| 390 | 394 | fetches decoded instructions in. The top of a `for` loop, which |
| 391 | 395 | the code jumps to rather than runs into, now starts at a multiple |
| 392 | | - of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it |
| 396 | + of 32 bytes (`alignLoop`, `align` in X64), the no-ops before it |
| 393 | 397 | never run. The sum 41% faster, the search 6%; rat within noise. |
| 394 | 398 | |
| 395 | 399 | 4. **`while` tested at the bottom.** A `while` loop tested its |
| | @@ -414,10 +418,10 @@ |
| 414 | 418 | jl top |
| 415 | 419 | ``` |
| 416 | 420 | |
| 417 | | - One pass reads the condition before the body, so `WhileStatement` |
| 421 | + One pass reads the condition before the body, so `whileStatement` |
| 418 | 422 | compiles it where it is written, for its messages, takes its code |
| 419 | | - back (`Rewind`), and reads it again after the body (`SaveScan`, |
| 420 | | - `RestoreScan`). A constant condition, as in `while true`, is no |
| 423 | + back (`rewind`), and reads it again after the body (`saveScan`, |
| 424 | + `restoreScan`, in Scan). A constant condition, as in `while true`, is no |
| 421 | 425 | test. The heap sort 3% faster, rat 2%; the search lost 7%, all of |
| 422 | 426 | it to where its loops fell: without the top aligned it lost |
| 423 | 427 | nothing, and rat gained nothing either. |
| | @@ -466,12 +470,12 @@ |
| 466 | 470 | ``` |
| 467 | 471 | |
| 468 | 472 | Now a real at depth `d` is held in `xmm8` to `xmm11`, in |
| 469 | | - `XRegs[d mod 4]` (`RegIn`): it is loaded and stored with `movsd` |
| 470 | | - (`movss` for an `F32`; `XValueTo`, `PopVar`), a constant is read |
| 471 | | - from 8 bytes of read-only data (`RealConst`), and an operation takes |
| 472 | | - its right operand from memory as it is, as `Op` does for an integer |
| 473 | | - (`RealOp`). A constant on the left of `+` or `*` is left on top by |
| 474 | | - `Op2`, in `item.pas`, as their order doesn't matter, rather than |
| 473 | + `XRegs[d mod 4]` (`regIn`): it is loaded and stored with `movsd` |
| 474 | + (`movss` for an `F32`; `xValueTo`, `popVar`), a constant is read |
| 475 | + from 8 bytes of read-only data (`realConst`), and an operation takes |
| 476 | + its right operand from memory as it is, as `op` does for an integer |
| 477 | + (`realOp`). A constant on the left of `+` or `*` is left on top by |
| 478 | + `Item.op2`, as their order doesn't matter, rather than |
| 475 | 479 | swapped under the other operand and back: |
| 476 | 480 | |
| 477 | 481 | ``` |
| | @@ -483,7 +487,7 @@ |
| 483 | 487 | movsd [rdi + r8 * 8], xmm9 |
| 484 | 488 | ``` |
| 485 | 489 | |
| 486 | | - `Call` moves a real argument straight to its `xmm0` to `xmm7`, and |
| 490 | + `call` moves a real argument straight to its `xmm0` to `xmm7`, and |
| 487 | 491 | takes a real result from `xmm0`; a comparison is `ucomisd` of the |
| 488 | 492 | two registers; a conversion is done in the value's own registers. |
| 489 | 493 | The registers from `xmm8` up carry no arguments, so that moving one |
| | @@ -509,7 +513,7 @@ |
| 509 | 513 | its index in a variable's register, is noted as the operand that |
| 510 | 514 | reads it, its base the array's address, made in the value's own |
| 511 | 515 | register; one in the frame has `rbp` as its base, whatever its |
| 512 | | - index (`IndexArray`, as `IndexSlice` does for a slice in |
| 516 | + index (`indexArray`, as `indexSlice` does for a slice in |
| 513 | 517 | registers): |
| 514 | 518 | |
| 515 | 519 | ``` |
| | @@ -518,8 +522,8 @@ |
| 518 | 522 | ``` |
| 519 | 523 | |
| 520 | 524 | A computed index in a chunk's array is added up as before, as it |
| 521 | | - would need a third register. `CheckIndex` compares a variable's |
| 522 | | - register as it is (`Peek`), not a copy. NBody 225 ms to 147; rat |
| 525 | + would need a third register. `checkIndex` compares a variable's |
| 526 | + register as it is (`peek`), not a copy. NBody 225 ms to 147; rat |
| 523 | 527 | within noise. |
| 524 | 528 | |
| 525 | 529 | 3. **An offset as the machine has it.** The sweep's four neighbours, |
| | @@ -540,12 +544,12 @@ |
| 540 | 544 | ``` |
| 541 | 545 | |
| 542 | 546 | Now a word in a register may carry an offset not yet added (`off`, |
| 543 | | - in `TEntry`): `Op` notes `x + c` and `x - c` there, carries it |
| 547 | + in `Entry`): `op` notes `x + c` and `x - c` there, carries it |
| 544 | 548 | through `+` and `-`, and through `*` by a constant, as `(x + a) * k` |
| 545 | 549 | is `x * k + a * k`. An element's operand takes it as its |
| 546 | | - displacement (`IndexOff`, for `IndexSlice` and `IndexArray`), and so |
| 547 | | - does a field's through a pointer (`Place`); anything else adds it |
| 548 | | - where the value is read (`Loaded`, `Peek`, `ValueTo`, with a `lea`, |
| 550 | + displacement (`indexOff`, for `indexSlice` and `indexArray`), and so |
| 551 | + does a field's through a pointer (`place`); anything else adds it |
| 552 | + where the value is read (`loaded`, `peek`, `valueTo`, with a `lea`, |
| 549 | 553 | which leaves the flags alone). A value changed in place, `lInto`, |
| 550 | 554 | carries none, and an index that is checked has it added first: |
| 551 | 555 | |
| | @@ -574,12 +578,12 @@ |
| 574 | 578 | mulsd xmm8, [rbp - 8] |
| 575 | 579 | ``` |
| 576 | 580 | |
| 577 | | - Now `ChooseRegs` gives the busiest reals of a procedure `xmm12` to |
| 581 | + Now `chooseRegs` gives the busiest reals of a procedure `xmm12` to |
| 578 | 582 | `xmm15` (`XVarRegs`), as it gives the busiest integers `rbx` and |
| 579 | 583 | `r12` to `r15`, and gen reads and writes them where they are: a |
| 580 | 584 | real in a variable's register is `lHeld` or `lInto`, as an |
| 581 | | - integer's is (`XValueTo`, `PopVar`, `Peek` for `RealOp` and |
| 582 | | - `Compare`): |
| 585 | + integer's is (`xValueTo`, `popVar`, `peek` for `realOp` and |
| 586 | + `compare`): |
| 583 | 587 | |
| 584 | 588 | ``` |
| 585 | 589 | subsd xmm8, [r9 + r12 * 8] |
| | @@ -589,15 +593,15 @@ |
| 589 | 593 | mulsd xmm8, xmm13 |
| 590 | 594 | ``` |
| 591 | 595 | |
| 592 | | - Each such variable has its slot too, which `PopVar` writes as it |
| 596 | + Each such variable has its slot too, which `popVar` writes as it |
| 593 | 597 | writes the register (`movsd [rbp - 8], xmm13` after the `movaps`), |
| 594 | 598 | off the chain the round waits on: the trap report finds the value |
| 595 | 599 | there, as it finds any variable's, whether a check failed or a |
| 596 | | - signal stopped the program, and `Call` loads the register from it |
| 597 | | - after each call (`XHome`, `HomeOf`), as System V keeps no xmm |
| 600 | + signal stopped the program, and `call` loads the register from it |
| 601 | + after each call (`xHome`, `homeOf`), as System V keeps no xmm |
| 598 | 602 | register through one. A register number from `XmmVar`, 16, up is |
| 599 | 603 | an xmm register's, so that `xmm12` isn't taken for `r12` where the |
| 600 | | - parser counts its registers (`RegsOf`, `RegAt`). NBody 147 ms to |
| 604 | + parser counts its registers (`regsOf`, `regAt`). NBody 147 ms to |
| 601 | 605 | 116, and a copy of it without the call to `sqrt` 114 to 81; rat |
| 602 | 606 | within noise. Storing the slot only before a call, and so showing |
| 603 | 607 | nothing in the trap report, bought 3% more on that copy and nothing |
| | @@ -615,69 +619,70 @@ |
| 615 | 619 | which there is no register. A comparison of reals still makes a |
| 616 | 620 | `BOOL` before it jumps, as `not (a < b)` isn't `a >= b` for a NaN; |
| 617 | 621 | neither kernel has one. |
| 622 | +## Diag.raz: messages |
| 618 | 623 | |
| 619 | | -## diag.pas: messages |
| 624 | +Every message the compiler prints is made here. A position, `Pos`, is a |
| 625 | +file and a stretch of bytes in it; `error`, `warning` and `note` show it |
| 626 | +as `file:line:column`, then the line of source with the stretch |
| 627 | +underlined, and `help` adds a line of advice. Notes and help are shown |
| 628 | +only with an error that was: not one at the place of the last, and not |
| 629 | +one under `quiet`, which the parser sets (`setQuiet`) when an error |
| 630 | +follows from another (see Parse.raz). The line and column are worked out |
| 631 | +only when a message is printed (`locate`, `chars`), so positions cost |
| 632 | +nothing while compiling. `addFile` keeps every source file the scanner |
| 633 | +opens, so a note can show a line of any of them. `finish` prints the |
| 634 | +count of errors and warnings and stops if there were errors; `fatal` is |
| 635 | +for errors with no place in the source. A message is made by `say` from |
| 636 | +a format and its arguments, and kept for good, since a message is made |
| 637 | +only where it is about to be shown. |
| 620 | 638 | |
| 621 | | -Every message the compiler prints is made here. A position, `TPos`, is |
| 622 | | -a file and a stretch of bytes in it; `Error`, `Warning` and `Note` show |
| 623 | | -it as `file:line:column`, then the line of source with the stretch |
| 624 | | -underlined, and `Help` adds a line of advice. Notes and help are shown |
| 625 | | -only with an error that was: not one at the place of the last, and |
| 626 | | -not one under `quiet`, which the parser sets when an error follows |
| 627 | | -from another (see parse.pas). The line and column are |
| 628 | | -worked out only when a message is printed (`Locate`, `Chars`), so |
| 629 | | -positions cost nothing while compiling. `AddFile` keeps every source |
| 630 | | -file the scanner opens, so a note can show a line of any of them. |
| 631 | | -`Finish` prints the count of errors and warnings and stops if there |
| 632 | | -were errors; `Fatal` is for errors with no place in the source. |
| 639 | +## Scan.raz: the scanner |
| 633 | 640 | |
| 634 | | -## scan.pas: the scanner |
| 635 | | - |
| 636 | | -The scanner turns bytes into symbols (`TSym`). The current symbol is in |
| 641 | +The scanner turns bytes into symbols (`Sym`). The current symbol is in |
| 637 | 642 | `tok`, with its value in `ival` (integers and characters), `rval` |
| 638 | | -(reals), `id` (identifiers) or `sbuf` and `slen` (strings, already |
| 639 | | -decoded). |
| 643 | +(reals), `id` (identifiers) or `strBytes`, the `slen` bytes of a string, |
| 644 | +already decoded. |
| 640 | 645 | |
| 641 | | -Read `Next` first: it skips white space and comments (`Comment`, which |
| 642 | | -counts nesting), then dispatches on the first character. `Number` |
| 643 | | -handles decimal, `0FFH`-style hexadecimal and reals; `LitChar` handles |
| 644 | | -one character of a literal, escapes included. `ReadReal` turns a real's |
| 645 | | -digits into the nearest `F64` exactly, as a fraction of two big |
| 646 | | -integers (`TBig`), since Free Pascal's `Val` can be one bit off. |
| 646 | +Read `next` first: it skips white space and comments (`comment`, which |
| 647 | +counts nesting), then dispatches on the first character. `number` |
| 648 | +handles decimal, `0FFH`-style hexadecimal and reals; `litChar` handles |
| 649 | +one character of a literal, escapes included. `readReal` turns a real's |
| 650 | +digits into the nearest `F64` exactly, as a fraction of two big integers |
| 651 | +(`Big`), since a library's reader can be one bit off. |
| 647 | 652 | |
| 648 | | -Identifiers are *interned*: `Intern` puts each distinct name once in a |
| 653 | +Identifiers are *interned*: `intern` puts each distinct name once in a |
| 649 | 654 | pool and hands back a small integer, so every later comparison of names |
| 650 | 655 | is a comparison of integers. The reserved words are interned first, in |
| 651 | | -the order of `TSym`, so a name's number tells at once whether it is a |
| 656 | +the order of `Sym`, so a name's number tells at once whether it is a |
| 652 | 657 | reserved word. |
| 653 | 658 | |
| 654 | | -Source is UTF-8, checked by `Utf8` wherever a byte above 127 appears: |
| 659 | +Source is UTF-8, checked by `utf8` wherever a byte above 127 appears: |
| 655 | 660 | in strings, characters and comments. Anywhere else it is an error. |
| 656 | 661 | |
| 657 | | -`TokPos` is the position of the current symbol, `SpanFrom` the stretch |
| 662 | +`tokPos` is the position of the current symbol, `spanFrom` the stretch |
| 658 | 663 | from a saved position to the end of the symbol before the current one, |
| 659 | | -and `MissingPos` the place to say a symbol is missing: just after the |
| 660 | | -symbol before, when the current one starts a new line. Errors go |
| 661 | | -through `Mark`, at the current symbol, `MarkAt`, at a position, or |
| 662 | | -`MarkHelp`, which adds a line of help; each reports the error |
| 663 | | -(`diag.Error`) and jumps to `recovery`, the parser's place to go on |
| 664 | | -from (see parse.pas), through `Fail`. An error with notes is reported |
| 665 | | -with `Error` and `Note`, then `Fail`. A message names what it found: `Found` describes |
| 666 | | -the current symbol and `SymText` spells an operator or reserved word, |
| 667 | | -so that the parser's `Expect` and `Expected` say `expected ";", found |
| 668 | | -"end"`. Errors in the text itself stop the |
| 669 | | -compiler (`MarkBytes`, `LexError`): past a string left open, the |
| 670 | | -symbols can't be trusted. A slip from another language, such as `!=` |
| 671 | | -for `#`, is reported by `Slip`, which reads on as if the right symbol |
| 672 | | -were written; not while the parser is `skipping` what it read once |
| 673 | | -before an error. `SaveScan` and `RestoreScan` keep |
| 674 | | -the scanner's state while an imported module is compiled. |
| 664 | +and `missingPos` the place to say a symbol is missing: just after the |
| 665 | +symbol before, when the current one starts a new line. Errors go through |
| 666 | +`mark`, at the current symbol, `markAt`, at a position, or `markHelp`, |
| 667 | +which adds a line of help; each reports the error (`Diag.error`) and |
| 668 | +returns `Failed`, an `ERROR` the parser passes up with `try` to where it |
| 669 | +goes on from (see Parse.raz). An error with notes is reported with |
| 670 | +`Diag.error` and `Diag.note`, then returns `Failed` itself. A message |
| 671 | +names what it found: `found` describes the current symbol and `symText` |
| 672 | +spells an operator or reserved word, so that the parser's `expect` and |
| 673 | +`expected` say `expected ";", found "end"`. Errors in the text itself |
| 674 | +stop the compiler (`markBytes`, `lexError`): past a string left open, |
| 675 | +the symbols can't be trusted. A slip from another language, such as `!=` |
| 676 | +for `#`, is reported by `slip`, which reads on as if the right symbol |
| 677 | +were written; not while the parser is `skipping` (`setSkipping`) what it |
| 678 | +read once before an error. `saveScan` and `restoreScan` keep the |
| 679 | +scanner's state while an imported module is compiled. |
| 675 | 680 | |
| 676 | | -## sym.pas: types, objects and scopes |
| 681 | +## Sym.raz: types, objects and scopes |
| 677 | 682 | |
| 678 | 683 | Two records hold everything the compiler knows about a program. |
| 679 | 684 | |
| 680 | | -`TType` is a type. Its `form` says what kind (`fInt`, `fPtr`, `fArray`, |
| 685 | +`Type` is a type. Its `form` says what kind (`fInt`, `fPtr`, `fArray`, |
| 681 | 686 | `fRecord`, `fVariant`, `fProc`, and so on), and the other fields mean |
| 682 | 687 | what that form needs: `base` is what a pointer points at, an array or |
| 683 | 688 | slice holds, or a procedure returns; `fields` lists a record's fields, |
| | @@ -685,365 +690,390 @@ |
| 685 | 690 | elements. `size` and `align` are worked out when the type is built, by |
| 686 | 691 | the C rules. `obj` is the declaration that named the type, if any. |
| 687 | 692 | |
| 688 | | -`TObj` is anything a name can stand for: a constant, type, variable, |
| 693 | +`Obj` is anything a name can stand for: a constant, type, variable, |
| 689 | 694 | procedure, module, field, variant case or built-in (`cls`), and `pos` |
| 690 | 695 | is where its name is declared, for notes. Where it |
| 691 | 696 | lives is decided here too: a module variable's chunk, a local's frame |
| 692 | 697 | offset in `val`, a procedure's chunk. |
| 693 | 698 | |
| 694 | 699 | Scopes are numbers. The universe is 0; each module, procedure, record |
| 695 | | -and match arm gets a new one from `NewScope`. All objects of all scopes |
| 696 | | -sit in one open-addressed hash table, keyed by scope and name: `Find` |
| 697 | | -looks in one scope, `Lookup` in each open scope from the innermost out. |
| 698 | | -A lookup costs the same however big a program grows, which matters at |
| 699 | | -a million lines a second. After an error, `Closest` looks through the |
| 700 | +and match arm gets a new one from `newScope`. All objects of all scopes |
| 701 | +sit in one open-addressed hash table, keyed by scope and name: `find` |
| 702 | +looks in one scope, `lookup` in each open scope from the innermost out. |
| 703 | +A lookup costs the same however big a program grows, which matters at a |
| 704 | +million lines a second. After an error, `closest` looks through the |
| 700 | 705 | whole table for the name in reach spelled most like one not found |
| 701 | | -(`Distance`), for "did you mean". |
| 706 | +(`distance`), for "did you mean". |
| 702 | 707 | |
| 703 | | -`Same` and `Alike` decide whether two types are the same: one |
| 704 | | -declaration for named types, built alike otherwise. `Fingerprint` |
| 705 | | -hashes what a declaration exports, for the check that a module's files |
| 706 | | -for different targets agree. The universe, the basic types and built-in |
| 707 | | -procedures, is set up in the unit's `initialization`. |
| 708 | +`same` and `alike` decide whether two types are the same: one |
| 709 | +declaration for named types, built alike otherwise. `fingerprint` hashes |
| 710 | +what a declaration exports, for the check that a module's files for |
| 711 | +different targets agree. The universe, the basic types and built-in |
| 712 | +procedures, is set up in the module's body. |
| 708 | 713 | |
| 709 | | -## chunk.pas: the compiled program |
| 714 | +Two routines can find an error: `insert`, for a name declared twice, and |
| 715 | +`openScope`, for too many scopes. Each shows it and returns |
| 716 | +`Scan.Failed`, for the parser to pass up with `try`. A record's module |
| 717 | +and an object's link, which the seed called `module` and `link`, are |
| 718 | +`home` and `chain`, since those are reserved words here; `setCurModule` |
| 719 | +is how the parser says which module it is in, as an exported variable |
| 720 | +can't be set from outside. |
| 710 | 721 | |
| 711 | | -Code is written into one growing buffer, `code`, with `pc` its length; |
| 712 | | -strings and other constants go to `rodata`. A chunk (`TChunk`) records |
| 722 | +## Chunk.raz: the compiled program |
| 723 | + |
| 724 | +Code is written into one growing buffer, `text`, with `pc` its length; |
| 725 | +strings and other constants go to `rodata`. A chunk (`Chunk`) records |
| 713 | 726 | its section, where its bytes start, how many, and the fixups it makes. |
| 714 | | -`BeginChunk` and `EndChunk` bracket a procedure; `SetData` and `SetBss` |
| 727 | +`beginChunk` and `endChunk` bracket a procedure; `setData` and `setBss` |
| 715 | 728 | make data chunks. |
| 716 | 729 | |
| 717 | | -A *fixup* (`TFixup`) is a 32-bit field in the code that will hold the |
| 730 | +A *fixup* (`Fixup`) is a 32-bit field in the code that will hold the |
| 718 | 731 | distance to another chunk: a call's target, or a rip-relative address. |
| 719 | | -`AddFixup` records one; `link.pas` fills them in. A constant's |
| 720 | | -addresses are *data fixups*, `AddDataFixup`'s: 64-bit fields in |
| 721 | | -rodata that will hold a chunk's address itself, since the executable is |
| 722 | | -loaded where the linker placed it. `NoteLine` records |
| 723 | | -which line and column of the source the code from `pc` on belongs to, |
| 724 | | -for gdb and for the places. `ExternChunk` makes, once per C symbol, |
| 725 | | -the quadword the dynamic loader will fill with that symbol's address. |
| 726 | | -None of this depends on the machine. |
| 732 | +`addFixup` records one; `Link.raz` fills them in. A constant's addresses |
| 733 | +are *data fixups*, `addDataFixup`'s: 64-bit fields in rodata that will |
| 734 | +hold a chunk's address itself, since the executable is loaded where the |
| 735 | +linker placed it. `noteLine` records which line and column of the source |
| 736 | +the code from `pc` on belongs to, for gdb and for the places. |
| 737 | +`externChunk` makes, once per C symbol, the quadword the dynamic loader |
| 738 | +will fill with that symbol's address. `takeBack` and `rewindTo` undo the |
| 739 | +end of what was made, for a body compiled a second time. What other |
| 740 | +modules set in a chunk, such as `live` or `adr`, has a setter each |
| 741 | +(`setLive`, `setAdr`), since an exported variable can't be set from |
| 742 | +outside. None of this depends on the machine. |
| 727 | 743 | |
| 728 | | -## x64.pas: machine code |
| 744 | +## X64.raz: machine code |
| 729 | 745 | |
| 730 | | -The encoder is `OpM` and `OpR`: a prefix, the REX byte, the opcode, the |
| 746 | +The encoder is `opM` and `opR`: a prefix, the REX byte, the opcode, the |
| 731 | 747 | ModRM and SIB bytes and the displacement, for a register and a memory |
| 732 | | -operand (`TMem`) or two registers. Everything else (`MovRR`, `Load`, |
| 733 | | -`Store`, `Alu`, `Sse`, ...) is a line or two that calls them with the |
| 748 | +operand (`Mem`) or two registers. Everything else (`movRR`, `load`, |
| 749 | +`store`, `alu`, `sse`, ...) is a line or two that calls them with the |
| 734 | 750 | right opcode. Only the instructions the code generator uses are here. |
| 735 | | -`test/enctest.pas` emits one of each for `objdump` to check. `Here` is |
| 751 | +`test/enctest.pas` emits one of each, with the seed's encoder, for `objdump` to check. `here` is |
| 736 | 752 | the address of the code being written, as an operand. |
| 737 | 753 | |
| 738 | | -The assembler, `Assemble`, reads a code procedure's instructions with |
| 754 | +The assembler, `assemble`, reads a code procedure's instructions with |
| 739 | 755 | the scanner, Intel's syntax as NASM writes it, and encodes them with |
| 740 | | -`OpM` and `OpR`: `Operand` reads a register, a memory operand or a |
| 741 | | -constant (by the parser's `AsmConst`, handed in: a whole expression, |
| 742 | | -or for a scale a single factor, so that `[rdi + rsi * 4 + 16]` doesn't |
| 743 | | -read `4 + 16` as the scale), and a few tables |
| 744 | | -give the operations of a kind their opcodes (`Alus`, `Unaries`, |
| 745 | | -`Shifts`, `SseOps`, `Plain`; `PackedOps` the SSE2 integer operations |
| 746 | | -on 16 bytes at a time, with `movdqu`, `movdqa` and `pmovmskb` beside |
| 747 | | -them, and `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, |
| 748 | | -`lzcnt`, `popcnt`). Labels are chains of jumps, as below. |
| 749 | | -`db` gives bytes it doesn't know. `RegNames` and `CondNames` are the |
| 750 | | -names of the registers and conditions. |
| 756 | +`opM` and `opR`: `operand` reads a register, a memory operand or a |
| 757 | +constant (by the parser's `asmConst`, handed in as a `ConstFn`: a whole |
| 758 | +expression, or for a scale a single factor, so that `[rdi + rsi * 4 + |
| 759 | +16]` doesn't read `4 + 16` as the scale), and a few tables give the |
| 760 | +operations of a kind their opcodes (`Alus`, `Unaries`, `Shifts`, |
| 761 | +`SseOps`, `Plain`; `PackedOps` the SSE2 integer operations on 16 bytes |
| 762 | +at a time, with `movdqu`, `movdqa` and `pmovmskb` beside them, and |
| 763 | +`BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`, |
| 764 | +`popcnt`). Labels are chains of jumps, as below. `db` gives bytes it |
| 765 | +doesn't know. An error in an instruction is returned as an `ERROR`, |
| 766 | +which `assemble` passes up. An offset that doesn't fit in 32 bits |
| 767 | +(`mem`) can't be returned, since an operand has nowhere to give it: it |
| 768 | +is shown, and the compile goes on to its end. `RegNames` and `CondNames` |
| 769 | +are the names of the registers and conditions. |
| 751 | 770 | |
| 752 | | -The listing, `raz -S`: with `listing` set, each routine of the encoder |
| 753 | | -says, with `Say`, the instruction it is about to write, as the |
| 754 | | -assembler would read it; `List` prints the main module's live |
| 755 | | -procedures with each line of source (`LineText` in `diag.pas`) and the |
| 756 | | -instructions under it. Nothing is disassembled: the listing is what |
| 757 | | -the encoder meant. |
| 771 | +The listing, `raz -S`: with `listing` set (`setListing`), each routine |
| 772 | +of the encoder says, with `say`, the instruction it is about to write, |
| 773 | +as the assembler would read it; `list` prints the main module's live |
| 774 | +procedures with each line of source (`lineText` in `Diag.raz`) and the |
| 775 | +instructions under it. Nothing is disassembled: the listing is what the |
| 776 | +encoder meant. |
| 758 | 777 | |
| 759 | 778 | Jumps within a procedure don't wait for the linker. A label not yet |
| 760 | 779 | placed is a chain of the jumps to it, each jump's 32-bit field holding |
| 761 | | -the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the |
| 780 | +the position of the one before (`jmp`, `jcc`); `fixLink` walks the |
| 762 | 781 | chain and patches each once the label's position is known. This is the |
| 763 | 782 | trick from Wirth's compilers, and it means a label is one integer. |
| 764 | | -`DropJumpTo` takes back a `jmp` just made to a label about to be |
| 783 | +`dropJumpTo` takes back a `jmp` just made to a label about to be |
| 765 | 784 | placed after it, so a `return` that ends a procedure doesn't jump to |
| 766 | 785 | the next instruction; the line notes made after it move back with it |
| 767 | | -(`TakeBack`, in `chunk.pas`). |
| 786 | +(`takeBack`, in `Chunk.raz`). |
| 768 | 787 | |
| 769 | | -## gen.pas: the stack machine |
| 788 | +## Gen.raz: the stack machine |
| 770 | 789 | |
| 771 | 790 | This is the only unit that knows the machine. The calls it answers: |
| 772 | 791 | |
| 773 | | -- values: `PushConst`, `PushAdr` (the address of a variable), |
| 774 | | - `PushVar` (its value), `PushProc`, and `PopVar`, which stores the |
| 775 | | - value on top; `Drop` and `Swap`; |
| 776 | | -- operations on the values on top: `Op`, `Compare`, `Neg`, |
| 777 | | - `Complement`, `NotBool`, `Narrow` (to a narrow integer's width), |
| 778 | | - the conversions between integers and reals, `CompareSeq` (of two |
| 779 | | - slices), and copying: `CopyBlock`, `CopyTo`, `ZeroVar`, and |
| 780 | | - `CopySlice` for `COPY`, a `rep movsb` that goes down when the |
| 781 | | - destination is above the source; |
| 782 | | -- jumps: `Jump`, `JumpIf` (on a `BOOL`), `JumpCmp` (on two integers), |
| 783 | | - `JumpOut` (`JumpCmp` from within an expression) and `CondValue` for |
| 784 | | - `and` and `or`, `Fix` where the label is, and `JoinJumps`; |
| 785 | | -- checks: `CheckIndex`, `CheckEnum`, and `IndexSlice`, `Slice` and |
| 786 | | - `SlicePtr`, which check as they make their address or slice; and |
| 787 | | - `IndexArray`, an element's address in an array in the frame or a |
| 792 | +- values: `pushConst`, `pushAdr` (the address of a variable), `pushVar` |
| 793 | + (its value), `pushProc`, and `popVar`, which stores the value on top; |
| 794 | + `drop` and `swap`; |
| 795 | +- operations on the values on top: `op`, `compare`, `neg`, `complement`, |
| 796 | + `notBool`, `narrow` (to a narrow integer's width), the conversions |
| 797 | + between integers and reals, `compareSeq` (of two slices), and copying: |
| 798 | + `copyBlock`, `copyTo`, `zeroVar`, and `copySlice` for `COPY`, a `rep |
| 799 | + movsb` that goes down when the destination is above the source; |
| 800 | +- jumps: `jump`, `jumpIf` (on a `BOOL`), `jumpCmp` (on two integers), |
| 801 | + `jumpOut` (`jumpCmp` from within an expression) and `condValue` for |
| 802 | + `and` and `or`, `fix` where the label is, and `joinJumps`; |
| 803 | +- checks: `checkIndex`, `checkEnum`, and `indexSlice`, `slice` and |
| 804 | + `slicePtr`, which check as they make their address or slice; and |
| 805 | + `indexArray`, an element's address in an array in the frame or a |
| 788 | 806 | chunk, the index checked before; |
| 789 | | -- calls (`Call`), frames (`BeginProc`, `EnterBody`, `ReturnJump`, |
| 790 | | - `EndProc`, the temporaries of `AllocTemp`), deferred statements |
| 791 | | - (`DeferBegin`, `DeferEnd`, `RunDefer`), the built-ins that are |
| 792 | | - instructions (`Atomic`, `SysCall`, `Fence`), and the program's entry |
| 793 | | - point (`EntryPoint`). |
| 807 | +- calls (`call`), frames (`beginProc`, `enterBody`, `returnJump`, |
| 808 | + `endProc`, the temporaries of `allocTemp`), deferred statements |
| 809 | + (`deferBegin`, `deferEnd`, `runDefer`), the built-ins that are |
| 810 | + instructions (`atomic`, `sysCall`, `fence`), and the program's entry |
| 811 | + point (`entryPoint`). |
| 794 | 812 | |
| 795 | | -A variable's place is a `TAdr`: relative to the frame (`bFrame`), to a |
| 813 | +A variable's place is an `Adr`: relative to the frame (`bFrame`), to a |
| 796 | 814 | chunk (`bGlobal`), to an address on the stack (`bStack`), or to one |
| 797 | 815 | held in a frame slot (`bSlot`, as a `var` parameter's is); plus `off`. |
| 798 | | -`Place` turns it into an x64 memory operand, popping or loading the |
| 816 | +`place` turns it into an x64 memory operand, popping or loading the |
| 799 | 817 | address first, so that a variable is read or written with one |
| 800 | 818 | instruction. |
| 801 | 819 | |
| 802 | 820 | The stack: `stack` notes where each value is, a constant (`lConst`), an |
| 803 | | -address not yet computed (`lAdr`, `AdrMem`'s operand), an element's |
| 804 | | -address not yet computed (`lIdx`, an operand `[base + index * size]`), |
| 805 | | -a variable's value not yet read (`lMem`, at an operand), a register |
| 821 | +address not yet computed (`lAdr`, `adrMem`'s operand), an element's |
| 822 | +address not yet computed (`lIdx`, an operand `[base + index * size]`), a |
| 823 | +variable's value not yet read (`lMem`, at an operand), a register |
| 806 | 824 | variable's own register, read in place (`lHeld`), the value's own |
| 807 | | -register (`lReg`) or a frame slot (`lFrame`); `owner` says which |
| 808 | | -value each register holds, `lIdx` and `lMem` holding theirs when it |
| 809 | | -is their index. A word in a register may have an offset not yet |
| 810 | | -added (`off`), which an operand takes as its displacement. The value at depth `d` belongs in |
| 811 | | -`Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division |
| 812 | | -and shifts need, and `rsi` and `rdi`, which copying needs, aren't among |
| 813 | | -them. A real belongs in `XRegs[d mod 4]`, `xmm8` to `xmm11`, which |
| 814 | | -carry no arguments; each depth has the two registers, and `owner` |
| 815 | | -doesn't tell them apart (`RegIn`, `RegOf`). `Claim` gives a value its register, and `Spill`s the lower one |
| 816 | | -that holds it, when an expression is deeper than the registers; |
| 817 | | -`Loaded` brings a value into its register, from its constant, address |
| 818 | | -or slot; `Place` uses a noted address as the operand itself, so that |
| 819 | | -`PopVar`, storing a constant, and `IncDec` are one instruction on |
| 820 | | -memory: `DEC(n)` is `sub qword [rbp - 8], 1`. `Top` is `Loaded` for the |
| 821 | | -value on top, and `Target` the register a new one is computed in. |
| 822 | | -`Drop` emits no code, and the dropped value's register still holds it |
| 823 | | -until the next push, which several routines rely on. Before a call every |
| 824 | | -value in a register is spilled (`SpillBelow`), since the call may change |
| 825 | | -them, and so are the values under a comparison that jumps out of an |
| 826 | | -expression (`JumpOut`), which both ways to its label must find in the |
| 827 | | -same place. `pushed` counts the quadwords on the machine's stack. A |
| 828 | | -call needs `rsp` a multiple of 16, so `Call` pads by one quadword when |
| 829 | | -`pushed` plus its stack arguments is odd; in a deferred statement |
| 830 | | -`pushed` starts at 1, for the return address. `Op` takes a |
| 831 | | -constant on the right as it is, after moving one on the left of `+`, |
| 832 | | -`*`, `&`, `|` or `xor` there, and a power of two on the right of `*`, |
| 833 | | -`div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow; an |
| 834 | | -element's size multiplies an index by a shift too (`MulI`), as a shift |
| 835 | | -takes one cycle and `imul` three. |
| 836 | | -Narrow integers are brought back to their width after each operation, |
| 837 | | -which is how `U8` arithmetic wraps; since they are computed in 64 bits, |
| 838 | | -a shift's count is cut to their width first, and `DivTrap` makes the |
| 839 | | -smallest value of one divided by -1 fault as an `I64`'s does. |
| 825 | +register (`lReg`) or a frame slot (`lFrame`); `owner` says which value |
| 826 | +each register holds, `lIdx` and `lMem` holding theirs when it is their |
| 827 | +index. A word in a register may have an offset not yet added (`off`), |
| 828 | +which an operand takes as its displacement. The value at depth `d` |
| 829 | +belongs in `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which |
| 830 | +division and shifts need, and `rsi` and `rdi`, which copying needs, |
| 831 | +aren't among them. A real belongs in `XRegs[d mod 4]`, `xmm8` to |
| 832 | +`xmm11`, which carry no arguments; each depth has the two registers, and |
| 833 | +`owner` doesn't tell them apart (`regIn`, `regOf`). `claim` gives a |
| 834 | +value its register, and `spill`s the lower one that holds it, when an |
| 835 | +expression is deeper than the registers; `loaded` brings a value into |
| 836 | +its register, from its constant, address or slot; `place` uses a noted |
| 837 | +address as the operand itself, so that `popVar`, storing a constant, and |
| 838 | +`incDec` are one instruction on memory: `DEC(n)` is `sub qword [rbp - |
| 839 | +8], 1`. `top` is `loaded` for the value on top, and `target` the |
| 840 | +register a new one is computed in. `drop` emits no code, and the dropped |
| 841 | +value's register still holds it until the next push, which several |
| 842 | +routines rely on. Before a call every value in a register is spilled |
| 843 | +(`spillBelow`), since the call may change them, and so are the values |
| 844 | +under a comparison that jumps out of an expression (`jumpOut`), which |
| 845 | +both ways to its label must find in the same place. `pushed` counts the |
| 846 | +quadwords on the machine's stack. A call needs `rsp` a multiple of 16, |
| 847 | +so `call` pads by one quadword when `pushed` plus its stack arguments is |
| 848 | +odd; in a deferred statement `pushed` starts at 1, for the return |
| 849 | +address. `op` takes a constant on the right as it is, after moving one |
| 850 | +on the left of `+`, `*`, `&`, `|` or `xor` there, and a power of two on |
| 851 | +the right of `*`, `div` or `mod` becomes shifts (`divPow2`), since |
| 852 | +`idiv` is slow; an element's size multiplies an index by a shift too |
| 853 | +(`mulI`), as a shift takes one cycle and `imul` three. Narrow integers |
| 854 | +are brought back to their width after each operation, which is how `U8` |
| 855 | +arithmetic wraps; since they are computed in 64 bits, a shift's count is |
| 856 | +cut to their width first, and `divTrap` makes the smallest value of one |
| 857 | +divided by -1 fault as an `I64`'s does. |
| 840 | 858 | |
| 841 | | -Checks: `Check` jumps, when the check fails, to code at the end of the |
| 842 | | -procedure (`EndFails`), so that a check that holds is a jump not taken. |
| 843 | | -That code puts the values involved where `Rt.fail` wants them and |
| 844 | | -calls a stub for its kind of failure (`failStubs`, made by |
| 845 | | -`EntryPoint`), which says the kind and passes the address it was |
| 846 | | -called from, by which `Rt` finds the place: the failing code has the |
| 847 | | -place of the offending expression (`LineCol` in `scan.pas`). It leaves |
| 848 | | -`rdi` and `rsi` as they were, so that the stub can pass them on, for |
| 849 | | -the trap report to show a parameter kept in them. A check |
| 850 | | -costs a compare and a jump where it stands, and a dozen bytes out of |
| 851 | | -the way. Code is aligned to 32 bytes, the windows x86 fetches it in, |
| 852 | | -so that where a procedure's loops fall doesn't depend on what comes |
| 853 | | -before it. |
| 859 | +Checks: `check` jumps, when the check fails, to code at the end of the |
| 860 | +procedure (`endFails`), so that a check that holds is a jump not taken. |
| 861 | +That code puts the values involved where `Rt.fail` wants them and calls |
| 862 | +a stub for its kind of failure (`failStubs`, made by `entryPoint`), |
| 863 | +which says the kind and passes the address it was called from, by which |
| 864 | +`Rt` finds the place: the failing code has the place of the offending |
| 865 | +expression (`lineCol` in `Scan.raz`). It leaves `rdi` and `rsi` as they |
| 866 | +were, so that the stub can pass them on, for the trap report to show a |
| 867 | +parameter kept in them. A check costs a compare and a jump where it |
| 868 | +stands, and a dozen bytes out of the way. Code is aligned to 32 bytes, |
| 869 | +the windows x86 fetches it in, so that where a procedure's loops fall |
| 870 | +doesn't depend on what comes before it. |
| 854 | 871 | |
| 855 | | -What the machine catches, the code doesn't test for (report section |
| 856 | | -10, appendix A): on x86-64 Linux a division by zero raises SIGFPE, a |
| 857 | | -stack overflow meets the guard page under the stack, a call to `nil` |
| 858 | | -jumps to an address never mapped, and so does a load or store within |
| 859 | | -`NilPage` bytes of address 0. `Rt`'s handlers say where and why. What |
| 860 | | -is left is a field of a `nil` pointer past that page: an item reached |
| 861 | | -through `^` is `deref` until `NilTest` (`item.pas`) has it used, and |
| 862 | | -gen's `CheckNil` tests the pointer only if the use reaches past |
| 863 | | -`NilPage`. A port whose machine catches less tests for more, in its |
| 864 | | -own `gen.pas`: `Rt.fail` stops for a nil pointer, a division by zero |
| 865 | | -and a stack overflow as the signals would. |
| 872 | +The stack has 256 places and a procedure may have 4,096 checks. Past |
| 873 | +either, `newEntry` or `check` shows the error and goes on, since a push |
| 874 | +or a check has nowhere to return it; the parser's `resetStack` makes the |
| 875 | +stack right again. `endProc` returns its error (a frame too big) and |
| 876 | +`codeProc` the assembler's. |
| 866 | 877 | |
| 878 | +What the machine catches, the code doesn't test for (report section 10, |
| 879 | +appendix A): on x86-64 Linux a division by zero raises SIGFPE, a stack |
| 880 | +overflow meets the guard page under the stack, a call to `nil` jumps to |
| 881 | +an address never mapped, and so does a load or store within `NilPage` |
| 882 | +bytes of address 0. `Rt`'s handlers say where and why. What is left is a |
| 883 | +field of a `nil` pointer past that page: an item reached through `^` is |
| 884 | +`deref` until `nilTest` (`Item.raz`) has it used, and Gen's `checkNil` |
| 885 | +tests the pointer only if the use reaches past `NilPage`. A port whose |
| 886 | +machine catches less tests for more, in its own `Gen.raz`: `Rt.fail` |
| 887 | +stops for a nil pointer, a division by zero and a stack overflow as the |
| 888 | +signals would. |
| 889 | + |
| 867 | 890 | Calls: the arguments are computed left to right onto the stack, where |
| 868 | | -they wait for `Call`, as any values do: a constant, an address or a |
| 869 | | -variable in a register only noted, the rest in registers, or spilled |
| 870 | | -by a call in a later argument, so that it can't disturb them. `Call` |
| 871 | | -spills what is under them, moves each into the register System V |
| 872 | | -wants (`ValueTo`), `r8` and `r9` last since they may hold others, and |
| 873 | | -aligns the stack. A slice goes as its two words, in two registers or |
| 874 | | -both on the stack, as System V passes a struct of two integers, and |
| 875 | | -the procedure keeps them in its frame. A record |
| 876 | | -passed by value goes as the address of a copy. A result of two words, |
| 877 | | -a slice or two single values that aren't reals (`InTwoRegs`, in |
| 878 | | -`sym.pas`), comes back in rax and rdx, as System V returns a struct of |
| 879 | | -two integers: `return` leaves both on the stack for `ReturnJump`. |
| 880 | | -`Call` leaves two results on the stack, and stores a slice in a |
| 881 | | -temporary of the caller's. Any other record, or |
| 891 | +they wait for `call`, as any values do: a constant, an address or a |
| 892 | +variable in a register only noted, the rest in registers, or spilled by |
| 893 | +a call in a later argument, so that it can't disturb them. `call` spills |
| 894 | +what is under them, moves each into the register System V wants |
| 895 | +(`valueTo`), `r8` and `r9` last since they may hold others, and aligns |
| 896 | +the stack. A slice goes as its two words, in two registers or both on |
| 897 | +the stack, as System V passes a struct of two integers, and the |
| 898 | +procedure keeps them in its frame. A record passed by value goes as the |
| 899 | +address of a copy. A result of two words, a slice or two single values |
| 900 | +that aren't reals (`inTwoRegs`, in `Sym.raz`), comes back in rax and |
| 901 | +rdx, as System V returns a struct of two integers: `return` leaves both |
| 902 | +on the stack for `returnJump`. `call` leaves two results on the stack, |
| 903 | +and stores a slice in a temporary of the caller's. Any other record, or |
| 882 | 904 | several results, come back in memory the caller provides, through a |
| 883 | | -hidden pointer in `rdi` (`BehindPointer`, `ResultTo`, `CopyResult`). C |
| 905 | +hidden pointer in `rdi` (`behindPointer`, `resultTo`, `copyResult`). C |
| 884 | 906 | returns a struct of up to 16 bytes in `rax` and `rdx`; raz sends every |
| 885 | 907 | record through memory, and only a slice or two single values in |
| 886 | 908 | registers. |
| 887 | 909 | |
| 888 | | -Frames: `BeginProc` gives the parameters their slots; `EnterBody` |
| 889 | | -writes the prologue, which zeroes the locals in memory (with a few |
| 890 | | -stores, or a loop for more; there are none to zero in `scale`) but not |
| 891 | | -the temporaries, since each is written before it is read; `EndProc` writes |
| 910 | +Frames: `beginProc` gives the parameters their slots; `enterBody` writes |
| 911 | +the prologue, which zeroes the locals in memory (with a few stores, or a |
| 912 | +loop for more; there are none to zero in `scale`) but not the |
| 913 | +temporaries, since each is written before it is read; `endProc` writes |
| 892 | 914 | the epilogue and patches the frame's size in. Within a statement, |
| 893 | | -scratch space comes from `AllocTemp` and is given back by |
| 894 | | -`ReleaseTemps`; `AllocPerm` reserves space for the whole procedure. |
| 915 | +scratch space comes from `allocTemp` and is given back by |
| 916 | +`releaseTemps`; `allocPerm` reserves space for the whole procedure. |
| 895 | 917 | |
| 896 | | -Register variables: the parser chooses them (`ChooseRegs`, below), |
| 897 | | -then gives the locals left in memory their slots (`PlaceLocals`), and |
| 898 | | -tells gen (`UseRegs`) the registers, as bits of `VarRegs`. A variable |
| 899 | | -in a register has no slot of its own. Gen saves the registers in slots |
| 900 | | -a quadword each, after the locals and not zeroed, as each is written |
| 901 | | -first. The |
| 902 | | -prologue saves them, zeroes them, and moves a parameter into its |
| 903 | | -register from the register or stack slot it arrived in; the epilogue |
| 904 | | -restores them. A slice may have two, its pointer's and its length's |
| 905 | | -(see "Better code again" above). A variable in one is an |
| 906 | | -item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it, |
| 907 | | -`Peek` reads it in place where the value is only read, so `i < n` is |
| 908 | | -`cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the |
| 909 | | -values for the trap report from the saves: each procedure's record in |
| 910 | | -the places says which it saved, and where (`saveMask`, `saveAt`). |
| 911 | | -A real takes one of `xmm12` to `xmm15` (`XVarRegs`, numbered from |
| 912 | | -`XmmVar` up, so that `xmm12` isn't taken for `r12`), which the prologue |
| 913 | | -zeroes and saves nothing for, and a slot as well, which `PopVar` |
| 914 | | -writes as it writes the register (`XHome`): the trap report finds it |
| 915 | | -there, and `Call` loads the register from it after each call, as |
| 916 | | -System V has no callee-saved xmm register. |
| 918 | +Register variables: the parser chooses them (`chooseRegs`, in |
| 919 | +Parse.raz), then gives the locals left in memory their slots |
| 920 | +(`placeLocals`), and tells Gen (`useRegs`) the registers, as bits of |
| 921 | +`VarRegs`. A variable in a register has no slot of its own. Gen saves |
| 922 | +the registers in slots a quadword each, after the locals and not zeroed, |
| 923 | +as each is written first. The prologue saves them, zeroes them, and |
| 924 | +moves a parameter into its register from the register or stack slot it |
| 925 | +arrived in; the epilogue restores them. A slice may have two, its |
| 926 | +pointer's and its length's (see "Better code again" above). A variable |
| 927 | +in one is an item at `bReg`: `pushVar` notes it (`lHeld`) rather than |
| 928 | +copying it, `peek` reads it in place where the value is only read, so `i |
| 929 | +< n` is `cmp rbx, r12`, and `popVar` and `incDecReg` write it. Rt finds |
| 930 | +the values for the trap report from the saves: each procedure's record |
| 931 | +in the places says which it saved, and where (`saveMask`, `saveAt`). A |
| 932 | +real takes one of `xmm12` to `xmm15` (`XVarRegs`, numbered from `XmmVar` |
| 933 | +up, so that `xmm12` isn't taken for `r12`), which the prologue zeroes |
| 934 | +and saves nothing for, and a slot as well, which `popVar` writes as it |
| 935 | +writes the register (`xHome`): the trap report finds it there, and |
| 936 | +`call` loads the register from it after each call, as System V has no |
| 937 | +callee-saved xmm register. |
| 917 | 938 | |
| 918 | | -## item.pas: items |
| 939 | +## Item.raz: items |
| 919 | 940 | |
| 920 | | -An operand is an *item* (`TItem`), as in Wirth's compilers. Its `mode` |
| 941 | +An operand is an *item* (`Item`), as in Wirth's compilers. Its `mode` |
| 921 | 942 | says what it is now: |
| 922 | 943 | |
| 923 | 944 | - `mConst`: a value known to the compiler, in `val`; |
| 924 | | -- `mVar`: a variable, at `base`, `chunk` and `off`, which `AdrOf` |
| 925 | | - makes a `TAdr` for gen; |
| 926 | | -- `mStack`: a value on gen's stack; |
| 945 | +- `mVar`: a variable, at `base`, `chunk` and `off`, which `adrOf` makes |
| 946 | + an `Adr` for Gen; |
| 947 | +- `mStack`: a value on Gen's stack; |
| 927 | 948 | - `mWords`: the two results of a call, as rax and rdx brought them, on |
| 928 | | - gen's stack, the first under the second. `a, b := f(x)` brings the |
| 929 | | - first to the top (`Swap`) and pops each into its variable, so that |
| 949 | + Gen's stack, the first under the second. `a, b := f(x)` brings the |
| 950 | + first to the top (`swap`) and pops each into its variable, so that |
| 930 | 951 | nothing goes through memory; `return f(x)` leaves them for |
| 931 | | - `ReturnJump`; anything else makes them a temporary (`CopyToTemp`); |
| 952 | + `returnJump`; anything else makes them a temporary (`copyToTemp`); |
| 932 | 953 | - `mCond`: a condition, as Wirth's compilers keep one: two integers on |
| 933 | | - gen's stack, compared by `rel` once something wants the result, and |
| 934 | | - the jumps of `and` and `or` that land where it is false (`fj`) or |
| 935 | | - true (`tj`); `CondJump` makes it a jump, `Value` a `BOOL`; |
| 954 | + Gen's stack, compared by `rel` once something wants the result, and |
| 955 | + the jumps of `and` and `or` that land where it is false (`fj`) or true |
| 956 | + (`tj`); `condJump` makes it a jump, `value` a `BOOL`; |
| 936 | 957 | - `mProc`, `mType`, `mStr`, and a few more the parser needs. |
| 937 | 958 | |
| 938 | 959 | An item also carries `pos`, the stretch of source it came from, which |
| 939 | 960 | the parser sets as it reads an expression; an error about an item is |
| 940 | | -reported there (`MarkAt(x.pos, ...)`), and one about two operands |
| 941 | | -across both (`Join(x.pos, y.pos)`). `What` names an item for a |
| 961 | +reported there (`Scan.markAt(x.pos, ...)`), and one about two operands |
| 962 | +across both (`Diag.join(x.pos, y.pos)`). `what` names an item for a |
| 942 | 963 | message: a constant without a type of its own as it is written |
| 943 | 964 | (`found 1.5`), anything else by its type. |
| 944 | 965 | |
| 945 | | -Two routines move items towards the machine: `Value` pushes an item's |
| 946 | | -value, and `Address` a variable's address. Both leave the item saying |
| 947 | | -it is on the stack. An item nothing more is done with is given back to |
| 948 | | -`Discard`. |
| 966 | +An error is shown where it is found, by `Scan.markAt` and the others, |
| 967 | +which return `Scan.Failed`. Every routine here that can find one returns |
| 968 | +an `ERROR` as its last result, and the ones that call it pass it up with |
| 969 | +`try`, to where the parser reads on. |
| 949 | 970 | |
| 950 | | -Expressions: `Op1` is called when the parser has seen an operator and |
| 951 | | -its left operand, `Op2` when it has the right one. `Op1` pushes the |
| 952 | | -left operand, unless it is a constant; for `and` and `or` it makes the |
| 953 | | -left one a condition (`MakeCond`) and jumps past the right one, which |
| 954 | | -`Op2` joins to it: `(a < b) and (c < d)` in an `if` is two compares |
| 955 | | -and two jumps, as `not` of a condition is the other comparison. `Op2` checks the types, folds constants |
| 956 | | -(`FoldInt`, `FoldReal`), and otherwise pushes the right operand and has |
| 957 | | -gen do the operation; a constant on the left goes on after the right, |
| 958 | | -and the two `Swap`. A set's operations are operations on its bits |
| 959 | | -(`SetOp`). `=` and `#` on arrays, slices and strings make a slice of |
| 960 | | -each side (`SeqSlice`) and have gen compare them right where the |
| 961 | | -comparison stands: no call. `+` on two string constants is `JoinStr`, |
| 962 | | -which makes one string of them, in a chunk of its own. |
| 971 | +Two routines move items towards the machine: `value` pushes an item's |
| 972 | +value, and `address` a variable's address. Both leave the item saying it |
| 973 | +is on the stack. An item nothing more is done with is given back to |
| 974 | +`discard`. |
| 963 | 975 | |
| 964 | | -Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| 965 | | -variable into an item for part of it, mostly by adding to `off`. An |
| 966 | | -item for another module's exported variable, or part of it, is `ro`: |
| 967 | | -it isn't a variable (`isVar`), and `NotReadOnly` says why where one is |
| 968 | | -wanted; what it points at is. An array or record constant is a |
| 969 | | -variable too, in read-only data: `AggregateConst` (`parse.pas`) reads |
| 970 | | -its literal, part by part in `ConstPart`, into the bytes of a chunk, |
| 971 | | -as a string constant's are; an address in it, of a string, an array |
| 972 | | -constant, a procedure or an `ERROR`, becomes a data fixup. `MakeItem` makes it |
| 973 | | -an `mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| 974 | | -passed, never changed. `Index` and `Slice` have gen make the bounds |
| 975 | | -checks, unless the module says `check off;` (`checks`, which |
| 976 | | -`CompileFile` sets for each module), and `Convert` the check of an |
| 977 | | -enumeration's position. |
| 976 | +Expressions: `op1` is called when the parser has seen an operator and |
| 977 | +its left operand, `op2` when it has the right one. `op1` pushes the left |
| 978 | +operand, unless it is a constant; for `and` and `or` it makes the left |
| 979 | +one a condition (`makeCond`) and jumps past the right one, which `op2` |
| 980 | +joins to it: `(a < b) and (c < d)` in an `if` is two compares and two |
| 981 | +jumps, as `not` of a condition is the other comparison. `op2` checks the |
| 982 | +types, folds constants (`foldInt`, `foldReal`), and otherwise pushes the |
| 983 | +right operand and has Gen do the operation; a constant on the left goes |
| 984 | +on after the right, and the two `swap`. A set's operations are |
| 985 | +operations on its bits (`setOp`). `=` and `#` on arrays, slices and |
| 986 | +strings make a slice of each side (`seqSlice`) and have Gen compare them |
| 987 | +right where the comparison stands: no call. `+` on two string constants |
| 988 | +is `joinStr`, which makes one string of them, in a chunk of its own. |
| 989 | + |
| 990 | +Variables: `deref`, `field`, `index` and `slice` turn an item for a |
| 991 | +variable into an item for part of it, mostly by adding to `off`. An item |
| 992 | +for another module's exported variable, or part of it, is `ro`: it isn't |
| 993 | +a variable (`isVar`), and `notReadOnly` says why where one is wanted; |
| 994 | +what it points at is. An array or record constant is a variable too, in |
| 995 | +read-only data: `aggregateConst` (`Parse.raz`) reads its literal, part |
| 996 | +by part in `constPart`, into the bytes of a chunk, as a string |
| 997 | +constant's are; an address in it, of a string, an array constant, a |
| 998 | +procedure or an `ERROR`, becomes a data fixup. `makeItem` makes it an |
| 999 | +`mVar` there that isn't `isVar`, so it is read, indexed, sliced and |
| 1000 | +passed, never changed. `index` and `slice` have Gen make the bounds |
| 1001 | +checks, unless the module says `check off;` (`checks`, set by |
| 1002 | +`setChecks`, which `compileFile` sets for each module), and `convert` |
| 1003 | +the check of an enumeration's position. |
| 978 | 1004 | |
| 979 | | -Assignment and conversion: `Coerce` makes a value fit the type it's |
| 1005 | +Assignment and conversion: `coerce` makes a value fit the type it's |
| 980 | 1006 | going to, or reports that it can't; it's where a string literal becomes |
| 981 | | -a slice, an array a slice, an untyped constant a typed one, and an |
| 982 | | -`INT` an `I64` (`Widens`). `CoerceExact` is for where the types must |
| 983 | | -be the same, as in a `for` loop, `INC` and several results. `Assign` |
| 984 | | -stores, or copies records and arrays. `Convert` is the written |
| 985 | | -conversion, `T(x)`. |
| 1007 | +a slice, an array a slice, an untyped constant a typed one, and an `INT` |
| 1008 | +an `I64` (`widens`). `coerceExact` is for where the types must be the |
| 1009 | +same, as in a `for` loop, `INC` and several results. `assign` stores, or |
| 1010 | +copies records and arrays. `convert` is the written conversion, `T(x)`. |
| 986 | 1011 | |
| 987 | | -Calls: `BeginCall`, then `PassArg` for each argument, then `EndCall`, |
| 988 | | -which has gen's `Call` make the call. Each argument is left on gen's |
| 989 | | -stack, a slice as its two words (`PushWords`). `return f(x);` passing |
| 990 | | -on f's several results, with `ReturnAll`, pushes them from the call's |
| 1012 | +Calls: `beginCall`, then `passArg` for each argument, then `endCall`, |
| 1013 | +which has Gen's `call` make the call. Each argument is left on Gen's |
| 1014 | +stack, a slice as its two words (`pushWords`). `return f(x);` passing on |
| 1015 | +f's several results, with `returnAll`, pushes them from the call's |
| 991 | 1016 | temporary if they are two words, or else copies them to the place its |
| 992 | 1017 | own hidden pointer names. |
| 993 | 1018 | |
| 994 | | -## parse.pas: the parser |
| 1019 | +## Parse.raz: the parser |
| 995 | 1020 | |
| 996 | 1021 | A recursive descent parser, one routine for each rule of the grammar in |
| 997 | | -the report, calling `item.pas`, and for jumps `gen.pas`, as it goes. |
| 1022 | +the report, calling `Item.raz`, and for jumps `Gen.raz`, as it goes. |
| 1023 | +Like the other modules it is written in raz: the Free Pascal seed built |
| 1024 | +it the first time, and it builds itself now. Its routines begin in |
| 1025 | +lower case, and each one that can find an error ends in an `ERROR` |
| 1026 | +result, passed up with `try` (see Recovery below). |
| 998 | 1027 | |
| 999 | | -Start with expressions, bottom up: `Factor`, `Term`, `SimpleExpr`, |
| 1000 | | -`Relation`, `Conjunct` (the `and`s), `Expression` (the `or`s). Then `Designator`, which reads a name and what follows |
| 1028 | +Start with expressions, bottom up: `factor`, `term`, `simpleExpr`, |
| 1029 | +`relation`, `conjunct` (the `and`s), `expression` (the `or`s). Then `designator`, which reads a name and what follows |
| 1001 | 1030 | it: `.field`, `^`, `[i]`, `[lo..hi]`, a call, a conversion. Each step |
| 1002 | | -calls the matching routine in `item.pas` on the item it has. The |
| 1031 | +calls the matching routine in `Item.raz` on the item it has. The |
| 1003 | 1032 | `want` parameter carries the type an expression must have, where it is |
| 1004 | | -known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build. |
| 1033 | +known, so that `[1, 2, 3]`, `{x: 1}` and `{red}` know what to build |
| 1034 | +(`arrayLiteral`, `recordLiteral`, `setLiteral`). |
| 1005 | 1035 | |
| 1006 | | -Then statements: `Statement` makes each one a recovery point (below) |
| 1007 | | -and `OneStatement` dispatches; `IfStatement`, |
| 1008 | | -`WhileStatement`, `ForStatement` and `MatchStatement` emit their jumps |
| 1036 | +Then statements: `statement` makes each one a recovery point (below) |
| 1037 | +and `oneStatement` dispatches; `ifStatement`, |
| 1038 | +`whileStatement`, `forStatement` and `matchStatement` emit their jumps |
| 1009 | 1039 | with gen's labels. A branch of an `if` that ends, in `return` or |
| 1010 | 1040 | `break` (`ends`), gets no jump past the others; and `if c then break; |
| 1011 | | -end` is one jump out of the loop where `c` holds (`ThenPart`, |
| 1012 | | -`LoneBreak`, `CondJumpTrue`), not a jump past a jump. A `for` loop |
| 1041 | +end` is one jump out of the loop where `c` holds (`thenPart`, |
| 1042 | +`loneBreak`, and `Item.condJumpTrue`), not a jump past a jump. A `for` loop |
| 1013 | 1043 | steps its variable at the top and tests at the bottom, so that a round |
| 1014 | 1044 | by 1 is one compare and one jump; its rare case, a body that moves the |
| 1015 | | -variable past the limit, has code of its own after (`ForNext`, whose |
| 1045 | +variable past the limit, has code of its own after (`Item.forNext`, whose |
| 1016 | 1046 | comment walks through the report's rule). The blank is a symbol of its own, `sBlank`, so |
| 1017 | 1047 | no declaration or expression takes it: `_ := e` loads a single value |
| 1018 | | -and drops it, and `MultiAssign` skips the results it stands for. A |
| 1048 | +and drops it, and `multiAssign` skips the results it stands for. A |
| 1019 | 1049 | `match` on an integer reads each arm's |
| 1020 | | -labels in `IntArm`, which refuses a value in two of them. The arms |
| 1050 | +labels in `intArm`, which refuses a value in two of them. The arms |
| 1021 | 1051 | come first and the test after them: the match starts with a jump to |
| 1022 | | -`Dispatch`, placed after the last arm, since a jump table can only be |
| 1052 | +`dispatch`, placed after the last arm, since a jump table can only be |
| 1023 | 1053 | made once every arm's place is known (an enumeration's arms are |
| 1024 | 1054 | picked the same way; a variant's, by its tag, arm by arm). |
| 1025 | | -`Dispatch` makes a jump table (gen's `JumpTable`, whose comment shows |
| 1055 | +`dispatch` makes a jump table (gen's `jumpTable`, whose comment shows |
| 1026 | 1056 | the code) for four labels or more that lie close together, else |
| 1027 | | -compares and branches one label after another with `MatchRange`, each |
| 1028 | | -jumping back to its arm. `Defer` is worth reading: a deferred statement |
| 1057 | +compares and branches one label after another with `Item.matchRange`, each |
| 1058 | +jumping back to its arm. `deferStatement` is worth reading: a deferred statement |
| 1029 | 1059 | is compiled where it stands, as a little subroutine of the procedure |
| 1030 | | -that the code jumps over (gen's `DeferBegin` and `DeferEnd`), and |
| 1031 | | -`RunDefers` emits a call to it at every way out of its statement list: |
| 1060 | +that the code jumps over (gen's `deferBegin` and `deferEnd`), and |
| 1061 | +`runDefers` emits a call to it at every way out of its statement list: |
| 1032 | 1062 | the end, `return` and `break`. A `return`'s result waits in the frame |
| 1033 | 1063 | meanwhile, so that gen's stack is empty when they run. |
| 1034 | 1064 | |
| 1035 | | -`try` (report 7.8) is `TryCall`, for the statement, an operand |
| 1036 | | -(`Factor`) and the right of a multiple assignment (`MultiAssign`). It |
| 1037 | | -reads the call as `Designator` does, takes the error, the last of its |
| 1065 | +`try` (report 7.8) is `tryCall`, for the statement, an operand |
| 1066 | +(`factor`) and the right of a multiple assignment (`multiAssign`). It |
| 1067 | +reads the call as `designator` does, takes the error, the last of its |
| 1038 | 1068 | results, out of the way into a slot of the frame, and leaves in `x` |
| 1039 | 1069 | the results that are left: none, one value, or a tuple of the others, |
| 1040 | | -`WithoutLast`. Then, in the code before and after, this: |
| 1070 | +`withoutLast`. Then, in the code before and after, this: |
| 1041 | 1071 | |
| 1042 | 1072 | ``` |
| 1043 | | - load [slot]; jz ok ; JumpIf(ok, false) |
| 1044 | | - and rsp, -16 ; TryAlign, if a deferred statement is in force |
| 1045 | | - call deferred... ; RunDefers(0), as for a return |
| 1046 | | - mov rax/rdx or the results ; TryReturn(slot): zeros, and the error last |
| 1073 | + load [slot]; jz ok ; Gen.jumpIf(ok, false) |
| 1074 | + and rsp, -16 ; Gen.tryAlign, if a deferred statement is in force |
| 1075 | + call deferred... ; runDefers(0), as for a return |
| 1076 | + mov rax/rdx or the results ; Gen.tryReturn(slot): zeros, and the error last |
| 1047 | 1077 | jmp exit |
| 1048 | 1078 | ok: ... |
| 1049 | 1079 | ``` |
| | @@ -1051,117 +1081,130 @@ |
| 1051 | 1081 | Unlike `return`, which is a statement and finds gen's stack empty, |
| 1052 | 1082 | `try` can stand in the middle of an expression, with values waiting |
| 1053 | 1083 | on gen's stack and quadwords pushed for another call's arguments. The |
| 1054 | | -out-of-line code therefore makes no entry on the stack: `TryReturn` |
| 1055 | | -loads registers and jumps, and `TryAlign` makes the machine's stack |
| 1084 | +out-of-line code therefore makes no entry on the stack: `tryReturn` |
| 1085 | +loads registers and jumps, and `tryAlign` makes the machine's stack |
| 1056 | 1086 | even for the deferred statements, which expect it so. The code that |
| 1057 | 1087 | follows is the code for an error that is nil, with the stack as it |
| 1058 | 1088 | was, so no model needs resetting. |
| 1059 | 1089 | |
| 1060 | | -Then declarations: `Declarations` reads `const`, `type`, `var`, `proc` |
| 1061 | | -and `extern`, each one through `Declaration`, a recovery point, which |
| 1062 | | -calls `ConstDecl`, `TypeDecl`, `VarDecl`, `ProcDecl` or `ExternDecl`. `ProcDecl` handles forward declarations, code procedures |
| 1063 | | -(`CodeBody`, which checks the signature, a slice parameter counting as |
| 1090 | +Then declarations: `declarations` reads `const`, `type`, `var`, `proc` |
| 1091 | +and `extern`, each one through `declaration`, a recovery point, which |
| 1092 | +calls `constDecl`, `typeDecl`, `varDecl`, `procDecl` or `externDecl` |
| 1093 | +(by `declarationOf`). `procDecl` handles forward declarations, code procedures |
| 1094 | +(`codeBody`, which checks the signature, a slice parameter counting as |
| 1064 | 1095 | two of the six integer registers and a slice result going in rax and |
| 1065 | | -rdx, and has gen's `CodeProc` run |
| 1066 | | -the target's assembler) and bodies (`Body`). |
| 1067 | | -`ParseType` and its helpers (`RecordType`, `VariantType`, `Signature`, |
| 1068 | | -`EnumType`) build `TType`s. |
| 1096 | +rdx, and has gen's `codeProc` run |
| 1097 | +the target's assembler) and bodies (`body`). |
| 1098 | +`parseType` and its helpers (`recordType`, `variantType`, `signature`, |
| 1099 | +`enumType`) build `Sym.Type`s. |
| 1069 | 1100 | |
| 1070 | | -Last, modules, at the end of the file. `CompileFile` reads one module. |
| 1071 | | -`ImportModule` finds a module's file, trying the target's names first, |
| 1072 | | -and compiles it if it isn't yet. `CheckOneModule` first makes sure no |
| 1101 | +Last, modules, at the end of the file. `compileFile` reads one module. |
| 1102 | +`importModule` finds a module's file, trying the target's names first, |
| 1103 | +and compiles it if it isn't yet. `checkOneModule` first makes sure no |
| 1073 | 1104 | later directory has the module too, unless the file is a link to one |
| 1074 | | -of its files there; `CheckSiblings` then reads the files for other |
| 1075 | | -targets beside it, declarations only, and `CompareIface` holds them |
| 1105 | +of its files there; `checkSiblings` then reads the files for other |
| 1106 | +targets beside it, declarations only, and `compareIface` holds them |
| 1076 | 1107 | to the same exports by their fingerprints. A generic module is |
| 1077 | 1108 | compiled once per distinct list of arguments, where it is first used. |
| 1078 | | -The import only notes where its arguments are (`SkipArgs`, `deferred`); |
| 1079 | | -`FirstUse`, from `QualIdent`, has `MakeInstance` go back there, where |
| 1080 | | -`ParseArgs` reads them, then compile the instance. `Params` binds them |
| 1081 | | -to the module's parameters, and `IsModule` decides whether an instance |
| 1109 | +The import only notes where its arguments are (`skipArgs`, `deferred`); |
| 1110 | +`firstUse`, from `qualIdent`, has `makeInstance` go back there, where |
| 1111 | +`parseArgs` reads them, then compile the instance. `moduleParams` binds them |
| 1112 | +to the module's parameters, and `isModule` decides whether an instance |
| 1082 | 1113 | already exists. An instance can't be compiled in the middle of another |
| 1083 | 1114 | procedure's code, since the code buffer is one run of bytes, so |
| 1084 | | -`InstancesFor` reads ahead through a procedure, or the module's body, |
| 1115 | +`instancesFor` reads ahead through a procedure, or the module's body, |
| 1085 | 1116 | before its code begins, and makes the instances it names. |
| 1086 | 1117 | |
| 1087 | 1118 | **Recovery.** After an error the parser carries on, so that one run |
| 1088 | | -reports them all. `Guard` notes, at the start of a statement or |
| 1089 | | -declaration, the scanner's state and the parser's; `setjmp` marks the |
| 1090 | | -place, and `MarkAt` jumps back to it after reporting. `Restore` puts |
| 1119 | +reports them all. The Pascal seed did it with `setjmp` and `longjmp`; |
| 1120 | +raz has neither, so here an error is a value. `Scan.mark` and the |
| 1121 | +routines like it show the message and return the `ERROR` `Scan.Failed`; |
| 1122 | +each routine that calls one passes it up with `try`, until it reaches a |
| 1123 | +recovery point, `statement` or `declaration`, which looks at what came |
| 1124 | +back. At the start of its construct it called `guard`, which notes in a |
| 1125 | +`Recover` the scanner's state and the parser's (the scopes, gen's stack, |
| 1126 | +the loop, the deferred statements, `quiet`). On an error `restore` puts |
| 1091 | 1127 | the state back, the construct's first symbol current again, and |
| 1092 | | -`SkipConstruct` reads past it, counting the words that open and close |
| 1128 | +`skipConstruct` reads past it, counting the words that open and close |
| 1093 | 1129 | blocks. A symbol at the start of a line ends the skip: by the usual |
| 1094 | 1130 | layout it starts the next declaration, so the statement gives up its |
| 1095 | | -procedure (`GiveUp`), and `SkipProc` reads to the procedure's |
| 1096 | | -`end Name;`. A name not declared is reported by `NotDeclared`, with |
| 1097 | | -what it may have meant: a module to import (`ModuleFile`) or a name |
| 1098 | | -spelled alike. It is then declared where it is used, with the type |
| 1099 | | -`tPoison` (`Poison`), as are the names of a declaration that had an |
| 1100 | | -error; a construct that uses poison sets `quiet`, and its errors |
| 1101 | | -aren't reported, since they follow from one that was. Nothing breaks |
| 1102 | | -for the half-made code: once there is an error, no executable is |
| 1103 | | -written. |
| 1131 | +procedure (`giveUp`), which passes the error on up to the next |
| 1132 | +recovery point, the declaration of the procedure, and `skipProc` |
| 1133 | +reads to the procedure's `end Name;`. A declaration that is not a |
| 1134 | +procedure is skipped by `skipDeclaration`. `guards` counts the recovery |
| 1135 | +points open: `giveUp` with none left ends the compile. A routine that |
| 1136 | +can't fail has no `ERROR` result (`declarations`, `declaration`). A name not declared is reported by |
| 1137 | +`notDeclared`, with what it may have meant: a module to import |
| 1138 | +(`moduleFile`) or a name spelled alike (`didYouMean`). It is then |
| 1139 | +declared where it is used, with the type `tPoison` (`poison`), as are |
| 1140 | +the names of a declaration that had an error; a construct that uses |
| 1141 | +poison sets `Diag.quiet` (by `Diag.setQuiet`, since a variable |
| 1142 | +exported by another module is read-only), and its errors aren't |
| 1143 | +reported, since they follow from one that was. Nothing breaks for the |
| 1144 | +half-made code: once there is an error, no executable is written. |
| 1104 | 1145 | |
| 1105 | 1146 | **Hints.** Some messages guess at the mistake from the layout. A |
| 1106 | | -block statement passes `CloseEnd` its start and how far its line is |
| 1147 | +block statement passes `closeEnd` its start and how far its line is |
| 1107 | 1148 | indented; an `end` further left was most likely meant for a block |
| 1108 | | -around it, so the first such statement is kept, and `EndMissing` |
| 1149 | +around it, so the first such statement is kept, and `endMissing` |
| 1109 | 1150 | notes it when a procedure's own `end` turns out to be missing. A type |
| 1110 | | -error in an argument goes through `Mismatch`, which notes the |
| 1111 | | -parameter it goes to (`passing`, set by `PassArg`). |
| 1151 | +error in an argument goes through `Item.mismatch`, which notes the |
| 1152 | +parameter it goes to (`Item.passing`, set by `Item.passArg`). |
| 1112 | 1153 | |
| 1113 | | -**Warnings.** `Declare` keeps each module's declarations in order in |
| 1114 | | -two lists, through `link`: its own and its procedures', parameters |
| 1115 | | -and a match's names for fields among them. `Named` marks an object |
| 1154 | +**Warnings.** `declare` keeps each module's declarations in order in |
| 1155 | +two lists, through `chain`: its own (`first`) and its procedures' |
| 1156 | +(`locals`), parameters |
| 1157 | +and a match's names for fields among them. `named` marks an object |
| 1116 | 1158 | `used` and `read` wherever a name finds it, except a procedure naming |
| 1117 | | -itself; `LeftSide` puts `read` back as it was for a variable named |
| 1159 | +itself; `leftSide` puts `read` back as it was for a variable named |
| 1118 | 1160 | whole before `:=`, which is written, not read. At the end of a module |
| 1119 | | -with no errors, `Warnings` merges the two lists by position and `Warn` |
| 1161 | +with no errors, `warnings` merges the two lists by position and `warn` |
| 1120 | 1162 | reports what isn't exported, doesn't start with `_`, and was never |
| 1121 | 1163 | used, or, for a variable, only assigned, and a procedure with results |
| 1122 | 1164 | that may reach its end (`openEnd`, from `ends`, which each statement |
| 1123 | 1165 | sets). The calls that drop results are a third list, `drops`, in |
| 1124 | | -order: `CompileFile` notes where the module's own begin, since the |
| 1125 | | -instances it makes along the way add theirs and take them back. A |
| 1166 | +order: `compileFile` notes where the module's own begin (`numDrops`), |
| 1167 | +since the instances it makes along the way add theirs and take them |
| 1168 | +back. A |
| 1126 | 1169 | generic module's instances |
| 1127 | 1170 | share its text, so only the first is warned of. |
| 1128 | 1171 | |
| 1129 | | -The built-in procedures are in `Builtin` (and `New_`, `Atomic`): each |
| 1172 | +The built-in procedures are in `builtin` (and `new_`, `atomic`): each |
| 1130 | 1173 | parses its own arguments, since `SIZE` takes a type and `INC` a |
| 1131 | 1174 | variable. |
| 1132 | 1175 | |
| 1133 | 1176 | **Register variables.** One pass can't know whether a variable's |
| 1134 | | -address will be taken before its code is made, so `ChooseRegs` reads |
| 1135 | | -the body ahead, as `InstancesFor` does, and counts each variable's |
| 1177 | +address will be taken before its code is made, so `chooseRegs` reads |
| 1178 | +the body ahead, as `instancesFor` does, and counts each variable's |
| 1136 | 1179 | uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three |
| 1137 | 1180 | deep or more); one `ADR` takes is out. The |
| 1138 | 1181 | busiest used at least three times get the five registers (`o^.reg`), |
| 1139 | 1182 | a slice two (`o^.reg2` too), its count halved for each, and the |
| 1140 | | -busiest reals the four of `XVarRegs`, each with a slot as well |
| 1141 | | -(`PlaceLocals`, `XHome`). An |
| 1183 | +busiest reals the four of `Gen.XVarRegs`, each with a slot as well |
| 1184 | +(`placeLocals`, `Gen.xHome`). An |
| 1142 | 1185 | address wanted after all, by a `var` parameter, say, comes to |
| 1143 | | -`Address` (`item.pas`), which puts the variable back in memory and |
| 1144 | | -sets `demoted`: `Body` then throws the procedure's code away |
| 1145 | | -(`MarkCode`, `Redo`), places the locals again, and compiles its |
| 1186 | +`Item.address`, which puts the variable back in memory and |
| 1187 | +sets `Item.demoted`: `body` then throws the procedure's code away |
| 1188 | +(`Gen.markCode`, `Gen.redo`), places the locals again, and compiles its |
| 1146 | 1189 | statements again. Its notes for |
| 1147 | | -the trap report are made after, by `VarKind`, which marks a variable |
| 1190 | +the trap report are made after, by `varKind`, which marks a variable |
| 1148 | 1191 | in a register with the register's number. |
| 1149 | 1192 | |
| 1150 | | -## link.pas: tree shaking and layout |
| 1193 | +## Link.raz: tree shaking and layout |
| 1151 | 1194 | |
| 1152 | | -`MarkLive` starts from the entry chunk and follows fixups, marking each |
| 1195 | +`markLive` starts from the entry chunk and follows fixups, marking each |
| 1153 | 1196 | chunk it reaches, through the data fixups of constants too; nothing |
| 1154 | | -else goes into the executable. `LinkAll` |
| 1155 | | -places the live chunks section by section (`Place`), each at its |
| 1197 | +else goes into the executable. `linkAll` |
| 1198 | +places the live chunks section by section (`place`), each at its |
| 1156 | 1199 | alignment, then copies their bytes into the images of text and rodata, |
| 1157 | 1200 | patching every fixup with the distance from the end of its field to its |
| 1158 | 1201 | target, and every data fixup with its target's address. |
| 1159 | 1202 | |
| 1160 | | -Between placing text and placing rodata, `MakePlaces` writes the |
| 1203 | +Between placing text and placing rodata, `makePlaces` writes the |
| 1161 | 1204 | places, a rodata chunk: for the trap report, each live procedure's |
| 1162 | | -address range and name, and its variables, which `Body` (`parse.pas`) |
| 1163 | | -notes with `NoteVar`: each one's name, its place in the frame and how |
| 1164 | | -`Rt` shows it (`VarKind`); then the line table (`MakeLines`), DWARF |
| 1205 | +address range and name, and its variables, which `Parse.body` |
| 1206 | +notes with `Chunk.noteVar`: each one's name, its place in the frame and how |
| 1207 | +`Rt` shows it (`varKind`); then the line table (`makeLines`), DWARF |
| 1165 | 1208 | 4's line program, which gdb reads as `.debug_line`: one sequence of |
| 1166 | 1209 | rows for each procedure, a row for each line note in live code, its |
| 1167 | 1210 | address, file, line and column. A row that moves the address and the |
| | @@ -1184,12 +1227,12 @@ |
| 1184 | 1227 | own code, and `exit` has `profile` say which procedures and places |
| 1185 | 1228 | those fell in, by the same tables: each procedure's samples in its own |
| 1186 | 1229 | code, and its total, counting once each sample it was on the chain of. |
| 1187 | | -The entry point gives Rt what the program has (`TLink`): the places, |
| 1230 | +The entry point gives Rt what the program has (`Gen.Link`): the places, |
| 1188 | 1231 | `calls`, which tells the calls under way, `place`, which says where an |
| 1189 | 1232 | address is, and the profiler's start. What it isn't given, nothing |
| 1190 | 1233 | reaches, so the linker leaves it out: `raz -noprofile` gives no |
| 1191 | 1234 | profiler, and `raz -s` none of them, whose failures say only their |
| 1192 | | -address (and `elf.pas` writes no symbols or line numbers). Hello is 28 |
| 1235 | +address (and `Elf.raz` writes no symbols or line numbers). Hello is 28 |
| 1193 | 1236 | KB, 21 KB without the profiler, and 7 KB stripped. |
| 1194 | 1237 | |
| 1195 | 1238 | The layout is chosen so the file can be mapped as it lies, with no |
| | @@ -1199,9 +1242,9 @@ |
| 1199 | 1242 | segments, with their different permissions, share a page. bss follows |
| 1200 | 1243 | the loader's table, or begins a page of its own. |
| 1201 | 1244 | |
| 1202 | | -## elf.pas: the executable |
| 1245 | +## Elf.raz: the executable |
| 1203 | 1246 | |
| 1204 | | -`WriteElf` writes the ELF header, the program headers, the text and |
| 1247 | +`writeElf` writes the ELF header, the program headers, the text and |
| 1205 | 1248 | rodata images, and then what isn't loaded: section headers, a symbol |
| 1206 | 1249 | table with every live procedure and variable by `Module.name`, and |
| 1207 | 1250 | the compile unit that names the line table. The line table is in the |
| | @@ -1211,34 +1254,36 @@ |
| 1211 | 1254 | loader accepts: an interpreter header naming the loader; a dynamic |
| 1212 | 1255 | section naming the libraries; and, for each C symbol the program uses, |
| 1213 | 1256 | a symbol and a `GLOB_DAT` relocation that has the loader write the |
| 1214 | | -symbol's address into the quadword `ExternChunk` made. Code calls C |
| 1257 | +symbol's address into the quadword `Chunk.externChunk` made. Code calls C |
| 1215 | 1258 | through that quadword. There is no PLT and no lazy binding. |
| 1216 | | -`SizeTables` and `PutTables` lay these out in the room `link.pas` left |
| 1259 | +`sizeTables` and `putTables` lay these out in the room `Link.raz` left |
| 1217 | 1260 | at the end of rodata. |
| 1218 | 1261 | |
| 1219 | | -Line numbers are DWARF 4: the line table `MakeLines` made from the |
| 1220 | | -notes `NoteLine` made, and the single compile unit gdb needs to find |
| 1221 | | -it, which `PutDebug` writes. There are no types or variables in it; |
| 1262 | +Line numbers are DWARF 4: the line table `makeLines` made from the |
| 1263 | +notes `Chunk.noteLine` made, and the single compile unit gdb needs to find |
| 1264 | +it, which `putDebug` writes. There are no types or variables in it; |
| 1222 | 1265 | gdb can stop at a line and show where it is, not print a variable. |
| 1223 | 1266 | |
| 1224 | | -## raz.pas: the command line |
| 1267 | +## Raz.raz: the command line |
| 1225 | 1268 | |
| 1226 | | -`raz.pas` parses the arguments, calls `CompileMain`, and works out what |
| 1227 | | -the program uses: `Use` marks the modules imported from the main module |
| 1228 | | -and `Rt`; `UsesC` decides whether the program is dynamic, refusing one |
| 1269 | +`Raz.raz` parses the arguments, calls `Parse.compileMain`, and works out what |
| 1270 | +the program uses: `use` marks the modules imported from the main module |
| 1271 | +and `Rt`; `usesC` decides whether the program is dynamic, refusing one |
| 1229 | 1272 | that both uses C and has a module saying `link static`. Then it has |
| 1230 | | -gen's `EntryPoint` write the entry point, and `MarkLive` mark what it |
| 1231 | | -reaches. On a target |
| 1232 | | -without reals (`HasReals`, beside `Target` in `parse.pas`; x86-64 has |
| 1273 | +gen's `entryPoint` write the entry point, and `Link.markLive` mark what it |
| 1274 | +reaches. The program is `proc main*: INT`; where raz is, for the library |
| 1275 | +beside it, it reads from `/proc/self/exe`, and the version it prints is |
| 1276 | +the module `Version`, which the build makes from `git describe`. On a target |
| 1277 | +without reals (`HasReals`, a constant of `Parse`; x86-64 has |
| 1233 | 1278 | them), a live chunk that computes with one is an error at the first |
| 1234 | | -place it does: `UsesReals` in `item.pas` notes it, from `Value` and a |
| 1279 | +place it does: `Item.usesReals` notes it, from `Item.value` and a |
| 1235 | 1280 | real literal, on the chunk of the procedure being compiled. A real |
| 1236 | 1281 | moved whole, as a record's bytes are, isn't noted. A static program starts at `_start` itself. A dynamic |
| 1237 | 1282 | one starts at a `_start` that calls `__libc_start_main` the way C's own |
| 1238 | 1283 | start-up does, which sets C up and calls back a `main` that runs the |
| 1239 | 1284 | module bodies and the program's `main`. |
| 1240 | 1285 | |
| 1241 | | -`raz -S` lists the code (`List`, in `x64.pas`) instead of writing |
| 1286 | +`raz -S` lists the code (`X64.list`) instead of writing |
| 1242 | 1287 | it. `raz run` writes the executable to a memfd, a file only in memory, and |
| 1243 | 1288 | executes it through `/proc/self/fd` in raz's own place, handing it the |
| 1244 | 1289 | rest of raz's `argv`. Nothing is left on disk, and the program's |
| | @@ -1259,23 +1304,23 @@ |
| 1259 | 1304 | a link to it (`make install` puts `raz` and `lib/` together in |
| 1260 | 1305 | `PREFIX/lib/raz`). In each directory it tries `X.linux-x86_64.raz`, |
| 1261 | 1306 | `X.linux.raz`, `X.x86_64.raz` and `X.raz`, in that order, and takes |
| 1262 | | - the first found (`ModuleFile`, `parse.pas`). |
| 1307 | + the first found (`moduleFile`, `Parse.raz`). |
| 1263 | 1308 | - **Errors.** Every error in a run is reported, up to 20 (`MaxErrors`, |
| 1264 | | - `diag.pas`); then the compiler stops. Two errors at the same place |
| 1309 | + `Diag.raz`); then the compiler stops. Two errors at the same place |
| 1265 | 1310 | are told as one. Any error is status 1. |
| 1266 | 1311 | - **Warnings.** Of a declaration never used, or a variable only |
| 1267 | | - assigned (`Warn`, `parse.pas`): not of what is exported, a name that |
| 1312 | + assigned (`warn`, `Parse.raz`): not of what is exported, a name that |
| 1268 | 1313 | starts with `_`, an enumeration's names or a generic module's |
| 1269 | 1314 | parameters. A procedure naming itself doesn't use it; a type named by |
| 1270 | 1315 | a pointer in its own declaration does. Assigning a variable whole |
| 1271 | 1316 | (`x :=`, `a, b :=`) doesn't read it; anything else does. Warnings |
| 1272 | 1317 | come only for a module with no errors, and a generic module's only |
| 1273 | 1318 | for its first instance. A procedure with results is warned of if |
| 1274 | | - it may reach its end without `return` (`ends`, `parse.pas`): each |
| 1319 | + it may reach its end without `return` (`ends`, `Parse.raz`): each |
| 1275 | 1320 | way through its statements must end in a `return`, an `if` with an |
| 1276 | 1321 | `else`, a `match`, a `while true` or a `repeat ... until false` |
| 1277 | 1322 | with no `break`. So is a call standing on its own that drops a |
| 1278 | | - result (`drops`, kept by `OneStatement` and told by `Dropped`); |
| 1323 | + result (`drops`, kept by `noteDrop` and told by `dropped`); |
| 1279 | 1324 | `_ := f(x)` drops one on purpose. `-Werror` makes each an error. |
| 1280 | 1325 | - **Registers for variables.** Up to five of a procedure's single |
| 1281 | 1326 | values, not reals, whose address is never taken, live in `rbx` and |
| | @@ -1317,24 +1362,29 @@ |
| 1317 | 1362 | module body runs. |
| 1318 | 1363 | - **Frames.** A deferred statement's slots stay reserved for the rest |
| 1319 | 1364 | of its procedure. A generic instance first used in a procedure, or in |
| 1320 | | - a module's body, is made before that code begins (`InstancesFor`). |
| 1365 | + a module's body, is made before that code begins (`instancesFor`). |
| 1321 | 1366 | - **The executable** carries DWARF line numbers and the names of |
| 1322 | 1367 | procedures for gdb, and the places (`Rt.places`), which hold the |
| 1323 | 1368 | line table, DWARF's, that gdb reads and `Rt` runs. |
| 1324 | 1369 | |
| 1325 | 1370 | ## Where to start changing it |
| 1326 | 1371 | |
| 1327 | | -- **A built-in procedure:** an entry in `sym.pas`'s universe, a case in |
| 1328 | | - `Builtin` in `parse.pas`, and the code, in `gen.pas` if it is an |
| 1372 | +- **A built-in procedure:** an entry in `Sym.raz`'s universe (`enter`), |
| 1373 | + a case in `builtin` in `Parse.raz`, and the code, in `Gen.raz` if it is an |
| 1329 | 1374 | instruction, or of calls to gen right there. |
| 1330 | | -- **An instruction:** a routine in `x64.pas` built on `OpM` or `OpR`, |
| 1331 | | - and a line in `test/enctest.pas` to check its encoding. |
| 1332 | | -- **A machine:** a new `gen.pas`, answering the same calls, and an |
| 1333 | | - encoder and assembler for it in place of `x64.pas`. |
| 1334 | | -- **A statement:** a reserved word in `scan.pas` (in `TSym` and `Words`, |
| 1335 | | - in the same order), a case in `OneStatement`, and a routine beside the |
| 1375 | +- **An instruction:** a routine in `X64.raz` built on `opM` or `opR`, |
| 1376 | + and a line in `test/enctest.pas` to check its encoding (the seed's |
| 1377 | + test; `test/X64Dump` is the port's). |
| 1378 | +- **A machine:** a new `Gen.raz`, answering the same calls, and an |
| 1379 | + encoder and assembler for it in place of `X64.raz`. |
| 1380 | +- **A statement:** a reserved word in `Scan.raz` (in `Sym` and `words`, |
| 1381 | + in the same order), a case in `oneStatement`, and a routine beside the |
| 1336 | 1382 | others. |
| 1337 | | -- **A check that fails:** `Check` in `gen.pas`, and the message in |
| 1383 | +- **A check that fails:** `check` in `Gen.raz`, and the message in |
| 1338 | 1384 | `lib/Rt.raz`'s `fail`; a reason a signal gives, `onSignal` there. |
| 1339 | 1385 | |
| 1340 | | -After any change: `make check`, `make bench`, `make loc`. |
| 1386 | +The compiler is in raz, and `make raz` builds it with the seed, the |
| 1387 | +frozen Pascal compiler in `seed/`, which is never edited to suit it; |
| 1388 | +`make fixpoint` has it build itself twice more and checks that the last |
| 1389 | +two are the same file. After any change: `make check`, `make bench`, |
| 1390 | +`make loc`. |