raz / commit 4b30a3c5

commit
4b30a3c51cd34890223b96448622bfe7ff7b5e7e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
d4dcb2c2
Report for stage 3: F32, real constants, extern var, link static, ending a program, Out and stdio

patch · log

1 file changed, +109 -33

doc/jed.md modified +109 -33

@@ -59,6 +59,12 @@
5959 `0FFFFFFFFFFFFFFFFH`, has type `U64`: so `-1` never fits a `U64`, and
6060 the big number goes nowhere else without a conversion.
6161
62+A real is an untyped constant too, which takes `F32` or `F64`,
63+whichever it is used as, rounded to the nearest value of that type;
64+where nothing says which, it is an `F64`. A real never becomes an
65+integer, nor an integer a real, without a conversion: `x := 1;` is an
66+error for `x: F64`, and `x := 1.0;` is right.
67+
6268 **Characters.** `'a'` is the code point of one character, an integer:
6369 `'a'` is 97 and `'é'` is 233.
6470
@@ -88,7 +94,7 @@
8894 ModParams = ident { "," ident } { ";" ident ":" "proc" Signature } .
8995 Import = "import" ImportItem { "," ImportItem } ";" .
9096 ImportItem = ident [ ":=" ident [ "(" Type { "," Type } ")" ] ] .
91-Link = "link" string ";" .
97+Link = "link" ( string | "static" ) ";" .
9298 ```
9399
94100 A module lives in a file of its own name: module `Out` is `Out.jed`.
@@ -171,7 +177,9 @@
171177 are zero before any body runs. `return;` ends a body early.
172178
173179 **`link "libX11.so.6";`** names a shared library to load at run time,
174-for the `extern` procedures of section 9.
180+for the `extern` declarations of section 9. **`link static;`** says the
181+module needs a program without C (section 10): it makes its own system
182+calls, or its own threads, which the C library wouldn't know about.
175183
176184 ## 4. Declarations
177185
@@ -195,11 +203,11 @@
195203
196204 **Variables** of a module live as long as the program; variables of a
197205 procedure live as long as its call. Both start at zero: every byte of
198-them is zero. So a number starts as `0` (`0.0` for `F64`), a `BOOL` as
199-`false`, a pointer, procedure or `ERROR` as `nil`, a slice as the empty
200-slice (`ptr` is `nil`, `len` is 0), a variant as its first case with
201-its fields zero, a set as the empty set, and an enumeration as its
202-first name. No variable is ever read before it has a value.
206+them is zero. So a number starts as `0` (`0.0` for `F32` and `F64`),
207+a `BOOL` as `false`, a pointer, procedure or `ERROR` as `nil`, a slice
208+as the empty slice (`ptr` is `nil`, `len` is 0), a variant as its
209+first case with its fields zero, a set as the empty set, and an
210+enumeration as its first name. No variable is ever read before it has a value.
203211
204212 ## 5. Types
205213
@@ -225,6 +233,7 @@
225233 | `I8` `I16` `I32` `I64` | 1, 2, 4, 8 | signed integers, two's complement |
226234 | `U8` `U16` `U32` `U64` | 1, 2, 4, 8 | unsigned integers |
227235 | `BOOL` | 1 | `false` (0), `true` (1) |
236+| `F32` | 4 | IEEE 754 single precision |
228237 | `F64` | 8 | IEEE 754 double precision |
229238
230239 ### 5.2 Arrays
@@ -368,8 +377,8 @@
368377 - `A` is a procedure and `B` a procedure type with the same signature.
369378
370379 Nothing else converts silently. In particular, there is no conversion
371-between integer types, or between integers and `F64`, without a written
372-conversion (section 6.7).
380+between integer types, between integers and reals, or between `F32`
381+and `F64`, without a written conversion (section 6.7).
373382
374383 ## 6. Expressions
375384
@@ -421,8 +430,12 @@
421430
422431 ### 6.3 Reals
423432
424-`+ - * /` and the relations work on `F64`, both operands `F64`. A real
425-literal has type `F64`.
433+`+ - * /` and the relations work on `F32` and on `F64`. Both operands
434+have the same type, or one is a real constant, which takes the other's
435+type (section 2). The result has that type: `F32` arithmetic is done in
436+single precision, rounded at each step as C rounds `float`. A
437+constant expression of reals is evaluated in double precision, then
438+rounded once to the type it takes.
426439
427440 ### 6.4 Pointers
428441
@@ -487,8 +500,10 @@
487500 | From | To | Result |
488501 |---|---|---|
489502 | integer | integer | the value cut to the new width, or extended by its sign (signed) or zeros (unsigned) |
490-| integer | `F64` | the nearest real |
491-| `F64` | integer | rounded toward zero; a NaN, or a real out of the range of `I64`, gives the smallest `I64`, then cut |
503+| integer | `F32`, `F64` | the nearest real |
504+| `F32`, `F64` | integer | rounded toward zero; a NaN, or a real out of the range of `I64`, gives the smallest `I64`, then cut |
505+| `F32` | `F64` | the same value |
506+| `F64` | `F32` | the nearest `F32`; too big a value gives an infinity |
492507 | enumeration | integer | its position |
493508 | integer | enumeration | the name at that position; a position out of range is a failed check (6.5) |
494509 | set | integer | its bits |
@@ -692,32 +707,57 @@
692707
693708 It is called like any procedure, with the System V convention: integer
694709 and pointer arguments arrive in `rdi`, `rsi`, `rdx`, `rcx`, `r8` and
695-`r9`, `F64` ones in `xmm0` to `xmm7`, and the result goes in `rax` (and
696-`rdx` for a second) or `xmm0`. Its parameters and results are therefore
697-of the types C can take (section 9), at most six integer and eight
698-`F64` parameters, and at most two results. The bytes are the whole
710+`r9`, `F32` and `F64` ones in `xmm0` to `xmm7`, and the result goes in
711+`rax` (and `rdx` for a second) or `xmm0`. Its parameters and results
712+are therefore of the types C can take (section 9), at most six integer
713+and eight real parameters, and at most two results. The bytes are the whole
699714 procedure, `ret` included; they must keep `rbx`, `rbp`, `rsp` and `r12`
700715 to `r15` as they found them. The compiler doesn't look inside them.
701716
702717 ## 9. Calling C
703718
704719 ```
705-ExternDeclaration = "extern" "proc" IdentDef Signature .
720+ExternDeclaration = "extern" ( "proc" IdentDef Signature
721+ | "var" IdentDef ":" Type ) .
706722 ```
707723
708724 ```
709725 link "libX11.so.6";
710726 extern proc XOpenDisplay(name: ^U8): ^U8;
711727 extern proc printf(format: ^U8; ...): I32;
728+extern var environ: ^^U8;
712729 ```
713730
714-An `extern` procedure is found by its name in the C libraries the
715-program links: the C library always, and each library named by `link`.
716-Its parameters and result are integers, `BOOL`, `F64`, pointers or
717-procedures; a record goes to C by pointer, never by value. `...`
718-accepts any number of further arguments of those types, and is allowed
719-only in an `extern`.
731+An `extern` procedure or variable is found by its name in the C
732+libraries the program links: the C library always, and each library
733+named by `link`.
734+
735+An `extern` procedure's parameters and result are integers, `BOOL`,
736+`F32`, `F64`, pointers or procedures; a record goes to C by pointer,
737+never by value. `...` accepts any number of further arguments of those
738+types, and is allowed only in an `extern`. As C does, an `F32` given
739+to `...` is passed as an `F64`, and a real constant there is an `F64`.
720740
741+An `extern` variable is C's variable itself: reading it reads C's,
742+assigning it writes C's. It can be of any type, declared as C declares
743+it, a record laid out as C lays it out (section 5.5); the compiler
744+can't check that it is. A C variable that is local to each thread,
745+such as `errno`, can't be reached this way; the C library has a
746+procedure for it (`__errno_location`).
747+
748+A C union has no jed type. A program reads one as an array of words,
749+and the member it wants through a pointer conversion:
750+
751+```
752+var ev: [24]I64; (* XEvent: 192 bytes *)
753+...
754+XNextEvent(dpy, ADR(ev));
755+if I32(ev[0]) = KeyPress then
756+ key := ^XKeyEvent(ADR(ev));
757+ ...
758+end;
759+```
760+
721761 Every jed procedure uses the System V calling convention, so a jed
722762 procedure whose parameters and result are of those types can be given
723763 to C as a callback.
@@ -727,12 +767,29 @@
727767 Any module may export a procedure `main`, declared `proc main*;` or
728768 `proc main*: I64;`. The program runs the bodies of all its modules
729769 (section 3), then the main module's `main`, if it has one, and ends
730-with status 0, or with the status `main` returns. The `main` of an
731-imported module is an ordinary procedure, which runs only if something
732-calls it; so a library module can carry a `main` that tests it.
733-A program that calls no C is a static executable with no run time
734-besides the code it uses. One that does is linked dynamically and
735-starts through the C library, so that C's own set-up and clean-up run.
770+with status 0, or with the status `main` returns, by calling
771+`Rt.exit`. The `main` of an imported module is an ordinary procedure,
772+which runs only if something calls it; so a library module can carry a
773+`main` that tests it.
774+A program uses C if any of its modules has an `extern` declaration or
775+a `link` of a library. A program that doesn't is a static executable
776+with no run time besides the code it uses. One that does is linked
777+dynamically and starts through the C library, so that C's own set-up
778+and clean-up run. A program with a module that says `link static;`
779+must not use C, and the compiler reports an error if it does.
780+
781+**Ending.** `Rt.exit(status)` ends the program. First it runs the
782+procedures registered with `Rt.atExit`, the last registered first,
783+each at most once; then it ends the program with `status`, through
784+C's `exit` in a program that uses C, so that C flushes its own
785+buffers. A failed check (section 6.5) runs them too, after its
786+message, so that `Out`'s buffer is written out. `Sys.exit` ends the
787+program at once, and a program stopped by a signal runs none of them.
788+
789+```
790+proc atExit*(p: proc): ERROR; (* Rt.Full if 32 are registered *)
791+proc exit*(status: I64);
792+```
736793
737794 Only what can be reached from `main` and the module bodies goes into
738795 the executable: unused procedures, variables and strings are left out.
@@ -751,9 +808,9 @@
751808
752809 | Module | What |
753810 |---|---|
754-| `Rt` | what the compiler's checks call when they fail, and string comparison |
755-| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target |
756-| `Out` | writing text and numbers to standard output |
811+| `Rt` | ending the program, `exit` and `atExit`; what the compiler's checks call when they fail; string comparison |
812+| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target, and `Sys.jed` over the C library for the others |
813+| `Out` | writing text and numbers to standard output, buffered |
757814 | `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
758815 | `Heap` | a general allocator, `Heap.heap` |
759816 | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
@@ -772,4 +829,23 @@
772829 end;
773830 ...
774831 end Mem.
832+```
833+
834+**Out and C's stdio.** `Out` keeps a buffer of 4 KB. It writes it out
835+when it is full, at each line feed when standard output is a terminal,
836+at `Out.flush`, and when the program ends by `Rt.exit`. C's `stdio`
837+keeps a buffer of its own for the same standard output. A program that
838+writes with both flushes one before writing with the other, or the
839+text comes out of order:
840+
841+```
842+extern proc printf(format: ^U8; ...): I32;
843+extern proc fflush(f: ^U8): I32;
844+extern var stdout: ^U8; (* C's FILE *stdout *)
845+...
846+Out.str("from jed\n");
847+Out.flush;
848+printf("from C\n");
849+fflush(stdout);
850+Out.str("from jed again\n");
775851 ```