raz / commit 74792a82

commit
74792a82326e647e289e58ede1e0b011bb97fbd7
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
2411fde8
Step 6: optional reals

HasReals, beside Target in parse.pas, says whether the target has
reals (x86-64: yes). gen's UsesReals notes on a proc's chunk the first
place its code computes with a real (Value of an F32/F64, a real
literal); after MarkLive, now called from raz.pas, not WriteElf, a
live chunk so noted is an error, "this target has no reals", when the
target lacks them. Unused code and module-level decls aren't noted,
so Fmt's interface can stay the same; a real moved whole (record
copy) needs no FPU and isn't noted either. No option to test it: it
was checked by flipping the const by hand.

conform/check.sh -noreals skips run/X with an X.reals marker (Arith,
Defers, Procs, RealRead), as appendix A has it.

patch · log

12 files changed, +53 -17

PLAN.md (history) modified +1 -1

@@ -120,7 +120,7 @@
120120 - [x] 4. The tests split: a conformance suite any implementation
121121 passes, apart from this compiler's own messages.
122122 - [x] 5. The system integer.
123-- [ ] 6. Optional reals: a target says whether it has them.
123+- [x] 6. Optional reals: a target says whether it has them.
124124 - [ ] 6a. `+` joins string constants (report, section 4).
125125 - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only
126126 data, used as a variable that can't be changed (section 4).

conform/check.sh (history) modified +6 -4

@@ -2,7 +2,7 @@
22 # The conformance suite: programs whose behaviour the report defines,
33 # which every implementation of raz must give, whatever its target.
44 #
5-# conform/check.sh COMPILER [MESSAGES]
5+# conform/check.sh [-noreals] COMPILER [MESSAGES]
66 #
77 # run from the directory above conform/. COMPILER is run as
88 # COMPILER -I DIR -o EXE FILE
@@ -14,6 +14,8 @@
1414 # run/X.expect its standard output
1515 # run/X.args its arguments, one per line (optional)
1616 # run/X.status its exit status (optional; 0 if missing)
17+# run/X.reals there if the program uses reals, which a target may
18+# leave out (report, appendix A); -noreals skips it
1719 # err/X.raz a program the report says is an error, which must not
1820 # compile; err/mods holds the modules it imports
1921 #
@@ -21,10 +23,9 @@
2123 # included, is the implementation's own (appendix A). One that keeps
2224 # its text may give MESSAGES, a directory of X.stderr and X.err, the
2325 # standard error of run/X and of compiling err/X, compared when there.
24-#
25-# Programs that use reals are among the rest, until a target may leave
26-# them out.
2726
27+noreals=
28+if [ "$1" = -noreals ]; then noreals=1; shift; fi
2829 raz=$1
2930 msg=${2:-/nonexistent}
3031 out=build/conform
@@ -34,6 +35,7 @@
3435
3536 for f in conform/run/*.raz; do
3637 name=$(basename $f .raz)
38+ [ -n "$noreals" ] && [ -f conform/run/$name.reals ] && continue
3739 if ! $raz -I conform/run/mods -o $out/$name $f 2>$out/$name.cerr; then
3840 echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)"
3941 fail=$((fail + 1)); continue

conform/run/Arith.reals (history) added +0 -0

An empty file.

conform/run/Defers.reals (history) added +0 -0

An empty file.

conform/run/Procs.reals (history) added +0 -0

An empty file.

conform/run/RealRead.reals (history) added +0 -0

An empty file.

doc/compiler.md (history) modified +14 -9

@@ -11,12 +11,12 @@
1111 | `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts |
1212 | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout |
14-| `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder |
15-| `src/gen.pas` | 1,733 | 1,481 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,464 | 2,221 | Declarations, statements, expressions, built-ins, recovery, warnings |
14+| `src/x64.pas` | 695 | 559 | Code buffer, chunks, fixups, places, instruction encoder |
15+| `src/gen.pas` | 1,750 | 1,493 | Items: loading, storing, copying, frames, calls |
16+| `src/parse.pas` | 2,468 | 2,222 | Declarations, statements, expressions, built-ins, recovery, warnings |
1717 | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places |
18-| `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers |
19-| `src/raz.pas` | 227 | 179 | The command line, the entry points |
18+| `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers |
19+| `src/raz.pas` | 234 | 185 | The command line, the entry points |
2020
2121 Every file opens with a comment that says what it is, what it depends
2222 on and what it leaves out. Read those first; they are the map.
@@ -67,9 +67,9 @@
6767 entry point, `_start`, is written as one more chunk: save `argc` and
6868 `argv` and the address of the places (below), call every used
6969 module's body, call `main`, call `Rt.exit`.
70-6. `WriteElf` (`elf.pas`) has `link.pas` mark what `_start` reaches,
71- place it and patch it, then writes headers, code, data, symbols and
72- line numbers.
70+6. `MarkLive` (`link.pas`) marks what `_start` reaches; then
71+ `WriteElf` (`elf.pas`) has `link.pas` place it and patch it, and
72+ writes headers, code, data, symbols and line numbers.
7373
7474 ## A procedure, compiled
7575
@@ -443,7 +443,12 @@
443443 the program uses: `Use` marks the modules imported from the main module
444444 and `Rt`; `UsesC` decides whether the program is dynamic, refusing one
445445 that both uses C and has a module saying `link static`. Then it writes
446-the entry point. A static program starts at `_start` itself. A dynamic
446+the entry point, and has `MarkLive` mark what it reaches. On a target
447+without reals (`HasReals`, beside `Target` in `parse.pas`; x86-64 has
448+them), a live chunk that computes with one is an error at the first
449+place it does: `UsesReals` in `gen.pas` notes it, from `Value` and a
450+real literal, on the chunk of the procedure being compiled. A real
451+moved whole, as a record's bytes are, isn't noted. A static program starts at `_start` itself. A dynamic
447452 one starts at a `_start` that calls `__libc_start_main` the way C's own
448453 start-up does, which sets C up and calls back a `main` that runs the
449454 module bodies and the program's `main`.

src/elf.pas modified +0 -1

@@ -316,7 +316,6 @@
316316 cap := 1 shl 16;
317317 buf := GetMem(cap);
318318 len := 0;
319- MarkLive(entry);
320319 dynamic := numNeeded > 0;
321320 roExtra := 0; dynSize := 0;
322321 if dynamic then SizeTables(roExtra, dynSize);

src/gen.pas modified +17 -0

@@ -76,6 +76,7 @@
7676 procedure MakeItem(var x: TItem; o: PObj);
7777 function RealOf(const x: TItem): double;
7878
79+procedure UsesReals(const p: TPos);
7980 procedure Value(var x: TItem);
8081 procedure Address(var x: TItem; r: longint);
8182 procedure Save(var x: TItem);
@@ -149,6 +150,7 @@
149150 pushEnd: longint; { the code just after the last push of rax }
150151 result: PType; { the procedure's result, or nil }
151152 exitL: longint;
153+ codeChunk: longint = -1; { the procedure or body being compiled, or -1 }
152154 { parameters arriving in registers: where each is stored on entry }
153155 inReg: array[0..13] of longint;
154156 inSlot: array[0..13] of int64;
@@ -367,8 +369,21 @@
367369 if Unpush then begin dec(pc); dec(depth); pushEnd := -1 end;
368370 end;
369371
372+{ Code computing with a real is noted on its chunk, at the first place,
373+ for a target without reals (report 5.1) to refuse if the chunk turns
374+ out to be used. A real moved whole, as a record's bytes are copied,
375+ needs nothing the target lacks, and isn't noted. }
376+procedure UsesReals(const p: TPos);
377+begin
378+ if (codeChunk >= 0) and not chunks^[codeChunk].reals then begin
379+ chunks^[codeChunk].reals := true;
380+ chunks^[codeChunk].realsAt := p;
381+ end;
382+end;
383+
370384 procedure Value(var x: TItem);
371385 begin
386+ if x.typ^.form = fReal then UsesReals(x.pos);
372387 case x.mode of
373388 mConst: ConstTo(RAX, x);
374389 mVar: LoadVar(x, RAX);
@@ -1571,6 +1586,7 @@
15711586 var i, top: longint; n: int64;
15721587 begin
15731588 BeginChunk(c);
1589+ codeChunk := c;
15741590 NoteLine(line, 1); { a declaration starts a line }
15751591 Push(RBP);
15761592 MovRR(RBP, RSP);
@@ -1707,6 +1723,7 @@
17071723 procedure EndProc(c: longint);
17081724 var n: int64;
17091725 begin
1726+ codeChunk := -1;
17101727 if depth <> 0 then Fatal('internal error: stack depth');
17111728 { running off the end returns zeros }
17121729 if result <> nil then

src/parse.pas modified +5 -1

@@ -27,6 +27,10 @@
2727 Target = 'linux-x86_64';
2828 TargetOS = 'linux';
2929 TargetArch = 'x86_64';
30+ { Whether the target has reals (report, appendix A): x86-64 always has
31+ SSE2. Without them, used code that computes with one is an error,
32+ which raz.pas reports once it knows what the program uses. }
33+ HasReals: boolean = true;
3034
3135 var
3236 libDir, mainDir: shortstring;
@@ -1053,7 +1057,7 @@
10531057 if ival < 0 then MakeConst(x, tU64, ival) else MakeConst(x, tConst, ival);
10541058 Next;
10551059 end;
1056- sReal: begin MakeReal(x, rval); Next end;
1060+ sReal: begin MakeReal(x, rval); UsesReals(TokPos); Next end;
10571061 sString: begin MakeStr(x); Next end;
10581062 sIdent: begin
10591063 Designator(x, called);

src/raz.pas modified +7 -0

@@ -212,6 +212,13 @@
212212 Ud2;
213213 EndChunk(start);
214214
215+ MarkLive(entry);
216+ if not HasReals then
217+ for i := 0 to numChunks - 1 do
218+ if chunks^[i].live and chunks^[i].reals then
219+ Error(chunks^[i].realsAt, 'this target has no reals');
220+ if errors > 0 then Finish;
221+
215222 mainPath := path;
216223 if run then begin
217224 { argv from Main.raz on is the program's, ending in nil as it must }

src/x64.pas modified +3 -1

@@ -20,7 +20,7 @@
2020
2121 interface
2222
23-uses scan;
23+uses scan, diag;
2424
2525 const
2626 RAX = 0; RCX = 1; RDX = 2; RBX = 3; RSP = 4; RBP = 5; RSI = 6; RDI = 7;
@@ -59,6 +59,8 @@
5959 adr: int64; { its address, set by the linker }
6060 live: boolean; { reached from the entry point }
6161 ext: boolean; { bss: the address of a C symbol, its name }
62+ reals: boolean; { code: computes with reals, first at realsAt }
63+ realsAt: TPos;
6264 end;
6365
6466 { A place in the source whose code starts at a place in text: for