raz / site / content / advanced / 10-targets.txt

85 lines · 3.3 KiB · raw · history · last change ddd82658

1title: One module, one file per target
2part: Standing alone
3summary: Portable programs, the code that isn't, and how the compiler keeps them in step.
4---
5raz 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 `Sys` is made, and it is how your own code should keep its system-dependent parts apart.
6
7## The files
8
9A module `Name` lives in `Name.raz`. A module whose code depends on the system comes in one file per target, `Name.linux-x86_64.raz`, which the compiler looks for first. The library has `Sys.linux-x86_64.raz`, which makes system calls, and `Sys.raz`, which calls the C library instead, for every other system. A program that imports `Sys` doesn't know or care which it got.
10
11```sh
12$ ls $RAZLIB | grep -i "^sys\|^thread"
13$ cd $RAZLIB && grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz
14```
15
16## A module of your own
17
18Here is a module with two files. `Clock.linux-x86_64.raz` is for this target; `Clock.raz` is what any other target would use. Both say what they are in `Name`, and both make `ticks`.
19
20```raz Clock.linux-x86_64
21module Clock;
22import Sys;
23
24const Name* = "the linux-x86_64 file";
25
26(* a number that only grows *)
27proc ticks*: I64;
28begin
29 return Sys.clock();
30end;
31
32end Clock.
33```
34
35```raz Clock
36module Clock;
37import Sys;
38
39const Name* = "the portable file";
40
41(* a number that only grows *)
42proc ticks*: I64;
43begin
44 return Sys.clock();
45end;
46
47end Clock.
48```
49
50A program that imports `Clock` gets the first of them here:
51
52```raz Show run
53module Show;
54import Out, Clock;
55
56proc main*;
57begin
58 Out.str("using "); Out.str(Clock.Name); Out.ln;
59 if Clock.ticks() > 0 then Out.str("and it ticks\n"); end;
60end;
61
62end Show.
63```
64
65## Held to one interface
66
67Two files can't be told apart by their callers, so the compiler holds them to the same interface. Here is the pair again with `ticks` changed in the portable file to return an `INT`:
68
69```sh
70$ mkdir drift && cp Clock*.raz Show.raz drift && cd drift
71$ sed -i 's/proc ticks\*: I64/proc ticks*: INT/; s/return Sys.clock()/return INT(Sys.clock())/' Clock.raz
72$ raz Show.raz
73```
74
75The 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.
76
77## What the compiler checks
78
79When it takes one of `Name.*.raz`, it also reads the declarations of the others beside it, and of `Name.raz` 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.
80
81So 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.
82
83## What stays the same everywhere
84
85The core of the library is what every target must have: `Rt`, `Out`, `Mem`, `Arena`, `Lock`, `Str` and `Buf`, and of `Sys` what they need. A program that keeps to the core runs on every target. `Thread`, `link` and `extern`, and most of `Sys`, are what a target may leave out when its system can't give them.