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.
| @@ -265,7 +265,7 @@ | ||
| 265 | 265 | makes them errors, and `make check` builds `lib/`, `examples/` |
| 266 | 266 | and `tools/` with it. The machinery, *main*; clearing the |
| 267 | 267 | 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 | |
| 269 | 269 | values: `index 7 is out of range for a length of 5`, the slice, |
| 270 | 270 | the enumeration position, at file, line and column. A table from |
| 271 | 271 | code addresses to lines goes into the executable, and `Rt` sets |
| @@ -273,7 +273,8 @@ | ||
| 273 | 273 | and why (a `nil` pointer, a `nil` procedure, stack overflow, a |
| 274 | 274 | bad address, a division fault) before it dies by that signal. |
| 275 | 275 | 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. | |
| 277 | 278 | The compiler and `Rt`, |
| 278 | 279 | *main*; the tests, *agent*. |
| 279 | 280 | - [ ] 27. `doc/compiler.md` (its table and routine names), `README.md` |
| @@ -8,15 +8,15 @@ | ||
| 8 | 8 | |
| 9 | 9 | | File | Lines | Code | What | |
| 10 | 10 | |---|---|---|---| |
| 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 | | |
| 13 | 13 | | `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 | | |
| 18 | 18 | | `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 | | |
| 20 | 20 | |
| 21 | 21 | Every file opens with a comment that says what it is, what it depends |
| 22 | 22 | on and what it leaves out. Read those first; they are the map. |
| @@ -65,7 +65,8 @@ | ||
| 65 | 65 | becomes a string chunk, a call, and a fixup to `Out.str`'s chunk. |
| 66 | 66 | 5. Back in `raz.pas`, `Use` marks the modules the program uses; the |
| 67 | 67 | 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`. | |
| 69 | 70 | 6. `WriteElf` (`elf.pas`) has `link.pas` mark what `_start` reaches, |
| 70 | 71 | place it and patch it, then writes headers, code, data, symbols and |
| 71 | 72 | line numbers. |
| @@ -220,8 +221,9 @@ | ||
| 220 | 221 | `test/enctest.pas` emits one of each for `objdump` to check. |
| 221 | 222 | |
| 222 | 223 | 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 | |
| 225 | 227 | dynamic loader will fill with that symbol's address. |
| 226 | 228 | |
| 227 | 229 | ## gen.pas: the code generator |
| @@ -269,8 +271,12 @@ | ||
| 269 | 271 | |
| 270 | 272 | Variables: `Deref`, `Field`, `Index` and `Slice` turn an item for a |
| 271 | 273 | 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. | |
| 274 | 280 | |
| 275 | 281 | Assignment and conversion: `Coerce` makes a value fit the type it's |
| 276 | 282 | going to, or reports that it can't; it's where a string literal becomes |
| @@ -371,6 +377,16 @@ | ||
| 371 | 377 | patching every fixup with the distance from the end of its field to its |
| 372 | 378 | target. |
| 373 | 379 | |
| 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 | + | |
| 374 | 390 | The layout is chosen so the file can be mapped as it lies: text just |
| 375 | 391 | after the headers, rodata on the next page, then a writable page that |
| 376 | 392 | begins with the dynamic loader's table (in a program that uses C) and |
| @@ -423,6 +439,6 @@ | ||
| 423 | 439 | in the same order), a case in `Statement`, and a routine beside the |
| 424 | 440 | others. |
| 425 | 441 | - **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. | |
| 427 | 443 | |
| 428 | 444 | After any change: `make check`, `make bench`, `make loc`. |
| @@ -1,18 +1,48 @@ | ||
| 1 | 1 | (* The run time: ending the program, and what the compiler's code |
| 2 | 2 | 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. *) | |
| 5 | 8 | module Rt; |
| 6 | 9 | import Sys; |
| 7 | 10 | |
| 8 | 11 | const |
| 9 | 12 | Full*: ERROR = "too many procedures to run at exit"; |
| 10 | 13 | 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; | |
| 11 | 19 | |
| 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 | + | |
| 12 | 33 | var |
| 13 | 34 | atExits: [MaxAtExit]proc; |
| 14 | 35 | numAtExit: I64; |
| 15 | 36 | 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 *) | |
| 16 | 46 | |
| 17 | 47 | (* Registers p to run when the program ends by exit. *) |
| 18 | 48 | proc atExit*(p: proc): ERROR; |
| @@ -49,27 +79,134 @@ | ||
| 49 | 79 | exit(2); |
| 50 | 80 | end; |
| 51 | 81 | |
| 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; | |
| 56 | 99 | 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; | |
| 60 | 101 | i := 20; |
| 61 | 102 | 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; | |
| 70 | 174 | exit(2); |
| 71 | 175 | end; |
| 72 | 176 | |
| 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 | + | |
| 73 | 210 | proc eq*(a, b: []U8): BOOL; |
| 74 | 211 | var i: I64; |
| 75 | 212 | begin |
| @@ -80,4 +217,10 @@ | ||
| 80 | 217 | return true; |
| 81 | 218 | end; |
| 82 | 219 | |
| 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); | |
| 83 | 226 | end Rt. |
| @@ -37,6 +37,7 @@ | ||
| 37 | 37 | quiet: boolean; |
| 38 | 38 | |
| 39 | 39 | function AddFile(const path: shortstring; buf: PChar; len: longint): longint; |
| 40 | +function ShownPath(const path: shortstring): shortstring; | |
| 40 | 41 | function PathOf(f: longint): shortstring; |
| 41 | 42 | function LineOf(const p: TPos): longint; |
| 42 | 43 | function Join(const p, q: TPos): TPos; |
| @@ -63,25 +64,31 @@ | ||
| 63 | 64 | var |
| 64 | 65 | files: ^TFile; { numFiles of them, in room for cap } |
| 65 | 66 | numFiles, cap: longint; |
| 66 | - colour, colourKnown: boolean; | |
| 67 | + colour, colourKnown, cwdKnown: boolean; | |
| 67 | 68 | cwd: shortstring; { the working directory and a /, or '' if unknown } |
| 68 | 69 | lastError: TPos; { where the last error was } |
| 69 | 70 | shown: boolean; { whether it was shown: its notes and help are only then } |
| 70 | 71 | |
| 71 | 72 | { Paths are shown from the working directory where they are under it, |
| 72 | 73 | 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; | |
| 74 | 75 | var d: array[0..255] of char; |
| 75 | 76 | begin |
| 76 | - if numFiles = 0 then | |
| 77 | + if not cwdKnown then begin | |
| 77 | 78 | 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 | |
| 78 | 87 | if numFiles = cap then begin |
| 79 | 88 | cap := 2 * cap + 8; |
| 80 | 89 | ReallocMem(files, cap * SizeOf(TFile)); |
| 81 | 90 | 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); | |
| 85 | 92 | files[numFiles].buf := buf; |
| 86 | 93 | files[numFiles].len := len; |
| 87 | 94 | AddFile := numFiles; |
| @@ -62,7 +62,6 @@ | ||
| 62 | 62 | |
| 63 | 63 | var |
| 64 | 64 | rtFail, rtEq, argcChunk, argvChunk: longint; |
| 65 | - fileChunk: longint; { the current file's name, for failed checks; -1 until needed } | |
| 66 | 65 | depth: longint; { values pushed and not yet popped } |
| 67 | 66 | |
| 68 | 67 | procedure MakeConst(var x: TItem; t: PType; v: int64); |
| @@ -537,26 +536,52 @@ | ||
| 537 | 536 | |
| 538 | 537 | { Checks } |
| 539 | 538 | |
| 539 | +const | |
| 540 | + NoBase = -2; { a TMem that is no memory } | |
| 541 | + | |
| 540 | 542 | { 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; | |
| 544 | 551 | 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; | |
| 550 | 552 | L := NoLabel; |
| 551 | 553 | 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)); | |
| 552 | 562 | AluI(aluAnd, RSP, -16); |
| 553 | - Lea(RDI, MemRip(fileChunk, 0)); | |
| 554 | - MovRI(RSI, CurLine); | |
| 555 | 563 | CallChunk(rtFail); |
| 556 | 564 | Ud2; |
| 557 | 565 | FixLink(L); |
| 566 | + NoteLine(before.line, before.col); | |
| 558 | 567 | end; |
| 559 | 568 | |
| 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 | + | |
| 560 | 585 | { Selectors } |
| 561 | 586 | |
| 562 | 587 | procedure Deref(var x: TItem); |
| @@ -610,7 +635,7 @@ | ||
| 610 | 635 | end else if t^.form = fArray then begin |
| 611 | 636 | Value(y); |
| 612 | 637 | CmpI(RAX, t^.len); |
| 613 | - Check(ccB); | |
| 638 | + Check(ccB, 0, y.pos, RAX, NoMem, t^.len, -1); | |
| 614 | 639 | if s <> 1 then ImulI(RAX, s); |
| 615 | 640 | if x.base = bStack then PopR(RCX) else Lea(RCX, MemOf(x, RCX)); |
| 616 | 641 | Alu(aluAdd, RAX, RCX); |
| @@ -621,7 +646,7 @@ | ||
| 621 | 646 | m := MemOf(x, RCX); |
| 622 | 647 | ml := m; ml.disp := ml.disp + 8; |
| 623 | 648 | AluM(aluCmp, RAX, ml); |
| 624 | - Check(ccB); | |
| 649 | + Check(ccB, 0, y.pos, RAX, ml, 0, -1); | |
| 625 | 650 | if s <> 1 then ImulI(RAX, s); |
| 626 | 651 | AluM(aluAdd, RAX, m); |
| 627 | 652 | x.base := bReg; x.reg := RAX; x.off := 0; x.isVar := true; x.vol := false; |
| @@ -649,14 +674,12 @@ | ||
| 649 | 674 | if t^.form = fPtr then PopR(R8) |
| 650 | 675 | else begin |
| 651 | 676 | 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); | |
| 658 | 681 | Alu(aluCmp, RCX, RDX); |
| 659 | - Check(ccBE); | |
| 682 | + Check(ccBE, 1, lo.pos, RCX, ml, t^.len, -1); | |
| 660 | 683 | end; |
| 661 | 684 | tmp := AllocTemp(16, 8); |
| 662 | 685 | MovRR(RAX, RDX); |
| @@ -1040,7 +1063,7 @@ | ||
| 1040 | 1063 | end else if IsInt(s) and (t^.form = fEnum) then begin |
| 1041 | 1064 | if x.mode = mConst then begin |
| 1042 | 1065 | 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; | |
| 1044 | 1067 | end else if IsInt(s) and (t^.form = fSet) then begin |
| 1045 | 1068 | if x.mode = mConst then begin |
| 1046 | 1069 | if t^.len < 64 then x.val := x.val and ((int64(1) shl t^.len) - 1) |
| @@ -1361,7 +1384,7 @@ | ||
| 1361 | 1384 | var i, top: longint; n: int64; |
| 1362 | 1385 | begin |
| 1363 | 1386 | BeginChunk(c); |
| 1364 | - NoteLine(line); | |
| 1387 | + NoteLine(line, 1); { a declaration starts a line } | |
| 1365 | 1388 | Push(RBP); |
| 1366 | 1389 | MovRR(RBP, RSP); |
| 1367 | 1390 | Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } |
| @@ -33,6 +33,7 @@ | ||
| 33 | 33 | extraAdr: int64; { that room } |
| 34 | 34 | bssAdr, bssSize: int64; { the writable segment, the loader's table first } |
| 35 | 35 | textImg, roImg: PBytes; |
| 36 | + placesChunk: longint = -1; { the rodata chunk of the places, made here } | |
| 36 | 37 | |
| 37 | 38 | procedure MarkLive(entry: longint); |
| 38 | 39 | procedure LinkAll(headerSize, roExtra, dynSize: int64); |
| @@ -87,6 +88,53 @@ | ||
| 87 | 88 | Place := a; |
| 88 | 89 | end; |
| 89 | 90 | |
| 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 | + | |
| 90 | 138 | { Places the live chunks, leaving roExtra bytes at the end of rodata |
| 91 | 139 | and dynSize at the start of the writable segment. } |
| 92 | 140 | procedure LinkAll(headerSize, roExtra, dynSize: int64); |
| @@ -95,6 +143,7 @@ | ||
| 95 | 143 | textOff := (headerSize + 15) div 16 * 16; |
| 96 | 144 | e := Place(secText, Base + textOff); |
| 97 | 145 | textSize := e - (Base + textOff); |
| 146 | + if placesChunk >= 0 then MakePlaces(Base + textOff, e); | |
| 98 | 147 | roOff := (textOff + textSize + Page - 1) div Page * Page; |
| 99 | 148 | e := Place(secRodata, Base + roOff); |
| 100 | 149 | extraAdr := (e + 7) div 8 * 8; |
| @@ -1406,7 +1406,7 @@ | ||
| 1406 | 1406 | begin |
| 1407 | 1407 | start := TokPos; |
| 1408 | 1408 | first := id; |
| 1409 | - NoteLine(CurLine); | |
| 1409 | + NoteLine(CurLine, CurCol); | |
| 1410 | 1410 | m := TempMark; |
| 1411 | 1411 | case tok of |
| 1412 | 1412 | sIdent: begin |
| @@ -1556,7 +1556,7 @@ | ||
| 1556 | 1556 | curResult := sig^.base; |
| 1557 | 1557 | EnterBody(p^.chunk, line); |
| 1558 | 1558 | Statements; |
| 1559 | - NoteLine(CurLine); | |
| 1559 | + NoteLine(CurLine, CurCol); | |
| 1560 | 1560 | Expect(sEnd); |
| 1561 | 1561 | EndProc(p^.chunk); |
| 1562 | 1562 | curResult := old; |
| @@ -2000,12 +2000,12 @@ | ||
| 2000 | 2000 | function CompileFile(const path: shortstring; n: longint; const args: TArgs; checkOnly: boolean): PModule; |
| 2001 | 2001 | var |
| 2002 | 2002 | 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; | |
| 2004 | 2004 | imp: PImport; lib: PLib; start: TPos; alias, mname, line, oldPath: longint; o: PObj; body: longint; iargs: TArgs; |
| 2005 | 2005 | begin |
| 2006 | 2006 | SaveScan(st); |
| 2007 | 2007 | SaveScopes(d, saved); |
| 2008 | - oldMod := curModule; oldFile := fileChunk; oldCheck := checkMode; oldPath := curFile; | |
| 2008 | + oldMod := curModule; oldCheck := checkMode; oldPath := curFile; | |
| 2009 | 2009 | curFile := Name(path); |
| 2010 | 2010 | if not Open(path) then Fatal('can''t read ' + path); |
| 2011 | 2011 | New(m); |
| @@ -2018,7 +2018,6 @@ | ||
| 2018 | 2018 | m^.body := -1; |
| 2019 | 2019 | m^.checkOnly := checkOnly; |
| 2020 | 2020 | curModule := m; |
| 2021 | - fileChunk := -1; | |
| 2022 | 2021 | checkMode := checkOnly; |
| 2023 | 2022 | if not checkOnly then begin |
| 2024 | 2023 | if numPending > High(pending) then Mark('imports nested more than ' + IntStr(High(pending) + 1) + ' deep'); |
| @@ -2090,7 +2089,7 @@ | ||
| 2090 | 2089 | curResult := nil; |
| 2091 | 2090 | EnterBody(body, line); |
| 2092 | 2091 | Statements; |
| 2093 | - NoteLine(CurLine); | |
| 2092 | + NoteLine(CurLine, CurCol); | |
| 2094 | 2093 | Expect(sEnd); |
| 2095 | 2094 | EndProc(body); |
| 2096 | 2095 | end; |
| @@ -2112,7 +2111,7 @@ | ||
| 2112 | 2111 | if lastModule = nil then firstModule := m else lastModule^.next := m; |
| 2113 | 2112 | lastModule := m; |
| 2114 | 2113 | end; |
| 2115 | - curModule := oldMod; fileChunk := oldFile; checkMode := oldCheck; curFile := oldPath; | |
| 2114 | + curModule := oldMod; checkMode := oldCheck; curFile := oldPath; | |
| 2116 | 2115 | RestoreScopes(d, saved); |
| 2117 | 2116 | RestoreScan(st); |
| 2118 | 2117 | CompileFile := m; |
| @@ -195,6 +195,12 @@ | ||
| 195 | 195 | Lea(RAX, Mem(RSP, 8)); |
| 196 | 196 | Store(MemRip(argvChunk, 0), RAX, 8); |
| 197 | 197 | 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); | |
| 198 | 204 | m := firstModule; |
| 199 | 205 | while m <> nil do begin |
| 200 | 206 | if m^.used and (m^.body >= 0) then CallChunk(m^.body); |
| @@ -49,7 +49,6 @@ | ||
| 49 | 49 | rval: double; |
| 50 | 50 | id: longint; |
| 51 | 51 | f: longint; { the file, as diag knows it } |
| 52 | - path: shortstring; | |
| 53 | 52 | end; |
| 54 | 53 | |
| 55 | 54 | const |
| @@ -89,8 +88,9 @@ | ||
| 89 | 88 | function AfterPrev: TPos; |
| 90 | 89 | function MissingPos: TPos; |
| 91 | 90 | function SpanFrom(const p: TPos): TPos; |
| 92 | -function CurPath: shortstring; | |
| 93 | 91 | function CurLine: longint; |
| 92 | +function CurCol: longint; | |
| 93 | +procedure LineCol(a: longint; out line, col: longint); | |
| 94 | 94 | |
| 95 | 95 | implementation |
| 96 | 96 | |
| @@ -223,16 +223,35 @@ | ||
| 223 | 223 | SpanFrom.b := cur.prevEnd; |
| 224 | 224 | end; |
| 225 | 225 | |
| 226 | -function CurPath: shortstring; | |
| 226 | +function CurLine: longint; | |
| 227 | 227 | begin |
| 228 | - CurPath := cur.path; | |
| 228 | + CurLine := cur.tokLine; | |
| 229 | 229 | end; |
| 230 | 230 | |
| 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; | |
| 232 | 236 | 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); | |
| 234 | 247 | end; |
| 235 | 248 | |
| 249 | +function CurCol: longint; | |
| 250 | +var line: longint; | |
| 251 | +begin | |
| 252 | + LineCol(cur.tokPos, line, CurCol); | |
| 253 | +end; | |
| 254 | + | |
| 236 | 255 | procedure Mark(const msg: shortstring); |
| 237 | 256 | begin |
| 238 | 257 | MarkAt(TokPos, msg); |
| @@ -342,7 +361,6 @@ | ||
| 342 | 361 | cur.tokPos := 0; |
| 343 | 362 | cur.tokLine := 1; |
| 344 | 363 | cur.tokLineStart := 0; |
| 345 | - cur.path := path; | |
| 346 | 364 | cur.f := AddFile(path, cur.buf, n); |
| 347 | 365 | Open := true; |
| 348 | 366 | end; |
| @@ -61,10 +61,11 @@ | ||
| 61 | 61 | ext: boolean; { bss: the address of a C symbol, its name } |
| 62 | 62 | end; |
| 63 | 63 | |
| 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 } | |
| 65 | 66 | TLine = record |
| 66 | 67 | 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 } | |
| 68 | 69 | end; |
| 69 | 70 | |
| 70 | 71 | TFixup = record |
| @@ -100,7 +101,8 @@ | ||
| 100 | 101 | procedure SetData(c: longint; p: PBytes; n, align: longint); |
| 101 | 102 | procedure SetBss(c: longint; size, align: longint); |
| 102 | 103 | function ExternChunk(name: longint): longint; |
| 103 | -procedure NoteLine(line: longint); | |
| 104 | +procedure NoteLine(line, col: longint); | |
| 105 | +function Here: TMem; | |
| 104 | 106 | |
| 105 | 107 | procedure Put1(b: longint); |
| 106 | 108 | procedure Put4(v: longint); |
| @@ -259,13 +261,13 @@ | ||
| 259 | 261 | chunks^[c].align := align; |
| 260 | 262 | end; |
| 261 | 263 | |
| 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); | |
| 265 | 267 | begin |
| 266 | 268 | 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; | |
| 269 | 271 | if numLines = lineCap then begin |
| 270 | 272 | lineCap := lineCap * 2; |
| 271 | 273 | lines := ReAllocMem(lines, lineCap * SizeOf(TLine)); |
| @@ -274,7 +276,14 @@ | ||
| 274 | 276 | lines^[numLines].chunk := curChunk; |
| 275 | 277 | lines^[numLines].path := curFile; |
| 276 | 278 | lines^[numLines].line := line; |
| 279 | + lines^[numLines].col := col; | |
| 277 | 280 | 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); | |
| 278 | 287 | end; |
| 279 | 288 | |
| 280 | 289 | { The quadword that holds the address of C's symbol name, filled in |
| @@ -4,7 +4,9 @@ | ||
| 4 | 4 | # X.raz the program |
| 5 | 5 | # X.expect its standard output |
| 6 | 6 | # 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) | |
| 8 | 10 | # A program without X.expect is compiled, not run. test/libc puts |
| 9 | 11 | # lib/Sys.raz, over the C library, before the system call one. |
| 10 | 12 | # Programs in test/err must not compile, and must print X.err; there |
| @@ -16,6 +18,7 @@ | ||
| 16 | 18 | raz=./raz |
| 17 | 19 | out=build/check |
| 18 | 20 | mkdir -p $out |
| 21 | +ulimit -c 0 # the tests stopped by a signal leave no core | |
| 19 | 22 | pass=0 |
| 20 | 23 | fail=0 |
| 21 | 24 | |
| @@ -32,8 +35,8 @@ | ||
| 32 | 35 | if [ -f $dir/$name.args ]; then |
| 33 | 36 | while IFS= read -r a; do set -- "$@" "$a"; done < $dir/$name.args |
| 34 | 37 | 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 ) | |
| 37 | 40 | want=0 |
| 38 | 41 | [ -f $dir/$name.status ] && want=$(cat $dir/$name.status) |
| 39 | 42 | if ! cmp -s $out/$name.out $dir/$name.expect; then |
| @@ -42,6 +45,10 @@ | ||
| 42 | 45 | fail=$((fail + 1)) |
| 43 | 46 | elif [ $status != $want ]; then |
| 44 | 47 | 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 | |
| 45 | 52 | fail=$((fail + 1)) |
| 46 | 53 | else |
| 47 | 54 | pass=$((pass + 1)) |
| @@ -0,0 +1 @@ | ||
| 1 | +before |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/libc/NilC.raz:11:5: stopped by a nil pointer |
| @@ -0,0 +1 @@ | ||
| 1 | +test/libc/Through.raz:29:7: index 3 is out of range for a length of 3 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/Bounds.raz:8:11: index 4 is out of range for a length of 4 |
| @@ -0,0 +1 @@ | ||
| 1 | +4611686018427387904 | |
| \ No newline at end of file |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +136 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/DivLowest.raz:13:5: stopped by a division fault |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +136 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/DivZero.raz:9:5: stopped by a division fault |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/Kinds.raz:92:16: 5 isn't a position of Color |
| @@ -0,0 +1 @@ | ||
| 1 | +1 | |
| \ No newline at end of file |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/NilPointer.raz:13:5: stopped by a nil pointer |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/NilProc.raz:8:5: stopped by a nil procedure |
| @@ -0,0 +1 @@ | ||
| 1 | +6 3 0 | |
| \ No newline at end of file |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/SliceBounds.raz:10:23: slice 9..6 is out of range for a length of 8 |
| @@ -0,0 +1 @@ | ||
| 1 | +0123 | |
| \ No newline at end of file |
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/SliceIndex.raz:11:11: index 3 is out of range for a length of 3 |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +139 |
| @@ -0,0 +1 @@ | ||
| 1 | +test/run/StackOverflow.raz:4:1: stopped by a stack overflow |