From 74792a82326e647e289e58ede1e0b011bb97fbd7 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:51:22 +0100 Subject: [PATCH] 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. --- diff --git a/PLAN.md b/PLAN.md index 990accf..8f545a1 100644 --- a/PLAN.md +++ b/PLAN.md @@ -120,7 +120,7 @@ - [x] 4. The tests split: a conformance suite any implementation passes, apart from this compiler's own messages. - [x] 5. The system integer. -- [ ] 6. Optional reals: a target says whether it has them. +- [x] 6. Optional reals: a target says whether it has them. - [ ] 6a. `+` joins string constants (report, section 4). - [ ] 6b. Array constants: `const P: [5]I64 = [...]`, in read-only data, used as a variable that can't be changed (section 4). diff --git a/conform/check.sh b/conform/check.sh index 7e8574f..4687d34 100755 --- a/conform/check.sh +++ b/conform/check.sh @@ -2,7 +2,7 @@ # The conformance suite: programs whose behaviour the report defines, # which every implementation of raz must give, whatever its target. # -# conform/check.sh COMPILER [MESSAGES] +# conform/check.sh [-noreals] COMPILER [MESSAGES] # # run from the directory above conform/. COMPILER is run as # COMPILER -I DIR -o EXE FILE @@ -14,6 +14,8 @@ # run/X.expect its standard output # run/X.args its arguments, one per line (optional) # run/X.status its exit status (optional; 0 if missing) +# run/X.reals there if the program uses reals, which a target may +# leave out (report, appendix A); -noreals skips it # err/X.raz a program the report says is an error, which must not # compile; err/mods holds the modules it imports # @@ -21,10 +23,9 @@ # included, is the implementation's own (appendix A). One that keeps # its text may give MESSAGES, a directory of X.stderr and X.err, the # standard error of run/X and of compiling err/X, compared when there. -# -# Programs that use reals are among the rest, until a target may leave -# them out. +noreals= +if [ "$1" = -noreals ]; then noreals=1; shift; fi raz=$1 msg=${2:-/nonexistent} out=build/conform @@ -34,6 +35,7 @@ for f in conform/run/*.raz; do name=$(basename $f .raz) + [ -n "$noreals" ] && [ -f conform/run/$name.reals ] && continue if ! $raz -I conform/run/mods -o $out/$name $f 2>$out/$name.cerr; then echo "FAIL $f: doesn't compile: $(cat $out/$name.cerr)" fail=$((fail + 1)); continue diff --git a/conform/run/Arith.reals b/conform/run/Arith.reals new file mode 100644 index 0000000..e69de29 diff --git a/conform/run/Defers.reals b/conform/run/Defers.reals new file mode 100644 index 0000000..e69de29 diff --git a/conform/run/Procs.reals b/conform/run/Procs.reals new file mode 100644 index 0000000..e69de29 diff --git a/conform/run/RealRead.reals b/conform/run/RealRead.reals new file mode 100644 index 0000000..e69de29 diff --git a/doc/compiler.md b/doc/compiler.md index 3755a59..dd12b4a 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -11,12 +11,12 @@ | `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout | -| `src/x64.pas` | 693 | 557 | Code buffer, chunks, fixups, places, instruction encoder | -| `src/gen.pas` | 1,733 | 1,481 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,464 | 2,221 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/x64.pas` | 695 | 559 | Code buffer, chunks, fixups, places, instruction encoder | +| `src/gen.pas` | 1,750 | 1,493 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,468 | 2,222 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | -| `src/elf.pas` | 425 | 352 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 227 | 179 | The command line, the entry points | +| `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers | +| `src/raz.pas` | 234 | 185 | The command line, the entry points | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -67,9 +67,9 @@ entry point, `_start`, is written as one more chunk: save `argc` and `argv` and the address of the places (below), call every used module's body, call `main`, call `Rt.exit`. -6. `WriteElf` (`elf.pas`) has `link.pas` mark what `_start` reaches, - place it and patch it, then writes headers, code, data, symbols and - line numbers. +6. `MarkLive` (`link.pas`) marks what `_start` reaches; then + `WriteElf` (`elf.pas`) has `link.pas` place it and patch it, and + writes headers, code, data, symbols and line numbers. ## A procedure, compiled @@ -443,7 +443,12 @@ the program uses: `Use` marks the modules imported from the main module and `Rt`; `UsesC` decides whether the program is dynamic, refusing one that both uses C and has a module saying `link static`. Then it writes -the entry point. A static program starts at `_start` itself. A dynamic +the entry point, and has `MarkLive` mark what it reaches. On a target +without reals (`HasReals`, beside `Target` in `parse.pas`; x86-64 has +them), a live chunk that computes with one is an error at the first +place it does: `UsesReals` in `gen.pas` notes it, from `Value` and a +real literal, on the chunk of the procedure being compiled. A real +moved whole, as a record's bytes are, isn't noted. A static program starts at `_start` itself. A dynamic one starts at a `_start` that calls `__libc_start_main` the way C's own start-up does, which sets C up and calls back a `main` that runs the module bodies and the program's `main`. diff --git a/src/elf.pas b/src/elf.pas index 9a8982f..dea8fbc 100644 --- a/src/elf.pas +++ b/src/elf.pas @@ -316,7 +316,6 @@ cap := 1 shl 16; buf := GetMem(cap); len := 0; - MarkLive(entry); dynamic := numNeeded > 0; roExtra := 0; dynSize := 0; if dynamic then SizeTables(roExtra, dynSize); diff --git a/src/gen.pas b/src/gen.pas index 1038c8f..b047817 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -76,6 +76,7 @@ procedure MakeItem(var x: TItem; o: PObj); function RealOf(const x: TItem): double; +procedure UsesReals(const p: TPos); procedure Value(var x: TItem); procedure Address(var x: TItem; r: longint); procedure Save(var x: TItem); @@ -149,6 +150,7 @@ pushEnd: longint; { the code just after the last push of rax } result: PType; { the procedure's result, or nil } exitL: longint; + codeChunk: longint = -1; { the procedure or body being compiled, or -1 } { parameters arriving in registers: where each is stored on entry } inReg: array[0..13] of longint; inSlot: array[0..13] of int64; @@ -367,8 +369,21 @@ if Unpush then begin dec(pc); dec(depth); pushEnd := -1 end; end; +{ Code computing with a real is noted on its chunk, at the first place, + for a target without reals (report 5.1) to refuse if the chunk turns + out to be used. A real moved whole, as a record's bytes are copied, + needs nothing the target lacks, and isn't noted. } +procedure UsesReals(const p: TPos); +begin + if (codeChunk >= 0) and not chunks^[codeChunk].reals then begin + chunks^[codeChunk].reals := true; + chunks^[codeChunk].realsAt := p; + end; +end; + procedure Value(var x: TItem); begin + if x.typ^.form = fReal then UsesReals(x.pos); case x.mode of mConst: ConstTo(RAX, x); mVar: LoadVar(x, RAX); @@ -1571,6 +1586,7 @@ var i, top: longint; n: int64; begin BeginChunk(c); + codeChunk := c; NoteLine(line, 1); { a declaration starts a line } Push(RBP); MovRR(RBP, RSP); @@ -1707,6 +1723,7 @@ procedure EndProc(c: longint); var n: int64; begin + codeChunk := -1; if depth <> 0 then Fatal('internal error: stack depth'); { running off the end returns zeros } if result <> nil then diff --git a/src/parse.pas b/src/parse.pas index 44375af..fe2aea5 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -27,6 +27,10 @@ Target = 'linux-x86_64'; TargetOS = 'linux'; TargetArch = 'x86_64'; + { Whether the target has reals (report, appendix A): x86-64 always has + SSE2. Without them, used code that computes with one is an error, + which raz.pas reports once it knows what the program uses. } + HasReals: boolean = true; var libDir, mainDir: shortstring; @@ -1053,7 +1057,7 @@ if ival < 0 then MakeConst(x, tU64, ival) else MakeConst(x, tConst, ival); Next; end; - sReal: begin MakeReal(x, rval); Next end; + sReal: begin MakeReal(x, rval); UsesReals(TokPos); Next end; sString: begin MakeStr(x); Next end; sIdent: begin Designator(x, called); diff --git a/src/raz.pas b/src/raz.pas index 21b938c..0ad9cc7 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -212,6 +212,13 @@ Ud2; EndChunk(start); + MarkLive(entry); + if not HasReals then + for i := 0 to numChunks - 1 do + if chunks^[i].live and chunks^[i].reals then + Error(chunks^[i].realsAt, 'this target has no reals'); + if errors > 0 then Finish; + mainPath := path; if run then begin { argv from Main.raz on is the program's, ending in nil as it must } diff --git a/src/x64.pas b/src/x64.pas index 0eba27a..4a62a7b 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -20,7 +20,7 @@ interface -uses scan; +uses scan, diag; const RAX = 0; RCX = 1; RDX = 2; RBX = 3; RSP = 4; RBP = 5; RSI = 6; RDI = 7; @@ -59,6 +59,8 @@ adr: int64; { its address, set by the linker } live: boolean; { reached from the entry point } ext: boolean; { bss: the address of a C symbol, its name } + reals: boolean; { code: computes with reals, first at realsAt } + realsAt: TPos; end; { A place in the source whose code starts at a place in text: for