From 389c4f3a19177c46e9913ce9b90395de8b49f690 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 10:53:23 +0100 Subject: [PATCH] Run-time messages: failed checks say what and with what values, signals say where and why A check passes Rt.fail what failed (an index, a slice, an enum pos), the values and its own address. The linker writes the places (addr, line, col, file of each line note in live code), which the entry point hands Rt; the failing code has the place of the offending expr. Rt sets handlers for SIGSEGV and SIGFPE, on a stack of their own: a nil pointer, a nil proc, a stack overflow, a bad address, a division fault; then the signal ends the program. Run tests take an optional X.stderr; tests of each failure and signal, one over C. The lowest I64 div -1 traps, as report 6.1 says: the plan is fixed. --- diff --git a/PLAN.md b/PLAN.md index 2af938b..e0f3039 100644 --- a/PLAN.md +++ b/PLAN.md @@ -265,7 +265,7 @@ makes them errors, and `make check` builds `lib/`, `examples/` and `tools/` with it. The machinery, *main*; clearing the warnings it finds in `lib/`, `examples/` and `tools/`, *agent*. -- [ ] 26. Run-time messages. A failed check says which and with what +- [x] 26. Run-time messages. A failed check says which and with what values: `index 7 is out of range for a length of 5`, the slice, the enumeration position, at file, line and column. A table from code addresses to lines goes into the executable, and `Rt` sets @@ -273,7 +273,8 @@ and why (a `nil` pointer, a `nil` procedure, stack overflow, a bad address, a division fault) before it dies by that signal. Run tests gain an optional `X.stderr`. And the lowest `I64` - divided by -1 wraps, as the report says, instead of trapping. + divided by -1 stops the program with SIGFPE, as the report says + (6.1), and is told as a division fault. The compiler and `Rt`, *main*; the tests, *agent*. - [ ] 27. `doc/compiler.md` (its table and routine names), `README.md` diff --git a/doc/compiler.md b/doc/compiler.md index 2f93287..0239bcd 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -8,15 +8,15 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/diag.pas` | 267 | 191 | Messages: positions, source excerpts, colour, counts | -| `src/scan.pas` | 687 | 553 | Symbols, literals, comments, positions | +| `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts | +| `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions | | `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout | -| `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder | -| `src/gen.pas` | 1,515 | 1,319 | Items: loading, storing, copying, frames, calls | -| `src/parse.pas` | 2,241 | 2,028 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/link.pas` | 131 | 95 | Tree shaking, layout, patching | +| `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder | +| `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls | +| `src/parse.pas` | 2,240 | 2,027 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 221 | 174 | The command line, the entry points | +| `src/raz.pas` | 227 | 178 | 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. @@ -65,7 +65,8 @@ becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. 5. Back in `raz.pas`, `Use` marks the modules the program uses; the entry point, `_start`, is written as one more chunk: save `argc` and - `argv`, call every used module's body, call `main`, call `Rt.exit`. + `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. @@ -220,8 +221,9 @@ `test/enctest.pas` emits one of each for `objdump` to check. Two more things live here because they are tied to code positions: -`NoteLine` records which source line the code from `pc` on belongs to, -for gdb; `ExternChunk` makes, once per C symbol, the quadword the +`NoteLine` records which line and column of the source the code from +`pc` on belongs to, for gdb and for the places; `Here` is the address +of the code being written, as an operand. `ExternChunk` makes, once per C symbol, the quadword the dynamic loader will fill with that symbol's address. ## gen.pas: the code generator @@ -269,8 +271,12 @@ Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a variable into an item for part of it, mostly by adding to `off`. -`Index` and `Slice` emit the bounds checks; `Check` jumps over a call -to `Rt.fail` with the file and line when the check holds. +`Index` and `Slice` emit the bounds checks, and `Convert` the check of +an enumeration's position; `Check` jumps over a call to `Rt.fail` when +the check holds. The call passes what failed, the values involved and +its own address, by which `Rt` finds the place; that code has the +place of the offending expression (`LineCol` in `scan.pas`), and the +code after it the statement's again. Assignment and conversion: `Coerce` makes a value fit the type it's going to, or reports that it can't; it's where a string literal becomes @@ -371,6 +377,16 @@ patching every fixup with the distance from the end of its field to its target. +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 +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 +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 +program. + The layout is chosen so the file can be mapped as it lies: text just after the headers, rodata on the next page, then a writable page that begins with the dynamic loader's table (in a program that uses C) and @@ -423,6 +439,6 @@ in the same order), a case in `Statement`, and a routine beside the others. - **A check that fails:** `Check` in `gen.pas`, and the message in - `lib/Rt.raz`'s `fail`. + `lib/Rt.raz`'s `fail`; a reason a signal gives, `onSignal` there. After any change: `make check`, `make bench`, `make loc`. diff --git a/lib/Rt.raz b/lib/Rt.raz index e68abd5..ea523e5 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -1,18 +1,48 @@ (* The run time: ending the program, and what the compiler's code calls: fail when a check fails, eq when = or # compares two strings; - and stop, for the library's own failures. - The compiler finds fail, eq, exit and cExit by these names. *) + stop, for the library's own failures; and the handlers that say + where the program was when a signal stops it. + The compiler finds fail, eq, exit, cExit and places by these names. + 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; import Sys; const Full*: ERROR = "too many procedures to run at exit"; MaxAtExit = 32; + SigFpe = 8; + SigSegv = 11; + Low = 10000H; (* no address below this is ever mapped *) + (* 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 *) + Place = record at, line, col, path: U32; end; + (* What rt_sigaction and sigaltstack take *) + Action = record + handler: proc(sig: I64; info, ctx: ^U8); + flags: U64; + restorer: proc; + mask: U64; + end; + Stack = record sp: ^U8; flags, size: I64; end; + var atExits: [MaxAtExit]proc; numAtExit: I64; 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. *) + places: ^I64; + line: [256]U8; (* the message being made *) + lineLen: I64; + act: Action; + alt: Stack; + altStack: [16384]U8; (* where the handlers run, even when the stack is full *) (* Registers p to run when the program ends by exit. *) proc atExit*(p: proc): ERROR; @@ -49,27 +79,134 @@ exit(2); end; -(* A check failed at line of file: says so on standard error and ends - the program with status 2. *) -proc fail*(file: ^U8; line: I64); -var buf: [20]U8; i, n: I64; +(* Messages, made in line, one byte kept for the line feed, and + written by say in one piece *) +proc put(s: []U8); +var i: I64; +begin + for i := 0 to LEN(s) - 1 do + if lineLen < LEN(line) - 1 then + line[lineLen] := s[i]; + INC(lineLen); + end; + end; +end; + +(* The digits are made from the last, those of a negative n from its + negative remainders, so that the lowest I64 comes out too. *) +proc putInt(n: I64); +var buf: [20]U8; i: I64; neg: BOOL; begin - n := 0; - while file[n] # 0 do n := n + 1; end; - Sys.write(Sys.stderr, file[0..n]); + neg := n < 0; i := 20; repeat - i := i - 1; - buf[i] := U8(line mod 10) + '0'; - line := line div 10; - until line = 0; - i := i - 1; - buf[i] := ':'; - Sys.write(Sys.stderr, buf[i..20]); - Sys.write(Sys.stderr, ": index out of range\n"); + DEC(i); + if neg then buf[i] := U8(-(n mod 10)) + '0'; else buf[i] := U8(n mod 10) + '0'; end; + n := n div 10; + until n = 0; + if neg then put("-"); end; + put(buf[i..20]); +end; + +proc putStr(s: ^U8); +var n: I64; +begin + n := 0; + while s[n] # 0 do INC(n); end; + put(s[0..n]); +end; + +proc say; +begin + line[lineLen] := '\n'; + Sys.write(Sys.stderr, line[0..lineLen + 1]); + lineLen := 0; +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. *) +proc where(a: U64); +var p, best: ^Place; i, n: I64; +begin + if (places = nil) or (a < U64(places[1])) or (a >= U64(places[2])) then return; end; + n := places[0]; + p := ^Place(places + 3); + best := nil; + for i := 0 to n - 1 do + if (U64(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) + I64(best^.path)); + put(":"); + putInt(I64(best^.line)); + put(":"); + putInt(I64(best^.col)); + put(": "); +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 + something whose length is len; 2 for a, which is no position of + the enumeration called name. *) +proc fail*(what, a, b, len: I64; at: U64; name: ^U8); +begin + where(at); + if what = 0 then + put("index "); + putInt(a); + elsif what = 1 then + put("slice "); + putInt(a); + put(".."); + putInt(b); + else + putInt(a); + put(" isn't a position of "); + putStr(name); + end; + if what < 2 then + put(" is out of range for a length of "); + putInt(len); + end; + say; exit(2); end; +(* Returns from a signal handler: rt_sigreturn *) +proc sigReturn; +code 0B8H, 0FH, 0, 0, 0, (* mov eax, 15 *) + 0FH, 05H; (* syscall *) + +(* A signal stops the program: says where it was and why on standard + error, and returns to the instruction that faulted. The handler was + reset as it was called, so the instruction faults again and the + signal ends the program. info and ctx are the kernel's siginfo and + ucontext. *) +proc onSignal(sig: I64; info, ctx: ^U8); +var adr, sp, ip: U64; q: ^U64; why: []U8; +begin + q := ^U64(info + 16); (* si_addr *) + adr := q^; + q := ^U64(ctx + 160); (* uc_mcontext's rsp, then rip *) + sp := q[0]; + ip := q[1]; + if sig = SigFpe then why := "a division fault"; + elsif ip < Low then + why := "a nil procedure"; + q := ^U64(sp); + ip := q^ - 1; (* the call, by its return address *) + elsif adr < Low then why := "a nil pointer"; + elsif adr - sp + 100000H < 200000H then why := "a stack overflow"; (* within a megabyte of rsp *) + else why := "a bad address"; + end; + where(ip); + put("stopped by "); + put(why); + say; +end; + proc eq*(a, b: []U8): BOOL; var i: I64; begin @@ -80,4 +217,10 @@ return true; end; +begin + alt := {sp: ADR(altStack[0]), size: LEN(altStack)}; + SYSCALL(131, ADR(alt), 0); (* sigaltstack *) + act := {handler: onSignal, flags: Flags, restorer: sigReturn}; + SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *) + SYSCALL(13, SigFpe, ADR(act), 0, 8); end Rt. diff --git a/src/diag.pas b/src/diag.pas index 871a56a..858fc01 100644 --- a/src/diag.pas +++ b/src/diag.pas @@ -37,6 +37,7 @@ quiet: boolean; function AddFile(const path: shortstring; buf: PChar; len: longint): longint; +function ShownPath(const path: shortstring): shortstring; function PathOf(f: longint): shortstring; function LineOf(const p: TPos): longint; function Join(const p, q: TPos): TPos; @@ -63,25 +64,31 @@ var files: ^TFile; { numFiles of them, in room for cap } numFiles, cap: longint; - colour, colourKnown: boolean; + colour, colourKnown, cwdKnown: boolean; cwd: shortstring; { the working directory and a /, or '' if unknown } lastError: TPos; { where the last error was } shown: boolean; { whether it was shown: its notes and help are only then } { Paths are shown from the working directory where they are under it, so messages read the same wherever the compiler or its library is. } -function AddFile(const path: shortstring; buf: PChar; len: longint): longint; +function ShownPath(const path: shortstring): shortstring; var d: array[0..255] of char; begin - if numFiles = 0 then + if not cwdKnown then begin if FpGetcwd(@d[0], SizeOf(d)) <> nil then cwd := StrPas(@d[0]) + '/'; + cwdKnown := true; + end; + if (cwd <> '') and (copy(path, 1, length(cwd)) = cwd) then ShownPath := copy(path, length(cwd) + 1, 255) + else ShownPath := path; +end; + +function AddFile(const path: shortstring; buf: PChar; len: longint): longint; +begin if numFiles = cap then begin cap := 2 * cap + 8; ReallocMem(files, cap * SizeOf(TFile)); end; - if (cwd <> '') and (copy(path, 1, length(cwd)) = cwd) then - files[numFiles].path := copy(path, length(cwd) + 1, 255) - else files[numFiles].path := path; + files[numFiles].path := ShownPath(path); files[numFiles].buf := buf; files[numFiles].len := len; AddFile := numFiles; diff --git a/src/gen.pas b/src/gen.pas index 23db77d..ebe2aea 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -62,7 +62,6 @@ var rtFail, rtEq, argcChunk, argvChunk: longint; - fileChunk: longint; { the current file's name, for failed checks; -1 until needed } depth: longint; { values pushed and not yet popped } procedure MakeConst(var x: TItem; t: PType; v: int64); @@ -537,26 +536,52 @@ { Checks } +const + NoBase = -2; { a TMem that is no memory } + { The program goes on if the condition cc holds, and otherwise stops - in Rt.fail with the file and line. } -procedure Check(cc: longint); -var L: longint; s: shortstring; + in Rt.fail(what, a, b, len, at, name), which says what failed at p, + found by at, the address of the call: an index a (what = 0), a slice + a..b (1), each for a length len, or a position a of the enumeration + called name (2). a is in register ra, b in RDX; len is in memory at + m, or n if m.base is NoBase. The failing code has p for its place, + and the code after it the place before it again. } +procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint); +var L, line, col: longint; before: TLine; begin - if fileChunk < 0 then begin - s := CurPath + #0; - fileChunk := NewChunk(secRodata, -1, -1); - SetData(fileChunk, @s[1], length(s), 1); - end; L := NoLabel; Jcc(cc, L); + before := lines^[numLines - 1]; + LineCol(p.a, line, col); + NoteLine(line, col); + MovRR(RSI, ra); + if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); + Lea(R8, Here); + MovRI(RDI, what); + if name >= 0 then Lea(R9, MemRip(name, 0)); AluI(aluAnd, RSP, -16); - Lea(RDI, MemRip(fileChunk, 0)); - MovRI(RSI, CurLine); CallChunk(rtFail); Ud2; FixLink(L); + NoteLine(before.line, before.col); end; +{ For Check: a length known here, not in memory } +function NoMem: TMem; +begin + NoMem := Mem(NoBase, 0); +end; + +{ t's name, ending in a zero byte, for Rt.fail } +function NameChunk(t: PType): longint; +var s: shortstring; c: longint; +begin + s := TypeName(t) + #0; + c := NewChunk(secRodata, -1, -1); + SetData(c, @s[1], length(s), 1); + NameChunk := c; +end; + { Selectors } procedure Deref(var x: TItem); @@ -610,7 +635,7 @@ end else if t^.form = fArray then begin Value(y); CmpI(RAX, t^.len); - Check(ccB); + Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1); if s <> 1 then ImulI(RAX, s); if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX)); Alu(aluAdd, RAX, RCX); @@ -621,7 +646,7 @@ m := MemOf(x, RCX); ml := m; ml.disp := ml.disp + 8; AluM(aluCmp, RAX, ml); - Check(ccB); + Check(ccB, 0, y.pos, RAX, ml, 0, -1); if s <> 1 then ImulI(RAX, s); AluM(aluAdd, RAX, m); x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; @@ -649,14 +674,12 @@ if t^.form = fPtr then PopR(R8) else begin m := MemOf(x, R8); - if t^.form = fArray then CmpI(RDX, t^.len) - else begin - ml := m; ml.disp := ml.disp + 8; - AluM(aluCmp, RDX, ml); - end; - Check(ccBE); + ml := m; ml.disp := ml.disp + 8; + if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end + else AluM(aluCmp, RDX, ml); + Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); Alu(aluCmp, RCX, RDX); - Check(ccBE); + Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); end; tmp := AllocTemp(16, 8); MovRR(RAX, RDX); @@ -1040,7 +1063,7 @@ end else if IsInt(s) and (t^.form = fEnum) then begin if x.mode = mConst then begin if (x.val < 0) or (x.val >= t^.len) then MarkAt(x.pos, IntStr(x.val) + ' isn''t a position of ' + TypeName(t)) - end else begin Value(x); CmpI(RAX, t^.len); Check(ccB) end; + end else begin Value(x); CmpI(RAX, t^.len); Check(ccB, 2, x.pos, RAX, NoMem, t^.len, NameChunk(t)) end; end else if IsInt(s) and (t^.form = fSet) then begin if x.mode = mConst then begin if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) @@ -1361,7 +1384,7 @@ var i, top: longint; n: int64; begin BeginChunk(c); - NoteLine(line); + NoteLine(line, 1); { a declaration starts a line } Push(RBP); MovRR(RBP, RSP); Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } diff --git a/src/link.pas b/src/link.pas index c32541b..a410762 100644 --- a/src/link.pas +++ b/src/link.pas @@ -33,6 +33,7 @@ extraAdr: int64; { that room } bssAdr, bssSize: int64; { the writable segment, the loader's table first } textImg, roImg: PBytes; + placesChunk: longint = -1; { the rodata chunk of the places, made here } procedure MarkLive(entry: longint); procedure LinkAll(headerSize, roExtra, dynSize: int64); @@ -87,6 +88,53 @@ Place := a; 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. } +procedure MakePlaces(start, stop: int64); +var files, offs: PLongs; buf: PBytes; i, f, n, nfiles, k: longint; s: shortstring; +begin + files := GetMem(numLines * 4 + 4); + offs := GetMem(numLines * 4 + 4); + n := 0; nfiles := 0; k := 0; + for i := 0 to numLines - 1 do + if chunks^[lines^[i].chunk].live then begin + inc(n); + f := 0; + while (f < nfiles) and (files^[f] <> lines^[i].path) do inc(f); + if f = nfiles then begin + files^[f] := lines^[i].path; + offs^[f] := k; + k := k + length(ShownPath(NameOf(files^[f]))) + 1; + inc(nfiles); + end; + end; + buf := GetMem(24 + 16 * n + k); + PInt64(@buf^[0])^ := n; + PInt64(@buf^[8])^ := start; + PInt64(@buf^[16])^ := stop; + k := 24; + 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; + end; + 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); + end; + SetData(placesChunk, buf, k, 8); + FreeMem(buf); FreeMem(files); FreeMem(offs); +end; + { Places the live chunks, leaving roExtra bytes at the end of rodata and dynSize at the start of the writable segment. } procedure LinkAll(headerSize, roExtra, dynSize: int64); @@ -95,6 +143,7 @@ textOff := (headerSize + 15) div 16 * 16; e := Place(secText, Base + textOff); textSize := e - (Base + textOff); + if placesChunk >= 0 then MakePlaces(Base + textOff, e); roOff := (textOff + textSize + Page - 1) div Page * Page; e := Place(secRodata, Base + roOff); extraAdr := (e + 7) div 8 * 8; diff --git a/src/parse.pas b/src/parse.pas index e70a4b8..a52f184 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1406,7 +1406,7 @@ begin start := TokPos; first := id; - NoteLine(CurLine); + NoteLine(CurLine, CurCol); m := TempMark; case tok of sIdent: begin @@ -1556,7 +1556,7 @@ curResult := sig^.base; EnterBody(p^.chunk, line); Statements; - NoteLine(CurLine); + NoteLine(CurLine, CurCol); Expect(sEnd); EndProc(p^.chunk); curResult := old; @@ -2000,12 +2000,12 @@ function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; var st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint; - oldMod: PModule; oldFile: longint; oldCheck: boolean; m, im: PModule; + oldMod: PModule; oldCheck: boolean; m, im: PModule; imp: PImport; lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; begin SaveScan(st); SaveScopes(d, saved); - oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; oldPath := curFile; + oldMod := curModule; oldCheck := checkMode; oldPath := curFile; curFile := Name(path); if not Open(path) then Fatal('can''t read ' + path); New(m); @@ -2018,7 +2018,6 @@ m^.body := -1; m^.checkOnly := checkOnly; curModule := m; - fileChunk := -1; checkMode := checkOnly; if not checkOnly then begin if numPending > High(pending) then Mark('imports nested more than ' + IntStr(High(pending) + 1) + ' deep'); @@ -2090,7 +2089,7 @@ curResult := nil; EnterBody(body, line); Statements; - NoteLine(CurLine); + NoteLine(CurLine, CurCol); Expect(sEnd); EndProc(body); end; @@ -2112,7 +2111,7 @@ if lastModule = nil then firstModule := m else lastModule^.next := m; lastModule := m; end; - curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; curFile := oldPath; + curModule := oldMod; checkMode := oldCheck; curFile := oldPath; RestoreScopes(d, saved); RestoreScan(st); CompileFile := m; diff --git a/src/raz.pas b/src/raz.pas index 01ffd2e..3fdee9d 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -195,6 +195,12 @@ Lea(RAX, Mem(RSP, 8)); Store(MemRip(argvChunk, 0), RAX, 8); end; + { the places, which the linker makes, for Rt to say where a failed + check or a signal was } + placesChunk := NewChunk(secRodata, -1, -1); + SetData(placesChunk, nil, 0, 8); + Lea(RAX, MemRip(placesChunk, 0)); + Store(MemRip(RtObj(rt, 'places')^.chunk, 0), RAX, 8); m := firstModule; while m <> nil do begin if m^.used and (m^.body >= 0) then CallChunk(m^.body); diff --git a/src/scan.pas b/src/scan.pas index 1b2425e..2cf8192 100644 --- a/src/scan.pas +++ b/src/scan.pas @@ -49,7 +49,6 @@ rval: double; id: longint; f: longint; { the file, as diag knows it } - path: shortstring; end; const @@ -89,8 +88,9 @@ function AfterPrev: TPos; function MissingPos: TPos; function SpanFrom(const p: TPos): TPos; -function CurPath: shortstring; function CurLine: longint; +function CurCol: longint; +procedure LineCol(a: longint; out line, col: longint); implementation @@ -223,16 +223,35 @@ SpanFrom.b := cur.prevEnd; end; -function CurPath: shortstring; +function CurLine: longint; begin - CurPath := cur.path; + CurLine := cur.tokLine; end; -function CurLine: longint; +{ The line and column of byte a of the file, at or before the current + symbol: counted back from the symbol's line, so that it costs only + the distance. A column counts characters, as diag's do. } +procedure LineCol(a: longint; out line, col: longint); +var i, start: longint; begin - CurLine := cur.tokLine; + line := cur.tokLine; + start := cur.tokLineStart; + while a < start do begin + dec(line); + dec(start); + while (start > 0) and (cur.buf[start - 1] <> #10) do dec(start); + end; + col := 1; + for i := start to a - 1 do + if (byte(cur.buf[i]) and $C0) <> $80 then inc(col); end; +function CurCol: longint; +var line: longint; +begin + LineCol(cur.tokPos, line, CurCol); +end; + procedure Mark(const msg: shortstring); begin MarkAt(TokPos, msg); @@ -342,7 +361,6 @@ cur.tokPos := 0; cur.tokLine := 1; cur.tokLineStart := 0; - cur.path := path; cur.f := AddFile(path, cur.buf, n); Open := true; end; diff --git a/src/x64.pas b/src/x64.pas index 874db7e..cd2c75f 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -61,10 +61,11 @@ ext: boolean; { bss: the address of a C symbol, its name } end; - { A source line whose code starts at a place in text, for gdb } + { A place in the source whose code starts at a place in text: for + gdb, and for Rt to say where a failed check or a signal was } TLine = record at, chunk: longint; { the place in text, and its chunk } - path, line: longint; { the file's path, interned, and the line } + path, line, col: longint; { the file's path, interned, the line and column } end; TFixup = record @@ -100,7 +101,8 @@ procedure SetData(c: longint; p: PBytes; n, align: longint); procedure SetBss(c: longint; size, align: longint); function ExternChunk(name: longint): longint; -procedure NoteLine(line: longint); +procedure NoteLine(line, col: longint); +function Here: TMem; procedure Put1(b: longint); procedure Put4(v: longint); @@ -259,13 +261,13 @@ chunks^[c].align := align; end; -{ The code from here on is for line of the current file. A line with - no code of its own gives way to the next. } -procedure NoteLine(line: longint); +{ The code from here on is for line and column of the current file. A + place with no code of its own gives way to the next. } +procedure NoteLine(line, col: longint); begin if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines); - if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) - and (lines^[numLines - 1].line = line) and (lines^[numLines - 1].path = curFile) then exit; + if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) and (lines^[numLines - 1].line = line) + and (lines^[numLines - 1].col = col) and (lines^[numLines - 1].path = curFile) then exit; if numLines = lineCap then begin lineCap := lineCap * 2; lines := ReAllocMem(lines, lineCap * SizeOf(TLine)); @@ -274,7 +276,14 @@ lines^[numLines].chunk := curChunk; lines^[numLines].path := curFile; lines^[numLines].line := line; + lines^[numLines].col := col; inc(numLines); +end; + +{ The address of the code about to be written, as an operand } +function Here: TMem; +begin + Here := MemRip(curChunk, pc - chunks^[curChunk].start); end; { The quadword that holds the address of C's symbol name, filled in diff --git a/test/check.sh b/test/check.sh index ac0901d..f548352 100755 --- a/test/check.sh +++ b/test/check.sh @@ -4,7 +4,9 @@ # X.raz the program # X.expect its standard output # X.args its arguments, one per line (optional) -# X.status its exit status (optional; 0 if missing) +# 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) # 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 @@ -16,6 +18,7 @@ raz=./raz out=build/check mkdir -p $out +ulimit -c 0 # the tests stopped by a signal leave no core pass=0 fail=0 @@ -32,8 +35,8 @@ if [ -f $dir/$name.args ]; then while IFS= read -r a; do set -- "$@" "$a"; done < $dir/$name.args fi - $out/$name "$@" > $out/$name.out 2>/dev/null - status=$? + # in $(...), so that what the shell says of a signal goes nowhere + status=$( { $out/$name "$@" > $out/$name.out 2>$out/$name.err; echo $?; } 2>/dev/null ) want=0 [ -f $dir/$name.status ] && want=$(cat $dir/$name.status) if ! cmp -s $out/$name.out $dir/$name.expect; then @@ -42,6 +45,10 @@ fail=$((fail + 1)) 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 + echo "FAIL $dir/$name: standard error differs" + diff $out/$name.err $dir/$name.stderr | head -5 fail=$((fail + 1)) else pass=$((pass + 1)) diff --git a/test/libc/NilC.expect b/test/libc/NilC.expect new file mode 100644 index 0000000..90be1f3 --- /dev/null +++ b/test/libc/NilC.expect @@ -0,0 +1 @@ +before diff --git a/test/libc/NilC.raz b/test/libc/NilC.raz new file mode 100644 index 0000000..5b3485d --- /dev/null +++ b/test/libc/NilC.raz @@ -0,0 +1,14 @@ +(* A program over C stopped by a signal says where, as a static one + does: C's own start-up runs before Rt's handlers are set *) +module NilC; +import Out; + +proc main*; +var p: ^I64; +begin + Out.str("before\n"); + Out.flush; + p^ := 1; +end; + +end NilC. diff --git a/test/libc/NilC.status b/test/libc/NilC.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/libc/NilC.status @@ -0,0 +1 @@ +139 diff --git a/test/libc/NilC.stderr b/test/libc/NilC.stderr new file mode 100644 index 0000000..9e71024 --- /dev/null +++ b/test/libc/NilC.stderr @@ -0,0 +1 @@ +test/libc/NilC.raz:11:5: stopped by a nil pointer diff --git a/test/libc/Through.stderr b/test/libc/Through.stderr new file mode 100644 index 0000000..bf8b9a0 --- /dev/null +++ b/test/libc/Through.stderr @@ -0,0 +1 @@ +test/libc/Through.raz:29:7: index 3 is out of range for a length of 3 diff --git a/test/run/Bounds.stderr b/test/run/Bounds.stderr new file mode 100644 index 0000000..41cdbe9 --- /dev/null +++ b/test/run/Bounds.stderr @@ -0,0 +1 @@ +test/run/Bounds.raz:8:11: index 4 is out of range for a length of 4 diff --git a/test/run/DivLowest.expect b/test/run/DivLowest.expect new file mode 100644 index 0000000..fb58abd --- /dev/null +++ b/test/run/DivLowest.expect @@ -0,0 +1 @@ +4611686018427387904 \ No newline at end of file diff --git a/test/run/DivLowest.raz b/test/run/DivLowest.raz new file mode 100644 index 0000000..0820737 --- /dev/null +++ b/test/run/DivLowest.raz @@ -0,0 +1,16 @@ +module DivLowest; +(* The lowest I64 divided by -1 stops the program with SIGFPE, as + dividing by zero does *) +import Out; + +proc main*; +var a, b: I64; +begin + a := -9223372036854775807 - 1; + b := ARGC() - 2; + Out.int(a div (-2)); + Out.flush; + Out.int(a div b); +end; + +end DivLowest. diff --git a/test/run/DivLowest.status b/test/run/DivLowest.status new file mode 100644 index 0000000..7296f25 --- /dev/null +++ b/test/run/DivLowest.status @@ -0,0 +1 @@ +136 diff --git a/test/run/DivLowest.stderr b/test/run/DivLowest.stderr new file mode 100644 index 0000000..81148eb --- /dev/null +++ b/test/run/DivLowest.stderr @@ -0,0 +1 @@ +test/run/DivLowest.raz:13:5: stopped by a division fault diff --git a/test/run/DivZero.expect b/test/run/DivZero.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/DivZero.raz b/test/run/DivZero.raz new file mode 100644 index 0000000..358d875 --- /dev/null +++ b/test/run/DivZero.raz @@ -0,0 +1,12 @@ +module DivZero; +(* Dividing by zero stops the program with SIGFPE, after saying where *) + +proc main*: I64; +var a, b: I64; +begin + a := 7; + b := ARGC() - 1; + return a mod b; +end; + +end DivZero. diff --git a/test/run/DivZero.status b/test/run/DivZero.status new file mode 100644 index 0000000..7296f25 --- /dev/null +++ b/test/run/DivZero.status @@ -0,0 +1 @@ +136 diff --git a/test/run/DivZero.stderr b/test/run/DivZero.stderr new file mode 100644 index 0000000..53e6339 --- /dev/null +++ b/test/run/DivZero.stderr @@ -0,0 +1 @@ +test/run/DivZero.raz:9:5: stopped by a division fault diff --git a/test/run/Kinds.stderr b/test/run/Kinds.stderr new file mode 100644 index 0000000..893ba2f --- /dev/null +++ b/test/run/Kinds.stderr @@ -0,0 +1 @@ +test/run/Kinds.raz:92:16: 5 isn't a position of Color diff --git a/test/run/NilPointer.expect b/test/run/NilPointer.expect new file mode 100644 index 0000000..56a6051 --- /dev/null +++ b/test/run/NilPointer.expect @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/test/run/NilPointer.raz b/test/run/NilPointer.raz new file mode 100644 index 0000000..7586744 --- /dev/null +++ b/test/run/NilPointer.raz @@ -0,0 +1,16 @@ +module NilPointer; +(* Following nil stops the program with SIGSEGV, after saying where *) +import Out; + +type Cell = record next: ^Cell; v: I64; end; + +proc main*; +var c: Cell; +begin + c.v := 1; + Out.int(c.v); + Out.flush; + Out.int(c.next^.v); +end; + +end NilPointer. diff --git a/test/run/NilPointer.status b/test/run/NilPointer.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/NilPointer.status @@ -0,0 +1 @@ +139 diff --git a/test/run/NilPointer.stderr b/test/run/NilPointer.stderr new file mode 100644 index 0000000..80b76e2 --- /dev/null +++ b/test/run/NilPointer.stderr @@ -0,0 +1 @@ +test/run/NilPointer.raz:13:5: stopped by a nil pointer diff --git a/test/run/NilProc.expect b/test/run/NilProc.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/NilProc.raz b/test/run/NilProc.raz new file mode 100644 index 0000000..738534d --- /dev/null +++ b/test/run/NilProc.raz @@ -0,0 +1,12 @@ +module NilProc; +(* Calling a nil procedure: the place is the call's *) + +var f: proc(n: I64): I64; + +proc main*: I64; +begin + return f(1) + + 1; +end; + +end NilProc. diff --git a/test/run/NilProc.status b/test/run/NilProc.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/NilProc.status @@ -0,0 +1 @@ +139 diff --git a/test/run/NilProc.stderr b/test/run/NilProc.stderr new file mode 100644 index 0000000..42a190b --- /dev/null +++ b/test/run/NilProc.stderr @@ -0,0 +1 @@ +test/run/NilProc.raz:8:5: stopped by a nil procedure diff --git a/test/run/SliceBounds.expect b/test/run/SliceBounds.expect new file mode 100644 index 0000000..6e6c716 --- /dev/null +++ b/test/run/SliceBounds.expect @@ -0,0 +1 @@ +6 3 0 \ No newline at end of file diff --git a/test/run/SliceBounds.raz b/test/run/SliceBounds.raz new file mode 100644 index 0000000..026ad9b --- /dev/null +++ b/test/run/SliceBounds.raz @@ -0,0 +1,15 @@ +module SliceBounds; +(* A slice must have 0 <= lo <= hi <= the length *) +import Out; + +proc main*; +var a: [8]I64; s: []I64; lo: I64; +begin + s := a[0..8]; + for lo := 0 to 9 by 3 do + Out.int(LEN(s[lo..6])); + Out.str(" "); + end; +end; + +end SliceBounds. diff --git a/test/run/SliceBounds.status b/test/run/SliceBounds.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/SliceBounds.status @@ -0,0 +1 @@ +2 diff --git a/test/run/SliceBounds.stderr b/test/run/SliceBounds.stderr new file mode 100644 index 0000000..12b90bb --- /dev/null +++ b/test/run/SliceBounds.stderr @@ -0,0 +1 @@ +test/run/SliceBounds.raz:10:23: slice 9..6 is out of range for a length of 8 diff --git a/test/run/SliceIndex.expect b/test/run/SliceIndex.expect new file mode 100644 index 0000000..6da806d --- /dev/null +++ b/test/run/SliceIndex.expect @@ -0,0 +1 @@ +0123 \ No newline at end of file diff --git a/test/run/SliceIndex.raz b/test/run/SliceIndex.raz new file mode 100644 index 0000000..5813060 --- /dev/null +++ b/test/run/SliceIndex.raz @@ -0,0 +1,15 @@ +module SliceIndex; +(* An index checked against a slice's length, which is in memory *) +import Out; + +proc main*; +var a: [8]I64; s: []I64; i: I64; +begin + s := a[2..5]; + for i := 0 to 3 do + Out.int(i); + s[i] := i; + end; +end; + +end SliceIndex. diff --git a/test/run/SliceIndex.status b/test/run/SliceIndex.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/SliceIndex.status @@ -0,0 +1 @@ +2 diff --git a/test/run/SliceIndex.stderr b/test/run/SliceIndex.stderr new file mode 100644 index 0000000..536baf2 --- /dev/null +++ b/test/run/SliceIndex.stderr @@ -0,0 +1 @@ +test/run/SliceIndex.raz:11:11: index 3 is out of range for a length of 3 diff --git a/test/run/StackOverflow.expect b/test/run/StackOverflow.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/StackOverflow.raz b/test/run/StackOverflow.raz new file mode 100644 index 0000000..6eb5c15 --- /dev/null +++ b/test/run/StackOverflow.raz @@ -0,0 +1,16 @@ +module StackOverflow; +(* A recursion with no end runs out of stack: SIGSEGV, said as such *) + +proc down(n: I64): I64; +var pad: [64]I64; +begin + pad[n mod 64] := n; + return down(n + 1) + pad[0]; +end; + +proc main*: I64; +begin + return down(0); +end; + +end StackOverflow. diff --git a/test/run/StackOverflow.status b/test/run/StackOverflow.status new file mode 100644 index 0000000..897bdc8 --- /dev/null +++ b/test/run/StackOverflow.status @@ -0,0 +1 @@ +139 diff --git a/test/run/StackOverflow.stderr b/test/run/StackOverflow.stderr new file mode 100644 index 0000000..49ebfa2 --- /dev/null +++ b/test/run/StackOverflow.stderr @@ -0,0 +1 @@ +test/run/StackOverflow.raz:4:1: stopped by a stack overflow