| 1 | title: What the compiler makes |
| 2 | part: The machine |
| 3 | summary: raz -S, reading a listing, what a check costs and what check off removes. |
| 4 | --- |
| 5 | The compiler has no optimizer and no intermediate form. It reads the source once and writes x86-64 machine code as it goes, so the code is what the source says, in the order it says it. That makes the output readable, and there is a switch for reading it: `raz -S Main.raz` prints the main module's code instead of writing it, each line of source and under it the instructions made for it, at their offsets in the procedure. |
| 6 | |
| 7 | ## A small procedure |
| 8 | |
| 9 | ```raz Add |
| 10 | module Add; |
| 11 | |
| 12 | proc add*(a, b: INT): INT; |
| 13 | begin |
| 14 | return a + b; |
| 15 | end; |
| 16 | |
| 17 | proc main*; |
| 18 | begin |
| 19 | _ := add(1, 2); |
| 20 | end; |
| 21 | |
| 22 | end Add. |
| 23 | ``` |
| 24 | |
| 25 | ```sh |
| 26 | $ raz -S Add.raz |
| 27 | ``` |
| 28 | |
| 29 | Every procedure starts by saving the caller's frame pointer and making its own (`push rbp; mov rbp, rsp`) and reserves room for what it needs. The arguments arrive in `rdi` and `rsi`, as the convention says, and the result leaves in `rax`. A call is `mov rdi, 1; mov rsi, 2; call Add.add`. Nothing is hidden: the frame pointer chain is what lets a failure show the calls under way with their variables. |
| 30 | |
| 31 | ## A loop with its checks |
| 32 | |
| 33 | This is the dot product of two slices. A slice is a pointer and a length, and it travels in two registers: |
| 34 | |
| 35 | ```raz Dot |
| 36 | module Dot; |
| 37 | |
| 38 | (* the dot product of two slices of equal length *) |
| 39 | proc dot*(a, b: []INT): INT; |
| 40 | var i, sum: INT; |
| 41 | begin |
| 42 | for i := 0 to LEN(a) - 1 do |
| 43 | sum := sum + a[i] * b[i]; |
| 44 | end; |
| 45 | return sum; |
| 46 | end; |
| 47 | |
| 48 | proc main*; |
| 49 | var x, y: [3]INT; |
| 50 | begin |
| 51 | x := [1, 2, 3]; |
| 52 | y := [4, 5, 6]; |
| 53 | _ := dot(x, y); |
| 54 | end; |
| 55 | |
| 56 | end Dot. |
| 57 | ``` |
| 58 | |
| 59 | ```sh |
| 60 | $ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' |
| 61 | ``` |
| 62 | |
| 63 | Read it from the top. `a` arrives in `rdi` and `rsi`, pointer and length; `b` in `rdx` and `rcx`, which the procedure saves in its frame. `i` and `sum` live in `rbx` and `r12`, which the procedure saves and restores because the convention says they belong to the caller. The loop's test is at the bottom, and its limit, `LEN(a) - 1`, is taken once. |
| 64 | |
| 65 | In the body, each index has a check: `cmp rbx, rsi; jae` compares `i` with `a`'s length in a single instruction and jumps to a stub at the end of the procedure, which calls `Rt.check0` with the values it needs to say what failed. When the index is in range, which is nearly always, the cost is one compare and a branch that isn't taken. |
| 66 | |
| 67 | ## Taking the checks out |
| 68 | |
| 69 | `check off;` after the imports leaves out the module's own checks of indexes and slice bounds. Here is the same procedure with it: |
| 70 | |
| 71 | ```raz Dot2 |
| 72 | module Dot2; |
| 73 | check off; |
| 74 | |
| 75 | proc dot*(a, b: []INT): INT; |
| 76 | var i, sum: INT; |
| 77 | begin |
| 78 | for i := 0 to LEN(a) - 1 do |
| 79 | sum := sum + a[i] * b[i]; |
| 80 | end; |
| 81 | return sum; |
| 82 | end; |
| 83 | |
| 84 | proc main*; |
| 85 | var x, y: [3]INT; |
| 86 | begin |
| 87 | x := [1, 2, 3]; |
| 88 | y := [4, 5, 6]; |
| 89 | _ := dot(x, y); |
| 90 | end; |
| 91 | |
| 92 | end Dot2. |
| 93 | ``` |
| 94 | |
| 95 | ```sh |
| 96 | $ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' |
| 97 | $ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' | wc -l |
| 98 | $ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' | wc -l |
| 99 | ``` |
| 100 | |
| 101 | The compares, the branches and the stubs are gone. It is faster, and it is no longer safe: an index out of range reads or writes whatever memory it reaches, and what follows isn't defined. A program turns checks off where it has measured and is sure, and says so at the top of the module, where a reader can see it. |
| 102 | |
| 103 | ## Comparing with C |
| 104 | |
| 105 | `make kernels` in the compiler's sources measures `test/bench`'s kernels against the same kernels written in Pascal and compiled by Free Pascal at `-O2`. The compiler's own guide, `doc/compiler.md`, gives the numbers and says what the compiler does and doesn't do. It is not the fastest code: it is code whose every instruction you can account for. |
| 106 | |
| 107 | ## Tools that read what it made |
| 108 | |
| 109 | - `gdb` finds procedures by name and lines by number, since the executable carries them: `break Dot.dot` and `list` work, and so does stepping a line at a time. |
| 110 | - `RAZ_PROFILE=1` samples the program every millisecond and says which procedures and lines took the time. |
| 111 | - `objdump -d` disassembles what is really in the file, if you want to see the bytes behind a listing. |
| 112 | |
| 113 | ```sh |
| 114 | $ raz -o Dot Dot.raz |
| 115 | $ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12 |
| 116 | ``` |
| 117 | |
| 118 | The listing is what the encoder meant; `objdump` is what the processor will read. They tell the same story. |