From 5dadea4419a0b4b9e000c6d53bd32175f5f623d7 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:31:05 +0100 Subject: [PATCH] One table of lines, not two The places held Rt's own table, 16 bytes a line note, beside DWARF's line table, which gdb read and wasn't loaded. Now link.pas makes the DWARF line program, its columns added and its small steps special opcodes, at the end of the places, loaded; .debug_line's header points at it there, and Rt runs it (row) to say where code comes from, through place, which the entry point gives it as it gives calls, so that raz -s leaves both out. hello 35,760 -> 28,424 bytes, -s 7,304 -> 6,752; rat 607,656 -> 505,184 bytes. The guide's table of lines is true again. --- diff --git a/PLAN.md b/PLAN.md index 9be170a..9757ea4 100644 --- a/PLAN.md +++ b/PLAN.md @@ -40,7 +40,7 @@ 4. Done: **`while` tested at the bottom**, its test moved after the body and jumped to first, as `for` is now. ulam found nothing in it, and lost 3% where the test was long: measured, and kept only if it pays. -5. **One table of lines, not two.** Rt finds a place in `.debug_line`, +5. Done: **One table of lines, not two.** Rt finds a place in `.debug_line`, loaded with the program, which gdb reads already, in place of the places' own 16 bytes a line: about 7 KB off hello, more off a big program, for a decoder of DWARF's line program in Rt. diff --git a/README.md b/README.md index ba00703..caacbcf 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ code instead of writing it: each line of source, and under it the instructions made for it. `-s` strips what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the -profiler, so hello, world is 7 KB instead of 35; `-noprofile` leaves +profiler, so hello, world is 7 KB instead of 28; `-noprofile` leaves out only the profiler. At run time, a failed check says where it is and what failed, as in diff --git a/doc/compiler.md b/doc/compiler.md index 1a42159..f66d08f 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -12,15 +12,15 @@ |---|---|---|---| | `src/diag.pas` | 316 | 233 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 886 | 747 | Symbols, literals, comments, positions | -| `src/sym.pas` | 644 | 527 | Types, objects, scopes, layout | +| `src/sym.pas` | 646 | 528 | Types, objects, scopes, layout | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 1,128 | 936 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,775 | 1,294 | The stack machine, for x86-64: values, operations, checks, calls, frames | -| `src/item.pas` | 1,551 | 1,278 | Items: types, constants, variables, operators, conversions, calls | -| `src/parse.pas` | 2,975 | 2,612 | Declarations, statements, expressions, built-ins, recovery, warnings | -| `src/link.pas` | 259 | 195 | Tree shaking, layout, patching, the places | -| `src/elf.pas` | 438 | 355 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 244 | 187 | The command line | +| `src/x64.pas` | 1,146 | 951 | The instruction encoder, jumps within a procedure, the assembler | +| `src/gen.pas` | 2,010 | 1,462 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/item.pas` | 1,574 | 1,299 | Items: types, constants, variables, operators, conversions, calls | +| `src/parse.pas` | 3,078 | 2,689 | Declarations, statements, expressions, built-ins, recovery, warnings | +| `src/link.pas` | 334 | 258 | Tree shaking, layout, patching, the places | +| `src/elf.pas` | 373 | 294 | Static and dynamic ELF, symbol table, line numbers | +| `src/raz.pas` | 245 | 188 | The command line | 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. @@ -417,6 +417,13 @@ it to where its loops fell: without the top aligned it lost nothing, and rat gained nothing either. +5. **One table of lines, not two.** Rt had a table of its own in the + places, 16 bytes a line note, beside the DWARF line table that gdb + read, which wasn't loaded. Now the DWARF table, its columns added, + is in the places, loaded, and Rt runs it (`row`, in `Rt.raz`) where + it looked through its own; `.debug_line`'s section header points + at it in rodata. Hello is 7 KB smaller, 28 KB; rat 17%, 505 KB. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is @@ -908,13 +915,17 @@ target, and every data fixup with its target's address. 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; and for the trap report, each live procedure's +places, a rodata chunk: for the trap report, each live procedure's address range and name, and its variables, which `Body` (`parse.pas`) notes with `NoteVar`: each one's name, its place in the frame and how -`Rt` shows it (`VarKind`). `Rt` reads them, through `Rt.places`, which -the entry point sets, to say where a failed check or a signal was: the -place with the highest address up to the one it has. Its signal +`Rt` shows it (`VarKind`); then the line table (`MakeLines`), DWARF +4's line program, which gdb reads as `.debug_line`: one sequence of +rows for each procedure, a row for each line note in live code, its +address, file, line and column. A row that moves the address and the +line a little is a byte, one of DWARF's special opcodes. `Rt` reads +the places, through `Rt.places`, which the entry point sets, and runs +the line table (`row`) to say where a failed check or a signal was: +the row 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 @@ -931,12 +942,12 @@ those fell in, by the same tables: each procedure's samples in its own code, and its total, counting once each sample it was on the chain of. The entry point gives Rt what the program has (`TLink`): the places, -`calls`, which tells the calls under way, and the profiler's start. -What it isn't given, nothing reaches, so the linker leaves it out: -`raz -noprofile` gives no profiler, and `raz -s` none of the three, -whose failures say only their address (and `elf.pas` writes no symbols -or line numbers). Hello is 35 KB, 25 KB without the profiler, and 7 KB -stripped. +`calls`, which tells the calls under way, `place`, which says where an +address is, and the profiler's start. What it isn't given, nothing +reaches, so the linker leaves it out: `raz -noprofile` gives no +profiler, and `raz -s` none of them, whose failures say only their +address (and `elf.pas` writes no symbols or line numbers). Hello is 28 +KB, 21 KB without the profiler, and 7 KB stripped. The layout is chosen so the file can be mapped as it lies, with no padding: text just after the headers, then rodata, then the dynamic @@ -949,8 +960,9 @@ `WriteElf` writes the ELF header, the program headers, the text and rodata images, and then what isn't loaded: section headers, a symbol -table with every live procedure and variable by `Module.name`, and the -line table. +table with every live procedure and variable by `Module.name`, and +the compile unit that names the line table. The line table is in the +places, in rodata; `.debug_line`'s header points there. A program that uses C is linked dynamically, in the least way the loader accepts: an interpreter header naming the loader; a dynamic @@ -961,10 +973,10 @@ `SizeTables` and `PutTables` lay these out in the room `link.pas` left at the end of rodata. -Line numbers are DWARF 4: `PutDebug` writes a line table with one -sequence per procedure, from the notes `NoteLine` made, and the single -compile unit gdb needs to find it. There are no types or variables in -it; gdb can stop at a line and show where it is, not print a variable. +Line numbers are DWARF 4: the line table `MakeLines` made from the +notes `NoteLine` made, and the single compile unit gdb needs to find +it, which `PutDebug` writes. There are no types or variables in it; +gdb can stop at a line and show where it is, not print a variable. ## raz.pas: the command line @@ -1063,7 +1075,8 @@ of its procedure. A generic instance first used in a procedure, or in a module's body, is made before that code begins (`InstancesFor`). - **The executable** carries DWARF line numbers and the names of - procedures for gdb, and `Rt`'s own table of places (`Rt.places`). + procedures for gdb, and the places (`Rt.places`), which hold the + line table, DWARF's, that gdb reads and `Rt` runs. ## Where to start changing it diff --git a/doc/raz.1 b/doc/raz.1 index d29811b..38af755 100644 --- a/doc/raz.1 +++ b/doc/raz.1 @@ -98,7 +98,7 @@ table, its line numbers, the places a failure is told by, and the profiler. A failure then says only its address, and the program is much -smaller: hello, world is 7 KB, against 35 KB. +smaller: hello, world is 7 KB, against 28 KB. .It Fl noprofile Leave out the profiler, which .Ev RAZ_PROFILE diff --git a/lib/Rt.raz b/lib/Rt.raz index dfb0c7c..ec234e0 100644 --- a/lib/Rt.raz +++ b/lib/Rt.raz @@ -3,7 +3,7 @@ failures; and the handlers that say where the program was when a signal stops it. The compiler finds fail, exit, cExit, places, profiler, - startProfile, teller and calls by these names. + startProfile, teller, calls, placer and place by these names. After a failed check or a signal, and on demand with trace, Rt says the calls under way and their variables, from the frames' chain of rbp and the compiler's tables in places. A program run with @@ -13,9 +13,9 @@ procedures it was calling. A program built with raz -s or -noprofile has no profiler: the entry point leaves profiler nil, nothing reaches startProfile, and the linker leaves it out with all - it uses. Built with -s it has no places either, and no teller: a - failure is told by its address, and calls, which would need the - places, is left out the same way. + it uses. Built with -s it has no places either, and no teller or + placer: a failure is told by its address, and calls and place, + which would need the places, are left out the same way. 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; @@ -35,14 +35,18 @@ MaxSamples = 100000; MaxProcs = 8192; MaxPlaces = 65536; + (* the line table's special opcodes, as the compiler makes them *) + LineBase = -5; + LineRange = 14; + OpcodeBase = 13; Depth = 24; (* the callers noted with each sample, at most *) (* 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 names *) - Place = record at, line, col, path: U32; end; + (* A row of the line table: the code from address at on comes from + line and column col of file, numbered from 1 *) + Row = record at: UINT; file, line, col: INT; end; (* A procedure, from address start to stop, called name, and its count variables from first on; it saves the registers of its variables, bits of regs, a quadword each from saveAt in its frame *) @@ -65,10 +69,11 @@ atExits: [MaxAtExit]proc; numAtExit: INT; cExit: proc(status: I32); (* C's exit, set first thing in a program that uses C *) - (* Set first thing too: the numbers of places, where the program's - code starts and stops, the numbers of procedures and variables; - the places, procedures and variables, and the paths and names. - The linker makes them. *) + (* Set first thing too: where the line table is in them, where the + program's code starts and stops, the numbers of procedures and + variables; the procedures and variables, and their names; and + the line table, DWARF's line program, which gdb reads as + .debug_line. The linker makes them. *) places: ^INT; line: [256]U8; (* the message being made *) (* rbx, r12, r13, r14 and r15 in the frame being told of, where the @@ -91,6 +96,8 @@ (* calls, put here by the entry point, or nil: a failure in a program built with raz -s, which has no places, tells no calls *) teller: proc(ip, bp: UINT; vars: BOOL); + (* place, put here by the entry point, or nil, as teller *) + placer: proc(a: UINT); procCount: [MaxProcs]INT; procTotal: [MaxProcs]INT; (* the samples each procedure was on the chain of *) placeCount: [MaxPlaces]INT; @@ -177,12 +184,122 @@ lineLen := 0; end; -(* The paths and names, after the places, procedures and variables *) +(* The names, after the procedures and variables *) proc names: ^U8; begin - return ^U8(places + 5) + 16 * places[0] + 28 * places[3] + 12 * places[4]; + return ^U8(places + 5) + 28 * places[3] + 12 * places[4]; +end; + +(* The line table *) +proc lines: ^U8; +begin + return ^U8(places) + places[0]; end; +(* The number p points at, in LEB128, unsigned, and where the next + thing starts *) +proc uleb(p: ^U8): (INT, ^U8); +var n, shift: INT; +begin + n := 0; + shift := 0; + while p^ >= 80H do + n := n | INT(p^ & 7FH) shl shift; + INC(shift, 7); + p := p + 1; + end; + return n | INT(p^) shl shift, p + 1; +end; + +(* The same, signed *) +proc sleb(p: ^U8): (INT, ^U8); +var n, shift, b: INT; +begin + n := 0; + shift := 0; + repeat + b := INT(p^); + p := p + 1; + n := n | (b & 7FH) shl shift; + INC(shift, 7); + until b < 80H; + if (shift < 64) and (b & 40H # 0) then n := n | (-1 shl shift); end; (* its sign *) + return n, p; +end; + +(* The path of the line table's file f, as a C string: the files' + names follow the table's header of 29 bytes, each ending in a zero + and followed by three numbers *) +proc path(f: INT): ^U8; +var p: ^U8; i, k: INT; +begin + p := lines() + 29; + for i := 2 to f do + while p^ # 0 do p := p + 1; end; + p := p + 1; + for k := 1 to 3 do _, p := uleb(p); end; + end; + return p; +end; + +(* The row of the line table that the code at address a comes from, + the one with the highest address up to a, and its number; or, if + k >= 0, row number k. The number is -1 if there is none. The table + is run as DWARF says, each opcode changing the row being made; the + compiler uses only those below, a row made by copy or by a special + opcode, which adds to the address and the line at once. *) +proc row(a: UINT; k: INT): (Row, INT); +var p, stop: ^U8; q: ^U32; at: ^UINT; r, best: Row; n, found, op, v: INT; +begin + p := lines(); + q := ^U32(p); + stop := p + 4 + INT(q^); + q := ^U32(p + 6); + p := p + 10 + INT(q^); (* the program, past the header *) + r := {file: 1, line: 1}; + n := 0; + found := -1; + while p < stop do + op := INT(p^); + p := p + 1; + if op >= OpcodeBase then + op := op - OpcodeBase; + r.at := r.at + UINT(op div LineRange); + r.line := r.line + LineBase + op mod LineRange; + op := 1; + elsif op = 0 then (* extended: its length, then which *) + v, p := uleb(p); + if p^ = 1 then r := {file: 1, line: 1}; (* end_sequence *) + elsif p^ = 2 then at := ^UINT(p + 1); r.at := at^; (* set_address *) + end; + p := p + v; + elsif op = 2 then v, p := uleb(p); r.at := r.at + UINT(v); (* advance_pc *) + elsif op = 3 then v, p := sleb(p); r.line := r.line + v; (* advance_line *) + elsif op = 4 then r.file, p := uleb(p); (* set_file *) + elsif op = 5 then r.col, p := uleb(p); (* set_column *) + end; + if op = 1 then (* copy, or a special opcode: the row r *) + if n = k then return r, n; end; + if (k < 0) and (r.at <= a) and ((found < 0) or (r.at >= best.at)) then + best := r; + found := n; + end; + INC(n); + end; + end; + return best, found; +end; + +(* Puts where row r comes from, as file:line:col *) +proc putRow(r: Row); +begin + putStr(path(r.file)); + put(":"); + putInt(r.line); + put(":"); + putInt(r.col); +end; + proc putHex(u: UINT); var buf: [16]U8; i: INT; begin @@ -196,33 +313,29 @@ put(buf[i..16]); 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. *) +(* The compiler's, as fail is: 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 row of the line table with the highest address up to a. *) +proc place*(a: UINT); +var r: Row; n: INT; +begin + if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; + r, n := row(a, -1); + if n < 0 then return; end; + putRow(r); + put(": "); +end; + +(* Puts where the code at address a is: place, or, in a program built + with raz -s, which keeps no places, the address *) proc where(a: UINT); -var p, best: ^Place; i, n: INT; begin - if places = nil then - (* built with raz -s, which keeps no places: the address *) + if placer # nil then placer(a); + else put("at "); putHex(a); put(": "); - return; end; - if (a < UINT(places[1])) or (a >= UINT(places[2])) then return; end; - n := places[0]; - p := ^Place(places + 5); - best := nil; - for i := 0 to n - 1 do - if (UINT(p[i].at) <= a) and ((best = nil) or (p[i].at > best^.at)) then best := p + i; end; - end; - if best = nil then return; end; - putStr(names() + INT(best^.path)); - put(":"); - putInt(INT(best^.line)); - put(":"); - putInt(INT(best^.col)); - put(": "); end; (* The frame pointer of the procedure that calls it, which a code @@ -247,7 +360,7 @@ var p: ^Proc; i: INT; begin if (places = nil) or (a < UINT(places[1])) or (a >= UINT(places[2])) then return nil; end; - p := ^Proc(^U8(places + 5) + 16 * places[0]); + p := ^Proc(places + 5); for i := 0 to places[3] - 1 do if (UINT(p[i].start) <= a) and (a < UINT(p[i].stop)) then return p + i; end; end; @@ -386,7 +499,7 @@ putStr(names() + INT(p^.name)); put(" at "); where(ip); - l := ^Local(^U8(places + 5) + 16 * places[0] + 28 * places[3]) + INT(p^.first); + l := ^Local(^U8(places + 5) + 28 * places[3]) + INT(p^.first); if not vars or (p^.count = 0) then DEC(lineLen, 2); end; (* where's ": " *) for i := 0 to INT(p^.count) - 1 do if vars then @@ -553,19 +666,16 @@ begin p := procAt(a); if p = nil then return -1; end; - return p - ^Proc(^U8(places + 5) + 16 * places[0]); + return p - ^Proc(places + 5); end; -(* The index of the place the code at address a comes from, or -1 *) +(* The number of the line table's row the code at address a comes + from, or -1 *) proc placeAt(a: UINT): INT; -var p: ^Place; i, best: INT; +var n: INT; begin - best := -1; - p := ^Place(places + 5); - for i := 0 to places[0] - 1 do - if (UINT(p[i].at) <= a) and ((best < 0) or (p[i].at > p[best].at)) then best := i; end; - end; - return best; + _, n := row(a, -1); + return n; end; (* Puts the share of n that k is, as a percentage four wide *) @@ -581,10 +691,9 @@ (* Says the ten that took most of count's n samples, by name, with their totals beside them if total isn't empty *) proc top(count, total: []INT; n: INT; byPlace: BOOL); -var i, k, best: INT; p: ^Proc; q: ^Place; +var i, k, best: INT; p: ^Proc; r: Row; begin - p := ^Proc(^U8(places + 5) + 16 * places[0]); - q := ^Place(places + 5); + p := ^Proc(places + 5); for k := 1 to 10 do best := 0; for i := 1 to LEN(count) - 1 do @@ -595,9 +704,8 @@ if LEN(total) > 0 then put(" "); putShare(total[best], n); end; put(" "); if byPlace then - putStr(names() + INT(q[best].path)); - put(":"); putInt(INT(q[best].line)); - put(":"); putInt(INT(q[best].col)); + r, _ := row(0, best); + putRow(r); else putStr(names() + INT(p[best].name)); end; say; @@ -658,7 +766,7 @@ top(procCount[0..places[3]], procTotal[0..places[3]], n, false); put("by place:"); say; - top(placeCount[0..places[0]], placeCount[0..0], n, true); + top(placeCount, placeCount[0..0], n, true); end; begin diff --git a/src/elf.pas b/src/elf.pas index 09277a7..79b576f 100644 --- a/src/elf.pas +++ b/src/elf.pas @@ -115,7 +115,7 @@ PutN(0, 1); end; -{ LEB128, unsigned and signed } +{ LEB128, unsigned } procedure PutU(v: qword); begin while v >= $80 do begin @@ -125,79 +125,14 @@ PutN(v, 1); end; -procedure PutS(v: int64); -begin - while (v < -64) or (v >= 64) do begin - PutN(v and $7F or $80, 1); - v := SarInt64(v, 7); - end; - PutN(v and $7F, 1); -end; - -{ The line table: one sequence for each live procedure with lines, - rows made only with the standard opcodes, no special ones. Then the - compile unit, which names the table, and its abbreviation. Section +{ The compile unit, which names the line table, and its abbreviation; + the table itself is in the places, in rodata (link.pas). Section offsets go in off, sizes in size: line, info, abbrev. } procedure PutDebug(var off, size: array of int64); -var - files: array[0..255] of longint; nfiles, i, f, c, cf, cl: longint; - at: int64; cwd: array[0..255] of char; +var cwd: array[0..255] of char; begin - FillChar(files, SizeOf(files), 0); - nfiles := 0; - for i := 0 to numLines - 1 do - if chunks^[lines^[i].chunk].live then begin - f := 0; - while (f < nfiles) and (files[f] <> lines^[i].path) do inc(f); - if (f = nfiles) and (nfiles <= High(files)) then begin - files[f] := lines^[i].path; - inc(nfiles); - end; - end; - - off[0] := len; - PutN(0, 4); { unit_length, patched below } - PutN(4, 2); { version } - PutN(0, 4); { header_length, patched below } - PutN(1, 1); PutN(1, 1); { minimum_instruction_length, maximum_operations } - PutN(1, 1); { default_is_stmt } - PutN(-5 and $FF, 1); PutN(14, 1); PutN(13, 1); { line_base, line_range, opcode_base } - for i := 1 to 12 do PutN(ord(i in [2, 3, 4, 5, 9, 12]), 1); { their operands } - PutN(0, 1); { no include directories } - for f := 0 to nfiles - 1 do begin - PutStr(NameOf(files[f])); - PutN(0, 3); { directory, time, length } - end; - PutN(0, 1); - Patch(off[0] + 6, len - off[0] - 10, 4); - i := 0; - while i < numLines do begin - c := lines^[i].chunk; - if not chunks^[c].live then begin inc(i); continue end; - PutN(0, 1); PutU(9); PutN(2, 1); { DW_LNE_set_address } - PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8); - at := lines^[i].at; cf := 1; cl := 1; - while (i < numLines) and (lines^[i].chunk = c) do begin - if lines^[i].at > at then begin - PutN(2, 1); PutU(lines^[i].at - at); { DW_LNS_advance_pc } - at := lines^[i].at; - end; - f := 1; - while (f <= nfiles) and (files[f - 1] <> lines^[i].path) do inc(f); - if f <> cf then begin PutN(4, 1); PutU(f); cf := f end; { DW_LNS_set_file } - if lines^[i].line <> cl then begin { DW_LNS_advance_line } - PutN(3, 1); PutS(lines^[i].line - cl); - cl := lines^[i].line; - end; - PutN(1, 1); { DW_LNS_copy: a row } - inc(i); - end; - PutN(2, 1); PutU(chunks^[c].start + chunks^[c].size - at); - PutN(0, 1); PutU(1); PutN(1, 1); { DW_LNE_end_sequence } - end; - Patch(off[0], len - off[0] - 4, 4); - size[0] := len - off[0]; - + off[0] := chunks^[placesChunk].adr - roAdr + roOff + linesOff; + size[0] := linesSize; off[1] := len; PutN(0, 4); PutN(4, 2); PutN(0, 4); PutN(8, 1); { length, version, abbrev, address size } PutU(1); @@ -399,7 +334,7 @@ shstrOff := len; s := ShNames; PutBytes(@s[1], length(s)); - if stripped then begin + if stripped or (placesChunk < 0) then begin for c := 0 to 2 do begin dbgOff[c] := len; dbgSize[c] := 0 end; end else PutDebug(dbgOff, dbgSize); diff --git a/src/link.pas b/src/link.pas index 952f201..edf0611 100644 --- a/src/link.pas +++ b/src/link.pas @@ -36,6 +36,7 @@ bssOff: int64; { the file offset of that table } textImg, roImg: PBytes; placesChunk: longint = -1; { the rodata chunk of the places, made here } + linesOff, linesSize: int64; { the line table, in the places: .debug_line } procedure MarkLive(entry: longint); procedure LinkAll(headerSize, roExtra, dynSize: int64); @@ -87,24 +88,116 @@ a := a + chunks^[c].size; end; Place := a; +end; + +{ The line table, DWARF 4's line program, which gdb reads as + .debug_line and Rt to say where code comes from: one sequence for + each live procedure with lines, a row for each place noted, its + address, file, line and column. A row whose address and line move a + little is one byte, a special opcode; the others are made of the + standard ones. Made in lp, lpLen bytes long. } +const + LineBase = -5; LineRange = 14; OpcodeBase = 13; { the special opcodes' } + +var + lp: PBytes; + lpLen, lpCap: longint; + +procedure PutB(v: int64); +begin + if lpLen = lpCap then begin + lpCap := lpCap * 2 + 256; + lp := ReAllocMem(lp, lpCap); + end; + lp^[lpLen] := byte(v); + inc(lpLen); +end; + +procedure PutN(v: int64; n: longint); +var i: longint; +begin + for i := 1 to n do begin PutB(v); v := v shr 8 end; +end; + +{ LEB128, unsigned and signed } +procedure PutU(v: int64); +begin + while v >= $80 do begin PutB(v and $7F or $80); v := v shr 7 end; + PutB(v); +end; + +procedure PutS(v: int64); +begin + while (v < -64) or (v >= 64) do begin PutB(v and $7F or $80); v := SarInt64(v, 7) end; + PutB(v and $7F); end; +{ The line program of the files paths, nfiles of them, numbered from + 1, as they are written in the table } +procedure MakeLines(paths: PLongs; nfiles: longint); +var i, f, c, cf, cl, cc, op: longint; at, da, dl: int64; s: shortstring; +begin + lpLen := 0; + PutN(0, 4); { unit_length, patched below } + PutN(4, 2); { version } + PutN(0, 4); { header_length, patched below } + PutB(1); PutB(1); { minimum_instruction_length, maximum_operations } + PutB(1); { default_is_stmt } + PutB(LineBase); PutB(LineRange); PutB(OpcodeBase); + for i := 1 to OpcodeBase - 1 do PutB(ord(i in [2, 3, 4, 5, 9, 12])); { their operands } + PutB(0); { no include directories } + for f := 0 to nfiles - 1 do begin + s := ShownPath(NameOf(paths^[f])); + for i := 1 to length(s) do PutB(ord(s[i])); + PutN(0, 4); { its end, directory, time, length } + end; + PutB(0); + PLongint(@lp^[6])^ := lpLen - 10; + i := 0; + while i < numLines do begin + c := lines^[i].chunk; + if not chunks^[c].live then begin inc(i); continue end; + PutB(0); PutU(9); PutB(2); { DW_LNE_set_address } + PutN(chunks^[c].adr + lines^[i].at - chunks^[c].start, 8); + at := lines^[i].at; cf := 1; cl := 1; cc := 0; + while (i < numLines) and (lines^[i].chunk = c) do begin + f := 1; + while paths^[f - 1] <> lines^[i].path do inc(f); + if f <> cf then begin PutB(4); PutU(f); cf := f end; { DW_LNS_set_file } + if lines^[i].col <> cc then begin PutB(5); PutU(lines^[i].col); cc := lines^[i].col end; { DW_LNS_set_column } + da := lines^[i].at - at; + dl := lines^[i].line - cl; + op := (dl - LineBase) + LineRange * da + OpcodeBase; + if (dl >= LineBase) and (dl < LineBase + LineRange) and (op <= 255) then PutB(op) { a special opcode: a row } + else begin + if da > 0 then begin PutB(2); PutU(da) end; { DW_LNS_advance_pc } + if dl <> 0 then begin PutB(3); PutS(dl) end; { DW_LNS_advance_line } + PutB(1); { DW_LNS_copy: a row } + end; + at := lines^[i].at; cl := lines^[i].line; + inc(i); + end; + PutB(2); PutU(chunks^[c].start + chunks^[c].size - at); + PutB(0); PutU(1); PutB(1); { DW_LNE_end_sequence } + end; + PLongint(@lp^[0])^ := lpLen - 4; +end; + { The places that Rt reads to say where a failed check or a signal - was, and what calls were under way: their numbers, the places', where - text starts and stops, the procedures' and their variables' (I64s); - for each place in live code its address, line, column and the offset - of its file's path (U32s); for each live procedure where it starts and - stops, the offset of its name, its first variable and how many it - has, and the registers of variables it saves, as bits of gen's - VarRegs, and the frame offset they are saved at (U32s); for each - variable the offset of its name, its place in the frame and its - kind, as parse.pas's VarKind (U32s); then the paths and names, each - ending in a zero byte. They are in no order, since Rt - looks at them only as the program stops. } + was, and what calls were under way: where the line table is in them, + where text starts and stops, the numbers of procedures and of their + variables (I64s); for each live procedure where it starts and stops, + the offset of its name, its first variable and how many it has, and + the registers of variables it saves, as bits of gen's VarRegs, and + the frame offset they are saved at (U32s); for each variable the + offset of its name, its place in the frame and its kind, as + parse.pas's VarKind (U32s); then the names, each ending in a zero + byte; then the line table (MakeLines), at linesOff. They are in no + order, since Rt looks at them only as the program stops. } procedure MakePlaces(start, stop: int64); var - files, offs, first, count: PLongs; buf: PBytes; - i, f, n, nfiles, k, np, nv, c, v, at, names: longint; + files, first, count: PLongs; buf: PBytes; + i, f, nfiles, k, np, nv, c, v, at, names: longint; procedure Name(const s: shortstring); begin @@ -117,7 +210,6 @@ begin files := GetMem(numLines * 4 + 4); - offs := GetMem(numLines * 4 + 4); first := GetMem(numChunks * 4); count := GetMem(numChunks * 4); FillChar(count^, numChunks * 4, 0); @@ -125,19 +217,17 @@ first^[varNotes[v].chunk] := v; inc(count^[varNotes[v].chunk]); end; - n := 0; nfiles := 0; k := 0; np := 0; nv := 0; + nfiles := 0; k := 0; np := 0; nv := 0; for i := 0 to numLines - 1 do if chunks^[lines^[i].chunk].live then begin - inc(n); 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; + MakeLines(files, nfiles); for c := 0 to numChunks - 1 do if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin inc(np); @@ -145,34 +235,11 @@ for v := first^[c] to first^[c] + count^[c] - 1 do k := k + length(NameOf(varNotes[v].name)) + 1; nv := nv + count^[c]; end; - buf := GetMem(40 + 16 * n + 28 * np + 12 * nv + k); - PInt64(@buf^[0])^ := n; - PInt64(@buf^[8])^ := start; - PInt64(@buf^[16])^ := stop; - PInt64(@buf^[24])^ := np; - PInt64(@buf^[32])^ := nv; - at := 40; - for i := 0 to numLines - 1 do - if chunks^[lines^[i].chunk].live then begin - f := 0; - while files^[f] <> lines^[i].path do inc(f); - PLongint(@buf^[at])^ := chunks^[lines^[i].chunk].adr + lines^[i].at - chunks^[lines^[i].chunk].start; - PLongint(@buf^[at + 4])^ := lines^[i].line; - PLongint(@buf^[at + 8])^ := lines^[i].col; - PLongint(@buf^[at + 12])^ := offs^[f]; - at := at + 16; - end; - { the pool of paths and names starts at k } - k := 40 + 16 * n + 28 * np + 12 * nv; + buf := GetMem(40 + 28 * np + 12 * nv + k + lpLen); + { the pool of names starts at k, the line table after it } + k := 40 + 28 * np + 12 * nv; names := k; - { the paths first, at the offsets offs gave the places: Name puts - each in the pool, and also writes its offset at at, which a path - doesn't want, so at is put back for the procedures to overwrite } - for f := 0 to nfiles - 1 do begin - i := at; - Name(ShownPath(NameOf(files^[f]))); - at := i; - end; + at := 40; v := 0; for c := 0 to numChunks - 1 do if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module >= 0) then begin @@ -195,8 +262,16 @@ PLongint(@buf^[at + 4])^ := varNotes[v].kind; at := at + 8; end; - SetData(placesChunk, buf, names, 8); - FreeMem(buf); FreeMem(files); FreeMem(offs); FreeMem(first); FreeMem(count); + linesOff := names; + linesSize := lpLen; + Move(lp^, buf^[names], lpLen); + PInt64(@buf^[0])^ := linesOff; + PInt64(@buf^[8])^ := start; + PInt64(@buf^[16])^ := stop; + PInt64(@buf^[24])^ := np; + PInt64(@buf^[32])^ := nv; + SetData(placesChunk, buf, names + lpLen, 8); + FreeMem(buf); FreeMem(files); FreeMem(first); FreeMem(count); end; { Places the live chunks, leaving roExtra bytes at the end of rodata diff --git a/src/raz.pas b/src/raz.pas index 3959c29..4290b35 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -115,7 +115,7 @@ m, rt: PModule; main: PObj; run, noProfile: boolean; - links: array[0..2] of TLink; + links: array[0..3] of TLink; numLinks: longint; @@ -204,15 +204,16 @@ if rt = nil then Fatal('can''t find module Rt'); { what Rt is given: the places, which the linker makes, for Rt to say - where a failed check or a signal was, and the calls under way then - (calls); and the profiler, which needs them too. raz -s gives none, - -noprofile no profiler. } + where a failed check or a signal was (place), and the calls under + way then (calls); and the profiler, which needs them too. raz -s + gives none, -noprofile no profiler. } numLinks := 0; if not stripped then begin placesChunk := NewChunk(secRodata, -1, -1); SetData(placesChunk, nil, 0, 8); AddLink(placesChunk, RtObj(rt, 'places')); AddLink(RtObj(rt, 'calls')^.chunk, RtObj(rt, 'teller')); + AddLink(RtObj(rt, 'place')^.chunk, RtObj(rt, 'placer')); if not noProfile then AddLink(RtObj(rt, 'startProfile')^.chunk, RtObj(rt, 'profiler')); end; entry := EntryPoint(UsesC, firstModule, main, RtObj(rt, 'cExit')^.chunk,