raz / commit 389c4f3a

commit
389c4f3a19177c46e9913ce9b90395de8b49f690
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
5a422e6e
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.

patch · log

46 files changed, +503 -85

PLAN.md (history) modified +3 -2

@@ -265,7 +265,7 @@
265265 makes them errors, and `make check` builds `lib/`, `examples/`
266266 and `tools/` with it. The machinery, *main*; clearing the
267267 warnings it finds in `lib/`, `examples/` and `tools/`, *agent*.
268-- [ ] 26. Run-time messages. A failed check says which and with what
268+- [x] 26. Run-time messages. A failed check says which and with what
269269 values: `index 7 is out of range for a length of 5`, the slice,
270270 the enumeration position, at file, line and column. A table from
271271 code addresses to lines goes into the executable, and `Rt` sets
@@ -273,7 +273,8 @@
273273 and why (a `nil` pointer, a `nil` procedure, stack overflow, a
274274 bad address, a division fault) before it dies by that signal.
275275 Run tests gain an optional `X.stderr`. And the lowest `I64`
276- divided by -1 wraps, as the report says, instead of trapping.
276+ divided by -1 stops the program with SIGFPE, as the report says
277+ (6.1), and is told as a division fault.
277278 The compiler and `Rt`,
278279 *main*; the tests, *agent*.
279280 - [ ] 27. `doc/compiler.md` (its table and routine names), `README.md`

doc/compiler.md (history) modified +29 -13

@@ -8,15 +8,15 @@
88
99 | File | Lines | Code | What |
1010 |---|---|---|---|
11-| `src/diag.pas` | 267 | 191 | Messages: positions, source excerpts, colour, counts |
12-| `src/scan.pas` | 687 | 553 | Symbols, literals, comments, positions |
11+| `src/diag.pas` | 274 | 197 | Messages: positions, source excerpts, colour, counts |
12+| `src/scan.pas` | 705 | 567 | Symbols, literals, comments, positions |
1313 | `src/sym.pas` | 583 | 483 | Types, objects, scopes, layout |
14-| `src/x64.pas` | 682 | 548 | Code buffer, chunks, fixups, line notes, instruction encoder |
15-| `src/gen.pas` | 1,515 | 1,319 | Items: loading, storing, copying, frames, calls |
16-| `src/parse.pas` | 2,241 | 2,028 | Declarations, statements, expressions, built-ins, recovery, warnings |
17-| `src/link.pas` | 131 | 95 | Tree shaking, layout, patching |
14+| `src/x64.pas` | 691 | 554 | Code buffer, chunks, fixups, places, instruction encoder |
15+| `src/gen.pas` | 1,538 | 1,332 | Items: loading, storing, copying, frames, calls |
16+| `src/parse.pas` | 2,240 | 2,027 | Declarations, statements, expressions, built-ins, recovery, warnings |
17+| `src/link.pas` | 180 | 138 | Tree shaking, layout, patching, the places |
1818 | `src/elf.pas` | 425 | 351 | Static and dynamic ELF, symbol table, line numbers |
19-| `src/raz.pas` | 221 | 174 | The command line, the entry points |
19+| `src/raz.pas` | 227 | 178 | 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.
@@ -65,7 +65,8 @@
6565 becomes a string chunk, a call, and a fixup to `Out.str`'s chunk.
6666 5. Back in `raz.pas`, `Use` marks the modules the program uses; the
6767 entry point, `_start`, is written as one more chunk: save `argc` and
68- `argv`, call every used module's body, call `main`, call `Rt.exit`.
68+ `argv` and the address of the places (below), call every used
69+ module's body, call `main`, call `Rt.exit`.
6970 6. `WriteElf` (`elf.pas`) has `link.pas` mark what `_start` reaches,
7071 place it and patch it, then writes headers, code, data, symbols and
7172 line numbers.
@@ -220,8 +221,9 @@
220221 `test/enctest.pas` emits one of each for `objdump` to check.
221222
222223 Two more things live here because they are tied to code positions:
223-`NoteLine` records which source line the code from `pc` on belongs to,
224-for gdb; `ExternChunk` makes, once per C symbol, the quadword the
224+`NoteLine` records which line and column of the source the code from
225+`pc` on belongs to, for gdb and for the places; `Here` is the address
226+of the code being written, as an operand. `ExternChunk` makes, once per C symbol, the quadword the
225227 dynamic loader will fill with that symbol's address.
226228
227229 ## gen.pas: the code generator
@@ -269,8 +271,12 @@
269271
270272 Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a
271273 variable into an item for part of it, mostly by adding to `off`.
272-`Index` and `Slice` emit the bounds checks; `Check` jumps over a call
273-to `Rt.fail` with the file and line when the check holds.
274+`Index` and `Slice` emit the bounds checks, and `Convert` the check of
275+an enumeration's position; `Check` jumps over a call to `Rt.fail` when
276+the check holds. The call passes what failed, the values involved and
277+its own address, by which `Rt` finds the place; that code has the
278+place of the offending expression (`LineCol` in `scan.pas`), and the
279+code after it the statement's again.
274280
275281 Assignment and conversion: `Coerce` makes a value fit the type it's
276282 going to, or reports that it can't; it's where a string literal becomes
@@ -371,6 +377,16 @@
371377 patching every fixup with the distance from the end of its field to its
372378 target.
373379
380+Between placing text and placing rodata, `MakePlaces` writes the
381+places, a rodata chunk: for each line note in live code, its address,
382+line, column and file. `Rt` reads them, through `Rt.places`, which the
383+entry point sets, to say where a failed check or a signal was: the
384+place with the highest address up to the one it has. Its signal
385+handlers, set in its body, run on a stack of their own, so that they
386+can tell a stack overflow, and leave by returning to the faulting
387+instruction with the handler reset, so that the signal ends the
388+program.
389+
374390 The layout is chosen so the file can be mapped as it lies: text just
375391 after the headers, rodata on the next page, then a writable page that
376392 begins with the dynamic loader's table (in a program that uses C) and
@@ -423,6 +439,6 @@
423439 in the same order), a case in `Statement`, and a routine beside the
424440 others.
425441 - **A check that fails:** `Check` in `gen.pas`, and the message in
426- `lib/Rt.raz`'s `fail`.
442+ `lib/Rt.raz`'s `fail`; a reason a signal gives, `onSignal` there.
427443
428444 After any change: `make check`, `make bench`, `make loc`.

lib/Rt.raz (history) modified +160 -17

@@ -1,18 +1,48 @@
11 (* The run time: ending the program, and what the compiler's code
22 calls: fail when a check fails, eq when = or # compares two strings;
3- and stop, for the library's own failures.
4- The compiler finds fail, eq, exit and cExit by these names. *)
3+ stop, for the library's own failures; and the handlers that say
4+ where the program was when a signal stops it.
5+ The compiler finds fail, eq, exit, cExit and places by these names.
6+ The handlers are Linux's on x86-64, set by system calls: what the
7+ kernel hands them is laid out as it is there. *)
58 module Rt;
69 import Sys;
710
811 const
912 Full*: ERROR = "too many procedures to run at exit";
1013 MaxAtExit = 32;
14+ SigFpe = 8;
15+ SigSegv = 11;
16+ Low = 10000H; (* no address below this is ever mapped *)
17+ (* SA_SIGINFO, SA_ONSTACK, SA_RESTORER and SA_RESETHAND *)
18+ Flags = 4H + 8000000H + 4000000H + 80000000H;
1119
20+type
21+ (* The code from address at on comes from line and col of the
22+ file whose path is at path in the paths *)
23+ Place = record at, line, col, path: U32; end;
24+ (* What rt_sigaction and sigaltstack take *)
25+ Action = record
26+ handler: proc(sig: I64; info, ctx: ^U8);
27+ flags: U64;
28+ restorer: proc;
29+ mask: U64;
30+ end;
31+ Stack = record sp: ^U8; flags, size: I64; end;
32+
1233 var
1334 atExits: [MaxAtExit]proc;
1435 numAtExit: I64;
1536 cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *)
37+ (* Set first thing too: the number of places, where the program's
38+ code starts and stops, the places, and the paths. The linker
39+ makes them. *)
40+ places: ^I64;
41+ line: [256]U8; (* the message being made *)
42+ lineLen: I64;
43+ act: Action;
44+ alt: Stack;
45+ altStack: [16384]U8; (* where the handlers run, even when the stack is full *)
1646
1747 (* Registers p to run when the program ends by exit. *)
1848 proc atExit*(p: proc): ERROR;
@@ -49,27 +79,134 @@
4979 exit(2);
5080 end;
5181
52-(* A check failed at line of file: says so on standard error and ends
53- the program with status 2. *)
54-proc fail*(file: ^U8; line: I64);
55-var buf: [20]U8; i, n: I64;
82+(* Messages, made in line, one byte kept for the line feed, and
83+ written by say in one piece *)
84+proc put(s: []U8);
85+var i: I64;
86+begin
87+ for i := 0 to LEN(s) - 1 do
88+ if lineLen < LEN(line) - 1 then
89+ line[lineLen] := s[i];
90+ INC(lineLen);
91+ end;
92+ end;
93+end;
94+
95+(* The digits are made from the last, those of a negative n from its
96+ negative remainders, so that the lowest I64 comes out too. *)
97+proc putInt(n: I64);
98+var buf: [20]U8; i: I64; neg: BOOL;
5699 begin
57- n := 0;
58- while file[n] # 0 do n := n + 1; end;
59- Sys.write(Sys.stderr, file[0..n]);
100+ neg := n < 0;
60101 i := 20;
61102 repeat
62- i := i - 1;
63- buf[i] := U8(line mod 10) + '0';
64- line := line div 10;
65- until line = 0;
66- i := i - 1;
67- buf[i] := ':';
68- Sys.write(Sys.stderr, buf[i..20]);
69- Sys.write(Sys.stderr, ": index out of range\n");
103+ DEC(i);
104+ if neg then buf[i] := U8(-(n mod 10)) + '0'; else buf[i] := U8(n mod 10) + '0'; end;
105+ n := n div 10;
106+ until n = 0;
107+ if neg then put("-"); end;
108+ put(buf[i..20]);
109+end;
110+
111+proc putStr(s: ^U8);
112+var n: I64;
113+begin
114+ n := 0;
115+ while s[n] # 0 do INC(n); end;
116+ put(s[0..n]);
117+end;
118+
119+proc say;
120+begin
121+ line[lineLen] := '\n';
122+ Sys.write(Sys.stderr, line[0..lineLen + 1]);
123+ lineLen := 0;
124+end;
125+
126+(* Puts where the code at address a comes from, as file:line:col: and
127+ a space, if a is in the program's code: the place with the highest
128+ address up to a. *)
129+proc where(a: U64);
130+var p, best: ^Place; i, n: I64;
131+begin
132+ if (places = nil) or (a < U64(places[1])) or (a >= U64(places[2])) then return; end;
133+ n := places[0];
134+ p := ^Place(places + 3);
135+ best := nil;
136+ for i := 0 to n - 1 do
137+ if (U64(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end;
138+ end;
139+ if best = nil then return; end;
140+ putStr(^U8(p + n) + I64(best^.path));
141+ put(":");
142+ putInt(I64(best^.line));
143+ put(":");
144+ putInt(I64(best^.col));
145+ put(": ");
146+end;
147+
148+(* A check failed in the code at address at: says where, and what
149+ failed, on standard error, then ends the program by exit with
150+ status 2. what is 0 for an index a and 1 for a slice a..b, of
151+ something whose length is len; 2 for a, which is no position of
152+ the enumeration called name. *)
153+proc fail*(what, a, b, len: I64; at: U64; name: ^U8);
154+begin
155+ where(at);
156+ if what = 0 then
157+ put("index ");
158+ putInt(a);
159+ elsif what = 1 then
160+ put("slice ");
161+ putInt(a);
162+ put("..");
163+ putInt(b);
164+ else
165+ putInt(a);
166+ put(" isn't a position of ");
167+ putStr(name);
168+ end;
169+ if what < 2 then
170+ put(" is out of range for a length of ");
171+ putInt(len);
172+ end;
173+ say;
70174 exit(2);
71175 end;
72176
177+(* Returns from a signal handler: rt_sigreturn *)
178+proc sigReturn;
179+code 0B8H, 0FH, 0, 0, 0, (* mov eax, 15 *)
180+ 0FH, 05H; (* syscall *)
181+
182+(* A signal stops the program: says where it was and why on standard
183+ error, and returns to the instruction that faulted. The handler was
184+ reset as it was called, so the instruction faults again and the
185+ signal ends the program. info and ctx are the kernel's siginfo and
186+ ucontext. *)
187+proc onSignal(sig: I64; info, ctx: ^U8);
188+var adr, sp, ip: U64; q: ^U64; why: []U8;
189+begin
190+ q := ^U64(info + 16); (* si_addr *)
191+ adr := q^;
192+ q := ^U64(ctx + 160); (* uc_mcontext's rsp, then rip *)
193+ sp := q[0];
194+ ip := q[1];
195+ if sig = SigFpe then why := "a division fault";
196+ elsif ip < Low then
197+ why := "a nil procedure";
198+ q := ^U64(sp);
199+ ip := q^ - 1; (* the call, by its return address *)
200+ elsif adr < Low then why := "a nil pointer";
201+ elsif adr - sp + 100000H < 200000H then why := "a stack overflow"; (* within a megabyte of rsp *)
202+ else why := "a bad address";
203+ end;
204+ where(ip);
205+ put("stopped by ");
206+ put(why);
207+ say;
208+end;
209+
73210 proc eq*(a, b: []U8): BOOL;
74211 var i: I64;
75212 begin
@@ -80,4 +217,10 @@
80217 return true;
81218 end;
82219
220+begin
221+ alt := {sp: ADR(altStack[0]), size: LEN(altStack)};
222+ SYSCALL(131, ADR(alt), 0); (* sigaltstack *)
223+ act := {handler: onSignal, flags: Flags, restorer: sigReturn};
224+ SYSCALL(13, SigSegv, ADR(act), 0, 8); (* rt_sigaction *)
225+ SYSCALL(13, SigFpe, ADR(act), 0, 8);
83226 end Rt.

src/diag.pas modified +13 -6

@@ -37,6 +37,7 @@
3737 quiet: boolean;
3838
3939 function AddFile(const path: shortstring; buf: PChar; len: longint): longint;
40+function ShownPath(const path: shortstring): shortstring;
4041 function PathOf(f: longint): shortstring;
4142 function LineOf(const p: TPos): longint;
4243 function Join(const p, q: TPos): TPos;
@@ -63,25 +64,31 @@
6364 var
6465 files: ^TFile; { numFiles of them, in room for cap }
6566 numFiles, cap: longint;
66- colour, colourKnown: boolean;
67+ colour, colourKnown, cwdKnown: boolean;
6768 cwd: shortstring; { the working directory and a /, or '' if unknown }
6869 lastError: TPos; { where the last error was }
6970 shown: boolean; { whether it was shown: its notes and help are only then }
7071
7172 { Paths are shown from the working directory where they are under it,
7273 so messages read the same wherever the compiler or its library is. }
73-function AddFile(const path: shortstring; buf: PChar; len: longint): longint;
74+function ShownPath(const path: shortstring): shortstring;
7475 var d: array[0..255] of char;
7576 begin
76- if numFiles = 0 then
77+ if not cwdKnown then begin
7778 if FpGetcwd(@d[0], SizeOf(d)) <> nil then cwd := StrPas(@d[0]) + '/';
79+ cwdKnown := true;
80+ end;
81+ if (cwd <> '') and (copy(path, 1, length(cwd)) = cwd) then ShownPath := copy(path, length(cwd) + 1, 255)
82+ else ShownPath := path;
83+end;
84+
85+function AddFile(const path: shortstring; buf: PChar; len: longint): longint;
86+begin
7887 if numFiles = cap then begin
7988 cap := 2 * cap + 8;
8089 ReallocMem(files, cap * SizeOf(TFile));
8190 end;
82- if (cwd <> '') and (copy(path, 1, length(cwd)) = cwd) then
83- files[numFiles].path := copy(path, length(cwd) + 1, 255)
84- else files[numFiles].path := path;
91+ files[numFiles].path := ShownPath(path);
8592 files[numFiles].buf := buf;
8693 files[numFiles].len := len;
8794 AddFile := numFiles;

src/gen.pas modified +45 -22

@@ -62,7 +62,6 @@
6262
6363 var
6464 rtFail, rtEq, argcChunk, argvChunk: longint;
65- fileChunk: longint; { the current file's name, for failed checks; -1 until needed }
6665 depth: longint; { values pushed and not yet popped }
6766
6867 procedure MakeConst(var x: TItem; t: PType; v: int64);
@@ -537,26 +536,52 @@
537536
538537 { Checks }
539538
539+const
540+ NoBase = -2; { a TMem that is no memory }
541+
540542 { The program goes on if the condition cc holds, and otherwise stops
541- in Rt.fail with the file and line. }
542-procedure Check(cc: longint);
543-var L: longint; s: shortstring;
543+ in Rt.fail(what, a, b, len, at, name), which says what failed at p,
544+ found by at, the address of the call: an index a (what = 0), a slice
545+ a..b (1), each for a length len, or a position a of the enumeration
546+ called name (2). a is in register ra, b in RDX; len is in memory at
547+ m, or n if m.base is NoBase. The failing code has p for its place,
548+ and the code after it the place before it again. }
549+procedure Check(cc, what: longint; const p: TPos; ra: longint; const m: TMem; n: int64; name: longint);
550+var L, line, col: longint; before: TLine;
544551 begin
545- if fileChunk < 0 then begin
546- s := CurPath + #0;
547- fileChunk := NewChunk(secRodata, -1, -1);
548- SetData(fileChunk, @s[1], length(s), 1);
549- end;
550552 L := NoLabel;
551553 Jcc(cc, L);
554+ before := lines^[numLines - 1];
555+ LineCol(p.a, line, col);
556+ NoteLine(line, col);
557+ MovRR(RSI, ra);
558+ if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false);
559+ Lea(R8, Here);
560+ MovRI(RDI, what);
561+ if name >= 0 then Lea(R9, MemRip(name, 0));
552562 AluI(aluAnd, RSP, -16);
553- Lea(RDI, MemRip(fileChunk, 0));
554- MovRI(RSI, CurLine);
555563 CallChunk(rtFail);
556564 Ud2;
557565 FixLink(L);
566+ NoteLine(before.line, before.col);
558567 end;
559568
569+{ For Check: a length known here, not in memory }
570+function NoMem: TMem;
571+begin
572+ NoMem := Mem(NoBase, 0);
573+end;
574+
575+{ t's name, ending in a zero byte, for Rt.fail }
576+function NameChunk(t: PType): longint;
577+var s: shortstring; c: longint;
578+begin
579+ s := TypeName(t) + #0;
580+ c := NewChunk(secRodata, -1, -1);
581+ SetData(c, @s[1], length(s), 1);
582+ NameChunk := c;
583+end;
584+
560585 { Selectors }
561586
562587 procedure Deref(var x: TItem);
@@ -610,7 +635,7 @@
610635 end else if t^.form = fArray then begin
611636 Value(y);
612637 CmpI(RAX, t^.len);
613- Check(ccB);
638+ Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1);
614639 if s <> 1 then ImulI(RAX, s);
615640 if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX));
616641 Alu(aluAdd, RAX, RCX);
@@ -621,7 +646,7 @@
621646 m := MemOf(x, RCX);
622647 ml := m; ml.disp := ml.disp + 8;
623648 AluM(aluCmp, RAX, ml);
624- Check(ccB);
649+ Check(ccB, 0, y.pos, RAX, ml, 0, -1);
625650 if s <> 1 then ImulI(RAX, s);
626651 AluM(aluAdd, RAX, m);
627652 x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false;
@@ -649,14 +674,12 @@
649674 if t^.form = fPtr then PopR(R8)
650675 else begin
651676 m := MemOf(x, R8);
652- if t^.form = fArray then CmpI(RDX, t^.len)
653- else begin
654- ml := m; ml.disp := ml.disp + 8;
655- AluM(aluCmp, RDX, ml);
656- end;
657- Check(ccBE);
677+ ml := m; ml.disp := ml.disp + 8;
678+ if t^.form = fArray then begin CmpI(RDX, t^.len); ml := NoMem end
679+ else AluM(aluCmp, RDX, ml);
680+ Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1);
658681 Alu(aluCmp, RCX, RDX);
659- Check(ccBE);
682+ Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1);
660683 end;
661684 tmp := AllocTemp(16, 8);
662685 MovRR(RAX, RDX);
@@ -1040,7 +1063,7 @@
10401063 end else if IsInt(s) and (t^.form = fEnum) then begin
10411064 if x.mode = mConst then begin
10421065 if (x.val < 0) or (x.val >= t^.len) then MarkAt(x.pos, IntStr(x.val) + ' isn''t a position of ' + TypeName(t))
1043- end else begin Value(x); CmpI(RAX, t^.len); Check(ccB) end;
1066+ end else begin Value(x); CmpI(RAX, t^.len); Check(ccB, 2, x.pos, RAX, NoMem, t^.len, NameChunk(t)) end;
10441067 end else if IsInt(s) and (t^.form = fSet) then begin
10451068 if x.mode = mConst then begin
10461069 if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1)
@@ -1361,7 +1384,7 @@
13611384 var i, top: longint; n: int64;
13621385 begin
13631386 BeginChunk(c);
1364- NoteLine(line);
1387+ NoteLine(line, 1); { a declaration starts a line }
13651388 Push(RBP);
13661389 MovRR(RBP, RSP);
13671390 Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 }

src/link.pas modified +49 -0

@@ -33,6 +33,7 @@
3333 extraAdr: int64; { that room }
3434 bssAdr, bssSize: int64; { the writable segment, the loader's table first }
3535 textImg, roImg: PBytes;
36+ placesChunk: longint = -1; { the rodata chunk of the places, made here }
3637
3738 procedure MarkLive(entry: longint);
3839 procedure LinkAll(headerSize, roExtra, dynSize: int64);
@@ -87,6 +88,53 @@
8788 Place := a;
8889 end;
8990
91+{ The places that Rt reads to say where a failed check or a signal
92+ was: their number and where text starts and stops (I64s); for each
93+ place in live code its address, line, column and the offset of its
94+ file's path (U32s); then the paths, each ending in a zero byte. They
95+ are in no order, since Rt looks at them only as the program stops. }
96+procedure MakePlaces(start, stop: int64);
97+var files, offs: PLongs; buf: PBytes; i, f, n, nfiles, k: longint; s: shortstring;
98+begin
99+ files := GetMem(numLines * 4 + 4);
100+ offs := GetMem(numLines * 4 + 4);
101+ n := 0; nfiles := 0; k := 0;
102+ for i := 0 to numLines - 1 do
103+ if chunks^[lines^[i].chunk].live then begin
104+ inc(n);
105+ f := 0;
106+ while (f < nfiles) and (files^[f] <> lines^[i].path) do inc(f);
107+ if f = nfiles then begin
108+ files^[f] := lines^[i].path;
109+ offs^[f] := k;
110+ k := k + length(ShownPath(NameOf(files^[f]))) + 1;
111+ inc(nfiles);
112+ end;
113+ end;
114+ buf := GetMem(24 + 16 * n + k);
115+ PInt64(@buf^[0])^ := n;
116+ PInt64(@buf^[8])^ := start;
117+ PInt64(@buf^[16])^ := stop;
118+ k := 24;
119+ for i := 0 to numLines - 1 do
120+ if chunks^[lines^[i].chunk].live then begin
121+ f := 0;
122+ while files^[f] <> lines^[i].path do inc(f);
123+ PLongint(@buf^[k])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start;
124+ PLongint(@buf^[k + 4])^ := lines^[i].line;
125+ PLongint(@buf^[k + 8])^ := lines^[i].col;
126+ PLongint(@buf^[k + 12])^ := offs^[f];
127+ k := k + 16;
128+ end;
129+ for f := 0 to nfiles - 1 do begin
130+ s := ShownPath(NameOf(files^[f])) + #0;
131+ Move(s[1], buf^[k], length(s));
132+ k := k + length(s);
133+ end;
134+ SetData(placesChunk, buf, k, 8);
135+ FreeMem(buf); FreeMem(files); FreeMem(offs);
136+end;
137+
90138 { Places the live chunks, leaving roExtra bytes at the end of rodata
91139 and dynSize at the start of the writable segment. }
92140 procedure LinkAll(headerSize, roExtra, dynSize: int64);
@@ -95,6 +143,7 @@
95143 textOff := (headerSize + 15) div 16 * 16;
96144 e := Place(secText, Base + textOff);
97145 textSize := e - (Base + textOff);
146+ if placesChunk >= 0 then MakePlaces(Base + textOff, e);
98147 roOff := (textOff + textSize + Page - 1) div Page * Page;
99148 e := Place(secRodata, Base + roOff);
100149 extraAdr := (e + 7) div 8 * 8;

src/parse.pas modified +6 -7

@@ -1406,7 +1406,7 @@
14061406 begin
14071407 start := TokPos;
14081408 first := id;
1409- NoteLine(CurLine);
1409+ NoteLine(CurLine, CurCol);
14101410 m := TempMark;
14111411 case tok of
14121412 sIdent: begin
@@ -1556,7 +1556,7 @@
15561556 curResult := sig^.base;
15571557 EnterBody(p^.chunk, line);
15581558 Statements;
1559- NoteLine(CurLine);
1559+ NoteLine(CurLine, CurCol);
15601560 Expect(sEnd);
15611561 EndProc(p^.chunk);
15621562 curResult := old;
@@ -2000,12 +2000,12 @@
20002000 function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule;
20012001 var
20022002 st: TScanState; saved: array[0..MaxDepth - 1] of longint; d: longint;
2003- oldMod: PModule; oldFile: longint; oldCheck: boolean; m, im: PModule;
2003+ oldMod: PModule; oldCheck: boolean; m, im: PModule;
20042004 imp: PImport; lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs;
20052005 begin
20062006 SaveScan(st);
20072007 SaveScopes(d, saved);
2008- oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; oldPath := curFile;
2008+ oldMod := curModule; oldCheck := checkMode; oldPath := curFile;
20092009 curFile := Name(path);
20102010 if not Open(path) then Fatal('can''t read ' + path);
20112011 New(m);
@@ -2018,7 +2018,6 @@
20182018 m^.body := -1;
20192019 m^.checkOnly := checkOnly;
20202020 curModule := m;
2021- fileChunk := -1;
20222021 checkMode := checkOnly;
20232022 if not checkOnly then begin
20242023 if numPending > High(pending) then Mark('imports nested more than ' + IntStr(High(pending) + 1) + ' deep');
@@ -2090,7 +2089,7 @@
20902089 curResult := nil;
20912090 EnterBody(body, line);
20922091 Statements;
2093- NoteLine(CurLine);
2092+ NoteLine(CurLine, CurCol);
20942093 Expect(sEnd);
20952094 EndProc(body);
20962095 end;
@@ -2112,7 +2111,7 @@
21122111 if lastModule = nil then firstModule := m else lastModule^.next := m;
21132112 lastModule := m;
21142113 end;
2115- curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; curFile := oldPath;
2114+ curModule := oldMod; checkMode := oldCheck; curFile := oldPath;
21162115 RestoreScopes(d, saved);
21172116 RestoreScan(st);
21182117 CompileFile := m;

src/raz.pas modified +6 -0

@@ -195,6 +195,12 @@
195195 Lea(RAX, Mem(RSP, 8));
196196 Store(MemRip(argvChunk, 0), RAX, 8);
197197 end;
198+ { the places, which the linker makes, for Rt to say where a failed
199+ check or a signal was }
200+ placesChunk := NewChunk(secRodata, -1, -1);
201+ SetData(placesChunk, nil, 0, 8);
202+ Lea(RAX, MemRip(placesChunk, 0));
203+ Store(MemRip(RtObj(rt, 'places')^.chunk, 0), RAX, 8);
198204 m := firstModule;
199205 while m <> nil do begin
200206 if m^.used and (m^.body >= 0) then CallChunk(m^.body);

src/scan.pas modified +25 -7

@@ -49,7 +49,6 @@
4949 rval: double;
5050 id: longint;
5151 f: longint; { the file, as diag knows it }
52- path: shortstring;
5352 end;
5453
5554 const
@@ -89,8 +88,9 @@
8988 function AfterPrev: TPos;
9089 function MissingPos: TPos;
9190 function SpanFrom(const p: TPos): TPos;
92-function CurPath: shortstring;
9391 function CurLine: longint;
92+function CurCol: longint;
93+procedure LineCol(a: longint; out line, col: longint);
9494
9595 implementation
9696
@@ -223,16 +223,35 @@
223223 SpanFrom.b := cur.prevEnd;
224224 end;
225225
226-function CurPath: shortstring;
226+function CurLine: longint;
227227 begin
228- CurPath := cur.path;
228+ CurLine := cur.tokLine;
229229 end;
230230
231-function CurLine: longint;
231+{ The line and column of byte a of the file, at or before the current
232+ symbol: counted back from the symbol's line, so that it costs only
233+ the distance. A column counts characters, as diag's do. }
234+procedure LineCol(a: longint; out line, col: longint);
235+var i, start: longint;
232236 begin
233- CurLine := cur.tokLine;
237+ line := cur.tokLine;
238+ start := cur.tokLineStart;
239+ while a < start do begin
240+ dec(line);
241+ dec(start);
242+ while (start > 0) and (cur.buf[start - 1] <> #10) do dec(start);
243+ end;
244+ col := 1;
245+ for i := start to a - 1 do
246+ if (byte(cur.buf[i]) and $C0) <> $80 then inc(col);
234247 end;
235248
249+function CurCol: longint;
250+var line: longint;
251+begin
252+ LineCol(cur.tokPos, line, CurCol);
253+end;
254+
236255 procedure Mark(const msg: shortstring);
237256 begin
238257 MarkAt(TokPos, msg);
@@ -342,7 +361,6 @@
342361 cur.tokPos := 0;
343362 cur.tokLine := 1;
344363 cur.tokLineStart := 0;
345- cur.path := path;
346364 cur.f := AddFile(path, cur.buf, n);
347365 Open := true;
348366 end;

src/x64.pas modified +17 -8

@@ -61,10 +61,11 @@
6161 ext: boolean; { bss: the address of a C symbol, its name }
6262 end;
6363
64- { A source line whose code starts at a place in text, for gdb }
64+ { A place in the source whose code starts at a place in text: for
65+ gdb, and for Rt to say where a failed check or a signal was }
6566 TLine = record
6667 at, chunk: longint; { the place in text, and its chunk }
67- path, line: longint; { the file's path, interned, and the line }
68+ path, line, col: longint; { the file's path, interned, the line and column }
6869 end;
6970
7071 TFixup = record
@@ -100,7 +101,8 @@
100101 procedure SetData(c: longint; p: PBytes; n, align: longint);
101102 procedure SetBss(c: longint; size, align: longint);
102103 function ExternChunk(name: longint): longint;
103-procedure NoteLine(line: longint);
104+procedure NoteLine(line, col: longint);
105+function Here: TMem;
104106
105107 procedure Put1(b: longint);
106108 procedure Put4(v: longint);
@@ -259,13 +261,13 @@
259261 chunks^[c].align := align;
260262 end;
261263
262-{ The code from here on is for line of the current file. A line with
263- no code of its own gives way to the next. }
264-procedure NoteLine(line: longint);
264+{ The code from here on is for line and column of the current file. A
265+ place with no code of its own gives way to the next. }
266+procedure NoteLine(line, col: longint);
265267 begin
266268 if (numLines > 0) and (lines^[numLines - 1].at = pc) then dec(numLines);
267- if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk)
268- and (lines^[numLines - 1].line = line) and (lines^[numLines - 1].path = curFile) then exit;
269+ if (numLines > 0) and (lines^[numLines - 1].chunk = curChunk) and (lines^[numLines - 1].line = line)
270+ and (lines^[numLines - 1].col = col) and (lines^[numLines - 1].path = curFile) then exit;
269271 if numLines = lineCap then begin
270272 lineCap := lineCap * 2;
271273 lines := ReAllocMem(lines, lineCap * SizeOf(TLine));
@@ -274,7 +276,14 @@
274276 lines^[numLines].chunk := curChunk;
275277 lines^[numLines].path := curFile;
276278 lines^[numLines].line := line;
279+ lines^[numLines].col := col;
277280 inc(numLines);
281+end;
282+
283+{ The address of the code about to be written, as an operand }
284+function Here: TMem;
285+begin
286+ Here := MemRip(curChunk, pc - chunks^[curChunk].start);
278287 end;
279288
280289 { The quadword that holds the address of C's symbol name, filled in

test/check.sh (history) modified +10 -3

@@ -4,7 +4,9 @@
44 # X.raz the program
55 # X.expect its standard output
66 # X.args its arguments, one per line (optional)
7-# X.status its exit status (optional; 0 if missing)
7+# X.status its exit status (optional; 0 if missing), 128 and the
8+# signal's number for one stopped by a signal
9+# X.stderr its standard error (optional; not compared if missing)
810 # A program without X.expect is compiled, not run. test/libc puts
911 # lib/Sys.raz, over the C library, before the system call one.
1012 # Programs in test/err must not compile, and must print X.err; there
@@ -16,6 +18,7 @@
1618 raz=./raz
1719 out=build/check
1820 mkdir -p $out
21+ulimit -c 0 # the tests stopped by a signal leave no core
1922 pass=0
2023 fail=0
2124
@@ -32,8 +35,8 @@
3235 if [ -f $dir/$name.args ]; then
3336 while IFS= read -r a; do set -- "$@" "$a"; done < $dir/$name.args
3437 fi
35- $out/$name "$@" > $out/$name.out 2>/dev/null
36- status=$?
38+ # in $(...), so that what the shell says of a signal goes nowhere
39+ status=$( { $out/$name "$@" > $out/$name.out 2>$out/$name.err; echo $?; } 2>/dev/null )
3740 want=0
3841 [ -f $dir/$name.status ] && want=$(cat $dir/$name.status)
3942 if ! cmp -s $out/$name.out $dir/$name.expect; then
@@ -42,6 +45,10 @@
4245 fail=$((fail + 1))
4346 elif [ $status != $want ]; then
4447 echo "FAIL $dir/$name: status $status, not $want"
48+ fail=$((fail + 1))
49+ elif [ -f $dir/$name.stderr ] && ! cmp -s $out/$name.err $dir/$name.stderr; then
50+ echo "FAIL $dir/$name: standard error differs"
51+ diff $out/$name.err $dir/$name.stderr | head -5
4552 fail=$((fail + 1))
4653 else
4754 pass=$((pass + 1))

test/libc/NilC.expect (history) added +1 -0

@@ -0,0 +1 @@
1+before

test/libc/NilC.raz (history) added +14 -0

@@ -0,0 +1,14 @@
1+(* A program over C stopped by a signal says where, as a static one
2+ does: C's own start-up runs before Rt's handlers are set *)
3+module NilC;
4+import Out;
5+
6+proc main*;
7+var p: ^I64;
8+begin
9+ Out.str("before\n");
10+ Out.flush;
11+ p^ := 1;
12+end;
13+
14+end NilC.

test/libc/NilC.status (history) added +1 -0

@@ -0,0 +1 @@
1+139

test/libc/NilC.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/libc/NilC.raz:11:5: stopped by a nil pointer

test/libc/Through.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/libc/Through.raz:29:7: index 3 is out of range for a length of 3

test/run/Bounds.stderr added +1 -0

@@ -0,0 +1 @@
1+test/run/Bounds.raz:8:11: index 4 is out of range for a length of 4

test/run/DivLowest.expect (history) added +1 -0

@@ -0,0 +1 @@
1+4611686018427387904
\ No newline at end of file

test/run/DivLowest.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+module DivLowest;
2+(* The lowest I64 divided by -1 stops the program with SIGFPE, as
3+ dividing by zero does *)
4+import Out;
5+
6+proc main*;
7+var a, b: I64;
8+begin
9+ a := -9223372036854775807 - 1;
10+ b := ARGC() - 2;
11+ Out.int(a div (-2));
12+ Out.flush;
13+ Out.int(a div b);
14+end;
15+
16+end DivLowest.

test/run/DivLowest.status (history) added +1 -0

@@ -0,0 +1 @@
1+136

test/run/DivLowest.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/DivLowest.raz:13:5: stopped by a division fault

test/run/DivZero.expect (history) added +0 -0

An empty file.

test/run/DivZero.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module DivZero;
2+(* Dividing by zero stops the program with SIGFPE, after saying where *)
3+
4+proc main*: I64;
5+var a, b: I64;
6+begin
7+ a := 7;
8+ b := ARGC() - 1;
9+ return a mod b;
10+end;
11+
12+end DivZero.

test/run/DivZero.status (history) added +1 -0

@@ -0,0 +1 @@
1+136

test/run/DivZero.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/DivZero.raz:9:5: stopped by a division fault

test/run/Kinds.stderr added +1 -0

@@ -0,0 +1 @@
1+test/run/Kinds.raz:92:16: 5 isn't a position of Color

test/run/NilPointer.expect (history) added +1 -0

@@ -0,0 +1 @@
1+1
\ No newline at end of file

test/run/NilPointer.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+module NilPointer;
2+(* Following nil stops the program with SIGSEGV, after saying where *)
3+import Out;
4+
5+type Cell = record next: ^Cell; v: I64; end;
6+
7+proc main*;
8+var c: Cell;
9+begin
10+ c.v := 1;
11+ Out.int(c.v);
12+ Out.flush;
13+ Out.int(c.next^.v);
14+end;
15+
16+end NilPointer.

test/run/NilPointer.status (history) added +1 -0

@@ -0,0 +1 @@
1+139

test/run/NilPointer.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/NilPointer.raz:13:5: stopped by a nil pointer

test/run/NilProc.expect (history) added +0 -0

An empty file.

test/run/NilProc.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module NilProc;
2+(* Calling a nil procedure: the place is the call's *)
3+
4+var f: proc(n: I64): I64;
5+
6+proc main*: I64;
7+begin
8+ return f(1)
9+ + 1;
10+end;
11+
12+end NilProc.

test/run/NilProc.status (history) added +1 -0

@@ -0,0 +1 @@
1+139

test/run/NilProc.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/NilProc.raz:8:5: stopped by a nil procedure

test/run/SliceBounds.expect added +1 -0

@@ -0,0 +1 @@
1+6 3 0
\ No newline at end of file

test/run/SliceBounds.raz added +15 -0

@@ -0,0 +1,15 @@
1+module SliceBounds;
2+(* A slice must have 0 <= lo <= hi <= the length *)
3+import Out;
4+
5+proc main*;
6+var a: [8]I64; s: []I64; lo: I64;
7+begin
8+ s := a[0..8];
9+ for lo := 0 to 9 by 3 do
10+ Out.int(LEN(s[lo..6]));
11+ Out.str(" ");
12+ end;
13+end;
14+
15+end SliceBounds.

test/run/SliceBounds.status added +1 -0

@@ -0,0 +1 @@
1+2

test/run/SliceBounds.stderr added +1 -0

@@ -0,0 +1 @@
1+test/run/SliceBounds.raz:10:23: slice 9..6 is out of range for a length of 8

test/run/SliceIndex.expect added +1 -0

@@ -0,0 +1 @@
1+0123
\ No newline at end of file

test/run/SliceIndex.raz added +15 -0

@@ -0,0 +1,15 @@
1+module SliceIndex;
2+(* An index checked against a slice's length, which is in memory *)
3+import Out;
4+
5+proc main*;
6+var a: [8]I64; s: []I64; i: I64;
7+begin
8+ s := a[2..5];
9+ for i := 0 to 3 do
10+ Out.int(i);
11+ s[i] := i;
12+ end;
13+end;
14+
15+end SliceIndex.

test/run/SliceIndex.status added +1 -0

@@ -0,0 +1 @@
1+2

test/run/SliceIndex.stderr added +1 -0

@@ -0,0 +1 @@
1+test/run/SliceIndex.raz:11:11: index 3 is out of range for a length of 3

test/run/StackOverflow.expect (history) added +0 -0

An empty file.

test/run/StackOverflow.raz (history) added +16 -0

@@ -0,0 +1,16 @@
1+module StackOverflow;
2+(* A recursion with no end runs out of stack: SIGSEGV, said as such *)
3+
4+proc down(n: I64): I64;
5+var pad: [64]I64;
6+begin
7+ pad[n mod 64] := n;
8+ return down(n + 1) + pad[0];
9+end;
10+
11+proc main*: I64;
12+begin
13+ return down(0);
14+end;
15+
16+end StackOverflow.

test/run/StackOverflow.status (history) added +1 -0

@@ -0,0 +1 @@
1+139

test/run/StackOverflow.stderr (history) added +1 -0

@@ -0,0 +1 @@
1+test/run/StackOverflow.raz:4:1: stopped by a stack overflow