raz / site / public / advanced / listing.html

225 lines · 12.7 KiB · raw · history · last change de634aad

1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>What the compiler makes - More Raz</title>
7<meta name="description" content="raz: a small systems language in Wirth's tradition, for x86-64 Linux.">
8<meta name="theme-color" content="#E07A5F">
9<meta property="og:title" content="What the compiler makes - More Raz">
10<meta property="og:description" content="A small systems language for x86-64 Linux. No run time, no GC, one report.">
11<meta property="og:image" content="https://raz.lucela.org/social.png">
12<meta name="twitter:card" content="summary_large_image">
13<link rel="icon" href="../favicon.svg" type="image/svg+xml">
14<link rel="icon" href="../favicon-32.png" type="image/png" sizes="32x32">
15<link rel="apple-touch-icon" href="../apple-touch-icon.png">
16<link rel="stylesheet" href="../style.css">
17</head>
18<body>
19<header><nav><a class="mark" href="../index.html"><img src="../logo.svg" width="26" height="26" alt="">raz</a><a href="../tour/index.html">Raz, Set, Go</a><a href="../advanced/index.html" aria-current="page">More Raz</a><a href="../spec.html">Report</a><a href="../source/index.html">Source</a><a href="../index.html#download">Download</a></nav></header>
20<main>
21<div class="tour"><aside><h2>More Raz</h2><h3>The library</h3><ol start='1'>
22<li><a href="fmt.html">Output and formatting</a></li>
23<li><a href="bytes.html">Strings and bytes</a></li>
24<li><a href="collections.html">Lists, arrays, maps and sorting</a></li>
25<li><a href="files.html">Files and the command line</a></li>
26<li><a href="processes.html">Time, randomness and other programs</a></li>
27<li><a href="allocators.html">Writing an allocator</a></li>
28</ol>
29<h3>Standing alone</h3><ol start='7'>
30<li><a href="static.html">A program with no run time</a></li>
31<li><a href="linkstatic.html">Threads without C, and keeping the two apart</a></li>
32<li><a href="lifecycle.html">Starting, ending, and signals</a></li>
33<li><a href="targets.html">One module, one file per target</a></li>
34</ol>
35<h3>The machine</h3><ol start='11'>
36<li><a href="asm.html">Procedures in assembly</a></li>
37<li><a href="syscalls.html">System calls</a></li>
38<li><a href="listing.html" aria-current="page">What the compiler makes</a></li>
39</ol>
40<h3>Shared memory</h3><ol start='14'>
41<li><a href="volatile.html">Volatile</a></li>
42<li><a href="atomics.html">Atomics</a></li>
43<li><a href="ordering.html">Ordering and publication</a></li>
44<li><a href="lockfree.html">A lock-free stack</a></li>
45<li><a href="ring.html">A ring buffer between threads</a></li>
46</ol></aside><article><p class="kicker">The machine &middot; lesson 13 of 18</p><h1>What the compiler makes</h1>
47<p>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: <code>raz -S Main.raz</code> 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.</p>
48<h2>A small procedure</h2>
49<div class="file"><span>Add.raz</span></div><pre class="code"><code><span class="kw">module</span> Add;
50
51<span class="kw">proc</span> add*(a, b: <span class="ty">INT</span>): <span class="ty">INT</span>;
52<span class="kw">begin</span>
53 <span class="kw">return</span> a + b;
54<span class="kw">end</span>;
55
56<span class="kw">proc</span> main*;
57<span class="kw">begin</span>
58 _ := add(<span class="number">1</span>, <span class="number">2</span>);
59<span class="kw">end</span>;
60
61<span class="kw">end</span> Add.</code></pre>
62<pre class="output term"><code><span class="cmd">$ raz -S Add.raz</span>
63
64Add.main:
65 8 proc main*;
66 0 push rbp
67 1 mov rbp, rsp
68 4 sub rsp, 16
69 10 _ := add(1, 2);
70 11 mov qword [rbp - 8], 2
71 19 mov qword [rbp - 16], 1
72 27 mov r8, [rbp - 16]
73 31 add r8, [rbp - 8]
74 11 end;
75 35 leave
76 36 ret</code></pre>
77<p>Every procedure starts by saving the caller's frame pointer and making its own (<code>push rbp; mov rbp, rsp</code>) and reserves room for what it needs. The arguments arrive in <code>rdi</code> and <code>rsi</code>, as the convention says, and the result leaves in <code>rax</code>. A call is <code>mov rdi, 1; mov rsi, 2; call Add.add</code>. Nothing is hidden: the frame pointer chain is what lets a failure show the calls under way with their variables.</p>
78<h2>A loop with its checks</h2>
79<p>This is the dot product of two slices. A slice is a pointer and a length, and it travels in two registers:</p>
80<div class="file"><span>Dot.raz</span></div><pre class="code"><code><span class="kw">module</span> Dot;
81
82<span class="comment">(* the dot product of two slices of equal length *)</span>
83<span class="kw">proc</span> dot*(a, b: []<span class="ty">INT</span>): <span class="ty">INT</span>;
84<span class="kw">var</span> i, sum: <span class="ty">INT</span>;
85<span class="kw">begin</span>
86 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(a) - <span class="number">1</span> <span class="kw">do</span>
87 sum := sum + a[i] * b[i];
88 <span class="kw">end</span>;
89 <span class="kw">return</span> sum;
90<span class="kw">end</span>;
91
92<span class="kw">proc</span> main*;
93<span class="kw">var</span> x, y: [<span class="number">3</span>]<span class="ty">INT</span>;
94<span class="kw">begin</span>
95 x := [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
96 y := [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
97 _ := dot(x, y);
98<span class="kw">end</span>;
99
100<span class="kw">end</span> Dot.</code></pre>
101<pre class="output term"><code><span class="cmd">$ raz -S Dot.raz | sed -n &#x27;/^Dot.dot:/,/^$/p&#x27;</span>
102Dot.dot:
103 4 proc dot*(a, b: []INT): INT;
104 0 push rbp
105 1 mov rbp, rsp
106 4 sub rsp, 64
107 11 mov [rbp - 56], rbx
108 15 xor ebx, ebx
109 17 mov [rbp - 48], r12
110 21 xor r12d, r12d
111 24 mov [rbp - 40], r13
112 28 xor r13d, r13d
113 31 mov [rbp - 32], rdx
114 35 mov [rbp - 24], rcx
115 7 for i := 0 to LEN(a) - 1 do
116 39 xor ebx, ebx
117 41 mov r8, rsi
118 44 lea r8, [r8 - 1]
119 48 mov r13, r8
120 51 cmp rbx, r13
121 54 jg 175
122 60 jmp 100
123 65 nop
124 73 nop
125 81 nop
126 89 nop
127 96 add rbx, 1
128 8 sum := sum + a[i] * b[i];
129 100 cmp rbx, rsi
130 103 jae 192
131 109 mov r10, rbx
132 112 cmp r10, [rbp - 24]
133 116 jae 203
134 122 shl r10, 3
135 126 add r10, [rbp - 32]
136 130 mov r10, [r10]
137 133 mov r9, [rdi + rbx * 8]
138 137 imul r9, r10
139 141 add r12, r9
140 144 cmp rbx, r13
141 147 jl 96
142 153 cmp rbx, r13
143 156 je 175
144 162 add rbx, 1
145 166 cmp rbx, r13
146 169 jle 100
147 10 return sum;
148 175 mov rax, r12
149 11 end;
150 178 mov rbx, [rbp - 56]
151 182 mov r12, [rbp - 48]
152 186 mov r13, [rbp - 40]
153 190 leave
154 191 ret
155 8 sum := sum + a[i] * b[i];
156 192 mov rax, rbx
157 195 mov rcx, rsi
158 198 call Rt.check0
159 203 mov rax, r10
160 206 mov rcx, [rbp - 24]
161 210 call Rt.check0</code></pre>
162<p>Read it from the top. <code>a</code> arrives in <code>rdi</code> and <code>rsi</code>, pointer and length; <code>b</code> in <code>rdx</code> and <code>rcx</code>, which the procedure saves in its frame. <code>i</code> and <code>sum</code> live in <code>rbx</code> and <code>r12</code>, 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, <code>LEN(a) - 1</code>, is taken once.</p>
163<p>In the body, each index has a check: <code>cmp rbx, rsi; jae</code> compares <code>i</code> with <code>a</code>'s length in a single instruction and jumps to a stub at the end of the procedure, which calls <code>Rt.check0</code> 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.</p>
164<h2>Taking the checks out</h2>
165<p><code>check off;</code> after the imports leaves out the module's own checks of indexes and slice bounds. Here is the same procedure with it:</p>
166<div class="file"><span>Dot2.raz</span></div><pre class="code"><code><span class="kw">module</span> Dot2;
167check off;
168
169<span class="kw">proc</span> dot*(a, b: []<span class="ty">INT</span>): <span class="ty">INT</span>;
170<span class="kw">var</span> i, sum: <span class="ty">INT</span>;
171<span class="kw">begin</span>
172 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(a) - <span class="number">1</span> <span class="kw">do</span>
173 sum := sum + a[i] * b[i];
174 <span class="kw">end</span>;
175 <span class="kw">return</span> sum;
176<span class="kw">end</span>;
177
178<span class="kw">proc</span> main*;
179<span class="kw">var</span> x, y: [<span class="number">3</span>]<span class="ty">INT</span>;
180<span class="kw">begin</span>
181 x := [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];
182 y := [<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>];
183 _ := dot(x, y);
184<span class="kw">end</span>;
185
186<span class="kw">end</span> Dot2.</code></pre>
187<pre class="output term"><code><span class="cmd">$ raz -S Dot2.raz | sed -n &#x27;/^Dot2.dot:/,/^$/p&#x27;</span>
188Dot2.raz:2:1: error: check off needs import SYSTEM
189 2 | check off;
190 | ^^^^^
191help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
192raz: 1 error
193<span class="cmd">$ raz -S Dot.raz | sed -n &#x27;/^Dot.dot:/,/^$/p&#x27; | wc -l</span>
19461
195<span class="cmd">$ raz -S Dot2.raz | sed -n &#x27;/^Dot2.dot:/,/^$/p&#x27; | wc -l</span>
196Dot2.raz:2:1: error: check off needs import SYSTEM
197 2 | check off;
198 | ^^^^^
199help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
200raz: 1 error
2010</code></pre>
202<p>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.</p>
203<h2>Comparing with C</h2>
204<p><code>make kernels</code> in the compiler's sources measures <code>test/bench</code>'s kernels against the same kernels written in Pascal and compiled by Free Pascal at <code>-O2</code>. The compiler's own guide, <code>doc/compiler.md</code>, 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.</p>
205<h2>Tools that read what it made</h2>
206<ul><li><code>gdb</code> finds procedures by name and lines by number, since the executable carries them: <code>break Dot.dot</code> and <code>list</code> work, and so does stepping a line at a time.</li><li><code>RAZ_PROFILE=1</code> samples the program every millisecond and says which procedures and lines took the time.</li><li><code>objdump -d</code> disassembles what is really in the file, if you want to see the bytes behind a listing.</li></ul>
207<pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span>
208<span class="cmd">$ objdump -d -M intel --no-show-raw-insn Dot | sed -n &#x27;/&lt;Dot.dot&gt;:/,/ret/p&#x27; | head -12</span>
2090000000000403c40 &lt;Dot.dot&gt;:
210 403c40: push rbp
211 403c41: mov rbp,rsp
212 403c44: sub rsp,0x40
213 403c4b: mov QWORD PTR [rbp-0x38],rbx
214 403c4f: xor ebx,ebx
215 403c51: mov QWORD PTR [rbp-0x30],r12
216 403c55: xor r12d,r12d
217 403c58: mov QWORD PTR [rbp-0x28],r13
218 403c5c: xor r13d,r13d
219 403c5f: mov QWORD PTR [rbp-0x20],rdx
220 403c63: mov QWORD PTR [rbp-0x18],rcx</code></pre>
221<p>The listing is what the encoder meant; <code>objdump</code> is what the processor will read. They tell the same story.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="syscalls.html">&larr; System calls</a><a href="index.html">Contents</a><a class="next" href="volatile.html">Volatile &rarr;</a></div></article></div>
222</main>
223<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
224</body>
225</html>