raz / commit 8310908e

commit
8310908e69225f5e9561f198811f11f2ae727d7a
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ab022b0b
Revise the report after the second review

UTF-8 source with ASCII identifiers; procedure parameters for generic
modules; modules chosen per target by file name; defer, scoped to its
statement list; code procedures in place of inline CODE.

patch · log

2 files changed, +94 -31

PLAN.md (history) modified +4 -4

@@ -3,7 +3,7 @@
33 jed (Polish *jeden*, one) is a small systems language in Wirth's
44 tradition: full control of memory, volatile access, atomics, calls into
55 C, generic modules. Its compiler is meant to be read in a long weekend:
6-about 4,300 lines of Free Pascal, one pass, no IR, no optimizer, writing
6+about 4,500 lines of Free Pascal, one pass, no IR, no optimizer, writing
77 x86-64 machine code and the ELF file itself.
88
99 The language is defined by `doc/jed.md`. Where this plan and the report
@@ -51,14 +51,14 @@
5151
5252 | File | Lines | What |
5353 |---|---|---|
54-| `src/scan.pas` | ~360 | Symbols, literals, comments, error messages |
54+| `src/scan.pas` | ~430 | Symbols, literals, comments, error messages |
5555 | `src/sym.pas` | ~390 | Types, objects, scopes, layout |
5656 | `src/x64.pas` | ~400 | Code buffer, chunks, fixups, instruction encoder |
5757 | `src/gen.pas` | ~660 | Items: loading, storing, copying, frames, calls |
58-| `src/parse.pas` | ~1,750 | Declarations, statements, expressions, built-ins |
58+| `src/parse.pas` | ~1,900 | Declarations, statements, expressions, built-ins |
5959 | `src/link.pas` | ~200 | Tree shaking, layout, patching |
6060 | `src/elf.pas` | ~450 | Static and dynamic ELF, symbol table |
61-| `src/jed.pas` | ~100 | The command line |
61+| `src/jed.pas` | ~115 | The command line |
6262
6363 ## Steps
6464

doc/jed.md modified +90 -27

@@ -31,31 +31,40 @@
3131
3232 ## 2. Words and symbols
3333
34-A program is written in ASCII. Spaces, tabs and line breaks separate
35-symbols and are otherwise ignored.
34+A program is written in UTF-8. The compiler rejects a file that isn't
35+valid UTF-8: a malformed or overlong sequence, a surrogate, or a code
36+point beyond 10FFFFH. Everything outside strings, characters and
37+comments is ASCII. Spaces, tabs and line breaks separate symbols and
38+are otherwise ignored.
3639
37-**Identifiers** are a letter or `_` followed by letters, digits and `_`,
40+**Identifiers** are an ASCII letter or `_` followed by letters, digits and `_`,
3841 up to 63 characters. Case matters: `count` and `Count` are different
3942 names.
4043
4144 **Reserved words** are lowercase, except `VOLATILE`:
4245
4346 ```
44-and begin break by const div do else elsif end extern for forward if
45-import in link match mod module not of or packed proc record repeat
46-return set shl shr then to type until var variant VOLATILE while xor
47+and begin break by code const defer div do else elsif end extern for
48+forward if import in link match mod module not of or packed proc record
49+repeat return set shl shr then to type until var variant VOLATILE while
50+xor
4751 ```
4852
4953 **Numbers.** `123` is decimal. `0FFH` is hexadecimal: it starts with a
5054 digit and ends in `H`. `1.5` and `2.0E-3` are reals; a real always has
5155 a digit on both sides of its point.
56+
57+**Characters.** `'a'` is the code point of one character, an integer:
58+`'a'` is 97 and `'é'` is 233.
5259
53-**Characters.** `'a'` is the code of one character, an integer.
60+**Strings.** `"héllo\n"` is a sequence of bytes, the UTF-8 of its text:
61+`"é"` is the two bytes 0C3H, 0A9H. Both characters and strings take the
62+escapes `\n` (line feed), `\t` (tab), `\r` (carriage return), `\0`
63+(zero), `\\`, `\'`, `\"`, `\u{H...}` (the code point with hexadecimal
64+value H..., in a string its UTF-8) and, in strings only, `\xHH` (the
65+byte with hexadecimal value HH, which needn't make valid UTF-8).
5466
55-**Strings.** `"hello\n"` is a sequence of bytes. Both characters and
56-strings take the escapes `\n` (line feed), `\t` (tab), `\r` (carriage
57-return), `\0` (zero), `\\`, `\'`, `\"` and `\xHH` (the byte with
58-hexadecimal value HH).
67+Error messages give the line and the column, counted in characters.
5968
6069 **Comments** are `(*` ... `*)` and nest.
6170
@@ -68,9 +77,10 @@
6877 ## 3. Modules
6978
7079 ```
71-Module = "module" ident [ "(" ident { "," ident } ")" ] ";"
80+Module = "module" ident [ "(" ModParams ")" ] ";"
7281 { Import } { Link } { Declaration }
7382 [ "begin" Statements ] "end" ident "." .
83+ModParams = ident { "," ident } { ";" ident ":" "proc" Signature } .
7484 Import = "import" ImportItem { "," ImportItem } ";" .
7585 ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] .
7686 Link = "link" string ";" .
@@ -103,10 +113,33 @@
103113 share one instance. Since every instance is checked on its own, an
104114 operation the argument type doesn't support (adding two records, say)
105115 is reported as an error in that instance.
116+
117+What a generic module needs to do with its types, beyond moving them
118+about, it is given as procedures, after the types:
119+
120+```
121+module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
122+```
123+
124+```
125+import Names := Map([]U8, I64, Str.hash, Str.eq);
126+```
106127
107-**Finding modules.** The compiler looks for `Name.jed` in the main
128+A procedure argument names a procedure, not a variable or an
129+expression, and its signature, with the type arguments put in, must be
130+the parameter's. Inside the module, `hash(k)` calls it directly. Two
131+imports make the same instance only if their types and their procedures
132+are the same.
133+
134+**Finding modules.** The compiler looks for module `Name` in the main
108135 module's directory, then in each directory given with `-I`, then in the
109-`lib` directory beside the compiler.
136+`lib` directory beside the compiler. In each directory it takes the
137+first of `Name.OS-ARCH.jed`, `Name.OS.jed`, `Name.ARCH.jed` and
138+`Name.jed` that exists, where `OS-ARCH` is the target, for now only
139+`linux-x86_64`. So a module whose code depends on the machine or the
140+system comes in one file per target, each with the same exports, and
141+the modules that import it don't change. Machine code (section 8) and
142+system calls belong in such files.
110143
111144 **Module bodies.** A module may end with `begin` and statements, its
112145 body. Every module's body runs once, before the main module's `main`:
@@ -445,7 +478,6 @@
445478 | `NEW(p, a)` | sets `p: ^T` to a fresh, zeroed `T` from the allocator `a: ^Mem.Allocator`, or to `nil` if `a` has no room |
446479 | `MSG(e)` | the message of the error `e`, a `[]U8` |
447480 | `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`) |
448-| `CODE(b1, b2, ...)` | puts the constant bytes in the code, see 7.7 |
449481 | `FENCE` | a full memory barrier |
450482 | `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically |
451483 | `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically |
@@ -453,6 +485,10 @@
453485 | `ARGC()` | the number of command line arguments, an `I64` |
454486 | `ARGV()` | the arguments, a `^^U8` (C strings) |
455487
488+`SYSCALL` exists only on Linux targets. Other systems have no stable
489+system call interface, and their modules call the C library instead
490+(section 9).
491+
456492 For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T`
457493 is an 8-byte integer or pointer type (an integer type for `XADD`). They
458494 are sequentially consistent.
@@ -470,7 +506,8 @@
470506 | "for" ident ":=" Expr "to" Expr [ "by" Expr ] "do" Statements "end"
471507 | "match" Expr "of" { "|" Arm } "end"
472508 | "return" [ Expr { "," Expr } ]
473- | "break" .
509+ | "break"
510+ | "defer" Statement .
474511 Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements .
475512 ```
476513
@@ -544,25 +581,31 @@
544581 `FENCE` also orders the program's ordinary accesses against those of
545582 other processors and devices.
546583
547-### 7.7 CODE
584+### 7.7 Defer
548585
549-`CODE` puts machine code where it stands. Between two statements the
550-compiler keeps nothing in registers but `rsp` and `rbp`, so code there
551-may use any other register. It must leave `rsp` and `rbp` as it found
552-them.
586+`defer s;` runs the statement `s` when the program leaves the statement
587+list the `defer` stands in: at its end, or by a `return`, or by a
588+`break` out of it. Statements deferred in the same list run in the
589+opposite order to their `defer`s, and only those whose `defer` has been
590+reached run.
553591
554592 ```
555-proc halt;
556-begin
557- CODE(0FAH, 0F4H); (* cli; hlt *)
558-end;
593+fd := Sys.open(name, Sys.rdonly);
594+if fd < 0 then return Sys.Failed; end;
595+defer Sys.close(fd);
596+...
559597 ```
560598
599+`return e;` evaluates `e` before the deferred statements run. A
600+deferred statement can't contain `return`, `break` or `defer` that
601+would leave it.
602+
561603 ## 8. Procedures
562604
563605 ```
564606 ProcDeclaration = "proc" IdentDef Signature ";"
565- ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] ) .
607+ ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ]
608+ | "code" Expr { "," Expr } ) .
566609 LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" }
567610 | "type" { ident "=" Type ";" }
568611 | "var" { ident { "," ident } ":" Type ";" } .
@@ -601,6 +644,26 @@
601644
602645 Procedures don't nest.
603646
647+**Code procedures.** A procedure whose body is `code` and a list of
648+constant bytes is machine code, written by hand:
649+
650+```
651+proc rdtsc*: U64;
652+code 0FH, 31H, (* rdtsc *)
653+ 48H, 0C1H, 0E2H, 20H, (* shl rdx, 32 *)
654+ 48H, 09H, 0D0H, (* or rax, rdx *)
655+ 0C3H; (* ret *)
656+```
657+
658+It is called like any procedure, with the System V convention: integer
659+and pointer arguments arrive in `rdi`, `rsi`, `rdx`, `rcx`, `r8` and
660+`r9`, `F64` ones in `xmm0` to `xmm7`, and the result goes in `rax` (and
661+`rdx` for a second) or `xmm0`. Its parameters and results are therefore
662+of the types C can take (section 9), at most six integer and eight
663+`F64` parameters, and at most two results. The bytes are the whole
664+procedure, `ret` included; they must keep `rbx`, `rbp`, `rsp` and `r12`
665+to `r15` as they found them. The compiler doesn't look inside them.
666+
604667 ## 9. Calling C
605668
606669 ```
@@ -643,7 +706,7 @@
643706 and indexing arrays and slices is checked, so a program's behaviour is
644707 defined by this report, with one exception: memory reached through a
645708 pointer. Dereferencing a stale or made-up pointer, indexing through a
646-pointer, `CODE` that breaks its rules, and C code can read or write any
709+pointer, a code procedure that breaks its rules, and C code can read or write any
647710 memory, and what follows isn't defined. Dereferencing `nil`, dividing
648711 by zero and calling a `nil` procedure stop the program with a signal.
649712