src: Gen.x86_64, Exe.linux and Host.linux, the target's units; src/cross.sh PLAN.md 5, step 3. A second target's files go beside these, chosen by section 3's suffixes in src/ as in lib/. Elf is Exe, the executable, and its writeElf is write. The target's name comes from Gen.Arch and Exe.Os (Parse.setTarget), no longer a fixed list. -S's listing goes through Gen, so only Gen knows X64. src/cross.sh stages a compiler for another target. What the compiler makes is unchanged: 202 programs compared byte for byte with HEAD's; the fixpoint holds.
| @@ -208,7 +208,8 @@ | ||
| 208 | 208 | written in it. A name that is taken is renamed, and nothing is said of |
| 209 | 209 | it. |
| 210 | 210 | |
| 211 | -The parser reaches the machine only through `Gen.raz`. Keep | |
| 211 | +The parser reaches the machine only through `Gen` (`src/Gen.x86_64.raz`), | |
| 212 | +and the executable's format only through `Exe` (`src/Exe.linux.raz`). Keep | |
| 212 | 213 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 213 | 214 | compiler changes. |
| 214 | 215 |
| @@ -158,14 +158,14 @@ | ||
| 158 | 158 | it and changes no language (section 3, appendix A). |
| 159 | 159 | |
| 160 | 160 | **The shape.** `Gen` is the only unit that knows the machine, and |
| 161 | -`Elf` the only one that knows the executable, so the port is new | |
| 161 | +`Exe` (`Exe.linux.raz`, once `Elf`) the only one that knows the executable, so the port is new | |
| 162 | 162 | units beside them, not a change to `Parse` or `Item`: |
| 163 | 163 | |
| 164 | 164 | - `A64.raz`, beside `X64.raz`: arm64's instructions encoded, branches |
| 165 | 165 | within a procedure, and the assembler for code procedures (Arm's |
| 166 | 166 | syntax, the instructions the library's code procedures use; `db` for |
| 167 | 167 | any other). |
| 168 | -- `Gen.arm64.raz`, beside `Gen.raz` (to be `Gen.x86_64.raz`): the same | |
| 168 | +- `Gen.arm64.raz`, beside `Gen.x86_64.raz`: the same | |
| 169 | 169 | calls, the stack machine kept in arm64's registers; Apple's calling |
| 170 | 170 | convention (x18 never touched, a frame record in x29, the arguments |
| 171 | 171 | past a variadic C procedure's own on the stack); every address by |
| @@ -176,7 +176,7 @@ | ||
| 176 | 176 | program (arm64's `sdiv` gives 0), so it is checked; a real out of an |
| 177 | 177 | `I64`'s range, or a NaN, gives the smallest `I64` (arm64's `fcvtzs` |
| 178 | 178 | saturates), so it is mended after. |
| 179 | -- `MachO.raz`, beside `Elf.raz`: a Mach-O executable for dyld, linked | |
| 179 | +- `Exe.darwin.raz`, beside `Exe.linux.raz`: a Mach-O executable for dyld, linked | |
| 180 | 180 | to libSystem, as every macOS program must be; C's symbols bound into |
| 181 | 181 | a table of quadwords, as GLOB_DAT does now; every address in data |
| 182 | 182 | rebased, since the program is position-independent; its symbol table |
| @@ -187,7 +187,7 @@ | ||
| 187 | 187 | `_NSGetExecutablePath`, C always, a program run from a file written |
| 188 | 188 | and made executable (no `memfd_create`). |
| 189 | 189 | - `Link`: the places in data that hold an address, listed for |
| 190 | - `MachO`'s rebasing. | |
| 190 | + `Exe.darwin.raz`'s rebasing. | |
| 191 | 191 | |
| 192 | 192 | A compiler compiles for the system it runs on, as now: its target is |
| 193 | 193 | the one it was built for, its back end and executable chosen at its |
| @@ -217,13 +217,14 @@ | ||
| 217 | 217 | the budget raised to 18,000 in `CLAUDE.md` and `doc/compiler.md`, |
| 218 | 218 | and the Makefile and scripts made to run on BSD's tools as on GNU's |
| 219 | 219 | (`sed -i`, `stat`, `sha256sum`). |
| 220 | -3. `Gen.raz` to `Gen.x86_64.raz`, `Elf` behind a unit of the target's | |
| 221 | - (`Exe`), `src/cross.sh`: Linux's compiler unchanged, byte for byte. | |
| 220 | +3. Done. `Gen.raz` to `Gen.x86_64.raz`, `Elf.raz` to `Exe.linux.raz` (the | |
| 221 | + unit `Exe`), `Host.raz` to `Host.linux.raz`, `src/cross.sh`: what | |
| 222 | + Linux's compiler makes unchanged, byte for byte. | |
| 222 | 223 | 4. `A64.raz`, its encoder checked against `llvm-mc`'s bytes. |
| 223 | 224 | 5. `Gen.arm64.raz`, in pieces: values and operations; jumps and |
| 224 | 225 | checks; calls and frames; reals; atomics, deferred statements, the |
| 225 | 226 | entry point. |
| 226 | -6. `MachO.raz` and `Link`'s rebases, its line numbers for lldb; | |
| 227 | +6. `Exe.darwin.raz` and `Link`'s rebases, its line numbers for lldb; | |
| 227 | 228 | `Host.darwin.raz`. |
| 228 | 229 | 7. The library: `CAbi`, `Sys`, `Rt`, `Thread`, `Bytes`, `Math`, `Str`. |
| 229 | 230 | 8. `test/` and `conform/` passing on the Mac; what differs by target |
| @@ -242,10 +243,10 @@ | ||
| 242 | 243 | d. The port is v0.26.0; the clean-up and 1.0 move one on. |
| 243 | 244 | e. Line numbers for lldb are in v0.26.0 (below). |
| 244 | 245 | |
| 245 | -**Line numbers on the Mac.** `Elf` writes DWARF 4 already: the line | |
| 246 | +**Line numbers on the Mac.** `Exe.linux.raz` writes DWARF 4 already: the line | |
| 246 | 247 | table `makeLines` makes, target-blind, and the one compile unit |
| 247 | 248 | `putDebug` writes. lldb reads DWARF from an executable's `__DWARF` |
| 248 | -segment, as it does Go's, so `MachO` writes the same three sections, | |
| 249 | +segment, as it does Go's, so `Exe.darwin.raz` writes the same three sections, | |
| 249 | 250 | `__debug_line`, `__debug_info` and `__debug_abbrev`, there, unloaded, |
| 250 | 251 | before `__LINKEDIT`; `make gdb`'s twin drives `lldb --batch`. About 50 |
| 251 | 252 | lines, `makeLines` and `putDebug` shared by the two writers. |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | # Reading the raz compiler |
| 2 | 2 | |
| 3 | -The compiler is twelve files of raz, about 13,100 lines of code (the | |
| 3 | +The compiler is twelve files of raz, about 13,200 lines of code (the | |
| 4 | 4 | budget is 18,000). It is its own compiler: a release of it, the one |
| 5 | 5 | `BOOT` names, builds `src/`, and `make fixpoint` checks that what it |
| 6 | 6 | makes makes itself again, byte for byte. Each release was built by the |
| @@ -21,13 +21,13 @@ | ||
| 21 | 21 | | `src/Sym.raz` | 866 | 689 | Types, objects, scopes, layout | |
| 22 | 22 | | `src/Chunk.raz` | 364 | 280 | The compiled program: chunks, fixups, line notes | |
| 23 | 23 | | `src/X64.raz` | 1,777 | 1,450 | The instruction encoder, jumps within a procedure, the assembler | |
| 24 | -| `src/Gen.raz` | 2,846 | 1,989 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 24 | +| `src/Gen.x86_64.raz` | 2,860 | 1,998 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 25 | 25 | | `src/Item.raz` | 2,553 | 2,051 | Items: types, constants, variables, operators, conversions, calls | |
| 26 | -| `src/Parse.raz` | 4,922 | 4,179 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 26 | +| `src/Parse.raz` | 4,938 | 4,191 | Declarations, statements, expressions, built-ins, recovery, warnings | | |
| 27 | 27 | | `src/Link.raz` | 411 | 317 | Tree shaking, layout, patching, the places | |
| 28 | -| `src/Elf.raz` | 450 | 356 | Static and dynamic ELF, symbol table, line numbers | | |
| 29 | -| `src/Host.raz` | 103 | 52 | The system running the compiler | | |
| 30 | -| `src/Raz.raz` | 492 | 384 | The command line, and `-q` for an editor | | |
| 28 | +| `src/Exe.linux.raz` | 455 | 358 | The executable, ELF: static and dynamic, symbol table, line numbers | | |
| 29 | +| `src/Host.linux.raz` | 103 | 52 | The system running the compiler | | |
| 30 | +| `src/Raz.raz` | 493 | 385 | The command line, and `-q` for an editor | | |
| 31 | 31 | |
| 32 | 32 | Every file opens with a comment that says what it is, what it depends |
| 33 | 33 | on and what it leaves out. Read those first; they are the map. |
| @@ -46,12 +46,18 @@ | ||
| 46 | 46 | knows what the program means: each operand is an *item*, as in Wirth's |
| 47 | 47 | compilers, and the routines there check the items' types, fold |
| 48 | 48 | constants and work out where variables are. Neither knows the machine: |
| 49 | -they drive a stack machine, `Gen.raz`, by its calls (push a value, | |
| 49 | +they drive a stack machine, `Gen.x86_64.raz`, by its calls (push a value, | |
| 50 | 50 | add the two on top, store the top in a variable, jump if it is false), |
| 51 | 51 | each with the kind of the value (a word, a 64-bit integer, an `F32`, |
| 52 | -an `F64`). `Gen.raz` is the only one that knows x86-64; a port to | |
| 53 | -another machine is a new `Gen.raz`, with an encoder of its own in place | |
| 54 | -of `X64.raz`. | |
| 52 | +an `F64`). `Gen` is the only one that knows x86-64; a port to | |
| 53 | +another machine is a new `Gen`, with an encoder of its own in place | |
| 54 | +of `X64.raz`. Likewise `Exe` is the only one that knows the executable's | |
| 55 | +format, and `Host` the host's services. Each is a unit with a file per | |
| 56 | +target, which the suffixes of the report's section 3 choose | |
| 57 | +(`Gen.x86_64.raz`, `Exe.linux.raz`, `Host.linux.raz`); `Gen.Arch` and | |
| 58 | +`Exe.Os` name the target, `"x86_64"` and `"linux"`, and `Raz` gives them | |
| 59 | +to `Parse`, whose lookup of a module's file is by them. `src/cross.sh` | |
| 60 | +builds a compiler for another target by staging (see Host below). | |
| 55 | 61 | |
| 56 | 62 | On x86-64 the stack lives in registers, as in Wirth's compilers |
| 57 | 63 | (Niklaus Wirth's Pascal and Oberon compilers): the value at depth `d`, |
| @@ -102,11 +108,11 @@ | ||
| 102 | 108 | 4. Each procedure becomes a chunk of code; `Out.str("hello, world\n")` |
| 103 | 109 | becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. |
| 104 | 110 | 5. Back in `Raz.raz`, `use` marks the modules the program uses; the |
| 105 | - entry point, `_start`, is written by `Gen.raz`'s `entryPoint` as one | |
| 111 | + entry point, `_start`, is written by `Gen.x86_64.raz`'s `entryPoint` as one | |
| 106 | 112 | more chunk: save `argc` and `argv` and the address of the places |
| 107 | 113 | (below), call every used module's body, call `main`, call `Rt.exit`. |
| 108 | 114 | 6. `markLive` (`Link.raz`) marks what `_start` reaches; then |
| 109 | - `writeElf` (`Elf.raz`) has `Link.linkAll` place it and patch it, and | |
| 115 | + `Exe.write` (`Exe.linux.raz`) has `Link.linkAll` place it and patch it, and | |
| 110 | 116 | writes headers, code, data, symbols and line numbers. |
| 111 | 117 | |
| 112 | 118 | ## A procedure, compiled |
| @@ -137,7 +143,7 @@ | ||
| 137 | 143 | `scale` calls nothing, so its parameters stay in `rdi` and `rsi`, |
| 138 | 144 | where they arrive (`keepArrived`): they are register variables that |
| 139 | 145 | cost nothing to keep. Every line of it can be traced to a routine in |
| 140 | -`Gen.raz`: the moves are `loaded`'s, the shift and the add `op`'s, the | |
| 146 | +`Gen.x86_64.raz`: the moves are `loaded`'s, the shift and the add `op`'s, the | |
| 141 | 147 | result `returnJump`'s, and the frame is `enterBody`'s and `endProc`'s. |
| 142 | 148 | It is not the fastest code. It is code whose every instruction you |
| 143 | 149 | can account for, which is the point. |
| @@ -1529,7 +1535,7 @@ | ||
| 1529 | 1535 | field of a `nil` pointer past that page: an item reached through `^` is |
| 1530 | 1536 | `deref` until `nilTest` (`Item.raz`) has it used, and Gen's `checkNil` |
| 1531 | 1537 | tests the pointer only if the use reaches past `NilPage`. A port whose |
| 1532 | -machine catches less tests for more, in its own `Gen.raz`: `Rt.fail` | |
| 1538 | +machine catches less tests for more, in its own `Gen.x86_64.raz`: `Rt.fail` | |
| 1533 | 1539 | stops for a nil pointer, a division by zero and a stack overflow as the |
| 1534 | 1540 | signals would. |
| 1535 | 1541 | |
| @@ -1826,7 +1832,7 @@ | ||
| 1826 | 1832 | ## Parse.raz: the parser |
| 1827 | 1833 | |
| 1828 | 1834 | A recursive descent parser, one routine for each rule of the grammar in |
| 1829 | -the report, calling `Item.raz`, and for jumps `Gen.raz`, as it goes. | |
| 1835 | +the report, calling `Item.raz`, and for jumps `Gen.x86_64.raz`, as it goes. | |
| 1830 | 1836 | Like the other modules it is written in raz: the Free Pascal seed built |
| 1831 | 1837 | it the first time, and it builds itself now. Its routines begin in |
| 1832 | 1838 | lower case, and each one that can find an error ends in an `ERROR` |
| @@ -2199,7 +2205,7 @@ | ||
| 2199 | 2205 | address is, and the profiler's start. What it isn't given, nothing |
| 2200 | 2206 | reaches, so the linker leaves it out: `raz -noprofile` gives no |
| 2201 | 2207 | profiler, and `raz -s` none of them, whose failures say only their |
| 2202 | -address (and `Elf.raz` writes no symbols or line numbers). Hello is 28 | |
| 2208 | +address (and `Exe.linux.raz` writes no symbols or line numbers). Hello is 28 | |
| 2203 | 2209 | KB, 21 KB without the profiler, and 7 KB stripped. |
| 2204 | 2210 | |
| 2205 | 2211 | The layout is chosen so the file can be mapped as it lies, with no |
| @@ -2209,9 +2215,11 @@ | ||
| 2209 | 2215 | segments, with their different permissions, share a page. bss follows |
| 2210 | 2216 | the loader's table, or begins a page of its own. |
| 2211 | 2217 | |
| 2212 | -## Elf.raz: the executable | |
| 2218 | +## Exe.linux.raz: the executable | |
| 2213 | 2219 | |
| 2214 | -`writeElf` writes the ELF header, the program headers, the text and | |
| 2220 | +`Exe`, the executable, is the one unit that knows the file format of | |
| 2221 | +the target's system; this is Linux's, ELF. A macOS `Exe.darwin.raz` | |
| 2222 | +will be Mach-O. `Exe.write` writes the ELF header, the program headers, the text and | |
| 2215 | 2223 | rodata images, and then what isn't loaded: section headers, a symbol |
| 2216 | 2224 | table with every live procedure and variable by `Module.name`, and |
| 2217 | 2225 | the compile unit that names the line table. The line table is in the |
| @@ -2226,7 +2234,7 @@ | ||
| 2226 | 2234 | loader and the first library are glibc's, |
| 2227 | 2235 | `/lib64/ld-linux-x86-64.so.2` and `libc.so.6`, or musl's, |
| 2228 | 2236 | `/lib/ld-musl-x86_64.so.1` and `libc.so`, which musl's loader, being |
| 2229 | -the library itself, takes to mean itself (`Elf.setMusl`). `-libc` | |
| 2237 | +the library itself, takes to mean itself (`Exe.setMusl`). `-libc` | |
| 2230 | 2238 | chooses; left out, `Host.cLibrary` does: musl where the machine has |
| 2231 | 2239 | its loader and not glibc's, as Alpine does. A symbol that isn't live |
| 2232 | 2240 | gets no relocation, so a program runs over a C library that lacks a |
| @@ -2238,11 +2246,11 @@ | ||
| 2238 | 2246 | it, which `putDebug` writes. There are no types or variables in it; |
| 2239 | 2247 | gdb can stop at a line and show where it is, not print a variable. |
| 2240 | 2248 | |
| 2241 | -## Host.raz: the system running the compiler | |
| 2249 | +## Host.linux.raz: the system running the compiler | |
| 2242 | 2250 | |
| 2243 | 2251 | The host is the system running raz; the target is the system its |
| 2244 | 2252 | program will run on. Everything the compiler asks of its host that the |
| 2245 | -library doesn't give is in `Host.raz`, five procedures, each a real | |
| 2253 | +library doesn't give is in `Host` (`Host.linux.raz`), five procedures, each a real | |
| 2246 | 2254 | difference between hosts. Nothing else in `src/` makes a system call, |
| 2247 | 2255 | reads `argv`, opens `/proc` or sets a file's mode; it reads, writes and |
| 2248 | 2256 | lists files, the environment and the working directory through the |
| @@ -2258,9 +2266,9 @@ | ||
| 2258 | 2266 | host can't make such a program, and `Raz` refuses one when it finds |
| 2259 | 2267 | the program uses C. Linux's is glibc, or musl where only its loader |
| 2260 | 2268 | is there. |
| 2261 | -- `writeExecutable`: the bytes `Elf` made, written as an executable. | |
| 2269 | +- `writeExecutable`: the bytes `Exe` made, written as an executable. | |
| 2262 | 2270 | Linux's writes the file and sets mode 755. It returns false on any |
| 2263 | - failure, which `Elf` reports. | |
| 2271 | + failure, which `Exe` reports. | |
| 2264 | 2272 | - `prepareRun`: the path `raz run` writes to, or an error, which is the |
| 2265 | 2273 | refusal of a host that can't run a program. Linux's is a memfd, |
| 2266 | 2274 | `/proc/self/fd/N`, kept open until `execute`. |
| @@ -2268,11 +2276,18 @@ | ||
| 2268 | 2276 | arguments are the compiler's from number `first` on (so its argument 0 |
| 2269 | 2277 | is `Main.raz`), and the environment. It returns only on failure. |
| 2270 | 2278 | |
| 2271 | -**Building for another host.** Stage the sources: copy `src/` to a | |
| 2272 | -directory, put that host's `Host.raz` there in place of raz's, and | |
| 2273 | -build `Raz.raz` from the copy with that host's library. This is the one | |
| 2274 | -way; raz knows no host but Linux. A module in the main file's | |
| 2275 | -directory is found first, so `-I` alone can't replace `Host.raz`, which | |
| 2279 | +**Building for another host, or another target.** Stage the sources: | |
| 2280 | +copy `src/` to a directory with only plain names, a target's file under | |
| 2281 | +the name of its unit (`Gen.x86_64.raz` as `Gen.raz`, `Exe.linux.raz` as | |
| 2282 | +`Exe.raz`) and the host's `Host.linux.raz`, or another host's, as | |
| 2283 | +`Host.raz`, and build `Raz.raz` from the copy with that host's library. | |
| 2284 | +`src/cross.sh TARGET` does it for a target (`darwin-arm64`) and the | |
| 2285 | +host's Host: the compiler it makes runs here and compiles for TARGET, | |
| 2286 | +and it names the files that are missing for one that has none. With | |
| 2287 | +`TARGET` the one raz is built for, its compiler is `./raz`'s but for | |
| 2288 | +the paths it holds. This is the one way; raz knows no target or host | |
| 2289 | +but the files it was built from. A module in the main file's | |
| 2290 | +directory is found first, so `-I` alone can't replace `Host`, which | |
| 2276 | 2291 | is right: a module is never replaced silently. The new file keeps every |
| 2277 | 2292 | export, `CLib` too, and opens with what it is, as raz's does. |
| 2278 | 2293 | `test/host/Host.raz` is such a file (its library from the environment, |
| @@ -2518,9 +2533,9 @@ | ||
| 2518 | 2533 | above `rsp`. **The code never writes below `rsp`**, no red zone: |
| 2519 | 2534 | where it looked, `rsp`-relative operands are in only two places, |
| 2520 | 2535 | `[rsp]` and `[rsp + 8]` read by the entry point and by the fail |
| 2521 | - stubs (`Gen.entryPoint`, all `X.mem(RSP, ...)` in `Gen.raz`), and | |
| 2536 | + stubs (`Gen.entryPoint`, all `X.mem(RSP, ...)` in `Gen.x86_64.raz`), and | |
| 2522 | 2537 | `X64.raz` and the assembler address only through the operands |
| 2523 | - `Gen.raz` gives them. Every other use of the stack is a `push` or | |
| 2538 | + `Gen.x86_64.raz` gives them. Every other use of the stack is a `push` or | |
| 2524 | 2539 | a `sub rsp`, which move `rsp` first, and a procedure with no calls |
| 2525 | 2540 | still makes its frame. An interrupt, then, needs no stack of its own |
| 2526 | 2541 | (IST) for that reason; it still needs room above the frame for its |
| @@ -2724,7 +2739,7 @@ | ||
| 2724 | 2739 | | `Sys.stdin` | `Scan`: `raz -q`'s source, read from standard input | |
| 2725 | 2740 | | `Sys.readLink` | `Host.libraryDir`: `/proc/self/exe`, to find the library beside the compiler | |
| 2726 | 2741 | | `Sys.stderr`, `Sys.stdout`, `Sys.write` | `Diag`: its messages, or `raz -q check`'s rows on standard output | |
| 2727 | -| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Elf`: the compilation directory of the line table | | |
| 2742 | +| `Sys.workDir` | `Diag`: shows a path relative to the working directory; `Exe`: the compilation directory of the line table | | |
| 2728 | 2743 | |
| 2729 | 2744 | `Rt`, the run time, besides `fail`, `exit` and the places (above): |
| 2730 | 2745 | |
| @@ -2790,7 +2805,7 @@ | ||
| 2790 | 2805 | | | report's least | this compiler | |
| 2791 | 2806 | |---|---|---| |
| 2792 | 2807 | | statements, expressions and types nested | 63 levels of statements, 63 of parentheses and calls | 256 together (`MaxNest`) | |
| 2793 | - | values waiting in one expression | none given | 1,024 (`MaxStack`, `Gen.raz`) | | |
| 2808 | + | values waiting in one expression | none given | 1,024 (`MaxStack`, `Gen.x86_64.raz`) | | |
| 2794 | 2809 | | chain of imports | 15 modules | 63 (`MaxPending`) | |
| 2795 | 2810 | | names declared together | 31 | 64 (`MaxNames`) | |
| 2796 | 2811 | | variables assigned together | 15 | 16 | |
| @@ -2900,17 +2915,18 @@ | ||
| 2900 | 2915 | ## Where to start changing it |
| 2901 | 2916 | |
| 2902 | 2917 | - **A built-in procedure:** an entry in `Sym.raz`'s universe (`enter`), |
| 2903 | - a case in `builtin` in `Parse.raz`, and the code, in `Gen.raz` if it is an | |
| 2918 | + a case in `builtin` in `Parse.raz`, and the code, in `Gen.x86_64.raz` if it is an | |
| 2904 | 2919 | instruction, or of calls to gen right there. |
| 2905 | 2920 | - **An instruction:** a routine in `X64.raz` built on `opM` or `opR`, |
| 2906 | 2921 | its encoding checked against `objdump -d` once, and a program in |
| 2907 | 2922 | `conform/` or `test/` that uses it. |
| 2908 | -- **A machine:** a new `Gen.raz`, answering the same calls, and an | |
| 2909 | - encoder and assembler for it in place of `X64.raz`. | |
| 2923 | +- **A machine:** a new `Gen.ARCH.raz`, answering the same calls, and an | |
| 2924 | + encoder and assembler for it in place of `X64.raz`; and `Exe.OS.raz` if | |
| 2925 | + the system's executable differs. | |
| 2910 | 2926 | - **A statement:** a reserved word in `Scan.raz` (in `Sym` and `words`, |
| 2911 | 2927 | in the same order), a case in `oneStatement`, and a routine beside the |
| 2912 | 2928 | others. |
| 2913 | -- **A check that fails:** `check` in `Gen.raz`, and the message in | |
| 2929 | +- **A check that fails:** `check` in `Gen.x86_64.raz`, and the message in | |
| 2914 | 2930 | `lib/Rt.raz`'s `fail`; a reason a signal gives, `onSignal` there. |
| 2915 | 2931 | |
| 2916 | 2932 | The compiler is in raz. `make raz` builds it with the release `BOOT` |
| @@ -1,450 +0,0 @@ | ||
| 1 | -(* Elf: the executable file. | |
| 2 | - | |
| 3 | - Writes an ELF64 executable for x86-64 Linux: the ELF header, the | |
| 4 | - program headers, the linked text and rodata, and section headers | |
| 5 | - with a symbol table, so that objdump and gdb can name each procedure | |
| 6 | - and variable. For gdb's line numbers there is DWARF 4: a line table | |
| 7 | - (.debug_line), and the one compile unit that points to it | |
| 8 | - (.debug_info, .debug_abbrev). There are no types or variables. | |
| 9 | - | |
| 10 | - A program that uses C is linked dynamically, in the least way the | |
| 11 | - loader accepts: it names its loader and libraries, and each C symbol | |
| 12 | - it uses is a quadword the loader fills in with the symbol's address | |
| 13 | - (a GLOB_DAT relocation), which the code calls or reads through. | |
| 14 | - There is no PLT and no lazy binding, and the program exports nothing. | |
| 15 | - | |
| 16 | - Depends on: Link for the images and addresses, Chunk for the chunks, | |
| 17 | - Scan for names and memory, Diag for errors, Sys for the working | |
| 18 | - directory, and Host to write the file. | |
| 19 | - Leaves out: position independent executables, symbol versions, the | |
| 20 | - rest of DWARF. | |
| 21 | - | |
| 22 | - The C library is glibc's or musl's, as setMusl says: the loader the | |
| 23 | - program names, and the library it needs first. | |
| 24 | - | |
| 25 | - Exported variables can't be set from outside, so needLib, setMusl, | |
| 26 | - setMainPath and setStripped do it. *) | |
| 27 | -module Elf; | |
| 28 | -import Sys, Host, Fmt, Diag, Scan, Chunk, Link; | |
| 29 | -import SYSTEM; | |
| 30 | - | |
| 31 | -var | |
| 32 | - needed: [32]INT; (* the libraries to load, names interned *) | |
| 33 | - numNeeded*: INT; (* none: a static program *) | |
| 34 | - mainPath: []const U8; (* the main module's file, for gdb *) | |
| 35 | - stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *) | |
| 36 | - interp: []const U8; (* the loader a program that uses C names *) | |
| 37 | - libc*: []const U8; (* and the C library it needs *) | |
| 38 | - | |
| 39 | - buf: ^U8; | |
| 40 | - len, cap: INT; | |
| 41 | - | |
| 42 | -(* The library name, to be loaded, unless it is already: false if there | |
| 43 | - are too many *) | |
| 44 | -proc needLib*(name: INT): BOOL; | |
| 45 | -var i: INT; | |
| 46 | -begin | |
| 47 | - for i := 0 to numNeeded - 1 do | |
| 48 | - if needed[i] = name then return true; end; | |
| 49 | - end; | |
| 50 | - if numNeeded = 32 then return false; end; | |
| 51 | - needed[numNeeded] := name; | |
| 52 | - INC(numNeeded); | |
| 53 | - return true; | |
| 54 | -end; | |
| 55 | - | |
| 56 | -(* A static program again *) | |
| 57 | -proc noLibs*; | |
| 58 | -begin | |
| 59 | - numNeeded := 0; | |
| 60 | -end; | |
| 61 | - | |
| 62 | -(* A program that uses C is linked against musl, if yes, or glibc. | |
| 63 | - musl's library is its own loader, which takes libc.so to mean | |
| 64 | - itself, whatever file the system keeps it in. *) | |
| 65 | -proc setMusl*(yes: BOOL); | |
| 66 | -begin | |
| 67 | - if yes then | |
| 68 | - interp := "/lib/ld-musl-x86_64.so.1"; | |
| 69 | - libc := "libc.so"; | |
| 70 | - else | |
| 71 | - interp := "/lib64/ld-linux-x86-64.so.2"; | |
| 72 | - libc := "libc.so.6"; | |
| 73 | - end; | |
| 74 | -end; | |
| 75 | - | |
| 76 | -proc setMainPath*(path: []const U8); | |
| 77 | -begin | |
| 78 | - mainPath := path; | |
| 79 | -end; | |
| 80 | - | |
| 81 | -proc setStripped*(yes: BOOL); | |
| 82 | -begin | |
| 83 | - stripped := yes; | |
| 84 | -end; | |
| 85 | - | |
| 86 | -proc room(n: INT); | |
| 87 | -begin | |
| 88 | - if len + n > cap then | |
| 89 | - buf := Scan.regrab(buf, len, cap * 2 + n); | |
| 90 | - cap := cap * 2 + n; | |
| 91 | - end; | |
| 92 | -end; | |
| 93 | - | |
| 94 | -(* The n low bytes of v, least significant first *) | |
| 95 | -proc putN(v: INT; n: INT); | |
| 96 | -var i: INT; | |
| 97 | -begin | |
| 98 | - room(n); | |
| 99 | - for i := 0 to n - 1 do | |
| 100 | - buf[len] := U8(v & 0FFH); | |
| 101 | - v := v shr 8; | |
| 102 | - INC(len); | |
| 103 | - end; | |
| 104 | -end; | |
| 105 | - | |
| 106 | -proc putBytes(p: ^U8; n: INT); | |
| 107 | -begin | |
| 108 | - room(n); | |
| 109 | - if n > 0 then COPY(buf[len..len + n], p[0..n]); end; | |
| 110 | - len := len + n; | |
| 111 | -end; | |
| 112 | - | |
| 113 | -proc padTo(off: INT); | |
| 114 | -begin | |
| 115 | - while len < off do putN(0, 1); end; | |
| 116 | -end; | |
| 117 | - | |
| 118 | -proc patch(at: INT; v: INT; n: INT); | |
| 119 | -var i: INT; | |
| 120 | -begin | |
| 121 | - for i := 0 to n - 1 do | |
| 122 | - buf[at + i] := U8(v & 0FFH); | |
| 123 | - v := v shr 8; | |
| 124 | - end; | |
| 125 | -end; | |
| 126 | - | |
| 127 | -proc phdr(typ, flags: INT; off, adr, filesz, memsz, align: INT); | |
| 128 | -begin | |
| 129 | - putN(typ, 4); | |
| 130 | - putN(flags, 4); | |
| 131 | - putN(off, 8); | |
| 132 | - putN(adr, 8); | |
| 133 | - putN(adr, 8); | |
| 134 | - putN(filesz, 8); | |
| 135 | - putN(memsz, 8); | |
| 136 | - putN(align, 8); | |
| 137 | -end; | |
| 138 | - | |
| 139 | -const | |
| 140 | - (* the section names, in .shstrtab; a section header names its own *) | |
| 141 | - ShNames = "\0.text\0.rodata\0.bss\0.symtab\0.strtab\0.shstrtab\0.dynamic\0.dynstr\0.dynsym\0.debug_line\0.debug_info\0.debug_abbrev\0"; | |
| 142 | - | |
| 143 | -(* Where in ShNames the name is: the byte after a zero that starts it and | |
| 144 | - is followed by one; the empty name, found nowhere, is 0 *) | |
| 145 | -proc nameAt(name: []const U8): INT; | |
| 146 | -var i, k: INT; same: BOOL; | |
| 147 | -begin | |
| 148 | - for i := 1 to LEN(ShNames) - LEN(name) - 1 do | |
| 149 | - if ShNames[i - 1] = 0 then | |
| 150 | - same := ShNames[i + LEN(name)] = 0; | |
| 151 | - for k := 0 to LEN(name) - 1 do | |
| 152 | - if ShNames[i + k] # name[k] then same := false; end; | |
| 153 | - end; | |
| 154 | - if same and (LEN(name) > 0) then return i; end; | |
| 155 | - end; | |
| 156 | - end; | |
| 157 | - return 0; | |
| 158 | -end; | |
| 159 | - | |
| 160 | -(* A section header. Its name is stored as its offset in ShNames. *) | |
| 161 | -proc shdr(name: []const U8; typ: INT; flags, adr, off, size: INT; lnk, info: INT; align, entsize: INT); | |
| 162 | -begin | |
| 163 | - putN(nameAt(name), 4); putN(typ, 4); putN(flags, 8); putN(adr, 8); | |
| 164 | - putN(off, 8); putN(size, 8); putN(lnk, 4); putN(info, 4); | |
| 165 | - putN(align, 8); putN(entsize, 8); | |
| 166 | -end; | |
| 167 | - | |
| 168 | -proc putStr(s: []const U8); | |
| 169 | -begin | |
| 170 | - putBytes(SYSTEM.PTR(s), LEN(s)); | |
| 171 | - putN(0, 1); | |
| 172 | -end; | |
| 173 | - | |
| 174 | -(* LEB128, unsigned *) | |
| 175 | -proc putU(v: INT); | |
| 176 | -begin | |
| 177 | - while v >= 80H do | |
| 178 | - putN(v & 7FH | 80H, 1); | |
| 179 | - v := v shr 7; | |
| 180 | - end; | |
| 181 | - putN(v, 1); | |
| 182 | -end; | |
| 183 | - | |
| 184 | -(* The compile unit, which names the line table, and its abbreviation; | |
| 185 | - the table itself is in the places, in rodata (Link). Section | |
| 186 | - offsets go in off, sizes in size: line, info, abbrev. *) | |
| 187 | -proc putDebug(off, size: []INT); | |
| 188 | -var cwd: []const U8; err: INT; | |
| 189 | -begin | |
| 190 | - off[0] := Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.roOff + Link.linesOff; | |
| 191 | - size[0] := Link.linesSize; | |
| 192 | - off[1] := len; | |
| 193 | - putN(0, 4); putN(4, 2); putN(0, 4); putN(8, 1); (* length, version, abbrev, address size *) | |
| 194 | - putU(1); | |
| 195 | - putStr(mainPath); | |
| 196 | - cwd, err := Sys.workDir(); | |
| 197 | - if err # 0 then cwd := ""; end; | |
| 198 | - putStr(cwd); | |
| 199 | - putStr("raz"); | |
| 200 | - putN(0, 4); (* the line table, at 0 *) | |
| 201 | - putN(Link.base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *) | |
| 202 | - patch(off[1], len - off[1] - 4, 4); | |
| 203 | - size[1] := len - off[1]; | |
| 204 | - | |
| 205 | - off[2] := len; | |
| 206 | - putU(1); putU(11H); putN(0, 1); (* 1: a compile unit, no children *) | |
| 207 | - putU(03H); putU(08H); (* name, a string *) | |
| 208 | - putU(1BH); putU(08H); (* comp_dir, a string *) | |
| 209 | - putU(25H); putU(08H); (* producer, a string *) | |
| 210 | - putU(10H); putU(17H); (* stmt_list, a section offset *) | |
| 211 | - putU(11H); putU(01H); (* low_pc, an address *) | |
| 212 | - putU(12H); putU(07H); (* high_pc, a length in 8 bytes *) | |
| 213 | - putN(0, 2); putN(0, 1); | |
| 214 | - size[2] := len - off[2]; | |
| 215 | -end; | |
| 216 | - | |
| 217 | -const | |
| 218 | - (* program header types *) | |
| 219 | - PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H; | |
| 220 | - | |
| 221 | -var | |
| 222 | - (* in the room at the end of rodata: where each of the loader's | |
| 223 | - tables starts, from extraAdr *) | |
| 224 | - symAt, relaAt, hashAt, strAt: INT; | |
| 225 | - nsym, strLen: INT; | |
| 226 | - | |
| 227 | -(* Whether chunk c is a C symbol the program uses *) | |
| 228 | -proc isExt(c: INT): BOOL; | |
| 229 | -begin | |
| 230 | - return Chunk.chunks[c].live and Chunk.chunks[c].ext; | |
| 231 | -end; | |
| 232 | - | |
| 233 | -(* The sizes of the loader's tables: what linkAll must leave room for. *) | |
| 234 | -proc sizeTables(var roExtra, dynSize: INT); | |
| 235 | -var c, i: INT; | |
| 236 | -begin | |
| 237 | - nsym := 1; | |
| 238 | - strLen := 1; | |
| 239 | - for i := 0 to numNeeded - 1 do strLen := strLen + LEN(Scan.nameOf(needed[i])) + 1; end; | |
| 240 | - for c := 0 to Chunk.numChunks - 1 do | |
| 241 | - if isExt(c) then | |
| 242 | - INC(nsym); | |
| 243 | - strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 244 | - end; | |
| 245 | - end; | |
| 246 | - symAt := (LEN(interp) + 1 + 7) div 8 * 8; | |
| 247 | - relaAt := symAt + nsym * 24; | |
| 248 | - hashAt := relaAt + (nsym - 1) * 24; | |
| 249 | - strAt := hashAt + 4 * (3 + nsym); | |
| 250 | - roExtra := strAt + strLen; | |
| 251 | - dynSize := (numNeeded + 10) * 16; | |
| 252 | -end; | |
| 253 | - | |
| 254 | -(* The loader's tables in the room at the end of rodata, which the | |
| 255 | - buffer already holds as zeros: the loader's name; the symbols, one | |
| 256 | - for each C symbol used; a GLOB_DAT relocation for each, at its | |
| 257 | - quadword; a hash table of one bucket; and their names. *) | |
| 258 | -proc putTables; | |
| 259 | -var c, k, i, at, name: INT; | |
| 260 | -begin | |
| 261 | - len := Link.extraAdr - Link.roAdr + Link.roOff; | |
| 262 | - putStr(interp); | |
| 263 | - padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt); | |
| 264 | - putN(0, 24); | |
| 265 | - name := strLen; | |
| 266 | - for c := Chunk.numChunks - 1 to 0 by -1 do (* names go last in the string table *) | |
| 267 | - if isExt(c) then name := name - LEN(Scan.nameOf(Chunk.chunks[c].name)) - 1; end; | |
| 268 | - end; | |
| 269 | - for c := 0 to Chunk.numChunks - 1 do | |
| 270 | - if isExt(c) then | |
| 271 | - putN(name, 4); putN(10H, 1); putN(0, 1); putN(0, 2); (* global, undefined *) | |
| 272 | - putN(0, 8); putN(0, 8); | |
| 273 | - name := name + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 274 | - end; | |
| 275 | - end; | |
| 276 | - k := 1; | |
| 277 | - for c := 0 to Chunk.numChunks - 1 do | |
| 278 | - if isExt(c) then | |
| 279 | - putN(Chunk.chunks[c].adr, 8); | |
| 280 | - putN((k shl 32) | 6, 8); (* R_X86_64_GLOB_DAT *) | |
| 281 | - putN(0, 8); | |
| 282 | - INC(k); | |
| 283 | - end; | |
| 284 | - end; | |
| 285 | - putN(1, 4); putN(nsym, 4); putN(nsym - 1, 4); (* the bucket chains every symbol *) | |
| 286 | - putN(0, 4); | |
| 287 | - for i := 1 to nsym - 1 do putN(i - 1, 4); end; | |
| 288 | - putN(0, 1); | |
| 289 | - for i := 0 to numNeeded - 1 do putStr(Scan.nameOf(needed[i])); end; | |
| 290 | - for c := 0 to Chunk.numChunks - 1 do | |
| 291 | - if isExt(c) then putStr(Scan.nameOf(Chunk.chunks[c].name)); end; | |
| 292 | - end; | |
| 293 | - len := Link.roOff + Link.roSize; | |
| 294 | - | |
| 295 | - (* the dynamic table, at the start of the writable segment *) | |
| 296 | - padTo(Link.bssOff); | |
| 297 | - at := 1; | |
| 298 | - for i := 0 to numNeeded - 1 do | |
| 299 | - putN(1, 8); putN(at, 8); (* DT_NEEDED *) | |
| 300 | - at := at + LEN(Scan.nameOf(needed[i])) + 1; | |
| 301 | - end; | |
| 302 | - putN(4, 8); putN(Link.extraAdr + hashAt, 8); (* DT_HASH *) | |
| 303 | - putN(5, 8); putN(Link.extraAdr + strAt, 8); (* DT_STRTAB *) | |
| 304 | - putN(6, 8); putN(Link.extraAdr + symAt, 8); (* DT_SYMTAB *) | |
| 305 | - putN(10, 8); putN(strLen, 8); (* DT_STRSZ *) | |
| 306 | - putN(11, 8); putN(24, 8); (* DT_SYMENT *) | |
| 307 | - putN(7, 8); putN(Link.extraAdr + relaAt, 8); (* DT_RELA *) | |
| 308 | - putN(8, 8); putN((nsym - 1) * 24, 8); (* DT_RELASZ *) | |
| 309 | - putN(9, 8); putN(24, 8); (* DT_RELAENT *) | |
| 310 | - putN(21, 8); putN(0, 8); (* DT_DEBUG, for gdb *) | |
| 311 | - putN(0, 16); (* DT_NULL *) | |
| 312 | -end; | |
| 313 | - | |
| 314 | -proc writeElf*(path: []const U8; entry: INT); | |
| 315 | -var | |
| 316 | - nph, nsh, c, nsyms, sec, strsCap: INT; | |
| 317 | - roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: INT; | |
| 318 | - dbgOff, dbgSize: [3]INT; | |
| 319 | - strs: ^U8; | |
| 320 | - s: []const U8; | |
| 321 | - dyn: BOOL; | |
| 322 | -begin | |
| 323 | - cap := 1 shl 16; | |
| 324 | - buf := Scan.grab(cap); | |
| 325 | - len := 0; | |
| 326 | - dyn := numNeeded > 0; | |
| 327 | - roExtra := 0; dynSize := 0; | |
| 328 | - if dyn then sizeTables(roExtra, dynSize); end; | |
| 329 | - (* room for the most program headers either kind of program has *) | |
| 330 | - Link.linkAll(64 + 6 * 56, roExtra, dynSize); | |
| 331 | - if dyn then | |
| 332 | - nph := 6; | |
| 333 | - else | |
| 334 | - nph := 2; | |
| 335 | - if Link.roSize > 0 then INC(nph); end; | |
| 336 | - if Link.bssSize > 0 then INC(nph); end; | |
| 337 | - end; | |
| 338 | - | |
| 339 | - (* ELF header *) | |
| 340 | - putN(464C457FH, 4); (* #127 'ELF' *) | |
| 341 | - putN(2, 1); putN(1, 1); putN(1, 1); putN(0, 9); (* 64-bit, little-endian, version 1 *) | |
| 342 | - putN(2, 2); (* ET_EXEC *) | |
| 343 | - putN(62, 2); (* EM_X86_64 *) | |
| 344 | - putN(1, 4); | |
| 345 | - putN(Chunk.chunks[entry].adr, 8); | |
| 346 | - putN(64, 8); (* program headers follow *) | |
| 347 | - putN(0, 8); (* section headers: patched below *) | |
| 348 | - putN(0, 4); | |
| 349 | - nsh := 10; | |
| 350 | - if dyn then nsh := 13; end; | |
| 351 | - putN(64, 2); putN(56, 2); putN(nph, 2); | |
| 352 | - putN(64, 2); putN(nsh, 2); putN(6, 2); (* .shstrtab is section 6 *) | |
| 353 | - | |
| 354 | - (* flags: 4 read, 5 read and run, 6 read and write *) | |
| 355 | - if dyn then | |
| 356 | - phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1); | |
| 357 | - end; | |
| 358 | - (* the first segment maps the headers too, so text can follow them *) | |
| 359 | - phdr(PtLoad, 5, 0, Link.base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); | |
| 360 | - if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end; | |
| 361 | - if dyn then | |
| 362 | - phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page); | |
| 363 | - phdr(PtDynamic, 6, Link.bssOff, Link.bssAdr, dynSize, dynSize, 8); | |
| 364 | - elsif Link.bssSize > 0 then | |
| 365 | - phdr(PtLoad, 6, 0, Link.bssAdr, 0, Link.bssSize, Link.Page); | |
| 366 | - end; | |
| 367 | - phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); (* a stack that can't run code *) | |
| 368 | - | |
| 369 | - padTo(Link.textOff); | |
| 370 | - putBytes(Link.textImg, Link.textSize); | |
| 371 | - padTo(Link.roOff); (* even when empty, so that every section lies in the file *) | |
| 372 | - putBytes(Link.roImg, Link.roSize); | |
| 373 | - if dyn then putTables; end; | |
| 374 | - | |
| 375 | - (* the symbol table: one local symbol for each named live chunk *) | |
| 376 | - padTo((len + 7) div 8 * 8); | |
| 377 | - symOff := len; | |
| 378 | - strsCap := 1 shl 16; | |
| 379 | - strs := Scan.grab(strsCap); | |
| 380 | - strsLen := 1; | |
| 381 | - strs[0] := 0; | |
| 382 | - putN(0, 24); | |
| 383 | - nsyms := 1; | |
| 384 | - for c := 0 to Chunk.numChunks - 1 do | |
| 385 | - if not stripped and Chunk.chunks[c].live and (Chunk.chunks[c].name >= 0) and not Chunk.chunks[c].ext then | |
| 386 | - s := Chunk.chunkName(c); | |
| 387 | - if strsLen + LEN(s) + 1 > strsCap then | |
| 388 | - strs := Scan.regrab(strs, strsLen, strsLen * 2 + 256); | |
| 389 | - strsCap := strsLen * 2 + 256; | |
| 390 | - end; | |
| 391 | - COPY(strs[strsLen..strsLen + LEN(s)], s); | |
| 392 | - strs[strsLen + LEN(s)] := 0; | |
| 393 | - if Chunk.chunks[c].sec = Chunk.secText then (* the section's index in the headers below *) | |
| 394 | - sec := 1; | |
| 395 | - elsif Chunk.chunks[c].sec = Chunk.secRodata then | |
| 396 | - sec := 2; | |
| 397 | - else | |
| 398 | - sec := 3; | |
| 399 | - end; | |
| 400 | - putN(strsLen, 4); | |
| 401 | - if sec = 1 then putN(2, 1); else putN(1, 1); end; (* STT_FUNC, STT_OBJECT *) | |
| 402 | - putN(0, 1); | |
| 403 | - putN(sec, 2); | |
| 404 | - putN(Chunk.chunks[c].adr, 8); | |
| 405 | - putN(Chunk.chunks[c].size, 8); | |
| 406 | - strsLen := strsLen + LEN(s) + 1; | |
| 407 | - INC(nsyms); | |
| 408 | - end; | |
| 409 | - end; | |
| 410 | - strOff := len; | |
| 411 | - putBytes(strs, strsLen); | |
| 412 | - shstrOff := len; | |
| 413 | - putBytes(SYSTEM.PTR(ShNames), LEN(ShNames)); | |
| 414 | - if stripped or (Link.placesChunk < 0) then | |
| 415 | - for c := 0 to 2 do dbgOff[c] := len; dbgSize[c] := 0; end; | |
| 416 | - else | |
| 417 | - putDebug(dbgOff, dbgSize); | |
| 418 | - end; | |
| 419 | - | |
| 420 | - padTo((len + 7) div 8 * 8); | |
| 421 | - shOff := len; | |
| 422 | - patch(40, shOff, 8); | |
| 423 | - (* types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM; | |
| 424 | - flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's | |
| 425 | - link is its string table's index: 5 .strtab, 8 .dynstr. The order | |
| 426 | - here is the index the code above relies on. *) | |
| 427 | - shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
| 428 | - shdr(".text", 1, 6, Link.base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); | |
| 429 | - shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0); | |
| 430 | - shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0); | |
| 431 | - shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); | |
| 432 | - shdr(".strtab", 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); | |
| 433 | - shdr(".shstrtab", 3, 0, 0, shstrOff, LEN(ShNames), 0, 0, 1, 0); | |
| 434 | - if dyn then (* the loader's tables, for readelf and objdump *) | |
| 435 | - shdr(".dynamic", 6, 3, Link.bssAdr, Link.bssOff, dynSize, 8, 0, 8, 16); | |
| 436 | - shdr(".dynstr", 3, 2, Link.extraAdr + strAt, Link.extraAdr + strAt - Link.roAdr + Link.roOff, strLen, 0, 0, 1, 0); | |
| 437 | - shdr(".dynsym", 11, 2, Link.extraAdr + symAt, Link.extraAdr + symAt - Link.roAdr + Link.roOff, nsym * 24, 8, 1, 8, 24); | |
| 438 | - end; | |
| 439 | - shdr(".debug_line", 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); | |
| 440 | - shdr(".debug_info", 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); | |
| 441 | - shdr(".debug_abbrev", 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0); | |
| 442 | - | |
| 443 | - if not Host.writeExecutable(path, buf[0..len]) then | |
| 444 | - Diag.fatal(Diag.say("can't write {}", [Fmt.str(path)])); | |
| 445 | - end; | |
| 446 | -end; | |
| 447 | - | |
| 448 | -begin | |
| 449 | - mainPath := ""; | |
| 450 | -end Elf. |
| @@ -0,0 +1,455 @@ | ||
| 1 | +(* Exe: the executable file, for Linux: ELF. | |
| 2 | + | |
| 3 | + Exe.darwin.raz, for macOS, will be Mach-O; a target has one Exe, chosen | |
| 4 | + by the suffix of its file (doc/raz.md, 3), and no other unit knows the | |
| 5 | + format. This one writes an ELF64 executable: the ELF header, the | |
| 6 | + program headers, the linked text and rodata, and section headers | |
| 7 | + with a symbol table, so that objdump and gdb can name each procedure | |
| 8 | + and variable. For gdb's line numbers there is DWARF 4: a line table | |
| 9 | + (.debug_line), and the one compile unit that points to it | |
| 10 | + (.debug_info, .debug_abbrev). There are no types or variables. | |
| 11 | + | |
| 12 | + A program that uses C is linked dynamically, in the least way the | |
| 13 | + loader accepts: it names its loader and libraries, and each C symbol | |
| 14 | + it uses is a quadword the loader fills in with the symbol's address | |
| 15 | + (a GLOB_DAT relocation), which the code calls or reads through. | |
| 16 | + There is no PLT and no lazy binding, and the program exports nothing. | |
| 17 | + | |
| 18 | + Depends on: Link for the images and addresses, Chunk for the chunks, | |
| 19 | + Scan for names and memory, Diag for errors, Sys for the working | |
| 20 | + directory, and Host to write the file. | |
| 21 | + Leaves out: position independent executables, symbol versions, the | |
| 22 | + rest of DWARF. | |
| 23 | + | |
| 24 | + The C library is glibc's or musl's, as setMusl says: the loader the | |
| 25 | + program names, and the library it needs first. | |
| 26 | + | |
| 27 | + Exported variables can't be set from outside, so needLib, setMusl, | |
| 28 | + setMainPath and setStripped do it. *) | |
| 29 | +module Exe; | |
| 30 | +import Sys, Host, Fmt, Diag, Scan, Chunk, Link; | |
| 31 | +import SYSTEM; | |
| 32 | + | |
| 33 | +const | |
| 34 | + Os* = "linux"; (* the target's operating system, as a file's suffix says it *) | |
| 35 | + | |
| 36 | +var | |
| 37 | + needed: [32]INT; (* the libraries to load, names interned *) | |
| 38 | + numNeeded*: INT; (* none: a static program *) | |
| 39 | + mainPath: []const U8; (* the main module's file, for gdb *) | |
| 40 | + stripped*: BOOL; (* -s: no symbols and no line numbers; the sections are there, empty *) | |
| 41 | + interp: []const U8; (* the loader a program that uses C names *) | |
| 42 | + libc*: []const U8; (* and the C library it needs *) | |
| 43 | + | |
| 44 | + buf: ^U8; | |
| 45 | + len, cap: INT; | |
| 46 | + | |
| 47 | +(* The library name, to be loaded, unless it is already: false if there | |
| 48 | + are too many *) | |
| 49 | +proc needLib*(name: INT): BOOL; | |
| 50 | +var i: INT; | |
| 51 | +begin | |
| 52 | + for i := 0 to numNeeded - 1 do | |
| 53 | + if needed[i] = name then return true; end; | |
| 54 | + end; | |
| 55 | + if numNeeded = 32 then return false; end; | |
| 56 | + needed[numNeeded] := name; | |
| 57 | + INC(numNeeded); | |
| 58 | + return true; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +(* A static program again *) | |
| 62 | +proc noLibs*; | |
| 63 | +begin | |
| 64 | + numNeeded := 0; | |
| 65 | +end; | |
| 66 | + | |
| 67 | +(* A program that uses C is linked against musl, if yes, or glibc. | |
| 68 | + musl's library is its own loader, which takes libc.so to mean | |
| 69 | + itself, whatever file the system keeps it in. *) | |
| 70 | +proc setMusl*(yes: BOOL); | |
| 71 | +begin | |
| 72 | + if yes then | |
| 73 | + interp := "/lib/ld-musl-x86_64.so.1"; | |
| 74 | + libc := "libc.so"; | |
| 75 | + else | |
| 76 | + interp := "/lib64/ld-linux-x86-64.so.2"; | |
| 77 | + libc := "libc.so.6"; | |
| 78 | + end; | |
| 79 | +end; | |
| 80 | + | |
| 81 | +proc setMainPath*(path: []const U8); | |
| 82 | +begin | |
| 83 | + mainPath := path; | |
| 84 | +end; | |
| 85 | + | |
| 86 | +proc setStripped*(yes: BOOL); | |
| 87 | +begin | |
| 88 | + stripped := yes; | |
| 89 | +end; | |
| 90 | + | |
| 91 | +proc room(n: INT); | |
| 92 | +begin | |
| 93 | + if len + n > cap then | |
| 94 | + buf := Scan.regrab(buf, len, cap * 2 + n); | |
| 95 | + cap := cap * 2 + n; | |
| 96 | + end; | |
| 97 | +end; | |
| 98 | + | |
| 99 | +(* The n low bytes of v, least significant first *) | |
| 100 | +proc putN(v: INT; n: INT); | |
| 101 | +var i: INT; | |
| 102 | +begin | |
| 103 | + room(n); | |
| 104 | + for i := 0 to n - 1 do | |
| 105 | + buf[len] := U8(v & 0FFH); | |
| 106 | + v := v shr 8; | |
| 107 | + INC(len); | |
| 108 | + end; | |
| 109 | +end; | |
| 110 | + | |
| 111 | +proc putBytes(p: ^U8; n: INT); | |
| 112 | +begin | |
| 113 | + room(n); | |
| 114 | + if n > 0 then COPY(buf[len..len + n], p[0..n]); end; | |
| 115 | + len := len + n; | |
| 116 | +end; | |
| 117 | + | |
| 118 | +proc padTo(off: INT); | |
| 119 | +begin | |
| 120 | + while len < off do putN(0, 1); end; | |
| 121 | +end; | |
| 122 | + | |
| 123 | +proc patch(at: INT; v: INT; n: INT); | |
| 124 | +var i: INT; | |
| 125 | +begin | |
| 126 | + for i := 0 to n - 1 do | |
| 127 | + buf[at + i] := U8(v & 0FFH); | |
| 128 | + v := v shr 8; | |
| 129 | + end; | |
| 130 | +end; | |
| 131 | + | |
| 132 | +proc phdr(typ, flags: INT; off, adr, filesz, memsz, align: INT); | |
| 133 | +begin | |
| 134 | + putN(typ, 4); | |
| 135 | + putN(flags, 4); | |
| 136 | + putN(off, 8); | |
| 137 | + putN(adr, 8); | |
| 138 | + putN(adr, 8); | |
| 139 | + putN(filesz, 8); | |
| 140 | + putN(memsz, 8); | |
| 141 | + putN(align, 8); | |
| 142 | +end; | |
| 143 | + | |
| 144 | +const | |
| 145 | + (* the section names, in .shstrtab; a section header names its own *) | |
| 146 | + ShNames = "\0.text\0.rodata\0.bss\0.symtab\0.strtab\0.shstrtab\0.dynamic\0.dynstr\0.dynsym\0.debug_line\0.debug_info\0.debug_abbrev\0"; | |
| 147 | + | |
| 148 | +(* Where in ShNames the name is: the byte after a zero that starts it and | |
| 149 | + is followed by one; the empty name, found nowhere, is 0 *) | |
| 150 | +proc nameAt(name: []const U8): INT; | |
| 151 | +var i, k: INT; same: BOOL; | |
| 152 | +begin | |
| 153 | + for i := 1 to LEN(ShNames) - LEN(name) - 1 do | |
| 154 | + if ShNames[i - 1] = 0 then | |
| 155 | + same := ShNames[i + LEN(name)] = 0; | |
| 156 | + for k := 0 to LEN(name) - 1 do | |
| 157 | + if ShNames[i + k] # name[k] then same := false; end; | |
| 158 | + end; | |
| 159 | + if same and (LEN(name) > 0) then return i; end; | |
| 160 | + end; | |
| 161 | + end; | |
| 162 | + return 0; | |
| 163 | +end; | |
| 164 | + | |
| 165 | +(* A section header. Its name is stored as its offset in ShNames. *) | |
| 166 | +proc shdr(name: []const U8; typ: INT; flags, adr, off, size: INT; lnk, info: INT; align, entsize: INT); | |
| 167 | +begin | |
| 168 | + putN(nameAt(name), 4); putN(typ, 4); putN(flags, 8); putN(adr, 8); | |
| 169 | + putN(off, 8); putN(size, 8); putN(lnk, 4); putN(info, 4); | |
| 170 | + putN(align, 8); putN(entsize, 8); | |
| 171 | +end; | |
| 172 | + | |
| 173 | +proc putStr(s: []const U8); | |
| 174 | +begin | |
| 175 | + putBytes(SYSTEM.PTR(s), LEN(s)); | |
| 176 | + putN(0, 1); | |
| 177 | +end; | |
| 178 | + | |
| 179 | +(* LEB128, unsigned *) | |
| 180 | +proc putU(v: INT); | |
| 181 | +begin | |
| 182 | + while v >= 80H do | |
| 183 | + putN(v & 7FH | 80H, 1); | |
| 184 | + v := v shr 7; | |
| 185 | + end; | |
| 186 | + putN(v, 1); | |
| 187 | +end; | |
| 188 | + | |
| 189 | +(* The compile unit, which names the line table, and its abbreviation; | |
| 190 | + the table itself is in the places, in rodata (Link). Section | |
| 191 | + offsets go in off, sizes in size: line, info, abbrev. *) | |
| 192 | +proc putDebug(off, size: []INT); | |
| 193 | +var cwd: []const U8; err: INT; | |
| 194 | +begin | |
| 195 | + off[0] := Chunk.chunks[Link.placesChunk].adr - Link.roAdr + Link.roOff + Link.linesOff; | |
| 196 | + size[0] := Link.linesSize; | |
| 197 | + off[1] := len; | |
| 198 | + putN(0, 4); putN(4, 2); putN(0, 4); putN(8, 1); (* length, version, abbrev, address size *) | |
| 199 | + putU(1); | |
| 200 | + putStr(mainPath); | |
| 201 | + cwd, err := Sys.workDir(); | |
| 202 | + if err # 0 then cwd := ""; end; | |
| 203 | + putStr(cwd); | |
| 204 | + putStr("raz"); | |
| 205 | + putN(0, 4); (* the line table, at 0 *) | |
| 206 | + putN(Link.base + Link.textOff, 8); putN(Link.textSize, 8); (* the code, all of it *) | |
| 207 | + patch(off[1], len - off[1] - 4, 4); | |
| 208 | + size[1] := len - off[1]; | |
| 209 | + | |
| 210 | + off[2] := len; | |
| 211 | + putU(1); putU(11H); putN(0, 1); (* 1: a compile unit, no children *) | |
| 212 | + putU(03H); putU(08H); (* name, a string *) | |
| 213 | + putU(1BH); putU(08H); (* comp_dir, a string *) | |
| 214 | + putU(25H); putU(08H); (* producer, a string *) | |
| 215 | + putU(10H); putU(17H); (* stmt_list, a section offset *) | |
| 216 | + putU(11H); putU(01H); (* low_pc, an address *) | |
| 217 | + putU(12H); putU(07H); (* high_pc, a length in 8 bytes *) | |
| 218 | + putN(0, 2); putN(0, 1); | |
| 219 | + size[2] := len - off[2]; | |
| 220 | +end; | |
| 221 | + | |
| 222 | +const | |
| 223 | + (* program header types *) | |
| 224 | + PtLoad = 1; PtDynamic = 2; PtInterp = 3; PtGnuStack = 6474E551H; | |
| 225 | + | |
| 226 | +var | |
| 227 | + (* in the room at the end of rodata: where each of the loader's | |
| 228 | + tables starts, from extraAdr *) | |
| 229 | + symAt, relaAt, hashAt, strAt: INT; | |
| 230 | + nsym, strLen: INT; | |
| 231 | + | |
| 232 | +(* Whether chunk c is a C symbol the program uses *) | |
| 233 | +proc isExt(c: INT): BOOL; | |
| 234 | +begin | |
| 235 | + return Chunk.chunks[c].live and Chunk.chunks[c].ext; | |
| 236 | +end; | |
| 237 | + | |
| 238 | +(* The sizes of the loader's tables: what linkAll must leave room for. *) | |
| 239 | +proc sizeTables(var roExtra, dynSize: INT); | |
| 240 | +var c, i: INT; | |
| 241 | +begin | |
| 242 | + nsym := 1; | |
| 243 | + strLen := 1; | |
| 244 | + for i := 0 to numNeeded - 1 do strLen := strLen + LEN(Scan.nameOf(needed[i])) + 1; end; | |
| 245 | + for c := 0 to Chunk.numChunks - 1 do | |
| 246 | + if isExt(c) then | |
| 247 | + INC(nsym); | |
| 248 | + strLen := strLen + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 249 | + end; | |
| 250 | + end; | |
| 251 | + symAt := (LEN(interp) + 1 + 7) div 8 * 8; | |
| 252 | + relaAt := symAt + nsym * 24; | |
| 253 | + hashAt := relaAt + (nsym - 1) * 24; | |
| 254 | + strAt := hashAt + 4 * (3 + nsym); | |
| 255 | + roExtra := strAt + strLen; | |
| 256 | + dynSize := (numNeeded + 10) * 16; | |
| 257 | +end; | |
| 258 | + | |
| 259 | +(* The loader's tables in the room at the end of rodata, which the | |
| 260 | + buffer already holds as zeros: the loader's name; the symbols, one | |
| 261 | + for each C symbol used; a GLOB_DAT relocation for each, at its | |
| 262 | + quadword; a hash table of one bucket; and their names. *) | |
| 263 | +proc putTables; | |
| 264 | +var c, k, i, at, name: INT; | |
| 265 | +begin | |
| 266 | + len := Link.extraAdr - Link.roAdr + Link.roOff; | |
| 267 | + putStr(interp); | |
| 268 | + padTo(Link.extraAdr - Link.roAdr + Link.roOff + symAt); | |
| 269 | + putN(0, 24); | |
| 270 | + name := strLen; | |
| 271 | + for c := Chunk.numChunks - 1 to 0 by -1 do (* names go last in the string table *) | |
| 272 | + if isExt(c) then name := name - LEN(Scan.nameOf(Chunk.chunks[c].name)) - 1; end; | |
| 273 | + end; | |
| 274 | + for c := 0 to Chunk.numChunks - 1 do | |
| 275 | + if isExt(c) then | |
| 276 | + putN(name, 4); putN(10H, 1); putN(0, 1); putN(0, 2); (* global, undefined *) | |
| 277 | + putN(0, 8); putN(0, 8); | |
| 278 | + name := name + LEN(Scan.nameOf(Chunk.chunks[c].name)) + 1; | |
| 279 | + end; | |
| 280 | + end; | |
| 281 | + k := 1; | |
| 282 | + for c := 0 to Chunk.numChunks - 1 do | |
| 283 | + if isExt(c) then | |
| 284 | + putN(Chunk.chunks[c].adr, 8); | |
| 285 | + putN((k shl 32) | 6, 8); (* R_X86_64_GLOB_DAT *) | |
| 286 | + putN(0, 8); | |
| 287 | + INC(k); | |
| 288 | + end; | |
| 289 | + end; | |
| 290 | + putN(1, 4); putN(nsym, 4); putN(nsym - 1, 4); (* the bucket chains every symbol *) | |
| 291 | + putN(0, 4); | |
| 292 | + for i := 1 to nsym - 1 do putN(i - 1, 4); end; | |
| 293 | + putN(0, 1); | |
| 294 | + for i := 0 to numNeeded - 1 do putStr(Scan.nameOf(needed[i])); end; | |
| 295 | + for c := 0 to Chunk.numChunks - 1 do | |
| 296 | + if isExt(c) then putStr(Scan.nameOf(Chunk.chunks[c].name)); end; | |
| 297 | + end; | |
| 298 | + len := Link.roOff + Link.roSize; | |
| 299 | + | |
| 300 | + (* the dynamic table, at the start of the writable segment *) | |
| 301 | + padTo(Link.bssOff); | |
| 302 | + at := 1; | |
| 303 | + for i := 0 to numNeeded - 1 do | |
| 304 | + putN(1, 8); putN(at, 8); (* DT_NEEDED *) | |
| 305 | + at := at + LEN(Scan.nameOf(needed[i])) + 1; | |
| 306 | + end; | |
| 307 | + putN(4, 8); putN(Link.extraAdr + hashAt, 8); (* DT_HASH *) | |
| 308 | + putN(5, 8); putN(Link.extraAdr + strAt, 8); (* DT_STRTAB *) | |
| 309 | + putN(6, 8); putN(Link.extraAdr + symAt, 8); (* DT_SYMTAB *) | |
| 310 | + putN(10, 8); putN(strLen, 8); (* DT_STRSZ *) | |
| 311 | + putN(11, 8); putN(24, 8); (* DT_SYMENT *) | |
| 312 | + putN(7, 8); putN(Link.extraAdr + relaAt, 8); (* DT_RELA *) | |
| 313 | + putN(8, 8); putN((nsym - 1) * 24, 8); (* DT_RELASZ *) | |
| 314 | + putN(9, 8); putN(24, 8); (* DT_RELAENT *) | |
| 315 | + putN(21, 8); putN(0, 8); (* DT_DEBUG, for gdb *) | |
| 316 | + putN(0, 16); (* DT_NULL *) | |
| 317 | +end; | |
| 318 | + | |
| 319 | +proc write*(path: []const U8; entry: INT); | |
| 320 | +var | |
| 321 | + nph, nsh, c, nsyms, sec, strsCap: INT; | |
| 322 | + roExtra, dynSize, symOff, strOff, shstrOff, shOff, strsLen: INT; | |
| 323 | + dbgOff, dbgSize: [3]INT; | |
| 324 | + strs: ^U8; | |
| 325 | + s: []const U8; | |
| 326 | + dyn: BOOL; | |
| 327 | +begin | |
| 328 | + cap := 1 shl 16; | |
| 329 | + buf := Scan.grab(cap); | |
| 330 | + len := 0; | |
| 331 | + dyn := numNeeded > 0; | |
| 332 | + roExtra := 0; dynSize := 0; | |
| 333 | + if dyn then sizeTables(roExtra, dynSize); end; | |
| 334 | + (* room for the most program headers either kind of program has *) | |
| 335 | + Link.linkAll(64 + 6 * 56, roExtra, dynSize); | |
| 336 | + if dyn then | |
| 337 | + nph := 6; | |
| 338 | + else | |
| 339 | + nph := 2; | |
| 340 | + if Link.roSize > 0 then INC(nph); end; | |
| 341 | + if Link.bssSize > 0 then INC(nph); end; | |
| 342 | + end; | |
| 343 | + | |
| 344 | + (* ELF header *) | |
| 345 | + putN(464C457FH, 4); (* #127 'ELF' *) | |
| 346 | + putN(2, 1); putN(1, 1); putN(1, 1); putN(0, 9); (* 64-bit, little-endian, version 1 *) | |
| 347 | + putN(2, 2); (* ET_EXEC *) | |
| 348 | + putN(62, 2); (* EM_X86_64 *) | |
| 349 | + putN(1, 4); | |
| 350 | + putN(Chunk.chunks[entry].adr, 8); | |
| 351 | + putN(64, 8); (* program headers follow *) | |
| 352 | + putN(0, 8); (* section headers: patched below *) | |
| 353 | + putN(0, 4); | |
| 354 | + nsh := 10; | |
| 355 | + if dyn then nsh := 13; end; | |
| 356 | + putN(64, 2); putN(56, 2); putN(nph, 2); | |
| 357 | + putN(64, 2); putN(nsh, 2); putN(6, 2); (* .shstrtab is section 6 *) | |
| 358 | + | |
| 359 | + (* flags: 4 read, 5 read and run, 6 read and write *) | |
| 360 | + if dyn then | |
| 361 | + phdr(PtInterp, 4, Link.extraAdr - Link.roAdr + Link.roOff, Link.extraAdr, LEN(interp) + 1, LEN(interp) + 1, 1); | |
| 362 | + end; | |
| 363 | + (* the first segment maps the headers too, so text can follow them *) | |
| 364 | + phdr(PtLoad, 5, 0, Link.base, Link.textOff + Link.textSize, Link.textOff + Link.textSize, Link.Page); | |
| 365 | + if Link.roSize > 0 then phdr(PtLoad, 4, Link.roOff, Link.roAdr, Link.roSize, Link.roSize, Link.Page); end; | |
| 366 | + if dyn then | |
| 367 | + phdr(PtLoad, 6, Link.bssOff, Link.bssAdr, dynSize, Link.bssSize, Link.Page); | |
| 368 | + phdr(PtDynamic, 6, Link.bssOff, Link.bssAdr, dynSize, dynSize, 8); | |
| 369 | + elsif Link.bssSize > 0 then | |
| 370 | + phdr(PtLoad, 6, 0, Link.bssAdr, 0, Link.bssSize, Link.Page); | |
| 371 | + end; | |
| 372 | + phdr(PtGnuStack, 6, 0, 0, 0, 0, 16); (* a stack that can't run code *) | |
| 373 | + | |
| 374 | + padTo(Link.textOff); | |
| 375 | + putBytes(Link.textImg, Link.textSize); | |
| 376 | + padTo(Link.roOff); (* even when empty, so that every section lies in the file *) | |
| 377 | + putBytes(Link.roImg, Link.roSize); | |
| 378 | + if dyn then putTables; end; | |
| 379 | + | |
| 380 | + (* the symbol table: one local symbol for each named live chunk *) | |
| 381 | + padTo((len + 7) div 8 * 8); | |
| 382 | + symOff := len; | |
| 383 | + strsCap := 1 shl 16; | |
| 384 | + strs := Scan.grab(strsCap); | |
| 385 | + strsLen := 1; | |
| 386 | + strs[0] := 0; | |
| 387 | + putN(0, 24); | |
| 388 | + nsyms := 1; | |
| 389 | + for c := 0 to Chunk.numChunks - 1 do | |
| 390 | + if not stripped and Chunk.chunks[c].live and (Chunk.chunks[c].name >= 0) and not Chunk.chunks[c].ext then | |
| 391 | + s := Chunk.chunkName(c); | |
| 392 | + if strsLen + LEN(s) + 1 > strsCap then | |
| 393 | + strs := Scan.regrab(strs, strsLen, strsLen * 2 + 256); | |
| 394 | + strsCap := strsLen * 2 + 256; | |
| 395 | + end; | |
| 396 | + COPY(strs[strsLen..strsLen + LEN(s)], s); | |
| 397 | + strs[strsLen + LEN(s)] := 0; | |
| 398 | + if Chunk.chunks[c].sec = Chunk.secText then (* the section's index in the headers below *) | |
| 399 | + sec := 1; | |
| 400 | + elsif Chunk.chunks[c].sec = Chunk.secRodata then | |
| 401 | + sec := 2; | |
| 402 | + else | |
| 403 | + sec := 3; | |
| 404 | + end; | |
| 405 | + putN(strsLen, 4); | |
| 406 | + if sec = 1 then putN(2, 1); else putN(1, 1); end; (* STT_FUNC, STT_OBJECT *) | |
| 407 | + putN(0, 1); | |
| 408 | + putN(sec, 2); | |
| 409 | + putN(Chunk.chunks[c].adr, 8); | |
| 410 | + putN(Chunk.chunks[c].size, 8); | |
| 411 | + strsLen := strsLen + LEN(s) + 1; | |
| 412 | + INC(nsyms); | |
| 413 | + end; | |
| 414 | + end; | |
| 415 | + strOff := len; | |
| 416 | + putBytes(strs, strsLen); | |
| 417 | + shstrOff := len; | |
| 418 | + putBytes(SYSTEM.PTR(ShNames), LEN(ShNames)); | |
| 419 | + if stripped or (Link.placesChunk < 0) then | |
| 420 | + for c := 0 to 2 do dbgOff[c] := len; dbgSize[c] := 0; end; | |
| 421 | + else | |
| 422 | + putDebug(dbgOff, dbgSize); | |
| 423 | + end; | |
| 424 | + | |
| 425 | + padTo((len + 7) div 8 * 8); | |
| 426 | + shOff := len; | |
| 427 | + patch(40, shOff, 8); | |
| 428 | + (* types: 1 PROGBITS, 2 SYMTAB, 3 STRTAB, 6 DYNAMIC, 8 NOBITS, 11 DYNSYM; | |
| 429 | + flags: 2 ALLOC, 3 WRITE+ALLOC, 6 ALLOC+EXECINSTR. A symbol table's | |
| 430 | + link is its string table's index: 5 .strtab, 8 .dynstr. The order | |
| 431 | + here is the index the code above relies on. *) | |
| 432 | + shdr("", 0, 0, 0, 0, 0, 0, 0, 0, 0); | |
| 433 | + shdr(".text", 1, 6, Link.base + Link.textOff, Link.textOff, Link.textSize, 0, 0, 16, 0); | |
| 434 | + shdr(".rodata", 1, 2, Link.roAdr, Link.roOff, Link.roSize, 0, 0, 16, 0); | |
| 435 | + shdr(".bss", 8, 3, Link.bssAdr + dynSize, Link.roOff + Link.roSize, Link.bssSize - dynSize, 0, 0, 16, 0); | |
| 436 | + shdr(".symtab", 2, 0, 0, symOff, nsyms * 24, 5, nsyms, 8, 24); | |
| 437 | + shdr(".strtab", 3, 0, 0, strOff, strsLen, 0, 0, 1, 0); | |
| 438 | + shdr(".shstrtab", 3, 0, 0, shstrOff, LEN(ShNames), 0, 0, 1, 0); | |
| 439 | + if dyn then (* the loader's tables, for readelf and objdump *) | |
| 440 | + shdr(".dynamic", 6, 3, Link.bssAdr, Link.bssOff, dynSize, 8, 0, 8, 16); | |
| 441 | + shdr(".dynstr", 3, 2, Link.extraAdr + strAt, Link.extraAdr + strAt - Link.roAdr + Link.roOff, strLen, 0, 0, 1, 0); | |
| 442 | + shdr(".dynsym", 11, 2, Link.extraAdr + symAt, Link.extraAdr + symAt - Link.roAdr + Link.roOff, nsym * 24, 8, 1, 8, 24); | |
| 443 | + end; | |
| 444 | + shdr(".debug_line", 1, 0, 0, dbgOff[0], dbgSize[0], 0, 0, 1, 0); | |
| 445 | + shdr(".debug_info", 1, 0, 0, dbgOff[1], dbgSize[1], 0, 0, 1, 0); | |
| 446 | + shdr(".debug_abbrev", 1, 0, 0, dbgOff[2], dbgSize[2], 0, 0, 1, 0); | |
| 447 | + | |
| 448 | + if not Host.writeExecutable(path, buf[0..len]) then | |
| 449 | + Diag.fatal(Diag.say("can't write {}", [Fmt.str(path)])); | |
| 450 | + end; | |
| 451 | +end; | |
| 452 | + | |
| 453 | +begin | |
| 454 | + mainPath := ""; | |
| 455 | +end Exe. |
| @@ -1,2846 +0,0 @@ | ||
| 1 | -(* Gen: the code generator, for x86-64. | |
| 2 | - | |
| 3 | - Item and Parse drive a stack machine: they push constants, addresses | |
| 4 | - and the values of variables, operate on the values on top, store | |
| 5 | - them and jump on them. Every value has a kind: a word, which is an | |
| 6 | - integer as big as an address, a pointer, a BOOL, a set or a narrower | |
| 7 | - integer; a long, a 64-bit integer on a machine whose word is smaller | |
| 8 | - (never here); an F32; an F64. This unit makes the machine's code for | |
| 9 | - those calls, and nothing above it knows the machine: a port is a new | |
| 10 | - Gen, with an encoder of its own. | |
| 11 | - | |
| 12 | - The stack is kept in registers, as in Wirth's compilers (Niklaus | |
| 13 | - Wirth's Pascal and Oberon compilers): the value at depth d, depth 0 | |
| 14 | - being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to | |
| 15 | - xmm11 if it is a real. A value that needs a register a lower one | |
| 16 | - holds spills that one: stores it in a frame slot, to free the | |
| 17 | - register. A constant, or an address in the frame or of a chunk, is | |
| 18 | - only noted until something needs it in a register, so that it can be | |
| 19 | - an instruction's operand. | |
| 20 | - | |
| 21 | - A slice is two words, a pointer and then a length. | |
| 22 | - | |
| 23 | - Calls use the System V convention, which x86-64 Linux has for C: | |
| 24 | - arguments in rdi, rsi, rdx, rcx, r8, r9 and xmm0 to xmm7, the rest on | |
| 25 | - the machine's stack; rbx, rbp and r12 to r15 are callee-saved, which | |
| 26 | - is to say a called procedure gives them back as it found them. The | |
| 27 | - arguments are computed left to right onto the stack of values, and | |
| 28 | - moved into their registers just before the call: a call in a later | |
| 29 | - argument spills the earlier ones, as any call spills the values under | |
| 30 | - it, so that it can't disturb them. A slice goes as its two words, as | |
| 31 | - System V passes a 16-byte struct of two integers; a record, array or | |
| 32 | - variant passed by value goes as the address of a copy the caller | |
| 33 | - makes. A slice, or two results that are single values, come back in | |
| 34 | - rax and rdx (inTwoRegs); any other record, or several results, comes | |
| 35 | - back in memory the caller provides, through a hidden pointer | |
| 36 | - (behindPointer), where C would send a small struct back in registers. | |
| 37 | - | |
| 38 | - A procedure's frame holds its parameters, its variables, and the | |
| 39 | - temporaries of the statement running, and the results of its calls | |
| 40 | - that hold an array (report 8). The variables are zeroed on | |
| 41 | - entry. | |
| 42 | - | |
| 43 | - Depends on: Scan and Diag for errors, Sym for types, Chunk for code | |
| 44 | - and data, X64 for instructions. | |
| 45 | - Leaves out: what the program means, its types and their checks; that | |
| 46 | - is Item. | |
| 47 | - | |
| 48 | - The seed's `Mark` in the middle of a push or a check can't give an | |
| 49 | - error back, so it is shown and the call goes on without doing what | |
| 50 | - it could not (see newEntry and check): the compile ends with the | |
| 51 | - message, and the parser's recovery makes the stack right again with | |
| 52 | - resetStack. A full stack (newEntry) ends the compile at once: the pops | |
| 53 | - after it would go below the stack. endProc returns its error, for the | |
| 54 | - parser to pass up. | |
| 55 | - | |
| 56 | - What the parser sets, `inPlace`, has a setter, as the exported | |
| 57 | - variables can't be set from outside. The seed's types lose their T | |
| 58 | - (`TAdr` is `Adr`), and its `Call` record, which the procedure `call` | |
| 59 | - takes, is `CallInfo`. *) | |
| 60 | -module Gen; | |
| 61 | -import Fmt, Diag, Scan, Sym, Chunk, X := X64; | |
| 62 | -import SYSTEM; | |
| 63 | - | |
| 64 | -const | |
| 65 | - NoLabel* = -1; | |
| 66 | - WordSize* = 8; (* the size of an address, and of INT *) | |
| 67 | - (* What the machine catches by itself, which the code needn't test for | |
| 68 | - (report section 10): on x86-64 Linux, a division by zero, a stack | |
| 69 | - overflow (at the guard page under the stack), a call to nil, and | |
| 70 | - a load or store within NilPage bytes of address 0, which is never | |
| 71 | - mapped. *) | |
| 72 | - NilPage* = 4096; | |
| 73 | - | |
| 74 | - RAX = X.RAX; RCX = X.RCX; RDX = X.RDX; RSP = X.RSP; RBP = X.RBP; | |
| 75 | - RSI = X.RSI; RDI = X.RDI; R8 = X.R8; R9 = X.R9; R10 = X.R10; R11 = X.R11; | |
| 76 | - | |
| 77 | -type | |
| 78 | - Kind* = (kWord, kLong, kF32, kF64); | |
| 79 | - | |
| 80 | - (* Where a variable is: its address is relative to the frame, to a | |
| 81 | - chunk, to an address on the stack, or to one held in a frame slot *) | |
| 82 | - Base* = (bFrame, bGlobal, bStack, bSlot, bReg, bPtr); | |
| 83 | - Adr* = record | |
| 84 | - base*: Base; | |
| 85 | - chunk*: INT; (* bGlobal: the chunk; bSlot: the frame slot; bReg: the register; | |
| 86 | - bPtr: the register that holds the address *) | |
| 87 | - off*: I64; (* added to the address; for bReg, which word, 0 or 8 *) | |
| 88 | - hi*: INT; (* bReg, a slice: the register of its length, the | |
| 89 | - second word; chunk is that of its pointer *) | |
| 90 | - end; | |
| 91 | - | |
| 92 | - (* The code made so far, to go back to for a second try at a body *) | |
| 93 | - CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end; | |
| 94 | - | |
| 95 | - (* For entryPoint: the address of chunk, put in the variable whose | |
| 96 | - chunk is into before the program starts; or, for C's symbols, the | |
| 97 | - address in chunk, the quadword the loader fills in *) | |
| 98 | - Link* = record chunk*, into*: INT; end; | |
| 99 | - | |
| 100 | - CallInfo* = record | |
| 101 | - sig*: ^Sym.Type; | |
| 102 | - param*: ^Sym.Obj; (* the parameter the next argument is for *) | |
| 103 | - obj*: ^Sym.Obj; (* the procedure called, if it has a name *) | |
| 104 | - n*: INT; (* the arguments *) | |
| 105 | - isF*: [64]BOOL; (* ... goes in an xmm register *) | |
| 106 | - pair*: [64]BOOL; (* ... is a slice's first word, the next its second *) | |
| 107 | - target*: I64; (* the frame slot of a procedure value, if chunk < 0 *) | |
| 108 | - chunk*: INT; (* else the procedure's chunk *) | |
| 109 | - ext*: BOOL; (* C's: called through the chunk holding its address *) | |
| 110 | - ret*: I64; (* set by call: the temporary a slice or a record result is in *) | |
| 111 | - end; | |
| 112 | - | |
| 113 | -const | |
| 114 | - (* the registers a procedure keeps its busiest variables in: System V's | |
| 115 | - callee-saved ones, so they live through calls (a register variable); | |
| 116 | - a procedure saves those it uses, and Rt finds them there for the | |
| 117 | - trap report *) | |
| 118 | - VarRegs*: [5]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15]; | |
| 119 | - (* the registers the trap report can show a variable in: VarRegs, then | |
| 120 | - rdi and rsi, where a procedure that calls nothing keeps the | |
| 121 | - parameters that arrive in them (keptIn) *) | |
| 122 | - ShownRegs*: [7]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15, X.RDI, X.RSI]; | |
| 123 | - (* the registers a procedure keeps its busiest reals in, xmm12 to | |
| 124 | - xmm15, numbered from XmmVar up so that they aren't taken for r12 to | |
| 125 | - r15 (regAt); and in a procedure that calls nothing, which has no | |
| 126 | - real parameter in them, xmm4 to xmm7 too, as nothing else uses | |
| 127 | - them there. Each has a slot too, written as it is, where the trap | |
| 128 | - report finds it, and from which it is loaded again after a call, as | |
| 129 | - System V has no callee-saved xmm register (xHome). *) | |
| 130 | - XmmVar* = 16; | |
| 131 | - XVarRegs*: [8]INT = [XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15, | |
| 132 | - XmmVar + 4, XmmVar + 5, XmmVar + 6, XmmVar + 7]; | |
| 133 | - (* a procedure's registers of variables, as bits: bit i is VarRegs[i], | |
| 134 | - bit XBit + i XVarRegs[i] *) | |
| 135 | - XBit* = 5; | |
| 136 | - | |
| 137 | -var | |
| 138 | - rtFail*, argcChunk*, argvChunk*: INT; | |
| 139 | - (* the stubs that check's code calls, one for each kind of failure, | |
| 140 | - made by entryPoint for those used *) | |
| 141 | - failStubs: [8]INT; | |
| 142 | - lost*: BOOL; (* a parameter kept where it arrived was wanted | |
| 143 | - for other work: the procedure is compiled again, | |
| 144 | - the parameter in memory *) | |
| 145 | - inPlace: INT; (* the register of a variable that the statement | |
| 146 | - being compiled assigns, x := x + e, where the | |
| 147 | - value can be worked out in the variable's own | |
| 148 | - register, from its first push on (lInto) *) | |
| 149 | - depth*: INT; (* the values on the stack *) | |
| 150 | - pushed*: INT; (* quadwords pushed on the machine's stack *) | |
| 151 | - codeChunk*: INT; (* the procedure or body being compiled, or -1 *) | |
| 152 | - | |
| 153 | -type | |
| 154 | - (* where a value on the stack is *) | |
| 155 | - Loc = (lConst, (* not loaded: val is the value *) | |
| 156 | - lHeld, (* not loaded: in register val, a register | |
| 157 | - variable's own (see VarRegs), read in place, which | |
| 158 | - nothing in an expression can change *) | |
| 159 | - lAdr, (* not loaded: the address of chunk, or of the frame | |
| 160 | - if chunk is -1, plus val *) | |
| 161 | - lIdx, (* not loaded: the address m, an element of an array | |
| 162 | - or of a slice in registers: the array's or the | |
| 163 | - pointer's register, or rbp, as the base, and the | |
| 164 | - index in a register variable's or the value's | |
| 165 | - own register (indexSlice, indexArray) *) | |
| 166 | - lMem, (* not loaded: the value of size bytes at m, in the | |
| 167 | - frame, a chunk, or at an address lAdr or lIdx | |
| 168 | - noted, read where it is used (pushVar) *) | |
| 169 | - lInto, (* in register val, a register variable's own, which | |
| 170 | - is what the value goes to (inPlace): so it is | |
| 171 | - changed in place *) | |
| 172 | - lReg, (* in its register *) | |
| 173 | - lFrame); (* spilled: in the frame slot val *) | |
| 174 | - | |
| 175 | - Entry = record | |
| 176 | - loc: Loc; | |
| 177 | - val: I64; (* lConst: the value; lHeld: the register; lAdr: the | |
| 178 | - offset; lFrame: the frame slot *) | |
| 179 | - chunk: INT; (* lAdr: the chunk, or -1 for the frame *) | |
| 180 | - m: X.Mem; (* lIdx, lMem *) | |
| 181 | - size: INT; (* lMem *) | |
| 182 | - signed: BOOL; (* lMem *) | |
| 183 | - k: Kind; | |
| 184 | - off: I64; (* lHeld, lReg: a word, its register plus off, the | |
| 185 | - constant of x + 1 not yet added (op), so that an | |
| 186 | - operand that uses it can take it as its | |
| 187 | - displacement: [r13 + r9 * 8 - 8] *) | |
| 188 | - end; | |
| 189 | - | |
| 190 | - (* a check of the procedure, whose failure endFails makes *) | |
| 191 | - Fail = record | |
| 192 | - L, what, ra, name: INT; | |
| 193 | - path, line, col: INT; (* the place, told now: an inlined procedure's | |
| 194 | - is in its own file (Parse's inlined) *) | |
| 195 | - m: X.Mem; | |
| 196 | - n: I64; | |
| 197 | - end; | |
| 198 | - | |
| 199 | -const | |
| 200 | - MaxStack = 1024; (* values waiting in one expression: 63 nested calls of up to 15 arguments each *) | |
| 201 | - StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; | |
| 202 | - (* the registers of the stack: the value at depth d is in Regs[d mod | |
| 203 | - NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi | |
| 204 | - and rdi, which copying needs, aren't among them. *) | |
| 205 | - NumRegs = 4; | |
| 206 | - Regs: [NumRegs]INT = [R8, R9, R10, R11]; | |
| 207 | - XRegs: [NumRegs]INT = [8, 9, 10, 11]; (* xmm8 to xmm11 *) | |
| 208 | - IntRegs: [6]INT = [RDI, RSI, RDX, RCX, R8, R9]; | |
| 209 | - | |
| 210 | -var | |
| 211 | - stack: [MaxStack]Entry; | |
| 212 | - owner: [NumRegs]INT; (* the value in each register, or -1 *) | |
| 213 | - frameTop, frameMax, permTop: I64; | |
| 214 | - frameFix: INT; (* where the frame size is patched in *) | |
| 215 | - paramTop: I64; (* the parameters' slots end here *) | |
| 216 | - zeroTop: I64; (* the locals the prologue zeroes end here *) | |
| 217 | - retSlot: I64; (* holds the hidden result pointer *) | |
| 218 | - resType: ^Sym.Type; (* the procedure's result, or nil *) | |
| 219 | - exitL: INT; | |
| 220 | - (* parameters arriving in registers: where each is stored on entry *) | |
| 221 | - inReg: [14]INT; | |
| 222 | - inSlot: [14]I64; | |
| 223 | - inSize: [14]INT; | |
| 224 | - inObj: [14]^Sym.Obj; | |
| 225 | - numIn: INT; | |
| 226 | - procParams: ^Sym.Obj; (* the parameters, for enterBody *) | |
| 227 | - regMask: INT; (* the registers of variables, as bits (XBit) *) | |
| 228 | - kept: INT; (* bit r: register r holds a parameter, as it arrived *) | |
| 229 | - saveAt: I64; (* where they are saved, a quadword each *) | |
| 230 | - homes: [8]I64; (* the slots of the variables in XVarRegs *) | |
| 231 | - frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) | |
| 232 | - fails: [4096]Fail; | |
| 233 | - numFails: INT; | |
| 234 | - | |
| 235 | -proc setInPlace*(r: INT); | |
| 236 | -begin | |
| 237 | - inPlace := r; | |
| 238 | -end; | |
| 239 | - | |
| 240 | -proc kindOf*(t: ^Sym.Type): Kind; | |
| 241 | -begin | |
| 242 | - if t^.form = Sym.fReal then | |
| 243 | - if t^.size = 4 then return kF32; end; | |
| 244 | - return kF64; | |
| 245 | - end; | |
| 246 | - if t^.size > WordSize then return kLong; end; | |
| 247 | - return kWord; | |
| 248 | -end; | |
| 249 | - | |
| 250 | -(* The stack, as in Wirth's compilers: the value at depth d is held in | |
| 251 | - register d mod NumRegs. A value, when a higher one wants its register, | |
| 252 | - and every value in a register when a call would clobber them, is | |
| 253 | - spilled to a frame slot, and loaded again where it is used. A constant | |
| 254 | - is only noted. | |
| 255 | - | |
| 256 | - drop emits no code, and a dropped value's register still holds it | |
| 257 | - until the next push claims it: place (for bStack), operands, | |
| 258 | - indexSlice and jumpCmp read a register after dropping its value. | |
| 259 | - | |
| 260 | - Registers outside the stack, free within one operation: rax, rcx and | |
| 261 | - rdx for arithmetic; rsi and rdi for copying; and, where the stack holds | |
| 262 | - nothing in them, r10 for the prologue's zeroing loop and r11 for a call | |
| 263 | - through a procedure value; xmm0 and xmm1 for reals. | |
| 264 | - | |
| 265 | - A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the | |
| 266 | - machine computes with reals, the value at depth d in XRegs[d mod | |
| 267 | - NumRegs]: so each depth has two registers, and holds its value in the | |
| 268 | - one of its kind, which owner doesn't tell apart. The xmm registers | |
| 269 | - from xmm8 up carry no arguments, so that call can move a real into | |
| 270 | - xmm0 to xmm7 without overwriting another still to go. *) | |
| 271 | - | |
| 272 | -proc fits32(v: I64): BOOL; | |
| 273 | -begin | |
| 274 | - return (v >= -2147483648) and (v <= 2147483647); | |
| 275 | -end; | |
| 276 | - | |
| 277 | -(* The register of kind k for the value at depth i *) | |
| 278 | -proc regIn(k: Kind; i: INT): INT; | |
| 279 | -begin | |
| 280 | - if k in {kF32, kF64} then return XRegs[i mod NumRegs]; end; | |
| 281 | - return Regs[i mod NumRegs]; | |
| 282 | -end; | |
| 283 | - | |
| 284 | -proc regOf(i: INT): INT; | |
| 285 | -begin | |
| 286 | - return regIn(stack[i].k, i); | |
| 287 | -end; | |
| 288 | - | |
| 289 | -proc isReal(i: INT): BOOL; | |
| 290 | -begin | |
| 291 | - return stack[i].k in {kF32, kF64}; | |
| 292 | -end; | |
| 293 | - | |
| 294 | -(* A real constant, its bits v, as an operand: 8 bytes in read-only | |
| 295 | - data, as the machine has no instruction that holds one *) | |
| 296 | -proc realConst(v: I64): X.Mem; | |
| 297 | -var c: INT; | |
| 298 | -begin | |
| 299 | - c := Chunk.newChunk(Chunk.secRodata, -1, -1); | |
| 300 | - Chunk.setData(c, ^U8(SYSTEM.ADR(v)), 8, 8); | |
| 301 | - return X.memRip(c, 0); | |
| 302 | -end; | |
| 303 | - | |
| 304 | -(* The real i into xmm register x, from wherever it is, as valueTo does | |
| 305 | - for an integer register *) | |
| 306 | -proc xValueTo(i, x: INT); | |
| 307 | -begin | |
| 308 | - match stack[i].loc of | |
| 309 | - | lConst: X.movsXM(x, realConst(stack[i].val), stack[i].k = kF32); | |
| 310 | - | lMem: X.movsXM(x, stack[i].m, stack[i].size = 4); | |
| 311 | - | lHeld: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end; | |
| 312 | - | lInto: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end; | |
| 313 | - | lReg: if regOf(i) # x then X.movX(x, regOf(i)); end; | |
| 314 | - | else: X.movsXM(x, X.mem(RBP, stack[i].val), false); (* spilled, as 8 bytes *) | |
| 315 | - end; | |
| 316 | -end; | |
| 317 | - | |
| 318 | -proc loaded(i: INT): INT; forward; | |
| 319 | -proc allocPerm*(size, align: I64): I64; forward; | |
| 320 | -proc allocTemp*(size, align: I64): I64; forward; | |
| 321 | - | |
| 322 | -(* The value i, in a register, goes to a frame slot; one noted that | |
| 323 | - holds its register, as an index, is worked out in it first *) | |
| 324 | -proc spill(i: INT); | |
| 325 | -var s: I64; | |
| 326 | -begin | |
| 327 | - if (stack[i].loc # lReg) or (stack[i].off # 0) then _ := loaded(i); end; | |
| 328 | - s := allocTemp(8, 8); | |
| 329 | - if isReal(i) then X.movsMX(X.mem(RBP, s), regOf(i), false); | |
| 330 | - else X.store(X.mem(RBP, s), regOf(i), 8); end; | |
| 331 | - stack[i].loc := lFrame; | |
| 332 | - stack[i].val := s; | |
| 333 | - owner[i mod NumRegs] := -1; | |
| 334 | -end; | |
| 335 | - | |
| 336 | -(* Each value below depth d read from memory now, as what comes next | |
| 337 | - may change it *) | |
| 338 | -proc readMems(d: INT); | |
| 339 | -var i: INT; | |
| 340 | -begin | |
| 341 | - for i := 0 to d - 1 do | |
| 342 | - if stack[i].loc = lMem then _ := loaded(i); end; | |
| 343 | - end; | |
| 344 | -end; | |
| 345 | - | |
| 346 | -(* Every value below depth d in a register, spilled; one noted in memory | |
| 347 | - is read first, as a call may change it *) | |
| 348 | -proc spillBelow(d: INT); | |
| 349 | -var r: INT; | |
| 350 | -begin | |
| 351 | - readMems(d); | |
| 352 | - for r := 0 to NumRegs - 1 do | |
| 353 | - if (owner[r] >= 0) and (owner[r] < d) then spill(owner[r]); end; | |
| 354 | - end; | |
| 355 | -end; | |
| 356 | - | |
| 357 | -(* The register of kind k of value i, for it to hold: a lower value at | |
| 358 | - its depth's registers is spilled. *) | |
| 359 | -proc claim(i: INT; k: Kind): INT; | |
| 360 | -var r: INT; | |
| 361 | -begin | |
| 362 | - r := i mod NumRegs; | |
| 363 | - if (owner[r] >= 0) and (owner[r] # i) then spill(owner[r]); end; | |
| 364 | - owner[r] := i; | |
| 365 | - return regIn(k, i); | |
| 366 | -end; | |
| 367 | - | |
| 368 | -(* The memory at the noted address i, plus off *) | |
| 369 | -proc adrMem(i: INT; off: I64): X.Mem; | |
| 370 | -begin | |
| 371 | - if stack[i].chunk < 0 then return X.mem(RBP, stack[i].val + off); end; | |
| 372 | - return X.memRip(stack[i].chunk, INT(stack[i].val + off)); | |
| 373 | -end; | |
| 374 | - | |
| 375 | -(* The value i, in its register *) | |
| 376 | -proc loaded(i: INT): INT; | |
| 377 | -var r: INT; | |
| 378 | -begin | |
| 379 | - if stack[i].loc = lInto then return INT(stack[i].val); end; | |
| 380 | - if (stack[i].loc # lReg) and isReal(i) then | |
| 381 | - xValueTo(i, claim(i, stack[i].k)); | |
| 382 | - stack[i].loc := lReg; | |
| 383 | - elsif stack[i].loc # lReg then | |
| 384 | - r := claim(i, kWord); | |
| 385 | - match stack[i].loc of | |
| 386 | - | lConst: X.movRI(r, stack[i].val); | |
| 387 | - | lHeld: X.movRR(r, INT(stack[i].val)); | |
| 388 | - | lAdr: X.lea(r, adrMem(i, 0)); | |
| 389 | - | lIdx: X.lea(r, stack[i].m); | |
| 390 | - | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 391 | - | else: X.load(r, X.mem(RBP, stack[i].val), 8, false); | |
| 392 | - end; | |
| 393 | - stack[i].loc := lReg; | |
| 394 | - end; | |
| 395 | - if stack[i].off # 0 then | |
| 396 | - X.lea(regOf(i), X.mem(regOf(i), stack[i].off)); | |
| 397 | - stack[i].off := 0; | |
| 398 | - end; | |
| 399 | - return regOf(i); | |
| 400 | -end; | |
| 401 | - | |
| 402 | -(* The register value i is in, only to be read: a variable's own, or | |
| 403 | - its register *) | |
| 404 | -proc peek(i: INT): INT; | |
| 405 | -begin | |
| 406 | - if (stack[i].loc = lHeld) and (stack[i].off = 0) then return INT(stack[i].val); end; | |
| 407 | - return loaded(i); | |
| 408 | -end; | |
| 409 | - | |
| 410 | -(* The offset of value i, taken from it, for the caller to add or to | |
| 411 | - make a displacement of *) | |
| 412 | -proc takeOff(i: INT): I64; | |
| 413 | -var o: I64; | |
| 414 | -begin | |
| 415 | - o := stack[i].off; | |
| 416 | - stack[i].off := 0; | |
| 417 | - return o; | |
| 418 | -end; | |
| 419 | - | |
| 420 | -(* The word on top, in a register, has o added: noted, if o can be a | |
| 421 | - displacement, else added now *) | |
| 422 | -proc putOff(o: I64); | |
| 423 | -begin | |
| 424 | - if fits32(stack[depth - 1].off + o) then | |
| 425 | - stack[depth - 1].off := stack[depth - 1].off + o; | |
| 426 | - else | |
| 427 | - X.movRI(RCX, o); | |
| 428 | - X.alu(X.aluAdd, loaded(depth - 1), RCX); | |
| 429 | - end; | |
| 430 | -end; | |
| 431 | - | |
| 432 | -proc top: INT; | |
| 433 | -begin | |
| 434 | - if depth = 0 then Diag.fatal("internal error: the stack is empty"); end; | |
| 435 | - return loaded(depth - 1); | |
| 436 | -end; | |
| 437 | - | |
| 438 | -(* A value on the stack's top. At the stack's last place the error is shown | |
| 439 | - and the compile ends there: with nothing pushed, the pops after it would | |
| 440 | - go below the stack. See the head of this unit. *) | |
| 441 | -proc newEntry(k: Kind; loc: Loc; v: I64); | |
| 442 | -begin | |
| 443 | - if depth > LEN(stack) - 1 then | |
| 444 | - _ := Scan.mark(Diag.say("more than {} values waiting in one expression", [Fmt.int(MaxStack)])); | |
| 445 | - Diag.finish(); (* ends the compile: an error was counted *) | |
| 446 | - return; | |
| 447 | - end; | |
| 448 | - stack[depth].k := k; | |
| 449 | - stack[depth].loc := loc; | |
| 450 | - stack[depth].val := v; | |
| 451 | - stack[depth].chunk := -1; | |
| 452 | - stack[depth].off := 0; | |
| 453 | - INC(depth); | |
| 454 | -end; | |
| 455 | - | |
| 456 | -(* An address, lIdx, or a value at it, lMem, noted as the operand m. Its | |
| 457 | - index may be in the register of the value just dropped, at this | |
| 458 | - depth: it is the new value's own, then. *) | |
| 459 | -proc newMem(loc: Loc; m: X.Mem; k: Kind; size: INT; signed: BOOL); | |
| 460 | -begin | |
| 461 | - newEntry(k, loc, 0); | |
| 462 | - stack[depth - 1].m := m; | |
| 463 | - stack[depth - 1].size := size; | |
| 464 | - stack[depth - 1].signed := signed; | |
| 465 | - if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1; end; | |
| 466 | -end; | |
| 467 | - | |
| 468 | -(* The register a new value on top is to be computed in *) | |
| 469 | -proc target: INT; | |
| 470 | -begin | |
| 471 | - return claim(depth, kWord); | |
| 472 | -end; | |
| 473 | - | |
| 474 | -(* The xmm register a new real on top is to be computed in *) | |
| 475 | -proc xTarget: INT; | |
| 476 | -begin | |
| 477 | - return claim(depth, kF64); | |
| 478 | -end; | |
| 479 | - | |
| 480 | -(* The value just computed in target goes on top *) | |
| 481 | -proc computed(k: Kind); | |
| 482 | -begin | |
| 483 | - newEntry(k, lReg, 0); | |
| 484 | -end; | |
| 485 | - | |
| 486 | -proc drop*; | |
| 487 | -begin | |
| 488 | - DEC(depth); | |
| 489 | - if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; end; | |
| 490 | -end; | |
| 491 | - | |
| 492 | -(* rsi and rdi are about to be used for other work, as in copying: a | |
| 493 | - parameter kept in either must go to memory (lost). *) | |
| 494 | -proc clobberArgs; | |
| 495 | -begin | |
| 496 | - if kept # 0 then lost := true; end; | |
| 497 | -end; | |
| 498 | - | |
| 499 | -(* The value i into register r, from wherever it is: a constant or an | |
| 500 | - address is made there, a variable's register or the value's own | |
| 501 | - register copied, a spilled value loaded. Unlike loaded, it leaves the | |
| 502 | - value where it was, so r is its only new copy. *) | |
| 503 | -proc valueTo(i, r: INT); | |
| 504 | -begin | |
| 505 | - if isReal(i) and (stack[i].loc in {lHeld, lInto}) then | |
| 506 | - X.movqRX(r, INT(stack[i].val)); | |
| 507 | - else | |
| 508 | - match stack[i].loc of | |
| 509 | - | lConst: X.movRI(r, stack[i].val); | |
| 510 | - | lHeld: X.movRR(r, INT(stack[i].val)); | |
| 511 | - | lInto: X.movRR(r, INT(stack[i].val)); | |
| 512 | - | lAdr: X.lea(r, adrMem(i, 0)); | |
| 513 | - | lIdx: X.lea(r, stack[i].m); | |
| 514 | - | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 515 | - | lReg: | |
| 516 | - if isReal(i) then X.movqRX(r, regOf(i)); | |
| 517 | - elsif regOf(i) # r then X.movRR(r, regOf(i)); end; | |
| 518 | - | else: X.load(r, X.mem(RBP, stack[i].val), 8, false); | |
| 519 | - end; | |
| 520 | - end; | |
| 521 | - if stack[i].off # 0 then X.lea(r, X.mem(r, stack[i].off)); end; | |
| 522 | -end; | |
| 523 | - | |
| 524 | -(* The value on top, popped into register r *) | |
| 525 | -proc popTo(r: INT); | |
| 526 | -begin | |
| 527 | - valueTo(depth - 1, r); | |
| 528 | - drop(); | |
| 529 | -end; | |
| 530 | - | |
| 531 | -(* Whether the value on top is a constant, and if so, which *) | |
| 532 | -proc topConst: (BOOL, I64); | |
| 533 | -begin | |
| 534 | - return stack[depth - 1].loc = lConst, stack[depth - 1].val; | |
| 535 | -end; | |
| 536 | - | |
| 537 | -proc resetStack*(d, p: INT); | |
| 538 | -var r: INT; | |
| 539 | -begin | |
| 540 | - depth := d; | |
| 541 | - pushed := p; | |
| 542 | - for r := 0 to NumRegs - 1 do owner[r] := -1; end; | |
| 543 | -end; | |
| 544 | - | |
| 545 | -proc pushConst*(k: Kind; v: I64); | |
| 546 | -begin | |
| 547 | - newEntry(k, lConst, v); | |
| 548 | -end; | |
| 549 | - | |
| 550 | -(* The register of the word at a, a variable in registers: the variable's | |
| 551 | - own, or for a slice its pointer's (off 0) or its length's (off 8) *) | |
| 552 | -proc regAt(a: Adr): INT; | |
| 553 | -var r: INT; | |
| 554 | -begin | |
| 555 | - if a.off = 0 then r := a.chunk; else r := a.hi; end; | |
| 556 | - if r >= XmmVar then r := r - XmmVar; end; (* an xmm register, XVarRegs *) | |
| 557 | - return r; | |
| 558 | -end; | |
| 559 | - | |
| 560 | -(* The memory a names. An address on the stack is popped, and is in its | |
| 561 | - register still; one in a slot is loaded into r. *) | |
| 562 | -proc place(a: Adr; r: INT): X.Mem; | |
| 563 | -var m: X.Mem; ra: INT; o: I64; | |
| 564 | -begin | |
| 565 | - if a.base = bReg then Diag.fatal("internal error: a register has no address"); end; | |
| 566 | - match a.base of | |
| 567 | - | bFrame: return X.mem(RBP, a.off); | |
| 568 | - | bGlobal: return X.memRip(a.chunk, INT(a.off)); | |
| 569 | - | bStack: | |
| 570 | - if stack[depth - 1].loc = lAdr then | |
| 571 | - m := adrMem(depth - 1, a.off); | |
| 572 | - drop(); | |
| 573 | - return m; | |
| 574 | - elsif stack[depth - 1].loc = lIdx then | |
| 575 | - m := stack[depth - 1].m; | |
| 576 | - m.disp := m.disp + INT(a.off); | |
| 577 | - drop(); | |
| 578 | - return m; | |
| 579 | - end; | |
| 580 | - (* an offset noted on the address is the operand's, as | |
| 581 | - p^.f + 8: [r8 + 8] *) | |
| 582 | - if fits32(a.off + stack[depth - 1].off) then o := takeOff(depth - 1); else o := 0; end; | |
| 583 | - ra := peek(depth - 1); | |
| 584 | - drop(); | |
| 585 | - return X.mem(ra, a.off + o); | |
| 586 | - | bPtr: return X.mem(a.chunk, a.off); | |
| 587 | - | else: (* bSlot: the address is in the frame slot a.chunk *) | |
| 588 | - X.load(r, X.mem(RBP, a.chunk), 8, false); | |
| 589 | - return X.mem(r, a.off); | |
| 590 | - end; | |
| 591 | -end; | |
| 592 | - | |
| 593 | -(* The address a names, on top of the stack *) | |
| 594 | -proc pushAdr*(a: Adr); | |
| 595 | -var m: X.Mem; r: INT; | |
| 596 | -begin | |
| 597 | - if (a.base = bStack) and (a.off = 0) then return; end; (* it is on top already *) | |
| 598 | - if a.base in {bFrame, bGlobal} then | |
| 599 | - newEntry(kWord, lAdr, a.off); | |
| 600 | - if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; end; | |
| 601 | - return; | |
| 602 | - end; | |
| 603 | - (* The order matters. With the address on the stack, place pops it and | |
| 604 | - must come first, so that target may reuse its register; otherwise | |
| 605 | - target comes first, since place may need r to load a slot's pointer. *) | |
| 606 | - if a.base = bStack then | |
| 607 | - m := place(a, -1); r := target(); | |
| 608 | - else | |
| 609 | - r := target(); m := place(a, r); | |
| 610 | - end; | |
| 611 | - (* an address that is already in r, [r], needs no instruction *) | |
| 612 | - if (m.base # r) or (m.index # X.NoIndex) or (m.disp # 0) then X.lea(r, m); end; | |
| 613 | - computed(kWord); | |
| 614 | -end; | |
| 615 | - | |
| 616 | -proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind); | |
| 617 | -var m: X.Mem; r: INT; | |
| 618 | -begin | |
| 619 | - r := inPlace; | |
| 620 | - inPlace := -1; | |
| 621 | - if r >= XmmVar then r := r - XmmVar; end; | |
| 622 | - if (a.base = bReg) and (regAt(a) = r) then | |
| 623 | - newEntry(k, lInto, r); | |
| 624 | - return; | |
| 625 | - end; | |
| 626 | - if a.base = bReg then | |
| 627 | - newEntry(k, lHeld, regAt(a)); | |
| 628 | - return; | |
| 629 | - end; | |
| 630 | - (* a variable in the frame or a chunk, or at an address only noted, is | |
| 631 | - only noted too: read where it is used, as an operand if it can be *) | |
| 632 | - if (a.base in {bFrame, bGlobal, bPtr}) or (a.base = bStack) and (stack[depth - 1].loc in {lAdr, lIdx}) then | |
| 633 | - m := place(a, -1); | |
| 634 | - newMem(lMem, m, k, size, signed); | |
| 635 | - return; | |
| 636 | - end; | |
| 637 | - (* in the order pushAdr explains *) | |
| 638 | - if a.base = bStack then | |
| 639 | - m := place(a, -1); r := target(); | |
| 640 | - else | |
| 641 | - r := target(); m := place(a, r); | |
| 642 | - end; | |
| 643 | - if k in {kF32, kF64} then X.movsXM(xTarget(), m, size = 4); | |
| 644 | - else X.load(r, m, size, signed); end; | |
| 645 | - computed(k); | |
| 646 | -end; | |
| 647 | - | |
| 648 | -proc pushProc*(c: INT; ext: BOOL); | |
| 649 | -var r: INT; | |
| 650 | -begin | |
| 651 | - r := target(); | |
| 652 | - if ext then X.load(r, X.memRip(c, 0), 8, false); | |
| 653 | - else X.lea(r, X.memRip(c, 0)); end; | |
| 654 | - computed(kWord); | |
| 655 | -end; | |
| 656 | - | |
| 657 | -(* Where xmm register r is in XVarRegs *) | |
| 658 | -proc xIndex(r: INT): INT; | |
| 659 | -begin | |
| 660 | - if r >= XVarRegs[0] then return r - XVarRegs[0]; end; | |
| 661 | - return r - XVarRegs[4] + 4; | |
| 662 | -end; | |
| 663 | - | |
| 664 | -(* The slot of the variable in xmm register x *) | |
| 665 | -proc homeOf(x: INT): X.Mem; | |
| 666 | -begin | |
| 667 | - return X.mem(RBP, homes[xIndex(x + XmmVar)]); | |
| 668 | -end; | |
| 669 | - | |
| 670 | -(* Pops a value into the variable at a; an address on the stack is under | |
| 671 | - the value. *) | |
| 672 | -proc popVar*(a: Adr; size: INT); | |
| 673 | -var r: INT; v: I64; c, fl: BOOL; | |
| 674 | -begin | |
| 675 | - if (a.base = bReg) and isReal(depth - 1) then | |
| 676 | - xValueTo(depth - 1, regAt(a)); | |
| 677 | - drop(); | |
| 678 | - X.movsMX(homeOf(regAt(a)), regAt(a), false); (* written through, as xHome says *) | |
| 679 | - return; | |
| 680 | - end; | |
| 681 | - if a.base = bReg then | |
| 682 | - c, v := topConst(); | |
| 683 | - if c then X.movRI(regAt(a), v); | |
| 684 | - elsif peek(depth - 1) # regAt(a) then X.movRR(regAt(a), peek(depth - 1)); end; (* else it is there, lInto *) | |
| 685 | - drop(); | |
| 686 | - return; | |
| 687 | - end; | |
| 688 | - c, v := topConst(); | |
| 689 | - if c and fits32(v) then | |
| 690 | - drop(); | |
| 691 | - X.storeI(place(a, RCX), INT(v), size); | |
| 692 | - return; | |
| 693 | - end; | |
| 694 | - if c then r := RAX; X.movRI(RAX, v); | |
| 695 | - else r := peek(depth - 1); end; | |
| 696 | - fl := (stack[depth - 1].loc # lConst) and isReal(depth - 1); | |
| 697 | - drop(); | |
| 698 | - if fl then X.movsMX(place(a, RCX), r, size = 4); | |
| 699 | - else X.store(place(a, RCX), r, size); end; | |
| 700 | -end; | |
| 701 | - | |
| 702 | -(* Register src to dst, an xmm register's if fl *) | |
| 703 | -proc movReg(fl: BOOL; dst, src: INT); | |
| 704 | -begin | |
| 705 | - if fl then X.movX(dst, src); else X.movRR(dst, src); end; | |
| 706 | -end; | |
| 707 | - | |
| 708 | -(* The two values on top, of one kind, change places. *) | |
| 709 | -proc swap*; | |
| 710 | -var e: Entry; x, y, t: INT; rx, ry, fl: BOOL; | |
| 711 | -begin | |
| 712 | - x := depth - 2; y := depth - 1; | |
| 713 | - (* a value noted at an address that uses its register is worked out, | |
| 714 | - so that its register moves with it *) | |
| 715 | - if stack[x].loc in {lIdx, lMem} then _ := loaded(x); end; | |
| 716 | - if stack[y].loc in {lIdx, lMem} then _ := loaded(y); end; | |
| 717 | - rx := stack[x].loc = lReg; | |
| 718 | - ry := stack[y].loc = lReg; | |
| 719 | - fl := isReal(y); | |
| 720 | - if fl then t := 0; else t := RAX; end; (* xmm0, or rax *) | |
| 721 | - if rx and ry then | |
| 722 | - movReg(fl, t, regOf(x)); | |
| 723 | - movReg(fl, regOf(x), regOf(y)); | |
| 724 | - movReg(fl, regOf(y), t); | |
| 725 | - elsif rx then | |
| 726 | - owner[x mod NumRegs] := -1; | |
| 727 | - movReg(fl, claim(y, stack[y].k), regOf(x)); | |
| 728 | - elsif ry then | |
| 729 | - owner[y mod NumRegs] := -1; | |
| 730 | - movReg(fl, claim(x, stack[x].k), regOf(y)); | |
| 731 | - end; | |
| 732 | - e := stack[x]; stack[x] := stack[y]; stack[y] := e; | |
| 733 | -end; | |
| 734 | - | |
| 735 | -(* Copying *) | |
| 736 | - | |
| 737 | -(* The most that copyBytes and zeroBytes do with moves, in place of rep | |
| 738 | - movsb and rep stosb, which take some twenty cycles to start, longer | |
| 739 | - than the few moves take. *) | |
| 740 | -const MaxMoves = 256; | |
| 741 | - | |
| 742 | -(* Up to this size a copy goes as words: a record just made field by | |
| 743 | - field is read back by loads as wide as its stores, and a 16-byte | |
| 744 | - load across two 8-byte stores waits for them to reach the cache. *) | |
| 745 | -const MaxWordMoves = 64; | |
| 746 | - | |
| 747 | -(* size bytes from [rsi] to [rdi], through xmm0 and rcx; rsi and rdi are | |
| 748 | - left as they fall. Up to MaxMoves bytes, as a record or a small array | |
| 749 | - mostly is, they go as moves: of 16 bytes from MaxWordMoves up, then | |
| 750 | - of 8, 4, 2 and 1. *) | |
| 751 | -proc copyBytes(size: I64); | |
| 752 | -var o, n: I64; | |
| 753 | -begin | |
| 754 | - if size > MaxMoves then | |
| 755 | - X.movRI(RCX, size); | |
| 756 | - X.repMovsb(); | |
| 757 | - return; | |
| 758 | - end; | |
| 759 | - o := 0; | |
| 760 | - while (size > MaxWordMoves) and (o + 16 <= size) do | |
| 761 | - X.movdquXM(0, X.mem(RSI, o)); | |
| 762 | - X.movdquMX(X.mem(RDI, o), 0); | |
| 763 | - INC(o, 16); | |
| 764 | - end; | |
| 765 | - while o < size do | |
| 766 | - n := 8; | |
| 767 | - while n > size - o do n := n div 2; end; | |
| 768 | - X.load(RCX, X.mem(RSI, o), INT(n), false); | |
| 769 | - X.store(X.mem(RDI, o), RCX, INT(n)); | |
| 770 | - INC(o, n); | |
| 771 | - end; | |
| 772 | -end; | |
| 773 | - | |
| 774 | -(* Pops a source's address, and a destination's under it, and copies | |
| 775 | - size bytes to off bytes past the destination. *) | |
| 776 | -proc copyBlock*(size, off: I64); | |
| 777 | -begin | |
| 778 | - clobberArgs(); | |
| 779 | - popTo(RSI); | |
| 780 | - popTo(RDI); | |
| 781 | - if off # 0 then X.lea(RDI, X.mem(RDI, off)); end; | |
| 782 | - copyBytes(size); | |
| 783 | -end; | |
| 784 | - | |
| 785 | -(* Pops a source's address, and copies size bytes to a: for a slice in | |
| 786 | - registers, its two words into them. *) | |
| 787 | -proc copyTo*(a: Adr; size: I64); | |
| 788 | -begin | |
| 789 | - clobberArgs(); | |
| 790 | - popTo(RSI); | |
| 791 | - if a.base = bReg then | |
| 792 | - X.load(a.chunk, X.mem(RSI, 0), 8, false); | |
| 793 | - X.load(a.hi, X.mem(RSI, 8), 8, false); | |
| 794 | - return; | |
| 795 | - end; | |
| 796 | - X.lea(RDI, place(a, RDI)); | |
| 797 | - copyBytes(size); | |
| 798 | -end; | |
| 799 | - | |
| 800 | -(* size zero bytes at [rdi], through xmm0 and rax; rdi is left as it | |
| 801 | - falls. Up to MaxMoves bytes they go as stores of 16, then of 8, 4, 2 | |
| 802 | - and 1 bytes, as copyBytes does. *) | |
| 803 | -proc zeroBytes(size: I64); | |
| 804 | -var o, n: I64; | |
| 805 | -begin | |
| 806 | - if size > MaxMoves then | |
| 807 | - X.movRI(RAX, 0); | |
| 808 | - X.movRI(RCX, size); | |
| 809 | - X.repStos(1); | |
| 810 | - return; | |
| 811 | - end; | |
| 812 | - o := 0; | |
| 813 | - if size >= 16 then X.pxorXX(0); end; | |
| 814 | - while o + 16 <= size do | |
| 815 | - X.movdquMX(X.mem(RDI, o), 0); | |
| 816 | - INC(o, 16); | |
| 817 | - end; | |
| 818 | - if o < size then X.movRI(RAX, 0); end; | |
| 819 | - while o < size do | |
| 820 | - n := 8; | |
| 821 | - while n > size - o do n := n div 2; end; | |
| 822 | - X.store(X.mem(RDI, o), RAX, INT(n)); | |
| 823 | - INC(o, n); | |
| 824 | - end; | |
| 825 | -end; | |
| 826 | - | |
| 827 | -proc zeroVar*(a: Adr; size: I64); | |
| 828 | -begin | |
| 829 | - clobberArgs(); | |
| 830 | - X.lea(RDI, place(a, RDI)); | |
| 831 | - zeroBytes(size); | |
| 832 | -end; | |
| 833 | - | |
| 834 | -(* Operations *) | |
| 835 | - | |
| 836 | -(* n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two *) | |
| 837 | -proc exactLog2(v: I64): INT; | |
| 838 | -var n: INT; | |
| 839 | -begin | |
| 840 | - n := 0; | |
| 841 | - while (v > 1) and ((v & 1) = 0) do v := v shr 1; INC(n); end; | |
| 842 | - if v # 1 then n := -1; end; | |
| 843 | - return n; | |
| 844 | -end; | |
| 845 | - | |
| 846 | -(* r times v, a size: by a shift where v is a power of two, as it | |
| 847 | - mostly is, since a shift takes one cycle and imul three *) | |
| 848 | -proc mulI(r: INT; v: I64); | |
| 849 | -begin | |
| 850 | - if exactLog2(v) > 0 then X.shiftI(X.shShl, r, exactLog2(v)); | |
| 851 | - elsif v # 1 then X.imulI(r, INT(v)); end; | |
| 852 | -end; | |
| 853 | - | |
| 854 | -(* r compared with v, through rdx if v needs more than 32 bits; with | |
| 855 | - zero by test, which sets the flags as cmp would and is shorter *) | |
| 856 | -proc cmpI(r: INT; v: I64); | |
| 857 | -begin | |
| 858 | - if v = 0 then X.testRR(r, r); | |
| 859 | - elsif fits32(v) then X.aluI(X.aluCmp, r, INT(v)); | |
| 860 | - else X.movRI(RDX, v); X.alu(X.aluCmp, r, RDX); end; | |
| 861 | -end; | |
| 862 | - | |
| 863 | -(* r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 864 | - zero, so a negative dividend first gets 2^k - 1 added (the bias). *) | |
| 865 | -proc divPow2(sy: Scan.Sym; r, k: INT; unsign: BOOL); | |
| 866 | -begin | |
| 867 | - (* unsigned: div shifts right; mod keeps the low k bits, by shifting | |
| 868 | - them up to the top and back down *) | |
| 869 | - if unsign then | |
| 870 | - if sy = Scan.sDiv then X.shiftI(X.shShr, r, k); | |
| 871 | - else X.shiftI(X.shShl, r, 64 - k); X.shiftI(X.shShr, r, 64 - k); end; | |
| 872 | - return; | |
| 873 | - end; | |
| 874 | - (* signed: sar 63 makes the sign into all ones or all zeros, and shr | |
| 875 | - 64 - k keeps k of them, the bias 2^k - 1 or 0; with it added, sar k | |
| 876 | - rounds towards zero. The remainder is r less the quotient times 2^k. *) | |
| 877 | - X.movRR(RCX, r); | |
| 878 | - X.shiftI(X.shSar, RCX, 63); | |
| 879 | - X.shiftI(X.shShr, RCX, 64 - k); | |
| 880 | - X.alu(X.aluAdd, RCX, r); | |
| 881 | - X.shiftI(X.shSar, RCX, k); | |
| 882 | - if sy = Scan.sDiv then X.movRR(r, RCX); | |
| 883 | - else X.shiftI(X.shShl, RCX, k); X.alu(X.aluSub, r, RCX); end; | |
| 884 | -end; | |
| 885 | - | |
| 886 | -(* Before rax div y, of a signed type of bits < 64: its smallest value | |
| 887 | - divided by -1 overflows it, which must stop the program as it does for | |
| 888 | - an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault, | |
| 889 | - so then rax is made the smallest I64, and it does. *) | |
| 890 | -proc divTrap(bits, y: INT); | |
| 891 | -var L: INT; | |
| 892 | -begin | |
| 893 | - L := NoLabel; | |
| 894 | - cmpI(y, -1); | |
| 895 | - X.jcc(X.ccNE, L); | |
| 896 | - cmpI(RAX, I64(-1) shl (bits - 1)); | |
| 897 | - X.jcc(X.ccNE, L); | |
| 898 | - X.shiftI(X.shShl, RAX, 64 - bits); | |
| 899 | - X.fixLink(L); | |
| 900 | -end; | |
| 901 | - | |
| 902 | -(* Whether the value on top is a word in memory that can be the right | |
| 903 | - operand of an instruction as it is: add r, [m] *) | |
| 904 | -proc topInMem: BOOL; | |
| 905 | -begin | |
| 906 | - return (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8); | |
| 907 | -end; | |
| 908 | - | |
| 909 | -(* The right operand of the two values on top, popped, in register y: | |
| 910 | - its own, or rcx for a constant; the left one stays, in its register, | |
| 911 | - which is returned with y. *) | |
| 912 | -proc operands: (INT, INT); | |
| 913 | -var v: I64; c: BOOL; y: INT; | |
| 914 | -begin | |
| 915 | - c, v := topConst(); | |
| 916 | - if c then X.movRI(RCX, v); y := RCX; else y := peek(depth - 1); end; | |
| 917 | - drop(); | |
| 918 | - return top(), y; | |
| 919 | -end; | |
| 920 | - | |
| 921 | -(* The two reals on top, the left op the right, worked out in the left's | |
| 922 | - xmm register. The right is taken from memory where it is, as a | |
| 923 | - constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. *) | |
| 924 | -proc realOp(sy: Scan.Sym; k: Kind); | |
| 925 | -var f: BOOL; c, x, y: INT; m: X.Mem; | |
| 926 | -begin | |
| 927 | - f := k = kF32; | |
| 928 | - match sy of | |
| 929 | - | sPlus: c := 58H; (* addsd, or addss for an F32 *) | |
| 930 | - | sMinus: c := 5CH; (* subsd, subss *) | |
| 931 | - | sTimes: c := 59H; (* mulsd, mulss *) | |
| 932 | - | else: c := 5EH; (* divsd, divss *) | |
| 933 | - end; | |
| 934 | - if stack[depth - 1].loc in {lConst, lMem, lFrame} then | |
| 935 | - match stack[depth - 1].loc of | |
| 936 | - | lConst: m := realConst(stack[depth - 1].val); | |
| 937 | - | lMem: m := stack[depth - 1].m; | |
| 938 | - | else: m := X.mem(RBP, stack[depth - 1].val); | |
| 939 | - end; | |
| 940 | - drop(); | |
| 941 | - X.sseM(c, top(), m, f); | |
| 942 | - else | |
| 943 | - y := peek(depth - 1); | |
| 944 | - drop(); | |
| 945 | - x := top(); | |
| 946 | - X.sse(c, x, y, f); | |
| 947 | - end; | |
| 948 | -end; | |
| 949 | - | |
| 950 | -(* The group 1 operation of +, -, &, | and xor *) | |
| 951 | -proc aluOf(sy: Scan.Sym): INT; | |
| 952 | -begin | |
| 953 | - match sy of | |
| 954 | - | sPlus: return X.aluAdd; | |
| 955 | - | sMinus: return X.aluSub; | |
| 956 | - | sAmp: return X.aluAnd; | |
| 957 | - | sBar: return X.aluOr; | |
| 958 | - | else: return X.aluXor; | |
| 959 | - end; | |
| 960 | -end; | |
| 961 | - | |
| 962 | -const | |
| 963 | - (* the condition codes of the comparisons, from sEql: = # < <= > >= *) | |
| 964 | - CcSigned: [6]INT = [X.ccE, X.ccNE, X.ccL, X.ccLE, X.ccG, X.ccGE]; | |
| 965 | - CcUnsigned: [6]INT = [X.ccE, X.ccNE, X.ccB, X.ccBE, X.ccA, X.ccAE]; | |
| 966 | - | |
| 967 | -(* The two values on top, the left op the right, an integer of size | |
| 968 | - bytes, signed or not, brought back to its width. *) | |
| 969 | -proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL); | |
| 970 | -var v, o: I64; r, y, sh, bits: INT; c: BOOL; m: X.Mem; | |
| 971 | -begin | |
| 972 | - if k in {kF32, kF64} then realOp(sy, k); return; end; | |
| 973 | - bits := size * 8; | |
| 974 | - (* a constant on the left of an operation whose order doesn't matter | |
| 975 | - goes right, where it can be an immediate or a shift *) | |
| 976 | - if (((sy = Scan.sPlus) or (sy = Scan.sTimes) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) and (stack[depth - 2].loc = lConst) | |
| 977 | - and (stack[depth - 1].loc # lConst) then swap(); end; | |
| 978 | - c, v := topConst(); | |
| 979 | - (* A word's offset (see Entry) is carried through + and -, and | |
| 980 | - through * by a constant, as (x + a) * k is x * k + a * k: the | |
| 981 | - operation is done on the registers, the offsets worked out here, | |
| 982 | - and put on the result. x + 1 is only noted. A value changed in | |
| 983 | - place, lInto, has no offset. *) | |
| 984 | - o := 0; | |
| 985 | - if (bits = 64) and (stack[depth - 2].loc # lInto) | |
| 986 | - and ((((sy = Scan.sPlus) or (sy = Scan.sMinus))) or ((sy = Scan.sTimes) and c and fits32(v))) then | |
| 987 | - o := takeOff(depth - 2); | |
| 988 | - if sy = Scan.sTimes then o := o * v; | |
| 989 | - elsif c and fits32(v) then | |
| 990 | - if sy = Scan.sPlus then o := o + v; else o := o - v; end; | |
| 991 | - drop(); | |
| 992 | - if not (stack[depth - 1].loc in {lReg, lHeld}) then _ := top(); end; | |
| 993 | - putOff(o); | |
| 994 | - return; | |
| 995 | - elsif sy = Scan.sPlus then o := o + takeOff(depth - 1); | |
| 996 | - else o := o - takeOff(depth - 1); end; | |
| 997 | - end; | |
| 998 | - sh := -1; | |
| 999 | - if c and (((sy = Scan.sTimes) or (sy = Scan.sDiv) or (sy = Scan.sMod))) then sh := exactLog2(v); end; | |
| 1000 | - if c and (sh >= 1) then | |
| 1001 | - drop(); | |
| 1002 | - r := top(); | |
| 1003 | - if sy = Scan.sTimes then X.shiftI(X.shShl, r, sh); else divPow2(sy, r, sh, not signed); end; | |
| 1004 | - elsif c and fits32(v) and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sBar) or (sy = Scan.sXor) or (sy = Scan.sAmp) or (sy = Scan.sTimes) or (sy = Scan.sShl) or (sy = Scan.sShr))) then | |
| 1005 | - drop(); | |
| 1006 | - r := top(); | |
| 1007 | - match sy of | |
| 1008 | - | sTimes: X.imulI(r, INT(v)); | |
| 1009 | - | sShl: X.shiftI(X.shShl, r, INT(v & I64(bits - 1))); | |
| 1010 | - | sShr: | |
| 1011 | - if signed then X.shiftI(X.shSar, r, INT(v & I64(bits - 1))); | |
| 1012 | - else X.shiftI(X.shShr, r, INT(v & I64(bits - 1))); end; | |
| 1013 | - | else: X.aluI(aluOf(sy), r, INT(v)); | |
| 1014 | - end; | |
| 1015 | - elsif topInMem() and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) then | |
| 1016 | - m := stack[depth - 1].m; | |
| 1017 | - drop(); | |
| 1018 | - r := top(); | |
| 1019 | - X.aluM(aluOf(sy), r, m); | |
| 1020 | - else | |
| 1021 | - r, y := operands(); | |
| 1022 | - if ((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor)) then | |
| 1023 | - X.alu(aluOf(sy), r, y); | |
| 1024 | - elsif sy = Scan.sTimes then | |
| 1025 | - X.imul(r, y); | |
| 1026 | - elsif ((sy = Scan.sDiv) or (sy = Scan.sMod)) then | |
| 1027 | - X.movRR(RAX, r); | |
| 1028 | - if not signed then | |
| 1029 | - X.movRI(RDX, 0); X.unary(6, y); (* div *) | |
| 1030 | - else | |
| 1031 | - (* only -1 can make the division overflow, so a constant | |
| 1032 | - divisor other than -1 needs no check *) | |
| 1033 | - if (bits < 64) and (not c or (v = -1)) then divTrap(bits, y); end; | |
| 1034 | - X.cqo(); X.unary(7, y); (* idiv, of rdx:rax, which cqo made from rax *) | |
| 1035 | - end; | |
| 1036 | - if sy = Scan.sDiv then X.movRR(r, RAX); else X.movRR(r, RDX); end; | |
| 1037 | - else (* sShl, sShr *) | |
| 1038 | - (* the count modulo the width: the machine takes it modulo 64 *) | |
| 1039 | - if y # RCX then X.movRR(RCX, y); end; | |
| 1040 | - if bits < 64 then X.aluI(X.aluAnd, RCX, bits - 1); end; | |
| 1041 | - if sy = Scan.sShl then X.shift(X.shShl, r); | |
| 1042 | - elsif signed then X.shift(X.shSar, r); | |
| 1043 | - else X.shift(X.shShr, r); end; | |
| 1044 | - end; | |
| 1045 | - end; | |
| 1046 | - if bits < 64 then X.extend(r, size, signed); end; | |
| 1047 | - stack[depth - 1].k := k; | |
| 1048 | - if o # 0 then putOff(o); end; | |
| 1049 | -end; | |
| 1050 | - | |
| 1051 | -(* ROL or ROR, by left, of the two values on top: an unsigned integer of | |
| 1052 | - size bytes, under the count (report section 6.1). x86 takes the count | |
| 1053 | - of rol and ror modulo 32, or 64 for a quadword, and a byte's or a | |
| 1054 | - word's rotation then goes round modulo 8 or 16 itself: as 8 and 16 | |
| 1055 | - divide 32, that is the count modulo the width, as the report has it, | |
| 1056 | - with no mask of the count's own, which a shift needs. A constant | |
| 1057 | - count is cut to the width here, an imm8; 0 rotates nothing. The bits | |
| 1058 | - above the width are zero and stay so: rol al and rol ax leave them, | |
| 1059 | - rol eax clears them. *) | |
| 1060 | -proc rotate*(left: BOOL; size: INT); | |
| 1061 | -var c: BOOL; v: I64; r, y, op: INT; | |
| 1062 | -begin | |
| 1063 | - if left then op := X.shRol; else op := X.shRor; end; | |
| 1064 | - c, v := topConst(); | |
| 1065 | - if c then | |
| 1066 | - drop(); | |
| 1067 | - r := top(); | |
| 1068 | - v := v & I64(size * 8 - 1); | |
| 1069 | - if v # 0 then X.rotate(op, r, size, INT(v)); end; | |
| 1070 | - else | |
| 1071 | - r, y := operands(); | |
| 1072 | - if y # RCX then X.movRR(RCX, y); end; | |
| 1073 | - X.rotate(op, r, size, -1); | |
| 1074 | - end; | |
| 1075 | - stack[depth - 1].k := kWord; | |
| 1076 | -end; | |
| 1077 | - | |
| 1078 | -(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top | |
| 1079 | - (report section 6.1). bsr finds the place h of its highest one bit, | |
| 1080 | - and the zeros above it are the width less 1 less h; bsf finds the | |
| 1081 | - place of its lowest, which is the zeros below it. Neither finds a | |
| 1082 | - place in 0, the one value with no one bit, and both say so by ZF: | |
| 1083 | - then the count is the whole width, as if h were -1, or the lowest | |
| 1084 | - one at the width. The bits above size bytes are zero, so a scan of | |
| 1085 | - all 64 sees the value's own. The baseline x86-64 lacks lzcnt and | |
| 1086 | - tzcnt, which would count 0 by themselves. For a U32: | |
| 1087 | - | |
| 1088 | - bsr r, r bsf r, r | |
| 1089 | - jnz L jnz L | |
| 1090 | - mov r, -1 mov r, 32 | |
| 1091 | - L: neg r L: | |
| 1092 | - add r, 31 *) | |
| 1093 | -proc countZeros*(leading: BOOL; size: INT); | |
| 1094 | -var r, w, L: INT; | |
| 1095 | -begin | |
| 1096 | - r := top(); | |
| 1097 | - w := size * 8; | |
| 1098 | - L := NoLabel; | |
| 1099 | - X.bitScan(leading, r); | |
| 1100 | - X.jcc(X.ccNE, L); | |
| 1101 | - if leading then X.movRI(r, -1); else X.movRI(r, w); end; | |
| 1102 | - X.fixLink(L); | |
| 1103 | - if leading then | |
| 1104 | - X.unary(3, r); (* neg *) | |
| 1105 | - X.aluI(X.aluAdd, r, w - 1); | |
| 1106 | - end; | |
| 1107 | - stack[depth - 1].k := kWord; | |
| 1108 | -end; | |
| 1109 | - | |
| 1110 | -(* POPCOUNT of the integer or set on top (report section 6.1), counted | |
| 1111 | - as lib/Bytes.raz counts bytes, without a loop: each two bits are | |
| 1112 | - made the sum of their ones, then each four, then each byte, and a | |
| 1113 | - multiplication by 0101...H adds the eight bytes' sums into the top | |
| 1114 | - byte. The baseline x86-64 lacks popcnt. rax and rdx are | |
| 1115 | - free between the stack machine's operations. | |
| 1116 | - | |
| 1117 | - mov rax, r; shr rax, 1; mov rdx, 5555...H; and rax, rdx; sub r, rax | |
| 1118 | - mov rax, r; shr rax, 2; mov rdx, 3333...H; and rax, rdx; and r, rdx; add r, rax | |
| 1119 | - mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx | |
| 1120 | - mov rdx, 0101...H; imul r, rdx; shr r, 56 *) | |
| 1121 | -proc popCount*; | |
| 1122 | -var r: INT; | |
| 1123 | -begin | |
| 1124 | - r := top(); | |
| 1125 | - (* each two bits: their value less its upper bit, which is its ones *) | |
| 1126 | - X.movRR(RAX, r); | |
| 1127 | - X.shiftI(X.shShr, RAX, 1); | |
| 1128 | - X.movRI(RDX, 5555555555555555H); | |
| 1129 | - X.alu(X.aluAnd, RAX, RDX); | |
| 1130 | - X.alu(X.aluSub, r, RAX); | |
| 1131 | - (* each four bits: the sums of its two pairs *) | |
| 1132 | - X.movRR(RAX, r); | |
| 1133 | - X.shiftI(X.shShr, RAX, 2); | |
| 1134 | - X.movRI(RDX, 3333333333333333H); | |
| 1135 | - X.alu(X.aluAnd, RAX, RDX); | |
| 1136 | - X.alu(X.aluAnd, r, RDX); | |
| 1137 | - X.alu(X.aluAdd, r, RAX); | |
| 1138 | - (* each byte: the sums of its two halves, at most 8, which fits four bits *) | |
| 1139 | - X.movRR(RAX, r); | |
| 1140 | - X.shiftI(X.shShr, RAX, 4); | |
| 1141 | - X.alu(X.aluAdd, r, RAX); | |
| 1142 | - X.movRI(RDX, 0F0F0F0F0F0F0F0FH); | |
| 1143 | - X.alu(X.aluAnd, r, RDX); | |
| 1144 | - (* the top byte of r times 0101...H is the sum of all eight *) | |
| 1145 | - X.movRI(RDX, 0101010101010101H); | |
| 1146 | - X.imul(r, RDX); | |
| 1147 | - X.shiftI(X.shShr, r, 56); | |
| 1148 | - stack[depth - 1].k := kWord; | |
| 1149 | -end; | |
| 1150 | - | |
| 1151 | -(* ABS of the real, or the signed integer of size bytes, on top (report | |
| 1152 | - section 6.1); an unsigned one needs no code. A real's sign bit is | |
| 1153 | - cleared, in rax, as neg flips it. An integer is negated, and its copy | |
| 1154 | - in rax taken back if the negation is negative (SF), as it is for a | |
| 1155 | - positive one. The smallest value negates to itself, negative, and so | |
| 1156 | - gives itself, as -x does, with no branch and no check. A narrow | |
| 1157 | - value is negated on all 64 bits, where its smallest is not the | |
| 1158 | - smallest, so it is cut back to its width after, as neg does: | |
| 1159 | - -128 in an I8 negates to 128, which is -128 again in 8 bits. | |
| 1160 | - | |
| 1161 | - movq rax, x; btr rax, 63; movq x, rax (F64; 31 for an F32) | |
| 1162 | - mov rax, r; neg r; cmovs r, rax (I64; then movsx r, r for a narrow one) *) | |
| 1163 | -proc abs*(k: Kind; size: INT); | |
| 1164 | -var r: INT; | |
| 1165 | -begin | |
| 1166 | - r := top(); | |
| 1167 | - if k in {kF32, kF64} then | |
| 1168 | - X.movqRX(RAX, r); | |
| 1169 | - if k = kF32 then X.btr(RAX, 31); else X.btr(RAX, 63); end; | |
| 1170 | - X.movqXR(r, RAX); | |
| 1171 | - else | |
| 1172 | - X.movRR(RAX, r); | |
| 1173 | - X.unary(3, r); (* neg *) | |
| 1174 | - X.cmov(X.ccS, r, RAX); | |
| 1175 | - if size < 8 then X.extend(r, size, true); end; | |
| 1176 | - end; | |
| 1177 | -end; | |
| 1178 | - | |
| 1179 | -(* The left op the right of the two values on top, as a BOOL *) | |
| 1180 | -proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); | |
| 1181 | -var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem; | |
| 1182 | -begin | |
| 1183 | - i := INT(sy) - INT(Scan.sEql); | |
| 1184 | - if k in {kF32, kF64} then | |
| 1185 | - f := k = kF32; | |
| 1186 | - y := peek(depth - 1); | |
| 1187 | - drop(); | |
| 1188 | - x := peek(depth - 1); | |
| 1189 | - drop(); | |
| 1190 | - r := target(); (* the BOOL, in the left's integer register *) | |
| 1191 | - (* ucomis sets the flags as an unsigned comparison would, but for an | |
| 1192 | - unordered pair (a NaN) it sets zero, parity and carry all. So only | |
| 1193 | - "above" can be tested straight: it is false for NaN, as every | |
| 1194 | - comparison but # must be. a < b is made b > a. = also asks for | |
| 1195 | - no parity, as NaN = NaN sets the zero flag, and # asks for | |
| 1196 | - parity as well. *) | |
| 1197 | - match sy of | |
| 1198 | - | sEql: X.ucomis(x, y, f); X.setCC(X.ccE, r); X.setCC(X.ccNP, RCX); X.alu(X.aluAnd, r, RCX); | |
| 1199 | - | sNeq: X.ucomis(x, y, f); X.setCC(X.ccNE, r); X.setCC(X.ccP, RCX); X.alu(X.aluOr, r, RCX); | |
| 1200 | - | sLss: X.ucomis(y, x, f); X.setCC(X.ccA, r); | |
| 1201 | - | sLeq: X.ucomis(y, x, f); X.setCC(X.ccAE, r); | |
| 1202 | - | sGtr: X.ucomis(x, y, f); X.setCC(X.ccA, r); | |
| 1203 | - | else: X.ucomis(x, y, f); X.setCC(X.ccAE, r); | |
| 1204 | - end; | |
| 1205 | - computed(kWord); | |
| 1206 | - else | |
| 1207 | - c, v := topConst(); | |
| 1208 | - if c then | |
| 1209 | - drop(); r := top(); cmpI(r, v); | |
| 1210 | - elsif topInMem() then | |
| 1211 | - m := stack[depth - 1].m; drop(); r := top(); X.aluM(X.aluCmp, r, m); | |
| 1212 | - else | |
| 1213 | - r, y := operands(); X.alu(X.aluCmp, r, y); | |
| 1214 | - end; | |
| 1215 | - if signed then X.setCC(CcSigned[i], r); else X.setCC(CcUnsigned[i], r); end; | |
| 1216 | - end; | |
| 1217 | - stack[depth - 1].k := kWord; | |
| 1218 | -end; | |
| 1219 | - | |
| 1220 | -(* A real is negated by flipping its sign bit, in rax *) | |
| 1221 | -proc neg*(k: Kind; size: INT; signed: BOOL); | |
| 1222 | -var r: INT; | |
| 1223 | -begin | |
| 1224 | - r := top(); | |
| 1225 | - if k in {kF32, kF64} then | |
| 1226 | - X.movqRX(RAX, r); | |
| 1227 | - if k = kF32 then X.btc(RAX, 31); else X.btc(RAX, 63); end; | |
| 1228 | - X.movqXR(r, RAX); | |
| 1229 | - else | |
| 1230 | - X.unary(3, r); (* neg *) | |
| 1231 | - if size < 8 then X.extend(r, size, signed); end; | |
| 1232 | - end; | |
| 1233 | -end; | |
| 1234 | - | |
| 1235 | -proc complement*(size: INT; signed: BOOL); | |
| 1236 | -var r: INT; | |
| 1237 | -begin | |
| 1238 | - r := top(); | |
| 1239 | - X.unary(2, r); (* not *) | |
| 1240 | - if size < 8 then X.extend(r, size, signed); end; | |
| 1241 | -end; | |
| 1242 | - | |
| 1243 | -proc notBool*; | |
| 1244 | -begin | |
| 1245 | - X.aluI(X.aluXor, top(), 1); | |
| 1246 | -end; | |
| 1247 | - | |
| 1248 | -(* The integer on top, brought to size bytes *) | |
| 1249 | -proc narrow*(size: INT; signed: BOOL); | |
| 1250 | -var r: INT; | |
| 1251 | -begin | |
| 1252 | - r := top(); | |
| 1253 | - if size < 8 then X.extend(r, size, signed); end; | |
| 1254 | - stack[depth - 1].k := kWord; | |
| 1255 | -end; | |
| 1256 | - | |
| 1257 | -(* The integer on top, as a real of kind k; big if it may be a U64 above | |
| 1258 | - high(I64), which the machine would take as negative *) | |
| 1259 | -proc intToReal*(k: Kind; big: BOOL); | |
| 1260 | -var L1, L2, r, x: INT; f: BOOL; | |
| 1261 | -begin | |
| 1262 | - f := k = kF32; | |
| 1263 | - r := top(); | |
| 1264 | - x := regIn(k, depth - 1); (* its xmm register *) | |
| 1265 | - if big then | |
| 1266 | - (* above high(I64): halve, keeping the low bit, and double *) | |
| 1267 | - L1 := NoLabel; L2 := NoLabel; | |
| 1268 | - X.testRR(r, r); | |
| 1269 | - X.jcc(X.ccS, L1); | |
| 1270 | - X.cvtsi2s(x, r, f); | |
| 1271 | - X.jmp(L2); | |
| 1272 | - X.fixLink(L1); | |
| 1273 | - X.movRR(RCX, r); | |
| 1274 | - X.shiftI(X.shShr, RCX, 1); | |
| 1275 | - X.aluI(X.aluAnd, r, 1); | |
| 1276 | - X.alu(X.aluOr, RCX, r); | |
| 1277 | - X.cvtsi2s(x, RCX, f); | |
| 1278 | - X.sse(58H, x, x, f); (* adds it to itself: doubles *) | |
| 1279 | - X.fixLink(L2); | |
| 1280 | - else | |
| 1281 | - X.cvtsi2s(x, r, f); | |
| 1282 | - end; | |
| 1283 | - stack[depth - 1].k := k; | |
| 1284 | -end; | |
| 1285 | - | |
| 1286 | -(* The real of kind k on top, truncated to an integer *) | |
| 1287 | -proc realToInt*(k: Kind); | |
| 1288 | -var x: INT; | |
| 1289 | -begin | |
| 1290 | - x := top(); | |
| 1291 | - X.cvtts2si(regIn(kWord, depth - 1), x, k = kF32); | |
| 1292 | - stack[depth - 1].k := kWord; | |
| 1293 | -end; | |
| 1294 | - | |
| 1295 | -(* The real of kind k on top, as the other kind *) | |
| 1296 | -proc realToReal*(k: Kind); | |
| 1297 | -var x: INT; | |
| 1298 | -begin | |
| 1299 | - x := top(); | |
| 1300 | - X.sse(5AH, x, x, k = kF32); (* cvtss2sd, or cvtsd2ss *) | |
| 1301 | - if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end; | |
| 1302 | -end; | |
| 1303 | - | |
| 1304 | -(* Whether the slices in the frame at a and b (the offsets of their | |
| 1305 | - pointers, the lengths 8 bytes on) are equal (or not, if not eq): the | |
| 1306 | - lengths, then the bytes, len shl shift of them, compared where the | |
| 1307 | - comparison stands. *) | |
| 1308 | -proc compareSeq*(a, b: I64; shift: INT; eq: BOOL); | |
| 1309 | -var differ, r: INT; | |
| 1310 | -begin | |
| 1311 | - clobberArgs(); | |
| 1312 | - r := target(); | |
| 1313 | - X.load(RCX, X.mem(RBP, b + 8), 8, false); | |
| 1314 | - X.aluM(X.aluCmp, RCX, X.mem(RBP, a + 8)); | |
| 1315 | - differ := NoLabel; | |
| 1316 | - X.jcc(X.ccNE, differ); | |
| 1317 | - X.load(RSI, X.mem(RBP, b), 8, false); | |
| 1318 | - X.load(RDI, X.mem(RBP, a), 8, false); | |
| 1319 | - if shift > 0 then X.shiftI(X.shShl, RCX, shift); end; | |
| 1320 | - (* repe cmpsb with rcx = 0 leaves the flags alone, so make them say | |
| 1321 | - equal first, which is right if there are no bytes *) | |
| 1322 | - X.alu(X.aluCmp, RCX, RCX); | |
| 1323 | - X.repeCmpsb(); | |
| 1324 | - X.fixLink(differ); | |
| 1325 | - if eq then X.setCC(X.ccE, r); else X.setCC(X.ccNE, r); end; | |
| 1326 | - computed(kWord); | |
| 1327 | -end; | |
| 1328 | - | |
| 1329 | -(* An error points at its message's length, an 8-byte word, with the | |
| 1330 | - text after it; nil is no error. Pops one, and makes the slice of its | |
| 1331 | - message at tmp, empty for nil. *) | |
| 1332 | -proc errorText*(tmp: I64); | |
| 1333 | -var L: INT; | |
| 1334 | -begin | |
| 1335 | - popTo(RAX); | |
| 1336 | - X.movRI(RCX, 0); | |
| 1337 | - L := NoLabel; | |
| 1338 | - X.testRR(RAX, RAX); | |
| 1339 | - X.jcc(X.ccE, L); | |
| 1340 | - X.load(RCX, X.mem(RAX, 0), 8, false); | |
| 1341 | - X.lea(RAX, X.mem(RAX, 8)); | |
| 1342 | - X.fixLink(L); | |
| 1343 | - X.store(X.mem(RBP, tmp), RAX, 8); | |
| 1344 | - X.store(X.mem(RBP, tmp + 8), RCX, 8); | |
| 1345 | -end; | |
| 1346 | - | |
| 1347 | -(* Pops a step and an integer variable's address, and adds the step to | |
| 1348 | - it, or takes it away. *) | |
| 1349 | -proc incDec*(size: INT; signed, dec: BOOL); | |
| 1350 | -var r, y, o: INT; v: I64; a: Adr; c: BOOL; | |
| 1351 | -begin | |
| 1352 | - c, v := topConst(); | |
| 1353 | - if dec then o := X.aluSub; else o := X.aluAdd; end; | |
| 1354 | - if c and fits32(v) then | |
| 1355 | - drop(); | |
| 1356 | - a.base := bStack; a.off := 0; | |
| 1357 | - X.aluMI(o, place(a, RCX), INT(v), size); | |
| 1358 | - return; | |
| 1359 | - end; | |
| 1360 | - r, y := operands(); | |
| 1361 | - drop(); | |
| 1362 | - X.load(RAX, X.mem(r, 0), size, signed); | |
| 1363 | - X.alu(o, RAX, y); | |
| 1364 | - X.store(X.mem(r, 0), RAX, size); | |
| 1365 | -end; | |
| 1366 | - | |
| 1367 | -(* INC or DEC of the variable in register r, by the value on top; a | |
| 1368 | - narrow one wraps at its width *) | |
| 1369 | -proc incDecReg*(r, size: INT; signed, dec: BOOL); | |
| 1370 | -var y, o: INT; v: I64; c: BOOL; | |
| 1371 | -begin | |
| 1372 | - if dec then o := X.aluSub; else o := X.aluAdd; end; | |
| 1373 | - c, v := topConst(); | |
| 1374 | - if c and fits32(v) then X.aluI(o, r, INT(v)); | |
| 1375 | - else y := top(); X.alu(o, r, y); end; | |
| 1376 | - drop(); | |
| 1377 | - if size < 8 then X.extend(r, size, signed); end; | |
| 1378 | -end; | |
| 1379 | - | |
| 1380 | -(* The pointer on top, which NEW got, to size zero bytes unless nil *) | |
| 1381 | -proc zeroNew*(size: I64); | |
| 1382 | -var L, r: INT; | |
| 1383 | -begin | |
| 1384 | - clobberArgs(); | |
| 1385 | - r := top(); | |
| 1386 | - L := NoLabel; | |
| 1387 | - X.testRR(r, r); | |
| 1388 | - X.jcc(X.ccE, L); | |
| 1389 | - X.movRR(RDI, r); | |
| 1390 | - X.movRI(RCX, size); | |
| 1391 | - X.movRI(RAX, 0); | |
| 1392 | - X.repStos(1); | |
| 1393 | - X.fixLink(L); | |
| 1394 | -end; | |
| 1395 | - | |
| 1396 | -(* CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the | |
| 1397 | - result where p was *) | |
| 1398 | -proc atomic*(which: INT); | |
| 1399 | -var r, n: INT; | |
| 1400 | -begin | |
| 1401 | - if which = Sym.bCas then readMems(depth - 3); else readMems(depth - 2); end; (* what it writes may be one *) | |
| 1402 | - if which = Sym.bCas then n := top(); drop(); end; | |
| 1403 | - popTo(RAX); (* cmpxchg compares with rax, so CAS's old value goes there *) | |
| 1404 | - r := top(); | |
| 1405 | - match which of | |
| 1406 | - | Sym.bCas: X.lockCmpxchg(X.mem(r, 0), n); X.setCC(X.ccE, r); | |
| 1407 | - | Sym.bXchg: X.xchg(X.mem(r, 0), RAX); X.movRR(r, RAX); | |
| 1408 | - | else: X.lockXadd(X.mem(r, 0), RAX); X.movRR(r, RAX); | |
| 1409 | - end; | |
| 1410 | - stack[depth - 1].k := kWord; | |
| 1411 | -end; | |
| 1412 | - | |
| 1413 | -(* The system call and its arguments, n values on the stack. Linux takes | |
| 1414 | - the number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9, | |
| 1415 | - and the syscall instruction clobbers rcx and r11. Every value in a | |
| 1416 | - register is spilled first, as r8 to r11 are the stack's registers, | |
| 1417 | - and the arguments are loaded from their slots. *) | |
| 1418 | -proc sysCall*(n: INT); | |
| 1419 | -const SysRegs: [7]INT = [RAX, RDI, RSI, RDX, R10, R8, R9]; | |
| 1420 | -var i: INT; | |
| 1421 | -begin | |
| 1422 | - clobberArgs(); | |
| 1423 | - spillBelow(depth); | |
| 1424 | - for i := 0 to n - 1 do valueTo(depth - n + i, SysRegs[i]); end; | |
| 1425 | - for i := 1 to n do drop(); end; | |
| 1426 | - X.syscall(); | |
| 1427 | - X.movRR(target(), RAX); | |
| 1428 | - computed(kWord); | |
| 1429 | -end; | |
| 1430 | - | |
| 1431 | -proc fence*; | |
| 1432 | -begin | |
| 1433 | - X.mfence(); | |
| 1434 | -end; | |
| 1435 | - | |
| 1436 | -(* Jumps. A label is the chain of the jumps to it, until it is placed. *) | |
| 1437 | - | |
| 1438 | -proc jump*(var L: INT); | |
| 1439 | -begin | |
| 1440 | - X.jmp(L); | |
| 1441 | -end; | |
| 1442 | - | |
| 1443 | -(* Pops a BOOL, and jumps if it is when *) | |
| 1444 | -proc jumpIf*(var L: INT; when: BOOL); | |
| 1445 | -var r: INT; | |
| 1446 | -begin | |
| 1447 | - r := peek(depth - 1); | |
| 1448 | - drop(); | |
| 1449 | - X.testRR(r, r); | |
| 1450 | - if when then X.jcc(X.ccNE, L); else X.jcc(X.ccE, L); end; | |
| 1451 | -end; | |
| 1452 | - | |
| 1453 | -(* Pops two integers, and jumps if the left op the right *) | |
| 1454 | -proc jumpCmp*(sy: Scan.Sym; signed: BOOL; var L: INT); | |
| 1455 | -var r, y, i: INT; v: I64; c: BOOL; m: X.Mem; | |
| 1456 | -begin | |
| 1457 | - i := INT(sy) - INT(Scan.sEql); | |
| 1458 | - (* a comparison changes neither: a variable's register will do *) | |
| 1459 | - c, v := topConst(); | |
| 1460 | - if c then | |
| 1461 | - drop(); r := peek(depth - 1); cmpI(r, v); | |
| 1462 | - elsif topInMem() then | |
| 1463 | - m := stack[depth - 1].m; drop(); r := peek(depth - 1); X.aluM(X.aluCmp, r, m); | |
| 1464 | - else | |
| 1465 | - y := peek(depth - 1); drop(); r := peek(depth - 1); X.alu(X.aluCmp, r, y); | |
| 1466 | - end; | |
| 1467 | - drop(); | |
| 1468 | - if signed then X.jcc(CcSigned[i], L); else X.jcc(CcUnsigned[i], L); end; | |
| 1469 | -end; | |
| 1470 | - | |
| 1471 | -(* jumpCmp out of an expression, past code that may spill: the values | |
| 1472 | - under the two compared are spilled first, so that they are where the | |
| 1473 | - code after the landing place expects them, whichever way it came. In | |
| 1474 | - v + (a < b and f(x) = 0), a call to f spills v on the way through, so | |
| 1475 | - v must be in its slot too where a < b jumps past the call. *) | |
| 1476 | -proc jumpOut*(sy: Scan.Sym; signed: BOOL; var L: INT); | |
| 1477 | -begin | |
| 1478 | - spillBelow(depth - 2); | |
| 1479 | - jumpCmp(sy, signed, L); | |
| 1480 | -end; | |
| 1481 | - | |
| 1482 | -(* The two values on top, compared, as a BOOL: true unless notOp, the | |
| 1483 | - opposite comparison, holds. fj and tj are the chains of jumps, made | |
| 1484 | - earlier by and, or and not, that are taken when the whole condition is | |
| 1485 | - false and when it is true; they land here, on the 0 and the 1. *) | |
| 1486 | -proc condValue*(notOp: Scan.Sym; signed: BOOL; fj, tj: INT); | |
| 1487 | -var r, e: INT; | |
| 1488 | -begin | |
| 1489 | - jumpOut(notOp, signed, fj); | |
| 1490 | - X.fixLink(tj); | |
| 1491 | - r := target(); | |
| 1492 | - X.movRI(r, 1); | |
| 1493 | - e := NoLabel; | |
| 1494 | - X.jmp(e); | |
| 1495 | - X.fixLink(fj); | |
| 1496 | - X.movRI(r, 0); | |
| 1497 | - X.fixLink(e); | |
| 1498 | - computed(kWord); | |
| 1499 | -end; | |
| 1500 | - | |
| 1501 | -(* jumpBack, fix, fixTo and joinJumps are Gen's names for the encoder's, | |
| 1502 | - so that a port can place jumps its own way. fix(L) places label L here; | |
| 1503 | - fixTo(L, top) makes every jump on chain L go to the earlier place top; | |
| 1504 | - joinJumps makes one chain of two. *) | |
| 1505 | -proc jumpBack*(top: INT); | |
| 1506 | -begin | |
| 1507 | - X.jmpBack(top); | |
| 1508 | -end; | |
| 1509 | - | |
| 1510 | -proc fix*(L: INT); | |
| 1511 | -begin | |
| 1512 | - X.fixLink(L); | |
| 1513 | -end; | |
| 1514 | - | |
| 1515 | -(* A jump table, for a match on the integer in frame slot slot, whose | |
| 1516 | - labels run from lo up for n values. The selector less lo, compared | |
| 1517 | - unsigned (so below lo is above n too), is out of the table if it | |
| 1518 | - isn't below n, and goes to the else arm at elseAt, or, if there is | |
| 1519 | - none, to endL, the end of the match. Else it indexes a table of | |
| 1520 | - 4-byte offsets, each from the start of the lea, that follows the | |
| 1521 | - jump, and the code jumps to its start plus the entry: | |
| 1522 | - | |
| 1523 | - mov rax, [slot] | |
| 1524 | - sub rax, lo | |
| 1525 | - cmp rax, n - 1 | |
| 1526 | - ja else | |
| 1527 | - lea rcx, [rip + start] start: this very lea | |
| 1528 | - movsxd rax, [rcx + rax*4 + 17] | |
| 1529 | - add rax, rcx | |
| 1530 | - jmp rax | |
| 1531 | - table: .long arm0 - start, arm1 - start, ... | |
| 1532 | - | |
| 1533 | - The four instructions after the compare are 7, 5, 3 and 2 bytes, so | |
| 1534 | - the table is 17 bytes on, which jumpTable checks. It returns start; | |
| 1535 | - the caller then makes the n entries with tableEntry. *) | |
| 1536 | -proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT; | |
| 1537 | -const TableAt = 17; | |
| 1538 | -var start: INT; | |
| 1539 | -begin | |
| 1540 | - X.load(RAX, X.mem(RBP, slot), 8, false); | |
| 1541 | - if lo # 0 then X.aluI(X.aluSub, RAX, INT(lo)); end; | |
| 1542 | - X.aluI(X.aluCmp, RAX, n - 1); | |
| 1543 | - if elseAt # NoLabel then X.jccBack(X.ccA, elseAt); else X.jcc(X.ccA, endL); end; | |
| 1544 | - start := Chunk.pc; | |
| 1545 | - X.lea(RCX, X.here()); | |
| 1546 | - X.load(RAX, X.memIdx(RCX, RAX, 4, TableAt), 4, true); | |
| 1547 | - X.alu(X.aluAdd, RAX, RCX); | |
| 1548 | - X.jmpR(RAX); | |
| 1549 | - if Chunk.pc # start + TableAt then Diag.fatal("internal error: the jump table is not where it was said"); end; | |
| 1550 | - return start; | |
| 1551 | -end; | |
| 1552 | - | |
| 1553 | -(* The next entry of the table that jumpTable started at table: the | |
| 1554 | - code at target *) | |
| 1555 | -proc tableEntry*(table, target: INT); | |
| 1556 | -begin | |
| 1557 | - Chunk.put4(target - table); | |
| 1558 | -end; | |
| 1559 | - | |
| 1560 | -proc alignLoop*; | |
| 1561 | -begin | |
| 1562 | - X.align(32); | |
| 1563 | -end; | |
| 1564 | - | |
| 1565 | -proc joinJumps*(L1, L2: INT): INT; | |
| 1566 | -begin | |
| 1567 | - return X.joinLinks(L1, L2); | |
| 1568 | -end; | |
| 1569 | - | |
| 1570 | -proc fixTo*(L, top: INT); | |
| 1571 | -begin | |
| 1572 | - X.fixLinkTo(L, top); | |
| 1573 | -end; | |
| 1574 | - | |
| 1575 | -(* Checks *) | |
| 1576 | - | |
| 1577 | -const | |
| 1578 | - NoBase = -2; (* a Mem that is no memory *) | |
| 1579 | - ARegister = -3; (* a Mem that is no memory but a register, its index *) | |
| 1580 | - | |
| 1581 | -(* The name s, interned *) | |
| 1582 | -proc internStr(s: []const U8): INT; | |
| 1583 | -begin | |
| 1584 | - return Scan.intern(SYSTEM.PTR(s), LEN(s)); | |
| 1585 | -end; | |
| 1586 | - | |
| 1587 | -(* The program goes on if the condition cc holds, and otherwise stops | |
| 1588 | - in Rt.fail(what, a, b, len, at, name), which says what failed at p, | |
| 1589 | - found by at, which a stub works out from where it was called; what is | |
| 1590 | - an index a (0), a slice a..b (1), each for a length len, a position a | |
| 1591 | - of the enumeration called name (2), a slice of a pointer a..b that | |
| 1592 | - runs backwards (3), a nil pointer (4), or a view at index a that runs | |
| 1593 | - past a length len (7). Rt.fail knows 5 and 6, a | |
| 1594 | - division fault and a stack overflow, which this machine catches | |
| 1595 | - itself, so no check here has them. The arguments go in rdi, rsi, rdx, | |
| 1596 | - rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m, | |
| 1597 | - or n if m.base is NoBase, or in register m.index if it is ARegister. | |
| 1598 | - rdi and rsi are passed on as they were, since a parameter may be kept | |
| 1599 | - in them, for the trap report to show: so a goes to the stub in rax. | |
| 1600 | - The failing code is out of the way, at the | |
| 1601 | - procedure's end (endFails), so that a check that holds costs a jump | |
| 1602 | - not taken; it has p for its place (its line and column, worked out | |
| 1603 | - now, while the source read is p's), never returns, and may use any | |
| 1604 | - register once it has read what it needs. *) | |
| 1605 | -proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT); | |
| 1606 | -begin | |
| 1607 | - if numFails > LEN(fails) - 1 then | |
| 1608 | - _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))])); | |
| 1609 | - return; | |
| 1610 | - end; | |
| 1611 | - fails[numFails].L := NoLabel; | |
| 1612 | - X.jcc(cc xor 1, fails[numFails].L); (* x86's conditions come in pairs, the other one its last bit flipped *) | |
| 1613 | - fails[numFails].what := what; | |
| 1614 | - fails[numFails].ra := ra; | |
| 1615 | - fails[numFails].m := m; | |
| 1616 | - fails[numFails].n := n; | |
| 1617 | - fails[numFails].name := name; | |
| 1618 | - fails[numFails].path := Chunk.curFilePath(); | |
| 1619 | - fails[numFails].line, fails[numFails].col := Scan.lineCol(p.a); | |
| 1620 | - INC(numFails); | |
| 1621 | -end; | |
| 1622 | - | |
| 1623 | -(* The failures of the procedure's checks: each gives Rt.fail its values | |
| 1624 | - and the place of the offending expression, through a stub for its | |
| 1625 | - kind, which takes the place from where it was called *) | |
| 1626 | -proc endFails; | |
| 1627 | -var i, path: INT; f: Fail; | |
| 1628 | -begin | |
| 1629 | - path := Chunk.curFilePath(); | |
| 1630 | - for i := 0 to numFails - 1 do | |
| 1631 | - f := fails[i]; | |
| 1632 | - X.fixLink(f.L); | |
| 1633 | - Chunk.setCurFile(f.path); | |
| 1634 | - Chunk.noteLine(f.line, f.col); | |
| 1635 | - Chunk.setCurFile(path); | |
| 1636 | - if f.ra # RAX then X.movRR(RAX, f.ra); end; | |
| 1637 | - if f.m.base = NoBase then X.movRI(RCX, f.n); | |
| 1638 | - elsif f.m.base = ARegister then X.movRR(RCX, f.m.index); | |
| 1639 | - else X.load(RCX, f.m, 8, false); end; | |
| 1640 | - if f.name >= 0 then X.lea(R9, X.memRip(f.name, 0)); end; | |
| 1641 | - if failStubs[f.what] < 0 then | |
| 1642 | - failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what])); | |
| 1643 | - end; | |
| 1644 | - X.callChunk(failStubs[f.what]); | |
| 1645 | - end; | |
| 1646 | - numFails := 0; | |
| 1647 | -end; | |
| 1648 | - | |
| 1649 | -(* For check: a length known here, not in memory *) | |
| 1650 | -proc noMem: X.Mem; | |
| 1651 | -begin | |
| 1652 | - return X.mem(NoBase, 0); | |
| 1653 | -end; | |
| 1654 | - | |
| 1655 | -(* For check: a length in register r, a slice's in registers *) | |
| 1656 | -proc inRegister(r: INT): X.Mem; | |
| 1657 | -var m: X.Mem; | |
| 1658 | -begin | |
| 1659 | - m := X.mem(ARegister, 0); | |
| 1660 | - m.index := r; | |
| 1661 | - return m; | |
| 1662 | -end; | |
| 1663 | - | |
| 1664 | -(* The index on top is below n, unsigned *) | |
| 1665 | -proc checkIndex*(n: I64; p: Diag.Pos); | |
| 1666 | -var r: INT; | |
| 1667 | -begin | |
| 1668 | - r := peek(depth - 1); | |
| 1669 | - cmpI(r, n); | |
| 1670 | - check(X.ccB, 0, p, r, noMem(), n, -1); | |
| 1671 | -end; | |
| 1672 | - | |
| 1673 | -(* The index on top is the element a view starts at, in an array of n | |
| 1674 | - elements: the view needs k elements from there, so the index must be | |
| 1675 | - below n - k + 1, which is below. A negative index is a huge one, | |
| 1676 | - unsigned, so one comparison does (report section 6.9). *) | |
| 1677 | -proc checkView*(n, below: I64; p: Diag.Pos); | |
| 1678 | -var r: INT; | |
| 1679 | -begin | |
| 1680 | - r := peek(depth - 1); | |
| 1681 | - cmpI(r, below); | |
| 1682 | - check(X.ccB, 7, p, r, noMem(), n, -1); | |
| 1683 | -end; | |
| 1684 | - | |
| 1685 | -(* What a failed index check is: an index past the length (0), or a | |
| 1686 | - view that runs past it (7), when the view needs room elements more | |
| 1687 | - than the one indexed. *) | |
| 1688 | -proc kindOfIndex(room: I64): INT; | |
| 1689 | -begin | |
| 1690 | - if room > 0 then return 7; end; | |
| 1691 | - return 0; | |
| 1692 | -end; | |
| 1693 | - | |
| 1694 | -(* A view of a slice's element, at the index in r, needs room elements | |
| 1695 | - after it: rdx holds the slice's length, which r is below already, | |
| 1696 | - so length - r, at least 1, can't wrap, and must be more than room. | |
| 1697 | - The length is at m for the message. *) | |
| 1698 | -proc checkRoom(r: INT; room: I64; p: Diag.Pos; m: X.Mem); | |
| 1699 | -begin | |
| 1700 | - X.alu(X.aluSub, RDX, r); | |
| 1701 | - X.aluI(X.aluCmp, RDX, INT(room)); | |
| 1702 | - check(X.ccA, 7, p, r, m, 0, -1); | |
| 1703 | -end; | |
| 1704 | - | |
| 1705 | -(* The integer on top is a position of an enumeration of n names *) | |
| 1706 | -proc checkEnum*(n: I64; name: INT; p: Diag.Pos); | |
| 1707 | -var r: INT; | |
| 1708 | -begin | |
| 1709 | - r := top(); | |
| 1710 | - cmpI(r, n); | |
| 1711 | - check(X.ccB, 2, p, r, noMem(), n, name); | |
| 1712 | -end; | |
| 1713 | - | |
| 1714 | -(* The address on top, from a pointer, which is to be used reach bytes | |
| 1715 | - past: the program stops if it is nil, unless the machine would stop | |
| 1716 | - it, as for a reach within NilPage. *) | |
| 1717 | -proc checkNil*(reach: I64; p: Diag.Pos); | |
| 1718 | -var r: INT; | |
| 1719 | -begin | |
| 1720 | - if reach <= NilPage then return; end; | |
| 1721 | - r := top(); | |
| 1722 | - X.testRR(r, r); | |
| 1723 | - check(X.ccNE, 4, p, r, noMem(), 0, -1); | |
| 1724 | -end; | |
| 1725 | - | |
| 1726 | -(* The displacement disp of an element of size bytes, with the offset of | |
| 1727 | - the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8 | |
| 1728 | - - 8] *) | |
| 1729 | -proc indexOff(size, disp: I64): I64; | |
| 1730 | -begin | |
| 1731 | - if fits32(disp + stack[depth - 1].off * size) then return disp + takeOff(depth - 1) * size; end; | |
| 1732 | - return disp; | |
| 1733 | -end; | |
| 1734 | - | |
| 1735 | -(* Pops an index, and the address of the slice at a if it is on the | |
| 1736 | - stack, and pushes the address of its element of size bytes: the | |
| 1737 | - index, checked against the length, times size, plus the pointer. | |
| 1738 | - It is worked out in the index's own register, which then holds the | |
| 1739 | - address; but if the slice's address is on the stack, under the | |
| 1740 | - index, the element's register is the one that holds that, still | |
| 1741 | - needed, so the work is done in rax. A slice in registers is | |
| 1742 | - compared with and added to them; for an element of 1, 2, 4 or 8 | |
| 1743 | - bytes, its address is only noted, as the operand [pointer + index * | |
| 1744 | - size], to be read or written where it is used (lIdx). *) | |
| 1745 | -proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos); | |
| 1746 | -var m, ml: X.Mem; r: INT; inRax: BOOL; o: I64; | |
| 1747 | -begin | |
| 1748 | - if (a.base = bReg) and (((size = 1) or (size = 2) or (size = 4) or (size = 8))) then | |
| 1749 | - o := 0; | |
| 1750 | - if not checked then o := indexOff(size, 0); end; | |
| 1751 | - r := peek(depth - 1); | |
| 1752 | - if checked then | |
| 1753 | - X.alu(X.aluCmp, r, a.hi); | |
| 1754 | - check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1755 | - if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1756 | - end; | |
| 1757 | - drop(); | |
| 1758 | - newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); | |
| 1759 | - return; | |
| 1760 | - end; | |
| 1761 | - inRax := a.base = bStack; | |
| 1762 | - if inRax then r := RAX; popTo(RAX); | |
| 1763 | - else r := top(); drop(); end; | |
| 1764 | - if a.base = bReg then | |
| 1765 | - if checked then | |
| 1766 | - X.alu(X.aluCmp, r, a.hi); | |
| 1767 | - check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1768 | - if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1769 | - end; | |
| 1770 | - mulI(r, size); | |
| 1771 | - X.alu(X.aluAdd, r, a.chunk); | |
| 1772 | - else | |
| 1773 | - m := place(a, RCX); | |
| 1774 | - ml := m; ml.disp := ml.disp + 8; | |
| 1775 | - if checked then | |
| 1776 | - X.aluM(X.aluCmp, r, ml); | |
| 1777 | - check(X.ccB, kindOfIndex(room), p, r, ml, 0, -1); | |
| 1778 | - if room > 0 then X.load(RDX, ml, 8, false); checkRoom(r, room, p, ml); end; | |
| 1779 | - end; | |
| 1780 | - mulI(r, size); | |
| 1781 | - X.aluM(X.aluAdd, r, m); | |
| 1782 | - end; | |
| 1783 | - if inRax then X.movRR(target(), RAX); | |
| 1784 | - else _ := claim(depth, kWord); end; (* the index's register again, now for the address *) | |
| 1785 | - computed(kWord); | |
| 1786 | -end; | |
| 1787 | - | |
| 1788 | -(* Whether r is one of the stack's registers, not a variable's *) | |
| 1789 | -proc isStackReg(r: INT): BOOL; | |
| 1790 | -var i: INT; | |
| 1791 | -begin | |
| 1792 | - for i := 0 to NumRegs - 1 do | |
| 1793 | - if Regs[i] = r then return true; end; | |
| 1794 | - end; | |
| 1795 | - return false; | |
| 1796 | -end; | |
| 1797 | - | |
| 1798 | -(* Pops an index and the pointer under it, and pushes the address of | |
| 1799 | - the element of size bytes: p + i * size. For an element of 1, 2, 4 | |
| 1800 | - or 8 bytes it is only noted, as the operand [p + i * size] (lIdx), to | |
| 1801 | - be read or written where it is used, when at most one of the two is | |
| 1802 | - in a stack register, the register the new value owns. Otherwise lea | |
| 1803 | - adds them up, in one instruction in place of a multiplication and an | |
| 1804 | - addition. *) | |
| 1805 | -proc indexPtr*(size: I64); | |
| 1806 | -var b, x, own: INT; o: I64; | |
| 1807 | -begin | |
| 1808 | - o := 0; | |
| 1809 | - if fits32(stack[depth - 2].off + stack[depth - 1].off * size) then | |
| 1810 | - o := takeOff(depth - 2) + takeOff(depth - 1) * size; | |
| 1811 | - end; | |
| 1812 | - b := peek(depth - 2); | |
| 1813 | - x := peek(depth - 1); | |
| 1814 | - own := Regs[(depth - 2) mod NumRegs]; | |
| 1815 | - drop(); | |
| 1816 | - drop(); | |
| 1817 | - if (not isStackReg(b) or (b = own)) and (not isStackReg(x) or (x = own)) then | |
| 1818 | - newMem(lIdx, X.memIdx(b, x, INT(size), o), kWord, 8, false); | |
| 1819 | - if b = own then owner[(depth - 1) mod NumRegs] := depth - 1; end; | |
| 1820 | - else | |
| 1821 | - X.lea(target(), X.memIdx(b, x, INT(size), o)); | |
| 1822 | - computed(kWord); | |
| 1823 | - end; | |
| 1824 | -end; | |
| 1825 | - | |
| 1826 | -(* Pops an index, checked, and makes the address of the element it | |
| 1827 | - picks of the array at a. An element of 1, 2, | |
| 1828 | - 4 or 8 bytes is only noted, as the operand [base + index * size] | |
| 1829 | - (lIdx): an array in the frame has rbp as its base, one reached | |
| 1830 | - through a pointer in a register (a var or const parameter) that | |
| 1831 | - register, and one in a chunk its address as the displacement | |
| 1832 | - (X.memAbs), with no lea. Any other is added up. *) | |
| 1833 | -proc indexArray*(a: Adr; size: I64); | |
| 1834 | -var r: INT; o: I64; | |
| 1835 | -begin | |
| 1836 | - if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base in {bFrame, bGlobal, bPtr}) then | |
| 1837 | - o := indexOff(size, a.off); | |
| 1838 | - r := peek(depth - 1); | |
| 1839 | - drop(); | |
| 1840 | - if a.base = bFrame then | |
| 1841 | - newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); | |
| 1842 | - elsif a.base = bPtr then | |
| 1843 | - newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); | |
| 1844 | - else | |
| 1845 | - newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false); | |
| 1846 | - end; | |
| 1847 | - else | |
| 1848 | - mulI(top(), size); | |
| 1849 | - pushAdr(a); | |
| 1850 | - op(Scan.sPlus, kWord, 8, false); | |
| 1851 | - end; | |
| 1852 | -end; | |
| 1853 | - | |
| 1854 | -(* Pops lo and hi, and the address of the array or slice at a if it is | |
| 1855 | - on the stack, and makes the slice lo..hi of it, with elements of size | |
| 1856 | - bytes, in the frame at tmp; the array has n elements. *) | |
| 1857 | -proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos); | |
| 1858 | -var m, ml: X.Mem; | |
| 1859 | -begin | |
| 1860 | - popTo(RDX); | |
| 1861 | - popTo(RCX); | |
| 1862 | - if a.base = bSlot then clobberArgs(); end; (* place loads its slot into rdi *) | |
| 1863 | - if a.base = bReg then | |
| 1864 | - ml := inRegister(a.hi); (* a slice in registers *) | |
| 1865 | - else | |
| 1866 | - m := place(a, RDI); | |
| 1867 | - ml := m; ml.disp := ml.disp + 8; | |
| 1868 | - end; | |
| 1869 | - if checked then | |
| 1870 | - (* two checks, both unsigned, so that a negative bound fails too: | |
| 1871 | - hi against the length, then lo against hi *) | |
| 1872 | - if isArray then cmpI(RDX, n); ml := noMem(); | |
| 1873 | - elsif a.base = bReg then X.alu(X.aluCmp, RDX, a.hi); | |
| 1874 | - else X.aluM(X.aluCmp, RDX, ml); end; | |
| 1875 | - check(X.ccBE, 1, p, RCX, ml, n, -1); | |
| 1876 | - X.alu(X.aluCmp, RCX, RDX); | |
| 1877 | - check(X.ccBE, 1, p, RCX, ml, n, -1); | |
| 1878 | - end; | |
| 1879 | - X.movRR(RAX, RDX); | |
| 1880 | - X.alu(X.aluSub, RAX, RCX); | |
| 1881 | - X.store(X.mem(RBP, tmp + 8), RAX, 8); | |
| 1882 | - mulI(RCX, size); | |
| 1883 | - if isArray then X.lea(RAX, m); | |
| 1884 | - elsif a.base = bReg then X.movRR(RAX, a.chunk); | |
| 1885 | - else X.load(RAX, m, 8, false); end; | |
| 1886 | - X.alu(X.aluAdd, RAX, RCX); | |
| 1887 | - X.store(X.mem(RBP, tmp), RAX, 8); | |
| 1888 | -end; | |
| 1889 | - | |
| 1890 | -(* The rcx bytes below rsi to those below rdi, n at a time while rcx | |
| 1891 | - holds n or more, through rax, moving rsi, rdi and rcx down: COPY's | |
| 1892 | - backward copy, as a loop, since rep movsb going down (std) moves a | |
| 1893 | - byte at a time. *) | |
| 1894 | -proc copyDown(n: INT); | |
| 1895 | -var top, out: INT; | |
| 1896 | -begin | |
| 1897 | - top := Chunk.pc; | |
| 1898 | - X.aluI(X.aluCmp, RCX, n); | |
| 1899 | - out := NoLabel; | |
| 1900 | - X.jcc(X.ccB, out); | |
| 1901 | - X.aluI(X.aluSub, RSI, n); | |
| 1902 | - X.aluI(X.aluSub, RDI, n); | |
| 1903 | - X.aluI(X.aluSub, RCX, n); | |
| 1904 | - X.load(RAX, X.mem(RSI, 0), n, false); | |
| 1905 | - X.store(X.mem(RDI, 0), RAX, n); | |
| 1906 | - X.jmpBack(top); | |
| 1907 | - X.fixLink(out); | |
| 1908 | -end; | |
| 1909 | - | |
| 1910 | -(* COPY: the elements of the slice in the frame at sOff, of size bytes | |
| 1911 | - each, to the start of the one at dOff, which must be at least as long | |
| 1912 | - (the source may be shorter), as one block. It goes forwards, by rep | |
| 1913 | - movsb, but backwards when the destination starts inside the source, | |
| 1914 | - so that an overlap comes out right: by words from the ends, then the | |
| 1915 | - last few bytes one by one. *) | |
| 1916 | -proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos); | |
| 1917 | -var ml: X.Mem; L, E: INT; | |
| 1918 | -begin | |
| 1919 | - clobberArgs(); | |
| 1920 | - X.load(RDX, X.mem(RBP, sOff + 8), 8, false); | |
| 1921 | - if checked then | |
| 1922 | - ml := X.mem(RBP, dOff + 8); | |
| 1923 | - X.movRI(RAX, 0); | |
| 1924 | - X.aluM(X.aluCmp, RDX, ml); | |
| 1925 | - check(X.ccBE, 1, p, RAX, ml, 0, -1); | |
| 1926 | - end; | |
| 1927 | - X.movRR(RCX, RDX); | |
| 1928 | - mulI(RCX, size); | |
| 1929 | - X.load(RSI, X.mem(RBP, sOff), 8, false); | |
| 1930 | - X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1931 | - L := NoLabel; | |
| 1932 | - X.alu(X.aluCmp, RDI, RSI); | |
| 1933 | - X.jcc(X.ccBE, L); (* the destination is not above: forwards, at L *) | |
| 1934 | - X.lea(RAX, X.memIdx(RSI, RCX, 1, 0)); | |
| 1935 | - X.alu(X.aluCmp, RDI, RAX); | |
| 1936 | - X.jcc(X.ccAE, L); (* nor does it start inside the source *) | |
| 1937 | - X.alu(X.aluAdd, RSI, RCX); (* backwards, from the ends *) | |
| 1938 | - X.alu(X.aluAdd, RDI, RCX); | |
| 1939 | - copyDown(8); | |
| 1940 | - copyDown(1); | |
| 1941 | - E := NoLabel; | |
| 1942 | - X.jmp(E); | |
| 1943 | - X.fixLink(L); | |
| 1944 | - X.repMovsb(); | |
| 1945 | - X.fixLink(E); | |
| 1946 | -end; | |
| 1947 | - | |
| 1948 | -(* The most elements FILL stores one by one: as for MaxMoves, a store | |
| 1949 | - each costs less than rep stos takes to start, which at 10 elements | |
| 1950 | - is twice the time. *) | |
| 1951 | -const FewFills = 16; | |
| 1952 | - | |
| 1953 | -(* rax, an element of size bytes, 1, 2, 4 or 8, into each of the | |
| 1954 | - elements of the slice in the frame at dOff, of which there are n if | |
| 1955 | - n isn't -1. Few, as of a short array, are a store each; else, at run | |
| 1956 | - time, a loop of stores for fewer than FewFills, or rep stos of the | |
| 1957 | - element's width: | |
| 1958 | - | |
| 1959 | - mov rdi, [rbp + dOff] | |
| 1960 | - mov rcx, [rbp + dOff + 8] | |
| 1961 | - cmp rcx, 16 | |
| 1962 | - jae many | |
| 1963 | - test rcx, rcx | |
| 1964 | - jz out | |
| 1965 | - top: mov [rdi], eax ; for a U32 | |
| 1966 | - add rdi, 4 | |
| 1967 | - sub rcx, 1 | |
| 1968 | - jne top | |
| 1969 | - jmp out | |
| 1970 | - many: rep stosd | |
| 1971 | - out: *) | |
| 1972 | -proc fillScalar(dOff, size, n: I64); | |
| 1973 | -var i: I64; many, out, top: INT; | |
| 1974 | -begin | |
| 1975 | - X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1976 | - if (n >= 0) and (n <= FewFills) then | |
| 1977 | - for i := 0 to n - 1 do X.store(X.mem(RDI, i * size), RAX, INT(size)); end; | |
| 1978 | - return; | |
| 1979 | - end; | |
| 1980 | - X.load(RCX, X.mem(RBP, dOff + 8), 8, false); | |
| 1981 | - many := NoLabel; | |
| 1982 | - out := NoLabel; | |
| 1983 | - X.aluI(X.aluCmp, RCX, FewFills); | |
| 1984 | - X.jcc(X.ccAE, many); | |
| 1985 | - X.testRR(RCX, RCX); | |
| 1986 | - X.jcc(X.ccE, out); | |
| 1987 | - top := Chunk.pc; | |
| 1988 | - X.store(X.mem(RDI, 0), RAX, INT(size)); | |
| 1989 | - X.aluI(X.aluAdd, RDI, INT(size)); | |
| 1990 | - X.aluI(X.aluSub, RCX, 1); | |
| 1991 | - X.jccBack(X.ccNE, top); | |
| 1992 | - X.jmp(out); | |
| 1993 | - X.fixLink(many); | |
| 1994 | - X.repStos(INT(size)); | |
| 1995 | - X.fixLink(out); | |
| 1996 | -end; | |
| 1997 | - | |
| 1998 | -(* The size bytes at rsi into each element of the slice in the frame at | |
| 1999 | - dOff: into the first by copyBytes, and from there forward by rep | |
| 2000 | - movsb, from d[0] on to d[1] on. A forward copy reads each byte after it has written it, size | |
| 2001 | - bytes before, so the first element repeats through the rest: | |
| 2002 | - | |
| 2003 | - mov rdi, [rbp + dOff] | |
| 2004 | - mov rdx, [rbp + dOff + 8] | |
| 2005 | - test rdx, rdx | |
| 2006 | - jz out ; nothing to fill | |
| 2007 | - ... ; copyBytes(size): d[0] := x | |
| 2008 | - mov rsi, [rbp + dOff] | |
| 2009 | - lea rdi, [rsi + size] | |
| 2010 | - lea rcx, [rdx - 1] | |
| 2011 | - imul rcx, size ; the bytes of d[1..n] | |
| 2012 | - rep movsb | |
| 2013 | - out: *) | |
| 2014 | -proc fillCopy(dOff, size: I64); | |
| 2015 | -var out: INT; | |
| 2016 | -begin | |
| 2017 | - X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 2018 | - X.load(RDX, X.mem(RBP, dOff + 8), 8, false); | |
| 2019 | - X.testRR(RDX, RDX); | |
| 2020 | - out := NoLabel; | |
| 2021 | - X.jcc(X.ccE, out); | |
| 2022 | - copyBytes(size); | |
| 2023 | - X.load(RSI, X.mem(RBP, dOff), 8, false); | |
| 2024 | - X.lea(RDI, X.mem(RSI, size)); | |
| 2025 | - X.lea(RCX, X.mem(RDX, -1)); | |
| 2026 | - mulI(RCX, size); | |
| 2027 | - X.repMovsb(); | |
| 2028 | - X.fixLink(out); | |
| 2029 | -end; | |
| 2030 | - | |
| 2031 | -(* FILL: every element of the slice in the frame at dOff, of size bytes | |
| 2032 | - each, set to the value on top of the stack if the element is a | |
| 2033 | - scalar, or else to the size bytes at the address on top. n is how | |
| 2034 | - many elements the slice has if that is known, as for an array, else | |
| 2035 | - -1. A scalar goes into rax and is stored as it is, a real's bits as | |
| 2036 | - an integer's (fillScalar); but a constant zero into an array of n | |
| 2037 | - elements is n * size zero bytes, as NEW's zeroing is: by moves, or | |
| 2038 | - rep stosb (zeroBytes). Any other element is copied (fillCopy). *) | |
| 2039 | -proc fillSlice*(dOff, size, n: I64; scalar: BOOL); | |
| 2040 | -var isConst: BOOL; v: I64; | |
| 2041 | -begin | |
| 2042 | - clobberArgs(); | |
| 2043 | - isConst, v := topConst(); | |
| 2044 | - if scalar and isConst and (v = 0) and (n >= 0) then | |
| 2045 | - drop(); | |
| 2046 | - X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 2047 | - zeroBytes(n * size); | |
| 2048 | - elsif scalar then | |
| 2049 | - popTo(RAX); | |
| 2050 | - fillScalar(dOff, size, n); | |
| 2051 | - else | |
| 2052 | - popTo(RSI); | |
| 2053 | - fillCopy(dOff, size); | |
| 2054 | - end; | |
| 2055 | -end; | |
| 2056 | - | |
| 2057 | -(* Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its | |
| 2058 | - bounds can't be known, but it can't run backwards. *) | |
| 2059 | -proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); | |
| 2060 | -begin | |
| 2061 | - clobberArgs(); | |
| 2062 | - popTo(RDX); | |
| 2063 | - popTo(RCX); | |
| 2064 | - popTo(RDI); | |
| 2065 | - if checked then | |
| 2066 | - X.alu(X.aluCmp, RCX, RDX); | |
| 2067 | - check(X.ccLE, 3, p, RCX, noMem(), 0, -1); | |
| 2068 | - end; | |
| 2069 | - X.movRR(RAX, RDX); | |
| 2070 | - X.alu(X.aluSub, RAX, RCX); | |
| 2071 | - X.store(X.mem(RBP, tmp + 8), RAX, 8); | |
| 2072 | - mulI(RCX, size); | |
| 2073 | - X.alu(X.aluAdd, RCX, RDI); | |
| 2074 | - X.store(X.mem(RBP, tmp), RCX, 8); | |
| 2075 | -end; | |
| 2076 | - | |
| 2077 | -(* Calls *) | |
| 2078 | - | |
| 2079 | -(* The call c, its c.n arguments the values on top of the stack, in | |
| 2080 | - registers, noted as constants or spilled. They go to registers or the | |
| 2081 | - machine's stack, the procedure is called, and its result, if any, | |
| 2082 | - comes back: one value is pushed, two results are pushed as two values, | |
| 2083 | - a slice or a record is stored in the temporary c.ret. | |
| 2084 | - | |
| 2085 | - ni and nf count the integer and the real registers used so far, k the | |
| 2086 | - arguments on the machine's stack, and pad is 1 when a padding quadword | |
| 2087 | - is needed. *) | |
| 2088 | - | |
| 2089 | -proc call*(var c: CallInfo); | |
| 2090 | -var | |
| 2091 | - res: ^Sym.Type; i, ni, nf, k, pad, r, first: INT; | |
| 2092 | - onStack: [64]BOOL; late: [2]INT; (* late: the values for r8 and r9 *) | |
| 2093 | -begin | |
| 2094 | - (* the arguments are the c.n values on top, where computing them left | |
| 2095 | - them; the registers in use under them are saved around the call *) | |
| 2096 | - clobberArgs(); | |
| 2097 | - first := depth - c.n; | |
| 2098 | - spillBelow(first); | |
| 2099 | - res := c.sig^.base; | |
| 2100 | - c.ret := 0; | |
| 2101 | - ni := 0; nf := 0; | |
| 2102 | - if (res # nil) and Sym.holdsArray(res) then | |
| 2103 | - (* a slice of it may be kept for the rest of the procedure | |
| 2104 | - (report 8), so its slot lasts as long *) | |
| 2105 | - c.ret := allocPerm(res^.size, res^.align); | |
| 2106 | - ni := 1; | |
| 2107 | - elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then | |
| 2108 | - c.ret := allocTemp(res^.size, res^.align); | |
| 2109 | - if Sym.behindPointer(res) then ni := 1; end; | |
| 2110 | - end; | |
| 2111 | - (* the System V classes: each argument in the next free register of | |
| 2112 | - its kind, or else on the stack *) | |
| 2113 | - k := 0; | |
| 2114 | - for i := 0 to c.n - 1 do | |
| 2115 | - if c.isF[i] then | |
| 2116 | - onStack[i] := nf >= 8; | |
| 2117 | - if nf < 8 then INC(nf); end; | |
| 2118 | - else | |
| 2119 | - (* a slice's words both go in registers, or both on the stack *) | |
| 2120 | - if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; | |
| 2121 | - elsif c.pair[i] then onStack[i] := ni > 4; | |
| 2122 | - else onStack[i] := ni >= 6; end; | |
| 2123 | - if not onStack[i] then INC(ni); end; | |
| 2124 | - end; | |
| 2125 | - if onStack[i] then INC(k); end; | |
| 2126 | - end; | |
| 2127 | - (* System V wants rsp a multiple of 16 at the call. A procedure's body | |
| 2128 | - has it so (push rbp, and a frame size that is a multiple of 16), so | |
| 2129 | - a quadword of padding makes the count pushed since even. The stack | |
| 2130 | - arguments go right to left, so that the first is at the lowest | |
| 2131 | - address. *) | |
| 2132 | - pad := 0; | |
| 2133 | - if ((pushed + k) & 1) # 0 then pad := 1; end; | |
| 2134 | - if pad # 0 then X.aluI(X.aluSub, RSP, 8); end; | |
| 2135 | - for i := c.n - 1 to 0 by -1 do | |
| 2136 | - if onStack[i] then | |
| 2137 | - valueTo(first + i, RAX); | |
| 2138 | - X.push(RAX); | |
| 2139 | - end; | |
| 2140 | - end; | |
| 2141 | - ni := 0; nf := 0; | |
| 2142 | - late[0] := -1; late[1] := -1; | |
| 2143 | - if (c.ret # 0) and Sym.behindPointer(res) then X.lea(RDI, X.mem(RBP, c.ret)); ni := 1; end; | |
| 2144 | - (* rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as | |
| 2145 | - they are stack registers and may hold arguments not yet moved: late | |
| 2146 | - is the value for each. The fifth goes through rax, so that writing r8 | |
| 2147 | - doesn't destroy a value still wanted for r9. *) | |
| 2148 | - for i := 0 to c.n - 1 do | |
| 2149 | - if not onStack[i] then | |
| 2150 | - if c.isF[i] then | |
| 2151 | - xValueTo(first + i, nf); | |
| 2152 | - INC(nf); | |
| 2153 | - else | |
| 2154 | - if ni < 4 then valueTo(first + i, IntRegs[ni]); else late[ni - 4] := first + i; end; | |
| 2155 | - INC(ni); | |
| 2156 | - end; | |
| 2157 | - end; | |
| 2158 | - end; | |
| 2159 | - if late[0] >= 0 then valueTo(late[0], RAX); end; | |
| 2160 | - if late[1] >= 0 then valueTo(late[1], R9); end; | |
| 2161 | - if late[0] >= 0 then X.movRR(R8, RAX); end; | |
| 2162 | - for i := 1 to c.n do drop(); end; | |
| 2163 | - (* a procedure taking ... is told in al how many xmm registers it got *) | |
| 2164 | - if c.sig^.variadic then X.movRI(RAX, nf); end; | |
| 2165 | - if c.ext then X.callM(X.memRip(c.chunk, 0)); | |
| 2166 | - elsif c.chunk >= 0 then X.callChunk(c.chunk); | |
| 2167 | - else | |
| 2168 | - (* r11 is scratch: caller-saved, carrying no argument, and free, | |
| 2169 | - as the value stack was spilled above *) | |
| 2170 | - X.load(R11, X.mem(RBP, c.target), 8, false); | |
| 2171 | - X.callR(R11); | |
| 2172 | - end; | |
| 2173 | - if k + pad > 0 then X.aluI(X.aluAdd, RSP, 8 * (k + pad)); end; | |
| 2174 | - (* the variables in xmm registers, which the call may have changed, | |
| 2175 | - from their slots *) | |
| 2176 | - for i := 0 to LEN(XVarRegs) - 1 do | |
| 2177 | - if (regMask & (1 shl (XBit + i))) # 0 then | |
| 2178 | - X.movsXM(XVarRegs[i] - XmmVar, X.mem(RBP, homes[i]), false); | |
| 2179 | - end; | |
| 2180 | - end; | |
| 2181 | - (* the result: a slice in rax and rdx goes to the caller's temporary, | |
| 2182 | - two results on the stack, the first under the second, and a single | |
| 2183 | - value on top *) | |
| 2184 | - if res # nil then | |
| 2185 | - if res^.form = Sym.fSlice then | |
| 2186 | - X.store(X.mem(RBP, c.ret), RAX, 8); | |
| 2187 | - X.store(X.mem(RBP, c.ret + 8), RDX, 8); | |
| 2188 | - elsif Sym.inTwoRegs(res) then | |
| 2189 | - X.movRR(target(), RAX); computed(kWord); | |
| 2190 | - X.movRR(target(), RDX); computed(kWord); | |
| 2191 | - elsif (c.ret = 0) and (res^.form = Sym.fReal) then | |
| 2192 | - X.movX(xTarget(), 0); | |
| 2193 | - computed(kindOf(res)); | |
| 2194 | - elsif c.ret = 0 then | |
| 2195 | - r := target(); | |
| 2196 | - X.movRR(r, RAX); | |
| 2197 | - if res^.form = Sym.fBool then X.extend(r, 1, false); | |
| 2198 | - elsif (res^.form = Sym.fInt) and (res^.size < 8) then X.extend(r, INT(res^.size), res^.signed); end; | |
| 2199 | - computed(kindOf(res)); | |
| 2200 | - end; | |
| 2201 | - end; | |
| 2202 | -end; | |
| 2203 | - | |
| 2204 | -(* Frames *) | |
| 2205 | - | |
| 2206 | -(* A slot of size bytes in the frame: its offset from rbp, negative, as | |
| 2207 | - the frame grows down. A temporary is released at the end of its | |
| 2208 | - statement (tempMark, releaseTemps); allocPerm's lasts the procedure. *) | |
| 2209 | -proc allocTemp*(size, align: I64): I64; | |
| 2210 | -begin | |
| 2211 | - if align < 1 then align := 1; end; | |
| 2212 | - frameTop := Sym.alignUp(frameTop + size, align); | |
| 2213 | - if frameTop > frameMax then frameMax := frameTop; end; | |
| 2214 | - return -frameTop; | |
| 2215 | -end; | |
| 2216 | - | |
| 2217 | -proc allocPerm*(size, align: I64): I64; | |
| 2218 | -var s: I64; | |
| 2219 | -begin | |
| 2220 | - s := allocTemp(size, align); | |
| 2221 | - permTop := frameTop; | |
| 2222 | - return s; | |
| 2223 | -end; | |
| 2224 | - | |
| 2225 | -proc tempMark*: I64; | |
| 2226 | -begin | |
| 2227 | - return frameTop; | |
| 2228 | -end; | |
| 2229 | - | |
| 2230 | -(* The slots allocated since the mark m are free again, but those | |
| 2231 | - allocPerm made stay *) | |
| 2232 | -proc releaseTemps*(m: I64); | |
| 2233 | -begin | |
| 2234 | - if m < permTop then m := permTop; end; | |
| 2235 | - frameTop := m; | |
| 2236 | -end; | |
| 2237 | - | |
| 2238 | -(* Every slot used so far stays reserved for the rest of the procedure: | |
| 2239 | - a deferred statement's temporaries live until it runs. *) | |
| 2240 | -proc keepTemps*; | |
| 2241 | -begin | |
| 2242 | - frameTop := frameMax; | |
| 2243 | - permTop := frameMax; | |
| 2244 | -end; | |
| 2245 | - | |
| 2246 | -(* The parameters' places: each in a frame slot of its own, stored there | |
| 2247 | - from its register on entry, or above the frame where the caller | |
| 2248 | - pushed it. A var parameter, and a record or array passed by value, | |
| 2249 | - holds an address (ind). *) | |
| 2250 | -proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); | |
| 2251 | -var p: ^Sym.Obj; ni, nf, k: INT; stackOff: I64; f: BOOL; | |
| 2252 | -begin | |
| 2253 | - frameTop := 0; frameMax := 0; permTop := 0; | |
| 2254 | - resetStack(0, 0); | |
| 2255 | - exitL := NoLabel; | |
| 2256 | - numIn := 0; | |
| 2257 | - lost := false; | |
| 2258 | - kept := 0; | |
| 2259 | - resType := sig^.base; | |
| 2260 | - procParams := params; | |
| 2261 | - regMask := 0; | |
| 2262 | - ni := 0; nf := 0; stackOff := 16; (* stackOff: above the saved rbp and the return address *) | |
| 2263 | - if (resType # nil) and Sym.behindPointer(resType) then | |
| 2264 | - retSlot := allocPerm(8, 8); | |
| 2265 | - inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; | |
| 2266 | - numIn := 1; ni := 1; | |
| 2267 | - end; | |
| 2268 | - p := params; | |
| 2269 | - while p # nil do | |
| 2270 | - p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice)); | |
| 2271 | - f := (p^.typ^.form = Sym.fReal) and not p^.isVar; | |
| 2272 | - if (p^.typ^.form = Sym.fSlice) and not p^.isVar then | |
| 2273 | - (* its two words, in two registers or both on the stack *) | |
| 2274 | - if ni <= 4 then | |
| 2275 | - p^.val := allocPerm(16, 8); | |
| 2276 | - for k := 0 to 1 do | |
| 2277 | - inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + I64(8 * k); | |
| 2278 | - inSize[numIn] := 8; inObj[numIn] := p; | |
| 2279 | - INC(numIn); INC(ni); | |
| 2280 | - end; | |
| 2281 | - else | |
| 2282 | - p^.val := stackOff; | |
| 2283 | - INC(stackOff, 16); | |
| 2284 | - end; | |
| 2285 | - elsif (f and (nf < 8)) or (not f and (ni < 6)) then | |
| 2286 | - p^.val := allocPerm(8, 8); | |
| 2287 | - (* a real's register, xmm nf, is told from an integer register by | |
| 2288 | - a negative number (enterBody decodes it) *) | |
| 2289 | - if f then | |
| 2290 | - inReg[numIn] := -1 - nf; INC(nf); | |
| 2291 | - else | |
| 2292 | - inReg[numIn] := IntRegs[ni]; INC(ni); | |
| 2293 | - end; | |
| 2294 | - inSlot[numIn] := p^.val; | |
| 2295 | - inObj[numIn] := p; | |
| 2296 | - if p^.ind then inSize[numIn] := 8; | |
| 2297 | - elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal} then inSize[numIn] := INT(p^.typ^.size); | |
| 2298 | - else inSize[numIn] := 8; end; | |
| 2299 | - INC(numIn); | |
| 2300 | - else | |
| 2301 | - p^.val := stackOff; | |
| 2302 | - INC(stackOff, 8); | |
| 2303 | - end; | |
| 2304 | - p := p^.next; | |
| 2305 | - end; | |
| 2306 | - paramTop := frameTop; | |
| 2307 | -end; | |
| 2308 | - | |
| 2309 | -(* The frame as beginProc left it, with the parameters' slots only: the | |
| 2310 | - locals are given theirs again once their registers are chosen. *) | |
| 2311 | -proc resetLocals*; | |
| 2312 | -begin | |
| 2313 | - frameTop := paramTop; frameMax := paramTop; permTop := paramTop; | |
| 2314 | -end; | |
| 2315 | - | |
| 2316 | -(* The register the word (0, or 1 for a slice's second) of parameter o | |
| 2317 | - arrives in, or -1 if it comes on the stack or in an xmm register *) | |
| 2318 | -proc arrivesIn(o: ^Sym.Obj; word: INT): INT; | |
| 2319 | -var i, r: INT; | |
| 2320 | -begin | |
| 2321 | - r := -1; | |
| 2322 | - for i := 0 to numIn - 1 do | |
| 2323 | - if (inObj[i] = o) and (inSlot[i] = o^.val + I64(8 * word)) and (inReg[i] >= 0) then r := inReg[i]; end; | |
| 2324 | - end; | |
| 2325 | - return r; | |
| 2326 | -end; | |
| 2327 | - | |
| 2328 | -(* Parameter o, of a procedure that calls nothing, stays in the register | |
| 2329 | - it arrives in if that is rdi or rsi, which only a call or a copy needs: | |
| 2330 | - no register is saved for it, and nothing is moved. A slice stays only | |
| 2331 | - if it has both. If the procedure turns out to copy after all, lost is | |
| 2332 | - set, and it is compiled again with o in memory. *) | |
| 2333 | -proc keepArrived*(o: ^Sym.Obj); | |
| 2334 | -var r: INT; | |
| 2335 | -begin | |
| 2336 | - r := arrivesIn(o, 0); | |
| 2337 | - if o^.typ^.form = Sym.fSlice then | |
| 2338 | - if (r = RDI) and (arrivesIn(o, 1) = RSI) then o^.reg := RDI; o^.reg2 := RSI; end; | |
| 2339 | - elsif (r = RDI) or (r = RSI) then o^.reg := r; end; | |
| 2340 | -end; | |
| 2341 | - | |
| 2342 | -(* Whether parameter o is kept where it arrived *) | |
| 2343 | -proc keptIn*(o: ^Sym.Obj): BOOL; | |
| 2344 | -begin | |
| 2345 | - return (o^.reg = RDI) or (o^.reg = RSI); | |
| 2346 | -end; | |
| 2347 | - | |
| 2348 | -(* The locals in memory have their slots: the prologue zeroes those and | |
| 2349 | - nothing below. The procedure keeps variables in the registers of | |
| 2350 | - mask, which are saved next, a quadword each, in slots that need no | |
| 2351 | - zeroing; the frame as it then stands is where a second try at the | |
| 2352 | - body starts. *) | |
| 2353 | -proc useRegs*(mask: INT); | |
| 2354 | -var i, k: INT; p: ^Sym.Obj; | |
| 2355 | -begin | |
| 2356 | - zeroTop := permTop; | |
| 2357 | - regMask := mask; | |
| 2358 | - (* the parameters left in rdi and rsi, as they arrived *) | |
| 2359 | - kept := 0; | |
| 2360 | - p := procParams; | |
| 2361 | - while p # nil do | |
| 2362 | - if ((p^.reg = RDI) or (p^.reg = RSI)) then kept := kept | (1 shl p^.reg); end; | |
| 2363 | - if ((p^.reg2 = RDI) or (p^.reg2 = RSI)) then kept := kept | (1 shl p^.reg2); end; | |
| 2364 | - p := p^.next; | |
| 2365 | - end; | |
| 2366 | - k := 0; | |
| 2367 | - for i := 0 to 4 do | |
| 2368 | - if (mask & (1 shl i)) # 0 then INC(k); end; | |
| 2369 | - end; | |
| 2370 | - if k > 0 then saveAt := allocPerm(8 * k, 8); end; | |
| 2371 | - frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; | |
| 2372 | -end; | |
| 2373 | - | |
| 2374 | -(* The variable in xmm register r of XVarRegs has a slot of 8 bytes at | |
| 2375 | - slot, kept as it changes *) | |
| 2376 | -proc xHome*(r: INT; slot: I64); | |
| 2377 | -begin | |
| 2378 | - homes[xIndex(r)] := slot; | |
| 2379 | -end; | |
| 2380 | - | |
| 2381 | -proc markCode*(var m: CodeMark); | |
| 2382 | -begin | |
| 2383 | - m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := X.saidCount(); | |
| 2384 | - m.fails := numFails; m.exit := exitL; | |
| 2385 | -end; | |
| 2386 | - | |
| 2387 | -(* The code made since m taken back, as if it had never been made; the | |
| 2388 | - frame is left as it is *) | |
| 2389 | -proc rewind*(m: CodeMark); | |
| 2390 | -begin | |
| 2391 | - Chunk.rewindTo(m.pc, m.fixups, m.lines); | |
| 2392 | - X.unsay(m.said); | |
| 2393 | - numFails := m.fails; | |
| 2394 | - exitL := m.exit; (* a try or return in it made jumps to the exit *) | |
| 2395 | -end; | |
| 2396 | - | |
| 2397 | -(* For a second try at the body: the code as it was at m, and the frame | |
| 2398 | - as useRegs found it *) | |
| 2399 | -proc redo*(m: CodeMark); | |
| 2400 | -begin | |
| 2401 | - Chunk.rewindTo(m.pc, m.fixups, m.lines); | |
| 2402 | - X.unsay(m.said); | |
| 2403 | - numFails := 0; | |
| 2404 | - lost := false; | |
| 2405 | - frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; | |
| 2406 | - exitL := NoLabel; | |
| 2407 | -end; | |
| 2408 | - | |
| 2409 | -(* The prologue, which is for the procedure's first line. Only the | |
| 2410 | - locals in memory are zeroed, from the parameters' slots down to | |
| 2411 | - zeroTop: the parameters are stored over their slots next, the | |
| 2412 | - registers of variables are zeroed in place, their save slots are | |
| 2413 | - written before they are read, and so is every temporary. *) | |
| 2414 | -proc enterBody*(c, line: INT); | |
| 2415 | -var i, loop, r: INT; n: I64; p: ^Sym.Obj; | |
| 2416 | -begin | |
| 2417 | - Chunk.beginChunk(c); | |
| 2418 | - codeChunk := c; | |
| 2419 | - Chunk.noteLine(line, 1); (* a declaration starts a line *) | |
| 2420 | - X.push(RBP); | |
| 2421 | - X.movRR(RBP, RSP); | |
| 2422 | - X.say("sub rsp, frame"); | |
| 2423 | - (* sub rsp, imm32: always the form with a 32-bit field, so that endProc | |
| 2424 | - can patch the frame's size in *) | |
| 2425 | - Chunk.put1(48H); Chunk.put1(81H); Chunk.put1(0ECH); | |
| 2426 | - frameFix := Chunk.pc; Chunk.put4(0); | |
| 2427 | - n := (Sym.alignUp(zeroTop, 8) - paramTop) div 8; | |
| 2428 | - if n = 1 then | |
| 2429 | - X.movRI(RAX, 0); | |
| 2430 | - X.store(X.mem(RBP, -paramTop - 8), RAX, 8); | |
| 2431 | - elsif n > 1 then | |
| 2432 | - (* the locals, 16 bytes at a time, from xmm8, which holds nothing | |
| 2433 | - yet (xmm0 to xmm7 may hold a parameter): up to 16 stores in a | |
| 2434 | - row, else a loop with r10 running from minus the bytes up to 0, | |
| 2435 | - the store's address scaled by 1. An odd quadword is the | |
| 2436 | - lowest. *) | |
| 2437 | - X.pxorXX(8); | |
| 2438 | - if n <= 32 then | |
| 2439 | - for i := 0 to INT(n div 2) - 1 do | |
| 2440 | - X.movdquMX(X.mem(RBP, -paramTop - 16 * I64(i + 1)), 8); | |
| 2441 | - end; | |
| 2442 | - else | |
| 2443 | - X.movRI(R10, -16 * (n div 2)); | |
| 2444 | - loop := Chunk.pc; | |
| 2445 | - X.movdquMX(X.memIdx(RBP, R10, 1, -paramTop), 8); | |
| 2446 | - X.aluI(X.aluAdd, R10, 16); | |
| 2447 | - X.jccBack(X.ccNE, loop); | |
| 2448 | - end; | |
| 2449 | - if n mod 2 = 1 then | |
| 2450 | - X.movRI(RAX, 0); | |
| 2451 | - X.store(X.mem(RBP, -paramTop - 8 * n), RAX, 8); | |
| 2452 | - end; | |
| 2453 | - end; | |
| 2454 | - (* each register that holds a variable is saved, for the caller, and | |
| 2455 | - zeroed, as a variable starts at zero *) | |
| 2456 | - Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt)); | |
| 2457 | - n := 0; | |
| 2458 | - for i := 0 to 4 do | |
| 2459 | - if (regMask & (1 shl i)) # 0 then | |
| 2460 | - X.store(X.mem(RBP, saveAt + 8 * n), VarRegs[i], 8); | |
| 2461 | - X.movRI(VarRegs[i], 0); | |
| 2462 | - INC(n); | |
| 2463 | - end; | |
| 2464 | - end; | |
| 2465 | - (* and each xmm register that holds a variable, zeroed, as its slot | |
| 2466 | - is: the caller keeps its own *) | |
| 2467 | - for i := 0 to LEN(XVarRegs) - 1 do | |
| 2468 | - if (regMask & (1 shl (XBit + i))) # 0 then | |
| 2469 | - X.movRI(RAX, 0); | |
| 2470 | - X.movqXR(XVarRegs[i] - XmmVar, RAX); | |
| 2471 | - end; | |
| 2472 | - end; | |
| 2473 | - (* the parameters that came in registers, to their slots or, for a | |
| 2474 | - register variable, to its register: a slice's second word, the one | |
| 2475 | - for the slot 8 bytes into its own, to its length's *) | |
| 2476 | - for i := 0 to numIn - 1 do | |
| 2477 | - if inReg[i] < 0 then | |
| 2478 | - X.movsMX(X.mem(RBP, inSlot[i]), -1 - inReg[i], false); | |
| 2479 | - if inObj[i]^.reg # 0 then X.movX(inObj[i]^.reg - XmmVar, -1 - inReg[i]); end; | |
| 2480 | - elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then | |
| 2481 | - r := inObj[i]^.reg; | |
| 2482 | - if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; | |
| 2483 | - if r # inReg[i] then X.movRR(r, inReg[i]); end; (* else it is kept where it is *) | |
| 2484 | - if inSize[i] < 8 then X.extend(r, inSize[i], inObj[i]^.typ^.signed); end; | |
| 2485 | - else | |
| 2486 | - X.store(X.mem(RBP, inSlot[i]), inReg[i], inSize[i]); | |
| 2487 | - end; | |
| 2488 | - end; | |
| 2489 | - (* a register variable's parameter that came on the stack, above the | |
| 2490 | - frame (val > 0), is loaded from there; one passed by its address | |
| 2491 | - (ind: var, or const) holds the address, 8 bytes, whatever its type: | |
| 2492 | - a var BOOL loaded as a byte was a bad pointer *) | |
| 2493 | - p := procParams; | |
| 2494 | - while p # nil do | |
| 2495 | - if (p^.reg # 0) and (p^.val > 0) then | |
| 2496 | - if p^.ind then | |
| 2497 | - X.load(p^.reg, X.mem(RBP, p^.val), 8, false); | |
| 2498 | - elsif p^.typ^.form = Sym.fReal then | |
| 2499 | - X.movsXM(p^.reg - XmmVar, X.mem(RBP, p^.val), p^.typ^.size = 4); | |
| 2500 | - elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet} then | |
| 2501 | - X.load(p^.reg, X.mem(RBP, p^.val), INT(p^.typ^.size), p^.typ^.signed); | |
| 2502 | - else | |
| 2503 | - X.load(p^.reg, X.mem(RBP, p^.val), 8, false); | |
| 2504 | - if p^.typ^.form = Sym.fSlice then X.load(p^.reg2, X.mem(RBP, p^.val + 8), 8, false); end; | |
| 2505 | - end; | |
| 2506 | - end; | |
| 2507 | - p := p^.next; | |
| 2508 | - end; | |
| 2509 | -end; | |
| 2510 | - | |
| 2511 | -(* Pops one of several results, a value of size bytes, into its place | |
| 2512 | - off bytes into the caller's, behind the hidden pointer. *) | |
| 2513 | -(* The frame slot that holds the hidden result pointer *) | |
| 2514 | -proc resultSlot*: I64; | |
| 2515 | -begin | |
| 2516 | - return retSlot; | |
| 2517 | -end; | |
| 2518 | - | |
| 2519 | -proc resultTo*(off: I64; size: INT); | |
| 2520 | -var r: INT; fl: BOOL; | |
| 2521 | -begin | |
| 2522 | - r := top(); | |
| 2523 | - fl := isReal(depth - 1); | |
| 2524 | - drop(); | |
| 2525 | - X.load(RCX, X.mem(RBP, retSlot), 8, false); | |
| 2526 | - if fl then X.movsMX(X.mem(RCX, off), r, size = 4); | |
| 2527 | - else X.store(X.mem(RCX, off), r, size); end; | |
| 2528 | -end; | |
| 2529 | - | |
| 2530 | -(* Pops an address, and copies size bytes from it to the result, off | |
| 2531 | - bytes into it. *) | |
| 2532 | -proc copyResult*(off, size: I64); | |
| 2533 | -begin | |
| 2534 | - clobberArgs(); | |
| 2535 | - popTo(RSI); | |
| 2536 | - X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2537 | - if off # 0 then X.lea(RDI, X.mem(RDI, off)); end; | |
| 2538 | - copyBytes(size); | |
| 2539 | -end; | |
| 2540 | - | |
| 2541 | -(* return: the result, a value on top, goes in rax or xmm0; two words, | |
| 2542 | - the two values on top, in rax and rdx; one behind the hidden pointer | |
| 2543 | - goes back with that pointer in rax. *) | |
| 2544 | -proc returnJump*; | |
| 2545 | -begin | |
| 2546 | - if resType # nil then | |
| 2547 | - if Sym.behindPointer(resType) then | |
| 2548 | - X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2549 | - elsif Sym.inTwoRegs(resType) then | |
| 2550 | - popTo(RDX); | |
| 2551 | - popTo(RAX); | |
| 2552 | - elsif resType^.form = Sym.fReal then | |
| 2553 | - xValueTo(depth - 1, 0); | |
| 2554 | - drop(); | |
| 2555 | - else | |
| 2556 | - popTo(RAX); | |
| 2557 | - end; | |
| 2558 | - end; | |
| 2559 | - X.jmp(exitL); | |
| 2560 | -end; | |
| 2561 | - | |
| 2562 | -(* The first of what try does where the error isn't nil (the report, 7.8): | |
| 2563 | - the machine's stack made even, as the deferred statements about to be | |
| 2564 | - run expect to find it. try can stand among the arguments of another | |
| 2565 | - call, where quadwords have been pushed. Nothing is lost: the frame is | |
| 2566 | - above rsp, and the code returns through the frame pointer. *) | |
| 2567 | -proc tryAlign*; | |
| 2568 | -begin | |
| 2569 | - X.aluI(X.aluAnd, RSP, -16); | |
| 2570 | -end; | |
| 2571 | - | |
| 2572 | -(* The procedure returns the error in register r, and a zero for each of | |
| 2573 | - its other results; it isn't rax, rcx, rdx or rdi, which this uses. *) | |
| 2574 | -proc returnError(r: INT); | |
| 2575 | -var f: ^Sym.Obj; | |
| 2576 | -begin | |
| 2577 | - if Sym.behindPointer(resType) then | |
| 2578 | - X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2579 | - zeroBytes(resType^.size); | |
| 2580 | - f := resType^.fields; | |
| 2581 | - while f^.next # nil do f := f^.next; end; | |
| 2582 | - X.load(RDX, X.mem(RBP, retSlot), 8, false); | |
| 2583 | - X.store(X.mem(RDX, f^.val), r, 8); | |
| 2584 | - X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2585 | - elsif Sym.inTwoRegs(resType) then | |
| 2586 | - X.movRR(RDX, r); | |
| 2587 | - X.movRI(RAX, 0); | |
| 2588 | - else | |
| 2589 | - X.movRR(RAX, r); | |
| 2590 | - end; | |
| 2591 | - X.jmp(exitL); | |
| 2592 | -end; | |
| 2593 | - | |
| 2594 | -(* The rest of it: the procedure returns the error in the frame slot, and | |
| 2595 | - a zero for each of its other results. Only code is made: no entry of | |
| 2596 | - the stack of values is touched, since the code that follows is the | |
| 2597 | - code for an error that is nil, with the stack as it was. *) | |
| 2598 | -proc tryReturn*(slot: I64); | |
| 2599 | -begin | |
| 2600 | - X.load(RSI, X.mem(RBP, slot), 8, false); | |
| 2601 | - returnError(RSI); | |
| 2602 | -end; | |
| 2603 | - | |
| 2604 | -(* try, with no deferred statement to run: the error on top is tested in | |
| 2605 | - its register r, which is returned, and, if it isn't nil, returned from | |
| 2606 | - the procedure by tryReturnFrom, so it needs no slot in the frame. *) | |
| 2607 | -proc tryTest*(var ok: INT): INT; | |
| 2608 | -var r: INT; | |
| 2609 | -begin | |
| 2610 | - r := loaded(depth - 1); | |
| 2611 | - jumpIf(ok, false); | |
| 2612 | - return r; | |
| 2613 | -end; | |
| 2614 | - | |
| 2615 | -proc tryReturnFrom*(r: INT); | |
| 2616 | -begin | |
| 2617 | - returnError(r); | |
| 2618 | -end; | |
| 2619 | - | |
| 2620 | -(* The end of the procedure in chunk c: the code that returns, and the | |
| 2621 | - frame's size, known only now, patched into its prologue. If it is | |
| 2622 | - open, running off its end, it returns zeros. *) | |
| 2623 | -proc endProc*(c: INT; open: BOOL): ERROR; | |
| 2624 | -var n: I64; i: INT; | |
| 2625 | -begin | |
| 2626 | - codeChunk := -1; | |
| 2627 | - if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; | |
| 2628 | - if open then | |
| 2629 | - if Sym.behindPointer(resType) then | |
| 2630 | - X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2631 | - X.movRI(RCX, resType^.size); | |
| 2632 | - X.movRI(RAX, 0); | |
| 2633 | - X.repStos(1); | |
| 2634 | - X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2635 | - else | |
| 2636 | - X.movRI(RAX, 0); | |
| 2637 | - if Sym.inTwoRegs(resType) then X.movRI(RDX, 0); end; | |
| 2638 | - X.movqXR(0, RAX); | |
| 2639 | - end; | |
| 2640 | - else | |
| 2641 | - X.dropJumpTo(exitL); | |
| 2642 | - end; | |
| 2643 | - X.fixLink(exitL); | |
| 2644 | - n := 0; | |
| 2645 | - for i := 0 to 4 do | |
| 2646 | - if (regMask & (1 shl i)) # 0 then | |
| 2647 | - X.load(VarRegs[i], X.mem(RBP, saveAt + 8 * n), 8, false); | |
| 2648 | - INC(n); | |
| 2649 | - end; | |
| 2650 | - end; | |
| 2651 | - X.leave(); | |
| 2652 | - X.ret(); | |
| 2653 | - endFails(); | |
| 2654 | - n := Sym.alignUp(frameMax, 16); | |
| 2655 | - if n < 16 then n := 16; end; | |
| 2656 | - if n > 2147483647 then return Scan.mark("frame too big"); end; | |
| 2657 | - Chunk.put4At(frameFix, INT(n)); | |
| 2658 | - (* the listing's text for the sub, which starts 3 bytes (REX.W, opcode, | |
| 2659 | - ModRM) before its immediate *) | |
| 2660 | - if X.listing then X.restate(frameFix - 3, Diag.say("sub rsp, {}", [Fmt.int(INT(n))])); end; | |
| 2661 | - Chunk.endChunk(c); | |
| 2662 | - return nil; | |
| 2663 | -end; | |
| 2664 | - | |
| 2665 | -(* A deferred statement is compiled where it stands, as a subroutine of | |
| 2666 | - its procedure that the code jumps over, in the procedure's own frame | |
| 2667 | - (the same rbp), so that it sees all its variables; deferBegin gives its | |
| 2668 | - address and what pushed was, and starts it, with the return address | |
| 2669 | - pushed. The stack is empty where it is called, and where it begins. *) | |
| 2670 | -proc deferBegin*(var L: INT): (INT, INT); | |
| 2671 | -var saved: INT; | |
| 2672 | -begin | |
| 2673 | - X.jmp(L); | |
| 2674 | - saved := pushed; | |
| 2675 | - pushed := 1; | |
| 2676 | - return Chunk.pc, saved; | |
| 2677 | -end; | |
| 2678 | - | |
| 2679 | -proc deferEnd*(L, saved: INT); | |
| 2680 | -begin | |
| 2681 | - keepTemps(); | |
| 2682 | - X.ret(); | |
| 2683 | - X.fixLink(L); | |
| 2684 | - pushed := saved; | |
| 2685 | -end; | |
| 2686 | - | |
| 2687 | -proc runDefer*(at: INT); | |
| 2688 | -begin | |
| 2689 | - X.callBack(at); | |
| 2690 | -end; | |
| 2691 | - | |
| 2692 | -(* The entry point, _start, and its chunk: it saves argc and argv, and | |
| 2693 | - the address of the places for Rt; runs every used module's body, then | |
| 2694 | - main; and ends with Rt.exit, its status main's or 0. A dynamic program | |
| 2695 | - starts as C's crt1 does, through __libc_start_main(main, argc, argv, | |
| 2696 | - nil, nil, rtld_fini, stack_end), with rsp aligned; its main first | |
| 2697 | - gives the library what it needs of C: Rt C's exit, in cExit, and | |
| 2698 | - Thread pthread_create and pthread_join. | |
| 2699 | - | |
| 2700 | - dynamic: the program uses C. first: the first module, whose chain | |
| 2701 | - gives the order of the bodies. mainObj: the program's main procedure, or | |
| 2702 | - nil. The first numCLinks of cLinks: C's symbols, each loaded from the | |
| 2703 | - quadword the loader fills in and stored in a variable of the | |
| 2704 | - library's, in a program that uses C. exitProc: the chunk of Rt's exit. The | |
| 2705 | - first numLinks of links: the addresses Rt is given, of what the program | |
| 2706 | - has: the places, and the profiler and calls; what it isn't given, | |
| 2707 | - nothing reaches, and the linker leaves out. | |
| 2708 | - | |
| 2709 | - bare: raz -bare, a program for the bare machine, a kernel's, with | |
| 2710 | - no Linux under it. Its entry point reads no argc and argv, which | |
| 2711 | - only Linux leaves on the stack, so that ARGC() is 0; and runs no body | |
| 2712 | - of Rt, whose system calls, to set the signals' handlers, would fault | |
| 2713 | - there. A kernel's own module sets up what Rt's body would. *) | |
| 2714 | -proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; | |
| 2715 | -var entry, start, i: INT; m: ^Sym.Module; | |
| 2716 | -begin | |
| 2717 | - entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); | |
| 2718 | - if dynamic then | |
| 2719 | - start := Chunk.newChunk(Chunk.secText, -1, internStr("main")); | |
| 2720 | - Chunk.beginChunk(entry); | |
| 2721 | - (* at the start rdx holds rtld_fini, from the dynamic loader, and the | |
| 2722 | - stack argc, then argv *) | |
| 2723 | - X.movRI(X.RBP, 0); (* no frame above, where a trace stops *) | |
| 2724 | - X.movRR(R9, RDX); (* rtld_fini, the sixth argument *) | |
| 2725 | - X.pop(RSI); (* argc, the second *) | |
| 2726 | - X.movRR(RDX, RSP); (* argv, the third *) | |
| 2727 | - X.aluI(X.aluAnd, RSP, -16); | |
| 2728 | - X.push(RAX); (* padding, so that the next push, the seventh *) | |
| 2729 | - X.push(RSP); (* argument, stack_end, leaves rsp aligned *) | |
| 2730 | - X.movRI(R8, 0); (* fini and init: none *) | |
| 2731 | - X.movRI(RCX, 0); | |
| 2732 | - X.lea(RDI, X.memRip(start, 0)); (* main, the first argument *) | |
| 2733 | - X.callM(X.memRip(Chunk.externChunk(internStr("__libc_start_main")), 0)); | |
| 2734 | - X.ud2(); (* it never returns *) | |
| 2735 | - Chunk.endChunk(entry); | |
| 2736 | - Chunk.beginChunk(start); | |
| 2737 | - X.push(RBP); (* aligns the stack again *) | |
| 2738 | - X.store(X.memRip(argcChunk, 0), RDI, 8); | |
| 2739 | - X.store(X.memRip(argvChunk, 0), RSI, 8); | |
| 2740 | - for i := 0 to numCLinks - 1 do | |
| 2741 | - X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); | |
| 2742 | - X.store(X.memRip(cLinks[i].into, 0), RAX, 8); | |
| 2743 | - end; | |
| 2744 | - elsif bare then | |
| 2745 | - start := entry; | |
| 2746 | - Chunk.beginChunk(entry); | |
| 2747 | - else | |
| 2748 | - start := entry; | |
| 2749 | - Chunk.beginChunk(entry); | |
| 2750 | - X.load(RAX, X.mem(RSP, 0), 8, false); | |
| 2751 | - X.store(X.memRip(argcChunk, 0), RAX, 8); | |
| 2752 | - X.lea(RAX, X.mem(RSP, 8)); | |
| 2753 | - X.store(X.memRip(argvChunk, 0), RAX, 8); | |
| 2754 | - end; | |
| 2755 | - for i := 0 to numLinks - 1 do | |
| 2756 | - X.lea(RAX, X.memRip(links[i].chunk, 0)); | |
| 2757 | - X.store(X.memRip(links[i].into, 0), RAX, 8); | |
| 2758 | - end; | |
| 2759 | - m := first; | |
| 2760 | - while m # nil do | |
| 2761 | - if m^.used and (m^.body >= 0) and not (bare and (m^.name = internStr("Rt"))) then X.callChunk(m^.body); end; | |
| 2762 | - m := m^.next; | |
| 2763 | - end; | |
| 2764 | - if mainObj # nil then X.callChunk(mainObj^.chunk); end; | |
| 2765 | - if (mainObj # nil) and (mainObj^.typ^.base # nil) then X.movRR(RDI, RAX); else X.movRI(RDI, 0); end; | |
| 2766 | - X.callChunk(exitProc); | |
| 2767 | - X.ud2(); (* exit never returns: trap if it does *) | |
| 2768 | - Chunk.endChunk(start); | |
| 2769 | - (* Rt.fail, for the failures of each kind: the place is where the | |
| 2770 | - check's call is. The return address is the next instruction's, which | |
| 2771 | - may belong to another line; one byte back is inside the call, so the | |
| 2772 | - lookup finds the call's line. rsp is aligned for the call, as the | |
| 2773 | - check's code may have pushed an odd number of quadwords. rdi and rsi | |
| 2774 | - as the failing code had them go on the stack, as Rt.fail's last two | |
| 2775 | - arguments, and a comes in rax (endFails). *) | |
| 2776 | - for i := 0 to LEN(failStubs) - 1 do | |
| 2777 | - if failStubs[i] >= 0 then | |
| 2778 | - Chunk.beginChunk(failStubs[i]); | |
| 2779 | - X.load(R8, X.mem(RSP, 0), 8, false); | |
| 2780 | - X.aluI(X.aluSub, R8, 1); | |
| 2781 | - X.aluI(X.aluAnd, RSP, -16); | |
| 2782 | - X.push(RSI); | |
| 2783 | - X.push(RDI); | |
| 2784 | - X.movRR(RSI, RAX); | |
| 2785 | - X.movRI(RDI, i); | |
| 2786 | - X.callChunk(rtFail); | |
| 2787 | - X.ud2(); | |
| 2788 | - Chunk.endChunk(failStubs[i]); | |
| 2789 | - end; | |
| 2790 | - end; | |
| 2791 | - return entry; | |
| 2792 | -end; | |
| 2793 | - | |
| 2794 | -(* A code procedure: its instructions, read by the assembler (X64), | |
| 2795 | - are the whole of chunk c *) | |
| 2796 | -proc codeProc*(c: INT; constExpr: X.ConstFn; nameFn: X.NameFn): ERROR; | |
| 2797 | -begin | |
| 2798 | - Chunk.beginChunk(c); | |
| 2799 | - try X.assemble(constExpr, nameFn); | |
| 2800 | - Chunk.endChunk(c); | |
| 2801 | - return nil; | |
| 2802 | -end; | |
| 2803 | - | |
| 2804 | -(* Whether the code procedure in chunk c is a square root of xmm0 in | |
| 2805 | - itself, then ret, as Math.sqrt is: | |
| 2806 | - | |
| 2807 | - sqrtsd xmm0, xmm0 F2 0F 51 C0 | |
| 2808 | - ret C3 | |
| 2809 | - | |
| 2810 | - If so, its prefix and opcode, F251H (F351H for sqrtss), which a call of it does in place | |
| 2811 | - on the value's own register (realInPlace); else 0. A call would spill | |
| 2812 | - the stack's reals and reload the variables' xmm registers around it, | |
| 2813 | - as System V keeps none through one, for an instruction of four bytes. *) | |
| 2814 | -proc oneRealOp*(c: INT): INT; | |
| 2815 | -var s: INT; | |
| 2816 | -begin | |
| 2817 | - s := Chunk.chunks[c].start; | |
| 2818 | - if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end; | |
| 2819 | - if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH) | |
| 2820 | - or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then | |
| 2821 | - return 0; | |
| 2822 | - end; | |
| 2823 | - return INT(Chunk.text[s]) * 256 + INT(Chunk.text[s + 2]); | |
| 2824 | -end; | |
| 2825 | - | |
| 2826 | -(* The operation op, of oneRealOp, on the real on top, in its register: | |
| 2827 | - Math.sqrt(d2) is sqrtsd xmm8, xmm8, with no call. *) | |
| 2828 | -proc realInPlace*(op: INT); | |
| 2829 | -var x: INT; | |
| 2830 | -begin | |
| 2831 | - x := top(); | |
| 2832 | - X.sse(op & 0FFH, x, x, op shr 8 = 0F3H); | |
| 2833 | -end; | |
| 2834 | - | |
| 2835 | -var i: INT; | |
| 2836 | -begin | |
| 2837 | - for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; | |
| 2838 | - inPlace := -1; | |
| 2839 | - codeChunk := -1; | |
| 2840 | - resetStack(0, 0); | |
| 2841 | - rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail")); | |
| 2842 | - argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc")); | |
| 2843 | - Chunk.setBss(argcChunk, 8, 8); | |
| 2844 | - argvChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argv")); | |
| 2845 | - Chunk.setBss(argvChunk, 8, 8); | |
| 2846 | -end Gen. |
| @@ -0,0 +1,2860 @@ | ||
| 1 | +(* Gen: the code generator, for x86-64 (Gen.x86_64.raz; another machine has | |
| 2 | + a Gen of its own, chosen by the suffix of its file, doc/raz.md, 3). | |
| 3 | + | |
| 4 | + Item and Parse drive a stack machine: they push constants, addresses | |
| 5 | + and the values of variables, operate on the values on top, store | |
| 6 | + them and jump on them. Every value has a kind: a word, which is an | |
| 7 | + integer as big as an address, a pointer, a BOOL, a set or a narrower | |
| 8 | + integer; a long, a 64-bit integer on a machine whose word is smaller | |
| 9 | + (never here); an F32; an F64. This unit makes the machine's code for | |
| 10 | + those calls, and nothing above it knows the machine: a port is a new | |
| 11 | + Gen, with an encoder of its own. | |
| 12 | + | |
| 13 | + The stack is kept in registers, as in Wirth's compilers (Niklaus | |
| 14 | + Wirth's Pascal and Oberon compilers): the value at depth d, depth 0 | |
| 15 | + being the bottom, is in r8 to r11, register d mod 4, or in xmm8 to | |
| 16 | + xmm11 if it is a real. A value that needs a register a lower one | |
| 17 | + holds spills that one: stores it in a frame slot, to free the | |
| 18 | + register. A constant, or an address in the frame or of a chunk, is | |
| 19 | + only noted until something needs it in a register, so that it can be | |
| 20 | + an instruction's operand. | |
| 21 | + | |
| 22 | + A slice is two words, a pointer and then a length. | |
| 23 | + | |
| 24 | + Calls use the System V convention, which x86-64 Linux has for C: | |
| 25 | + arguments in rdi, rsi, rdx, rcx, r8, r9 and xmm0 to xmm7, the rest on | |
| 26 | + the machine's stack; rbx, rbp and r12 to r15 are callee-saved, which | |
| 27 | + is to say a called procedure gives them back as it found them. The | |
| 28 | + arguments are computed left to right onto the stack of values, and | |
| 29 | + moved into their registers just before the call: a call in a later | |
| 30 | + argument spills the earlier ones, as any call spills the values under | |
| 31 | + it, so that it can't disturb them. A slice goes as its two words, as | |
| 32 | + System V passes a 16-byte struct of two integers; a record, array or | |
| 33 | + variant passed by value goes as the address of a copy the caller | |
| 34 | + makes. A slice, or two results that are single values, come back in | |
| 35 | + rax and rdx (inTwoRegs); any other record, or several results, comes | |
| 36 | + back in memory the caller provides, through a hidden pointer | |
| 37 | + (behindPointer), where C would send a small struct back in registers. | |
| 38 | + | |
| 39 | + A procedure's frame holds its parameters, its variables, and the | |
| 40 | + temporaries of the statement running, and the results of its calls | |
| 41 | + that hold an array (report 8). The variables are zeroed on | |
| 42 | + entry. | |
| 43 | + | |
| 44 | + Depends on: Scan and Diag for errors, Sym for types, Chunk for code | |
| 45 | + and data, X64 for instructions. | |
| 46 | + Leaves out: what the program means, its types and their checks; that | |
| 47 | + is Item. | |
| 48 | + | |
| 49 | + The seed's `Mark` in the middle of a push or a check can't give an | |
| 50 | + error back, so it is shown and the call goes on without doing what | |
| 51 | + it could not (see newEntry and check): the compile ends with the | |
| 52 | + message, and the parser's recovery makes the stack right again with | |
| 53 | + resetStack. A full stack (newEntry) ends the compile at once: the pops | |
| 54 | + after it would go below the stack. endProc returns its error, for the | |
| 55 | + parser to pass up. | |
| 56 | + | |
| 57 | + What the parser sets, `inPlace`, has a setter, as the exported | |
| 58 | + variables can't be set from outside. The seed's types lose their T | |
| 59 | + (`TAdr` is `Adr`), and its `Call` record, which the procedure `call` | |
| 60 | + takes, is `CallInfo`. *) | |
| 61 | +module Gen; | |
| 62 | +import Fmt, Diag, Scan, Sym, Chunk, X := X64; | |
| 63 | +import SYSTEM; | |
| 64 | + | |
| 65 | +const | |
| 66 | + Arch* = "x86_64"; (* the target's machine, as a file's suffix says it *) | |
| 67 | + NoLabel* = -1; | |
| 68 | + WordSize* = 8; (* the size of an address, and of INT *) | |
| 69 | + (* What the machine catches by itself, which the code needn't test for | |
| 70 | + (report section 10): on x86-64 Linux, a division by zero, a stack | |
| 71 | + overflow (at the guard page under the stack), a call to nil, and | |
| 72 | + a load or store within NilPage bytes of address 0, which is never | |
| 73 | + mapped. *) | |
| 74 | + NilPage* = 4096; | |
| 75 | + | |
| 76 | + RAX = X.RAX; RCX = X.RCX; RDX = X.RDX; RSP = X.RSP; RBP = X.RBP; | |
| 77 | + RSI = X.RSI; RDI = X.RDI; R8 = X.R8; R9 = X.R9; R10 = X.R10; R11 = X.R11; | |
| 78 | + | |
| 79 | +type | |
| 80 | + Kind* = (kWord, kLong, kF32, kF64); | |
| 81 | + | |
| 82 | + (* Where a variable is: its address is relative to the frame, to a | |
| 83 | + chunk, to an address on the stack, or to one held in a frame slot *) | |
| 84 | + Base* = (bFrame, bGlobal, bStack, bSlot, bReg, bPtr); | |
| 85 | + Adr* = record | |
| 86 | + base*: Base; | |
| 87 | + chunk*: INT; (* bGlobal: the chunk; bSlot: the frame slot; bReg: the register; | |
| 88 | + bPtr: the register that holds the address *) | |
| 89 | + off*: I64; (* added to the address; for bReg, which word, 0 or 8 *) | |
| 90 | + hi*: INT; (* bReg, a slice: the register of its length, the | |
| 91 | + second word; chunk is that of its pointer *) | |
| 92 | + end; | |
| 93 | + | |
| 94 | + (* The code made so far, to go back to for a second try at a body *) | |
| 95 | + CodeMark* = record pc*, fixups*, lines*, said*, fails*, exit*: INT; end; | |
| 96 | + | |
| 97 | + (* For entryPoint: the address of chunk, put in the variable whose | |
| 98 | + chunk is into before the program starts; or, for C's symbols, the | |
| 99 | + address in chunk, the quadword the loader fills in *) | |
| 100 | + Link* = record chunk*, into*: INT; end; | |
| 101 | + | |
| 102 | + CallInfo* = record | |
| 103 | + sig*: ^Sym.Type; | |
| 104 | + param*: ^Sym.Obj; (* the parameter the next argument is for *) | |
| 105 | + obj*: ^Sym.Obj; (* the procedure called, if it has a name *) | |
| 106 | + n*: INT; (* the arguments *) | |
| 107 | + isF*: [64]BOOL; (* ... goes in an xmm register *) | |
| 108 | + pair*: [64]BOOL; (* ... is a slice's first word, the next its second *) | |
| 109 | + target*: I64; (* the frame slot of a procedure value, if chunk < 0 *) | |
| 110 | + chunk*: INT; (* else the procedure's chunk *) | |
| 111 | + ext*: BOOL; (* C's: called through the chunk holding its address *) | |
| 112 | + ret*: I64; (* set by call: the temporary a slice or a record result is in *) | |
| 113 | + end; | |
| 114 | + | |
| 115 | +const | |
| 116 | + (* the registers a procedure keeps its busiest variables in: System V's | |
| 117 | + callee-saved ones, so they live through calls (a register variable); | |
| 118 | + a procedure saves those it uses, and Rt finds them there for the | |
| 119 | + trap report *) | |
| 120 | + VarRegs*: [5]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15]; | |
| 121 | + (* the registers the trap report can show a variable in: VarRegs, then | |
| 122 | + rdi and rsi, where a procedure that calls nothing keeps the | |
| 123 | + parameters that arrive in them (keptIn) *) | |
| 124 | + ShownRegs*: [7]INT = [X.RBX, X.R12, X.R13, X.R14, X.R15, X.RDI, X.RSI]; | |
| 125 | + (* the registers a procedure keeps its busiest reals in, xmm12 to | |
| 126 | + xmm15, numbered from XmmVar up so that they aren't taken for r12 to | |
| 127 | + r15 (regAt); and in a procedure that calls nothing, which has no | |
| 128 | + real parameter in them, xmm4 to xmm7 too, as nothing else uses | |
| 129 | + them there. Each has a slot too, written as it is, where the trap | |
| 130 | + report finds it, and from which it is loaded again after a call, as | |
| 131 | + System V has no callee-saved xmm register (xHome). *) | |
| 132 | + XmmVar* = 16; | |
| 133 | + XVarRegs*: [8]INT = [XmmVar + 12, XmmVar + 13, XmmVar + 14, XmmVar + 15, | |
| 134 | + XmmVar + 4, XmmVar + 5, XmmVar + 6, XmmVar + 7]; | |
| 135 | + (* a procedure's registers of variables, as bits: bit i is VarRegs[i], | |
| 136 | + bit XBit + i XVarRegs[i] *) | |
| 137 | + XBit* = 5; | |
| 138 | + | |
| 139 | +var | |
| 140 | + rtFail*, argcChunk*, argvChunk*: INT; | |
| 141 | + (* the stubs that check's code calls, one for each kind of failure, | |
| 142 | + made by entryPoint for those used *) | |
| 143 | + failStubs: [8]INT; | |
| 144 | + lost*: BOOL; (* a parameter kept where it arrived was wanted | |
| 145 | + for other work: the procedure is compiled again, | |
| 146 | + the parameter in memory *) | |
| 147 | + inPlace: INT; (* the register of a variable that the statement | |
| 148 | + being compiled assigns, x := x + e, where the | |
| 149 | + value can be worked out in the variable's own | |
| 150 | + register, from its first push on (lInto) *) | |
| 151 | + depth*: INT; (* the values on the stack *) | |
| 152 | + pushed*: INT; (* quadwords pushed on the machine's stack *) | |
| 153 | + codeChunk*: INT; (* the procedure or body being compiled, or -1 *) | |
| 154 | + | |
| 155 | +type | |
| 156 | + (* where a value on the stack is *) | |
| 157 | + Loc = (lConst, (* not loaded: val is the value *) | |
| 158 | + lHeld, (* not loaded: in register val, a register | |
| 159 | + variable's own (see VarRegs), read in place, which | |
| 160 | + nothing in an expression can change *) | |
| 161 | + lAdr, (* not loaded: the address of chunk, or of the frame | |
| 162 | + if chunk is -1, plus val *) | |
| 163 | + lIdx, (* not loaded: the address m, an element of an array | |
| 164 | + or of a slice in registers: the array's or the | |
| 165 | + pointer's register, or rbp, as the base, and the | |
| 166 | + index in a register variable's or the value's | |
| 167 | + own register (indexSlice, indexArray) *) | |
| 168 | + lMem, (* not loaded: the value of size bytes at m, in the | |
| 169 | + frame, a chunk, or at an address lAdr or lIdx | |
| 170 | + noted, read where it is used (pushVar) *) | |
| 171 | + lInto, (* in register val, a register variable's own, which | |
| 172 | + is what the value goes to (inPlace): so it is | |
| 173 | + changed in place *) | |
| 174 | + lReg, (* in its register *) | |
| 175 | + lFrame); (* spilled: in the frame slot val *) | |
| 176 | + | |
| 177 | + Entry = record | |
| 178 | + loc: Loc; | |
| 179 | + val: I64; (* lConst: the value; lHeld: the register; lAdr: the | |
| 180 | + offset; lFrame: the frame slot *) | |
| 181 | + chunk: INT; (* lAdr: the chunk, or -1 for the frame *) | |
| 182 | + m: X.Mem; (* lIdx, lMem *) | |
| 183 | + size: INT; (* lMem *) | |
| 184 | + signed: BOOL; (* lMem *) | |
| 185 | + k: Kind; | |
| 186 | + off: I64; (* lHeld, lReg: a word, its register plus off, the | |
| 187 | + constant of x + 1 not yet added (op), so that an | |
| 188 | + operand that uses it can take it as its | |
| 189 | + displacement: [r13 + r9 * 8 - 8] *) | |
| 190 | + end; | |
| 191 | + | |
| 192 | + (* a check of the procedure, whose failure endFails makes *) | |
| 193 | + Fail = record | |
| 194 | + L, what, ra, name: INT; | |
| 195 | + path, line, col: INT; (* the place, told now: an inlined procedure's | |
| 196 | + is in its own file (Parse's inlined) *) | |
| 197 | + m: X.Mem; | |
| 198 | + n: I64; | |
| 199 | + end; | |
| 200 | + | |
| 201 | +const | |
| 202 | + MaxStack = 1024; (* values waiting in one expression: 63 nested calls of up to 15 arguments each *) | |
| 203 | + StubNames: [8][]const U8 = ["check0", "check1", "check2", "check3", "check4", "check5", "check6", "check7"]; | |
| 204 | + (* the registers of the stack: the value at depth d is in Regs[d mod | |
| 205 | + NumRegs]. rax, rdx and rcx, which division and shifts need, and rsi | |
| 206 | + and rdi, which copying needs, aren't among them. *) | |
| 207 | + NumRegs = 4; | |
| 208 | + Regs: [NumRegs]INT = [R8, R9, R10, R11]; | |
| 209 | + XRegs: [NumRegs]INT = [8, 9, 10, 11]; (* xmm8 to xmm11 *) | |
| 210 | + IntRegs: [6]INT = [RDI, RSI, RDX, RCX, R8, R9]; | |
| 211 | + | |
| 212 | +var | |
| 213 | + stack: [MaxStack]Entry; | |
| 214 | + owner: [NumRegs]INT; (* the value in each register, or -1 *) | |
| 215 | + frameTop, frameMax, permTop: I64; | |
| 216 | + frameFix: INT; (* where the frame size is patched in *) | |
| 217 | + paramTop: I64; (* the parameters' slots end here *) | |
| 218 | + zeroTop: I64; (* the locals the prologue zeroes end here *) | |
| 219 | + retSlot: I64; (* holds the hidden result pointer *) | |
| 220 | + resType: ^Sym.Type; (* the procedure's result, or nil *) | |
| 221 | + exitL: INT; | |
| 222 | + (* parameters arriving in registers: where each is stored on entry *) | |
| 223 | + inReg: [14]INT; | |
| 224 | + inSlot: [14]I64; | |
| 225 | + inSize: [14]INT; | |
| 226 | + inObj: [14]^Sym.Obj; | |
| 227 | + numIn: INT; | |
| 228 | + procParams: ^Sym.Obj; (* the parameters, for enterBody *) | |
| 229 | + regMask: INT; (* the registers of variables, as bits (XBit) *) | |
| 230 | + kept: INT; (* bit r: register r holds a parameter, as it arrived *) | |
| 231 | + saveAt: I64; (* where they are saved, a quadword each *) | |
| 232 | + homes: [8]I64; (* the slots of the variables in XVarRegs *) | |
| 233 | + frameAt: [3]I64; (* frameTop, frameMax, permTop after the declarations *) | |
| 234 | + fails: [4096]Fail; | |
| 235 | + numFails: INT; | |
| 236 | + | |
| 237 | +(* -S: the text of each instruction is kept, and list prints it. Raz asks | |
| 238 | + Gen, not the encoder, so that no unit but Gen names the machine. *) | |
| 239 | +proc setListing*(yes: BOOL); | |
| 240 | +begin | |
| 241 | + X.setListing(yes); | |
| 242 | +end; | |
| 243 | + | |
| 244 | +proc list*(home: INT); | |
| 245 | +begin | |
| 246 | + X.list(home); | |
| 247 | +end; | |
| 248 | + | |
| 249 | +proc setInPlace*(r: INT); | |
| 250 | +begin | |
| 251 | + inPlace := r; | |
| 252 | +end; | |
| 253 | + | |
| 254 | +proc kindOf*(t: ^Sym.Type): Kind; | |
| 255 | +begin | |
| 256 | + if t^.form = Sym.fReal then | |
| 257 | + if t^.size = 4 then return kF32; end; | |
| 258 | + return kF64; | |
| 259 | + end; | |
| 260 | + if t^.size > WordSize then return kLong; end; | |
| 261 | + return kWord; | |
| 262 | +end; | |
| 263 | + | |
| 264 | +(* The stack, as in Wirth's compilers: the value at depth d is held in | |
| 265 | + register d mod NumRegs. A value, when a higher one wants its register, | |
| 266 | + and every value in a register when a call would clobber them, is | |
| 267 | + spilled to a frame slot, and loaded again where it is used. A constant | |
| 268 | + is only noted. | |
| 269 | + | |
| 270 | + drop emits no code, and a dropped value's register still holds it | |
| 271 | + until the next push claims it: place (for bStack), operands, | |
| 272 | + indexSlice and jumpCmp read a register after dropping its value. | |
| 273 | + | |
| 274 | + Registers outside the stack, free within one operation: rax, rcx and | |
| 275 | + rdx for arithmetic; rsi and rdi for copying; and, where the stack holds | |
| 276 | + nothing in them, r10 for the prologue's zeroing loop and r11 for a call | |
| 277 | + through a procedure value; xmm0 and xmm1 for reals. | |
| 278 | + | |
| 279 | + A real, an F32 or an F64, is held in xmm8 to xmm11 (XRegs), where the | |
| 280 | + machine computes with reals, the value at depth d in XRegs[d mod | |
| 281 | + NumRegs]: so each depth has two registers, and holds its value in the | |
| 282 | + one of its kind, which owner doesn't tell apart. The xmm registers | |
| 283 | + from xmm8 up carry no arguments, so that call can move a real into | |
| 284 | + xmm0 to xmm7 without overwriting another still to go. *) | |
| 285 | + | |
| 286 | +proc fits32(v: I64): BOOL; | |
| 287 | +begin | |
| 288 | + return (v >= -2147483648) and (v <= 2147483647); | |
| 289 | +end; | |
| 290 | + | |
| 291 | +(* The register of kind k for the value at depth i *) | |
| 292 | +proc regIn(k: Kind; i: INT): INT; | |
| 293 | +begin | |
| 294 | + if k in {kF32, kF64} then return XRegs[i mod NumRegs]; end; | |
| 295 | + return Regs[i mod NumRegs]; | |
| 296 | +end; | |
| 297 | + | |
| 298 | +proc regOf(i: INT): INT; | |
| 299 | +begin | |
| 300 | + return regIn(stack[i].k, i); | |
| 301 | +end; | |
| 302 | + | |
| 303 | +proc isReal(i: INT): BOOL; | |
| 304 | +begin | |
| 305 | + return stack[i].k in {kF32, kF64}; | |
| 306 | +end; | |
| 307 | + | |
| 308 | +(* A real constant, its bits v, as an operand: 8 bytes in read-only | |
| 309 | + data, as the machine has no instruction that holds one *) | |
| 310 | +proc realConst(v: I64): X.Mem; | |
| 311 | +var c: INT; | |
| 312 | +begin | |
| 313 | + c := Chunk.newChunk(Chunk.secRodata, -1, -1); | |
| 314 | + Chunk.setData(c, ^U8(SYSTEM.ADR(v)), 8, 8); | |
| 315 | + return X.memRip(c, 0); | |
| 316 | +end; | |
| 317 | + | |
| 318 | +(* The real i into xmm register x, from wherever it is, as valueTo does | |
| 319 | + for an integer register *) | |
| 320 | +proc xValueTo(i, x: INT); | |
| 321 | +begin | |
| 322 | + match stack[i].loc of | |
| 323 | + | lConst: X.movsXM(x, realConst(stack[i].val), stack[i].k = kF32); | |
| 324 | + | lMem: X.movsXM(x, stack[i].m, stack[i].size = 4); | |
| 325 | + | lHeld: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end; | |
| 326 | + | lInto: if INT(stack[i].val) # x then X.movX(x, INT(stack[i].val)); end; | |
| 327 | + | lReg: if regOf(i) # x then X.movX(x, regOf(i)); end; | |
| 328 | + | else: X.movsXM(x, X.mem(RBP, stack[i].val), false); (* spilled, as 8 bytes *) | |
| 329 | + end; | |
| 330 | +end; | |
| 331 | + | |
| 332 | +proc loaded(i: INT): INT; forward; | |
| 333 | +proc allocPerm*(size, align: I64): I64; forward; | |
| 334 | +proc allocTemp*(size, align: I64): I64; forward; | |
| 335 | + | |
| 336 | +(* The value i, in a register, goes to a frame slot; one noted that | |
| 337 | + holds its register, as an index, is worked out in it first *) | |
| 338 | +proc spill(i: INT); | |
| 339 | +var s: I64; | |
| 340 | +begin | |
| 341 | + if (stack[i].loc # lReg) or (stack[i].off # 0) then _ := loaded(i); end; | |
| 342 | + s := allocTemp(8, 8); | |
| 343 | + if isReal(i) then X.movsMX(X.mem(RBP, s), regOf(i), false); | |
| 344 | + else X.store(X.mem(RBP, s), regOf(i), 8); end; | |
| 345 | + stack[i].loc := lFrame; | |
| 346 | + stack[i].val := s; | |
| 347 | + owner[i mod NumRegs] := -1; | |
| 348 | +end; | |
| 349 | + | |
| 350 | +(* Each value below depth d read from memory now, as what comes next | |
| 351 | + may change it *) | |
| 352 | +proc readMems(d: INT); | |
| 353 | +var i: INT; | |
| 354 | +begin | |
| 355 | + for i := 0 to d - 1 do | |
| 356 | + if stack[i].loc = lMem then _ := loaded(i); end; | |
| 357 | + end; | |
| 358 | +end; | |
| 359 | + | |
| 360 | +(* Every value below depth d in a register, spilled; one noted in memory | |
| 361 | + is read first, as a call may change it *) | |
| 362 | +proc spillBelow(d: INT); | |
| 363 | +var r: INT; | |
| 364 | +begin | |
| 365 | + readMems(d); | |
| 366 | + for r := 0 to NumRegs - 1 do | |
| 367 | + if (owner[r] >= 0) and (owner[r] < d) then spill(owner[r]); end; | |
| 368 | + end; | |
| 369 | +end; | |
| 370 | + | |
| 371 | +(* The register of kind k of value i, for it to hold: a lower value at | |
| 372 | + its depth's registers is spilled. *) | |
| 373 | +proc claim(i: INT; k: Kind): INT; | |
| 374 | +var r: INT; | |
| 375 | +begin | |
| 376 | + r := i mod NumRegs; | |
| 377 | + if (owner[r] >= 0) and (owner[r] # i) then spill(owner[r]); end; | |
| 378 | + owner[r] := i; | |
| 379 | + return regIn(k, i); | |
| 380 | +end; | |
| 381 | + | |
| 382 | +(* The memory at the noted address i, plus off *) | |
| 383 | +proc adrMem(i: INT; off: I64): X.Mem; | |
| 384 | +begin | |
| 385 | + if stack[i].chunk < 0 then return X.mem(RBP, stack[i].val + off); end; | |
| 386 | + return X.memRip(stack[i].chunk, INT(stack[i].val + off)); | |
| 387 | +end; | |
| 388 | + | |
| 389 | +(* The value i, in its register *) | |
| 390 | +proc loaded(i: INT): INT; | |
| 391 | +var r: INT; | |
| 392 | +begin | |
| 393 | + if stack[i].loc = lInto then return INT(stack[i].val); end; | |
| 394 | + if (stack[i].loc # lReg) and isReal(i) then | |
| 395 | + xValueTo(i, claim(i, stack[i].k)); | |
| 396 | + stack[i].loc := lReg; | |
| 397 | + elsif stack[i].loc # lReg then | |
| 398 | + r := claim(i, kWord); | |
| 399 | + match stack[i].loc of | |
| 400 | + | lConst: X.movRI(r, stack[i].val); | |
| 401 | + | lHeld: X.movRR(r, INT(stack[i].val)); | |
| 402 | + | lAdr: X.lea(r, adrMem(i, 0)); | |
| 403 | + | lIdx: X.lea(r, stack[i].m); | |
| 404 | + | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 405 | + | else: X.load(r, X.mem(RBP, stack[i].val), 8, false); | |
| 406 | + end; | |
| 407 | + stack[i].loc := lReg; | |
| 408 | + end; | |
| 409 | + if stack[i].off # 0 then | |
| 410 | + X.lea(regOf(i), X.mem(regOf(i), stack[i].off)); | |
| 411 | + stack[i].off := 0; | |
| 412 | + end; | |
| 413 | + return regOf(i); | |
| 414 | +end; | |
| 415 | + | |
| 416 | +(* The register value i is in, only to be read: a variable's own, or | |
| 417 | + its register *) | |
| 418 | +proc peek(i: INT): INT; | |
| 419 | +begin | |
| 420 | + if (stack[i].loc = lHeld) and (stack[i].off = 0) then return INT(stack[i].val); end; | |
| 421 | + return loaded(i); | |
| 422 | +end; | |
| 423 | + | |
| 424 | +(* The offset of value i, taken from it, for the caller to add or to | |
| 425 | + make a displacement of *) | |
| 426 | +proc takeOff(i: INT): I64; | |
| 427 | +var o: I64; | |
| 428 | +begin | |
| 429 | + o := stack[i].off; | |
| 430 | + stack[i].off := 0; | |
| 431 | + return o; | |
| 432 | +end; | |
| 433 | + | |
| 434 | +(* The word on top, in a register, has o added: noted, if o can be a | |
| 435 | + displacement, else added now *) | |
| 436 | +proc putOff(o: I64); | |
| 437 | +begin | |
| 438 | + if fits32(stack[depth - 1].off + o) then | |
| 439 | + stack[depth - 1].off := stack[depth - 1].off + o; | |
| 440 | + else | |
| 441 | + X.movRI(RCX, o); | |
| 442 | + X.alu(X.aluAdd, loaded(depth - 1), RCX); | |
| 443 | + end; | |
| 444 | +end; | |
| 445 | + | |
| 446 | +proc top: INT; | |
| 447 | +begin | |
| 448 | + if depth = 0 then Diag.fatal("internal error: the stack is empty"); end; | |
| 449 | + return loaded(depth - 1); | |
| 450 | +end; | |
| 451 | + | |
| 452 | +(* A value on the stack's top. At the stack's last place the error is shown | |
| 453 | + and the compile ends there: with nothing pushed, the pops after it would | |
| 454 | + go below the stack. See the head of this unit. *) | |
| 455 | +proc newEntry(k: Kind; loc: Loc; v: I64); | |
| 456 | +begin | |
| 457 | + if depth > LEN(stack) - 1 then | |
| 458 | + _ := Scan.mark(Diag.say("more than {} values waiting in one expression", [Fmt.int(MaxStack)])); | |
| 459 | + Diag.finish(); (* ends the compile: an error was counted *) | |
| 460 | + return; | |
| 461 | + end; | |
| 462 | + stack[depth].k := k; | |
| 463 | + stack[depth].loc := loc; | |
| 464 | + stack[depth].val := v; | |
| 465 | + stack[depth].chunk := -1; | |
| 466 | + stack[depth].off := 0; | |
| 467 | + INC(depth); | |
| 468 | +end; | |
| 469 | + | |
| 470 | +(* An address, lIdx, or a value at it, lMem, noted as the operand m. Its | |
| 471 | + index may be in the register of the value just dropped, at this | |
| 472 | + depth: it is the new value's own, then. *) | |
| 473 | +proc newMem(loc: Loc; m: X.Mem; k: Kind; size: INT; signed: BOOL); | |
| 474 | +begin | |
| 475 | + newEntry(k, loc, 0); | |
| 476 | + stack[depth - 1].m := m; | |
| 477 | + stack[depth - 1].size := size; | |
| 478 | + stack[depth - 1].signed := signed; | |
| 479 | + if m.index = Regs[(depth - 1) mod NumRegs] then owner[(depth - 1) mod NumRegs] := depth - 1; end; | |
| 480 | +end; | |
| 481 | + | |
| 482 | +(* The register a new value on top is to be computed in *) | |
| 483 | +proc target: INT; | |
| 484 | +begin | |
| 485 | + return claim(depth, kWord); | |
| 486 | +end; | |
| 487 | + | |
| 488 | +(* The xmm register a new real on top is to be computed in *) | |
| 489 | +proc xTarget: INT; | |
| 490 | +begin | |
| 491 | + return claim(depth, kF64); | |
| 492 | +end; | |
| 493 | + | |
| 494 | +(* The value just computed in target goes on top *) | |
| 495 | +proc computed(k: Kind); | |
| 496 | +begin | |
| 497 | + newEntry(k, lReg, 0); | |
| 498 | +end; | |
| 499 | + | |
| 500 | +proc drop*; | |
| 501 | +begin | |
| 502 | + DEC(depth); | |
| 503 | + if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1; end; | |
| 504 | +end; | |
| 505 | + | |
| 506 | +(* rsi and rdi are about to be used for other work, as in copying: a | |
| 507 | + parameter kept in either must go to memory (lost). *) | |
| 508 | +proc clobberArgs; | |
| 509 | +begin | |
| 510 | + if kept # 0 then lost := true; end; | |
| 511 | +end; | |
| 512 | + | |
| 513 | +(* The value i into register r, from wherever it is: a constant or an | |
| 514 | + address is made there, a variable's register or the value's own | |
| 515 | + register copied, a spilled value loaded. Unlike loaded, it leaves the | |
| 516 | + value where it was, so r is its only new copy. *) | |
| 517 | +proc valueTo(i, r: INT); | |
| 518 | +begin | |
| 519 | + if isReal(i) and (stack[i].loc in {lHeld, lInto}) then | |
| 520 | + X.movqRX(r, INT(stack[i].val)); | |
| 521 | + else | |
| 522 | + match stack[i].loc of | |
| 523 | + | lConst: X.movRI(r, stack[i].val); | |
| 524 | + | lHeld: X.movRR(r, INT(stack[i].val)); | |
| 525 | + | lInto: X.movRR(r, INT(stack[i].val)); | |
| 526 | + | lAdr: X.lea(r, adrMem(i, 0)); | |
| 527 | + | lIdx: X.lea(r, stack[i].m); | |
| 528 | + | lMem: X.load(r, stack[i].m, stack[i].size, stack[i].signed); | |
| 529 | + | lReg: | |
| 530 | + if isReal(i) then X.movqRX(r, regOf(i)); | |
| 531 | + elsif regOf(i) # r then X.movRR(r, regOf(i)); end; | |
| 532 | + | else: X.load(r, X.mem(RBP, stack[i].val), 8, false); | |
| 533 | + end; | |
| 534 | + end; | |
| 535 | + if stack[i].off # 0 then X.lea(r, X.mem(r, stack[i].off)); end; | |
| 536 | +end; | |
| 537 | + | |
| 538 | +(* The value on top, popped into register r *) | |
| 539 | +proc popTo(r: INT); | |
| 540 | +begin | |
| 541 | + valueTo(depth - 1, r); | |
| 542 | + drop(); | |
| 543 | +end; | |
| 544 | + | |
| 545 | +(* Whether the value on top is a constant, and if so, which *) | |
| 546 | +proc topConst: (BOOL, I64); | |
| 547 | +begin | |
| 548 | + return stack[depth - 1].loc = lConst, stack[depth - 1].val; | |
| 549 | +end; | |
| 550 | + | |
| 551 | +proc resetStack*(d, p: INT); | |
| 552 | +var r: INT; | |
| 553 | +begin | |
| 554 | + depth := d; | |
| 555 | + pushed := p; | |
| 556 | + for r := 0 to NumRegs - 1 do owner[r] := -1; end; | |
| 557 | +end; | |
| 558 | + | |
| 559 | +proc pushConst*(k: Kind; v: I64); | |
| 560 | +begin | |
| 561 | + newEntry(k, lConst, v); | |
| 562 | +end; | |
| 563 | + | |
| 564 | +(* The register of the word at a, a variable in registers: the variable's | |
| 565 | + own, or for a slice its pointer's (off 0) or its length's (off 8) *) | |
| 566 | +proc regAt(a: Adr): INT; | |
| 567 | +var r: INT; | |
| 568 | +begin | |
| 569 | + if a.off = 0 then r := a.chunk; else r := a.hi; end; | |
| 570 | + if r >= XmmVar then r := r - XmmVar; end; (* an xmm register, XVarRegs *) | |
| 571 | + return r; | |
| 572 | +end; | |
| 573 | + | |
| 574 | +(* The memory a names. An address on the stack is popped, and is in its | |
| 575 | + register still; one in a slot is loaded into r. *) | |
| 576 | +proc place(a: Adr; r: INT): X.Mem; | |
| 577 | +var m: X.Mem; ra: INT; o: I64; | |
| 578 | +begin | |
| 579 | + if a.base = bReg then Diag.fatal("internal error: a register has no address"); end; | |
| 580 | + match a.base of | |
| 581 | + | bFrame: return X.mem(RBP, a.off); | |
| 582 | + | bGlobal: return X.memRip(a.chunk, INT(a.off)); | |
| 583 | + | bStack: | |
| 584 | + if stack[depth - 1].loc = lAdr then | |
| 585 | + m := adrMem(depth - 1, a.off); | |
| 586 | + drop(); | |
| 587 | + return m; | |
| 588 | + elsif stack[depth - 1].loc = lIdx then | |
| 589 | + m := stack[depth - 1].m; | |
| 590 | + m.disp := m.disp + INT(a.off); | |
| 591 | + drop(); | |
| 592 | + return m; | |
| 593 | + end; | |
| 594 | + (* an offset noted on the address is the operand's, as | |
| 595 | + p^.f + 8: [r8 + 8] *) | |
| 596 | + if fits32(a.off + stack[depth - 1].off) then o := takeOff(depth - 1); else o := 0; end; | |
| 597 | + ra := peek(depth - 1); | |
| 598 | + drop(); | |
| 599 | + return X.mem(ra, a.off + o); | |
| 600 | + | bPtr: return X.mem(a.chunk, a.off); | |
| 601 | + | else: (* bSlot: the address is in the frame slot a.chunk *) | |
| 602 | + X.load(r, X.mem(RBP, a.chunk), 8, false); | |
| 603 | + return X.mem(r, a.off); | |
| 604 | + end; | |
| 605 | +end; | |
| 606 | + | |
| 607 | +(* The address a names, on top of the stack *) | |
| 608 | +proc pushAdr*(a: Adr); | |
| 609 | +var m: X.Mem; r: INT; | |
| 610 | +begin | |
| 611 | + if (a.base = bStack) and (a.off = 0) then return; end; (* it is on top already *) | |
| 612 | + if a.base in {bFrame, bGlobal} then | |
| 613 | + newEntry(kWord, lAdr, a.off); | |
| 614 | + if a.base = bGlobal then stack[depth - 1].chunk := a.chunk; end; | |
| 615 | + return; | |
| 616 | + end; | |
| 617 | + (* The order matters. With the address on the stack, place pops it and | |
| 618 | + must come first, so that target may reuse its register; otherwise | |
| 619 | + target comes first, since place may need r to load a slot's pointer. *) | |
| 620 | + if a.base = bStack then | |
| 621 | + m := place(a, -1); r := target(); | |
| 622 | + else | |
| 623 | + r := target(); m := place(a, r); | |
| 624 | + end; | |
| 625 | + (* an address that is already in r, [r], needs no instruction *) | |
| 626 | + if (m.base # r) or (m.index # X.NoIndex) or (m.disp # 0) then X.lea(r, m); end; | |
| 627 | + computed(kWord); | |
| 628 | +end; | |
| 629 | + | |
| 630 | +proc pushVar*(a: Adr; size: INT; signed: BOOL; k: Kind); | |
| 631 | +var m: X.Mem; r: INT; | |
| 632 | +begin | |
| 633 | + r := inPlace; | |
| 634 | + inPlace := -1; | |
| 635 | + if r >= XmmVar then r := r - XmmVar; end; | |
| 636 | + if (a.base = bReg) and (regAt(a) = r) then | |
| 637 | + newEntry(k, lInto, r); | |
| 638 | + return; | |
| 639 | + end; | |
| 640 | + if a.base = bReg then | |
| 641 | + newEntry(k, lHeld, regAt(a)); | |
| 642 | + return; | |
| 643 | + end; | |
| 644 | + (* a variable in the frame or a chunk, or at an address only noted, is | |
| 645 | + only noted too: read where it is used, as an operand if it can be *) | |
| 646 | + if (a.base in {bFrame, bGlobal, bPtr}) or (a.base = bStack) and (stack[depth - 1].loc in {lAdr, lIdx}) then | |
| 647 | + m := place(a, -1); | |
| 648 | + newMem(lMem, m, k, size, signed); | |
| 649 | + return; | |
| 650 | + end; | |
| 651 | + (* in the order pushAdr explains *) | |
| 652 | + if a.base = bStack then | |
| 653 | + m := place(a, -1); r := target(); | |
| 654 | + else | |
| 655 | + r := target(); m := place(a, r); | |
| 656 | + end; | |
| 657 | + if k in {kF32, kF64} then X.movsXM(xTarget(), m, size = 4); | |
| 658 | + else X.load(r, m, size, signed); end; | |
| 659 | + computed(k); | |
| 660 | +end; | |
| 661 | + | |
| 662 | +proc pushProc*(c: INT; ext: BOOL); | |
| 663 | +var r: INT; | |
| 664 | +begin | |
| 665 | + r := target(); | |
| 666 | + if ext then X.load(r, X.memRip(c, 0), 8, false); | |
| 667 | + else X.lea(r, X.memRip(c, 0)); end; | |
| 668 | + computed(kWord); | |
| 669 | +end; | |
| 670 | + | |
| 671 | +(* Where xmm register r is in XVarRegs *) | |
| 672 | +proc xIndex(r: INT): INT; | |
| 673 | +begin | |
| 674 | + if r >= XVarRegs[0] then return r - XVarRegs[0]; end; | |
| 675 | + return r - XVarRegs[4] + 4; | |
| 676 | +end; | |
| 677 | + | |
| 678 | +(* The slot of the variable in xmm register x *) | |
| 679 | +proc homeOf(x: INT): X.Mem; | |
| 680 | +begin | |
| 681 | + return X.mem(RBP, homes[xIndex(x + XmmVar)]); | |
| 682 | +end; | |
| 683 | + | |
| 684 | +(* Pops a value into the variable at a; an address on the stack is under | |
| 685 | + the value. *) | |
| 686 | +proc popVar*(a: Adr; size: INT); | |
| 687 | +var r: INT; v: I64; c, fl: BOOL; | |
| 688 | +begin | |
| 689 | + if (a.base = bReg) and isReal(depth - 1) then | |
| 690 | + xValueTo(depth - 1, regAt(a)); | |
| 691 | + drop(); | |
| 692 | + X.movsMX(homeOf(regAt(a)), regAt(a), false); (* written through, as xHome says *) | |
| 693 | + return; | |
| 694 | + end; | |
| 695 | + if a.base = bReg then | |
| 696 | + c, v := topConst(); | |
| 697 | + if c then X.movRI(regAt(a), v); | |
| 698 | + elsif peek(depth - 1) # regAt(a) then X.movRR(regAt(a), peek(depth - 1)); end; (* else it is there, lInto *) | |
| 699 | + drop(); | |
| 700 | + return; | |
| 701 | + end; | |
| 702 | + c, v := topConst(); | |
| 703 | + if c and fits32(v) then | |
| 704 | + drop(); | |
| 705 | + X.storeI(place(a, RCX), INT(v), size); | |
| 706 | + return; | |
| 707 | + end; | |
| 708 | + if c then r := RAX; X.movRI(RAX, v); | |
| 709 | + else r := peek(depth - 1); end; | |
| 710 | + fl := (stack[depth - 1].loc # lConst) and isReal(depth - 1); | |
| 711 | + drop(); | |
| 712 | + if fl then X.movsMX(place(a, RCX), r, size = 4); | |
| 713 | + else X.store(place(a, RCX), r, size); end; | |
| 714 | +end; | |
| 715 | + | |
| 716 | +(* Register src to dst, an xmm register's if fl *) | |
| 717 | +proc movReg(fl: BOOL; dst, src: INT); | |
| 718 | +begin | |
| 719 | + if fl then X.movX(dst, src); else X.movRR(dst, src); end; | |
| 720 | +end; | |
| 721 | + | |
| 722 | +(* The two values on top, of one kind, change places. *) | |
| 723 | +proc swap*; | |
| 724 | +var e: Entry; x, y, t: INT; rx, ry, fl: BOOL; | |
| 725 | +begin | |
| 726 | + x := depth - 2; y := depth - 1; | |
| 727 | + (* a value noted at an address that uses its register is worked out, | |
| 728 | + so that its register moves with it *) | |
| 729 | + if stack[x].loc in {lIdx, lMem} then _ := loaded(x); end; | |
| 730 | + if stack[y].loc in {lIdx, lMem} then _ := loaded(y); end; | |
| 731 | + rx := stack[x].loc = lReg; | |
| 732 | + ry := stack[y].loc = lReg; | |
| 733 | + fl := isReal(y); | |
| 734 | + if fl then t := 0; else t := RAX; end; (* xmm0, or rax *) | |
| 735 | + if rx and ry then | |
| 736 | + movReg(fl, t, regOf(x)); | |
| 737 | + movReg(fl, regOf(x), regOf(y)); | |
| 738 | + movReg(fl, regOf(y), t); | |
| 739 | + elsif rx then | |
| 740 | + owner[x mod NumRegs] := -1; | |
| 741 | + movReg(fl, claim(y, stack[y].k), regOf(x)); | |
| 742 | + elsif ry then | |
| 743 | + owner[y mod NumRegs] := -1; | |
| 744 | + movReg(fl, claim(x, stack[x].k), regOf(y)); | |
| 745 | + end; | |
| 746 | + e := stack[x]; stack[x] := stack[y]; stack[y] := e; | |
| 747 | +end; | |
| 748 | + | |
| 749 | +(* Copying *) | |
| 750 | + | |
| 751 | +(* The most that copyBytes and zeroBytes do with moves, in place of rep | |
| 752 | + movsb and rep stosb, which take some twenty cycles to start, longer | |
| 753 | + than the few moves take. *) | |
| 754 | +const MaxMoves = 256; | |
| 755 | + | |
| 756 | +(* Up to this size a copy goes as words: a record just made field by | |
| 757 | + field is read back by loads as wide as its stores, and a 16-byte | |
| 758 | + load across two 8-byte stores waits for them to reach the cache. *) | |
| 759 | +const MaxWordMoves = 64; | |
| 760 | + | |
| 761 | +(* size bytes from [rsi] to [rdi], through xmm0 and rcx; rsi and rdi are | |
| 762 | + left as they fall. Up to MaxMoves bytes, as a record or a small array | |
| 763 | + mostly is, they go as moves: of 16 bytes from MaxWordMoves up, then | |
| 764 | + of 8, 4, 2 and 1. *) | |
| 765 | +proc copyBytes(size: I64); | |
| 766 | +var o, n: I64; | |
| 767 | +begin | |
| 768 | + if size > MaxMoves then | |
| 769 | + X.movRI(RCX, size); | |
| 770 | + X.repMovsb(); | |
| 771 | + return; | |
| 772 | + end; | |
| 773 | + o := 0; | |
| 774 | + while (size > MaxWordMoves) and (o + 16 <= size) do | |
| 775 | + X.movdquXM(0, X.mem(RSI, o)); | |
| 776 | + X.movdquMX(X.mem(RDI, o), 0); | |
| 777 | + INC(o, 16); | |
| 778 | + end; | |
| 779 | + while o < size do | |
| 780 | + n := 8; | |
| 781 | + while n > size - o do n := n div 2; end; | |
| 782 | + X.load(RCX, X.mem(RSI, o), INT(n), false); | |
| 783 | + X.store(X.mem(RDI, o), RCX, INT(n)); | |
| 784 | + INC(o, n); | |
| 785 | + end; | |
| 786 | +end; | |
| 787 | + | |
| 788 | +(* Pops a source's address, and a destination's under it, and copies | |
| 789 | + size bytes to off bytes past the destination. *) | |
| 790 | +proc copyBlock*(size, off: I64); | |
| 791 | +begin | |
| 792 | + clobberArgs(); | |
| 793 | + popTo(RSI); | |
| 794 | + popTo(RDI); | |
| 795 | + if off # 0 then X.lea(RDI, X.mem(RDI, off)); end; | |
| 796 | + copyBytes(size); | |
| 797 | +end; | |
| 798 | + | |
| 799 | +(* Pops a source's address, and copies size bytes to a: for a slice in | |
| 800 | + registers, its two words into them. *) | |
| 801 | +proc copyTo*(a: Adr; size: I64); | |
| 802 | +begin | |
| 803 | + clobberArgs(); | |
| 804 | + popTo(RSI); | |
| 805 | + if a.base = bReg then | |
| 806 | + X.load(a.chunk, X.mem(RSI, 0), 8, false); | |
| 807 | + X.load(a.hi, X.mem(RSI, 8), 8, false); | |
| 808 | + return; | |
| 809 | + end; | |
| 810 | + X.lea(RDI, place(a, RDI)); | |
| 811 | + copyBytes(size); | |
| 812 | +end; | |
| 813 | + | |
| 814 | +(* size zero bytes at [rdi], through xmm0 and rax; rdi is left as it | |
| 815 | + falls. Up to MaxMoves bytes they go as stores of 16, then of 8, 4, 2 | |
| 816 | + and 1 bytes, as copyBytes does. *) | |
| 817 | +proc zeroBytes(size: I64); | |
| 818 | +var o, n: I64; | |
| 819 | +begin | |
| 820 | + if size > MaxMoves then | |
| 821 | + X.movRI(RAX, 0); | |
| 822 | + X.movRI(RCX, size); | |
| 823 | + X.repStos(1); | |
| 824 | + return; | |
| 825 | + end; | |
| 826 | + o := 0; | |
| 827 | + if size >= 16 then X.pxorXX(0); end; | |
| 828 | + while o + 16 <= size do | |
| 829 | + X.movdquMX(X.mem(RDI, o), 0); | |
| 830 | + INC(o, 16); | |
| 831 | + end; | |
| 832 | + if o < size then X.movRI(RAX, 0); end; | |
| 833 | + while o < size do | |
| 834 | + n := 8; | |
| 835 | + while n > size - o do n := n div 2; end; | |
| 836 | + X.store(X.mem(RDI, o), RAX, INT(n)); | |
| 837 | + INC(o, n); | |
| 838 | + end; | |
| 839 | +end; | |
| 840 | + | |
| 841 | +proc zeroVar*(a: Adr; size: I64); | |
| 842 | +begin | |
| 843 | + clobberArgs(); | |
| 844 | + X.lea(RDI, place(a, RDI)); | |
| 845 | + zeroBytes(size); | |
| 846 | +end; | |
| 847 | + | |
| 848 | +(* Operations *) | |
| 849 | + | |
| 850 | +(* n if v is 2^n, which is 0 for 1; -1 if v isn't a power of two *) | |
| 851 | +proc exactLog2(v: I64): INT; | |
| 852 | +var n: INT; | |
| 853 | +begin | |
| 854 | + n := 0; | |
| 855 | + while (v > 1) and ((v & 1) = 0) do v := v shr 1; INC(n); end; | |
| 856 | + if v # 1 then n := -1; end; | |
| 857 | + return n; | |
| 858 | +end; | |
| 859 | + | |
| 860 | +(* r times v, a size: by a shift where v is a power of two, as it | |
| 861 | + mostly is, since a shift takes one cycle and imul three *) | |
| 862 | +proc mulI(r: INT; v: I64); | |
| 863 | +begin | |
| 864 | + if exactLog2(v) > 0 then X.shiftI(X.shShl, r, exactLog2(v)); | |
| 865 | + elsif v # 1 then X.imulI(r, INT(v)); end; | |
| 866 | +end; | |
| 867 | + | |
| 868 | +(* r compared with v, through rdx if v needs more than 32 bits; with | |
| 869 | + zero by test, which sets the flags as cmp would and is shorter *) | |
| 870 | +proc cmpI(r: INT; v: I64); | |
| 871 | +begin | |
| 872 | + if v = 0 then X.testRR(r, r); | |
| 873 | + elsif fits32(v) then X.aluI(X.aluCmp, r, INT(v)); | |
| 874 | + else X.movRI(RDX, v); X.alu(X.aluCmp, r, RDX); end; | |
| 875 | +end; | |
| 876 | + | |
| 877 | +(* r div or mod 2^k, 1 <= k <= 62, with shifts: div truncates towards | |
| 878 | + zero, so a negative dividend first gets 2^k - 1 added (the bias). *) | |
| 879 | +proc divPow2(sy: Scan.Sym; r, k: INT; unsign: BOOL); | |
| 880 | +begin | |
| 881 | + (* unsigned: div shifts right; mod keeps the low k bits, by shifting | |
| 882 | + them up to the top and back down *) | |
| 883 | + if unsign then | |
| 884 | + if sy = Scan.sDiv then X.shiftI(X.shShr, r, k); | |
| 885 | + else X.shiftI(X.shShl, r, 64 - k); X.shiftI(X.shShr, r, 64 - k); end; | |
| 886 | + return; | |
| 887 | + end; | |
| 888 | + (* signed: sar 63 makes the sign into all ones or all zeros, and shr | |
| 889 | + 64 - k keeps k of them, the bias 2^k - 1 or 0; with it added, sar k | |
| 890 | + rounds towards zero. The remainder is r less the quotient times 2^k. *) | |
| 891 | + X.movRR(RCX, r); | |
| 892 | + X.shiftI(X.shSar, RCX, 63); | |
| 893 | + X.shiftI(X.shShr, RCX, 64 - k); | |
| 894 | + X.alu(X.aluAdd, RCX, r); | |
| 895 | + X.shiftI(X.shSar, RCX, k); | |
| 896 | + if sy = Scan.sDiv then X.movRR(r, RCX); | |
| 897 | + else X.shiftI(X.shShl, RCX, k); X.alu(X.aluSub, r, RCX); end; | |
| 898 | +end; | |
| 899 | + | |
| 900 | +(* Before rax div y, of a signed type of bits < 64: its smallest value | |
| 901 | + divided by -1 overflows it, which must stop the program as it does for | |
| 902 | + an I64 (report section 6.1). idiv on the 64-bit values wouldn't fault, | |
| 903 | + so then rax is made the smallest I64, and it does. *) | |
| 904 | +proc divTrap(bits, y: INT); | |
| 905 | +var L: INT; | |
| 906 | +begin | |
| 907 | + L := NoLabel; | |
| 908 | + cmpI(y, -1); | |
| 909 | + X.jcc(X.ccNE, L); | |
| 910 | + cmpI(RAX, I64(-1) shl (bits - 1)); | |
| 911 | + X.jcc(X.ccNE, L); | |
| 912 | + X.shiftI(X.shShl, RAX, 64 - bits); | |
| 913 | + X.fixLink(L); | |
| 914 | +end; | |
| 915 | + | |
| 916 | +(* Whether the value on top is a word in memory that can be the right | |
| 917 | + operand of an instruction as it is: add r, [m] *) | |
| 918 | +proc topInMem: BOOL; | |
| 919 | +begin | |
| 920 | + return (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8); | |
| 921 | +end; | |
| 922 | + | |
| 923 | +(* The right operand of the two values on top, popped, in register y: | |
| 924 | + its own, or rcx for a constant; the left one stays, in its register, | |
| 925 | + which is returned with y. *) | |
| 926 | +proc operands: (INT, INT); | |
| 927 | +var v: I64; c: BOOL; y: INT; | |
| 928 | +begin | |
| 929 | + c, v := topConst(); | |
| 930 | + if c then X.movRI(RCX, v); y := RCX; else y := peek(depth - 1); end; | |
| 931 | + drop(); | |
| 932 | + return top(), y; | |
| 933 | +end; | |
| 934 | + | |
| 935 | +(* The two reals on top, the left op the right, worked out in the left's | |
| 936 | + xmm register. The right is taken from memory where it is, as a | |
| 937 | + constant, a variable or a spilled value is: mulsd xmm8, [rbp - 16]. *) | |
| 938 | +proc realOp(sy: Scan.Sym; k: Kind); | |
| 939 | +var f: BOOL; c, x, y: INT; m: X.Mem; | |
| 940 | +begin | |
| 941 | + f := k = kF32; | |
| 942 | + match sy of | |
| 943 | + | sPlus: c := 58H; (* addsd, or addss for an F32 *) | |
| 944 | + | sMinus: c := 5CH; (* subsd, subss *) | |
| 945 | + | sTimes: c := 59H; (* mulsd, mulss *) | |
| 946 | + | else: c := 5EH; (* divsd, divss *) | |
| 947 | + end; | |
| 948 | + if stack[depth - 1].loc in {lConst, lMem, lFrame} then | |
| 949 | + match stack[depth - 1].loc of | |
| 950 | + | lConst: m := realConst(stack[depth - 1].val); | |
| 951 | + | lMem: m := stack[depth - 1].m; | |
| 952 | + | else: m := X.mem(RBP, stack[depth - 1].val); | |
| 953 | + end; | |
| 954 | + drop(); | |
| 955 | + X.sseM(c, top(), m, f); | |
| 956 | + else | |
| 957 | + y := peek(depth - 1); | |
| 958 | + drop(); | |
| 959 | + x := top(); | |
| 960 | + X.sse(c, x, y, f); | |
| 961 | + end; | |
| 962 | +end; | |
| 963 | + | |
| 964 | +(* The group 1 operation of +, -, &, | and xor *) | |
| 965 | +proc aluOf(sy: Scan.Sym): INT; | |
| 966 | +begin | |
| 967 | + match sy of | |
| 968 | + | sPlus: return X.aluAdd; | |
| 969 | + | sMinus: return X.aluSub; | |
| 970 | + | sAmp: return X.aluAnd; | |
| 971 | + | sBar: return X.aluOr; | |
| 972 | + | else: return X.aluXor; | |
| 973 | + end; | |
| 974 | +end; | |
| 975 | + | |
| 976 | +const | |
| 977 | + (* the condition codes of the comparisons, from sEql: = # < <= > >= *) | |
| 978 | + CcSigned: [6]INT = [X.ccE, X.ccNE, X.ccL, X.ccLE, X.ccG, X.ccGE]; | |
| 979 | + CcUnsigned: [6]INT = [X.ccE, X.ccNE, X.ccB, X.ccBE, X.ccA, X.ccAE]; | |
| 980 | + | |
| 981 | +(* The two values on top, the left op the right, an integer of size | |
| 982 | + bytes, signed or not, brought back to its width. *) | |
| 983 | +proc op*(sy: Scan.Sym; k: Kind; size: INT; signed: BOOL); | |
| 984 | +var v, o: I64; r, y, sh, bits: INT; c: BOOL; m: X.Mem; | |
| 985 | +begin | |
| 986 | + if k in {kF32, kF64} then realOp(sy, k); return; end; | |
| 987 | + bits := size * 8; | |
| 988 | + (* a constant on the left of an operation whose order doesn't matter | |
| 989 | + goes right, where it can be an immediate or a shift *) | |
| 990 | + if (((sy = Scan.sPlus) or (sy = Scan.sTimes) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) and (stack[depth - 2].loc = lConst) | |
| 991 | + and (stack[depth - 1].loc # lConst) then swap(); end; | |
| 992 | + c, v := topConst(); | |
| 993 | + (* A word's offset (see Entry) is carried through + and -, and | |
| 994 | + through * by a constant, as (x + a) * k is x * k + a * k: the | |
| 995 | + operation is done on the registers, the offsets worked out here, | |
| 996 | + and put on the result. x + 1 is only noted. A value changed in | |
| 997 | + place, lInto, has no offset. *) | |
| 998 | + o := 0; | |
| 999 | + if (bits = 64) and (stack[depth - 2].loc # lInto) | |
| 1000 | + and ((((sy = Scan.sPlus) or (sy = Scan.sMinus))) or ((sy = Scan.sTimes) and c and fits32(v))) then | |
| 1001 | + o := takeOff(depth - 2); | |
| 1002 | + if sy = Scan.sTimes then o := o * v; | |
| 1003 | + elsif c and fits32(v) then | |
| 1004 | + if sy = Scan.sPlus then o := o + v; else o := o - v; end; | |
| 1005 | + drop(); | |
| 1006 | + if not (stack[depth - 1].loc in {lReg, lHeld}) then _ := top(); end; | |
| 1007 | + putOff(o); | |
| 1008 | + return; | |
| 1009 | + elsif sy = Scan.sPlus then o := o + takeOff(depth - 1); | |
| 1010 | + else o := o - takeOff(depth - 1); end; | |
| 1011 | + end; | |
| 1012 | + sh := -1; | |
| 1013 | + if c and (((sy = Scan.sTimes) or (sy = Scan.sDiv) or (sy = Scan.sMod))) then sh := exactLog2(v); end; | |
| 1014 | + if c and (sh >= 1) then | |
| 1015 | + drop(); | |
| 1016 | + r := top(); | |
| 1017 | + if sy = Scan.sTimes then X.shiftI(X.shShl, r, sh); else divPow2(sy, r, sh, not signed); end; | |
| 1018 | + elsif c and fits32(v) and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sBar) or (sy = Scan.sXor) or (sy = Scan.sAmp) or (sy = Scan.sTimes) or (sy = Scan.sShl) or (sy = Scan.sShr))) then | |
| 1019 | + drop(); | |
| 1020 | + r := top(); | |
| 1021 | + match sy of | |
| 1022 | + | sTimes: X.imulI(r, INT(v)); | |
| 1023 | + | sShl: X.shiftI(X.shShl, r, INT(v & I64(bits - 1))); | |
| 1024 | + | sShr: | |
| 1025 | + if signed then X.shiftI(X.shSar, r, INT(v & I64(bits - 1))); | |
| 1026 | + else X.shiftI(X.shShr, r, INT(v & I64(bits - 1))); end; | |
| 1027 | + | else: X.aluI(aluOf(sy), r, INT(v)); | |
| 1028 | + end; | |
| 1029 | + elsif topInMem() and (((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor))) then | |
| 1030 | + m := stack[depth - 1].m; | |
| 1031 | + drop(); | |
| 1032 | + r := top(); | |
| 1033 | + X.aluM(aluOf(sy), r, m); | |
| 1034 | + else | |
| 1035 | + r, y := operands(); | |
| 1036 | + if ((sy = Scan.sPlus) or (sy = Scan.sMinus) or (sy = Scan.sAmp) or (sy = Scan.sBar) or (sy = Scan.sXor)) then | |
| 1037 | + X.alu(aluOf(sy), r, y); | |
| 1038 | + elsif sy = Scan.sTimes then | |
| 1039 | + X.imul(r, y); | |
| 1040 | + elsif ((sy = Scan.sDiv) or (sy = Scan.sMod)) then | |
| 1041 | + X.movRR(RAX, r); | |
| 1042 | + if not signed then | |
| 1043 | + X.movRI(RDX, 0); X.unary(6, y); (* div *) | |
| 1044 | + else | |
| 1045 | + (* only -1 can make the division overflow, so a constant | |
| 1046 | + divisor other than -1 needs no check *) | |
| 1047 | + if (bits < 64) and (not c or (v = -1)) then divTrap(bits, y); end; | |
| 1048 | + X.cqo(); X.unary(7, y); (* idiv, of rdx:rax, which cqo made from rax *) | |
| 1049 | + end; | |
| 1050 | + if sy = Scan.sDiv then X.movRR(r, RAX); else X.movRR(r, RDX); end; | |
| 1051 | + else (* sShl, sShr *) | |
| 1052 | + (* the count modulo the width: the machine takes it modulo 64 *) | |
| 1053 | + if y # RCX then X.movRR(RCX, y); end; | |
| 1054 | + if bits < 64 then X.aluI(X.aluAnd, RCX, bits - 1); end; | |
| 1055 | + if sy = Scan.sShl then X.shift(X.shShl, r); | |
| 1056 | + elsif signed then X.shift(X.shSar, r); | |
| 1057 | + else X.shift(X.shShr, r); end; | |
| 1058 | + end; | |
| 1059 | + end; | |
| 1060 | + if bits < 64 then X.extend(r, size, signed); end; | |
| 1061 | + stack[depth - 1].k := k; | |
| 1062 | + if o # 0 then putOff(o); end; | |
| 1063 | +end; | |
| 1064 | + | |
| 1065 | +(* ROL or ROR, by left, of the two values on top: an unsigned integer of | |
| 1066 | + size bytes, under the count (report section 6.1). x86 takes the count | |
| 1067 | + of rol and ror modulo 32, or 64 for a quadword, and a byte's or a | |
| 1068 | + word's rotation then goes round modulo 8 or 16 itself: as 8 and 16 | |
| 1069 | + divide 32, that is the count modulo the width, as the report has it, | |
| 1070 | + with no mask of the count's own, which a shift needs. A constant | |
| 1071 | + count is cut to the width here, an imm8; 0 rotates nothing. The bits | |
| 1072 | + above the width are zero and stay so: rol al and rol ax leave them, | |
| 1073 | + rol eax clears them. *) | |
| 1074 | +proc rotate*(left: BOOL; size: INT); | |
| 1075 | +var c: BOOL; v: I64; r, y, op: INT; | |
| 1076 | +begin | |
| 1077 | + if left then op := X.shRol; else op := X.shRor; end; | |
| 1078 | + c, v := topConst(); | |
| 1079 | + if c then | |
| 1080 | + drop(); | |
| 1081 | + r := top(); | |
| 1082 | + v := v & I64(size * 8 - 1); | |
| 1083 | + if v # 0 then X.rotate(op, r, size, INT(v)); end; | |
| 1084 | + else | |
| 1085 | + r, y := operands(); | |
| 1086 | + if y # RCX then X.movRR(RCX, y); end; | |
| 1087 | + X.rotate(op, r, size, -1); | |
| 1088 | + end; | |
| 1089 | + stack[depth - 1].k := kWord; | |
| 1090 | +end; | |
| 1091 | + | |
| 1092 | +(* CLZ, if leading, or CTZ of the unsigned integer of size bytes on top | |
| 1093 | + (report section 6.1). bsr finds the place h of its highest one bit, | |
| 1094 | + and the zeros above it are the width less 1 less h; bsf finds the | |
| 1095 | + place of its lowest, which is the zeros below it. Neither finds a | |
| 1096 | + place in 0, the one value with no one bit, and both say so by ZF: | |
| 1097 | + then the count is the whole width, as if h were -1, or the lowest | |
| 1098 | + one at the width. The bits above size bytes are zero, so a scan of | |
| 1099 | + all 64 sees the value's own. The baseline x86-64 lacks lzcnt and | |
| 1100 | + tzcnt, which would count 0 by themselves. For a U32: | |
| 1101 | + | |
| 1102 | + bsr r, r bsf r, r | |
| 1103 | + jnz L jnz L | |
| 1104 | + mov r, -1 mov r, 32 | |
| 1105 | + L: neg r L: | |
| 1106 | + add r, 31 *) | |
| 1107 | +proc countZeros*(leading: BOOL; size: INT); | |
| 1108 | +var r, w, L: INT; | |
| 1109 | +begin | |
| 1110 | + r := top(); | |
| 1111 | + w := size * 8; | |
| 1112 | + L := NoLabel; | |
| 1113 | + X.bitScan(leading, r); | |
| 1114 | + X.jcc(X.ccNE, L); | |
| 1115 | + if leading then X.movRI(r, -1); else X.movRI(r, w); end; | |
| 1116 | + X.fixLink(L); | |
| 1117 | + if leading then | |
| 1118 | + X.unary(3, r); (* neg *) | |
| 1119 | + X.aluI(X.aluAdd, r, w - 1); | |
| 1120 | + end; | |
| 1121 | + stack[depth - 1].k := kWord; | |
| 1122 | +end; | |
| 1123 | + | |
| 1124 | +(* POPCOUNT of the integer or set on top (report section 6.1), counted | |
| 1125 | + as lib/Bytes.raz counts bytes, without a loop: each two bits are | |
| 1126 | + made the sum of their ones, then each four, then each byte, and a | |
| 1127 | + multiplication by 0101...H adds the eight bytes' sums into the top | |
| 1128 | + byte. The baseline x86-64 lacks popcnt. rax and rdx are | |
| 1129 | + free between the stack machine's operations. | |
| 1130 | + | |
| 1131 | + mov rax, r; shr rax, 1; mov rdx, 5555...H; and rax, rdx; sub r, rax | |
| 1132 | + mov rax, r; shr rax, 2; mov rdx, 3333...H; and rax, rdx; and r, rdx; add r, rax | |
| 1133 | + mov rax, r; shr rax, 4; add r, rax; mov rdx, 0F0F...H; and r, rdx | |
| 1134 | + mov rdx, 0101...H; imul r, rdx; shr r, 56 *) | |
| 1135 | +proc popCount*; | |
| 1136 | +var r: INT; | |
| 1137 | +begin | |
| 1138 | + r := top(); | |
| 1139 | + (* each two bits: their value less its upper bit, which is its ones *) | |
| 1140 | + X.movRR(RAX, r); | |
| 1141 | + X.shiftI(X.shShr, RAX, 1); | |
| 1142 | + X.movRI(RDX, 5555555555555555H); | |
| 1143 | + X.alu(X.aluAnd, RAX, RDX); | |
| 1144 | + X.alu(X.aluSub, r, RAX); | |
| 1145 | + (* each four bits: the sums of its two pairs *) | |
| 1146 | + X.movRR(RAX, r); | |
| 1147 | + X.shiftI(X.shShr, RAX, 2); | |
| 1148 | + X.movRI(RDX, 3333333333333333H); | |
| 1149 | + X.alu(X.aluAnd, RAX, RDX); | |
| 1150 | + X.alu(X.aluAnd, r, RDX); | |
| 1151 | + X.alu(X.aluAdd, r, RAX); | |
| 1152 | + (* each byte: the sums of its two halves, at most 8, which fits four bits *) | |
| 1153 | + X.movRR(RAX, r); | |
| 1154 | + X.shiftI(X.shShr, RAX, 4); | |
| 1155 | + X.alu(X.aluAdd, r, RAX); | |
| 1156 | + X.movRI(RDX, 0F0F0F0F0F0F0F0FH); | |
| 1157 | + X.alu(X.aluAnd, r, RDX); | |
| 1158 | + (* the top byte of r times 0101...H is the sum of all eight *) | |
| 1159 | + X.movRI(RDX, 0101010101010101H); | |
| 1160 | + X.imul(r, RDX); | |
| 1161 | + X.shiftI(X.shShr, r, 56); | |
| 1162 | + stack[depth - 1].k := kWord; | |
| 1163 | +end; | |
| 1164 | + | |
| 1165 | +(* ABS of the real, or the signed integer of size bytes, on top (report | |
| 1166 | + section 6.1); an unsigned one needs no code. A real's sign bit is | |
| 1167 | + cleared, in rax, as neg flips it. An integer is negated, and its copy | |
| 1168 | + in rax taken back if the negation is negative (SF), as it is for a | |
| 1169 | + positive one. The smallest value negates to itself, negative, and so | |
| 1170 | + gives itself, as -x does, with no branch and no check. A narrow | |
| 1171 | + value is negated on all 64 bits, where its smallest is not the | |
| 1172 | + smallest, so it is cut back to its width after, as neg does: | |
| 1173 | + -128 in an I8 negates to 128, which is -128 again in 8 bits. | |
| 1174 | + | |
| 1175 | + movq rax, x; btr rax, 63; movq x, rax (F64; 31 for an F32) | |
| 1176 | + mov rax, r; neg r; cmovs r, rax (I64; then movsx r, r for a narrow one) *) | |
| 1177 | +proc abs*(k: Kind; size: INT); | |
| 1178 | +var r: INT; | |
| 1179 | +begin | |
| 1180 | + r := top(); | |
| 1181 | + if k in {kF32, kF64} then | |
| 1182 | + X.movqRX(RAX, r); | |
| 1183 | + if k = kF32 then X.btr(RAX, 31); else X.btr(RAX, 63); end; | |
| 1184 | + X.movqXR(r, RAX); | |
| 1185 | + else | |
| 1186 | + X.movRR(RAX, r); | |
| 1187 | + X.unary(3, r); (* neg *) | |
| 1188 | + X.cmov(X.ccS, r, RAX); | |
| 1189 | + if size < 8 then X.extend(r, size, true); end; | |
| 1190 | + end; | |
| 1191 | +end; | |
| 1192 | + | |
| 1193 | +(* The left op the right of the two values on top, as a BOOL *) | |
| 1194 | +proc compare*(sy: Scan.Sym; k: Kind; signed: BOOL); | |
| 1195 | +var f: BOOL; r, x, y, i: INT; v: I64; c: BOOL; m: X.Mem; | |
| 1196 | +begin | |
| 1197 | + i := INT(sy) - INT(Scan.sEql); | |
| 1198 | + if k in {kF32, kF64} then | |
| 1199 | + f := k = kF32; | |
| 1200 | + y := peek(depth - 1); | |
| 1201 | + drop(); | |
| 1202 | + x := peek(depth - 1); | |
| 1203 | + drop(); | |
| 1204 | + r := target(); (* the BOOL, in the left's integer register *) | |
| 1205 | + (* ucomis sets the flags as an unsigned comparison would, but for an | |
| 1206 | + unordered pair (a NaN) it sets zero, parity and carry all. So only | |
| 1207 | + "above" can be tested straight: it is false for NaN, as every | |
| 1208 | + comparison but # must be. a < b is made b > a. = also asks for | |
| 1209 | + no parity, as NaN = NaN sets the zero flag, and # asks for | |
| 1210 | + parity as well. *) | |
| 1211 | + match sy of | |
| 1212 | + | sEql: X.ucomis(x, y, f); X.setCC(X.ccE, r); X.setCC(X.ccNP, RCX); X.alu(X.aluAnd, r, RCX); | |
| 1213 | + | sNeq: X.ucomis(x, y, f); X.setCC(X.ccNE, r); X.setCC(X.ccP, RCX); X.alu(X.aluOr, r, RCX); | |
| 1214 | + | sLss: X.ucomis(y, x, f); X.setCC(X.ccA, r); | |
| 1215 | + | sLeq: X.ucomis(y, x, f); X.setCC(X.ccAE, r); | |
| 1216 | + | sGtr: X.ucomis(x, y, f); X.setCC(X.ccA, r); | |
| 1217 | + | else: X.ucomis(x, y, f); X.setCC(X.ccAE, r); | |
| 1218 | + end; | |
| 1219 | + computed(kWord); | |
| 1220 | + else | |
| 1221 | + c, v := topConst(); | |
| 1222 | + if c then | |
| 1223 | + drop(); r := top(); cmpI(r, v); | |
| 1224 | + elsif topInMem() then | |
| 1225 | + m := stack[depth - 1].m; drop(); r := top(); X.aluM(X.aluCmp, r, m); | |
| 1226 | + else | |
| 1227 | + r, y := operands(); X.alu(X.aluCmp, r, y); | |
| 1228 | + end; | |
| 1229 | + if signed then X.setCC(CcSigned[i], r); else X.setCC(CcUnsigned[i], r); end; | |
| 1230 | + end; | |
| 1231 | + stack[depth - 1].k := kWord; | |
| 1232 | +end; | |
| 1233 | + | |
| 1234 | +(* A real is negated by flipping its sign bit, in rax *) | |
| 1235 | +proc neg*(k: Kind; size: INT; signed: BOOL); | |
| 1236 | +var r: INT; | |
| 1237 | +begin | |
| 1238 | + r := top(); | |
| 1239 | + if k in {kF32, kF64} then | |
| 1240 | + X.movqRX(RAX, r); | |
| 1241 | + if k = kF32 then X.btc(RAX, 31); else X.btc(RAX, 63); end; | |
| 1242 | + X.movqXR(r, RAX); | |
| 1243 | + else | |
| 1244 | + X.unary(3, r); (* neg *) | |
| 1245 | + if size < 8 then X.extend(r, size, signed); end; | |
| 1246 | + end; | |
| 1247 | +end; | |
| 1248 | + | |
| 1249 | +proc complement*(size: INT; signed: BOOL); | |
| 1250 | +var r: INT; | |
| 1251 | +begin | |
| 1252 | + r := top(); | |
| 1253 | + X.unary(2, r); (* not *) | |
| 1254 | + if size < 8 then X.extend(r, size, signed); end; | |
| 1255 | +end; | |
| 1256 | + | |
| 1257 | +proc notBool*; | |
| 1258 | +begin | |
| 1259 | + X.aluI(X.aluXor, top(), 1); | |
| 1260 | +end; | |
| 1261 | + | |
| 1262 | +(* The integer on top, brought to size bytes *) | |
| 1263 | +proc narrow*(size: INT; signed: BOOL); | |
| 1264 | +var r: INT; | |
| 1265 | +begin | |
| 1266 | + r := top(); | |
| 1267 | + if size < 8 then X.extend(r, size, signed); end; | |
| 1268 | + stack[depth - 1].k := kWord; | |
| 1269 | +end; | |
| 1270 | + | |
| 1271 | +(* The integer on top, as a real of kind k; big if it may be a U64 above | |
| 1272 | + high(I64), which the machine would take as negative *) | |
| 1273 | +proc intToReal*(k: Kind; big: BOOL); | |
| 1274 | +var L1, L2, r, x: INT; f: BOOL; | |
| 1275 | +begin | |
| 1276 | + f := k = kF32; | |
| 1277 | + r := top(); | |
| 1278 | + x := regIn(k, depth - 1); (* its xmm register *) | |
| 1279 | + if big then | |
| 1280 | + (* above high(I64): halve, keeping the low bit, and double *) | |
| 1281 | + L1 := NoLabel; L2 := NoLabel; | |
| 1282 | + X.testRR(r, r); | |
| 1283 | + X.jcc(X.ccS, L1); | |
| 1284 | + X.cvtsi2s(x, r, f); | |
| 1285 | + X.jmp(L2); | |
| 1286 | + X.fixLink(L1); | |
| 1287 | + X.movRR(RCX, r); | |
| 1288 | + X.shiftI(X.shShr, RCX, 1); | |
| 1289 | + X.aluI(X.aluAnd, r, 1); | |
| 1290 | + X.alu(X.aluOr, RCX, r); | |
| 1291 | + X.cvtsi2s(x, RCX, f); | |
| 1292 | + X.sse(58H, x, x, f); (* adds it to itself: doubles *) | |
| 1293 | + X.fixLink(L2); | |
| 1294 | + else | |
| 1295 | + X.cvtsi2s(x, r, f); | |
| 1296 | + end; | |
| 1297 | + stack[depth - 1].k := k; | |
| 1298 | +end; | |
| 1299 | + | |
| 1300 | +(* The real of kind k on top, truncated to an integer *) | |
| 1301 | +proc realToInt*(k: Kind); | |
| 1302 | +var x: INT; | |
| 1303 | +begin | |
| 1304 | + x := top(); | |
| 1305 | + X.cvtts2si(regIn(kWord, depth - 1), x, k = kF32); | |
| 1306 | + stack[depth - 1].k := kWord; | |
| 1307 | +end; | |
| 1308 | + | |
| 1309 | +(* The real of kind k on top, as the other kind *) | |
| 1310 | +proc realToReal*(k: Kind); | |
| 1311 | +var x: INT; | |
| 1312 | +begin | |
| 1313 | + x := top(); | |
| 1314 | + X.sse(5AH, x, x, k = kF32); (* cvtss2sd, or cvtsd2ss *) | |
| 1315 | + if k = kF32 then stack[depth - 1].k := kF64; else stack[depth - 1].k := kF32; end; | |
| 1316 | +end; | |
| 1317 | + | |
| 1318 | +(* Whether the slices in the frame at a and b (the offsets of their | |
| 1319 | + pointers, the lengths 8 bytes on) are equal (or not, if not eq): the | |
| 1320 | + lengths, then the bytes, len shl shift of them, compared where the | |
| 1321 | + comparison stands. *) | |
| 1322 | +proc compareSeq*(a, b: I64; shift: INT; eq: BOOL); | |
| 1323 | +var differ, r: INT; | |
| 1324 | +begin | |
| 1325 | + clobberArgs(); | |
| 1326 | + r := target(); | |
| 1327 | + X.load(RCX, X.mem(RBP, b + 8), 8, false); | |
| 1328 | + X.aluM(X.aluCmp, RCX, X.mem(RBP, a + 8)); | |
| 1329 | + differ := NoLabel; | |
| 1330 | + X.jcc(X.ccNE, differ); | |
| 1331 | + X.load(RSI, X.mem(RBP, b), 8, false); | |
| 1332 | + X.load(RDI, X.mem(RBP, a), 8, false); | |
| 1333 | + if shift > 0 then X.shiftI(X.shShl, RCX, shift); end; | |
| 1334 | + (* repe cmpsb with rcx = 0 leaves the flags alone, so make them say | |
| 1335 | + equal first, which is right if there are no bytes *) | |
| 1336 | + X.alu(X.aluCmp, RCX, RCX); | |
| 1337 | + X.repeCmpsb(); | |
| 1338 | + X.fixLink(differ); | |
| 1339 | + if eq then X.setCC(X.ccE, r); else X.setCC(X.ccNE, r); end; | |
| 1340 | + computed(kWord); | |
| 1341 | +end; | |
| 1342 | + | |
| 1343 | +(* An error points at its message's length, an 8-byte word, with the | |
| 1344 | + text after it; nil is no error. Pops one, and makes the slice of its | |
| 1345 | + message at tmp, empty for nil. *) | |
| 1346 | +proc errorText*(tmp: I64); | |
| 1347 | +var L: INT; | |
| 1348 | +begin | |
| 1349 | + popTo(RAX); | |
| 1350 | + X.movRI(RCX, 0); | |
| 1351 | + L := NoLabel; | |
| 1352 | + X.testRR(RAX, RAX); | |
| 1353 | + X.jcc(X.ccE, L); | |
| 1354 | + X.load(RCX, X.mem(RAX, 0), 8, false); | |
| 1355 | + X.lea(RAX, X.mem(RAX, 8)); | |
| 1356 | + X.fixLink(L); | |
| 1357 | + X.store(X.mem(RBP, tmp), RAX, 8); | |
| 1358 | + X.store(X.mem(RBP, tmp + 8), RCX, 8); | |
| 1359 | +end; | |
| 1360 | + | |
| 1361 | +(* Pops a step and an integer variable's address, and adds the step to | |
| 1362 | + it, or takes it away. *) | |
| 1363 | +proc incDec*(size: INT; signed, dec: BOOL); | |
| 1364 | +var r, y, o: INT; v: I64; a: Adr; c: BOOL; | |
| 1365 | +begin | |
| 1366 | + c, v := topConst(); | |
| 1367 | + if dec then o := X.aluSub; else o := X.aluAdd; end; | |
| 1368 | + if c and fits32(v) then | |
| 1369 | + drop(); | |
| 1370 | + a.base := bStack; a.off := 0; | |
| 1371 | + X.aluMI(o, place(a, RCX), INT(v), size); | |
| 1372 | + return; | |
| 1373 | + end; | |
| 1374 | + r, y := operands(); | |
| 1375 | + drop(); | |
| 1376 | + X.load(RAX, X.mem(r, 0), size, signed); | |
| 1377 | + X.alu(o, RAX, y); | |
| 1378 | + X.store(X.mem(r, 0), RAX, size); | |
| 1379 | +end; | |
| 1380 | + | |
| 1381 | +(* INC or DEC of the variable in register r, by the value on top; a | |
| 1382 | + narrow one wraps at its width *) | |
| 1383 | +proc incDecReg*(r, size: INT; signed, dec: BOOL); | |
| 1384 | +var y, o: INT; v: I64; c: BOOL; | |
| 1385 | +begin | |
| 1386 | + if dec then o := X.aluSub; else o := X.aluAdd; end; | |
| 1387 | + c, v := topConst(); | |
| 1388 | + if c and fits32(v) then X.aluI(o, r, INT(v)); | |
| 1389 | + else y := top(); X.alu(o, r, y); end; | |
| 1390 | + drop(); | |
| 1391 | + if size < 8 then X.extend(r, size, signed); end; | |
| 1392 | +end; | |
| 1393 | + | |
| 1394 | +(* The pointer on top, which NEW got, to size zero bytes unless nil *) | |
| 1395 | +proc zeroNew*(size: I64); | |
| 1396 | +var L, r: INT; | |
| 1397 | +begin | |
| 1398 | + clobberArgs(); | |
| 1399 | + r := top(); | |
| 1400 | + L := NoLabel; | |
| 1401 | + X.testRR(r, r); | |
| 1402 | + X.jcc(X.ccE, L); | |
| 1403 | + X.movRR(RDI, r); | |
| 1404 | + X.movRI(RCX, size); | |
| 1405 | + X.movRI(RAX, 0); | |
| 1406 | + X.repStos(1); | |
| 1407 | + X.fixLink(L); | |
| 1408 | +end; | |
| 1409 | + | |
| 1410 | +(* CAS(p, old, new), XCHG(p, v) or XADD(p, v), from the stack: the | |
| 1411 | + result where p was *) | |
| 1412 | +proc atomic*(which: INT); | |
| 1413 | +var r, n: INT; | |
| 1414 | +begin | |
| 1415 | + if which = Sym.bCas then readMems(depth - 3); else readMems(depth - 2); end; (* what it writes may be one *) | |
| 1416 | + if which = Sym.bCas then n := top(); drop(); end; | |
| 1417 | + popTo(RAX); (* cmpxchg compares with rax, so CAS's old value goes there *) | |
| 1418 | + r := top(); | |
| 1419 | + match which of | |
| 1420 | + | Sym.bCas: X.lockCmpxchg(X.mem(r, 0), n); X.setCC(X.ccE, r); | |
| 1421 | + | Sym.bXchg: X.xchg(X.mem(r, 0), RAX); X.movRR(r, RAX); | |
| 1422 | + | else: X.lockXadd(X.mem(r, 0), RAX); X.movRR(r, RAX); | |
| 1423 | + end; | |
| 1424 | + stack[depth - 1].k := kWord; | |
| 1425 | +end; | |
| 1426 | + | |
| 1427 | +(* The system call and its arguments, n values on the stack. Linux takes | |
| 1428 | + the number in rax and the arguments in rdi, rsi, rdx, r10, r8 and r9, | |
| 1429 | + and the syscall instruction clobbers rcx and r11. Every value in a | |
| 1430 | + register is spilled first, as r8 to r11 are the stack's registers, | |
| 1431 | + and the arguments are loaded from their slots. *) | |
| 1432 | +proc sysCall*(n: INT); | |
| 1433 | +const SysRegs: [7]INT = [RAX, RDI, RSI, RDX, R10, R8, R9]; | |
| 1434 | +var i: INT; | |
| 1435 | +begin | |
| 1436 | + clobberArgs(); | |
| 1437 | + spillBelow(depth); | |
| 1438 | + for i := 0 to n - 1 do valueTo(depth - n + i, SysRegs[i]); end; | |
| 1439 | + for i := 1 to n do drop(); end; | |
| 1440 | + X.syscall(); | |
| 1441 | + X.movRR(target(), RAX); | |
| 1442 | + computed(kWord); | |
| 1443 | +end; | |
| 1444 | + | |
| 1445 | +proc fence*; | |
| 1446 | +begin | |
| 1447 | + X.mfence(); | |
| 1448 | +end; | |
| 1449 | + | |
| 1450 | +(* Jumps. A label is the chain of the jumps to it, until it is placed. *) | |
| 1451 | + | |
| 1452 | +proc jump*(var L: INT); | |
| 1453 | +begin | |
| 1454 | + X.jmp(L); | |
| 1455 | +end; | |
| 1456 | + | |
| 1457 | +(* Pops a BOOL, and jumps if it is when *) | |
| 1458 | +proc jumpIf*(var L: INT; when: BOOL); | |
| 1459 | +var r: INT; | |
| 1460 | +begin | |
| 1461 | + r := peek(depth - 1); | |
| 1462 | + drop(); | |
| 1463 | + X.testRR(r, r); | |
| 1464 | + if when then X.jcc(X.ccNE, L); else X.jcc(X.ccE, L); end; | |
| 1465 | +end; | |
| 1466 | + | |
| 1467 | +(* Pops two integers, and jumps if the left op the right *) | |
| 1468 | +proc jumpCmp*(sy: Scan.Sym; signed: BOOL; var L: INT); | |
| 1469 | +var r, y, i: INT; v: I64; c: BOOL; m: X.Mem; | |
| 1470 | +begin | |
| 1471 | + i := INT(sy) - INT(Scan.sEql); | |
| 1472 | + (* a comparison changes neither: a variable's register will do *) | |
| 1473 | + c, v := topConst(); | |
| 1474 | + if c then | |
| 1475 | + drop(); r := peek(depth - 1); cmpI(r, v); | |
| 1476 | + elsif topInMem() then | |
| 1477 | + m := stack[depth - 1].m; drop(); r := peek(depth - 1); X.aluM(X.aluCmp, r, m); | |
| 1478 | + else | |
| 1479 | + y := peek(depth - 1); drop(); r := peek(depth - 1); X.alu(X.aluCmp, r, y); | |
| 1480 | + end; | |
| 1481 | + drop(); | |
| 1482 | + if signed then X.jcc(CcSigned[i], L); else X.jcc(CcUnsigned[i], L); end; | |
| 1483 | +end; | |
| 1484 | + | |
| 1485 | +(* jumpCmp out of an expression, past code that may spill: the values | |
| 1486 | + under the two compared are spilled first, so that they are where the | |
| 1487 | + code after the landing place expects them, whichever way it came. In | |
| 1488 | + v + (a < b and f(x) = 0), a call to f spills v on the way through, so | |
| 1489 | + v must be in its slot too where a < b jumps past the call. *) | |
| 1490 | +proc jumpOut*(sy: Scan.Sym; signed: BOOL; var L: INT); | |
| 1491 | +begin | |
| 1492 | + spillBelow(depth - 2); | |
| 1493 | + jumpCmp(sy, signed, L); | |
| 1494 | +end; | |
| 1495 | + | |
| 1496 | +(* The two values on top, compared, as a BOOL: true unless notOp, the | |
| 1497 | + opposite comparison, holds. fj and tj are the chains of jumps, made | |
| 1498 | + earlier by and, or and not, that are taken when the whole condition is | |
| 1499 | + false and when it is true; they land here, on the 0 and the 1. *) | |
| 1500 | +proc condValue*(notOp: Scan.Sym; signed: BOOL; fj, tj: INT); | |
| 1501 | +var r, e: INT; | |
| 1502 | +begin | |
| 1503 | + jumpOut(notOp, signed, fj); | |
| 1504 | + X.fixLink(tj); | |
| 1505 | + r := target(); | |
| 1506 | + X.movRI(r, 1); | |
| 1507 | + e := NoLabel; | |
| 1508 | + X.jmp(e); | |
| 1509 | + X.fixLink(fj); | |
| 1510 | + X.movRI(r, 0); | |
| 1511 | + X.fixLink(e); | |
| 1512 | + computed(kWord); | |
| 1513 | +end; | |
| 1514 | + | |
| 1515 | +(* jumpBack, fix, fixTo and joinJumps are Gen's names for the encoder's, | |
| 1516 | + so that a port can place jumps its own way. fix(L) places label L here; | |
| 1517 | + fixTo(L, top) makes every jump on chain L go to the earlier place top; | |
| 1518 | + joinJumps makes one chain of two. *) | |
| 1519 | +proc jumpBack*(top: INT); | |
| 1520 | +begin | |
| 1521 | + X.jmpBack(top); | |
| 1522 | +end; | |
| 1523 | + | |
| 1524 | +proc fix*(L: INT); | |
| 1525 | +begin | |
| 1526 | + X.fixLink(L); | |
| 1527 | +end; | |
| 1528 | + | |
| 1529 | +(* A jump table, for a match on the integer in frame slot slot, whose | |
| 1530 | + labels run from lo up for n values. The selector less lo, compared | |
| 1531 | + unsigned (so below lo is above n too), is out of the table if it | |
| 1532 | + isn't below n, and goes to the else arm at elseAt, or, if there is | |
| 1533 | + none, to endL, the end of the match. Else it indexes a table of | |
| 1534 | + 4-byte offsets, each from the start of the lea, that follows the | |
| 1535 | + jump, and the code jumps to its start plus the entry: | |
| 1536 | + | |
| 1537 | + mov rax, [slot] | |
| 1538 | + sub rax, lo | |
| 1539 | + cmp rax, n - 1 | |
| 1540 | + ja else | |
| 1541 | + lea rcx, [rip + start] start: this very lea | |
| 1542 | + movsxd rax, [rcx + rax*4 + 17] | |
| 1543 | + add rax, rcx | |
| 1544 | + jmp rax | |
| 1545 | + table: .long arm0 - start, arm1 - start, ... | |
| 1546 | + | |
| 1547 | + The four instructions after the compare are 7, 5, 3 and 2 bytes, so | |
| 1548 | + the table is 17 bytes on, which jumpTable checks. It returns start; | |
| 1549 | + the caller then makes the n entries with tableEntry. *) | |
| 1550 | +proc jumpTable*(slot, lo: I64; n, elseAt: INT; var endL: INT): INT; | |
| 1551 | +const TableAt = 17; | |
| 1552 | +var start: INT; | |
| 1553 | +begin | |
| 1554 | + X.load(RAX, X.mem(RBP, slot), 8, false); | |
| 1555 | + if lo # 0 then X.aluI(X.aluSub, RAX, INT(lo)); end; | |
| 1556 | + X.aluI(X.aluCmp, RAX, n - 1); | |
| 1557 | + if elseAt # NoLabel then X.jccBack(X.ccA, elseAt); else X.jcc(X.ccA, endL); end; | |
| 1558 | + start := Chunk.pc; | |
| 1559 | + X.lea(RCX, X.here()); | |
| 1560 | + X.load(RAX, X.memIdx(RCX, RAX, 4, TableAt), 4, true); | |
| 1561 | + X.alu(X.aluAdd, RAX, RCX); | |
| 1562 | + X.jmpR(RAX); | |
| 1563 | + if Chunk.pc # start + TableAt then Diag.fatal("internal error: the jump table is not where it was said"); end; | |
| 1564 | + return start; | |
| 1565 | +end; | |
| 1566 | + | |
| 1567 | +(* The next entry of the table that jumpTable started at table: the | |
| 1568 | + code at target *) | |
| 1569 | +proc tableEntry*(table, target: INT); | |
| 1570 | +begin | |
| 1571 | + Chunk.put4(target - table); | |
| 1572 | +end; | |
| 1573 | + | |
| 1574 | +proc alignLoop*; | |
| 1575 | +begin | |
| 1576 | + X.align(32); | |
| 1577 | +end; | |
| 1578 | + | |
| 1579 | +proc joinJumps*(L1, L2: INT): INT; | |
| 1580 | +begin | |
| 1581 | + return X.joinLinks(L1, L2); | |
| 1582 | +end; | |
| 1583 | + | |
| 1584 | +proc fixTo*(L, top: INT); | |
| 1585 | +begin | |
| 1586 | + X.fixLinkTo(L, top); | |
| 1587 | +end; | |
| 1588 | + | |
| 1589 | +(* Checks *) | |
| 1590 | + | |
| 1591 | +const | |
| 1592 | + NoBase = -2; (* a Mem that is no memory *) | |
| 1593 | + ARegister = -3; (* a Mem that is no memory but a register, its index *) | |
| 1594 | + | |
| 1595 | +(* The name s, interned *) | |
| 1596 | +proc internStr(s: []const U8): INT; | |
| 1597 | +begin | |
| 1598 | + return Scan.intern(SYSTEM.PTR(s), LEN(s)); | |
| 1599 | +end; | |
| 1600 | + | |
| 1601 | +(* The program goes on if the condition cc holds, and otherwise stops | |
| 1602 | + in Rt.fail(what, a, b, len, at, name), which says what failed at p, | |
| 1603 | + found by at, which a stub works out from where it was called; what is | |
| 1604 | + an index a (0), a slice a..b (1), each for a length len, a position a | |
| 1605 | + of the enumeration called name (2), a slice of a pointer a..b that | |
| 1606 | + runs backwards (3), a nil pointer (4), or a view at index a that runs | |
| 1607 | + past a length len (7). Rt.fail knows 5 and 6, a | |
| 1608 | + division fault and a stack overflow, which this machine catches | |
| 1609 | + itself, so no check here has them. The arguments go in rdi, rsi, rdx, | |
| 1610 | + rcx, r8 and r9: a is in register ra, b in RDX; len is in memory at m, | |
| 1611 | + or n if m.base is NoBase, or in register m.index if it is ARegister. | |
| 1612 | + rdi and rsi are passed on as they were, since a parameter may be kept | |
| 1613 | + in them, for the trap report to show: so a goes to the stub in rax. | |
| 1614 | + The failing code is out of the way, at the | |
| 1615 | + procedure's end (endFails), so that a check that holds costs a jump | |
| 1616 | + not taken; it has p for its place (its line and column, worked out | |
| 1617 | + now, while the source read is p's), never returns, and may use any | |
| 1618 | + register once it has read what it needs. *) | |
| 1619 | +proc check(cc, what: INT; p: Diag.Pos; ra: INT; m: X.Mem; n: I64; name: INT); | |
| 1620 | +begin | |
| 1621 | + if numFails > LEN(fails) - 1 then | |
| 1622 | + _ := Scan.mark(Diag.say("more than {} checks in one procedure", [Fmt.int(LEN(fails))])); | |
| 1623 | + return; | |
| 1624 | + end; | |
| 1625 | + fails[numFails].L := NoLabel; | |
| 1626 | + X.jcc(cc xor 1, fails[numFails].L); (* x86's conditions come in pairs, the other one its last bit flipped *) | |
| 1627 | + fails[numFails].what := what; | |
| 1628 | + fails[numFails].ra := ra; | |
| 1629 | + fails[numFails].m := m; | |
| 1630 | + fails[numFails].n := n; | |
| 1631 | + fails[numFails].name := name; | |
| 1632 | + fails[numFails].path := Chunk.curFilePath(); | |
| 1633 | + fails[numFails].line, fails[numFails].col := Scan.lineCol(p.a); | |
| 1634 | + INC(numFails); | |
| 1635 | +end; | |
| 1636 | + | |
| 1637 | +(* The failures of the procedure's checks: each gives Rt.fail its values | |
| 1638 | + and the place of the offending expression, through a stub for its | |
| 1639 | + kind, which takes the place from where it was called *) | |
| 1640 | +proc endFails; | |
| 1641 | +var i, path: INT; f: Fail; | |
| 1642 | +begin | |
| 1643 | + path := Chunk.curFilePath(); | |
| 1644 | + for i := 0 to numFails - 1 do | |
| 1645 | + f := fails[i]; | |
| 1646 | + X.fixLink(f.L); | |
| 1647 | + Chunk.setCurFile(f.path); | |
| 1648 | + Chunk.noteLine(f.line, f.col); | |
| 1649 | + Chunk.setCurFile(path); | |
| 1650 | + if f.ra # RAX then X.movRR(RAX, f.ra); end; | |
| 1651 | + if f.m.base = NoBase then X.movRI(RCX, f.n); | |
| 1652 | + elsif f.m.base = ARegister then X.movRR(RCX, f.m.index); | |
| 1653 | + else X.load(RCX, f.m, 8, false); end; | |
| 1654 | + if f.name >= 0 then X.lea(R9, X.memRip(f.name, 0)); end; | |
| 1655 | + if failStubs[f.what] < 0 then | |
| 1656 | + failStubs[f.what] := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr(StubNames[f.what])); | |
| 1657 | + end; | |
| 1658 | + X.callChunk(failStubs[f.what]); | |
| 1659 | + end; | |
| 1660 | + numFails := 0; | |
| 1661 | +end; | |
| 1662 | + | |
| 1663 | +(* For check: a length known here, not in memory *) | |
| 1664 | +proc noMem: X.Mem; | |
| 1665 | +begin | |
| 1666 | + return X.mem(NoBase, 0); | |
| 1667 | +end; | |
| 1668 | + | |
| 1669 | +(* For check: a length in register r, a slice's in registers *) | |
| 1670 | +proc inRegister(r: INT): X.Mem; | |
| 1671 | +var m: X.Mem; | |
| 1672 | +begin | |
| 1673 | + m := X.mem(ARegister, 0); | |
| 1674 | + m.index := r; | |
| 1675 | + return m; | |
| 1676 | +end; | |
| 1677 | + | |
| 1678 | +(* The index on top is below n, unsigned *) | |
| 1679 | +proc checkIndex*(n: I64; p: Diag.Pos); | |
| 1680 | +var r: INT; | |
| 1681 | +begin | |
| 1682 | + r := peek(depth - 1); | |
| 1683 | + cmpI(r, n); | |
| 1684 | + check(X.ccB, 0, p, r, noMem(), n, -1); | |
| 1685 | +end; | |
| 1686 | + | |
| 1687 | +(* The index on top is the element a view starts at, in an array of n | |
| 1688 | + elements: the view needs k elements from there, so the index must be | |
| 1689 | + below n - k + 1, which is below. A negative index is a huge one, | |
| 1690 | + unsigned, so one comparison does (report section 6.9). *) | |
| 1691 | +proc checkView*(n, below: I64; p: Diag.Pos); | |
| 1692 | +var r: INT; | |
| 1693 | +begin | |
| 1694 | + r := peek(depth - 1); | |
| 1695 | + cmpI(r, below); | |
| 1696 | + check(X.ccB, 7, p, r, noMem(), n, -1); | |
| 1697 | +end; | |
| 1698 | + | |
| 1699 | +(* What a failed index check is: an index past the length (0), or a | |
| 1700 | + view that runs past it (7), when the view needs room elements more | |
| 1701 | + than the one indexed. *) | |
| 1702 | +proc kindOfIndex(room: I64): INT; | |
| 1703 | +begin | |
| 1704 | + if room > 0 then return 7; end; | |
| 1705 | + return 0; | |
| 1706 | +end; | |
| 1707 | + | |
| 1708 | +(* A view of a slice's element, at the index in r, needs room elements | |
| 1709 | + after it: rdx holds the slice's length, which r is below already, | |
| 1710 | + so length - r, at least 1, can't wrap, and must be more than room. | |
| 1711 | + The length is at m for the message. *) | |
| 1712 | +proc checkRoom(r: INT; room: I64; p: Diag.Pos; m: X.Mem); | |
| 1713 | +begin | |
| 1714 | + X.alu(X.aluSub, RDX, r); | |
| 1715 | + X.aluI(X.aluCmp, RDX, INT(room)); | |
| 1716 | + check(X.ccA, 7, p, r, m, 0, -1); | |
| 1717 | +end; | |
| 1718 | + | |
| 1719 | +(* The integer on top is a position of an enumeration of n names *) | |
| 1720 | +proc checkEnum*(n: I64; name: INT; p: Diag.Pos); | |
| 1721 | +var r: INT; | |
| 1722 | +begin | |
| 1723 | + r := top(); | |
| 1724 | + cmpI(r, n); | |
| 1725 | + check(X.ccB, 2, p, r, noMem(), n, name); | |
| 1726 | +end; | |
| 1727 | + | |
| 1728 | +(* The address on top, from a pointer, which is to be used reach bytes | |
| 1729 | + past: the program stops if it is nil, unless the machine would stop | |
| 1730 | + it, as for a reach within NilPage. *) | |
| 1731 | +proc checkNil*(reach: I64; p: Diag.Pos); | |
| 1732 | +var r: INT; | |
| 1733 | +begin | |
| 1734 | + if reach <= NilPage then return; end; | |
| 1735 | + r := top(); | |
| 1736 | + X.testRR(r, r); | |
| 1737 | + check(X.ccNE, 4, p, r, noMem(), 0, -1); | |
| 1738 | +end; | |
| 1739 | + | |
| 1740 | +(* The displacement disp of an element of size bytes, with the offset of | |
| 1741 | + the index on top taken into it if it fits: s[k - 1] is [r13 + r9 * 8 | |
| 1742 | + - 8] *) | |
| 1743 | +proc indexOff(size, disp: I64): I64; | |
| 1744 | +begin | |
| 1745 | + if fits32(disp + stack[depth - 1].off * size) then return disp + takeOff(depth - 1) * size; end; | |
| 1746 | + return disp; | |
| 1747 | +end; | |
| 1748 | + | |
| 1749 | +(* Pops an index, and the address of the slice at a if it is on the | |
| 1750 | + stack, and pushes the address of its element of size bytes: the | |
| 1751 | + index, checked against the length, times size, plus the pointer. | |
| 1752 | + It is worked out in the index's own register, which then holds the | |
| 1753 | + address; but if the slice's address is on the stack, under the | |
| 1754 | + index, the element's register is the one that holds that, still | |
| 1755 | + needed, so the work is done in rax. A slice in registers is | |
| 1756 | + compared with and added to them; for an element of 1, 2, 4 or 8 | |
| 1757 | + bytes, its address is only noted, as the operand [pointer + index * | |
| 1758 | + size], to be read or written where it is used (lIdx). *) | |
| 1759 | +proc indexSlice*(a: Adr; size, room: I64; checked: BOOL; p: Diag.Pos); | |
| 1760 | +var m, ml: X.Mem; r: INT; inRax: BOOL; o: I64; | |
| 1761 | +begin | |
| 1762 | + if (a.base = bReg) and (((size = 1) or (size = 2) or (size = 4) or (size = 8))) then | |
| 1763 | + o := 0; | |
| 1764 | + if not checked then o := indexOff(size, 0); end; | |
| 1765 | + r := peek(depth - 1); | |
| 1766 | + if checked then | |
| 1767 | + X.alu(X.aluCmp, r, a.hi); | |
| 1768 | + check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1769 | + if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1770 | + end; | |
| 1771 | + drop(); | |
| 1772 | + newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); | |
| 1773 | + return; | |
| 1774 | + end; | |
| 1775 | + inRax := a.base = bStack; | |
| 1776 | + if inRax then r := RAX; popTo(RAX); | |
| 1777 | + else r := top(); drop(); end; | |
| 1778 | + if a.base = bReg then | |
| 1779 | + if checked then | |
| 1780 | + X.alu(X.aluCmp, r, a.hi); | |
| 1781 | + check(X.ccB, kindOfIndex(room), p, r, inRegister(a.hi), 0, -1); | |
| 1782 | + if room > 0 then X.movRR(RDX, a.hi); checkRoom(r, room, p, inRegister(a.hi)); end; | |
| 1783 | + end; | |
| 1784 | + mulI(r, size); | |
| 1785 | + X.alu(X.aluAdd, r, a.chunk); | |
| 1786 | + else | |
| 1787 | + m := place(a, RCX); | |
| 1788 | + ml := m; ml.disp := ml.disp + 8; | |
| 1789 | + if checked then | |
| 1790 | + X.aluM(X.aluCmp, r, ml); | |
| 1791 | + check(X.ccB, kindOfIndex(room), p, r, ml, 0, -1); | |
| 1792 | + if room > 0 then X.load(RDX, ml, 8, false); checkRoom(r, room, p, ml); end; | |
| 1793 | + end; | |
| 1794 | + mulI(r, size); | |
| 1795 | + X.aluM(X.aluAdd, r, m); | |
| 1796 | + end; | |
| 1797 | + if inRax then X.movRR(target(), RAX); | |
| 1798 | + else _ := claim(depth, kWord); end; (* the index's register again, now for the address *) | |
| 1799 | + computed(kWord); | |
| 1800 | +end; | |
| 1801 | + | |
| 1802 | +(* Whether r is one of the stack's registers, not a variable's *) | |
| 1803 | +proc isStackReg(r: INT): BOOL; | |
| 1804 | +var i: INT; | |
| 1805 | +begin | |
| 1806 | + for i := 0 to NumRegs - 1 do | |
| 1807 | + if Regs[i] = r then return true; end; | |
| 1808 | + end; | |
| 1809 | + return false; | |
| 1810 | +end; | |
| 1811 | + | |
| 1812 | +(* Pops an index and the pointer under it, and pushes the address of | |
| 1813 | + the element of size bytes: p + i * size. For an element of 1, 2, 4 | |
| 1814 | + or 8 bytes it is only noted, as the operand [p + i * size] (lIdx), to | |
| 1815 | + be read or written where it is used, when at most one of the two is | |
| 1816 | + in a stack register, the register the new value owns. Otherwise lea | |
| 1817 | + adds them up, in one instruction in place of a multiplication and an | |
| 1818 | + addition. *) | |
| 1819 | +proc indexPtr*(size: I64); | |
| 1820 | +var b, x, own: INT; o: I64; | |
| 1821 | +begin | |
| 1822 | + o := 0; | |
| 1823 | + if fits32(stack[depth - 2].off + stack[depth - 1].off * size) then | |
| 1824 | + o := takeOff(depth - 2) + takeOff(depth - 1) * size; | |
| 1825 | + end; | |
| 1826 | + b := peek(depth - 2); | |
| 1827 | + x := peek(depth - 1); | |
| 1828 | + own := Regs[(depth - 2) mod NumRegs]; | |
| 1829 | + drop(); | |
| 1830 | + drop(); | |
| 1831 | + if (not isStackReg(b) or (b = own)) and (not isStackReg(x) or (x = own)) then | |
| 1832 | + newMem(lIdx, X.memIdx(b, x, INT(size), o), kWord, 8, false); | |
| 1833 | + if b = own then owner[(depth - 1) mod NumRegs] := depth - 1; end; | |
| 1834 | + else | |
| 1835 | + X.lea(target(), X.memIdx(b, x, INT(size), o)); | |
| 1836 | + computed(kWord); | |
| 1837 | + end; | |
| 1838 | +end; | |
| 1839 | + | |
| 1840 | +(* Pops an index, checked, and makes the address of the element it | |
| 1841 | + picks of the array at a. An element of 1, 2, | |
| 1842 | + 4 or 8 bytes is only noted, as the operand [base + index * size] | |
| 1843 | + (lIdx): an array in the frame has rbp as its base, one reached | |
| 1844 | + through a pointer in a register (a var or const parameter) that | |
| 1845 | + register, and one in a chunk its address as the displacement | |
| 1846 | + (X.memAbs), with no lea. Any other is added up. *) | |
| 1847 | +proc indexArray*(a: Adr; size: I64); | |
| 1848 | +var r: INT; o: I64; | |
| 1849 | +begin | |
| 1850 | + if (((size = 1) or (size = 2) or (size = 4) or (size = 8))) and (a.base in {bFrame, bGlobal, bPtr}) then | |
| 1851 | + o := indexOff(size, a.off); | |
| 1852 | + r := peek(depth - 1); | |
| 1853 | + drop(); | |
| 1854 | + if a.base = bFrame then | |
| 1855 | + newMem(lIdx, X.memIdx(RBP, r, INT(size), o), kWord, 8, false); | |
| 1856 | + elsif a.base = bPtr then | |
| 1857 | + newMem(lIdx, X.memIdx(a.chunk, r, INT(size), o), kWord, 8, false); | |
| 1858 | + else | |
| 1859 | + newMem(lIdx, X.memAbs(a.chunk, r, INT(size), INT(o)), kWord, 8, false); | |
| 1860 | + end; | |
| 1861 | + else | |
| 1862 | + mulI(top(), size); | |
| 1863 | + pushAdr(a); | |
| 1864 | + op(Scan.sPlus, kWord, 8, false); | |
| 1865 | + end; | |
| 1866 | +end; | |
| 1867 | + | |
| 1868 | +(* Pops lo and hi, and the address of the array or slice at a if it is | |
| 1869 | + on the stack, and makes the slice lo..hi of it, with elements of size | |
| 1870 | + bytes, in the frame at tmp; the array has n elements. *) | |
| 1871 | +proc slice*(a: Adr; isArray: BOOL; n, size, tmp: I64; checked: BOOL; p: Diag.Pos); | |
| 1872 | +var m, ml: X.Mem; | |
| 1873 | +begin | |
| 1874 | + popTo(RDX); | |
| 1875 | + popTo(RCX); | |
| 1876 | + if a.base = bSlot then clobberArgs(); end; (* place loads its slot into rdi *) | |
| 1877 | + if a.base = bReg then | |
| 1878 | + ml := inRegister(a.hi); (* a slice in registers *) | |
| 1879 | + else | |
| 1880 | + m := place(a, RDI); | |
| 1881 | + ml := m; ml.disp := ml.disp + 8; | |
| 1882 | + end; | |
| 1883 | + if checked then | |
| 1884 | + (* two checks, both unsigned, so that a negative bound fails too: | |
| 1885 | + hi against the length, then lo against hi *) | |
| 1886 | + if isArray then cmpI(RDX, n); ml := noMem(); | |
| 1887 | + elsif a.base = bReg then X.alu(X.aluCmp, RDX, a.hi); | |
| 1888 | + else X.aluM(X.aluCmp, RDX, ml); end; | |
| 1889 | + check(X.ccBE, 1, p, RCX, ml, n, -1); | |
| 1890 | + X.alu(X.aluCmp, RCX, RDX); | |
| 1891 | + check(X.ccBE, 1, p, RCX, ml, n, -1); | |
| 1892 | + end; | |
| 1893 | + X.movRR(RAX, RDX); | |
| 1894 | + X.alu(X.aluSub, RAX, RCX); | |
| 1895 | + X.store(X.mem(RBP, tmp + 8), RAX, 8); | |
| 1896 | + mulI(RCX, size); | |
| 1897 | + if isArray then X.lea(RAX, m); | |
| 1898 | + elsif a.base = bReg then X.movRR(RAX, a.chunk); | |
| 1899 | + else X.load(RAX, m, 8, false); end; | |
| 1900 | + X.alu(X.aluAdd, RAX, RCX); | |
| 1901 | + X.store(X.mem(RBP, tmp), RAX, 8); | |
| 1902 | +end; | |
| 1903 | + | |
| 1904 | +(* The rcx bytes below rsi to those below rdi, n at a time while rcx | |
| 1905 | + holds n or more, through rax, moving rsi, rdi and rcx down: COPY's | |
| 1906 | + backward copy, as a loop, since rep movsb going down (std) moves a | |
| 1907 | + byte at a time. *) | |
| 1908 | +proc copyDown(n: INT); | |
| 1909 | +var top, out: INT; | |
| 1910 | +begin | |
| 1911 | + top := Chunk.pc; | |
| 1912 | + X.aluI(X.aluCmp, RCX, n); | |
| 1913 | + out := NoLabel; | |
| 1914 | + X.jcc(X.ccB, out); | |
| 1915 | + X.aluI(X.aluSub, RSI, n); | |
| 1916 | + X.aluI(X.aluSub, RDI, n); | |
| 1917 | + X.aluI(X.aluSub, RCX, n); | |
| 1918 | + X.load(RAX, X.mem(RSI, 0), n, false); | |
| 1919 | + X.store(X.mem(RDI, 0), RAX, n); | |
| 1920 | + X.jmpBack(top); | |
| 1921 | + X.fixLink(out); | |
| 1922 | +end; | |
| 1923 | + | |
| 1924 | +(* COPY: the elements of the slice in the frame at sOff, of size bytes | |
| 1925 | + each, to the start of the one at dOff, which must be at least as long | |
| 1926 | + (the source may be shorter), as one block. It goes forwards, by rep | |
| 1927 | + movsb, but backwards when the destination starts inside the source, | |
| 1928 | + so that an overlap comes out right: by words from the ends, then the | |
| 1929 | + last few bytes one by one. *) | |
| 1930 | +proc copySlice*(dOff, sOff, size: I64; checked: BOOL; p: Diag.Pos); | |
| 1931 | +var ml: X.Mem; L, E: INT; | |
| 1932 | +begin | |
| 1933 | + clobberArgs(); | |
| 1934 | + X.load(RDX, X.mem(RBP, sOff + 8), 8, false); | |
| 1935 | + if checked then | |
| 1936 | + ml := X.mem(RBP, dOff + 8); | |
| 1937 | + X.movRI(RAX, 0); | |
| 1938 | + X.aluM(X.aluCmp, RDX, ml); | |
| 1939 | + check(X.ccBE, 1, p, RAX, ml, 0, -1); | |
| 1940 | + end; | |
| 1941 | + X.movRR(RCX, RDX); | |
| 1942 | + mulI(RCX, size); | |
| 1943 | + X.load(RSI, X.mem(RBP, sOff), 8, false); | |
| 1944 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1945 | + L := NoLabel; | |
| 1946 | + X.alu(X.aluCmp, RDI, RSI); | |
| 1947 | + X.jcc(X.ccBE, L); (* the destination is not above: forwards, at L *) | |
| 1948 | + X.lea(RAX, X.memIdx(RSI, RCX, 1, 0)); | |
| 1949 | + X.alu(X.aluCmp, RDI, RAX); | |
| 1950 | + X.jcc(X.ccAE, L); (* nor does it start inside the source *) | |
| 1951 | + X.alu(X.aluAdd, RSI, RCX); (* backwards, from the ends *) | |
| 1952 | + X.alu(X.aluAdd, RDI, RCX); | |
| 1953 | + copyDown(8); | |
| 1954 | + copyDown(1); | |
| 1955 | + E := NoLabel; | |
| 1956 | + X.jmp(E); | |
| 1957 | + X.fixLink(L); | |
| 1958 | + X.repMovsb(); | |
| 1959 | + X.fixLink(E); | |
| 1960 | +end; | |
| 1961 | + | |
| 1962 | +(* The most elements FILL stores one by one: as for MaxMoves, a store | |
| 1963 | + each costs less than rep stos takes to start, which at 10 elements | |
| 1964 | + is twice the time. *) | |
| 1965 | +const FewFills = 16; | |
| 1966 | + | |
| 1967 | +(* rax, an element of size bytes, 1, 2, 4 or 8, into each of the | |
| 1968 | + elements of the slice in the frame at dOff, of which there are n if | |
| 1969 | + n isn't -1. Few, as of a short array, are a store each; else, at run | |
| 1970 | + time, a loop of stores for fewer than FewFills, or rep stos of the | |
| 1971 | + element's width: | |
| 1972 | + | |
| 1973 | + mov rdi, [rbp + dOff] | |
| 1974 | + mov rcx, [rbp + dOff + 8] | |
| 1975 | + cmp rcx, 16 | |
| 1976 | + jae many | |
| 1977 | + test rcx, rcx | |
| 1978 | + jz out | |
| 1979 | + top: mov [rdi], eax ; for a U32 | |
| 1980 | + add rdi, 4 | |
| 1981 | + sub rcx, 1 | |
| 1982 | + jne top | |
| 1983 | + jmp out | |
| 1984 | + many: rep stosd | |
| 1985 | + out: *) | |
| 1986 | +proc fillScalar(dOff, size, n: I64); | |
| 1987 | +var i: I64; many, out, top: INT; | |
| 1988 | +begin | |
| 1989 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 1990 | + if (n >= 0) and (n <= FewFills) then | |
| 1991 | + for i := 0 to n - 1 do X.store(X.mem(RDI, i * size), RAX, INT(size)); end; | |
| 1992 | + return; | |
| 1993 | + end; | |
| 1994 | + X.load(RCX, X.mem(RBP, dOff + 8), 8, false); | |
| 1995 | + many := NoLabel; | |
| 1996 | + out := NoLabel; | |
| 1997 | + X.aluI(X.aluCmp, RCX, FewFills); | |
| 1998 | + X.jcc(X.ccAE, many); | |
| 1999 | + X.testRR(RCX, RCX); | |
| 2000 | + X.jcc(X.ccE, out); | |
| 2001 | + top := Chunk.pc; | |
| 2002 | + X.store(X.mem(RDI, 0), RAX, INT(size)); | |
| 2003 | + X.aluI(X.aluAdd, RDI, INT(size)); | |
| 2004 | + X.aluI(X.aluSub, RCX, 1); | |
| 2005 | + X.jccBack(X.ccNE, top); | |
| 2006 | + X.jmp(out); | |
| 2007 | + X.fixLink(many); | |
| 2008 | + X.repStos(INT(size)); | |
| 2009 | + X.fixLink(out); | |
| 2010 | +end; | |
| 2011 | + | |
| 2012 | +(* The size bytes at rsi into each element of the slice in the frame at | |
| 2013 | + dOff: into the first by copyBytes, and from there forward by rep | |
| 2014 | + movsb, from d[0] on to d[1] on. A forward copy reads each byte after it has written it, size | |
| 2015 | + bytes before, so the first element repeats through the rest: | |
| 2016 | + | |
| 2017 | + mov rdi, [rbp + dOff] | |
| 2018 | + mov rdx, [rbp + dOff + 8] | |
| 2019 | + test rdx, rdx | |
| 2020 | + jz out ; nothing to fill | |
| 2021 | + ... ; copyBytes(size): d[0] := x | |
| 2022 | + mov rsi, [rbp + dOff] | |
| 2023 | + lea rdi, [rsi + size] | |
| 2024 | + lea rcx, [rdx - 1] | |
| 2025 | + imul rcx, size ; the bytes of d[1..n] | |
| 2026 | + rep movsb | |
| 2027 | + out: *) | |
| 2028 | +proc fillCopy(dOff, size: I64); | |
| 2029 | +var out: INT; | |
| 2030 | +begin | |
| 2031 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 2032 | + X.load(RDX, X.mem(RBP, dOff + 8), 8, false); | |
| 2033 | + X.testRR(RDX, RDX); | |
| 2034 | + out := NoLabel; | |
| 2035 | + X.jcc(X.ccE, out); | |
| 2036 | + copyBytes(size); | |
| 2037 | + X.load(RSI, X.mem(RBP, dOff), 8, false); | |
| 2038 | + X.lea(RDI, X.mem(RSI, size)); | |
| 2039 | + X.lea(RCX, X.mem(RDX, -1)); | |
| 2040 | + mulI(RCX, size); | |
| 2041 | + X.repMovsb(); | |
| 2042 | + X.fixLink(out); | |
| 2043 | +end; | |
| 2044 | + | |
| 2045 | +(* FILL: every element of the slice in the frame at dOff, of size bytes | |
| 2046 | + each, set to the value on top of the stack if the element is a | |
| 2047 | + scalar, or else to the size bytes at the address on top. n is how | |
| 2048 | + many elements the slice has if that is known, as for an array, else | |
| 2049 | + -1. A scalar goes into rax and is stored as it is, a real's bits as | |
| 2050 | + an integer's (fillScalar); but a constant zero into an array of n | |
| 2051 | + elements is n * size zero bytes, as NEW's zeroing is: by moves, or | |
| 2052 | + rep stosb (zeroBytes). Any other element is copied (fillCopy). *) | |
| 2053 | +proc fillSlice*(dOff, size, n: I64; scalar: BOOL); | |
| 2054 | +var isConst: BOOL; v: I64; | |
| 2055 | +begin | |
| 2056 | + clobberArgs(); | |
| 2057 | + isConst, v := topConst(); | |
| 2058 | + if scalar and isConst and (v = 0) and (n >= 0) then | |
| 2059 | + drop(); | |
| 2060 | + X.load(RDI, X.mem(RBP, dOff), 8, false); | |
| 2061 | + zeroBytes(n * size); | |
| 2062 | + elsif scalar then | |
| 2063 | + popTo(RAX); | |
| 2064 | + fillScalar(dOff, size, n); | |
| 2065 | + else | |
| 2066 | + popTo(RSI); | |
| 2067 | + fillCopy(dOff, size); | |
| 2068 | + end; | |
| 2069 | +end; | |
| 2070 | + | |
| 2071 | +(* Pops a pointer, lo and hi, and makes the slice p[lo..hi] at tmp. Its | |
| 2072 | + bounds can't be known, but it can't run backwards. *) | |
| 2073 | +proc slicePtr*(size, tmp: I64; checked: BOOL; p: Diag.Pos); | |
| 2074 | +begin | |
| 2075 | + clobberArgs(); | |
| 2076 | + popTo(RDX); | |
| 2077 | + popTo(RCX); | |
| 2078 | + popTo(RDI); | |
| 2079 | + if checked then | |
| 2080 | + X.alu(X.aluCmp, RCX, RDX); | |
| 2081 | + check(X.ccLE, 3, p, RCX, noMem(), 0, -1); | |
| 2082 | + end; | |
| 2083 | + X.movRR(RAX, RDX); | |
| 2084 | + X.alu(X.aluSub, RAX, RCX); | |
| 2085 | + X.store(X.mem(RBP, tmp + 8), RAX, 8); | |
| 2086 | + mulI(RCX, size); | |
| 2087 | + X.alu(X.aluAdd, RCX, RDI); | |
| 2088 | + X.store(X.mem(RBP, tmp), RCX, 8); | |
| 2089 | +end; | |
| 2090 | + | |
| 2091 | +(* Calls *) | |
| 2092 | + | |
| 2093 | +(* The call c, its c.n arguments the values on top of the stack, in | |
| 2094 | + registers, noted as constants or spilled. They go to registers or the | |
| 2095 | + machine's stack, the procedure is called, and its result, if any, | |
| 2096 | + comes back: one value is pushed, two results are pushed as two values, | |
| 2097 | + a slice or a record is stored in the temporary c.ret. | |
| 2098 | + | |
| 2099 | + ni and nf count the integer and the real registers used so far, k the | |
| 2100 | + arguments on the machine's stack, and pad is 1 when a padding quadword | |
| 2101 | + is needed. *) | |
| 2102 | + | |
| 2103 | +proc call*(var c: CallInfo); | |
| 2104 | +var | |
| 2105 | + res: ^Sym.Type; i, ni, nf, k, pad, r, first: INT; | |
| 2106 | + onStack: [64]BOOL; late: [2]INT; (* late: the values for r8 and r9 *) | |
| 2107 | +begin | |
| 2108 | + (* the arguments are the c.n values on top, where computing them left | |
| 2109 | + them; the registers in use under them are saved around the call *) | |
| 2110 | + clobberArgs(); | |
| 2111 | + first := depth - c.n; | |
| 2112 | + spillBelow(first); | |
| 2113 | + res := c.sig^.base; | |
| 2114 | + c.ret := 0; | |
| 2115 | + ni := 0; nf := 0; | |
| 2116 | + if (res # nil) and Sym.holdsArray(res) then | |
| 2117 | + (* a slice of it may be kept for the rest of the procedure | |
| 2118 | + (report 8), so its slot lasts as long *) | |
| 2119 | + c.ret := allocPerm(res^.size, res^.align); | |
| 2120 | + ni := 1; | |
| 2121 | + elsif (res # nil) and (Sym.behindPointer(res) or (res^.form = Sym.fSlice)) then | |
| 2122 | + c.ret := allocTemp(res^.size, res^.align); | |
| 2123 | + if Sym.behindPointer(res) then ni := 1; end; | |
| 2124 | + end; | |
| 2125 | + (* the System V classes: each argument in the next free register of | |
| 2126 | + its kind, or else on the stack *) | |
| 2127 | + k := 0; | |
| 2128 | + for i := 0 to c.n - 1 do | |
| 2129 | + if c.isF[i] then | |
| 2130 | + onStack[i] := nf >= 8; | |
| 2131 | + if nf < 8 then INC(nf); end; | |
| 2132 | + else | |
| 2133 | + (* a slice's words both go in registers, or both on the stack *) | |
| 2134 | + if (i > 0) and c.pair[i - 1] then onStack[i] := onStack[i - 1]; | |
| 2135 | + elsif c.pair[i] then onStack[i] := ni > 4; | |
| 2136 | + else onStack[i] := ni >= 6; end; | |
| 2137 | + if not onStack[i] then INC(ni); end; | |
| 2138 | + end; | |
| 2139 | + if onStack[i] then INC(k); end; | |
| 2140 | + end; | |
| 2141 | + (* System V wants rsp a multiple of 16 at the call. A procedure's body | |
| 2142 | + has it so (push rbp, and a frame size that is a multiple of 16), so | |
| 2143 | + a quadword of padding makes the count pushed since even. The stack | |
| 2144 | + arguments go right to left, so that the first is at the lowest | |
| 2145 | + address. *) | |
| 2146 | + pad := 0; | |
| 2147 | + if ((pushed + k) & 1) # 0 then pad := 1; end; | |
| 2148 | + if pad # 0 then X.aluI(X.aluSub, RSP, 8); end; | |
| 2149 | + for i := c.n - 1 to 0 by -1 do | |
| 2150 | + if onStack[i] then | |
| 2151 | + valueTo(first + i, RAX); | |
| 2152 | + X.push(RAX); | |
| 2153 | + end; | |
| 2154 | + end; | |
| 2155 | + ni := 0; nf := 0; | |
| 2156 | + late[0] := -1; late[1] := -1; | |
| 2157 | + if (c.ret # 0) and Sym.behindPointer(res) then X.lea(RDI, X.mem(RBP, c.ret)); ni := 1; end; | |
| 2158 | + (* rdi, rsi, rdx, rcx and the xmm registers first; r8 and r9 last, as | |
| 2159 | + they are stack registers and may hold arguments not yet moved: late | |
| 2160 | + is the value for each. The fifth goes through rax, so that writing r8 | |
| 2161 | + doesn't destroy a value still wanted for r9. *) | |
| 2162 | + for i := 0 to c.n - 1 do | |
| 2163 | + if not onStack[i] then | |
| 2164 | + if c.isF[i] then | |
| 2165 | + xValueTo(first + i, nf); | |
| 2166 | + INC(nf); | |
| 2167 | + else | |
| 2168 | + if ni < 4 then valueTo(first + i, IntRegs[ni]); else late[ni - 4] := first + i; end; | |
| 2169 | + INC(ni); | |
| 2170 | + end; | |
| 2171 | + end; | |
| 2172 | + end; | |
| 2173 | + if late[0] >= 0 then valueTo(late[0], RAX); end; | |
| 2174 | + if late[1] >= 0 then valueTo(late[1], R9); end; | |
| 2175 | + if late[0] >= 0 then X.movRR(R8, RAX); end; | |
| 2176 | + for i := 1 to c.n do drop(); end; | |
| 2177 | + (* a procedure taking ... is told in al how many xmm registers it got *) | |
| 2178 | + if c.sig^.variadic then X.movRI(RAX, nf); end; | |
| 2179 | + if c.ext then X.callM(X.memRip(c.chunk, 0)); | |
| 2180 | + elsif c.chunk >= 0 then X.callChunk(c.chunk); | |
| 2181 | + else | |
| 2182 | + (* r11 is scratch: caller-saved, carrying no argument, and free, | |
| 2183 | + as the value stack was spilled above *) | |
| 2184 | + X.load(R11, X.mem(RBP, c.target), 8, false); | |
| 2185 | + X.callR(R11); | |
| 2186 | + end; | |
| 2187 | + if k + pad > 0 then X.aluI(X.aluAdd, RSP, 8 * (k + pad)); end; | |
| 2188 | + (* the variables in xmm registers, which the call may have changed, | |
| 2189 | + from their slots *) | |
| 2190 | + for i := 0 to LEN(XVarRegs) - 1 do | |
| 2191 | + if (regMask & (1 shl (XBit + i))) # 0 then | |
| 2192 | + X.movsXM(XVarRegs[i] - XmmVar, X.mem(RBP, homes[i]), false); | |
| 2193 | + end; | |
| 2194 | + end; | |
| 2195 | + (* the result: a slice in rax and rdx goes to the caller's temporary, | |
| 2196 | + two results on the stack, the first under the second, and a single | |
| 2197 | + value on top *) | |
| 2198 | + if res # nil then | |
| 2199 | + if res^.form = Sym.fSlice then | |
| 2200 | + X.store(X.mem(RBP, c.ret), RAX, 8); | |
| 2201 | + X.store(X.mem(RBP, c.ret + 8), RDX, 8); | |
| 2202 | + elsif Sym.inTwoRegs(res) then | |
| 2203 | + X.movRR(target(), RAX); computed(kWord); | |
| 2204 | + X.movRR(target(), RDX); computed(kWord); | |
| 2205 | + elsif (c.ret = 0) and (res^.form = Sym.fReal) then | |
| 2206 | + X.movX(xTarget(), 0); | |
| 2207 | + computed(kindOf(res)); | |
| 2208 | + elsif c.ret = 0 then | |
| 2209 | + r := target(); | |
| 2210 | + X.movRR(r, RAX); | |
| 2211 | + if res^.form = Sym.fBool then X.extend(r, 1, false); | |
| 2212 | + elsif (res^.form = Sym.fInt) and (res^.size < 8) then X.extend(r, INT(res^.size), res^.signed); end; | |
| 2213 | + computed(kindOf(res)); | |
| 2214 | + end; | |
| 2215 | + end; | |
| 2216 | +end; | |
| 2217 | + | |
| 2218 | +(* Frames *) | |
| 2219 | + | |
| 2220 | +(* A slot of size bytes in the frame: its offset from rbp, negative, as | |
| 2221 | + the frame grows down. A temporary is released at the end of its | |
| 2222 | + statement (tempMark, releaseTemps); allocPerm's lasts the procedure. *) | |
| 2223 | +proc allocTemp*(size, align: I64): I64; | |
| 2224 | +begin | |
| 2225 | + if align < 1 then align := 1; end; | |
| 2226 | + frameTop := Sym.alignUp(frameTop + size, align); | |
| 2227 | + if frameTop > frameMax then frameMax := frameTop; end; | |
| 2228 | + return -frameTop; | |
| 2229 | +end; | |
| 2230 | + | |
| 2231 | +proc allocPerm*(size, align: I64): I64; | |
| 2232 | +var s: I64; | |
| 2233 | +begin | |
| 2234 | + s := allocTemp(size, align); | |
| 2235 | + permTop := frameTop; | |
| 2236 | + return s; | |
| 2237 | +end; | |
| 2238 | + | |
| 2239 | +proc tempMark*: I64; | |
| 2240 | +begin | |
| 2241 | + return frameTop; | |
| 2242 | +end; | |
| 2243 | + | |
| 2244 | +(* The slots allocated since the mark m are free again, but those | |
| 2245 | + allocPerm made stay *) | |
| 2246 | +proc releaseTemps*(m: I64); | |
| 2247 | +begin | |
| 2248 | + if m < permTop then m := permTop; end; | |
| 2249 | + frameTop := m; | |
| 2250 | +end; | |
| 2251 | + | |
| 2252 | +(* Every slot used so far stays reserved for the rest of the procedure: | |
| 2253 | + a deferred statement's temporaries live until it runs. *) | |
| 2254 | +proc keepTemps*; | |
| 2255 | +begin | |
| 2256 | + frameTop := frameMax; | |
| 2257 | + permTop := frameMax; | |
| 2258 | +end; | |
| 2259 | + | |
| 2260 | +(* The parameters' places: each in a frame slot of its own, stored there | |
| 2261 | + from its register on entry, or above the frame where the caller | |
| 2262 | + pushed it. A var parameter, and a record or array passed by value, | |
| 2263 | + holds an address (ind). *) | |
| 2264 | +proc beginProc*(sig: ^Sym.Type; params: ^Sym.Obj); | |
| 2265 | +var p: ^Sym.Obj; ni, nf, k: INT; stackOff: I64; f: BOOL; | |
| 2266 | +begin | |
| 2267 | + frameTop := 0; frameMax := 0; permTop := 0; | |
| 2268 | + resetStack(0, 0); | |
| 2269 | + exitL := NoLabel; | |
| 2270 | + numIn := 0; | |
| 2271 | + lost := false; | |
| 2272 | + kept := 0; | |
| 2273 | + resType := sig^.base; | |
| 2274 | + procParams := params; | |
| 2275 | + regMask := 0; | |
| 2276 | + ni := 0; nf := 0; stackOff := 16; (* stackOff: above the saved rbp and the return address *) | |
| 2277 | + if (resType # nil) and Sym.behindPointer(resType) then | |
| 2278 | + retSlot := allocPerm(8, 8); | |
| 2279 | + inReg[0] := RDI; inSlot[0] := retSlot; inSize[0] := 8; inObj[0] := nil; | |
| 2280 | + numIn := 1; ni := 1; | |
| 2281 | + end; | |
| 2282 | + p := params; | |
| 2283 | + while p # nil do | |
| 2284 | + p^.ind := p^.isVar or (Sym.isAggregate(p^.typ) and (p^.typ^.form # Sym.fSlice)); | |
| 2285 | + f := (p^.typ^.form = Sym.fReal) and not p^.isVar; | |
| 2286 | + if (p^.typ^.form = Sym.fSlice) and not p^.isVar then | |
| 2287 | + (* its two words, in two registers or both on the stack *) | |
| 2288 | + if ni <= 4 then | |
| 2289 | + p^.val := allocPerm(16, 8); | |
| 2290 | + for k := 0 to 1 do | |
| 2291 | + inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + I64(8 * k); | |
| 2292 | + inSize[numIn] := 8; inObj[numIn] := p; | |
| 2293 | + INC(numIn); INC(ni); | |
| 2294 | + end; | |
| 2295 | + else | |
| 2296 | + p^.val := stackOff; | |
| 2297 | + INC(stackOff, 16); | |
| 2298 | + end; | |
| 2299 | + elsif (f and (nf < 8)) or (not f and (ni < 6)) then | |
| 2300 | + p^.val := allocPerm(8, 8); | |
| 2301 | + (* a real's register, xmm nf, is told from an integer register by | |
| 2302 | + a negative number (enterBody decodes it) *) | |
| 2303 | + if f then | |
| 2304 | + inReg[numIn] := -1 - nf; INC(nf); | |
| 2305 | + else | |
| 2306 | + inReg[numIn] := IntRegs[ni]; INC(ni); | |
| 2307 | + end; | |
| 2308 | + inSlot[numIn] := p^.val; | |
| 2309 | + inObj[numIn] := p; | |
| 2310 | + if p^.ind then inSize[numIn] := 8; | |
| 2311 | + elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet, Sym.fReal} then inSize[numIn] := INT(p^.typ^.size); | |
| 2312 | + else inSize[numIn] := 8; end; | |
| 2313 | + INC(numIn); | |
| 2314 | + else | |
| 2315 | + p^.val := stackOff; | |
| 2316 | + INC(stackOff, 8); | |
| 2317 | + end; | |
| 2318 | + p := p^.next; | |
| 2319 | + end; | |
| 2320 | + paramTop := frameTop; | |
| 2321 | +end; | |
| 2322 | + | |
| 2323 | +(* The frame as beginProc left it, with the parameters' slots only: the | |
| 2324 | + locals are given theirs again once their registers are chosen. *) | |
| 2325 | +proc resetLocals*; | |
| 2326 | +begin | |
| 2327 | + frameTop := paramTop; frameMax := paramTop; permTop := paramTop; | |
| 2328 | +end; | |
| 2329 | + | |
| 2330 | +(* The register the word (0, or 1 for a slice's second) of parameter o | |
| 2331 | + arrives in, or -1 if it comes on the stack or in an xmm register *) | |
| 2332 | +proc arrivesIn(o: ^Sym.Obj; word: INT): INT; | |
| 2333 | +var i, r: INT; | |
| 2334 | +begin | |
| 2335 | + r := -1; | |
| 2336 | + for i := 0 to numIn - 1 do | |
| 2337 | + if (inObj[i] = o) and (inSlot[i] = o^.val + I64(8 * word)) and (inReg[i] >= 0) then r := inReg[i]; end; | |
| 2338 | + end; | |
| 2339 | + return r; | |
| 2340 | +end; | |
| 2341 | + | |
| 2342 | +(* Parameter o, of a procedure that calls nothing, stays in the register | |
| 2343 | + it arrives in if that is rdi or rsi, which only a call or a copy needs: | |
| 2344 | + no register is saved for it, and nothing is moved. A slice stays only | |
| 2345 | + if it has both. If the procedure turns out to copy after all, lost is | |
| 2346 | + set, and it is compiled again with o in memory. *) | |
| 2347 | +proc keepArrived*(o: ^Sym.Obj); | |
| 2348 | +var r: INT; | |
| 2349 | +begin | |
| 2350 | + r := arrivesIn(o, 0); | |
| 2351 | + if o^.typ^.form = Sym.fSlice then | |
| 2352 | + if (r = RDI) and (arrivesIn(o, 1) = RSI) then o^.reg := RDI; o^.reg2 := RSI; end; | |
| 2353 | + elsif (r = RDI) or (r = RSI) then o^.reg := r; end; | |
| 2354 | +end; | |
| 2355 | + | |
| 2356 | +(* Whether parameter o is kept where it arrived *) | |
| 2357 | +proc keptIn*(o: ^Sym.Obj): BOOL; | |
| 2358 | +begin | |
| 2359 | + return (o^.reg = RDI) or (o^.reg = RSI); | |
| 2360 | +end; | |
| 2361 | + | |
| 2362 | +(* The locals in memory have their slots: the prologue zeroes those and | |
| 2363 | + nothing below. The procedure keeps variables in the registers of | |
| 2364 | + mask, which are saved next, a quadword each, in slots that need no | |
| 2365 | + zeroing; the frame as it then stands is where a second try at the | |
| 2366 | + body starts. *) | |
| 2367 | +proc useRegs*(mask: INT); | |
| 2368 | +var i, k: INT; p: ^Sym.Obj; | |
| 2369 | +begin | |
| 2370 | + zeroTop := permTop; | |
| 2371 | + regMask := mask; | |
| 2372 | + (* the parameters left in rdi and rsi, as they arrived *) | |
| 2373 | + kept := 0; | |
| 2374 | + p := procParams; | |
| 2375 | + while p # nil do | |
| 2376 | + if ((p^.reg = RDI) or (p^.reg = RSI)) then kept := kept | (1 shl p^.reg); end; | |
| 2377 | + if ((p^.reg2 = RDI) or (p^.reg2 = RSI)) then kept := kept | (1 shl p^.reg2); end; | |
| 2378 | + p := p^.next; | |
| 2379 | + end; | |
| 2380 | + k := 0; | |
| 2381 | + for i := 0 to 4 do | |
| 2382 | + if (mask & (1 shl i)) # 0 then INC(k); end; | |
| 2383 | + end; | |
| 2384 | + if k > 0 then saveAt := allocPerm(8 * k, 8); end; | |
| 2385 | + frameAt[0] := frameTop; frameAt[1] := frameMax; frameAt[2] := permTop; | |
| 2386 | +end; | |
| 2387 | + | |
| 2388 | +(* The variable in xmm register r of XVarRegs has a slot of 8 bytes at | |
| 2389 | + slot, kept as it changes *) | |
| 2390 | +proc xHome*(r: INT; slot: I64); | |
| 2391 | +begin | |
| 2392 | + homes[xIndex(r)] := slot; | |
| 2393 | +end; | |
| 2394 | + | |
| 2395 | +proc markCode*(var m: CodeMark); | |
| 2396 | +begin | |
| 2397 | + m.pc := Chunk.pc; m.fixups := Chunk.numFixups; m.lines := Chunk.numLines; m.said := X.saidCount(); | |
| 2398 | + m.fails := numFails; m.exit := exitL; | |
| 2399 | +end; | |
| 2400 | + | |
| 2401 | +(* The code made since m taken back, as if it had never been made; the | |
| 2402 | + frame is left as it is *) | |
| 2403 | +proc rewind*(m: CodeMark); | |
| 2404 | +begin | |
| 2405 | + Chunk.rewindTo(m.pc, m.fixups, m.lines); | |
| 2406 | + X.unsay(m.said); | |
| 2407 | + numFails := m.fails; | |
| 2408 | + exitL := m.exit; (* a try or return in it made jumps to the exit *) | |
| 2409 | +end; | |
| 2410 | + | |
| 2411 | +(* For a second try at the body: the code as it was at m, and the frame | |
| 2412 | + as useRegs found it *) | |
| 2413 | +proc redo*(m: CodeMark); | |
| 2414 | +begin | |
| 2415 | + Chunk.rewindTo(m.pc, m.fixups, m.lines); | |
| 2416 | + X.unsay(m.said); | |
| 2417 | + numFails := 0; | |
| 2418 | + lost := false; | |
| 2419 | + frameTop := frameAt[0]; frameMax := frameAt[1]; permTop := frameAt[2]; | |
| 2420 | + exitL := NoLabel; | |
| 2421 | +end; | |
| 2422 | + | |
| 2423 | +(* The prologue, which is for the procedure's first line. Only the | |
| 2424 | + locals in memory are zeroed, from the parameters' slots down to | |
| 2425 | + zeroTop: the parameters are stored over their slots next, the | |
| 2426 | + registers of variables are zeroed in place, their save slots are | |
| 2427 | + written before they are read, and so is every temporary. *) | |
| 2428 | +proc enterBody*(c, line: INT); | |
| 2429 | +var i, loop, r: INT; n: I64; p: ^Sym.Obj; | |
| 2430 | +begin | |
| 2431 | + Chunk.beginChunk(c); | |
| 2432 | + codeChunk := c; | |
| 2433 | + Chunk.noteLine(line, 1); (* a declaration starts a line *) | |
| 2434 | + X.push(RBP); | |
| 2435 | + X.movRR(RBP, RSP); | |
| 2436 | + X.say("sub rsp, frame"); | |
| 2437 | + (* sub rsp, imm32: always the form with a 32-bit field, so that endProc | |
| 2438 | + can patch the frame's size in *) | |
| 2439 | + Chunk.put1(48H); Chunk.put1(81H); Chunk.put1(0ECH); | |
| 2440 | + frameFix := Chunk.pc; Chunk.put4(0); | |
| 2441 | + n := (Sym.alignUp(zeroTop, 8) - paramTop) div 8; | |
| 2442 | + if n = 1 then | |
| 2443 | + X.movRI(RAX, 0); | |
| 2444 | + X.store(X.mem(RBP, -paramTop - 8), RAX, 8); | |
| 2445 | + elsif n > 1 then | |
| 2446 | + (* the locals, 16 bytes at a time, from xmm8, which holds nothing | |
| 2447 | + yet (xmm0 to xmm7 may hold a parameter): up to 16 stores in a | |
| 2448 | + row, else a loop with r10 running from minus the bytes up to 0, | |
| 2449 | + the store's address scaled by 1. An odd quadword is the | |
| 2450 | + lowest. *) | |
| 2451 | + X.pxorXX(8); | |
| 2452 | + if n <= 32 then | |
| 2453 | + for i := 0 to INT(n div 2) - 1 do | |
| 2454 | + X.movdquMX(X.mem(RBP, -paramTop - 16 * I64(i + 1)), 8); | |
| 2455 | + end; | |
| 2456 | + else | |
| 2457 | + X.movRI(R10, -16 * (n div 2)); | |
| 2458 | + loop := Chunk.pc; | |
| 2459 | + X.movdquMX(X.memIdx(RBP, R10, 1, -paramTop), 8); | |
| 2460 | + X.aluI(X.aluAdd, R10, 16); | |
| 2461 | + X.jccBack(X.ccNE, loop); | |
| 2462 | + end; | |
| 2463 | + if n mod 2 = 1 then | |
| 2464 | + X.movRI(RAX, 0); | |
| 2465 | + X.store(X.mem(RBP, -paramTop - 8 * n), RAX, 8); | |
| 2466 | + end; | |
| 2467 | + end; | |
| 2468 | + (* each register that holds a variable is saved, for the caller, and | |
| 2469 | + zeroed, as a variable starts at zero *) | |
| 2470 | + Chunk.setSaves(c, regMask & ((1 shl XBit) - 1), INT(saveAt)); | |
| 2471 | + n := 0; | |
| 2472 | + for i := 0 to 4 do | |
| 2473 | + if (regMask & (1 shl i)) # 0 then | |
| 2474 | + X.store(X.mem(RBP, saveAt + 8 * n), VarRegs[i], 8); | |
| 2475 | + X.movRI(VarRegs[i], 0); | |
| 2476 | + INC(n); | |
| 2477 | + end; | |
| 2478 | + end; | |
| 2479 | + (* and each xmm register that holds a variable, zeroed, as its slot | |
| 2480 | + is: the caller keeps its own *) | |
| 2481 | + for i := 0 to LEN(XVarRegs) - 1 do | |
| 2482 | + if (regMask & (1 shl (XBit + i))) # 0 then | |
| 2483 | + X.movRI(RAX, 0); | |
| 2484 | + X.movqXR(XVarRegs[i] - XmmVar, RAX); | |
| 2485 | + end; | |
| 2486 | + end; | |
| 2487 | + (* the parameters that came in registers, to their slots or, for a | |
| 2488 | + register variable, to its register: a slice's second word, the one | |
| 2489 | + for the slot 8 bytes into its own, to its length's *) | |
| 2490 | + for i := 0 to numIn - 1 do | |
| 2491 | + if inReg[i] < 0 then | |
| 2492 | + X.movsMX(X.mem(RBP, inSlot[i]), -1 - inReg[i], false); | |
| 2493 | + if inObj[i]^.reg # 0 then X.movX(inObj[i]^.reg - XmmVar, -1 - inReg[i]); end; | |
| 2494 | + elsif (inObj[i] # nil) and (inObj[i]^.reg # 0) then | |
| 2495 | + r := inObj[i]^.reg; | |
| 2496 | + if inSlot[i] # inObj[i]^.val then r := inObj[i]^.reg2; end; | |
| 2497 | + if r # inReg[i] then X.movRR(r, inReg[i]); end; (* else it is kept where it is *) | |
| 2498 | + if inSize[i] < 8 then X.extend(r, inSize[i], inObj[i]^.typ^.signed); end; | |
| 2499 | + else | |
| 2500 | + X.store(X.mem(RBP, inSlot[i]), inReg[i], inSize[i]); | |
| 2501 | + end; | |
| 2502 | + end; | |
| 2503 | + (* a register variable's parameter that came on the stack, above the | |
| 2504 | + frame (val > 0), is loaded from there; one passed by its address | |
| 2505 | + (ind: var, or const) holds the address, 8 bytes, whatever its type: | |
| 2506 | + a var BOOL loaded as a byte was a bad pointer *) | |
| 2507 | + p := procParams; | |
| 2508 | + while p # nil do | |
| 2509 | + if (p^.reg # 0) and (p^.val > 0) then | |
| 2510 | + if p^.ind then | |
| 2511 | + X.load(p^.reg, X.mem(RBP, p^.val), 8, false); | |
| 2512 | + elsif p^.typ^.form = Sym.fReal then | |
| 2513 | + X.movsXM(p^.reg - XmmVar, X.mem(RBP, p^.val), p^.typ^.size = 4); | |
| 2514 | + elsif p^.typ^.form in {Sym.fInt, Sym.fBool, Sym.fEnum, Sym.fSet} then | |
| 2515 | + X.load(p^.reg, X.mem(RBP, p^.val), INT(p^.typ^.size), p^.typ^.signed); | |
| 2516 | + else | |
| 2517 | + X.load(p^.reg, X.mem(RBP, p^.val), 8, false); | |
| 2518 | + if p^.typ^.form = Sym.fSlice then X.load(p^.reg2, X.mem(RBP, p^.val + 8), 8, false); end; | |
| 2519 | + end; | |
| 2520 | + end; | |
| 2521 | + p := p^.next; | |
| 2522 | + end; | |
| 2523 | +end; | |
| 2524 | + | |
| 2525 | +(* Pops one of several results, a value of size bytes, into its place | |
| 2526 | + off bytes into the caller's, behind the hidden pointer. *) | |
| 2527 | +(* The frame slot that holds the hidden result pointer *) | |
| 2528 | +proc resultSlot*: I64; | |
| 2529 | +begin | |
| 2530 | + return retSlot; | |
| 2531 | +end; | |
| 2532 | + | |
| 2533 | +proc resultTo*(off: I64; size: INT); | |
| 2534 | +var r: INT; fl: BOOL; | |
| 2535 | +begin | |
| 2536 | + r := top(); | |
| 2537 | + fl := isReal(depth - 1); | |
| 2538 | + drop(); | |
| 2539 | + X.load(RCX, X.mem(RBP, retSlot), 8, false); | |
| 2540 | + if fl then X.movsMX(X.mem(RCX, off), r, size = 4); | |
| 2541 | + else X.store(X.mem(RCX, off), r, size); end; | |
| 2542 | +end; | |
| 2543 | + | |
| 2544 | +(* Pops an address, and copies size bytes from it to the result, off | |
| 2545 | + bytes into it. *) | |
| 2546 | +proc copyResult*(off, size: I64); | |
| 2547 | +begin | |
| 2548 | + clobberArgs(); | |
| 2549 | + popTo(RSI); | |
| 2550 | + X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2551 | + if off # 0 then X.lea(RDI, X.mem(RDI, off)); end; | |
| 2552 | + copyBytes(size); | |
| 2553 | +end; | |
| 2554 | + | |
| 2555 | +(* return: the result, a value on top, goes in rax or xmm0; two words, | |
| 2556 | + the two values on top, in rax and rdx; one behind the hidden pointer | |
| 2557 | + goes back with that pointer in rax. *) | |
| 2558 | +proc returnJump*; | |
| 2559 | +begin | |
| 2560 | + if resType # nil then | |
| 2561 | + if Sym.behindPointer(resType) then | |
| 2562 | + X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2563 | + elsif Sym.inTwoRegs(resType) then | |
| 2564 | + popTo(RDX); | |
| 2565 | + popTo(RAX); | |
| 2566 | + elsif resType^.form = Sym.fReal then | |
| 2567 | + xValueTo(depth - 1, 0); | |
| 2568 | + drop(); | |
| 2569 | + else | |
| 2570 | + popTo(RAX); | |
| 2571 | + end; | |
| 2572 | + end; | |
| 2573 | + X.jmp(exitL); | |
| 2574 | +end; | |
| 2575 | + | |
| 2576 | +(* The first of what try does where the error isn't nil (the report, 7.8): | |
| 2577 | + the machine's stack made even, as the deferred statements about to be | |
| 2578 | + run expect to find it. try can stand among the arguments of another | |
| 2579 | + call, where quadwords have been pushed. Nothing is lost: the frame is | |
| 2580 | + above rsp, and the code returns through the frame pointer. *) | |
| 2581 | +proc tryAlign*; | |
| 2582 | +begin | |
| 2583 | + X.aluI(X.aluAnd, RSP, -16); | |
| 2584 | +end; | |
| 2585 | + | |
| 2586 | +(* The procedure returns the error in register r, and a zero for each of | |
| 2587 | + its other results; it isn't rax, rcx, rdx or rdi, which this uses. *) | |
| 2588 | +proc returnError(r: INT); | |
| 2589 | +var f: ^Sym.Obj; | |
| 2590 | +begin | |
| 2591 | + if Sym.behindPointer(resType) then | |
| 2592 | + X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2593 | + zeroBytes(resType^.size); | |
| 2594 | + f := resType^.fields; | |
| 2595 | + while f^.next # nil do f := f^.next; end; | |
| 2596 | + X.load(RDX, X.mem(RBP, retSlot), 8, false); | |
| 2597 | + X.store(X.mem(RDX, f^.val), r, 8); | |
| 2598 | + X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2599 | + elsif Sym.inTwoRegs(resType) then | |
| 2600 | + X.movRR(RDX, r); | |
| 2601 | + X.movRI(RAX, 0); | |
| 2602 | + else | |
| 2603 | + X.movRR(RAX, r); | |
| 2604 | + end; | |
| 2605 | + X.jmp(exitL); | |
| 2606 | +end; | |
| 2607 | + | |
| 2608 | +(* The rest of it: the procedure returns the error in the frame slot, and | |
| 2609 | + a zero for each of its other results. Only code is made: no entry of | |
| 2610 | + the stack of values is touched, since the code that follows is the | |
| 2611 | + code for an error that is nil, with the stack as it was. *) | |
| 2612 | +proc tryReturn*(slot: I64); | |
| 2613 | +begin | |
| 2614 | + X.load(RSI, X.mem(RBP, slot), 8, false); | |
| 2615 | + returnError(RSI); | |
| 2616 | +end; | |
| 2617 | + | |
| 2618 | +(* try, with no deferred statement to run: the error on top is tested in | |
| 2619 | + its register r, which is returned, and, if it isn't nil, returned from | |
| 2620 | + the procedure by tryReturnFrom, so it needs no slot in the frame. *) | |
| 2621 | +proc tryTest*(var ok: INT): INT; | |
| 2622 | +var r: INT; | |
| 2623 | +begin | |
| 2624 | + r := loaded(depth - 1); | |
| 2625 | + jumpIf(ok, false); | |
| 2626 | + return r; | |
| 2627 | +end; | |
| 2628 | + | |
| 2629 | +proc tryReturnFrom*(r: INT); | |
| 2630 | +begin | |
| 2631 | + returnError(r); | |
| 2632 | +end; | |
| 2633 | + | |
| 2634 | +(* The end of the procedure in chunk c: the code that returns, and the | |
| 2635 | + frame's size, known only now, patched into its prologue. If it is | |
| 2636 | + open, running off its end, it returns zeros. *) | |
| 2637 | +proc endProc*(c: INT; open: BOOL): ERROR; | |
| 2638 | +var n: I64; i: INT; | |
| 2639 | +begin | |
| 2640 | + codeChunk := -1; | |
| 2641 | + if (depth # 0) or (pushed # 0) then Diag.fatal("internal error: stack depth"); end; | |
| 2642 | + if open then | |
| 2643 | + if Sym.behindPointer(resType) then | |
| 2644 | + X.load(RDI, X.mem(RBP, retSlot), 8, false); | |
| 2645 | + X.movRI(RCX, resType^.size); | |
| 2646 | + X.movRI(RAX, 0); | |
| 2647 | + X.repStos(1); | |
| 2648 | + X.load(RAX, X.mem(RBP, retSlot), 8, false); | |
| 2649 | + else | |
| 2650 | + X.movRI(RAX, 0); | |
| 2651 | + if Sym.inTwoRegs(resType) then X.movRI(RDX, 0); end; | |
| 2652 | + X.movqXR(0, RAX); | |
| 2653 | + end; | |
| 2654 | + else | |
| 2655 | + X.dropJumpTo(exitL); | |
| 2656 | + end; | |
| 2657 | + X.fixLink(exitL); | |
| 2658 | + n := 0; | |
| 2659 | + for i := 0 to 4 do | |
| 2660 | + if (regMask & (1 shl i)) # 0 then | |
| 2661 | + X.load(VarRegs[i], X.mem(RBP, saveAt + 8 * n), 8, false); | |
| 2662 | + INC(n); | |
| 2663 | + end; | |
| 2664 | + end; | |
| 2665 | + X.leave(); | |
| 2666 | + X.ret(); | |
| 2667 | + endFails(); | |
| 2668 | + n := Sym.alignUp(frameMax, 16); | |
| 2669 | + if n < 16 then n := 16; end; | |
| 2670 | + if n > 2147483647 then return Scan.mark("frame too big"); end; | |
| 2671 | + Chunk.put4At(frameFix, INT(n)); | |
| 2672 | + (* the listing's text for the sub, which starts 3 bytes (REX.W, opcode, | |
| 2673 | + ModRM) before its immediate *) | |
| 2674 | + if X.listing then X.restate(frameFix - 3, Diag.say("sub rsp, {}", [Fmt.int(INT(n))])); end; | |
| 2675 | + Chunk.endChunk(c); | |
| 2676 | + return nil; | |
| 2677 | +end; | |
| 2678 | + | |
| 2679 | +(* A deferred statement is compiled where it stands, as a subroutine of | |
| 2680 | + its procedure that the code jumps over, in the procedure's own frame | |
| 2681 | + (the same rbp), so that it sees all its variables; deferBegin gives its | |
| 2682 | + address and what pushed was, and starts it, with the return address | |
| 2683 | + pushed. The stack is empty where it is called, and where it begins. *) | |
| 2684 | +proc deferBegin*(var L: INT): (INT, INT); | |
| 2685 | +var saved: INT; | |
| 2686 | +begin | |
| 2687 | + X.jmp(L); | |
| 2688 | + saved := pushed; | |
| 2689 | + pushed := 1; | |
| 2690 | + return Chunk.pc, saved; | |
| 2691 | +end; | |
| 2692 | + | |
| 2693 | +proc deferEnd*(L, saved: INT); | |
| 2694 | +begin | |
| 2695 | + keepTemps(); | |
| 2696 | + X.ret(); | |
| 2697 | + X.fixLink(L); | |
| 2698 | + pushed := saved; | |
| 2699 | +end; | |
| 2700 | + | |
| 2701 | +proc runDefer*(at: INT); | |
| 2702 | +begin | |
| 2703 | + X.callBack(at); | |
| 2704 | +end; | |
| 2705 | + | |
| 2706 | +(* The entry point, _start, and its chunk: it saves argc and argv, and | |
| 2707 | + the address of the places for Rt; runs every used module's body, then | |
| 2708 | + main; and ends with Rt.exit, its status main's or 0. A dynamic program | |
| 2709 | + starts as C's crt1 does, through __libc_start_main(main, argc, argv, | |
| 2710 | + nil, nil, rtld_fini, stack_end), with rsp aligned; its main first | |
| 2711 | + gives the library what it needs of C: Rt C's exit, in cExit, and | |
| 2712 | + Thread pthread_create and pthread_join. | |
| 2713 | + | |
| 2714 | + dynamic: the program uses C. first: the first module, whose chain | |
| 2715 | + gives the order of the bodies. mainObj: the program's main procedure, or | |
| 2716 | + nil. The first numCLinks of cLinks: C's symbols, each loaded from the | |
| 2717 | + quadword the loader fills in and stored in a variable of the | |
| 2718 | + library's, in a program that uses C. exitProc: the chunk of Rt's exit. The | |
| 2719 | + first numLinks of links: the addresses Rt is given, of what the program | |
| 2720 | + has: the places, and the profiler and calls; what it isn't given, | |
| 2721 | + nothing reaches, and the linker leaves out. | |
| 2722 | + | |
| 2723 | + bare: raz -bare, a program for the bare machine, a kernel's, with | |
| 2724 | + no Linux under it. Its entry point reads no argc and argv, which | |
| 2725 | + only Linux leaves on the stack, so that ARGC() is 0; and runs no body | |
| 2726 | + of Rt, whose system calls, to set the signals' handlers, would fault | |
| 2727 | + there. A kernel's own module sets up what Rt's body would. *) | |
| 2728 | +proc entryPoint*(dynamic, bare: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; | |
| 2729 | +var entry, start, i: INT; m: ^Sym.Module; | |
| 2730 | +begin | |
| 2731 | + entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); | |
| 2732 | + if dynamic then | |
| 2733 | + start := Chunk.newChunk(Chunk.secText, -1, internStr("main")); | |
| 2734 | + Chunk.beginChunk(entry); | |
| 2735 | + (* at the start rdx holds rtld_fini, from the dynamic loader, and the | |
| 2736 | + stack argc, then argv *) | |
| 2737 | + X.movRI(X.RBP, 0); (* no frame above, where a trace stops *) | |
| 2738 | + X.movRR(R9, RDX); (* rtld_fini, the sixth argument *) | |
| 2739 | + X.pop(RSI); (* argc, the second *) | |
| 2740 | + X.movRR(RDX, RSP); (* argv, the third *) | |
| 2741 | + X.aluI(X.aluAnd, RSP, -16); | |
| 2742 | + X.push(RAX); (* padding, so that the next push, the seventh *) | |
| 2743 | + X.push(RSP); (* argument, stack_end, leaves rsp aligned *) | |
| 2744 | + X.movRI(R8, 0); (* fini and init: none *) | |
| 2745 | + X.movRI(RCX, 0); | |
| 2746 | + X.lea(RDI, X.memRip(start, 0)); (* main, the first argument *) | |
| 2747 | + X.callM(X.memRip(Chunk.externChunk(internStr("__libc_start_main")), 0)); | |
| 2748 | + X.ud2(); (* it never returns *) | |
| 2749 | + Chunk.endChunk(entry); | |
| 2750 | + Chunk.beginChunk(start); | |
| 2751 | + X.push(RBP); (* aligns the stack again *) | |
| 2752 | + X.store(X.memRip(argcChunk, 0), RDI, 8); | |
| 2753 | + X.store(X.memRip(argvChunk, 0), RSI, 8); | |
| 2754 | + for i := 0 to numCLinks - 1 do | |
| 2755 | + X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); | |
| 2756 | + X.store(X.memRip(cLinks[i].into, 0), RAX, 8); | |
| 2757 | + end; | |
| 2758 | + elsif bare then | |
| 2759 | + start := entry; | |
| 2760 | + Chunk.beginChunk(entry); | |
| 2761 | + else | |
| 2762 | + start := entry; | |
| 2763 | + Chunk.beginChunk(entry); | |
| 2764 | + X.load(RAX, X.mem(RSP, 0), 8, false); | |
| 2765 | + X.store(X.memRip(argcChunk, 0), RAX, 8); | |
| 2766 | + X.lea(RAX, X.mem(RSP, 8)); | |
| 2767 | + X.store(X.memRip(argvChunk, 0), RAX, 8); | |
| 2768 | + end; | |
| 2769 | + for i := 0 to numLinks - 1 do | |
| 2770 | + X.lea(RAX, X.memRip(links[i].chunk, 0)); | |
| 2771 | + X.store(X.memRip(links[i].into, 0), RAX, 8); | |
| 2772 | + end; | |
| 2773 | + m := first; | |
| 2774 | + while m # nil do | |
| 2775 | + if m^.used and (m^.body >= 0) and not (bare and (m^.name = internStr("Rt"))) then X.callChunk(m^.body); end; | |
| 2776 | + m := m^.next; | |
| 2777 | + end; | |
| 2778 | + if mainObj # nil then X.callChunk(mainObj^.chunk); end; | |
| 2779 | + if (mainObj # nil) and (mainObj^.typ^.base # nil) then X.movRR(RDI, RAX); else X.movRI(RDI, 0); end; | |
| 2780 | + X.callChunk(exitProc); | |
| 2781 | + X.ud2(); (* exit never returns: trap if it does *) | |
| 2782 | + Chunk.endChunk(start); | |
| 2783 | + (* Rt.fail, for the failures of each kind: the place is where the | |
| 2784 | + check's call is. The return address is the next instruction's, which | |
| 2785 | + may belong to another line; one byte back is inside the call, so the | |
| 2786 | + lookup finds the call's line. rsp is aligned for the call, as the | |
| 2787 | + check's code may have pushed an odd number of quadwords. rdi and rsi | |
| 2788 | + as the failing code had them go on the stack, as Rt.fail's last two | |
| 2789 | + arguments, and a comes in rax (endFails). *) | |
| 2790 | + for i := 0 to LEN(failStubs) - 1 do | |
| 2791 | + if failStubs[i] >= 0 then | |
| 2792 | + Chunk.beginChunk(failStubs[i]); | |
| 2793 | + X.load(R8, X.mem(RSP, 0), 8, false); | |
| 2794 | + X.aluI(X.aluSub, R8, 1); | |
| 2795 | + X.aluI(X.aluAnd, RSP, -16); | |
| 2796 | + X.push(RSI); | |
| 2797 | + X.push(RDI); | |
| 2798 | + X.movRR(RSI, RAX); | |
| 2799 | + X.movRI(RDI, i); | |
| 2800 | + X.callChunk(rtFail); | |
| 2801 | + X.ud2(); | |
| 2802 | + Chunk.endChunk(failStubs[i]); | |
| 2803 | + end; | |
| 2804 | + end; | |
| 2805 | + return entry; | |
| 2806 | +end; | |
| 2807 | + | |
| 2808 | +(* A code procedure: its instructions, read by the assembler (X64), | |
| 2809 | + are the whole of chunk c *) | |
| 2810 | +proc codeProc*(c: INT; constExpr: X.ConstFn; nameFn: X.NameFn): ERROR; | |
| 2811 | +begin | |
| 2812 | + Chunk.beginChunk(c); | |
| 2813 | + try X.assemble(constExpr, nameFn); | |
| 2814 | + Chunk.endChunk(c); | |
| 2815 | + return nil; | |
| 2816 | +end; | |
| 2817 | + | |
| 2818 | +(* Whether the code procedure in chunk c is a square root of xmm0 in | |
| 2819 | + itself, then ret, as Math.sqrt is: | |
| 2820 | + | |
| 2821 | + sqrtsd xmm0, xmm0 F2 0F 51 C0 | |
| 2822 | + ret C3 | |
| 2823 | + | |
| 2824 | + If so, its prefix and opcode, F251H (F351H for sqrtss), which a call of it does in place | |
| 2825 | + on the value's own register (realInPlace); else 0. A call would spill | |
| 2826 | + the stack's reals and reload the variables' xmm registers around it, | |
| 2827 | + as System V keeps none through one, for an instruction of four bytes. *) | |
| 2828 | +proc oneRealOp*(c: INT): INT; | |
| 2829 | +var s: INT; | |
| 2830 | +begin | |
| 2831 | + s := Chunk.chunks[c].start; | |
| 2832 | + if (Chunk.chunks[c].size # 5) or (Chunk.chunks[c].numFix # 0) then return 0; end; | |
| 2833 | + if ((Chunk.text[s] # 0F2H) and (Chunk.text[s] # 0F3H)) or (Chunk.text[s + 1] # 0FH) | |
| 2834 | + or (Chunk.text[s + 2] # 51H) or (Chunk.text[s + 3] # 0C0H) or (Chunk.text[s + 4] # 0C3H) then | |
| 2835 | + return 0; | |
| 2836 | + end; | |
| 2837 | + return INT(Chunk.text[s]) * 256 + INT(Chunk.text[s + 2]); | |
| 2838 | +end; | |
| 2839 | + | |
| 2840 | +(* The operation op, of oneRealOp, on the real on top, in its register: | |
| 2841 | + Math.sqrt(d2) is sqrtsd xmm8, xmm8, with no call. *) | |
| 2842 | +proc realInPlace*(op: INT); | |
| 2843 | +var x: INT; | |
| 2844 | +begin | |
| 2845 | + x := top(); | |
| 2846 | + X.sse(op & 0FFH, x, x, op shr 8 = 0F3H); | |
| 2847 | +end; | |
| 2848 | + | |
| 2849 | +var i: INT; | |
| 2850 | +begin | |
| 2851 | + for i := 0 to LEN(failStubs) - 1 do failStubs[i] := -1; end; | |
| 2852 | + inPlace := -1; | |
| 2853 | + codeChunk := -1; | |
| 2854 | + resetStack(0, 0); | |
| 2855 | + rtFail := Chunk.newChunk(Chunk.secText, internStr("Rt"), internStr("fail")); | |
| 2856 | + argcChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argc")); | |
| 2857 | + Chunk.setBss(argcChunk, 8, 8); | |
| 2858 | + argvChunk := Chunk.newChunk(Chunk.secBss, -1, internStr("argv")); | |
| 2859 | + Chunk.setBss(argvChunk, 8, 8); | |
| 2860 | +end Gen. |
| @@ -0,0 +1,103 @@ | ||
| 1 | +(* Host: what the compiler asks of the system it runs on, beyond the | |
| 2 | + library. The host is the system running raz; the target, the one its | |
| 3 | + program will run on. This file is Linux's. | |
| 4 | + | |
| 5 | + Another host stages its own file in its place: copy src/ to a | |
| 6 | + directory, put that host's Host.raz there instead of this one, and | |
| 7 | + build the copy with the host's library (doc/compiler.md says how). | |
| 8 | + Raz and Exe import only this module for what follows, so a host | |
| 9 | + gives these five procedures and keeps every export: | |
| 10 | + | |
| 11 | + libraryDir where the standard library is, as a directory path | |
| 12 | + cLibrary which C library a program that uses C is linked | |
| 13 | + against by default, or none if the host can't make one | |
| 14 | + writeExecutable the file at path made to hold an executable | |
| 15 | + prepareRun the path raz run writes its program to, or an error | |
| 16 | + if this host can't run it; asked first, before any | |
| 17 | + source is read, so its error is the refusal | |
| 18 | + execute replaces the compiler with the program written there | |
| 19 | + | |
| 20 | + A host that can't run programs refuses in prepareRun and gives | |
| 21 | + execute any body; one that has no C gives cLibrary the value none. | |
| 22 | + Strings it returns must live until the compiler exits. | |
| 23 | + | |
| 24 | + Depends on: Sys and Files for Linux's services, Scan for memory that | |
| 25 | + lasts until exit, Diag and Fmt for paths. Leaves out: compiling and | |
| 26 | + the executable format, which are Parse and Exe's. *) | |
| 27 | +module Host; | |
| 28 | +import Sys, Files, Fmt, Diag, Scan; | |
| 29 | +import SYSTEM; | |
| 30 | + | |
| 31 | +type | |
| 32 | + CLib* = (none, glibc, musl); (* the C library to link against *) | |
| 33 | + | |
| 34 | +const | |
| 35 | + LoaderGlibc = "/lib64/ld-linux-x86-64.so.2"; | |
| 36 | + LoaderMusl = "/lib/ld-musl-x86_64.so.1"; | |
| 37 | + MemfdCreate = 319; (* the system calls raz run makes *) | |
| 38 | + MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) | |
| 39 | + Execve = 59; | |
| 40 | + Mode755 = 1EDH; (* rwxr-xr-x *) | |
| 41 | + CreateRun: ERROR = "can't create a memfd"; | |
| 42 | + | |
| 43 | +(* The directory of the library, ending in what the name of raz leaves, | |
| 44 | + "lib": the one beside raz as the system says (a link to raz is | |
| 45 | + followed), or, with no /proc, beside argument 0 as it was typed *) | |
| 46 | +proc libraryDir*: []const U8; | |
| 47 | +var path: []const U8; error, i: INT; | |
| 48 | +begin | |
| 49 | + path, error := Sys.readLink("/proc/self/exe"); | |
| 50 | + if error # 0 then path := Files.arg(0); end; | |
| 51 | + i := LEN(path); | |
| 52 | + while (i > 0) and (path[i - 1] # '/') do DEC(i); end; | |
| 53 | + if i = 0 then return "lib"; end; | |
| 54 | + return Diag.say("{}lib", [Fmt.str(path[0..i])]); | |
| 55 | +end; | |
| 56 | + | |
| 57 | +(* The C library a program that uses C is linked against when -libc | |
| 58 | + doesn't say: glibc, or musl only if the machine has musl's loader and | |
| 59 | + not glibc's, as Alpine has *) | |
| 60 | +proc cLibrary*: CLib; | |
| 61 | +begin | |
| 62 | + if (Sys.access(LoaderGlibc, {}) # 0) and (Sys.access(LoaderMusl, {}) = 0) then return musl; end; | |
| 63 | + return glibc; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +(* The file at path made to hold data, an executable: written, then | |
| 67 | + given mode 755. False if either failed. *) | |
| 68 | +proc writeExecutable*(path, data: []const U8): BOOL; | |
| 69 | +begin | |
| 70 | + return Files.write(path, data) and (Sys.chmod(path, Mode755) >= 0); | |
| 71 | +end; | |
| 72 | + | |
| 73 | +(* s as a C string, in memory that is never freed *) | |
| 74 | +proc cstr(s: []const U8): ^U8; | |
| 75 | +var p: ^U8; | |
| 76 | +begin | |
| 77 | + p := Scan.grab(LEN(s) + 1); | |
| 78 | + if LEN(s) > 0 then COPY(p[0..LEN(s)], s); end; | |
| 79 | + p[LEN(s)] := 0; | |
| 80 | + return p; | |
| 81 | +end; | |
| 82 | + | |
| 83 | +(* The path raz run writes its program to: a memfd, a file that lives | |
| 84 | + only in memory. Its descriptor stays open until execute, whose | |
| 85 | + close-on-exec flag lets the kernel release it, or until the compiler | |
| 86 | + ends after an error. *) | |
| 87 | +proc prepareRun*: ([]const U8, ERROR); | |
| 88 | +var fd: INT; | |
| 89 | +begin | |
| 90 | + fd := SYSTEM.SYSCALL(MemfdCreate, cstr("raz"), MfdCloexec); | |
| 91 | + if fd < 0 then return "", CreateRun; end; | |
| 92 | + return Diag.say("/proc/self/fd/{}", [Fmt.int(fd)]), nil; | |
| 93 | +end; | |
| 94 | + | |
| 95 | +(* The program at path runs in the compiler's place, with the compiler's | |
| 96 | + arguments from number first on as its own, first as its argument 0, | |
| 97 | + and the environment. It returns only if that failed: minus the errno. *) | |
| 98 | +proc execute*(path: []const U8; first: INT): INT; | |
| 99 | +begin | |
| 100 | + return SYSTEM.SYSCALL(Execve, cstr(path), SYSTEM.ARGV() + first, SYSTEM.ARGV() + (Files.argCount() + 1)); | |
| 101 | +end; | |
| 102 | + | |
| 103 | +end Host. |
| @@ -1,103 +0,0 @@ | ||
| 1 | -(* Host: what the compiler asks of the system it runs on, beyond the | |
| 2 | - library. The host is the system running raz; the target, the one its | |
| 3 | - program will run on. This file is Linux's. | |
| 4 | - | |
| 5 | - Another host stages its own file in its place: copy src/ to a | |
| 6 | - directory, put that host's Host.raz there instead of this one, and | |
| 7 | - build the copy with the host's library (doc/compiler.md says how). | |
| 8 | - Raz and Elf import only this module for what follows, so a host | |
| 9 | - gives these five procedures and keeps every export: | |
| 10 | - | |
| 11 | - libraryDir where the standard library is, as a directory path | |
| 12 | - cLibrary which C library a program that uses C is linked | |
| 13 | - against by default, or none if the host can't make one | |
| 14 | - writeExecutable the file at path made to hold an executable | |
| 15 | - prepareRun the path raz run writes its program to, or an error | |
| 16 | - if this host can't run it; asked first, before any | |
| 17 | - source is read, so its error is the refusal | |
| 18 | - execute replaces the compiler with the program written there | |
| 19 | - | |
| 20 | - A host that can't run programs refuses in prepareRun and gives | |
| 21 | - execute any body; one that has no C gives cLibrary the value none. | |
| 22 | - Strings it returns must live until the compiler exits. | |
| 23 | - | |
| 24 | - Depends on: Sys and Files for Linux's services, Scan for memory that | |
| 25 | - lasts until exit, Diag and Fmt for paths. Leaves out: compiling and | |
| 26 | - the ELF format, which are Parse and Elf's. *) | |
| 27 | -module Host; | |
| 28 | -import Sys, Files, Fmt, Diag, Scan; | |
| 29 | -import SYSTEM; | |
| 30 | - | |
| 31 | -type | |
| 32 | - CLib* = (none, glibc, musl); (* the C library to link against *) | |
| 33 | - | |
| 34 | -const | |
| 35 | - LoaderGlibc = "/lib64/ld-linux-x86-64.so.2"; | |
| 36 | - LoaderMusl = "/lib/ld-musl-x86_64.so.1"; | |
| 37 | - MemfdCreate = 319; (* the system calls raz run makes *) | |
| 38 | - MfdCloexec = 1; (* an ELF still executes: the kernel opens it first *) | |
| 39 | - Execve = 59; | |
| 40 | - Mode755 = 1EDH; (* rwxr-xr-x *) | |
| 41 | - CreateRun: ERROR = "can't create a memfd"; | |
| 42 | - | |
| 43 | -(* The directory of the library, ending in what the name of raz leaves, | |
| 44 | - "lib": the one beside raz as the system says (a link to raz is | |
| 45 | - followed), or, with no /proc, beside argument 0 as it was typed *) | |
| 46 | -proc libraryDir*: []const U8; | |
| 47 | -var path: []const U8; error, i: INT; | |
| 48 | -begin | |
| 49 | - path, error := Sys.readLink("/proc/self/exe"); | |
| 50 | - if error # 0 then path := Files.arg(0); end; | |
| 51 | - i := LEN(path); | |
| 52 | - while (i > 0) and (path[i - 1] # '/') do DEC(i); end; | |
| 53 | - if i = 0 then return "lib"; end; | |
| 54 | - return Diag.say("{}lib", [Fmt.str(path[0..i])]); | |
| 55 | -end; | |
| 56 | - | |
| 57 | -(* The C library a program that uses C is linked against when -libc | |
| 58 | - doesn't say: glibc, or musl only if the machine has musl's loader and | |
| 59 | - not glibc's, as Alpine has *) | |
| 60 | -proc cLibrary*: CLib; | |
| 61 | -begin | |
| 62 | - if (Sys.access(LoaderGlibc, {}) # 0) and (Sys.access(LoaderMusl, {}) = 0) then return musl; end; | |
| 63 | - return glibc; | |
| 64 | -end; | |
| 65 | - | |
| 66 | -(* The file at path made to hold data, an executable: written, then | |
| 67 | - given mode 755. False if either failed. *) | |
| 68 | -proc writeExecutable*(path, data: []const U8): BOOL; | |
| 69 | -begin | |
| 70 | - return Files.write(path, data) and (Sys.chmod(path, Mode755) >= 0); | |
| 71 | -end; | |
| 72 | - | |
| 73 | -(* s as a C string, in memory that is never freed *) | |
| 74 | -proc cstr(s: []const U8): ^U8; | |
| 75 | -var p: ^U8; | |
| 76 | -begin | |
| 77 | - p := Scan.grab(LEN(s) + 1); | |
| 78 | - if LEN(s) > 0 then COPY(p[0..LEN(s)], s); end; | |
| 79 | - p[LEN(s)] := 0; | |
| 80 | - return p; | |
| 81 | -end; | |
| 82 | - | |
| 83 | -(* The path raz run writes its program to: a memfd, a file that lives | |
| 84 | - only in memory. Its descriptor stays open until execute, whose | |
| 85 | - close-on-exec flag lets the kernel release it, or until the compiler | |
| 86 | - ends after an error. *) | |
| 87 | -proc prepareRun*: ([]const U8, ERROR); | |
| 88 | -var fd: INT; | |
| 89 | -begin | |
| 90 | - fd := SYSTEM.SYSCALL(MemfdCreate, cstr("raz"), MfdCloexec); | |
| 91 | - if fd < 0 then return "", CreateRun; end; | |
| 92 | - return Diag.say("/proc/self/fd/{}", [Fmt.int(fd)]), nil; | |
| 93 | -end; | |
| 94 | - | |
| 95 | -(* The program at path runs in the compiler's place, with the compiler's | |
| 96 | - arguments from number first on as its own, first as its argument 0, | |
| 97 | - and the environment. It returns only if that failed: minus the errno. *) | |
| 98 | -proc execute*(path: []const U8; first: INT): INT; | |
| 99 | -begin | |
| 100 | - return SYSTEM.SYSCALL(Execve, cstr(path), SYSTEM.ARGV() + first, SYSTEM.ARGV() + (Files.argCount() + 1)); | |
| 101 | -end; | |
| 102 | - | |
| 103 | -end Host. |
| @@ -9,12 +9,12 @@ | ||
| 9 | 9 | text right after the headers, rodata on the next page, then a |
| 10 | 10 | writable page that starts with the table the dynamic loader reads |
| 11 | 11 | (none in a static program) and goes on as bss, in memory only. The |
| 12 | - loader's other tables come at the end of rodata; Elf writes them | |
| 12 | + loader's other tables come at the end of rodata; Exe writes them | |
| 13 | 13 | in the room left there. |
| 14 | 14 | |
| 15 | 15 | Depends on: Chunk for chunks and fixups, Scan for names and memory, |
| 16 | 16 | Diag for errors and paths. |
| 17 | - Leaves out: the file format; that is Elf. *) | |
| 17 | + Leaves out: the file format; that is Exe. *) | |
| 18 | 18 | module Link; |
| 19 | 19 | import Fmt, Diag, Scan, Chunk; |
| 20 | 20 | import SYSTEM; |
| @@ -82,7 +82,6 @@ | ||
| 82 | 82 | MaxPending = 64; (* modules being compiled at once *) |
| 83 | 83 | MaxFors = 256; (* for loops of a procedure whose limit may go in a register *) |
| 84 | 84 | MaxNest = 256; (* statements, expressions and types nested; each level is a few recursive calls of the parser *) |
| 85 | - Suffixes: [4][]const U8 = [".linux-x86_64", ".linux", ".x86_64", ""]; | |
| 86 | 85 | |
| 87 | 86 | var |
| 88 | 87 | libDir*, mainDir: []const U8; |
| @@ -90,6 +89,7 @@ | ||
| 90 | 89 | numInc*: INT; |
| 91 | 90 | userDirs: [][]const U8; (* RAZ_PATH, or HOME/.raz/lib; set by the driver *) |
| 92 | 91 | thisVersion: []const U8; (* the compiler's version; set by the driver *) |
| 92 | + targetOs, targetArch: []const U8; (* the target's names, Exe.Os and Gen.Arch; set by the driver *) | |
| 93 | 93 | originNote: []const U8; (* for the module about to be opened, see libraryNote *) |
| 94 | 94 | firstModule*, lastModule*: PModule; (* in the order they were finished *) |
| 95 | 95 | |
| @@ -174,6 +174,14 @@ | ||
| 174 | 174 | userDirs := dirs; |
| 175 | 175 | end; |
| 176 | 176 | |
| 177 | +(* The target's operating system and machine, as the suffixes of a | |
| 178 | + module's files say them: Exe.Os and Gen.Arch, which the driver gives *) | |
| 179 | +proc setTarget*(os, arch: []const U8); | |
| 180 | +begin | |
| 181 | + targetOs := os; | |
| 182 | + targetArch := arch; | |
| 183 | +end; | |
| 184 | + | |
| 177 | 185 | (* The driver's version of the compiler, which a library's note names *) |
| 178 | 186 | proc setVersion*(v: []const U8); |
| 179 | 187 | begin |
| @@ -4715,11 +4723,19 @@ | ||
| 4715 | 4723 | return libDir; |
| 4716 | 4724 | end; |
| 4717 | 4725 | |
| 4718 | -(* The file of module n in dir that suffix j says: Name.linux-x86_64.raz | |
| 4719 | - for a file of this target alone, and so on to Name.raz *) | |
| 4726 | +(* The file of module n in dir that suffix j says: Name.OS-ARCH.raz for | |
| 4727 | + a file of this target alone (Name.linux-x86_64.raz, say), then | |
| 4728 | + Name.OS.raz, Name.ARCH.raz, and Name.raz *) | |
| 4720 | 4729 | proc fileName(n, j: INT): []const U8; |
| 4721 | 4730 | begin |
| 4722 | - return Diag.say("{}{}.raz", [Fmt.str(nameOf(n)), Fmt.str(Suffixes[j])]); | |
| 4731 | + if j = 0 then | |
| 4732 | + return Diag.say("{}.{}-{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetOs), Fmt.str(targetArch)]); | |
| 4733 | + elsif j = 1 then | |
| 4734 | + return Diag.say("{}.{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetOs)]); | |
| 4735 | + elsif j = 2 then | |
| 4736 | + return Diag.say("{}.{}.raz", [Fmt.str(nameOf(n)), Fmt.str(targetArch)]); | |
| 4737 | + end; | |
| 4738 | + return Diag.say("{}.raz", [Fmt.str(nameOf(n))]); | |
| 4723 | 4739 | end; |
| 4724 | 4740 | |
| 4725 | 4741 | proc moduleFile(n: INT): ([]const U8, []const U8, BOOL); |
| @@ -77,8 +77,8 @@ | ||
| 77 | 77 | 32 bits and goes on, since an operand has nowhere to return an error |
| 78 | 78 | to; the compile ends with the message shown. The listing's text is |
| 79 | 79 | made by `Diag.say`, which keeps it, and only when `-S` asks for it. |
| 80 | -- **`Link` and `Elf`.** What `raz.pas` set there has a setter each: | |
| 81 | - `Link.setPlaces`, `Elf.needLib`, `noLibs`, `setMainPath`, | |
| 80 | +- **`Link` and `Exe`.** What `raz.pas` set there has a setter each: | |
| 81 | + `Link.setPlaces`, `Exe.needLib`, `noLibs`, `setMainPath`, | |
| 82 | 82 | `setStripped` and `X64.setListing`. The file is written by |
| 83 | 83 | `Files.write` and made executable by `Sys.chmod`, in place of one |
| 84 | 84 | `open` with mode 755 (which the umask would trim). |
| @@ -134,7 +134,7 @@ | ||
| 134 | 134 | library beside it, it reads from `/proc/self/exe` as FPC's `ParamStr(0)` |
| 135 | 135 | does: `Files.arg(0)` is what the shell was typed, and `./raz` made the |
| 136 | 136 | library `./lib`, so the same program held other paths in its file. |
| 137 | - `raz run` writes the memfd with `Elf.writeElf` and executes it with | |
| 137 | + `raz run` writes the memfd with `Exe.write` and executes it with | |
| 138 | 138 | the system call, `execve`, which `Sys` has no procedure for. The |
| 139 | 139 | version is the module `Version`, which `check.sh` makes in |
| 140 | 140 | `build/src/` from `build/version.inc`, as the seed's `{$I}` is. |
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | Leaves out: the work itself; the other units compile, link and |
| 35 | 35 | write, and this only reads the command line and runs them in order. *) |
| 36 | 36 | module Raz; |
| 37 | -import Sys, Out, Str, Version, Files, Host, Fmt, Diag, Scan, Sym, Chunk, X64, Gen, Parse, Link, Elf; | |
| 37 | +import Sys, Out, Str, Version, Files, Host, Fmt, Diag, Scan, Sym, Chunk, Gen, Parse, Link, Exe; | |
| 38 | 38 | import SYSTEM; |
| 39 | 39 | |
| 40 | 40 | const |
| @@ -72,20 +72,20 @@ | ||
| 72 | 72 | end; |
| 73 | 73 | |
| 74 | 74 | (* Whether the program uses C: if so, the libraries it needs go to |
| 75 | - Elf, the C library first. *) | |
| 75 | + Exe, the C library first. *) | |
| 76 | 76 | proc usesC: BOOL; |
| 77 | 77 | var m, st, c: ^Sym.Module; l: ^Sym.Lib; |
| 78 | 78 | begin |
| 79 | 79 | st := nil; c := nil; |
| 80 | - Elf.noLibs(); | |
| 81 | - _ := Elf.needLib(internStr(Elf.libc)); | |
| 80 | + Exe.noLibs(); | |
| 81 | + _ := Exe.needLib(internStr(Exe.libc)); | |
| 82 | 82 | m := Parse.firstModule; |
| 83 | 83 | while m # nil do |
| 84 | 84 | if m^.used and m^.linkStatic then st := m; end; |
| 85 | 85 | if m^.used and m^.usesC then c := m; end; |
| 86 | 86 | l := m^.libs; |
| 87 | 87 | while m^.used and (l # nil) do |
| 88 | - if not Elf.needLib(l^.name) then Diag.fatal("more than 32 libraries"); end; | |
| 88 | + if not Exe.needLib(l^.name) then Diag.fatal("more than 32 libraries"); end; | |
| 89 | 89 | l := l^.next; |
| 90 | 90 | end; |
| 91 | 91 | m := m^.next; |
| @@ -93,7 +93,7 @@ | ||
| 93 | 93 | if (st # nil) and (c # nil) then |
| 94 | 94 | Diag.fatal(Diag.say("{} says link static, but {} uses C", [Fmt.str(Scan.nameOf(st^.name)), Fmt.str(Scan.nameOf(c^.name))])); |
| 95 | 95 | end; |
| 96 | - if c = nil then Elf.noLibs(); end; | |
| 96 | + if c = nil then Exe.noLibs(); end; | |
| 97 | 97 | return c # nil; |
| 98 | 98 | end; |
| 99 | 99 | |
| @@ -350,9 +350,9 @@ | ||
| 350 | 350 | Diag.setWerror(true); |
| 351 | 351 | elsif s = "-S" then |
| 352 | 352 | listing := true; |
| 353 | - X64.setListing(true); | |
| 353 | + Gen.setListing(true); | |
| 354 | 354 | elsif s = "-s" then |
| 355 | - Elf.setStripped(true); | |
| 355 | + Exe.setStripped(true); | |
| 356 | 356 | elsif s = "-base" then |
| 357 | 357 | INC(i); |
| 358 | 358 | base, ok := address(Files.arg(i)); |
| @@ -371,7 +371,7 @@ | ||
| 371 | 371 | INC(i); |
| 372 | 372 | s := Files.arg(i); |
| 373 | 373 | if (s # "glibc") and (s # "musl") then Diag.fatal(say1("unknown C library {}: glibc or musl", s)); end; |
| 374 | - Elf.setMusl(s = "musl"); | |
| 374 | + Exe.setMusl(s = "musl"); | |
| 375 | 375 | libcSaid := true; |
| 376 | 376 | elsif (s = "-h") or (s = "--help") then |
| 377 | 377 | usage(); |
| @@ -400,8 +400,9 @@ | ||
| 400 | 400 | outPath, err := Host.prepareRun(); |
| 401 | 401 | if err # nil then Diag.fatal(MSG(err)); end; |
| 402 | 402 | end; |
| 403 | - if not libcSaid then Elf.setMusl(Host.cLibrary() = Host.musl); end; | |
| 403 | + if not libcSaid then Exe.setMusl(Host.cLibrary() = Host.musl); end; | |
| 404 | 404 | Parse.setVersion(Version.Text); |
| 405 | + Parse.setTarget(Exe.Os, Gen.Arch); | |
| 405 | 406 | if LEN(libDir) = 0 then libDir := Host.libraryDir(); end; |
| 406 | 407 | Parse.setLibDir(libDir); |
| 407 | 408 | userLibraries(); |
| @@ -446,7 +447,7 @@ | ||
| 446 | 447 | -noprofile no profiler, and -bare no profiler, since Rt's body, |
| 447 | 448 | which starts it, doesn't run. *) |
| 448 | 449 | numLinks := 0; |
| 449 | - if not Elf.stripped then giveRtPlaces(rt, not noProfile and not bare); end; | |
| 450 | + if not Exe.stripped then giveRtPlaces(rt, not noProfile and not bare); end; | |
| 450 | 451 | (* what a program that uses C is given of C: Rt its exit, and |
| 451 | 452 | Thread, if the program has it, the calls that make and join C's |
| 452 | 453 | threads, so that the C library knows of them *) |
| @@ -475,17 +476,17 @@ | ||
| 475 | 476 | end; |
| 476 | 477 | if Diag.errors > 0 then Diag.finish(); end; |
| 477 | 478 | if listing then |
| 478 | - X64.list(m^.sname); | |
| 479 | + Gen.list(m^.sname); | |
| 479 | 480 | return 0; |
| 480 | 481 | end; |
| 481 | 482 | |
| 482 | - Elf.setMainPath(path); | |
| 483 | + Exe.setMainPath(path); | |
| 483 | 484 | if run then |
| 484 | - Elf.writeElf(outPath, entry); | |
| 485 | + Exe.write(outPath, entry); | |
| 485 | 486 | _ := Host.execute(outPath, first); (* the program's argv is ours from Main.raz on *) |
| 486 | 487 | Diag.fatal(say1("can't run {}", path)); |
| 487 | 488 | end; |
| 488 | - Elf.writeElf(outPath, entry); | |
| 489 | + Exe.write(outPath, entry); | |
| 489 | 490 | return 0; |
| 490 | 491 | end; |
| 491 | 492 |
| @@ -0,0 +1,59 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# A compiler that runs here and compiles for TARGET (OS-ARCH, as | |
| 3 | +# darwin-arm64 or linux-x86_64): raz built by staging, for the target | |
| 4 | +# is whatever files the compiler was built from (doc/raz.md, 3). | |
| 5 | +# | |
| 6 | +# src/cross.sh TARGET [OUT] run from the repository root | |
| 7 | +# | |
| 8 | +# Stage build/cross/TARGET/src/ holds src/'s generic files and, under | |
| 9 | +# the plain names, the target's own: Gen.ARCH.raz as Gen.raz and | |
| 10 | +# Exe.OS.raz as Exe.raz; and the host's, Host.OS.raz as Host.raz, the | |
| 11 | +# host being the system the compiler will run on, which is this one. | |
| 12 | +# With only plain names there, module lookup has one choice each. BOOT's | |
| 13 | +# release builds the stage as the Makefile builds raz, from a directory | |
| 14 | +# where src/Raz.raz is again at src/, so the paths a program holds are | |
| 15 | +# the same. The compiler is OUT, build/cross/TARGET/raz if not said; | |
| 16 | +# its library is lib/, beside it. | |
| 17 | +set -e | |
| 18 | +target=$1 | |
| 19 | +out=${2:-build/cross/$target/raz} | |
| 20 | +case $target in | |
| 21 | +*-*) ;; | |
| 22 | +*) echo "usage: src/cross.sh OS-ARCH [OUT]" >&2; exit 2;; | |
| 23 | +esac | |
| 24 | +os=${target%%-*} | |
| 25 | +arch=${target#*-} | |
| 26 | +host=$(uname -s | tr A-Z a-z) # the OS that runs the compiler | |
| 27 | + | |
| 28 | +# Each unit with files by target, and the file this build takes of it | |
| 29 | +missing= | |
| 30 | +for f in Gen.$arch.raz Exe.$os.raz Host.$host.raz; do | |
| 31 | + [ -f src/$f ] || missing="$missing src/$f" | |
| 32 | +done | |
| 33 | +if [ -n "$missing" ]; then | |
| 34 | + echo "cross.sh: no files for $target (host $host):$missing" >&2 | |
| 35 | + exit 1 | |
| 36 | +fi | |
| 37 | +[ -f build/src/Version.raz ] || make build/src/Version.raz | |
| 38 | +[ -x build/boot/raz ] || make build/boot/raz | |
| 39 | + | |
| 40 | +d=build/cross/$target | |
| 41 | +rm -rf $d | |
| 42 | +mkdir -p $d/src $d/v | |
| 43 | +for f in src/*.raz; do | |
| 44 | + case $f in | |
| 45 | + *.*.raz) ;; # a target's file, below | |
| 46 | + *) cp $f $d/src/;; | |
| 47 | + esac | |
| 48 | +done | |
| 49 | +cp src/Gen.$arch.raz $d/src/Gen.raz | |
| 50 | +cp src/Exe.$os.raz $d/src/Exe.raz | |
| 51 | +cp src/Host.$host.raz $d/src/Host.raz | |
| 52 | +cp build/src/Version.raz $d/v/ | |
| 53 | +ln -s ../../../lib $d/lib | |
| 54 | +boot=$PWD/build/boot/raz | |
| 55 | +(cd $d && $boot -Werror -I src -I v -o raz src/Raz.raz) | |
| 56 | +case $out in | |
| 57 | +build/cross/$target/raz) ;; | |
| 58 | +*) cp $d/raz "$out";; | |
| 59 | +esac |
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | #!/bin/sh |
| 2 | 2 | # Builds the compiler with another host, test/host/Host.raz in place of |
| 3 | -# src/Host.raz (the staging doc/compiler.md describes), and checks what | |
| 3 | +# src/Host.linux.raz (the staging doc/compiler.md describes), and checks what | |
| 4 | 4 | # that host changes: the library it names, a refused raz run, no C, and |
| 5 | 5 | # a refused write. |
| 6 | 6 | # Usage: test/compiler-host.sh path/to/raz |
| @@ -30,6 +30,8 @@ | ||
| 30 | 30 | # The staged sources and the compiler built from them. |
| 31 | 31 | mkdir "$top/src" "$top/work" |
| 32 | 32 | cp src/*.raz "$top/src" |
| 33 | +# staged as src/cross.sh does: the plain name takes the place of Linux's | |
| 34 | +rm "$top/src/Host.linux.raz" | |
| 33 | 35 | cp test/host/Host.raz "$top/src/Host.raz" |
| 34 | 36 | "$raz" -Werror -I "$top/src" -I build/src -o "$top/compiler" "$top/src/Raz.raz" 2>"$top/build.err" || { |
| 35 | 37 | cat "$top/build.err" |