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.main:
     8  proc main*;
             0  push rbp
             1  mov rbp, rsp
             4  sub rsp, 16
    10      _ := add(1, 2);
            11  mov qword [rbp - 8], 2
            19  mov qword [rbp - 16], 1
            27  mov r8, [rbp - 16]
            31  add r8, [rbp - 8]
    11  end;
            35  leave
            36  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 - 56], rbx
            15  xor ebx, ebx
            17  mov [rbp - 48], r12
            21  xor r12d, r12d
            24  mov [rbp - 40], r13
            28  xor r13d, r13d
            31  mov [rbp - 32], rdx
            35  mov [rbp - 24], rcx
     7      for i := 0 to LEN(a) - 1 do
            39  xor ebx, ebx
            41  mov r8, rsi
            44  lea r8, [r8 - 1]
            48  mov r13, r8
            51  cmp rbx, r13
            54  jg 175
            60  jmp 100
            65  nop
            73  nop
            81  nop
            89  nop
            96  add rbx, 1
     8          sum := sum + a[i] * b[i];
           100  cmp rbx, rsi
           103  jae 192
           109  mov r10, rbx
           112  cmp r10, [rbp - 24]
           116  jae 203
           122  shl r10, 3
           126  add r10, [rbp - 32]
           130  mov r10, [r10]
           133  mov r9, [rdi + rbx * 8]
           137  imul r9, r10
           141  add r12, r9
           144  cmp rbx, r13
           147  jl 96
           153  cmp rbx, r13
           156  je 175
           162  add rbx, 1
           166  cmp rbx, r13
           169  jle 100
    10      return sum;
           175  mov rax, r12
    11  end;
           178  mov rbx, [rbp - 56]
           182  mov r12, [rbp - 48]
           186  mov r13, [rbp - 40]
           190  leave
           191  ret
     8          sum := sum + a[i] * b[i];
           192  mov rax, rbx
           195  mov rcx, rsi
           198  call Rt.check0
           203  mov rax, r10
           206  mov rcx, [rbp - 24]
           210  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 - 56], rbx
            15  xor ebx, ebx
            17  mov [rbp - 48], r12
            21  xor r12d, r12d
            24  mov [rbp - 40], r13
            28  xor r13d, r13d
            31  mov [rbp - 32], rdx
            35  mov [rbp - 24], rcx
     7      for i := 0 to LEN(a) - 1 do
            39  xor ebx, ebx
            41  mov r8, rsi
            44  lea r8, [r8 - 1]
            48  mov r13, r8
            51  cmp rbx, r13
            54  jg 156
            60  jmp 100
            65  nop
            73  nop
            81  nop
            89  nop
            96  add rbx, 1
     8          sum := sum + a[i] * b[i];
           100  mov r10, rbx
           103  shl r10, 3
           107  add r10, [rbp - 32]
           111  mov r10, [r10]
           114  mov r9, [rdi + rbx * 8]
           118  imul r9, r10
           122  add r12, r9
           125  cmp rbx, r13
           128  jl 96
           134  cmp rbx, r13
           137  je 156
           143  add rbx, 1
           147  cmp rbx, r13
           150  jle 100
    10      return sum;
           156  mov rax, r12
    11  end;
           159  mov rbx, [rbp - 56]
           163  mov r12, [rbp - 48]
           167  mov r13, [rbp - 40]
           171  leave
           172  ret
$ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' | wc -l
61
$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' | wc -l
50

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
0000000000403c60 <Dot.dot>:
  403c60:	push   rbp
  403c61:	mov    rbp,rsp
  403c64:	sub    rsp,0x40
  403c6b:	mov    QWORD PTR [rbp-0x38],rbx
  403c6f:	xor    ebx,ebx
  403c71:	mov    QWORD PTR [rbp-0x30],r12
  403c75:	xor    r12d,r12d
  403c78:	mov    QWORD PTR [rbp-0x28],r13
  403c7c:	xor    r13d,r13d
  403c7f:	mov    QWORD PTR [rbp-0x20],rdx
  403c83:	mov    QWORD PTR [rbp-0x18],rcx

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.