From a5915c27e21418cc888dd221d7739a808b541f03 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:15:48 +0100 Subject: [PATCH] Step 9d: the trap report parse's Body notes each procedure's scalar parameters and variables (NoteVar, chunk.pas): name, place in the frame, and VarKind, how Rt shows it (signed, unsigned, BOOL, address, real; size; through the slot's address for a var parameter). link's MakePlaces adds to the places each live procedure's range and name and its variables; Rt reads them. On a failed check, a code-tested stop or a signal, Rt follows the rbp chain (callerFrame, framePtr) and says each call under way, at most 20, with its variables (calls, putValue, putReal, putHex); a stack overflow's first frame without them. Rt.trace says the same on demand. Thread.start clears rbp in a new thread so the chain ends there. ChunkName moves to chunk.pas, for link and the listing. Report 2026-10.12: sections 10 and 11 say so; Rt.trace. The tests' expected standard error gains the calls; check.sh and conform/check.sh compare it with addresses masked (0x?). test/libc/Through drops a freed pointer. test/run/Trace. --- diff --git a/PLAN.md b/PLAN.md index 33b4e8a..932507c 100644 --- a/PLAN.md +++ b/PLAN.md @@ -152,7 +152,7 @@ encoder knows and a few of the system's, bytes for the rest. - [x] 9c. `raz -S`: each line of source with its instructions, named by the encoder as it makes them, not by a disassembler. -- [ ] 9d. The trap report: on a failed check or a trap, the calls +- [x] 9d. The trap report: on a failed check or a trap, the calls under way, each with its variables by name and value, from tables the compiler writes; `Rt.trace` gives the same on demand. diff --git a/README.md b/README.md index 65e6dc2..a6edc27 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,14 @@ `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends the program with status 2. A program stopped by a signal says where it was and why first: a `nil` pointer, a `nil` procedure, a stack -overflow, a bad address or a division fault. +overflow, a bad address or a division fault. Either then says the +calls under way, each with its variables: + + Main.raz:12:9: index 7 is out of range for a length of 5 + in Main.get at Main.raz:12:9: i = 7, n = 5 + in Main.main at Main.raz:20:5: count = 3 + +`Rt.trace` says the same at any time. `raz run Main.raz a b` compiles the same way, then runs the program with the arguments `a b`, writing no file: its output and exit status diff --git a/conform/check.sh b/conform/check.sh index 4687d34..9b56675 100755 --- a/conform/check.sh +++ b/conform/check.sh @@ -22,7 +22,8 @@ # What a program says on standard error, the compiler's messages # 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. +# standard error of run/X and of compiling err/X, compared when there, +# with each address it shows, 0x and hex digits, as 0x?. noreals= if [ "$1" = -noreals ]; then noreals=1; shift; fi @@ -55,7 +56,8 @@ elif [ $status != $want ]; then echo "FAIL $f: status $status, not $want" fail=$((fail + 1)) - elif [ -f $msg/$name.stderr ] && ! cmp -s $out/$name.err $msg/$name.stderr; then + elif [ -f $msg/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err | + cmp -s - $msg/$name.stderr; then echo "FAIL $f: standard error differs" diff $out/$name.err $msg/$name.stderr | head -5 fail=$((fail + 1)) diff --git a/doc/book/PLAN.md b/doc/book/PLAN.md new file mode 100644 index 0000000..ea6c683 --- /dev/null +++ b/doc/book/PLAN.md @@ -0,0 +1,106 @@ +# Stage: the book "Compiling Raz" + +This is the owner's go-ahead for a new stage: a book, "Compiling Raz", +explaining how the raz compiler works. Follow CLAUDE.md's way of working: +one commit per step, push it, then stop for review. Don't change the +language or the compiler for the book's sake. If the book exposes a bug +or a clumsy spot, report it rather than fixing it. + +## Read first +- CLAUDE.md, doc/raz.md (the report), doc/compiler.md, README.md. +- src/*.pas, the whole compiler (about 5,000 lines), and enough of lib/ + (Rt, Heap, Mem, Thread, Sys) to explain the runtime. +- The tiza skill, for drawings. + +## The reader +A working programmer: not good at Pascal, little knowledge of compilers. +- Assume they roughly know registers, the stack and memory. Explain only + what raz does with them (frames, calling convention, layout). +- Introduce compiler ideas (tokens, grammar, generating code while + parsing, symbol tables) briefly and in friendly terms where they first + appear. +- Teach just enough Pascal to read the compiler: one early chapter, then + more as features come up. Teach enough raz the same way. +- Keep the two languages visibly apart: ```raz and ```pascal code blocks, + and say which one is on screen. +- Don't explain formats that can be looked up. ELF gets a page on what + raz writes and why, plus a pointer to the spec. +- Target 40-50k words. + +## Tone +Like Learn You a Haskell and Starting FORTH: tiny steps, talking straight +to the reader, every idea shown at once on a real example, drawings that +carry meaning. Warm, with an occasional dry aside. Never a joke per +paragraph; humor is seasoning. Plain, short sentences. + +## Mascot: Razmataz +A small, very serious creature with a flashy name, which suits a frugal +compiler (razzmatazz minus one z). +- Its motto, "once is enough", plays on "raz" meaning "one" or "once" in + Polish and Russian. Check that the compiler really works in one pass + before leaning on this; if it doesn't, find the honest version of the + joke. +- Razmataz appears in tiza drawings: eating characters in the scanner, + stacking boxes in frames, throwing unused code overboard in linking. + +## Structure +Build up gradually, not unit by unit: +1. Introduction and a whirlwind tour: compile `exit 42` (or the smallest + real program) through every stage, shallowly. +2. Enough Pascal (and raz) to follow along. +3. One chapter per language feature, followed through scanner, parser, + symbols, code generator and machine code: variables and expressions, + control flow, procedures and frames, records and arrays, pointers and + the heap, strings, modules, defer, C interop, threads. +4. Closing chapters: x64 encoding, link (tree shaking, layout), elf, + speed (how it reaches 400k lines a second), and where to go next. + +Refine this order in the outline (step 1). + +## Format and tooling +- Markdown in doc/book/: NN-slug.md per chapter, plus metadata.yaml + (title, author Luis, cover). It must read well as plain text. +- `make book` builds build/compiling-raz.epub with pandoc. If pandoc gets + installed locally, update the desktop guide (see the global + CLAUDE.md). +- Drawings: .tiza source and a rendered PNG, both committed (CI has no + tiza). Every image has a caption that stands on its own in plain text. + Use ASCII art in code blocks for diagrams that are really structure + (frames, memory layout); those read the same in both formats. +- Disassembly is cleaned up: instructions only, the raz source line + beside them as a comment, and bytes only where the bytes are the point. + Show raw objdump output once, early, next to the cleaned version, and + say how to get it. +- A checker, written in raz alongside tools/Doc.raz (reuse what exists), + run by `make check`. It must: + - compile and run every raz example marked as runnable and compare the + expected output written in the book; + - confirm every quoted Pascal excerpt still matches src/, quoted by + unit and routine name, never by line number; + - regenerate or verify the cleaned disassembly. + The goal: when the compiler changes, `make check` fails rather than + the book going quietly wrong. +- CI (.github/workflows/): on a push to main that touches doc/book/**, + src/** or lib/**, build the epub and replace the file on a standing + prerelease tagged `book` (gh release upload --clobber), so the owner + has a fixed URL to open on an iPad. Tagged releases also attach that + version's epub. + +## Steps (each one is a commit, then stop for review) +1. The outline: doc/book/OUTLINE.md, chapter by chapter, with what each + one teaches, its running example, and the drawings planned. +2. The tooling: doc/book/ skeleton, `make book`, the checker in + `make check`, the CI job. One placeholder chapter proves the pipeline + end to end. +3. Razmataz's design sheet (tiza) and one sample chapter, the procedures + and frames one, to set tone and depth. +4. Then one chapter per step, in order. + +doc/compiler.md stays as it is: the short guide for people changing the +compiler. The book can point to it but never replaces it. + +## Left to the tooling step +- How the checker marks runnable examples and quoted excerpts inside + Markdown. +- Whether the cleaned disassembly comes from the compiler's own line + information (`make gdb` suggests it has some) or from objdump. diff --git a/doc/compiler.md b/doc/compiler.md index 42e5fc2..40f09fc 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -11,12 +11,12 @@ | `src/diag.pas` | 296 | 218 | 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/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 1,039 | 877 | The instruction encoder, jumps within a procedure, the assembler | +| `src/chunk.pas` | 276 | 217 | The compiled program: chunks, fixups, line notes | +| `src/x64.pas` | 1,031 | 870 | The instruction encoder, jumps within a procedure, the assembler | | `src/gen.pas` | 1,321 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,529 | 2,267 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | +| `src/parse.pas` | 2,559 | 2,291 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/link.pas` | 228 | 180 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers | | `src/raz.pas` | 195 | 147 | The command line | @@ -496,9 +496,15 @@ Between placing text and placing rodata, `MakePlaces` writes the places, a rodata chunk: for each line note in live code, its address, -line, column and file. `Rt` reads them, through `Rt.places`, which the +line, column and file; and for the trap report, each live procedure's +address range and name, and its variables, which `Body` (`parse.pas`) +notes with `NoteVar`: each one's name, its place in the frame and how +`Rt` shows it (`VarKind`). `Rt` reads them, through `Rt.places`, which the entry point sets, to say where a failed check or a signal was: the -place with the highest address up to the one it has. Its signal +place with the highest address up to the one it has. Then it follows +the chain of frames, each `rbp` pointing at its caller's, and says +each call under way and its variables (`calls`); `Rt.trace` does the +same on demand. A thread's first frame has `rbp` zero, which ends it. Its signal handlers, set in its body, run on a stack of their own, so that they can tell a stack overflow, and leave by returning to the faulting instruction with the handler reset, so that the signal ends the diff --git a/doc/raz.md b/doc/raz.md index 2c601fb..cb639d5 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -1125,8 +1125,13 @@ ``` proc atExit*(p: proc): ERROR; (* Rt.Full if 32 are registered *) proc exit*(status: INT); +proc trace*; (* the calls under way, on standard error *) ``` +`Rt.trace` says on standard error the calls under way, from the one +that calls it out, each with its variables, as a program that stops +does. + Only what can be reached from `main` and the module bodies goes into the executable: unused procedures, variables and strings are left out. The executable names its procedures and carries line numbers, so that @@ -1144,7 +1149,8 @@ program, on every target. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a `nil` pointer, a `nil` procedure, a stack overflow, a -bad address or a division fault. Whether the machine catches these or +bad address or a division fault; then the calls under way, each with +its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation's choice (appendix A); that they stop the program isn't. @@ -1180,7 +1186,7 @@ | Module | What | |---|---| -| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail | +| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail | | `Sys` | system calls: `open`, `openRead`, `create`, `read`, `write`, `seek`, `close`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `symlink`, `readLink`, `info`, `openDir`, `nextEntry`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `termSize`, `ready`; the environment, `env`, `workDir`, `setWorkDir`; programs, pipes and signals; time, `clock`, `now`, `sleep`; `random`; and sockets. One file per target, and `Sys.raz` over the C library for the others | | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | @@ -1735,7 +1741,9 @@ can't be changed; `+` joins string constants (section 4). `match` takes an integer, with constants and ranges as labels (7.4). - **2026-10.12.** A code procedure is written in the target's assembly - language, not as bytes (section 8; appendix A). + language, not as bytes (section 8; appendix A). A program that stops + says the calls under way and their variables, and `Rt.trace` says + them on demand (sections 10 and 11). ## Appendix A. What an implementation chooses diff --git a/lib/Rt.raz b/lib/Rt.raz index 0b98b86..1df5e38 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -3,6 +3,9 @@ failures; and the handlers that say where the program was when a signal stops it. The compiler finds fail, exit, cExit and places by these names. + After a failed check or a signal, and on demand with trace, Rt says + the calls under way and their variables, from the frames' chain of + rbp and the compiler's tables in places. The handlers are Linux's on x86-64, set by system calls: what the kernel hands them is laid out as it is there. *) module Rt; @@ -14,13 +17,22 @@ SigFpe = 8; SigSegv = 11; Low = 10000H; (* no address below this is ever mapped *) + Digits = "0123456789abcdef"; (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *) Flags = 4H + 8000000H + 4000000H + 80000000H; type (* The code from address at on comes from line and col of the - file whose path is at path in the paths *) + file whose path is at path in the names *) Place = record at, line, col, path: U32; end; + (* A procedure, from address start to stop, called name, and its + count variables from first on *) + Proc = record start, stop, name, first, count: U32; end; + (* A variable of a procedure, at off from its frame, shown by kind: + 1 a signed integer, 2 an unsigned one, 3 a BOOL, 4 an address, + 5 a real; plus its size times 16, and 256 if the frame holds its + address *) + Local = record name: U32; off: I32; kind: U32; end; (* What rt_sigaction and sigaltstack take *) Action = record handler: proc(sig: INT; info, ctx: ^U8); @@ -34,9 +46,10 @@ atExits: [MaxAtExit]proc; numAtExit: INT; cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *) - (* Set first thing too: the number of places, where the program's - code starts and stops, the places, and the paths. The linker - makes them. *) + (* Set first thing too: the numbers of places, where the program's + code starts and stops, the numbers of procedures and variables; + the places, procedures and variables, and the paths and names. + The linker makes them. *) places: ^INT; line: [256]U8; (* the message being made *) lineLen: INT; @@ -123,6 +136,12 @@ lineLen := 0; end; +(* The paths and names, after the places, procedures and variables *) +proc names: ^U8; +begin + return ^U8(places + 5) + 16 * places[0] + 20 * places[3] + 12 * places[4]; +end; + (* Puts where the code at address a comes from, as file:line:col: and a space, if a is in the program's code: the place with the highest address up to a. *) @@ -131,13 +150,13 @@ begin if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; n := places[0]; - p := ^Place(places + 3); + p := ^Place(places + 5); best := nil; for i := 0 to n - 1 do if (UINT(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end; end; if best = nil then return; end; - putStr(^U8(p + n) + INT(best^.path)); + putStr(names() + INT(best^.path)); put(":"); putInt(INT(best^.line)); put(":"); @@ -145,6 +164,137 @@ put(": "); end; +(* The frame pointer of the procedure that calls it, which a code + procedure leaves as it is *) +proc framePtr: UINT; +code + mov rax, rbp; + ret; +end framePtr; + +(* The frame of the procedure that called the one calling this *) +proc callerFrame: UINT; +var q: ^UINT; +begin + q := ^UINT(framePtr()); (* callerFrame's own: its caller's frame is in it *) + q := ^UINT(q^); + return q^; +end; + +(* The procedure whose code holds address a, or nil *) +proc procAt(a: UINT): ^Proc; +var p: ^Proc; i: INT; +begin + if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end; + p := ^Proc(^U8(places + 5) + 16 * places[0]); + for i := 0 to places[3] - 1 do + if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end; + end; + return nil; +end; + +proc putHex(u: UINT); +var buf: [16]U8; i: INT; +begin + i := 16; + repeat + DEC(i); + buf[i] := Digits[INT(u & 15)]; + u := u shr 4; + until u = 0; + put("0x"); + put(buf[i..16]); +end; + +(* x with six figures after the point, or as m e n beyond them *) +proc putReal(x: F64); +var e, i: INT; f: U64; +begin + if x # x then put("nan"); return; end; + if x < 0.0 then put("-"); x := -x; end; + if (x > 1.0) and (x * 2.0 = x) then put("inf"); return; end; + e := 0; + while x >= 1.0E15 do x := x / 10.0; INC(e); end; + while (x # 0.0) and (x < 0.001) do x := x * 10.0; DEC(e); end; + f := U64(x * 1000000.0 + 0.5); + putInt(INT(f div 1000000)); + put("."); + for i := 1 to 6 do + f := f mod 1000000 * 10; + put(Digits[INT(f div 1000000)..INT(f div 1000000) + 1]); + end; + if e # 0 then put("e"); putInt(e); end; +end; + +(* The value of variable l of the frame at bp *) +proc putValue(bp: UINT; l: ^Local); +var a: ^U8; w: ^UINT; h: ^U16; s: ^U32; f: ^F32; d: ^F64; v: UINT; bits: INT; +begin + a := ^U8(bp) + INT(l^.off); + w := ^UINT(a); + if l^.kind & 256 # 0 then a := ^U8(w^); w := ^UINT(a); end; + h := ^U16(a); s := ^U32(a); f := ^F32(a); d := ^F64(a); + bits := INT(l^.kind shr 4 & 15) * 8; + match bits of + | 8: v := UINT(a^); + | 16: v := UINT(h^); + | 32: v := UINT(s^); + | else: v := w^; + end; + match l^.kind & 15 of + | 1: + if (bits < 64) and (v shr (bits - 1) # 0) then putInt(INT(v) - INT(1) shl bits); + else putInt(INT(v)); + end; + | 2: putInt(INT(v)); + | 3: if v # 0 then put("true"); else put("false"); end; + | 4: if v = 0 then put("nil"); else putHex(v); end; + | else: if bits = 32 then putReal(F64(f^)); else putReal(d^); end; + end; +end; + +(* Says the calls under way, a line each, from the one at address ip + whose frame is at bp: where each is, and its variables, unless vars + is false for the first. At most 20; a frame that isn't the + program's ends them. *) +proc calls(ip, bp: UINT; vars: BOOL); +var p: ^Proc; l: ^Local; i, n: INT; q: ^UINT; +begin + for n := 1 to 20 do + p := procAt(ip); + if p = nil then return; end; + put(" in "); + putStr(names() + INT(p^.name)); + put(" at "); + where(ip); + l := ^Local(^U8(places + 5) + 16 * places[0] + 20 * places[3]) + INT(p^.first); + if not vars or (p^.count = 0) then DEC(lineLen, 2); end; (* where's ": " *) + for i := 0 to INT(p^.count) - 1 do + if vars then + if i > 0 then put(", "); end; + putStr(names() + INT(l[i].name)); + put(" = "); + putValue(bp, l + i); + end; + end; + say; + vars := true; + q := ^UINT(bp); + if (bp & 7 # 0) or (q[0] <= bp) then return; end; + ip := q[1] - 1; (* the call, by its return address *) + bp := q[0]; + end; +end; + +(* Says on standard error the calls under way, from the one that calls + it, each with its variables, as after a failed check. *) +proc trace*; +var q: ^UINT; +begin + q := ^UINT(framePtr()); + calls(q[1] - 1, q[0], true); +end; + (* A check failed in the code at address at: says where, and what failed, on standard error, then ends the program by exit with status 2. what is 0 for an index a and 1 for a slice a..b, of @@ -166,6 +316,7 @@ else put("a stack overflow"); end; say; + calls(at, callerFrame(), true); act := {}; (* the signal's default action *) SYSCALL(13, sig, ADR(act), 0, 8); (* rt_sigaction *) SYSCALL(62, SYSCALL(39), sig); (* kill(getpid(), sig) *) @@ -189,6 +340,7 @@ putInt(len); end; say; + calls(at, callerFrame(), true); exit(2); end; @@ -205,8 +357,10 @@ signal ends the program. info and ctx are the kernel's siginfo and ucontext. *) proc onSignal(sig: INT; info, ctx: ^U8); -var adr, sp, ip: UINT; q: ^UINT; why: []U8; +var adr, sp, ip, bp: UINT; q: ^UINT; why: []U8; begin + q := ^UINT(ctx + 120); (* uc_mcontext's rbp *) + bp := q^; q := ^UINT(info + 16); (* si_addr *) adr := q^; q := ^UINT(ctx + 160); (* uc_mcontext's rsp, then rip *) @@ -225,6 +379,8 @@ put("stopped by "); put(why); say; + (* the variables of a frame the stack overflowed in may be past it *) + calls(ip, bp, why # "a stack overflow"); end; begin diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index f9817fe..0078dc3 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -53,6 +53,7 @@ jnz parent; pop rax; (* fn *) pop rdi; (* arg *) + xor ebp, ebp; (* the first frame: Rt's report of the calls stops here *) call rax; xor edi, edi; mov eax, 60; (* exit, this thread only *) diff --git a/src/chunk.pas b/src/chunk.pas index c0d0f46..dd30556 100644 --- a/src/chunk.pas +++ b/src/chunk.pas @@ -44,6 +44,14 @@ path, line, col: longint; { the file's path, interned, the line and column } end; + { A variable of a procedure, for the trap report: Rt shows it, by + kind, from its place in the frame } + TVarNote = record + chunk, name: longint; + off: longint; { from the frame pointer } + kind: longint; { how Rt shows it: see link.pas } + end; + TFixup = record at: longint; { the 32-bit field, as an offset in text } chunk: longint; { the chunk it refers to } @@ -70,6 +78,8 @@ numLines: longint; curFile: longint; { the file being compiled, for NoteLine } curChunk: longint; { the chunk of code being written } + varNotes: ^TVarNote; + numVarNotes: longint; function NewChunk(sec: TSection; module, name: longint): longint; procedure BeginChunk(c: longint); @@ -79,6 +89,8 @@ function ExternChunk(name: longint): longint; procedure NoteLine(line, col: longint); procedure AddFixup(at, chunk, addend: longint); +procedure NoteVar(chunk, name, off, kind: longint); +function ChunkName(c: longint): shortstring; procedure Put1(b: longint); procedure Put4(v: longint); @@ -223,6 +235,26 @@ fixups^[numFixups].chunk := chunk; fixups^[numFixups].addend := addend; inc(numFixups); +end; + +procedure NoteVar(chunk, name, off, kind: longint); +begin + if numVarNotes and 255 = 0 then varNotes := ReAllocMem(varNotes, (numVarNotes + 256) * SizeOf(TVarNote)); + varNotes[numVarNotes].chunk := chunk; + varNotes[numVarNotes].name := name; + varNotes[numVarNotes].off := off; + varNotes[numVarNotes].kind := kind; + inc(numVarNotes); +end; + +{ The name of chunk c: its module's and its own, or literal and its + number for a literal } +function ChunkName(c: longint): shortstring; +begin + with chunks^[c] do + if name < 0 then ChunkName := 'literal' + IntStr(c) + else if module < 0 then ChunkName := NameOf(name) + else ChunkName := NameOf(module) + '.' + NameOf(name); end; initialization diff --git a/src/link.pas b/src/link.pas index 5721a48..cac7600 100644 --- a/src/link.pas +++ b/src/link.pas @@ -37,17 +37,9 @@ procedure MarkLive(entry: longint); procedure LinkAll(headerSize, roExtra, dynSize: int64); -function ChunkName(c: longint): shortstring; implementation -function ChunkName(c: longint): shortstring; -begin - if chunks^[c].name < 0 then exit('a literal'); - if chunks^[c].module < 0 then exit(NameOf(chunks^[c].name)); - ChunkName := NameOf(chunks^[c].module) + '.' + NameOf(chunks^[c].name); -end; - procedure MarkLive(entry: longint); var stack: PLongs; sp, c, f, t: longint; begin @@ -89,16 +81,40 @@ end; { The places that Rt reads to say where a failed check or a signal - was: their number and where text starts and stops (I64s); for each - place in live code its address, line, column and the offset of its - file's path (U32s); then the paths, each ending in a zero byte. They - are in no order, since Rt looks at them only as the program stops. } + was, and what calls were under way: their numbers, the places', where + text starts and stops, the procedures' and their variables' (I64s); + for each place in live code its address, line, column and the offset + of its file's path (U32s); for each live procedure where it starts and + stops, the offset of its name, its first variable and how many it has + (U32s); for each variable the offset of its name, its place in the + frame and its kind, as parse.pas's VarKind (U32s); then the paths and + names, each ending in a zero byte. They are in no order, since Rt + looks at them only as the program stops. } procedure MakePlaces(start, stop: int64); -var files, offs: PLongs; buf: PBytes; i, f, n, nfiles, k: longint; s: shortstring; +var + files, offs, first, count: PLongs; buf: PBytes; + i, f, n, nfiles, k, np, nv, c, v, at, names: longint; + + procedure Name(const s: shortstring); + begin + PLongint(@buf^[at])^ := names - k; + Move(s[1], buf^[names], length(s)); + buf^[names + length(s)] := 0; + names := names + length(s) + 1; + at := at + 4; + end; + begin files := GetMem(numLines * 4 + 4); offs := GetMem(numLines * 4 + 4); - n := 0; nfiles := 0; k := 0; + first := GetMem(numChunks * 4); + count := GetMem(numChunks * 4); + FillChar(count^, numChunks * 4, 0); + for v := numVarNotes - 1 downto 0 do begin + first^[varNotes[v].chunk] := v; + inc(count^[varNotes[v].chunk]); + end; + n := 0; nfiles := 0; k := 0; np := 0; nv := 0; for i := 0 to numLines - 1 do if chunks^[lines^[i].chunk].live then begin inc(n); @@ -111,28 +127,60 @@ inc(nfiles); end; end; - buf := GetMem(24 + 16 * n + k); + for c := 0 to numChunks - 1 do + if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin + inc(np); + k := k + length(ChunkName(c)) + 1; + for v := first^[c] to first^[c] + count^[c] - 1 do k := k + length(NameOf(varNotes[v].name)) + 1; + nv := nv + count^[c]; + end; + buf := GetMem(40 + 16 * n + 20 * np + 12 * nv + k); PInt64(@buf^[0])^ := n; PInt64(@buf^[8])^ := start; PInt64(@buf^[16])^ := stop; - k := 24; + PInt64(@buf^[24])^ := np; + PInt64(@buf^[32])^ := nv; + at := 40; for i := 0 to numLines - 1 do if chunks^[lines^[i].chunk].live then begin f := 0; while files^[f] <> lines^[i].path do inc(f); - PLongint(@buf^[k])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start; - PLongint(@buf^[k + 4])^ := lines^[i].line; - PLongint(@buf^[k + 8])^ := lines^[i].col; - PLongint(@buf^[k + 12])^ := offs^[f]; - k := k + 16; + PLongint(@buf^[at])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start; + PLongint(@buf^[at + 4])^ := lines^[i].line; + PLongint(@buf^[at + 8])^ := lines^[i].col; + PLongint(@buf^[at + 12])^ := offs^[f]; + at := at + 16; end; + { the pool of paths and names starts at k } + k := 40 + 16 * n + 20 * np + 12 * nv; + names := k; for f := 0 to nfiles - 1 do begin - s := ShownPath(NameOf(files^[f])) + #0; - Move(s[1], buf^[k], length(s)); - k := k + length(s); + i := at; + Name(ShownPath(NameOf(files^[f]))); + at := i; end; - SetData(placesChunk, buf, k, 8); - FreeMem(buf); FreeMem(files); FreeMem(offs); + v := 0; + for c := 0 to numChunks - 1 do + if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin + PLongint(@buf^[at])^ := chunks^[c].adr; + PLongint(@buf^[at + 4])^ := chunks^[c].adr + chunks^[c].size; + at := at + 8; + Name(ChunkName(c)); + PLongint(@buf^[at])^ := v; + PLongint(@buf^[at + 4])^ := count^[c]; + at := at + 8; + v := v + count^[c]; + end; + for c := 0 to numChunks - 1 do + if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then + for v := first^[c] to first^[c] + count^[c] - 1 do begin + Name(NameOf(varNotes[v].name)); + PLongint(@buf^[at])^ := varNotes[v].off; + PLongint(@buf^[at + 4])^ := varNotes[v].kind; + at := at + 8; + end; + SetData(placesChunk, buf, names, 8); + FreeMem(buf); FreeMem(files); FreeMem(offs); FreeMem(first); FreeMem(count); end; { Places the live chunks, leaving roExtra bytes at the end of rodata diff --git a/src/parse.pas b/src/parse.pas index db36858..f6a7162 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1631,8 +1631,26 @@ procedure Declarations(top: boolean); forward; +{ How Rt shows a variable of o's type in the trap report: 1 a signed + integer, 2 an unsigned one, 3 a BOOL, 4 an address, 5 a real; plus + its size times 16, and 256 if the frame holds its address; 0 if it + isn't shown. } +function VarKind(o: PObj): longint; +var k: longint; +begin + case o^.typ^.form of + fInt: k := 2 - ord(o^.typ^.signed); + fEnum, fSet: k := 2; + fBool: k := 3; + fPtr, fProc, fError: k := 4; + fReal: k := 5; + else exit(0); + end; + VarKind := k + o^.typ^.size * 16 + ord(o^.ind) * 256; +end; + procedure Body(p: PObj; sig: PType; line: longint); -var q, params, last, o: PObj; old: PType; +var q, params, last, o, before: PObj; old: PType; begin OpenScope(NewScope); params := nil; last := nil; @@ -1656,7 +1674,19 @@ o := q; end; curProc := p; + before := curModule^.lastLocal; Declarations(false); + { its parameters and variables, for the trap report } + if before = nil then o := curModule^.locals else o := before^.link; + q := params; + while q <> nil do begin + if VarKind(q) <> 0 then NoteVar(p^.chunk, q^.name, q^.val, VarKind(q)); + q := q^.next; + end; + while o <> nil do begin + if (o^.cls = oVar) and (VarKind(o) <> 0) then NoteVar(p^.chunk, o^.name, o^.val, VarKind(o)); + o := o^.link; + end; Expect(sBegin); old := curResult; curResult := sig^.base; diff --git a/src/x64.pas b/src/x64.pas index 80ad9dc..7194b4e 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -277,14 +277,6 @@ XN := 'xmm' + IntStr(x); end; -function ChunkName(c: longint): shortstring; -begin - with chunks^[c] do - if name < 0 then ChunkName := 'data' + IntStr(c) - else if module < 0 then ChunkName := NameOf(name) - else ChunkName := NameOf(module) + '.' + NameOf(name); -end; - { A memory operand, with the size of what it holds, as the assembler reads it } function MN(const m: TMem; size: longint = 0): shortstring; diff --git a/test/check.sh b/test/check.sh index 00467d7..28bc3c1 100755 --- a/test/check.sh +++ b/test/check.sh @@ -8,7 +8,8 @@ # X.args its arguments, one per line (optional) # X.status its exit status (optional; 0 if missing), 128 and the # signal's number for one stopped by a signal -# X.stderr its standard error (optional; not compared if missing) +# X.stderr its standard error (optional; not compared if missing), +# each address in it, 0x and hex digits, written 0x? # A program without X.expect is compiled, not run. test/libc puts # lib/Sys.raz, over the C library, before the system call one. # Programs in test/err must not compile, and must print X.err; there @@ -54,7 +55,8 @@ elif [ $status != $want ]; then echo "FAIL $dir/$name: status $status, not $want" fail=$((fail + 1)) - elif [ -f $dir/$name.stderr ] && ! cmp -s $out/$name.err $dir/$name.stderr; then + elif [ -f $dir/$name.stderr ] && ! sed 's/0x[0-9a-f]*/0x?/g' $out/$name.err | + cmp -s - $dir/$name.stderr; then echo "FAIL $dir/$name: standard error differs" diff $out/$name.err $dir/$name.stderr | head -5 fail=$((fail + 1)) diff --git a/test/libc/NilC.stderr b/test/libc/NilC.stderr index 9e71024..523e08a 100644 --- a/test/libc/NilC.stderr +++ b/test/libc/NilC.stderr @@ -1 +1,2 @@ test/libc/NilC.raz:11:5: stopped by a nil pointer + in NilC.main at test/libc/NilC.raz:11:5: p = nil diff --git a/test/libc/Through.raz b/test/libc/Through.raz index 7a61432..20aa9e0 100644 --- a/test/libc/Through.raz +++ b/test/libc/Through.raz @@ -25,6 +25,7 @@ Mem.free(Heap.heap, ^U8(p)); end; Out.int(sum); Out.ln; + p := nil; (* its address would change from run to run *) i := 3; a[i] := 1; Out.str("not reached\n"); diff --git a/test/libc/Through.stderr b/test/libc/Through.stderr index bf8b9a0..f6eeb0a 100644 --- a/test/libc/Through.stderr +++ b/test/libc/Through.stderr @@ -1 +1,2 @@ -test/libc/Through.raz:29:7: index 3 is out of range for a length of 3 +test/libc/Through.raz:30:7: index 3 is out of range for a length of 3 + in Through.main at test/libc/Through.raz:30:7: p = nil, list = nil, i = 3, sum = 500500 diff --git a/test/msg/Bounds.stderr b/test/msg/Bounds.stderr index 1fe45e0..8322110 100644 --- a/test/msg/Bounds.stderr +++ b/test/msg/Bounds.stderr @@ -1 +1,2 @@ conform/run/Bounds.raz:8:11: index 4 is out of range for a length of 4 + in Bounds.main at conform/run/Bounds.raz:8:11: i = 4 diff --git a/test/msg/Kinds.stderr b/test/msg/Kinds.stderr index 8403af4..9091215 100644 --- a/test/msg/Kinds.stderr +++ b/test/msg/Kinds.stderr @@ -1 +1,2 @@ conform/run/Kinds.raz:92:16: 5 isn't a position of Color + in Kinds.main at conform/run/Kinds.raz:92:16: s = 5, t = 12, c = 1, r = -1, one = 10 diff --git a/test/msg/SliceBounds.stderr b/test/msg/SliceBounds.stderr index 364ba6b..cc06a4e 100644 --- a/test/msg/SliceBounds.stderr +++ b/test/msg/SliceBounds.stderr @@ -1 +1,2 @@ conform/run/SliceBounds.raz:10:23: slice 9..6 is out of range for a length of 8 + in SliceBounds.main at conform/run/SliceBounds.raz:10:23: lo = 9 diff --git a/test/msg/SliceIndex.stderr b/test/msg/SliceIndex.stderr index ed51c74..9a1cf2b 100644 --- a/test/msg/SliceIndex.stderr +++ b/test/msg/SliceIndex.stderr @@ -1 +1,2 @@ conform/run/SliceIndex.raz:11:11: index 3 is out of range for a length of 3 + in SliceIndex.main at conform/run/SliceIndex.raz:11:11: i = 3 diff --git a/test/msg/SlicePtrBackwards.stderr b/test/msg/SlicePtrBackwards.stderr index 4c8d8da..3427aa6 100644 --- a/test/msg/SlicePtrBackwards.stderr +++ b/test/msg/SlicePtrBackwards.stderr @@ -1 +1,2 @@ conform/run/SlicePtrBackwards.raz:11:23: slice 2..0 runs backwards + in SlicePtrBackwards.main at conform/run/SlicePtrBackwards.raz:11:23: p = 0x?, hi = 0 diff --git a/test/run/CheckOff.stderr b/test/run/CheckOff.stderr index 9de665b..ed23828 100644 --- a/test/run/CheckOff.stderr +++ b/test/run/CheckOff.stderr @@ -1 +1,2 @@ test/run/CheckOff.raz:15:17: index 2 is out of range for a length of 2 + in CheckOff.main at test/run/CheckOff.raz:15:17: i = 2 diff --git a/test/run/DivLowest.stderr b/test/run/DivLowest.stderr index 81148eb..7b7da86 100644 --- a/test/run/DivLowest.stderr +++ b/test/run/DivLowest.stderr @@ -1 +1,2 @@ test/run/DivLowest.raz:13:5: stopped by a division fault + in DivLowest.main at test/run/DivLowest.raz:13:5: a = -9223372036854775808, b = -1 diff --git a/test/run/DivLowestI16.stderr b/test/run/DivLowestI16.stderr index 3044000..54bdcb8 100644 --- a/test/run/DivLowestI16.stderr +++ b/test/run/DivLowestI16.stderr @@ -1 +1,2 @@ test/run/DivLowestI16.raz:12:5: stopped by a division fault + in DivLowestI16.main at test/run/DivLowestI16.raz:12:5: a = -32768, b = -1 diff --git a/test/run/DivLowestI32.stderr b/test/run/DivLowestI32.stderr index 11ed03d..9212cfc 100644 --- a/test/run/DivLowestI32.stderr +++ b/test/run/DivLowestI32.stderr @@ -1 +1,2 @@ test/run/DivLowestI32.raz:11:5: stopped by a division fault + in DivLowestI32.main at test/run/DivLowestI32.raz:11:5: a = -2147483648 diff --git a/test/run/DivLowestI8.stderr b/test/run/DivLowestI8.stderr index 17e094b..a0d185e 100644 --- a/test/run/DivLowestI8.stderr +++ b/test/run/DivLowestI8.stderr @@ -1 +1,2 @@ test/run/DivLowestI8.raz:13:5: stopped by a division fault + in DivLowestI8.main at test/run/DivLowestI8.raz:13:5: a = -128, b = -1 diff --git a/test/run/DivZero.stderr b/test/run/DivZero.stderr index 53e6339..a9aa5a0 100644 --- a/test/run/DivZero.stderr +++ b/test/run/DivZero.stderr @@ -1 +1,2 @@ test/run/DivZero.raz:9:5: stopped by a division fault + in DivZero.main at test/run/DivZero.raz:9:5: a = 7, b = 0 diff --git a/test/run/NilBig.stderr b/test/run/NilBig.stderr index 7231701..8a69414 100644 --- a/test/run/NilBig.stderr +++ b/test/run/NilBig.stderr @@ -1 +1,2 @@ test/run/NilBig.raz:14:5: stopped by a nil pointer + in NilBig.main at test/run/NilBig.raz:14:5 diff --git a/test/run/NilPointer.stderr b/test/run/NilPointer.stderr index 80b76e2..627f005 100644 --- a/test/run/NilPointer.stderr +++ b/test/run/NilPointer.stderr @@ -1 +1,2 @@ test/run/NilPointer.raz:13:5: stopped by a nil pointer + in NilPointer.main at test/run/NilPointer.raz:13:5 diff --git a/test/run/NilProc.stderr b/test/run/NilProc.stderr index 42a190b..a4ac8fb 100644 --- a/test/run/NilProc.stderr +++ b/test/run/NilProc.stderr @@ -1 +1,2 @@ test/run/NilProc.raz:8:5: stopped by a nil procedure + in NilProc.main at test/run/NilProc.raz:8:5 diff --git a/test/run/StackOverflow.stderr b/test/run/StackOverflow.stderr index 070b0b7..0910336 100644 --- a/test/run/StackOverflow.stderr +++ b/test/run/StackOverflow.stderr @@ -1 +1,18 @@ test/run/StackOverflow.raz:7:1: stopped by a stack overflow + in StackOverflow.down at test/run/StackOverflow.raz:7:1 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 14 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 13 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 12 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 11 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 10 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 9 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 8 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 7 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 6 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 5 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 4 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 3 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 2 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 1 + in StackOverflow.down at test/run/StackOverflow.raz:11:5: n = 0 + in StackOverflow.main at test/run/StackOverflow.raz:16:5 diff --git a/test/run/ThreadOverflow.stderr b/test/run/ThreadOverflow.stderr index b4f3903..bf49229 100644 --- a/test/run/ThreadOverflow.stderr +++ b/test/run/ThreadOverflow.stderr @@ -1 +1,21 @@ test/run/ThreadOverflow.raz:6:1: stopped by a stack overflow + in ThreadOverflow.down at test/run/ThreadOverflow.raz:6:1 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1925 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1924 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1923 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1922 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1921 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1920 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1919 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1918 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1917 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1916 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1915 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1914 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1913 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1912 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1911 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1910 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1909 + in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1908 diff --git a/test/run/Trace.expect b/test/run/Trace.expect new file mode 100644 index 0000000..4023f20 --- /dev/null +++ b/test/run/Trace.expect @@ -0,0 +1 @@ +go diff --git a/test/run/Trace.raz b/test/run/Trace.raz new file mode 100644 index 0000000..64f3ec2 --- /dev/null +++ b/test/run/Trace.raz @@ -0,0 +1,31 @@ +module Trace; +(* The calls under way, with their variables: on demand with Rt.trace, + and after a failed check *) +import Out, Rt; + +type Color = (red, green, blue); + +proc inner(var n: I64; f: F64; ok: BOOL; c: Color; b: U8; s: I16); +var a: [2]I64; k: INT; +begin + Rt.trace; + if ok and (c = blue) and (b > 0) and (s < 0) and (f < 0.0) then k := INT(n); end; + a[k] := 1; +end; + +proc outer(n: I64); +var x: I64; r: F32; +begin + r := 0.25; + x := n + 1 + I64(r); + inner(x, -1.5, true, blue, 200, -7); +end; + +proc main*; +begin + Out.str("go\n"); + Out.flush; + outer(4); +end; + +end Trace. diff --git a/test/run/Trace.status b/test/run/Trace.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/Trace.status @@ -0,0 +1 @@ +2 diff --git a/test/run/Trace.stderr b/test/run/Trace.stderr new file mode 100644 index 0000000..631688f --- /dev/null +++ b/test/run/Trace.stderr @@ -0,0 +1,7 @@ + in Trace.inner at test/run/Trace.raz:11:5: n = 5, f = -1.500000, ok = true, c = 2, b = 200, s = -7, k = 0 + in Trace.outer at test/run/Trace.raz:21:5: n = 4, x = 5, r = 0.250000 + in Trace.main at test/run/Trace.raz:28:5 +test/run/Trace.raz:13:7: index 5 is out of range for a length of 2 + in Trace.inner at test/run/Trace.raz:13:7: n = 5, f = -1.500000, ok = true, c = 2, b = 200, s = -7, k = 5 + in Trace.outer at test/run/Trace.raz:21:5: n = 4, x = 5, r = 0.250000 + in Trace.main at test/run/Trace.raz:28:5