The machine · lesson 13 of 18

What the compiler makes

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.

A small procedure

Add.raz
module Add;

proc add*(a, b: INT): INT;
begin
    return a + b;
end;

proc main*;
begin
    _ := add(1, 2);
end;

end Add.
$ raz -S Add.raz

Add.add:
     3  proc add*(a, b: INT): INT;
             0  push rbp
             1  mov rbp, rsp
             4  sub rsp, 16
     5      return a + b;
            11  mov r8, rdi
            14  add r8, rsi
            17  mov rax, r8
     6  end;
            20  leave
            21  ret

Add.main:
     8  proc main*;
             0  push rbp
             1  mov rbp, rsp
             4  sub rsp, 16
    10      _ := add(1, 2);
            11  mov rdi, 1
            16  mov rsi, 2
            21  call Add.add
            26  mov r8, rax
    11  end;
            29  leave
            30  ret

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.

A loop with its checks

This is the dot product of two slices. A slice is a pointer and a length, and it travels in two registers:

Dot.raz
module Dot;

(* the dot product of two slices of equal length *)
proc dot*(a, b: []INT): INT;
var i, sum: INT;
begin
    for i := 0 to LEN(a) - 1 do
        sum := sum + a[i] * b[i];
    end;
    return sum;
end;

proc main*;
var x, y: [3]INT;
begin
    x := [1, 2, 3];
    y := [4, 5, 6];
    _ := dot(x, y);
end;

end Dot.
$ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p'
Dot.dot:
     4  proc dot*(a, b: []INT): INT;
             0  push rbp
             1  mov rbp, rsp
             4  sub rsp, 64
            11  mov [rbp - 48], rbx
            15  xor ebx, ebx
            17  mov [rbp - 40], r12
            21  xor r12d, r12d
            24  mov [rbp - 32], rdx
            28  mov [rbp - 24], rcx
     7      for i := 0 to LEN(a) - 1 do
            32  xor ebx, ebx
            34  mov r8, rsi
            37  sub r8, 1
            41  mov [rbp - 56], r8
            45  cmp rbx, [rbp - 56]
            49  jg 146
            55  jmp 68
            60  nop
            64  add rbx, 1
     8          sum := sum + a[i] * b[i];
            68  cmp rbx, rsi
            71  jae 159
            77  mov r10, rbx
            80  cmp r10, [rbp - 24]
            84  jae 170
            90  shl r10, 3
            94  add r10, [rbp - 32]
            98  mov r10, [r10]
           101  mov r9, [rdi + rbx * 8]
           105  imul r9, r10
           109  add r12, r9
           112  cmp rbx, [rbp - 56]
           116  jl 64
           122  cmp rbx, [rbp - 56]
           126  je 146
           132  add rbx, 1
           136  cmp rbx, [rbp - 56]
           140  jle 68
    10      return sum;
           146  mov rax, r12
    11  end;
           149  mov rbx, [rbp - 48]
           153  mov r12, [rbp - 40]
           157  leave
           158  ret
     8          sum := sum + a[i] * b[i];
           159  mov rax, rbx
           162  mov rcx, rsi
           165  call Rt.check0
           170  mov rax, r10
           173  mov rcx, [rbp - 24]
           177  call Rt.check0

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.

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.

Taking the checks out

check off; after the imports leaves out the module's own checks of indexes and slice bounds. Here is the same procedure with it:

Dot2.raz
module Dot2;
check off;

proc dot*(a, b: []INT): INT;
var i, sum: INT;
begin
    for i := 0 to LEN(a) - 1 do
        sum := sum + a[i] * b[i];
    end;
    return sum;
end;

proc main*;
var x, y: [3]INT;
begin
    x := [1, 2, 3];
    y := [4, 5, 6];
    _ := dot(x, y);
end;

end Dot2.
$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p'
Dot2.dot:
     4  proc dot*(a, b: []INT): INT;
             0  push rbp
             1  mov rbp, rsp
             4  sub rsp, 64
            11  mov [rbp - 48], rbx
            15  xor ebx, ebx
            17  mov [rbp - 40], r12
            21  xor r12d, r12d
            24  mov [rbp - 32], rdx
            28  mov [rbp - 24], rcx
     7      for i := 0 to LEN(a) - 1 do
            32  xor ebx, ebx
            34  mov r8, rsi
            37  sub r8, 1
            41  mov [rbp - 56], r8
            45  cmp rbx, [rbp - 56]
            49  jg 127
            55  jmp 68
            60  nop
            64  add rbx, 1
     8          sum := sum + a[i] * b[i];
            68  mov r10, rbx
            71  shl r10, 3
            75  add r10, [rbp - 32]
            79  mov r10, [r10]
            82  mov r9, [rdi + rbx * 8]
            86  imul r9, r10
            90  add r12, r9
            93  cmp rbx, [rbp - 56]
            97  jl 64
           103  cmp rbx, [rbp - 56]
           107  je 127
           113  add rbx, 1
           117  cmp rbx, [rbp - 56]
           121  jle 68
    10      return sum;
           127  mov rax, r12
    11  end;
           130  mov rbx, [rbp - 48]
           134  mov r12, [rbp - 40]
           138  leave
           139  ret
$ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' | wc -l
55
$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' | wc -l
44

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.

Comparing with C

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.

Tools that read what it made

  • 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.
  • RAZ_PROFILE=1 samples the program every millisecond and says which procedures and lines took the time.
  • objdump -d disassembles what is really in the file, if you want to see the bytes behind a listing.
$ raz -o Dot Dot.raz
$ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12
0000000000403b80 <Dot.dot>:
  403b80:	push   rbp
  403b81:	mov    rbp,rsp
  403b84:	sub    rsp,0x40
  403b8b:	mov    QWORD PTR [rbp-0x30],rbx
  403b8f:	xor    ebx,ebx
  403b91:	mov    QWORD PTR [rbp-0x28],r12
  403b95:	xor    r12d,r12d
  403b98:	mov    QWORD PTR [rbp-0x20],rdx
  403b9c:	mov    QWORD PTR [rbp-0x18],rcx
  403ba0:	xor    ebx,ebx
  403ba2:	mov    r8,rsi

The listing is what the encoder meant; objdump is what the processor will read. They tell the same story.

Save a program under its module’s name and run it with raz run Name.raz.