raz / site / public / advanced / targets.html

112 lines · 8.1 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>One module, one file per target - 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="One module, one file per target - 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" aria-current="page">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">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">Standing alone &middot; lesson 10 of 18</p><h1>One module, one file per target</h1>
47<p>raz is first made for x86-64 Linux, but the language is written to be implemented elsewhere, and the library is arranged for it. The part of a program that depends on the system goes in a module of its own, one file per target. This is how <code>Sys</code> is made, and it is how your own code should keep its system-dependent parts apart.</p>
48<h2>The files</h2>
49<p>A module <code>Name</code> lives in <code>Name.raz</code>. A module whose code depends on the system comes in one file per target, <code>Name.linux-x86_64.raz</code>, which the compiler looks for first. The library has <code>Sys.linux-x86_64.raz</code>, which makes system calls, and <code>Sys.raz</code>, which calls the C library instead, for every other system. A program that imports <code>Sys</code> doesn't know or care which it got.</p>
50<pre class="output term"><code><span class="cmd">$ ls $RAZLIB | grep -i &quot;^sys\|^thread&quot;</span>
51Sys.linux-x86_64.raz
52Sys.raz
53Thread.linux-x86_64.raz
54<span class="cmd">$ cd $RAZLIB &amp;&amp; grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz</span>
55Sys.linux-x86_64.raz:151
56Sys.raz:0</code></pre>
57<h2>A module of your own</h2>
58<p>Here is a module with two files. <code>Clock.linux-x86_64.raz</code> is for this target; <code>Clock.raz</code> is what any other target would use. Both say what they are in <code>Name</code>, and both make <code>ticks</code>.</p>
59<div class="file"><span>Clock.linux-x86_64.raz</span></div><pre class="code"><code><span class="kw">module</span> Clock;
60<span class="kw">import</span> Sys;
61
62<span class="kw">const</span> Name* = <span class="string">&quot;the linux-x86_64 file&quot;</span>;
63
64<span class="comment">(* a number that only grows *)</span>
65<span class="kw">proc</span> ticks*: <span class="ty">I64</span>;
66<span class="kw">begin</span>
67 <span class="kw">return</span> Sys.clock();
68<span class="kw">end</span>;
69
70<span class="kw">end</span> Clock.</code></pre>
71<div class="file"><span>Clock.raz</span></div><pre class="code"><code><span class="kw">module</span> Clock;
72<span class="kw">import</span> Sys;
73
74<span class="kw">const</span> Name* = <span class="string">&quot;the portable file&quot;</span>;
75
76<span class="comment">(* a number that only grows *)</span>
77<span class="kw">proc</span> ticks*: <span class="ty">I64</span>;
78<span class="kw">begin</span>
79 <span class="kw">return</span> Sys.clock();
80<span class="kw">end</span>;
81
82<span class="kw">end</span> Clock.</code></pre>
83<p>A program that imports <code>Clock</code> gets the first of them here:</p>
84<div class="file"><span>Show.raz</span></div><pre class="code"><code><span class="kw">module</span> Show;
85<span class="kw">import</span> Out, Clock;
86
87<span class="kw">proc</span> main*;
88<span class="kw">begin</span>
89 Out.str(<span class="string">&quot;using &quot;</span>); Out.str(Clock.Name); Out.ln;
90 <span class="kw">if</span> Clock.ticks() &gt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;and it ticks\n&quot;</span>); <span class="kw">end</span>;
91<span class="kw">end</span>;
92
93<span class="kw">end</span> Show.</code></pre>
94<pre class="output"><code>using the linux-x86_64 file
95and it ticks
96</code></pre>
97<h2>Held to one interface</h2>
98<p>Two files can't be told apart by their callers, so the compiler holds them to the same interface. Here is the pair again with <code>ticks</code> changed in the portable file to return an <code>INT</code>:</p>
99<pre class="output term"><code><span class="cmd">$ mkdir drift &amp;&amp; cp Clock*.raz Show.raz drift &amp;&amp; cd drift</span>
100<span class="cmd">$ sed -i &#x27;s/proc ticks\*: I64/proc ticks*: INT/; s/return Sys.clock()/return INT(Sys.clock())/&#x27; Clock.raz</span>
101<span class="cmd">$ raz Show.raz</span>
102raz: ./Clock.raz: ticks isn&#x27;t exported as in ./Clock.linux-x86_64.raz</code></pre>
103<p>The program stops compiling on every target, including the ones that would never have used that file. A mismatch is found where it is made, not where someone first builds for a second target.</p>
104<h2>What the compiler checks</h2>
105<p>When it takes one of <code>Name.*.raz</code>, it also reads the declarations of the others beside it, and of <code>Name.raz</code> if there is one, and reports an error unless they all export the same names, each the same kind of thing: a constant of the same type (its value may differ), a type of the same kind with the same exported fields, a variable of the same type, a procedure of the same signature. The code, the unexported fields, and so the size and layout, may differ.</p>
106<p>So a program written against the interface works on every target that has the module, and the places where it doesn't are the compiler's to find, not yours.</p>
107<h2>What stays the same everywhere</h2>
108<p>The core of the library is what every target must have: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. <code>Thread</code>, <code>link</code> and <code>extern</code>, and most of <code>Sys</code>, are what a target may leave out when its system can't give them.</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="lifecycle.html">&larr; Starting, ending, and signals</a><a href="index.html">Contents</a><a class="next" href="asm.html">Procedures in assembly &rarr;</a></div></article></div>
109</main>
110<footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
111</body>
112</html>