raz / commit 6fafd396

commit
6fafd396160f134b1cc5f2e5a5295c31ad233220
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
Add the language report and the plan

doc/jed.md defines the language; PLAN.md lists the decisions and the
steps to build the compiler.

patch · log

2 files changed, +666 -0

PLAN.md (history) added +95 -0

@@ -0,0 +1,95 @@
1+# jed: the plan
2+
3+jed (Polish *jeden*, one) is a small systems language in Wirth's
4+tradition: full control of memory, volatile access, atomics, calls into
5+C, generic modules. Its compiler is meant to be read in a long weekend:
6+about 4,000 lines of Free Pascal, one pass, no IR, no optimizer, writing
7+x86-64 machine code and the ELF file itself.
8+
9+The language is defined by `doc/jed.md`. Where this plan and the report
10+disagree, the report wins and this plan is fixed.
11+
12+## Ground rules
13+
14+- The compiler is Free Pascal 3.2.2, `{$mode objfpc}{$H-}`: records,
15+ pointers, shortstrings. No classes, ansistrings, exceptions, dynamic
16+ arrays or SysUtils.
17+- Clarity over cleverness. Every unit opens with a header comment: what
18+ it is, what it depends on, what it leaves out. Comments say why, and
19+ what wherever a layout or encoding isn't obvious.
20+- One commit per step, as `Luis <42945842+ldiaz94@users.noreply.github.com>`, unsigned, with
21+ no attribution trailer.
22+- Work stops after every stage for review. A step starts only on an
23+ explicit go-ahead.
24+- Not in scope for now: man pages, QEMU or bare metal tests, aarch64,
25+ an optimizer.
26+
27+## Decisions
28+
29+1. Source files end in `.jed`, one module per file, named after it.
30+2. "Not equal" is `#`.
31+3. Every statement ends in `;`, including `end;` and `until x;`. Every
32+ match arm starts with `|`, the `else` arm too.
33+4. Code generation is a stack machine through `rax`: no register
34+ allocation, and every access the program writes is one instruction
35+ in source order. Volatile is therefore a type rule; the code
36+ generator honours it by doing nothing clever.
37+5. Every procedure uses the System V calling convention, so any
38+ procedure with scalar parameters can be handed to C as a callback.
39+6. Records are laid out as C lays them out.
40+7. Modules are compiled together, from source, in one run: no symbol
41+ files, no separate compilation. A generic module is compiled once
42+ per distinct list of type arguments.
43+8. Tree shaking: the linker keeps only the procedures, variables and
44+ literals reachable from the entry point.
45+9. Variadic arguments exist only to call C. jed code gets the same
46+ effect from a slice of a variant (`Fmt.print`).
47+10. Strings are a library over `[]U8`, not part of the language.
48+
49+## The compiler, in reading order
50+
51+| File | Lines | What |
52+|---|---|---|
53+| `src/scan.pas` | ~350 | Symbols, literals, comments, error messages |
54+| `src/sym.pas` | ~350 | Types, objects, scopes, layout |
55+| `src/x64.pas` | ~400 | Code buffer, chunks, fixups, instruction encoder |
56+| `src/gen.pas` | ~600 | Items: loading, storing, copying, frames, calls |
57+| `src/parse.pas` | ~1,500 | Declarations, statements, expressions, built-ins |
58+| `src/link.pas` | ~200 | Tree shaking, layout, patching |
59+| `src/elf.pas` | ~450 | Static and dynamic ELF, symbol table |
60+| `src/jed.pas` | ~100 | The command line |
61+
62+## Steps
63+
64+Stage 1: the core
65+
66+- [ ] 1. Language report, `doc/jed.md`. Reviewed and approved before
67+ any more code.
68+- [ ] 2. Scanner and symbol table, `scan.pas` and `sym.pas` (drafts
69+ exist), brought in line with the report; they compile.
70+- [ ] 3. Machine code, `x64.pas`.
71+- [ ] 4. Linking and output, `link.pas` and `elf.pas`: tree shaking,
72+ static ELF, symbol table. A hand built chunk runs.
73+- [ ] 5. Parser and code generator, `gen.pas` and `parse.pas`:
74+ everything in the report except multiple results, `NEW`, generic
75+ modules and `extern`.
76+- [ ] 6. `lib/Rt.jed`, `lib/Out.jed`; examples (hello, args, variants
77+ and slices, volatile); `make check` runs them against expected
78+ output.
79+
80+Stage 2: allocation, results, generics
81+
82+- [ ] 7. Multiple results.
83+- [ ] 8. `NEW`; `lib/Mem.jed`, `lib/Arena.jed`, `lib/Heap.jed`.
84+- [ ] 9. Generic modules; `lib/Str.jed`, `lib/Buf.jed`, `lib/Fmt.jed`;
85+ examples.
86+
87+Stage 3: calling C
88+
89+- [ ] 10. `extern`, `link`, dynamic ELF, start through
90+ `__libc_start_main`; examples with `printf` and an X11 window.
91+
92+Wrap up
93+
94+- [ ] 11. `README.md` and `doc/compiler.md`, a guide to reading the
95+ compiler file by file.

doc/jed.md added +571 -0

@@ -0,0 +1,571 @@
1+# The programming language jed
2+
3+jed is a small systems language for x86-64 Linux. A jed program can lay
4+a record over a device's registers, read and write memory exactly as
5+written, take part in lock-free algorithms, make system calls and call
6+C libraries. It has no garbage collector and no hidden run time: what
7+the program does is what its text says.
8+
9+This report defines the language. It is short on purpose. Whatever it
10+doesn't allow, jed doesn't have.
11+
12+## 1. A first look
13+
14+```
15+module Hello;
16+import Out;
17+
18+proc main*;
19+begin
20+ Out.str("hello, world\n");
21+end;
22+
23+end Hello.
24+```
25+
26+A program is a set of modules. One of them, the main module, has a
27+procedure `main`; the program starts there. Saved as `Hello.jed`, it is
28+compiled with `jed Hello.jed`, which writes the executable `Hello`.
29+
30+## 2. Words and symbols
31+
32+A program is written in ASCII. Spaces, tabs and line breaks separate
33+symbols and are otherwise ignored.
34+
35+**Identifiers** are a letter or `_` followed by letters, digits and `_`,
36+up to 63 characters. Case matters: `count` and `Count` are different
37+names.
38+
39+**Reserved words** are lowercase, except `VOLATILE`:
40+
41+```
42+and begin by const div do else elsif end extern for forward if import
43+link match mod module not of or proc record repeat return shl shr then
44+to type until var variant VOLATILE while xor
45+```
46+
47+**Numbers.** `123` is decimal. `0FFH` is hexadecimal: it starts with a
48+digit and ends in `H`. `1.5` and `2.0E-3` are reals; a real always has
49+a digit on both sides of its point.
50+
51+**Characters.** `'a'` is the code of one character, an integer.
52+
53+**Strings.** `"hello\n"` is a sequence of bytes. Both characters and
54+strings take the escapes `\n` (line feed), `\t` (tab), `\r` (carriage
55+return), `\0` (zero), `\\`, `\'`, `\"` and `\xHH` (the byte with
56+hexadecimal value HH).
57+
58+**Comments** are `(*` ... `*)` and nest.
59+
60+**Operators and delimiters:**
61+
62+```
63++ - * / = # < <= > >= := : ; , . .. ... ( ) [ ] ^ |
64+```
65+
66+## 3. Modules
67+
68+```
69+Module = "module" ident [ "(" ident { "," ident } ")" ] ";"
70+ { Import } { Link } { Declaration } "end" ident "." .
71+Import = "import" ImportItem { "," ImportItem } ";" .
72+ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] .
73+Link = "link" string ";" .
74+```
75+
76+A module lives in a file of its own name: module `Out` is `Out.jed`.
77+The name after `end` repeats the module's name.
78+
79+**Exports.** A name declared at the top of a module with `*` after it
80+is exported: other modules can use it as `Module.name`. Record fields
81+are exported the same way. Everything else is private to the module.
82+
83+**Imports.** `import Out;` makes `Out`'s exports available as `Out.x`.
84+`import O := Out;` uses the name `O` instead. A module can't import
85+itself, directly or through others.
86+
87+**Generic modules** take types as parameters:
88+
89+```
90+module List(T);
91+type
92+ Node* = record next*: ^Node; val*: T; end;
93+...
94+end List.
95+```
96+
97+An import gives the arguments: `import Ints := List(I64);`. Each
98+distinct list of arguments makes one *instance*: the module compiled
99+with `T` standing for `I64`. Two imports of `List(I64)`, in any modules,
100+share one instance. Since every instance is checked on its own, an
101+operation the argument type doesn't support (adding two records, say)
102+is reported as an error in that instance.
103+
104+**Finding modules.** The compiler looks for `Name.jed` in the main
105+module's directory, then in each directory given with `-I`, then in the
106+`lib` directory beside the compiler.
107+
108+**No module bodies.** Module variables start at zero, and there is no
109+code that runs when a module starts. A module that needs setting up
110+does it on first use.
111+
112+**`link "libX11.so.6";`** names a shared library to load at run time,
113+for the `extern` procedures of section 9.
114+
115+## 4. Declarations
116+
117+```
118+Declaration = "const" { IdentDef "=" Expr ";" }
119+ | "type" { IdentDef "=" Type ";" }
120+ | "var" { IdentList ":" Type ";" }
121+ | ProcDeclaration ";"
122+ | ExternDeclaration ";" .
123+IdentDef = ident [ "*" ] .
124+IdentList = IdentDef { "," IdentDef } .
125+```
126+
127+A name must be declared before it is used, with two exceptions: the
128+base type of a pointer may name a type declared later in the same
129+`type` section (so that two records can point at each other), and a
130+procedure may be declared `forward` and defined later (section 8).
131+
132+**Constants** are evaluated by the compiler. Their expressions use only
133+literals, other constants, operators, `SIZE` and the `LEN` of arrays.
134+
135+**Variables** of a module live as long as the program; variables of a
136+procedure live as long as its call. Both start at zero.
137+
138+## 5. Types
139+
140+```
141+Type = QualIdent
142+ | "[" Expr "]" Type (* array *)
143+ | "[" "]" Type (* slice *)
144+ | "^" [ "VOLATILE" ] Type (* pointer *)
145+ | "record" { IdentList ":" Type ";" } "end"
146+ | "variant" { Case ";" } "end"
147+ | "proc" Signature .
148+Case = ident [ "(" FieldList { ";" FieldList } ")" ] .
149+FieldList = ident { "," ident } ":" Type .
150+QualIdent = [ ident "." ] ident .
151+```
152+
153+### 5.1 Basic types
154+
155+| Type | Size | Values |
156+|---|---|---|
157+| `I8` `I16` `I32` `I64` | 1, 2, 4, 8 | signed integers, two's complement |
158+| `U8` `U16` `U32` `U64` | 1, 2, 4, 8 | unsigned integers |
159+| `BOOL` | 1 | `false` (0), `true` (1) |
160+| `F64` | 8 | IEEE 754 double precision |
161+
162+### 5.2 Arrays
163+
164+`[n]T` is `n` elements of type `T`, one after another. `n` is a
165+constant greater than zero. Arrays are values: assigning one copies it.
166+
167+### 5.3 Slices
168+
169+`[]T` is a view of elements that live elsewhere: a pointer to the first
170+and a count, 16 bytes in all.
171+
172+```
173+Slice = record ptr: ^T; len: I64; end; (* its layout *)
174+```
175+
176+A slice is made from an array (`a[2..5]`, or an array used where a
177+slice is wanted), from another slice, or from a pointer (`p[0..n]`). It
178+doesn't own what it points at.
179+
180+### 5.4 Pointers
181+
182+`^T` is the address of a `T`, 8 bytes. `nil` is the pointer to nothing.
183+
184+`^VOLATILE T` is the address of a `T` that something besides the
185+program may read or change: a device register, memory shared with
186+another process. Section 7.6 says what that promises.
187+
188+### 5.5 Records
189+
190+```
191+type Cell = record ch: U8; attr: U8; end;
192+```
193+
194+Fields are laid out in order as C lays them out: each at the next
195+multiple of its own alignment, and the whole record padded to a
196+multiple of its largest alignment. A record declared like a C struct
197+has the same layout as that struct.
198+
199+### 5.6 Variants
200+
201+A variant holds one of several cases, each with its own fields:
202+
203+```
204+type
205+ Shape* = variant
206+ circle(r: F64);
207+ rect(w, h: F64);
208+ dot;
209+ end;
210+```
211+
212+The case names are declared in the scope around the type, so `circle`
213+can be used directly, and as `Shapes.circle` from another module if
214+`Shape` is exported. A case with fields is built by calling it like a
215+procedure, `circle(2.0)`; a case without fields is just `dot`. The only
216+way to read the fields is `match` (section 7.4).
217+
218+Layout: an `I64` tag (0 for the first case, 1 for the second, and so
219+on) followed by the fields of the largest case, laid out as a record.
220+
221+### 5.7 Procedure types
222+
223+```
224+type Compare = proc(a, b: I64): BOOL;
225+```
226+
227+A value of a procedure type is the address of a procedure with that
228+signature, 8 bytes, or `nil`.
229+
230+### 5.8 Same and assignable
231+
232+Two named types are the same only if they are the same declaration.
233+Two anonymous types are the same if they are built alike: `^I64` is
234+`^I64` wherever it is written.
235+
236+A value of type `A` can be assigned to a variable of type `B` (and
237+passed to a parameter of type `B`, and returned as a `B`) if they are
238+the same type, or if:
239+
240+- `A` is an integer constant whose value fits in the integer type `B`;
241+- `A` is `nil` and `B` is a pointer or procedure type;
242+- `A` is `^T` and `B` is `^VOLATILE T`;
243+- `A` is `[n]T` and `B` is `[]T`;
244+- `A` is a string literal and `B` is `[]U8`, `^U8` (a pointer to the
245+ bytes followed by a zero byte, as C wants), or `[n]U8` with `n` at
246+ least the string's length (the rest is zero);
247+- `A` is a procedure and `B` a procedure type with the same signature.
248+
249+Nothing else converts silently. In particular, there is no conversion
250+between integer types, or between integers and `F64`, without a written
251+conversion (section 6.6).
252+
253+## 6. Expressions
254+
255+```
256+Expr = SimpleExpr [ Relation SimpleExpr ] .
257+Relation = "=" | "#" | "<" | "<=" | ">" | ">=" .
258+SimpleExpr = [ "-" ] Term { AddOp Term } .
259+AddOp = "+" | "-" | "or" | "xor" .
260+Term = Factor { MulOp Factor } .
261+MulOp = "*" | "/" | "div" | "mod" | "and" | "shl" | "shr" .
262+Factor = number | char | string | Designator | "(" Expr ")"
263+ | "not" Factor
264+ | "[" [ Expr { "," Expr } ] "]"
265+ | "^" [ "VOLATILE" ] Type "(" Expr ")" .
266+Designator = QualIdent { Selector } .
267+Selector = "." ident | "^" | "[" Expr [ ".." Expr ] "]"
268+ | "(" [ Expr { "," Expr } ] ")" .
269+```
270+
271+The operators bind in the order of the grammar: `not` most tightly,
272+then the multiplying operators, then the adding ones, then the
273+relations. So `a + b * c = d` is `(a + (b * c)) = d`. A unary minus
274+applies to the first term: `-a * b` is `-(a * b)`.
275+
276+### 6.1 Integers
277+
278+Both operands of `+ - * div mod and or xor` have the same integer
279+type, or one is an untyped constant that takes the other's type. The
280+result has that type and wraps around at its width: in `U8`,
281+`200 + 100` is `44`.
282+
283+`div` rounds toward zero and `mod` takes the sign of the dividend, as
284+in C: `-7 div 2` is `-3`, `-7 mod 2` is `-1`. Dividing by zero stops
285+the program (`SIGFPE`).
286+
287+`and`, `or`, `xor` and `not` work on every bit. `x shl n` and
288+`x shr n` shift by `n mod 64`; `shr` brings in copies of the sign bit
289+for signed types and zeros for unsigned ones.
290+
291+Relations compare as signed or unsigned according to the type.
292+
293+### 6.2 Booleans
294+
295+`and` and `or` on `BOOL` evaluate their right operand only when it
296+decides the result. `not` negates, `xor` is "different", and `=` and
297+`#` compare.
298+
299+### 6.3 Reals
300+
301+`+ - * /` and the relations work on `F64`, both operands `F64`. A real
302+literal has type `F64`.
303+
304+### 6.4 Pointers
305+
306+- `p^` is the variable `p` points at.
307+- `p + i` and `p - i`, with `i` an integer, move `p` by `i` elements:
308+ by `i * SIZE(T)` bytes for `p: ^T`.
309+- `p - q`, with both of the same pointer type, is the number of
310+ elements between them, an `I64`.
311+- `p[i]` is `(p + i)^`. `p[lo..hi]` is the slice of the `hi - lo`
312+ elements from `p + lo`. Neither is checked.
313+- Pointers compare with `= # < <= > >=` as unsigned addresses.
314+ Procedure values compare with `=` and `#` only.
315+
316+### 6.5 Arrays and slices
317+
318+`a[i]` is an element of an array or slice, and `a[lo..hi]` is the slice
319+of the elements from `lo` up to but not including `hi`. Both are
320+checked: `i` must be below the length, and `0 <= lo <= hi <= length`.
321+A check that fails prints the file and line to standard error and stops
322+the program with status 2.
323+
324+`[e1, e2, ...]` builds an array where the type it must have is known:
325+assigned to an array or slice variable, passed to such a parameter, or
326+returned as one. Its elements must be assignable to the element type.
327+
328+Arrays, slices, records and variants can't be compared; write a loop,
329+or use the library's `Str.eq` for strings.
330+
331+### 6.6 Conversions
332+
333+A type name applied like a procedure converts: `I32(x)`, `F64(n)`,
334+`^Cell(addr)`. A pointer type written out uses the `^` form of the
335+grammar: `^VOLATILE U32(0FEE00000H)`.
336+
337+| From | To | Result |
338+|---|---|---|
339+| integer | integer | the value cut to the new width, or extended by its sign (signed) or zeros (unsigned) |
340+| integer | `F64` | the nearest real |
341+| `F64` | integer | rounded toward zero |
342+| integer | pointer, procedure | the address |
343+| pointer, procedure | integer | the address |
344+| pointer, procedure | pointer, procedure | the same address, seen as the new type |
345+
346+### 6.7 Built-in procedures
347+
348+| Call | Result |
349+|---|---|
350+| `SIZE(T)` | the size of type `T` in bytes, a constant |
351+| `ADR(x)` | the address of variable `x`, a `^T` |
352+| `LEN(a)` | the number of elements of an array (a constant) or a slice, an `I64` |
353+| `NEW(p)` | sets `p: ^T` to a fresh, zeroed `T` from `Heap`; the module must import `Heap` |
354+| `NEW(p, a)` | the same, from the allocator `a: ^Mem.Allocator` |
355+| `SYSCALL(n, x1, ..., x6)` | Linux system call `n` with up to six integer or pointer arguments; the raw result, an `I64` (a negative number is `-errno`) |
356+| `CODE(b1, b2, ...)` | puts the constant bytes in the code, see 7.7 |
357+| `FENCE` | a full memory barrier |
358+| `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically |
359+| `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically |
360+| `XADD(p, x)` | adds `x` to `p^` and returns the value before, atomically |
361+| `ARGC()` | the number of command line arguments, an `I64` |
362+| `ARGV()` | the arguments, a `^^U8` (C strings) |
363+
364+For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T`
365+is an 8-byte integer or pointer type (an integer type for `XADD`). They
366+are sequentially consistent.
367+
368+## 7. Statements
369+
370+```
371+Statements = { Statement ";" } .
372+Statement = Designator [ ":=" Expr ]
373+ | Designator "," Designator { "," Designator } ":=" Expr
374+ | "if" Expr "then" Statements
375+ { "elsif" Expr "then" Statements } [ "else" Statements ] "end"
376+ | "while" Expr "do" Statements "end"
377+ | "repeat" Statements "until" Expr
378+ | "for" ident ":=" Expr "to" Expr [ "by" Expr ] "do" Statements "end"
379+ | "match" Expr "of" { "|" Arm } "end"
380+ | "return" [ Expr { "," Expr } ] .
381+Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements .
382+```
383+
384+Every statement ends in `;`, including the ones that end in `end` or
385+`until`.
386+
387+### 7.1 Assignment and calls
388+
389+`x := e` evaluates `e` and stores it in `x`; arrays, records, slices and
390+variants are copied. A designator on its own is a call, and any result
391+it returns is dropped: `Out.ln;`, `write(fd, buf);`.
392+
393+`a, b := f(x);` assigns the results of a procedure that returns several
394+(section 8), in order.
395+
396+### 7.2 Conditions and loops
397+
398+`if`, `while` and `repeat` take `BOOL` expressions. `repeat` runs its
399+body at least once and stops when the condition is true.
400+
401+`for i := a to b by s do ... end;` sets the integer variable `i` to `a`,
402+`a + s`, ... while `i <= b` (or `i >= b` if the constant `s` is
403+negative). `s` is 1 if left out. `b` is evaluated once, before the
404+first round.
405+
406+There is no `break` or `goto`. A loop that must stop early tests a
407+condition, or its body is a procedure that returns.
408+
409+### 7.3 Return
410+
411+`return e;` ends the procedure with the result `e`; `return a, b;` with
412+several results; `return;` from a procedure without results. A
413+procedure with results that runs off its end returns an undefined value.
414+
415+### 7.4 Match
416+
417+```
418+match s of
419+| circle(r): area := 3.14159 * r * r;
420+| rect(w, h): area := w * h;
421+| dot: area := 0.0;
422+end;
423+```
424+
425+`match` runs the arm of the case `s` holds. An arm may name the case's
426+fields, in order, all of them; the names stand for those fields of `s`
427+itself, so assigning to one changes `s`. An arm may also leave the
428+fields unnamed. Every case must have an arm, unless there is an
429+`| else:` arm, which must come last.
430+
431+### 7.5 Order of evaluation
432+
433+Operands, arguments and the designators of an assignment are evaluated
434+from left to right.
435+
436+### 7.6 Volatile
437+
438+A variable reached through a `^VOLATILE T` is volatile, and so is any
439+field or element of it. Each read or write of a volatile variable of a
440+basic or pointer type is exactly one memory access, of exactly its
441+size, and volatile accesses happen in the order the program writes
442+them. Copying a whole record or array through a volatile pointer is
443+not allowed, since it can't be one access.
444+
445+A `^VOLATILE T` doesn't convert to `^T` without a written conversion.
446+
447+`FENCE` also orders the program's ordinary accesses against those of
448+other processors and devices.
449+
450+### 7.7 CODE
451+
452+`CODE` puts machine code where it stands. Between two statements the
453+compiler keeps nothing in registers but `rsp` and `rbp`, so code there
454+may use any other register. It must leave `rsp` and `rbp` as it found
455+them.
456+
457+```
458+proc halt;
459+begin
460+ CODE(0FAH, 0F4H); (* cli; hlt *)
461+end;
462+```
463+
464+## 8. Procedures
465+
466+```
467+ProcDeclaration = "proc" IdentDef Signature ";"
468+ ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] ) .
469+LocalDeclaration = "const" { ident "=" Expr ";" }
470+ | "type" { ident "=" Type ";" }
471+ | "var" { ident { "," ident } ":" Type ";" } .
472+Signature = "(" [ Params { ";" Params } [ ";" "..." ] ] ")" [ ":" Results ]
473+ | [ ":" Results ] .
474+Params = [ "var" ] ident { "," ident } ":" Type .
475+Results = Type | "(" Type { "," Type } ")" .
476+```
477+
478+A procedure without parameters may leave out the parentheses, both
479+where it is declared (`proc main*;`) and where it is called (`Out.ln`).
480+Used without parentheses in an expression, a procedure is a procedure
481+value, not a call.
482+
483+**Parameters.** A value parameter is a copy: the procedure may change
484+it, and the caller's variable stays as it was. A `var` parameter is the
485+caller's variable itself; its argument must be a variable of exactly
486+the parameter's type.
487+
488+**Results.** A procedure returns nothing, one value, or several. A call
489+that returns several values can only stand on the right of a multiple
490+assignment, or alone as a statement.
491+
492+```
493+proc divmod(a, b: I64): (I64, I64);
494+begin
495+ return a div b, a mod b;
496+end;
497+
498+q, r := divmod(17, 5);
499+```
500+
501+**forward.** `proc f(x: I64): I64; forward;` declares `f` so that it
502+can be called before its body appears; the body comes later with the
503+same signature.
504+
505+Procedures don't nest.
506+
507+## 9. Calling C
508+
509+```
510+ExternDeclaration = "extern" "proc" IdentDef Signature .
511+```
512+
513+```
514+link "libX11.so.6";
515+extern proc XOpenDisplay(name: ^U8): ^U8;
516+extern proc printf(format: ^U8; ...): I32;
517+```
518+
519+An `extern` procedure is found by its name in the C libraries the
520+program links: the C library always, and each library named by `link`.
521+Its parameters and result are integers, `BOOL`, `F64`, pointers or
522+procedures; a record goes to C by pointer, never by value. `...`
523+accepts any number of further arguments of those types, and is allowed
524+only in an `extern`.
525+
526+Every jed procedure uses the System V calling convention, so a jed
527+procedure whose parameters and result are of those types can be given
528+to C as a callback.
529+
530+## 10. Programs
531+
532+The main module declares `proc main;` or `proc main: I64;`. The program
533+runs `main`, and ends with status 0, or with the status `main` returns.
534+A program that calls no C is a static executable with no run time
535+besides the code it uses. One that does is linked dynamically and
536+starts through the C library, so that C's own set-up and clean-up run.
537+
538+Only what can be reached from `main` goes into the executable: unused
539+procedures, variables and strings are left out.
540+
541+**Not checked.** Dereferencing `nil` or a stale pointer, indexing
542+through a pointer, integer overflow (it wraps), and a missing `return`
543+in a procedure with results. Dividing by zero and dereferencing `nil`
544+stop the program with a signal.
545+
546+## 11. The library
547+
548+Modules written in jed, in the compiler's `lib` directory:
549+
550+| Module | What |
551+|---|---|
552+| `Rt` | what the compiler's checks call when they fail |
553+| `Out` | writing text and numbers to standard output |
554+| `Mem` | `mmap`, and `Allocator`, the interface of every allocator |
555+| `Heap` | a general allocator; what `NEW(p)` uses |
556+| `Arena` | an allocator that frees everything at once |
557+| `Str` | operations on `[]U8` strings |
558+| `Buf` | a growable byte buffer |
559+| `Fmt` | formatted output: `Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.int(m)])` |
560+| `List` | a generic singly linked list, `List(T)` |
561+
562+```
563+module Mem;
564+type
565+ Allocator* = record
566+ alloc*: proc(a: ^Allocator; size, align: I64): ^U8;
567+ free*: proc(a: ^Allocator; p: ^U8);
568+ end;
569+...
570+end Mem.
571+```