From 1d90e30fc9d40fe7ddb960d62dfb01958fbbcca5 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:07:23 +0100 Subject: [PATCH] Step 9c: raz -S With listing set (-S), each encoder routine says the instruction it is about to write, as the assembler would read it (Say: registers by size, memory as [base + index * scale + disp], chunks by Module.name); the assembler says each instruction as written. List prints the main module's live procedures: each line of source (diag's LineText), and under it the instructions at their offsets; a jump or call within the procedure says where to by its offset. The prologue's sub rsp is restated once EndProc knows the frame. -S writes no executable. The text is built only when listing: bench 949k lines/s. test/list/Listing; check.sh compares raz -S with X.expect. --- diff --git a/PLAN.md b/PLAN.md index e737a49..33b4e8a 100644 --- a/PLAN.md +++ b/PLAN.md @@ -150,7 +150,7 @@ - [x] 9b. Assembly procedures in place of byte procedures: the target's own syntax (appendix A), for the instructions the encoder knows and a few of the system's, bytes for the rest. -- [ ] 9c. `raz -S`: each line of source with its instructions, named +- [x] 9c. `raz -S`: each line of source with its instructions, named by the encoder as it makes them, not by a disassembler. - [ ] 9d. The trap report: on a failed check or a trap, the calls under way, each with its variables by name and value, from diff --git a/README.md b/README.md index 7328ff9..65e6dc2 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ ## Using it - raz [-I dir]... [-o file] [-Werror] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] Main.raz raz run [-I dir]... [-Werror] Main.raz [arg]... raz --version @@ -86,7 +86,9 @@ declared, a name spelled almost alike. The compiler goes on to find the others, up to 20. It warns of what is declared and never used, and of a variable assigned but never read, unless its name starts with -`_`; `-Werror` makes warnings errors. +`_`; `-Werror` makes warnings errors. `-S` prints the main module's +code instead of writing it: each line of source, and under it the +instructions made for it. At run time, a failed check says where it is and what failed, as in `Main.raz:12:9: index 7 is out of range for a length of 5`, and ends diff --git a/doc/compiler.md b/doc/compiler.md index a642271..42e5fc2 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -8,17 +8,17 @@ | File | Lines | Code | What | |---|---|---|---| -| `src/diag.pas` | 274 | 198 | Messages: positions, source excerpts, colour, counts | +| `src/diag.pas` | 296 | 218 | Messages: positions, source excerpts, colour, counts | | `src/scan.pas` | 851 | 722 | Symbols, literals, comments, positions | | `src/sym.pas` | 596 | 497 | Types, objects, scopes, layout | | `src/chunk.pas` | 244 | 192 | The compiled program: chunks, fixups, line notes | -| `src/x64.pas` | 834 | 694 | The instruction encoder, jumps within a procedure, the assembler | -| `src/gen.pas` | 1,319 | 1,037 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/x64.pas` | 1,039 | 877 | The instruction encoder, jumps within a procedure, the assembler | +| `src/gen.pas` | 1,321 | 1,039 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/item.pas` | 1,318 | 1,100 | Items: types, constants, variables, operators, conversions, calls | | `src/parse.pas` | 2,529 | 2,267 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/link.pas` | 180 | 139 | Tree shaking, layout, patching, the places | | `src/elf.pas` | 424 | 351 | Static and dynamic ELF, symbol table, line numbers | -| `src/raz.pas` | 188 | 141 | The command line | +| `src/raz.pas` | 195 | 147 | 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. @@ -243,6 +243,13 @@ `db` gives bytes it doesn't know. `RegNames` and `CondNames` are the names of the registers and conditions. +The listing, `raz -S`: with `listing` set, each routine of the encoder +says, with `Say`, the instruction it is about to write, as the +assembler would read it; `List` prints the main module's live +procedures with each line of source (`LineText` in `diag.pas`) and the +instructions under it. Nothing is disassembled: the listing is what +the encoder meant. + Jumps within a procedure don't wait for the linker. A label not yet placed is a chain of the jumps to it, each jump's 32-bit field holding the position of the one before (`Jmp`, `Jcc`); `FixLink` walks the @@ -540,7 +547,8 @@ start-up does, which sets C up and calls back a `main` that runs the module bodies and the program's `main`. -`raz run` writes the executable to a memfd, a file only in memory, and +`raz -S` lists the code (`List`, in `x64.pas`) instead of writing +it. `raz run` writes the executable to a memfd, a file only in memory, and executes it through `/proc/self/fd` in raz's own place, handing it the rest of raz's `argv`. Nothing is left on disk, and the program's status is the command's. diff --git a/src/diag.pas b/src/diag.pas index 858fc01..0dbd882 100644 --- a/src/diag.pas +++ b/src/diag.pas @@ -42,6 +42,7 @@ function LineOf(const p: TPos): longint; function Join(const p, q: TPos): TPos; function Source(const p: TPos): shortstring; +function LineText(const path: shortstring; line: longint): shortstring; procedure Error(const p: TPos; const msg: shortstring); procedure Warning(const p: TPos; const msg: shortstring); procedure Note(const p: TPos; const msg: shortstring); @@ -210,6 +211,27 @@ end; { Messages } + +{ Line line of the file at path, as written } +function LineText(const path: shortstring; line: longint): shortstring; +var i, k, n: longint; s: shortstring; +begin + s := ''; + for i := 0 to numFiles - 1 do + if files[i].path = ShownPath(path) then begin + k := 0; n := 1; + while (n < line) and (k < files[i].len) do begin + if files[i].buf[k] = #10 then inc(n); + inc(k); + end; + while (k < files[i].len) and (files[i].buf[k] <> #10) and (length(s) < 255) do begin + s := s + files[i].buf[k]; + inc(k); + end; + break; + end; + LineText := s; +end; procedure Error(const p: TPos; const msg: shortstring); begin diff --git a/src/gen.pas b/src/gen.pas index 4b3476e..5ee96c3 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -1139,6 +1139,7 @@ NoteLine(line, 1); { a declaration starts a line } Push(RBP); MovRR(RBP, RSP); + Say('sub rsp, frame'); Put1($48); Put1($81); Put1($EC); { sub rsp, imm32 } frameFix := pc; Put4(0); n := (AlignUp(frameMax, 8) - paramTop) div 8; @@ -1217,6 +1218,7 @@ if n < 16 then n := 16; if n > high(longint) then Mark('frame too big'); Put4At(frameFix, n); + Restate(frameFix - 3, 'sub rsp, ' + IntStr(n)); EndChunk(c); end; diff --git a/src/raz.pas b/src/raz.pas index 4efe0e2..6ad444e 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -1,12 +1,13 @@ { raz.pas: the command line. - raz [-I dir]... [-o file] Main.raz + raz [-I dir]... [-o file] [-Werror] [-S] Main.raz raz run [-I dir]... Main.raz [arg]... raz --version Compiles the main module and every module it imports, writes the entry point, links what it reaches, and writes the executable: by - default the main module's file without its .raz. raz run writes it + default the main module's file without its .raz; with -S, it lists + the main module's code instead. raz run writes it to a memfd instead, a file that lives only in memory, and executes it in raz's place with the arguments after Main.raz, so its status is the program's; its argv[0] is Main.raz. @@ -25,7 +26,7 @@ {$mode objfpc}{$H-} -uses math, BaseUnix, syscall, scan, diag, sym, chunk, gen, parse, link, elf; +uses math, BaseUnix, syscall, scan, diag, sym, chunk, x64, gen, parse, link, elf; const { what git describe says, made by make: 0.3.0 at the tag v0.3.0, @@ -115,6 +116,8 @@ outPath := ParamStr(i); end else if s = '-Werror' then werror := true + else if s = '-S' then + listing := true else if s = '--version' then begin writeln('raz ', Version, ', report ', Report); Halt(0); @@ -128,7 +131,7 @@ inc(i); end; if path = '' then - Fatal('usage: raz [-I dir]... [-o file] [-Werror] Main.raz, or raz run [options] Main.raz [arg]...'); + Fatal('usage: raz [-I dir]... [-o file] [-Werror] [-S] Main.raz, or raz run [options] Main.raz [arg]...'); if run and (outPath <> '') then Fatal('raz run takes no -o'); dir := ParamStr(0); i := length(dir); @@ -172,6 +175,10 @@ if chunks^[i].live and chunks^[i].reals then Error(chunks^[i].realsAt, 'this target has no reals'); if errors > 0 then Finish; + if listing then begin + List(m^.sname); + exit; + end; mainPath := path; if run then begin diff --git a/src/x64.pas b/src/x64.pas index 4a34e27..80ad9dc 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -45,6 +45,7 @@ var labelAt: longint; { the last place a jump was made to land } + listing: boolean; { -S: each instruction's text is kept, for List } function Here: TMem; function Mem(base: longint; disp: int64): TMem; @@ -120,6 +121,9 @@ 'be', 'a', 's', 'ns', 'p', 'np', 'l', 'ge', 'le', 'g'); procedure Assemble(constExpr: TConstFn); +procedure Say(const s: shortstring); +procedure Restate(at: longint; const s: shortstring); +procedure List(module: longint); implementation @@ -216,15 +220,107 @@ Prefix(pfx, w, reg, NoIndex, rm, byteReg and ((reg in [4..7]) or (rm in [4..7]))); Opcode(op); Put1($C0 or (reg and 7) shl 3 or (rm and 7)); +end; + +{ The listing: the text of each instruction, as it is made } + +type + TSaid = record + at: longint; { where the instruction starts in code } + text: string[63]; + end; + +var + said: ^TSaid; + numSaid, saidCap: longint; + +procedure SayAt(at: longint; const s: shortstring); +begin + if not listing then exit; + if numSaid = saidCap then begin + saidCap := saidCap * 2 + 1024; + said := ReAllocMem(said, saidCap * SizeOf(TSaid)); + end; + said[numSaid].at := at; + said[numSaid].text := s; + inc(numSaid); end; +{ The text of the instruction about to be written } +procedure Say(const s: shortstring); +begin + SayAt(pc, s); +end; + +{ The text of the instruction at at, said again, as when its operand is + patched } +procedure Restate(at: longint; const s: shortstring); +var i: longint; +begin + i := numSaid - 1; + while (i >= 0) and (said[i].at <> at) do dec(i); + if i >= 0 then said[i].text := s; +end; + +function RN(r: longint; size: longint = 8): shortstring; +begin + case size of + 1: RN := RegNames[3, r]; + 2: RN := RegNames[2, r]; + 4: RN := RegNames[1, r]; + else RN := RegNames[0, r]; + end; +end; + +function XN(x: longint): shortstring; +begin + XN := 'xmm' + IntStr(x); +end; + +function ChunkName(c: longint): shortstring; +begin + with chunks^[c] do + if name < 0 then ChunkName := 'data' + IntStr(c) + else if module < 0 then ChunkName := NameOf(name) + else ChunkName := NameOf(module) + '.' + NameOf(name); +end; + +{ A memory operand, with the size of what it holds, as the assembler + reads it } +function MN(const m: TMem; size: longint = 0): shortstring; +var s: shortstring; +begin + case size of + 1: s := 'byte '; + 2: s := 'word '; + 4: s := 'dword '; + 8: s := 'qword '; + else s := ''; + end; + if m.base = RIP then s := s + '[' + ChunkName(m.chunk) + else s := s + '[' + RN(m.base); + if m.index <> NoIndex then s := s + ' + ' + RN(m.index) + ' * ' + IntStr(m.scale); + if m.disp > 0 then s := s + ' + ' + IntStr(m.disp) + else if m.disp < 0 then s := s + ' - ' + IntStr(-m.disp); + MN := s + ']'; +end; + +const + AluNames: array[0..7] of string[3] = ('add', 'or', 'adc', 'sbb', 'and', 'sub', 'xor', 'cmp'); + ShiftNames: array[0..7] of string[3] = ('rol', 'ror', 'rcl', 'rcr', 'shl', 'shr', 'sal', 'sar'); + UnaryNames: array[2..7] of string[4] = ('not', 'neg', 'mul', 'imul', 'div', 'idiv'); + procedure MovRR(dst, src: longint); begin + if listing then Say('mov ' + RN(dst) + ', ' + RN(src)); OpR(0, true, $89, src, dst); end; procedure MovRI(r: longint; v: int64); begin + if not listing then + else if v = 0 then Say('xor ' + RN(r, 4) + ', ' + RN(r, 4)) + else Say('mov ' + RN(r) + ', ' + IntStr(v)); if (v >= low(longint)) and (v <= high(longint)) then begin if v = 0 then OpR(0, false, $31, r, r) { xor r32, r32 } else if v > 0 then begin { mov r32, imm32 } @@ -241,6 +337,12 @@ procedure Load(r: longint; const m: TMem; size: longint; signed: boolean); begin + if not listing then + else if size = 8 then Say('mov ' + RN(r) + ', ' + MN(m)) + else if (size = 4) and not signed then Say('mov ' + RN(r, 4) + ', ' + MN(m)) + else if size = 4 then Say('movsxd ' + RN(r) + ', ' + MN(m, 4)) + else if signed then Say('movsx ' + RN(r) + ', ' + MN(m, size)) + else Say('movzx ' + RN(r, 4) + ', ' + MN(m, size)); case size of 1: if signed then OpM(0, true, $BE0F, r, m) else OpM(0, false, $B60F, r, m); 2: if signed then OpM(0, true, $BF0F, r, m) else OpM(0, false, $B70F, r, m); @@ -251,6 +353,7 @@ procedure Store(const m: TMem; r: longint; size: longint); begin + if listing then Say('mov ' + MN(m) + ', ' + RN(r, size)); case size of 1: OpM(0, false, $88, r, m, true); 2: OpM($66, false, $89, r, m); @@ -261,16 +364,19 @@ procedure Lea(r: longint; const m: TMem); begin + if listing then Say('lea ' + RN(r) + ', ' + MN(m)); OpM(0, true, $8D, r, m); end; procedure Alu(op, dst, src: longint); begin + if listing then Say(AluNames[op] + ' ' + RN(dst) + ', ' + RN(src)); OpR(0, true, op shl 3 or 1, src, dst); end; procedure AluI(op, r: longint; v: longint); begin + if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + IntStr(v)); if (v >= -128) and (v <= 127) then begin OpR(0, true, $83, op, r); Put1(v) end else begin @@ -281,44 +387,56 @@ { op r, [m] } procedure AluM(op, r: longint; const m: TMem); begin + if listing then Say(AluNames[op] + ' ' + RN(r) + ', ' + MN(m)); OpM(0, true, op shl 3 or 3, r, m); end; procedure Imul(dst, src: longint); begin + if listing then Say('imul ' + RN(dst) + ', ' + RN(src)); OpR(0, true, $AF0F, dst, src); end; procedure ImulI(r: longint; v: longint); begin + if listing then Say('imul ' + RN(r) + ', ' + RN(r) + ', ' + IntStr(v)); OpR(0, true, $69, r, r); Put4(v); end; procedure Shift(op, r: longint); begin + if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', cl'); OpR(0, true, $D3, op, r); end; procedure ShiftI(op, r, n: longint); begin + if listing then Say(ShiftNames[op] + ' ' + RN(r) + ', ' + IntStr(n)); OpR(0, true, $C1, op, r); Put1(n); end; procedure Unary(ext, r: longint); begin + if listing then Say(UnaryNames[ext] + ' ' + RN(r)); OpR(0, true, $F7, ext, r); end; procedure Cqo; begin + if listing then Say('cqo'); Put1($48); Put1($99); end; { Brings a value of size bytes in r to 64 bits, by its sign or zeros. } procedure Extend(r, size: longint; signed: boolean); begin + if not listing then + else if size = 4 then begin + if signed then Say('movsxd ' + RN(r) + ', ' + RN(r, 4)) else Say('mov ' + RN(r, 4) + ', ' + RN(r, 4)) + end else if signed then Say('movsx ' + RN(r) + ', ' + RN(r, size)) + else Say('movzx ' + RN(r, 4) + ', ' + RN(r, size)); case size of 1: if signed then OpR(0, true, $BE0F, r, r) else OpR(0, false, $B60F, r, r); 2: if signed then OpR(0, true, $BF0F, r, r) else OpR(0, false, $B70F, r, r); @@ -329,29 +447,35 @@ { r := 1 if the condition holds, else 0 } procedure SetCC(cc, r: longint); begin + if listing then Say('set' + CondNames[cc] + ' ' + RN(r, 1)); OpR(0, false, $900F + cc shl 8, 0, r); + if listing then Say('movzx ' + RN(r, 4) + ', ' + RN(r, 1)); OpR(0, false, $B60F, r, r); end; procedure TestRR(a, b: longint); begin + if listing then Say('test ' + RN(a) + ', ' + RN(b)); OpR(0, true, $85, b, a); end; procedure Push(r: longint); begin + if listing then Say('push ' + RN(r)); if r >= 8 then Put1($41); Put1($50 + r and 7); end; procedure Pop(r: longint); begin + if listing then Say('pop ' + RN(r)); if r >= 8 then Put1($41); Put1($58 + r and 7); end; procedure CallChunk(c: longint); begin + if listing then Say('call ' + ChunkName(c)); Put1($E8); AddFixup(pc, c, 0); Put4(0); @@ -359,91 +483,117 @@ procedure CallBack(target: longint); begin + if listing then Say('call'); Put1($E8); Put4(target - (pc + 4)); end; procedure CallR(r: longint); begin + if listing then Say('call ' + RN(r)); OpR(0, false, $FF, 2, r); end; procedure CallM(const m: TMem); begin + if listing then Say('call ' + MN(m)); OpM(0, false, $FF, 2, m); end; -procedure Ret; begin Put1($C3) end; -procedure Leave; begin Put1($C9) end; -procedure Syscall; begin Put1($0F); Put1($05) end; -procedure Mfence; begin Put1($0F); Put1($AE); Put1($F0) end; -procedure Ud2; begin Put1($0F); Put1($0B) end; -procedure RepMovsb; begin Put1($F3); Put1($A4) end; -procedure RepStosb; begin Put1($F3); Put1($AA) end; -procedure RepeCmpsb; begin Put1($F3); Put1($A6) end; +procedure Ret; begin if listing then Say('ret'); Put1($C3) end; +procedure Leave; begin Say('leave'); Put1($C9) end; +procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end; +procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; +procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end; +procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; +procedure RepStosb; begin Say('rep stosb'); Put1($F3); Put1($AA) end; +procedure RepeCmpsb; begin Say('repe cmpsb'); Put1($F3); Put1($A6) end; procedure LockCmpxchg(const m: TMem; r: longint); begin + if listing then Say('lock cmpxchg ' + MN(m) + ', ' + RN(r)); Put1($F0); OpM(0, true, $B10F, r, m); end; procedure Xchg(const m: TMem; r: longint); begin + if listing then Say('xchg ' + MN(m) + ', ' + RN(r)); OpM(0, true, $87, r, m); end; procedure LockXadd(const m: TMem; r: longint); begin + if listing then Say('lock xadd ' + MN(m) + ', ' + RN(r)); Put1($F0); OpM(0, true, $C10F, r, m); end; procedure MovqXR(x, r: longint); begin + if listing then Say('movq ' + XN(x) + ', ' + RN(r)); OpR($66, true, $6E0F, x, r); end; procedure MovqRX(r, x: longint); begin + if listing then Say('movq ' + RN(r) + ', ' + XN(x)); OpR($66, true, $7E0F, x, r); end; procedure MovsdXM(x: longint; const m: TMem); begin + if listing then Say('movsd ' + XN(x) + ', ' + MN(m)); OpM($F2, false, $100F, x, m); end; procedure MovsdMX(const m: TMem; x: longint); begin + if listing then Say('movsd ' + MN(m) + ', ' + XN(x)); OpM($F2, false, $110F, x, m); end; const RealPfx: array[boolean] of longint = ($F2, $F3); + RealEnd: array[boolean] of string[2] = ('sd', 'ss'); procedure Sse(op, x, y: longint; single: boolean); +var s: shortstring; begin + if listing then begin + case op of + $58: s := 'add' + RealEnd[single]; + $59: s := 'mul' + RealEnd[single]; + $5C: s := 'sub' + RealEnd[single]; + $5E: s := 'div' + RealEnd[single]; + else s := 'cvt' + RealEnd[single] + '2' + RealEnd[not single]; + end; + Say(s + ' ' + XN(x) + ', ' + XN(y)); + end; OpR(RealPfx[single], false, $0F or op shl 8, x, y); end; procedure Ucomis(x, y: longint; single: boolean); begin + if listing then Say('ucomi' + RealEnd[single] + ' ' + XN(x) + ', ' + XN(y)); if single then OpR(0, false, $2E0F, x, y) else OpR($66, false, $2E0F, x, y); end; procedure Cvtsi2s(x, r: longint; single: boolean); begin + if listing then Say('cvtsi2' + RealEnd[single] + ' ' + XN(x) + ', ' + RN(r)); OpR(RealPfx[single], true, $2A0F, x, r); end; procedure Cvtts2si(r, x: longint; single: boolean); begin + if listing then Say('cvtt' + RealEnd[single] + '2si ' + RN(r) + ', ' + XN(x)); OpR(RealPfx[single], true, $2C0F, r, x); end; procedure Btc(r, bit: longint); begin + if listing then Say('btc ' + RN(r) + ', ' + IntStr(bit)); OpR(0, true, $BA0F, 7, r); Put1(bit); end; @@ -454,6 +604,7 @@ procedure Jmp(var L: longint); begin + if listing then Say('jmp'); Put1($E9); Put4(L); L := pc - 4; @@ -461,6 +612,7 @@ procedure Jcc(cc: longint; var L: longint); begin + if listing then Say('j' + CondNames[cc]); Put1($0F); Put1($80 + cc); Put4(L); L := pc - 4; @@ -468,12 +620,14 @@ procedure JmpBack(target: longint); begin + if listing then Say('jmp'); Put1($E9); Put4(target - (pc + 4)); end; procedure JccBack(cc, target: longint); begin + if listing then Say('j' + CondNames[cc]); Put1($0F); Put1($80 + cc); Put4(target - (pc + 4)); end; @@ -635,7 +789,7 @@ $90F3, $90, $0B0F, $CC, $F4, $9948, $99, $F0AE0F, $E8AE0F, $F8AE0F); var w: shortstring; o: array[0..2] of TOpd; n, i, k, size, pfx, cc: longint; - wide, byteOp: boolean; at, start: TPos; v: int64; + wide, byteOp, wasListing: boolean; at, start: TPos; v: int64; first: longint; names, places, chains: array[0..63] of longint; numLabels: longint; usedAt: array[0..63] of TPos; @@ -687,6 +841,7 @@ if w = '' then Mark('expected an instruction'); Next; if tok = sColon then begin + SayAt(pc, w + ':'); k := Lab(Intern(@w[1], length(w))); if places[k] >= 0 then MarkAt(at, 'label ' + w + ' is defined twice'); places[k] := pc; @@ -694,6 +849,9 @@ Next; continue; end; + first := pc; + wasListing := listing; + listing := false; { the instruction is said as it is written } if w = 'lock' then begin Put1($F0); w := AsmWord; @@ -825,10 +983,57 @@ end; end; if tok <> sSemicolon then Mark('expected ";"'); + listing := wasListing; + SayAt(first, Source(SpanFrom(at))); Next; end; for k := 0 to numLabels - 1 do if places[k] < 0 then MarkAt(usedAt[k], 'label ' + NameOf(names[k]) + ' isn''t defined'); +end; + +{ The listing, raz -S: each live procedure of the module in the order + it was made, each line of source it comes from, and under it the + instructions made for it, at their offsets in the procedure; a jump + within it says where to by its offset too. } +procedure List(module: longint); +var order: ^longint; n, i, j, k, c, t, line: longint; text: shortstring; +begin + order := GetMem(numChunks * SizeOf(longint)); + n := 0; + for c := 0 to numChunks - 1 do + if chunks^[c].live and (chunks^[c].sec = secText) and (chunks^[c].module = module) then begin + i := n; + while (i > 0) and (chunks^[order[i - 1]].start > chunks^[c].start) do begin + order[i] := order[i - 1]; + dec(i); + end; + order[i] := c; + inc(n); + end; + i := 0; j := 0; + for k := 0 to n - 1 do begin + c := order[k]; + writeln; + writeln(ChunkName(c), ':'); + line := -1; + while (i < numSaid) and (said[i].at < chunks^[c].start) do inc(i); + while (i < numSaid) and (said[i].at < chunks^[c].start + chunks^[c].size) do begin + while (j < numLines) and (lines^[j].at <= said[i].at) do begin + if (lines^[j].chunk = c) and (lines^[j].line <> line) then begin + line := lines^[j].line; + writeln(line: 6, ' ', LineText(NameOf(lines^[j].path), line)); + end; + inc(j); + end; + text := said[i].text; + t := said[i].at; + if (text = 'jmp') or (text = 'call') then text := text + ' ' + IntStr(t + 5 + Get4At(t + 1) - chunks^[c].start) + else if (text[1] = 'j') and (pos(' ', text) = 0) then text := text + ' ' + IntStr(t + 6 + Get4At(t + 2) - chunks^[c].start); + writeln(t - chunks^[c].start: 14, ' ', text); + inc(i); + end; + end; + FreeMem(order); end; end. diff --git a/test/check.sh b/test/check.sh index f2bf121..00467d7 100755 --- a/test/check.sh +++ b/test/check.sh @@ -92,6 +92,12 @@ pass=$((pass + 1)) fi done +# raz -S: test/list/X.raz lists as X.expect +for f in test/list/*.raz; do + want=test/list/$(basename $f .raz).expect + if $raz -S $f 2>&1 | cmp -s $want -; then pass=$((pass + 1)) + else echo "FAIL raz -S $f"; $raz -S $f 2>&1 | diff $want - | head; fail=$((fail + 1)); fi +done # the report's version is the one the compiler says it follows # the version the compiler follows: the report's, or one it names while # the compiler catches up diff --git a/test/list/Listing.expect b/test/list/Listing.expect new file mode 100644 index 0000000..c3e2a80 --- /dev/null +++ b/test/list/Listing.expect @@ -0,0 +1,73 @@ + +Listing.twice: + 0 lea rax, [rdi + rdi] + 4 ret + +Listing.add: + 12 proc add(a, b: I64): I64; + 0 push rbp + 1 mov rbp, rsp + 4 sub rsp, 16 + 11 mov [rbp - 8], rdi + 15 mov [rbp - 16], rsi + 14 return a + b * 2; + 19 mov r8, [rbp - 8] + 23 mov r9, [rbp - 16] + 27 shl r9, 1 + 31 add r8, r9 + 34 mov rax, r8 + 37 jmp 49 + 15 end add; + 42 xor eax, eax + 44 movq xmm0, rax + 49 leave + 50 ret + +Listing.main: + 17 proc main*; + 0 push rbp + 1 mov rbp, rsp + 4 sub rsp, 48 + 11 xor eax, eax + 13 mov [rbp - 8], rax + 20 for i := 1 to 3 do + 17 mov rax, 1 + 22 mov [rbp - 8], rax + 26 mov rax, 3 + 31 mov [rbp - 16], rax + 35 mov r8, [rbp - 8] + 39 mov r9, [rbp - 16] + 43 cmp r8, r9 + 46 jg 173 + 21 if i # 2 then total := add(total, twice(i)); end; + 52 mov r8, [rbp - 8] + 56 cmp r8, 2 + 60 setne r8b + 64 movzx r8d, r8b + 68 test r8, r8 + 71 je 135 + 77 mov r8, [Listing.total] + 84 mov [rbp - 24], r8 + 88 mov r8, [rbp - 8] + 92 mov [rbp - 32], r8 + 96 mov rdi, [rbp - 32] + 100 call Listing.twice + 105 mov r8, rax + 108 mov [rbp - 40], r8 + 112 mov rdi, [rbp - 24] + 116 mov rsi, [rbp - 40] + 120 call Listing.add + 125 mov r8, rax + 128 mov [Listing.total], r8 + 135 mov r8, [rbp - 16] + 139 mov r9, [rbp - 8] + 143 sub r8, r9 + 146 cmp r8, 1 + 150 jb 173 + 156 mov r8, [rbp - 8] + 160 add r8, 1 + 164 mov [rbp - 8], r8 + 168 jmp 35 + 23 end main; + 173 leave + 174 ret diff --git a/test/list/Listing.raz b/test/list/Listing.raz new file mode 100644 index 0000000..a384a11 --- /dev/null +++ b/test/list/Listing.raz @@ -0,0 +1,25 @@ +module Listing; +(* raz -S: each line of source, and the instructions made for it *) + +var total: I64; + +proc twice(x: I64): I64; +code + lea rax, [rdi + rdi]; + ret; +end twice; + +proc add(a, b: I64): I64; +begin + return a + b * 2; +end add; + +proc main*; +var i: I64; +begin + for i := 1 to 3 do + if i # 2 then total := add(total, twice(i)); end; + end; +end main; + +end Listing.