raz / commit 69417b8a

commit
69417b8a296133209ff6396d27626982b219d485
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d9e9f115
compiler.md: the registers the code uses, a clear direction flag, and no red zone, read from Gen and X64

patch · log

1 file changed, +49 -0

doc/compiler.md (history) modified +49 -0

@@ -2390,6 +2390,55 @@
23902390 Under `-bare`, what `Rt`'s body does (handlers, tables, a first stack)
23912391 a kernel does in a module body of its own, which runs before `main`.
23922392
2393+**What the compiler's code does to the machine.** A kernel that
2394+interrupts raz code, and runs raz code from the handler, needs these
2395+facts; each was read in the source, where it says.
2396+
2397+- *Registers.* The code follows System V. It uses `rax`, `rcx`, `rdx`,
2398+ `rsi`, `rdi` and `r8` to `r11` freely (`r8` to `r11` also hold the
2399+ value stack, `Gen.Regs`; `r10` and `r11` are scratch in a prologue
2400+ and a call through a procedure value); `rbp` is the frame pointer;
2401+ `rbx` and `r12` to `r15` hold a procedure's busiest variables
2402+ (`Gen.VarRegs`) and are saved in its frame and restored on return,
2403+ so they live through calls. All of `xmm0` to `xmm15` may change: the
2404+ value stack's reals are in `xmm8` to `xmm11`, the variables' in
2405+ `xmm12` to `xmm15` (and `xmm4` to `xmm7` in a procedure that calls
2406+ nothing), `xmm8` is a scratch of the prologue, `xmm0` to `xmm7` carry
2407+ arguments and results; none is kept through a call, so a variable in
2408+ one is loaded again from its slot after it (`Gen.call`). The flags
2409+ change at will. The code uses no x87 register, no AVX, and never
2410+ writes `MXCSR`, so it counts on the default (round to nearest, all
2411+ exceptions masked). A 16 byte move of memory is `movdqu`, never an
2412+ aligned form, so nothing in it needs an aligned address; `movaps`
2413+ appears only between registers. `syscall` (`SYSTEM.SYSCALL`)
2414+ changes `rcx` and `r11` as the machine does.
2415+- *The stack.* `rsp` is a multiple of 16 at every call, as System V
2416+ asks: a procedure's frame is a multiple of 16 (`Gen.endProc`, at
2417+ least 16) and a call pushes a quadword of padding where an odd
2418+ number are pushed (`Gen.call`). A procedure begins `push rbp; mov
2419+ rbp, rsp; sub rsp, frame` and ends `leave; ret`: every local and
2420+ temporary is at a negative offset from `rbp` inside that frame, so
2421+ above `rsp`. **The code never writes below `rsp`**, no red zone:
2422+ where it looked, `rsp`-relative operands are in only two places,
2423+ `[rsp]` and `[rsp + 8]` read by the entry point and by the fail
2424+ stubs (`Gen.entryPoint`, all `X.mem(RSP, ...)` in `Gen.raz`), and
2425+ `X64.raz` and the assembler address only through the operands
2426+ `Gen.raz` gives them. Every other use of the stack is a `push` or
2427+ a `sub rsp`, which move `rsp` first, and a procedure with no calls
2428+ still makes its frame. An interrupt, then, needs no stack of its own
2429+ (IST) for that reason; it still needs room above the frame for its
2430+ own pushes, as any code does.
2431+- *The direction flag.* The code never sets it: neither `std` nor `cld`
2432+ is emitted anywhere. `rep movsb` and `rep stos` (`X64.repMovsb`,
2433+ `repStos`: `COPY`, `FILL`, a record's copy, zeroing) go upwards, and
2434+ a copy that must go downward is a loop of moves
2435+ (`Gen.copyDown`), since `rep movsb` with the flag set is slow. So it
2436+ must be clear whenever raz code runs, at entry and at every call,
2437+ as System V asks; an interrupt stub that uses `std` must clear it
2438+ before it calls a procedure.
2439+
2440+The recipe for an interrupt's entry, and its test, follow.
2441+
23932442 ## This compiler's choices
23942443
23952444 What the report's appendix A leaves to an implementation, and what