| | @@ -31,31 +31,40 @@ |
| 31 | 31 | |
| 32 | 32 | ## 2. Words and symbols |
| 33 | 33 | |
| 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. |
| 36 | 39 | |
| 37 | | -**Identifiers** are a letter or `_` followed by letters, digits and `_`, |
| 40 | +**Identifiers** are an ASCII letter or `_` followed by letters, digits and `_`, |
| 38 | 41 | up to 63 characters. Case matters: `count` and `Count` are different |
| 39 | 42 | names. |
| 40 | 43 | |
| 41 | 44 | **Reserved words** are lowercase, except `VOLATILE`: |
| 42 | 45 | |
| 43 | 46 | ``` |
| 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 |
| 47 | 51 | ``` |
| 48 | 52 | |
| 49 | 53 | **Numbers.** `123` is decimal. `0FFH` is hexadecimal: it starts with a |
| 50 | 54 | digit and ends in `H`. `1.5` and `2.0E-3` are reals; a real always has |
| 51 | 55 | 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. |
| 52 | 59 | |
| 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). |
| 54 | 66 | |
| 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. |
| 59 | 68 | |
| 60 | 69 | **Comments** are `(*` ... `*)` and nest. |
| 61 | 70 | |
| | @@ -68,9 +77,10 @@ |
| 68 | 77 | ## 3. Modules |
| 69 | 78 | |
| 70 | 79 | ``` |
| 71 | | -Module = "module" ident [ "(" ident { "," ident } ")" ] ";" |
| 80 | +Module = "module" ident [ "(" ModParams ")" ] ";" |
| 72 | 81 | { Import } { Link } { Declaration } |
| 73 | 82 | [ "begin" Statements ] "end" ident "." . |
| 83 | +ModParams = ident { "," ident } { ";" ident ":" "proc" Signature } . |
| 74 | 84 | Import = "import" ImportItem { "," ImportItem } ";" . |
| 75 | 85 | ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] . |
| 76 | 86 | Link = "link" string ";" . |
| | @@ -103,10 +113,33 @@ |
| 103 | 113 | share one instance. Since every instance is checked on its own, an |
| 104 | 114 | operation the argument type doesn't support (adding two records, say) |
| 105 | 115 | 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 | +``` |
| 106 | 127 | |
| 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 |
| 108 | 135 | 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. |
| 110 | 143 | |
| 111 | 144 | **Module bodies.** A module may end with `begin` and statements, its |
| 112 | 145 | body. Every module's body runs once, before the main module's `main`: |
| | @@ -445,7 +478,6 @@ |
| 445 | 478 | | `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 | |
| 446 | 479 | | `MSG(e)` | the message of the error `e`, a `[]U8` | |
| 447 | 480 | | `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 | |
| 449 | 481 | | `FENCE` | a full memory barrier | |
| 450 | 482 | | `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; atomically | |
| 451 | 483 | | `XCHG(p, x)` | sets `p^ := x` and returns the value before, atomically | |
| | @@ -453,6 +485,10 @@ |
| 453 | 485 | | `ARGC()` | the number of command line arguments, an `I64` | |
| 454 | 486 | | `ARGV()` | the arguments, a `^^U8` (C strings) | |
| 455 | 487 | |
| 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 | + |
| 456 | 492 | For `CAS`, `XCHG` and `XADD`, `p` is a `^T` or `^VOLATILE T` where `T` |
| 457 | 493 | is an 8-byte integer or pointer type (an integer type for `XADD`). They |
| 458 | 494 | are sequentially consistent. |
| | @@ -470,7 +506,8 @@ |
| 470 | 506 | | "for" ident ":=" Expr "to" Expr [ "by" Expr ] "do" Statements "end" |
| 471 | 507 | | "match" Expr "of" { "|" Arm } "end" |
| 472 | 508 | | "return" [ Expr { "," Expr } ] |
| 473 | | - | "break" . |
| 509 | + | "break" |
| 510 | + | "defer" Statement . |
| 474 | 511 | Arm = ( ident [ "(" ident { "," ident } ")" ] | "else" ) ":" Statements . |
| 475 | 512 | ``` |
| 476 | 513 | |
| | @@ -544,25 +581,31 @@ |
| 544 | 581 | `FENCE` also orders the program's ordinary accesses against those of |
| 545 | 582 | other processors and devices. |
| 546 | 583 | |
| 547 | | -### 7.7 CODE |
| 584 | +### 7.7 Defer |
| 548 | 585 | |
| 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. |
| 553 | 591 | |
| 554 | 592 | ``` |
| 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 | +... |
| 559 | 597 | ``` |
| 560 | 598 | |
| 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 | + |
| 561 | 603 | ## 8. Procedures |
| 562 | 604 | |
| 563 | 605 | ``` |
| 564 | 606 | ProcDeclaration = "proc" IdentDef Signature ";" |
| 565 | | - ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] ) . |
| 607 | + ( "forward" | { LocalDeclaration } "begin" Statements "end" [ ident ] |
| 608 | + | "code" Expr { "," Expr } ) . |
| 566 | 609 | LocalDeclaration = "const" { ident [ ":" "ERROR" ] "=" Expr ";" } |
| 567 | 610 | | "type" { ident "=" Type ";" } |
| 568 | 611 | | "var" { ident { "," ident } ":" Type ";" } . |
| | @@ -601,6 +644,26 @@ |
| 601 | 644 | |
| 602 | 645 | Procedures don't nest. |
| 603 | 646 | |
| 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 | + |
| 604 | 667 | ## 9. Calling C |
| 605 | 668 | |
| 606 | 669 | ``` |
| | @@ -643,7 +706,7 @@ |
| 643 | 706 | and indexing arrays and slices is checked, so a program's behaviour is |
| 644 | 707 | defined by this report, with one exception: memory reached through a |
| 645 | 708 | 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 |
| 647 | 710 | memory, and what follows isn't defined. Dereferencing `nil`, dividing |
| 648 | 711 | by zero and calling a `nil` procedure stop the program with a signal. |
| 649 | 712 | |